xref: /btstack/example/pbap_client_demo.c (revision fcd55a0b5a8c0d39e1cebac18e790e1dbf38bd64)
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__ "pbap_client_demo.c"
39 
40 // *****************************************************************************
41 /* EXAMPLE_START(pbap_client_demo): PBAP Client - Get Contacts from Phonebook Server
42  *
43  * @text Note: The Bluetooth address of the remote Phonbook server is hardcoded.
44  * Change it before running example, then use the UI to connect to it, to set and
45  * query contacts.
46  */
47 // *****************************************************************************
48 
49 
50 #include "btstack_config.h"
51 
52 #include <stdint.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 
57 #include "btstack.h"
58 
59 #ifdef HAVE_BTSTACK_STDIN
60 #include "btstack_stdin.h"
61 #endif
62 
63 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
64 
65 static bd_addr_t    remote_addr;
66 // MBP2016 "F4-0F-24-3B-1B-E1"
67 // Nexus 7 "30-85-A9-54-2E-78"
68 // iPhone SE "BC:EC:5D:E6:15:03"
69 // PTS "001BDC080AA5"
70 static  char * remote_addr_string = "DC:52:85:B4:AD:2B ";
71 
72 static char * phone_number = "911";
73 
74 static const char * pb_name   = "pb";
75 static const char * fav_name  = "fav";
76 static const char * ich_name  = "ich";
77 static const char * och_name  = "och";
78 static const char * mch_name  = "mch";
79 static const char * cch_name  = "cch";
80 static const char * spd_name  = "spd";
81 
82 static const char * phonebook_name;
83 static char phonebook_folder[30];
84 static char phonebook_path[30];
85 static enum {
86     PBAP_PATH_ROOT,
87     PBAP_PATH_FOLDER,
88     PBAP_PATH_PHONEBOOK
89 } pbap_client_demo_path_type;
90 
91 static btstack_packet_callback_registration_t hci_event_callback_registration;
92 static uint16_t pbap_cid;
93 
94 static int sim1_selected;
95 
96 static void refresh_phonebook_folder_and_path(void){
97     snprintf(phonebook_path, sizeof(phonebook_path),   "%s%s.vcf", sim1_selected ? "SIM1/telecom/" : "telecom/", phonebook_name);
98     snprintf(phonebook_folder, sizeof(phonebook_folder), "%s%s",   sim1_selected ? "SIM1/telecom/" : "telecom/", phonebook_name);
99     printf("[-] Phonebook folder '%s'\n", phonebook_folder);
100     printf("[-] Phonebook path   '%s'\n", phonebook_path);
101 }
102 
103 static void select_phonebook(const char * phonebook){
104     phonebook_name = phonebook;
105     printf("[-] Phonebook name   '%s'\n", phonebook_name);
106     refresh_phonebook_folder_and_path();
107 }
108 
109 #ifdef HAVE_BTSTACK_STDIN
110 
111 // Testing User Interface
112 static void show_usage(void){
113     bd_addr_t iut_address;
114     gap_local_bd_addr(iut_address);
115 
116     printf("\n--- Bluetooth PBAP Client (HF) Test Console %s ---\n", bd_addr_to_str(iut_address));
117     printf("Phonebook:       '%s'\n", phonebook_folder);
118     printf("Phonebook path   '%s'\n", phonebook_path);
119     printf("\n");
120     printf("a - establish PBAP connection to %s\n", bd_addr_to_str(remote_addr));
121     printf("b - select SIM1\n");
122     printf("r - set path to '/telecom'\n");
123     printf("R - set path to '/SIM1/telecom'\n");
124     printf("u - set path to '%s'\n", phonebook_folder);
125     printf("v - set vCardSelector to N and TEL\n");
126     printf("V - set vCardSelectorOperator to AND\n");
127 
128     printf("e - select phonebook '%s'\n", pb_name);
129     printf("f - select phonebook '%s'\n", fav_name);
130     printf("i - select phonebook '%s'\n", ich_name);
131     printf("o - select phonebook '%s'\n", och_name);
132     printf("m - select phonebook '%s'\n", mch_name);
133     printf("c - select phonebook '%s'\n", cch_name);
134     printf("s - select phonebook '%s'\n", spd_name);
135 
136     printf("d - get size of    '%s'\n",             phonebook_path);
137     printf("g - pull phonebook '%s'\n",             phonebook_path);
138     printf("h - pull vCard listing '%s'\n",         phonebook_name);
139     printf("l - get owner vCard 0.vcf\n");
140     printf("j - Lookup contact with number '%s'\n", phone_number);
141     printf("t - disconnect\n");
142     printf("p - authenticate using password '0000'\n");
143     printf("r - set path to 'telecom'\n");
144     printf("x - abort operation\n");
145     printf("\n");
146 }
147 
148 static void stdin_process(char c){
149     switch (c){
150         case '\n':
151         case '\r':
152             break;
153         case 'a':
154             printf("[+] Connecting to %s...\n", bd_addr_to_str(remote_addr));
155             pbap_connect(&packet_handler, remote_addr, &pbap_cid);
156             break;
157         case 'b':
158             printf("[+] SIM1 selected'\n");
159             sim1_selected = 1;
160             refresh_phonebook_folder_and_path();
161             break;
162 
163         case 'd':
164             printf("[+] Get size of phonebook '%s'\n", phonebook_path);
165             pbap_get_phonebook_size(pbap_cid, phonebook_path);
166             break;
167         case 'g':
168             printf("[+] Pull phonebook '%s'\n", phonebook_path);
169             pbap_pull_phonebook(pbap_cid, phonebook_path);
170             break;
171         case 'h':
172             if (pbap_client_demo_path_type != PBAP_PATH_FOLDER){
173                 printf("[!] Pull vCard list requires to set phonebook folder, e.g. using 'r' command\n");
174                 break;
175             }
176             printf("[+] Pull vCard list for '%s'\n", phonebook_name);
177             pbap_pull_vcard_listing(pbap_cid, phonebook_name);
178             break;
179         case 'j':
180             if (pbap_client_demo_path_type != PBAP_PATH_PHONEBOOK){
181                 printf("[!] Pull vCard list requires to set phonenbook folder, e.g. using 'u' command\n");
182                 break;
183             }
184             printf("[+] Lookup name for number '%s'\n", phone_number);
185             pbap_lookup_by_number(pbap_cid, phone_number);
186             break;
187         case 'l':
188             if (pbap_client_demo_path_type != PBAP_PATH_PHONEBOOK){
189                 printf("[!] Pull vCard entry requires to set phonenbook folder, e.g. using 'u' command\n");
190                 break;
191             }
192             printf("[+] Pull vCard '0.vcf'\n");
193             pbap_pull_vcard_entry(pbap_cid, "0.vcf");
194             break;
195         case 'c':
196             printf("[+] Select phonebook '%s'\n", cch_name);
197             select_phonebook(cch_name);
198             break;
199         case 'e':
200             printf("[+] Select phonebook '%s'\n", pb_name);
201             select_phonebook(pb_name);
202             break;
203         case 'f':
204             printf("[+] Select phonebook '%s'\n", fav_name);
205             select_phonebook(fav_name);
206             break;
207         case 'i':
208             printf("[+] Select phonebook '%s'\n", ich_name);
209             select_phonebook(ich_name);
210             break;
211         case 'm':
212             printf("[+] Select phonebook '%s'\n", mch_name);
213             select_phonebook(mch_name);
214             break;
215         case 'o':
216             printf("[+] Select phonebook '%s'\n", och_name);
217             select_phonebook(och_name);
218             break;
219         case 's':
220             printf("[+] Select phonebook '%s'\n", spd_name);
221             select_phonebook(spd_name);
222             break;
223 
224         case 'p':
225             pbap_authentication_password(pbap_cid, "0000");
226             break;
227         case 'v':
228             printf("[+] Set vCardSelector 'N' and 'TEL'\n");
229             pbap_set_vcard_selector(pbap_cid, PBAP_PROPERTY_MASK_N | PBAP_PROPERTY_MASK_TEL);
230             break;
231         case 'V':
232             printf("[+] Set vCardSelectorOperator 'AND'\n");
233             pbap_set_vcard_selector_operator(pbap_cid, PBAP_VCARD_SELECTOR_OPERATOR_AND);
234             break;
235         case 'r':
236             printf("[+] Set path to '/telecom'\n");
237             pbap_client_demo_path_type = PBAP_PATH_FOLDER;
238             pbap_set_phonebook(pbap_cid, "telecom");
239             break;
240         case 'R':
241             printf("[+] Set path to '/SIM1/telecom'\n");
242             pbap_client_demo_path_type = PBAP_PATH_FOLDER;
243             pbap_set_phonebook(pbap_cid, "SIM1/telecom");
244             break;
245         case 'u':
246             printf("[+] Set path to '%s'\n", phonebook_folder);
247             pbap_client_demo_path_type = PBAP_PATH_PHONEBOOK;
248             pbap_set_phonebook(pbap_cid, phonebook_folder);
249             break;
250         case 'x':
251             printf("[+] Abort'\n");
252             pbap_abort(pbap_cid);
253             break;
254         case 't':
255             pbap_disconnect(pbap_cid);
256             break;
257         default:
258             show_usage();
259             break;
260     }
261 }
262 
263 // packet handler for interactive console
264 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
265     UNUSED(channel);
266     UNUSED(size);
267     int i;
268     uint8_t status;
269     char buffer[32];
270     switch (packet_type){
271         case HCI_EVENT_PACKET:
272             switch (hci_event_packet_get_type(packet)) {
273                 case BTSTACK_EVENT_STATE:
274                     // BTstack activated, get started
275                     if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
276                         show_usage();
277                     }
278                     break;
279                 case HCI_EVENT_PBAP_META:
280                     switch (hci_event_pbap_meta_get_subevent_code(packet)){
281                         case PBAP_SUBEVENT_CONNECTION_OPENED:
282                             status = pbap_subevent_connection_opened_get_status(packet);
283                             if (status){
284                                 printf("[!] Connection failed, status 0x%02x\n", status);
285                             } else {
286                                 printf("[+] Connected\n");
287                             }
288                             pbap_client_demo_path_type = PBAP_PATH_ROOT;
289                             break;
290                         case PBAP_SUBEVENT_CONNECTION_CLOSED:
291                             printf("[+] Connection closed\n");
292                             break;
293                         case PBAP_SUBEVENT_OPERATION_COMPLETED:
294                             printf("[+] Operation complete, status 0x%02x\n",
295                                    pbap_subevent_operation_completed_get_status(packet));
296                             break;
297                         case PBAP_SUBEVENT_AUTHENTICATION_REQUEST:
298                             printf("[?] Authentication requested\n");
299                             break;
300                         case PBAP_SUBEVENT_PHONEBOOK_SIZE:
301                             status = pbap_subevent_phonebook_size_get_status(packet);
302                             if (status){
303                                 printf("[!] Get Phonebook size error: 0x%02x\n", status);
304                             } else {
305                                 printf("[+] Phonebook size: %u\n", pbap_subevent_phonebook_size_get_phonebook_size(packet));
306                             }
307                             break;
308                         case PBAP_SUBEVENT_CARD_RESULT:
309                             memcpy(buffer, pbap_subevent_card_result_get_name(packet), pbap_subevent_card_result_get_name_len(packet));
310                             buffer[pbap_subevent_card_result_get_name_len(packet)] = 0;
311                             printf("[-] Name:   '%s'\n", buffer);
312                             memcpy(buffer, pbap_subevent_card_result_get_handle(packet), pbap_subevent_card_result_get_handle_len(packet));
313                             buffer[pbap_subevent_card_result_get_handle_len(packet)] = 0;
314                             printf("[-] Handle: '%s'\n", buffer);
315                             break;
316                         default:
317                             break;
318                     }
319                     break;
320                 default:
321                     break;
322             }
323             break;
324         case PBAP_DATA_PACKET:
325             for (i=0;i<size;i++){
326                 printf("%c", packet[i]);
327             }
328             break;
329         default:
330             break;
331     }
332 }
333 #else
334 
335 // packet handler for emdded system with fixed operation sequence
336 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
337     UNUSED(channel);
338     UNUSED(size);
339     int i;
340     switch (packet_type){
341         case HCI_EVENT_PACKET:
342             switch (hci_event_packet_get_type(packet)) {
343                 case BTSTACK_EVENT_STATE:
344                     // BTstack activated, get started
345                     if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
346                         printf("[+] Connect to Phone Book Server on %s\n", bd_addr_to_str(remote_addr));
347                         pbap_connect(&packet_handler, remote_addr, &pbap_cid);
348                     }
349                     break;
350                 case HCI_EVENT_PBAP_META:
351                     switch (hci_event_pbap_meta_get_subevent_code(packet)){
352                         case PBAP_SUBEVENT_CONNECTION_OPENED:
353                             printf("[+] Connected\n");
354                             printf("[+] Pull phonebook\n");
355                             pbap_pull_phonebook(pbap_cid, phonebook_path);
356                             break;
357                         case PBAP_SUBEVENT_CONNECTION_CLOSED:
358                             printf("[+] Connection closed\n");
359                             break;
360                         case PBAP_SUBEVENT_OPERATION_COMPLETED:
361                             printf("[+] Operation complete\n");
362                             printf("[+] Pull Phonebook complete\n");
363                             pbap_disconnect(pbap_cid);
364                             break;
365                         default:
366                             break;
367                     }
368                     break;
369                 default:
370                     break;
371             }
372             break;
373         case PBAP_DATA_PACKET:
374             for (i=0;i<size;i++){
375                 printf("%c", packet[i]);
376             }
377             break;
378         default:
379             break;
380     }
381 }
382 #endif
383 
384 int btstack_main(int argc, const char * argv[]);
385 int btstack_main(int argc, const char * argv[]){
386     (void)argc;
387     (void)argv;
388 
389     // init L2CAP
390     l2cap_init();
391 
392 #ifdef ENABLE_BLE
393     // Initialize LE Security Manager. Needed for cross-transport key derivation
394     sm_init();
395 #endif
396 
397     // init RFCOM
398     rfcomm_init();
399 
400     // init GOEP Client
401     goep_client_init();
402 
403     // init PBAP Client
404     pbap_client_init();
405 
406     // register for HCI events
407     hci_event_callback_registration.callback = &packet_handler;
408     hci_add_event_handler(&hci_event_callback_registration);
409 
410     sscanf_bd_addr(remote_addr_string, remote_addr);
411 
412     select_phonebook(pb_name);
413 
414 #ifdef HAVE_BTSTACK_STDIN
415     btstack_stdin_setup(stdin_process);
416 #endif
417 
418     // turn on!
419     hci_power_control(HCI_POWER_ON);
420 
421     return 0;
422 }
423 /* EXAMPLE_END */
424