{
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);
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);
{
if(NULL == ip_address)
return;
- if(NV_WATCH_HOST_NAME_LEN_MAX <= strlen(ip_address))
+ if(NV_WATCH_HOST_NAME_LEN_MIN > strlen(ip_address) || NV_WATCH_HOST_NAME_LEN_MAX <= strlen(ip_address))
return;
_lv_memset_00(s_watch_protocol_data.watch_host_name, NV_WATCH_HOST_NAME_LEN_MAX);
void mmi_protocol_md5_key_str_set(char * md5_key)
{
- if(NULL == md5_key)
- return;
- if(NV_PROTOCOL_MD5_KEY_LEN_MAX <= strlen(md5_key))
+ if(NULL == md5_key || 0 == strlen(md5_key) || NV_PROTOCOL_MD5_KEY_LEN_MAX <= strlen(md5_key))
return;
_lv_memset_00(s_watch_protocol_data.protocol_md5_key, NV_PROTOCOL_MD5_KEY_LEN_MAX);
void mmi_protocol_device_name_set(char * device_name)
{
- if(NULL == device_name)
- return;
- if(NV_PROTOCOL_DEVICE_NAME_LEN_MAX <= strlen(device_name))
+ if(NULL == device_name || 0 == strlen(device_name) || NV_PROTOCOL_DEVICE_NAME_LEN_MAX <= strlen(device_name))
return;
_lv_memset_00(s_watch_protocol_data.protocol_device_name, NV_PROTOCOL_DEVICE_NAME_LEN_MAX);
}
}
}
+
+#ifdef __MMI_ZHSX_SUPPORT_AUTHENTICATE__
+#define MMI_AUTHENTICATE_CONNECT_TIME_INTERVAL (1000 * 5) // ms
+#define MMI_AUTHENTICATE_CONNECT_TIMEOUT (1000 * 60) // 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
/**
{
#ifdef __MMI_ZHSX_SUPPORT__
printf("[@CHENG_DEBUG][%s][%d]: \n",__FUNCTION__,__LINE__);
+ //cplog_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