#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;
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");
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() {
{
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)
{
//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);
}
}
/* 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服务应用属性响应结果回调 */