ESP8266_NONOS_MESH_API  V1.0.0
mesh.h
1 /*
2  * ESPRSSIF MIT License
3  *
4  * Copyright (c) 2016 <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 
25 #ifndef __LWIP_API_MESH_H__
26 #define __LWIP_API_MESH_H__
27 
28 #if 1//def ESP_MESH_SUPPORT
29 #include "ip_addr.h"
30 #include "user_interface.h"
31 #include "espconn.h"
32 
33 const struct eth_addr mesh_bcast_addr = {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
34 const struct eth_addr mesh_mcast_addr = {{0x01, 0x00, 0x5E, 0x00, 0x00, 0x00}};
35 
36 #define ESP_MESH_VER (0)
37 #define ESP_MESH_GROUP_ID_LEN (6)
38 #define ESP_MESH_ADDR_LEN (6)
39 #define ESP_MESH_OPTION_MAX_LEN (255)
40 #define ESP_MESH_PKT_LEN_MAX (1300)
41 #define ESP_MESH_FRAG_ID_MASK (0xFFFF)
42 #define ESP_MESH_FRAG_IDX_MASK (0x3FFF)
43 #define ESP_MESH_OT_LEN_LEN (sizeof(uint16_t))
44 #define ESP_MESH_HLEN (sizeof(struct mesh_header_format))
45 #define ESP_MESH_OPTION_HLEN (sizeof(struct mesh_header_option_format))
46 #define ESP_MESH_OP_MAX_PER_PKT ((ESP_MESH_PKT_LEN_MAX - ESP_MESH_HLEN) / ESP_MESH_OPTION_MAX_LEN)
47 #define ESP_MESH_DEV_MAX_PER_OP ((ESP_MESH_OPTION_MAX_LEN - ESP_MESH_OPTION_HLEN) / ESP_MESH_ADDR_LEN)
48 #define ESP_MESH_DEV_MAX_PER_PKT (ESP_MESH_OP_MAX_PER_PKT * ESP_MESH_DEV_MAX_PER_OP)
49 #define ESP_MESH_OPTION_CR_LEN (ESP_MESH_OPTION_HLEN)
50 #define ESP_MESH_BCAST_ADDR (mesh_bcast_addr.addr)
51 #define ESP_MESH_MCAST_ADDR (mesh_mcast_addr.addr)
52 
53 typedef void (* espconn_mesh_callback)(int8_t result);
54 typedef void (* espconn_mesh_scan_callback)(void *arg, int8_t status);
55 typedef void (* espconn_mesh_usr_callback)(void *arg);
56 
57 enum mesh_op_result {
58  MESH_ONLINE_SUC = 0,
59  MESH_LOCAL_SUC = 1,
60  MESH_DISABLE_SUC = 2,
61  MESH_OP_FAILURE = -1
62 };
63 enum mesh_type {
64  MESH_CLOSE = 0,
65  MESH_LOCAL,
66  MESH_ONLINE,
67  MESH_NONE = 0xFF
68 };
69 
70 enum mesh_status {
71  MESH_DISABLE = 0,
72  MESH_WIFI_CONN,
73  MESH_NET_CONN,
74  MESH_LOCAL_AVAIL,
75  MESH_ONLINE_AVAIL
76 };
77 
78 enum mesh_node_type {
79  MESH_NODE_PARENT = 0,
80  MESH_NODE_CHILD,
81  MESH_NODE_ALL
82 };
83 
85  uint16_t sub_count;
86  uint8_t mac[ESP_MESH_ADDR_LEN];
87 } __packed;
88 
89 /**************************************************************************
90  * mesh header format:
91  * |0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7|
92  * __________________________________________________________________
93  * |ver|oe| flags | proto | len |
94  * ------------------------------------------------------------------
95  * | dst_addr _______________________________|
96  * |_________________________________| | |
97  * | src_addr |
98  * |----------------------------------------------------------------|
99  * |__________ot_len_________________| option_list |
100  * | |
101  * |-----------------------------------------------------------------
102  *
103  * format of flags:
104  * | 0 1 2 3 4|
105  * -------------
106  * |cp|cr| rsv |
107  * -------------
108  *
109  * format of proto:
110  * |0 1 2 3 4 5 6 7|
111  * -------------------
112  * |d|p2p| protocol |
113  * -------------------
114  *
115  * format of option element:
116  * |0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 ......|
117  * ------------------------------------------------------------------------
118  * | otype | olen | ovalue |
119  * ------------------------------------------------------------------------
120  **************************************************************************/
121 
122 
124  uint8_t otype;
125  uint8_t olen;
126  uint8_t ovalue[0];
127 } __packed;
128 
130  uint16_t ot_len;
132 } __packed;
133 
135  uint16_t id;
136  struct {
137  uint16_t resv:1;
138  uint16_t mf:1;
139  uint16_t idx:14;
140  } offset;
141 } __packed;
142 
144  uint8_t ver:2; //
145  uint8_t oe: 1; // option exist flag
146  uint8_t cp: 1;
147  uint8_t cr: 1;
148  uint8_t rsv:3;
149  struct {
150  uint8_t d: 1;
151  uint8_t p2p:1;
152  uint8_t protocol:6;
153  } proto;
154  uint16_t len;
155  uint8_t dst_addr[ESP_MESH_ADDR_LEN];
156  uint8_t src_addr[ESP_MESH_ADDR_LEN];
158 } __packed;
159 
161  espconn_mesh_scan_callback usr_scan_cb;
162  uint8_t grp_id[ESP_MESH_GROUP_ID_LEN];
163  bool grp_set;
164 };
165 
166 enum mesh_option_type {
167  M_O_CONGEST_REQ = 0,
168  M_O_CONGEST_RESP,
169  M_O_ROUTER_SPREAD,
170  M_O_ROUTE_ADD,
171  M_O_ROUTE_DEL,
172  M_O_TOPO_REQ,
173  M_O_TOPO_RESP,
174  M_O_MCAST_GRP,
175  M_O_MESH_FRAG,
176  M_O_USR_FRAG,
177  M_O_USR_OPTION,
178 };
179 
180 enum mesh_usr_proto_type {
181  M_PROTO_NONE = 0,
182  M_PROTO_HTTP,
183  M_PROTO_JSON,
184  M_PROTO_MQTT,
185  M_PROTO_BIN,
186 };
187 
188 enum mesh_pkt_direct {
189  MESH_ROUTE_DOWNLOADS = 0,
190  MESH_ROUTE_UPWARDS,
191 };
192 
193 enum espnow_dbg_data_type {
194  M_FREQ_CAL = 0,
195  WIFI_STATUS,
196  FREE_HEAP_SIZE,
197  CHILD_NUM,
198  SUB_DEV_NUM,
199  MESH_STATUS,
200  MESH_VERSION,
201  MESH_ROUTER,
202  MESH_LAYER,
203  MESH_ASSOC,
204  MESH_CHANNEL,
205 };
206 
244 void * espconn_mesh_create_packet(uint8_t *dst_addr, uint8_t *src_addr, bool p2p,
245  bool piggyback_cr, enum mesh_usr_proto_type proto,
246  uint16_t data_len, bool option, uint16_t ot_len,
247  bool frag, enum mesh_option_type frag_type,
248  bool mf, uint16_t frag_idx, uint16_t frag_id);
249 
260 void * espconn_mesh_create_option(uint8_t otype, uint8_t *ovalue, uint8_t val_len);
261 
272  struct mesh_header_option_format *option);
273 
286  enum mesh_option_type otype, uint16_t oidx,
287  struct mesh_header_option_format **option);
288 
300  uint8_t **usr_data, uint16_t *data_len);
301 
313  uint8_t *usr_data, uint16_t data_len);
314 
324 bool espconn_mesh_get_src_addr(struct mesh_header_format *head, uint8_t **src_addr);
325 
335 bool espconn_mesh_get_dst_addr(struct mesh_header_format *head, uint8_t **dst_addr);
336 
346 bool espconn_mesh_set_src_addr(struct mesh_header_format *head, uint8_t *src_addr);
347 
357 bool espconn_mesh_set_dst_addr(struct mesh_header_format *head, uint8_t *dst_addr);
358 
369  enum mesh_usr_proto_type *proto);
370 
381  enum mesh_usr_proto_type proto);
382 
394 bool espconn_mesh_local_addr(struct ip_addr *ip);
395 
412 bool espconn_mesh_get_node_info(enum mesh_node_type type,
413  uint8_t **info, uint16_t *count);
414 
426 bool espconn_mesh_get_router(struct station_config *router);
427 
438 bool espconn_mesh_set_router(struct station_config *router);
439 
452 bool espconn_mesh_encrypt_init(AUTH_MODE mode, uint8_t *passwd, uint8_t pw_len);
453 
466 bool espconn_mesh_group_id_init(uint8_t *grp_id, uint16_t gid_len);
467 
477 bool espconn_mesh_regist_conn_ready_cb(espconn_mesh_usr_callback cb);
478 
489 
502 
514 
527 
542 
561 
576 
592 
611 
620 
632 
642 
655 
666 
675 
684 
693 
702 
715 
728 
735 
void * espconn_mesh_create_option(uint8_t otype, uint8_t *ovalue, uint8_t val_len)
The function is used to create mesh option.
uint8_t otype
Definition: mesh.h:124
bool espconn_mesh_add_option(struct mesh_header_format *head, struct mesh_header_option_format *option)
The function is used to add mesh option in mesh packet.
uint8_t src_addr[ESP_MESH_ADDR_LEN]
Definition: mesh.h:156
bool espconn_mesh_regist_conn_ready_cb(espconn_mesh_usr_callback cb)
The function is used to register user callback. If TCP connection with parent node is ready...
uint8_t ovalue[0]
Definition: mesh.h:126
Definition: mesh.h:143
Definition: mesh.h:84
uint8_t dst_addr[ESP_MESH_ADDR_LEN]
Definition: mesh.h:155
bool espconn_mesh_get_router(struct station_config *router)
The function is used to get router AP information used by mesh node.
uint16_t len
Definition: mesh.h:154
uint16_t id
Definition: mesh.h:135
espconn_mesh_scan_callback usr_scan_cb
Definition: mesh.h:161
uint8_t mac[ESP_MESH_ADDR_LEN]
Definition: mesh.h:86
bool espconn_mesh_group_id_init(uint8_t *grp_id, uint16_t gid_len)
The function is used to init group id for mesh node.
void * espconn_mesh_create_packet(uint8_t *dst_addr, uint8_t *src_addr, bool p2p, bool piggyback_cr, enum mesh_usr_proto_type proto, uint16_t data_len, bool option, uint16_t ot_len, bool frag, enum mesh_option_type frag_type, bool mf, uint16_t frag_idx, uint16_t frag_id)
The function is used to create mesh packet.
bool espconn_mesh_encrypt_init(AUTH_MODE mode, uint8_t *passwd, uint8_t pw_len)
The function is used to init encrypt algorithm and password for mesh AP.
bool espconn_mesh_set_usr_data(struct mesh_header_format *head, uint8_t *usr_data, uint16_t data_len)
The function is used to set user data in mesh packet..
uint16_t ot_len
Definition: mesh.h:130
bool espconn_mesh_get_usr_data(struct mesh_header_format *head, uint8_t **usr_data, uint16_t *data_len)
The function is used to get user data in mesh packet..
bool espconn_mesh_local_addr(struct ip_addr *ip)
Check whether the IP address is mesh local IP address or not.
uint8_t oe
Definition: mesh.h:145
bool espconn_mesh_set_dst_addr(struct mesh_header_format *head, uint8_t *dst_addr)
The function is used to set destination address of mesh packet.
uint8_t ver
Definition: mesh.h:144
bool espconn_mesh_set_usr_data_proto(struct mesh_header_format *head, enum mesh_usr_proto_type proto)
The function is used to set protocol used by user data in mesh packet.
uint16_t mf
Definition: mesh.h:138
bool espconn_mesh_get_usr_data_proto(struct mesh_header_format *head, enum mesh_usr_proto_type *proto)
The function is used to get protocol used by user data in mesh packet.
uint16_t resv
Definition: mesh.h:137
uint16_t idx
Definition: mesh.h:139
uint8_t grp_id[ESP_MESH_GROUP_ID_LEN]
Definition: mesh.h:162
uint8_t cp
Definition: mesh.h:146
uint8_t protocol
Definition: mesh.h:152
struct mesh_header_option_header_type option[0]
Definition: mesh.h:157
bool espconn_mesh_get_src_addr(struct mesh_header_format *head, uint8_t **src_addr)
The function is used to get source address of mesh packet.
bool espconn_mesh_get_option(struct mesh_header_format *head, enum mesh_option_type otype, uint16_t oidx, struct mesh_header_option_format **option)
The function is used to get mesh option in mesh packet..
uint8_t p2p
Definition: mesh.h:151
Definition: mesh.h:160
uint16_t sub_count
Definition: mesh.h:85
uint8_t d
Definition: mesh.h:150
Definition: mesh.h:134
bool espconn_mesh_set_src_addr(struct mesh_header_format *head, uint8_t *src_addr)
The function is used to set source address of mesh packet.
bool espconn_mesh_get_dst_addr(struct mesh_header_format *head, uint8_t **dst_addr)
The function is used to get destination address of mesh packet.
Definition: mesh.h:129
uint8_t rsv
Definition: mesh.h:148
bool espconn_mesh_get_node_info(enum mesh_node_type type, uint8_t **info, uint16_t *count)
The function is used to get the information of mesh node..
uint8_t olen
Definition: mesh.h:125
bool espconn_mesh_set_router(struct station_config *router)
The function is used to set router AP information for mesh node.
uint8_t cr
Definition: mesh.h:147
struct mesh_header_option_format olist[0]
Definition: mesh.h:131
Definition: mesh.h:123
bool grp_set
Definition: mesh.h:163