Skip to content

Commit 6028b42

Browse files
Otpvondoiatsxiaoxiang781216
authored andcommitted
uorb/generator:Fix generator bugs and formatting issues.
replace kmm_zalloc with zalloc and malloc, and fix format and return value errors. Signed-off-by: likun17 <[email protected]>
1 parent 9cf9b86 commit 6028b42

File tree

1 file changed

+93
-66
lines changed

1 file changed

+93
-66
lines changed

system/uorb/generator.c

Lines changed: 93 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ struct sensor_gen_info_s
4848
* Private Data
4949
****************************************************************************/
5050

51-
static bool g_should_exit = false;
51+
static bool g_gen_should_exit = false;
5252

5353
/****************************************************************************
5454
* Private Functions
@@ -77,7 +77,7 @@ x:0.1,y:9.7,z:0.81,temperature:22.15\n\n\
7777
\t\tsim - sensor_baro0:\n\
7878
\t\t uorb_generator -n 100 -r 5 -s -t sensor_baro0 timestamp:23191100,\
7979
pressure:999.12,temperature:26.34\n\n\
80-
\t\tfies - sensor_accel1\n\
80+
\t\tfiles - sensor_accel1\n\
8181
\t\t uorb_generator -f /data/uorb/20240823061723/sensor_accel0.csv\
8282
-t sensor_accel1\
8383
\t\t\n\
@@ -86,27 +86,43 @@ pressure:999.12,temperature:26.34\n\n\
8686

8787
static void exit_handler(int signo)
8888
{
89-
g_should_exit = true;
89+
(void)signo;
90+
g_gen_should_exit = true;
9091
}
9192

9293
static int get_play_orb_id(FAR const char *filter,
9394
FAR struct sensor_gen_info_s *info)
9495
{
9596
struct orb_object object;
97+
int len;
98+
int idx;
9699

97-
if (filter)
100+
if (filter == NULL)
101+
{
102+
uorbinfo_raw("The entered built-in topic name is NULL.");
103+
return ERROR;
104+
}
105+
106+
len = strlen(filter);
107+
if (len > 0)
98108
{
99109
object.meta = orb_get_meta(filter);
100-
if (object.meta == NULL)
110+
if (object.meta)
101111
{
102-
uorbinfo_raw("Input error built-in name:[%s]", filter);
103-
return ERROR;
112+
object.instance = 0;
113+
for (idx = 0; idx < len; idx++)
114+
{
115+
if (isdigit(filter[idx]))
116+
{
117+
object.instance = filter[idx] - '0';
118+
break;
119+
}
120+
}
104121
}
105-
106-
object.instance = 0;
107-
if (isdigit(filter[strlen(filter) - 1]))
122+
else
108123
{
109-
object.instance = filter[strlen(filter) - 1] - '0';
124+
uorbinfo_raw("The entered built-in topic name is invalid.");
125+
return ERROR;
110126
}
111127

112128
info->obj = object;
@@ -133,7 +149,7 @@ static int get_play_orb_id(FAR const char *filter,
133149
static int replay_worker(FAR struct sensor_gen_info_s *sensor_gen)
134150
{
135151
struct lib_meminstream_s meminstream;
136-
bool is_frist = true;
152+
bool is_first = true;
137153
uint64_t last_time;
138154
uint64_t tmp_time;
139155
FAR uint8_t *data;
@@ -143,7 +159,7 @@ static int replay_worker(FAR struct sensor_gen_info_s *sensor_gen)
143159
int ret;
144160
int fd;
145161

146-
line = kmm_zalloc(GENERATOR_CACHE_BUFF);
162+
line = zalloc(GENERATOR_CACHE_BUFF);
147163
if (line == NULL)
148164
{
149165
return -ENOMEM;
@@ -154,42 +170,46 @@ static int replay_worker(FAR struct sensor_gen_info_s *sensor_gen)
154170
if (strstr(line, sensor_gen->obj.meta->o_name) == NULL)
155171
{
156172
uorbinfo_raw("Topic and file do not match!");
157-
return -EINVAL;
173+
ret = -EINVAL;
174+
goto out_line;
158175
}
159176
}
160177
else
161178
{
162179
uorbinfo_raw("Playback file format error!");
163-
return -EINVAL;
180+
ret = -EINVAL;
181+
goto out_line;
164182
}
165183

166-
data = kmm_zalloc(sensor_gen->obj.meta->o_size);
184+
data = zalloc(sensor_gen->obj.meta->o_size);
167185
if (data == NULL)
168186
{
169-
return -ENOMEM;
187+
ret = -ENOMEM;
188+
goto out_line;
170189
}
171190

172191
fd = orb_advertise_multi_queue_persist(sensor_gen->obj.meta,
173192
data, &sensor_gen->obj.instance, 1);
174193
if (fd < 0)
175194
{
176195
uorbinfo_raw("Playback orb advertise failed[%d]!", fd);
177-
kmm_free(data);
178-
return fd;
196+
ret = fd;
197+
goto out_data;
179198
}
180199

181-
while (fgets(line, GENERATOR_CACHE_BUFF, sensor_gen->file) != NULL)
200+
while (fgets(line, GENERATOR_CACHE_BUFF, sensor_gen->file) != NULL &&
201+
!g_gen_should_exit)
182202
{
183203
lib_meminstream(&meminstream, line, GENERATOR_CACHE_BUFF);
184204
ret = lib_bscanf(&meminstream.common, &lastc,
185205
sensor_gen->obj.meta->o_format, data);
186206
if (ret >= 0)
187207
{
188208
tmp_time = *(uint64_t *)data;
189-
if (is_frist)
209+
if (is_first)
190210
{
191211
last_time = *(uint64_t *)data;
192-
is_frist = false;
212+
is_first = false;
193213
}
194214
else
195215
{
@@ -205,6 +225,7 @@ static int replay_worker(FAR struct sensor_gen_info_s *sensor_gen)
205225
if (OK != orb_publish(sensor_gen->obj.meta, fd, data))
206226
{
207227
uorbinfo_raw("Topic publish error!");
228+
ret = ERROR;
208229
break;
209230
}
210231
}
@@ -216,8 +237,12 @@ static int replay_worker(FAR struct sensor_gen_info_s *sensor_gen)
216237
}
217238

218239
orb_unadvertise(fd);
219-
kmm_free(data);
220-
return 0;
240+
241+
out_data:
242+
free(data);
243+
out_line:
244+
free(line);
245+
return ret;
221246
}
222247

223248
/****************************************************************************
@@ -251,7 +276,7 @@ static int fake_worker(FAR struct sensor_gen_info_s *sensor_gen,
251276
return -EINVAL;
252277
}
253278

254-
data = kmm_zalloc(sensor_gen->obj.meta->o_size);
279+
data = malloc(sensor_gen->obj.meta->o_size);
255280
if (data == NULL)
256281
{
257282
return -ENOMEM;
@@ -275,23 +300,25 @@ static int fake_worker(FAR struct sensor_gen_info_s *sensor_gen,
275300

276301
interval = topic_rate ? (1000000 / topic_rate) : 500000;
277302

278-
for (i = 0; i < nb_cycle; ++i)
303+
for (i = 0; i < nb_cycle && !g_gen_should_exit; ++i)
279304
{
280305
*(uint64_t *)data = orb_absolute_time();
281306
if (OK != orb_publish(sensor_gen->obj.meta, fd, data))
282307
{
283308
uorbinfo_raw("Topic publish error!");
284-
goto error;
309+
goto error_fd;
285310
}
286311

287312
nxsig_usleep(interval);
288313
}
289314

290-
kmm_free(data);
315+
free(data);
291316
return 0;
292317

318+
error_fd:
319+
orb_unadvertise(fd);
293320
error:
294-
kmm_free(data);
321+
free(data);
295322
return -1;
296323
}
297324

@@ -301,7 +328,7 @@ static int fake_worker(FAR struct sensor_gen_info_s *sensor_gen,
301328

302329
int main(int argc, FAR char *argv[])
303330
{
304-
FAR struct sensor_gen_info_s sensor_tmp;
331+
struct sensor_gen_info_s sensor_tmp;
305332
float topic_rate = 0.0f;
306333
FAR char *filter = NULL;
307334
FAR char *topic = NULL;
@@ -311,7 +338,7 @@ int main(int argc, FAR char *argv[])
311338
int opt;
312339
int ret;
313340

314-
g_should_exit = false;
341+
g_gen_should_exit = false;
315342
if (signal(SIGINT, exit_handler) == SIG_ERR)
316343
{
317344
return 1;
@@ -320,39 +347,39 @@ int main(int argc, FAR char *argv[])
320347
while ((opt = getopt(argc, argv, "f:t:r:n:sh")) != -1)
321348
{
322349
switch (opt)
323-
{
324-
case 'f':
325-
path = optarg;
326-
break;
327-
328-
case 't':
329-
topic = optarg;
330-
break;
331-
332-
case 'r':
333-
topic_rate = atof(optarg);
334-
if (topic_rate < 0)
335-
{
336-
goto error;
337-
}
338-
break;
339-
340-
case 'n':
341-
nb_cycle = strtol(optarg, NULL, 0);
342-
if (nb_cycle < 1)
343-
{
350+
{
351+
case 'f':
352+
path = optarg;
353+
break;
354+
355+
case 't':
356+
topic = optarg;
357+
break;
358+
359+
case 'r':
360+
topic_rate = atof(optarg);
361+
if (topic_rate < 0)
362+
{
363+
goto error;
364+
}
365+
break;
366+
367+
case 'n':
368+
nb_cycle = strtol(optarg, NULL, 0);
369+
if (nb_cycle < 1)
370+
{
371+
goto error;
372+
}
373+
break;
374+
375+
case 's':
376+
sim = true;
377+
break;
378+
379+
case 'h':
380+
default:
344381
goto error;
345-
}
346-
break;
347-
348-
case 's':
349-
sim = true;
350-
break;
351-
352-
case 'h':
353-
default:
354-
goto error;
355-
}
382+
}
356383
}
357384

358385
if (optind < argc)
@@ -363,7 +390,7 @@ int main(int argc, FAR char *argv[])
363390
ret = get_play_orb_id(topic, &sensor_tmp);
364391
if (ret < 0)
365392
{
366-
return -ERROR;
393+
return ERROR;
367394
}
368395

369396
if (sim)
@@ -376,7 +403,7 @@ int main(int argc, FAR char *argv[])
376403
if (sensor_tmp.file == NULL)
377404
{
378405
uorbinfo_raw("Failed to open file:[%s]!", path);
379-
return -ERROR;
406+
return ERROR;
380407
}
381408

382409
ret = replay_worker(&sensor_tmp);
@@ -387,5 +414,5 @@ int main(int argc, FAR char *argv[])
387414

388415
error:
389416
usage();
390-
return -ERROR;
417+
return ERROR;
391418
}

0 commit comments

Comments
 (0)