1.修复删除所有消息接口的bug
2.完善初始化时的错误处理 3.添加freeRTOS的支持 4.添加rt-thread的支持 5.消除一些编译警告
This commit is contained in:
@@ -26,7 +26,8 @@
|
||||
#define msg_debug(fmt, ...)
|
||||
#endif
|
||||
|
||||
#define MSG_ASSERT {while(1);}
|
||||
#define MSG_ASSERT \
|
||||
{msg_printf("!!!!!wrong config!!!!! file:%s line:%d\n", __FILE__, __LINE__);while(1);}
|
||||
|
||||
#pragma pack(1)
|
||||
/* 消息数据 */
|
||||
@@ -71,7 +72,7 @@ typedef struct {
|
||||
struct list_head msg_list; // 消息链表
|
||||
} has_static_sub_list_t;
|
||||
static has_static_sub_list_t g_sub_list[] = SUBSCIBE_INFO;
|
||||
static unsigned char g_sub_list_index[MODULE_MAX]; // 订阅表id索引
|
||||
static unsigned char g_sub_list_index[MODULE_MAX]; // 订阅表id索引,同时表示模块是否可用
|
||||
|
||||
|
||||
/* 检查ID是否合法,id:检查的ID,ret:不合法时return的值 */
|
||||
@@ -87,17 +88,19 @@ if (g_sub_list_index[(id)] == MODULE_MAX) { \
|
||||
return (ret);}
|
||||
|
||||
/**
|
||||
* @brief 初始化消息表
|
||||
* @brief 初始化消息表,必须在所有接口调用之前调用init
|
||||
*
|
||||
* @return 0:成功 -1:失败
|
||||
*/
|
||||
int has_msg_init()
|
||||
{
|
||||
int j;
|
||||
has_static_pub_list_t *pub_list;
|
||||
/* 设置无效,标记是否初始化 */
|
||||
memset(g_sub_list_index, MODULE_MAX, sizeof(g_sub_list_index));
|
||||
memset(g_pub_list, 0, sizeof(g_pub_list));
|
||||
for (int i = 0; i < (sizeof(g_sub_list) / sizeof(g_sub_list[0])); i++) {
|
||||
for (unsigned int i = 0; i < (sizeof(g_sub_list) / sizeof(g_sub_list[0])); i++) {
|
||||
#ifdef CHECK_SUBSCIBE_INFO
|
||||
/* 检查各模块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"
|
||||
@@ -107,8 +110,8 @@ int has_msg_init()
|
||||
}
|
||||
/* 已经被初始化了,配置错误 */
|
||||
if (g_sub_list_index[g_sub_list[i].module_id] != MODULE_MAX) {
|
||||
msg_printf("[msg]warning: msg two same module id:%d\n", g_sub_list[i].module_id);
|
||||
continue;
|
||||
msg_printf("[msg warning]: msg two same module id:%d\n", g_sub_list[i].module_id);
|
||||
MSG_ASSERT; // 配置都能写错,必须进断言
|
||||
}
|
||||
|
||||
/* 检查订阅表ID是否配置正确 */
|
||||
@@ -131,24 +134,38 @@ int has_msg_init()
|
||||
MSG_ASSERT; // 配置都能写错,必须进断言
|
||||
return -1;
|
||||
}
|
||||
g_sub_list_index[g_sub_list[i].module_id] = i; // 索引表赋值
|
||||
#endif
|
||||
|
||||
/* 通知发布者,需要发给此模块 */
|
||||
for (int k = 0; k < g_sub_list[i].sub_module_cnt; k++) { // 寻找发布者
|
||||
if (g_pub_list[g_sub_list[i].sub_module[k]].pub_num == 0) {
|
||||
msg_mutex_init(&g_pub_list[g_sub_list[i].sub_module[k]].buffer_mutex);
|
||||
pub_list = &g_pub_list[g_sub_list[i].sub_module[k]];
|
||||
#ifdef MSG_OPT_MUTEX_SEM
|
||||
if (pub_list->pub_num == 0) {
|
||||
if (msg_mutex_init(&pub_list->buffer_mutex) != 0) {
|
||||
msg_printf("[msg warning]:pub mutex init err! pub id:%d\n"
|
||||
, g_sub_list[i].sub_module[k]);
|
||||
/* 使用计数没有加锁,释放msg的内存时有可能出错 */
|
||||
continue;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/* 注册所找到发布者的发布表 */
|
||||
g_pub_list[g_sub_list[i].sub_module[k]].
|
||||
pub_module[g_pub_list[g_sub_list[i].sub_module[k]].pub_num]
|
||||
= g_sub_list[i].module_id;
|
||||
g_pub_list[g_sub_list[i].sub_module[k]].pub_num ++;
|
||||
pub_list->pub_module[pub_list->pub_num] = g_sub_list[i].module_id;
|
||||
pub_list->pub_num ++;
|
||||
}
|
||||
msg_mutex_init(&g_sub_list[i].msg_mutex); // 初始化该模块互斥量
|
||||
INIT_LIST_HEAD(&g_sub_list[i].msg_list); // 初始化订阅链表
|
||||
#ifdef MSG_OPT_MUTEX_SEM
|
||||
msg_sem_init(&g_sub_list[i].sem); // 初始化同步量
|
||||
if (msg_sem_init(&g_sub_list[i].sem) != 0) { // 初始化该模块同步量
|
||||
msg_printf("[msg warning]:sub sem init err! sub id:%d\n", i);
|
||||
continue;
|
||||
}
|
||||
if (msg_mutex_init(&g_sub_list[i].msg_mutex) != 0) { // 初始化该模块互斥量
|
||||
msg_printf("[msg warning]:sub mutex init err! sub id:%d\n", i);
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
g_sub_list_index[g_sub_list[i].module_id] = i; // 索引表赋值,相当于enable
|
||||
}
|
||||
#ifdef MSG_OPT_DEBUG
|
||||
// msg_printf("show index:\n");
|
||||
@@ -184,23 +201,26 @@ int has_msg_init()
|
||||
*/
|
||||
int has_msg_init_module(has_module_ID_e module_id)
|
||||
{
|
||||
int i, j;
|
||||
unsigned int i;
|
||||
int j;
|
||||
has_static_pub_list_t *pub_list;
|
||||
/* 检查ID */
|
||||
#ifdef CHECK_SUBSCIBE_INFO
|
||||
if ((module_id >= MODULE_MAX) || (module_id == INVALID_ID)) {
|
||||
msg_printf("%s:module_id:%d is invalid\n", __func__, module_id);
|
||||
MSG_ASSERT; // 配置都能写错,必须进断言
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
/* 初始化该模块订阅表 */
|
||||
for (i = 0; i < (sizeof(g_sub_list) / sizeof(g_sub_list[0])); i++)
|
||||
{
|
||||
if (g_sub_list[i].module_id == (unsigned char)module_id) {
|
||||
// g_sub_list_index[module_id] = MODULE_MAX;
|
||||
// g_pub_list[module_id].pub_num = 0;
|
||||
#ifdef CHECK_SUBSCIBE_INFO
|
||||
/* 已经被初始化了,配置错误 */
|
||||
if (g_sub_list_index[module_id] != MODULE_MAX) {
|
||||
msg_printf("[msg]warning: msg two same module id:%d\n", module_id);
|
||||
break;
|
||||
msg_printf("[msg warning]: msg two same module id:%d\n", module_id);
|
||||
MSG_ASSERT; // 配置都能写错,必须进断言
|
||||
}
|
||||
/* 检查订阅表ID是否配置正确 */
|
||||
for (j = 0; j < (MODULE_MAX - 1); j++) {
|
||||
@@ -223,24 +243,38 @@ int has_msg_init_module(has_module_ID_e module_id)
|
||||
MSG_ASSERT; // 配置都能写错,必须进断言
|
||||
return -1;
|
||||
}
|
||||
g_sub_list_index[module_id] = i; // 索引表赋值
|
||||
#endif
|
||||
|
||||
/* 通知发布者,需要发给此模块 */
|
||||
for (int k = 0; k < g_sub_list[i].sub_module_cnt; k++) { // 寻找发布者
|
||||
if (g_pub_list[g_sub_list[i].sub_module[k]].pub_num == 0) {
|
||||
msg_mutex_init(&g_pub_list[g_sub_list[i].sub_module[k]].buffer_mutex);
|
||||
pub_list = &g_pub_list[g_sub_list[i].sub_module[k]];
|
||||
#ifdef MSG_OPT_MUTEX_SEM
|
||||
if (pub_list->pub_num == 0) {
|
||||
if (msg_mutex_init(&pub_list->buffer_mutex) != 0) {
|
||||
msg_printf("[msg warning]:pub mutex init err! pub id:%d\n"
|
||||
, g_sub_list[i].sub_module[k]);
|
||||
/* 使用计数没有加锁,释放msg的内存时有可能出错 */
|
||||
continue;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/* 注册所找到发布者的发布表 */
|
||||
g_pub_list[g_sub_list[i].sub_module[k]].
|
||||
pub_module[g_pub_list[g_sub_list[i].sub_module[k]].pub_num] = module_id;
|
||||
g_pub_list[g_sub_list[i].sub_module[k]].pub_num ++;
|
||||
pub_list->pub_module[pub_list->pub_num] = module_id;
|
||||
pub_list->pub_num ++;
|
||||
}
|
||||
msg_mutex_init(&g_sub_list[i].msg_mutex); // 初始化该模块互斥量
|
||||
INIT_LIST_HEAD(&g_sub_list[i].msg_list); // 初始化订阅链表
|
||||
#ifdef MSG_OPT_MUTEX_SEM
|
||||
msg_sem_init(&g_sub_list[i].sem); // 初始化同步量
|
||||
if (msg_sem_init(&g_sub_list[i].sem) != 0) { // 初始化该模块同步量
|
||||
msg_printf("[msg warning]:sub sem init err! sub id:%d\n", i);
|
||||
break;
|
||||
}
|
||||
if (msg_mutex_init(&g_sub_list[i].msg_mutex) != 0) { // 初始化该模块互斥量
|
||||
msg_printf("[msg warning]:sub mutex init err! sub id:%d\n", i);
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
g_sub_list_index[module_id] = i; // 索引表赋值,相当于enable
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -264,6 +298,7 @@ int has_msg_os_init()
|
||||
{
|
||||
has_msg_init_module(i);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* TODO:更合理的方式是每个消息一个互斥量,但是可能会导致每个消息malloc和free的时候频繁init和deinit */
|
||||
@@ -309,9 +344,9 @@ int has_msg_publish(has_module_ID_e module_id, void *buffer, unsigned int length
|
||||
has_static_pub_list_t *pub_list;
|
||||
has_static_sub_list_t *sub_list;
|
||||
|
||||
MSG_CHECK_MODULE_ID(module_id, -1);
|
||||
MSG_CHECK_ID_AND_INDEX(module_id, -1);
|
||||
// MSG_CHECK_MODULE_ID 如果使用这个宏检查,未初始化的模块也可以发布
|
||||
pub_list = &g_pub_list[module_id];
|
||||
// MSG_CHECK_ID_AND_INDEX TODO:如果使用这个,未初始化的模块也不能发布
|
||||
if (pub_list->pub_num == 0) {
|
||||
// msg_printf("no one subscribe you:%d\n", module_id);
|
||||
return 0;
|
||||
@@ -342,7 +377,7 @@ int has_msg_publish(has_module_ID_e module_id, void *buffer, unsigned int length
|
||||
list_del(&node[i].list); // 添加失败,删除链表
|
||||
msg_mutex_unlock(&sub_list->msg_mutex);
|
||||
msg_check_and_free_message(&node[i]);
|
||||
msg_printf("warning:Failed to add msg on module:%d !!!\n"
|
||||
msg_printf("[msg warning]:Failed to add msg on module:%d !!!\n"
|
||||
, sub_list->module_id);
|
||||
continue;
|
||||
}
|
||||
@@ -660,7 +695,7 @@ int has_msg_delete_all_message(has_module_ID_e module_id)
|
||||
list_add_tail(&node->list, &free_list); // 加入临时链表
|
||||
}
|
||||
}
|
||||
INIT_LIST_HEAD(&sub_list->msg_list);
|
||||
// INIT_LIST_HEAD(&sub_list->msg_list);
|
||||
msg_mutex_unlock(&sub_list->msg_mutex);
|
||||
/* 释放内存 */
|
||||
list_for_each_entry_safe(node, temp, &free_list, list) {
|
||||
|
||||
Reference in New Issue
Block a user