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