From d32a26e48a71bdba55e62160be94c88ac80098e2 Mon Sep 17 00:00:00 2001 From: helloyifa <447523396@qq.com> Date: Thu, 12 Jun 2025 12:10:43 +0800 Subject: [PATCH] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0ota=E6=BF=80=E6=B4=BB=20?= =?UTF-8?q?=E8=BF=98=E6=B2=A1=E6=9C=89=E5=AE=8C=E6=88=90=202.=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E5=8A=A9=E7=9C=A0=E4=BB=AA=20=203.=20=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E8=93=9D=E7=89=99=E6=93=8D=E4=BD=9C=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../beken_wss_paopao/main/CMakeLists.txt | 1 + .../boarding_service/wifi_boarding_utils.c | 230 +++++++++++- .../beken_wss_paopao/main/iot/iot_lamp.c | 4 + .../beken_wss_paopao/main/iot/iot_lamp.h | 1 + .../beken_wss_paopao/main/iot/iot_ota.c | 343 ++++++++++++++++++ .../beken_wss_paopao/main/iot/iot_ota.h | 31 ++ .../main/iot/iot_sleep_helper.c | 152 +++++--- .../main/iot/iot_sleep_helper.h | 1 + .../projects/beken_wss_paopao/main/spi_led.c | 3 +- .../projects/beken_wss_paopao/main/spi_led.h | 2 +- .../main/websocket_rtc_demo.c | 14 +- 11 files changed, 722 insertions(+), 60 deletions(-) create mode 100755 bk_aidk/projects/beken_wss_paopao/main/iot/iot_ota.c create mode 100755 bk_aidk/projects/beken_wss_paopao/main/iot/iot_ota.h diff --git a/bk_aidk/projects/beken_wss_paopao/main/CMakeLists.txt b/bk_aidk/projects/beken_wss_paopao/main/CMakeLists.txt index 9bfe1ff2..c7397dda 100755 --- a/bk_aidk/projects/beken_wss_paopao/main/CMakeLists.txt +++ b/bk_aidk/projects/beken_wss_paopao/main/CMakeLists.txt @@ -25,6 +25,7 @@ list(APPEND srcs iot/iot_speaker.c iot/iot_sleep_helper.c iot/thing.c + iot/iot_ota.c ) if (CONFIG_NETWORK_AUTO_RECONNECT) list(APPEND srcs diff --git a/bk_aidk/projects/beken_wss_paopao/main/boarding_service/wifi_boarding_utils.c b/bk_aidk/projects/beken_wss_paopao/main/boarding_service/wifi_boarding_utils.c index cc8ab64b..cea2669d 100755 --- a/bk_aidk/projects/beken_wss_paopao/main/boarding_service/wifi_boarding_utils.c +++ b/bk_aidk/projects/beken_wss_paopao/main/boarding_service/wifi_boarding_utils.c @@ -20,6 +20,10 @@ #include "wifi_boarding_internal.h" #include "wifi_boarding_utils.h" +//helloyifa +#include "../iot/iot_sleep_helper.h" +#include "../iot/iot_lamp.h" +#include "spi_led.h" static ble_boarding_info_t *s_ble_boarding_info = NULL; static beken_semaphore_t s_ble_sema = NULL; @@ -449,10 +453,11 @@ static int32_t wifi_boarding_gatts_cb(bk_gatts_cb_event_t event, bk_gatt_if_t ga bk_ble_gatts_get_attr_value(param->handle, &buff_size, &tmp_buff); wboard_loge("state: %s", param->value); - - //�������ܵ���ָ�� + char recv [param->len]; + os_memcpy((char *)recv, param->value, param->len); + char* ptr; - if (NULL != (ptr = strstr((char *)param->value, "AT+SSID"))){ + if (NULL != (ptr = strstr((char *)recv, "AT+SSID"))){ wboard_loge("start scan wifi!!"); //�ص� if (s_ble_boarding_info && s_ble_boarding_info->scan_wifi_cb) @@ -460,7 +465,7 @@ static int32_t wifi_boarding_gatts_cb(bk_gatts_cb_event_t event, bk_gatt_if_t ga s_ble_boarding_info->scan_wifi_cb(); } } - if (NULL != (ptr = strstr((char *)param->value, "AT+SCAN"))){ + if (NULL != (ptr = strstr((char *)recv, "AT+SCAN"))){ wboard_loge("start scan wifi!!"); //�ص� if (s_ble_boarding_info && s_ble_boarding_info->scan_wifi_cb) @@ -469,14 +474,14 @@ static int32_t wifi_boarding_gatts_cb(bk_gatts_cb_event_t event, bk_gatt_if_t ga } } - if (NULL != (ptr = strstr((char *)param->value, "AT+WIFI"))){ + if (NULL != (ptr = strstr((char *)recv, "AT+WIFI"))){ wboard_loge("recv wifi info!!"); char m_ssid[64] = {0}; char m_pwd [64] = {0}; memset(m_ssid,0,sizeof(m_ssid)); memset(m_pwd,0,sizeof(m_pwd)); - sscanf((char *)param->value,"AT+WIFI=%[^,],%[^,]", m_ssid,m_pwd); + sscanf((char *)recv,"AT+WIFI=%[^,],%[^,]", m_ssid,m_pwd); wboard_loge("%s %s ",m_ssid,m_pwd); //����SSID if(strlen(m_ssid) > 0){ @@ -522,10 +527,221 @@ static int32_t wifi_boarding_gatts_cb(bk_gatts_cb_event_t event, bk_gatt_if_t ga } } - + /** + * 7.蓝牙控制音量 + * AT+VOLUME=100 (0-100) + * 回复:AT+VOLUME=OK + */ + else if (NULL != (ptr = strstr((char *)recv, "AT+VOLUME"))) + { + wboard_loge("AT+VOLUME!!"); + uint8_t str_respone[32] = {0}; + memset(str_respone, 0, sizeof(str_respone)); + + char volume[12] = {0}; + memset(volume, 0, sizeof(volume)); + sscanf((char *)recv, "AT+VOLUME=%s", volume); + if (strcmp(volume, "?") == 0) + { + // 返回当前音量 + } + else + { + // 设置音量 + // int v = atoi(volume); + } + sprintf((char *)str_respone, "%s", "AT+VOLUME=OK"); + wifi_boarding_notify(str_respone, strlen((char *)&str_respone)); + } + /** + * 蓝牙控制灯光亮度 + * AT+BRIGHT=100 (0-100) + * 回复:AT+BRIGHT=OK + */ + else if (NULL != (ptr = strstr((char *)recv, "AT+BRIGHT"))) + { + wboard_loge("AT+BRIGHT!!"); + uint8_t str_respone[32] = {0}; + memset(str_respone, 0, sizeof(str_respone)); + char bright[12] = {0}; + memset(bright, 0, sizeof(bright)); + sscanf((char *)recv, "AT+BRIGHT=%s", bright); + if (strcmp(bright, "?") == 0) + { + // 返回当前亮度 + sprintf((char *)str_respone, "%s", "AT+BRIGHT=100"); + wifi_boarding_notify(str_respone, strlen((char *)&str_respone)); + } + else + { + int brightness = atoi(bright); + ws2812_set_all_led_brightness(brightness); + sprintf((char *)str_respone, "%s", "AT+BRIGHT=OK"); + wifi_boarding_notify(str_respone, strlen((char *)&str_respone)); + } + } + /** + * 蓝牙控制打开关闭助眠仪 + * AT+SLEEP_EN=1 (0-关闭 1-打开) + * 回复:AT+SLEEP_EN=OK + */ + else if (NULL != (ptr = strstr((char *)recv, "AT+SLEEP_EN"))) + { + wboard_loge("AT+SLEEP_EN!!"); + uint8_t str_respone[32] = {0}; + memset(str_respone, 0, sizeof(str_respone)); + + char enable[12] = {0}; + memset(enable, 0, sizeof(enable)); + sscanf((char *)recv, "AT+SLEEP_EN=%s", enable); + if (strcmp(enable, "?") == 0) + { + sprintf((char *)str_respone, "%s", "AT+SLEEP_EN=1"); + } + else + { + int open = atoi(enable); + if(open){ + sleep_helper_open(); + }else{ + sleep_helper_close(); + } + sprintf((char *)str_respone, "%s", "AT+SLEEP_EN=OK"); + } + wifi_boarding_notify(str_respone, strlen((char *)&str_respone)); + } + /** + * 蓝牙控制助眠仪模式 + * 发送:AT+SLEEP_MODE=1 (1-3 代表3种模式) + * 回复:AT+SLEEP_MODE=OK + */ + else if (NULL != (ptr = strstr((char *)recv, "AT+SLEEP_MODE"))) + { + wboard_loge("AT+SLEEP_MODE!!"); + uint8_t str_respone[32] = {0}; + memset(str_respone, 0, sizeof(str_respone)); + char mode[12] = {0}; + memset(mode, 0, sizeof(mode)); + sscanf((char *)recv, "AT+SLEEP_MODE=%s", mode); + if (strcmp(mode, "?") == 0) + { + sprintf((char *)str_respone, "%s", "AT+SLEEP_MODE=1"); + } + else + { + int v = atoi(mode); + switch(v){ + case 1: + sleep_helper_set_mode(SLEEP_MODE_1); + break; + case 2: + sleep_helper_set_mode(SLEEP_MODE_1); + break; + case 3: + sleep_helper_set_mode(SLEEP_MODE_1); + break; + } + sprintf((char *)str_respone, "%s", "AT+SLEEP_MODE=OK"); + } + wifi_boarding_notify(str_respone, strlen((char *)&str_respone)); + } + + /** + * 蓝牙控制助眠仪超时时间 + * 发送:AT+SLEEP_TIMEOUT=1 (0-30 分钟) + * 回复:AT+SLEEP_TIMEOUT=OK + */ + else if (NULL != (ptr = strstr((char *)recv, "AT+SLEEP_TIMEOUT"))) + { + wboard_loge("AT+SLEEP_TIMEOUT!!"); + uint8_t str_respone[32] = {0}; + memset(str_respone, 0, sizeof(str_respone)); + char mode[12] = {0}; + memset(mode, 0, sizeof(mode)); + sscanf((char *)recv, "AT+SLEEP_TIMEOUT=%s", mode); + if (strcmp(mode, "?") == 0) + { + sprintf((char *)str_respone, "%s", "AT+SLEEP_TIMEOUT=10"); + } + else + { + int v = atoi(mode); + sleep_helper_set_timeout(v); + sprintf((char *)str_respone, "%s", "AT+SLEEP_MODE=OK"); + } + wifi_boarding_notify(str_respone, strlen((char *)&str_respone)); + } + /* + *11.蓝牙控制助眠仪工作等级 + 发送: AT+SLEEP_LEVEL=1 (1-9 代表9个等级强度) + 回复: AT+SLEEP_LEVEL=OK + */ + else if (NULL != (ptr = strstr((char *)recv, "AT+SLEEP_LEVEL"))) + { + wboard_loge("AT+SLEEP_LEVEL!!"); + uint8_t str_respone[32] = {0}; + memset(str_respone, 0, sizeof(str_respone)); + char level[12] = {0}; + memset(level, 0, sizeof(level)); + sscanf((char *)recv, "AT+SLEEP_LEVEL=%s", level); + if (strcmp(level, "?") == 0) + { + sprintf((char *)str_respone, "%s", "AT+SLEEP_LEVEL=1"); + } + else + { + int v = atoi(level); + sleep_helper_set_level(v); + sprintf((char *)str_respone, "%s", "AT+SLEEP_LEVEL=OK"); + } + wifi_boarding_notify(str_respone, strlen((char *)&str_respone)); + } + /** + * 蓝牙控制OTA + */ + else if (NULL != (ptr = strstr((char *)recv, "AT+OTA"))) + { + wboard_loge("AT+OTA!!"); + uint8_t str_respone[32] = {0}; + memset(str_respone, 0, sizeof(str_respone)); + // char ota_url[128] = {0}; + // memset(ota_url, 0, sizeof(ota_url)); + // sscanf((char *)recv, "AT+OTA=%s", ota_url); + + // 2. 查找等号位置 + char *equal_pos = strchr((char *)param->value, '='); + if (equal_pos != NULL) + { + // 3. 计算等号后子串长度 + int len = strlen(equal_pos + 1); // +1跳过等号本身 + + // 4. 动态分配内存 + char *ota_url = (char *)malloc(len + 1); // +1存放结束符'\0' + + // 5. 复制子串并添加结束符 + strncpy(ota_url, equal_pos + 1, len); + ota_url[len] = '\0'; // 确保字符串终止 + + sprintf((char *)str_respone, "%s", "AT+OTA=OK"); + wifi_boarding_notify(str_respone, strlen((char *)&str_respone)); + if (strlen(ota_url) > 0) + { + wboard_loge("OTA-->%s", ota_url); + wboard_loge("OTA--len >%d", strlen(ota_url)); + ws2812_led_start_ota(); + lamp_http_ota_start(ota_url); + free(ota_url); + } + } + + // int v = atoi(mode); + // sleep_helper_set_timeout(v); + } //wifi_boarding_notify(param->value,param->len); } + + else { wboard_loge("invalid write handle %d", param->handle); diff --git a/bk_aidk/projects/beken_wss_paopao/main/iot/iot_lamp.c b/bk_aidk/projects/beken_wss_paopao/main/iot/iot_lamp.c index 6821920c..9fb9d02e 100755 --- a/bk_aidk/projects/beken_wss_paopao/main/iot/iot_lamp.c +++ b/bk_aidk/projects/beken_wss_paopao/main/iot/iot_lamp.c @@ -140,6 +140,10 @@ void https_ota_start(char *url) os_printf("https_ota_start failed\r\n"); } + +void lamp_http_ota_start(char *url){ + https_ota_start(url); +} #endif int iot_lamp_check_ota(){ diff --git a/bk_aidk/projects/beken_wss_paopao/main/iot/iot_lamp.h b/bk_aidk/projects/beken_wss_paopao/main/iot/iot_lamp.h index 66039f65..953e462c 100755 --- a/bk_aidk/projects/beken_wss_paopao/main/iot/iot_lamp.h +++ b/bk_aidk/projects/beken_wss_paopao/main/iot/iot_lamp.h @@ -19,6 +19,7 @@ extern "C" { void lamp_init(); +void lamp_http_ota_start(char *url); void lamp_deinit(); cJSON * iot_lamp_get_device_desc(); diff --git a/bk_aidk/projects/beken_wss_paopao/main/iot/iot_ota.c b/bk_aidk/projects/beken_wss_paopao/main/iot/iot_ota.c new file mode 100755 index 00000000..a334c373 --- /dev/null +++ b/bk_aidk/projects/beken_wss_paopao/main/iot/iot_ota.c @@ -0,0 +1,343 @@ +#include +#include +#include +#include +#include +#include +#include "bk_private/bk_init.h" +#include +#include +#include "components/webclient.h" +#include "cJSON.h" +#include "components/bk_uid.h" +#include "bk_genie_comm.h" +#include "iot_lamp.h" +//OTA +#include "modules/ota.h" +#include "spi_led.h" + +#define TAG "ota" +#define LOGI(...) BK_LOGI(TAG, ##__VA_ARGS__) +#define LOGW(...) BK_LOGW(TAG, ##__VA_ARGS__) +#define LOGE(...) BK_LOGE(TAG, ##__VA_ARGS__) +#define LOGD(...) BK_LOGD(TAG, ##__VA_ARGS__) + + +#define RCV_BUF_SIZE 1024 +#define SEND_HEADER_SIZE 1024 +#define POST_DATA_MAX_SIZE 1024 +#define MAX_URL_LEN 256 + +#define IOT_DEVICE_TYPE "bread-compact-wifi" + +#define IOT_OTA "ota" +#define IOT_OTA_VERSION "1.0.4" +//#define IOT_OTA_CHECK_URL "http://106.52.233.130:8888/ir58/ir58.json" +#define IOT_OTA_CHECK_VERSION_URL "https://xiaozhi.xa-poka.com/xiaozhi/ota/" + +/* +static char activation_code[12]; +static int has_new_version; +static int has_mqtt_config; +static int has_websocket_config; +static int has_server_time; +static int has_activation_code; +static int has_serial_number; +static int has_activation_challenge; + +static char firmware_url[128]; + */ +static int has_activation_code; +static int has_websocket_config; + +int ota_has_activation_code(){ + return has_activation_code; +} + +int ota_has_websocket_config(){ + return has_websocket_config; +} + + +int test_ota(){ + int ret = 0; + struct webclient_session* session = NULL; + unsigned char *buffer = NULL; + int bytes_read, resp_status; + int content_length = -1; + char *url = NULL; + + url = web_strdup("https://xiaozhi.xa-poka.com/xiaozhi/otaMag/download/59cc091e-eaf3-417d-a524-d5e3d95883f4"); + if(url == NULL) + { + BK_LOGE(TAG,"no memory for create get request uri buffer.\n"); + return BK_ERR_NO_MEM; + } + + buffer = (unsigned char *) web_malloc(1024); + if (buffer == NULL) + { + BK_LOGE(TAG,"no memory for receive buffer.\n"); + + goto __exit; + + } + + /* create webclient session and set header response size */ + session = webclient_session_create(1024); + if (session == NULL) + { + + goto __exit; + } + + /* send GET request by default header */ + if ((resp_status = webclient_get(session, url)) != 200) + { + BK_LOGE(TAG,"webclient GET request failed, response(%d) error.\n", resp_status); + ret = BK_ERR_STATE; + goto __exit; + } + + BK_LOGI(TAG,"webclient get response data: \n"); + + content_length = webclient_content_length_get(session); + if (content_length < 0) + { + BK_LOGI(TAG,"webclient GET request type is chunked.\n"); + do + { + bytes_read = webclient_read(session, (void *)buffer, 1024); + if (bytes_read <= 0) + { + break; + } + + } while (1); + + BK_LOGI(TAG,"\n"); + } + else + { + int content_pos = 0; + + do + { + bytes_read = webclient_read(session, (void *)buffer, + content_length - content_pos > 1024 ? + 1024 : content_length - content_pos); + if (bytes_read <= 0) + { + break; + } + content_pos += bytes_read; + BK_LOGE(TAG,"%s bytes_read:%d content_pos:%d\n", __func__, bytes_read, content_pos); + } while (content_pos < content_length); + + if (content_pos != content_length) { + BK_LOGE(TAG,"%s error! recv:%d content_length:%d\n", __func__, content_pos, content_length); + ret = BK_ERR_STATE; + } + + } + +__exit: + if (session) + { + webclient_close(session); + } + + if (buffer) + { + web_free(buffer); + } + + if (url) + { + web_free(url); + } + + return ret; +} + + +int ota_check_version_resp_paser(char * text){ + cJSON *json = NULL; + __maybe_unused char *StatusCode = NULL, *state, *detail, *reason; + __maybe_unused int create_ts; + + json = cJSON_Parse(text); + if (!json) + { + BK_LOGE(TAG, "Error before: [%s]\n", cJSON_GetErrorPtr()); + return BK_FAIL; + } + + //cJSON *code = cJSON_GetObjectItem(json, "status"); + cJSON * activation = cJSON_GetObjectItem(json, "activation"); + if (cJSON_IsObject(activation)) + { + // 设备未激活 + has_activation_code = 1; + char *activation_code = cJSON_GetObjectItem(activation, "code")->valuestring; + BK_LOGE(TAG, "%s, activation code:%s\r\n", __func__, activation_code); + } + else + { + has_activation_code = 0; + } + + //has_websocket_config_ = false; + cJSON *websocket = cJSON_GetObjectItem(json, "websocket"); + if (cJSON_IsObject(websocket)) { + char *websocket_url = cJSON_GetObjectItem(websocket, "url")->valuestring; + BK_LOGE(TAG, "%s, websocket_url:%s\r\n", __func__, websocket_url); + has_websocket_config = 1; + } else { + //ESP_LOGI(TAG, "No websocket section found!"); + } + + + cJSON_Delete(json); + + return BK_OK; + +} + +/** + * Specification: https://ccnphfhqs21z.feishu.cn/wiki/FjW6wZmisimNBBkov6OcmfvknVd + * 根据小智的协议 联网成功后 通过OTA 检测设备是否在平台上激活 + * 如果没有激活则返回验证码,然后登录小程序或者平台进行输入验证码添加设备 + * + * 如果激活了 返回websocket 通讯地址直接进行连接平台 + */ +int ota_check_version(){ + BK_LOGE(TAG, "Current version: %s", IOT_OTA_VERSION); + + //test_ota(); + //int ret = 0; + struct webclient_session* session = NULL; + unsigned char *buffer = NULL; + char *post_data = NULL; + int bytes_read, resp_status; + //int content_length = -1; + char *url = NULL; + url = web_strdup(IOT_OTA_CHECK_VERSION_URL); + if(url == NULL) + { + BK_LOGE(TAG,"no memory for create get request uri buffer.\n"); + return BK_FAIL; + } + + buffer = (unsigned char *) web_malloc(1024); + if (buffer == NULL) + { + BK_LOGE(TAG,"no memory for receive buffer.\n"); + goto __exit; + + } + + /* create webclient session and set header response size */ + session = webclient_session_create(1024); + if (session == NULL) + { + goto __exit; + } + + /*Generate https header*/ + webclient_header_fields_add(session, "Content-Type: application/json\r\n"); + webclient_header_fields_add(session, "User-Agent: bread-compact-wifi/1.0.1\r\n"); + + + uint8_t base_mac[BK_MAC_ADDR_LEN] = {0}; + bk_get_mac(base_mac, MAC_TYPE_BASE); + char device_id[64]; + os_memset(device_id,0,sizeof(device_id)); + sprintf(device_id, "Device-Id: %02X:%02X:%02X:%02X:%02X:%02X\r\n", base_mac[0], base_mac[1], base_mac[2], base_mac[3], base_mac[4], base_mac[5]); + //webclient_header_fields_add(session, "Device-Id: 11:22:33:44:55:66\r\n"); + webclient_header_fields_add(session, device_id); + + + + cJSON *ota_desc = cJSON_CreateObject(); + + char mac_address[24]; + os_memset(mac_address,0,sizeof(device_id)); + sprintf(mac_address, "%02X:%02X:%02X:%02X:%02X:%02X", base_mac[0], base_mac[1], base_mac[2], base_mac[3], base_mac[4], base_mac[5]); + //cJSON_AddStringToObject(ota_desc, "mac_address", "11:22:33:44:55:66"); + cJSON_AddStringToObject(ota_desc, "mac_address", mac_address); + cJSON_AddStringToObject(ota_desc, "chip_model_name", "BK7258"); + + cJSON *application = cJSON_CreateObject(); + + cJSON_AddStringToObject(application, "version", IOT_OTA_VERSION); + cJSON_AddStringToObject(application, "elf_sha256", "22986216df095587c42f8aeb06b239781c68ad8df80321e260556da7fcf5f522"); + //添加application属性到ota_desc + cJSON_AddItemToObjectCS(ota_desc,"application",application); + + cJSON *board = cJSON_CreateObject(); + cJSON_AddStringToObject(board, "type", IOT_DEVICE_TYPE); + cJSON_AddStringToObject(board, "name", "iR58_xxxx"); + cJSON_AddStringToObject(board, "ssid", "1603"); + cJSON_AddNumberToObject(board, "rssi", -50); + cJSON_AddNumberToObject(board, "channel", 1); + cJSON_AddStringToObject(board, "ip", ""); + cJSON_AddStringToObject(board, "mac", "11:22:33:44:55:66"); + cJSON_AddItemToObjectCS(ota_desc,"board",board); + + char *json_data = cJSON_PrintUnformatted(ota_desc); + + /*Generate data*/ + post_data = os_malloc(POST_DATA_MAX_SIZE); + if (post_data == NULL) + { + BK_LOGE(TAG, "no memory for post_data buffer\n"); + goto __exit; + } + os_memset(post_data, 0, POST_DATA_MAX_SIZE); + int data_len = 0; + data_len += os_snprintf(post_data + data_len, POST_DATA_MAX_SIZE, json_data); + + webclient_header_fields_add(session, "Content-Length: %d\r\n", os_strlen(post_data)); + + BK_LOGE(TAG, "data_len: %d", data_len ); + BK_LOGE(TAG, "strlen: %d", strlen(json_data) ); + BK_LOGE(TAG, "json_data: %s", json_data); + /* send POST request by default header */ + if ((resp_status = webclient_post(session, IOT_OTA_CHECK_VERSION_URL, post_data, data_len)) != 200) + { + BK_LOGE(TAG, "webclient POST request failed, response(%d) error.\n", resp_status); + return BK_FAIL; + } + BK_LOGE(TAG, "webclient post response data: \n"); + do + { + bytes_read = webclient_read(session, buffer, RCV_BUF_SIZE); + if (bytes_read > 0) + { + break; + } + } while (1); + BK_LOGE(TAG, "bytes_read: %d\n", bytes_read); + BK_LOGE(TAG, "buffer %s.\n", buffer); + + //resp_status = bk_agora_ai_agent_start_rsp_parse(buffer); + ota_check_version_resp_paser((char *)buffer); + return BK_OK; +__exit: + if (session) + { + webclient_close(session); + } + + if (buffer) + { + web_free(buffer); + } + + if (url) + { + web_free(url); + } + return BK_FAIL; +} \ No newline at end of file diff --git a/bk_aidk/projects/beken_wss_paopao/main/iot/iot_ota.h b/bk_aidk/projects/beken_wss_paopao/main/iot/iot_ota.h new file mode 100755 index 00000000..aabc297e --- /dev/null +++ b/bk_aidk/projects/beken_wss_paopao/main/iot/iot_ota.h @@ -0,0 +1,31 @@ +/************************************************************* + * + * Copyright (C) POKA + * All rights reserved. + * + *************************************************************/ +#ifndef __IOT_OTA_H__ +#define __IOT_OTA_H__ + +#ifdef __cplusplus +extern "C" { +#endif + + +#include "cJSON.h" + +typedef struct +{ + uint8_t device_id[6]; + char activation_code[32]; + char websocket_url[128]; +} bk_genie_device_info_t; + +int ota_check_version(); +int ota_has_activation_code(); +int ota_has_websocket_config(); + +#ifdef __cplusplus +} +#endif +#endif /* __IOT_OTA_H__ */ \ No newline at end of file diff --git a/bk_aidk/projects/beken_wss_paopao/main/iot/iot_sleep_helper.c b/bk_aidk/projects/beken_wss_paopao/main/iot/iot_sleep_helper.c index 41f29ed1..a87f07fa 100755 --- a/bk_aidk/projects/beken_wss_paopao/main/iot/iot_sleep_helper.c +++ b/bk_aidk/projects/beken_wss_paopao/main/iot/iot_sleep_helper.c @@ -33,6 +33,15 @@ */ static uint32_t pwm_chan = 1; +#define PWM_CLOCK_SOURCE (26000000) +#define PWM_FREQ (20000)//50Khz +#define DUTY_MAX (2047) +static uint32_t s_period = PWM_CLOCK_SOURCE / PWM_FREQ; +/** + * 10分钟 + */ +static uint32_t work_timeout = 60*1000*10; +static beken_timer_t timer_close_handle; /** * 模式1 单周期时序表 */ @@ -152,7 +161,7 @@ static mode_info_t mode_3[49] = { }; //static uint32_t g_mode_level[SLEEP_MODE_LEVEL_NUM] = {65, 60, 55, 50, 45, 40, 35,30,25}; -static uint32_t g_mode_level[SLEEP_MODE_LEVEL_NUM] = {25, 30, 35, 40, 45, 50, 55,60,60}; +static uint32_t g_mode_level[SLEEP_MODE_LEVEL_NUM] = {35, 40, 45, 50, 60, 70, 80,90,100}; //static uint32_t g_mode_level[SLEEP_MODE_LEVEL_NUM] = { 30, 35, 40, 45, 50, 55}; static int helper_start = 0; @@ -250,49 +259,9 @@ static void sleep_helper_thread(void *arg) } -void sleep_helper_init(){ - bk_pwm_driver_init(); - - //GPIO - gpio_dev_unmap(GPIO_44); - gpio_dev_unmap(GPIO_45); - - - gpio_dev_unmap(GPIO_50); - bk_gpio_enable_input(GPIO_50); - bk_gpio_enable_pull(GPIO_50); - bk_gpio_pull_up(GPIO_50); - - pwm_init_config_t config = {0}; - config.period_cycle = 100; - //默认初始化不输出波形 - config.duty_cycle = 0; - //config.duty_cycle = g_mode_level[SLEEP_MODEL_LEVEL_1-1]; - bk_pwm_init(pwm_chan, &config); - bk_pwm_start(pwm_chan); - - helper_start = 1; - bk_err_t ret = BK_OK; - ret = rtos_init_queue(&sleep_helper_msg_que, "sleep_helper_msg_que", sizeof(sleep_msg_t), 2); - if (ret != kNoErr) - { - LOGE("create take_photo_msg queue fail\n"); - } - ret = rtos_create_thread(&sleep_helper_thread_hdl, - 9, - "sleep_helper", - (beken_thread_function_t)sleep_helper_thread, - 20 * 1024, - NULL); - if (ret != kNoErr) - { - sleep_helper_thread_hdl = NULL; - } -} - void sleep_helper_set_mode(sleep_mode mode){ current_mode = mode; - + LOGE("sleep_helper_set_mode %d \n",mode); if (sleep_helper_msg_que) { sleep_msg_t msg; @@ -301,16 +270,56 @@ void sleep_helper_set_mode(sleep_mode mode){ } } -void sleep_helper_set_level(sleep_model_level level){ +/** + * 定时回调 + */ +static void sleep_helper_close_timer_handle( void *arg ) +{ + LOGE("sleep_helper time to close %d \n"); + sleep_helper_close(); +} +/** + * 启动定时关闭 + */ +void sleep_helper_start_timer(){ + //超时自动关闭 + rtos_start_timer(&timer_close_handle); +} + +void sleep_helper_reload_timer(){ + //复位定时器 + rtos_reload_timer(&timer_close_handle); +} + +void sleep_helper_close_timer(){ + rtos_stop_timer(&timer_close_handle); +} +/** + * 设置定时时间 参数分钟 + */ +void sleep_helper_set_timeout(int timeout_m){ + if(timeout_m < 0 || timeout_m > 30){ + work_timeout = 10; + } + work_timeout = timeout_m*60*1000;//60*1000*3 + LOGE("sleep_helper_set_timeout: %d ms\n",work_timeout); + rtos_change_period(&timer_close_handle, work_timeout); + sleep_helper_reload_timer(); +} + +void sleep_helper_set_level(sleep_model_level level){ + LOGE("sleep_helper_set_level %d \n",level); if(current_mode == SLEEP_MODE_OFF){ sleep_helper_open(); } if(level>0 && level< 10){ current_level = level; pwm_period_duty_config_t config = {0}; - config.period_cycle = 100; - config.duty_cycle = g_mode_level[level-1]; + //config.period_cycle = 100; + config.period_cycle = s_period; + //config.duty_cycle = g_mode_level[level-1]; + config.duty_cycle = s_period*g_mode_level[level-1]/100; LOGE("sleep_helper_set_level %d \n",g_mode_level[level-1]); bk_pwm_set_period_duty(pwm_chan,&config); } @@ -328,19 +337,21 @@ void sleep_helper_open(){ return ; } - - pwm_init_config_t config = {0}; - config.period_cycle = 100; - config.duty_cycle = g_mode_level[0];//默认等级1 + //config.period_cycle = 100; + config.period_cycle = s_period; + //config.duty_cycle = g_mode_level[0];//默认等级1 + config.duty_cycle = s_period*g_mode_level[0]/100; bk_pwm_init(pwm_chan, &config); bk_pwm_start(pwm_chan); //默认模式1 sleep_helper_set_mode(SLEEP_MODE_1); + sleep_helper_start_timer(); } void sleep_helper_close(){ LOGE("sleep_helper_close\n"); + sleep_helper_close_timer(); current_mode = SLEEP_MODE_OFF; // 关闭睡眠仪 // 关闭PWM @@ -449,6 +460,49 @@ void iot_sleep_helper_parser_invoke(char* cmd,char * paramters_json){ cJSON_Delete(params); } +void sleep_helper_init(){ + bk_pwm_driver_init(); + + //GPIO + gpio_dev_unmap(GPIO_44); + gpio_dev_unmap(GPIO_45); + + + gpio_dev_unmap(GPIO_50); + bk_gpio_enable_input(GPIO_50); + bk_gpio_enable_pull(GPIO_50); + bk_gpio_pull_up(GPIO_50); + + pwm_init_config_t config = {0}; + //config.period_cycle = 100; + config.period_cycle = s_period; + //默认初始化不输出波形 + config.duty_cycle = 0; + //config.duty_cycle = g_mode_level[SLEEP_MODEL_LEVEL_1-1]; + bk_pwm_init(pwm_chan, &config); + bk_pwm_start(pwm_chan); + + helper_start = 1; + bk_err_t ret = BK_OK; + ret = rtos_init_queue(&sleep_helper_msg_que, "sleep_helper_msg_que", sizeof(sleep_msg_t), 2); + if (ret != kNoErr) + { + LOGE("create take_photo_msg queue fail\n"); + } + ret = rtos_create_thread(&sleep_helper_thread_hdl, + 9, + "sleep_helper", + (beken_thread_function_t)sleep_helper_thread, + 20 * 1024, + NULL); + if (ret != kNoErr) + { + sleep_helper_thread_hdl = NULL; + } + //初始化定时器 + rtos_init_timer(&timer_close_handle, work_timeout, sleep_helper_close_timer_handle, 0); +} + void sleep_helper_deinit(){ diff --git a/bk_aidk/projects/beken_wss_paopao/main/iot/iot_sleep_helper.h b/bk_aidk/projects/beken_wss_paopao/main/iot/iot_sleep_helper.h index ebdaa174..beb668ca 100755 --- a/bk_aidk/projects/beken_wss_paopao/main/iot/iot_sleep_helper.h +++ b/bk_aidk/projects/beken_wss_paopao/main/iot/iot_sleep_helper.h @@ -62,6 +62,7 @@ void sleep_helper_init(); void sleep_helper_open(); void sleep_helper_close(); void sleep_helper_set_mode(sleep_mode mode); +void sleep_helper_set_timeout(int timeout); void sleep_helper_set_level(sleep_model_level level); void sleep_helper_deinit(); diff --git a/bk_aidk/projects/beken_wss_paopao/main/spi_led.c b/bk_aidk/projects/beken_wss_paopao/main/spi_led.c index fb9cf2bf..6405d852 100755 --- a/bk_aidk/projects/beken_wss_paopao/main/spi_led.c +++ b/bk_aidk/projects/beken_wss_paopao/main/spi_led.c @@ -318,11 +318,10 @@ void ws2812_led_asr_wakeup(){ } void ws2812_set_all_led_brightness(int brightness){ + bk_printf("ws2812_set_all_led_brightness br:%d\n",brightness); if(brightness<0 || brightness>100){ return; } - - bk_printf("ws2812_set_all_led_brightness br:%d\n",brightness); if(current_color ==0x000000 && brightness > 0){ ws2812_set_all_led(WHITE, brightness); }else{ diff --git a/bk_aidk/projects/beken_wss_paopao/main/spi_led.h b/bk_aidk/projects/beken_wss_paopao/main/spi_led.h index 3773dd8e..052d47a1 100755 --- a/bk_aidk/projects/beken_wss_paopao/main/spi_led.h +++ b/bk_aidk/projects/beken_wss_paopao/main/spi_led.h @@ -5,7 +5,7 @@ extern "C" { #endif -typedef enum sleep_mode +typedef enum led_type_t { TYPE_OFF= 0, TYPE_WAKEUP, diff --git a/bk_aidk/projects/beken_wss_paopao/main/websocket_rtc_demo.c b/bk_aidk/projects/beken_wss_paopao/main/websocket_rtc_demo.c index 60c0a461..beddcdd0 100755 --- a/bk_aidk/projects/beken_wss_paopao/main/websocket_rtc_demo.c +++ b/bk_aidk/projects/beken_wss_paopao/main/websocket_rtc_demo.c @@ -33,6 +33,7 @@ #include "cJSON.h" #include "audio_process.h" #include "thing.h" +#include "iot_ota.h" #define TAG "WS_MAIN" #define LOGI(...) BK_LOGI(TAG, ##__VA_ARGS__) @@ -994,8 +995,10 @@ fail: return BK_FAIL; } - +//OTA +#include "modules/ota.h" /* call this api when wifi autoconnect */ +extern int bk_https_ota_download(const char *url); void beken_auto_run(void) { if (!rtc_runing) @@ -1003,6 +1006,15 @@ void beken_auto_run(void) // bk_genie_wakeup_agent(); audio_en = true; video_en = false; + int check_version = ota_check_version(); + if(check_version == BK_OK){ + LOGE("beken_auto_run check_version %d\n",check_version); + + LOGE("ota_has_activation_code %d\n",ota_has_activation_code()); + LOGE("ota_has_websocket_config %d\n",ota_has_websocket_config()); + } + + //bk_https_ota_download("https://xiaozhi.xa-poka.com/xiaozhi/otaMag/download/59cc091e-eaf3-417d-a524-d5e3d95883f4"); beken_rtc_start(); } }