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__ "goep_client.c" 39 40 #include "btstack_config.h" 41 42 #include <stdint.h> 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <string.h> 46 47 #include "btstack_debug.h" 48 #include "hci_dump.h" 49 #include "bluetooth_sdp.h" 50 #include "btstack_event.h" 51 #include "classic/goep_client.h" 52 #include "classic/obex.h" 53 #include "classic/obex_iterator.h" 54 #include "classic/rfcomm.h" 55 #include "classic/sdp_client_rfcomm.h" 56 57 //------------------------------------------------------------------------------------------------------------ 58 // goep_client.c 59 // 60 61 typedef enum { 62 GOEP_INIT, 63 GOEP_W4_SDP, 64 GOEP_W4_CONNECTION, 65 GOEP_CONNECTED, 66 } goep_state_t; 67 68 typedef struct { 69 uint16_t cid; 70 goep_state_t state; 71 bd_addr_t bd_addr; 72 hci_con_handle_t con_handle; 73 uint8_t incoming; 74 uint8_t bearer_l2cap; 75 uint16_t bearer_port; // l2cap: psm, rfcomm: channel nr 76 uint16_t bearer_cid; 77 uint16_t bearer_mtu; 78 79 uint8_t obex_opcode; 80 uint32_t obex_connection_id; 81 int obex_connection_id_set; 82 83 btstack_packet_handler_t client_handler; 84 } goep_client_t; 85 86 static goep_client_t _goep_client; 87 static goep_client_t * goep_client = &_goep_client; 88 89 static inline void goep_client_emit_connected_event(goep_client_t * context, uint8_t status){ 90 uint8_t event[22]; 91 int pos = 0; 92 event[pos++] = HCI_EVENT_GOEP_META; 93 pos++; // skip len 94 event[pos++] = GOEP_SUBEVENT_CONNECTION_OPENED; 95 little_endian_store_16(event,pos,context->cid); 96 pos+=2; 97 event[pos++] = status; 98 memcpy(&event[pos], context->bd_addr, 6); 99 pos += 6; 100 little_endian_store_16(event,pos,context->con_handle); 101 pos += 2; 102 event[pos++] = context->incoming; 103 event[1] = pos - 2; 104 if (pos != sizeof(event)) log_error("goep_client_emit_connected_event size %u", pos); 105 context->client_handler(HCI_EVENT_PACKET, context->cid, &event[0], pos); 106 } 107 108 static inline void goep_client_emit_connection_closed_event(goep_client_t * context){ 109 uint8_t event[5]; 110 int pos = 0; 111 event[pos++] = HCI_EVENT_GOEP_META; 112 pos++; // skip len 113 event[pos++] = GOEP_SUBEVENT_CONNECTION_CLOSED; 114 little_endian_store_16(event,pos,context->cid); 115 pos+=2; 116 event[1] = pos - 2; 117 if (pos != sizeof(event)) log_error("goep_client_emit_connection_closed_event size %u", pos); 118 context->client_handler(HCI_EVENT_PACKET, context->cid, &event[0], pos); 119 } 120 121 static inline void goep_client_emit_can_send_now_event(goep_client_t * context){ 122 uint8_t event[5]; 123 int pos = 0; 124 event[pos++] = HCI_EVENT_GOEP_META; 125 pos++; // skip len 126 event[pos++] = GOEP_SUBEVENT_CAN_SEND_NOW; 127 little_endian_store_16(event,pos,context->cid); 128 pos+=2; 129 event[1] = pos - 2; 130 if (pos != sizeof(event)) log_error("goep_client_emit_can_send_now_event size %u", pos); 131 context->client_handler(HCI_EVENT_PACKET, context->cid, &event[0], pos); 132 } 133 134 static void goep_client_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 135 UNUSED(channel); 136 UNUSED(size); 137 uint8_t status; 138 switch (packet_type){ 139 case HCI_EVENT_PACKET: 140 switch (hci_event_packet_get_type(packet)) { 141 case RFCOMM_EVENT_CHANNEL_OPENED: 142 status = rfcomm_event_channel_opened_get_status(packet); 143 if (status) { 144 log_info("goep_client: RFCOMM channel open failed, status %u", rfcomm_event_channel_opened_get_status(packet)); 145 goep_client->state = GOEP_INIT; 146 } else { 147 goep_client->bearer_mtu = rfcomm_event_channel_opened_get_max_frame_size(packet); 148 log_info("goep_client: RFCOMM channel open succeeded. cid %u, max frame size %u", goep_client->bearer_cid, goep_client->bearer_mtu); 149 goep_client->state = GOEP_CONNECTED; 150 } 151 goep_client_emit_connected_event(goep_client, status); 152 return; 153 case RFCOMM_EVENT_CAN_SEND_NOW: 154 goep_client_emit_can_send_now_event(goep_client); 155 break; 156 case RFCOMM_CHANNEL_CLOSED: 157 goep_client->state = GOEP_INIT; 158 goep_client_emit_connection_closed_event(goep_client); 159 break; 160 default: 161 break; 162 } 163 break; 164 case RFCOMM_DATA_PACKET: 165 goep_client->client_handler(GOEP_DATA_PACKET, goep_client->cid, packet, size); 166 break; 167 default: 168 break; 169 } 170 } 171 172 static void goep_client_handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 173 UNUSED(packet_type); 174 UNUSED(channel); 175 UNUSED(size); 176 177 switch (packet[0]){ 178 case SDP_EVENT_QUERY_RFCOMM_SERVICE: 179 goep_client->bearer_port = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet); 180 break; 181 case SDP_EVENT_QUERY_COMPLETE: 182 if (goep_client->bearer_port == 0){ 183 log_info("Remote GOEP RFCOMM Server Channel not found"); 184 goep_client->state = GOEP_INIT; 185 goep_client_emit_connected_event(goep_client, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE); 186 break; 187 } 188 log_info("Remote GOEP RFCOMM Server Channel: %u", goep_client->bearer_port); 189 rfcomm_create_channel(&goep_client_packet_handler, goep_client->bd_addr, goep_client->bearer_port, &goep_client->bearer_cid); 190 break; 191 } 192 } 193 194 static void goep_client_packet_append(const uint8_t * data, uint16_t len){ 195 uint8_t * buffer = rfcomm_get_outgoing_buffer(); 196 uint16_t pos = big_endian_read_16(buffer, 1); 197 memcpy(&buffer[pos], data, len); 198 pos += len; 199 big_endian_store_16(buffer, 1, pos); 200 } 201 202 static void goep_client_packet_init(uint16_t goep_cid, uint8_t opcode){ 203 UNUSED(goep_cid); 204 rfcomm_reserve_packet_buffer(); 205 uint8_t * buffer = rfcomm_get_outgoing_buffer(); 206 buffer[0] = opcode; 207 big_endian_store_16(buffer, 1, 3); 208 // store opcode for parsing of response 209 goep_client->obex_opcode = opcode; 210 } 211 212 static void goep_client_packet_add_connection_id(uint16_t goep_cid){ 213 UNUSED(goep_cid); 214 // add connection_id header if set, must be first header if used 215 if (goep_client->obex_connection_id != OBEX_CONNECTION_ID_INVALID){ 216 uint8_t header[5]; 217 header[0] = OBEX_HEADER_CONNECTION_ID; 218 big_endian_store_32(header, 1, goep_client->obex_connection_id); 219 goep_client_packet_append(&header[0], sizeof(header)); 220 } 221 } 222 223 void goep_client_init(void){ 224 memset(goep_client, 0, sizeof(goep_client_t)); 225 goep_client->state = GOEP_INIT; 226 goep_client->cid = 1; 227 goep_client->obex_connection_id = OBEX_CONNECTION_ID_INVALID; 228 } 229 230 uint8_t goep_client_create_connection(btstack_packet_handler_t handler, bd_addr_t addr, uint16_t uuid, uint16_t * out_cid){ 231 if (goep_client->state != GOEP_INIT) return BTSTACK_MEMORY_ALLOC_FAILED; 232 goep_client->client_handler = handler; 233 goep_client->state = GOEP_W4_SDP; 234 memcpy(goep_client->bd_addr, addr, 6); 235 sdp_client_query_rfcomm_channel_and_name_for_uuid(&goep_client_handle_query_rfcomm_event, goep_client->bd_addr, uuid); 236 *out_cid = goep_client->cid; 237 return 0; 238 } 239 240 uint8_t goep_client_disconnect(uint16_t goep_cid){ 241 UNUSED(goep_cid); 242 rfcomm_disconnect(goep_client->bearer_cid); 243 return 0; 244 } 245 246 void goep_client_set_connection_id(uint16_t goep_cid, uint32_t connection_id){ 247 UNUSED(goep_cid); 248 goep_client->obex_connection_id = connection_id; 249 } 250 251 uint8_t goep_client_get_request_opcode(uint16_t goep_cid){ 252 UNUSED(goep_cid); 253 return goep_client->obex_opcode; 254 } 255 256 void goep_client_request_can_send_now(uint16_t goep_cid){ 257 UNUSED(goep_cid); 258 rfcomm_request_can_send_now_event(goep_client->bearer_cid); 259 } 260 261 void goep_client_create_connect_request(uint16_t goep_cid, uint8_t obex_version_number, uint8_t flags, uint16_t maximum_obex_packet_length){ 262 UNUSED(goep_cid); 263 goep_client_packet_init(goep_cid, OBEX_OPCODE_CONNECT); 264 uint8_t fields[4]; 265 fields[0] = obex_version_number; 266 fields[1] = flags; 267 // workaround: limit OBEX packet len to RFCOMM MTU to avoid handling of fragemented packets 268 maximum_obex_packet_length = btstack_min(maximum_obex_packet_length, goep_client->bearer_mtu); 269 big_endian_store_16(fields, 2, maximum_obex_packet_length); 270 goep_client_packet_append(&fields[0], sizeof(fields)); 271 } 272 273 void goep_client_create_get_request(uint16_t goep_cid){ 274 UNUSED(goep_cid); 275 goep_client_packet_init(goep_cid, OBEX_OPCODE_GET | OBEX_OPCODE_FINAL_BIT_MASK); 276 goep_client_packet_add_connection_id(goep_cid); 277 } 278 279 void goep_client_create_set_path_request(uint16_t goep_cid, uint8_t flags){ 280 UNUSED(goep_cid); 281 goep_client_packet_init(goep_cid, OBEX_OPCODE_SETPATH); 282 uint8_t fields[2]; 283 fields[0] = flags; 284 fields[1] = 0; // reserved 285 goep_client_packet_append(&fields[0], sizeof(fields)); 286 goep_client_packet_add_connection_id(goep_cid); 287 } 288 289 void goep_client_add_header_target(uint16_t goep_cid, uint16_t length, const uint8_t * target){ 290 UNUSED(goep_cid); 291 uint8_t header[3]; 292 header[0] = OBEX_HEADER_TARGET; 293 big_endian_store_16(header, 1, 1 + 2 + length); 294 goep_client_packet_append(&header[0], sizeof(header)); 295 goep_client_packet_append(target, length); 296 } 297 298 void goep_client_add_header_name(uint16_t goep_cid, const char * name){ 299 UNUSED(goep_cid); 300 int len_incl_zero = strlen(name) + 1; 301 uint8_t * buffer = rfcomm_get_outgoing_buffer(); 302 uint16_t pos = big_endian_read_16(buffer, 1); 303 buffer[pos++] = OBEX_HEADER_NAME; 304 big_endian_store_16(buffer, pos, 1 + 2 + len_incl_zero*2); 305 pos += 2; 306 int i; 307 // @note name[len] == 0 308 for (i = 0 ; i < len_incl_zero ; i++){ 309 buffer[pos++] = 0; 310 buffer[pos++] = *name++; 311 } 312 big_endian_store_16(buffer, 1, pos); 313 } 314 315 void goep_client_add_header_type(uint16_t goep_cid, const char * type){ 316 UNUSED(goep_cid); 317 uint8_t header[3]; 318 header[0] = OBEX_HEADER_TYPE; 319 int len_incl_zero = strlen(type) + 1; 320 big_endian_store_16(header, 1, 1 + 2 + len_incl_zero); 321 goep_client_packet_append(&header[0], sizeof(header)); 322 goep_client_packet_append((const uint8_t*)type, len_incl_zero); 323 } 324 325 int goep_client_execute(uint16_t goep_cid){ 326 UNUSED(goep_cid); 327 uint8_t * buffer = rfcomm_get_outgoing_buffer(); 328 uint16_t pos = big_endian_read_16(buffer, 1); 329 return rfcomm_send_prepared(goep_client->bearer_cid, pos); 330 } 331