static int bp_sbp = 0;
static int bp_dbp = 0;
+static int bp_pressure = 0;
+static int bp_fatigue = 0;
+static int bp_emo = 0;
static char *imei = NULL;
if (bytes_read > 0)
{
buffer[bytes_read] = '\0';
- printf("token: %s\n", buffer);
+ printf("bp_get_token: %s\n", buffer);
} else
printf("read token failed\n");
// 创建套接字
- listen_socket = socket(AF_INET, SOCK_STREAM, 0);
+ listen_socket = lwip_socket(AF_INET, SOCK_STREAM, 0);
if(listen_socket < 0)
{
int err = errno;
printf("socket create errno: %d, reason: %s\n", err, strerror(err));
goto FAILED;
}
+
+ int opt = 1;
+ lwip_setsockopt(listen_socket, SOL_SOCKET, SO_KEEPALIVE, (const void*)&opt, sizeof(opt));
+ lwip_setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR,(const void*)&opt, sizeof(opt));
// 设置服务器地址
server_addr.sin_family = AF_INET;
cJSON *token = cJSON_GetObjectItem(data_obj, "token");
cJSON *sbp = cJSON_GetObjectItem(data_obj, "sbp");
cJSON *dbp = cJSON_GetObjectItem(data_obj, "dbp");
+ cJSON *pressure = cJSON_GetObjectItem(data_obj, "pressure");
+ cJSON *fatigue = cJSON_GetObjectItem(data_obj, "fatigue");
+ cJSON *emo = cJSON_GetObjectItem(data_obj, "emo");
if (status && cJSON_IsTrue(status) && data_obj)
{
bp_save_token(token->valuestring, strlen(token->valuestring));
}
- if (sbp && dbp)
+ if (sbp && dbp && emo && pressure && fatigue)
{
printf("sbp: %d\n", sbp->valueint);
bp_sbp = sbp->valueint;
printf("dbp: %d\n", dbp->valueint);
bp_dbp = dbp->valueint;
+ printf("emo: %d\n", emo->valueint);
+ bp_emo = emo->valueint;
+
+ printf("pressure: %d\n", pressure->valueint);
+ bp_pressure = pressure->valueint;
+
+ printf("fatigue: %d\n", fatigue->valueint);
+ bp_fatigue = fatigue->valueint;
+
BP_GET_STATUS = 1;
}
while (total_sent < size)
{
- ssize_t sent = send(listen_socket, buffer + total_sent, size - total_sent, 0);
+ ssize_t sent = lwip_send(listen_socket, buffer + total_sent, size - total_sent, 0);
if (sent < 0) {
printf("errno : %d\n",errno);
// 处理错误情况
int tcp_sign_up(void)
{
- int ret = -1;
+ int ret = 0;
char len_str[5] = {0};
sprintf(len_str, "%04x", 2);
// 获取IMEI
if(imei == NULL)
{
- uint8_t retry = 0;
- while((imei == NULL || strlen(imei) == 0) && retry < 5)
+
+ imei = uapi_dev_runtime_get_cat1_imei();
+
+ if(imei == NULL || strlen(imei) == 0)
{
- retry++;
- LOS_Msleep(1000);
+ LOS_Msleep(2000);
imei = uapi_dev_runtime_get_cat1_imei();
}
char *head_str = (char *)malloc(head_len);
if(head_str == NULL)
{
- printf("head_str malloc failed\n");
+ printf("tcp_sign_up head_str malloc failed\n");
ret = -1;
goto FAILED;
}
// printf("send_str : %s\n", send_str);
// 发送数据
- tcp_send(g_listen_sock, send_str, send_len);
- LOS_Msleep(1000);
+ ret = tcp_send(g_listen_sock, send_str, send_len);
+ if(ret != 0)
+ {
+ printf("tcp_send failed\n");
+ goto FAILED;
+ }
+
+
FAILED:
// 释放资源
if(head_str != NULL)
static void upload_bp_data(int *data, int cnt)
{
#if 0
- if (data == NULL || cnt == 0 || LK_STATUS == 0)
- {
- printf("数据为空或服务器未登录\n");
+ if (data == NULL || cnt == 0)
return;
- }
int ret = -1;
char *data_str = NULL;
char md5_str[33] = {0};
uint32_t begin_time = 0;
uint32_t end_time = 0;
-
- printf("upload_bp_data\n");
-
-
-/*########################################################################################################*/
- uint8_t retry = 0;
- const char *imei = uapi_dev_runtime_get_cat1_imei();
- while ((imei == NULL || strlen(imei) == 0) && retry < 10) {
- retry++;
- LOS_Msleep(2000);
- imei = uapi_dev_runtime_get_cat1_imei();
- }
-
- if (imei == NULL || strlen(imei) == 0) {
- printf("获取IMEI失败\n");
- goto FAILED;
- }
- printf("imei:%s\n", imei);
+ char *encode_header_str = NULL;
+ char *header_json = NULL;
+ int encode_header_len = 0;
- // 分配JSON缓冲区
- char *json_data = (char *)malloc(512);
- if (json_data == NULL) {
- printf("json_data malloc failed\n");
- goto FAILED;
- }
- printf("json_data malloc succed\n");
// 获取测量时间
bp_get_measure_time(&begin_time, &end_time);
printf("end_time_str:%s\n", end_time_str);
// 构建JSON数据前缀部分
+ header_json = (char *)malloc(256);
+ if(header_json == NULL)
+ goto FAILED;
+
+ char token[64] = {0};
+ bp_get_token(token, 64);
+ printf("bp_get_token:%s\n", token);
+ uint8_t retry = 0;
+ while(strlen(token) == 0 && retry < 10)
+ {
+ bp_tcp_login();
+ LOS_Msleep(500);
+ bp_get_token(token, 64);
+ }
+
temp_len = 0;
- memset(json_data, 0, 512);
+ temp_len += snprintf(header_json + temp_len,256 - temp_len, "{\"headers\":[");
+ temp_len += snprintf(header_json + temp_len,256 - temp_len, "{\"key\":\"token\",");
+ temp_len += snprintf(header_json + temp_len,256 - temp_len, "\"value\":\"%s\"}", token);
+ temp_len += snprintf(header_json + temp_len,256 - temp_len, "]}");
+
+ if(temp_len > 256)
+ {
+ goto FAILED;
+ }
+
+ encode_header_len = temp_len * 3 / 2;
+ encode_header_str = (char *)malloc(encode_header_len);
+ if(NULL == encode_header_str)
+ {
+ printf("encode_header_str malloc failed\n");
+ goto FAILED;
+ }
+
+ memset(encode_header_str,0, encode_header_len);
+ size_t output_header_len = 0;
+ if(0 != mbedtls_base64_encode((unsigned char*)encode_header_str, encode_header_len, &output_header_len, (const unsigned char*)header_json, temp_len))
+ {
+ printf("encode_header_str base64 encode failed\n");
+ goto FAILED;
+ }
+
+ // 分配JSON缓冲区
+ int json_len = 512 + calculate_base64_length(cnt);
+ char *json_data = (char *)malloc(json_len);
+ if (json_data == NULL)
+ {
+ printf("json_data malloc failed\n");
+ goto FAILED;
+ }
+ printf("json_data malloc succed\n");
+
+ temp_len = 0;
+ memset(json_data, 0, json_len);
+
temp_len += snprintf(json_data + temp_len, 512 - temp_len, "{\"app_key\":\"%s\",", APP_KEY);
// temp_len += snprintf(json_data + temp_len, 512 - temp_len, "\"device_code\":\"%s\",", "864430010001091");
// encode_str[output_len] = '\0';
// printf("encode_str : %s\n",encode_str);
- char token[64] = {0};
- bp_get_token(token, 64);
- printf("bp_get_token:%s\n", token);
- retry = 0;
- while(strlen(token) == 0 && retry < 10)
- {
- bp_tcp_login();
- LOS_Msleep(500);
- bp_get_token(token, 64);
- }
// 构建content部分
data_len = 1 + strlen("POST") + 1 + strlen(URL_HEADER) + strlen(CREATE_URL) + strlen(token) + 1 + 1 + output_len + 1 + calculate_base64_length(cnt) + 1 + strlen(tail_encode_str);
goto FAILED;
uapi_get_TIME_STR(begin_time, begin_time_str, '-', ':', DATE_TIME_DEFULL);
uapi_get_TIME_STR(end_time, end_time_str, '-', ':', DATE_TIME_DEFULL);
- // printf("begin_time_str:%s\n", begin_time_str);
- // printf("end_time_str:%s\n", end_time_str);
+ printf("begin_time_str:%s\n", begin_time_str);
+ printf("end_time_str:%s\n", end_time_str);
// 构建JSON数据前缀部分
header_json = (char *)malloc(256);
printf("json_data malloc failed\n");
goto FAILED;
}
- // printf("json_data malloc succed\n");
+ printf("json_data malloc succed\n");
temp_len = 0;
memset(json_data, 0, json_len);
goto FAILED;
encode_len = temp_len * 3 / 2;
- // printf("encode_len : %d\n", encode_len);
+ printf("encode_len : %d\n", encode_len);
encode_str = (char *)malloc(encode_len);
if (encode_str == NULL)
{
mbedtls_base64_encode((unsigned char*)encode_str, encode_len, &output_len,
(const unsigned char*)json_data, strlen(json_data));
- // printf("Base64编码后长度: %d, 输出长度: %d\n", encode_len, (int)output_len);
+ printf("Base64编码后长度: %d, 输出长度: %d\n", encode_len, (int)output_len);
encode_str[output_len] = '\0';
// printf("encode_str : %s\n",encode_str);
snprintf(data_str, data_len, ",POST,%s%s,%s,%s", URL_HEADER, CREATE_URL, encode_header_str, encode_str);
sprintf(len_str, "%04x", strlen(data_str) + 8);
- // printf("data_len : %d\n",data_len);
+ printf("data_len : %d\n",data_len);
head_len = strlen(BID) + 1 + strlen(imei) + 1 + strlen(len_str) + 1 + strlen("TCP_HTTP") + 1;
head_str = (char *)malloc(head_len);
goto FAILED;
}
snprintf(code_str, code_len, "%s%s", head_str, KEY);
- // printf("code_len:%d\n", code_len);
+ printf("code_len:%d\n", code_len);
calculate_md5(code_str, md5_str);
if (strlen(md5_str) == 0)
}
snprintf(send_str, send_len, "[%s%s%s]", md5_str, head_str,data_str);
send_len = strlen(send_str);
- // printf("send_len : %d\n", send_len);
+ printf("send_len : %d\n", send_len);
// printf("send_str : %s\n", send_str);
- ret = tcp_send(g_listen_sock, send_str, send_len);
- if(ret != 0)
- {
- printf("tcp_send failed\n");
- goto FAILED;
- }
+ // ret = tcp_send(g_listen_sock, send_str, send_len);
+ // if(ret != 0)
+ // {
+ // printf("tcp_send failed\n");
+ // goto FAILED;
+ // }
- while(BP_GET_STATUS != 1)
+ for(int i = 0; i < send_len; i += 512)
{
- LOS_Msleep(5000);
- ret = tcp_send(g_listen_sock, send_str, send_len);
- if(ret != 0)
+ int chunk_size = (send_len - i > 512) ? 512 : send_len - i;
+
+ ret = tcp_send(g_listen_sock, send_str + i, chunk_size);
+ if(ret != 0)
{
- printf("tcp_send failed\n");
+ printf("tcp_send failed at offset %d\n", i);
goto FAILED;
}
+
+ LOS_Msleep(100);
}
-
// int offset = 0;
// while (offset < send_len)
// {
send_len = strlen(send_str);
printf("实际send_len : %d\n", send_len);
- // 打印调试信息
- printf("md5_str:%s\n", md5_str);
- printf("md5_len:%zu\n", strlen(md5_str));
- printf("head_len:%d\n", head_len);
- printf("head_str:%s\n", head_str);
- printf("data_len:%d\n", data_len);
- printf("data_str:%s\n", data_str);
- printf("send_len : %d\n", send_len);
- printf("send_str : %s\n", send_str);
+ // printf("md5_str:%s\n", md5_str);
+ // printf("md5_len:%zu\n", strlen(md5_str));
+ // printf("head_len:%d\n", head_len);
+ // printf("head_str:%s\n", head_str);
+ // printf("data_len:%d\n", data_len);
+ // printf("data_str:%s\n", data_str);
+ // printf("send_len : %d\n", send_len);
+ // printf("send_str : %s\n", send_str);
// 发送数据
tcp_send(g_listen_sock, send_str, send_len);
- LOS_Msleep(2000);
+
ret = 0; // 成功
FAILED:
}
snprintf(send_str, send_len, "[%s%s%s]", md5_str, head_str, data_str);
send_len = strlen(send_str);
- printf("实际send_len : %d\n", send_len);
+ // printf("实际send_len : %d\n", send_len);
- // 打印调试信息
- printf("md5_str:%s\n", md5_str);
- printf("md5_len:%zu\n", strlen(md5_str));
- printf("head_len:%d\n", head_len);
- printf("head_str:%s\n", head_str);
- printf("data_len:%d\n", data_len);
- printf("data_str:%s\n", data_str);
- printf("send_len : %d\n", send_len);
- printf("send_str : %s\n", send_str);
+ // printf("md5_str:%s\n", md5_str);
+ // printf("md5_len:%zu\n", strlen(md5_str));
+ // printf("head_len:%d\n", head_len);
+ // printf("head_str:%s\n", head_str);
+ // printf("data_len:%d\n", data_len);
+ // printf("data_str:%s\n", data_str);
+ // printf("send_len : %d\n", send_len);
+ // printf("send_str : %s\n", send_str);
// 发送数据
tcp_send(g_listen_sock, send_str, send_len);
- LOS_Msleep(2000);
+
ret = 0; // 成功
FAILED:
tcp_data = NULL;
tcp_data_cnt = 0;
}
- if (g_listen_sock >= 0)
- {
- lwip_close(g_listen_sock);
- g_listen_sock = -1;
- }
+ // if (g_listen_sock >= 0)
+ // {
+ // lwip_close(g_listen_sock);
+ // g_listen_sock = -1;
+ // }
BP_GET_STATUS = 0;
}
#endif
}
-void bp_tcp_init(void)
+static void bp_tcp_deinit(void)
+{
+ if (g_listen_sock >= 0)
+ {
+ lwip_close(g_listen_sock);
+ g_listen_sock = -1;
+ }
+}
+
+int bp_tcp_init(void)
{
- tcp_sign_up();
-
- if(LK_STATUS == 0 )
+ if(tcp_sign_up() != 0)
{
- printf("LK failed\n");
- return;
+ printf("tcp_sign_up failed\n");
+ return -1;
}
+ // if(LK_STATUS == 0 )
+ // {
+ // printf("LK failed\n");
+ // return -1;
+ // }
+
char token[64] = {0};
bp_get_token(token, 64);
if (strlen(token) == 0)
{
printf("token get failed\n");
- return;
+ return -1;
}
-
+ return 0;
}
static void tcp_data_handler(int *data,int len)
}
}
+ // if(LK_STATUS != 1 && g_listen_sock > 0)
+ // {
+ // bp_tcp_init();
+ // }
+
if(tcp_data_cnt >= MAX_CNT)
{
printf("tcp_data_cnt : %d\n",tcp_data_cnt);
LOS_Msleep(1000);
if(LK_STATUS == 1)
+ {
upload_bp_data(tcp_data,tcp_data_cnt);
+ // do_custom_cmd_service_check_msg(CUSTOM_CMD_MSG_BP_TEST, NULL, 0);
+ }
+
else
printf("NOT LK\n");
- LOS_Msleep(2000);
+ // LOS_Msleep(2000);
bp_tcp_data_clean();
}
else
{
printf("tcp_data_cnt:%d\n",tcp_data_cnt);
- memcpy_s(tcp_data + tcp_data_cnt,len * sizeof(int),data,len * sizeof(int));
+ memcpy_s(tcp_data + tcp_data_cnt,(MAX_CNT - tcp_data_cnt) * sizeof(int),data,len * sizeof(int));
// for(int i = tcp_data_cnt; i < tcp_data_cnt+len; i ++)
// {
}
else if(mail.cmd == BP_TCP_SERVICE_CMD_SERVER_DEINIT)
{
- bp_tcp_data_clean();
+ bp_tcp_deinit();
}
-
}
}
printf("rx_buffer malloc failed\n");
return;
}
- uint8_t retry = 0;
+
while (1)
{
if(!uapi_dev_runtime_get_bp_status_flag())
continue;
}
else
- bp_tcp_init();
+ do_bp_tcp_server_send_data(BP_TCP_SERVICE_CMD_SERVER_INIT,NULL,0);
}
// if (tcp_socket_init() != 0)
app_task_definition_t taskcfg={0};
taskcfg.create_queue_handle = create_bp_tcp_queue;
taskcfg.attr.name="bp_tcp_server_send_task";
- taskcfg.attr.priority = osPriorityNormal;
+ taskcfg.attr.priority = osPriorityBelowNormal3;
taskcfg.attr.stack_size = BP_TCP_BOARD_TASK_SIZE;
taskcfg.func = bp_tcp_server_send_task;
taskcfg.use_mem = USE_EXTEND_MEM;
taskcfg.create_queue_handle = NULL;
taskcfg.attr.name="bp_tcp_server_recv_task";
- taskcfg.attr.priority = osPriorityNormal;
+ taskcfg.attr.priority = osPriorityBelowNormal4;
taskcfg.attr.stack_size = BP_TCP_BOARD_TASK_SIZE;
taskcfg.func = bp_tcp_server_recv_task;
taskcfg.use_mem = USE_EXTEND_MEM;