xref: /btstack/src/classic/obex_iterator.c (revision cd5f23a3250874824c01a2b3326a9522fea3f99f)
1 /*
2  * Copyright (C) 2014 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 MATTHIAS
24  * RINGWALD 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__ "obex_iterator.c"
39 
40 #include "btstack_config.h"
41 
42 #include <stdint.h>
43 #include <stdlib.h>
44 
45 #ifdef ENABLE_OBEX_DUMP
46 #include <stdio.h>
47 #endif
48 
49 #include "hci_cmd.h"
50 #include "btstack_debug.h"
51 #include "hci.h"
52 #include "bluetooth_sdp.h"
53 #include "classic/sdp_client_rfcomm.h"
54 #include "btstack_event.h"
55 
56 #include "classic/obex.h"
57 #include "classic/obex_iterator.h"
58 
59 static int obex_packet_header_offset_for_opcode(uint8_t opcode){
60     switch (opcode){
61         case OBEX_OPCODE_SETPATH:
62             return 5;
63         case OBEX_OPCODE_CONNECT:
64             return 7;
65         default:
66             return 3;
67     }
68 }
69 
70 static void obex_iterator_reset(obex_iterator_t *context){
71     memset(context, 0, sizeof(obex_iterator_t));
72     context->valid = false;
73 }
74 
75 // check if num bytes are available, and if not, invalidate iterator
76 static bool obex_iterator_check(obex_iterator_t *context, uint32_t num_bytes){
77     if ((context->offset + num_bytes) >= context->length){
78         context->valid = false;
79     }
80     return context->valid;
81 }
82 
83 static void obex_iterator_prepare(obex_iterator_t *context){
84     if (obex_iterator_check(context, 1) == false) return;
85     const uint8_t * data = context->data + context->offset;
86     uint8_t encoding = data[0] >> 6;
87     switch (encoding){
88         case 0:
89         case 1:
90             // 16-bit length info prefixed
91             if (obex_iterator_check(context, 3) == false) return;
92             context->data_size   = big_endian_read_16(data, 1);
93             context->header_size = 3;
94             break;
95         case 2:
96             // 8-bit value
97             context->data_size   = 1;
98             context->header_size = 1;
99             break;
100         case 3:
101             // 32-bit value
102             context->data_size   = 4;
103             context->header_size = 1;
104             break;
105         default:
106             // avoid compiler warning about unused cases (encoding in [0..3])
107             break;
108     }
109     (void) obex_iterator_check(context, context->header_size + context->data_size);
110 }
111 
112 static void obex_iterator_init(obex_iterator_t *context, int header_offset, const uint8_t * packet_data, uint16_t packet_len){
113     if (header_offset >= packet_len) return;
114     context->data   = packet_data + header_offset;
115     context->length = packet_len  - header_offset;
116     context->valid = true;
117     obex_iterator_prepare(context);
118 }
119 
120 void obex_iterator_init_with_request_packet(obex_iterator_t *context, const uint8_t * packet_data, uint16_t packet_len){
121     obex_iterator_reset(context);
122     if (packet_len == 0) return;
123     int header_offset = obex_packet_header_offset_for_opcode(packet_data[0]);
124     obex_iterator_init(context, header_offset, packet_data, packet_len);
125 }
126 
127 void obex_iterator_init_with_response_packet(obex_iterator_t *context, uint8_t request_opcode, const uint8_t * packet_data, uint16_t packet_len){
128     obex_iterator_reset(context);
129     int header_offset = (request_opcode == OBEX_OPCODE_CONNECT) ? 7 : 3;
130     obex_iterator_init(context, header_offset, packet_data, packet_len);
131 }
132 
133 int  obex_iterator_has_more(const obex_iterator_t * context){
134     return context->valid;
135 }
136 
137 void obex_iterator_next(obex_iterator_t * context){
138     if (context->valid == false) return;
139     context->offset += context->header_size + context->data_size;
140     obex_iterator_prepare(context);
141 }
142 
143 // OBEX packet header access functions
144 
145 // @note BODY/END-OF-BODY headers might be incomplete
146 uint8_t obex_iterator_get_hi(const obex_iterator_t * context){
147     return context->data[context->offset];
148 }
149 uint8_t obex_iterator_get_data_8(const obex_iterator_t * context){
150     return context->data[context->offset+1];
151 }
152 uint32_t obex_iterator_get_data_32(const obex_iterator_t * context){
153     return big_endian_read_32(context->data, context->offset + 1);
154 }
155 uint32_t obex_iterator_get_data_len(const obex_iterator_t * context){
156     return context->data_size;
157 }
158 
159 const uint8_t * obex_iterator_get_data(const obex_iterator_t * context){
160     return &context->data[context->offset + context->header_size];
161 }
162 
163 #ifdef ENABLE_OBEX_DUMP
164 void obex_dump_packet(uint8_t request_opcode, uint8_t * packet, uint16_t size){
165     obex_iterator_t it;
166     printf("OBEX Opcode: 0x%02x\n", request_opcode);
167     int header_offset = (request_opcode == OBEX_OPCODE_CONNECT) ? 7 : 3;
168     printf("OBEX Header: ");
169     printf_hexdump(packet, header_offset);
170     for (obex_iterator_init_with_response_packet(&it, request_opcode, packet, size); obex_iterator_has_more(&it) ; obex_iterator_next(&it)){
171         uint8_t hi = obex_iterator_get_hi(&it);
172         printf("HI: %x - ", hi);
173         uint8_t encoding = hi >> 6;
174         uint16_t len;
175         switch (encoding){
176             case 0:
177             case 1:
178                 len = obex_iterator_get_data_len(&it);
179                 printf_hexdump(obex_iterator_get_data(&it), len);
180                 break;
181             case 2:
182                 printf("%02x\n", obex_iterator_get_data_8(&it));
183                 break;
184             case 3:
185                 printf("%08x\n", (int) obex_iterator_get_data_32(&it));
186                 break;
187             default:
188                 btstack_assert(false);
189                 break;
190         }
191 
192     }
193 }
194 #endif
195