Skip to content

Commit 574e137

Browse files
committed
完善了部分uorb函数
1 parent bcb53e0 commit 574e137

File tree

3 files changed

+254
-36
lines changed

3 files changed

+254
-36
lines changed

components/utilities/uORB/uORB.c

Lines changed: 233 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ static rt_list_t _node_list;
1919
// round up to nearest power of two
2020
// Such as 0 => 1, 1 => 1, 2 => 2 ,3 => 4, 10 => 16, 60 => 64, 65...255 => 128
2121
// 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)
22+
static inline rt_uint8_t round_pow_of_two_8(rt_uint8_t n)
2423
{
2524
if (n == 0)
2625
{
@@ -45,11 +44,11 @@ round_pow_of_two_8(rt_uint8_t n)
4544
}
4645

4746

48-
struct rt_uorb_node *uorb_node_create(const struct orb_metadata *meta, const rt_uint8_t instance, rt_uint8_t queue_size)
47+
orb_node_t *orb_node_create(const struct orb_metadata *meta, const rt_uint8_t instance, rt_uint8_t queue_size)
4948
{
5049
RT_ASSERT(meta != RT_NULL);
5150

52-
struct rt_uorb_node *node = (struct rt_uorb_node *)rt_calloc(sizeof(struct rt_uorb_node), 1);
51+
orb_node_t *node = (orb_node_t *)rt_calloc(sizeof(orb_node_t), 1);
5352

5453
node->meta = meta;
5554
node->instance = instance;
@@ -66,22 +65,58 @@ struct rt_uorb_node *uorb_node_create(const struct orb_metadata *meta, const rt_
6665
rt_list_insert_after(_node_list.prev, &node->list);
6766

6867
// 注册设备
69-
rt_uorb_register(node, name, 0, RT_NULL);
68+
// rt_uorb_register(node, name, 0, RT_NULL);
7069

7170
return node;
7271
}
7372

74-
rt_err_t uorb_node_delete(struct rt_uorb_node *node)
73+
rt_err_t orb_node_delete(orb_node_t *node)
7574
{
7675
return 0;
7776
}
7877

79-
struct rt_uorb_node *uorb_node_find(const struct orb_metadata *meta, int instance)
78+
79+
orb_node_t *orb_node_find(const struct orb_metadata *meta, int instance)
8080
{
8181
// 遍历_node_list
82+
rt_list_t *pos;
83+
rt_node_t *node;
84+
rt_list_for_each(pos, &_node_list)
85+
{
86+
node = rt_list_entry(pos, rt_node_t, list);
87+
if (node->meta == meta && node->instance == instance)
88+
{
89+
return node;
90+
}
91+
}
92+
93+
return RT_NULL;
94+
}
95+
96+
bool orb_node_exists(const struct orb_metadata *meta, int instance)
97+
{
98+
if (!meta)
99+
{
100+
return false;
101+
}
102+
103+
if (instance < 0 || instance > (ORB_MULTI_MAX_INSTANCES - 1))
104+
{
105+
return false;
106+
}
107+
108+
orb_node_t *node = orb_node_find(meta, instance);
109+
110+
if (node && node->advertised)
111+
{
112+
return true;
113+
}
114+
115+
return false;
82116
}
83117

84-
int uorb_node_read(struct rt_uorb_node *node, void *data, int *generation)
118+
119+
int orb_node_read(orb_node_t *node, void *data, int *generation)
85120
{
86121
RT_ASSERT(node != RT_NULL);
87122
RT_ASSERT(data != RT_NULL);
@@ -108,70 +143,245 @@ int uorb_node_read(struct rt_uorb_node *node, void *data, int *generation)
108143
}
109144

110145

111-
int uorb_node_write(struct rt_uorb_node *node, void *data)
146+
int orb_node_write(orb_node_t *node, void *data)
112147
{
113148
RT_ASSERT(node != RT_NULL);
114149
RT_ASSERT(data != RT_NULL);
115150

151+
// create buffer
116152
if (!node->data)
117153
{
118154
const size_t size = node->meta->o_size * node->queue_size;
119155
node->data = rt_calloc(size, 1);
120156
}
121157

158+
// buffer invalid
122159
if (!node->data)
123160
{
124-
return -1;
161+
return -RT_ERROR;
125162
}
126163

164+
// copy data to buffer
127165
rt_memcpy(node->data, (node->meta->o_size * node->generation % node->queue_size), data, node->meta->o_size);
128166

129-
// callbacks
130-
// for ()
131-
// {
167+
// invoke callbacks
168+
rt_list_t *pos;
169+
orb_callback_t *item;
170+
rt_list_for_each(pos, &node->callbacks)
171+
{
172+
item = rt_list_entry(pos, orb_callback_t, list);
173+
if (item->call)
174+
{
175+
item->call();
176+
}
177+
}
132178

133-
// }
179+
// mark data valid
180+
node->data_valid = true;
134181

135-
node->data_valid = 1;
182+
// update generation
136183
node->generation++;
137184

138185
return node->meta->o_size;
139186
}
140187

188+
bool orb_node_ready(orb_subscribe_t *handle)
189+
{
190+
if (!handle)
191+
{
192+
return false;
193+
}
194+
195+
if (handle->node)
196+
{
197+
return handle->node->advertised;
198+
}
199+
200+
201+
handle->node = orb_node_find(handle->meta, handle->instance);
202+
203+
if (handle->node)
204+
{
205+
handle->node->subscriber_count++;
206+
handle->generation = handle->node->generation - (handle->node->data_valid ? 1 : 0);
207+
208+
return handle->node->advertised;
209+
}
210+
211+
return false;
212+
}
213+
141214
orb_subscribe_t orb_subscribe_multi(const struct orb_metadata *meta, unsigned instance)
142215
{
143-
struct rt_uorb_subscribe *sub = rt_calloc(sizeof(struct rt_uorb_subscribe), 1);
216+
orb_subscribe_t *sub = rt_calloc(sizeof(orb_subscribe_t), 1);
144217

145218
sub->meta = meta;
146219
sub->instance = instance;
220+
sub->interval = 0;
147221
sub->generation = 0;
148-
sub->node = uorb_node_find(meta, instance);
222+
sub->node = orb_node_find(meta, instance);
149223

150224
return sub;
151225
}
152226

153-
int orb_check(orb_subscribe_t sub, rt_bool_t *updated)
227+
int orb_unsubscribe(orb_subscribe_t *handle)
154228
{
229+
if (!handle)
230+
{
231+
return -RT_ERROR;
232+
}
233+
234+
rt_free(handle);
235+
return RT_EOK;
155236
}
156237

157-
int orb_copy(const struct orb_metadata *meta, int handle, void *buffer)
238+
int orb_check(orb_subscribe_t *handle, rt_bool_t *updated)
158239
{
240+
if (!handle || !updated)
241+
{
242+
return -RT_ERROR;
243+
}
244+
245+
if (!orb_node_ready(handle))
246+
{
247+
return -RT_ERROR;
248+
}
249+
250+
if (hrt_elapsed_time(&handle->last_update) >= handle->interval)
251+
{
252+
updated = handle->generation != handle->node->generation;
253+
}
254+
else
255+
{
256+
updated = false;
257+
}
258+
259+
return RT_EOK;
159260
}
160261

161-
int orb_unsubscribe(int handle)
262+
int orb_copy(const struct orb_metadata *meta, orb_subscribe_t *handle, void *buffer)
162263
{
264+
if (!buffer)
265+
{
266+
return -RT_ERROR;
267+
}
268+
269+
if (!meta && !handle)
270+
{
271+
return -RT_ERROR;
272+
}
163273
}
164274

275+
165276
orb_advertise_t orb_advertise_multi_queue(const struct orb_metadata *meta, const void *data, int *instance,
166277
unsigned int queue_size)
167278
{
168-
}
279+
if (!meta)
280+
{
281+
return RT_NULL;
282+
}
283+
284+
orb_node_t *node = RT_NULL;
285+
286+
int max_inst = ORB_MULTI_MAX_INSTANCES;
287+
int inst = 0;
288+
289+
if (!instance)
290+
{
291+
node = orb_node_find(meta, 0);
292+
if (node)
293+
{
294+
max_inst = 0;
295+
}
296+
else
297+
{
298+
max_inst = 1;
299+
}
300+
}
301+
302+
for (inst = 0; inst < max_inst, inst++)
303+
{
304+
node = orb_node_find(meta, inst)
305+
{
306+
if (node)
307+
{
308+
if (node->advertised)
309+
{
310+
break;
311+
}
312+
}
313+
else
314+
{
315+
node = orb_node_create(meta, inst, queue_size);
316+
break;
317+
}
318+
}
319+
}
169320

321+
if (node)
322+
{
323+
node->advertised = true;
324+
if (data)
325+
{
326+
orb_node_write(node, data);
327+
}
328+
329+
if (instance)
330+
{
331+
*instance = inst;
332+
}
333+
}
170334

171-
int orb_publish(const struct orb_metadata *meta, orb_advertise_t handle, const void *data)
335+
return node;
336+
}
337+
338+
int orb_unadvertise(orb_node_t *node)
172339
{
340+
if (!node)
341+
{
342+
return -RT_ERROR;
343+
}
344+
345+
node->advertised = false;
346+
return RT_EOK;
173347
}
174348

175-
int orb_unadvertise(orb_advertise_t handle)
349+
350+
int orb_publish(const struct orb_metadata *meta, orb_node_t *node, const void *data)
176351
{
352+
if (!data)
353+
{
354+
return -RT_ERROR;
355+
}
356+
357+
if (!meta && !node)
358+
{
359+
return -RT_ERROR;
360+
}
361+
else if (meta && !node)
362+
{
363+
node = orb_node_find(meta, 0);
364+
if (!node)
365+
{
366+
return -RT_ERROR;
367+
}
368+
}
369+
else if (meta && node)
370+
{
371+
if (node->meta != meta)
372+
{
373+
return -RT_ERROR;
374+
}
375+
}
376+
else // (!meta && node)
377+
{
378+
}
379+
380+
if (orb_node_write(node, data) == node->meta->o_size)
381+
{
382+
return RT_EOK;
383+
}
384+
385+
return -RT_ERROR;
177386
}
387+

0 commit comments

Comments
 (0)