xref: /btstack/src/classic/goep_client.c (revision 607adee94a4e9e829ed57736930779885e0f1f94)
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.h"
56 #include "classic/sdp_util.h"
57 #include "l2cap.h"
58 
59 //------------------------------------------------------------------------------------------------------------
60 // goep_client.c
61 //
62 
63 typedef enum {
64     GOEP_INIT,
65     GOEP_W4_SDP,
66     GOEP_W4_CONNECTION,
67     GOEP_CONNECTED,
68 } goep_state_t;
69 
70 typedef struct {
71     uint16_t         cid;
72     goep_state_t     state;
73     bd_addr_t        bd_addr;
74     hci_con_handle_t con_handle;
75     uint8_t          incoming;
76     uint8_t          rfcomm_port;
77     uint16_t         l2cap_psm;
78     uint16_t         bearer_cid;
79     uint16_t         bearer_mtu;
80     uint32_t         pbap_supported_features;
81 
82     uint8_t          obex_opcode;
83     uint32_t         obex_connection_id;
84     int              obex_connection_id_set;
85 
86     btstack_packet_handler_t client_handler;
87 } goep_client_t;
88 
89 static goep_client_t _goep_client;
90 static goep_client_t * goep_client = &_goep_client;
91 
92 static uint8_t            attribute_value[30];
93 static const unsigned int attribute_value_buffer_size = sizeof(attribute_value);
94 
95 static inline void goep_client_emit_connected_event(goep_client_t * context, uint8_t status){
96     uint8_t event[22];
97     int pos = 0;
98     event[pos++] = HCI_EVENT_GOEP_META;
99     pos++;  // skip len
100     event[pos++] = GOEP_SUBEVENT_CONNECTION_OPENED;
101     little_endian_store_16(event,pos,context->cid);
102     pos+=2;
103     event[pos++] = status;
104     memcpy(&event[pos], context->bd_addr, 6);
105     pos += 6;
106     little_endian_store_16(event,pos,context->con_handle);
107     pos += 2;
108     event[pos++] = context->incoming;
109     event[1] = pos - 2;
110     if (pos != sizeof(event)) log_error("goep_client_emit_connected_event size %u", pos);
111     context->client_handler(HCI_EVENT_PACKET, context->cid, &event[0], pos);
112 }
113 
114 static inline void goep_client_emit_connection_closed_event(goep_client_t * context){
115     uint8_t event[5];
116     int pos = 0;
117     event[pos++] = HCI_EVENT_GOEP_META;
118     pos++;  // skip len
119     event[pos++] = GOEP_SUBEVENT_CONNECTION_CLOSED;
120     little_endian_store_16(event,pos,context->cid);
121     pos+=2;
122     event[1] = pos - 2;
123     if (pos != sizeof(event)) log_error("goep_client_emit_connection_closed_event size %u", pos);
124     context->client_handler(HCI_EVENT_PACKET, context->cid, &event[0], pos);
125 }
126 
127 static inline void goep_client_emit_can_send_now_event(goep_client_t * context){
128     uint8_t event[5];
129     int pos = 0;
130     event[pos++] = HCI_EVENT_GOEP_META;
131     pos++;  // skip len
132     event[pos++] = GOEP_SUBEVENT_CAN_SEND_NOW;
133     little_endian_store_16(event,pos,context->cid);
134     pos+=2;
135     event[1] = pos - 2;
136     if (pos != sizeof(event)) log_error("goep_client_emit_can_send_now_event size %u", pos);
137     context->client_handler(HCI_EVENT_PACKET, context->cid, &event[0], pos);
138 }
139 
140 static void goep_client_handle_connection_opened(goep_client_t * context, uint8_t status, uint16_t mtu){
141     if (status) {
142         context->state = GOEP_INIT;
143         log_info("goep_client: open failed, status %u", status);
144     } else {
145         context->bearer_mtu = mtu;
146         context->state = GOEP_CONNECTED;
147         log_info("goep_client: connection opened. cid %u, max frame size %u", context->bearer_cid, context->bearer_mtu);
148     }
149     goep_client_emit_connected_event(context, status);
150 }
151 
152 static void goep_client_handle_connection_close(goep_client_t * context){
153     context->state = GOEP_INIT;
154     goep_client_emit_connection_closed_event(context);
155 }
156 
157 static void goep_client_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
158     UNUSED(channel);
159     UNUSED(size);
160     goep_client_t * context = goep_client;
161     switch (packet_type){
162         case HCI_EVENT_PACKET:
163             switch (hci_event_packet_get_type(packet)) {
164                 case L2CAP_EVENT_CHANNEL_OPENED:
165                     goep_client_handle_connection_opened(context, l2cap_event_channel_opened_get_status(packet),
166                         btstack_min(l2cap_event_channel_opened_get_remote_mtu(packet), l2cap_event_channel_opened_get_local_mtu(packet)));
167                     return;
168                 case L2CAP_EVENT_CAN_SEND_NOW:
169                     goep_client_emit_can_send_now_event(context);
170                     break;
171                 case L2CAP_EVENT_CHANNEL_CLOSED:
172                     goep_client_handle_connection_close(context);
173                     break;
174                 case RFCOMM_EVENT_CHANNEL_OPENED:
175                     goep_client_handle_connection_opened(context, rfcomm_event_channel_opened_get_status(packet), rfcomm_event_channel_opened_get_max_frame_size(packet));
176                     return;
177                 case RFCOMM_EVENT_CAN_SEND_NOW:
178                     goep_client_emit_can_send_now_event(context);
179                     break;
180                 case RFCOMM_EVENT_CHANNEL_CLOSED:
181                     goep_client_handle_connection_close(context);
182                     break;
183                 default:
184                     break;
185             }
186             break;
187         case L2CAP_DATA_PACKET:
188         case RFCOMM_DATA_PACKET:
189             context->client_handler(GOEP_DATA_PACKET, context->cid, packet, size);
190             break;
191         default:
192             break;
193     }
194 }
195 
196 static void goep_client_handle_sdp_query_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
197     goep_client_t * context = goep_client;
198 
199     UNUSED(packet_type);
200     UNUSED(channel);
201     UNUSED(size);
202 
203     des_iterator_t des_list_it;
204     des_iterator_t prot_it;
205     uint8_t status;
206 
207 
208     switch (hci_event_packet_get_type(packet)){
209         case SDP_EVENT_QUERY_ATTRIBUTE_VALUE:
210 
211             // check if relevant attribute
212             switch(sdp_event_query_attribute_byte_get_attribute_id(packet)){
213                 case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST:
214                 case BLUETOOTH_ATTRIBUTE_PBAP_SUPPORTED_FEATURES:
215                 case BLUETOOTH_ATTRIBUTE_GOEP_L2CAP_PSM:
216                     break;
217                 default:
218                     return;
219             }
220 
221             // warn if attribute too large to fit in our buffer
222             if (sdp_event_query_attribute_byte_get_attribute_length(packet) > attribute_value_buffer_size) {
223                 log_error("SDP attribute value size exceeded for attribute %x: available %d, required %d", sdp_event_query_attribute_byte_get_attribute_id(packet), attribute_value_buffer_size, sdp_event_query_attribute_byte_get_attribute_length(packet));
224                 break;
225             }
226 
227             // store single byte
228             attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet);
229 
230             // wait until value fully received
231             if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) != sdp_event_query_attribute_byte_get_attribute_length(packet)) break;
232 
233             // process attributes
234             switch(sdp_event_query_attribute_byte_get_attribute_id(packet)) {
235                 case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST:
236                     for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) {
237                         uint8_t       *des_element;
238                         uint8_t       *element;
239                         uint32_t       uuid;
240 
241                         if (des_iterator_get_type(&des_list_it) != DE_DES) continue;
242 
243                         des_element = des_iterator_get_element(&des_list_it);
244                         des_iterator_init(&prot_it, des_element);
245                         element = des_iterator_get_element(&prot_it);
246 
247                         if (de_get_element_type(element) != DE_UUID) continue;
248 
249                         uuid = de_get_uuid32(element);
250                         switch (uuid){
251                             case BLUETOOTH_PROTOCOL_RFCOMM:
252                                 if (!des_iterator_has_more(&prot_it)) continue;
253                                 des_iterator_next(&prot_it);
254                                 element = des_iterator_get_element(&prot_it);
255                                 context->rfcomm_port = element[de_get_header_size(element)];
256                                 break;
257                             default:
258                                 break;
259                         }
260                     }
261                     break;
262                 case BLUETOOTH_ATTRIBUTE_GOEP_L2CAP_PSM:
263                     de_element_get_uint16(attribute_value, &context->l2cap_psm);
264                     break;
265                 case BLUETOOTH_ATTRIBUTE_PBAP_SUPPORTED_FEATURES:
266                     if (de_get_element_type(attribute_value) != DE_UINT) break;
267                     if (de_get_size_type(attribute_value)    != DE_SIZE_32) break;
268                     context->pbap_supported_features  = big_endian_read_32(attribute_value, de_get_header_size(attribute_value));
269                     log_info("pbap_supported_features 0x%x", context->pbap_supported_features);
270                     break;
271                 default:
272                     break;
273             }
274             break;
275 
276         case SDP_EVENT_QUERY_COMPLETE:
277             status = sdp_event_query_complete_get_status(packet);
278             if (status != ERROR_CODE_SUCCESS){
279                 log_info("GOEP client, SDP query failed 0x%02x", status);
280                 context->state = GOEP_INIT;
281                 goep_client_emit_connected_event(goep_client, status);
282                 break;
283             }
284             if (context->rfcomm_port == 0 && context->l2cap_psm == 0){
285                 log_info("No GOEP RFCOMM or L2CAP server found");
286                 context->state = GOEP_INIT;
287                 goep_client_emit_connected_event(goep_client, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE);
288                 break;
289             }
290             if (context->l2cap_psm){
291                 log_info("Remote GOEP L2CAP PSM: %u", context->l2cap_psm);
292                 l2cap_create_channel(&goep_client_packet_handler, context->bd_addr, context->rfcomm_port, 0xffff, &context->bearer_cid);
293             } else {
294                 log_info("Remote GOEP RFCOMM Server Channel: %u", context->rfcomm_port);
295                 rfcomm_create_channel(&goep_client_packet_handler, context->bd_addr, context->rfcomm_port, &context->bearer_cid);
296             }
297             break;
298     }
299 }
300 
301 static uint8_t * goep_client_get_outgoing_buffer(goep_client_t * context){
302     if (context->l2cap_psm){
303         return l2cap_get_outgoing_buffer();
304     } else {
305         return rfcomm_get_outgoing_buffer();
306     }
307 }
308 
309 static void goep_client_packet_append(const uint8_t * data, uint16_t len){
310      goep_client_t * context = goep_client;
311      uint8_t * buffer = goep_client_get_outgoing_buffer(context);
312      uint16_t pos = big_endian_read_16(buffer, 1);
313      memcpy(&buffer[pos], data, len);
314      pos += len;
315      big_endian_store_16(buffer, 1, pos);
316 }
317 
318 static void goep_client_packet_init(uint16_t goep_cid, uint8_t opcode){
319     UNUSED(goep_cid);
320     goep_client_t * context = goep_client;
321     if (context->l2cap_psm){
322         l2cap_reserve_packet_buffer();
323     } else {
324         rfcomm_reserve_packet_buffer();
325     }
326     uint8_t * buffer = goep_client_get_outgoing_buffer(context);
327     buffer[0] = opcode;
328     big_endian_store_16(buffer, 1, 3);
329     // store opcode for parsing of response
330     context->obex_opcode = opcode;
331 }
332 
333 static void goep_client_packet_add_connection_id(uint16_t goep_cid){
334     UNUSED(goep_cid);
335     goep_client_t * context = goep_client;
336     // add connection_id header if set, must be first header if used
337     if (context->obex_connection_id != OBEX_CONNECTION_ID_INVALID){
338         uint8_t header[5];
339         header[0] = OBEX_HEADER_CONNECTION_ID;
340         big_endian_store_32(header, 1, context->obex_connection_id);
341         goep_client_packet_append(&header[0], sizeof(header));
342     }
343 }
344 
345 void goep_client_init(void){
346     memset(goep_client, 0, sizeof(goep_client_t));
347     goep_client->state = GOEP_INIT;
348     goep_client->cid = 1;
349     goep_client->obex_connection_id = OBEX_CONNECTION_ID_INVALID;
350 }
351 
352 uint8_t goep_client_create_connection(btstack_packet_handler_t handler, bd_addr_t addr, uint16_t uuid, uint16_t * out_cid){
353     goep_client_t * context = goep_client;
354     if (context->state != GOEP_INIT) return BTSTACK_MEMORY_ALLOC_FAILED;
355     context->client_handler = handler;
356     context->state = GOEP_W4_SDP;
357     context->l2cap_psm   = 0;
358     context->rfcomm_port = 0;
359     // Backwards compatibility: If the PbapSupportedFeatures attribute is not present 0x00000003
360     // shall be assumed for a remote PSE.
361     context->pbap_supported_features = 0x03;
362     memcpy(context->bd_addr, addr, 6);
363     sdp_client_query_uuid16(&goep_client_handle_sdp_query_event, context->bd_addr, uuid);
364     *out_cid = context->cid;
365     return 0;
366 }
367 
368 uint8_t goep_client_disconnect(uint16_t goep_cid){
369     UNUSED(goep_cid);
370     goep_client_t * context = goep_client;
371     rfcomm_disconnect(context->bearer_cid);
372     return 0;
373 }
374 
375 void goep_client_set_connection_id(uint16_t goep_cid, uint32_t connection_id){
376     UNUSED(goep_cid);
377     goep_client_t * context = goep_client;
378     context->obex_connection_id = connection_id;
379 }
380 
381 uint8_t goep_client_get_request_opcode(uint16_t goep_cid){
382     UNUSED(goep_cid);
383     goep_client_t * context = goep_client;
384     return context->obex_opcode;
385 }
386 
387 void goep_client_request_can_send_now(uint16_t goep_cid){
388     UNUSED(goep_cid);
389     goep_client_t * context = goep_client;
390     if (context->l2cap_psm){
391         l2cap_request_can_send_now_event(context->bearer_cid);
392     } else {
393         rfcomm_request_can_send_now_event(context->bearer_cid);
394     }
395 }
396 
397 void goep_client_create_connect_request(uint16_t goep_cid, uint8_t obex_version_number, uint8_t flags, uint16_t maximum_obex_packet_length){
398     UNUSED(goep_cid);
399     goep_client_t * context = goep_client;
400     goep_client_packet_init(goep_cid, OBEX_OPCODE_CONNECT);
401     uint8_t fields[4];
402     fields[0] = obex_version_number;
403     fields[1] = flags;
404     // workaround: limit OBEX packet len to L2CAP/RFCOMM MTU to avoid handling of fragemented packets
405     maximum_obex_packet_length = btstack_min(maximum_obex_packet_length, context->bearer_mtu);
406     big_endian_store_16(fields, 2, maximum_obex_packet_length);
407     goep_client_packet_append(&fields[0], sizeof(fields));
408 }
409 
410 void goep_client_create_get_request(uint16_t goep_cid){
411     UNUSED(goep_cid);
412     goep_client_packet_init(goep_cid, OBEX_OPCODE_GET | OBEX_OPCODE_FINAL_BIT_MASK);
413     goep_client_packet_add_connection_id(goep_cid);
414 }
415 
416 void goep_client_create_set_path_request(uint16_t goep_cid, uint8_t flags){
417     UNUSED(goep_cid);
418     goep_client_packet_init(goep_cid, OBEX_OPCODE_SETPATH);
419     uint8_t fields[2];
420     fields[0] = flags;
421     fields[1] = 0;  // reserved
422     goep_client_packet_append(&fields[0], sizeof(fields));
423     goep_client_packet_add_connection_id(goep_cid);
424 }
425 
426 static void goep_client_add_header(uint16_t goep_cid, uint8_t header_type, uint16_t header_length, const uint8_t * header_data){
427     UNUSED(goep_cid);
428     uint8_t header[3];
429     header[0] = header_type;
430     big_endian_store_16(header, 1, 1 + 2 + header_length);
431     goep_client_packet_append(&header[0], sizeof(header));
432     goep_client_packet_append(header_data, header_length);
433 }
434 
435 void goep_client_add_header_target(uint16_t goep_cid, uint16_t length, const uint8_t * target){
436     goep_client_add_header(goep_cid, OBEX_HEADER_TARGET, length,  target);
437 }
438 
439 void goep_client_add_header_application_parameters(uint16_t goep_cid, uint16_t length, const uint8_t * data){
440     goep_client_add_header(goep_cid, OBEX_HEADER_APPLICATION_PARAMETERS, length,  data);
441 }
442 
443 void goep_client_add_header_name(uint16_t goep_cid, const char * name){
444     UNUSED(goep_cid);
445     goep_client_t * context = goep_client;
446     int len_incl_zero = strlen(name) + 1;
447     uint8_t * buffer = goep_client_get_outgoing_buffer(context);
448     uint16_t pos = big_endian_read_16(buffer, 1);
449     buffer[pos++] = OBEX_HEADER_NAME;
450     big_endian_store_16(buffer, pos, 1 + 2 + len_incl_zero*2);
451     pos += 2;
452     int i;
453     // @note name[len] == 0
454     for (i = 0 ; i < len_incl_zero ; i++){
455         buffer[pos++] = 0;
456         buffer[pos++] = *name++;
457     }
458     big_endian_store_16(buffer, 1, pos);
459  }
460 
461 void goep_client_add_header_type(uint16_t goep_cid, const char * type){
462     UNUSED(goep_cid);
463     uint8_t header[3];
464     header[0] = OBEX_HEADER_TYPE;
465     int len_incl_zero = strlen(type) + 1;
466     big_endian_store_16(header, 1, 1 + 2 + len_incl_zero);
467     goep_client_packet_append(&header[0], sizeof(header));
468     goep_client_packet_append((const uint8_t*)type, len_incl_zero);
469 }
470 
471 int goep_client_execute(uint16_t goep_cid){
472     UNUSED(goep_cid);
473     goep_client_t * context = goep_client;
474     uint8_t * buffer = goep_client_get_outgoing_buffer(context);
475     uint16_t pos = big_endian_read_16(buffer, 1);
476     if (context->l2cap_psm){
477         return l2cap_send_prepared(context->bearer_cid, pos);
478     } else {
479         return rfcomm_send_prepared(context->bearer_cid, pos);
480     }
481 }
482