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