xref: /btstack/example/pbap_client_demo.c (revision 8c94c0448e3940a70f8fcb0bae89685e5a7bafd6)
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__ "pbap_client_demo.c"
39 
40 // *****************************************************************************
41 /* EXAMPLE_START(pbap_client_demo): Connect to Phonebook Server and get contacts.
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_run_loop.h"
58 #include "l2cap.h"
59 #include "classic/rfcomm.h"
60 #include "btstack_event.h"
61 #include "classic/goep_client.h"
62 #include "classic/pbap_client.h"
63 
64 #ifdef HAVE_BTSTACK_STDIN
65 #include "btstack_stdin.h"
66 #endif
67 
68 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
69 
70 static bd_addr_t    remote_addr;
71 // MBP2016 "F4-0F-24-3B-1B-E1"
72 // Nexus 7 "30-85-A9-54-2E-78"
73 // iPhone SE "BC:EC:5D:E6:15:03"
74 // PTS "001BDC080AA5"
75 static  char * remote_addr_string = "001BDC080AA5";
76 
77 static char * phone_number = "911";
78 
79 static const char * phonebook_path = "telecom/pb.vcf";
80 
81 static btstack_packet_callback_registration_t hci_event_callback_registration;
82 static uint16_t pbap_cid;
83 
84 #ifdef HAVE_BTSTACK_STDIN
85 
86 // Testig User Interface
87 static void show_usage(void){
88     bd_addr_t iut_address;
89     gap_local_bd_addr(iut_address);
90 
91     printf("\n--- Bluetooth PBAP Client (HF) Test Console %s ---\n", bd_addr_to_str(iut_address));
92     printf("\n");
93     printf("a - establish PBAP connection to %s\n", bd_addr_to_str(remote_addr));
94     printf("b - set phonebook  '/telecom/pb'\n");
95     printf("c - set phonebook  '/SIM1/telecom/pb'\n");
96     printf("r - set path to    '/root/telecom'\n");
97     printf("d - get size of    '%s'\n", phonebook_path);
98     printf("e - pull phonebook '%s'\n", phonebook_path);
99     printf("f - disconnnect\n");
100     printf("g - Lookup contact with number '%s'\n", phone_number);
101     printf("p - authenticate using password '0000'\n");
102     printf("r - set path to 'telecom'\n");
103     printf("\n");
104 }
105 
106 static void stdin_process(char c){
107     switch (c){
108         case 'a':
109             printf("[+] Connecting to %s...\n", bd_addr_to_str(remote_addr));
110             pbap_connect(&packet_handler, remote_addr, &pbap_cid);
111             break;
112         case 'b':
113             printf("[+] Set Phonebook 'telecom/pb'\n");
114             pbap_set_phonebook(pbap_cid, "telecom/pb");
115             break;
116         case 'c':
117             printf("[+] Set Phonebook 'SIM1/telecom/pb'\n");
118             pbap_set_phonebook(pbap_cid, "SIM1/telecom/pb");
119             break;
120         case 'd':
121             pbap_get_phonebook_size(pbap_cid, phonebook_path);
122             break;
123         case 'e':
124             pbap_pull_phonebook(pbap_cid, phonebook_path);
125             break;
126         case 'f':
127             pbap_disconnect(pbap_cid);
128             break;
129         case 'g':
130             pbap_lookup_by_number(pbap_cid, phone_number);
131             break;
132         case 'p':
133             pbap_authentication_password(pbap_cid, "0000");
134             break;
135         case 'r':
136             printf("[+] Set path to 'telecom'\n");
137             pbap_set_phonebook(pbap_cid, "telecom");
138             break;
139         default:
140             show_usage();
141             break;
142     }
143 }
144 
145 // packet handler for interactive console
146 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
147     UNUSED(channel);
148     UNUSED(size);
149     int i;
150     uint8_t status;
151     char buffer[32];
152     switch (packet_type){
153         case HCI_EVENT_PACKET:
154             switch (hci_event_packet_get_type(packet)) {
155                 case BTSTACK_EVENT_STATE:
156                     // BTstack activated, get started
157                     if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
158                         show_usage();
159                     }
160                     break;
161                 case HCI_EVENT_PBAP_META:
162                     switch (hci_event_pbap_meta_get_subevent_code(packet)){
163                         case PBAP_SUBEVENT_CONNECTION_OPENED:
164                             status = pbap_subevent_connection_opened_get_status(packet);
165                             if (status){
166                                 printf("[!] Connection failed, status 0x%02x\n", status);
167                             } else {
168                                 printf("[+] Connected\n");
169                             }
170                             break;
171                         case PBAP_SUBEVENT_CONNECTION_CLOSED:
172                             printf("[+] Connection closed\n");
173                             break;
174                         case PBAP_SUBEVENT_OPERATION_COMPLETED:
175                             printf("[+] Operation complete\n");
176                             break;
177                         case PBAP_SUBEVENT_AUTHENTICATION_REQUEST:
178                             printf("[?] Authentication requested\n");
179                             break;
180                         case PBAP_SUBEVENT_PHONEBOOK_SIZE:
181                             status = pbap_subevent_phonebook_size_get_status(packet);
182                             if (status){
183                                 printf("[!] Get Phonebook size error: 0x%x\n", status);
184                             } else {
185                                 printf("[+] Phonebook size: %u\n", pbap_subevent_phonebook_size_get_phoneboook_size(packet));
186                             }
187                             break;
188                         case PBAP_SUBEVENT_CARD_RESULT:
189                             memcpy(buffer, pbap_subevent_card_result_get_name(packet), pbap_subevent_card_result_get_name_len(packet));
190                             buffer[pbap_subevent_card_result_get_name_len(packet)] = 0;
191                             printf("[-] Name:   '%s'\n", buffer);
192                             memcpy(buffer, pbap_subevent_card_result_get_handle(packet), pbap_subevent_card_result_get_handle_len(packet));
193                             buffer[pbap_subevent_card_result_get_handle_len(packet)] = 0;
194                             printf("[-] Handle: '%s'\n", buffer);
195                             break;
196                         default:
197                             break;
198                     }
199                     break;
200                 default:
201                     break;
202             }
203             break;
204         case PBAP_DATA_PACKET:
205             for (i=0;i<size;i++){
206                 printf("%c", packet[i]);
207             }
208             break;
209         default:
210             break;
211     }
212 }
213 #else
214 
215 // packet handler for emdded system with fixed operation sequence
216 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
217     UNUSED(channel);
218     UNUSED(size);
219     int i;
220     switch (packet_type){
221         case HCI_EVENT_PACKET:
222             switch (hci_event_packet_get_type(packet)) {
223                 case BTSTACK_EVENT_STATE:
224                     // BTstack activated, get started
225                     if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
226                         printf("[+] Connect to Phone Book Server on %s\n", bd_addr_to_str(remote_addr));
227                         pbap_connect(&packet_handler, remote_addr, &pbap_cid);
228                     }
229                     break;
230                 case HCI_EVENT_PBAP_META:
231                     switch (hci_event_pbap_meta_get_subevent_code(packet)){
232                         case PBAP_SUBEVENT_CONNECTION_OPENED:
233                             printf("[+] Connected\n");
234                             printf("[+] Pull phonebook\n");
235                             pbap_pull_phonebook(pbap_cid);
236                             break;
237                         case PBAP_SUBEVENT_CONNECTION_CLOSED:
238                             printf("[+] Connection closed\n");
239                             break;
240                         case PBAP_SUBEVENT_OPERATION_COMPLETED:
241                             printf("[+] Operation complete\n");
242                             printf("[+] Pull Phonebook complete\n");
243                             pbap_disconnect(pbap_cid);
244                             break;
245                         default:
246                             break;
247                     }
248                     break;
249                 default:
250                     break;
251             }
252             break;
253         case PBAP_DATA_PACKET:
254             for (i=0;i<size;i++){
255                 printf("%c", packet[i]);
256             }
257             break;
258         default:
259             break;
260     }
261 }
262 #endif
263 
264 int btstack_main(int argc, const char * argv[]);
265 int btstack_main(int argc, const char * argv[]){
266 
267     (void)argc;
268     (void)argv;
269 
270     // init L2CAP
271     l2cap_init();
272 
273     // init RFCOM
274     rfcomm_init();
275 
276     // init GOEP Client
277     goep_client_init();
278 
279     // init PBAP Client
280     pbap_client_init();
281 
282     // register for HCI events
283     hci_event_callback_registration.callback = &packet_handler;
284     hci_add_event_handler(&hci_event_callback_registration);
285 
286     sscanf_bd_addr(remote_addr_string, remote_addr);
287 
288 #ifdef HAVE_BTSTACK_STDIN
289     btstack_stdin_setup(stdin_process);
290 #endif
291 
292     // turn on!
293     hci_power_control(HCI_POWER_ON);
294 
295     return 0;
296 }
297 /* EXAMPLE_END */
298