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 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__ "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 // length includes the header, remove from data size 95 if (context->data_size < 3){ 96 context->valid = false; 97 return; 98 } 99 context->data_size -= 3; 100 break; 101 case 2: 102 // 8-bit value 103 context->data_size = 1; 104 context->header_size = 1; 105 break; 106 case 3: 107 // 32-bit value 108 context->data_size = 4; 109 context->header_size = 1; 110 break; 111 default: 112 // avoid compiler warning about unused cases (encoding in [0..3]) 113 break; 114 } 115 (void) obex_iterator_check(context, context->header_size + context->data_size); 116 } 117 118 static void obex_iterator_init(obex_iterator_t *context, int header_offset, const uint8_t * packet_data, uint16_t packet_len){ 119 if (header_offset >= packet_len) return; 120 context->data = packet_data + header_offset; 121 context->length = packet_len - header_offset; 122 context->valid = true; 123 obex_iterator_prepare(context); 124 } 125 126 void obex_iterator_init_with_request_packet(obex_iterator_t *context, const uint8_t * packet_data, uint16_t packet_len){ 127 obex_iterator_reset(context); 128 if (packet_len == 0) return; 129 int header_offset = obex_packet_header_offset_for_opcode(packet_data[0]); 130 obex_iterator_init(context, header_offset, packet_data, packet_len); 131 } 132 133 void obex_iterator_init_with_response_packet(obex_iterator_t *context, uint8_t request_opcode, const uint8_t * packet_data, uint16_t packet_len){ 134 obex_iterator_reset(context); 135 int header_offset = (request_opcode == OBEX_OPCODE_CONNECT) ? 7 : 3; 136 obex_iterator_init(context, header_offset, packet_data, packet_len); 137 } 138 139 int obex_iterator_has_more(const obex_iterator_t * context){ 140 return context->valid; 141 } 142 143 void obex_iterator_next(obex_iterator_t * context){ 144 if (context->valid == false) return; 145 context->offset += context->header_size + context->data_size; 146 obex_iterator_prepare(context); 147 } 148 149 // OBEX packet header access functions 150 151 // @note BODY/END-OF-BODY headers might be incomplete 152 uint8_t obex_iterator_get_hi(const obex_iterator_t * context){ 153 return context->data[context->offset]; 154 } 155 uint8_t obex_iterator_get_data_8(const obex_iterator_t * context){ 156 return context->data[context->offset+1]; 157 } 158 uint32_t obex_iterator_get_data_32(const obex_iterator_t * context){ 159 return big_endian_read_32(context->data, context->offset + 1); 160 } 161 uint32_t obex_iterator_get_data_len(const obex_iterator_t * context){ 162 return context->data_size; 163 } 164 165 const uint8_t * obex_iterator_get_data(const obex_iterator_t * context){ 166 return &context->data[context->offset + context->header_size]; 167 } 168 169 #ifdef ENABLE_OBEX_DUMP 170 void obex_dump_packet(uint8_t request_opcode, uint8_t * packet, uint16_t size){ 171 obex_iterator_t it; 172 printf("OBEX Opcode: 0x%02x\n", request_opcode); 173 int header_offset = (request_opcode == OBEX_OPCODE_CONNECT) ? 7 : 3; 174 printf("OBEX Header: "); 175 printf_hexdump(packet, header_offset); 176 for (obex_iterator_init_with_response_packet(&it, request_opcode, packet, size); obex_iterator_has_more(&it) ; obex_iterator_next(&it)){ 177 uint8_t hi = obex_iterator_get_hi(&it); 178 printf("HI: %x - ", hi); 179 uint8_t encoding = hi >> 6; 180 uint16_t len; 181 switch (encoding){ 182 case 0: 183 case 1: 184 len = obex_iterator_get_data_len(&it); 185 printf_hexdump(obex_iterator_get_data(&it), len); 186 break; 187 case 2: 188 printf("%02x\n", obex_iterator_get_data_8(&it)); 189 break; 190 case 3: 191 printf("%08x\n", (int) obex_iterator_get_data_32(&it)); 192 break; 193 default: 194 btstack_assert(false); 195 break; 196 } 197 198 } 199 } 200 #endif 201