Skip to content

Commit bcb53e0

Browse files
committed
初步增加uorb的框架
1 parent a0e1f95 commit bcb53e0

File tree

6 files changed

+468
-0
lines changed

6 files changed

+468
-0
lines changed

components/utilities/uORB/Kconfig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
menuconfig RT_USING_UORB
2+
bool "enable uorb pkgs"
3+
default n
4+
5+
if RT_USING_UORB
6+
7+
config ORB_MULTI_MAX_INSTANCES
8+
int "max number of multi topic instance"
9+
default 4
10+
11+
config ORB_MAX_SUBSCRIBES
12+
int "max number of topic subscribe"
13+
default 10
14+
endif
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from building import *
2+
3+
cwd = GetCurrentDir()
4+
src = Glob('*.c')
5+
path = [cwd]
6+
7+
8+
group = DefineGroup('Utilities', src, depend = ['RT_USING_UORB'], CPPPATH = path)
9+
10+
Return('group')

components/utilities/uORB/uORB.c

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
/*
2+
* Copyright (c) 2006-2024, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2024-09-12 latercomer the first version
9+
*/
10+
11+
#define LOG_TAG "uorb"
12+
#define LOG_LVL LOG_LVL_INFO
13+
14+
#include "uORB.h"
15+
#include <rtdbg.h>
16+
17+
static rt_list_t _node_list;
18+
19+
// round up to nearest power of two
20+
// Such as 0 => 1, 1 => 1, 2 => 2 ,3 => 4, 10 => 16, 60 => 64, 65...255 => 128
21+
// Note: When the input value > 128, the output is always 128
22+
static inline rt_uint8_t
23+
round_pow_of_two_8(rt_uint8_t n)
24+
{
25+
if (n == 0)
26+
{
27+
return 1;
28+
}
29+
30+
// Avoid is already a power of 2
31+
rt_uint8_t value = n - 1;
32+
33+
// Fill 1
34+
value |= value >> 1U;
35+
value |= value >> 2U;
36+
value |= value >> 4U;
37+
38+
// Unable to round-up, take the value of round-down
39+
if (value == RT_UINT8_MAX)
40+
{
41+
value >>= 1U;
42+
}
43+
44+
return value + 1;
45+
}
46+
47+
48+
struct rt_uorb_node *uorb_node_create(const struct orb_metadata *meta, const rt_uint8_t instance, rt_uint8_t queue_size)
49+
{
50+
RT_ASSERT(meta != RT_NULL);
51+
52+
struct rt_uorb_node *node = (struct rt_uorb_node *)rt_calloc(sizeof(struct rt_uorb_node), 1);
53+
54+
node->meta = meta;
55+
node->instance = instance;
56+
node->queue_size = queue_size;
57+
node->generation = 0;
58+
node->advertised = 0;
59+
node->subscriber_count = 0;
60+
node->data_valid = 0;
61+
node->data = RT_NULL;
62+
63+
char *name = rt_calloc(RT_NAME_MAX, 1);
64+
rt_snprintf(name, RT_NAME_MAX, "%s%d", meta->o_name, instance);
65+
66+
rt_list_insert_after(_node_list.prev, &node->list);
67+
68+
// 注册设备
69+
rt_uorb_register(node, name, 0, RT_NULL);
70+
71+
return node;
72+
}
73+
74+
rt_err_t uorb_node_delete(struct rt_uorb_node *node)
75+
{
76+
return 0;
77+
}
78+
79+
struct rt_uorb_node *uorb_node_find(const struct orb_metadata *meta, int instance)
80+
{
81+
// 遍历_node_list
82+
}
83+
84+
int uorb_node_read(struct rt_uorb_node *node, void *data, int *generation)
85+
{
86+
RT_ASSERT(node != RT_NULL);
87+
RT_ASSERT(data != RT_NULL);
88+
89+
if (!node->data)
90+
{
91+
return 0;
92+
}
93+
94+
if (node->queue_size == 1)
95+
{
96+
rt_memcpy(data, node->data, node->meta->o_size);
97+
if (generation)
98+
{
99+
generation = node->generation;
100+
}
101+
}
102+
else
103+
{
104+
// TODO:
105+
}
106+
107+
return node->meta->o_size;
108+
}
109+
110+
111+
int uorb_node_write(struct rt_uorb_node *node, void *data)
112+
{
113+
RT_ASSERT(node != RT_NULL);
114+
RT_ASSERT(data != RT_NULL);
115+
116+
if (!node->data)
117+
{
118+
const size_t size = node->meta->o_size * node->queue_size;
119+
node->data = rt_calloc(size, 1);
120+
}
121+
122+
if (!node->data)
123+
{
124+
return -1;
125+
}
126+
127+
rt_memcpy(node->data, (node->meta->o_size * node->generation % node->queue_size), data, node->meta->o_size);
128+
129+
// callbacks
130+
// for ()
131+
// {
132+
133+
// }
134+
135+
node->data_valid = 1;
136+
node->generation++;
137+
138+
return node->meta->o_size;
139+
}
140+
141+
orb_subscribe_t orb_subscribe_multi(const struct orb_metadata *meta, unsigned instance)
142+
{
143+
struct rt_uorb_subscribe *sub = rt_calloc(sizeof(struct rt_uorb_subscribe), 1);
144+
145+
sub->meta = meta;
146+
sub->instance = instance;
147+
sub->generation = 0;
148+
sub->node = uorb_node_find(meta, instance);
149+
150+
return sub;
151+
}
152+
153+
int orb_check(orb_subscribe_t sub, rt_bool_t *updated)
154+
{
155+
}
156+
157+
int orb_copy(const struct orb_metadata *meta, int handle, void *buffer)
158+
{
159+
}
160+
161+
int orb_unsubscribe(int handle)
162+
{
163+
}
164+
165+
orb_advertise_t orb_advertise_multi_queue(const struct orb_metadata *meta, const void *data, int *instance,
166+
unsigned int queue_size)
167+
{
168+
}
169+
170+
171+
int orb_publish(const struct orb_metadata *meta, orb_advertise_t handle, const void *data)
172+
{
173+
}
174+
175+
int orb_unadvertise(orb_advertise_t handle)
176+
{
177+
}

components/utilities/uORB/uORB.h

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* Copyright (c) 2006-2024, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2024-09-12 latercomer the first version
9+
*/
10+
11+
12+
#ifndef _UORB_H_
13+
#define _UORB_H_
14+
15+
16+
#include <rtthread.h>
17+
18+
19+
#ifdef __cplusplus
20+
extern "C" {
21+
#endif // __cplusplus
22+
23+
24+
/**
25+
* Object metadata.
26+
*/
27+
struct orb_metadata
28+
{
29+
const char *o_name; /**< unique object name */
30+
const uint16_t o_size; /**< object size */
31+
const uint16_t o_size_no_padding; /**< object size w/o padding at the end (for logger) */
32+
const char *o_fields; /**< semicolon separated list of fields (with type) */
33+
uint8_t o_id; /**< ORB_ID enum */
34+
};
35+
36+
typedef const struct orb_metadata *orb_id_t;
37+
38+
39+
/**
40+
* Generates a pointer to the uORB metadata structure for
41+
* a given topic.
42+
*
43+
* The topic must have been declared previously in scope
44+
* with ORB_DECLARE().
45+
*
46+
* @param _name The name of the topic.
47+
*/
48+
#define ORB_ID(_name) &__orb_##_name
49+
50+
/**
51+
* Declare (prototype) the uORB metadata for a topic (used by code generators).
52+
*
53+
* @param _name The name of the topic.
54+
*/
55+
#if defined(__cplusplus)
56+
#define ORB_DECLARE(_name) extern "C" const struct orb_metadata __orb_##_name
57+
#else
58+
#define ORB_DECLARE(_name) extern const struct orb_metadata __orb_##_name
59+
#endif //__cplusplus
60+
61+
62+
/**
63+
* Define (instantiate) the uORB metadata for a topic.
64+
*
65+
* The uORB metadata is used to help ensure that updates and
66+
* copies are accessing the right data.
67+
*
68+
* Note that there must be no more than one instance of this macro
69+
* for each topic.
70+
*
71+
* @param _name The name of the topic.
72+
* @param _struct The structure the topic provides.
73+
* @param _size_no_padding Struct size w/o padding at the end
74+
* @param _fields All fields in a semicolon separated list e.g: "float[3] position;bool armed"
75+
* @param _orb_id_enum ORB ID enum e.g.: ORB_ID::vehicle_status
76+
*/
77+
#define ORB_DEFINE(_name, _struct, _size_no_padding, _fields, _orb_id_enum) \
78+
const struct orb_metadata __orb_##_name = { \
79+
#_name, \
80+
sizeof(_struct), \
81+
_size_no_padding, \
82+
_fields, \
83+
_orb_id_enum, \
84+
}; \
85+
struct hack
86+
87+
88+
/**
89+
* ORB topic advertiser handle.
90+
*
91+
* Advertiser handles are global; once obtained they can be shared freely
92+
* and do not need to be closed or released.
93+
*
94+
* This permits publication from interrupt context and other contexts where
95+
* a file-descriptor-based handle would not otherwise be in scope for the
96+
* publisher.
97+
*/
98+
typedef void *orb_advertise_t;
99+
typedef void *orb_subscribe_t;
100+
101+
102+
struct rt_uorb_node
103+
{
104+
rt_list_t list;
105+
const struct orb_metadata *meta;
106+
rt_uint8_t instance;
107+
rt_uint8_t queue_size;
108+
rt_uint32_t generation;
109+
// rt_list_t callbacks;
110+
rt_bool_t advertised;
111+
rt_uint8_t subscriber_count;
112+
rt_bool_t data_valid;
113+
rt_uint8_t *data;
114+
};
115+
116+
struct rt_uorb_subscribe
117+
{
118+
const struct orb_metadata *meta;
119+
rt_uint8_t instance;
120+
rt_tick_t update_interval;
121+
122+
struct rt_uorb_device *node;
123+
rt_uint32_t generation;
124+
rt_tick_t last_update;
125+
rt_bool_t callback_registered;
126+
};
127+
128+
129+
#ifdef __cplusplus
130+
}
131+
#endif // __cplusplus
132+
133+
134+
#endif // _UORB_H_

0 commit comments

Comments
 (0)