mirror of
https://gitee.com/dlmu-cone/bf_original_balance_chassis
synced 2026-07-24 03:27:45 +08:00
完成消息中心模块,但是不提供队列功能
This commit is contained in:
55
modules/message_center/message_center.c
Normal file
55
modules/message_center/message_center.c
Normal file
@@ -0,0 +1,55 @@
|
||||
#include "message_center.h"
|
||||
#include "string.h"
|
||||
|
||||
/* 消息初始化用 */
|
||||
static char pname[MAX_EVENT_COUNT][MAX_EVENT_NAME_LEN+1];
|
||||
static char sname[MAX_EVENT_COUNT][MAX_EVENT_NAME_LEN+1];
|
||||
static void* p_ptr[MAX_EVENT_COUNT]={NULL};
|
||||
static void** s_pptr[MAX_EVENT_COUNT]={NULL}; // 因为要修改指针,所以需要二重指针
|
||||
|
||||
void MessageInit()
|
||||
{
|
||||
// pub必须唯一,即消息名称不能重复,不得有多个pub发布相同消息名称
|
||||
// 对每一个subscriber,寻找相同消息名称的publisher,可能有多个sub从相同pub获取消息
|
||||
for (size_t i = 0; i < MAX_EVENT_COUNT; i++)
|
||||
{
|
||||
if(s_pptr[i]!=NULL)
|
||||
for (size_t j = 0; j < MAX_EVENT_COUNT; j++) //遍历publisher
|
||||
{
|
||||
if(p_ptr[j]!=NULL) //不为空
|
||||
if(strcmp(&sname[i],pname[j])==0) //比较消息名是否一致
|
||||
{volatile size_t ss=strlen(sname[i]);
|
||||
volatile size_t sss=strlen(pname[j]);
|
||||
*s_pptr[i]=p_ptr[j]; // 将sub的指针指向pub的数据
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
else //到结尾,退出
|
||||
while(1); //如果你卡在这里,说明没有找到消息发布者!请确认消息名称是否键入错误
|
||||
}
|
||||
else //说明已经遍历完所有的subs
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* 传入数据地址 */
|
||||
void PublisherRegister(char* name,void* data)
|
||||
{
|
||||
static uint8_t idx;
|
||||
for (size_t i = 0; i < idx; i++)
|
||||
{
|
||||
if(strcmp(pname[i],name)==0)
|
||||
while(1); //运行至此说明pub的消息发布名称冲突
|
||||
}
|
||||
strcpy(pname[idx],name);
|
||||
p_ptr[idx++]=data;
|
||||
}
|
||||
|
||||
/* 注意传入的是指针的地址,传参时使用&对数据指针取地址 */
|
||||
void SubscribeEvent(char* name,void** data_ptr)
|
||||
{
|
||||
static uint8_t idx;
|
||||
strcpy(sname[idx],name);
|
||||
s_pptr[idx++]=data_ptr;
|
||||
}
|
||||
51
modules/message_center/message_center.h
Normal file
51
modules/message_center/message_center.h
Normal file
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
* @file message_center.h
|
||||
* @author NeoZeng neozng1@hnu.edu.cn
|
||||
* @brief 这是一个伪pubsub机制,仅对应用之间的通信进行了隔离
|
||||
* @todo 实现基于链表-队列的pubsub机制
|
||||
* @version 0.1
|
||||
* @date 2022-11-30
|
||||
*
|
||||
* @copyright Copyright (c) 2022
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef PUBSUB_H
|
||||
#define PUBSUB_H
|
||||
|
||||
#include "stdlib.h"
|
||||
#include "stdint-gcc.h"
|
||||
|
||||
|
||||
#define MAX_EVENT_NAME_LEN 32 //最大的事件名长度,每个事件都有字符串来命名
|
||||
#define MAX_EVENT_COUNT 12 //最多支持的事件数量
|
||||
|
||||
|
||||
/**
|
||||
* @brief 在所有应用初始化结束之后调用,当作app的"回调函数"
|
||||
*
|
||||
*/
|
||||
void MessageInit();
|
||||
|
||||
/**
|
||||
* @brief 注册成为消息发布者
|
||||
*
|
||||
* @param name 消息标识名,注意不要超过MAX_EVENT_NAME_LEN
|
||||
* @param data 发布者的数据地址
|
||||
*/
|
||||
void PublisherRegister(char* name,void* data);
|
||||
|
||||
/**
|
||||
* @brief 订阅消息,成为消息订阅者
|
||||
*
|
||||
* @param name 消息标识名
|
||||
* @param data 保存数据的指针的地址,注意这是一个二级指针,传入的时候对数据指针取地址(&)
|
||||
*/
|
||||
void SubscribeEvent(char* name,void** data);
|
||||
|
||||
#endif // !PUBSUB_H
|
||||
|
||||
|
||||
|
||||
|
||||
/* 以下是队列版的pubsub,TODO */
|
||||
0
modules/message_center/message_center.md
Normal file
0
modules/message_center/message_center.md
Normal file
Reference in New Issue
Block a user