#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 "wanson_client.h" #include "agora_client.h" #include "ota_client.h" #include #include "camera_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 bool is_ota = 0; static bool is_set_wifi_info = 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(1000); } } 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"); //2024.12.31 配网成功获取IP后 关闭蓝牙 if(ble_boarding_get_state() != BOARDING_STATE_IDLE){ ble_boarding_set_state(BOARDING_STATE_DONE); rtos_delay_milliseconds(2000); if(ble_client_is_connect()){ ble_client_boarding_deinit(); } } g_mqtt_wifi_flag = 1; //ble_boarding_notice(2); } 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"); } } static void ilock_settings_key_wakeup(gpio_id_t gpio_id) { } void timer_ble_andle_callback( void *arg ) { //软件定时器 时间戳每秒加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){ if(ble_client_is_connect()){ timer_ble_close_tick = 0; BK_LOGE(TAG,"ble_client_is_connect --> do not sleep\r\n"); return; } timer_ble_close_tick = 0; rtos_stop_timer(&timer_ble_handle); //关闭蓝牙 ble_client_boarding_deinit(); if(!is_set_wifi_info){ //deep sleep and set key wakeup BK_LOGE(TAG,"not set wifo info --> do sleep\r\n"); rtos_delay_milliseconds(300); gpio_dev_unmap(ILOCK_SETTINGS_KEY_GPIO); bk_gpio_enable_input(ILOCK_SETTINGS_KEY_GPIO); bk_gpio_enable_interrupt(ILOCK_SETTINGS_KEY_GPIO); bk_gpio_register_isr(ILOCK_SETTINGS_KEY_GPIO, ilock_settings_key_wakeup); bk_gpio_register_wakeup_source(ILOCK_SETTINGS_KEY_GPIO,GPIO_INT_TYPE_LOW_LEVEL); bk_pm_wakeup_source_set(PM_WAKEUP_SOURCE_INT_GPIO, NULL); bk_pm_sleep_mode_set(PM_MODE_DEEP_SLEEP); } } } 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,"enter powersave 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,"try enter powersave!\r\n"); if(agora_is_runing()){ BK_LOGE(TAG,"agora_is_runing!\r\n"); reset_powersave_timer(); return; } if(ble_boarding_get_state() != BOARDING_STATE_CLOSE){ int remaining = ILOCK_BLE_BOARDING_CLOSE_TIMEOUT - timer_ble_close_tick ; BK_LOGE(TAG,"ble working! remaining: %ds\r\n",remaining); reset_powersave_timer(); return; } if(is_ota || ilock_client_is_ota_mode() == 1){ BK_LOGE(TAG,"ota !\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}; #ifdef ILOCK_TEST_STATIC_MAC //C8 47 8C E4 F4 DE sprintf(mac_str,"%02X%02X%02X%02X%02X%02X",0xC8,0x47,0x8C,0xE4,0xF4,0xDE); #else 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]); #endif //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'; if(ILOCK_PRODUCT == ILOCK_PRODUCT_LFV1){ ilock_device_id[3]='1'; } else if(ILOCK_PRODUCT == ILOCK_PRODUCT_LFV2){ ilock_device_id[3]='2'; } 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'; if(ILOCK_PRODUCT == ILOCK_PRODUCT_LFV1){ ilock_device_id[10]='3'; } else if(ILOCK_PRODUCT == ILOCK_PRODUCT_LFV2){ ilock_device_id[10]='4'; } 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]; BK_LOGE(TAG,"deviceID: %s\n", ilock_device_id); 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(char *uid){ BK_LOGE(TAG,"unlock_remote\r\n"); int user_id = -1; if(uid!=NULL && strlen(uid)){ char str_uid[10]; strncpy(str_uid, uid+2, 9); str_uid[9] = '\0'; user_id = atol(str_uid); BK_LOGE(TAG,"unlock_remote str_uid:%s userid:%d\r\n",str_uid,user_id); } ilock_open_lock(user_id); //开锁成功回复 poka_client_replay_invoke(EVENT_UNLOCK_REMOTE,poka_client_get_message_id(),FUN_TRUE,NULL); } /** * 接收到紧急开锁指令 */ void event_unlock_remote_sos_callback(char *uid){ BK_LOGE(TAG,"unlock_remote_sos\r\n"); int user_id = -1; if(uid!=NULL && strlen(uid)){ char str_uid[10]; strncpy(str_uid, uid+2, 9); str_uid[9] = '\0'; user_id = atol(str_uid); BK_LOGE(TAG,"unlock_remote_sos str_uid:%s userid:%d\r\n",str_uid,user_id); } ilock_open_lock_sos(user_id); //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(char *uid){ BK_LOGE(TAG,"lock_remote\r\n"); int user_id = -1; if(uid!=NULL && strlen(uid)){ char str_uid[10]; strncpy(str_uid, uid+2, 9); str_uid[9] = '\0'; user_id = atol(str_uid); BK_LOGE(TAG,"lock_remote str_uid:%s userid:%d\r\n",str_uid,user_id); } ilock_close_lock(user_id); //关锁成功回复 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"); poka_stop_reboot_timer(); system_start_ota_timer(); is_ota = 1; //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; BK_LOGE(TAG,"cloud wake up\r\n"); 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_set_wifi_callback(char* ssid,int ssid_len ,char* password ,int password_len){ BK_LOGE(TAG,"set_wifi ssid:%s pwd:%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 rtos_delay_milliseconds(300); bk_reboot(); } void event_set_video_quality_callback(int quality){ BK_LOGE(TAG,"event_event_set_video_quality:%d\r\n",quality); agora_set_video_quality(quality); } 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)); } } void funs_ota_callback(int device_type, char* ota_file_url){ BK_LOGE(TAG,"funs_ota_callback device_type :%d\r\n", device_type); poka_client_replay_invoke(FUNS_OTA,poka_client_get_message_id(),FUN_TRUE,NULL); if(ota_file_url == NULL || strlen(ota_file_url) == 0){ return; } ota_file_t ota_file = {0}; int ret = ota_file_download_from_http(ota_file_url,&ota_file); if(ret != BK_OK){ BK_LOGE(TAG,"download ota file fail !!\r\n"); return; } BK_LOGE(TAG,"ota_data_len :%d\r\n",ota_file.ota_data_len); /* uint8_t *ota_data; ota_data = (uint8_t *)psram_malloc(30*1024+3); for(int i = 0; i<30*1024+3;i++){ ota_data[i]=88&0xFF; } ilock_client_ota_start(0x11,(uint8_t *)ota_data, 30*1024+3); if (ota_data != NULL) { os_free((void *)ota_data); ota_data = NULL; } */ //BK_LOGE(TAG, "** %02X %02X %02X %02X %02X \n",ota_file.ota_data,ota_file.ota_data[1],ota_file.ota_data[2],ota_file.ota_data[3]); ilock_client_ota_start(0x11,(uint8_t *)ota_file.ota_data, ota_file.ota_data_len); if (ota_file.ota_data != NULL) { psram_free(ota_file.ota_data); ota_file.ota_data = NULL; } } void funs_take_photo_callback(){ BK_LOGE(TAG,"funs_take_photo_callback \r\n"); camera_client_take_photo(); } 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, .event_set_wifi = event_set_wifi_callback, .event_set_video_quality = event_set_video_quality_callback, .funs_ota = funs_ota_callback, .funs_take_photo = funs_take_photo_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); } camera_client_take_photo(); } 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,"event_code> %d \r\n,",dev_info->event_code); //高4字节 int ev_type = dev_info->event_code&0xF ; int ev_source = (dev_info->event_code&0xF0)>>4 ; BK_LOGE(TAG,"ev_source> %d \r\n,",ev_source); //低4字节 BK_LOGE(TAG,"ev_type> %d \r\n,",ev_type); 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); if(ev_type == 1 && ev_source == 9){ camera_client_take_photo(); } #if 1 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); cJSON *data = cJSON_CreateObject(); //app_time_timestamp_ms() char str_time[32]={0}; sprintf(str_time,"%lld",app_time_timestamp_ms()); if(strlen(str_time) > 0){ cJSON_AddStringToObject(data, "event_time", str_time); } cJSON_AddNumberToObject(data, "lock_state", dev_info->lock_lock_state); cJSON_AddNumberToObject(data, "door_state", dev_info->lock_door_state); cJSON_AddNumberToObject(data, "power_state", dev_info->lock_power_supply); cJSON_AddNumberToObject(data, "battery_capacity", dev_info->lock_batt_value); cJSON_AddNumberToObject(data, "battery_state", dev_info->lock_batt_state); char *jsonStr = cJSON_Print(data); poka_client_report_event(EVENT_LOCK,jsonStr); cJSON_Delete(data); #endif } #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); #if 1 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 == 0x47 ||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); } } #endif } 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 is_set_wifi_info = 1; 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){ //audio_player_playvoice(VOICE_DINGDONG); rtos_delay_milliseconds(200); ilock_play_voice(3); } if(key_code == KEY_BACK_RESET_FACTORY){ BK_LOGE(TAG,"KEY_BACK_RESET_FACTORY\n"); #if(CONFIG_SYS_CPU0) bk_set_env_enhance("ssid", "", 0); bk_set_env_enhance("password", "", 0); #endif 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(300); bk_reboot(); } if(key_code == KEY_BACK_RESET_FACTORY){ rtos_delay_milliseconds(300); bk_reboot(); } } } static key_cb_ops_t key_event_ops = { .on_key_event = on_key_event, }; static void camera_client_on_take_photo_event (char *photo_id, int len){ if(photo_id==NULL){ return; } cJSON *data = cJSON_CreateObject(); //app_time_timestamp_ms() char str_time[32]={0}; sprintf(str_time,"%lld",app_time_timestamp_ms()); if(strlen(str_time) > 0){ cJSON_AddStringToObject(data, "event_time", str_time); } cJSON_AddStringToObject(data, "face_url", photo_id); char *jsonStr = cJSON_Print(data); poka_client_report_event(EVENT_ALERT_ERROR_FACE,jsonStr); cJSON_Delete(data); } static camera_cb_ops_t camera_client_event_ops = { .on_take_photo_event = camera_client_on_take_photo_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" : ""); if(sta_disconnected->disconnect_reason == 256){ //rtos_delay_milliseconds(300); //bk_reboot(); } //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){ is_set_wifi_info = 0; }else{ is_set_wifi_info = 1; } //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); char version[32] ={0}; sprintf(version,"%02X%02X%02X%02X",ILOCK_SOFT_VERSION_1,ILOCK_SOFT_VERSION_2,ILOCK_SOFT_VERSION_3,ILOCK_SOFT_VERSION_4); poka_client_report_device_tags(version,mac_str,sta_config.ssid,sta_config.password,ap_bssid,ap_rssi); } void event_agora_camera_link_state_callback(camera_link_state state){ BK_LOGE(TAG,"camera_link :%d\r\n",state); cJSON *data = cJSON_CreateObject(); if(state == CAMERA_LINK_SUCCESS){ //poka_client_report_event_int(EVENT_CAMERA_LINK,1); cJSON_AddNumberToObject(data, "link_state", 1); } else if(state == CAMERA_LINK_FAIL){ //poka_client_report_event_int(EVENT_CAMERA_LINK,0); cJSON_AddNumberToObject(data, "link_state", 0); } else if(state == CAMERA_LINK_INTERRUP){ //poka_client_report_event_int(EVENT_CAMERA_LINK,2); cJSON_AddNumberToObject(data, "link_state", 2); } char *jsonStr = cJSON_Print(data); poka_client_report_event(EVENT_CAMERA_LINK,jsonStr); cJSON_Delete(data); } static agora_client_cb_ops_t agora_client_event_ops = { .event_camera_link_state = event_agora_camera_link_state_callback, }; #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 init_ilock(){ } void test_ota(){ ota_file_t ota_file = {0}; #if 0 bk_psram_heap_get_used_state(); rtos_delay_milliseconds(20); ota_file_t *ota_f = &ota_file; int test_len = 66*1024; ota_f->ota_data = malloc(test_len); bk_psram_heap_get_used_state(); memset(ota_f->ota_data,0x09,test_len); for(int i = 0; iota_data++=0xF8&0xFF; //*ota_f->ota_data++ = (uint8_t)0xF8; //uint8_t daa = 0xF8; //os_memcpy(ota_f->ota_data+i, &daa, 1); } //ilock_client_ota_start(0x11,(uint8_t *)ota_f->ota_data, 65*1024); for(int i = 0; iota_data[i]); rtos_delay_milliseconds(2); } rtos_delay_milliseconds(20); bk_psram_heap_get_used_state(); if (ota_f->ota_data != NULL) { os_free((void *)ota_f->ota_data); ota_f->ota_data = NULL; } #else int ret = ota_file_download_from_http("http://106.52.233.130:8888/HC32L07X.bin",&ota_file); if(ret != BK_OK){ BK_LOGE(TAG,"download ota file fail !!\r\n"); return; } BK_LOGE(TAG,"ota_data_len :%d\r\n",ota_file.ota_data_len); BK_LOGE(TAG, "%02X %02X %02X %02X\n",ota_file.ota_data[ota_file.ota_data_len-1],ota_file.ota_data[ota_file.ota_data_len-2],ota_file.ota_data[ota_file.ota_data_len-3],ota_file.ota_data[ota_file.ota_data_len-4]); int result = ilock_client_ota_start(0x11,(uint8_t *)ota_file.ota_data, ota_file.ota_data_len); BK_LOGE(TAG,"test_ota result:%d !!\r\n",result); if(result == 0 ){ //OTA SUCCESS }else{ //OTA_FAILED } if (ota_file.ota_data != NULL) { free(ota_file.ota_data); ota_file.ota_data = NULL; } #endif } #include int bk_https_ota_download(const char *url); void user_app_main(void){ #if (CONFIG_SYS_CPU0) BK_LOGE(TAG,"VERSION:%d %d %d %d\r\n",ILOCK_SOFT_VERSION_1,ILOCK_SOFT_VERSION_2,ILOCK_SOFT_VERSION_3,ILOCK_SOFT_VERSION_4); #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 0 gpio_dev_unmap(GPIO_7); bk_gpio_enable_output(GPIO_7); bk_gpio_set_output_high(GPIO_7); #endif //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); #if ILOCK_TEST_BROAD key_client_start(); #endif //lock body and lock front ilock_set_callback(&ilock_event_ops); ilock_init(); //camera camera_client_set_cb(&camera_client_event_ops); net_check_start(); //system system_client_start(); //判断是否已经配置了网络 //如果没有配置网络不启动wifi 开启蓝牙配网 并且5分钟超时后进入休眠 ble_timeout_close_init(); mqtt_waiting_for_wifi_connected(); ilock_rx_start(); //init poka cloud plat poka_client_set_callback(&cloud_event_ops); poka_client_init(); //init agora agora_set_callback(&agora_client_event_ops); ilock_set_utc(app_time_timestamp_s()); rtos_delay_milliseconds(500); report_device_info_to_cloud(); ilock_read_version(); rtos_delay_milliseconds(500); ilock_read_serialno(); //ilock_read_state(); reset_powersave_timer(); //app_time_rtc_ntp_sync_init(); //test_ota(); //test_http_poka(); //ilock_example_httpc_get("http://106.52.233.130:8888/LFV2_2.0.2.2_2024.11.13.rbl"); //bk_https_ota_download("https://dl.bekencorp.com/d/armino_bin/bk_avdk/v2.0.1.32/86box/bk7258/encrypt/app_pack.rbl?sign=r6nMQqQbhZBE18Qgf1Hu2dSkeZXgqLn-_-tEtyTV_c0=:0"); //bk_http_ota_download("http://106.52.233.130:8888/LFV2_2.0.2.2_2024.11.13.rbl"); //camera_client_take_photo(); //test_upload_photo(); //uint8_t mjpeg[64] = {0}; //ota_example_httpc_post("https://lock.xa-poka.com/pokalock/pokaLockFile/catching",(uint8_t*)&mjpeg,64); //ota_example_httpc_post("http://192.168.0.39:8080/upload",(uint8_t*)&mjpeg,64); //uint8_t mjpeg[64] = {0}; //webclient_post_photo("https://lock.xa-poka.com/pokalock/pokaLockFile/catching",(uint8_t*)&mjpeg,64); //webclient_post_photo("http://192.168.0.39/upload",(uint8_t*)&mjpeg,64); #if ILOCK_AI_ASR ilock_close_camera(); rtos_delay_milliseconds(250); ilock_open_camera(); rtos_delay_milliseconds(3000); poka_wanson_asr_test_init(); #endif #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; }