From: @CHENG <2393524978@qq.com> Date: Wed, 25 Sep 2024 07:30:39 +0000 (+0800) Subject: __MMI_CUSTOM_AI_AUTHENTICATE_SUPPORT__ X-Git-Url: http://git.snicker.com.cn/?a=commitdiff_plain;h=994508613a3c3f7ac459ac342eb9fc3a13391cef;p=mirrors%2FASR3603_Z5.git __MMI_CUSTOM_AI_AUTHENTICATE_SUPPORT__ --- diff --git a/2.sdk/project/Z5_CIRCLE_240_240/gui/lv_watch/resource/png/mmi_aigc/ANIM_AIGC_LISTENING.gif b/2.sdk/project/Z5_CIRCLE_240_240/gui/lv_watch/resource/png/mmi_aigc/ANIM_AIGC_LISTENING.gif new file mode 100644 index 00000000..ca881c62 Binary files /dev/null and b/2.sdk/project/Z5_CIRCLE_240_240/gui/lv_watch/resource/png/mmi_aigc/ANIM_AIGC_LISTENING.gif differ diff --git a/2.sdk/project/Z5_CIRCLE_240_240/gui/lv_watch/resource/png/mmi_aigc/ANIM_AIGC_THINKING.gif b/2.sdk/project/Z5_CIRCLE_240_240/gui/lv_watch/resource/png/mmi_aigc/ANIM_AIGC_THINKING.gif new file mode 100644 index 00000000..8bde1339 Binary files /dev/null and b/2.sdk/project/Z5_CIRCLE_240_240/gui/lv_watch/resource/png/mmi_aigc/ANIM_AIGC_THINKING.gif differ diff --git a/2.sdk/project/Z5_CIRCLE_240_240/gui/lv_watch/resource/png/mmi_aigc/ICON_AIGC_CANCEL_BUTTON.png b/2.sdk/project/Z5_CIRCLE_240_240/gui/lv_watch/resource/png/mmi_aigc/ICON_AIGC_CANCEL_BUTTON.png new file mode 100644 index 00000000..de9d638e Binary files /dev/null and b/2.sdk/project/Z5_CIRCLE_240_240/gui/lv_watch/resource/png/mmi_aigc/ICON_AIGC_CANCEL_BUTTON.png differ diff --git a/2.sdk/project/Z5_CIRCLE_240_240/gui/lv_watch/resource/png/mmi_aigc/ICON_AIGC_DEFAULT.png b/2.sdk/project/Z5_CIRCLE_240_240/gui/lv_watch/resource/png/mmi_aigc/ICON_AIGC_DEFAULT.png new file mode 100644 index 00000000..2ecf59bf Binary files /dev/null and b/2.sdk/project/Z5_CIRCLE_240_240/gui/lv_watch/resource/png/mmi_aigc/ICON_AIGC_DEFAULT.png differ diff --git a/2.sdk/project/Z5_CIRCLE_240_240/gui/lv_watch/resource/png/mmi_aigc/ICON_AIGC_PROMPT.png b/2.sdk/project/Z5_CIRCLE_240_240/gui/lv_watch/resource/png/mmi_aigc/ICON_AIGC_PROMPT.png new file mode 100644 index 00000000..df210cec Binary files /dev/null and b/2.sdk/project/Z5_CIRCLE_240_240/gui/lv_watch/resource/png/mmi_aigc/ICON_AIGC_PROMPT.png differ diff --git a/2.sdk/project/Z5_CIRCLE_240_240/gui/lv_watch/resource/png/mmi_aigc/ICON_AIGC_RECORD_BUTTON_DEFAULT.png b/2.sdk/project/Z5_CIRCLE_240_240/gui/lv_watch/resource/png/mmi_aigc/ICON_AIGC_RECORD_BUTTON_DEFAULT.png new file mode 100644 index 00000000..2f8f4523 Binary files /dev/null and b/2.sdk/project/Z5_CIRCLE_240_240/gui/lv_watch/resource/png/mmi_aigc/ICON_AIGC_RECORD_BUTTON_DEFAULT.png differ diff --git a/2.sdk/project/Z5_CIRCLE_240_240/gui/lv_watch/resource/png/mmi_aigc/ICON_AIGC_RECORD_BUTTON_PRESSED.png b/2.sdk/project/Z5_CIRCLE_240_240/gui/lv_watch/resource/png/mmi_aigc/ICON_AIGC_RECORD_BUTTON_PRESSED.png new file mode 100644 index 00000000..80989d0a Binary files /dev/null and b/2.sdk/project/Z5_CIRCLE_240_240/gui/lv_watch/resource/png/mmi_aigc/ICON_AIGC_RECORD_BUTTON_PRESSED.png differ diff --git a/2.sdk/project/Z5_CIRCLE_240_240/gui/lv_watch/resource/png/mmi_aigc/ICON_AIGC_THINKING.png b/2.sdk/project/Z5_CIRCLE_240_240/gui/lv_watch/resource/png/mmi_aigc/ICON_AIGC_THINKING.png new file mode 100644 index 00000000..b5d28b80 Binary files /dev/null and b/2.sdk/project/Z5_CIRCLE_240_240/gui/lv_watch/resource/png/mmi_aigc/ICON_AIGC_THINKING.png differ diff --git a/2.sdk/project/Z5_CIRCLE_240_240/product/craneg_modem/config/watch/lv_watch_conf_CUSTOM.h b/2.sdk/project/Z5_CIRCLE_240_240/product/craneg_modem/config/watch/lv_watch_conf_CUSTOM.h index 54e587c6..240ee05c 100644 --- a/2.sdk/project/Z5_CIRCLE_240_240/product/craneg_modem/config/watch/lv_watch_conf_CUSTOM.h +++ b/2.sdk/project/Z5_CIRCLE_240_240/product/craneg_modem/config/watch/lv_watch_conf_CUSTOM.h @@ -224,6 +224,9 @@ #define __MMI_CUSTOM_VOICECHAT_SUPPORT__ +//#define __MMI_CUSTOM_AIGC_SUPPORT__ +#define __MMI_CUSTOM_AI_AUTHENTICATE_SUPPORT__ + #endif diff --git a/2.sdk/project/Z5_WL_K7/product/craneg_modem/config/watch/lv_watch_conf_CUSTOM.h b/2.sdk/project/Z5_WL_K7/product/craneg_modem/config/watch/lv_watch_conf_CUSTOM.h index a41fb7c8..0db8d324 100644 --- a/2.sdk/project/Z5_WL_K7/product/craneg_modem/config/watch/lv_watch_conf_CUSTOM.h +++ b/2.sdk/project/Z5_WL_K7/product/craneg_modem/config/watch/lv_watch_conf_CUSTOM.h @@ -200,6 +200,7 @@ #define __MMI_SOS_NUMBER_NAME_SUPPORT__ #define __MMI_CUSTOM_AIGC_SUPPORT__ +#define __MMI_CUSTOM_AI_AUTHENTICATE_SUPPORT__ #define __MMI_CHIP_TEMPERATURE_MONITOR_SUPPORT__ diff --git a/2.sdk/sdk/gui/lv_watch/lv_apps/custom_function/mmi_ai_authenticate.c b/2.sdk/sdk/gui/lv_watch/lv_apps/custom_function/mmi_ai_authenticate.c new file mode 100644 index 00000000..594b9b87 --- /dev/null +++ b/2.sdk/sdk/gui/lv_watch/lv_apps/custom_function/mmi_ai_authenticate.c @@ -0,0 +1,774 @@ + + + +#include "lv_watch.h" + +#ifdef __MMI_CUSTOM_AI_AUTHENTICATE_SUPPORT__ + + +#include "hal/dm/cJSON.h" +#include "hal/dm/libhttpclient.h" + + +static struct http_client * s_ai_http_client = NULL; +static struct http_client_list * s_ai_http_client_list_header = NULL; +static uint8_t s_ai_http_try_count = 0; +static char * s_ai_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; + + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: data_len=%d\n", __FUNCTION__, __LINE__, len); + + temp = (void*)malloc(response_len + len); + if (NULL == temp) + { + AI_AUTH_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; + + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: response_len=%d\n", __FUNCTION__, __LINE__, response_len); +} + +static void mmi_ai_authenticate_http_post_resource_free(void) +{ + if(NULL != s_ai_http_client_list_header) + { + http_client_list_destroy(s_ai_http_client_list_header); + s_ai_http_client_list_header = NULL; + } + + if(NULL != s_ai_http_client) + { + http_client_shutdown(s_ai_http_client); + s_ai_http_client = NULL; + } + + if (response_data) + free(response_data); + + response_data = NULL; + response_len = 0; +} + +static int mmi_ai_authenticate_http_post_json_data(char * url, char * post_data, char * token, uint32_t method) +{ + int response_code= 0; + int ret = 0; + + if (NULL != s_ai_http_client) + { + AI_AUTH_DEBUG_PRINTF("client not null\n"); + ret = -2; + goto EXIT; + } + if(HTTPCLIENT_REQUEST_MAX <= method) + { + AI_AUTH_DEBUG_PRINTF("method error\n"); + ret = -5; + goto EXIT; + } + + s_ai_http_client = http_client_init(); + http_client_setopt(s_ai_http_client, HTTPCLIENT_OPT_URL, url); /*set URL:support http/https and ip4/ip6*/ + http_client_setopt(s_ai_http_client, HTTPCLIENT_OPT_HTTP1_0, 0); /*"0" is HTTP 1.1, "1" is HTTP 1.0*/ + http_client_setopt(s_ai_http_client, HTTPCLIENT_OPT_RESPONSECB, response_cb); /*response callback*/ + //http_client_setopt(s_ai_http_client, HTTPCLIENT_OPT_RESPONSECB_DATA, private_data); /*set user private data,*/ + http_client_setopt(s_ai_http_client, HTTPCLIENT_OPT_METHOD, method); /*set method,support GET/POST/PUT*/ + http_client_setopt(s_ai_http_client, HTTPCLIENT_OPT_POSTDATA, post_data); /*post data is http context*/ + http_client_setopt(s_ai_http_client, HTTPCLIENT_OPT_POSTLENGTH, strlen(post_data)); /*http context length*/ + + if(NULL != token) + { + char temp_str[64] = {0}; + sprintf(temp_str, "token: %s\r\n", token); + s_ai_http_client_list_header = http_client_list_append(s_ai_http_client_list_header, temp_str); + } + s_ai_http_client_list_header = http_client_list_append(s_ai_http_client_list_header, "Content-Type: application/json\r\n"); + s_ai_http_client_list_header = http_client_list_append(s_ai_http_client_list_header, "Accept: */*\r\n"); + http_client_setopt(s_ai_http_client, HTTPCLIENT_OPT_HTTPHEADER, s_ai_http_client_list_header); + + http_client_getinfo(s_ai_http_client, HTTPCLIENT_GETINFO_TCP_STATE, &response_code); + if(response_code != HTTP_TCP_ESTABLISHED) + { + AI_AUTH_DEBUG_PRINTF("http tcp not connecting\n"); + ret = -1; + goto EXIT; + } + + response_code = http_client_perform(s_ai_http_client); /*execute http send and recv*/ + if(response_code != HTTP_CLIENT_OK) + { + AI_AUTH_DEBUG_PRINTF("http perform have some wrong=%d\n", response_code); + ret = -3; + goto EXIT; + } + + http_client_getinfo(s_ai_http_client, HTTPCLIENT_GETINFO_RESPONSE_CODE, &response_code); + if (response_code != 200 && response_code != 204) + { + AI_AUTH_DEBUG_PRINTF("response is not 'HTTP OK'=%d\n", response_code); + } + + if(NULL != s_ai_http_revceive_buf) + { + lv_mem_free(s_ai_http_revceive_buf); + s_ai_http_revceive_buf = NULL; + } + s_ai_http_revceive_buf = lv_mem_alloc(response_len + 1); + if(NULL == s_ai_http_revceive_buf) + { + AI_AUTH_DEBUG_PRINTF("malloc failed\n"); + ret = -4; + goto EXIT; + } + _lv_memset_00(s_ai_http_revceive_buf, response_len + 1); + _lv_memcpy(s_ai_http_revceive_buf, response_data, response_len); + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: data_len=%d\n", __FUNCTION__, __LINE__, response_len); + +EXIT: + mmi_ai_authenticate_http_post_resource_free(); + + return ret; +} + +int mmi_ai_authenticate_http_post_data(char * url, char * post_data, char * token, uint32_t method) +{ + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: %d\n", __FUNCTION__, __LINE__, strlen(post_data)); + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG]: %s\n", post_data); + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG]: %s\n", post_data + 100); + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG]: %s\n", post_data + 200); + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG]: %s\n", post_data + 300); + + int ret_value = 0; + + s_ai_http_try_count = 0; + + while(1) + { + s_ai_http_try_count++; + + int ret = mmi_ai_authenticate_http_post_json_data(url, post_data, token, method); + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: ret=%d\n", __FUNCTION__, __LINE__, ret); + if(0 == ret) + { + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: \n", __FUNCTION__, __LINE__); + ret_value = 0; + break; + } + else + { + if(AI_AUTHENTICATE_HTTP_TRY_COUNT <= s_ai_http_try_count) + { + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: \n", __FUNCTION__, __LINE__); + mmi_ai_authenticate_http_post_resource_free(); + ret_value = -1; + break; + } + + uos_sleep(10); + } + } + + return ret_value; +} + + +#include "../../../../product/craneg_modem/pcac/mbedTLS/include/mbedtls/cipher.h" +// key_len: MBEDTLS_CIPHER_AES_128_CBC-16; MBEDTLS_CIPHER_AES_192_CBC-24;MBEDTLS_CIPHER_AES_256_CBC-32 +static int mmi_ai_authenticate_aes_decrypt(unsigned char * aes_key, unsigned int key_len, + unsigned char * aes_iv, unsigned int iv_len, + unsigned char * input_buf, unsigned int input_len, + unsigned char * output_buf, unsigned int * output_len) +{ + int ret = -1; + unsigned char base64_output_buf[128 + 1] = {0}; + unsigned int base64_output_len = 0; + mbedtls_cipher_context_t cipher_context = {0}; + mbedtls_cipher_info_t * cipher_info = NULL; + + //mbedtls_base64_encode mbedtls_base64_decode + ret = mbedtls_base64_decode(base64_output_buf, 128, &base64_output_len, input_buf, input_len); + if(0 != ret) + { + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: base64 decode error,ret=%d\n", __FUNCTION__, __LINE__, ret); + return ret; + } + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: base64_output_len=%d\n", __FUNCTION__, __LINE__, base64_output_len); + + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: %s\n", __FUNCTION__, __LINE__, aes_key); + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: %s\n", __FUNCTION__, __LINE__, aes_iv); + + mbedtls_cipher_init(&cipher_context); + + cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_256_CBC); + ret = mbedtls_cipher_setup(&cipher_context, cipher_info); + if(0 != ret) + { + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: setup error,ret=%d\n", __FUNCTION__, __LINE__, ret); + return ret; + } + + ret = mbedtls_cipher_setkey(&cipher_context, aes_key, 8 * key_len, MBEDTLS_DECRYPT); + if(0 != ret) + { + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: setkey error,ret=%d\n", __FUNCTION__, __LINE__, ret); + return ret; + } + + if(0 != (base64_output_len % 8)) // PKCS5Padding + { + unsigned char padding_len = 8 - (base64_output_len % 8); + unsigned int new_input_len = base64_output_len + padding_len; + unsigned char * new_input_buf = lv_mem_alloc(new_input_len + 1); + if(NULL == new_input_buf) + { + AI_AUTH_DEBUG_PRINTF("malloc failed\n"); + return ret; + } + _lv_memset_00(new_input_buf, new_input_len + 1); + _lv_memcpy(new_input_buf, base64_output_buf, base64_output_len); + + unsigned char i = 0; + for(i = base64_output_len; i < new_input_len; i++) + { + new_input_buf[i] = (unsigned char) padding_len; + } + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: padding_len=%d,new_len=%d\n", __FUNCTION__, __LINE__, padding_len, new_input_len); + + ret = mbedtls_cipher_crypt(&cipher_context, aes_iv, iv_len, new_input_buf, new_input_len, output_buf, output_len); + lv_mem_free(new_input_buf); + } + else + { + ret = mbedtls_cipher_crypt(&cipher_context, aes_iv, iv_len, base64_output_buf, base64_output_len, output_buf, output_len); + } + if(0 != ret) + { + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: crypt error,ret=%d\n", __FUNCTION__, __LINE__, ret); + return ret; + } + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: output_len=%d\n", __FUNCTION__, __LINE__, *output_len); + + mbedtls_cipher_free(&cipher_context); + + return ret; +} + +static int mmi_ai_authenticate_aes_encrypt(unsigned char * aes_key, unsigned int key_len, + unsigned char * aes_iv, unsigned int iv_len, + unsigned char * input_buf, unsigned int input_len, + unsigned char * output_buf, unsigned int * output_len) +{ + int ret = -1; + unsigned char base64_input_buf[128 + 1] = {0}; + unsigned int base64_input_len = 0; + mbedtls_cipher_context_t cipher_context = {0}; + mbedtls_cipher_info_t * cipher_info = NULL; + + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: %d,%s\n", __FUNCTION__, __LINE__, key_len, aes_key); + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: %d,%s\n", __FUNCTION__, __LINE__, iv_len, aes_iv); + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: %d,%s\n", __FUNCTION__, __LINE__, input_len, input_buf); + + mbedtls_cipher_init(&cipher_context); + + cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_256_CBC); + ret = mbedtls_cipher_setup(&cipher_context, cipher_info); + if(0 != ret) + { + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: setup error,ret=%d\n", __FUNCTION__, __LINE__, ret); + return ret; + } + + ret = mbedtls_cipher_setkey(&cipher_context, aes_key, 8 * key_len, MBEDTLS_ENCRYPT); + if(0 != ret) + { + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: setkey error,ret=%d\n", __FUNCTION__, __LINE__, ret); + return ret; + } + + ret = mbedtls_cipher_crypt(&cipher_context, aes_iv, iv_len, input_buf, input_len, base64_input_buf, &base64_input_len); + if(0 != ret) + { + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: crypt error,ret=%d\n", __FUNCTION__, __LINE__, ret); + return ret; + } + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: output_len=%d\n", __FUNCTION__, __LINE__, base64_input_len); + + mbedtls_cipher_free(&cipher_context); + + + //mbedtls_base64_encode mbedtls_base64_decode + ret = mbedtls_base64_encode(output_buf, 128, &output_len, base64_input_buf, base64_input_len); + if(0 != ret) + { + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: base64 encode error,ret=%d\n", __FUNCTION__, __LINE__, ret); + return ret; + } + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: base64_output_len=%d\n", __FUNCTION__, __LINE__, *output_len); + + return ret; +} + +static void mmi_ai_random_string_generate(char * str_buf, uint16_t str_size, uint16_t str_len) +{ + if(NULL == str_buf || 0 == str_len || str_size < (str_len + 1)) + return; + + char charupper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + char charlower[] = "abcdefghijklmnopqrstuvwxyz"; + char charnumber[] = "1234567890"; + uint8_t i = 0; + for(i = 0; i < str_len; i++) + { + unsigned int seed = 0; + long rand_num = 0; + uint8_t flag = 0; + + Hal_Rtc_Gettime_Count_Utc(&seed); + srand(seed); + rand_num = arc4random() * rand(); + flag = rand_num % 3; + + if(0 == flag) + str_buf[i] = charupper[rand_num % (strlen(charupper))]; + else if(1 == flag) + str_buf[i] = charlower[rand_num % (strlen(charlower))]; + else + str_buf[i] = charnumber[rand_num % (strlen(charnumber))]; + } + str_buf[i] = '\0'; +} + + +static char s_ai_device_password[AI_AUTHENTICATE_PASSWORD_LEN + 1] = {0}; +static char s_ai_device_token[AI_AUTHENTICATE_TOKEN_LEN + 1] = {0}; + +char * mmi_ai_device_password_get(void) +{ + return s_ai_device_password; +} + +int mmi_ai_device_password_set(char * password, int len) +{ + if(NULL == password) + return -1; + + if(AI_AUTHENTICATE_PASSWORD_LEN < len) + return -2; + + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: %d\n", __FUNCTION__, __LINE__, len); + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG]: %s\n", password); + _lv_memset_00(s_ai_device_password, AI_AUTHENTICATE_PASSWORD_LEN + 1); + _lv_memcpy(s_ai_device_password, password, len); + return 0; +} + +char * mmi_ai_device_token_get(void) +{ + return s_ai_device_token; +} + +int mmi_ai_device_token_set(char * token) +{ + if(NULL == token) + return -1; + + if(AI_AUTHENTICATE_TOKEN_LEN < strlen(token)) + return -2; + + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: \n", __FUNCTION__, __LINE__); + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG]: %s\n", token); + _lv_memset_00(s_ai_device_token, AI_AUTHENTICATE_TOKEN_LEN + 1); + _lv_memcpy(s_ai_device_token, token, strlen(token)); + return 0; +} + +void mmi_ai_device_active(void) +{ + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: \n", __FUNCTION__, __LINE__); + + char * device_password = mmi_ai_device_password_get(); + if(0 != strlen(device_password)) + { + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: \n", __FUNCTION__, __LINE__); + mmi_ai_device_login(); + return; + } + + int temp_len = 0; + int post_data_len = 512; + char * post_data = lv_mem_alloc(post_data_len); + if(NULL == post_data) + return ; + _lv_memset_00(post_data, post_data_len); + + char imei[16 + 1] = {0}; + app_adp_get_imei_req(imei); + temp_len += sprintf(post_data + temp_len, "{\"deviceId\":\"%s\",", imei); + + temp_len += sprintf(post_data + temp_len, "\"pid\":\"%s\",", AI_AUTHENTICATE_DEVICE_PID); + + uint32_t cur_time_stamp = 0; + Hal_Rtc_Gettime_Count_Utc(&cur_time_stamp); + temp_len += sprintf(post_data + temp_len, "\"timeStamp\":\"%d000\",", cur_time_stamp); // ms + + char md5_input[256] = {0}; + unsigned char output[16] = {0}; + char md5_output[32 + 1] = {0}; + + sprintf(md5_input, "%s%s%d000%s", imei, AI_AUTHENTICATE_DEVICE_PID, cur_time_stamp, AI_AUTHENTICATE_DEVICE_KEY); // Md5(deviceId+pid+timeStamp+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]); + } + temp_len += sprintf(post_data + temp_len, "\"sign\":\"%s\",", md5_output); + + temp_len += sprintf(post_data + temp_len, "\"signMethod\":\"md5\"}"); + + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: %d\n", __FUNCTION__, __LINE__, temp_len); + + char url[256] = {0}; + sprintf(url, "%s%s", AI_AUTHENTICATE_BASE_URL, "/dev/auth/active"); + int ret = mmi_ai_authenticate_http_post_data(url, post_data, NULL, HTTPCLIENT_REQUEST_PUT); + + if(NULL != post_data) + { + lv_mem_free(post_data); + post_data = NULL; + } + + if(0 == ret) + { + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: %d\n", __FUNCTION__, __LINE__, strlen(s_ai_http_revceive_buf)); + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG]: %s\n", s_ai_http_revceive_buf); + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG]: %s\n", s_ai_http_revceive_buf + 100); + + cJSON * json_recv = cJSON_Parse(s_ai_http_revceive_buf); + if(NULL != json_recv) + { + if(cJSON_IsTrue(cJSON_GetObjectItem(json_recv, "success"))) + { + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: true\n", __FUNCTION__, __LINE__); + + cJSON * json_data = cJSON_GetObjectItem(json_recv, "data"); + char * encrypte_password = cJSON_GetStringValue(cJSON_GetObjectItem(json_data, "password")); + char * random_str = cJSON_GetStringValue(cJSON_GetObjectItem(json_data, "nonce")); + + // update + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%d]: %s\n", __LINE__, encrypte_password); + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%d]: %s\n", __LINE__, random_str); + + unsigned char aes_key[32 + 1] = {0}; + unsigned char aes_iv[16 + 1] = {0}; + unsigned int input_len = 0; + unsigned char output_buf[128 + 1] = {0}; + unsigned int output_len = 0; + int ret = -1; + + _lv_memcpy(aes_key, AI_AUTHENTICATE_DEVICE_KEY, 32); + _lv_memcpy(aes_iv, random_str, 16); + input_len = strlen(encrypte_password); + + ret = mmi_ai_authenticate_aes_decrypt(aes_key, 32, aes_iv, 16, encrypte_password, input_len, output_buf, &output_len); + if(0 == ret) + { + mmi_ai_device_password_set(output_buf, output_len); + mmi_ai_device_login(); + } + } + else + { + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: false\n", __FUNCTION__, __LINE__); + } + } + else + { + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: not json file\n", __FUNCTION__, __LINE__); + } + + if(NULL != json_recv) + { + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: free json root\n", __FUNCTION__, __LINE__); + cJSON_Delete(json_recv); + json_recv = NULL; + } + } + + if(NULL != s_ai_http_revceive_buf) + { + lv_mem_free(s_ai_http_revceive_buf); + s_ai_http_revceive_buf = NULL; + } +} + +void mmi_ai_device_login(void) +{ + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: \n", __FUNCTION__, __LINE__); + + int temp_len = 0; + int post_data_len = 512; + char * post_data = lv_mem_alloc(post_data_len); + if(NULL == post_data) + return ; + _lv_memset_00(post_data, post_data_len); + + char imei[16 + 1] = {0}; + app_adp_get_imei_req(imei); + temp_len += sprintf(post_data + temp_len, "{\"deviceId\":\"%s\",", imei); + + char random_str[16 + 1] = {0}; + mmi_ai_random_string_generate(random_str, 16 + 1, 16); + temp_len += sprintf(post_data + temp_len, "\"nonce\":\"%s\",", random_str); + + char * device_password = mmi_ai_device_password_get(); + unsigned char aes_key[32 + 1] = {0}; + unsigned char aes_iv[16 + 1] = {0}; + unsigned int input_len = 0; + unsigned char output_buf[128 + 1] = {0}; + unsigned int output_len = 0; + + _lv_memcpy(aes_key, AI_AUTHENTICATE_DEVICE_KEY, 32); + _lv_memcpy(aes_iv, random_str, 16); + input_len = strlen(device_password); + + mmi_ai_authenticate_aes_encrypt(aes_key, 32, aes_iv, 16, device_password, input_len, output_buf, &output_len); + temp_len += sprintf(post_data + temp_len, "\"password\":\"%s\"}", output_buf); + + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: %d\n", __FUNCTION__, __LINE__, temp_len); + + char url[256] = {0}; + sprintf(url, "%s%s", AI_AUTHENTICATE_BASE_URL, "/dev/auth/sign_in"); + int ret = mmi_ai_authenticate_http_post_data(url, post_data, NULL, HTTPCLIENT_REQUEST_POST); + + if(NULL != post_data) + { + lv_mem_free(post_data); + post_data = NULL; + } + + if(0 == ret) + { + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: %d\n", __FUNCTION__, __LINE__, strlen(s_ai_http_revceive_buf)); + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG]: %s\n", s_ai_http_revceive_buf); + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG]: %s\n", s_ai_http_revceive_buf + 100); + + cJSON * json_recv = cJSON_Parse(s_ai_http_revceive_buf); + if(NULL != json_recv) + { + if(cJSON_IsTrue(cJSON_GetObjectItem(json_recv, "success"))) + { + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: true\n", __FUNCTION__, __LINE__); + + cJSON * json_data = cJSON_GetObjectItem(json_recv, "data"); + char * token_str = cJSON_GetStringValue(cJSON_GetObjectItem(json_data, "token")); + + // update + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%d]: %s\n", __LINE__, token_str); + if(NULL != token_str) + { + mmi_ai_device_token_set(token_str); + } + } + else + { + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: false\n", __FUNCTION__, __LINE__); + } + } + else + { + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: not json file\n", __FUNCTION__, __LINE__); + } + + if(NULL != json_recv) + { + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: free json root\n", __FUNCTION__, __LINE__); + cJSON_Delete(json_recv); + json_recv = NULL; + } + } + + if(NULL != s_ai_http_revceive_buf) + { + lv_mem_free(s_ai_http_revceive_buf); + s_ai_http_revceive_buf = NULL; + } +} + + + +#if 0 +#include "../../../../product/craneg_modem/pcac/mbedtls/include/mbedtls/aes.h" +void __test_decrypt(void) +{ + unsigned char * encrypte_password = "PT+ATeLgL/T1atezfRvUHos5mKi7IYuz5n5Hs/kWePE="; + unsigned char * key_str = "4EepovakpbqhZUnUDwH42KAADYGv4UUe"; + unsigned char * random_str = "gEzghPxPt5cJv2yK"; + unsigned int input_len = 0; + unsigned char output_buf[128 + 1] = {0}; + unsigned char aes_key[32 + 1] = {0}; + unsigned char aes_iv[16 + 1] = {0}; + int ret = -1; + + mbedtls_aes_context aes_ctx = {0}; + + mbedtls_aes_init( &aes_ctx ); + + ret = mbedtls_aes_setkey_dec( &aes_ctx, aes_key, 32 * 8 ); + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: ret=%d\n", __FUNCTION__, __LINE__, ret); + + _lv_memcpy(aes_key, key_str, 32); + _lv_memcpy(aes_iv, random_str, 16); + input_len = strlen(encrypte_password); + if(0 != (input_len % 8)) // PKCS5Padding + { + unsigned char padding_len = 8 - (input_len % 8); + unsigned int new_len = input_len + padding_len; + unsigned char * new_buf = lv_mem_alloc(new_len + 1); + if(NULL == new_buf) + { + AI_AUTH_DEBUG_PRINTF("malloc failed\n"); + return ret; + } + _lv_memset_00(new_buf, new_len + 1); + _lv_memcpy(new_buf, encrypte_password, input_len); + + unsigned char i = 0; + for(i = input_len; i < new_len; i++) + { + new_buf[i] = (unsigned char) padding_len; + } + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: padding_len=%d,new_len=%d\n", __FUNCTION__, __LINE__, padding_len, new_len); + for(i = 0; i < new_len; i += 8) + { + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG]%x,%x,%x,%x,%x,%x,%x,%x,", + new_buf[i], new_buf[i+1], new_buf[i+2], new_buf[i+3], + new_buf[i+4], new_buf[i+5], new_buf[i+6], new_buf[i+7]); + } + + ret = mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_DECRYPT, new_len, aes_iv, new_buf, output_buf); + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: ret=%d\n", __FUNCTION__, __LINE__, ret); + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: %d,%s\n", __FUNCTION__, __LINE__, strlen(output_buf), output_buf); + + lv_mem_free(new_buf); + } + else + { + ret = mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_DECRYPT, input_len, aes_iv, encrypte_password, output_buf); + AI_AUTH_DEBUG_PRINTF("[@CHENG_DEBUG][%s][%d]: ret=%d\n", __FUNCTION__, __LINE__, ret); + } + mbedtls_aes_free( &aes_ctx ); + +} + + +#include "../../../../product/craneg_modem/pcac/mbedTLS/include/mbedtls/cipher.h" +void __test_cipher_decrypt(void) +{ + unsigned char * encrypte_password = "PT+ATeLgL/T1atezfRvUHos5mKi7IYuz5n5Hs/kWePE="; + unsigned char * key_str = "4EepovakpbqhZUnUDwH42KAADYGv4UUe"; + unsigned char * random_str = "gEzghPxPt5cJv2yK"; + unsigned int input_len = 0; + unsigned char output_buf[128 + 1] = {0}; + unsigned int output_len = 0; + unsigned char aes_key[32 + 1] = {0}; + unsigned char aes_iv[16 + 1] = {0}; + int ret = -1; + + unsigned char base64_output_buf[128 + 1] = {0}; + unsigned int base64_output_len = 0; + + //mbedtls_base64_encode mbedtls_base64_decode + if(0 != mbedtls_base64_decode(base64_output_buf, 128, &base64_output_len, encrypte_password, strlen(encrypte_password))) + { + printf("[@CHENG_DEBUG][%s][%d]: base64 decode error\n", __FUNCTION__, __LINE__); + return ; + } + printf("[@CHENG_DEBUG][%s][%d]: base64_output_len=%d\n", __FUNCTION__, __LINE__, base64_output_len); + + + _lv_memcpy(aes_key, key_str, 32); + _lv_memcpy(aes_iv, random_str, 16); + + mbedtls_cipher_context_t cipher_context = {0}; + mbedtls_cipher_info_t * cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_256_CBC); + + mbedtls_cipher_init(&cipher_context); + + ret = mbedtls_cipher_setup(&cipher_context, cipher_info); + printf("[@CHENG_DEBUG][%s][%d]: ret=%d\n", __FUNCTION__, __LINE__, ret); + + ret = mbedtls_cipher_setkey(&cipher_context, aes_key, 8 * 32, MBEDTLS_DECRYPT); + printf("[@CHENG_DEBUG][%s][%d]: ret=%d\n", __FUNCTION__, __LINE__, ret); + + //input_len = strlen(encrypte_password); + input_len = strlen(base64_output_buf); + if(0 != (input_len % 8)) // PKCS5Padding + { + unsigned char padding_len = 8 - (input_len % 8); + unsigned int new_len = input_len + padding_len; + unsigned char * new_buf = lv_mem_alloc(new_len + 1); + if(NULL == new_buf) + { + printf("malloc failed\n"); + return ret; + } + _lv_memset_00(new_buf, new_len + 1); + //_lv_memcpy(new_buf, encrypte_password, input_len); + _lv_memcpy(new_buf, base64_output_buf, input_len); + + unsigned char i = 0; + for(i = input_len; i < new_len; i++) + { + new_buf[i] = (unsigned char) padding_len; + } + printf("[@CHENG_DEBUG][%s][%d]: padding_len=%d,new_len=%d\n", __FUNCTION__, __LINE__, padding_len, new_len); + for(i = 0; i < new_len; i += 8) + { + printf("[@CHENG_DEBUG]%x,%x,%x,%x,%x,%x,%x,%x,", + new_buf[i], new_buf[i+1], new_buf[i+2], new_buf[i+3], + new_buf[i+4], new_buf[i+5], new_buf[i+6], new_buf[i+7]); + } + + ret = mbedtls_cipher_crypt(&cipher_context, aes_iv, 16, new_buf, new_len, output_buf, &output_len); + printf("[@CHENG_DEBUG][%s][%d]: ret=%d,output_len=%d\n", __FUNCTION__, __LINE__, ret, output_len); + printf("[@CHENG_DEBUG][%s][%d]: %d,%s\n", __FUNCTION__, __LINE__, strlen(output_buf), output_buf); + + lv_mem_free(new_buf); + } + else + { + //ret = mbedtls_cipher_crypt(&cipher_context, aes_iv, 16, encrypte_password, input_len, output_buf, &output_len); + ret = mbedtls_cipher_crypt(&cipher_context, aes_iv, 16, base64_output_buf, input_len, output_buf, &output_len); + printf("[@CHENG_DEBUG][%s][%d]: ret=%d,output_len=%d\n", __FUNCTION__, __LINE__, ret, output_len); + printf("[@CHENG_DEBUG][%s][%d]: %d,%s\n", __FUNCTION__, __LINE__, strlen(output_buf), output_buf); + } + + mbedtls_cipher_free(&cipher_context); + + //return ret; +} +#endif + + +#endif + diff --git a/2.sdk/sdk/gui/lv_watch/lv_apps/custom_function/mmi_ai_authenticate.h b/2.sdk/sdk/gui/lv_watch/lv_apps/custom_function/mmi_ai_authenticate.h new file mode 100644 index 00000000..91b1e07c --- /dev/null +++ b/2.sdk/sdk/gui/lv_watch/lv_apps/custom_function/mmi_ai_authenticate.h @@ -0,0 +1,54 @@ + + + +#ifndef __MMI_AI_AUTHENTICATE_H__ +#define __MMI_AI_AUTHENTICATE_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef LV_CONF_INCLUDE_SIMPLE +#include "lvgl.h" +#include "lv_watch_conf.h" +#else +#include "../../../lvgl/lvgl.h" +#include "../../../lv_watch_conf.h" +#endif + +#ifdef __MMI_CUSTOM_AI_AUTHENTICATE_SUPPORT__ + +#define __MMI_AI_AUTH_DEBUG__ +#ifdef __MMI_AI_AUTH_DEBUG__ +#define AI_AUTH_DEBUG_PRINTF(fmt, ...) printf("[AUTH]"fmt, ##__VA_ARGS__) +//#define AI_AUTH_DEBUG_PRINTF(fmt, ...) CPUartLogPrintf("[AUTH]"fmt, ##__VA_ARGS__) +//#define AI_AUTH_DEBUG_PRINTF(fmt, ...) cplog_printf("[AUTH]"fmt, ##__VA_ARGS__) +#else +#define AI_AUTH_DEBUG_PRINTF(fmt, ...) +#endif + +#define AI_AUTHENTICATE_BASE_URL ("http://dmp.raisound.com/bz-api") // ("https://dmp.raisound.com/bz-api") +#define AI_AUTHENTICATE_PASSWORD_LEN (64) +#define AI_AUTHENTICATE_TOKEN_LEN (64) + +#define AI_AUTHENTICATE_HTTP_TRY_COUNT (3) + +#define AI_AUTHENTICATE_DEVICE_PID ("qAtdDOGXgXYjT2HGFr3a") +#define AI_AUTHENTICATE_DEVICE_KEY ("MWrMZjdtcPhj41jWuoqFAhTmqyFluqj1") + + + + +void mmi_ai_device_active(void); +void mmi_ai_device_login(void); +char * mmi_ai_device_token_get(void); + + +#endif + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif +