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