xref: /btstack/src/btstack_tlv_builder.h (revision 849f0fb42e98c1fff4bdcbddc36037b16efbe362)
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 TODO
40  */
41 
42 #ifndef BTSTACK_TLV_BUILDER_H
43 #define BTSTACK_TLV_BUILDER_H
44 
45 #include <stdint.h>
46 
47 #if defined __cplusplus
48 extern "C" {
49 #endif
50 
51 
52 /* API_START */
53 
54 typedef struct {
55     uint8_t * buffer;
56     uint16_t size;
57     uint16_t len_pos;
58     uint16_t write_pos;
59 } btstack_tlv_builder_context_t;
60 
61 /**
62  * @brief Initialize Length/Tag/Value builder with buffer and size for 8-bit tag and length fields
63  *
64  * @param context
65  * @param buffer
66  * @param size
67  */
68 void btstack_ltv_builder_init(btstack_tlv_builder_context_t * context, uint8_t * buffer, uint16_t size);
69 
70 /**
71  * @brief Query remaining space in buffer
72  *
73  * @param context
74  * @return number of bytes that can be added
75  */
76 uint16_t btstack_ltv_builder_remaining_space(btstack_tlv_builder_context_t * context);
77 
78 /**
79  * @brief Get constructed event length
80  * @param context
81  * @return number of bytes in event
82  */
83 uint16_t btstack_ltv_builder_get_length(btstack_tlv_builder_context_t * context);
84 
85 /**
86  * @brief Add tag
87  * @param context
88  * @return number of bytes in event
89  */
90 void btstack_ltv_builder_add_tag(btstack_tlv_builder_context_t * context, uint8_t tag);
91 
92 /**
93  * @bbrief Add uint8_t to current tag
94  * @param context
95  * @param value
96  */
97 void btstack_ltv_builder_add_08(btstack_tlv_builder_context_t * context, uint8_t value);
98 
99 /**
100  * @bbrief Add uint16_t value to current tag
101  * @param context
102  * @param value
103  */
104 void btstack_ltv_builder_add_bige_endian_16(btstack_tlv_builder_context_t * context, uint16_t value);
105 
106 /**
107  * @bbrief Add 24 bit from uint32_t byte to current tag
108  * @param context
109  * @param value
110  */
111 void btstack_ltv_builder_add_bige_endian_24(btstack_tlv_builder_context_t * context, uint32_t value);
112 
113 /**
114  * @bbrief Add uint32_t to current tag
115  * @param context
116  * @param value
117  */
118 void btstack_ltv_builder_add_bige_endian_32(btstack_tlv_builder_context_t * context, uint32_t value);
119 
120 /**
121  * @bbrief Add byte sequence to current tag
122  * @param context
123  * @param value
124  */
125 void btstack_ltv_builder_add_bytes(btstack_tlv_builder_context_t * context, const uint8_t * data, uint16_t length);
126 
127 /**
128  * @bbrief Add string to current tag
129  * @param context
130  * @param value
131  */
132 void btstack_ltv_builder_add_string(btstack_tlv_builder_context_t * context, const char * text);
133 
134 #if defined __cplusplus
135 }
136 #endif
137 #endif // BTSTACK_TLV_BUILDER_H
138