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