# 使用方法 ## 创建om_config.h配置文件 参照`config/om_config_template.h`和[配置文件介绍](config.md),编写`om_config.h`。 ## 添加源文件和头文件到工程 ### CMake 直接将src文件夹作为子目录加入 add_subdirectory(user_dir/one-message/src om.out) 将om_config.h配置文件所在目录加入到OneMessage目标 target_include_directories(OneMessage PUBLIC config_file_dir) ### Makefile 将src下的所有目录和源文件,以及om_config.h配置文件所在目录加入到编译目标当中。 ## 引用头文件 `include "om.h"` ## 初始化 om_status_t om_init(); ## 创建话题/订阅者/链接 动态分配内存 om_topic_t* om_create_topic(const char* name,size_t buff_len) om_suber_t* om_create_suber(om_topic_t* link) om_status_t om_topic_link(om_topic_t* source, om_topic_t* target); 静态内存 om_topic_t* om_create_topic_static(om_topic_t* topic, const char* name,size_t buff_len) om_suber_t* om_create_suber_static(om_suber_t* suber, om_topic_t* link) om_status_t om_topic_link_static(om_suber_t* suber, om_link_t* link, om_topic_t* source, om_topic_t* target); ## 发布消息 om_status_t om_publish(om_topic_t* topic, void* buff, uint32_t size, bool block, bool in_isr) block参数决定同时有其他线程发布这个话题时是否等待,in_isr取决于是否在中断中调用 ## 异步订阅话题 om_suber_t* om_subscribe(om_topic_t* topic); om_suber_t* om_subscribe_static(om_topic_t* topic, om_suber_t* sub); om_status_t om_suber_export(om_suber_t* suber, void* buff, bool in_isr); bool om_suber_available(om_suber_t* suber); * om_subscribe会返回一个可导出话题数据的订阅者 * 当订阅者接收到新数据时,调用om_suber_export会将数据写入buff,并返回OM_OK. * om_suber_available判断是否有新数据 ## 为话题添加订阅队列 与普通订阅者不同,使用队列可以存储一个topic上的多个消息,但是无法使用回调函数。 动态分配内存 om_fifo_t* om_queue_add(om_topic_t* topic, uint32_t len); example: /* 创建topic */ om_topic_t* topic = om_create_topic("topic", 1u); /* 添加队列 */ om_fifo_t* queue = om_queue_add(topic, 10); /* 发布消息 */ //om_publish(...) /* 加锁 */ OM_TOPIC_LOCK(topic); /* 获取消息数量 */ uint32_t count = om_fifo_readable_item_count(queue); /* 弹出数据 */ for(int i=0;i10kb)时,解析性能可能较低。 2. 为了提高带宽利用率和打包速度,使用crc32作为topic的id,存在重复的可能性,出现此情况时请给topic更名。后续考虑使用分布更均匀的生成算法。