From: @CHENG <2393524978@qq.com> Date: Mon, 29 Apr 2024 07:49:12 +0000 (+0800) Subject: gengxin X-Git-Url: http://git.snicker.com.cn/?a=commitdiff_plain;h=557448edeb4507d4da64a0d8e6e522b9d8e09dd7;p=mirrors%2FASR3603_Z5.git gengxin --- diff --git a/2.sdk/sdk/gui/lv_watch/include/ui_nvm_interface.h b/2.sdk/sdk/gui/lv_watch/include/ui_nvm_interface.h index d04b2cdc..c8e5aeef 100644 --- a/2.sdk/sdk/gui/lv_watch/include/ui_nvm_interface.h +++ b/2.sdk/sdk/gui/lv_watch/include/ui_nvm_interface.h @@ -206,6 +206,9 @@ typedef struct typedef struct { +#ifdef __MMI_ZHSX_SUPPORT_AUTHENTICATE__ + uint8_t authenticate_flag; // 0-default; 1-done +#endif char watch_host_name[NV_WATCH_HOST_NAME_LEN_MAX]; int watch_host_port; char protocol_md5_key[NV_PROTOCOL_MD5_KEY_LEN_MAX]; diff --git a/2.sdk/sdk/gui/lv_watch/mmi_protocol/mmi_protocol_api.c b/2.sdk/sdk/gui/lv_watch/mmi_protocol/mmi_protocol_api.c index efb9229d..c5682822 100644 --- a/2.sdk/sdk/gui/lv_watch/mmi_protocol/mmi_protocol_api.c +++ b/2.sdk/sdk/gui/lv_watch/mmi_protocol/mmi_protocol_api.c @@ -76,6 +76,9 @@ void mmi_watch_protocol_data_nv_default(nv_watch_protocol_data_t * data) { PROTOCOL_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: nv default\n", __FUNCTION__, __LINE__); +#ifdef __MMI_ZHSX_SUPPORT_AUTHENTICATE__ + data->authenticate_flag = 0; +#endif strncpy(data->watch_host_name, MMI_PROTOCOL_IP_ADDRESS_DEFAULT, NV_WATCH_HOST_NAME_LEN_MAX - 1); data->watch_host_port = MMI_PROTOCOL_IP_PORT_DEFAULT; strncpy(data->protocol_md5_key, MMI_PROTOCOL_MD5_KEY_DEFAULT, NV_PROTOCOL_MD5_KEY_LEN_MAX - 1); @@ -85,6 +88,279 @@ void mmi_watch_protocol_data_nv_default(nv_watch_protocol_data_t * data) data->weather_update_interval = MMI_PROTOCOL_WEATHER_UPDATE_INTERVAL; } +#ifdef __MMI_ZHSX_SUPPORT_AUTHENTICATE__ +#include "hal/dm/cJSON.h" +#include "hal/dm/libhttpclient.h" + +//#define MMI_AUTHENTICATE_URL "http://app.api.thisiot.com/dev/device/check" +#define MMI_AUTHENTICATE_URL "http://iot.api.thisiot.com/dev/device/check" + +static struct http_client * s_authenticate_http_client = NULL; +static struct http_client_list * s_authenticate_http_client_list_header = NULL; +static uint8_t s_authenticate_http_try_count = 0; +static char * s_authenticate_http_revceive_buf = NULL; + +static char * response_data = NULL; +static int response_len = 0; + +/* +data: the data is received http context data +len: the length of http context data +num: the http response header "Content-Length" value +cbdata: user private data +*/ +static void response_cb(char * data, int len, int num, void *cbdata) +{ + char * temp; + + PROTOCOL_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: data_len=%d\n", __FUNCTION__, __LINE__, len); + + temp = (void*)malloc(response_len + len); + if (NULL == temp) + { + PROTOCOL_DEBUG_PRINTF("malloc failed\n"); + return; + } + memset(temp, 0, response_len + len); + if (response_data) { + memcpy(temp, response_data, response_len); + free(response_data); + } + memcpy(&temp[response_len], data, len); + response_data = temp; + response_len += len; + + PROTOCOL_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: response_len=%d\n", __FUNCTION__, __LINE__, response_len); +} + +void mmi_authenticate_http_resource_free(void) +{ + if(NULL != s_authenticate_http_client_list_header) + { + http_client_list_destroy(s_authenticate_http_client_list_header); + s_authenticate_http_client_list_header = NULL; + } + + if(NULL != s_authenticate_http_client) + { + http_client_shutdown(s_authenticate_http_client); + s_authenticate_http_client = NULL; + } + + if (response_data) + free(response_data); + + response_data = NULL; + response_len = 0; +} + +int mmi_authenticate_http_data(char * url) +{ + int response_code= 0; + int ret = 0; + + if (NULL != s_authenticate_http_client) + { + PROTOCOL_DEBUG_PRINTF("client not null\n"); + ret = -2; + goto EXIT; + } + + s_authenticate_http_client = http_client_init(); + http_client_setopt(s_authenticate_http_client, HTTPCLIENT_OPT_URL, url); /*set URL:support http/https and ip4/ip6*/ + http_client_setopt(s_authenticate_http_client, HTTPCLIENT_OPT_HTTP1_0, 0); /*"0" is HTTP 1.1, "1" is HTTP 1.0*/ + http_client_setopt(s_authenticate_http_client, HTTPCLIENT_OPT_RESPONSECB, response_cb); /*response callback*/ + //http_client_setopt(s_authenticate_http_client, HTTPCLIENT_OPT_RESPONSECB_DATA, private_data); /*set user private data,*/ + http_client_setopt(s_authenticate_http_client, HTTPCLIENT_OPT_METHOD, HTTPCLIENT_REQUEST_GET); /*set method,support GET/POST/PUT*/ + //http_client_setopt(s_authenticate_http_client, HTTPCLIENT_OPT_POSTDATA, post_data); /*post data is http context*/ + //http_client_setopt(s_authenticate_http_client, HTTPCLIENT_OPT_POSTLENGTH, strlen(post_data)); /*http context length*/ + + s_authenticate_http_client_list_header = http_client_list_append(s_authenticate_http_client_list_header, "Content-Type: application/json\r\n"); + s_authenticate_http_client_list_header = http_client_list_append(s_authenticate_http_client_list_header, "Accept: */*\r\n"); + http_client_setopt(s_authenticate_http_client, HTTPCLIENT_OPT_HTTPHEADER, s_authenticate_http_client_list_header); + + http_client_getinfo(s_authenticate_http_client, HTTPCLIENT_GETINFO_TCP_STATE, &response_code); + if(response_code != HTTP_TCP_ESTABLISHED) + { + PROTOCOL_DEBUG_PRINTF("http tcp not connecting\n"); + ret = -1; + goto EXIT; + } + + response_code = http_client_perform(s_authenticate_http_client); /*execute http send and recv*/ + if(response_code != HTTP_CLIENT_OK) + { + PROTOCOL_DEBUG_PRINTF("http perform have some wrong=%d\n", response_code); + ret = -3; + goto EXIT; + } + + http_client_getinfo(s_authenticate_http_client, HTTPCLIENT_GETINFO_RESPONSE_CODE, &response_code); + if (response_code != 200 && response_code != 204) + { + PROTOCOL_DEBUG_PRINTF("response is not 'HTTP OK'=%d\n", response_code); + } + + if(NULL != s_authenticate_http_revceive_buf) + { + lv_mem_free(s_authenticate_http_revceive_buf); + s_authenticate_http_revceive_buf = NULL; + } + s_authenticate_http_revceive_buf = lv_mem_alloc(response_len + 1); + if(NULL == s_authenticate_http_revceive_buf) + { + PROTOCOL_DEBUG_PRINTF("malloc failed\n"); + ret = -4; + goto EXIT; + } + _lv_memset_00(s_authenticate_http_revceive_buf, response_len + 1); + _lv_memcpy(s_authenticate_http_revceive_buf, response_data, response_len); + PROTOCOL_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: data_len=%d\n", __FUNCTION__, __LINE__, response_len); + +EXIT: + mmi_authenticate_http_resource_free(); + + return ret; +} + +void mmi_watch_authenticate_action(void) +{ + PROTOCOL_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: \n", __FUNCTION__, __LINE__); + + int ret_value = 0; + char url[256] = {0}; + char * bid = mmi_protocol_device_name_get(); + char * key = mmi_protocol_md5_key_str_get(); + char rand_str[32 + 1] = {0}; + char md5_input[256] = {0}; + unsigned char output[16] = {0}; + char md5_output[32 + 1] = {0}; + + char imei[16 + 1] = {0}; + app_adp_get_imei_req(imei); + + srand(Hal_Timer_Get_Counter()); + sprintf(rand_str, "%032d", rand()); + + sprintf(md5_input, "%s*%s*%s*%s", imei, bid, rand_str, key); // imei*bid*sonce*key + mbedtls_md5_ret(md5_input, strlen(md5_input), output); + for(int i = 0; i < 16; i++) + { + sprintf(&md5_output[i*2], "%02x", output[i]); + } + + // url + sprintf(url, "%s?imei=%s&bid=%s&sonce=%s&sign=%s", MMI_AUTHENTICATE_URL, imei, bid, rand_str, md5_output); + + s_authenticate_http_try_count = 0; + while(1) + { + s_authenticate_http_try_count++; + + int ret = mmi_authenticate_http_data(url); + PROTOCOL_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: ret=%d\n", __FUNCTION__, __LINE__, ret); + //cplog_printf("[@CHENG_DEBUG][%s][%d]: ret=%d\n", __FUNCTION__, __LINE__, ret); + if(0 == ret) + { + PROTOCOL_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: \n", __FUNCTION__, __LINE__); + ret_value = 0; + break; + } + else + { + if(3 <= s_authenticate_http_try_count) + { + PROTOCOL_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: \n", __FUNCTION__, __LINE__); + mmi_authenticate_http_resource_free(); + ret_value = -1; + break; + } + + uos_sleep(10); + } + } + + if(0 == ret_value) + { + PROTOCOL_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: %d\n", __FUNCTION__, __LINE__, strlen(s_authenticate_http_revceive_buf)); + PROTOCOL_DEBUG_PRINTF("[@CHENG_DEBUG]: %s\n", s_authenticate_http_revceive_buf); + PROTOCOL_DEBUG_PRINTF("[@CHENG_DEBUG]: %s\n", s_authenticate_http_revceive_buf + 100); + //cplog_printf("[@CHENG_DEBUG][%d]: %s\n", __LINE__, s_authenticate_http_revceive_buf); + //cplog_printf("[@CHENG_DEBUG]: %s\n", s_authenticate_http_revceive_buf + 100); + + cJSON * json_recv = cJSON_Parse(s_authenticate_http_revceive_buf); + if(NULL != json_recv) + { + if(cJSON_IsTrue(cJSON_GetObjectItem(json_recv, "success"))) // "isSuccess" + { + PROTOCOL_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: true\n", __FUNCTION__, __LINE__); + //cplog_printf("[@CHENG_DEBUG][%s][%d]: true\n", __FUNCTION__, __LINE__); + + cJSON * json_data = cJSON_GetObjectItem(json_recv, "data"); + if(NULL != json_data) + { + char * bid = cJSON_GetStringValue(cJSON_GetObjectItem(json_data, "bid")); + //PROTOCOL_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: bid=%s\n", __FUNCTION__, __LINE__, bid); + if(NULL != bid) + mmi_protocol_device_name_set(bid); + + char * key = cJSON_GetStringValue(cJSON_GetObjectItem(json_data, "secretKey")); + //PROTOCOL_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: key=%s\n", __FUNCTION__, __LINE__, key); + if(NULL != key) + mmi_protocol_md5_key_str_set(key); + + char * host = cJSON_GetStringValue(cJSON_GetObjectItem(json_data, "host")); + //PROTOCOL_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: host=%s\n", __FUNCTION__, __LINE__, host); + if(NULL != host) + mmi_watch_host_name_set(host); + + int port = cJSON_GetIntValue(cJSON_GetObjectItem(json_data, "port")); + //PROTOCOL_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: port=%d\n", __FUNCTION__, __LINE__, port); + if(0 < port) + mmi_watch_host_port_set(port); + } + + mmi_watch_authenticate_flag_set(1); + mmi_launcher_tcp_first_connect(0); + } + else + { + PROTOCOL_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: false\n", __FUNCTION__, __LINE__); + //cplog_printf("[@CHENG_DEBUG][%s][%d]: false\n", __FUNCTION__, __LINE__); + } + } + else + { + PROTOCOL_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: not json file\n", __FUNCTION__, __LINE__); + } + + if(NULL != json_recv) + { + PROTOCOL_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: free json root\n", __FUNCTION__, __LINE__); + cJSON_Delete(json_recv); + json_recv = NULL; + } + } + + if(NULL != s_authenticate_http_revceive_buf) + { + lv_mem_free(s_authenticate_http_revceive_buf); + s_authenticate_http_revceive_buf = NULL; + } +} + +uint8_t mmi_watch_authenticate_flag_get(void) +{ + return (s_watch_protocol_data.authenticate_flag); +} + +void mmi_watch_authenticate_flag_set(uint8_t flag) +{ + s_watch_protocol_data.authenticate_flag = flag; + mmi_watch_protocol_data_nv_write(); +} +#endif + char * mmi_watch_host_name_get(void) { return (s_watch_protocol_data.watch_host_name); diff --git a/2.sdk/sdk/gui/lv_watch/mmi_protocol/mmi_protocol_api.h b/2.sdk/sdk/gui/lv_watch/mmi_protocol/mmi_protocol_api.h index ad00a41d..844d939c 100644 --- a/2.sdk/sdk/gui/lv_watch/mmi_protocol/mmi_protocol_api.h +++ b/2.sdk/sdk/gui/lv_watch/mmi_protocol/mmi_protocol_api.h @@ -71,6 +71,11 @@ bool mmi_protocol_send_cmd_check(int cmd); void mmi_watch_protocol_data_nv_read(void); void mmi_watch_protocol_data_nv_default(nv_watch_protocol_data_t * data); +#ifdef __MMI_ZHSX_SUPPORT_AUTHENTICATE__ +uint8_t mmi_watch_authenticate_flag_get(void); +void mmi_watch_authenticate_flag_set(uint8_t flag); +void mmi_watch_authenticate_action(void); +#endif char * mmi_watch_host_name_get(void); void mmi_watch_host_name_set(char * ip_address); int mmi_watch_host_port_get(void); diff --git a/2.sdk/sdk/gui/lv_watch/modem/mmi_modem_tcp/mmi_modem_tcp_net.c b/2.sdk/sdk/gui/lv_watch/modem/mmi_modem_tcp/mmi_modem_tcp_net.c index a69a595d..542aa4cc 100644 --- a/2.sdk/sdk/gui/lv_watch/modem/mmi_modem_tcp/mmi_modem_tcp_net.c +++ b/2.sdk/sdk/gui/lv_watch/modem/mmi_modem_tcp/mmi_modem_tcp_net.c @@ -72,6 +72,68 @@ void mmi_launcher_tcp_first_connect(uint32_t para) } } } + +#ifdef __MMI_ZHSX_SUPPORT_AUTHENTICATE__ +#define MMI_AUTHENTICATE_CONNECT_TIME_INTERVAL (1000 * 5) // ms +#define MMI_AUTHENTICATE_CONNECT_TIMEOUT (1000 * 60 * 3) // ms + +static void * s_authenticate_connect_timer_id = NULL; +static uint32_t s_authenticate_connect_time = 0; + +void mmi_watch_msg_authenticate(void) +{ + send_task_msg_authenticate_action(); +} + +void mmi_watch_authenticate_pre(uint32_t para) +{ + if((FALSE == watch_modem_sim_present_check_req()) + && (MMI_AUTHENTICATE_CONNECT_TIMEOUT <= s_authenticate_connect_time) + ) + { + printf("[@CHENG_DEBUG][%s][%d]: \n",__FUNCTION__,__LINE__); + + if(NULL != s_authenticate_connect_timer_id) + { + uos_timer_stop(s_authenticate_connect_timer_id); + uos_timer_delete(s_authenticate_connect_timer_id); + s_authenticate_connect_timer_id = NULL; + } + s_authenticate_connect_time = 0; + } + else + { + if(MMI_MODEM_SIGNAL_BAR_0 == watch_modem_get_signal_bar_req() + && (MMI_AUTHENTICATE_CONNECT_TIMEOUT > s_authenticate_connect_time) + ) + { + printf("[@CHENG_DEBUG][%s][%d]: \n",__FUNCTION__,__LINE__); + + if(NULL == s_authenticate_connect_timer_id) + { + uos_timer_create(&s_authenticate_connect_timer_id); + } + uos_timer_stop(s_authenticate_connect_timer_id); + uos_timer_start(s_authenticate_connect_timer_id, MS_TO_TICKS(MMI_AUTHENTICATE_CONNECT_TIME_INTERVAL), 0, mmi_watch_authenticate_pre, NULL); + s_authenticate_connect_time += MMI_AUTHENTICATE_CONNECT_TIME_INTERVAL; + } + else + { + printf("[@CHENG_DEBUG][%s][%d]: \n",__FUNCTION__,__LINE__); + + if(NULL != s_authenticate_connect_timer_id) + { + uos_timer_stop(s_authenticate_connect_timer_id); + uos_timer_delete(s_authenticate_connect_timer_id); + s_authenticate_connect_timer_id = NULL; + } + s_authenticate_connect_time = 0; + mmi_watch_msg_authenticate(); + } + } +} +#endif + #endif /** @@ -83,7 +145,19 @@ void mmi_modem_tcp_task_init(void) printf("[@CHENG_DEBUG][%s][%d]: \n",__FUNCTION__,__LINE__); mmi_modem_tcp_task_interface_init(); + +#ifdef __MMI_ZHSX_SUPPORT_AUTHENTICATE__ + if(0 == mmi_watch_authenticate_flag_get()) + { + mmi_watch_authenticate_pre(0); + } + else + { + mmi_launcher_tcp_first_connect(0); + } +#else mmi_launcher_tcp_first_connect(0); +#endif #else diff --git a/2.sdk/sdk/gui/lv_watch/modem/mmi_modem_tcp/mmi_modem_tcp_task.c b/2.sdk/sdk/gui/lv_watch/modem/mmi_modem_tcp/mmi_modem_tcp_task.c index 2900143c..ba01b20f 100644 --- a/2.sdk/sdk/gui/lv_watch/modem/mmi_modem_tcp/mmi_modem_tcp_task.c +++ b/2.sdk/sdk/gui/lv_watch/modem/mmi_modem_tcp/mmi_modem_tcp_task.c @@ -47,6 +47,12 @@ VOID mmi_modem_tcp_task_handle(UI_INTER_MSG_ID MsgId, UI_Inter_Msg_t * pmsg) case MMI_MODEM_TCP_MSG_READ: mmi_modem_tcp_read_task_cmdmsg(pmsg); break; + + #ifdef __MMI_ZHSX_SUPPORT_AUTHENTICATE__ + case MMI_MODEM_TCP_MSG_AUTHENTICATE_ACTION: + mmi_watch_authenticate_action(); + break; + #endif default: break; @@ -162,6 +168,15 @@ void send_task_msg_tcp_read_msg(void *param) send_task_msg_tcp(MMI_MODEM_TCP_MSG_READ,param); } +#ifdef __MMI_ZHSX_SUPPORT_AUTHENTICATE__ +void send_task_msg_authenticate_action(void) +{ + send_task_msg_tcp(MMI_MODEM_TCP_MSG_AUTHENTICATE_ACTION, NULL); +} +#endif + + + /** * Send data to TCP */ diff --git a/2.sdk/sdk/gui/lv_watch/modem/mmi_modem_tcp/mmi_modem_tcp_task.h b/2.sdk/sdk/gui/lv_watch/modem/mmi_modem_tcp/mmi_modem_tcp_task.h index 9ea84194..10a54b29 100644 --- a/2.sdk/sdk/gui/lv_watch/modem/mmi_modem_tcp/mmi_modem_tcp_task.h +++ b/2.sdk/sdk/gui/lv_watch/modem/mmi_modem_tcp/mmi_modem_tcp_task.h @@ -26,6 +26,9 @@ typedef enum MMI_MODEM_TCP_MSG_RECONNECT, MMI_MODEM_TCP_MSG_SEND, MMI_MODEM_TCP_MSG_READ, +#ifdef __MMI_ZHSX_SUPPORT_AUTHENTICATE__ + MMI_MODEM_TCP_MSG_AUTHENTICATE_ACTION, +#endif MMI_MODEM_TCP_MSG_MAX, MMI_MODEM_TCP_MSG_ID_END = MMI_MODEM_TCP_MSG_MAX, @@ -39,6 +42,9 @@ void send_task_msg_tcp_connected(void *param); void send_task_msg_tcp_read_msg(void *param); void send_task_msg_tcp_connect(void *param); void send_task_msg_tcp_disconnect(void *param); +#ifdef __MMI_ZHSX_SUPPORT_AUTHENTICATE__ +void send_task_msg_authenticate_action(void); +#endif void send_task_msg_tcp_write_msg(int cmd_type, void* cmd_param);