xref: /btstack/src/classic/goep_client.c (revision 057d63da777cbb6bf71841e5c953f6971513bba2)
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 #include "l2cap.h"
57 
58 //------------------------------------------------------------------------------------------------------------
59 // goep_client.c
60 //
61 
62 typedef enum {
63     GOEP_INIT,
64     GOEP_W4_SDP,
65     GOEP_W4_CONNECTION,
66     GOEP_CONNECTED,
67 } goep_state_t;
68 
69 typedef struct {
70     uint16_t         cid;
71     goep_state_t     state;
72     bd_addr_t        bd_addr;
73     hci_con_handle_t con_handle;
74     uint8_t          incoming;
75     uint8_t          bearer_l2cap;
76     uint16_t         bearer_port;   // l2cap: psm, rfcomm: channel nr
77     uint16_t         bearer_cid;
78     uint16_t         bearer_mtu;
79 
80     uint8_t          obex_opcode;
81     uint32_t         obex_connection_id;
82     int              obex_connection_id_set;
83 
84     btstack_packet_handler_t client_handler;
85 } goep_client_t;
86 
87 static goep_client_t _goep_client;
88 static goep_client_t * goep_client = &_goep_client;
89 
90 static inline void goep_client_emit_connected_event(goep_client_t * context, uint8_t status){
91     uint8_t event[22];
92     int pos = 0;
93     event[pos++] = HCI_EVENT_GOEP_META;
94     pos++;  // skip len
95     event[pos++] = GOEP_SUBEVENT_CONNECTION_OPENED;
96     little_endian_store_16(event,pos,context->cid);
97     pos+=2;
98     event[pos++] = status;
99     memcpy(&event[pos], context->bd_addr, 6);
100     pos += 6;
101     little_endian_store_16(event,pos,context->con_handle);
102     pos += 2;
103     event[pos++] = context->incoming;
104     event[1] = pos - 2;
105     if (pos != sizeof(event)) log_error("goep_client_emit_connected_event size %u", pos);
106     context->client_handler(HCI_EVENT_PACKET, context->cid, &event[0], pos);
107 }
108 
109 static inline void goep_client_emit_connection_closed_event(goep_client_t * context){
110     uint8_t event[5];
111     int pos = 0;
112     event[pos++] = HCI_EVENT_GOEP_META;
113     pos++;  // skip len
114     event[pos++] = GOEP_SUBEVENT_CONNECTION_CLOSED;
115     little_endian_store_16(event,pos,context->cid);
116     pos+=2;
117     event[1] = pos - 2;
118     if (pos != sizeof(event)) log_error("goep_client_emit_connection_closed_event size %u", pos);
119     context->client_handler(HCI_EVENT_PACKET, context->cid, &event[0], pos);
120 }
121 
122 static inline void goep_client_emit_can_send_now_event(goep_client_t * context){
123     uint8_t event[5];
124     int pos = 0;
125     event[pos++] = HCI_EVENT_GOEP_META;
126     pos++;  // skip len
127     event[pos++] = GOEP_SUBEVENT_CAN_SEND_NOW;
128     little_endian_store_16(event,pos,context->cid);
129     pos+=2;
130     event[1] = pos - 2;
131     if (pos != sizeof(event)) log_error("goep_client_emit_can_send_now_event size %u", pos);
132     context->client_handler(HCI_EVENT_PACKET, context->cid, &event[0], pos);
133 }
134 
135 static void goep_client_handle_connection_opened(goep_client_t * context, uint8_t status, uint16_t mtu){
136     if (status) {
137         context->state = GOEP_INIT;
138         log_info("goep_client: open failed, status %u", status);
139     } else {
140         context->bearer_mtu = mtu;
141         context->state = GOEP_CONNECTED;
142         log_info("goep_client: connection opened. cid %u, max frame size %u", context->bearer_cid, context->bearer_mtu);
143     }
144     goep_client_emit_connected_event(context, status);
145 }
146 
147 static void goep_client_handle_connection_close(goep_client_t * context){
148     context->state = GOEP_INIT;
149     goep_client_emit_connection_closed_event(context);
150 }
151 
152 static void goep_client_packet_handler_rfcomm(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
153     UNUSED(channel);
154     UNUSED(size);
155     goep_client_t * context = goep_client;
156     switch (packet_type){
157         case HCI_EVENT_PACKET:
158             switch (hci_event_packet_get_type(packet)) {
159                 case RFCOMM_EVENT_CHANNEL_OPENED:
160                     goep_client_handle_connection_opened(context, rfcomm_event_channel_opened_get_status(packet), rfcomm_event_channel_opened_get_max_frame_size(packet));
161                     return;
162                 case RFCOMM_EVENT_CAN_SEND_NOW:
163                     goep_client_emit_can_send_now_event(context);
164                     break;
165                 case RFCOMM_EVENT_CHANNEL_CLOSED:
166                     goep_client_handle_connection_close(context);
167                     break;
168                 default:
169                     break;
170             }
171             break;
172         case RFCOMM_DATA_PACKET:
173             context->client_handler(GOEP_DATA_PACKET, context->cid, packet, size);
174             break;
175         default:
176             break;
177     }
178 }
179 
180 static void goep_client_handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
181     UNUSED(packet_type);
182     UNUSED(channel);
183     UNUSED(size);
184     goep_client_t * context = goep_client;
185     switch (packet[0]){
186         case SDP_EVENT_QUERY_RFCOMM_SERVICE:
187             context->bearer_port = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet);
188             break;
189         case SDP_EVENT_QUERY_COMPLETE:
190             if (sdp_event_query_complete_get_status(packet)){
191                 log_info("GOEP client, SDP query failed 0x%02x", sdp_event_query_complete_get_status(packet));
192                 context->state = GOEP_INIT;
193                 goep_client_emit_connected_event(goep_client, sdp_event_query_complete_get_status(packet));
194                 break;
195             }
196 
197             if (context->bearer_port == 0){
198                 log_info("Remote GOEP RFCOMM Server Channel not found");
199                 context->state = GOEP_INIT;
200                 goep_client_emit_connected_event(goep_client, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE);
201                 break;
202             }
203             log_info("Remote GOEP RFCOMM Server Channel: %u", context->bearer_port);
204             rfcomm_create_channel(&goep_client_packet_handler_rfcomm, context->bd_addr, context->bearer_port, &context->bearer_cid);
205             break;
206     }
207 }
208 
209 static uint8_t * goep_client_get_outgoing_buffer(goep_client_t * context){
210     if (context->bearer_l2cap){
211         return l2cap_get_outgoing_buffer();
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         l2cap_reserve_packet_buffer();
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         l2cap_request_can_send_now_event(context->bearer_cid);
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         return l2cap_send_prepared(context->bearer_cid, pos);
381     } else {
382         return rfcomm_send_prepared(context->bearer_cid, pos);
383     }
384 }
385