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_init(obex_iterator_t *context, int header_offset, const uint8_t * packet_data, uint16_t packet_len){ 71 memset(context, 0, sizeof(obex_iterator_t)); 72 context->data = packet_data + header_offset; 73 context->length = packet_len - header_offset; 74 } 75 76 void obex_iterator_init_with_request_packet(obex_iterator_t *context, const uint8_t * packet_data, uint16_t packet_len){ 77 int header_offset = obex_packet_header_offset_for_opcode(packet_data[0]); 78 obex_iterator_init(context, header_offset, packet_data, packet_len); 79 } 80 81 void obex_iterator_init_with_response_packet(obex_iterator_t *context, uint8_t request_opcode, const uint8_t * packet_data, uint16_t packet_len){ 82 int header_offset = (request_opcode == OBEX_OPCODE_CONNECT) ? 7 : 3; 83 obex_iterator_init(context, header_offset, packet_data, packet_len); 84 } 85 86 int obex_iterator_has_more(const obex_iterator_t * context){ 87 return context->offset < context->length; 88 } 89 90 void obex_iterator_next(obex_iterator_t * context){ 91 int len = 0; 92 const uint8_t * data = context->data + context->offset; 93 int encoding = data[0] >> 6; 94 switch (encoding){ 95 case 0: 96 case 1: 97 // 16-bit length info prefixed 98 len = big_endian_read_16(data, 1); 99 break; 100 case 2: 101 // 8-bit value 102 len = 2; 103 break; 104 case 3: 105 // 32-bit value 106 len = 5; 107 break; 108 // avoid compiler warning about unused cases (by unclever compilers) 109 default: 110 break; 111 } 112 context->offset += len; 113 } 114 115 // OBEX packet header access functions 116 117 // @note BODY/END-OF-BODY headers might be incomplete 118 uint8_t obex_iterator_get_hi(const obex_iterator_t * context){ 119 return context->data[context->offset]; 120 } 121 uint8_t obex_iterator_get_data_8(const obex_iterator_t * context){ 122 return context->data[context->offset+1]; 123 } 124 uint32_t obex_iterator_get_data_32(const obex_iterator_t * context){ 125 return big_endian_read_32(context->data, context->offset + 1); 126 } 127 uint32_t obex_iterator_get_data_len(const obex_iterator_t * context){ 128 const uint8_t * data = context->data + context->offset; 129 int encoding = data[0] >> 6; 130 switch (encoding){ 131 case 0: 132 case 1: 133 // 16-bit length info prefixed 134 return big_endian_read_16(data, 1) - 3; 135 case 2: 136 // 8-bit value 137 return 1; 138 case 3: 139 // 32-bit value 140 return 4; 141 // avoid compiler warning about unused cases (by unclever compilers) 142 default: 143 return 0; 144 } 145 } 146 147 const uint8_t * obex_iterator_get_data(const obex_iterator_t * context){ 148 const uint8_t * data = context->data + context->offset; 149 int encoding = data[0] >> 6; 150 switch (encoding){ 151 case 0: 152 case 1: 153 // 16-bit length info prefixed 154 return &data[3]; 155 default: 156 // 8-bit value 157 // 32-bit value 158 return &data[1]; 159 } 160 } 161 162 #ifdef ENABLE_OBEX_DUMP 163 void obex_dump_packet(uint8_t request_opcode, uint8_t * packet, uint16_t size){ 164 obex_iterator_t it; 165 printf("OBEX Opcode: 0x%02x\n", request_opcode); 166 int header_offset = (request_opcode == OBEX_OPCODE_CONNECT) ? 7 : 3; 167 printf("OBEX Header: "); 168 printf_hexdump(packet, header_offset); 169 for (obex_iterator_init_with_response_packet(&it, request_opcode, packet, size); obex_iterator_has_more(&it) ; obex_iterator_next(&it)){ 170 uint8_t hi = obex_iterator_get_hi(&it); 171 printf("HI: %x - ", hi); 172 uint8_t encoding = hi >> 6; 173 uint16_t len; 174 switch (encoding){ 175 case 0: 176 case 1: 177 len = obex_iterator_get_data_len(&it); 178 printf_hexdump(obex_iterator_get_data(&it), len); 179 break; 180 case 2: 181 printf("%02x\n", obex_iterator_get_data_8(&it)); 182 break; 183 case 3: 184 printf("%08x\n", (int) obex_iterator_get_data_32(&it)); 185 break; 186 default: 187 btstack_assert(false); 188 break; 189 } 190 191 } 192 } 193 #endif 194