diff --git a/main/wifimeshnet.c b/main/wifimeshnet.c index 23aa7c4..86636f7 100644 --- a/main/wifimeshnet.c +++ b/main/wifimeshnet.c @@ -9,7 +9,8 @@ #include "mesh_netif.h" #include "driver/gpio.h" #include "freertos/semphr.h" - +#include "init.h" +#include "string.h" #if CONFIG_WIFI_MESH_NETWORK == 1 @@ -31,7 +32,7 @@ static mesh_addr_t mesh_parent_addr; static int mesh_layer = -1; static esp_ip4_addr_t s_current_ip; -static wifimeshnet_callback_t wifimeshnet_callback={NULL,NULL,NULL}; +static wifimeshnet_callback_t wifimeshnet_callback= {NULL,NULL,NULL}; static void recv_cb(mesh_addr_t *from, mesh_data_t *data) { @@ -44,207 +45,208 @@ static void recv_cb(mesh_addr_t *from, mesh_data_t *data) } static void mesh_event_handler(void *arg, esp_event_base_t event_base, - int32_t event_id, void *event_data) + int32_t event_id, void *event_data) { if(wifimeshnet_callback.mesh_event_handler!=NULL) { - wifimeshnet_callback.mesh_event_handler(arg,event_base,event_id,event_data); + wifimeshnet_callback.mesh_event_handler(arg,event_base,event_id,event_data); }; mesh_addr_t id = {0,}; static uint8_t last_layer = 0; - switch (event_id) { + switch (event_id) + { case MESH_EVENT_STARTED: { esp_mesh_get_id(&id); ESP_LOGI(MESH_TAG, "ID:"MACSTR"", MAC2STR(id.addr)); - mesh_layer = esp_mesh_get_layer(); - } - break; +mesh_layer = esp_mesh_get_layer(); +} +break; case MESH_EVENT_STOPPED: - { - ESP_LOGI(MESH_TAG, ""); - mesh_layer = esp_mesh_get_layer(); - } - break; +{ +ESP_LOGI(MESH_TAG, ""); +mesh_layer = esp_mesh_get_layer(); +} +break; case MESH_EVENT_CHILD_CONNECTED: - { - mesh_event_child_connected_t *child_connected = (mesh_event_child_connected_t *)event_data; - ESP_LOGI(MESH_TAG, "aid:%d, "MACSTR"", - child_connected->aid, - MAC2STR(child_connected->mac)); - } - break; +{ +mesh_event_child_connected_t *child_connected = (mesh_event_child_connected_t *)event_data; +ESP_LOGI(MESH_TAG, "aid:%d, "MACSTR"", + child_connected->aid, + MAC2STR(child_connected->mac)); +} +break; case MESH_EVENT_CHILD_DISCONNECTED: - { - mesh_event_child_disconnected_t *child_disconnected = (mesh_event_child_disconnected_t *)event_data; - ESP_LOGI(MESH_TAG, "aid:%d, "MACSTR"", - child_disconnected->aid, - MAC2STR(child_disconnected->mac)); - } - break; +{ +mesh_event_child_disconnected_t *child_disconnected = (mesh_event_child_disconnected_t *)event_data; +ESP_LOGI(MESH_TAG, "aid:%d, "MACSTR"", + child_disconnected->aid, + MAC2STR(child_disconnected->mac)); +} +break; case MESH_EVENT_ROUTING_TABLE_ADD: - { - mesh_event_routing_table_change_t *routing_table = (mesh_event_routing_table_change_t *)event_data; - ESP_LOGW(MESH_TAG, "add %d, new:%d", - routing_table->rt_size_change, - routing_table->rt_size_new); - } - break; +{ +mesh_event_routing_table_change_t *routing_table = (mesh_event_routing_table_change_t *)event_data; +ESP_LOGW(MESH_TAG, "add %d, new:%d", + routing_table->rt_size_change, + routing_table->rt_size_new); +} +break; case MESH_EVENT_ROUTING_TABLE_REMOVE: - { - mesh_event_routing_table_change_t *routing_table = (mesh_event_routing_table_change_t *)event_data; - ESP_LOGW(MESH_TAG, "remove %d, new:%d", - routing_table->rt_size_change, - routing_table->rt_size_new); - } - break; +{ +mesh_event_routing_table_change_t *routing_table = (mesh_event_routing_table_change_t *)event_data; +ESP_LOGW(MESH_TAG, "remove %d, new:%d", + routing_table->rt_size_change, + routing_table->rt_size_new); +} +break; case MESH_EVENT_NO_PARENT_FOUND: - { - mesh_event_no_parent_found_t *no_parent = (mesh_event_no_parent_found_t *)event_data; - ESP_LOGI(MESH_TAG, "scan times:%d", - no_parent->scan_times); - } +{ +mesh_event_no_parent_found_t *no_parent = (mesh_event_no_parent_found_t *)event_data; +ESP_LOGI(MESH_TAG, "scan times:%d", + no_parent->scan_times); +} /* TODO handler for the failure */ - break; +break; case MESH_EVENT_PARENT_CONNECTED: - { - mesh_event_connected_t *connected = (mesh_event_connected_t *)event_data; - esp_mesh_get_id(&id); - mesh_layer = connected->self_layer; - memcpy(&mesh_parent_addr.addr, connected->connected.bssid, 6); - ESP_LOGI(MESH_TAG, - "layer:%d-->%d, parent:"MACSTR"%s, ID:"MACSTR"", - last_layer, mesh_layer, MAC2STR(mesh_parent_addr.addr), - esp_mesh_is_root() ? "" : - (mesh_layer == 2) ? "" : "", MAC2STR(id.addr)); - last_layer = mesh_layer; - mesh_netifs_start(esp_mesh_is_root()); - } - break; +{ +mesh_event_connected_t *connected = (mesh_event_connected_t *)event_data; +esp_mesh_get_id(&id); +mesh_layer = connected->self_layer; +memcpy(&mesh_parent_addr.addr, connected->connected.bssid, 6); +ESP_LOGI(MESH_TAG, + "layer:%d-->%d, parent:"MACSTR"%s, ID:"MACSTR"", + last_layer, mesh_layer, MAC2STR(mesh_parent_addr.addr), + esp_mesh_is_root() ? "" : + (mesh_layer == 2) ? "" : "", MAC2STR(id.addr)); +last_layer = mesh_layer; +mesh_netifs_start(esp_mesh_is_root()); +} +break; case MESH_EVENT_PARENT_DISCONNECTED: - { - mesh_event_disconnected_t *disconnected = (mesh_event_disconnected_t *)event_data; - ESP_LOGI(MESH_TAG, - "reason:%d", - disconnected->reason); - mesh_layer = esp_mesh_get_layer(); - mesh_netifs_stop(); - } - break; +{ +mesh_event_disconnected_t *disconnected = (mesh_event_disconnected_t *)event_data; +ESP_LOGI(MESH_TAG, + "reason:%d", + disconnected->reason); +mesh_layer = esp_mesh_get_layer(); +mesh_netifs_stop(); +} +break; case MESH_EVENT_LAYER_CHANGE: - { - mesh_event_layer_change_t *layer_change = (mesh_event_layer_change_t *)event_data; - mesh_layer = layer_change->new_layer; - ESP_LOGI(MESH_TAG, "layer:%d-->%d%s", - last_layer, mesh_layer, - esp_mesh_is_root() ? "" : - (mesh_layer == 2) ? "" : ""); - last_layer = mesh_layer; - } - break; +{ +mesh_event_layer_change_t *layer_change = (mesh_event_layer_change_t *)event_data; +mesh_layer = layer_change->new_layer; +ESP_LOGI(MESH_TAG, "layer:%d-->%d%s", + last_layer, mesh_layer, + esp_mesh_is_root() ? "" : + (mesh_layer == 2) ? "" : ""); +last_layer = mesh_layer; +} +break; case MESH_EVENT_ROOT_ADDRESS: - { - mesh_event_root_address_t *root_addr = (mesh_event_root_address_t *)event_data; - ESP_LOGI(MESH_TAG, "root address:"MACSTR"", - MAC2STR(root_addr->addr)); - } - break; +{ +mesh_event_root_address_t *root_addr = (mesh_event_root_address_t *)event_data; +ESP_LOGI(MESH_TAG, "root address:"MACSTR"", + MAC2STR(root_addr->addr)); +} +break; case MESH_EVENT_VOTE_STARTED: - { - mesh_event_vote_started_t *vote_started = (mesh_event_vote_started_t *)event_data; - ESP_LOGI(MESH_TAG, - "attempts:%d, reason:%d, rc_addr:"MACSTR"", - vote_started->attempts, - vote_started->reason, - MAC2STR(vote_started->rc_addr.addr)); - } - break; +{ +mesh_event_vote_started_t *vote_started = (mesh_event_vote_started_t *)event_data; +ESP_LOGI(MESH_TAG, + "attempts:%d, reason:%d, rc_addr:"MACSTR"", + vote_started->attempts, + vote_started->reason, + MAC2STR(vote_started->rc_addr.addr)); +} +break; case MESH_EVENT_VOTE_STOPPED: - { - ESP_LOGI(MESH_TAG, ""); - break; - } +{ +ESP_LOGI(MESH_TAG, ""); +break; +} case MESH_EVENT_ROOT_SWITCH_REQ: - { - mesh_event_root_switch_req_t *switch_req = (mesh_event_root_switch_req_t *)event_data; - ESP_LOGI(MESH_TAG, - "reason:%d, rc_addr:"MACSTR"", - switch_req->reason, - MAC2STR( switch_req->rc_addr.addr)); - } - break; +{ +mesh_event_root_switch_req_t *switch_req = (mesh_event_root_switch_req_t *)event_data; +ESP_LOGI(MESH_TAG, + "reason:%d, rc_addr:"MACSTR"", + switch_req->reason, + MAC2STR( switch_req->rc_addr.addr)); +} +break; case MESH_EVENT_ROOT_SWITCH_ACK: - { - /* new root */ - mesh_layer = esp_mesh_get_layer(); - esp_mesh_get_parent_bssid(&mesh_parent_addr); - ESP_LOGI(MESH_TAG, "layer:%d, parent:"MACSTR"", mesh_layer, MAC2STR(mesh_parent_addr.addr)); - } - break; +{ +/* new root */ +mesh_layer = esp_mesh_get_layer(); +esp_mesh_get_parent_bssid(&mesh_parent_addr); +ESP_LOGI(MESH_TAG, "layer:%d, parent:"MACSTR"", mesh_layer, MAC2STR(mesh_parent_addr.addr)); +} +break; case MESH_EVENT_TODS_STATE: - { - mesh_event_toDS_state_t *toDs_state = (mesh_event_toDS_state_t *)event_data; - ESP_LOGI(MESH_TAG, "state:%d", *toDs_state); - } - break; +{ +mesh_event_toDS_state_t *toDs_state = (mesh_event_toDS_state_t *)event_data; +ESP_LOGI(MESH_TAG, "state:%d", *toDs_state); +} +break; case MESH_EVENT_ROOT_FIXED: - { - mesh_event_root_fixed_t *root_fixed = (mesh_event_root_fixed_t *)event_data; - ESP_LOGI(MESH_TAG, "%s", - root_fixed->is_fixed ? "fixed" : "not fixed"); - } - break; +{ +mesh_event_root_fixed_t *root_fixed = (mesh_event_root_fixed_t *)event_data; +ESP_LOGI(MESH_TAG, "%s", + root_fixed->is_fixed ? "fixed" : "not fixed"); +} +break; case MESH_EVENT_ROOT_ASKED_YIELD: - { - mesh_event_root_conflict_t *root_conflict = (mesh_event_root_conflict_t *)event_data; - ESP_LOGI(MESH_TAG, - ""MACSTR", rssi:%d, capacity:%d", - MAC2STR(root_conflict->addr), - root_conflict->rssi, - root_conflict->capacity); - } - break; +{ +mesh_event_root_conflict_t *root_conflict = (mesh_event_root_conflict_t *)event_data; +ESP_LOGI(MESH_TAG, + ""MACSTR", rssi:%d, capacity:%d", + MAC2STR(root_conflict->addr), + root_conflict->rssi, + root_conflict->capacity); +} +break; case MESH_EVENT_CHANNEL_SWITCH: - { - mesh_event_channel_switch_t *channel_switch = (mesh_event_channel_switch_t *)event_data; - ESP_LOGI(MESH_TAG, "new channel:%d", channel_switch->channel); - } - break; +{ +mesh_event_channel_switch_t *channel_switch = (mesh_event_channel_switch_t *)event_data; +ESP_LOGI(MESH_TAG, "new channel:%d", channel_switch->channel); +} +break; case MESH_EVENT_SCAN_DONE: - { - mesh_event_scan_done_t *scan_done = (mesh_event_scan_done_t *)event_data; - ESP_LOGI(MESH_TAG, "number:%d", - scan_done->number); - } - break; +{ +mesh_event_scan_done_t *scan_done = (mesh_event_scan_done_t *)event_data; +ESP_LOGI(MESH_TAG, "number:%d", + scan_done->number); +} +break; case MESH_EVENT_NETWORK_STATE: - { - mesh_event_network_state_t *network_state = (mesh_event_network_state_t *)event_data; - ESP_LOGI(MESH_TAG, "is_rootless:%d", - network_state->is_rootless); - } - break; +{ +mesh_event_network_state_t *network_state = (mesh_event_network_state_t *)event_data; +ESP_LOGI(MESH_TAG, "is_rootless:%d", + network_state->is_rootless); +} +break; case MESH_EVENT_STOP_RECONNECTION: - { - ESP_LOGI(MESH_TAG, ""); - } - break; +{ +ESP_LOGI(MESH_TAG, ""); +} +break; case MESH_EVENT_FIND_NETWORK: - { - mesh_event_find_network_t *find_network = (mesh_event_find_network_t *)event_data; - ESP_LOGI(MESH_TAG, "new channel:%d, router BSSID:"MACSTR"", - find_network->channel, MAC2STR(find_network->router_bssid)); - } - break; +{ +mesh_event_find_network_t *find_network = (mesh_event_find_network_t *)event_data; +ESP_LOGI(MESH_TAG, "new channel:%d, router BSSID:"MACSTR"", + find_network->channel, MAC2STR(find_network->router_bssid)); +} +break; case MESH_EVENT_ROUTER_SWITCH: - { - mesh_event_router_switch_t *router_switch = (mesh_event_router_switch_t *)event_data; - ESP_LOGI(MESH_TAG, "new router:%s, channel:%d, "MACSTR"", - router_switch->ssid, router_switch->channel, MAC2STR(router_switch->bssid)); - } - break; +{ +mesh_event_router_switch_t *router_switch = (mesh_event_router_switch_t *)event_data; +ESP_LOGI(MESH_TAG, "new router:%s, channel:%d, "MACSTR"", + router_switch->ssid, router_switch->channel, MAC2STR(router_switch->bssid)); +} +break; default: ESP_LOGI(MESH_TAG, "unknown id:%d", event_id); break; @@ -307,6 +309,88 @@ void wifimeshnet_init(wifimeshnet_callback_t callback) cfg.mesh_ap.max_connection = CONFIG_MESH_AP_CONNECTIONS; memcpy((uint8_t *) &cfg.mesh_ap.password, CONFIG_MESH_AP_PASSWD, strlen(CONFIG_MESH_AP_PASSWD)); + + {//加载配置 + cJSON *obj=system_config_get_item("wifimeshnet"); + if(obj==NULL) + { + wifimeshnet_set_config(NULL); + } + else + { + if(cJSON_HasObjectItem(obj,"mesh_id")) + { + cJSON *item=cJSON_GetObjectItem(obj,"mesh_id"); + if(cJSON_IsArray(item)) + { + for(size_t i=0;i<6;i++) + { + if(imesh_id, MESH_ID, 6); + config->mesh_channel=CONFIG_MESH_CHANNEL; + config->mesh_ap_max_connections=CONFIG_MESH_AP_CONNECTIONS; + config->router_ssid_len=strlen(CONFIG_MESH_ROUTER_SSID); + memcpy((uint8_t *) config->router_ssid, CONFIG_MESH_ROUTER_SSID,config->router_ssid_len); + memcpy((uint8_t *) config->router_password, CONFIG_MESH_ROUTER_PASSWD,strlen(CONFIG_MESH_ROUTER_PASSWD)); + memcpy((uint8_t *) config->mesh_ap_password, CONFIG_MESH_AP_PASSWD,strlen(CONFIG_MESH_AP_PASSWD)); + + } + + {//检查参数 + if(config->router_ssid_len==0) + { + config->router_ssid_len=strlen((char *)config->router_ssid); + } + + if(config->mesh_ap_max_connections==0) + { + config->mesh_ap_max_connections=10; + } + } + + { + cJSON *obj=system_config_get_item("wifimeshnet"); + if(obj==NULL) + { + obj=cJSON_CreateObject(); + } + + {//删除原有数据项 + if(cJSON_HasObjectItem(obj,"mesh_id")) + { + cJSON_DeleteItemFromObject(obj,"mesh_id"); + } + if(cJSON_HasObjectItem(obj,"mesh_ap_password")) + { + cJSON_DeleteItemFromObject(obj,"mesh_ap_password"); + } + if(cJSON_HasObjectItem(obj,"mesh_ap_max_connections")) + { + cJSON_DeleteItemFromObject(obj,"mesh_ap_max_connections"); + } + if(cJSON_HasObjectItem(obj,"mesh_channel")) + { + cJSON_DeleteItemFromObject(obj,"mesh_channel"); + } + if(cJSON_HasObjectItem(obj,"router_ssid")) + { + cJSON_DeleteItemFromObject(obj,"router_ssid"); + } + if(cJSON_HasObjectItem(obj,"router_password")) + { + cJSON_DeleteItemFromObject(obj,"router_password"); + } + if(cJSON_HasObjectItem(obj,"router_ssid_len")) + { + cJSON_DeleteItemFromObject(obj,"router_ssid_len"); + } + } + + {//添加数据项 + + {//mesh_id + + int mesh_id_int[6]={0}; + for(size_t i=0;i<6;i++) + { + mesh_id_int[i]=config->mesh_id[i]; + } + + cJSON *mesh_id=cJSON_CreateIntArray(mesh_id_int,6); + cJSON_AddItemToObject(obj,"mesh_id",mesh_id); + } + + {//mesh_ap_password + cJSON *mesh_ap_password=cJSON_CreateString((char *)config->mesh_ap_password); + cJSON_AddItemToObject(obj,"mesh_ap_password",mesh_ap_password); + } + {//mesh_ap_max_connections + cJSON *mesh_ap_max_connections=cJSON_CreateNumber(config->mesh_ap_max_connections); + cJSON_AddItemToObject(obj,"mesh_ap_max_connections",mesh_ap_max_connections); + } + {//mesh_channel + cJSON *mesh_channel=cJSON_CreateNumber(config->mesh_channel); + cJSON_AddItemToObject(obj,"mesh_channel",mesh_channel); + } + + {//router_ssid + cJSON *router_ssid=cJSON_CreateString((char *)config->router_ssid); + cJSON_AddItemToObject(obj,"router_ssid",router_ssid); + } + + {//router_password + cJSON *router_password=cJSON_CreateString((char *)config->router_password); + cJSON_AddItemToObject(obj,"router_password",router_password); + } + + {//router_ssid_len + cJSON *router_ssid_len=cJSON_CreateNumber(config->router_ssid_len); + cJSON_AddItemToObject(obj,"router_ssid_len",router_ssid_len); + } + + + } + + //保存数据 + system_config_put_item(obj,"wifimeshnet"); + system_config_save(); + + cJSON_Delete(obj); + } + + if(cfg==NULL) + { + free(config); + } +} + #endif // CONFIG_WIFI_MESH_NETWORK diff --git a/main/wifimeshnet.h b/main/wifimeshnet.h index f381f06..dcf125a 100644 --- a/main/wifimeshnet.h +++ b/main/wifimeshnet.h @@ -36,6 +36,19 @@ typedef struct //初始化wifimeshnet void wifimeshnet_init(wifimeshnet_callback_t callback); +typedef struct +{ + uint8_t mesh_id[6]; + uint8_t mesh_ap_password[64]; + size_t mesh_ap_max_connections; + int mesh_channel; + uint8_t router_ssid[32]; + size_t router_ssid_len; + uint8_t router_password[64]; +} wifimeshnet_config_t; +//设置配置参数 +void wifimeshnet_set_config(wifimeshnet_config_t *cfg); + #ifdef __cplusplus } #endif // __cplusplus