Make API CTF-agnostic
[babeltrace.git] / plugins / text / dmesg / dmesg.c
1 /*
2 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
3 * Copyright 2017 Philippe Proulx <jeremie.galarneau@efficios.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #define BT_LOG_TAG "PLUGIN-TEXT-DMESG-SRC"
25 #include "logging.h"
26
27 #include <stdbool.h>
28 #include <string.h>
29 #include <ctype.h>
30 #include <stdio.h>
31 #include <babeltrace/assert-internal.h>
32 #include <babeltrace/babeltrace.h>
33 #include <babeltrace/values-internal.h>
34 #include <babeltrace/compat/utc-internal.h>
35 #include <babeltrace/compat/stdio-internal.h>
36 #include <glib.h>
37
38 #define NSEC_PER_USEC 1000UL
39 #define NSEC_PER_MSEC 1000000UL
40 #define NSEC_PER_SEC 1000000000ULL
41 #define USEC_PER_SEC 1000000UL
42
43 struct dmesg_component;
44
45 struct dmesg_notif_iter {
46 struct dmesg_component *dmesg_comp;
47 struct bt_private_connection_private_notification_iterator *pc_notif_iter; /* Weak */
48 char *linebuf;
49 size_t linebuf_len;
50 FILE *fp;
51 struct bt_notification *tmp_event_notif;
52
53 enum {
54 STATE_EMIT_STREAM_BEGINNING,
55 STATE_EMIT_PACKET_BEGINNING,
56 STATE_EMIT_EVENT,
57 STATE_EMIT_PACKET_END,
58 STATE_EMIT_STREAM_END,
59 STATE_DONE,
60 } state;
61 };
62
63 struct dmesg_component {
64 struct {
65 GString *path;
66 bt_bool read_from_stdin;
67 bt_bool no_timestamp;
68 } params;
69
70 struct bt_trace *trace;
71 struct bt_stream_class *stream_class;
72 struct bt_event_class *event_class;
73 struct bt_stream *stream;
74 struct bt_packet *packet;
75 struct bt_clock_class *clock_class;
76 struct bt_clock_class_priority_map *cc_prio_map;
77 };
78
79 static
80 struct bt_field_type *create_event_payload_ft(void)
81 {
82 struct bt_field_type *root_ft = NULL;
83 struct bt_field_type *ft = NULL;
84 int ret;
85
86 root_ft = bt_field_type_structure_create();
87 if (!root_ft) {
88 BT_LOGE_STR("Cannot create an empty structure field type object.");
89 goto error;
90 }
91
92 ft = bt_field_type_string_create();
93 if (!ft) {
94 BT_LOGE_STR("Cannot create a string field type object.");
95 goto error;
96 }
97
98 ret = bt_field_type_structure_append_member(root_ft, "str", ft);
99 if (ret) {
100 BT_LOGE("Cannot add `str` member to structure field type: "
101 "ret=%d", ret);
102 goto error;
103 }
104
105 goto end;
106
107 error:
108 BT_PUT(root_ft);
109
110 end:
111 bt_put(ft);
112 return root_ft;
113 }
114
115 static
116 int create_meta(struct dmesg_component *dmesg_comp, bool has_ts)
117 {
118 struct bt_field_type *ft = NULL;
119 const char *trace_name = NULL;
120 gchar *basename = NULL;
121 int ret = 0;
122
123 dmesg_comp->trace = bt_trace_create();
124 if (!dmesg_comp->trace) {
125 BT_LOGE_STR("Cannot create an empty trace object.");
126 goto error;
127 }
128
129 if (dmesg_comp->params.read_from_stdin) {
130 trace_name = "STDIN";
131 } else {
132 basename = g_path_get_basename(dmesg_comp->params.path->str);
133 BT_ASSERT(basename);
134
135 if (strcmp(basename, G_DIR_SEPARATOR_S) != 0 &&
136 strcmp(basename, ".") != 0) {
137 trace_name = basename;
138 }
139 }
140
141 if (trace_name) {
142 ret = bt_trace_set_name(dmesg_comp->trace, trace_name);
143 if (ret) {
144 BT_LOGE("Cannot set trace's name: name=\"%s\"", trace_name);
145 goto error;
146 }
147 }
148
149 dmesg_comp->stream_class = bt_stream_class_create(dmesg_comp->trace);
150 if (!dmesg_comp->stream_class) {
151 BT_LOGE_STR("Cannot create a stream class object.");
152 goto error;
153 }
154
155 if (has_ts) {
156 dmesg_comp->clock_class = bt_clock_class_create();
157 if (!dmesg_comp->clock_class) {
158 BT_LOGE_STR("Cannot create clock class.");
159 goto error;
160 }
161
162 ret = bt_stream_class_set_default_clock_class(
163 dmesg_comp->stream_class, dmesg_comp->clock_class);
164 if (ret) {
165 BT_LOGE_STR("Cannot set stream class's default clock class.");
166 goto error;
167 }
168 }
169
170 dmesg_comp->event_class = bt_event_class_create(
171 dmesg_comp->stream_class);
172 if (!dmesg_comp->event_class) {
173 BT_LOGE_STR("Cannot create an event class object.");
174 goto error;
175 }
176
177 ret = bt_event_class_set_name(dmesg_comp->event_class, "string");
178 if (ret) {
179 BT_LOGE_STR("Cannot set event class's name.");
180 goto error;
181 }
182
183 ft = create_event_payload_ft();
184 if (!ft) {
185 BT_LOGE_STR("Cannot create event payload field type.");
186 goto error;
187 }
188
189 ret = bt_event_class_set_payload_field_type(dmesg_comp->event_class, ft);
190 if (ret) {
191 BT_LOGE_STR("Cannot set event class's event payload field type.");
192 goto error;
193 }
194
195 goto end;
196
197 error:
198 ret = -1;
199
200 end:
201 bt_put(ft);
202
203 if (basename) {
204 g_free(basename);
205 }
206
207 return ret;
208 }
209
210 static
211 int handle_params(struct dmesg_component *dmesg_comp, struct bt_value *params)
212 {
213 struct bt_value *read_from_stdin = NULL;
214 struct bt_value *no_timestamp = NULL;
215 struct bt_value *path = NULL;
216 const char *path_str;
217 int ret = 0;
218
219 no_timestamp = bt_value_map_get(params, "no-extract-timestamp");
220 if (no_timestamp) {
221 if (!bt_value_is_bool(no_timestamp)) {
222 BT_LOGE("Expecting a boolean value for the `no-extract-timestamp` parameter: "
223 "type=%s",
224 bt_value_type_string(
225 bt_value_get_type(no_timestamp)));
226 goto error;
227 }
228
229 ret = bt_value_bool_get(no_timestamp,
230 &dmesg_comp->params.no_timestamp);
231 BT_ASSERT(ret == 0);
232 }
233
234 path = bt_value_map_get(params, "path");
235 if (path) {
236 if (dmesg_comp->params.read_from_stdin) {
237 BT_LOGE_STR("Cannot specify both `read-from-stdin` and `path` parameters.");
238 goto error;
239 }
240
241 if (!bt_value_is_string(path)) {
242 BT_LOGE("Expecting a string value for the `path` parameter: "
243 "type=%s",
244 bt_value_type_string(
245 bt_value_get_type(path)));
246 goto error;
247 }
248
249 ret = bt_value_string_get(path, &path_str);
250 BT_ASSERT(ret == 0);
251 g_string_assign(dmesg_comp->params.path, path_str);
252 } else {
253 dmesg_comp->params.read_from_stdin = true;
254 }
255
256 goto end;
257
258 error:
259 ret = -1;
260
261 end:
262 bt_put(read_from_stdin);
263 bt_put(path);
264 bt_put(no_timestamp);
265 return ret;
266 }
267
268 static
269 int create_packet_and_stream(struct dmesg_component *dmesg_comp)
270 {
271 int ret = 0;
272
273 dmesg_comp->stream = bt_stream_create(dmesg_comp->stream_class);
274 if (!dmesg_comp->stream) {
275 BT_LOGE_STR("Cannot create stream object.");
276 goto error;
277 }
278
279 dmesg_comp->packet = bt_packet_create(dmesg_comp->stream);
280 if (!dmesg_comp->packet) {
281 BT_LOGE_STR("Cannot create packet object.");
282 goto error;
283 }
284
285 ret = bt_trace_make_static(dmesg_comp->trace);
286 if (ret) {
287 BT_LOGE_STR("Cannot make trace static.");
288 goto error;
289 }
290
291 goto end;
292
293 error:
294 ret = -1;
295
296 end:
297 return ret;
298 }
299
300 static
301 int try_create_meta_stream_packet(struct dmesg_component *dmesg_comp,
302 bool has_ts)
303 {
304 int ret = 0;
305
306 if (dmesg_comp->trace) {
307 /* Already created */
308 goto end;
309 }
310
311 ret = create_meta(dmesg_comp, has_ts);
312 if (ret) {
313 BT_LOGE("Cannot create metadata objects: dmesg-comp-addr=%p",
314 dmesg_comp);
315 goto error;
316 }
317
318 ret = create_packet_and_stream(dmesg_comp);
319 if (ret) {
320 BT_LOGE("Cannot create packet and stream objects: "
321 "dmesg-comp-addr=%p", dmesg_comp);
322 goto error;
323 }
324
325 goto end;
326
327 error:
328 ret = -1;
329
330 end:
331 return ret;
332 }
333
334 static
335 void destroy_dmesg_component(struct dmesg_component *dmesg_comp)
336 {
337 if (!dmesg_comp) {
338 return;
339 }
340
341 if (dmesg_comp->params.path) {
342 g_string_free(dmesg_comp->params.path, TRUE);
343 }
344
345 bt_put(dmesg_comp->packet);
346 bt_put(dmesg_comp->trace);
347 bt_put(dmesg_comp->stream_class);
348 bt_put(dmesg_comp->event_class);
349 bt_put(dmesg_comp->stream);
350 bt_put(dmesg_comp->clock_class);
351 bt_put(dmesg_comp->cc_prio_map);
352 g_free(dmesg_comp);
353 }
354
355 static
356 enum bt_component_status create_port(struct bt_private_component *priv_comp)
357 {
358 return bt_private_component_source_add_output_private_port(priv_comp,
359 "out", NULL, NULL);
360 }
361
362 BT_HIDDEN
363 enum bt_component_status dmesg_init(struct bt_private_component *priv_comp,
364 struct bt_value *params, void *init_method_data)
365 {
366 int ret = 0;
367 struct dmesg_component *dmesg_comp = g_new0(struct dmesg_component, 1);
368 enum bt_component_status status = BT_COMPONENT_STATUS_OK;
369
370 if (!dmesg_comp) {
371 BT_LOGE_STR("Failed to allocate one dmesg component structure.");
372 goto error;
373 }
374
375 dmesg_comp->params.path = g_string_new(NULL);
376 if (!dmesg_comp->params.path) {
377 BT_LOGE_STR("Failed to allocate a GString.");
378 goto error;
379 }
380
381 ret = handle_params(dmesg_comp, params);
382 if (ret) {
383 BT_LOGE("Invalid parameters: comp-addr=%p", priv_comp);
384 goto error;
385 }
386
387 if (!dmesg_comp->params.read_from_stdin &&
388 !g_file_test(dmesg_comp->params.path->str,
389 G_FILE_TEST_IS_REGULAR)) {
390 BT_LOGE("Input path is not a regular file: "
391 "comp-addr=%p, path=\"%s\"", priv_comp,
392 dmesg_comp->params.path->str);
393 goto error;
394 }
395
396 status = create_port(priv_comp);
397 if (status != BT_COMPONENT_STATUS_OK) {
398 goto error;
399 }
400
401 (void) bt_private_component_set_user_data(priv_comp, dmesg_comp);
402 goto end;
403
404 error:
405 destroy_dmesg_component(dmesg_comp);
406 (void) bt_private_component_set_user_data(priv_comp, NULL);
407
408 if (status >= 0) {
409 status = BT_COMPONENT_STATUS_ERROR;
410 }
411
412 end:
413 return status;
414 }
415
416 BT_HIDDEN
417 void dmesg_finalize(struct bt_private_component *priv_comp)
418 {
419 void *data = bt_private_component_get_user_data(priv_comp);
420
421 destroy_dmesg_component(data);
422 }
423
424 static
425 struct bt_notification *create_init_event_notif_from_line(
426 struct dmesg_notif_iter *notif_iter,
427 const char *line, const char **new_start)
428 {
429 struct bt_event *event;
430 struct bt_notification *notif = NULL;
431 bool has_timestamp = false;
432 unsigned long sec, usec, msec;
433 unsigned int year, mon, mday, hour, min;
434 uint64_t ts = 0;
435 int ret = 0;
436 struct dmesg_component *dmesg_comp = notif_iter->dmesg_comp;
437
438 *new_start = line;
439
440 if (dmesg_comp->params.no_timestamp) {
441 goto skip_ts;
442 }
443
444 /* Extract time from input line */
445 if (sscanf(line, "[%lu.%lu] ", &sec, &usec) == 2) {
446 ts = (uint64_t) sec * USEC_PER_SEC + (uint64_t) usec;
447
448 /*
449 * The clock class we use has a 1 GHz frequency: convert
450 * from µs to ns.
451 */
452 ts *= NSEC_PER_USEC;
453 has_timestamp = true;
454 } else if (sscanf(line, "[%u-%u-%u %u:%u:%lu.%lu] ",
455 &year, &mon, &mday, &hour, &min,
456 &sec, &msec) == 7) {
457 time_t ep_sec;
458 struct tm ti;
459
460 memset(&ti, 0, sizeof(ti));
461 ti.tm_year = year - 1900; /* From 1900 */
462 ti.tm_mon = mon - 1; /* 0 to 11 */
463 ti.tm_mday = mday;
464 ti.tm_hour = hour;
465 ti.tm_min = min;
466 ti.tm_sec = sec;
467
468 ep_sec = bt_timegm(&ti);
469 if (ep_sec != (time_t) -1) {
470 ts = (uint64_t) ep_sec * NSEC_PER_SEC
471 + (uint64_t) msec * NSEC_PER_MSEC;
472 }
473
474 has_timestamp = true;
475 }
476
477 if (has_timestamp) {
478 /* Set new start for the message portion of the line */
479 *new_start = strchr(line, ']');
480 BT_ASSERT(*new_start);
481 (*new_start)++;
482
483 if ((*new_start)[0] == ' ') {
484 (*new_start)++;
485 }
486 }
487
488 skip_ts:
489 /*
490 * At this point, we know if the stream class's event header
491 * field type should have a timestamp or not, so we can lazily
492 * create the metadata, stream, and packet objects.
493 */
494 ret = try_create_meta_stream_packet(dmesg_comp, has_timestamp);
495 if (ret) {
496 /* try_create_meta_stream_packet() logs errors */
497 goto error;
498 }
499
500 notif = bt_notification_event_create(notif_iter->pc_notif_iter,
501 dmesg_comp->event_class, dmesg_comp->packet);
502 if (!notif) {
503 BT_LOGE_STR("Cannot create event notification.");
504 goto error;
505 }
506
507 event = bt_notification_event_borrow_event(notif);
508 BT_ASSERT(event);
509
510 if (dmesg_comp->clock_class) {
511 ret = bt_event_set_default_clock_value(event, ts);
512 BT_ASSERT(ret == 0);
513 }
514
515 goto end;
516
517 error:
518 BT_PUT(notif);
519
520 end:
521 return notif;
522 }
523
524 static
525 int fill_event_payload_from_line(const char *line, struct bt_event *event)
526 {
527 struct bt_field *ep_field = NULL;
528 struct bt_field *str_field = NULL;
529 size_t len;
530 int ret;
531
532 ep_field = bt_event_borrow_payload_field(event);
533 BT_ASSERT(ep_field);
534 str_field = bt_field_structure_borrow_member_field_by_index(ep_field,
535 0);
536 if (!str_field) {
537 BT_LOGE_STR("Cannot borrow `timestamp` field from event payload structure field.");
538 goto error;
539 }
540
541 len = strlen(line);
542 if (line[len - 1] == '\n') {
543 /* Do not include the newline character in the payload */
544 len--;
545 }
546
547 ret = bt_field_string_clear(str_field);
548 if (ret) {
549 BT_LOGE_STR("Cannot clear string field object.");
550 goto error;
551 }
552
553 ret = bt_field_string_append_with_length(str_field, line, len);
554 if (ret) {
555 BT_LOGE("Cannot append value to string field object: "
556 "len=%zu", len);
557 goto error;
558 }
559
560 goto end;
561
562 error:
563 ret = -1;
564
565 end:
566 return ret;
567 }
568
569 static
570 struct bt_notification *create_notif_from_line(
571 struct dmesg_notif_iter *dmesg_notif_iter, const char *line)
572 {
573 struct bt_event *event = NULL;
574 struct bt_notification *notif = NULL;
575 const char *new_start;
576 int ret;
577
578 notif = create_init_event_notif_from_line(dmesg_notif_iter,
579 line, &new_start);
580 if (!notif) {
581 BT_LOGE_STR("Cannot create and initialize event notification from line.");
582 goto error;
583 }
584
585 event = bt_notification_event_borrow_event(notif);
586 BT_ASSERT(event);
587 ret = fill_event_payload_from_line(new_start, event);
588 if (ret) {
589 BT_LOGE("Cannot fill event payload field from line: "
590 "ret=%d", ret);
591 goto error;
592 }
593
594 goto end;
595
596 error:
597 BT_PUT(notif);
598
599 end:
600 return notif;
601 }
602
603 static
604 void destroy_dmesg_notif_iter(struct dmesg_notif_iter *dmesg_notif_iter)
605 {
606 if (!dmesg_notif_iter) {
607 return;
608 }
609
610 if (dmesg_notif_iter->fp && dmesg_notif_iter->fp != stdin) {
611 if (fclose(dmesg_notif_iter->fp)) {
612 BT_LOGE_ERRNO("Cannot close input file", ".");
613 }
614 }
615
616 bt_put(dmesg_notif_iter->tmp_event_notif);
617 free(dmesg_notif_iter->linebuf);
618 g_free(dmesg_notif_iter);
619 }
620
621 BT_HIDDEN
622 enum bt_notification_iterator_status dmesg_notif_iter_init(
623 struct bt_private_connection_private_notification_iterator *priv_notif_iter,
624 struct bt_private_port *priv_port)
625 {
626 struct bt_private_component *priv_comp = NULL;
627 struct dmesg_component *dmesg_comp;
628 struct dmesg_notif_iter *dmesg_notif_iter =
629 g_new0(struct dmesg_notif_iter, 1);
630 enum bt_notification_iterator_status status =
631 BT_NOTIFICATION_ITERATOR_STATUS_OK;
632
633 if (!dmesg_notif_iter) {
634 BT_LOGE_STR("Failed to allocate on dmesg notification iterator structure.");
635 goto error;
636 }
637
638 priv_comp = bt_private_connection_private_notification_iterator_get_private_component(
639 priv_notif_iter);
640 BT_ASSERT(priv_comp);
641 dmesg_comp = bt_private_component_get_user_data(priv_comp);
642 BT_ASSERT(dmesg_comp);
643 dmesg_notif_iter->dmesg_comp = dmesg_comp;
644 dmesg_notif_iter->pc_notif_iter = priv_notif_iter;
645
646 if (dmesg_comp->params.read_from_stdin) {
647 dmesg_notif_iter->fp = stdin;
648 } else {
649 dmesg_notif_iter->fp = fopen(dmesg_comp->params.path->str, "r");
650 if (!dmesg_notif_iter->fp) {
651 BT_LOGE_ERRNO("Cannot open input file in read mode", ": path=\"%s\"",
652 dmesg_comp->params.path->str);
653 goto error;
654 }
655 }
656
657 (void) bt_private_connection_private_notification_iterator_set_user_data(priv_notif_iter,
658 dmesg_notif_iter);
659 goto end;
660
661 error:
662 destroy_dmesg_notif_iter(dmesg_notif_iter);
663 (void) bt_private_connection_private_notification_iterator_set_user_data(priv_notif_iter,
664 NULL);
665 if (status >= 0) {
666 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
667 }
668
669 end:
670 bt_put(priv_comp);
671 return status;
672 }
673
674 BT_HIDDEN
675 void dmesg_notif_iter_finalize(
676 struct bt_private_connection_private_notification_iterator *priv_notif_iter)
677 {
678 destroy_dmesg_notif_iter(bt_private_connection_private_notification_iterator_get_user_data(
679 priv_notif_iter));
680 }
681
682 static
683 enum bt_notification_iterator_status dmesg_notif_iter_next_one(
684 struct dmesg_notif_iter *dmesg_notif_iter,
685 struct bt_notification **notif)
686 {
687 ssize_t len;
688 struct dmesg_component *dmesg_comp;
689 enum bt_notification_iterator_status status =
690 BT_NOTIFICATION_ITERATOR_STATUS_OK;
691
692 BT_ASSERT(dmesg_notif_iter);
693 dmesg_comp = dmesg_notif_iter->dmesg_comp;
694 BT_ASSERT(dmesg_comp);
695
696 if (dmesg_notif_iter->state == STATE_DONE) {
697 status = BT_NOTIFICATION_ITERATOR_STATUS_END;
698 goto end;
699 }
700
701 if (dmesg_notif_iter->tmp_event_notif ||
702 dmesg_notif_iter->state == STATE_EMIT_PACKET_END ||
703 dmesg_notif_iter->state == STATE_EMIT_STREAM_END) {
704 goto handle_state;
705 }
706
707 while (true) {
708 const char *ch;
709 bool only_spaces = true;
710
711 len = bt_getline(&dmesg_notif_iter->linebuf,
712 &dmesg_notif_iter->linebuf_len, dmesg_notif_iter->fp);
713 if (len < 0) {
714 if (errno == EINVAL) {
715 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
716 } else if (errno == ENOMEM) {
717 status =
718 BT_NOTIFICATION_ITERATOR_STATUS_NOMEM;
719 } else {
720 if (dmesg_notif_iter->state == STATE_EMIT_STREAM_BEGINNING) {
721 /* Stream did not even begin */
722 status = BT_NOTIFICATION_ITERATOR_STATUS_END;
723 goto end;
724 } else {
725 /* End current packet now */
726 dmesg_notif_iter->state =
727 STATE_EMIT_PACKET_END;
728 goto handle_state;
729 }
730 }
731
732 goto end;
733 }
734
735 BT_ASSERT(dmesg_notif_iter->linebuf);
736
737 /* Ignore empty lines, once trimmed */
738 for (ch = dmesg_notif_iter->linebuf; *ch != '\0'; ch++) {
739 if (!isspace(*ch)) {
740 only_spaces = false;
741 break;
742 }
743 }
744
745 if (!only_spaces) {
746 break;
747 }
748 }
749
750 dmesg_notif_iter->tmp_event_notif = create_notif_from_line(
751 dmesg_notif_iter, dmesg_notif_iter->linebuf);
752 if (!dmesg_notif_iter->tmp_event_notif) {
753 BT_LOGE("Cannot create event notification from line: "
754 "dmesg-comp-addr=%p, line=\"%s\"", dmesg_comp,
755 dmesg_notif_iter->linebuf);
756 goto end;
757 }
758
759 handle_state:
760 BT_ASSERT(dmesg_comp->trace);
761
762 switch (dmesg_notif_iter->state) {
763 case STATE_EMIT_STREAM_BEGINNING:
764 BT_ASSERT(dmesg_notif_iter->tmp_event_notif);
765 *notif = bt_notification_stream_begin_create(
766 dmesg_notif_iter->pc_notif_iter, dmesg_comp->stream);
767 dmesg_notif_iter->state = STATE_EMIT_PACKET_BEGINNING;
768 break;
769 case STATE_EMIT_PACKET_BEGINNING:
770 BT_ASSERT(dmesg_notif_iter->tmp_event_notif);
771 *notif = bt_notification_packet_begin_create(
772 dmesg_notif_iter->pc_notif_iter, dmesg_comp->packet);
773 dmesg_notif_iter->state = STATE_EMIT_EVENT;
774 break;
775 case STATE_EMIT_EVENT:
776 BT_ASSERT(dmesg_notif_iter->tmp_event_notif);
777 *notif = dmesg_notif_iter->tmp_event_notif;
778 dmesg_notif_iter->tmp_event_notif = NULL;
779 break;
780 case STATE_EMIT_PACKET_END:
781 *notif = bt_notification_packet_end_create(
782 dmesg_notif_iter->pc_notif_iter, dmesg_comp->packet);
783 dmesg_notif_iter->state = STATE_EMIT_STREAM_END;
784 break;
785 case STATE_EMIT_STREAM_END:
786 *notif = bt_notification_stream_end_create(
787 dmesg_notif_iter->pc_notif_iter, dmesg_comp->stream);
788 dmesg_notif_iter->state = STATE_DONE;
789 break;
790 default:
791 break;
792 }
793
794 if (!*notif) {
795 BT_LOGE("Cannot create notification: dmesg-comp-addr=%p",
796 dmesg_comp);
797 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
798 }
799
800 end:
801 return status;
802 }
803
804 BT_HIDDEN
805 enum bt_notification_iterator_status dmesg_notif_iter_next(
806 struct bt_private_connection_private_notification_iterator *priv_notif_iter,
807 bt_notification_array notifs, uint64_t capacity,
808 uint64_t *count)
809 {
810 struct dmesg_notif_iter *dmesg_notif_iter =
811 bt_private_connection_private_notification_iterator_get_user_data(
812 priv_notif_iter);
813 enum bt_notification_iterator_status status =
814 BT_NOTIFICATION_ITERATOR_STATUS_OK;
815 uint64_t i = 0;
816
817 while (i < capacity && status == BT_NOTIFICATION_ITERATOR_STATUS_OK) {
818 status = dmesg_notif_iter_next_one(dmesg_notif_iter, &notifs[i]);
819 if (status == BT_NOTIFICATION_ITERATOR_STATUS_OK) {
820 i++;
821 }
822 }
823
824 if (i > 0) {
825 /*
826 * Even if dmesg_notif_iter_next_one() returned
827 * something else than
828 * BT_NOTIFICATION_ITERATOR_STATUS_OK, we accumulated
829 * notification objects in the output notification
830 * array, so we need to return
831 * BT_NOTIFICATION_ITERATOR_STATUS_OK so that they are
832 * transfered to downstream. This other status occurs
833 * again the next time muxer_notif_iter_do_next() is
834 * called, possibly without any accumulated
835 * notification, in which case we'll return it.
836 */
837 *count = i;
838 status = BT_NOTIFICATION_ITERATOR_STATUS_OK;
839 }
840
841 return status;
842 }
This page took 0.045343 seconds and 4 git commands to generate.