消息:优化信息的处理逻辑 1501/1
authorCaisking <3449750381@qq.com>
Fri, 6 Dec 2024 10:33:40 +0000 (18:33 +0800)
committerCaisking <3449750381@qq.com>
Fri, 6 Dec 2024 10:33:40 +0000 (18:33 +0800)
code/sdk/application/wearable/fd230app/nativeui/message/src/MessageModel.cpp
code/sdk/application/wearable/service/ble_service/blemoder/bletest.h
code/sdk/application/wearable/service/ble_service/bleservice/bleANCS.c
code/sdk/application/wearable/service/ble_service/bleservice/bleEvent.c
code/sdk/application/wearable/service/msg_center/src/msg_center_customer.cpp
code/sdk/application/wearable/service/msg_center/src/notify/message_notification.cpp
code/sdk/drivers/peripheral/utils/custom_fileoperate_service.c
code/sdk/protocol/bt/host/bt/bts/bt_sample/ble_profile/ble_at_sample/src/sample_ble_ancs.c

index 41a0f97..4954658 100644 (file)
@@ -35,6 +35,7 @@
 #include <map>
 #include <service/graphic_service.h>
 #include <sstream>
+#include <stdlib.h>
 #include <string>
 #include <thread>
 #include <vector>
 #define MESSAGE_JSON_DIR "user/userdata/message/"
 #define PRINTFDATA 0
 
+#include "notification_manager.h"
+#include "msg_center.h"
+#include "message_notification.h"
+
 extern "C" errcode_t uapi_get_TIME_STR(uint32_t time, char *p_time,
                                        char date_separator, char time_separator,
                                        date_format_t format);
 
+// #include "msg_center_customerC.h"
+
+
+void show_notify_messgae() {
+    printf("show_notify_messgae\n");
+    OHOS::NotificationManager::GetInstance()->SetNotifyPriority(PRIORITY_THREE);
+    OHOS::NotificationManager::GetInstance()->ShowNotify(OHOS::CreateMessageView, TOPIC_EVENT_MESSAGENOTE, true);
+}
+
 #ifdef __cplusplus
 extern "C" {
 #endif
+
 static void Notify(void *args, const Topic topic, Request *request);
 
 using namespace OHOS;
@@ -111,70 +126,92 @@ void MessageUnsubscribeTopic(Topic topic) {
   broadcastApi->Release((IUnknown *)broadcastApi);
 }
 
-// 存储消息更新的定时器
-static uint32_t messageTimestamp = 0;
-
-static void Notify(void *args, const Topic topic, Request *request) {
-  UNUSED(args);
-  UNUSED(topic);
-
-  if (request == NULL) {
-    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Received NULL request");
-    return;
-  }
-
-  WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Received request %d", request->msgId);
-
-  if (request->msgId == TOPIC_EVENT_MESSAGE_UPDATA) {
-    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,
-                  "Processing TOPIC_EVENT_MESSAGE_UPDATA");
+// 存储消息更新的定时器  
+static uint32_t messageTimestamp = 0;  
+
+static void Notify(void *args, const Topic topic, Request *request) {  
+    UNUSED(args);  
+    UNUSED(topic);  
+
+    if (request == NULL) {  
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Received NULL request");  
+        return;  
+    }  
+
+    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Received request %d", request->msgId);  
+
+    if (request->msgId == TOPIC_EVENT_MESSAGE_UPDATA) {  
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Processing TOPIC_EVENT_MESSAGE_UPDATA");  
+
+        if (request->data == NULL) {  
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Received NULL data");  
+            return;  
+        }  
+
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Received request %d", request->msgId);
+        ancs_data_info_t *messageInfo = (ancs_data_info_t *)request->data;
+        ancs_data_info_t messageInfo_;
+  
+        messageInfo_.title = (char*)malloc(messageInfo->titlelen + 1);  
+        if (messageInfo_.title == NULL) {  
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to allocate memory for title");  
+            return;  
+        }  
+        memcpy(messageInfo_.title, messageInfo->title, messageInfo->titlelen);  
+        messageInfo_.title[messageInfo->titlelen] = '\0'; 
+
+
+        messageInfo_.msg =  (char*)malloc(messageInfo->msglen + 1);  
+        if (messageInfo_.msg == NULL) {  
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to allocate memory for message");  
+            free(messageInfo_.title); // Free previously allocated title  
+            return;  
+        }  
+        memcpy(messageInfo_.msg, messageInfo->msg, messageInfo->msglen);  
+        messageInfo_.msg[messageInfo->msglen] = '\0';  
+
+   
+        messageInfo_.appid = messageInfo->appid;  
+        messageInfo_.uid = messageInfo->uid;  
+        messageInfo_.time = messageInfo->time;  
+        messageInfo_.read = 0;  
+
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "message_data %p %p\r\n", messageInfo_.title, messageInfo_.msg);  
+
+
+        MessageModel::GetInstance().AddMessageData(&messageInfo_);  
+
+
+        uint32_t currentTimestamp = OHOS::TraingModel::GetInstance()->get_current_time();  
+        if (currentTimestamp - messageTimestamp <= 1) {  
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "messageTimestamp too short");  
+            free(messageInfo_.title);  
+            free(messageInfo_.msg);  
+            return;  
+        }  
+        messageTimestamp = currentTimestamp;  
+
+
+        if (uapi_dev_runtime_get_train_status() ||  
+            uapi_dev_runtime_get_saveele_status() ||  
+            uapi_dev_runtime_get_respiratory_training_status()) {  
+  
+            free(messageInfo_.title);  
+            free(messageInfo_.msg);  
+            return;  
+        }
 
-    // 创建新的消息信息
-    ancs_data_info_t *messageInfo = new (std::nothrow) ancs_data_info_t();
-    if (messageInfo == nullptr) {
-      WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,
-                    "Failed to allocate memory for ancs_data_info_t");
-      return; // 直接返回,不需要释放
-    }
 
-    // 复制请求数据到新的消息信息
-    int rets = memcpy_s(messageInfo, sizeof(ancs_data_info_t),
-                        (ancs_data_info_t *)request->data, request->len);
-    if (rets != EOK) {
-      delete messageInfo; // 释放内存
-      WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,
-                    "memcpy_s ancs_data_info_t failed, ret = %d", rets);
-      return;
-    }
+        // CustomEventPublish(TOPIC_EVENT_MESSAGENOTE, NULL, 0);
 
-    // 更新到模型里面的数组
-    MessageModel::GetInstance().AddMessageData(messageInfo);
+        show_notify_messgae();
 
-    // 检查时间戳
-    uint32_t currentTimestamp =
-        OHOS::TraingModel::GetInstance()->get_current_time();
-    if (currentTimestamp - messageTimestamp <= 1) {
-      WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "messageTimestamp too short");
-      delete messageInfo; // 确保释放内存
-      return;
-    }
-    messageTimestamp = currentTimestamp;
-
-    // 检查训练模式
-    if (uapi_dev_runtime_get_train_status() ||
-        uapi_dev_runtime_get_saveele_status() ||
-        uapi_dev_runtime_get_respiratory_training_status()) {
-      // 在这些状态下不弹窗
-      delete messageInfo; // 确保释放内存
-      return;
-    }
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Message popup sent");  
 
-    // 发送弹窗通知
-    CustomEventPublish(TOPIC_EVENT_MESSAGENOTE, NULL, 0);
-    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Message popup sent");
 
-    delete messageInfo; // 在成功完成后释放内存
-  }
+        free(messageInfo_.title);  
+        free(messageInfo_.msg);  
+    }  
 }
 void saveancsdata() {
   // printf("saveancs\r\n");
@@ -392,45 +429,43 @@ void MessageModel::SetCurrentIndex(uint8_t index) {
 uint8_t MessageModel::GetCurrentIndex() { return currentIndex_; }
 
 // 增加消息项[添加数组项]
-void MessageModel::AddMessageData(ancs_data_info_t *messageInfo) {
-  if (messageInfo == nullptr) {
-    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "messageInfo is null");
-    return;
-  }
-
-  // 创建并填充 MessageData
-  MessageData data;
-  data.isRead = messageInfo->read;
-  data.labelDetails = messageInfo->msg;
-  data.labelTitle = messageInfo->title;
-  data.timestamp = messageInfo->time;
-
-  WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP,
-                "AddMessageData data.isRead is %d, data.labelDetails is %s, "
-                "data.labelTitle is %s, data.timestamp is %d",
-                data.isRead, data.labelDetails.c_str(), data.labelTitle.c_str(),
-                data.timestamp);
-
-  // 处理时间字符串
-  char labelTime[20];
-  if (uapi_get_TIME_STR(data.timestamp, labelTime, '.', ':', DATE_Y_M_D_H_M) !=
-      ERRCODE_SUCC) {
-    data.labelTime = "--"; // 默认值
-  } else {
-    data.labelTime = labelTime; // 直接使用 labelTime
-  }
-
-  // 添加数据到消息向量
-  MessageVector.push_back(data);
-
-  // 删除多余的数据
-  if (MessageVector.size() > MaxMessageListCount) {
-    MessageVector.erase(MessageVector.begin(),
-                        MessageVector.begin() +
-                            (MessageVector.size() - MaxMessageListCount));
-  }
-
-  WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "messageInfo add is ok");
+void MessageModel::AddMessageData(ancs_data_info_t *messageInfo) {  
+    if (messageInfo == nullptr) {  
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "messageInfo is null");  
+        return;  
+    }  
+
+    // 创建并填充 MessageData  
+    MessageData data;  
+    data.isRead = messageInfo->read;  
+    data.labelDetails = messageInfo->msg ? messageInfo->msg : "";   
+    data.labelTitle = messageInfo->title ? messageInfo->title : ""; 
+    data.timestamp = messageInfo->time;  
+
+    WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP,  
+                  "AddMessageData data.isRead is %d, data.labelDetails is %s, "  
+                  "data.labelTitle is %s, data.timestamp is %u",  
+                  data.isRead, data.labelDetails.c_str(), data.labelTitle.c_str(),  
+                  data.timestamp);  
+
+    // 处理时间字符串  
+    char labelTime[20];  
+    if (uapi_get_TIME_STR(data.timestamp, labelTime, '.', ':', DATE_Y_M_D_H_M) != ERRCODE_SUCC) {  
+        data.labelTime = "--"; // 默认值  
+    } else {  
+        data.labelTime = labelTime; // 直接使用 labelTime  
+    }  
+
+    // 添加数据到消息向量  
+    MessageVector.push_back(data);  
+
+    // 删除多余的数据  
+    if (MessageVector.size() > MaxMessageListCount) {  
+        MessageVector.erase(MessageVector.begin(),   
+                            MessageVector.begin() + (MessageVector.size() - MaxMessageListCount));  
+    }  
+
+    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "messageInfo add is ok");  
 }
 // 获取目前的项
 MessageData *MessageModel::GetCurrentMessageData() {
index 4612663..e6c9587 100644 (file)
@@ -99,7 +99,9 @@ typedef struct ancs_data_info
     char *title;//消息发送者
     char *msg;//消息内容
     int hasCode;//消息hash code
-    int read;//是否已读 0 未读 1 已读
+    int read;   //是否已读 0 未读 1 已读
+    int titlelen;
+    int msglen;
 }ancs_data_info_t;
 
 struct message_infor_t
index ddef525..279b782 100644 (file)
@@ -174,7 +174,7 @@ uint8_t is_filtration_data(char *data)
 {
     for (size_t i = 0; i < filtration_data_count; i++)
     {
-        if (strcmp(data,filtration_data_T[i].name)==0)
+        if (strncmp(data,filtration_data_T[i].name,filtration_data_max_len)==0)
         {
             if(filtration_data_T[i].on)
             {
@@ -305,53 +305,72 @@ static void ble_ancs_client_notify_source_cbk(uint8_t client_id, uint16_t conn_i
 //static  char old_time_buf[64]={0};
 static uint32_t ancs_msg_index =0;
 
+static char title_buf[300] = {0};  
+static char msg_buf[300] = {0};  
+static char app_buf[70] = {0};  
 
 /* ANCS服务通知属性响应结果回调 */  
 static void ble_ancs_client_notification_attr_rsp_cbk(uint8_t client_id, uint16_t conn_id,  
     ble_ancs_notification_attr_rsp_t *notify_attr_rsp, errcode_t status)  
 {  
     if (status == ERRCODE_BT_SUCCESS) {  
+        if (notify_attr_rsp == NULL) {  
+            printf("[ANCSClientNotifyATTRSPCBK]notify_attr_rsp is NULL.\r\n");  
+            return;  
+        }  
+
         printf("[ANCSClientNotifyATTRSPCBK]conn_id:%d status:0x%x notify_uid:%lu\r\n",  
             conn_id, status, notify_attr_rsp->notification_uid);  
 
         ble_ancs_attr_rsp_t *node = (ble_ancs_attr_rsp_t *)notify_attr_rsp->attr_rsp_lst.head;  
-        uint16_t att_num = 1;  
-        char title_buf[300] = {0};  
-        char msg_buf[300] = {0};  
-        char app_buf[70] = {0};  
-        time_t timestamp = 0;  // 初始化时间戳  
+        uint16_t att_num = 0;  
+        time_t timestamp = 0;  
 
         while (node != NULL) {  
-            if (node != NULL) {  
+            if (node->att != NULL) {  
                 printf("[ANCSClientNotifyATTRSPCBK]att id:%d length:%lu att_value:%s\r\n",  
                     node->att_id, node->att_length, node->att);  
-                
+
+                size_t att_length = strlen((char*)node->att);  
                 if (node->att_id == 1) {  
-                    sprintf_s(title_buf, 300, "%s", node->att);  
+                    memset_s(title_buf, 300, 0, 300);  
+                    if (att_length < 300) {  
+                        memcpy_s(title_buf, 300, node->att, att_length);  
+                    } else {  
+                        printf("标题缓冲区溢出: %s\r\n", node->att);  
+                    }  
                 } else if (node->att_id == 3) {  
-                    sprintf_s(msg_buf, 300, "%s", node->att);  
+                    memset_s(msg_buf, 300, 0,300);  
+                    if (att_length < 300) {  
+                        memcpy_s(msg_buf, 300, node->att, att_length);  
+                    } else {  
+                        printf("消息缓冲区溢出: %s\r\n", node->att);  
+                    }  
                 } else if (node->att_id == 0) {  
-                    sprintf_s(app_buf, 70, "%s", node->att);  
+                    memset_s(app_buf, 70, 0, 70);  
+                    if (att_length < 70) {  
+                        memcpy_s(app_buf,70, node->att, att_length);  
+                    } else {  
+                        printf("应用缓冲区溢出: %s\r\n", node->att);  
+                    }  
                 } else if (node->att_id == 5) {  
                     struct tm tm = {0};  
-                    // 将 node->att 强制转换为 const char *  
                     if (strptime((const char *)node->att, "%Y%m%dT%H%M%S", &tm) != NULL) {  
-                        timestamp = mktime(&tm);  // 获取时间戳  
+                        timestamp = mktime(&tm);  
                     } else {  
                         printf("时间字符串解析失败: %s\r\n", node->att);  
                     }  
                 }  
             }  
             node = lnext(node);  
-            if (att_num == notify_attr_rsp->att_num) {  
+            att_num++;  
+            if (att_num >= notify_attr_rsp->att_num) {  
                 break;  
             }  
-            att_num++;  
         }  
 
         printf("ble_ancs_client_notification_attr_rsp_cbk test\r\n");  
 
-        // 只有在成功解析时间戳后才进行后续操作  
         if (is_filtration_data(app_buf) && (timestamp >= subscribe_notify_creationTime)) {  
             message_data(notify_attr_rsp->notification_uid, 0, title_buf, msg_buf, 0);  
         }  
index 5f86f84..8efdd04 100644 (file)
@@ -6544,18 +6544,24 @@ void message_data(int uid,int appid,char  *title,char  *msg,int hasCode)
         timestampe = 0;\r
     }\r
     ancs_data_info_t data;\r
-    //memset_s(&data,sizeof(ancs_data_info_t),0,sizeof(ancs_data_info_t));\r
+    data.title=malloc(strlen(title)+1);\r
+    data.msg = malloc(strlen(msg) + 1);\r
+    memset_s(data.title, TITLE_BUF_LEN, 0, strlen(title) + 1);\r
+    memset_s(data.msg, MSG_BUF_LEN, 0, strlen(msg) + 1);\r
+    memcpy_s(data.title, TITLE_BUF_LEN + 1, title, strlen(title));\r
+    memcpy_s(data.msg, MSG_BUF_LEN + 1, msg, strlen(msg));\r
 \r
-    data.appid=appid;\r
-    // memcpy_s(data.title,TITLE_BUF_LEN+1,title,strlen(title));\r
-    // memcpy_s(data.msg,MSG_BUF_LEN+1,msg,strlen(msg));\r
-    data.title=title;\r
-    data.msg=msg;\r
+\r
+    data.appid = appid;\r
     data.uid=uid;\r
     data.time=timestampe;\r
     data.read=0;\r
-    data.hasCode=hasCode;\r
-      WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP,"message_data\r\n");\r
+    data.hasCode = hasCode;\r
+\r
+    data.msglen = strlen(msg);\r
+    data.titlelen = strlen(title);\r
+    \r
+    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"message_data %p %p\r\n",  data.title,   data.msg);\r
 \r
     CustomEventPublish_TOPIC_EVENT_MESSAGENOTE(&data);\r
     return ;\r
index de6d7e4..d5c0eb6 100644 (file)
@@ -2058,11 +2058,7 @@ void MsgCenterReportBtStatusToJS(uint16_t msgId)
     }
 }
 
-void show_notify_messgae() {
-    printf("show_notify_messgae\n");
-    NotificationManager::GetInstance()->SetNotifyPriority(PRIORITY_THREE);
-    NotificationManager::GetInstance()->ShowNotify(showMessageNotify, TOPIC_EVENT_MESSAGENOTE, true);
-}
+
 
 #endif
 
index c2e5beb..499d79c 100644 (file)
@@ -79,7 +79,7 @@ bool MessageViewEventListener::OnDrag(UIView &view, const DragEvent &event) {
 bool MessageViewEventListener::OnDragEnd(UIView &view, const DragEvent &event) {
   WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "MessageViewEventListener::OnDragEnd");
   if (event.GetDragDirection() == DragEvent::DIRECTION_LEFT_TO_RIGHT) {
-    NotificationManager::GetInstance()->StopNotify();
+    // NotificationManager::GetInstance()->StopNotify();
   }
   return true;
 }
index eadd410..bc90505 100644 (file)
@@ -192,7 +192,7 @@ static void custom_fileoperate_service_check_task(void const *arg)
             break;
 
         case CUSTOM_FILEOPERATE_MSG_TYPE_MESSAGE_DATA_NOTIFY:
-            show_notify_messgae();
+            // show_notify_messgae();
             break;
 
             PRINT("Unknown CUSTOM_FILE_OPERATE_MSG cmd 0x%02X \r\n", mail.cmd);
index 560afcd..7c37258 100644 (file)
@@ -76,30 +76,32 @@ static void ble_ancs_client_notify_source_cbk(uint8_t client_id, uint16_t conn_i
 }
 
 /* ANCS服务通知属性响应结果回调 */
-static void ble_ancs_client_notification_attr_rsp_cbk(uint8_t client_id, uint16_t conn_id,
-    ble_ancs_notification_attr_rsp_t *notify_attr_rsp, errcode_t status)
-{
-    if (status == ERRCODE_BT_SUCCESS) {
-        bts_info("[ANCSClientNotifyATTRSPCBK]conn_id:%d status:0x%x notify_uid:%lu\r\n",
-            conn_id, status, notify_attr_rsp->notification_uid);
+static void ble_ancs_client_notification_attr_rsp_cbk(uint8_t client_id, uint16_t conn_id,  
+    ble_ancs_notification_attr_rsp_t *notify_attr_rsp, errcode_t status)  
+{  
+    if (status == ERRCODE_BT_SUCCESS) {  
+        if (notify_attr_rsp == NULL) {  
+            bts_error("[ANCSClientNotifyATTRSPCBK]notify_attr_rsp is NULL.\r\n");  
+            return;  
+        }  
 
-        ble_ancs_attr_rsp_t *node = (ble_ancs_attr_rsp_t *)notify_attr_rsp->attr_rsp_lst.head;
-        uint16_t att_num = 1;
-        while (node != NULL) {
-            if (node != NULL) {
-                bts_info_no_header("[ANCSClientNotifyATTRSPCBK]att id:%d length:%lu att_value:%s\r\n",
-                    node->att_id, node->att_length, node->att);
-            }
-            node = lnext(node);
-            if (att_num == notify_attr_rsp->att_num) {
-                break;
-            }
-            att_num = att_num + 1;
-        }
-    } else {
-        bts_error("[ANCSClientNotifyATTRSPCBK]conn_id:%d status:0x%x.\r\n",
-            conn_id, status);
-    }
+        bts_info("[ANCSClientNotifyATTRSPCBK]conn_id:%d status:0x%x notify_uid:%lu\r\n",  
+            conn_id, status, notify_attr_rsp->notification_uid);  
+        
+        ble_ancs_attr_rsp_t *node = (ble_ancs_attr_rsp_t *)notify_attr_rsp->attr_rsp_lst.head;  
+        uint16_t att_num = 0; // 初始化为0  
+        while (node != NULL && att_num < notify_attr_rsp->att_num) {  
+            if (node->att != NULL) { // 确保 att 不为 NULL  
+                bts_info_no_header("[ANCSClientNotifyATTRSPCBK]att id:%d length:%lu att_value:%s\r\n",  
+                    node->att_id, node->att_length, node->att);  
+            }  
+            node = lnext(node);  
+            att_num++;  
+        }  
+    } else {  
+        bts_error("[ANCSClientNotifyATTRSPCBK]conn_id:%d status:0x%x.\r\n",  
+            conn_id, status);  
+    }  
 }
 
 /* ANCS服务应用属性响应结果回调 */