diff --git a/bk_aidk/projects/beken_wss/main/beken_config.h b/bk_aidk/projects/beken_wss/main/beken_config.h index 25228c6e..9664c4fb 100755 --- a/bk_aidk/projects/beken_wss/main/beken_config.h +++ b/bk_aidk/projects/beken_wss/main/beken_config.h @@ -38,7 +38,7 @@ //#define CONFIG_UVC_CAMERA /* config CONFIG_USB_UVC in cp1 */ #define CONFIG_DVP_CAMERA -#define SPK_GAIN_MAX (0X15) +#define SPK_GAIN_MAX (0X30)// #define SPK_VOLUME_LEVEL (11) //[0,10] #define BANDWIDTH_ESTIMATE_MIN_BITRATE (500000) diff --git a/bk_aidk/projects/beken_wss_paopao/main/CMakeLists.txt b/bk_aidk/projects/beken_wss_paopao/main/CMakeLists.txt index fb861c44..8c728e3a 100755 --- a/bk_aidk/projects/beken_wss_paopao/main/CMakeLists.txt +++ b/bk_aidk/projects/beken_wss_paopao/main/CMakeLists.txt @@ -1,9 +1,10 @@ -set(incs . include asr bt_pan) +set(incs . include asr bt_pan iot) set(srcs) list(APPEND srcs app_main.c - spi_led.c + spi_led.c + ) if (CONFIG_SYS_CPU0) @@ -20,6 +21,10 @@ list(APPEND srcs countdown.c motor.c audio_para.c + iot/iot_lamp.c + iot/iot_speaker.c + iot/iot_sleep_helper.c + iot/thing.c ) if (CONFIG_NETWORK_AUTO_RECONNECT) list(APPEND srcs diff --git a/bk_aidk/projects/beken_wss_paopao/main/app_main.c b/bk_aidk/projects/beken_wss_paopao/main/app_main.c index 5af55ecc..cb8c5f1e 100755 --- a/bk_aidk/projects/beken_wss_paopao/main/app_main.c +++ b/bk_aidk/projects/beken_wss_paopao/main/app_main.c @@ -41,6 +41,8 @@ #include "components/bluetooth/bk_dm_bluetooth.h" #include "app_main.h" #include "spi_led.h" + + extern void user_app_main(void); extern void rtos_set_user_app_entry(beken_thread_function_t entry); extern int bk_cli_init(void); @@ -93,6 +95,10 @@ const struct factory_config_t s_user_config[] = { {"user_key2", (void *)&s_user_value2, 4, BK_TRUE, 4}, }; #endif +//helloyifa +#if (CONFIG_SYS_CPU0) + #include "iot/thing.h" +#endif static int beken_rtc_cli_init(void) { @@ -644,6 +650,7 @@ static void handle_system_event(key_event_t event) bk_config_write("volume", (void *)&volume, 4); + thing_init(); // spi_led_init(); #endif #endif diff --git a/bk_aidk/projects/beken_wss_paopao/main/beken_sdk/beken_rtc.c b/bk_aidk/projects/beken_wss_paopao/main/beken_sdk/beken_rtc.c index 17769ef0..5d4df6e4 100755 --- a/bk_aidk/projects/beken_wss_paopao/main/beken_sdk/beken_rtc.c +++ b/bk_aidk/projects/beken_wss_paopao/main/beken_sdk/beken_rtc.c @@ -13,7 +13,7 @@ #include #endif #include "bk_genie_comm.h" - +#include "thing.h" #define TAG "beken_rtc" #define LOGI(...) BK_LOGI(TAG, ##__VA_ARGS__) @@ -23,7 +23,7 @@ void _rtc_websocket_audio_receive_data(rtc_session *rtc_session, uint8 *data, uint32_t len, rtc_user_audio_rx_data_handle_cb cb); #define DEBUG_CRC -#define BEKEN_RTC_TXT_SIZE 500 +#define BEKEN_RTC_TXT_SIZE 1024 static const uint8 crc8_table[256] = { 0x00, 0xF7, 0xB9, 0x4E, 0x25, 0xD2, 0x9C, 0x6B, @@ -825,7 +825,7 @@ int rtc_websocket_send_text(transport web_socket, void *str, enum MsgType msgtyp break; } case BEKEN_RTC_SEND_LISTEN:{ - // 创建主 JSON 对象 + // 锟斤拷锟斤拷锟斤拷 JSON 锟斤拷锟斤拷 cJSON *listenMessage = cJSON_CreateObject(); if (listenMessage == NULL) { @@ -838,13 +838,13 @@ int rtc_websocket_send_text(transport web_socket, void *str, enum MsgType msgtyp cJSON_AddStringToObject(listenMessage, "state", "detect"); cJSON_AddStringToObject(listenMessage, "mode", "auto"); - cJSON_AddStringToObject(listenMessage, "text", "小美同学"); + cJSON_AddStringToObject(listenMessage, "text", "小锟斤拷同学"); - // 将 cJSON 对象转换为字符串 + // 锟斤拷 cJSON 锟斤拷锟斤拷转锟斤拷为锟街凤拷锟斤拷 char *jsonString = cJSON_Print(listenMessage); BK_LOGE("WebSocket", "Sending: %s\r\n", jsonString); websocket_client_send_text(web_socket, jsonString, strlen(jsonString), 10*1000); - // 删除 cJSON 对象以释放内存 + // 删锟斤拷 cJSON 锟斤拷锟斤拷锟斤拷锟酵凤拷锟节达拷 cJSON_Delete(listenMessage); break; } @@ -857,19 +857,19 @@ int rtc_websocket_send_text(transport web_socket, void *str, enum MsgType msgtyp // bk_printf("Global session_id_BEKEN_RTC_SEND_START_LISTEN: %s\n", session_id); - // 添加session_id字段 + // 锟斤拷锟斤拷session_id锟街讹拷 cJSON_AddStringToObject(startlistent, "session_id", ""); - // 添加type字段 + // 锟斤拷锟斤拷type锟街讹拷 cJSON_AddStringToObject(startlistent, "type", "listen"); - // 添加state字段 + // 锟斤拷锟斤拷state锟街讹拷 cJSON_AddStringToObject(startlistent, "state", "start"); - // 添加mode字段 + // 锟斤拷锟斤拷mode锟街讹拷 cJSON_AddStringToObject(startlistent, "mode", "auto") ; - // 生成JSON字符串 + // 锟斤拷锟斤拷JSON锟街凤拷锟斤拷 char *jsonString = cJSON_Print(startlistent); websocket_client_send_text(web_socket, jsonString, strlen(jsonString), 10*1000); @@ -885,19 +885,19 @@ int rtc_websocket_send_text(transport web_socket, void *str, enum MsgType msgtyp // bk_printf("Global session_id_BEKEN_RTC_SEND_STOP_LISTEN: %s\n", session_id); - // 添加session_id字段 + // 锟斤拷锟斤拷session_id锟街讹拷 cJSON_AddStringToObject(stoplistent, "session_id", ""); - // 添加type字段 + // 锟斤拷锟斤拷type锟街讹拷 cJSON_AddStringToObject(stoplistent, "type", "listen"); - // 添加state字段 + // 锟斤拷锟斤拷state锟街讹拷 cJSON_AddStringToObject(stoplistent, "state", "stop"); - // 添加mode字段 + // 锟斤拷锟斤拷mode锟街讹拷 cJSON_AddStringToObject(stoplistent, "mode", "auto") ; - // 生成JSON字符串 + // 锟斤拷锟斤拷JSON锟街凤拷锟斤拷 char *jsonString = cJSON_Print(stoplistent); websocket_client_send_text(web_socket, jsonString, strlen(jsonString), 10*1000); @@ -906,79 +906,79 @@ int rtc_websocket_send_text(transport web_socket, void *str, enum MsgType msgtyp } case BEKEN_RTC_SEND_START:{ - // 创建主 JSON 对象 + // 锟斤拷锟斤拷锟斤拷 JSON 锟斤拷锟斤拷 cJSON *startMessage = cJSON_CreateObject(); if (startMessage == NULL) { } - // 添加 "type" 字段 + // 锟斤拷锟斤拷 "type" 锟街讹拷 cJSON_AddStringToObject(startMessage, "type", "start"); - // 添加 "mode" 字段 + // 锟斤拷锟斤拷 "mode" 锟街讹拷 cJSON_AddStringToObject(startMessage, "mode", "auto"); - // 创建 "audio_params" JSON 对象 + // 锟斤拷锟斤拷 "audio_params" JSON 锟斤拷锟斤拷 cJSON *audio_params = cJSON_CreateObject(); if (audio_params == NULL) { cJSON_Delete(startMessage); } - // 向 "audio_params" 添加字段 + // 锟斤拷 "audio_params" 锟斤拷锟斤拷锟街讹拷 cJSON_AddStringToObject(audio_params, "format", "opus"); cJSON_AddNumberToObject(audio_params, "sample_rate", 16000); cJSON_AddNumberToObject(audio_params, "channels", 1); cJSON_AddNumberToObject(audio_params, "frame_duration", 60); - // 将 "audio_params" 添加到主 JSON 对象 + // 锟斤拷 "audio_params" 锟斤拷锟接碉拷锟斤拷 JSON 锟斤拷锟斤拷 cJSON_AddItemToObject(startMessage, "audio_params", audio_params); - // 将 cJSON 对象转换为字符串 + // 锟斤拷 cJSON 锟斤拷锟斤拷转锟斤拷为锟街凤拷锟斤拷 char *jsonString = cJSON_Print(startMessage); BK_LOGE("WebSocket", "Sending: %s\r\n", jsonString); websocket_client_send_text(web_socket, jsonString, strlen(jsonString), 10*1000); - // 删除 cJSON 对象以释放内存 + // 删锟斤拷 cJSON 锟斤拷锟斤拷锟斤拷锟酵凤拷锟节达拷 cJSON_Delete(startMessage); break;} case BEKEN_RTC_SEND_HELLO:{ - // 创建主 JSON 对象 + // 锟斤拷锟斤拷锟斤拷 JSON 锟斤拷锟斤拷 cJSON *helloMessage = cJSON_CreateObject(); if (helloMessage == NULL) { //return NULL; } - // 添加 "type" 字段 + // 锟斤拷锟斤拷 "type" 锟街讹拷 cJSON_AddStringToObject(helloMessage, "type", "hello"); - // 添加 "version" 字段 + // 锟斤拷锟斤拷 "version" 锟街讹拷 cJSON_AddNumberToObject(helloMessage, "version", 3); - // 添加 "transport" 字段 + // 锟斤拷锟斤拷 "transport" 锟街讹拷 cJSON_AddStringToObject(helloMessage, "transport", "websocket"); - // 创建 "audio_params" JSON 对象 + // 锟斤拷锟斤拷 "audio_params" JSON 锟斤拷锟斤拷 cJSON *audio_params = cJSON_CreateObject(); if (audio_params == NULL) { cJSON_Delete(helloMessage); //return NULL; } - // 向 "audio_params" 添加字段 + // 锟斤拷 "audio_params" 锟斤拷锟斤拷锟街讹拷 cJSON_AddStringToObject(audio_params, "format", "opus"); cJSON_AddNumberToObject(audio_params, "sample_rate", 16000); cJSON_AddNumberToObject(audio_params, "channels", 1); cJSON_AddNumberToObject(audio_params, "frame_duration", 60); - // 将 "audio_params" 添加到主 JSON 对象 + // 锟斤拷 "audio_params" 锟斤拷锟接碉拷锟斤拷 JSON 锟斤拷锟斤拷 cJSON_AddItemToObject(helloMessage, "audio_params", audio_params); - // 将 cJSON 对象转换为字符串 + // 锟斤拷 cJSON 锟斤拷锟斤拷转锟斤拷为锟街凤拷锟斤拷 char *jsonString = cJSON_Print(helloMessage); BK_LOGE("WebSocket", "Sending: %s\r\n", jsonString); websocket_client_send_text(web_socket, jsonString, strlen(jsonString), 10*1000); - // 删除 cJSON 对象以释放内存 + // 删锟斤拷 cJSON 锟斤拷锟斤拷锟斤拷锟酵凤拷锟节达拷 cJSON_Delete(helloMessage); /* @@ -990,6 +990,21 @@ int rtc_websocket_send_text(transport web_socket, void *str, enum MsgType msgtyp websocket_client_send_text(web_socket, buf, n, 10*1000);*/ break;} + case BEKEN_RTC_SEND_IOT_DESC:{ + extern char *getGlobalSessionId(void); + + cJSON* iot_desc = (cJSON*)thing_gen_iot_descriptors(getGlobalSessionId()); + char *message = cJSON_PrintUnformatted(iot_desc); + if (message == NULL) + { + cJSON_Delete(iot_desc); + return -1; + } + LOGE("iot -->(len:%d) %s\n",strlen(message),message); + int ret = websocket_client_send_text(web_socket, message, strlen(message), 10*1000); + LOGE("send iot desc ret:%d \n",ret); + cJSON_Delete(iot_desc); + break;} default: BK_LOGE("WebSocket", "Unsupported message type"); return -1; diff --git a/bk_aidk/projects/beken_wss_paopao/main/boarding_service/boarding_core.c b/bk_aidk/projects/beken_wss_paopao/main/boarding_service/boarding_core.c index de193c28..6b2fde12 100755 --- a/bk_aidk/projects/beken_wss_paopao/main/boarding_service/boarding_core.c +++ b/bk_aidk/projects/beken_wss_paopao/main/boarding_service/boarding_core.c @@ -61,6 +61,91 @@ bk_err_t bk_genie_send_msg(bk_genie_msg_t *msg) return ret; } + +//helloyifa +//static wifi_scan_result_t *scan_result = NULL; +static wifi_scan_result_t scan_result = {0}; +static wifi_scan_ap_info_t aps_tmps[15]; +int ssid_count_in_scan_result(char * ssid){ + + int count = 0; + for(int i = 0 ;i<15 ;i++){ + + if (strcmp(ssid, aps_tmps[i].ssid) == 0) { + //printf("锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷锟絓n"); + count++; + } + } + return count; +} + + +void ble_set_wifi_scan_result(wifi_scan_result_t *result){ + os_printf("ble_set_wifi_scan_result\n"); + //if(result!=NULL){ + wifi_scan_result_t *scan_result = result; + //} + + #if 1 + if (scan_result != NULL && scan_result->ap_num > 0) + { + char str_ssid[256] = {0}; + // AT+SSID= + strcat(str_ssid, "AT+SSID="); + int count = 0; + if (scan_result->ap_num > 20) + { + count = 20; + } + else + { + count = scan_result->ap_num; + } + int ap_index = 0; + for (int i = 0; i < count; i++) + { + if (strlen(scan_result->aps[i].ssid) > 0) + { + // os_printf("WIFI_SCAN:%s\r\n",scan_result->aps[i].ssid); + // 锟叫讹拷锟角凤拷锟斤拷锟接癸拷锟斤拷锟斤拷锟斤拷 + if (ssid_count_in_scan_result(scan_result->aps[i].ssid) > 0) + { + // os_printf("%s alread in list\r\n",scan_result->aps[i].ssid); + continue; + } + + // 锟斤拷锟芥到锟叫憋拷 + memset(aps_tmps[ap_index].ssid, 0, sizeof(aps_tmps[ap_index].ssid)); + strcpy(aps_tmps[ap_index].ssid, scan_result->aps[i].ssid); + ap_index++; + + strcat(str_ssid, scan_result->aps[i].ssid); + if (i != count) + { + strcat(str_ssid, ","); + } + } + } + os_printf("STR:%s\r\n", str_ssid); + //锟截达拷WIFI 锟叫憋拷 + wifi_boarding_notify((uint8_t *)str_ssid, strlen(str_ssid)); + + } +#endif + +} +//wifi_scan_result_t scan_result = {0}; +static int scan_wifi_event_cb(void *arg, event_module_t event_module, + int event_id, void *event_data) +{ + + BK_LOG_ON_ERR(bk_wifi_scan_get_result(&scan_result)); + BK_LOGE(TAG, "WIFI_SCAN: ap count:%d\r\n", scan_result.ap_num); + ble_set_wifi_scan_result(&scan_result); + BK_LOG_ON_ERR(bk_wifi_scan_dump_result(&scan_result)); + return BK_OK; +} + //extern char *app_id_record; //extern char *channel_name_record; extern uint8_t network_disc_evt_posted; @@ -112,9 +197,24 @@ static void bk_genie_message_handle(void) { switch (msg.event) { + //helloyifa + case DBEVT_WIFI_STATION_SCAN: + { + ble_notify_boarding_state(BOARDING_STATE_IDLE); + //锟秸碉拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷指锟斤拷 锟斤拷始锟斤拷锟斤拷 + ble_notify_boarding_state(BOARDING_STATE_SCAN); + //is_ble_boarding = 1; + bk_wifi_scan_free_result(&scan_result); + BK_LOG_ON_ERR(bk_event_register_cb(EVENT_MOD_WIFI, EVENT_WIFI_SCAN_DONE,scan_wifi_event_cb, NULL)); + BK_LOG_ON_ERR(bk_wifi_scan_start(NULL)); + } + break; case DBEVT_WIFI_STATION_CONNECT: { - LOGI("DBEVT_WIFI_STATION_CONNECT\n"); + //helloyifa + be_set_smart_config_running(); + ble_notify_boarding_state(BOARDING_STATE_SETWIFI); + LOGE("DBEVT_WIFI_STATION_CONNECT\n"); bk_genie_boarding_info_t *bk_genie_boarding_info = (bk_genie_boarding_info_t *) msg.param; bk_genie_wifi_sta_connect(bk_genie_boarding_info->boarding_info.ssid_value, bk_genie_boarding_info->boarding_info.password_value); @@ -123,7 +223,9 @@ static void bk_genie_message_handle(void) case DBEVT_WIFI_STATION_CONNECTED: { - LOGI("DBEVT_WIFI_STATION_CONNECTED\n"); + //helloyifa + ble_notify_boarding_state(BOARDING_STATE_DONE); + LOGE("DBEVT_WIFI_STATION_CONNECTED\n"); netif_ip4_config_t ip4_config; extern uint32_t uap_ip_is_start(void); @@ -140,8 +242,8 @@ static void bk_genie_message_handle(void) } LOGI("ip: %s\n", ip4_config.ip); - - bk_genie_boarding_event_notify_with_data(BOARDING_OP_STATION_START, BK_OK, ip4_config.ip, strlen(ip4_config.ip)); + //helloyifa + //bk_genie_boarding_event_notify_with_data(BOARDING_OP_STATION_START, BK_OK, ip4_config.ip, strlen(ip4_config.ip)); } break; #if CONFIG_BK_AGORA_DEV_STARTUP_AGENT diff --git a/bk_aidk/projects/beken_wss_paopao/main/boarding_service/boarding_service.c b/bk_aidk/projects/beken_wss_paopao/main/boarding_service/boarding_service.c index 124498cd..7fffb0ef 100755 --- a/bk_aidk/projects/beken_wss_paopao/main/boarding_service/boarding_service.c +++ b/bk_aidk/projects/beken_wss_paopao/main/boarding_service/boarding_service.c @@ -82,6 +82,23 @@ void bk_genie_boarding_event_message(uint16_t opcode, int status) msg.param = status << 16 | opcode; bk_genie_send_msg(&msg); } +//helloyifa +static void ble_boarding_scan_wifi_handle(){ + //锟秸碉拷锟斤拷始锟斤拷锟斤拷 锟斤拷锟斤拷息锟斤拷boarding_core锟斤拷锟斤拷 + bk_genie_msg_t msg; + msg.event = DBEVT_WIFI_STATION_SCAN; + msg.param = 0; + bk_genie_send_msg(&msg); +} + +static void ble_boarding_set_wifi_handle(){ + //锟秸碉拷wifi SSID 锟斤拷锟斤拷 + + bk_genie_msg_t msg; + msg.event = DBEVT_WIFI_STATION_CONNECT; + msg.param = (uint32_t)bk_genie_boarding_info; + bk_genie_send_msg(&msg); +} static void bk_genie_boarding_operation_handle(uint16_t opcode, uint16_t length, uint8_t *data) { @@ -372,6 +389,8 @@ int bk_genie_boarding_init(void) } bk_genie_boarding_info->boarding_info.cb = bk_genie_boarding_operation_handle; + bk_genie_boarding_info->boarding_info.scan_wifi_cb = ble_boarding_scan_wifi_handle; + bk_genie_boarding_info->boarding_info.set_wifi_cb = ble_boarding_set_wifi_handle; wifi_boarding_init(&bk_genie_boarding_info->boarding_info); 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 7c032d95..cc8ab64b 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 @@ -98,6 +98,7 @@ static bk_gatt_if_t s_gatts_if = 0; static uint16_t s_prop_cli_config; static uint8_t s_ssid[64]; static uint8_t s_password[64]; +static uint8_t s_cmd[64]; static uint16_t s_conn_ind = ~0; @@ -147,6 +148,16 @@ static const bk_gatts_attr_db_t s_gatts_attr_db_service_boarding[] = BK_GATT_PERM_READ | BK_GATT_PERM_WRITE, BK_GATT_AUTO_RSP), }, + //helloyifa + //cmd + { + BK_GATT_CHAR_DECL(0xea03, + sizeof(s_cmd), (uint8_t *)s_cmd, + BK_GATT_CHAR_PROP_BIT_READ | BK_GATT_CHAR_PROP_BIT_WRITE, + BK_GATT_PERM_READ | BK_GATT_PERM_WRITE, + BK_GATT_AUTO_RSP), + }, + }; static uint16_t s_service_attr_handle = INVALID_ATTR_HANDLE; @@ -155,7 +166,8 @@ static uint16_t s_char_desc_attr_handle = INVALID_ATTR_HANDLE; static uint16_t s_char_operation_char_handle = INVALID_ATTR_HANDLE; static uint16_t s_char_ssid_char_handle = INVALID_ATTR_HANDLE; static uint16_t s_char_password_char_handle = INVALID_ATTR_HANDLE; - +//helloyfia +static uint16_t s_char_cmd_char_handle = INVALID_ATTR_HANDLE; static uint16_t *const s_boarding_attr_handle_list[sizeof(s_gatts_attr_db_service_boarding) / sizeof(s_gatts_attr_db_service_boarding[0])] = { @@ -165,8 +177,47 @@ static uint16_t *const s_boarding_attr_handle_list[sizeof(s_gatts_attr_db_servic &s_char_operation_char_handle, &s_char_ssid_char_handle, &s_char_password_char_handle, + //helloyifa + &s_char_cmd_char_handle, }; +int ble_notify_boarding_state(boarding_state state){ + uint8_t str_respone [32] = {0}; + memset(str_respone,0,sizeof(str_respone)); + switch (state) + { + case BOARDING_STATE_IDLE: + strcat((char*)&str_respone,"AT+STATE=0"); + break; + case BOARDING_STATE_SCAN: + strcat((char*)&str_respone,"AT+STATE=1"); + break; + case BOARDING_STATE_SETWIFI: + strcat((char*)&str_respone,"AT+STATE=2"); + break; + case BOARDING_STATE_DONE: + strcat((char*)&str_respone,"AT+STATE=3"); + break; + case BOARDING_STATE_PASSWORD_ERROR: + strcat((char*)&str_respone,"AT+STATE=6"); + break; + case BOARDING_STATE_AP_NOFOUND: + strcat((char*)&str_respone,"AT+STATE=7"); + break; + default: + break; + } + + if(strlen((char*)&str_respone) > 0){ + os_printf("notify:%s\r\n",str_respone); + + wifi_boarding_notify(str_respone, strlen((char*)&str_respone)); + //uint8_t con_idx = s_conn_ind; + //bk_ble_send_noti_value(con_idx, strlen((char*)&str_respone), str_respone, PRF_TASK_ID_BOARDING, BOARDING_IDX_CHAR_VALUE); + } + return 0; +} + static int32_t dm_gatts_get_buff_from_attr_handle(bk_gatts_attr_db_t *attr_list, uint16_t *attr_handle_list, uint32_t size, uint16_t attr_handle, uint32_t *output_index, uint8_t **output_buff, uint32_t *output_size) { uint32_t i; @@ -384,6 +435,96 @@ static int32_t wifi_boarding_gatts_cb(bk_gatts_cb_event_t event, bk_gatt_if_t ga os_memcpy((uint8_t *)s_ble_boarding_info->password_value, param->value, param->len); wboard_logi("password: %s", s_ble_boarding_info->password_value); } + + + //锟截碉拷 + if (s_ble_boarding_info && s_ble_boarding_info->set_wifi_cb) + { + s_ble_boarding_info->set_wifi_cb(s_ble_boarding_info->ssid_value,s_ble_boarding_info->ssid_length,s_ble_boarding_info->password_value,s_ble_boarding_info->password_length); + } + } + //helloyifa + else if (s_char_cmd_char_handle == param->handle) + { + bk_ble_gatts_get_attr_value(param->handle, &buff_size, &tmp_buff); + + wboard_loge("state: %s", param->value); + + //锟斤拷锟斤拷锟斤拷锟杰碉拷锟斤拷指锟斤拷 + char* ptr; + if (NULL != (ptr = strstr((char *)param->value, "AT+SSID"))){ + wboard_loge("start scan wifi!!"); + //锟截碉拷 + if (s_ble_boarding_info && s_ble_boarding_info->scan_wifi_cb) + { + s_ble_boarding_info->scan_wifi_cb(); + } + } + if (NULL != (ptr = strstr((char *)param->value, "AT+SCAN"))){ + wboard_loge("start scan wifi!!"); + //锟截碉拷 + if (s_ble_boarding_info && s_ble_boarding_info->scan_wifi_cb) + { + s_ble_boarding_info->scan_wifi_cb(); + } + } + + if (NULL != (ptr = strstr((char *)param->value, "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); + wboard_loge("%s %s ",m_ssid,m_pwd); + //锟斤拷锟斤拷SSID + if(strlen(m_ssid) > 0){ + if (s_ble_boarding_info->ssid_value) + { + os_free(s_ble_boarding_info->ssid_value); + s_ble_boarding_info->ssid_value = NULL; + s_ble_boarding_info->ssid_length = 0; + } + + s_ble_boarding_info->ssid_length =strlen(m_ssid); + s_ble_boarding_info->ssid_value = os_malloc(strlen(m_ssid)+1); + + os_memset(s_ble_boarding_info->ssid_value, 0, strlen(m_ssid)+1); + os_memcpy((uint8_t *)s_ble_boarding_info->ssid_value, m_ssid, strlen(m_ssid)); + wboard_loge("ssid: %s", s_ble_boarding_info->ssid_value); + } + + if (s_ble_boarding_info->password_value) + { + os_free(s_ble_boarding_info->password_value); + s_ble_boarding_info->password_value = NULL; + s_ble_boarding_info->password_length = 0; + } + + if(strlen(m_pwd) > 0){ + s_ble_boarding_info->password_length = 0 ; + s_ble_boarding_info->password_value = ""; + }else{ + s_ble_boarding_info->password_length = strlen(m_pwd); + s_ble_boarding_info->password_value = os_malloc(strlen(m_pwd) + 1); + + os_memset(s_ble_boarding_info->password_value, 0, strlen(m_pwd) + 1); + os_memcpy((uint8_t *)s_ble_boarding_info->password_value, m_pwd, strlen(m_pwd)); + wboard_loge("password: %s", s_ble_boarding_info->password_value); + } + + //锟截碉拷 + if (s_ble_boarding_info && s_ble_boarding_info->set_wifi_cb) + { + //(char* ssid, int ssid_len ,char* password,int password_len); + s_ble_boarding_info->set_wifi_cb(m_ssid,strlen(m_ssid),m_pwd,strlen(m_pwd)); + } + + } + + + //wifi_boarding_notify(param->value,param->len); } else { @@ -533,6 +674,19 @@ static int32_t wifi_boarding_gatts_cb(bk_gatts_cb_event_t event, bk_gatt_if_t ga ); s_conn_ind = ~0; + + //helloyifa + //wifi_boarding_adv_stop(); + //wifi_boarding_adv_start(); + + const bk_ble_gap_ext_adv_t ext_adv = + { + .instance = 0, + .duration = 0, + .max_events = 0, + }; + + bk_ble_gap_adv_start(1, &ext_adv); } break; @@ -814,8 +968,12 @@ int wifi_boarding_deinit() void dm_ble_gap_get_identity_addr(uint8_t *addr) { + // + //uint8_t base_mac[BK_MAC_ADDR_LEN] = {0}; + //bk_get_mac(base_mac, MAC_TYPE_BASE); + // uint8_t *identity_addr = addr; - bk_get_mac((uint8_t *)identity_addr, MAC_TYPE_BLUETOOTH); + bk_get_mac((uint8_t *)identity_addr, MAC_TYPE_BASE); for (int i = 0; i < BK_BD_ADDR_LEN / 2; i++) { @@ -835,11 +993,11 @@ int wifi_boarding_adv_start(void) dm_ble_gap_get_identity_addr(identity_addr); os_memcpy(current_addr, identity_addr, sizeof(identity_addr)); + wboard_loge("BT MAC %02x %02x %02x %02x %02x %02x ", current_addr[0],current_addr[1],current_addr[2],current_addr[3],current_addr[4],current_addr[5]); + //current_addr[5] |= 0xc0; + //current_addr[0]++; - current_addr[5] |= 0xc0; - current_addr[0]++; - - snprintf((char *)(adv_name), sizeof(adv_name) - 1, "bk_genie-%02X%02X%02X", current_addr[2], current_addr[1], current_addr[0]); + snprintf((char *)(adv_name), sizeof(adv_name) - 1, "iR58_%02X%02X",current_addr[1], current_addr[0]); wboard_logi("adv name %s", adv_name); diff --git a/bk_aidk/projects/beken_wss_paopao/main/include/beken_rtc.h b/bk_aidk/projects/beken_wss_paopao/main/include/beken_rtc.h index 0e4a6b20..29e57f00 100755 --- a/bk_aidk/projects/beken_wss_paopao/main/include/beken_rtc.h +++ b/bk_aidk/projects/beken_wss_paopao/main/include/beken_rtc.h @@ -25,6 +25,8 @@ enum MsgType { BEKEN_RTC_SEND_ABORT = 3, BEKEN_RTC_SEND_STOP_LISTEN = 4, BEKEN_RTC_SEND_START_LISTEN = 5, + + BEKEN_RTC_SEND_IOT_DESC = 6, }; /* @@ -134,6 +136,7 @@ void rtc_websocket_parse_text(text_info_t *text, cJSON *root); void rtc_fill_audio_info(audio_info_t *info, char *type, uint32_t adc_rate, uint32_t dac_rate, uint32_t enc_ms, uint32_t dec_ms, uint32_t size); int bk_rtc_video_data_send(const uint8_t *data_ptr, size_t data_len, const video_frame_info_t *info_ptr); bk_err_t bk_rtc_register_video_rx_handle(rtc_video_rx_data_handle video_rx_handle); + #ifdef __cplusplus } #endif diff --git a/bk_aidk/projects/beken_wss_paopao/main/include/bk_genie_comm.h b/bk_aidk/projects/beken_wss_paopao/main/include/bk_genie_comm.h index 22dd3ea8..569c11f6 100755 --- a/bk_aidk/projects/beken_wss_paopao/main/include/bk_genie_comm.h +++ b/bk_aidk/projects/beken_wss_paopao/main/include/bk_genie_comm.h @@ -42,6 +42,8 @@ typedef enum { + //helloyifa + DBEVT_WIFI_STATION_SCAN, DBEVT_WIFI_STATION_CONNECT, DBEVT_WIFI_STATION_CONNECTED, DBEVT_WIFI_STATION_DISCONNECTED, diff --git a/bk_aidk/projects/beken_wss_paopao/main/include/bk_genie_smart_config.h b/bk_aidk/projects/beken_wss_paopao/main/include/bk_genie_smart_config.h index 77b6d6c9..7af1e655 100755 --- a/bk_aidk/projects/beken_wss_paopao/main/include/bk_genie_smart_config.h +++ b/bk_aidk/projects/beken_wss_paopao/main/include/bk_genie_smart_config.h @@ -66,4 +66,6 @@ int bk_genie_get_agent_info(bk_genie_agent_info_t *info); void bk_genie_prepare_for_smart_config(void); int bk_genie_wakeup_agent(void); int bk_genie_is_net_pan_configured(void); +//helloyifa +void be_set_smart_config_running(); #endif diff --git a/bk_aidk/projects/beken_wss_paopao/main/include/wifi_boarding_utils.h b/bk_aidk/projects/beken_wss_paopao/main/include/wifi_boarding_utils.h index fba80fa1..0c0327be 100755 --- a/bk_aidk/projects/beken_wss_paopao/main/include/wifi_boarding_utils.h +++ b/bk_aidk/projects/beken_wss_paopao/main/include/wifi_boarding_utils.h @@ -21,7 +21,34 @@ typedef enum BOARDING_OP_START_AGENT_FROM_DEV = 16, } boarding_opcode_t; +typedef enum boarding_state +{ + BOARDING_STATE_IDLE, //0 无操作 + BOARDING_STATE_SCAN, //1 正在扫描SSID + BOARDING_STATE_SETWIFI, //2 正在设置SSID PWD + BOARDING_STATE_DONE, //3 已经连接WIFI + BOARDING_STATE_FAIL, //4 失败 + BOARDING_STATE_CLOSE, //5 蓝牙已经关闭 + BOARDING_STATE_PASSWORD_ERROR, //6 密码错误 + BOARDING_STATE_AP_NOFOUND //7 AP 不存在 +} boarding_state; + +typedef struct ble_boarding_cb_ops{ + /** + * 小程序发送AT+START 触发开始扫描wifi + * 回调prf_id att_idx用于回传WIFI列表 + */ + void (*event_start_boarding)(); + void (*event_wifi_info)(char* ssid, int ssid_len ,char* password,int password_len); + void (*event_agora_info)(char* license, int license_len); +}ble_boarding_cb_ops_t; + typedef void (*ble_boarding_op_cb_t)(uint16_t opcode, uint16_t length, uint8_t *data); +//开始扫描wifi +typedef void (*ble_boarding_scan_wifi_cb_t)(); +//接收到APP下发的配网信息 +typedef void (*ble_boarding_set_wifi_cb_t)(char* ssid, int ssid_len ,char* password,int password_len); + typedef struct { char *ssid_value; @@ -30,6 +57,9 @@ typedef struct uint8_t boarding_notify[2]; uint16_t ssid_length; uint16_t password_length; + //helloyifa + ble_boarding_scan_wifi_cb_t scan_wifi_cb; + ble_boarding_set_wifi_cb_t set_wifi_cb; } ble_boarding_info_t; int wifi_boarding_init(ble_boarding_info_t *info); @@ -37,4 +67,5 @@ int wifi_boarding_deinit(); int wifi_boarding_adv_start(void); int wifi_boarding_adv_stop(void); int wifi_boarding_notify(uint8_t *data, uint16_t length); - +//helloyifa +int ble_notify_boarding_state(boarding_state state); 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 new file mode 100755 index 00000000..7bf6016e --- /dev/null +++ b/bk_aidk/projects/beken_wss_paopao/main/iot/iot_lamp.c @@ -0,0 +1,135 @@ +#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" + +#define TAG "iot_lamp" +#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__) + +/* + [{ + * "name": "Screen", + * "description": "杩欐槸灏忕墰鐗涚殑鐏厜鐨勪寒搴", + * "properties": { + * "theme": { + * "description": "涓婚", + * "type": "string" + * }, + * "brightness": { + * "description": "褰撳墠浜害鐧惧垎姣", + * "type": "number" + * }}, + * "methods": { + * "SetTheme": { + * "description": "璁剧疆灞忓箷涓婚", + * "parameters": { + * "theme_name": { + * "description": "涓婚妯″紡, light 鎴 dark", + * "type": "string" + * } + * } + * }, + * "SetBrightness": { + * "description": "璁剧疆浜害", + * "parameters": { + * "brightness": { + * "description": "0鍒100涔嬮棿鐨勬暣鏁", + * "type": "number" + * } + * } + * } + * } + * }] +*/ + +#define IOT_LAMP_DEVICE_NAME "lamp" +#define IOT_LAMP_DEVICE_FUNS_SET_BRIGHTNESS "SetBrightness" + +cJSON* iot_lamp_get_device_desc(){ + + cJSON *lamp_desc = cJSON_CreateObject(); + cJSON_AddStringToObject(lamp_desc, "name", IOT_LAMP_DEVICE_NAME); + cJSON_AddStringToObject(lamp_desc, "description", "杩欐槸灏忕墰鐗涚殑鐏厜"); + + cJSON *properties = cJSON_CreateObject(); + //瀹氫箟brightness 灞炴 + cJSON *brightness = cJSON_CreateObject(); + cJSON_AddStringToObject(brightness, "description", "褰撳墠浜害鐧惧垎姣"); + cJSON_AddStringToObject(brightness, "type", "number"); + //娣诲姞brightness灞炴у埌properties + cJSON_AddItemToObjectCS(properties,"brightness",brightness); + //娣诲姞properties 鍒發amp_desc 鎻忚堪 + cJSON_AddItemToObjectCS(lamp_desc,"properties",properties); + + + //瀹氫箟鏂规硶methods + cJSON *methods = cJSON_CreateObject(); + + + + //瀹氫箟涓涓缃寒搴︾殑鏂规硶 + /*"SetBrightness": { + "description": "璁剧疆浜害", + "parameters": { + "brightness": { + "description": "0鍒100涔嬮棿鐨勬暣鏁", + "type": "number" + } + } + } + */ + cJSON *SetBrightness = cJSON_CreateObject(); + cJSON_AddStringToObject(SetBrightness, "description", "璁剧疆浜害"); + + cJSON *parameters = cJSON_CreateObject(); + cJSON *p_brightness = cJSON_CreateObject(); + cJSON_AddStringToObject(p_brightness, "type", "number"); + cJSON_AddStringToObject(p_brightness, "description", "0鍒100涔嬮棿鐨勬暣鏁"); + + + cJSON_AddItemToObjectCS(parameters,"brightness",p_brightness); + cJSON_AddItemToObjectCS(SetBrightness,"parameters",parameters); + + //cJSON_AddStringToObject(SetBrightness, "type", "number"); + cJSON_AddItemToObjectCS(methods,IOT_LAMP_DEVICE_FUNS_SET_BRIGHTNESS,SetBrightness); + cJSON_AddItemToObjectCS(lamp_desc,"methods",methods); + + //鎵撳嵃涓涓嬫弿杩 + //char *json_desc = cJSON_Print(lamp_desc); + //LOGE("lamp_desc-->%s\n",json_desc); + //cJSON_Delete(lamp_desc); + return lamp_desc; +} + +void iot_lamp_report_state(int brightness){ + +} + +void iot_lamp_parser_invoke(char* cmd,char * paramters_json){ + + //璁剧疆浜害 + if (strcmp(cmd, "SetBrightness") == 0) + { + //鑾峰彇浜害鍊 + cJSON* params = cJSON_Parse(paramters_json); + if(params == NULL) return; + // {"brightness": 60} + int brightness = cJSON_GetObjectItem(params, "brightness")->valueint; + LOGE("invoke brightness-->%d\n",brightness); + cJSON_Delete(params); + } + +} 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 new file mode 100755 index 00000000..ca71e9a6 --- /dev/null +++ b/bk_aidk/projects/beken_wss_paopao/main/iot/iot_lamp.h @@ -0,0 +1,26 @@ +/************************************************************* + * + * Copyright (C) POKA + * All rights reserved. + * + *************************************************************/ +#ifndef __IOT_LAMP_H__ +#define __IOT_LAMP_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +//#include + +//#include "beken_config.h" +//#include "bk_websocket_client.h" +#include "cJSON.h" + +cJSON * iot_lamp_get_device_desc(); +void iot_lamp_parser_invoke(char* cmd,char * paramters_json); +#ifdef __cplusplus +} +#endif +#endif /* __AGORA_RTC_H__ */ + 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 new file mode 100755 index 00000000..42320fdb --- /dev/null +++ b/bk_aidk/projects/beken_wss_paopao/main/iot/iot_sleep_helper.c @@ -0,0 +1,128 @@ +#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 +#include +#include "pwm_hal.h" + +#include +#include "gpio_driver.h" + +#include "iot_sleep_helper.h" + +#define TAG "sleep_helper" +#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__) + +/* +*PWM 閫氶亾 +*/ +static uint32_t pwm_chan = 1; + +/** + * 妯″紡1 鍗曞懆鏈熸椂搴忚〃 + */ +static mode_info_t model_1[7] = { + {0, 2000}, + {1, 2000}, + {0, 2000}, + {-1, 2000}, + {1, 2000}, + {0, 2000}, + {-1, 2000}, +}; + + + +static int helper_start = 0; +//static beken_queue_t sleep_helper_msg_que = NULL; +static beken_thread_t sleep_helper_thread_hdl = NULL; + +typedef struct +{ + sleep_mode model; + sleep_model_level level; +} sleep_msg_t; + +static void sleep_helper_thread(void *arg) +{ + while (helper_start) + { + LOGE("doing...\n"); + for (int i = 0; i < 7; i++) + { + int dir = model_1[i].dir; + int delay = model_1[i].delay_ms; + LOGE("dir:%d delay:%d\n", dir, delay); + //璁剧疆鏂瑰悜 + switch(dir){ + case 1: + bk_gpio_set_output_high(GPIO_44); + bk_gpio_set_output_low(GPIO_45); + break; + case 0: + bk_gpio_set_output_low(GPIO_44); + bk_gpio_set_output_low(GPIO_45); + break; + case -1: + bk_gpio_set_output_low(GPIO_44); + bk_gpio_set_output_high(GPIO_45); + break; + } + rtos_delay_milliseconds(delay); + } + } + +} + +void sleep_helper_init(){ + bk_pwm_driver_init(); + + //GPIO + gpio_dev_unmap(GPIO_44); + gpio_dev_unmap(GPIO_45); + bk_gpio_enable_output(44); + bk_gpio_enable_output(45); + + pwm_init_config_t config = {0}; + config.period_cycle = 200; + config.duty_cycle = 100; + bk_pwm_init(pwm_chan, &config); + bk_pwm_start(pwm_chan); +} + +void sleep_helper_set_mode(sleep_mode mode){ + //model 1 + helper_start = 1; + + bk_err_t ret = BK_OK; + 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_level(sleep_model_level level){ + +} +void sleep_helper_deinit(){ + +} \ No newline at end of file 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 new file mode 100755 index 00000000..0459c6c6 --- /dev/null +++ b/bk_aidk/projects/beken_wss_paopao/main/iot/iot_sleep_helper.h @@ -0,0 +1,60 @@ +/************************************************************* + * + * Copyright (C) POKA + * All rights reserved. + * + *************************************************************/ +#ifndef __IOT_SLEEP_HELPER_H__ +#define __IOT_SLEEP_HELPER_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "cJSON.h" + +/** + * 3绉嶆ā寮 + */ +typedef enum sleep_mode +{ + SLEEP_MODE_1 = 1, + SLEEP_MODE_2, + SLEEP_MODE_3, +} sleep_mode; + +/** + * 9涓瓑绾 + */ +typedef enum sleep_level +{ + SLEEP_MODEL_LEVEL_1 = 1, + SLEEP_MODEL_LEVEL_2, + SLEEP_MODEL_LEVEL_3, +} sleep_model_level; + +typedef struct +{ + /** + * 杈撳嚭鏂瑰悜 1 楂 0 鍩哄噯 -1 璐熺數鍘 + */ + int dir; //1,0,-1 + /** + *寤舵椂鏃堕棿 ms + */ + int delay_ms; +} mode_info_t; + + +void sleep_helper_init(); +void sleep_helper_set_mode(sleep_mode mode); +void sleep_helper_set_level(sleep_model_level level); +void sleep_helper_deinit(); + +cJSON * iot_sleep_helper_get_device_desc(); +void iot_lamp_parser_invoke(char* cmd,char * paramters_json); +#ifdef __cplusplus +} +#endif +#endif /* __IOT_SLEEP_HELPER_H__ */ + diff --git a/bk_aidk/projects/beken_wss_paopao/main/iot/iot_speaker.c b/bk_aidk/projects/beken_wss_paopao/main/iot/iot_speaker.c new file mode 100755 index 00000000..1144369d --- /dev/null +++ b/bk_aidk/projects/beken_wss_paopao/main/iot/iot_speaker.c @@ -0,0 +1,224 @@ +#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_speaker.h" + +#if (CONFIG_SYS_CPU0) +#include "beken_config.h" +#include "aud_intf.h" +#include "bk_factory_config.h" +#if CONFIG_NETWORK_AUTO_RECONNECT +#include "bk_genie_smart_config.h" +#endif +#include "motor.h" +#endif + +#define TAG "iot_speaker" +#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__) + + +#if (CONFIG_SYS_CPU0) +static uint32_t volume = 7; // volume level, not gain. +static uint32_t g_volume_gain[SPK_VOLUME_LEVEL] = {0, 0x05, 0x18, 0x10, 0x13, 0x15, 0x18, 0x21, 0x22, 0x25, 0x30}; +void speaker_volume_init(void) +{ + int volume_size = bk_config_read("volume", (void *)&volume, 4); + if (volume_size != 4) + { + BK_LOGE(TAG, "read volume config fail, use default config volume_size:%d\n", volume_size); + } + if (volume > (SPK_VOLUME_LEVEL - 1)) + { + volume = SPK_VOLUME_LEVEL - 1; + if (0 != bk_config_write("volume", (void *)&volume, 4)) + { + BK_LOGE(TAG, "storage volume: %d fail\n", volume); + } + } + + /* SPK_GAIN_MAX * [(exp(i/(SPK_VOLUME_LEVEL-1)-1)/(exp(1)-1)] */ + //uint32_t step[SPK_VOLUME_LEVEL] = {0, 6, 12, 20, 28, 37, 47, 58, 71, 84, 100}; + //for (uint32_t i = 0; i < SPK_VOLUME_LEVEL; i++) + //{ + //g_volume_gain[i] = SPK_GAIN_MAX * step[i] / 100; + //BK_LOGE(TAG, "g_volume_gain: %02X \n", g_volume_gain[i]); + //} +} + +void speaker_volume_increase() +{ + BK_LOGI(TAG, " volume up\r\n"); + if (volume == (SPK_VOLUME_LEVEL - 1)) + { + BK_LOGI(TAG, "volume have reached maximum volume: %d\n", SPK_GAIN_MAX); + return; + } + if (BK_OK == bk_aud_intf_set_spk_gain(g_volume_gain[volume + 1])) + { + volume += 1; + if (0 != bk_config_write("volume", (void *)&volume, 4)) + { + BK_LOGE(TAG, "storage volume: %d fail\n", volume); + } + BK_LOGI(TAG, "current volume: %d\n", volume); + } + else + { + BK_LOGI(TAG, "set volume fail\n"); + } +} + +void speaker_volume_decrease() +{ + BK_LOGI(TAG, " volume down\r\n"); + if (volume == 0) + { + BK_LOGI(TAG, "volume have reached minimum volume: 0\n"); + return; + } + if (BK_OK == bk_aud_intf_set_spk_gain(g_volume_gain[volume - 1])) + { + volume -= 1; + if (0 != bk_config_write("volume", (void *)&volume, 4)) + { + BK_LOGE(TAG, "storage volume: %d fail\n", volume); + } + BK_LOGI(TAG, "current volume: %d\n", volume); + } + else + { + BK_LOGI(TAG, "set volume fail\n"); + } +} + +void speaker_volume_set_abs(uint8_t level, uint8_t has_precision) +{ + bk_err_t ret = 0; + + BK_LOGI(TAG, "%s volume abs %d %d\n", __func__, level, has_precision); + + if (level > SPK_VOLUME_LEVEL - 1) + { + BK_LOGE(TAG, "%s invalid level %d >= %d\n", __func__, level, SPK_VOLUME_LEVEL); + level = SPK_VOLUME_LEVEL - 1; + } + + if (level == 0 && has_precision) + { + BK_LOGW(TAG, "%s set raw gain 2 because precision\n", __func__); + ret = bk_aud_intf_set_spk_gain(2); + } + else + { + ret = bk_aud_intf_set_spk_gain(g_volume_gain[level]); + } + + if (BK_OK == ret) + { + volume = level; + + if (0 != bk_config_write("volume", (void *)&volume, sizeof(volume))) + { + BK_LOGE(TAG, "%s storage volume: %d fail\n", __func__, level); + } + + BK_LOGI(TAG, "%s current volume: %d\n", __func__, level); + } + else + { + BK_LOGE(TAG, "%s set volume %d fail\n", __func__, level); + } +} + +uint32_t speaker_volume_get_current() +{ + return volume; +} + +uint32_t speaker_volume_get_level_count() +{ + return SPK_VOLUME_LEVEL; +} +#endif + + +cJSON* iot_speaker_get_device_desc() +{ + cJSON *speaker_desc = cJSON_CreateObject(); + cJSON_AddStringToObject(speaker_desc, "name", "speaker"); + cJSON_AddStringToObject(speaker_desc, "description", "鎵0鍣 闊抽噺璋冭妭"); + + cJSON *properties = cJSON_CreateObject(); + //volume 灞炴 + cJSON *volume = cJSON_CreateObject(); + cJSON_AddStringToObject(volume, "description", "褰撳墠璁惧闊抽噺"); + cJSON_AddStringToObject(volume, "type", "number"); + //娣诲姞brightness灞炴у埌properties + cJSON_AddItemToObjectCS(properties,"volume",volume); + //娣诲姞properties speaker_desc 鎻忚堪 + cJSON_AddItemToObjectCS(speaker_desc,"properties",properties); + + + //瀹氫箟鏂规硶methods + cJSON *methods = cJSON_CreateObject(); + cJSON *setVolume = cJSON_CreateObject(); + cJSON_AddStringToObject(setVolume, "description", "璁剧疆闊抽噺"); + + cJSON *parameters = cJSON_CreateObject(); + cJSON *p_volume = cJSON_CreateObject(); + cJSON_AddStringToObject(p_volume, "type", "number"); + cJSON_AddStringToObject(p_volume, "description", "0鍒100涔嬮棿鐨勬暣鏁"); + + + cJSON_AddItemToObjectCS(parameters,"volume",p_volume); + cJSON_AddItemToObjectCS(setVolume,"parameters",parameters); + + //cJSON_AddStringToObject(SetBrightness, "type", "number"); + cJSON_AddItemToObjectCS(methods,"setVolume",setVolume); + cJSON_AddItemToObjectCS(speaker_desc,"methods",methods); + + //鎵撳嵃涓涓嬫弿杩 + //char *json_desc = cJSON_Print(speaker_desc); + //LOGE("speaker_desc-->%s\n",json_desc); + //cJSON_Delete(speaker_desc); + return speaker_desc; +} + + +void iot_speaker_parser_invoke(char* cmd,char * paramters_json){ + + LOGE("iot_speaker_parser_invoke :%s\n",paramters_json); + //璁剧疆闊抽噺 + if (strcmp(cmd, "setVolume") == 0) + { + //鑾峰彇浜害鍊 + cJSON* params = cJSON_Parse(paramters_json); + if(params == NULL){ + LOGE("params == NULL\n"); + return; + } + // {"volume": 60} + int volume = cJSON_GetObjectItem(params, "volume")->valueint; + LOGE("volume :%d\n",volume); + if(volume > 10){ + int level = volume/10; + LOGE("speaker_setVolume--> volume :%d level:%d\n",volume,level); + speaker_volume_set_abs(level,0); + } + cJSON_Delete(params); + } + +} \ No newline at end of file diff --git a/bk_aidk/projects/beken_wss_paopao/main/iot/iot_speaker.h b/bk_aidk/projects/beken_wss_paopao/main/iot/iot_speaker.h new file mode 100755 index 00000000..98759810 --- /dev/null +++ b/bk_aidk/projects/beken_wss_paopao/main/iot/iot_speaker.h @@ -0,0 +1,15 @@ +#ifndef __IOT_SPEAKER_H__ +#define __IOT_SPEAKER_H__ +#ifdef __cplusplus +extern "C" { +#endif +#include "cJSON.h" + +void speaker_volume_init(); + +cJSON* iot_speaker_get_device_desc(); +void iot_speaker_parser_invoke(char* cmd,char * paramters_json); +#ifdef __cplusplus +} +#endif +#endif \ No newline at end of file diff --git a/bk_aidk/projects/beken_wss_paopao/main/iot/test.txt b/bk_aidk/projects/beken_wss_paopao/main/iot/test.txt new file mode 100755 index 00000000..ac666a81 --- /dev/null +++ b/bk_aidk/projects/beken_wss_paopao/main/iot/test.txt @@ -0,0 +1,72 @@ +/** + * IoT璁惧鐩稿叧娑堟伅 + * + { + "session_id": "<浼氳瘽ID>", + "type": "iot", + "descriptors": <璁惧鎻忚堪JSON> + } + +{ + "session_id": "<浼氳瘽ID>", + "type": "iot", + "states": <鐘舵丣SON> +} + +/** + * descriptorMessage鐨刯son涓 + * [{ + * "name": "Screen", + * "description": "杩欐槸涓涓睆骞曪紝鍙缃富棰樺拰浜害", + * "properties": { + * "theme": { + * "description": "涓婚", + * "type": "string" + * }, + * "brightness": { + * "description": "褰撳墠浜害鐧惧垎姣", + * "type": "number" + * }}, + * "methods": { + * "SetTheme": { + * "description": "璁剧疆灞忓箷涓婚", + * "parameters": { + * "theme_name": { + * "description": "涓婚妯″紡, light 鎴 dark", + * "type": "string" + * } + * } + * }, + * "SetBrightness": { + * "description": "璁剧疆浜害", + * "parameters": { + * "brightness": { + * "description": "0鍒100涔嬮棿鐨勬暣鏁", + * "type": "number" + * } + * } + * } + * } + * }] + * + * + * statesMessage鐨刯son涓 + * [ + * { + * "name": "Speaker", + * "state": { + * "volume": 70 + * } + * }, + * { + * "name": "Screen", + * "state": { + * "theme": "dark", + * "brightness": 75 + * } + * } + * ] + * + * @throws Exception + */ + \ No newline at end of file diff --git a/bk_aidk/projects/beken_wss_paopao/main/iot/thing.c b/bk_aidk/projects/beken_wss_paopao/main/iot/thing.c new file mode 100755 index 00000000..ab53f05d --- /dev/null +++ b/bk_aidk/projects/beken_wss_paopao/main/iot/thing.c @@ -0,0 +1,125 @@ +#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 "thing.h" +#include "iot_lamp.h" +#include "iot_speaker.h" +#include "iot_sleep_helper.h" +#define TAG "thing" +#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__) + + +/** + * 鐢熸垚IOT 鎻忚堪鏂囦欢 鐢ㄤ簬鎰忓浘璇嗗埆鎺у埗 + */ +void thing_init() +{ + #ifdef IOT_SPEAKER + speaker_volume_init(); + #endif + + #ifdef IOT_SLEEP_HELPER + sleep_helper_init(); + sleep_helper_set_mode(SLEEP_MODE_1); + #endif +} + +cJSON* thing_gen_iot_descriptors(char *session_id) +{ + // 1.鍒ゆ柇鏁版嵁鍚堟硶 + // 2.鍙戦佺粰鏈嶅姟鍣 + cJSON *messageRoot = cJSON_CreateObject(); + cJSON_AddStringToObject(messageRoot, "session_id", session_id); + cJSON_AddStringToObject(messageRoot, "type", "iot"); + cJSON_AddBoolToObject(messageRoot, "update", true); + + cJSON *descriptorArray = cJSON_CreateArray(); + + #ifdef IOT_LAMP + cJSON* lamp_desc = iot_lamp_get_device_desc(); + //cJSON_AddItemToArray(descriptorArray, cJSON_Duplicate(descriptor, 1)); + cJSON_AddItemToArray(descriptorArray, lamp_desc); + #endif + + #ifdef IOT_SPEAKER + cJSON* speaker_desc = iot_speaker_get_device_desc(); + //cJSON_AddItemToArray(descriptorArray, cJSON_Duplicate(descriptor, 1)); + cJSON_AddItemToArray(descriptorArray, speaker_desc); + #endif + + cJSON_AddItemToObject(messageRoot, "descriptors", descriptorArray); + return messageRoot; +} + +/** + * 瑙f瀽骞冲彴涓嬪彂鐨処OT鎸囦护,鑾峰彇鍝嶅簲鐨勫弬鏁拌皟鐢ㄤ笉鍚岀殑纭欢鍔熻兘 + * 鐗╄仈缃戞寚浠: + * + * { + "type": "iot", + "commands": [ + { + "name": "lamp", + "method": "SetBrightness", + "parameters": { + "brightness": 50 + } + } + ] +} +*/ + +void thing_iot_invoke(char *iot_json){ + cJSON* iot_invoke = cJSON_Parse(iot_json); + cJSON *commands = cJSON_GetObjectItem(iot_invoke, "commands"); + // 妫鏌ユ槸鍚︿负鏁扮粍绫诲瀷 + if (commands ==NULL && !cJSON_IsArray(commands)) { + cJSON_Delete(iot_invoke); + return ; + } + // 閬嶅巻鏁扮粍骞舵墦鍗板厓绱 + int arraySize = cJSON_GetArraySize(commands); + for (int i = 0; i < arraySize; i++) { + cJSON *item = cJSON_GetArrayItem(commands, i); + char *name = cJSON_GetObjectItem(item, "name")->valuestring; + char *method = cJSON_GetObjectItem(item, "method")->valuestring; + + cJSON *parameters = cJSON_GetObjectItem(item, "parameters"); + char *str_parameters =cJSON_PrintUnformatted(parameters); + LOGE("cmd:%s method:%s\n",name,method); + //鐏厜鎸囦护 + if (strcmp(name, "lamp") == 0) + { + iot_lamp_parser_invoke(method,str_parameters); + } + + //鎵0鍣ㄦ寚浠 + if (strcmp(name, "speaker") == 0) + { + iot_speaker_parser_invoke(method,str_parameters); + } + } + // 閲婃斁鍐呭瓨 + cJSON_Delete(iot_invoke); +} + +// AddStringProperty +// AddNumberProperty +// AddBooleanProperty +// AddMethod + + diff --git a/bk_aidk/projects/beken_wss_paopao/main/iot/thing.h b/bk_aidk/projects/beken_wss_paopao/main/iot/thing.h new file mode 100755 index 00000000..8a8a656a --- /dev/null +++ b/bk_aidk/projects/beken_wss_paopao/main/iot/thing.h @@ -0,0 +1,51 @@ +#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" + + +#define IOT_LAMP 1 +#define IOT_SPEAKER 1 +#define IOT_SLEEP_HELPER 1 + +void thing_init(); + +cJSON * thing_create(char * name,char *description); + +/* +"theme": { + "description": "涓婚", + "type": "string" + },*/ +cJSON * thing_add_string_property(char * name,char *description); +/* +"theme": { + "description": "涓婚", + "type": "string" + },*/ +cJSON * thing_add_number_property(char * name,char *description); + +//AddStringProperty +//AddNumberProperty +//AddBooleanProperty +//AddMethod + + +/** + * 鐢熸垚IOT 鎻忚堪鏂囦欢 鐢ㄤ簬鎰忓浘璇嗗埆鎺у埗 + */ +cJSON* thing_gen_iot_descriptors(char *session_id); + +/** + * 瑙f瀽骞冲彴涓嬪彂鐨処OT鎸囦护,鑾峰彇鍝嶅簲鐨勫弬鏁拌皟鐢ㄤ笉鍚岀殑纭欢鍔熻兘 + */ +void thing_iot_invoke(char *iot_json); \ No newline at end of file diff --git a/bk_aidk/projects/beken_wss_paopao/main/smart_config/bk_genie_smart_config.c b/bk_aidk/projects/beken_wss_paopao/main/smart_config/bk_genie_smart_config.c index 64fb2666..ae77ff04 100755 --- a/bk_aidk/projects/beken_wss_paopao/main/smart_config/bk_genie_smart_config.c +++ b/bk_aidk/projects/beken_wss_paopao/main/smart_config/bk_genie_smart_config.c @@ -868,6 +868,9 @@ void event_handler_init(void) } extern bk_err_t beken_rtc_stop(void); +void be_set_smart_config_running(){ + smart_config_running = true; +} void bk_genie_prepare_for_smart_config(void) { smart_config_running = true; @@ -920,9 +923,13 @@ int bk_genie_smart_config_init(void) if (flag != 0x74l) #endif { - bk_bluetooth_deinit(); + //bk_bluetooth_deinit(); } } + //helloyifa + //smart_config_running = true; + bk_genie_boarding_init(); + wifi_boarding_adv_start(); return 0; } 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 865201a5..b51aa0e9 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 @@ -32,6 +32,8 @@ #include "app_event.h" #include "cJSON.h" #include "audio_process.h" +#include "thing.h" + #define TAG "WS_MAIN" #define LOGI(...) BK_LOGI(TAG, ##__VA_ARGS__) #define LOGE(...) BK_LOGE(TAG, ##__VA_ARGS__) @@ -542,12 +544,22 @@ void rtc_websocket_msg_handle(char *json_text, unsigned int size) app_event_send_msg(APP_EVT_AGENT_JOINED, 0); smart_config_running = false; __get_beken_rtc()->disconnecting_state = 0; + + //鍙戦佽澶嘔OT鑳藉姏鎻忚堪鍒版湇鍔″櫒 + //rtc_websocket_send_text(client, "", BEKEN_RTC_SEND_IOT_DESC); + } else { LOGE("join WebSocket server fail\r\n"); } } + //瑙f瀽鏈嶅姟鍣ㄤ笅鍙戠殑IOT 鎸囦护 + else if (strcmp(type->valuestring, "iot") == 0) + { + thing_iot_invoke(json_text); + } + // else if ((strcmp(type->valuestring, "reply_text") == 0) || (strcmp(type->valuestring, "request_text") == 0)) { // text_info_t info = {0}; // info.text_type = (strcmp(type->valuestring, "request_text") == 0) ? 0:1; @@ -676,6 +688,7 @@ void sent_abort_msg() } } + // 妫鏌ヤ簩杩涘埗鏁版嵁鎺ユ敹鐘舵佺殑鎺ュ彛鍑芥暟 uint8_t get_binary_data_status(void) { @@ -748,6 +761,9 @@ void rtc_websocket_event_handler(void *event_handler_arg, char *event_base, int3 rtc_websocket_send_text(client, (void *)(&audio_info), BEKEN_RTC_SEND_HELLO); rtos_delay_milliseconds(100); rtc_websocket_send_text(client, (void *)(&audio_info), BEKEN_RTC_SEND_START); + + rtos_delay_milliseconds(100); + rtc_websocket_send_text(client, (void *)(&audio_info), BEKEN_RTC_SEND_IOT_DESC); // rtc_websocket_send_text(client, (void *)(&audio_info), BEKEN_RTC_SEND_START_LISTEN); // rtos_delay_milliseconds(200); // rtc_websocket_send_text(client, (void *)(&audio_info), BEKEN_RTC_SEND_LISTEN); @@ -800,7 +816,7 @@ void beken_rtc_main(void) bk_printf("headers = %s\r\n", headers); websocket_cfg.headers = headers; websocket_cfg.uri = "wss://xiaozhi.xa-poka.com/websocket/xiaozhi/v1/"; - // websocket_cfg.uri = "ws://43.139.216.160:8000/xiaozhi/v1/"; + //websocket_cfg.uri = "ws://43.139.216.160:8000/xiaozhi/v1/"; websocket_cfg.ws_event_handler = rtc_websocket_event_handler; rtc_session *rtc_session = rtc_websocket_create(&websocket_cfg, rtc_user_audio_rx_data_handle, &audio_info); if (rtc_session == NULL)