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