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