<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[【FAQ】全志XR806芯片 如何创建自定义状态回调函数？]]></title><description><![CDATA[<p dir="auto"><strong>问题背景</strong><br />
XR_MCU的SDK中，audio，wlan，Fs等模块被引用时，已经根据状态，设置好了回调函数，但是有客户不清楚如何利用SDK自身资源，创建自己应用模块的状态回调函数。</p>
<p dir="auto"><strong>问题描述</strong><br />
如何创建自定义状态回调函数。</p>
<p dir="auto"><strong>问题分析</strong><br />
状态回调函数依赖于SDK的framework框架，且已经为用户预留了自定义回调函数的框架，audio，bt，fs，net的状态回调框架都是完全公开的，参照编写即可。</p>
<p dir="auto"><strong>解决方法</strong></p>
<p dir="auto"><strong>1 添加自定义状态回调类型</strong><br />
在project\common\framework\sys_ctrl\sys_ctrl.h文件中，修改ctrl_msg_type结构体，在CTRL_MSG_TYPE_USER后添加自定义类型，如CTRL_MSG_TYPE_TEST。</p>
<pre><code>typedef enum ctrl_msg_type{
	CTRL_MSG_TYPE_SYSTEM = 0,
	CTRL_MSG_TYPE_NETWORK,
	CTRL_MSG_TYPE_VKEY,
	CTRL_MSG_TYPE_VOLUME,
	CTRL_MSG_TYPE_SDCARD,
	CTRL_MSG_TYPE_FS,
	CTRL_MSG_TYPE_AUDIO,
	CTRL_MSG_TYPE_HANDLER,

	/* message defined by user starts from CTRL_MSG_TYPE_USER */
	CTRL_MSG_TYPE_USER = 0x100,
        CTRL_MSG_TYPE_TEST, //自主添加的回调函数
} ctrl_msg_type;
</code></pre>
<p dir="auto"><strong>2 创建回调函数</strong><br />
在.h文件中添加状态类型</p>
<pre><code>enum test_status {
	TEST_MSG_STATE_FIRSR,        //状态1
	TEST_MSG_STATE_SECOND,        //状态2
	TEST_MSG_STATE_UNDEFINE,
};
</code></pre>
<p dir="auto">在.c文件中添加实体函数</p>
<pre><code>/* 处理状态变化 */
static void test_ctrl_msg_process(uint32_t event, uint32_t data, void *arg)
{
	switch (EVENT_SUBTYPE(event)) {
	case TEST_MSG_STATE_FIRSR:
		test_act_first(data);
		break;
	case TEST_MSG_STATE_SECOND:
		test_act_second(data);
		break;
	default:
		break;
	}
}
</code></pre>
<pre><code>/* 创建回调函数 */
int test_init(void)
{
	observer_base *base = sys_callback_observer_create(CTRL_MSG_TYPE_TEST,    //监控event类型
	                                                   0xFFFF,                //监控类型可分类，可参考fs_ctrl.h    
	                                                   test_ctrl_msg_process, //回调函数
	                                                   NULL);
	if (base == NULL) {
		printf("create fail\n");
		return -1;
	}
        /* 挂载到观察链表 */
	if (sys_ctrl_attach(base) != 0) {
		printf("attach fail\n");
		return -1;
	}
}
</code></pre>
<p dir="auto"><strong>3 发送状态</strong><br />
当线程中状态发生变化时，通过project\common\framework\sys_ctrl\sys_ctrl.h中定义的API，会调用步骤2中设置好的回调函数。<br />
常规用法</p>
<pre><code>/* 实体 */
int sys_event_send(uint16_t type, uint16_t subtype, uint32_t data, uint32_t wait_ms);
/* 例,其中wait_ms为队列等待超时时间，队列深度默认为6 */
sys_event_send(CTRL_MSG_TYPE_TEST,TEST_MSG_STATE_FIRSR,0,0);
</code></pre>
<p dir="auto">特殊用法，发送完成后自动释放数据</p>
<pre><code>/* 实体 */
int sys_event_send_with_free(uint16_t type, uint16_t subtype, void *data, uint32_t wait_ms);
/* 例，其中data是希望传输到回调函数的数据，使用该API能在传输完成后释放内存 */
struct STtest *testdata = malloc(sizeof(*testdata))；
memset(testdata,0,sizeof(*testdata);
testdata = dataget(testdata) //数据赋值
sys_event_send_with_free（CTRL_MSG_TYPE_TEST,TEST_MSG_STATE_FIRSR,testdata ,0）;//发送完成后会自动释放数据
</code></pre>
<p dir="auto">特殊用法，自定义销毁方式</p>
<pre><code>/* 实体 */
int sys_event_send_with_destruct(uint16_t type, uint16_t subtype, void *data, void (*destruct)(event_msg *), uint32_t wait_ms)
</code></pre>
<p dir="auto">该API和sys_event_send_with_free差异点为，sys_event_send_with_free执行完回调后，会固定执行free(data),但是sys_event_send_with_destruct在执行完回调后，继续执行destruct函数，destruct函数由用户自主编写，可以选择销毁data，也可以选择特殊处理。</p>
]]></description><link>https://bbs.aw-ol.com/topic/744/faq-全志xr806芯片-如何创建自定义状态回调函数</link><generator>RSS for Node</generator><lastBuildDate>Thu, 05 Mar 2026 11:00:22 GMT</lastBuildDate><atom:link href="https://bbs.aw-ol.com/topic/744.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 08 Dec 2021 01:55:26 GMT</pubDate><ttl>60</ttl></channel></rss>