xref: /btstack/src/hci_event_builder.h (revision 64b4329f5bf32b92c612ea7ea6267ac254f10927)
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 /**
39  *  @brief Builder for HCI Events
40  */
41 
42 #ifndef HCI_EVENT_BUILDER_H
43 #define HCI_EVENT_BUILDER_H
44 
45 #include <stdint.h>
46 
47 #include "bluetooth.h"
48 
49 #if defined __cplusplus
50 extern "C" {
51 #endif
52 
53 /* API_START */
54 
55 typedef struct {
56     uint8_t * buffer;
57     uint16_t size;
58     uint16_t pos;
59 } hci_event_builder_context_t;
60 
61 /**
62  * @brief Initialize HCI Event Builder with buffer, event and subevent type
63  *
64  * @param context
65  * @param buffer
66  * @param size
67  * @param event_type
68  * @param subevent_type, use 0 for regular events
69  */
70 void hci_event_builder_init(hci_event_builder_context_t * context, uint8_t * buffer, uint16_t size, uint8_t event_type, uint8_t subevent_type);
71 
72 /**
73  * @brief Query remaining space in event buffer
74  *
75  * @param context
76  * @return number of bytes that can be added
77  */
78 uint16_t hci_event_builder_remaining_space(hci_event_builder_context_t * context);
79 
80 /**
81  * @brief Get constructed event length
82  * @param context
83  * @return number of bytes in event
84  */
85 uint16_t hci_event_builder_get_length(hci_event_builder_context_t * context);
86 
87 /**
88  * @bbrief Add uint8_t to event
89  * @param context
90  * @param value
91  */
92 void hci_event_builder_add_08(hci_event_builder_context_t * context, uint8_t value);
93 
94 /**
95  * @bbrief Add uint16_t value to event
96  * @param context
97  * @param value
98  */
99 void hci_event_builder_add_16(hci_event_builder_context_t * context, uint16_t value);
100 
101 /**
102  * @bbrief Add 24 bit from uint32_t byte to event
103  * @param context
104  * @param value
105  */
106 void hci_event_builder_add_24(hci_event_builder_context_t * context, uint32_t value);
107 
108 /**
109  * @bbrief Add uint32_t to event
110  * @param context
111  * @param value
112  */
113 void hci_event_builder_add_32(hci_event_builder_context_t * context, uint32_t value);
114 
115 /**
116  * @bbrief Add uint8_t[8] in big_endian to event
117  * @param context
118  * @param value
119  */
120 void hci_event_builder_add_64(hci_event_builder_context_t * context, const uint8_t * value);
121 
122 /**
123  * @bbrief Add uint8_t[16] in big_endian to event
124  * @param context
125  * @param value
126  */
127 void hci_event_builder_add_128(hci_event_builder_context_t * context, const uint8_t * value);
128 
129 /**
130  * @bbrief Add Bluetooth address to event
131  * @param context
132  * @param value
133  */
134 void hci_event_builder_add_bd_addr(hci_event_builder_context_t * context, bd_addr_t addr);
135 
136 /**
137  * @bbrief Add HCI Connection Handle to event
138  * @param context
139  * @param value
140  */
141 void hci_event_builder_add_con_handle(hci_event_builder_context_t * context, hci_con_handle_t con_handle);
142 
143 /**
144  * @bbrief Add string to event
145  * @param context
146  * @param value
147  */
148 void hci_event_builder_add_string(hci_event_builder_context_t * context, const char * text);
149 
150 /**
151  * @bbrief Add byte sequence to event
152  * @param context
153  * @param value
154  */
155 void hci_event_builder_add_bytes(hci_event_builder_context_t * context, const uint8_t * data, uint16_t length);
156 
157 /* API_END */
158 
159 #if defined __cplusplus
160 }
161 #endif
162 #endif // HCI_EVENT_BUILDER_H
163