From: luozheng <1732036298@qq.com> Date: Thu, 22 May 2025 07:01:17 +0000 (+0800) Subject: 乘车码代码合并,待测试 X-Git-Tag: leiniao_debug_1.0.0_20250922_020716~287 X-Git-Url: http://git.snicker.com.cn/css/static/gitweb.css?a=commitdiff_plain;h=922715e81110a93341266119a6be04fd2511a252;p=mirrors%2FFD230.git 乘车码代码合并,待测试 --- diff --git a/code/sdk/application/brandy/brandy_standard/thread_pri_init.c b/code/sdk/application/brandy/brandy_standard/thread_pri_init.c index ce383e4a6..72a4cc46f 100644 --- a/code/sdk/application/brandy/brandy_standard/thread_pri_init.c +++ b/code/sdk/application/brandy/brandy_standard/thread_pri_init.c @@ -523,8 +523,8 @@ static void thread_main(const void *unused) #endif #ifdef ENABLE_CUSTOM_I2C - custom_i2c_init(I2C_BUS_3); - custom_i2c_init(I2C_BUS_0); + // custom_i2c_init(I2C_BUS_3); + // custom_i2c_init(I2C_BUS_0); custom_i2c_init(I2C_BUS_2); #ifdef SUPORT_MAG_SERVICE @@ -558,11 +558,11 @@ static void thread_main(const void *unused) #endif start_custom_ble_service(); -// #ifdef SUPPORT_ALIPAY_SEC -// #if !defined(A5133_DEMO_SERVER) && !defined(A5133_DEMO_CLIENT) -// uapi_alipay_svr_init(); -// #endif -// #endif +#ifdef SUPPORT_ALIPAY_SEC +#if !defined(A5133_DEMO_SERVER) && !defined(A5133_DEMO_CLIENT) + uapi_alipay_svr_init(); +#endif +#endif #ifdef SUPPORT_A5133_SERVICE uapi_a5133_service_init(); diff --git a/code/sdk/application/wearable/alipay/driver_adapt/soc_adapt.c b/code/sdk/application/wearable/alipay/driver_adapt/soc_adapt.c index 2aa4dd3fe..7f3b052dc 100644 --- a/code/sdk/application/wearable/alipay/driver_adapt/soc_adapt.c +++ b/code/sdk/application/wearable/alipay/driver_adapt/soc_adapt.c @@ -25,7 +25,7 @@ unsigned char *IIC_Master_Init(void) { errcode_t result; result = uapi_i2c_master_init(ALIPAY_SEC_I2C_INDEX, 100000, I2C_SPEED_MODE_SS); /* 100000: 100KHz */ - if (result != ERRCODE_SUCC) { + if (result != ERRCODE_SUCC && result!= ERRCODE_I2C_ALREADY_INIT) { MyPrintf("se i2c init fail! ret=0x%x", result); return NULL; } diff --git a/code/sdk/application/wearable/fd230/nativeapp/nativeui/aivoice/src/AiVoiceModel.cpp b/code/sdk/application/wearable/fd230/nativeapp/nativeui/aivoice/src/AiVoiceModel.cpp index 8bcb8ae7c..14dbbaa60 100644 --- a/code/sdk/application/wearable/fd230/nativeapp/nativeui/aivoice/src/AiVoiceModel.cpp +++ b/code/sdk/application/wearable/fd230/nativeapp/nativeui/aivoice/src/AiVoiceModel.cpp @@ -194,7 +194,9 @@ namespace OHOS void AiVoiceModel::CancelCurAIChat(void) { // cancel_AI_cur_chat(); //线程发送取消当前对话 + #ifdef SUPPORT_BLE_SERVICE cancel_AI_cur_chat_();// 直接发送取消当前对话 + #endif } int AiVoiceModel::GetAIResponseType(void) diff --git a/code/sdk/application/wearable/fd230/nativeapp/nativeui/watchQRcode/src/WatchQRCodeView.cpp b/code/sdk/application/wearable/fd230/nativeapp/nativeui/watchQRcode/src/WatchQRCodeView.cpp index 152472286..c2df96d04 100644 --- a/code/sdk/application/wearable/fd230/nativeapp/nativeui/watchQRcode/src/WatchQRCodeView.cpp +++ b/code/sdk/application/wearable/fd230/nativeapp/nativeui/watchQRcode/src/WatchQRCodeView.cpp @@ -63,13 +63,15 @@ namespace OHOS extern "C" { - -// #include "build_version.h" -// #include "battery_hal.h" -// extern uint8_t battery_get_voltage(float *voltage); -// const char *get_version_string(void); + #ifdef SUPPORT_BLE_SERVICE +#include "build_version.h" +#include "battery_hal.h" + extern uint8_t battery_get_voltage(float *voltage); + const char *get_version_string(void); +#else extern uint8_t battery_get_voltage(float *voltage){ return 0;} const char *get_version_string(void); +#endif } diff --git a/code/sdk/application/wearable/service/alipay_msg/CMakeLists.txt b/code/sdk/application/wearable/service/alipay_msg/CMakeLists.txt index 296189182..01b067d30 100644 --- a/code/sdk/application/wearable/service/alipay_msg/CMakeLists.txt +++ b/code/sdk/application/wearable/service/alipay_msg/CMakeLists.txt @@ -10,6 +10,7 @@ set(PRIVATE_HEADER ${ROOT_DIR}/middleware/services/gui/uikit/utils/interfaces/kits/gfx_utils ${ROOT_DIR}/include/middleware/services/bts/br ${ROOT_DIR}/include/middleware/services/bts/common + ${ROOT_DIR}/zhihui/services/ble_service/transspecard/ ) set(SOURCES diff --git a/code/sdk/application/wearable/service/alipay_msg/alipay_app.cpp b/code/sdk/application/wearable/service/alipay_msg/alipay_app.cpp index 8801d49f8..38b82aa9a 100644 --- a/code/sdk/application/wearable/service/alipay_msg/alipay_app.cpp +++ b/code/sdk/application/wearable/service/alipay_msg/alipay_app.cpp @@ -21,6 +21,7 @@ #ifdef __cplusplus extern "C" { #endif +#include "transspecard.h" #define HOSTENT_TIMEOUT 2000 #define TCP_CONNECT_TIMEOUT 2000 @@ -36,6 +37,7 @@ uint16_t g_tcp_data_readed = 0; uint32_t g_tcp_fd = 0; uint8_t g_netstatus = 0; +#define ALIPAY_TRANSSPECARD_SUPPORT_BLE 1 void msg_center_alipay_reply_ack(uint8_t cmd_id, uint8_t type) { uint8_t ack = 1; /* 与手机APK侧约定 */ @@ -45,6 +47,16 @@ void msg_center_alipay_reply_ack(uint8_t cmd_id, uint8_t type) errcode_t msg_center_alipay_network_status(uint8_t cmd_id, uint8_t type, void *usr_data, uint16_t usr_len) { + #if ALIPAY_TRANSSPECARD_SUPPORT_BLE + if(cmd_id != ALIPAY_TRANSSPECARD) + { + printf("msg_center_alipay_network_status cmd_id error:%d\r\n", cmd_id); + return -1; + } + g_netstatus = *((uint8_t *)usr_data); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_MSG_CENTER, "msg_center_alipay_network_status:netstatus = %d\r\n", g_netstatus); + return 0; + #else uint8_t tl_len = msg_center_get_tlv_tl_len(usr_data); uint16_t payload_len = usr_len - tl_len; uint8_t *tlv_payload = (uint8_t *)msg_center_get_tlv_payload(usr_data); @@ -53,6 +65,7 @@ errcode_t msg_center_alipay_network_status(uint8_t cmd_id, uint8_t type, void *u WEARABLE_LOGD(WEARABLE_LOG_MODULE_MSG_CENTER, "msg_center_alipay netstatus = %d\r\n", g_netstatus); msg_center_alipay_reply_ack(cmd_id, type); return 0; + #endif } uint8_t msg_center_alipay_get_network_status(void) @@ -62,6 +75,19 @@ uint8_t msg_center_alipay_get_network_status(void) int msg_center_alipay_hostent_request(void *usr_data, uint16_t usr_len) { + #if ALIPAY_TRANSSPECARD_SUPPORT_BLE + WEARABLE_LOGE(WEARABLE_LOG_MODULE_MSG_CENTER, "DNS域名解析请求:%s\r\n", (char *)usr_data); + int ret; + if (g_alipay_hostent_sem == NULL) { + g_alipay_hostent_sem = osSemaphoreNew(1, 0, NULL); + } + ret = send_transspecard_data(MSGCENTER_TYPE_ID_ALIPAY_HOST_INFO, (uint8_t *)usr_data, usr_len); + if (ret != 0) { + return -1; + } else { + return 0; + } + #else int ret; if (g_alipay_hostent_sem == NULL) { g_alipay_hostent_sem = osSemaphoreNew(1, 0, NULL); @@ -73,10 +99,23 @@ int msg_center_alipay_hostent_request(void *usr_data, uint16_t usr_len) } else { return 0; } + #endif } errcode_t msg_center_alipay_hostent_recv(uint8_t cmd_id, uint8_t type, void *usr_data, uint16_t usr_len) { + #if ALIPAY_TRANSSPECARD_SUPPORT_BLE + if(cmd_id != ALIPAY_TRANSSPECARD) + { + printf("msg_center_alipay_network_status cmd_id error:%d\r\n", cmd_id); + return -1; + } + (void)memcpy_s(g_alipay_ip, IPV4_LEN, usr_data, usr_len); + WEARABLE_LOGI(WEARABLE_LOG_MODULE_MSG_CENTER, "接收app下发的域名解析,域名转换后的IP地址:%d.%d.%d.%d\r\n", + g_alipay_ip[0], g_alipay_ip[1], g_alipay_ip[2], g_alipay_ip[3]); + osSemaphoreRelease(g_alipay_hostent_sem); + return ERRCODE_SUCC; + #else uint8_t tl_len = msg_center_get_tlv_tl_len(usr_data); uint16_t payload_len = usr_len - tl_len; uint8_t *tlv_payload = (uint8_t *)msg_center_get_tlv_payload(usr_data); @@ -86,6 +125,7 @@ errcode_t msg_center_alipay_hostent_recv(uint8_t cmd_id, uint8_t type, void *usr g_alipay_ip[0], g_alipay_ip[1], g_alipay_ip[2], g_alipay_ip[3]); osSemaphoreRelease(g_alipay_hostent_sem); return ERRCODE_SUCC; + #endif } int msg_center_alipay_hostent_reponse(unsigned char *ip) @@ -98,7 +138,7 @@ int msg_center_alipay_hostent_reponse(unsigned char *ip) } else { ret = -1; } - WEARABLE_LOGD(WEARABLE_LOG_MODULE_MSG_CENTER, "hostent wait status = 0x%x\r\n", status); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_MSG_CENTER, "DNS解析 信号量获取状态 = 0x%x\r\n", status); osSemaphoreDelete(g_alipay_hostent_sem); g_alipay_hostent_sem = NULL; return ret; @@ -106,6 +146,19 @@ int msg_center_alipay_hostent_reponse(unsigned char *ip) int msg_center_alipay_connect_request(void *usr_data, uint16_t usr_len) { + #if ALIPAY_TRANSSPECARD_SUPPORT_BLE + WEARABLE_LOGE(WEARABLE_LOG_MODULE_MSG_CENTER, "tcp连接请求\r\n"); + int ret; + if (g_alipay_tcp_sem == NULL) { + g_alipay_tcp_sem = osSemaphoreNew(1, 0, NULL); + } + ret = send_transspecard_data(MSGCENTER_TYPE_ID_ALIPAY_CONNECT, (uint8_t *)usr_data, usr_len); + if (ret != 0) { + return -1; + } else { + return 0; + } + #else int ret; if (g_alipay_tcp_sem == NULL) { g_alipay_tcp_sem = osSemaphoreNew(1, 0, NULL); @@ -116,10 +169,26 @@ int msg_center_alipay_connect_request(void *usr_data, uint16_t usr_len) } else { return 0; } + #endif } errcode_t msg_center_alipay_connect_recv(uint8_t cmd_id, uint8_t type, void *usr_data, uint16_t usr_len) { + #if ALIPAY_TRANSSPECARD_SUPPORT_BLE + if(cmd_id != ALIPAY_TRANSSPECARD) + { + printf("msg_center_alipay_network_status cmd_id error:%d\r\n", cmd_id); + return -1; + } + WEARABLE_LOGE(WEARABLE_LOG_MODULE_MSG_CENTER, "接收tcp连接状态\r\n"); + alipay_connect_reponse rep; + (void)memcpy_s(&rep, sizeof(alipay_connect_reponse), usr_data, usr_len); + WEARABLE_LOGI(WEARABLE_LOG_MODULE_MSG_CENTER, "recv alipay_connect_reponse info--> %d %d\r\n", + rep.ret, rep.fd); + g_tcp_fd = rep.fd; + osSemaphoreRelease(g_alipay_tcp_sem); + return ERRCODE_SUCC; + #else uint8_t tl_len = msg_center_get_tlv_tl_len(usr_data); uint16_t payload_len = usr_len - tl_len; uint8_t *tlv_payload = (uint8_t *)msg_center_get_tlv_payload(usr_data); @@ -131,10 +200,12 @@ errcode_t msg_center_alipay_connect_recv(uint8_t cmd_id, uint8_t type, void *usr g_tcp_fd = rep.fd; osSemaphoreRelease(g_alipay_tcp_sem); return ERRCODE_SUCC; + #endif } int msg_center_alipay_connect_reponse(void) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_MSG_CENTER, "msg_center_alipay_connect_reponse\r\n"); int ret; osStatus_t status = osSemaphoreAcquire(g_alipay_tcp_sem, TCP_CONNECT_TIMEOUT); if (status == osOK) { @@ -142,12 +213,22 @@ int msg_center_alipay_connect_reponse(void) } else { ret = -1; } - WEARABLE_LOGD(WEARABLE_LOG_MODULE_MSG_CENTER, "connect wait status = 0x%x\r\n", status); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_MSG_CENTER, "建立TCP连接 信号量获取状态 = %x\r\n", status); return ret; } int msg_center_alipay_tcpdata_request(void *usr_data, uint16_t usr_len) { + #if ALIPAY_TRANSSPECARD_SUPPORT_BLE + WEARABLE_LOGE(WEARABLE_LOG_MODULE_MSG_CENTER, "tcp数据请求 = %d\r\n",usr_len); + int ret; + ret = send_transspecard_data(MSGCENTER_TYPE_ID_ALIPAY_TCPDATA_RECV, usr_data, usr_len); + if (ret != ERRCODE_SUCC) { + return -1; + } else { + return (int)usr_len; + } + #else WEARABLE_LOGD(WEARABLE_LOG_MODULE_MSG_CENTER, "tcpdata send len = 0x%x\r\n", usr_len); int ret; ret = msg_center_send_data(MSGCENTER_CMD_ALIPAY, MSGCENTER_TYPE_ID_ALIPAY_TCPDATA_RECV, usr_data, usr_len); @@ -156,10 +237,12 @@ int msg_center_alipay_tcpdata_request(void *usr_data, uint16_t usr_len) } else { return (int)usr_len; } + #endif } int msg_center_alipay_tcpdata_wait(uint32_t timeout) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_MSG_CENTER, "msg_center_alipay_tcpdata_wait = %d\r\n",timeout); int ret; osStatus_t status = osSemaphoreAcquire(g_alipay_tcp_sem, timeout); if (status == osOK) { @@ -167,12 +250,35 @@ int msg_center_alipay_tcpdata_wait(uint32_t timeout) } else { ret = 0; } - WEARABLE_LOGD(WEARABLE_LOG_MODULE_MSG_CENTER, "tcpdata wait status = 0x%x\r\n", status); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_MSG_CENTER, "请求TCP数据 信号量获取状态 0x%x\r\n", status); return ret; } errcode_t msg_center_alipay_tcpdata_recv(uint8_t cmd_id, uint8_t type, void *usr_data, uint16_t usr_len) { + #if ALIPAY_TRANSSPECARD_SUPPORT_BLE + if(cmd_id != ALIPAY_TRANSSPECARD) + { + printf("msg_center_alipay_network_status cmd_id error:%d\r\n", cmd_id); + return -1; + } + WEARABLE_LOGE(WEARABLE_LOG_MODULE_MSG_CENTER, "接收tcp数据:%d\r\n",usr_len); + if (g_tcp_data != NULL) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_MSG_CENTER, + "The last buff hasn't been read yet, current payload len is %u.\r\n", usr_len); + return ERRCODE_FAIL; + } + g_tcp_data = (uint8_t *)malloc(usr_len); + if (g_tcp_data != NULL) { + g_tcp_data_len = usr_len; + (void)memcpy_s(g_tcp_data, g_tcp_data_len, usr_data, usr_len); + } + + WEARABLE_LOGI(WEARABLE_LOG_MODULE_MSG_CENTER, "recv tcpdata len = %d\r\n", + g_tcp_data_len); + osSemaphoreRelease(g_alipay_tcp_sem); + return ERRCODE_SUCC; + #else uint8_t tl_len = msg_center_get_tlv_tl_len(usr_data); uint16_t payload_len = usr_len - tl_len; uint8_t *tlv_payload = (uint8_t *)msg_center_get_tlv_payload(usr_data); @@ -191,10 +297,44 @@ errcode_t msg_center_alipay_tcpdata_recv(uint8_t cmd_id, uint8_t type, void *usr g_tcp_data_len); osSemaphoreRelease(g_alipay_tcp_sem); return ERRCODE_SUCC; + #endif } +//接受数据 int msg_center_alipay_tcpdata_read(void *mem, int len) { + #if ALIPAY_TRANSSPECARD_SUPPORT_BLE + int ret = 0; + // printf("进入 msg_center_alipay_tcpdata_read 函数,请求读取的长度为: %d\n", len); + + if (g_tcp_data != NULL) { + // printf("g_tcp_data 不为 NULL,准备读取数据\n"); + int read_len = min(len, g_tcp_data_len); + // printf("实际可读取的长度为: %d\n", read_len); + + (void)memcpy_s(mem, len, g_tcp_data + g_tcp_data_readed, read_len); + // printf("已将 %d 字节的数据从 g_tcp_data 复制到 mem\n", read_len); + + g_tcp_data_readed += read_len; + g_tcp_data_len -= read_len; + // printf("更新后,已读取的数据长度为: %d,剩余数据长度为: %d\n", g_tcp_data_readed, g_tcp_data_len); + + ret = read_len; + + if (g_tcp_data_len == 0) { + // printf("数据已全部读取,释放 g_tcp_data 内存\n"); + free(g_tcp_data); + g_tcp_data = NULL; + g_tcp_data_readed = 0; + // printf("g_tcp_data 已置为 NULL,已读取数据长度重置为 0\n"); + } + } else { + ret = -2; // no data for read + // printf("g_tcp_data 为 NULL,没有数据可供读取,返回 -2\n"); + } + // printf("离开 msg_center_alipay_tcpdata_read 函数,实际读取的长度为: %d\n", ret); + return ret; + #else int ret = 0; if (g_tcp_data != NULL) { int read_len = min(len, g_tcp_data_len); @@ -212,10 +352,33 @@ int msg_center_alipay_tcpdata_read(void *mem, int len) } WEARABLE_LOGI(WEARABLE_LOG_MODULE_MSG_CENTER, "read tcpdata len = (%d, %d)\r\n", len, ret); return ret; + #endif } int msg_center_alipay_close_request(void) { + #if ALIPAY_TRANSSPECARD_SUPPORT_BLE + WEARABLE_LOGE(WEARABLE_LOG_MODULE_MSG_CENTER, "发送socket关闭请求\r\n"); + if (g_alipay_tcp_sem != NULL) { + osSemaphoreDelete(g_alipay_tcp_sem); + g_alipay_tcp_sem = NULL; + } + if (g_tcp_data != NULL) { + WEARABLE_LOGI(WEARABLE_LOG_MODULE_MSG_CENTER, "need free data buff.\r\n"); + free(g_tcp_data); + g_tcp_data = NULL; + g_tcp_data_readed = 0; + } + int ret; + int send_ret = send_transspecard_data(MSGCENTER_TYPE_ID_ALIPAY_CLOSE, &g_tcp_fd, sizeof(g_tcp_fd)); + if (send_ret != ERRCODE_SUCC) { + ret = -1; + } else { + ret = 0; + } + g_tcp_fd = 0; + return ret; + #else if (g_alipay_tcp_sem != NULL) { osSemaphoreDelete(g_alipay_tcp_sem); g_alipay_tcp_sem = NULL; @@ -236,15 +399,25 @@ int msg_center_alipay_close_request(void) } g_tcp_fd = 0; return ret; + #endif } errcode_t msg_center_alipay_close(uint8_t cmd_id, uint8_t type, void *usr_data, uint16_t usr_len) { + #if ALIPAY_TRANSSPECARD_SUPPORT_BLE + if(cmd_id != ALIPAY_TRANSSPECARD) + { + printf("msg_center_alipay_close cmd_id error:%d,ecpected cmd_id is:0x09\r\n", cmd_id); + return -1; + } + return ERRCODE_SUCC; + #else uint8_t tl_len = msg_center_get_tlv_tl_len(usr_data); uint16_t payload_len = usr_len - tl_len; uint8_t *tlv_payload = (uint8_t *)msg_center_get_tlv_payload(usr_data); return ERRCODE_SUCC; + #endif } errcode_t msg_center_alipay_time_sync(uint8_t cmd_id, uint8_t type, void *usr_data, uint16_t usr_len) diff --git a/code/sdk/build/config/target_config/brandy/config.py b/code/sdk/build/config/target_config/brandy/config.py index 88d33de6f..ebe3ed2a9 100644 --- a/code/sdk/build/config/target_config/brandy/config.py +++ b/code/sdk/build/config/target_config/brandy/config.py @@ -403,7 +403,8 @@ target = { "batterry_service", "bleservice", "blemoder", - # "factorytest_service", + "transspecard", + "factorytest_service", # "hid_service", # "producttest", "ppg_hal", diff --git a/code/sdk/drivers/chips/brandy/clocks/drivers/acore/clocks_config_priv.c b/code/sdk/drivers/chips/brandy/clocks/drivers/acore/clocks_config_priv.c index 5037787a2..ba97fa885 100644 --- a/code/sdk/drivers/chips/brandy/clocks/drivers/acore/clocks_config_priv.c +++ b/code/sdk/drivers/chips/brandy/clocks/drivers/acore/clocks_config_priv.c @@ -371,7 +371,7 @@ static const system_mcu_perip_clk_t g_system_mcu_perp_clk_set_config[] = { {HAL_CLOCKS_APP_UART_H1_CLK, TURN_ON}, // uart h1 clock {HAL_CLOCKS_APP_I2C0_CLK, TURN_OFF}, // i2c0 clock {HAL_CLOCKS_APP_I2C1_CLK, TURN_ON}, // i2c1 clock - {HAL_CLOCKS_APP_I2C2_CLK, TURN_OFF}, // i2c2 clock + {HAL_CLOCKS_APP_I2C2_CLK, TURN_ON}, // i2c2 clock {HAL_CLOCKS_APP_I2C3_CLK, TURN_ON}, // i2c3 clock {HAL_CLOCKS_APP_QSPI_1CS_CLK, TURN_ON}, // qspi clock {HAL_CLOCKS_APP_XIP_BUS_CLK, TURN_ON}, // xip bus clock diff --git a/code/sdk/middleware/services/media/foundation/sample/wrapper/audio_capture_wrapper.cpp b/code/sdk/middleware/services/media/foundation/sample/wrapper/audio_capture_wrapper.cpp index 791048adf..8a0e81dcb 100644 --- a/code/sdk/middleware/services/media/foundation/sample/wrapper/audio_capture_wrapper.cpp +++ b/code/sdk/middleware/services/media/foundation/sample/wrapper/audio_capture_wrapper.cpp @@ -418,6 +418,7 @@ EXIT: /* ------------------------------- AI录音数据上传接口 ------------------------------- */ #define AI_RECORD_TEST 0 //若需要调试可以打开该宏 +#ifdef SUPPORT_BLE_SERVICE #include "bleService.h" static uint32_t AI_audio_size=0; static uint8_t Cur_AI_Model= 0; @@ -555,6 +556,8 @@ EXIT: g_audioSourceProcessInput.bThreadRun = false; return -1; } +#endif + #ifdef __cplusplus }; #endif diff --git a/code/sdk/zhihui/services/ble_service/CMakeLists.txt b/code/sdk/zhihui/services/ble_service/CMakeLists.txt index d2c28bbd7..e7fbf954a 100644 --- a/code/sdk/zhihui/services/ble_service/CMakeLists.txt +++ b/code/sdk/zhihui/services/ble_service/CMakeLists.txt @@ -1,2 +1,3 @@ +add_subdirectory_if_exist(transspecard) add_subdirectory_if_exist(bleservice) add_subdirectory_if_exist(blemoder) diff --git a/code/sdk/zhihui/services/ble_service/transspecard/CMakeLists.txt b/code/sdk/zhihui/services/ble_service/transspecard/CMakeLists.txt new file mode 100644 index 000000000..dd82ce66d --- /dev/null +++ b/code/sdk/zhihui/services/ble_service/transspecard/CMakeLists.txt @@ -0,0 +1,89 @@ + +set(COMPONENT_NAME "transspecard") +set(PUBLIC_HEADER + ${CMAKE_CURRENT_SOURCE_DIR} + +) + +set(PRIVATE_HEADER + ${ROOT_DIR}/protocol/bt/host/bt/bts/bt_sample/ble_profile/profiles/include + ${ROOT_DIR}/protocol/bt/host/bt/bts/bg_common/include/sdk_common + ${ROOT_DIR}/protocol/bt/host/bt/bts/host/inc + ${ROOT_DIR}/protocol/bt/host/bt/bts/bg_common/include + ${ROOT_DIR}/protocol/bt/host/bt/bts/bg_common/include/log + ${ROOT_DIR}/protocol/bt/host/bt/bts/bg_common/include/gdk_common + + ${ROOT_DIR}/zhihui/services/ble_service/bleservice + ${ROOT_DIR}/application/wearable/fd230/service/alipay_msg/ +) + + +if("FD230" IN_LIST DEFINES) +set(PRIVATE_HEADER + ${PRIVATE_HEADER} + ${ROOT_DIR}/application/wearable/fd230/nativeapp/nativeui/bluetooth/include + ${ROOT_DIR}/application/wearable/fd230/CustomUIComponentTool/uicomponents + ${ROOT_DIR}/application/wearable/fd230/nativeapp/nativeui + ${ROOT_DIR}/application/wearable/fd230/nativeapp/nativelauncher/include + ${ROOT_DIR}/application/wearable/fd230/nativeapp/nativeui/screennotify/include + ${ROOT_DIR}/application/wearable/fd230/service/broadcast/include +) +else() +set(PRIVATE_HEADER + ${PRIVATE_HEADER} + ${ROOT_DIR}/application/wearable/nativeapp/nativeui/bluetooth/include + ${ROOT_DIR}/application/wearable/fd230/CustomUIComponentTool/uicomponents + ${ROOT_DIR}/application/wearable/nativeapp/nativeui + ${ROOT_DIR}/application/wearable/nativeapp/nativelauncher/include + ${ROOT_DIR}/application/wearable/nativeapp/nativeui/screennotify/include + ) +endif() +if("FD230" IN_LIST DEFINES) +set(SOURCES + ${CMAKE_CURRENT_SOURCE_DIR}/transspecard.c +) +else() +set(SOURCES + +) + +endif() + +set(COMPONENT_PUBLIC_CCFLAGS +) + +set(COMPONENT_CCFLAGS +# -std=c++11 +-Wno-error +-Wno-unused-parameter +-fno-exceptions +-fno-unwind-tables +-fno-asynchronous-unwind-tables +-Wno-undef +-Wno-bitwise-op-parentheses +-Wno-shift-op-parentheses +-nostdlibinc +-nostdinc +-nostdlib +-Wno-c++11-narrowing +-Wno-error=unused-function +) + +set(WHOLE_LINK + true +) + +set(MAIN_COMPONENT + false +) + +set(PUBLIC_DEFINES + CUSTOM_HID_SERVICE +) + +set(PRIVATE_DEFINES + +) + +build_component() + diff --git a/code/sdk/zhihui/services/ble_service/transspecard/transspecard.c b/code/sdk/zhihui/services/ble_service/transspecard/transspecard.c new file mode 100644 index 000000000..461693b7b --- /dev/null +++ b/code/sdk/zhihui/services/ble_service/transspecard/transspecard.c @@ -0,0 +1,778 @@ +#include "transspecard.h" +#include +#include "wearable_log.h" +#include "bleService.h" +#include +#include "alipay_app.h" +#include "factorytestService.h" +#include +#include +#include +#include + +#define alipay_transspecard_log 0 // 用于日志打印 +#define ALIPAY_TRANSSPECARD_ERRCODE_SUCC 0 +#define ALIPAY_TRANSSPECARD_ERRCODE_FAIL -1 +#define MAX_RETRY_TIMES 3 // 重传次数 +#define PACKET_INTERVAL_TIMEOUT 1000 // 数据包间隔超时时间,单位:ms +#define PACKET_DELTA_TIMEOUT 70 // 数据包时间差超时时间,单位:ms +#define PACKET_HEAD_AND_TAIL_LEN 18 // 数据包头部和尾部的长度 + +static const transspecard_pkt_recv_info_t g_transspecard_alipay_sec_tbl[] = { + {ALIPAY_TRANSSPECARD, TRANSSPECARD_TYPE_ID_ALIPAY_NETWORK_STATUS, msg_center_alipay_network_status}, + {ALIPAY_TRANSSPECARD, TRANSSPECARD_TYPE_ID_ALIPAY_HOST_INFO, msg_center_alipay_hostent_recv}, + {ALIPAY_TRANSSPECARD, TRANSSPECARD_TYPE_ID_ALIPAY_CONNECT, msg_center_alipay_connect_recv}, + {ALIPAY_TRANSSPECARD, TRANSSPECARD_TYPE_ID_ALIPAY_TCPDATA_RECV, msg_center_alipay_tcpdata_recv}, + {ALIPAY_TRANSSPECARD, TRANSSPECARD_TYPE_ID_ALIPAY_CLOSE, msg_center_alipay_close}, + {ALIPAY_TRANSSPECARD, TRANSSPECARD_TYPE_ID_ALIPAY_TIME_SYNC, msg_center_alipay_time_sync}, + {ALIPAY_TRANSSPECARD, TRANSSPECARD_TYPE_ID_ALIPAY_ACK, msg_center_alipay_receive_ack}, +}; + +static uint8_t alipay_transspecard_sec_type_dispatch(uint8_t type_id, uint8_t type, void *usr_data, uint16_t usr_len) +{ + uint8_t tbl_size = sizeof(g_transspecard_alipay_sec_tbl) / sizeof(g_transspecard_alipay_sec_tbl[0]); + uint8_t i; + for (i = 0; i < tbl_size; i++) + { + transspecard_pkt_recv_info_t *item = (transspecard_pkt_recv_info_t *)&g_transspecard_alipay_sec_tbl[i]; + if ((item->type_id == type_id) && (item->type == type) && (item->recv_hook != NULL)) + { + item->recv_hook(type_id, type, usr_data, usr_len); + return ALIPAY_TRANSSPECARD_ERRCODE_SUCC; + } + } + return ALIPAY_TRANSSPECARD_ERRCODE_FAIL; +} + +/** + * 计算crc时的求和函数,可以递归,从而实现多段数据的组合计算 + */ +static uint8_t calculate_lrc_transspecard(uint8_t *data, uint16_t len, uint8_t _lrcValue) +{ + uint8_t lrcValue = _lrcValue; + if (data == NULL) + { + return ALIPAY_TRANSSPECARD_ERRCODE_FAIL; + } + for (uint16_t i = 0; i < len; i++) + { + lrcValue = lrcValue + data[i]; + } + return lrcValue; +} + +static uint8_t get_crc_from_lrc_transspecard(uint8_t lrcValue) +{ + uint8_t crcbuf = 0; + crcbuf = lrcValue % 256; + crcbuf = 256 - crcbuf; + crcbuf = crcbuf % 256; + return crcbuf; +} + +static uint8_t calculate_CRC_transspecard(uint8_t *data, uint16_t len) // 计算 +{ + uint8_t crcbuf = 0; + uint8_t lrcValue = 0x00; + lrcValue = calculate_lrc_transspecard(data + 17, len - 17, 0x00); + crcbuf = get_crc_from_lrc_transspecard(lrcValue); + return crcbuf; +} + +// 设置乘车码消息类型ID +static uint8_t set_alipay_transspecard_type_Id(uint8_t type) +{ + uint8_t type_id = 0; + switch (type) + { + case 1: + type_id = 0x01; + break; + case 2: + type_id = 0x02; + break; + case 3: + type_id = 0x03; + break; + case 4: + type_id = 0x04; + break; + case 5: + type_id = 0x05; + break; + case 0xff: + type_id = 0xff; + default: + printf("set_alipay_transspecard_type_Id error,%d\r\n", type); + type_id = 0xff; + break; + } + return type_id; +} + +// 获取阿里的ble_nus最大数据长度 +static uint16_t get_alipay_ble_nus_max_data_len(void) +{ + uint16_t alipay_ble_nus_max_data_len = 0; + if (ble_nus_max_data_len > 512) + { + alipay_ble_nus_max_data_len = 512; + } + else + { + alipay_ble_nus_max_data_len = ble_nus_max_data_len; + } + return alipay_ble_nus_max_data_len; +} +static uint8_t ble_transspecard_receive_ack_id = 0xff; +static osSemaphoreId_t g_transspecard_ack_sem = NULL; // 全局信号量 +static uint8_t send_retry_count = 0; + +errcode_t msg_center_alipay_receive_ack(uint8_t type_id, uint8_t type, void *usr_data, uint16_t usr_len) +{ + uint8_t *data = (uint8_t *)usr_data; + if (data == NULL) + { + return ALIPAY_TRANSSPECARD_ERRCODE_FAIL; + } + printf("msg_center_alipay_receive_ack: %d\n", data[0]); + ble_transspecard_receive_ack_id = data[0]; + if (g_transspecard_ack_sem != NULL) + { + osSemaphoreRelease(g_transspecard_ack_sem); // 有重传请求,释放信号量触发重传 + } + return ALIPAY_TRANSSPECARD_ERRCODE_SUCC; +} + +uint8_t ble_transspecard_receive_ack(void) +{ + return ble_transspecard_receive_ack_id; +} + +// 组装数据包的函数 +static uint8_t *assemble_packet(uint8_t type_id, uint8_t packet_id, uint8_t send_packet_total, uint16_t datalen, uint16_t current_datalen, void *data) +{ + uint8_t *buf = (uint8_t *)malloc(17 + current_datalen + 1); + if (buf == NULL) + { + printf("malloc buf failed!\r\n"); + return NULL; + } + +#if alipay_transspecard_log + printf("为第 %u 个分包分配内存,大小为: %u 字节\n", packet_id, 17 + current_datalen + 1); +#endif + + memset(buf, 0, 17 + current_datalen + 1); + + // 设置消息头部信息 + buf[0] = 0x72; // 消息头 + buf[1] = 0x09; // 类型 :0x09 代表支付宝乘车码 + buf[2] = set_alipay_transspecard_type_Id(type_id); // 类型ID + memcpy(buf + 3, &send_packet_total, 4); // 总包数 + memcpy(buf + 7, &datalen, 4); // 总数据长度 + memcpy(buf + 11, &packet_id, 4); // 数据包ID,从1开始递增 + memcpy(buf + 15, ¤t_datalen, 2); // 包数据长度 + + // 处理数据和计算CRC + if (data == NULL) + { + // 数据为空 按照18个字节总长度发送给app + buf[17] = 0x00; // 直接写0x00的校验和 + } + else + { + // 数据不为空 + memcpy(buf + 17, data + (packet_id - 1) * (get_alipay_ble_nus_max_data_len() - PACKET_HEAD_AND_TAIL_LEN), current_datalen); // 拷贝当前段数据到buf+17的位置 + buf[17 + current_datalen] = calculate_CRC_transspecard(buf, 17 + current_datalen); // 计算CRC校验码 + } + +#if alipay_transspecard_log + printf("第 %u 个分包的CRC校验码: 0x%02x\n", packet_id, buf[17 + current_datalen]); + printf("打印组装的数据:\r\n"); + for (int j = 0; j < 17 + current_datalen + 1; j++) + { + printf("%02x ", buf[j]); + } + printf("\r\n"); +#endif + + return buf; +} + +// 发送指定范围的数据包 +static uint8_t send_packets_range(uint8_t type_id, void *data, uint16_t datalen, uint8_t start_packet, uint8_t send_packet_total) +{ + uint32_t singlebuflen = (get_alipay_ble_nus_max_data_len() - PACKET_HEAD_AND_TAIL_LEN); + uint8_t current_packet = start_packet; + + while (current_packet <= send_packet_total) + { + uint16_t current_datalen = (current_packet == send_packet_total) ? (datalen % singlebuflen) : singlebuflen; + uint8_t *buf = assemble_packet(type_id, current_packet, send_packet_total, datalen, current_datalen, data); + if (buf == NULL) + { + return ALIPAY_TRANSSPECARD_ERRCODE_FAIL; + } + + // 发送数据 + ble_factorytest_server_send_ota_report(buf, 17 + current_datalen + 1); + printf("发送第 %u 个分包(总 %u 包)\n", current_packet, send_packet_total); + msleep(PACKET_DELTA_TIMEOUT); // 增加延时,确保可以成功发送成功 + + // 释放内存 + free(buf); + buf = NULL; + current_packet++; + } + + return ALIPAY_TRANSSPECARD_ERRCODE_SUCC; +} +// 发送数据的函数,支持分包发送 +uint8_t send_transspecard_data(uint8_t type_id, void *data, uint16_t datalen) +{ + uint32_t singlebuflen = (get_alipay_ble_nus_max_data_len() - PACKET_HEAD_AND_TAIL_LEN); + // 计算分包数量,即总包数 + uint8_t send_packet_total = datalen / singlebuflen; + if (datalen % singlebuflen != 0) + { + send_packet_total++; + } + + printf("总数据长度: %u 字节, 预计分包数量: %u 个\n", datalen, send_packet_total); + + // 单包传输逻辑 + if (send_packet_total == 1) + { + uint16_t current_datalen = datalen; + uint8_t *buf = assemble_packet(type_id, 1, send_packet_total, datalen, current_datalen, data); + if (buf == NULL) + { + return ALIPAY_TRANSSPECARD_ERRCODE_FAIL; // 假设ALIPAY_TRANSSPECARD_ERRCODE_FAIL为1 + } + + // 发送数据 + ble_factorytest_server_send_ota_report(buf, 17 + current_datalen + 1); + // printf("发送第 1 个分包(总 1 包)\n"); + msleep(PACKET_DELTA_TIMEOUT); // 增加延时,确保可以成功发送成功 + + // 释放内存 + free(buf); + buf = NULL; + return ALIPAY_TRANSSPECARD_ERRCODE_SUCC; + } + + // 多包传输逻辑 + uint8_t current_packet = 1; + int all_packets_sent = 0; + // 初始化信号量 + if (g_transspecard_ack_sem == NULL) + { + g_transspecard_ack_sem = osSemaphoreNew(1, 0, NULL); + } + + // 先顺序发送所有数据包 + if (send_packets_range(type_id, data, datalen, 1, send_packet_total) == ALIPAY_TRANSSPECARD_ERRCODE_FAIL) + { + return ALIPAY_TRANSSPECARD_ERRCODE_FAIL; + } + + send_retry_count = 0; + while (send_retry_count < MAX_RETRY_TIMES) + { + osStatus_t status = osSemaphoreAcquire(g_transspecard_ack_sem, PACKET_INTERVAL_TIMEOUT); // 等待信号量,直到有重传请求或全部接收成功 + if (status == osOK) + { + uint8_t ack_id = ble_transspecard_receive_ack(); + if (ack_id > 0 && ack_id <= send_packet_total) + { + // send_retry_count = 0; + printf("收到重传请求,从第 %u 个分包开始重新发送\n", ack_id); + // 调用函数重传指定范围的数据包 + if (send_packets_range(type_id, data, datalen, ack_id, send_packet_total) == ALIPAY_TRANSSPECARD_ERRCODE_FAIL) + { + printf("重传失败\n"); + return ALIPAY_TRANSSPECARD_ERRCODE_FAIL; + } + } + else if (ack_id == 0) + { + printf("对方全部收到,进行下一步操作\n"); + break; + } + } + else + { + printf("信号量获取失败\n"); + send_retry_count++; + msleep(PACKET_INTERVAL_TIMEOUT); // 每秒检查一次 + continue; + } + } + + // 发送完成后删除信号量 + osSemaphoreDelete(g_transspecard_ack_sem); + g_transspecard_ack_sem = NULL; + + return ALIPAY_TRANSSPECARD_ERRCODE_SUCC; +} + +#if 0 +uint8_t send_transspecard_data(uint8_t type_id, void *data, uint16_t datalen) +{ + uint32_t singlebuflen = ble_nus_max_data_len - 20; + // uint32_t singlebuflen = 100; //测试用 + // 计算分包数量,即总包数 + uint8_t send_packet_total = datalen / singlebuflen; + if (datalen % singlebuflen != 0) + { + send_packet_total++; + } + +#if alipay_transspecard_log + printf("总数据长度: %u 字节, 预计分包数量: %u 个\n", datalen, send_packet_total); +#endif + + uint8_t current_packet = 1; + ble_transspecard_receive_ack_id = 0xff; + while (current_packet <= send_packet_total) + { + uint16_t current_datalen = (current_packet == send_packet_total) ? (datalen % singlebuflen) : singlebuflen; + uint8_t *buf = (uint8_t *)malloc(17 + current_datalen + 1); + if (buf == NULL) + { + printf("malloc buf failed!\r\n"); + return ALIPAY_TRANSSPECARD_ERRCODE_FAIL; + } + +#if alipay_transspecard_log + printf("为第 %u 个分包分配内存,大小为: %u 字节\n", current_packet, 17 + current_datalen + 1); +#endif + + memset(buf, 0, 17 + current_datalen + 1); + + uint8_t packet_id = current_packet; + // 设置消息头部信息 + buf[0] = 0x72; // 消息头 + buf[1] = 0x09; // 类型 :0x09 代表支付宝乘车码 + buf[2] = set_alipay_transspecard_type_Id(type_id); // 类型ID + memcpy(buf + 3, &send_packet_total, 4); // 总包数 + memcpy(buf + 7, &datalen, 4); // 总数据长度 + memcpy(buf + 11, &packet_id, 4); // 数据包ID,从1开始递增 + memcpy(buf + 15, ¤t_datalen, 2); // 包数据长度 + + // 处理数据和计算CRC + if (data == NULL) + { + // 数据为空 按照18个字节总长度发送给app + buf[17] = 0x00; // 直接写0x00的校验和 + } + else + { + // 数据不为空 + memcpy(buf + 17, data + (current_packet - 1) * singlebuflen, current_datalen); // 拷贝当前段数据到buf+17的位置 + buf[17 + current_datalen] = calculate_CRC_transspecard(buf, 17 + current_datalen); // 计算CRC校验码 + } + +#if alipay_transspecard_log + printf("第 %u 个分包的CRC校验码: 0x%02x\n", current_packet, buf[17 + current_datalen]); + printf("打印组装的数据:\r\n"); + for (int j = 0; j < 17 + current_datalen + 1; j++) + { + printf("%02x ", buf[j]); + } + printf("\r\n"); +#endif + + // 发送数据 + ble_factorytest_server_send_ota_report(buf, 17 + current_datalen + 1); + printf("发送第 %u 个分包(总 %u 包)\n", packet_id, send_packet_total); + msleep(PACKET_DELTA_TIMEOUT); // 增加延时,确保可以成功发送成功 + + if (0) + { +#if alipay_transspecard_log + printf("支持分包传输,等待确认\n"); +#endif + send_retry_count = 0; + uint8_t ack; + while (send_retry_count < MAX_RETRY_TIMES) + { +#if alipay_transspecard_log + printf("等待第 %u 个分包的确认\n", packet_id); +#endif + msleep(PACKET_INTERVAL_TIMEOUT); // 每秒检查一次 + ack = ble_transspecard_receive_ack(); + if (ack == 0) + { +#if alipay_transspecard_log + printf("第 %u 个分包收到确认\n", packet_id); +#endif + // 对方收到了,发送下一包 + free(buf); + buf = NULL; + current_packet++; + break; + } + else if (ack > 0 && ack <= send_packet_total) + { +#if alipay_transspecard_log + printf("收到重传请求,重传第 %u 个分包\n", ack); +#endif + // 从指定的包开始重新发送 + free(buf); + buf = NULL; + current_packet = ack; + break; + } + else + { + // 未收到确认,重试 + send_retry_count++; +#if alipay_transspecard_log + printf("第 %u 个分包未收到确认,重试第 %u 次\n", packet_id, send_retry_count); +#endif + ble_factorytest_server_send_ota_report(buf, 17 + current_datalen + 1); + msleep(PACKET_DELTA_TIMEOUT); // 增加延时,确保可以成功发送成功 + } + } + + if (send_retry_count >= MAX_RETRY_TIMES) + { +#if alipay_transspecard_log + printf("第 %u 个分包发送失败,达到最大重试次数\n", packet_id); +#endif + free(buf); + buf = NULL; + return ALIPAY_TRANSSPECARD_ERRCODE_FAIL; + } + } + else + { +#if alipay_transspecard_log + printf("不支持分包传输,等待确认\n"); +#endif + // 释放内存 + free(buf); + buf = NULL; + current_packet++; + } + } + + return ALIPAY_TRANSSPECARD_ERRCODE_SUCC; +} +#endif + +// 接收缓冲区最大长度 +#define MAX_RECEIVE_BUFFER_SIZE (1024 * 4) // +#define RECEIVE_PACKET_FIRST 0x01 // 第一个包 +#define RECEVIE_PACKET_COMPLETED_ACK 0x00 // 最后一个包接收完成ACK +#define RECEIVE_PACKET_HEADER 0x73 // 消息头 +#define RECEIVE_PACKET_TYPE 0x09 // 类型 :0x09 代表支付宝乘车码 +// 全局变量,用于存储接收状态 +static uint8_t receive_packet_total = 0; +static uint16_t receive_total_datalen = 0; +static uint8_t *packet_received_flags = NULL; +static uint8_t *receive_buffer = NULL; +static uint8_t expected_packet_id = 1; +static uint8_t receive_retry_count = 0; +static osTimerId_t ble_transspecard_receive_timer = NULL; // 接收定时器 +static uint8_t isProcessingData = 0; // 全局标志,指示是否正在处理数据 + +// 封装通知发送方重发的函数 +static void notify_resend(uint8_t packet_id) +{ + if (receive_packet_total <= 1) + { + printf("只有一个包,无需重传\n"); + return; + } + // 发送重传请求 + uint8_t *retransmit_req = (uint8_t *)malloc(19); + if (retransmit_req == NULL) + { + printf("malloc retransmit_req failed!\r\n"); + return; + } + uint32_t temp_32 = 1; + retransmit_req[0] = 0x72; // 消息头 + retransmit_req[1] = 0x09; // 类型 :0x09 代表支付宝乘车码 + retransmit_req[2] = 0xFF; // 重传请求类型ID + memcpy(retransmit_req + 3, &temp_32, 4); // 总包数 + memcpy(retransmit_req + 7, &temp_32, 4); // 总数据长度 + memcpy(retransmit_req + 11, &temp_32, 4); // 数据包ID,从1开始递增 + memcpy(retransmit_req + 15, &temp_32, 2); // 包数据长度 + retransmit_req[17] = packet_id; + retransmit_req[18] = calculate_CRC_transspecard(retransmit_req, 18); // 计算CRC校验码 + ble_factorytest_server_send_ota_report(retransmit_req, 19); + msleep(PACKET_DELTA_TIMEOUT); // 增加延时,确保可以成功发送成功 + + free(retransmit_req); + retransmit_req = NULL; + return; +} + +// 释放动态分配的内存 +static void ble_transspecard_receive_cleanup_resources() +{ + if (packet_received_flags != NULL) + { + free(packet_received_flags); + packet_received_flags = NULL; + } + if (receive_buffer != NULL) + { + free(receive_buffer); + receive_buffer = NULL; + } + receive_packet_total = 0; + receive_total_datalen = 0; + return; +} + +// 接收定时器回调函数 +static void ble_transspecard_receive_timer_CallBack(void *arg) +{ + printf("等待第 %u 个数据包超时,请求重传重传次数:%d\n", expected_packet_id, receive_retry_count); + receive_retry_count++; + if (receive_retry_count >= MAX_RETRY_TIMES) + { + printf("第 %u 个数据包接收失败,达到最大重试次数\n", expected_packet_id); + // 释放动态分配的内存 + ble_transspecard_receive_cleanup_resources(); + // 停止并删除定时器 + if (ble_transspecard_receive_timer != NULL) + { + osTimerStop(ble_transspecard_receive_timer); + osTimerDelete(ble_transspecard_receive_timer); + ble_transspecard_receive_timer = NULL; + } + receive_retry_count = 0; + // 通知发送方重新从第一包开始发送 + notify_resend(RECEIVE_PACKET_FIRST); + return; + } + // 发送重传请求 + notify_resend(expected_packet_id); +} + +// 封装解析数据的函数 +uint8_t parse_transspecard_data(void *buf, uint16_t event_size) +{ + if (isProcessingData) + { + printf("Already processing data, discarding new data!\n"); + return ALIPAY_TRANSSPECARD_ERRCODE_FAIL; + } + + isProcessingData = 1; + +#if alipay_transspecard_log + printf("parse_transspecard_data len:%d\r\n", event_size); +#endif + // 检查数据包长度是否小于17字节 + if (event_size < 17) + { + printf("received packet length is too short!\r\n"); + isProcessingData = 0; + ble_transspecard_receive_cleanup_resources(); // 释放动态分配的内存 + notify_resend(RECEIVE_PACKET_FIRST); // 通知发送方重新从第一包开始发送 + return ALIPAY_TRANSSPECARD_ERRCODE_FAIL; + } + + // 解析消息头部信息 + uint8_t message_header = *(uint8_t *)buf; + uint8_t message_type = *(uint8_t *)(buf + 1); + uint8_t type_id = *(uint8_t *)(buf + 2); + uint32_t current_packet_total = *(uint32_t *)(buf + 3); + uint32_t current_total_datalen = *(uint32_t *)(buf + 7); + uint32_t current_packet_id = *(uint32_t *)(buf + 11); + uint16_t current_datalen = *(uint16_t *)(buf + 15); + +#if alipay_transspecard_log + // 打印头部信息 + printf("Message Header: 0x%02X\n", message_header); + printf("Message Type: 0x%02X\n", message_type); + printf("Type ID: 0x%02X\n", type_id); + printf("Current Packet Total: %u\n", current_packet_total); + printf("Current Total Data Length: %u\n", current_total_datalen); + printf("Current Packet ID: %u\n", current_packet_id); + printf("Current Data Length: %u\n", current_datalen); +#endif + + // 检查消息头和类型 + if (message_header != RECEIVE_PACKET_HEADER || message_type != RECEIVE_PACKET_TYPE) + { + printf("Invalid message header or type!\r\n"); + isProcessingData = 0; + ble_transspecard_receive_cleanup_resources(); + notify_resend(RECEIVE_PACKET_FIRST); + return ALIPAY_TRANSSPECARD_ERRCODE_FAIL; + } + + if (receive_packet_total == 0) + { + // 首次接收到数据包,检查是否从第一包开始 + if (current_packet_id != 1) + { + printf("Did not receive the first packet! Expected 1, but got %u.\r\n", current_packet_id); + isProcessingData = 0; + ble_transspecard_receive_cleanup_resources(); + notify_resend(RECEIVE_PACKET_FIRST); + return ALIPAY_TRANSSPECARD_ERRCODE_FAIL; + } + + // 首次接收到数据包,初始化相关信息 + receive_packet_total = current_packet_total; + receive_total_datalen = current_total_datalen; + + if (receive_total_datalen > MAX_RECEIVE_BUFFER_SIZE) + { + printf("Received data length exceeds buffer size!\r\n"); + isProcessingData = 0; + ble_transspecard_receive_cleanup_resources(); + notify_resend(RECEIVE_PACKET_FIRST); + return ALIPAY_TRANSSPECARD_ERRCODE_FAIL; + } + + packet_received_flags = (uint8_t *)malloc(receive_packet_total); + if (packet_received_flags == NULL) + { + printf("malloc packet received flags failed!\r\n"); + isProcessingData = 0; + ble_transspecard_receive_cleanup_resources(); + notify_resend(RECEIVE_PACKET_FIRST); + return ALIPAY_TRANSSPECARD_ERRCODE_FAIL; + } + memset(packet_received_flags, 0, receive_packet_total); + + receive_buffer = (uint8_t *)malloc(receive_total_datalen); + if (receive_buffer == NULL) + { + printf("malloc receive buffer failed!\r\n"); + isProcessingData = 0; + ble_transspecard_receive_cleanup_resources(); + notify_resend(RECEIVE_PACKET_FIRST); + return ALIPAY_TRANSSPECARD_ERRCODE_FAIL; + } + memset(receive_buffer, 0, receive_total_datalen); + + expected_packet_id = 1; + } + + if (current_packet_id < 1 || current_packet_id > receive_packet_total) + { + printf("invalid packet id: %u!\r\n", current_packet_id); + isProcessingData = 0; + ble_transspecard_receive_cleanup_resources(); + notify_resend(RECEIVE_PACKET_FIRST); + return ALIPAY_TRANSSPECARD_ERRCODE_FAIL; + } + + // 检查是否按顺序接收 + if (current_packet_id != expected_packet_id) + { + printf("Received packet out of order! Expected %u, but got %u.\r\n", expected_packet_id, current_packet_id); + isProcessingData = 0; + notify_resend(expected_packet_id); + return ALIPAY_TRANSSPECARD_ERRCODE_FAIL; + } + + if (packet_received_flags[current_packet_id - 1]) + { + // 该数据包已经接收过,忽略 + printf("Packet %u has already been received, ignoring.\n", current_packet_id); + isProcessingData = 0; + return ALIPAY_TRANSSPECARD_ERRCODE_SUCC; + } + + // 检查 CRC 校验码 + uint8_t calculated_crc = calculate_CRC_transspecard(buf, 17 + current_datalen); + uint8_t received_crc = *(uint8_t *)(buf + 17 + current_datalen); +#if alipay_transspecard_log + printf("Calculated CRC: 0x%02X, Received CRC: 0x%02X\n", calculated_crc, received_crc); +#endif + if (calculated_crc != received_crc) + { + printf("CRC check failed for packet %u!\r\n", current_packet_id); + isProcessingData = 0; + notify_resend(expected_packet_id); + return ALIPAY_TRANSSPECARD_ERRCODE_FAIL; + } + +#if alipay_transspecard_log + // 打印接收到的数据 + printf("Received data for packet %u:\n", current_packet_id); + for (int i = 0; i < current_datalen; i++) + { + printf("%02x ", *(uint8_t *)(buf + 17 + i)); + } + printf("\n"); +#endif + + // 拷贝数据到接收缓冲区 + memcpy(receive_buffer + (current_packet_id - 1) * (get_alipay_ble_nus_max_data_len() - PACKET_HEAD_AND_TAIL_LEN), buf + 17, current_datalen); + packet_received_flags[current_packet_id - 1] = 1; + + // 停止当前定时器(如果存在) + if (ble_transspecard_receive_timer != NULL) + { + osTimerStop(ble_transspecard_receive_timer); + osTimerDelete(ble_transspecard_receive_timer); + ble_transspecard_receive_timer = NULL; + receive_retry_count = 0; + } + + // 检查是否是最后一个数据包 + if (current_packet_id == receive_packet_total) + { + // 所有数据包都已接收,进行后续处理 + printf("All packets received, total data length: %u\n", receive_total_datalen); +#if alipay_transspecard_log + // 打印完整数据 + printf("Complete received data:\n"); + for (int i = 0; i < receive_total_datalen; i++) + { + printf("%02x ", receive_buffer[i]); + } + printf("\n"); +#endif + // 这里可以添加对完整数据的处理逻辑,例如调用其他函数进行数据解析等 + uint8_t ret = alipay_transspecard_sec_type_dispatch(message_type, type_id, receive_buffer, receive_total_datalen); + if (ret != ALIPAY_TRANSSPECARD_ERRCODE_SUCC) + { + printf("alipay_transspecard_sec_type_dispatch failed!\r\n"); + } + + notify_resend(RECEVIE_PACKET_COMPLETED_ACK); // 通知发送方我已经全部收到数据 + // 释放资源 + ble_transspecard_receive_cleanup_resources(); + } + else + { +#if alipay_transspecard_log + // 还有未接收的数据包,等待下一个数据包 + printf("Waiting for packet %u...\n", current_packet_id + 1); +#endif + // 开启定时器等待下一个数据包 + expected_packet_id = current_packet_id + 1; + uint32_t tick = osMs2Tick(PACKET_INTERVAL_TIMEOUT); + osStatus_t retTimer; + ble_transspecard_receive_timer = osTimerNew((osTimerFunc_t)ble_transspecard_receive_timer_CallBack, osTimerPeriodic, NULL, NULL); + retTimer = osTimerStart(ble_transspecard_receive_timer, tick); + if (retTimer != osOK && ble_transspecard_receive_timer != NULL) // 启动定时器失败 + { + printf("Start receive timer for packet %u failed, ret = %lx!!", expected_packet_id, retTimer); + isProcessingData = 0; // 重置处理标志 + notify_resend(expected_packet_id); // 通知发送方重新从指定包开始发送 + osTimerDelete(ble_transspecard_receive_timer); + ble_transspecard_receive_timer = NULL; + return ALIPAY_TRANSSPECARD_ERRCODE_FAIL; + } + } + + // 处理完成后,设置标志为0 + isProcessingData = 0; + + return ALIPAY_TRANSSPECARD_ERRCODE_SUCC; +} \ No newline at end of file diff --git a/code/sdk/zhihui/services/ble_service/transspecard/transspecard.h b/code/sdk/zhihui/services/ble_service/transspecard/transspecard.h new file mode 100644 index 000000000..e5fd09b63 --- /dev/null +++ b/code/sdk/zhihui/services/ble_service/transspecard/transspecard.h @@ -0,0 +1,48 @@ +#ifndef TRANSSPECARD_H +#define TRANSSPECARD_H + +#ifdef __cplusplus +extern "C" +{ +#endif + +#include +#include +#include "stdbool.h" +#include "bts_def.h" +#include "errcode.h" +#include + +#define ALIPAY_TRANSSPECARD 0x09 + +extern uint16_t ble_nus_max_data_len; + +typedef uint8_t (*transspecard_pkt_recv_hook)(uint8_t type_id,uint8_t type, void *usr_data, uint16_t usr_len); + +typedef struct +{ + uint8_t type_id; + uint8_t type; + transspecard_pkt_recv_hook recv_hook; +} transspecard_pkt_recv_info_t; + +typedef enum transspecard_type_id +{ + TRANSSPECARD_TYPE_ID_ALIPAY_NETWORK_STATUS = 0x1, // 网络状态 + TRANSSPECARD_TYPE_ID_ALIPAY_HOST_INFO, // 主机信息 + TRANSSPECARD_TYPE_ID_ALIPAY_CONNECT, // 连接 + TRANSSPECARD_TYPE_ID_ALIPAY_TCPDATA_RECV, // TCP数据接收 + TRANSSPECARD_TYPE_ID_ALIPAY_CLOSE, // 关闭 + TRANSSPECARD_TYPE_ID_ALIPAY_TIME_SYNC = 0x7, // 时间同步 + TRANSSPECARD_TYPE_ID_ALIPAY_ACK = 0xff, // ack回复 + +} transspecard_type_id_t; + +errcode_t msg_center_alipay_receive_ack(uint8_t type_id,uint8_t type, void *usr_data, uint16_t usr_len); +uint8_t send_transspecard_data(uint8_t type_id, void *data, uint16_t datalen); +uint8_t parse_transspecard_data(void *buf, uint16_t event_size); + +#ifdef __cplusplus +} +#endif +#endif \ No newline at end of file diff --git a/code/sdk/zhihui/services/factorytest_service/CMakeLists.txt b/code/sdk/zhihui/services/factorytest_service/CMakeLists.txt index 28a13ee57..b1e19cb70 100644 --- a/code/sdk/zhihui/services/factorytest_service/CMakeLists.txt +++ b/code/sdk/zhihui/services/factorytest_service/CMakeLists.txt @@ -7,6 +7,7 @@ if("FD230" IN_LIST DEFINES) set(PRIVATE_HEADER ${ROOT_DIR}/application/wearable/fd230/nativeapp/nativeui/bluetooth/include ${ROOT_DIR}/application/wearable/fd230/nativeapp/nativeui/phoneservice/include/ + ${ROOT_DIR}/zhihui/services/ble_service/transspecard ) else() set(PRIVATE_HEADER diff --git a/code/sdk/zhihui/services/factorytest_service/factorytestService.c b/code/sdk/zhihui/services/factorytest_service/factorytestService.c index bc7334ee7..c3e559e8d 100644 --- a/code/sdk/zhihui/services/factorytest_service/factorytestService.c +++ b/code/sdk/zhihui/services/factorytest_service/factorytestService.c @@ -15,6 +15,8 @@ #include "BluetoothWrapper.h" #endif +#include "transspecard.h" + /* HID gatt notification flag, do not need confirm */ #define GATT_NOTIFICATION_FLAG 0 /* HID gatt indication flag, need confirm */ @@ -153,7 +155,7 @@ errcode_t ble_factorytest_server_send_input_report(const uint8_t *data, uint8_t } -errcode_t ble_factorytest_server_send_ota_report(const uint8_t *data, uint8_t len) +errcode_t ble_factorytest_server_send_ota_report(const uint8_t *data, uint16_t len) { // printf("ble_hid_server_send_input_report:%d %d %d",data[0],data[1],len); gatts_ntf_ind_t param = {0}; @@ -169,8 +171,33 @@ errcode_t ble_factorytest_server_send_ota_report(const uint8_t *data, uint8_t le bt_os_free(param.value); return ERRCODE_BT_FAIL; } + + // printf("发送到app:"); + // for(int i=0;i> 24); databuf[1] = (uint8_t)((ft_result.gr_data_final & 0x00FF0000) >> 16); @@ -1501,7 +1511,9 @@ void factorytest_protocol_soft_test_decode(uint8_t submode,uint8_t testitem,uint databuf[3] = (uint8_t)((ppg_data & 0x000000FF) >> 0); #endif + #ifdef SUPPORT_PPG_SERVICE send_ppg_cmd_msg(PPG_SERVICE_CMD_TYPE_STOP_PPG_TEST); + #endif PRINT("%2X %2X %2X %2X \r\n",databuf[0],databuf[1],databuf[2],databuf[3]); @@ -1514,14 +1526,14 @@ void factorytest_protocol_soft_test_decode(uint8_t submode,uint8_t testitem,uint } else if(testitem == 0x04) { //打开血氧检测 -#ifdef PPG_HX3695 +#ifdef SUPPORT_PPG_SERVICE send_ppg_cmd_msg(PPG_SERVICE_CMD_TYPE_DO_SINGLE_CHECK_MODE_TEST); #elif defined PPG_VC9202 send_ppg_cmd_msg(PPG_SERVICE_CMD_TYPE_DO_SPO2_TEST); #endif LOS_Msleep(500); uint8_t databuf[8] = {0}; -#ifdef PPG_HX3695 +#ifdef SUPPORT_PPG_SERVICE FT_RESULTS_t ft_result = hx3695_factroy_test(SINGLE_CHECK_TEST); databuf[0] = (uint8_t)((ft_result.red_data_final & 0xFF000000) >> 24); databuf[1] = (uint8_t)((ft_result.red_data_final & 0x00FF0000) >> 16); @@ -1542,7 +1554,9 @@ void factorytest_protocol_soft_test_decode(uint8_t submode,uint8_t testitem,uint databuf[6] = (uint8_t)((red_data & 0x0000FF00) >> 8); databuf[7] = (uint8_t)((red_data & 0x000000FF) >> 0); #endif + #ifdef SUPPORT_PPG_SERVICE send_ppg_cmd_msg(PPG_SERVICE_CMD_TYPE_STOP_PPG_TEST); + #endif PRINT("%2X %2X %2X %2X \r\n",databuf[0],databuf[1],databuf[2],databuf[3]); PRINT("%2X %2X %2X %2X \r\n",databuf[4],databuf[5],databuf[6],databuf[7]); @@ -1555,9 +1569,11 @@ void factorytest_protocol_soft_test_decode(uint8_t submode,uint8_t testitem,uint } else if(testitem == 0x05) { //灰卡测试 + #ifdef SUPPORT_PPG_SERVICE send_ppg_cmd_msg(PPG_SERVICE_CMD_TYPE_DO_GRAYCARD_TEST); + #endif LOS_Msleep(500); -#ifdef PPG_HX3695 +#ifdef SUPPORT_PPG_SERVICE uint8_t databuf[12] = {0}; FT_RESULTS_t ft_result = hx3695_factroy_test(GRAY_CARD_TEST); databuf[0] = (uint8_t)((ft_result.gr_data_final & 0xFF000000) >> 24); @@ -1602,7 +1618,9 @@ void factorytest_protocol_soft_test_decode(uint8_t submode,uint8_t testitem,uint databuf, 8); #endif + #ifdef SUPPORT_PPG_SERVICE send_ppg_cmd_msg(PPG_SERVICE_CMD_TYPE_STOP_PPG_TEST); + #endif // PRINT("%2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X\r\n", // databuf[0],databuf[1],databuf[2],databuf[3], // databuf[4],databuf[5],databuf[6],databuf[7], @@ -1613,10 +1631,12 @@ void factorytest_protocol_soft_test_decode(uint8_t submode,uint8_t testitem,uint } else if(testitem == 0x06) { //漏光测试 + #ifdef SUPPORT_PPG_SERVICE send_ppg_cmd_msg(PPG_SERVICE_CMD_TYPE_DO_LEAKLIGHT_TEST); + #endif LOS_Msleep(500); uint8_t databuf[12] = {0}; -#ifdef PPG_HX3695 +#ifdef SUPPORT_PPG_SERVICE FT_RESULTS_t ft_result = hx3695_factroy_test(LEAK_LIGHT_TEST); databuf[0] = (uint8_t)((ft_result.gr_data_final & 0xFF000000) >> 24); @@ -1654,7 +1674,9 @@ void factorytest_protocol_soft_test_decode(uint8_t submode,uint8_t testitem,uint databuf[10] = (uint8_t)((aps_val & 0x0000FF00) >> 8); databuf[11] = (uint8_t)((aps_val & 0x000000FF) >> 0); #endif + #ifdef SUPPORT_PPG_SERVICE send_ppg_cmd_msg(PPG_SERVICE_CMD_TYPE_STOP_PPG_TEST); + #endif // PRINT("%2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X\r\n", // databuf[0],databuf[1],databuf[2],databuf[3],