source: asp3_tinet_ecnl_arm/trunk/btstack/src/sdp_query_rfcomm.c@ 374

Last change on this file since 374 was 374, checked in by coas-nagasima, 5 years ago

mbed関連を更新
シリアルドライバをmbedのHALを使うよう変更
ファイルディスクリプタの処理を更新

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 10.2 KB
Line 
1/*
2 * Copyright (C) 2009-2013 by Matthias Ringwald
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 MATTHIAS RINGWALD 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 btstack@ringwald.ch
34 *
35 */
36
37/*
38 * sdp_rfcomm_query.c
39 */
40
41#include <stdint.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45
46#include <btstack/hci_cmds.h>
47#include <btstack/sdp_util.h>
48
49#include "sdp_client.h"
50#include "sdp_query_rfcomm.h"
51
52static void dummy_notify_app(sdp_query_event_t* event, void * context);
53
54typedef enum {
55 GET_PROTOCOL_LIST_LENGTH = 1,
56 GET_PROTOCOL_LENGTH,
57 GET_PROTOCOL_ID_HEADER_LENGTH,
58 GET_PROTOCOL_ID,
59 GET_PROTOCOL_VALUE_LENGTH,
60 GET_PROTOCOL_VALUE
61} pdl_state_t;
62
63
64// higher layer query - get rfcomm channel and name
65
66const uint8_t des_attributeIDList[] = { 0x35, 0x05, 0x0A, 0x00, 0x01, 0x01, 0x00}; // Arribute: 0x0001 - 0x0100
67
68static uint8_t des_serviceSearchPattern[5] = {0x35, 0x03, 0x19, 0x00, 0x00};
69
70static uint8_t sdp_service_name[SDP_SERVICE_NAME_LEN+1];
71static uint8_t sdp_rfcom_channel_nr = 0;
72static uint8_t sdp_service_name_header_size;
73
74static pdl_state_t pdl_state = GET_PROTOCOL_LIST_LENGTH;
75static int protocol_value_bytes_received = 0;
76static uint16_t protocol_id = 0;
77static int protocol_offset;
78static int protocol_size;
79static int protocol_id_bytes_to_read;
80static int protocol_value_size;
81static de_state_t de_header_state;
82static de_state_t sn_de_header_state;
83
84static void *sdp_app_context;
85static void (*sdp_app_callback)(sdp_query_event_t * event, void * context) = dummy_notify_app;
86
87//
88
89static void dummy_notify_app(sdp_query_event_t* event, void * context){}
90
91void sdp_query_rfcomm_register_callback(void (*sdp_callback)(sdp_query_event_t* event, void * context), void * context){
92 sdp_app_callback = dummy_notify_app;
93 if (sdp_callback != NULL){
94 sdp_app_callback = sdp_callback;
95 }
96 sdp_app_context = context;
97}
98
99void handleProtocolDescriptorListData(uint32_t attribute_value_length, uint32_t data_offset, uint8_t data){
100 // init state on first byte
101 if (data_offset == 0){
102 pdl_state = GET_PROTOCOL_LIST_LENGTH;
103 }
104
105 // log_info("handleProtocolDescriptorListData (%u,%u) %02x", attribute_value_length, data_offset, data);
106
107 switch(pdl_state){
108
109 case GET_PROTOCOL_LIST_LENGTH:
110 if (!de_state_size(data, &de_header_state)) break;
111 // log_info(" query: PD List payload is %d bytes.", de_header_state.de_size);
112 // log_info(" query: PD List offset %u, list size %u", de_header_state.de_offset, de_header_state.de_size);
113
114 pdl_state = GET_PROTOCOL_LENGTH;
115 break;
116
117 case GET_PROTOCOL_LENGTH:
118 // check size
119 if (!de_state_size(data, &de_header_state)) break;
120 // log_info(" query: PD Record payload is %d bytes.", de_header_state.de_size);
121
122 // cache protocol info
123 protocol_offset = de_header_state.de_offset;
124 protocol_size = de_header_state.de_size;
125
126 pdl_state = GET_PROTOCOL_ID_HEADER_LENGTH;
127 break;
128
129 case GET_PROTOCOL_ID_HEADER_LENGTH:
130 protocol_offset++;
131 if (!de_state_size(data, &de_header_state)) break;
132
133 protocol_id = 0;
134 protocol_id_bytes_to_read = de_header_state.de_size;
135 // log_info(" query: ID data is stored in %d bytes.", protocol_id_bytes_to_read);
136 pdl_state = GET_PROTOCOL_ID;
137
138 break;
139
140 case GET_PROTOCOL_ID:
141 protocol_offset++;
142
143 protocol_id = (protocol_id << 8) | data;
144 protocol_id_bytes_to_read--;
145 if (protocol_id_bytes_to_read > 0) break;
146
147 // log_info(" query: Protocol ID: %04x.", protocol_id);
148
149 if (protocol_offset >= protocol_size){
150 pdl_state = GET_PROTOCOL_LENGTH;
151 // log_info(" query: Get next protocol");
152 break;
153 }
154
155 pdl_state = GET_PROTOCOL_VALUE_LENGTH;
156 protocol_value_bytes_received = 0;
157 break;
158
159 case GET_PROTOCOL_VALUE_LENGTH:
160 protocol_offset++;
161
162 if (!de_state_size(data, &de_header_state)) break;
163
164 protocol_value_size = de_header_state.de_size;
165 pdl_state = GET_PROTOCOL_VALUE;
166 sdp_rfcom_channel_nr = 0;
167 break;
168
169 case GET_PROTOCOL_VALUE:
170 protocol_offset++;
171 protocol_value_bytes_received++;
172
173 // log_info(" query: protocol_value_bytes_received %u, protocol_value_size %u", protocol_value_bytes_received, protocol_value_size);
174
175 if (protocol_value_bytes_received < protocol_value_size) break;
176
177 if (protocol_id == 0x0003){
178 // log_info("\n\n ******* Data ***** %02x\n\n", data);
179 sdp_rfcom_channel_nr = data;
180 }
181
182 // log_info(" query: protocol done");
183 // log_info(" query: Protocol offset %u, protocol size %u", protocol_offset, protocol_size);
184
185 if (protocol_offset >= protocol_size) {
186 pdl_state = GET_PROTOCOL_LENGTH;
187 break;
188
189 }
190 pdl_state = GET_PROTOCOL_ID_HEADER_LENGTH;
191 // log_info(" query: Get next protocol");
192 break;
193 default:
194 break;
195 }
196}
197
198void handleServiceNameData(uint32_t attribute_value_length, uint32_t data_offset, uint8_t data){
199 int name_len;
200 int name_pos;
201
202 // Get Header Len
203 if (data_offset == 0){
204 de_state_size(data, &sn_de_header_state);
205 sdp_service_name_header_size = sn_de_header_state.addon_header_bytes + 1;
206 return;
207 }
208
209 // Get Header
210 if (data_offset < sdp_service_name_header_size){
211 de_state_size(data, &sn_de_header_state);
212 return;
213 }
214
215 // Process payload
216 name_len = attribute_value_length - sdp_service_name_header_size;
217 name_pos = data_offset - sdp_service_name_header_size;
218
219 if (name_pos < SDP_SERVICE_NAME_LEN){
220 sdp_service_name[name_pos] = data;
221 name_pos++;
222
223 // terminate if name complete
224 if (name_pos >= name_len){
225 sdp_service_name[name_pos] = 0;
226 }
227
228 // terminate if buffer full
229 if (name_pos == SDP_SERVICE_NAME_LEN){
230 sdp_service_name[name_pos] = 0;
231 }
232 }
233
234 // notify on last char
235 if (data_offset == attribute_value_length - 1 && sdp_rfcom_channel_nr!=0){
236 sdp_query_rfcomm_service_event_t value_event = {
237 SDP_QUERY_RFCOMM_SERVICE,
238 sdp_rfcom_channel_nr,
239 (uint8_t *) sdp_service_name
240 };
241 (*sdp_app_callback)((sdp_query_event_t*)&value_event, sdp_app_context);
242 sdp_rfcom_channel_nr = 0;
243 }
244}
245
246
247static void handle_sdp_parser_event(sdp_query_event_t * event){
248 sdp_query_attribute_value_event_t * ve;
249 switch (event->type){
250 case SDP_EVENT_QUERY_ATTRIBUTE_VALUE:
251 ve = (sdp_query_attribute_value_event_t*) event;
252 // log_info("handle_sdp_parser_event [ AID, ALen, DOff, Data] : [%x, %u, %u] BYTE %02x",
253 // ve->attribute_id, ve->attribute_length, ve->data_offset, ve->data);
254
255 switch (ve->attribute_id){
256 case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST:
257 // find rfcomm channel
258 handleProtocolDescriptorListData(ve->attribute_length, ve->data_offset, ve->data);
259 break;
260 case 0x0100:
261 // get service name
262 handleServiceNameData(ve->attribute_length, ve->data_offset, ve->data);
263 break;
264 default:
265 // give up
266 return;
267 }
268 break;
269 case SDP_EVENT_QUERY_COMPLETE:
270 (*sdp_app_callback)(event, sdp_app_context);
271 break;
272 }
273 // insert higher level code HERE
274}
275
276void sdp_query_rfcomm_init(){
277 // init
278 de_state_init(&de_header_state);
279 de_state_init(&sn_de_header_state);
280 pdl_state = GET_PROTOCOL_LIST_LENGTH;
281 protocol_offset = 0;
282 sdp_parser_register_callback(handle_sdp_parser_event);
283}
284
285
286void sdp_query_rfcomm_channel_and_name_for_search_pattern(bd_addr_t remote, uint8_t * serviceSearchPattern){
287 sdp_parser_init();
288 sdp_query_rfcomm_init();
289 sdp_client_query(remote, serviceSearchPattern, (uint8_t*)&des_attributeIDList[0]);
290}
291
292void sdp_query_rfcomm_channel_and_name_for_uuid(bd_addr_t remote, uint16_t uuid){
293 net_store_16(des_serviceSearchPattern, 3, uuid);
294 sdp_query_rfcomm_channel_and_name_for_search_pattern(remote, (uint8_t*)des_serviceSearchPattern);
295}
296
297void sdp_query_rfcomm_deregister_callback(){
298 sdp_query_rfcomm_init();
299 sdp_app_callback = dummy_notify_app;
300 sdp_app_context = NULL;
301}
Note: See TracBrowser for help on using the repository browser.