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