xref: /btstack/src/hci_event_builder.c (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 #define BTSTACK_FILE__ "hci_event_builder.c"
39 
40 #include "hci_event_builder.h"
41 
42 #include "btstack_debug.h"
43 #include "btstack_util.h"
44 
45 #include <stdint.h>
46 #include <stddef.h>
47 
48 static void hci_event_builder_increment_pos(hci_event_builder_context_t * context, uint16_t size){
49     btstack_assert((context->pos + size) <= context->size);
50     context->pos += size;
51     context->buffer[1] = (uint8_t) (context->pos - 2);
52 }
53 
54 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){
55     uint16_t subevent_overhead = subevent_type == 0 ? 0 : 1;
56     btstack_assert(buffer != NULL);
57     btstack_assert(size + subevent_overhead >= 2);
58     context->buffer = buffer;
59     context->size = size;
60     context->pos = 0;
61     hci_event_builder_add_08(context, event_type);
62     context->pos += 1;
63     if (subevent_type != 0){
64         hci_event_builder_add_08(context, subevent_type);
65     }
66 }
67 
68 uint16_t hci_event_builder_remaining_space(hci_event_builder_context_t * context){
69     return context->size - context->pos;
70 }
71 
72 uint16_t hci_event_builder_get_length(hci_event_builder_context_t * context){
73     return context->pos;
74 }
75 
76 void hci_event_builder_add_08(hci_event_builder_context_t * context, uint8_t value){
77     uint16_t pos = context->pos;
78     hci_event_builder_increment_pos(context, 1);
79     context->buffer[pos] = value;
80 }
81 
82 void hci_event_builder_add_16(hci_event_builder_context_t * context, uint16_t value){
83     uint16_t pos = context->pos;
84     hci_event_builder_increment_pos(context, 2);
85     little_endian_store_16(context->buffer, pos, value);
86 }
87 
88 void hci_event_builder_add_24(hci_event_builder_context_t * context, uint32_t value){
89     uint16_t pos = context->pos;
90     hci_event_builder_increment_pos(context, 3);
91     little_endian_store_24(context->buffer, pos, value);
92 }
93 
94 void hci_event_builder_add_32(hci_event_builder_context_t * context, uint32_t value){
95     uint16_t pos = context->pos;
96     hci_event_builder_increment_pos(context, 4);
97     little_endian_store_32(context->buffer, pos, value);
98 }
99 
100 void hci_event_builder_add_64(hci_event_builder_context_t * context, const uint8_t * value){
101     uint16_t pos = context->pos;
102     hci_event_builder_increment_pos(context, 8);
103     reverse_64(value, &context->buffer[pos]);
104 }
105 
106 void hci_event_builder_add_128(hci_event_builder_context_t * context, const uint8_t * value){
107     uint16_t pos = context->pos;
108     hci_event_builder_increment_pos(context, 16);
109     reverse_128(value, &context->buffer[pos]);
110 }
111 
112 void hci_event_builder_add_bd_addr(hci_event_builder_context_t * context, bd_addr_t addr){
113     uint16_t pos = context->pos;
114     hci_event_builder_increment_pos(context, 6);
115     reverse_bd_addr(addr, &context->buffer[pos]);
116 }
117 
118 void hci_event_builder_add_con_handle(hci_event_builder_context_t * context, hci_con_handle_t con_handle){
119     hci_event_builder_add_16(context, (uint16_t) con_handle);
120 }
121 
122 void hci_event_builder_add_string(hci_event_builder_context_t * context, const char * text){
123     uint16_t length = (uint16_t) strlen(text);
124     uint16_t pos = context->pos;
125     hci_event_builder_increment_pos(context, length);
126     memcpy(&context->buffer[pos], text, length);
127 }
128 
129 void hci_event_builder_add_bytes(hci_event_builder_context_t * context, const uint8_t * data, uint16_t length){
130     uint16_t pos = context->pos;
131     hci_event_builder_increment_pos(context, length);
132     memcpy(&context->buffer[pos], data, length);
133 }
134