mesh.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * ESPRESSIF MIT License
  3. *
  4. * Copyright (c) 2015 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
  5. *
  6. * Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case,
  7. * it is free of charge, to any person obtaining a copy of this software and associated
  8. * documentation files (the "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the Software is furnished
  11. * to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all copies or
  14. * substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. */
  24. #ifndef __LWIP_API_MESH_H__
  25. #define __LWIP_API_MESH_H__
  26. #include "ip_addr.h"
  27. #include "user_interface.h"
  28. #include "espconn.h"
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. #define ESP_MESH_GROUP_ID_LEN (6)
  33. typedef void (* espconn_mesh_callback)();
  34. typedef void (* espconn_mesh_scan_callback)(void *arg, int8_t status);
  35. enum mesh_type {
  36. MESH_CLOSE = 0,
  37. MESH_LOCAL,
  38. MESH_ONLINE,
  39. MESH_NONE = 0xFF
  40. };
  41. /** \defgroup Mesh_APIs Mesh APIs
  42. * @brief Mesh APIs
  43. *
  44. *
  45. *
  46. */
  47. /** @addtogroup Mesh_APIs
  48. * @{
  49. */
  50. enum mesh_status {
  51. MESH_DISABLE = 0,
  52. MESH_WIFI_CONN,
  53. MESH_NET_CONN,
  54. MESH_LOCAL_AVAIL,
  55. MESH_ONLINE_AVAIL
  56. };
  57. enum mesh_node_type {
  58. MESH_NODE_PARENT = 0,
  59. MESH_NODE_CHILD,
  60. MESH_NODE_ALL
  61. };
  62. struct mesh_scan_para_type {
  63. espconn_mesh_scan_callback usr_scan_cb; // scan done callback
  64. uint8_t grp_id[ESP_MESH_GROUP_ID_LEN]; // group id
  65. bool grp_set; // group set
  66. };
  67. /**
  68. * @brief Check whether the IP address is mesh local IP address or not.
  69. *
  70. * @attention 1. The range of mesh local IP address is 2.255.255.* ~ max_hop.255.255.*.
  71. * @attention 2. IP pointer should not be NULL. If the IP pointer is NULL, it will return false.
  72. *
  73. * @param struct ip_addr *ip : IP address
  74. *
  75. * @return true : the IP address is mesh local IP address
  76. * @return false : the IP address is not mesh local IP address
  77. */
  78. bool espconn_mesh_local_addr(struct ip_addr *ip);
  79. /**
  80. * @brief Get the information of router used by mesh network.
  81. *
  82. * @attention 1. The function should be called after mesh_enable_done
  83. *
  84. * @param struct station_config *router: router inforamtion
  85. *
  86. * @return true : succeed
  87. * @return false : fail
  88. */
  89. bool espconn_mesh_get_router(struct station_config *router);
  90. /**
  91. * @brief Set the information of router used by mesh network.
  92. *
  93. * @attention The function must be called before espconn_mesh_enable.
  94. *
  95. * @param struct station_config *router: router information.
  96. * user should initialize the ssid and password.
  97. *
  98. * @return true : succeed
  99. * @return false : fail
  100. */
  101. bool espconn_mesh_set_router(struct station_config *router);
  102. /**
  103. * @brief Set server setup by user.
  104. *
  105. * @attention If users wants to use themself server, they use the function.
  106. * but the function must be called before espconn_mesh_enable.
  107. * at the same time, users need to implement the server.
  108. *
  109. * @param struct ip_addr *ip : ip address of server.
  110. * @param uint16_t port : port used by server.
  111. *
  112. * @return true : succeed
  113. * @return false : fail
  114. */
  115. bool espconn_mesh_server_init(struct ip_addr *ip, uint16_t port);
  116. /**
  117. * @brief Get the information of mesh node.
  118. *
  119. * @param enum mesh_node_type typ : mesh node type.
  120. * @param uint8_t **info : the information will be saved in *info.
  121. * @param uint8_t *count : the node count in *info.
  122. *
  123. * @return true : succeed
  124. * @return false : fail
  125. */
  126. bool espconn_mesh_get_node_info(enum mesh_node_type type,
  127. uint8_t **info, uint8_t *count);
  128. /**
  129. * @brief Set WiFi cryption algrithm and password for mesh node.
  130. *
  131. * @attention The function must be called before espconn_mesh_enable.
  132. *
  133. * @param AUTH_MODE mode : cryption algrithm (WPA/WAP2/WPA_WPA2).
  134. * @param uint8_t *passwd : password of WiFi.
  135. * @param uint8_t passwd_len : length of password (8 <= passwd_len <= 64).
  136. *
  137. * @return true : succeed
  138. * @return false : fail
  139. */
  140. bool espconn_mesh_encrypt_init(AUTH_MODE mode, uint8_t *passwd, uint8_t passwd_len);
  141. /**
  142. * @brief Set prefix of SSID for mesh node.
  143. *
  144. * @attention The function must be called before espconn_mesh_enable.
  145. *
  146. * @param uint8_t *prefix : prefix of SSID.
  147. * @param uint8_t prefix_len : length of prefix (0 < passwd_len <= 22).
  148. *
  149. * @return true : succeed
  150. * @return false : fail
  151. */
  152. bool espconn_mesh_set_ssid_prefix(uint8_t *prefix, uint8_t prefix_len);
  153. /**
  154. * @brief Set max hop for mesh network.
  155. *
  156. * @attention The function must be called before espconn_mesh_enable.
  157. *
  158. * @param uint8_t max_hops : max hop of mesh network (1 <= max_hops < 10, 4 is recommended).
  159. *
  160. * @return true : succeed
  161. * @return false : fail
  162. */
  163. bool espconn_mesh_set_max_hops(uint8_t max_hops);
  164. /**
  165. * @brief Set group ID of mesh node.
  166. *
  167. * @attention The function must be called before espconn_mesh_enable.
  168. *
  169. * @param uint8_t *grp_id : group ID.
  170. * @param uint16_t gid_len: length of group ID, now gid_len = 6.
  171. *
  172. * @return true : succeed
  173. * @return false : fail
  174. */
  175. bool espconn_mesh_group_id_init(uint8_t *grp_id, uint16_t gid_len);
  176. /**
  177. * @brief Set the curent device type.
  178. *
  179. * @param uint8_t dev_type : device type of mesh node
  180. *
  181. * @return true : succeed
  182. * @return false : fail
  183. */
  184. bool espconn_mesh_set_dev_type(uint8_t dev_type);
  185. /**
  186. * @brief Get the curent device type.
  187. *
  188. * @param none
  189. *
  190. * @return device type
  191. */
  192. uint8_t espconn_mesh_get_dev_type();
  193. /**
  194. * @brief Try to establish mesh connection to server.
  195. *
  196. * @attention If espconn_mesh_connect fail, returns non-0 value, there is no connection, so it
  197. * won't enter any espconn callback.
  198. *
  199. * @param struct espconn *usr_esp : the network connection structure, the usr_esp to
  200. * listen to the connection
  201. *
  202. * @return 0 : succeed
  203. * @return Non-0 : error code
  204. * - ESPCONN_RTE - Routing Problem
  205. * - ESPCONN_MEM - Out of memory
  206. * - ESPCONN_ISCONN - Already connected
  207. * - ESPCONN_ARG - Illegal argument, can't find the corresponding connection
  208. * according to structure espconn
  209. */
  210. int8_t espconn_mesh_connect(struct espconn *usr_esp);
  211. /**
  212. * @brief Disconnect a mesh connection.
  213. *
  214. * @attention Do not call this API in any espconn callback. If needed, please use system
  215. * task to trigger espconn_mesh_disconnect.
  216. *
  217. * @param struct espconn *usr_esp : the network connection structure
  218. *
  219. * @return 0 : succeed
  220. * @return Non-0 : error code
  221. * - ESPCONN_ARG - illegal argument, can't find the corresponding TCP connection
  222. * according to structure espconn
  223. */
  224. int8_t espconn_mesh_disconnect(struct espconn *usr_esp);
  225. /**
  226. * @brief Get current mesh status.
  227. *
  228. * @param null
  229. *
  230. * @return the current mesh status, please refer to enum mesh_status.
  231. */
  232. int8_t espconn_mesh_get_status();
  233. /**
  234. * @brief Send data through mesh network.
  235. *
  236. * @attention Please call espconn_mesh_sent after espconn_sent_callback of the pre-packet.
  237. *
  238. * @param struct espconn *usr_esp : the network connection structure
  239. * @param uint8 *pdata : pointer of data
  240. * @param uint16 len : data length
  241. *
  242. * @return 0 : succeed
  243. * @return Non-0 : error code
  244. * - ESPCONN_MEM - out of memory
  245. * - ESPCONN_ARG - illegal argument, can't find the corresponding network transmission
  246. * according to structure espconn
  247. * - ESPCONN_MAXNUM - buffer of sending data is full
  248. * - ESPCONN_IF - send UDP data fail
  249. */
  250. int8_t espconn_mesh_sent(struct espconn *usr_esp, uint8 *pdata, uint16 len);
  251. /**
  252. * @brief Get max hop of mesh network.
  253. *
  254. * @param null.
  255. *
  256. * @return the current max hop of mesh
  257. */
  258. uint8_t espconn_mesh_get_max_hops();
  259. /**
  260. * @brief To enable mesh network.
  261. *
  262. * @attention 1. the function should be called in user_init.
  263. * @attention 2. if mesh node can not scan the mesh AP, it will be isolate node without trigger enable_cb.
  264. * user can use espconn_mesh_get_status to get current status of node.
  265. * @attention 3. if user try to enable online mesh, but node fails to establish mesh connection
  266. * the node will work with local mesh.
  267. *
  268. * @param espconn_mesh_callback enable_cb : callback function of mesh-enable
  269. * @param enum mesh_type type : type of mesh, local or online.
  270. *
  271. * @return null
  272. */
  273. void espconn_mesh_enable(espconn_mesh_callback enable_cb, enum mesh_type type);
  274. /**
  275. * @brief To disable mesh network.
  276. *
  277. * @attention When mesh network is disabed, the system will trigger disable_cb.
  278. *
  279. * @param espconn_mesh_callback disable_cb : callback function of mesh-disable
  280. * @param enum mesh_type type : type of mesh, local or online.
  281. *
  282. * @return null
  283. */
  284. void espconn_mesh_disable(espconn_mesh_callback disable_cb);
  285. /**
  286. * @brief To print version of mesh.
  287. *
  288. * @param null
  289. *
  290. * @return null
  291. */
  292. void espconn_mesh_print_ver();
  293. /**
  294. * @brief To get AP around node.
  295. *
  296. * @attention User can get normal AP or mesh AP using the function.
  297. * If user plans to get normal AP, he/she needs to clear grp_set flag in para.
  298. * If user plans to get mesh AP, he/she needs to set grp_set and grp_id;
  299. *
  300. * @param struct mesh_scan_para_type *para : callback function of mesh-disable
  301. *
  302. * @return null
  303. */
  304. void espconn_mesh_scan(struct mesh_scan_para_type *para);
  305. /**
  306. * @}
  307. */
  308. #ifdef __cplusplus
  309. }
  310. #endif
  311. #endif