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