xref: /btstack/src/hci_dump_dispatch.c (revision 1c61f116252e4e085b550a22fb477ae41423cc9c)
1 /*
2  * Copyright (C) 2024 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #define BTSTACK_FILE__ "hci_dump_dispatch.c"
39 
40 #include "hci_dump_dispatch.h"
41 
42 #include <stdint.h>
43 #include <string.h>
44 
45 static btstack_linked_list_t hci_dump_list = NULL;
46 
47 static void hci_dump_reset_all(void) {
48     btstack_linked_list_iterator_t it;
49     btstack_linked_list_iterator_init(&it, &hci_dump_list);
50     while (btstack_linked_list_iterator_has_next(&it)) {
51         hci_dump_dispatch_item_t *list_item = (hci_dump_dispatch_item_t *)btstack_linked_list_iterator_next(&it);
52         if (list_item->hci_dump->reset) {
53             list_item->hci_dump->reset();
54         }
55     }
56 }
57 
58 static void hci_dump_log_packet_all(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) {
59     btstack_linked_list_iterator_t it;
60     btstack_linked_list_iterator_init(&it, &hci_dump_list);
61     while (btstack_linked_list_iterator_has_next(&it)) {
62         hci_dump_dispatch_item_t *list_item = (hci_dump_dispatch_item_t *)btstack_linked_list_iterator_next(&it);
63         if (list_item->hci_dump->log_packet) {
64             list_item->hci_dump->log_packet(packet_type, in, packet, len);
65         }
66     }
67 }
68 
69 static void hci_dump_log_message_all(int log_level, const char *format, va_list argptr) {
70     btstack_linked_list_iterator_t it;
71     btstack_linked_list_iterator_init(&it, &hci_dump_list);
72     while (btstack_linked_list_iterator_has_next(&it)) {
73         hci_dump_dispatch_item_t *list_item = (hci_dump_dispatch_item_t *)btstack_linked_list_iterator_next(&it);
74         if (list_item->hci_dump->log_message) {
75             list_item->hci_dump->log_message(log_level, format, argptr);
76         }
77     }
78 }
79 
80 void hci_dump_dispatch_init(void){
81 }
82 
83 void hci_dump_dispatch_register(hci_dump_dispatch_item_t *list_item, const hci_dump_t *dump) {
84     list_item->hci_dump = dump;
85     btstack_linked_list_add(&hci_dump_list, (btstack_linked_item_t *)list_item);
86 }
87 
88 void hci_dump_dispatch_unregister(hci_dump_t *dump) {
89     btstack_linked_list_remove(&hci_dump_list, (btstack_linked_item_t *)dump);
90 }
91 
92 void hci_dump_dispatch_deinit(void){
93     hci_dump_list = NULL;
94 }
95 
96 static const hci_dump_t hci_dump_dispatch = {
97     .reset = hci_dump_reset_all,
98     .log_packet = hci_dump_log_packet_all,
99     .log_message = hci_dump_log_message_all
100 };
101 
102 const hci_dump_t * hci_dump_dispatch_instance(void){
103     return &hci_dump_dispatch;
104 }
105