This commit is contained in:
dianke3yu 2025-05-20 11:13:59 +08:00
commit 440e1addd4
22 changed files with 1252 additions and 49 deletions

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -13,7 +13,7 @@
#include <driver/aon_rtc.h>
#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 对象
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> JSON <20><><EFBFBD><EFBFBD>
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", "С<EFBFBD><EFBFBD>ͬѧ");
// 将 cJSON 对象转换为字符串
// <EFBFBD><EFBFBD> cJSON <20><><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Ϊ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
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 对象以释放内存
// ɾ<EFBFBD><EFBFBD> cJSON <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7>ڴ<EFBFBD>
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字段
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD>session_id<EFBFBD>ֶ<EFBFBD>
cJSON_AddStringToObject(startlistent, "session_id", "");
// 添加type字段
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD>type<EFBFBD>ֶ<EFBFBD>
cJSON_AddStringToObject(startlistent, "type", "listen");
// 添加state字段
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD>state<EFBFBD>ֶ<EFBFBD>
cJSON_AddStringToObject(startlistent, "state", "start");
// 添加mode字段
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD>mode<EFBFBD>ֶ<EFBFBD>
cJSON_AddStringToObject(startlistent, "mode", "auto") ;
// 生成JSON字符串
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD>JSON<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>
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字段
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD>session_id<EFBFBD>ֶ<EFBFBD>
cJSON_AddStringToObject(stoplistent, "session_id", "");
// 添加type字段
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD>type<EFBFBD>ֶ<EFBFBD>
cJSON_AddStringToObject(stoplistent, "type", "listen");
// 添加state字段
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD>state<EFBFBD>ֶ<EFBFBD>
cJSON_AddStringToObject(stoplistent, "state", "stop");
// 添加mode字段
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD>mode<EFBFBD>ֶ<EFBFBD>
cJSON_AddStringToObject(stoplistent, "mode", "auto") ;
// 生成JSON字符串
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD>JSON<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>
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 对象
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> JSON <20><><EFBFBD><EFBFBD>
cJSON *startMessage = cJSON_CreateObject();
if (startMessage == NULL) {
}
// 添加 "type" 字段
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD> "type" <20>ֶ<EFBFBD>
cJSON_AddStringToObject(startMessage, "type", "start");
// 添加 "mode" 字段
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD> "mode" <20>ֶ<EFBFBD>
cJSON_AddStringToObject(startMessage, "mode", "auto");
// 创建 "audio_params" JSON 对象
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD> "audio_params" JSON <20><><EFBFBD><EFBFBD>
cJSON *audio_params = cJSON_CreateObject();
if (audio_params == NULL) {
cJSON_Delete(startMessage);
}
// 向 "audio_params" 添加字段
// <EFBFBD><EFBFBD> "audio_params" <20><><EFBFBD><EFBFBD><EFBFBD>ֶ<EFBFBD>
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 对象
// <EFBFBD><EFBFBD> "audio_params" <20><><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD> JSON <20><><EFBFBD><EFBFBD>
cJSON_AddItemToObject(startMessage, "audio_params", audio_params);
// 将 cJSON 对象转换为字符串
// <EFBFBD><EFBFBD> cJSON <20><><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Ϊ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
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 对象以释放内存
// ɾ<EFBFBD><EFBFBD> cJSON <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7>ڴ<EFBFBD>
cJSON_Delete(startMessage);
break;}
case BEKEN_RTC_SEND_HELLO:{
// 创建主 JSON 对象
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> JSON <20><><EFBFBD><EFBFBD>
cJSON *helloMessage = cJSON_CreateObject();
if (helloMessage == NULL) {
//return NULL;
}
// 添加 "type" 字段
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD> "type" <20>ֶ<EFBFBD>
cJSON_AddStringToObject(helloMessage, "type", "hello");
// 添加 "version" 字段
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD> "version" <20>ֶ<EFBFBD>
cJSON_AddNumberToObject(helloMessage, "version", 3);
// 添加 "transport" 字段
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD> "transport" <20>ֶ<EFBFBD>
cJSON_AddStringToObject(helloMessage, "transport", "websocket");
// 创建 "audio_params" JSON 对象
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD> "audio_params" JSON <20><><EFBFBD><EFBFBD>
cJSON *audio_params = cJSON_CreateObject();
if (audio_params == NULL) {
cJSON_Delete(helloMessage);
//return NULL;
}
// 向 "audio_params" 添加字段
// <EFBFBD><EFBFBD> "audio_params" <20><><EFBFBD><EFBFBD><EFBFBD>ֶ<EFBFBD>
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 对象
// <EFBFBD><EFBFBD> "audio_params" <20><><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD> JSON <20><><EFBFBD><EFBFBD>
cJSON_AddItemToObject(helloMessage, "audio_params", audio_params);
// 将 cJSON 对象转换为字符串
// <EFBFBD><EFBFBD> cJSON <20><><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Ϊ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
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 对象以释放内存
// ɾ<EFBFBD><EFBFBD> cJSON <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7>ڴ<EFBFBD>
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;

View File

@ -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("<22><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\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);
// <20>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ӹ<EFBFBD><D3B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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;
}
// <20><><EFBFBD><EFBFBD>б<EFBFBD>
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);
//<2F>ش<EFBFBD>WIFI <20>б<EFBFBD>
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);
//<2F>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8> <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
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

View File

@ -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(){
//<2F>յ<EFBFBD><D5B5><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2>boarding_core<72><65><EFBFBD><EFBFBD>
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(){
//<2F>յ<EFBFBD>wifi SSID <20><><EFBFBD><EFBFBD>
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);

View File

@ -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);
}
//<2F>ص<EFBFBD>
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);
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܵ<EFBFBD><DCB5><EFBFBD>ָ<EFBFBD><D6B8>
char* ptr;
if (NULL != (ptr = strstr((char *)param->value, "AT+SSID"))){
wboard_loge("start scan wifi!!");
//<2F>ص<EFBFBD>
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!!");
//<2F>ص<EFBFBD>
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);
//<2F><><EFBFBD><EFBFBD>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);
}
//<2F>ص<EFBFBD>
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);

View File

@ -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

View File

@ -42,6 +42,8 @@
typedef enum
{
//helloyifa
DBEVT_WIFI_STATION_SCAN,
DBEVT_WIFI_STATION_CONNECT,
DBEVT_WIFI_STATION_CONNECTED,
DBEVT_WIFI_STATION_DISCONNECTED,

View File

@ -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

View File

@ -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);

View File

@ -0,0 +1,135 @@
#include <common/sys_config.h>
#include <components/log.h>
#include <modules/wifi.h>
#include <components/netif.h>
#include <components/event.h>
#include <string.h>
#include "bk_private/bk_init.h"
#include <components/system.h>
#include <os/os.h>
#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 到lamp_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);
}
}

View File

@ -0,0 +1,26 @@
/*************************************************************
*
* Copyright (C) POKA
* All rights reserved.
*
*************************************************************/
#ifndef __IOT_LAMP_H__
#define __IOT_LAMP_H__
#ifdef __cplusplus
extern "C" {
#endif
//#include <stdbool.h>
//#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__ */

View File

@ -0,0 +1,128 @@
#include <common/sys_config.h>
#include <components/log.h>
#include <modules/wifi.h>
#include <components/netif.h>
#include <components/event.h>
#include <string.h>
#include "bk_private/bk_init.h"
#include <components/system.h>
#include <os/os.h>
#include "components/webclient.h"
#include "cJSON.h"
#include "components/bk_uid.h"
#include "bk_genie_comm.h"
#include <os/os.h>
#include <driver/pwm.h>
#include "pwm_hal.h"
#include <driver/gpio.h>
#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(){
}

View File

@ -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__ */

View File

@ -0,0 +1,224 @@
#include <common/sys_config.h>
#include <components/log.h>
#include <modules/wifi.h>
#include <components/netif.h>
#include <components/event.h>
#include <string.h>
#include "bk_private/bk_init.h"
#include <components/system.h>
#include <os/os.h>
#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", "扬声器 音量调节");
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);
}
}

View File

@ -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

View File

@ -0,0 +1,72 @@
/**
* IoT设备相关消息
*
{
"session_id": "<会话ID>",
"type": "iot",
"descriptors": <设备描述JSON>
}
{
"session_id": "<会话ID>",
"type": "iot",
"states": <状态JSON>
}
/**
* descriptorMessage的json串
* [{
* "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的json串
* [
* {
* "name": "Speaker",
* "state": {
* "volume": 70
* }
* },
* {
* "name": "Screen",
* "state": {
* "theme": "dark",
* "brightness": 75
* }
* }
* ]
*
* @throws Exception
*/

View File

@ -0,0 +1,125 @@
#include <common/sys_config.h>
#include <components/log.h>
#include <modules/wifi.h>
#include <components/netif.h>
#include <components/event.h>
#include <string.h>
#include "bk_private/bk_init.h"
#include <components/system.h>
#include <os/os.h>
#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;
}
/**
* IOT指令,
* :
*
* {
"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);
}
//扬声器指令
if (strcmp(name, "speaker") == 0)
{
iot_speaker_parser_invoke(method,str_parameters);
}
}
// 释放内存
cJSON_Delete(iot_invoke);
}
// AddStringProperty
// AddNumberProperty
// AddBooleanProperty
// AddMethod

View File

@ -0,0 +1,51 @@
#include <common/sys_config.h>
#include <components/log.h>
#include <modules/wifi.h>
#include <components/netif.h>
#include <components/event.h>
#include <string.h>
#include "bk_private/bk_init.h"
#include <components/system.h>
#include <os/os.h>
#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);
/**
* IOT指令,
*/
void thing_iot_invoke(char *iot_json);

View File

@ -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;
}

View File

@ -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;
//发送设备IOT能力描述到服务器
//rtc_websocket_send_text(client, "", BEKEN_RTC_SEND_IOT_DESC);
}
else
{
LOGE("join WebSocket server fail\r\n");
}
}
//解析服务器下发的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)