xref: /btstack/src/btstack_tlv_builder.h (revision 50e8757d79bbbf53b0c2fc7539af4e1918c5bdf5)
1*50e8757dSMatthias Ringwald /*
2*50e8757dSMatthias Ringwald  * Copyright (C) 2024 BlueKitchen GmbH
3*50e8757dSMatthias Ringwald  *
4*50e8757dSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5*50e8757dSMatthias Ringwald  * modification, are permitted provided that the following conditions
6*50e8757dSMatthias Ringwald  * are met:
7*50e8757dSMatthias Ringwald  *
8*50e8757dSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9*50e8757dSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10*50e8757dSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11*50e8757dSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12*50e8757dSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13*50e8757dSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14*50e8757dSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15*50e8757dSMatthias Ringwald  *    from this software without specific prior written permission.
16*50e8757dSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17*50e8757dSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18*50e8757dSMatthias Ringwald  *    monetary gain.
19*50e8757dSMatthias Ringwald  *
20*50e8757dSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21*50e8757dSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*50e8757dSMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*50e8757dSMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24*50e8757dSMatthias Ringwald  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25*50e8757dSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26*50e8757dSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27*50e8757dSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28*50e8757dSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29*50e8757dSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30*50e8757dSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*50e8757dSMatthias Ringwald  * SUCH DAMAGE.
32*50e8757dSMatthias Ringwald  *
33*50e8757dSMatthias Ringwald  * Please inquire about commercial licensing options at
34*50e8757dSMatthias Ringwald  * [email protected]
35*50e8757dSMatthias Ringwald  *
36*50e8757dSMatthias Ringwald  */
37*50e8757dSMatthias Ringwald 
38*50e8757dSMatthias Ringwald /**
39*50e8757dSMatthias Ringwald  *  @brief TODO
40*50e8757dSMatthias Ringwald  */
41*50e8757dSMatthias Ringwald 
42*50e8757dSMatthias Ringwald #ifndef BTSTACK_TLV_BUILDER_H
43*50e8757dSMatthias Ringwald #define BTSTACK_TLV_BUILDER_H
44*50e8757dSMatthias Ringwald 
45*50e8757dSMatthias Ringwald #include <stdint.h>
46*50e8757dSMatthias Ringwald 
47*50e8757dSMatthias Ringwald #if defined __cplusplus
48*50e8757dSMatthias Ringwald extern "C" {
49*50e8757dSMatthias Ringwald #endif
50*50e8757dSMatthias Ringwald 
51*50e8757dSMatthias Ringwald 
52*50e8757dSMatthias Ringwald /* API_START */
53*50e8757dSMatthias Ringwald 
54*50e8757dSMatthias Ringwald typedef struct {
55*50e8757dSMatthias Ringwald     uint8_t * buffer;
56*50e8757dSMatthias Ringwald     uint16_t size;
57*50e8757dSMatthias Ringwald     uint16_t len_pos;
58*50e8757dSMatthias Ringwald     uint16_t write_pos;
59*50e8757dSMatthias Ringwald } btstack_tlv_builder_context_t;
60*50e8757dSMatthias Ringwald 
61*50e8757dSMatthias Ringwald /**
62*50e8757dSMatthias Ringwald  * @brief Initialize Length/Tag/Value builder with buffer and size for 8-bit tag and length fields
63*50e8757dSMatthias Ringwald  *
64*50e8757dSMatthias Ringwald  * @param context
65*50e8757dSMatthias Ringwald  * @param buffer
66*50e8757dSMatthias Ringwald  * @param size
67*50e8757dSMatthias Ringwald  */
68*50e8757dSMatthias Ringwald void btstack_ltv_builder_init(btstack_tlv_builder_context_t * context, uint8_t * buffer, uint16_t size);
69*50e8757dSMatthias Ringwald 
70*50e8757dSMatthias Ringwald /**
71*50e8757dSMatthias Ringwald  * @brief Query remaining space in buffer
72*50e8757dSMatthias Ringwald  *
73*50e8757dSMatthias Ringwald  * @param context
74*50e8757dSMatthias Ringwald  * @return number of bytes that can be added
75*50e8757dSMatthias Ringwald  */
76*50e8757dSMatthias Ringwald uint16_t btstack_ltv_builder_remaining_space(btstack_tlv_builder_context_t * context);
77*50e8757dSMatthias Ringwald 
78*50e8757dSMatthias Ringwald /**
79*50e8757dSMatthias Ringwald  * @brief Get constructed event length
80*50e8757dSMatthias Ringwald  * @param context
81*50e8757dSMatthias Ringwald  * @return number of bytes in event
82*50e8757dSMatthias Ringwald  */
83*50e8757dSMatthias Ringwald uint16_t btstack_ltv_builder_get_length(btstack_tlv_builder_context_t * context);
84*50e8757dSMatthias Ringwald 
85*50e8757dSMatthias Ringwald /**
86*50e8757dSMatthias Ringwald  * @brief Add tag
87*50e8757dSMatthias Ringwald  * @param context
88*50e8757dSMatthias Ringwald  * @return number of bytes in event
89*50e8757dSMatthias Ringwald  */
90*50e8757dSMatthias Ringwald void btstack_ltv_builder_add_tag(btstack_tlv_builder_context_t * context, uint8_t tag);
91*50e8757dSMatthias Ringwald 
92*50e8757dSMatthias Ringwald /**
93*50e8757dSMatthias Ringwald  * @bbrief Add uint8_t to current tag
94*50e8757dSMatthias Ringwald  * @param context
95*50e8757dSMatthias Ringwald  * @param value
96*50e8757dSMatthias Ringwald  */
97*50e8757dSMatthias Ringwald void btstack_ltv_builder_add_08(btstack_tlv_builder_context_t * context, uint8_t value);
98*50e8757dSMatthias Ringwald 
99*50e8757dSMatthias Ringwald /**
100*50e8757dSMatthias Ringwald  * @bbrief Add uint16_t value to current tag
101*50e8757dSMatthias Ringwald  * @param context
102*50e8757dSMatthias Ringwald  * @param value
103*50e8757dSMatthias Ringwald  */
104*50e8757dSMatthias Ringwald void btstack_ltv_builder_add_bige_endian_16(btstack_tlv_builder_context_t * context, uint16_t value);
105*50e8757dSMatthias Ringwald 
106*50e8757dSMatthias Ringwald /**
107*50e8757dSMatthias Ringwald  * @bbrief Add 24 bit from uint32_t byte to current tag
108*50e8757dSMatthias Ringwald  * @param context
109*50e8757dSMatthias Ringwald  * @param value
110*50e8757dSMatthias Ringwald  */
111*50e8757dSMatthias Ringwald void btstack_ltv_builder_add_bige_endian_24(btstack_tlv_builder_context_t * context, uint32_t value);
112*50e8757dSMatthias Ringwald 
113*50e8757dSMatthias Ringwald /**
114*50e8757dSMatthias Ringwald  * @bbrief Add uint32_t to current tag
115*50e8757dSMatthias Ringwald  * @param context
116*50e8757dSMatthias Ringwald  * @param value
117*50e8757dSMatthias Ringwald  */
118*50e8757dSMatthias Ringwald void btstack_ltv_builder_add_bige_endian_32(btstack_tlv_builder_context_t * context, uint32_t value);
119*50e8757dSMatthias Ringwald 
120*50e8757dSMatthias Ringwald /**
121*50e8757dSMatthias Ringwald  * @bbrief Add byte sequence to current tag
122*50e8757dSMatthias Ringwald  * @param context
123*50e8757dSMatthias Ringwald  * @param value
124*50e8757dSMatthias Ringwald  */
125*50e8757dSMatthias Ringwald void btstack_ltv_builder_add_bytes(btstack_tlv_builder_context_t * context, const uint8_t * data, uint16_t length);
126*50e8757dSMatthias Ringwald 
127*50e8757dSMatthias Ringwald /**
128*50e8757dSMatthias Ringwald  * @bbrief Add string to current tag
129*50e8757dSMatthias Ringwald  * @param context
130*50e8757dSMatthias Ringwald  * @param value
131*50e8757dSMatthias Ringwald  */
132*50e8757dSMatthias Ringwald void btstack_ltv_builder_add_string(btstack_tlv_builder_context_t * context, const char * text);
133*50e8757dSMatthias Ringwald 
134*50e8757dSMatthias Ringwald #if defined __cplusplus
135*50e8757dSMatthias Ringwald }
136*50e8757dSMatthias Ringwald #endif
137*50e8757dSMatthias Ringwald #endif // BTSTACK_TLV_BUILDER_H
138