Skip to content

Commit 25c5d63

Browse files
chenzihan0416Otpvondoiats
authored andcommitted
system/uorb: Return negated errno from uORB API on failure.
The uORB API returned -1 and set errno on failure, so the error code was only available through errno and had to be read right after the call. Make the uORB API return a negated errno value on failure and update the function descriptions in uORB.h accordingly. orb_advsub_open() now returns the errno saved after SNIOC_REGISTER instead of the errno left by the following close(). BREAKING CHANGE: uORB functions (orb_open, orb_close, orb_advertise_*, orb_subscribe_*, orb_publish_*, orb_copy_*, orb_ioctl, orb_flush, orb_get_state, orb_get_events, orb_check, orb_exists, orb_set_interval, orb_set_batch_interval, orb_get_info, ...) now return a negated errno value on failure instead of -1 with errno set. Callers that only test for a negative return value keep working. Callers that read errno after a failed call must use the returned value instead, e.g. replace "if (orb_copy(...) < 0) printf("%d", errno);" with "ret = orb_copy(...); if (ret < 0) printf("%d", -ret);". Signed-off-by: chenzihan1 <chenzihan1@xiaomi.com> Signed-off-by: likun17 <likun17@xiaomi.com>
1 parent ec386cb commit 25c5d63

2 files changed

Lines changed: 99 additions & 63 deletions

File tree

‎system/uorb/uORB/uORB.c‎

Lines changed: 77 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,21 @@
3535
#include <nuttx/streams.h>
3636
#include <uORB/uORB.h>
3737

38+
/****************************************************************************
39+
* Pre-processor Definitions
40+
****************************************************************************/
41+
42+
#define ORB_IOCTL(fd, cmd, arg) \
43+
do \
44+
{ \
45+
if (ioctl(fd, cmd, arg) < 0) \
46+
{ \
47+
return -errno; \
48+
} \
49+
\
50+
} \
51+
while (0)
52+
3853
/****************************************************************************
3954
* Private Functions
4055
****************************************************************************/
@@ -54,7 +69,7 @@
5469
* non_wakeup The non wakeup flag.
5570
*
5671
* Returned Value:
57-
* fd on success, otherwise returns negative value and set errno.
72+
* fd on success, otherwise returns a negated errno value on failure.
5873
****************************************************************************/
5974

6075
static int orb_advsub_open(FAR const struct orb_metadata *meta, int flags,
@@ -76,6 +91,7 @@ static int orb_advsub_open(FAR const struct orb_metadata *meta, int flags,
7691
if (fd < 0)
7792
{
7893
struct sensor_reginfo_s reginfo;
94+
7995
strlcpy(reginfo.path, path, NAME_MAX);
8096
reginfo.esize = meta->o_size;
8197
reginfo.nbuffer = queue_size;
@@ -92,7 +108,7 @@ static int orb_advsub_open(FAR const struct orb_metadata *meta, int flags,
92108
fd = open(ORB_USENSOR_PATH, O_WRONLY | O_CLOEXEC);
93109
if (fd < 0)
94110
{
95-
return fd;
111+
return -errno;
96112
}
97113

98114
/* Register new device node */
@@ -102,13 +118,13 @@ static int orb_advsub_open(FAR const struct orb_metadata *meta, int flags,
102118
close(fd);
103119
if (ret < 0 && err != EEXIST)
104120
{
105-
return ret;
121+
return -err;
106122
}
107123

108124
fd = open(path, flags);
109125
if (fd < 0)
110126
{
111-
return fd;
127+
return -errno;
112128
}
113129

114130
if (err != EEXIST)
@@ -177,15 +193,30 @@ orb_advertise_multi_queue_flags(FAR const struct orb_metadata *meta,
177193

178194
int orb_open(FAR const char *name, int instance, int flags)
179195
{
196+
int ret;
180197
char path[ORB_PATH_MAX];
181198

182199
snprintf(path, ORB_PATH_MAX, ORB_SENSOR_PATH"%s%d", name, instance);
183-
return open(path, O_CLOEXEC | flags);
200+
ret = open(path, O_CLOEXEC | flags);
201+
if (ret < 0)
202+
{
203+
return -errno;
204+
}
205+
206+
return ret;
184207
}
185208

186209
int orb_close(int fd)
187210
{
188-
return close(fd);
211+
int ret;
212+
213+
ret = close(fd);
214+
if (ret < 0)
215+
{
216+
return -errno;
217+
}
218+
219+
return ret;
189220
}
190221

191222
int orb_advertise_multi_queue_info(FAR const struct orb_metadata *meta,
@@ -210,7 +241,15 @@ orb_advertise_multi_queue_persist_info(FAR const struct orb_metadata *meta,
210241

211242
ssize_t orb_publish_multi(int fd, FAR const void *data, size_t len)
212243
{
213-
return write(fd, data, len);
244+
ssize_t ret;
245+
246+
ret = write(fd, data, len);
247+
if (ret < 0)
248+
{
249+
return -errno;
250+
}
251+
252+
return ret;
214253
}
215254

216255
int orb_subscribe_multi(FAR const struct orb_metadata *meta,
@@ -227,33 +266,34 @@ int orb_subscribe_multi_nonwakeup(FAR const struct orb_metadata *meta,
227266

228267
ssize_t orb_copy_multi(int fd, FAR void *buffer, size_t len)
229268
{
230-
return read(fd, buffer, len);
269+
ssize_t ret;
270+
271+
ret = read(fd, buffer, len);
272+
if (ret < 0)
273+
{
274+
return -errno;
275+
}
276+
277+
return ret;
231278
}
232279

233280
int orb_get_state(int fd, FAR struct orb_state *state)
234281
{
235282
struct sensor_state_s tmp;
236-
int ret;
237283

238284
if (!state)
239285
{
240286
return -EINVAL;
241287
}
242288

243-
ret = ioctl(fd, SNIOC_GET_STATE, (unsigned long)(uintptr_t)&tmp);
244-
if (ret < 0)
245-
{
246-
return ret;
247-
}
248-
289+
ORB_IOCTL(fd, SNIOC_GET_STATE, (unsigned long)(uintptr_t)&tmp);
249290
state->max_frequency = tmp.min_interval ?
250291
1000000 / tmp.min_interval : 0;
251292
state->min_batch_interval = tmp.min_latency;
252293
state->queue_size = tmp.nbuffer;
253294
state->nsubscribers = tmp.nsubscribers;
254295
state->generation = tmp.generation;
255-
256-
return ret;
296+
return 0;
257297
}
258298

259299
int orb_get_events(int fd, FAR unsigned int *events)
@@ -263,67 +303,62 @@ int orb_get_events(int fd, FAR unsigned int *events)
263303
return -EINVAL;
264304
}
265305

266-
return ioctl(fd, SNIOC_GET_EVENTS, (unsigned long)(uintptr_t)events);
306+
ORB_IOCTL(fd, SNIOC_GET_EVENTS, (unsigned long)(uintptr_t)events);
307+
return 0;
267308
}
268309

269310
int orb_check(int fd, FAR bool *updated)
270311
{
271-
return ioctl(fd, SNIOC_UPDATED, (unsigned long)(uintptr_t)updated);
312+
ORB_IOCTL(fd, SNIOC_UPDATED, (unsigned long)(uintptr_t)updated);
313+
return 0;
272314
}
273315

274316
int orb_ioctl(int fd, int cmd, unsigned long arg)
275317
{
276-
return ioctl(fd, cmd, arg);
318+
ORB_IOCTL(fd, cmd, arg);
319+
return 0;
277320
}
278321

279322
int orb_flush(int fd)
280323
{
281-
return ioctl(fd, SNIOC_FLUSH, 0);
324+
ORB_IOCTL(fd, SNIOC_FLUSH, 0);
325+
return 0;
282326
}
283327

284328
int orb_set_interval(int fd, unsigned interval)
285329
{
286-
return ioctl(fd, SNIOC_SET_INTERVAL, (unsigned long)interval);
330+
ORB_IOCTL(fd, SNIOC_SET_INTERVAL, (unsigned long)interval);
331+
return 0;
287332
}
288333

289334
int orb_get_interval(int fd, FAR unsigned *interval)
290335
{
291336
struct sensor_ustate_s tmp;
292-
int ret;
293-
294-
ret = ioctl(fd, SNIOC_GET_USTATE, (unsigned long)(uintptr_t)&tmp);
295-
if (ret < 0)
296-
{
297-
return ret;
298-
}
299337

338+
ORB_IOCTL(fd, SNIOC_GET_USTATE, (unsigned long)(uintptr_t)&tmp);
300339
*interval = tmp.interval;
301-
return ret;
340+
return 0;
302341
}
303342

304343
int orb_get_info(int fd, FAR orb_info_t *info)
305344
{
306-
return ioctl(fd, SNIOC_GET_INFO, (unsigned long)(uintptr_t)info);
345+
ORB_IOCTL(fd, SNIOC_GET_INFO, (unsigned long)(uintptr_t)info);
346+
return 0;
307347
}
308348

309349
int orb_set_batch_interval(int fd, unsigned batch_interval)
310350
{
311-
return ioctl(fd, SNIOC_BATCH, (unsigned long)batch_interval);
351+
ORB_IOCTL(fd, SNIOC_BATCH, (unsigned long)batch_interval);
352+
return 0;
312353
}
313354

314355
int orb_get_batch_interval(int fd, FAR unsigned *batch_interval)
315356
{
316357
struct sensor_ustate_s tmp;
317-
int ret;
318-
319-
ret = ioctl(fd, SNIOC_GET_USTATE, (unsigned long)(uintptr_t)&tmp);
320-
if (ret < 0)
321-
{
322-
return ret;
323-
}
324358

359+
ORB_IOCTL(fd, SNIOC_GET_USTATE, (unsigned long)(uintptr_t)&tmp);
325360
*batch_interval = tmp.latency;
326-
return ret;
361+
return 0;
327362
}
328363

329364
orb_abstime orb_absolute_time(void)
@@ -347,14 +382,14 @@ int orb_exists(FAR const struct orb_metadata *meta, int instance)
347382
fd = open(path, 0);
348383
if (fd < 0)
349384
{
350-
return -1;
385+
return -errno;
351386
}
352387

353388
ret = ioctl(fd, SNIOC_GET_STATE, (unsigned long)(uintptr_t)&state);
354389
close(fd);
355390
if (ret < 0)
356391
{
357-
return -1;
392+
return -errno;
358393
}
359394

360395
return state.nadvertisers > 0 ? 0 : -1;

0 commit comments

Comments
 (0)