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