#include #include #include #include #include #include #include "bk_private/bk_init.h" #include #include #include #include "cli.h" #include "media_service.h" #include #include #include "gpio_map.h" #include "gpio_driver.h" #include #include "bk_wifi.h" #include #include "bk_uart.h" #include #include #include //#include //dtim #include "bk_manual_ps.h" #include "bk_mac_ps.h" #include "bk_mcu_ps.h" #include "bk_ps.h" #include "bk_wifi.h" #include "modules/pm.h" #include "sys_driver.h" #include "bk_pm_internal_api.h" #include #include #include #include #include #include #include #include #include #include #include #include #include "bk_gpio.h" #include #include #include "gpio_driver.h" //WIFI #include #include #include #include //TIME //#include "app_time.h" #include #include "ilock_client.h" #include "poka_client.h" //#include "agora_client.h" #include "ble_client.h" #include "net_client.h" #include "ilock_config.h" #include "wifi_scan.h" #include "system_client.h" #include "key_client.h" #include "common.h" #include "cJSON.h" #include "app_time.h" //OTA #include "modules/ota.h" #include "audio_player.h" //MQTT #include "mqtt_instance.h" #include "iot_export_mqtt.h" #include "agora_client.h" #define TAG "iLOCK" #define CONFIG_WIFI_SSID "1603-5G"//"MEGSCREEN_TEST"//"cs-ruowang-2.4G"//"Carl"//"aclsemi" #define CONFIG_WIFI_PASSWORD "88888888"//"987654321"//"wohenruo"//"12345678"//"ACL8semi" #if (CONFIG_SYS_CPU0) #include "bk_ef.h" #define AGORA_RTC_CMD_CNT (sizeof(s_agora_rtc_commands) / sizeof(struct cli_command)) void reset_powersave_timer(); static bool g_mqtt_wifi_flag = 0; wifi_scan_result_t scan_result = {0}; //static bool is_upload_bssid = 0; static bool is_ble_boarding = 0; static timer_id_t time_task_powersave = TIMER_ID1; static int timer_ble_close_tick = 0; static beken_timer_t timer_ble_handle; static int power_mode = PM_MODE_DEFAULT; static inline bool mqtt_is_wifi_connected(void) { return g_mqtt_wifi_flag; } void mqtt_waiting_for_wifi_connected(void) { while (0 == mqtt_is_wifi_connected()) { os_printf("[wifi] connecting ......\r\n"); rtos_delay_milliseconds(500); } } static int netif_event_cb(void *arg, event_module_t event_module, int event_id, void *event_data) { netif_event_got_ip4_t *got_ip; switch (event_id) { case EVENT_NETIF_GOT_IP4: got_ip = (netif_event_got_ip4_t *)event_data; BK_LOGI(TAG, "%s got ip %s.\n", got_ip->netif_if == NETIF_IF_STA ? "STA" : "unknown netif", got_ip->ip); break; default: BK_LOGI(TAG, "rx event <%d %d>\n", event_module, event_id); break; } return BK_OK; } int wifi_event_cb(void *arg, event_module_t event_module, int event_id, void *event_data) { wifi_event_sta_disconnected_t *sta_disconnected; wifi_event_sta_connected_t *sta_connected; switch (event_id) { case EVENT_WIFI_STA_CONNECTED: sta_connected = (wifi_event_sta_connected_t *)event_data; BK_LOGI(TAG, "STA connected to %s\n", sta_connected->ssid); break; case EVENT_WIFI_STA_DISCONNECTED: sta_disconnected = (wifi_event_sta_disconnected_t *)event_data; BK_LOGI(TAG, "STA disconnected, reason(%d)\n", sta_disconnected->disconnect_reason); break; default: BK_LOGI(TAG, "rx event <%d %d>\n", event_module, event_id); break; } return BK_OK; } 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; } static void wifi_sta_rw_event_func(void *new_evt) { wifi_linkstate_reason_t info = *((wifi_linkstate_reason_t *)new_evt); BK_LOGE(TAG,"WIFI_LINKSTATE:%d rcode:%d \r\n",info.state,info.reason_code); if (info.state == WIFI_LINKSTATE_STA_GOT_IP) { BK_LOGE(TAG,"WIFI_LINKSTATE_STA_GOT_IP\r\n"); g_mqtt_wifi_flag = 1; //ble_boarding_notice(2); ble_boarding_set_state(BOARDING_STATE_DONE); } else if (info.state == WIFI_LINKSTATE_STA_DISCONNECTED) { BK_LOGI(TAG,"WIFI_LINKSTATE_STA_DISCONNECTED\r\n"); if(g_mqtt_wifi_flag == 1){ BK_LOGE(TAG,"WIFI_DISCONNECT! REBOOT!!\r\n"); rtos_delay_milliseconds(500); bk_reboot(); } } else { BK_LOGI(TAG,"WIFI_LINKSTATE\r\n"); } } void timer_ble_andle_callback( void *arg ) { //bk_err_t err = kNoErr; //rtos_stop_timer(&station_ps_timer_handle); //rtos_start_timer(&station_ps_stop_timer_handle); //软件定时器 时间戳每秒加1 作为时钟 timer_ble_close_tick ++; //BK_LOGE(TAG,"timer_ble_andle_callback --> %d\r\n",timer_ble_close_tick); if(timer_ble_close_tick > ILOCK_BLE_BOARDING_CLOSE_TIMEOUT){ timer_ble_close_tick = 0; rtos_stop_timer(&timer_ble_handle); //关闭蓝牙 //system_client_do(MSG_EXIT_POWER_SAVE); ble_client_boarding_deinit(); //BK_LOGE(TAG,"ble_timeout_close !!!\r\n"); } } static void ilock_powersave_rx_wakeup(gpio_id_t gpio_id) { bk_gpio_register_isr(gpio_id ,NULL); //重新使能串口接收 bk_uart_set_enable_rx(UART_ID_1, 1); if(power_mode != PM_MODE_DEFAULT){ bk_pm_sleep_mode_set(PM_MODE_DEFAULT); bk_pm_module_vote_sleep_ctrl(PM_SLEEP_MODULE_NAME_APP,0x0,0x0); //bk_pm_module_vote_sleep_ctrl(PM_POWER_MODULE_NAME_APP,0x1,0x0); power_mode = PM_MODE_DEFAULT; BK_LOGE(TAG,"bk_pm_sleep_mode_set :DEFAULT\r\n"); } BK_LOGE(TAG,"wakeup :%d \r\n",gpio_id); system_client_do(MSG_EXIT_POWER_SAVE); reset_powersave_timer(); } static void key_gpio_int_isr(gpio_id_t id) { BK_LOGE(TAG,"key_gpio_int_isr :%d \r\n",id); bk_gpio_register_isr(GPIO_1 ,NULL); } void ilock_power_save_enter(void) { //#if (CONFIG_SYS_CPU0) bk_timer_stop(time_task_powersave); bk_gpio_register_isr(GPIO_34, ilock_powersave_rx_wakeup); bk_gpio_register_wakeup_source(GPIO_34,GPIO_INT_TYPE_RISING_EDGE);//GPIO_INT_TYPE_RISING_EDGE GPIO_INT_TYPE_HIGH_LEVEL bk_pm_wakeup_source_set(PM_WAKEUP_SOURCE_INT_GPIO, NULL); if(power_mode != PM_MODE_LOW_VOLTAGE){ BK_LOGE(TAG,"ilock_power_save_enter finish!!!\r\n"); bk_pm_sleep_mode_set(PM_MODE_LOW_VOLTAGE); power_mode = PM_MODE_LOW_VOLTAGE; system_client_do(MSG_ENTER_POWER_SAVE); //bk_pm_module_vote_sleep_ctrl(PM_POWER_MODULE_NAME_APP,0x1,0x0); bk_pm_module_vote_sleep_ctrl(PM_SLEEP_MODULE_NAME_APP,0x1,0x1); } //#endif } static void enter_powersave_callback_timer_isr(timer_id_t chan) { BK_LOGE(TAG,"enter_powersave!!! %d\r\n",chan); if(agora_is_runing()){ BK_LOGE(TAG,"agora_is_runing!!!!!!!!\r\n"); reset_powersave_timer(); return; } if(ble_boarding_get_state() != BOARDING_STATE_CLOSE){ BK_LOGE(TAG,"ble not client !!!!!!!!\r\n"); reset_powersave_timer(); return; } ilock_power_save_enter(); } void reset_powersave_timer(){ //BK_LOGE(TAG,"reset powersave timer!\r\n"); bk_timer_stop(time_task_powersave); bk_timer_start(time_task_powersave, ILOCK_POWERSAVE_TIMEOUT, enter_powersave_callback_timer_isr); } void ble_timeout_close_init(void){ bk_err_t result = rtos_init_timer(&timer_ble_handle, 1000, timer_ble_andle_callback, 0); if(kNoErr != result) { BK_LOGE(TAG,"ble_timeout init_timer fail\r\n"); return; } result = rtos_start_timer(&timer_ble_handle); if(kNoErr != result) { BK_LOGE(TAG,"ble_timeout timer fail\r\n"); return; } } void init_mac(){ uint8_t base_mac[BK_MAC_ADDR_LEN] = {0}; uint8_t sta_mac[BK_MAC_ADDR_LEN] = {0}; uint8_t ap_mac[BK_MAC_ADDR_LEN] = {0}; bk_get_mac(base_mac, MAC_TYPE_BASE); bk_wifi_sta_get_mac(sta_mac); bk_wifi_ap_get_mac(ap_mac); BK_LOGE(TAG,"MAC address: %02x-%02x-%02x-%02x-%02x-%02x\r\n", base_mac[0], base_mac[1], base_mac[2], base_mac[3], base_mac[4], base_mac[5]); BK_LOGE(TAG,"base mac: "BK_MAC_FORMAT"\n", BK_MAC_STR(base_mac)); BK_LOGE(TAG,"sta mac: "BK_MAC_FORMAT"\n", BK_MAC_STR(sta_mac)); BK_LOGE(TAG,"ap mac: "BK_MAC_FORMAT"\n", BK_MAC_STR(ap_mac)); char mac_str[32]={0}; sprintf(mac_str,"%02X%02X%02X%02X%02X%02X",base_mac[0], base_mac[1], base_mac[2], base_mac[3], base_mac[4], base_mac[5]); char * ilock_device_id = poka_client_get_device_id(); ilock_device_id[0]='L'; ilock_device_id[1]='F'; ilock_device_id[2]='V'; ilock_device_id[3]='1'; ilock_device_id[4]='_'; ilock_device_id[5]='A'; ilock_device_id[6]='0'; ilock_device_id[7]='F'; ilock_device_id[8]='E'; ilock_device_id[9]='2'; ilock_device_id[10]='3'; ilock_device_id[11]='0'; ilock_device_id[12]='2'; ilock_device_id[13]=mac_str[0]; ilock_device_id[14]=mac_str[1]; ilock_device_id[15]=mac_str[2]; ilock_device_id[16]=mac_str[3]; ilock_device_id[17]=mac_str[4]; ilock_device_id[18]=mac_str[5]; ilock_device_id[19]=mac_str[6]; ilock_device_id[20]=mac_str[7]; ilock_device_id[21]=mac_str[8]; ilock_device_id[22]=mac_str[9]; ilock_device_id[23]=mac_str[10]; ilock_device_id[24]=mac_str[11]; char * device_id = ilock_client_get_device_id(); device_id[0]='A'; device_id[1]='0'; device_id[2]='F'; device_id[3]='E'; device_id[4]='2'; device_id[5]='3'; device_id[6]='0'; device_id[7]='2'; device_id[8]=mac_str[0]; device_id[9]=mac_str[1]; device_id[10]=mac_str[2]; device_id[11]=mac_str[3]; device_id[12]=mac_str[4]; device_id[13]=mac_str[5]; device_id[14]=mac_str[6]; device_id[15]=mac_str[7]; device_id[16]=mac_str[8]; device_id[17]=mac_str[9]; device_id[18]=mac_str[10]; device_id[19]=mac_str[11]; BK_LOGE(TAG,"device_id: %s\n", device_id); } #if 1 void event_remote_video_callback(int audio, char* license, char* token){ BK_LOGE(TAG,"remote_video: audio %d\r\n",audio); if(license != NULL){ BK_LOGE(TAG,"event_remote_video: license %s\r\n",license); } BK_LOGE(TAG,"remote_video: audio %d\r\n",audio); //BK_LOGE(TAG,"event_remote_video: audio:%d\r\n",audio); //BK_LOGE(TAG,"event_remote_video: token:%s\r\n",token); if(license !=NULL && strlen(license) > 0){ agora_join_channel(poka_client_get_device_id(),token,license);//ilock_device_id }else{ #if(CONFIG_SYS_CPU0) char agora_license[33] = { 0 }; os_memset(agora_license,'\0',sizeof(agora_license)); int ret = bk_get_env_enhance("agora", (void *)&agora_license, 32); if(ret > 0){ BK_LOGE(TAG,"agora value:%d, %s\r\n", strlen(agora_license), agora_license); agora_join_channel(poka_client_get_device_id(),token,agora_license);//ilock_device_id }else{ BK_LOGE(TAG,"remote_video: license NULL\r\n"); } #endif } //回复 poka_client_replay_invoke(EVENT_REMOTE_VIDEO,poka_client_get_message_id(),FUN_TRUE,NULL); } void event_remote_video_close_callback(){ BK_LOGE(TAG,"remote_video_close\r\n"); agora_level_channel(); //回复 poka_client_replay_invoke(EVENT_REMOTE_VIDEO_CLOSE,poka_client_get_message_id(),FUN_TRUE,NULL); } /** * 接收到远程开锁指令 */ void event_unlock_remote_callback(){ BK_LOGE(TAG,"unlock_remote\r\n"); ilock_open_lock(); //开锁成功回复 poka_client_replay_invoke(EVENT_UNLOCK_REMOTE,poka_client_get_message_id(),FUN_TRUE,NULL); } /** * 接收到紧急开锁指令 */ void event_unlock_remote_sos_callback(){ BK_LOGE(TAG,"unlock_remote_sos\r\n"); ilock_open_lock_sos(); //ilock_send_lock_commond("12345678",CMD_OPEN_LOCK_SOS,0); //SOS开锁回复 poka_client_replay_invoke(EVENT_UNLOCK_REMOTE_SOS,poka_client_get_message_id(),FUN_TRUE,NULL); } /** * 接收到远程关锁指令 */ void event_lock_remote_callback(){ BK_LOGE(TAG,"lock_remote\r\n"); ilock_close_lock(); //关锁成功回复 poka_client_replay_invoke(EVENT_LOCK_REMOTE,poka_client_get_message_id(),FUN_TRUE,NULL); } /** * OTA (HTTP) */ void event_lock_http_ota_callback(char *file_url){ BK_LOGE(TAG,"http_ota\r\n"); //ilock_close_lock(); if(file_url!=NULL && strlen(file_url)!=0){ int ret; ret = bk_http_ota_download(file_url); if (0 != ret){ os_printf("http_ota download failed."); poka_client_replay_invoke(EVENT_LOCK_OTA_HTTP,poka_client_get_message_id(),FUN_FALSE,NULL); }else{ os_printf("http_ota download success."); poka_client_replay_invoke(EVENT_LOCK_OTA_HTTP,poka_client_get_message_id(),FUN_TRUE,NULL); } BK_LOGE(TAG,"http_ota finish\r\n"); } } /** * OTA (HTTPS) */ void event_lock_https_ota_callback(char *file_url){ BK_LOGE(TAG,"https_ota\r\n"); } /** * 接收心跳 */ void event_hearbeat_callback(){ BK_LOGE(TAG,"hearbeat_callback\r\n"); poka_client_replay_invoke(EVENT_HEARBEAT,poka_client_get_message_id(),FUN_TRUE,NULL); ilock_set_utc(app_time_timestamp_s()); } /** * 数据传输 */ void event_data_transfer_callback(data_transfer_t * data){ BK_LOGE(TAG,"data_transfer_callback\r\n"); //BK_LOGE(TAG,"event_data_transfer_callback %d\r\n",data->messageid); //BK_LOGE(TAG,"event_data_transfer_callback %s\r\n",data->data_value); BK_LOGE(TAG,"data_transfer_callback len %d\r\n",data->data_len); unsigned char hex[128]={0x00}; unsigned int out_len = 0; StringToHex(data->data_value,hex,&out_len); BK_LOGE(TAG,"string2hex %d\r\n",out_len); if(out_len > 0){ ilock_sendData((char*)hex,out_len); poka_client_replay_invoke(EVENT_DATA_TRANSFER,poka_client_get_message_id(),FUN_TRUE,NULL); } //free(data); } /** * 平台数据 用于唤醒cpu 退出低压 */ void event_cloud_callback(data_transfer_t * data){ if(power_mode != PM_MODE_DEFAULT){ bk_pm_sleep_mode_set(PM_MODE_DEFAULT); bk_pm_module_vote_sleep_ctrl(PM_SLEEP_MODULE_NAME_APP,0x0,0x0); power_mode = PM_MODE_DEFAULT; } system_client_do(MSG_EXIT_POWER_SAVE); reset_powersave_timer(); } void event_agora_license_callback(char* license,int license_len ){ BK_LOGE(TAG,"license:%s\r\n",license); #if(CONFIG_SYS_CPU0) if(license!=NULL){ int ret = 0; ret = bk_set_env_enhance("agora", license, license_len); if(EF_NO_ERR == ret){ BK_LOGE(TAG,"agora value:%d, %s\r\n", license_len, license); } else{ BK_LOGE(TAG,"agora set fail:%d\r\n", ret); } } #endif } void event_link_state_callback(int state){ BK_LOGE(TAG,"event_link_state_callback:%d\r\n", state); if(state == MQTT_INSTANCE_EVENT_CONNECTED){ //连接成功时 //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)); } } static poka_client_cb_ops_t cloud_event_ops = { .event_cloud = event_cloud_callback, .event_link_state = event_link_state_callback, .event_remote_video = event_remote_video_callback, .event_remote_video_close = event_remote_video_close_callback, .event_unlock_remote = event_unlock_remote_callback, .event_unlock_remote_sos = event_unlock_remote_sos_callback, .event_lock_remote = event_lock_remote_callback, .event_hearbeat = event_hearbeat_callback, .event_data_transfer = event_data_transfer_callback, .event_ota_http = event_lock_http_ota_callback, .event_ota_https = event_lock_https_ota_callback, .event_agora_license = event_agora_license_callback, }; #endif void event_ilock_lock_callback(int state){ BK_LOGE(TAG,"ilock_lock :%d\r\n",state); if(state == 1){ //dev_info_lasted.lock_lock_state = 1; //poka_client_report_properties_keyvalue("lock_state","1"); }else{ //关锁失败回复 //poka_client_replay_invoke(EVENT_LOCK_REMOTE,poka_client_get_message_id(),FUN_FALSE,NULL); } } void event_ilock_unlock_callback(int state){ BK_LOGE(TAG,"ilock_unlock :%d\r\n",state); if(state == 1){ //dev_info_lasted.lock_lock_state = 0; //poka_client_report_properties_keyvalue("lock_state","0"); }else{ //开锁失败回复 //poka_client_replay_invoke(EVENT_UNLOCK_REMOTE,poka_client_get_message_id(),FUN_FALSE,NULL); } } void event_ilock_unlock_sos_callback(int state){ BK_LOGE(TAG,"ilock_unlock_sos :%d\r\n",state); if(state == 1){ dev_info_lasted.lock_lock_state = 0; poka_client_report_properties_keyvalue("lock_state","0"); }else{ //SOS开锁失败回复 //poka_client_replay_invoke(EVENT_UNLOCK_REMOTE_SOS,poka_client_get_message_id(),FUN_FALSE,NULL); } } void event_ilock_doorbell_callback(int audio, int video){ BK_LOGE(TAG,"doorbell!!\r\n"); cJSON *data = cJSON_CreateObject(); cJSON_AddNumberToObject(data, "video", 1); char *jsonStr = cJSON_Print(data); poka_client_report_event(EVENT_DOORBELL,jsonStr); cJSON_Delete(data); if(!agora_is_runing()){ //system_client_do(MSG_AGORA); audio_player_playvoice(VOICE_DINGDONG); } } void event_ilock_network_boarding_callback(){ BK_LOGE(TAG,"network_boarding!!\r\n"); } void event_ilock_state_callback(ilock_dev *dev_info){ BK_LOGE(TAG,"\r\n\r\n"); BK_LOGE(TAG,"batt_value> %d \r\n,",dev_info->lock_batt_value); BK_LOGE(TAG,"power_supply> %d \r\n,",dev_info->lock_power_supply); BK_LOGE(TAG,"door_state> %d \r\n,",dev_info->lock_door_state); BK_LOGE(TAG,"lock_state> %d \r\n,",dev_info->lock_lock_state); BK_LOGE(TAG,"lockedin_state> %d \r\n,",dev_info->lock_lockedin_state); BK_LOGE(TAG,"batt_state> %d \r\n,",dev_info->lock_batt_state); BK_LOGE(TAG,"disable_lock_state> %d \r\n\r\n,",dev_info->lock_disable_lock_state); int diff = 0 ; if(dev_info_lasted.lock_lockedin_state != dev_info->lock_lockedin_state){ dev_info_lasted.lock_lockedin_state = dev_info->lock_lockedin_state; diff = 1; //poka_client_report_properties_keyvalue_int(PROP_LOCKEDIN_STATE,dev_info_lasted.lock_lockedin_state); } if(dev_info_lasted.lock_door_state != dev_info->lock_door_state){ dev_info_lasted.lock_door_state = dev_info->lock_door_state; diff = 1; //poka_client_report_properties_keyvalue_int(PROP_DOOR_STATE,dev_info_lasted.lock_door_state); } if(dev_info_lasted.lock_lock_state != dev_info->lock_lock_state){ dev_info_lasted.lock_lock_state = dev_info->lock_lock_state; diff = 1; //poka_client_report_properties_keyvalue_int(PROP_LOCK_STATE,dev_info_lasted.lock_lock_state); } if(dev_info_lasted.lock_batt_state != dev_info->lock_batt_state){ dev_info_lasted.lock_batt_state = dev_info->lock_batt_state; diff = 1; //poka_client_report_properties_keyvalue_int(PROP_BATTERY_STATE,dev_info_lasted.lock_batt_state); } if(dev_info_lasted.lock_batt_value != dev_info->lock_batt_value){ //避免跳变 if(abs(dev_info_lasted.lock_batt_value - dev_info->lock_batt_value) > 2){ //poka_client_report_properties_keyvalue_int(PROP_BATTERY_CAP,dev_info_lasted.lock_batt_value); dev_info_lasted.lock_batt_value = dev_info->lock_batt_value; //diff = 1; } } if(dev_info_lasted.lock_disable_lock_state != dev_info->lock_disable_lock_state){ dev_info_lasted.lock_disable_lock_state = dev_info->lock_disable_lock_state; //poka_client_report_properties_keyvalue_int(PROP_DISABLE_LOCK,dev_info_lasted.lock_disable_lock_state); diff = 1; } BK_LOGE(TAG,"diff> %d \r\n,",diff); if(diff == 1){ #if 1 poka_client_report_properties("",dev_info->lock_lockedin_state, dev_info->lock_door_state, dev_info->lock_lock_state, dev_info->lock_batt_state, dev_info->lock_batt_value, dev_info->lock_disable_lock_state, dev_info->lock_power_supply); #endif } //os_free(dev_info); } void event_ilock_read_params_callback(char* params, int len){ BK_LOGE(TAG,"ilock_read_params_callback !!\r\n"); os_memset(dev_info_lasted.lock_params,0,sizeof(dev_info_lasted.lock_params)); os_memcpy(&dev_info_lasted.lock_params, params, len); } void event_lock_recv_data_callback(uint8_t *data, int len){ //BK_LOGE(TAG,"event_lock_recv_data_callback flag:%02X len:%d\r\n",data[3],len); BK_LOGE(TAG,"lock_recv_data_callback len:%d\r\n",len); if(len > 0){ int cmd = data[3]; //0x23,0x25、0x26、0x27、0x45、0x46、0x52、0x53、0x54、0x55、0x56 if(cmd == 0x22 ||cmd == 0x23 || cmd == 0x25 ||cmd == 0x26 ||cmd == 0x27||cmd == 0x42||cmd == 0x43 ||cmd == 0x45 ||cmd == 0x46 ||cmd == 0x52 ||cmd == 0x53 ||cmd == 0x54||cmd == 0x55||cmd == 0x56){ unsigned char out [64] = {0}; hex_to_asciistring(data,len,out); poka_client_report_event(EVENT_DATA_TRANSFER,(char*)&out); } } } void event_lock_version_callback(char * version, int len){ if(version != NULL && len > 0){ poka_client_report_tags_keyvalue(TAGS_ILOCK_BODY_VERSION, version); } } static ilock_client_cb_ops_t ilock_event_ops = { .event_lock_lock = event_ilock_lock_callback, .event_lock_unlock = event_ilock_unlock_callback, .event_lock_unlock_sos = event_ilock_unlock_sos_callback, .event_doorbell = event_ilock_doorbell_callback, .event_network_boarding = event_ilock_network_boarding_callback, .event_lock_state = event_ilock_state_callback, .event_lock_params = event_ilock_read_params_callback, .event_lock_recv_data = event_lock_recv_data_callback, .event_lock_version = event_lock_version_callback }; //BLE_WIFI void event_ble_boarding_wifi_info_callback(char* ssid, int ssid_len, char* password,int password_len){ is_ble_boarding = 0; if(ssid == NULL || password == NULL) return; bk_wifi_sta_stop(); BK_LOGE(TAG,"boarding_wifi_info!! %s %s\r\n",ssid , password); #if(CONFIG_SYS_CPU0) int ret = 0; ret = bk_set_env_enhance("ssid", ssid, ssid_len); if(EF_NO_ERR == ret){ bk_printf("ssid value:%d, %s\r\n", ssid_len, ssid); } else{ bk_printf("ssid set fail:%d\r\n", ret); } ret = bk_set_env_enhance("password", password, password_len); if(EF_NO_ERR == ret){ bk_printf("password value:%d, %s\r\n", password_len, password); } else{ bk_printf("password set fail:%d\r\n", ret); } #endif ble_boarding_set_state(BOARDING_STATE_SETWIFI); //ble_boarding_notice(1); rtos_delay_milliseconds(1500); //ble_boarding_notice(2); //bk_reboot(); wifi_sta_config_t sta_config = {0}; os_strcpy(sta_config.ssid, ssid); os_strcpy(sta_config.password, password); //os_strcpy(sta_config.ssid, "1603"); //os_strcpy(sta_config.password, "88888888"); bk_wifi_sta_set_config(&sta_config); bk_wifi_sta_start(); } void event_ble_boarding_start_callback(){ BK_LOGE(TAG,"event_ble_boarding start\r\n"); //重置一下蓝牙关闭时间 timer_ble_close_tick = 0; ble_boarding_set_state(BOARDING_STATE_IDLE); //收到蓝牙的搜索指令 开始搜索 ble_boarding_set_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)); } void event_agora_info_callback(char* license, int license_len){ BK_LOGE(TAG,"event_agora_info:(%d) %s\r\n",license_len,license); #if(CONFIG_SYS_CPU0) int ret = 0; ret = bk_set_env_enhance("agora", license, license_len); if(EF_NO_ERR == ret){ bk_printf("agora value:%d, %s\r\n", license_len, license); } else{ bk_printf("agora set fail:%d\r\n", ret); } #endif } static ble_boarding_cb_ops_t ble_boarding_event_ops = { .event_wifi_info = event_ble_boarding_wifi_info_callback, .event_start_boarding = event_ble_boarding_start_callback, .event_agora_info = event_agora_info_callback }; static void on_key_event (key_event_type key_event,key_event_code key_code){ if(key_event == KEY_DOWN ){ //唤醒系统 if(power_mode != PM_MODE_DEFAULT){ bk_pm_sleep_mode_set(PM_MODE_DEFAULT); bk_pm_module_vote_sleep_ctrl(PM_SLEEP_MODULE_NAME_APP,0x0,0x0); power_mode = PM_MODE_DEFAULT; } system_client_do(MSG_EXIT_POWER_SAVE); reset_powersave_timer(); if(key_code == KEY_BACK_REBOOT){ ilock_play_voice(3); } if(key_code == KEY_BACK_RESET_FACTORY){ //test_audio(); ilock_play_voice(1); rtos_delay_milliseconds(1000); ilock_factory_reset(); } } else if(key_event == KEY_UP){ if(key_code == KEY_BACK_REBOOT){ rtos_delay_milliseconds(500); bk_reboot(); } } } static key_cb_ops_t key_event_ops = { .on_key_event = on_key_event, }; int app_wifi_event_cb(void *arg, event_module_t event_module, int event_id, void *event_data) { wifi_event_sta_disconnected_t *sta_disconnected; wifi_event_sta_connected_t *sta_connected; //wifi_event_ap_disconnected_t *ap_disconnected; //wifi_event_ap_connected_t *ap_connected; BK_LOGE(TAG,"app_wifi_event_cb event_id->%d\n", event_id); switch (event_id) { case EVENT_WIFI_STA_CONNECTED: sta_connected = (wifi_event_sta_connected_t *)event_data; BK_LOGE(TAG,"BK STA connected %s\n", sta_connected->ssid); break; case EVENT_WIFI_STA_DISCONNECTED: sta_disconnected = (wifi_event_sta_disconnected_t *)event_data; BK_LOGE(TAG,"BK STA disconnected, reason(%d)%s\n", sta_disconnected->disconnect_reason, sta_disconnected->local_generated ? ", local_generated" : ""); //AP 不存在 if(sta_disconnected->disconnect_reason == 257){ ble_boarding_set_state(BOARDING_STATE_AP_NOFOUND); } //密码错误 if(sta_disconnected->disconnect_reason == 258 || sta_disconnected->disconnect_reason == WIFI_REASON_PREV_AUTH_NOT_VALID){ ble_boarding_set_state(BOARDING_STATE_PASSWORD_ERROR); } break; default: BK_LOGE(TAG,"rx event <%d %d>\n", event_module, event_id); break; } return BK_OK; } void init_wifi(){ bk_wlan_status_register_cb(wifi_sta_rw_event_func); BK_LOG_ON_ERR(bk_event_register_cb(EVENT_MOD_WIFI, EVENT_ID_ALL, app_wifi_event_cb, NULL)); //BK_LOG_ON_ERR(bk_event_register_cb(EVENT_MOD_NETIF, EVENT_ID_ALL, cli_netif_event_cb, NULL)); wifi_sta_config_t sta_config = {0}; char wifi_ssid[32] = { 0 }; char wifi_password[32] = { 0 }; #if(CONFIG_SYS_CPU0) int ret = bk_get_env_enhance("ssid", (void *)&wifi_ssid, sizeof(wifi_ssid)); if(ret > 0){ bk_printf("ssid value:%d, %s\r\n", strlen(wifi_ssid), wifi_ssid); } char agora_license[32] = { 0 }; ret = bk_get_env_enhance("agora", (void *)&agora_license, sizeof(agora_license)); if(ret > 0){ bk_printf("agora value:%d, %s\r\n", strlen(agora_license), agora_license); } ret = bk_get_env_enhance("password", (void *)&wifi_password, sizeof(wifi_password)); if(ret > 0){ bk_printf("ssid value:%d, %s\r\n", strlen(wifi_password), wifi_password); } #endif if(strlen(wifi_ssid) > 0 && strlen(wifi_password) == 0){ BK_LOGE(TAG,"!!!!!!! no wifi password\r\n"); sta_config.security = WIFI_SECURITY_NONE;//WIFI_SECURITY_AUTO os_strcpy(sta_config.password, ""); }else{ os_strcpy(sta_config.ssid, wifi_ssid); os_strcpy(sta_config.password, wifi_password); } if(strlen(wifi_ssid) == 0 && strlen(wifi_password) == 0){ BK_LOGE(TAG,"test wifi \r\n"); os_strcpy(sta_config.ssid, "1603-5G"); os_strcpy(sta_config.password, "88888888"); } //os_strcpy(sta_config.ssid, "TP-LINK_B85087"); //os_strcpy(sta_config.ssid, "H3C_WIFI"); //os_strcpy(sta_config.password, ""); //sta_config.security = WIFI_SECURITY_NONE; //设置DTIM10(1:DTIM1) bk_wifi_send_listen_interval_req(1); bk_wifi_sta_set_config(&sta_config); bk_wifi_sta_start(); } void init_esay(){ #if(CONFIG_SYS_CPU0) BK_LOG_ON_ERR(easyflash_init()); #endif } void report_device_info_to_cloud(){ uint8_t base_mac[BK_MAC_ADDR_LEN] = {0}; bk_get_mac(base_mac, MAC_TYPE_BASE); char mac_str[32]={0}; sprintf(mac_str,"%02X%02X%02X%02X%02X%02X",base_mac[0], base_mac[1], base_mac[2], base_mac[3], base_mac[4], base_mac[5]); wifi_sta_config_t sta_config = {0}; bk_wifi_sta_get_config(&sta_config); //BK_LOGE(TAG,"SSID :%s \r\n",sta_config.ssid); //BK_LOGE(TAG,"PWD :%s \r\n",sta_config.password); wifi_link_status_t link_status = {0}; os_memset(&link_status, 0x0, sizeof(link_status)); bk_wifi_sta_get_link_status(&link_status); char ap_bssid[32] ={0}; sprintf(ap_bssid,"%02X%02X%02X%02X%02X%02X",link_status.bssid[0],link_status.bssid[1],link_status.bssid[2],link_status.bssid[3],link_status.bssid[4],link_status.bssid[5]); BK_LOGE(TAG,"ap_bssid :%s \r\n",ap_bssid); char ap_rssi[8] ={0}; sprintf(ap_rssi,"%d",link_status.rssi); poka_client_report_device_tags(ILOCK_SOFT_VERSION,mac_str,sta_config.ssid,sta_config.password,ap_bssid,ap_rssi); } static void event_handler_init(void) { BK_LOG_ON_ERR(bk_event_register_cb(EVENT_MOD_WIFI, EVENT_ID_ALL, wifi_event_cb, NULL)); BK_LOG_ON_ERR(bk_event_register_cb(EVENT_MOD_NETIF, EVENT_ID_ALL, netif_event_cb, NULL)); } static void sta_connect(void) { wifi_sta_config_t sta_config = WIFI_DEFAULT_STA_CONFIG(); strncpy(sta_config.ssid, CONFIG_WIFI_SSID, WIFI_SSID_STR_LEN); if (strlen(CONFIG_WIFI_PASSWORD)) { strncpy(sta_config.password, CONFIG_WIFI_PASSWORD, WIFI_PASSWORD_LEN); } BK_LOGI(TAG, "sta ssid:%s password:%s.\n", sta_config.ssid, sta_config.password); BK_LOG_ON_ERR(bk_wifi_sta_set_config(&sta_config)); BK_LOG_ON_ERR(bk_wifi_sta_start()); } #endif extern void user_app_main(void); extern void rtos_set_user_app_entry(beken_thread_function_t entry); extern int bk_cli_init(void); extern void bk_set_jtag_mode(uint32_t cpu_id, uint32_t group_id); void user_app_main(void){ #if (CONFIG_SYS_CPU0) BK_LOGE(TAG,"VERSION:%s\r\n",ILOCK_SOFT_VERSION); #if 1 bk_gpio_driver_init(); gpio_dev_unmap(GPIO_34); bk_gpio_enable_input(GPIO_34); //bk_gpio_disable_pull(GPIO_9); //bk_gpio_set_config(GPIO_9, &cfg); #endif #if 1 gpio_dev_unmap(GPIO_7); bk_gpio_enable_output(GPIO_7); bk_gpio_set_output_high(GPIO_7); #endif event_handler_init(); //DEVICE ID init_mac(); //FLASH init_esay(); //TIMER bk_timer_driver_init(); //WIFI init_wifi(); ble_boarding_set_callback(&ble_boarding_event_ops); ble_client_boarding_init(); //key key_client_set_cb(&key_event_ops); key_client_start(); mqtt_waiting_for_wifi_connected(); ble_timeout_close_init(); net_check_start(); //system system_client_start(); //lock body and lock front ilock_set_callback(&ilock_event_ops); ilock_init(); ilock_rx_start(); //init poka cloud plat poka_client_set_callback(&cloud_event_ops); poka_client_init(); ilock_set_utc(app_time_timestamp_s()); rtos_delay_milliseconds(2000); report_device_info_to_cloud(); ilock_read_version(); rtos_delay_milliseconds(1000); ilock_read_serialno(); //ilock_read_state(); reset_powersave_timer(); //bk_cli_init(); #else #endif } int main(void) { #if (CONFIG_SYS_CPU0) rtos_set_user_app_entry((beken_thread_function_t)user_app_main); // bk_set_printf_sync(true); // shell_set_log_level(BK_LOG_WARN); #endif bk_init(); media_service_init(); //shell_echo_set(0); return 0; }