int mico_start_alink(void) { const uint8_t *cid; uint8_t cid_len = 0; mico_Context_t *context = mico_system_context_get(); application_config_t *app_config = context->user_config_data; int ret = -1; cid = mico_generate_cid( &cid_len ); memset(&alink_config, 0, sizeof(struct alink_config)); struct device_info* dev = (struct device_info*) malloc(sizeof(struct device_info)); memset(dev, 0, sizeof(struct device_info)); strcpy(dev->key, ALINK_KEY); strcpy(dev->secret, ALINK_SECRET); sprintf(dev->name, "%s(%c%c%c%c)",DEV_NAME, context->micoStatus.mac[12], context->micoStatus.mac[13], context->micoStatus.mac[15], context->micoStatus.mac[16]); strcpy(dev->model, DEV_MODEL); strcpy(dev->manufacturer, DEV_MANUFACTURE); strcpy(dev->type, DEV_TYPE); strcpy(dev->category, DEV_CATEGORY); strcpy(dev->sn, DEV_SN); snprintf(dev->version, STR_NAME_LEN, "%d", FIRMWARE_REVISION_NUM); memcpy(dev->mac, mico_get_mac_addr(), 18); memcpy(dev->cid, cid, Min(cid_len, STR_CID_LEN) ); custom_log("WSF", "dev->mac %s", dev->mac); custom_log("WSF", "dev->cid %s", dev->cid); #ifdef ALINK_RAWDATA custom_log("WSF", "ALINK RAWDATA !!!"); #else dev->dev_callback[ACB_GET_DEVICE_STATUS] = device_status_get; dev->dev_callback[ACB_SET_DEVICE_STATUS] = device_command_execute; #endif dev->sys_callback[ALINK_FUNC_SERVER_STATUS] = alink_network_callback; alink_set_callback(ALINK_FUNC_AVAILABLE_MEMORY, get_available_memory); #ifdef DEBUG alink_set_loglevel(ALINK_LL_NONE); #else alink_set_loglevel(ALINK_LL_NONE); #endif memset(&alink_config, 0, sizeof(struct alink_config)); #ifdef SANDBOX sandbox_setting(); #endif if( app_config->resetflag == 1) { alink_config.factory_reset = 1; } alink_set_config(&alink_config); #ifdef ALINK_RAWDATA alink_start_rawdata(dev, alink_uart_get, alink_uart_put); #else alink_start(dev); #endif alink_wait_connect(NULL,-1); free((void*)dev); return ret; }
int ICACHE_FLASH_ATTR alink_demo() { struct device_info main_dev; memset(&main_dev, 0, sizeof(main_dev)); alink_fill_deviceinfo(&main_dev); // 必须根据PRD表格更新设备信息 //alink_set_loglevel(ALINK_LL_DEBUG | ALINK_LL_INFO | ALINK_LL_WARN | ALINK_LL_ERROR); alink_set_loglevel(ALINK_LL_ERROR); //alink_enable_sandbox_mode(); // 线上环境需要注解此函数 main_dev.sys_callback[ALINK_FUNC_SERVER_STATUS] = alink_handler_systemstates_callback; alink_set_callback(ALINK_FUNC_AVAILABLE_MEMORY, print_mem_callback); /* ALINK_CONFIG_LEN 2048 */ alink_register_cb(ALINK_FUNC_READ_CONFIG, (void *)&read_config); alink_register_cb(ALINK_FUNC_WRITE_CONFIG, (void *)&write_config); alink_register_cb(ALINK_FUNC_GET_STATUS, alink_get_debuginfo); //alink_enable_sandbox(&main_dev); // 线上环境需要注解此函数 alink_register_cb(ALINK_FUNC_OTA_FIRMWARE_SAVE, esp_ota_firmware_update); alink_register_cb(ALINK_FUNC_OTA_UPGRADE, esp_ota_upgrade); /*start alink-sdk */ #ifdef PASS_THROUGH //透传方式(设备与服务器采用raw data通讯) alink_start_rawdata(&main_dev, rawdata_get_callback, rawdata_set_callback); #else // 非透传方式(设备与服务器采用json格式数据通讯) main_dev.dev_callback[ACB_GET_DEVICE_STATUS] = main_dev_get_device_status_callback; main_dev.dev_callback[ACB_SET_DEVICE_STATUS] = main_dev_set_device_status_callback; alink_start(&main_dev); //register main device here #endif //PASS_THROUGH os_printf("%s %d wait time=%d \n", __FUNCTION__, __LINE__, ALINK_WAIT_FOREVER); ESP_DBG(("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")); if (ALINK_OK == alink_wait_connect(NULL, ALINK_WAIT_FOREVER)) //wait main device login, -1 means wait forever { #if USER_UART_CTRL_DEV_EN char send_buf_alink_connOK[] = { 0x31, 0x31, 0x31, 0x31 }; // demo data to tell uart mcu dev, alink conn success uart0_write_data(send_buf_alink_connOK, sizeof(send_buf_alink_connOK)); #endif } else { #if USER_UART_CTRL_DEV_EN char send_buf_alink_connFailed[] = { 0x32, 0x32, 0x32, 0x32 }; // demo data to tell uart mcu dev, alink conn success uart0_write_data(send_buf_alink_connFailed, sizeof(send_buf_alink_connFailed)); #endif } if (need_notify_app) { need_notify_app = 0; uint8 macaddr[6]; char mac[17 + 1]; if (wifi_get_macaddr(0, macaddr)) { os_printf("macaddr=%02x:%02x:%02x:%02x:%02x:%02x\n", MAC2STR(macaddr)); snprintf(mac, sizeof(mac), "%02x:%02x:%02x:%02x:%02x:%02x", MAC2STR(macaddr)); zconfig_notify_app(DEV_MODEL, mac, ""); // if not factory reset , } } //printf("%s %d \n",__FUNCTION__,__LINE__); //printf("alink_demo heap_size %d\n",system_get_free_heap_size()); //system_print_meminfo(); /* 设备主动上报数据 */ while (sample_running) { //os_printf("%s %d \n",__FUNCTION__,__LINE__); #ifdef PASS_THROUGH alink_device_post_raw_data(); #else alink_device_post_data(NULL); #endif //PASS_THROUGH if (need_factory_reset) { wsf_deb("key to factory_reset\n"); need_factory_reset = 0; alink_factory_reset(); } alink_sleep(1000); } /* 设备退出alink-sdk */ alink_end(); return 0; }