xref: /btstack/src/classic/goep_client.c (revision eef3970d7037477002c686a681dcbc61285fde7e)
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_rfcomm.h"
56 
57 //------------------------------------------------------------------------------------------------------------
58 // goep_client.c
59 //
60 
61 typedef enum {
62     GOEP_INIT,
63     GOEP_W4_SDP,
64     GOEP_W4_CONNECTION,
65     GOEP_CONNECTED,
66 } goep_state_t;
67 
68 typedef struct {
69     uint16_t         cid;
70     goep_state_t     state;
71     bd_addr_t        bd_addr;
72     hci_con_handle_t con_handle;
73     uint8_t          incoming;
74     uint8_t          bearer_l2cap;
75     uint16_t         bearer_port;   // l2cap: psm, rfcomm: channel nr
76     uint16_t         bearer_cid;
77     uint16_t         bearer_mtu;
78 
79     uint8_t          obex_opcode;
80     uint32_t         obex_connection_id;
81     int              obex_connection_id_set;
82 
83     btstack_packet_handler_t client_handler;
84 } goep_client_t;
85 
86 static goep_client_t _goep_client;
87 static goep_client_t * goep_client = &_goep_client;
88 
89 static inline void goep_client_emit_connected_event(goep_client_t * context, uint8_t status){
90     uint8_t event[22];
91     int pos = 0;
92     event[pos++] = HCI_EVENT_GOEP_META;
93     pos++;  // skip len
94     event[pos++] = GOEP_SUBEVENT_CONNECTION_OPENED;
95     little_endian_store_16(event,pos,context->cid);
96     pos+=2;
97     event[pos++] = status;
98     memcpy(&event[pos], context->bd_addr, 6);
99     pos += 6;
100     little_endian_store_16(event,pos,context->con_handle);
101     pos += 2;
102     event[pos++] = context->incoming;
103     event[1] = pos - 2;
104     if (pos != sizeof(event)) log_error("goep_client_emit_connected_event size %u", pos);
105     context->client_handler(HCI_EVENT_PACKET, context->cid, &event[0], pos);
106 }
107 
108 static inline void goep_client_emit_connection_closed_event(goep_client_t * context){
109     uint8_t event[5];
110     int pos = 0;
111     event[pos++] = HCI_EVENT_GOEP_META;
112     pos++;  // skip len
113     event[pos++] = GOEP_SUBEVENT_CONNECTION_CLOSED;
114     little_endian_store_16(event,pos,context->cid);
115     pos+=2;
116     event[1] = pos - 2;
117     if (pos != sizeof(event)) log_error("goep_client_emit_connection_closed_event size %u", pos);
118     context->client_handler(HCI_EVENT_PACKET, context->cid, &event[0], pos);
119 }
120 
121 static inline void goep_client_emit_can_send_now_event(goep_client_t * context){
122     uint8_t event[5];
123     int pos = 0;
124     event[pos++] = HCI_EVENT_GOEP_META;
125     pos++;  // skip len
126     event[pos++] = GOEP_SUBEVENT_CAN_SEND_NOW;
127     little_endian_store_16(event,pos,context->cid);
128     pos+=2;
129     event[1] = pos - 2;
130     if (pos != sizeof(event)) log_error("goep_client_emit_can_send_now_event size %u", pos);
131     context->client_handler(HCI_EVENT_PACKET, context->cid, &event[0], pos);
132 }
133 
134 static void goep_client_handle_connection_opened(goep_client_t * context, uint8_t status, uint16_t mtu){
135     if (status) {
136         context->state = GOEP_INIT;
137         log_info("goep_client: open failed, status %u", status);
138     } else {
139         context->bearer_mtu = mtu;
140         context->state = GOEP_CONNECTED;
141         log_info("goep_client: connection opened. cid %u, max frame size %u", context->bearer_cid, context->bearer_mtu);
142     }
143     goep_client_emit_connected_event(context, status);
144 }
145 
146 static void goep_client_handle_connection_close(goep_client_t * context){
147     context->state = GOEP_INIT;
148     goep_client_emit_connection_closed_event(context);
149 }
150 
151 static void goep_client_packet_handler_rfcomm(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
152     UNUSED(channel);
153     UNUSED(size);
154     goep_client_t * context = goep_client;
155     switch (packet_type){
156         case HCI_EVENT_PACKET:
157             switch (hci_event_packet_get_type(packet)) {
158                 case RFCOMM_EVENT_CHANNEL_OPENED:
159                     goep_client_handle_connection_opened(context, rfcomm_event_channel_opened_get_status(packet), rfcomm_event_channel_opened_get_max_frame_size(packet));
160                     return;
161                 case RFCOMM_EVENT_CAN_SEND_NOW:
162                     goep_client_emit_can_send_now_event(context);
163                     break;
164                 case RFCOMM_EVENT_CHANNEL_CLOSED:
165                     goep_client_handle_connection_close(context);
166                     break;
167                 default:
168                     break;
169             }
170             break;
171         case RFCOMM_DATA_PACKET:
172             context->client_handler(GOEP_DATA_PACKET, context->cid, packet, size);
173             break;
174         default:
175             break;
176     }
177 }
178 
179 static void goep_client_handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
180     UNUSED(packet_type);
181     UNUSED(channel);
182     UNUSED(size);
183     goep_client_t * context = goep_client;
184     switch (packet[0]){
185         case SDP_EVENT_QUERY_RFCOMM_SERVICE:
186             context->bearer_port = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet);
187             break;
188         case SDP_EVENT_QUERY_COMPLETE:
189             if (sdp_event_query_complete_get_status(packet)){
190                 log_info("GOEP client, SDP query failed 0x%02x", sdp_event_query_complete_get_status(packet));
191                 context->state = GOEP_INIT;
192                 goep_client_emit_connected_event(goep_client, sdp_event_query_complete_get_status(packet));
193                 break;
194             }
195 
196             if (context->bearer_port == 0){
197                 log_info("Remote GOEP RFCOMM Server Channel not found");
198                 context->state = GOEP_INIT;
199                 goep_client_emit_connected_event(goep_client, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE);
200                 break;
201             }
202             log_info("Remote GOEP RFCOMM Server Channel: %u", context->bearer_port);
203             rfcomm_create_channel(&goep_client_packet_handler_rfcomm, context->bd_addr, context->bearer_port, &context->bearer_cid);
204             break;
205     }
206 }
207 
208 static uint8_t * goep_client_get_outgoing_buffer(goep_client_t * context){
209     if (context->bearer_l2cap){
210         // TODO: implement l2cap variant
211         return NULL;
212     } else {
213         return rfcomm_get_outgoing_buffer();
214     }
215 }
216 
217 static void goep_client_packet_append(const uint8_t * data, uint16_t len){
218      goep_client_t * context = goep_client;
219      uint8_t * buffer = goep_client_get_outgoing_buffer(context);
220      uint16_t pos = big_endian_read_16(buffer, 1);
221      memcpy(&buffer[pos], data, len);
222      pos += len;
223      big_endian_store_16(buffer, 1, pos);
224 }
225 
226 static void goep_client_packet_init(uint16_t goep_cid, uint8_t opcode){
227     UNUSED(goep_cid);
228     goep_client_t * context = goep_client;
229     if (context->bearer_l2cap){
230         // TODO: implement l2cap variant
231     } else {
232         rfcomm_reserve_packet_buffer();
233     }
234     uint8_t * buffer = goep_client_get_outgoing_buffer(context);
235     buffer[0] = opcode;
236     big_endian_store_16(buffer, 1, 3);
237     // store opcode for parsing of response
238     context->obex_opcode = opcode;
239 }
240 
241 static void goep_client_packet_add_connection_id(uint16_t goep_cid){
242     UNUSED(goep_cid);
243     goep_client_t * context = goep_client;
244     // add connection_id header if set, must be first header if used
245     if (context->obex_connection_id != OBEX_CONNECTION_ID_INVALID){
246         uint8_t header[5];
247         header[0] = OBEX_HEADER_CONNECTION_ID;
248         big_endian_store_32(header, 1, context->obex_connection_id);
249         goep_client_packet_append(&header[0], sizeof(header));
250     }
251 }
252 
253 void goep_client_init(void){
254     memset(goep_client, 0, sizeof(goep_client_t));
255     goep_client->state = GOEP_INIT;
256     goep_client->cid = 1;
257     goep_client->obex_connection_id = OBEX_CONNECTION_ID_INVALID;
258 }
259 
260 uint8_t goep_client_create_connection(btstack_packet_handler_t handler, bd_addr_t addr, uint16_t uuid, uint16_t * out_cid){
261     goep_client_t * context = goep_client;
262     if (context->state != GOEP_INIT) return BTSTACK_MEMORY_ALLOC_FAILED;
263     context->client_handler = handler;
264     context->state = GOEP_W4_SDP;
265     memcpy(context->bd_addr, addr, 6);
266     sdp_client_query_rfcomm_channel_and_name_for_uuid(&goep_client_handle_query_rfcomm_event, context->bd_addr, uuid);
267     *out_cid = context->cid;
268     return 0;
269 }
270 
271 uint8_t goep_client_disconnect(uint16_t goep_cid){
272     UNUSED(goep_cid);
273     goep_client_t * context = goep_client;
274     rfcomm_disconnect(context->bearer_cid);
275     return 0;
276 }
277 
278 void goep_client_set_connection_id(uint16_t goep_cid, uint32_t connection_id){
279     UNUSED(goep_cid);
280     goep_client_t * context = goep_client;
281     context->obex_connection_id = connection_id;
282 }
283 
284 uint8_t goep_client_get_request_opcode(uint16_t goep_cid){
285     UNUSED(goep_cid);
286     goep_client_t * context = goep_client;
287     return context->obex_opcode;
288 }
289 
290 void goep_client_request_can_send_now(uint16_t goep_cid){
291     UNUSED(goep_cid);
292     goep_client_t * context = goep_client;
293     if (context->bearer_l2cap){
294         // TODO: implement l2cap variant
295     } else {
296         rfcomm_request_can_send_now_event(context->bearer_cid);
297     }
298 }
299 
300 void goep_client_create_connect_request(uint16_t goep_cid, uint8_t obex_version_number, uint8_t flags, uint16_t maximum_obex_packet_length){
301     UNUSED(goep_cid);
302     goep_client_t * context = goep_client;
303     goep_client_packet_init(goep_cid, OBEX_OPCODE_CONNECT);
304     uint8_t fields[4];
305     fields[0] = obex_version_number;
306     fields[1] = flags;
307     // workaround: limit OBEX packet len to RFCOMM MTU to avoid handling of fragemented packets
308     maximum_obex_packet_length = btstack_min(maximum_obex_packet_length, context->bearer_mtu);
309     big_endian_store_16(fields, 2, maximum_obex_packet_length);
310     goep_client_packet_append(&fields[0], sizeof(fields));
311 }
312 
313 void goep_client_create_get_request(uint16_t goep_cid){
314     UNUSED(goep_cid);
315     goep_client_packet_init(goep_cid, OBEX_OPCODE_GET | OBEX_OPCODE_FINAL_BIT_MASK);
316     goep_client_packet_add_connection_id(goep_cid);
317 }
318 
319 void goep_client_create_set_path_request(uint16_t goep_cid, uint8_t flags){
320     UNUSED(goep_cid);
321     goep_client_packet_init(goep_cid, OBEX_OPCODE_SETPATH);
322     uint8_t fields[2];
323     fields[0] = flags;
324     fields[1] = 0;  // reserved
325     goep_client_packet_append(&fields[0], sizeof(fields));
326     goep_client_packet_add_connection_id(goep_cid);
327 }
328 
329 static void goep_client_add_header(uint16_t goep_cid, uint8_t header_type, uint16_t header_length, const uint8_t * header_data){
330     UNUSED(goep_cid);
331     uint8_t header[3];
332     header[0] = header_type;
333     big_endian_store_16(header, 1, 1 + 2 + header_length);
334     goep_client_packet_append(&header[0], sizeof(header));
335     goep_client_packet_append(header_data, header_length);
336 }
337 
338 void goep_client_add_header_target(uint16_t goep_cid, uint16_t length, const uint8_t * target){
339     goep_client_add_header(goep_cid, OBEX_HEADER_TARGET, length,  target);
340 }
341 
342 void goep_client_add_header_application_parameters(uint16_t goep_cid, uint16_t length, const uint8_t * data){
343     goep_client_add_header(goep_cid, OBEX_HEADER_APPLICATION_PARAMETERS, length,  data);
344 }
345 
346 void goep_client_add_header_name(uint16_t goep_cid, const char * name){
347     UNUSED(goep_cid);
348     goep_client_t * context = goep_client;
349     int len_incl_zero = strlen(name) + 1;
350     uint8_t * buffer = goep_client_get_outgoing_buffer(context);
351     uint16_t pos = big_endian_read_16(buffer, 1);
352     buffer[pos++] = OBEX_HEADER_NAME;
353     big_endian_store_16(buffer, pos, 1 + 2 + len_incl_zero*2);
354     pos += 2;
355     int i;
356     // @note name[len] == 0
357     for (i = 0 ; i < len_incl_zero ; i++){
358         buffer[pos++] = 0;
359         buffer[pos++] = *name++;
360     }
361     big_endian_store_16(buffer, 1, pos);
362  }
363 
364 void goep_client_add_header_type(uint16_t goep_cid, const char * type){
365     UNUSED(goep_cid);
366     uint8_t header[3];
367     header[0] = OBEX_HEADER_TYPE;
368     int len_incl_zero = strlen(type) + 1;
369     big_endian_store_16(header, 1, 1 + 2 + len_incl_zero);
370     goep_client_packet_append(&header[0], sizeof(header));
371     goep_client_packet_append((const uint8_t*)type, len_incl_zero);
372 }
373 
374 int goep_client_execute(uint16_t goep_cid){
375     UNUSED(goep_cid);
376     goep_client_t * context = goep_client;
377     uint8_t * buffer = goep_client_get_outgoing_buffer(context);
378     uint16_t pos = big_endian_read_16(buffer, 1);
379     if (context->bearer_l2cap){
380         // TODO: implement l2cap variant
381         return -1;
382     } else {
383         return rfcomm_send_prepared(context->bearer_cid, pos);
384     }
385 }
386