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