xref: /btstack/src/classic/goep_client.c (revision c37cd8f3d1350b92a2f66c31b2a5fcd75f8c91a4)
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_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
135     UNUSED(channel);
136     UNUSED(size);
137     uint8_t status;
138     switch (packet_type){
139         case HCI_EVENT_PACKET:
140             switch (hci_event_packet_get_type(packet)) {
141                 case RFCOMM_EVENT_CHANNEL_OPENED:
142                     status = rfcomm_event_channel_opened_get_status(packet);
143                     if (status) {
144                         log_info("goep_client: RFCOMM channel open failed, status %u", rfcomm_event_channel_opened_get_status(packet));
145                         goep_client->state = GOEP_INIT;
146                     } else {
147                         goep_client->bearer_mtu = rfcomm_event_channel_opened_get_max_frame_size(packet);
148                         log_info("goep_client: RFCOMM channel open succeeded. cid %u, max frame size %u", goep_client->bearer_cid, goep_client->bearer_mtu);
149                         goep_client->state = GOEP_CONNECTED;
150                     }
151                     goep_client_emit_connected_event(goep_client, status);
152                     return;
153                 case RFCOMM_EVENT_CAN_SEND_NOW:
154                     goep_client_emit_can_send_now_event(goep_client);
155                     break;
156                 case RFCOMM_CHANNEL_CLOSED:
157                     goep_client->state = GOEP_INIT;
158                     goep_client_emit_connection_closed_event(goep_client);
159                     break;
160                 default:
161                     break;
162             }
163             break;
164         case RFCOMM_DATA_PACKET:
165             goep_client->client_handler(GOEP_DATA_PACKET, goep_client->cid, packet, size);
166             break;
167         default:
168             break;
169     }
170 }
171 
172 static void goep_client_handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
173     UNUSED(packet_type);
174     UNUSED(channel);
175     UNUSED(size);
176 
177     switch (packet[0]){
178         case SDP_EVENT_QUERY_RFCOMM_SERVICE:
179             goep_client->bearer_port = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet);
180             break;
181         case SDP_EVENT_QUERY_COMPLETE:
182             if (sdp_event_query_complete_get_status(packet)){
183                 log_info("GOEP client, SDP query failed 0x%02x", sdp_event_query_complete_get_status(packet));
184                 goep_client->state = GOEP_INIT;
185                 goep_client_emit_connected_event(goep_client, sdp_event_query_complete_get_status(packet));
186                 break;
187             }
188 
189             if (goep_client->bearer_port == 0){
190                 log_info("Remote GOEP RFCOMM Server Channel not found");
191                 goep_client->state = GOEP_INIT;
192                 goep_client_emit_connected_event(goep_client, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE);
193                 break;
194             }
195             log_info("Remote GOEP RFCOMM Server Channel: %u", goep_client->bearer_port);
196             rfcomm_create_channel(&goep_client_packet_handler, goep_client->bd_addr, goep_client->bearer_port, &goep_client->bearer_cid);
197             break;
198     }
199 }
200 
201 static void goep_client_packet_append(const uint8_t * data, uint16_t len){
202      uint8_t * buffer = rfcomm_get_outgoing_buffer();
203      uint16_t pos = big_endian_read_16(buffer, 1);
204      memcpy(&buffer[pos], data, len);
205      pos += len;
206      big_endian_store_16(buffer, 1, pos);
207 }
208 
209 static void goep_client_packet_init(uint16_t goep_cid, uint8_t opcode){
210     UNUSED(goep_cid);
211     rfcomm_reserve_packet_buffer();
212     uint8_t * buffer = rfcomm_get_outgoing_buffer();
213     buffer[0] = opcode;
214     big_endian_store_16(buffer, 1, 3);
215     // store opcode for parsing of response
216     goep_client->obex_opcode = opcode;
217 }
218 
219 static void goep_client_packet_add_connection_id(uint16_t goep_cid){
220     UNUSED(goep_cid);
221     // add connection_id header if set, must be first header if used
222     if (goep_client->obex_connection_id != OBEX_CONNECTION_ID_INVALID){
223         uint8_t header[5];
224         header[0] = OBEX_HEADER_CONNECTION_ID;
225         big_endian_store_32(header, 1, goep_client->obex_connection_id);
226         goep_client_packet_append(&header[0], sizeof(header));
227     }
228 }
229 
230 void goep_client_init(void){
231     memset(goep_client, 0, sizeof(goep_client_t));
232     goep_client->state = GOEP_INIT;
233     goep_client->cid = 1;
234     goep_client->obex_connection_id = OBEX_CONNECTION_ID_INVALID;
235 }
236 
237 uint8_t goep_client_create_connection(btstack_packet_handler_t handler, bd_addr_t addr, uint16_t uuid, uint16_t * out_cid){
238     if (goep_client->state != GOEP_INIT) return BTSTACK_MEMORY_ALLOC_FAILED;
239     goep_client->client_handler = handler;
240     goep_client->state = GOEP_W4_SDP;
241     memcpy(goep_client->bd_addr, addr, 6);
242     sdp_client_query_rfcomm_channel_and_name_for_uuid(&goep_client_handle_query_rfcomm_event, goep_client->bd_addr, uuid);
243     *out_cid = goep_client->cid;
244     return 0;
245 }
246 
247 uint8_t goep_client_disconnect(uint16_t goep_cid){
248     UNUSED(goep_cid);
249     rfcomm_disconnect(goep_client->bearer_cid);
250     return 0;
251 }
252 
253 void goep_client_set_connection_id(uint16_t goep_cid, uint32_t connection_id){
254     UNUSED(goep_cid);
255     goep_client->obex_connection_id = connection_id;
256 }
257 
258 uint8_t goep_client_get_request_opcode(uint16_t goep_cid){
259     UNUSED(goep_cid);
260     return goep_client->obex_opcode;
261 }
262 
263 void goep_client_request_can_send_now(uint16_t goep_cid){
264     UNUSED(goep_cid);
265     rfcomm_request_can_send_now_event(goep_client->bearer_cid);
266 }
267 
268 void goep_client_create_connect_request(uint16_t goep_cid, uint8_t obex_version_number, uint8_t flags, uint16_t maximum_obex_packet_length){
269     UNUSED(goep_cid);
270     goep_client_packet_init(goep_cid, OBEX_OPCODE_CONNECT);
271     uint8_t fields[4];
272     fields[0] = obex_version_number;
273     fields[1] = flags;
274     // workaround: limit OBEX packet len to RFCOMM MTU to avoid handling of fragemented packets
275     maximum_obex_packet_length = btstack_min(maximum_obex_packet_length, goep_client->bearer_mtu);
276     big_endian_store_16(fields, 2, maximum_obex_packet_length);
277     goep_client_packet_append(&fields[0], sizeof(fields));
278 }
279 
280 void goep_client_create_get_request(uint16_t goep_cid){
281     UNUSED(goep_cid);
282     goep_client_packet_init(goep_cid, OBEX_OPCODE_GET | OBEX_OPCODE_FINAL_BIT_MASK);
283     goep_client_packet_add_connection_id(goep_cid);
284 }
285 
286 void goep_client_create_set_path_request(uint16_t goep_cid, uint8_t flags){
287     UNUSED(goep_cid);
288     goep_client_packet_init(goep_cid, OBEX_OPCODE_SETPATH);
289     uint8_t fields[2];
290     fields[0] = flags;
291     fields[1] = 0;  // reserved
292     goep_client_packet_append(&fields[0], sizeof(fields));
293     goep_client_packet_add_connection_id(goep_cid);
294 }
295 
296 void goep_client_add_header_target(uint16_t goep_cid, uint16_t length, const uint8_t * target){
297     UNUSED(goep_cid);
298     uint8_t header[3];
299     header[0] = OBEX_HEADER_TARGET;
300     big_endian_store_16(header, 1, 1 + 2 + length);
301     goep_client_packet_append(&header[0], sizeof(header));
302     goep_client_packet_append(target, length);
303 }
304 
305 void goep_client_add_header_name(uint16_t goep_cid, const char * name){
306     UNUSED(goep_cid);
307     int len_incl_zero = strlen(name) + 1;
308     uint8_t * buffer = rfcomm_get_outgoing_buffer();
309     uint16_t pos = big_endian_read_16(buffer, 1);
310     buffer[pos++] = OBEX_HEADER_NAME;
311     big_endian_store_16(buffer, pos, 1 + 2 + len_incl_zero*2);
312     pos += 2;
313     int i;
314     // @note name[len] == 0
315     for (i = 0 ; i < len_incl_zero ; i++){
316         buffer[pos++] = 0;
317         buffer[pos++] = *name++;
318     }
319     big_endian_store_16(buffer, 1, pos);
320  }
321 
322 void goep_client_add_header_type(uint16_t goep_cid, const char * type){
323     UNUSED(goep_cid);
324     uint8_t header[3];
325     header[0] = OBEX_HEADER_TYPE;
326     int len_incl_zero = strlen(type) + 1;
327     big_endian_store_16(header, 1, 1 + 2 + len_incl_zero);
328     goep_client_packet_append(&header[0], sizeof(header));
329     goep_client_packet_append((const uint8_t*)type, len_incl_zero);
330 }
331 
332 int goep_client_execute(uint16_t goep_cid){
333     UNUSED(goep_cid);
334     uint8_t * buffer = rfcomm_get_outgoing_buffer();
335     uint16_t pos = big_endian_read_16(buffer, 1);
336     return rfcomm_send_prepared(goep_client->bearer_cid, pos);
337 }
338