增加代码健壮性和可读性
This commit is contained in:
@@ -1,6 +1,4 @@
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include "list.h"
|
||||
#include "has_task_msg.h"
|
||||
#include "has_task_msg_os_port.h"
|
||||
@@ -15,10 +13,12 @@
|
||||
#endif
|
||||
|
||||
#ifdef MSG_OPT_DEBUG
|
||||
/* 打开检查订阅表是否配置正确 */
|
||||
#define MSG_OPT_CHECK_SUBSCRIBE_LIST
|
||||
#define msg_debug(fmt, ...) msg_printf("[msg]"fmt, ##__VA_ARGS__)
|
||||
#else
|
||||
#define msg_debug(fmt, ...)
|
||||
#endif
|
||||
|
||||
#define MSG_ASSERT {while(1);}
|
||||
|
||||
#pragma pack(1)
|
||||
#ifdef MSG_OPT_PUB_LIST
|
||||
@@ -59,7 +59,7 @@ static has_static_pub_list_t g_pub_list[MODULE_MAX] = {0};
|
||||
|
||||
/* 静态订阅列表 */
|
||||
typedef struct {
|
||||
#if defined(MSG_OPT_CHECK_SUBSCRIBE_LIST) || defined(MSG_OPT_DYNAMIC_SUB)
|
||||
#if defined(MSG_OPT_DYNAMIC_SUB)
|
||||
unsigned char module_id; // 当前模块ID
|
||||
unsigned char sub_module_cnt; // 订阅数量
|
||||
unsigned char sub_module[MODULE_MAX - 1]; // 订阅表
|
||||
@@ -76,35 +76,24 @@ typedef struct {
|
||||
static has_static_sub_list_t g_sub_list[] = SUBSCIBE_INFO;
|
||||
static unsigned char g_sub_list_index[MODULE_MAX] = {0}; // 订阅表id索引
|
||||
|
||||
#ifdef MSG_OPT_CHECK_SUBSCRIBE_LIST
|
||||
#define MSG_CHECK_SUBSCIBE_INFO(id, ret) \
|
||||
if ((id) == INVALID_ID) { \
|
||||
msg_printf("%s invalid id:0, please check SUBSCIBE_INFO\n", __func__); \
|
||||
return (ret);}
|
||||
#else
|
||||
#define MSG_CHECK_SUBSCIBE_INFO(id, ret)
|
||||
#endif
|
||||
|
||||
/* 检查ID是否合法,id:检查的ID,ret:不合法时return的结果 */
|
||||
#define MSG_CHECK_MODULE_ID(id, ret) \
|
||||
if (((id) >= MODULE_MAX) || ((id) == INVALID_ID)) { \
|
||||
msg_printf("%s ID wrong:%d\n", __func__, (id)); return (ret); }
|
||||
|
||||
int has_msg_init() {
|
||||
int has_msg_init()
|
||||
{
|
||||
int j;
|
||||
memset(g_sub_list_index, 0, sizeof(g_sub_list_index));
|
||||
#ifdef MSG_OPT_PUB_LIST
|
||||
memset(g_pub_list, 0, sizeof(g_pub_list));
|
||||
#endif
|
||||
for (int i = 0; i < (sizeof(g_sub_list) / sizeof(g_sub_list[0])); i++) {
|
||||
#ifdef MSG_OPT_CHECK_SUBSCRIBE_LIST
|
||||
/* 检查各模块ID是否配置正确 */
|
||||
if ((g_sub_list[i].module_id >= MODULE_MAX) || (g_sub_list[i].module_id == INVALID_ID)) {
|
||||
msg_printf("module_id:%d is invalid, please check the macro:SUBSCIBE_INFO row:%d\n", g_sub_list[i].module_id, i);
|
||||
memset(g_sub_list, 0, sizeof(g_sub_list));
|
||||
#ifdef MSG_OPT_PUB_LIST
|
||||
memset(g_pub_list, 0, sizeof(g_pub_list));
|
||||
#endif
|
||||
MSG_ASSERT; // 配置都能写错,必须进断言
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -113,26 +102,19 @@ int has_msg_init() {
|
||||
if (g_sub_list[i].sub_module[j] == INVALID_ID) {
|
||||
break;
|
||||
} else if (g_sub_list[i].sub_module[j] >= MODULE_MAX) {
|
||||
msg_printf("sub_module id:%d is invalid, module:id %d\n please check the macro:\
|
||||
SUBSCIBE_INFO row:%d, sub list:%d\n", g_sub_list[i].sub_module[j], g_sub_list[i].module_id, i, j);
|
||||
memset(g_sub_list, 0, sizeof(g_sub_list));
|
||||
#ifdef MSG_OPT_PUB_LIST
|
||||
memset(g_pub_list, 0, sizeof(g_pub_list));
|
||||
#endif
|
||||
msg_printf("sub_module id:%d is invalid, module:id %d\nplease check the macro:"
|
||||
" SUBSCIBE_INFO row:%d, sub list:%d\n", g_sub_list[i].sub_module[j], g_sub_list[i].module_id, i, j);
|
||||
MSG_ASSERT; // 配置都能写错,必须进断言
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
/* 检查订阅数量是否配置正确 */
|
||||
if (g_sub_list[i].sub_module_cnt != j) {
|
||||
msg_printf("sub_module_cnt is config as %d, which actually is %d, please check the\
|
||||
macro: SUBSCIBE_INFO row:%d\n", g_sub_list[i].sub_module_cnt, j, i);
|
||||
memset(g_sub_list, 0, sizeof(g_sub_list));
|
||||
#ifdef MSG_OPT_PUB_LIST
|
||||
memset(g_pub_list, 0, sizeof(g_pub_list));
|
||||
#endif
|
||||
msg_printf("sub_module_cnt is config as %d, which actually is %d, please check the"
|
||||
" macro: SUBSCIBE_INFO row:%d\n", g_sub_list[i].sub_module_cnt, j, i);
|
||||
MSG_ASSERT; // 配置都能写错,必须进断言
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
g_sub_list_index[g_sub_list[i].module_id] = i; // 索引表赋值
|
||||
#ifdef MSG_OPT_PUB_LIST
|
||||
/* 构建发布表 */
|
||||
@@ -148,11 +130,31 @@ int has_msg_init() {
|
||||
msg_mutex_init(&g_sub_list[i].msg_mutex); // 初始化该模块互斥量
|
||||
INIT_LIST_HEAD(&g_sub_list[i].msg_list); // 初始化订阅链表
|
||||
}
|
||||
#ifdef MSG_OPT_DEBUG
|
||||
// for (int i = 0; i < (sizeof(g_sub_list) / sizeof(g_sub_list[0])); i++) {
|
||||
// msg_printf("module:%d subscribes to:\n", g_sub_list[i].module_id);
|
||||
// for (int x = 0; x < g_sub_list[i].sub_module_cnt; x++) {
|
||||
// msg_printf("%d ", g_sub_list[i].sub_module[x]);
|
||||
// }
|
||||
// msg_printf("\n");
|
||||
// }
|
||||
#ifdef MSG_OPT_PUB_LIST
|
||||
// msg_printf("\n");
|
||||
// for (int i = 1; i < MODULE_MAX; i++) {
|
||||
// msg_printf("module:%d publish to:\n", i);
|
||||
// for (int x = 0; x < g_pub_list[i].pub_num; x++) {
|
||||
// msg_printf("%d ", g_pub_list[i].pub_module[x]);
|
||||
// }
|
||||
// msg_printf("\n");
|
||||
// }
|
||||
#endif
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int has_msg_publish(has_module_ID_e module_id, void *buffer, unsigned int length) {
|
||||
int has_msg_publish(has_module_ID_e module_id, void *buffer, unsigned int length)
|
||||
{
|
||||
has_msg_node_t *node;
|
||||
if (buffer == NULL) {
|
||||
return -1;
|
||||
@@ -176,6 +178,7 @@ int has_msg_publish(has_module_ID_e module_id, void *buffer, unsigned int length
|
||||
msg_printf("malloc fail,pub id:%d\n", module_id);
|
||||
return -1;
|
||||
}
|
||||
msg_debug("publish: id:%d node addr:0x%p\n", module_id, node);
|
||||
msg_buffer = (has_msg_buffer_t *)(&node[g_pub_list[module_id].pub_num]);
|
||||
msg_buffer->node_cnt = g_pub_list[module_id].pub_num;
|
||||
msg_buffer->length = length;
|
||||
@@ -192,13 +195,15 @@ int has_msg_publish(has_module_ID_e module_id, void *buffer, unsigned int length
|
||||
#else
|
||||
int i;
|
||||
|
||||
msg_debug("publish: id:%d\n", module_id);
|
||||
for (i = 0; i < (sizeof(g_sub_list) / sizeof(g_sub_list[0])); i++) { // 遍历模块
|
||||
#ifdef MSG_OPT_DYNAMIC_SUB
|
||||
msg_mutex_lock(&g_sub_list[i].msg_mutex);
|
||||
#endif
|
||||
for (int j = 0; j < g_sub_list[i].sub_module_cnt; j++) { // 查找订阅表
|
||||
if (module_id == g_sub_list[i].sub_module[j]) { // 找到订阅信息
|
||||
if ((unsigned char)module_id == g_sub_list[i].sub_module[j]) { // 找到订阅信息
|
||||
node = msg_malloc(sizeof(has_msg_node_t) + length);
|
||||
msg_debug("node addr:0x%p\n", node);
|
||||
if (node == NULL) {
|
||||
msg_printf("malloc fail,pub id:%d\n", module_id);
|
||||
#ifdef MSG_OPT_DYNAMIC_SUB
|
||||
@@ -230,27 +235,34 @@ int has_msg_publish(has_module_ID_e module_id, void *buffer, unsigned int length
|
||||
|
||||
/* TODO:更合理的方式是每个消息一个互斥量,但是可能会导致每个消息malloc和free的时候频繁init和deinit */
|
||||
#ifdef MSG_OPT_PUB_LIST
|
||||
/* 检查计数判断该消息是否被所有订阅模块处理完,处理完就释放内存
|
||||
msg_buff_stu:消息结构体指针 np:节点结构体指针 */
|
||||
#define MSG_CHECK_AND_FREE_MESSAGE(msg_buff_stu, np) \
|
||||
{ \
|
||||
msg_mutex_lock(&g_pub_list[msg_buff_stu->module_id].buffer_mutex); \
|
||||
msg_buff_stu->node_cnt --; \
|
||||
if (msg_buff_stu->node_cnt == 0) { \
|
||||
np = (has_msg_node_t *)((unsigned char *)msg_buff_stu \
|
||||
- g_pub_list[msg_buff_stu->module_id].pub_num * sizeof(has_msg_node_t)); \
|
||||
msg_free(np);} \
|
||||
msg_mutex_unlock(&g_pub_list[msg_buff_stu->module_id].buffer_mutex); \
|
||||
/* 检查计数判断该消息是否被所有订阅模块处理完,处理完就释放内存 */
|
||||
static inline void msg_check_and_free_message(has_msg_node_t *node)
|
||||
{
|
||||
has_msg_node_t *temp = NULL;
|
||||
unsigned char id = node->msg_buff->module_id;
|
||||
#ifdef MSG_OPT_USE_MUTEX
|
||||
msg_mutex_t *target_mutex = &g_pub_list[id].buffer_mutex;
|
||||
#endif
|
||||
msg_mutex_lock(target_mutex);
|
||||
node->msg_buff->node_cnt --;
|
||||
if (node->msg_buff->node_cnt == 0) {
|
||||
temp = ((has_msg_node_t *)node->msg_buff - g_pub_list[id].pub_num);
|
||||
}
|
||||
msg_mutex_unlock(target_mutex);
|
||||
if (temp) {
|
||||
msg_debug("free node addr:0x%p\n", temp);
|
||||
msg_free(temp);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
int has_msg_handle(has_module_ID_e module_id, has_msg_handle_cb cb) {
|
||||
int has_msg_handle(has_module_ID_e module_id, has_msg_handle_cb cb)
|
||||
{
|
||||
has_msg_node_t *node;
|
||||
has_static_sub_list_t *sub_list;
|
||||
MSG_CHECK_MODULE_ID(module_id, -1);
|
||||
|
||||
sub_list = &g_sub_list[g_sub_list_index[module_id]];
|
||||
MSG_CHECK_SUBSCIBE_INFO(sub_list->module_id, -1);
|
||||
|
||||
/* 处理一个消息,先入先出 */
|
||||
msg_mutex_lock(&sub_list->msg_mutex);
|
||||
@@ -264,31 +276,31 @@ int has_msg_handle(has_module_ID_e module_id, has_msg_handle_cb cb) {
|
||||
}
|
||||
|
||||
#ifdef MSG_OPT_PUB_LIST
|
||||
has_msg_buffer_t *msg_buffer;
|
||||
msg_buffer = node->msg_buff;
|
||||
if (cb != NULL) {
|
||||
cb(msg_buffer->module_id, msg_buffer->buffer, msg_buffer->length);
|
||||
cb(node->msg_buff->module_id, node->msg_buff->buffer, node->msg_buff->length);
|
||||
}
|
||||
MSG_CHECK_AND_FREE_MESSAGE(msg_buffer, node);
|
||||
msg_debug("handle:pub id:%d handle id:%d\n", node->msg_buff->module_id, module_id);
|
||||
msg_check_and_free_message(node);
|
||||
#else
|
||||
if (cb != NULL) {
|
||||
cb(node->module_id, node->buffer, node->length);
|
||||
}
|
||||
msg_debug("handle:pub id:%d handle id:%d free addr:0x%p\n", node->module_id, module_id, node);
|
||||
msg_free(node);
|
||||
#endif
|
||||
|
||||
msg_free(node);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int has_msg_handle_by_module(has_module_ID_e module_id, has_msg_handle_cb cb, has_module_ID_e pub_module_id) {
|
||||
int has_msg_handle_by_module(has_module_ID_e module_id, has_msg_handle_cb cb, has_module_ID_e pub_module_id)
|
||||
{
|
||||
has_msg_node_t *node, *temp;
|
||||
has_static_sub_list_t *sub_list;
|
||||
MSG_CHECK_MODULE_ID(module_id, -1);
|
||||
MSG_CHECK_MODULE_ID(pub_module_id, -1);
|
||||
|
||||
sub_list = &g_sub_list[g_sub_list_index[module_id]];
|
||||
MSG_CHECK_SUBSCIBE_INFO(sub_list->module_id, -1);
|
||||
|
||||
/* 处理一个指定模块的消息,先入先出 */
|
||||
msg_mutex_lock(&sub_list->msg_mutex);
|
||||
@@ -310,30 +322,30 @@ int has_msg_handle_by_module(has_module_ID_e module_id, has_msg_handle_cb cb, ha
|
||||
}
|
||||
|
||||
#ifdef MSG_OPT_PUB_LIST
|
||||
has_msg_buffer_t *msg_buffer;
|
||||
msg_buffer = node->msg_buff;
|
||||
if (cb != NULL) {
|
||||
cb(msg_buffer->module_id, msg_buffer->buffer, msg_buffer->length);
|
||||
cb(node->msg_buff->module_id, node->msg_buff->buffer, node->msg_buff->length);
|
||||
}
|
||||
MSG_CHECK_AND_FREE_MESSAGE(msg_buffer, node);
|
||||
msg_debug("handle:pub id:%d handle id:%d\n", node->msg_buff->module_id, module_id);
|
||||
msg_check_and_free_message(node);
|
||||
#else
|
||||
if (cb != NULL) {
|
||||
cb(node->module_id, node->buffer, node->length);
|
||||
}
|
||||
msg_debug("handle:pub id:%d handle id:%d free addr:0x%p\n", node->module_id, module_id, node);
|
||||
msg_free(node);
|
||||
#endif
|
||||
|
||||
msg_free(node);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int has_msg_handle_latest(has_module_ID_e module_id, has_msg_handle_cb cb) {
|
||||
int has_msg_handle_latest(has_module_ID_e module_id, has_msg_handle_cb cb)
|
||||
{
|
||||
has_msg_node_t *node;
|
||||
has_static_sub_list_t *sub_list;
|
||||
MSG_CHECK_MODULE_ID(module_id, -1);
|
||||
|
||||
sub_list = &g_sub_list[g_sub_list_index[module_id]];
|
||||
MSG_CHECK_SUBSCIBE_INFO(sub_list->module_id, -1);
|
||||
|
||||
/* 处理最新的消息 */
|
||||
msg_mutex_lock(&sub_list->msg_mutex);
|
||||
@@ -347,26 +359,27 @@ int has_msg_handle_latest(has_module_ID_e module_id, has_msg_handle_cb cb) {
|
||||
}
|
||||
|
||||
#ifdef MSG_OPT_PUB_LIST
|
||||
has_msg_buffer_t *msg_buffer;
|
||||
msg_buffer = node->msg_buff;
|
||||
if (cb != NULL) {
|
||||
cb(msg_buffer->module_id, msg_buffer->buffer, msg_buffer->length);
|
||||
cb(node->msg_buff->module_id, node->msg_buff->buffer, node->msg_buff->length);
|
||||
}
|
||||
MSG_CHECK_AND_FREE_MESSAGE(msg_buffer, node);
|
||||
msg_debug("handle:pub id:%d handle id:%d\n", node->msg_buff->module_id, module_id);
|
||||
msg_check_and_free_message(node);
|
||||
#else
|
||||
if (cb != NULL) {
|
||||
cb(node->module_id, node->buffer, node->length);
|
||||
}
|
||||
msg_debug("handle:pub id:%d handle id:%d free addr:0x%p\n", node->module_id, module_id, node);
|
||||
msg_free(node);
|
||||
#endif
|
||||
|
||||
msg_free(node);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 将消息复制出接口处理,不建议使用 */
|
||||
#if 1
|
||||
unsigned int has_msg_receive(has_module_ID_e module_id, unsigned char *pub_module_id, unsigned char *buf_out) {
|
||||
unsigned int has_msg_receive(has_module_ID_e module_id, unsigned char *pub_module_id, unsigned char *buf_out)
|
||||
{
|
||||
has_msg_node_t *node;
|
||||
has_static_sub_list_t *sub_list;
|
||||
unsigned int len = 0;
|
||||
@@ -376,7 +389,6 @@ unsigned int has_msg_receive(has_module_ID_e module_id, unsigned char *pub_modul
|
||||
}
|
||||
|
||||
sub_list = &g_sub_list[g_sub_list_index[module_id]];
|
||||
MSG_CHECK_SUBSCIBE_INFO(sub_list->module_id, 0);
|
||||
|
||||
/* 处理一个消息,先入先出 */
|
||||
msg_mutex_lock(&sub_list->msg_mutex);
|
||||
@@ -390,14 +402,13 @@ unsigned int has_msg_receive(has_module_ID_e module_id, unsigned char *pub_modul
|
||||
}
|
||||
|
||||
#ifdef MSG_OPT_PUB_LIST
|
||||
has_msg_buffer_t *msg_buffer;
|
||||
msg_buffer = node->msg_buff;
|
||||
memcpy(buf_out, msg_buffer->buffer, msg_buffer->length);
|
||||
memcpy(buf_out, node->msg_buff->buffer, node->msg_buff->length);
|
||||
if (pub_module_id != NULL) {
|
||||
*pub_module_id = msg_buffer->module_id;
|
||||
*pub_module_id = node->msg_buff->module_id;
|
||||
}
|
||||
len = msg_buffer->length;
|
||||
MSG_CHECK_AND_FREE_MESSAGE(msg_buffer, node);
|
||||
len = node->msg_buff->length;
|
||||
msg_debug("handle:pub id:%d handle id:%d\n", node->msg_buff->module_id, module_id);
|
||||
msg_check_and_free_message(node);
|
||||
#else
|
||||
memcpy(buf_out, node->buffer, node->length);
|
||||
if (pub_module_id != NULL) {
|
||||
@@ -405,12 +416,14 @@ unsigned int has_msg_receive(has_module_ID_e module_id, unsigned char *pub_modul
|
||||
}
|
||||
|
||||
len = node->length;
|
||||
msg_debug("handle:pub id:%d handle id:%d free addr:0x%p\n", node->module_id, module_id, node);
|
||||
msg_free(node);
|
||||
#endif
|
||||
return len;
|
||||
}
|
||||
|
||||
unsigned int has_msg_receive_latest(has_module_ID_e module_id, unsigned char *pub_module_id, unsigned char *buf_out) {
|
||||
unsigned int has_msg_receive_latest(has_module_ID_e module_id, unsigned char *pub_module_id, unsigned char *buf_out)
|
||||
{
|
||||
has_msg_node_t *node;
|
||||
has_static_sub_list_t *sub_list;
|
||||
unsigned int len = 0;
|
||||
@@ -420,7 +433,6 @@ unsigned int has_msg_receive_latest(has_module_ID_e module_id, unsigned char *pu
|
||||
}
|
||||
|
||||
sub_list = &g_sub_list[g_sub_list_index[module_id]];
|
||||
MSG_CHECK_SUBSCIBE_INFO(sub_list->module_id, 0);
|
||||
|
||||
/* 处理最新的消息 */
|
||||
msg_mutex_lock(&sub_list->msg_mutex);
|
||||
@@ -434,14 +446,13 @@ unsigned int has_msg_receive_latest(has_module_ID_e module_id, unsigned char *pu
|
||||
}
|
||||
|
||||
#ifdef MSG_OPT_PUB_LIST
|
||||
has_msg_buffer_t *msg_buffer;
|
||||
msg_buffer = node->msg_buff;
|
||||
memcpy(buf_out, msg_buffer->buffer, msg_buffer->length);
|
||||
memcpy(buf_out, node->msg_buff->buffer, node->msg_buff->length);
|
||||
if (pub_module_id != NULL) {
|
||||
*pub_module_id = msg_buffer->module_id;
|
||||
*pub_module_id = node->msg_buff->module_id;
|
||||
}
|
||||
len = msg_buffer->length;
|
||||
MSG_CHECK_AND_FREE_MESSAGE(msg_buffer, node);
|
||||
len = node->msg_buff->length;
|
||||
msg_debug("handle:pub id:%d handle id:%d\n", node->msg_buff->module_id, module_id);
|
||||
msg_check_and_free_message(node);
|
||||
#else
|
||||
memcpy(buf_out, node->buffer, node->length);
|
||||
if (pub_module_id != NULL) {
|
||||
@@ -449,20 +460,21 @@ unsigned int has_msg_receive_latest(has_module_ID_e module_id, unsigned char *pu
|
||||
}
|
||||
|
||||
len = node->length;
|
||||
msg_debug("handle:pub id:%d handle id:%d free addr:0x%p\n", node->module_id, module_id, node);
|
||||
msg_free(node);
|
||||
#endif
|
||||
return len;
|
||||
}
|
||||
#endif
|
||||
|
||||
unsigned int has_msg_is_message_empty(has_module_ID_e module_id) {
|
||||
unsigned int has_msg_is_message_empty(has_module_ID_e module_id)
|
||||
{
|
||||
has_msg_node_t *node, *temp;
|
||||
has_static_sub_list_t *sub_list;
|
||||
unsigned int num = 0;
|
||||
MSG_CHECK_MODULE_ID(module_id, -1);
|
||||
|
||||
sub_list = &g_sub_list[g_sub_list_index[module_id]];
|
||||
MSG_CHECK_SUBSCIBE_INFO(sub_list->module_id, -1);
|
||||
|
||||
msg_mutex_lock(&sub_list->msg_mutex);
|
||||
if (list_empty(&sub_list->msg_list)) {
|
||||
@@ -474,14 +486,14 @@ unsigned int has_msg_is_message_empty(has_module_ID_e module_id) {
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int has_msg_get_message_number(has_module_ID_e module_id) {
|
||||
unsigned int has_msg_get_message_number(has_module_ID_e module_id)
|
||||
{
|
||||
has_msg_node_t *node, *temp;
|
||||
has_static_sub_list_t *sub_list;
|
||||
unsigned int num = 0;
|
||||
MSG_CHECK_MODULE_ID(module_id, 0);
|
||||
|
||||
sub_list = &g_sub_list[g_sub_list_index[module_id]];
|
||||
MSG_CHECK_SUBSCIBE_INFO(sub_list->module_id, 0);
|
||||
|
||||
msg_mutex_lock(&sub_list->msg_mutex);
|
||||
list_for_each_entry_safe(node, temp, &sub_list->msg_list, list) {
|
||||
@@ -491,42 +503,73 @@ unsigned int has_msg_get_message_number(has_module_ID_e module_id) {
|
||||
return num;
|
||||
}
|
||||
|
||||
int has_msg_printf_subscribe(has_module_ID_e module_id) {
|
||||
int has_msg_printf_subscribe(has_module_ID_e module_id)
|
||||
{
|
||||
has_static_sub_list_t *sub_list;
|
||||
MSG_CHECK_MODULE_ID(module_id, -1);
|
||||
|
||||
sub_list = &g_sub_list[g_sub_list_index[module_id]];
|
||||
MSG_CHECK_SUBSCIBE_INFO(sub_list->module_id, -1);
|
||||
|
||||
msg_printf("module:%d subscribes to:\n", module_id);
|
||||
for (int i = 0; i < sub_list->sub_module_cnt; i++) {
|
||||
msg_printf("%d ", sub_list->sub_module[i]);
|
||||
}
|
||||
msg_printf("\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int has_msg_delete_all_message(has_module_ID_e module_id) {
|
||||
int has_msg_delete_all_message(has_module_ID_e module_id)
|
||||
{
|
||||
has_msg_node_t *node, *temp;
|
||||
has_static_sub_list_t *sub_list;
|
||||
#ifdef MSG_OPT_PUB_LIST
|
||||
struct list_head free_list;
|
||||
has_msg_node_t *free_temp;
|
||||
unsigned char id;
|
||||
INIT_LIST_HEAD(&free_list);
|
||||
#endif
|
||||
MSG_CHECK_MODULE_ID(module_id, -1);
|
||||
|
||||
sub_list = &g_sub_list[g_sub_list_index[module_id]];
|
||||
MSG_CHECK_SUBSCIBE_INFO(sub_list->module_id, -1);
|
||||
|
||||
msg_mutex_lock(&sub_list->msg_mutex);
|
||||
if (!list_empty(&sub_list->msg_list)) {
|
||||
list_for_each_entry_safe(node, temp, &sub_list->msg_list, list) {
|
||||
list_del(&node->list);
|
||||
#ifdef MSG_OPT_PUB_LIST
|
||||
list_add_tail(&node->list, &free_list); // 加入临时链表
|
||||
#else
|
||||
msg_debug("id:%d free addr:0x%p\n", module_id, node);
|
||||
msg_free(node);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
INIT_LIST_HEAD(&sub_list->msg_list);
|
||||
msg_mutex_unlock(&sub_list->msg_mutex);
|
||||
#ifdef MSG_OPT_PUB_LIST
|
||||
/* 释放内存 */
|
||||
list_for_each_entry_safe(node, temp, &free_list, list) {
|
||||
/* msg_check_and_free_message(node); */
|
||||
free_temp = NULL;
|
||||
id = node->msg_buff->module_id;
|
||||
msg_mutex_lock(&g_pub_list[id].buffer_mutex);
|
||||
node->msg_buff->node_cnt --;
|
||||
if (node->msg_buff->node_cnt == 0) {
|
||||
free_temp = ((has_msg_node_t *)node->msg_buff - g_pub_list[id].pub_num);
|
||||
}
|
||||
msg_mutex_unlock(&g_pub_list[id].buffer_mutex);
|
||||
if (free_temp) {
|
||||
msg_debug("id:%d free addr:0x%p\n", module_id, free_temp);
|
||||
msg_free(free_temp);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef MSG_OPT_DYNAMIC_SUB
|
||||
int has_msg_subscribe(has_module_ID_e module_id, has_module_ID_e sub_id) {
|
||||
int has_msg_subscribe(has_module_ID_e module_id, has_module_ID_e sub_id)
|
||||
{
|
||||
has_static_sub_list_t *sub_list;
|
||||
int j;
|
||||
if ((module_id >= MODULE_MAX) || (sub_id >= MODULE_MAX)
|
||||
@@ -534,7 +577,6 @@ int has_msg_subscribe(has_module_ID_e module_id, has_module_ID_e sub_id) {
|
||||
return -1;
|
||||
}
|
||||
sub_list = &g_sub_list[g_sub_list_index[module_id]];
|
||||
MSG_CHECK_SUBSCIBE_INFO(sub_list->module_id, -1);
|
||||
|
||||
msg_mutex_lock(&sub_list->msg_mutex);
|
||||
if (sub_list->sub_module_cnt < (MODULE_MAX - 1)) {
|
||||
@@ -559,14 +601,14 @@ int has_msg_subscribe(has_module_ID_e module_id, has_module_ID_e sub_id) {
|
||||
}
|
||||
}
|
||||
|
||||
int has_msg_unsubscribe(has_module_ID_e module_id, has_module_ID_e sub_id) {
|
||||
int has_msg_unsubscribe(has_module_ID_e module_id, has_module_ID_e sub_id)
|
||||
{
|
||||
has_static_sub_list_t *sub_list;
|
||||
if ((module_id >= MODULE_MAX) || (sub_id >= MODULE_MAX)
|
||||
|| (module_id == INVALID_ID) || (sub_id == INVALID_ID)) {
|
||||
return -1;
|
||||
}
|
||||
sub_list = &g_sub_list[g_sub_list_index[module_id]];
|
||||
MSG_CHECK_SUBSCIBE_INFO(sub_list->module_id, -1);
|
||||
|
||||
msg_mutex_lock(&sub_list->msg_mutex);
|
||||
for (int j = 0; j < sub_list->sub_module_cnt; j++) {
|
||||
|
||||
@@ -5,14 +5,14 @@
|
||||
#define MSG_OPT_OS_freeRTOS 2
|
||||
#define MSG_OPT_OS_RT_THREAD 3
|
||||
#define MSG_OPT_OS_NONE_OR_OSAL 4
|
||||
#define MSG_OPT_OS_USER_DEFINE 5
|
||||
#define MSG_OPT_OS_USER_DEFINED 5
|
||||
|
||||
/* TODO:rtt和freertos未测过 */
|
||||
#define MSG_OPT_OS MSG_OPT_OS_LINUX
|
||||
// #define MSG_OPT_OS MSG_OPT_OS_freeRTOS
|
||||
// #define MSG_OPT_OS MSG_OPT_OS_RT_THREAD
|
||||
// #define MSG_OPT_OS MSG_OPT_OS_NONE_OR_OSAL
|
||||
// #define MSG_OPT_OS MSG_OPT_OS_USER_DEFINE
|
||||
// #define MSG_OPT_OS MSG_OPT_OS_NONE_OR_OSAL // 裸机或osal
|
||||
// #define MSG_OPT_OS MSG_OPT_OS_USER_DEFINED // 自定义
|
||||
|
||||
#define MSG_OPT_USE_MUTEX // 支持互斥量
|
||||
|
||||
@@ -26,9 +26,9 @@
|
||||
|
||||
#ifdef MSG_OPT_USE_MUTEX
|
||||
#define msg_mutex_t pthread_mutex_t
|
||||
#define msg_mutex_init(m) pthread_mutex_init((msg_mutex_t *)m, NULL)
|
||||
#define msg_mutex_lock(m) pthread_mutex_lock((msg_mutex_t *)m)
|
||||
#define msg_mutex_unlock(m) pthread_mutex_unlock((msg_mutex_t *)m)
|
||||
#define msg_mutex_init(m) pthread_mutex_init(m, NULL)
|
||||
#define msg_mutex_lock(m) pthread_mutex_lock(m)
|
||||
#define msg_mutex_unlock(m) pthread_mutex_unlock(m)
|
||||
#else
|
||||
#define msg_mutex_t
|
||||
#define msg_mutex_init(m)
|
||||
@@ -125,7 +125,7 @@ static inline void msg_mutex_unlock(void *mutex) {
|
||||
#define msg_printf printf
|
||||
|
||||
/* ----------------------- for user define ----------------------- */
|
||||
#elif (MSG_OPT_OS == MSG_OPT_OS_USER_DEFINE)
|
||||
#elif (MSG_OPT_OS == MSG_OPT_OS_USER_DEFINED)
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user