xref: /btstack/src/classic/goep_client.c (revision 4e13f83869f72447800856cff2421b1c7ce02f90)
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 BLUEKITCHEN
24  * GMBH 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 <string.h>
44 
45 #include "btstack_debug.h"
46 #include "hci_dump.h"
47 #include "bluetooth_sdp.h"
48 #include "btstack_event.h"
49 #include "classic/goep_client.h"
50 #include "classic/obex_message_builder.h"
51 #include "classic/obex.h"
52 #include "classic/obex_iterator.h"
53 #include "classic/rfcomm.h"
54 #include "classic/sdp_client.h"
55 #include "classic/sdp_util.h"
56 #include "l2cap.h"
57 
58 //------------------------------------------------------------------------------------------------------------
59 // goep_client.c
60 //
61 
62 #ifdef ENABLE_GOEP_L2CAP
63 #ifndef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
64 #error "ENABLE_GOEP_L2CAP requires ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE. Please enable ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE or disable ENABLE_GOEP_L2CAP"
65 #endif
66 #endif
67 
68 static goep_client_t   goep_client_singleton;
69 
70 static goep_client_t *    goep_client_sdp_active;
71 static uint8_t            goep_client_sdp_query_attribute_value[30];
72 static const unsigned int goep_client_sdp_query_attribute_value_buffer_size = sizeof(goep_client_sdp_query_attribute_value);
73 
74 static uint8_t goep_packet_buffer[150];
75 
76 #ifdef ENABLE_GOEP_L2CAP
77 static uint8_t ertm_buffer[1000];
78 static l2cap_ertm_config_t ertm_config = {
79     1,  // ertm mandatory
80     2,  // max transmit, some tests require > 1
81     2000,
82     12000,
83     512,    // l2cap ertm mtu
84     2,
85     2,
86     1,      // 16-bit FCS
87 };
88 #endif
89 
90 static inline void goep_client_emit_connected_event(goep_client_t * goep_client, uint8_t status){
91     uint8_t event[15];
92     int pos = 0;
93     event[pos++] = HCI_EVENT_GOEP_META;
94     pos++;  // skip len
95     event[pos++] = GOEP_SUBEVENT_CONNECTION_OPENED;
96     little_endian_store_16(event, pos, goep_client->cid);
97     pos+=2;
98     event[pos++] = status;
99     (void)memcpy(&event[pos], goep_client->bd_addr, 6);
100     pos += 6;
101     little_endian_store_16(event, pos, goep_client->con_handle);
102     pos += 2;
103     event[pos++] = goep_client->incoming;
104     event[1] = pos - 2;
105     if (pos != sizeof(event)) log_error("goep_client_emit_connected_event size %u", pos);
106     goep_client->client_handler(HCI_EVENT_PACKET, goep_client->cid, &event[0], pos);
107 }
108 
109 static inline void goep_client_emit_connection_closed_event(goep_client_t * goep_client){
110     uint8_t event[5];
111     int pos = 0;
112     event[pos++] = HCI_EVENT_GOEP_META;
113     pos++;  // skip len
114     event[pos++] = GOEP_SUBEVENT_CONNECTION_CLOSED;
115     little_endian_store_16(event, pos, goep_client->cid);
116     pos+=2;
117     event[1] = pos - 2;
118     if (pos != sizeof(event)) log_error("goep_client_emit_connection_closed_event size %u", pos);
119     goep_client->client_handler(HCI_EVENT_PACKET, goep_client->cid, &event[0], pos);
120 }
121 
122 static inline void goep_client_emit_can_send_now_event(goep_client_t * goep_client){
123     uint8_t event[5];
124     int pos = 0;
125     event[pos++] = HCI_EVENT_GOEP_META;
126     pos++;  // skip len
127     event[pos++] = GOEP_SUBEVENT_CAN_SEND_NOW;
128     little_endian_store_16(event, pos, goep_client->cid);
129     pos+=2;
130     event[1] = pos - 2;
131     if (pos != sizeof(event)) log_error("goep_client_emit_can_send_now_event size %u", pos);
132     goep_client->client_handler(HCI_EVENT_PACKET, goep_client->cid, &event[0], pos);
133 }
134 
135 static void goep_client_handle_connection_opened(goep_client_t * goep_client, uint8_t status, uint16_t mtu){
136     if (status) {
137         goep_client->state = GOEP_CLIENT_INIT;
138         log_info("goep_client: open failed, status %u", status);
139     } else {
140         goep_client->bearer_mtu = mtu;
141         goep_client->state = GOEP_CLIENT_CONNECTED;
142         log_info("goep_client: connection opened. cid %u, max frame size %u", goep_client->bearer_cid, goep_client->bearer_mtu);
143     }
144     goep_client_emit_connected_event(goep_client, status);
145 }
146 
147 static void goep_client_handle_connection_close(goep_client_t * goep_client){
148     goep_client->state = GOEP_CLIENT_INIT;
149     goep_client_emit_connection_closed_event(goep_client);
150 }
151 
152 static goep_client_t * goep_client_for_cid(uint16_t cid){
153     if (cid == goep_client_singleton.cid){
154         return &goep_client_singleton;
155     } else {
156         return NULL;
157     }
158 }
159 
160 static goep_client_t * goep_client_for_bearer_cid(uint16_t bearer_cid){
161     if (bearer_cid == goep_client_singleton.bearer_cid){
162         return &goep_client_singleton;
163     } else {
164         return NULL;
165     }
166 }
167 
168 static void goep_client_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
169     UNUSED(channel);
170     UNUSED(size);
171     goep_client_t * goep_client;
172     switch (packet_type){
173         case HCI_EVENT_PACKET:
174             switch (hci_event_packet_get_type(packet)) {
175 #ifdef ENABLE_GOEP_L2CAP
176                 case L2CAP_EVENT_CHANNEL_OPENED:
177                     goep_client = goep_client_for_bearer_cid(l2cap_event_channel_closed_get_local_cid(packet));
178                     btstack_assert(goep_client != NULL);
179                     goep_client_handle_connection_opened(goep_client, l2cap_event_channel_opened_get_status(packet),
180                                                          btstack_min(l2cap_event_channel_opened_get_remote_mtu(packet), l2cap_event_channel_opened_get_local_mtu(packet)));
181                     break;
182                 case L2CAP_EVENT_CAN_SEND_NOW:
183                     goep_client = goep_client_for_bearer_cid(l2cap_event_can_send_now_get_local_cid(packet));
184                     btstack_assert(goep_client != NULL);
185                     goep_client_emit_can_send_now_event(goep_client);
186                     break;
187                 case L2CAP_EVENT_CHANNEL_CLOSED:
188                     goep_client = goep_client_for_bearer_cid(l2cap_event_channel_closed_get_local_cid(packet));
189                     btstack_assert(goep_client != NULL);
190                     goep_client_handle_connection_close(goep_client);
191                     break;
192 #endif
193                 case RFCOMM_EVENT_CHANNEL_OPENED:
194                     goep_client = goep_client_for_bearer_cid(rfcomm_event_channel_opened_get_rfcomm_cid(packet));
195                     btstack_assert(goep_client != NULL);
196                     goep_client_handle_connection_opened(goep_client, rfcomm_event_channel_opened_get_status(packet), rfcomm_event_channel_opened_get_max_frame_size(packet));
197                     return;
198                 case RFCOMM_EVENT_CAN_SEND_NOW:
199                     goep_client = goep_client_for_bearer_cid(rfcomm_event_can_send_now_get_rfcomm_cid(packet));
200                     btstack_assert(goep_client != NULL);
201                     goep_client_emit_can_send_now_event(goep_client);
202                     break;
203                 case RFCOMM_EVENT_CHANNEL_CLOSED:
204                     goep_client = goep_client_for_bearer_cid(rfcomm_event_channel_closed_get_rfcomm_cid(packet));
205                     btstack_assert(goep_client != NULL);
206                     goep_client_handle_connection_close(goep_client);
207                     break;
208                 default:
209                     break;
210             }
211             break;
212         case L2CAP_DATA_PACKET:
213         case RFCOMM_DATA_PACKET:
214             goep_client = goep_client_for_bearer_cid(channel);
215             btstack_assert(goep_client != NULL);
216             goep_client->client_handler(GOEP_DATA_PACKET, goep_client->cid, packet, size);
217             break;
218         default:
219             break;
220     }
221 }
222 
223 static void goep_client_handle_sdp_query_end_of_record(goep_client_t * goep_client){
224     if (goep_client->uuid == BLUETOOTH_SERVICE_CLASS_MESSAGE_ACCESS_SERVER){
225         if (goep_client->mas_info.instance_id == goep_client->map_mas_instance_id){
226             // Requested MAS Instance found, accept info
227             log_info("MAS Instance #%u found", goep_client->map_mas_instance_id);
228             goep_client->rfcomm_port = goep_client->mas_info.rfcomm_port;
229             goep_client->profile_supported_features = goep_client->mas_info.supported_features;
230             goep_client->map_supported_message_types = goep_client->mas_info.supported_message_types;
231 #ifdef ENABLE_GOEP_L2CAP
232             goep_client->l2cap_psm = goep_client->mas_info.l2cap_psm;
233 #endif
234         }
235     }
236 }
237 
238 static void goep_client_handle_sdp_query_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
239     goep_client_t * goep_client = goep_client_sdp_active;
240     btstack_assert(goep_client != NULL);
241 
242     UNUSED(packet_type);
243     UNUSED(channel);
244     UNUSED(size);
245 
246     des_iterator_t des_list_it;
247     des_iterator_t prot_it;
248     uint8_t status;
249     uint16_t record_index;
250 
251     switch (hci_event_packet_get_type(packet)){
252         case SDP_EVENT_QUERY_ATTRIBUTE_VALUE:
253 
254             // detect new record
255             record_index = sdp_event_query_attribute_byte_get_record_id(packet);
256             if (record_index != goep_client->record_index){
257                 goep_client->record_index = record_index;
258                 goep_client_handle_sdp_query_end_of_record(goep_client);
259                 memset(&goep_client->mas_info, 0, sizeof(goep_client->mas_info));
260             }
261 
262             // check if relevant attribute
263             switch(sdp_event_query_attribute_byte_get_attribute_id(packet)){
264                 case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST:
265                 case BLUETOOTH_ATTRIBUTE_PBAP_SUPPORTED_FEATURES:
266                 case BLUETOOTH_ATTRIBUTE_MAS_INSTANCE_ID:
267                 case BLUETOOTH_ATTRIBUTE_SUPPORTED_MESSAGE_TYPES:
268 #ifdef ENABLE_GOEP_L2CAP
269                 case BLUETOOTH_ATTRIBUTE_GOEP_L2CAP_PSM:
270 #endif
271                     break;
272                 default:
273                     return;
274             }
275 
276             // warn if attribute too large to fit in our buffer
277             if (sdp_event_query_attribute_byte_get_attribute_length(packet) > goep_client_sdp_query_attribute_value_buffer_size) {
278                 log_error("SDP attribute value size exceeded for attribute %x: available %d, required %d", sdp_event_query_attribute_byte_get_attribute_id(packet), goep_client_sdp_query_attribute_value_buffer_size, sdp_event_query_attribute_byte_get_attribute_length(packet));
279                 break;
280             }
281 
282             // store single byte
283             goep_client_sdp_query_attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet);
284 
285             // wait until value fully received
286             if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) != sdp_event_query_attribute_byte_get_attribute_length(packet)) break;
287 
288             // process attributes
289             switch(sdp_event_query_attribute_byte_get_attribute_id(packet)) {
290                 case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST:
291                     for (des_iterator_init(&des_list_it, goep_client_sdp_query_attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) {
292                         uint8_t       *des_element;
293                         uint8_t       *element;
294                         uint32_t       uuid;
295 
296                         if (des_iterator_get_type(&des_list_it) != DE_DES) continue;
297 
298                         des_element = des_iterator_get_element(&des_list_it);
299                         des_iterator_init(&prot_it, des_element);
300 
301                         // first element is UUID
302                         element = des_iterator_get_element(&prot_it);
303                         if (de_get_element_type(element) != DE_UUID) continue;
304 
305                         uuid = de_get_uuid32(element);
306                         des_iterator_next(&prot_it);
307                         if (!des_iterator_has_more(&prot_it)) continue;
308 
309                         // second element is RFCOMM server channel or L2CAP PSM
310                         element = des_iterator_get_element(&prot_it);
311                         if (uuid == BLUETOOTH_PROTOCOL_RFCOMM){
312                             if (goep_client->uuid == BLUETOOTH_SERVICE_CLASS_MESSAGE_ACCESS_SERVER) {
313                                 goep_client->mas_info.rfcomm_port = element[de_get_header_size(element)];
314                             } else {
315                                 goep_client->rfcomm_port = element[de_get_header_size(element)];
316                             }
317                         }
318                     }
319                     break;
320 #ifdef ENABLE_GOEP_L2CAP
321                 case BLUETOOTH_ATTRIBUTE_GOEP_L2CAP_PSM:
322                     if (goep_client->uuid == BLUETOOTH_SERVICE_CLASS_MESSAGE_ACCESS_SERVER){
323                         de_element_get_uint16(goep_client_sdp_query_attribute_value, &goep_client->mas_info.l2cap_psm);
324                     } else {
325                         de_element_get_uint16(goep_client_sdp_query_attribute_value, &goep_client->l2cap_psm);
326                     }
327                     break;
328 #endif
329                 // BLUETOOTH_ATTRIBUTE_PBAP_SUPPORTED_FEATURES == BLUETOOTH_ATTRIBUTE_MAP_SUPPORTED_FEATURES == 0x0317
330                 case BLUETOOTH_ATTRIBUTE_PBAP_SUPPORTED_FEATURES:
331                     if (de_get_element_type(goep_client_sdp_query_attribute_value) != DE_UINT) break;
332                     if (de_get_size_type(goep_client_sdp_query_attribute_value) != DE_SIZE_32) break;
333                     if (goep_client->uuid == BLUETOOTH_SERVICE_CLASS_MESSAGE_ACCESS_SERVER) {
334                         goep_client->mas_info.supported_features  = big_endian_read_32(goep_client_sdp_query_attribute_value, de_get_header_size(goep_client_sdp_query_attribute_value));
335                     } else {
336                         goep_client->profile_supported_features  = big_endian_read_32(goep_client_sdp_query_attribute_value, de_get_header_size(goep_client_sdp_query_attribute_value));
337                     }
338                     break;
339 
340                 case BLUETOOTH_ATTRIBUTE_MAS_INSTANCE_ID:
341                     if (de_get_element_type(goep_client_sdp_query_attribute_value) != DE_UINT) break;
342                     if (de_get_size_type(goep_client_sdp_query_attribute_value) != DE_SIZE_8) break;
343                     goep_client->mas_info.instance_id = goep_client_sdp_query_attribute_value[de_get_header_size(goep_client_sdp_query_attribute_value)];
344                     break;
345 
346                 case BLUETOOTH_ATTRIBUTE_SUPPORTED_MESSAGE_TYPES:
347                     if (de_get_element_type(goep_client_sdp_query_attribute_value) != DE_UINT) break;
348                     if (de_get_size_type(goep_client_sdp_query_attribute_value) != DE_SIZE_8) break;
349                     goep_client->mas_info.supported_message_types = goep_client_sdp_query_attribute_value[de_get_header_size(goep_client_sdp_query_attribute_value)];
350                     break;
351 
352                 default:
353                     break;
354             }
355             break;
356 
357         case SDP_EVENT_QUERY_COMPLETE:
358             goep_client_sdp_active = NULL;
359             goep_client_handle_sdp_query_end_of_record(goep_client);
360             status = sdp_event_query_complete_get_status(packet);
361             if (status != ERROR_CODE_SUCCESS){
362                 log_info("GOEP client, SDP query failed 0x%02x", status);
363                 goep_client->state = GOEP_CLIENT_INIT;
364                 goep_client_emit_connected_event(goep_client, status);
365                 break;
366             }
367             if ((goep_client->rfcomm_port == 0) && (goep_client->l2cap_psm == 0)){
368                 log_info("No GOEP RFCOMM or L2CAP server found");
369                 goep_client->state = GOEP_CLIENT_INIT;
370                 goep_client_emit_connected_event(goep_client, SDP_SERVICE_NOT_FOUND);
371                 break;
372             }
373 #ifdef ENABLE_GOEP_L2CAP
374             if (goep_client->l2cap_psm){
375                 log_info("Remote GOEP L2CAP PSM: %u", goep_client->l2cap_psm);
376                 l2cap_ertm_create_channel(&goep_client_packet_handler, goep_client->bd_addr, goep_client->l2cap_psm,
377                                           &ertm_config, ertm_buffer, sizeof(ertm_buffer), &goep_client->bearer_cid);
378                 return;
379             }
380 #endif
381             log_info("Remote GOEP RFCOMM Server Channel: %u", goep_client->rfcomm_port);
382             rfcomm_create_channel(&goep_client_packet_handler, goep_client->bd_addr, goep_client->rfcomm_port, &goep_client->bearer_cid);
383             break;
384 
385         default:
386             break;
387     }
388 }
389 
390 static uint8_t * goep_client_get_outgoing_buffer(goep_client_t * goep_client){
391     if (goep_client->l2cap_psm){
392         return goep_packet_buffer;
393     } else {
394         return rfcomm_get_outgoing_buffer();
395     }
396 }
397 
398 static uint16_t goep_client_get_outgoing_buffer_len(goep_client_t * goep_client){
399     if (goep_client->l2cap_psm){
400         return sizeof(goep_packet_buffer);
401     } else {
402         return rfcomm_get_max_frame_size(goep_client->bearer_cid);
403     }
404 }
405 
406 static void goep_client_packet_init(goep_client_t *goep_client, uint8_t opcode) {
407     if (goep_client->l2cap_psm != 0){
408     } else {
409         rfcomm_reserve_packet_buffer();
410     }
411     // store opcode for parsing of response
412     goep_client->obex_opcode = opcode;
413 }
414 
415 void goep_client_init(void){
416     goep_client_t * goep_client = &goep_client_singleton;
417     memset(goep_client, 0, sizeof(goep_client_t));
418     goep_client->state = GOEP_CLIENT_INIT;
419     goep_client->cid = 1;
420     goep_client->obex_connection_id = OBEX_CONNECTION_ID_INVALID;
421 }
422 
423 void goep_client_deinit(void){
424     memset(&goep_client_singleton, 0, sizeof(goep_client_t));
425     memset(goep_client_sdp_query_attribute_value, 0, sizeof(goep_client_sdp_query_attribute_value));
426     memset(goep_packet_buffer, 0, sizeof(goep_packet_buffer));
427 }
428 
429 static void geop_client_sdp_query_start(void * context){
430     uint16_t goep_cid = (uint16_t)(uintptr_t) context;
431     goep_client_t * goep_client = goep_client_for_cid(goep_cid);
432     if (context != NULL){
433         goep_client_sdp_active = goep_client;
434         sdp_client_query_uuid16(&goep_client_handle_sdp_query_event, goep_client->bd_addr,
435                                 goep_client->uuid);
436     }
437 }
438 
439 uint8_t goep_client_create_connection(btstack_packet_handler_t handler, bd_addr_t addr, uint16_t uuid, uint16_t * out_cid){
440     goep_client_t * goep_client = &goep_client_singleton;
441     if (goep_client->state != GOEP_CLIENT_INIT) return BTSTACK_MEMORY_ALLOC_FAILED;
442     memset(goep_client, 0, sizeof(goep_client_t));
443     goep_client->client_handler = handler;
444     goep_client->state = GOEP_CLIENT_W4_SDP;
445     goep_client->uuid = uuid;
446     (void)memcpy(goep_client->bd_addr, addr, 6);
447     goep_client->profile_supported_features = PROFILE_FEATURES_NOT_PRESENT;
448     *out_cid = goep_client->cid;
449     goep_client->sdp_query_request.callback = geop_client_sdp_query_start;
450     goep_client->sdp_query_request.context = (void *)(uintptr_t) goep_client->cid;
451     sdp_client_register_query_callback(&goep_client->sdp_query_request);
452     return ERROR_CODE_SUCCESS;
453 }
454 
455 uint32_t goep_client_get_pbap_supported_features(uint16_t goep_cid){
456     goep_client_t * goep_client = goep_client_for_cid(goep_cid);
457     if (goep_client == NULL){
458         return PBAP_FEATURES_NOT_PRESENT;
459     }
460     return goep_client->profile_supported_features;
461 }
462 
463 uint32_t goep_client_get_map_supported_features(uint16_t goep_cid){
464     goep_client_t * goep_client = goep_client_for_cid(goep_cid);
465     if (goep_client == NULL){
466         return PROFILE_FEATURES_NOT_PRESENT;
467     }
468     return goep_client->profile_supported_features;
469 }
470 
471 uint8_t goep_client_get_map_mas_instance_id(uint16_t goep_cid){
472     goep_client_t * goep_client = goep_client_for_cid(goep_cid);
473     if (goep_client == NULL){
474         return 0;
475     }
476     return goep_client->map_mas_instance_id;
477 }
478 
479 uint8_t goep_client_get_map_suported_message_types(uint16_t goep_cid){
480     goep_client_t * goep_client = goep_client_for_cid(goep_cid);
481     if (goep_client == NULL){
482         return 0;
483     }
484     return goep_client->map_supported_message_types;
485 }
486 
487 
488 bool goep_client_version_20_or_higher(uint16_t goep_cid){
489     goep_client_t * goep_client = goep_client_for_cid(goep_cid);
490     if (goep_client == NULL){
491         return false;
492     }
493     return goep_client->l2cap_psm != 0;
494 }
495 
496 void goep_client_request_can_send_now(uint16_t goep_cid){
497     goep_client_t * goep_client = goep_client_for_cid(goep_cid);
498     if (goep_client == NULL){
499         return;
500     }
501     if (goep_client->l2cap_psm){
502         l2cap_request_can_send_now_event(goep_client->bearer_cid);
503     } else {
504         rfcomm_request_can_send_now_event(goep_client->bearer_cid);
505     }
506 }
507 
508 uint8_t goep_client_disconnect(uint16_t goep_cid){
509     goep_client_t * goep_client = goep_client_for_cid(goep_cid);
510     if (goep_client == NULL){
511         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
512     }
513     if (goep_client->l2cap_psm){
514         l2cap_disconnect(goep_client->bearer_cid);
515     } else {
516         rfcomm_disconnect(goep_client->bearer_cid);
517     }
518     return ERROR_CODE_SUCCESS;
519 }
520 
521 void goep_client_set_connection_id(uint16_t goep_cid, uint32_t connection_id){
522     goep_client_t * goep_client = goep_client_for_cid(goep_cid);
523     if (goep_client == NULL){
524         return;
525     }
526     goep_client->obex_connection_id = connection_id;
527 }
528 
529 uint8_t goep_client_get_request_opcode(uint16_t goep_cid){
530     goep_client_t * goep_client = goep_client_for_cid(goep_cid);
531     if (goep_client == NULL){
532         return 0;
533     }
534     return goep_client->obex_opcode;
535 }
536 
537 void goep_client_request_create_connect(uint16_t goep_cid, uint8_t obex_version_number, uint8_t flags, uint16_t maximum_obex_packet_length){
538     goep_client_t * goep_client = goep_client_for_cid(goep_cid);
539     if (goep_client == NULL){
540         return;
541     }
542     goep_client_packet_init(goep_client, OBEX_OPCODE_CONNECT);
543     // workaround: limit OBEX packet len to L2CAP/RFCOMM MTU to avoid handling of fragemented packets
544     maximum_obex_packet_length = btstack_min(maximum_obex_packet_length, goep_client->bearer_mtu);
545 
546     uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client);
547     uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client);
548     obex_message_builder_request_create_connect(buffer, buffer_len, obex_version_number, flags, maximum_obex_packet_length);
549 }
550 
551 void goep_client_request_create_get(uint16_t goep_cid){
552     goep_client_t * goep_client = goep_client_for_cid(goep_cid);
553     if (goep_client == NULL){
554         return;
555     }
556     goep_client_packet_init(goep_client, OBEX_OPCODE_GET | OBEX_OPCODE_FINAL_BIT_MASK);
557     uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client);
558     uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client);
559     obex_message_builder_request_create_get(buffer, buffer_len, goep_client->obex_connection_id);
560 }
561 
562 void goep_client_request_create_put(uint16_t goep_cid){
563     goep_client_t * goep_client = goep_client_for_cid(goep_cid);
564     if (goep_client == NULL){
565         return;
566     }
567     goep_client_packet_init(goep_client, OBEX_OPCODE_PUT | OBEX_OPCODE_FINAL_BIT_MASK);
568     uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client);
569     uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client);
570     obex_message_builder_request_create_put(buffer, buffer_len, goep_client->obex_connection_id);
571 }
572 
573 void goep_client_request_create_set_path(uint16_t goep_cid, uint8_t flags){
574     goep_client_t * goep_client = goep_client_for_cid(goep_cid);
575     if (goep_client == NULL){
576         return;
577     }
578     goep_client_packet_init(goep_client, OBEX_OPCODE_SETPATH);
579     uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client);
580     uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client);
581     obex_message_builder_request_create_set_path(buffer, buffer_len, flags, goep_client->obex_connection_id);
582 }
583 
584 void goep_client_request_create_abort(uint16_t goep_cid){
585     goep_client_t * goep_client = goep_client_for_cid(goep_cid);
586     if (goep_client == NULL){
587         return;
588     }
589     goep_client_packet_init(goep_client, OBEX_OPCODE_ABORT);
590 
591     uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client);
592     uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client);
593     obex_message_builder_request_create_abort(buffer, buffer_len, goep_client->obex_connection_id);
594 }
595 
596 void goep_client_request_create_disconnect(uint16_t goep_cid){
597     goep_client_t * goep_client = goep_client_for_cid(goep_cid);
598     if (goep_client == NULL){
599         return;
600     }
601     goep_client_packet_init(goep_client, OBEX_OPCODE_DISCONNECT);
602     uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client);
603     uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client);
604     obex_message_builder_request_create_disconnect(buffer, buffer_len, goep_client->obex_connection_id);
605 }
606 
607 void goep_client_header_add_byte(uint16_t goep_cid, uint8_t header_type, uint8_t value){
608     goep_client_t * goep_client = goep_client_for_cid(goep_cid);
609     if (goep_client == NULL){
610         return;
611     }
612     uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client);
613     uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client);
614     obex_message_builder_header_add_byte(buffer, buffer_len, header_type, value);
615 }
616 
617 void goep_client_header_add_word(uint16_t goep_cid, uint8_t header_type, uint32_t value){
618     goep_client_t * goep_client = goep_client_for_cid(goep_cid);
619     if (goep_client == NULL){
620         return;
621     }
622     uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client);
623     uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client);
624     obex_message_builder_header_add_word(buffer, buffer_len, header_type, value);
625 }
626 
627 void goep_client_header_add_variable(uint16_t goep_cid, uint8_t header_type, const uint8_t * header_data, uint16_t header_data_length){
628     goep_client_t * goep_client = goep_client_for_cid(goep_cid);
629     if (goep_client == NULL){
630         return;
631     }
632     uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client);
633     uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client);
634     obex_message_builder_header_add_variable(buffer, buffer_len, header_type, header_data, header_data_length);
635 }
636 
637 void goep_client_header_add_srm_enable(uint16_t goep_cid){
638     goep_client_t * goep_client = goep_client_for_cid(goep_cid);
639     if (goep_client == NULL){
640         return;
641     }
642     uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client);
643     uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client);
644     obex_message_builder_header_add_srm_enable(buffer, buffer_len);
645 }
646 
647 void goep_client_header_add_target(uint16_t goep_cid, const uint8_t * target, uint16_t length){
648     goep_client_t * goep_client = goep_client_for_cid(goep_cid);
649     if (goep_client == NULL){
650         return;
651     }
652     uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client);
653     uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client);
654     obex_message_builder_header_add_target(buffer, buffer_len, target, length);
655 }
656 
657 void goep_client_header_add_application_parameters(uint16_t goep_cid, const uint8_t * data, uint16_t length){
658     goep_client_t * goep_client = goep_client_for_cid(goep_cid);
659     if (goep_client == NULL){
660         return;
661     }
662     uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client);
663     uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client);
664     obex_message_builder_header_add_application_parameters(buffer, buffer_len, data, length);
665 }
666 
667 void goep_client_header_add_challenge_response(uint16_t goep_cid, const uint8_t * data, uint16_t length){
668     goep_client_t * goep_client = goep_client_for_cid(goep_cid);
669     if (goep_client == NULL){
670         return;
671     }
672     uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client);
673     uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client);
674     obex_message_builder_header_add_challenge_response(buffer, buffer_len, data, length);
675 }
676 
677 void goep_client_body_add_static(uint16_t goep_cid, const uint8_t * data, uint32_t length){
678     goep_client_t * goep_client = goep_client_for_cid(goep_cid);
679     if (goep_client == NULL){
680         return;
681     }
682     uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client);
683     uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client);
684     obex_message_builder_body_add_static(buffer, buffer_len, data, length);
685 }
686 
687 uint16_t goep_client_body_get_outgoing_buffer_len(uint16_t goep_cid) {
688     goep_client_t * goep_client = goep_client_for_cid(goep_cid);
689     if (goep_client == NULL){
690         return 0;
691     }
692     return goep_client_get_outgoing_buffer_len(goep_client);
693 };
694 
695 void goep_client_body_fillup_static(uint16_t goep_cid, const uint8_t * data, uint32_t length, uint32_t * ret_length){
696     goep_client_t * goep_client = goep_client_for_cid(goep_cid);
697     if (goep_client == NULL){
698         return;
699     }
700     uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client);
701     uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client);
702     obex_message_builder_body_fillup_static(buffer, buffer_len, data, length, ret_length);
703 }
704 
705 void goep_client_header_add_name(uint16_t goep_cid, const char * name){
706     goep_client_t * goep_client = goep_client_for_cid(goep_cid);
707     if (goep_client == NULL){
708         return;
709     }
710     uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client);
711     uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client);
712     obex_message_builder_header_add_name(buffer, buffer_len, name);
713 }
714 
715 void goep_client_header_add_name_prefix(uint16_t goep_cid, const char * name, uint16_t name_len){
716     goep_client_t * goep_client = goep_client_for_cid(goep_cid);
717     if (goep_client == NULL){
718         return;
719     }
720     uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client);
721     uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client);
722     obex_message_builder_header_add_name_prefix(buffer, buffer_len, name, name_len);
723 }
724 
725 void goep_client_header_add_type(uint16_t goep_cid, const char * type){
726     goep_client_t * goep_client = goep_client_for_cid(goep_cid);
727     if (goep_client == NULL){
728         return;
729     }
730     uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client);
731     uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client);
732     obex_message_builder_header_add_type(buffer, buffer_len, type);
733 }
734 
735 void goep_client_header_add_length(uint16_t goep_cid, uint32_t length){
736     goep_client_t * goep_client = goep_client_for_cid(goep_cid);
737     if (goep_client == NULL){
738         return;
739     }
740     uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client);
741     uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client);
742     obex_message_builder_header_add_length(buffer, buffer_len, length);
743 }
744 
745 uint16_t goep_client_request_get_max_body_size(uint16_t goep_cid){
746     goep_client_t * goep_client = goep_client_for_cid(goep_cid);
747     if (goep_client == NULL){
748         return 0;
749     }
750     uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client);
751     uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client);
752     uint16_t pos = big_endian_read_16(buffer, 1);
753     return buffer_len - pos;
754 }
755 
756 int goep_client_execute(uint16_t goep_cid){
757     goep_client_t * goep_client = goep_client_for_cid(goep_cid);
758     if (goep_client == NULL){
759         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
760     }
761     return goep_client_execute_with_final_bit (goep_cid, true);
762 }
763 
764 int goep_client_execute_with_final_bit(uint16_t goep_cid, bool final){
765     goep_client_t * goep_client = goep_client_for_cid(goep_cid);
766     if (goep_client == NULL){
767         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
768     }
769     uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client);
770     uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client);
771 
772     obex_message_builder_set_final_bit (buffer, buffer_len, final);
773 
774     uint16_t pos = big_endian_read_16(buffer, 1);
775     if (goep_client->l2cap_psm){
776         return l2cap_send(goep_client->bearer_cid, buffer, pos);
777     } else {
778         return rfcomm_send_prepared(goep_client->bearer_cid, pos);
779     }
780 }
781 
782