xref: /btstack/example/pbap_client_demo.c (revision 8c94c0448e3940a70f8fcb0bae89685e5a7bafd6)
1f1b34e8dSMatthias Ringwald /*
2f1b34e8dSMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3f1b34e8dSMatthias Ringwald  *
4f1b34e8dSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5f1b34e8dSMatthias Ringwald  * modification, are permitted provided that the following conditions
6f1b34e8dSMatthias Ringwald  * are met:
7f1b34e8dSMatthias Ringwald  *
8f1b34e8dSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9f1b34e8dSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10f1b34e8dSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11f1b34e8dSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12f1b34e8dSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13f1b34e8dSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14f1b34e8dSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15f1b34e8dSMatthias Ringwald  *    from this software without specific prior written permission.
16f1b34e8dSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17f1b34e8dSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18f1b34e8dSMatthias Ringwald  *    monetary gain.
19f1b34e8dSMatthias Ringwald  *
20f1b34e8dSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21f1b34e8dSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22f1b34e8dSMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23f1b34e8dSMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24f1b34e8dSMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25f1b34e8dSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26f1b34e8dSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27f1b34e8dSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28f1b34e8dSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29f1b34e8dSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30f1b34e8dSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31f1b34e8dSMatthias Ringwald  * SUCH DAMAGE.
32f1b34e8dSMatthias Ringwald  *
33f1b34e8dSMatthias Ringwald  * Please inquire about commercial licensing options at
34f1b34e8dSMatthias Ringwald  * [email protected]
35f1b34e8dSMatthias Ringwald  *
36f1b34e8dSMatthias Ringwald  */
37ab2c6ae4SMatthias Ringwald 
38ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "pbap_client_demo.c"
39f1b34e8dSMatthias Ringwald 
4015de8206SMilanka Ringwald // *****************************************************************************
4115de8206SMilanka Ringwald /* EXAMPLE_START(pbap_client_demo): Connect to Phonebook Server and get contacts.
4258d7a529SMilanka Ringwald  *
4358d7a529SMilanka Ringwald  * @text Note: The Bluetooth address of the remote Phonbook server is hardcoded.
4458d7a529SMilanka Ringwald  * Change it before running example, then use the UI to connect to it, to set and
4558d7a529SMilanka Ringwald  * query contacts.
4615de8206SMilanka Ringwald  */
4715de8206SMilanka Ringwald // *****************************************************************************
4815de8206SMilanka Ringwald 
4915de8206SMilanka Ringwald 
50f1b34e8dSMatthias Ringwald #include "btstack_config.h"
51f1b34e8dSMatthias Ringwald 
52f1b34e8dSMatthias Ringwald #include <stdint.h>
53f1b34e8dSMatthias Ringwald #include <stdio.h>
54f1b34e8dSMatthias Ringwald #include <stdlib.h>
55f1b34e8dSMatthias Ringwald #include <string.h>
56f1b34e8dSMatthias Ringwald 
57f1b34e8dSMatthias Ringwald #include "btstack_run_loop.h"
58f1b34e8dSMatthias Ringwald #include "l2cap.h"
59cde6ece4SMatthias Ringwald #include "classic/rfcomm.h"
60f1b34e8dSMatthias Ringwald #include "btstack_event.h"
61f1b34e8dSMatthias Ringwald #include "classic/goep_client.h"
62f1b34e8dSMatthias Ringwald #include "classic/pbap_client.h"
63f1b34e8dSMatthias Ringwald 
647ea7688aSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN
657ea7688aSMatthias Ringwald #include "btstack_stdin.h"
66f1b34e8dSMatthias Ringwald #endif
67f1b34e8dSMatthias Ringwald 
68f1b34e8dSMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
69f1b34e8dSMatthias Ringwald 
70f1b34e8dSMatthias Ringwald static bd_addr_t    remote_addr;
71f1b34e8dSMatthias Ringwald // MBP2016 "F4-0F-24-3B-1B-E1"
72f1b34e8dSMatthias Ringwald // Nexus 7 "30-85-A9-54-2E-78"
73c1bc0b8eSMatthias Ringwald // iPhone SE "BC:EC:5D:E6:15:03"
74c1bc0b8eSMatthias Ringwald // PTS "001BDC080AA5"
75*8c94c044SMatthias Ringwald static  char * remote_addr_string = "001BDC080AA5";
76e03b79abSMatthias Ringwald 
77e03b79abSMatthias Ringwald static char * phone_number = "911";
78f1b34e8dSMatthias Ringwald 
79*8c94c044SMatthias Ringwald static const char * phonebook_path = "telecom/pb.vcf";
80*8c94c044SMatthias Ringwald 
81f1b34e8dSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
82f1b34e8dSMatthias Ringwald static uint16_t pbap_cid;
83f1b34e8dSMatthias Ringwald 
847ea7688aSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN
85f1b34e8dSMatthias Ringwald 
86f1b34e8dSMatthias Ringwald // Testig User Interface
87f1b34e8dSMatthias Ringwald static void show_usage(void){
88f1b34e8dSMatthias Ringwald     bd_addr_t iut_address;
89f1b34e8dSMatthias Ringwald     gap_local_bd_addr(iut_address);
90f1b34e8dSMatthias Ringwald 
91f1b34e8dSMatthias Ringwald     printf("\n--- Bluetooth PBAP Client (HF) Test Console %s ---\n", bd_addr_to_str(iut_address));
92f1b34e8dSMatthias Ringwald     printf("\n");
93f1b34e8dSMatthias Ringwald     printf("a - establish PBAP connection to %s\n", bd_addr_to_str(remote_addr));
94f1b34e8dSMatthias Ringwald     printf("b - set phonebook  '/telecom/pb'\n");
95f1b34e8dSMatthias Ringwald     printf("c - set phonebook  '/SIM1/telecom/pb'\n");
96e650aceaSMatthias Ringwald     printf("r - set path to    '/root/telecom'\n");
97*8c94c044SMatthias Ringwald     printf("d - get size of    '%s'\n", phonebook_path);
98*8c94c044SMatthias Ringwald     printf("e - pull phonebook '%s'\n", phonebook_path);
996ff88f1fSMatthias Ringwald     printf("f - disconnnect\n");
100e03b79abSMatthias Ringwald     printf("g - Lookup contact with number '%s'\n", phone_number);
101c1bc0b8eSMatthias Ringwald     printf("p - authenticate using password '0000'\n");
102e650aceaSMatthias Ringwald     printf("r - set path to 'telecom'\n");
1038c0f3635SMilanka Ringwald     printf("\n");
104f1b34e8dSMatthias Ringwald }
105f1b34e8dSMatthias Ringwald 
10695a8ee01SMatthias Ringwald static void stdin_process(char c){
10795a8ee01SMatthias Ringwald     switch (c){
108f1b34e8dSMatthias Ringwald         case 'a':
109f1b34e8dSMatthias Ringwald             printf("[+] Connecting to %s...\n", bd_addr_to_str(remote_addr));
110f1b34e8dSMatthias Ringwald             pbap_connect(&packet_handler, remote_addr, &pbap_cid);
111f1b34e8dSMatthias Ringwald             break;
112f1b34e8dSMatthias Ringwald         case 'b':
113f1b34e8dSMatthias Ringwald             printf("[+] Set Phonebook 'telecom/pb'\n");
114f1b34e8dSMatthias Ringwald             pbap_set_phonebook(pbap_cid, "telecom/pb");
115f1b34e8dSMatthias Ringwald             break;
116f1b34e8dSMatthias Ringwald         case 'c':
117f1b34e8dSMatthias Ringwald             printf("[+] Set Phonebook 'SIM1/telecom/pb'\n");
118f1b34e8dSMatthias Ringwald             pbap_set_phonebook(pbap_cid, "SIM1/telecom/pb");
119f1b34e8dSMatthias Ringwald             break;
120f1b34e8dSMatthias Ringwald         case 'd':
121*8c94c044SMatthias Ringwald             pbap_get_phonebook_size(pbap_cid, phonebook_path);
122f1b34e8dSMatthias Ringwald             break;
123f1b34e8dSMatthias Ringwald         case 'e':
124*8c94c044SMatthias Ringwald             pbap_pull_phonebook(pbap_cid, phonebook_path);
1256ff88f1fSMatthias Ringwald             break;
1266ff88f1fSMatthias Ringwald         case 'f':
127f1b34e8dSMatthias Ringwald             pbap_disconnect(pbap_cid);
128f1b34e8dSMatthias Ringwald             break;
129e03b79abSMatthias Ringwald         case 'g':
130e03b79abSMatthias Ringwald             pbap_lookup_by_number(pbap_cid, phone_number);
131e03b79abSMatthias Ringwald             break;
132c1bc0b8eSMatthias Ringwald         case 'p':
133c1bc0b8eSMatthias Ringwald             pbap_authentication_password(pbap_cid, "0000");
134c1bc0b8eSMatthias Ringwald             break;
135e650aceaSMatthias Ringwald         case 'r':
136e650aceaSMatthias Ringwald             printf("[+] Set path to 'telecom'\n");
137e650aceaSMatthias Ringwald             pbap_set_phonebook(pbap_cid, "telecom");
138e650aceaSMatthias Ringwald             break;
139f1b34e8dSMatthias Ringwald         default:
140f1b34e8dSMatthias Ringwald             show_usage();
141f1b34e8dSMatthias Ringwald             break;
142f1b34e8dSMatthias Ringwald     }
143f1b34e8dSMatthias Ringwald }
144f1b34e8dSMatthias Ringwald 
145f1b34e8dSMatthias Ringwald // packet handler for interactive console
146f1b34e8dSMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
147f1b34e8dSMatthias Ringwald     UNUSED(channel);
148f1b34e8dSMatthias Ringwald     UNUSED(size);
149f1b34e8dSMatthias Ringwald     int i;
1506ff88f1fSMatthias Ringwald     uint8_t status;
15183f1bca0SMatthias Ringwald     char buffer[32];
152f1b34e8dSMatthias Ringwald     switch (packet_type){
153f1b34e8dSMatthias Ringwald         case HCI_EVENT_PACKET:
154f1b34e8dSMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
155f1b34e8dSMatthias Ringwald                 case BTSTACK_EVENT_STATE:
156f1b34e8dSMatthias Ringwald                     // BTstack activated, get started
157f1b34e8dSMatthias Ringwald                     if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
158f1b34e8dSMatthias Ringwald                         show_usage();
159f1b34e8dSMatthias Ringwald                     }
160f1b34e8dSMatthias Ringwald                     break;
161f1b34e8dSMatthias Ringwald                 case HCI_EVENT_PBAP_META:
162f1b34e8dSMatthias Ringwald                     switch (hci_event_pbap_meta_get_subevent_code(packet)){
163f1b34e8dSMatthias Ringwald                         case PBAP_SUBEVENT_CONNECTION_OPENED:
16483f1bca0SMatthias Ringwald                             status = pbap_subevent_connection_opened_get_status(packet);
16583f1bca0SMatthias Ringwald                             if (status){
16683f1bca0SMatthias Ringwald                                 printf("[!] Connection failed, status 0x%02x\n", status);
16783f1bca0SMatthias Ringwald                             } else {
168f1b34e8dSMatthias Ringwald                                 printf("[+] Connected\n");
16983f1bca0SMatthias Ringwald                             }
170f1b34e8dSMatthias Ringwald                             break;
171f1b34e8dSMatthias Ringwald                         case PBAP_SUBEVENT_CONNECTION_CLOSED:
172f1b34e8dSMatthias Ringwald                             printf("[+] Connection closed\n");
173f1b34e8dSMatthias Ringwald                             break;
174f1b34e8dSMatthias Ringwald                         case PBAP_SUBEVENT_OPERATION_COMPLETED:
175f1b34e8dSMatthias Ringwald                             printf("[+] Operation complete\n");
176f1b34e8dSMatthias Ringwald                             break;
177c1bc0b8eSMatthias Ringwald                         case PBAP_SUBEVENT_AUTHENTICATION_REQUEST:
178c1bc0b8eSMatthias Ringwald                             printf("[?] Authentication requested\n");
179c1bc0b8eSMatthias Ringwald                             break;
1806ff88f1fSMatthias Ringwald                         case PBAP_SUBEVENT_PHONEBOOK_SIZE:
1816ff88f1fSMatthias Ringwald                             status = pbap_subevent_phonebook_size_get_status(packet);
1826ff88f1fSMatthias Ringwald                             if (status){
18383f1bca0SMatthias Ringwald                                 printf("[!] Get Phonebook size error: 0x%x\n", status);
1846ff88f1fSMatthias Ringwald                             } else {
1856ff88f1fSMatthias Ringwald                                 printf("[+] Phonebook size: %u\n", pbap_subevent_phonebook_size_get_phoneboook_size(packet));
1866ff88f1fSMatthias Ringwald                             }
1876ff88f1fSMatthias Ringwald                             break;
18883f1bca0SMatthias Ringwald                         case PBAP_SUBEVENT_CARD_RESULT:
18983f1bca0SMatthias Ringwald                             memcpy(buffer, pbap_subevent_card_result_get_name(packet), pbap_subevent_card_result_get_name_len(packet));
19083f1bca0SMatthias Ringwald                             buffer[pbap_subevent_card_result_get_name_len(packet)] = 0;
19183f1bca0SMatthias Ringwald                             printf("[-] Name:   '%s'\n", buffer);
19283f1bca0SMatthias Ringwald                             memcpy(buffer, pbap_subevent_card_result_get_handle(packet), pbap_subevent_card_result_get_handle_len(packet));
19383f1bca0SMatthias Ringwald                             buffer[pbap_subevent_card_result_get_handle_len(packet)] = 0;
19483f1bca0SMatthias Ringwald                             printf("[-] Handle: '%s'\n", buffer);
19583f1bca0SMatthias Ringwald                             break;
196f1b34e8dSMatthias Ringwald                         default:
197f1b34e8dSMatthias Ringwald                             break;
198f1b34e8dSMatthias Ringwald                     }
199f1b34e8dSMatthias Ringwald                     break;
200f1b34e8dSMatthias Ringwald                 default:
201f1b34e8dSMatthias Ringwald                     break;
202f1b34e8dSMatthias Ringwald             }
203f1b34e8dSMatthias Ringwald             break;
204f1b34e8dSMatthias Ringwald         case PBAP_DATA_PACKET:
205f1b34e8dSMatthias Ringwald             for (i=0;i<size;i++){
206f1b34e8dSMatthias Ringwald                 printf("%c", packet[i]);
207f1b34e8dSMatthias Ringwald             }
208f1b34e8dSMatthias Ringwald             break;
209f1b34e8dSMatthias Ringwald         default:
210f1b34e8dSMatthias Ringwald             break;
211f1b34e8dSMatthias Ringwald     }
212f1b34e8dSMatthias Ringwald }
213f1b34e8dSMatthias Ringwald #else
214f1b34e8dSMatthias Ringwald 
215f1b34e8dSMatthias Ringwald // packet handler for emdded system with fixed operation sequence
216f1b34e8dSMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
217f1b34e8dSMatthias Ringwald     UNUSED(channel);
218f1b34e8dSMatthias Ringwald     UNUSED(size);
219f1b34e8dSMatthias Ringwald     int i;
220f1b34e8dSMatthias Ringwald     switch (packet_type){
221f1b34e8dSMatthias Ringwald         case HCI_EVENT_PACKET:
222f1b34e8dSMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
223f1b34e8dSMatthias Ringwald                 case BTSTACK_EVENT_STATE:
224f1b34e8dSMatthias Ringwald                     // BTstack activated, get started
225f1b34e8dSMatthias Ringwald                     if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
226f1b34e8dSMatthias Ringwald                         printf("[+] Connect to Phone Book Server on %s\n", bd_addr_to_str(remote_addr));
227f1b34e8dSMatthias Ringwald                         pbap_connect(&packet_handler, remote_addr, &pbap_cid);
228f1b34e8dSMatthias Ringwald                     }
229f1b34e8dSMatthias Ringwald                     break;
230f1b34e8dSMatthias Ringwald                 case HCI_EVENT_PBAP_META:
231f1b34e8dSMatthias Ringwald                     switch (hci_event_pbap_meta_get_subevent_code(packet)){
232f1b34e8dSMatthias Ringwald                         case PBAP_SUBEVENT_CONNECTION_OPENED:
233f1b34e8dSMatthias Ringwald                             printf("[+] Connected\n");
234f1b34e8dSMatthias Ringwald                             printf("[+] Pull phonebook\n");
235f1b34e8dSMatthias Ringwald                             pbap_pull_phonebook(pbap_cid);
236f1b34e8dSMatthias Ringwald                             break;
237f1b34e8dSMatthias Ringwald                         case PBAP_SUBEVENT_CONNECTION_CLOSED:
238f1b34e8dSMatthias Ringwald                             printf("[+] Connection closed\n");
239f1b34e8dSMatthias Ringwald                             break;
240f1b34e8dSMatthias Ringwald                         case PBAP_SUBEVENT_OPERATION_COMPLETED:
241f1b34e8dSMatthias Ringwald                             printf("[+] Operation complete\n");
242f1b34e8dSMatthias Ringwald                             printf("[+] Pull Phonebook complete\n");
243f1b34e8dSMatthias Ringwald                             pbap_disconnect(pbap_cid);
244f1b34e8dSMatthias Ringwald                             break;
245f1b34e8dSMatthias Ringwald                         default:
246f1b34e8dSMatthias Ringwald                             break;
247f1b34e8dSMatthias Ringwald                     }
248f1b34e8dSMatthias Ringwald                     break;
249f1b34e8dSMatthias Ringwald                 default:
250f1b34e8dSMatthias Ringwald                     break;
251f1b34e8dSMatthias Ringwald             }
252f1b34e8dSMatthias Ringwald             break;
253f1b34e8dSMatthias Ringwald         case PBAP_DATA_PACKET:
254f1b34e8dSMatthias Ringwald             for (i=0;i<size;i++){
255f1b34e8dSMatthias Ringwald                 printf("%c", packet[i]);
256f1b34e8dSMatthias Ringwald             }
257f1b34e8dSMatthias Ringwald             break;
258f1b34e8dSMatthias Ringwald         default:
259f1b34e8dSMatthias Ringwald             break;
260f1b34e8dSMatthias Ringwald     }
261f1b34e8dSMatthias Ringwald }
262f1b34e8dSMatthias Ringwald #endif
263f1b34e8dSMatthias Ringwald 
264f1b34e8dSMatthias Ringwald int btstack_main(int argc, const char * argv[]);
265f1b34e8dSMatthias Ringwald int btstack_main(int argc, const char * argv[]){
266f1b34e8dSMatthias Ringwald 
267f1b34e8dSMatthias Ringwald     (void)argc;
268f1b34e8dSMatthias Ringwald     (void)argv;
269f1b34e8dSMatthias Ringwald 
270f1b34e8dSMatthias Ringwald     // init L2CAP
271f1b34e8dSMatthias Ringwald     l2cap_init();
272f1b34e8dSMatthias Ringwald 
273f1b34e8dSMatthias Ringwald     // init RFCOM
274f1b34e8dSMatthias Ringwald     rfcomm_init();
275f1b34e8dSMatthias Ringwald 
276f1b34e8dSMatthias Ringwald     // init GOEP Client
277f1b34e8dSMatthias Ringwald     goep_client_init();
278f1b34e8dSMatthias Ringwald 
279f1b34e8dSMatthias Ringwald     // init PBAP Client
280f1b34e8dSMatthias Ringwald     pbap_client_init();
281f1b34e8dSMatthias Ringwald 
282a4fe6467SMatthias Ringwald     // register for HCI events
283a4fe6467SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
284a4fe6467SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
285a4fe6467SMatthias Ringwald 
286a4fe6467SMatthias Ringwald     sscanf_bd_addr(remote_addr_string, remote_addr);
287a4fe6467SMatthias Ringwald 
2887ea7688aSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN
289f1b34e8dSMatthias Ringwald     btstack_stdin_setup(stdin_process);
290f1b34e8dSMatthias Ringwald #endif
291f1b34e8dSMatthias Ringwald 
292f1b34e8dSMatthias Ringwald     // turn on!
293f1b34e8dSMatthias Ringwald     hci_power_control(HCI_POWER_ON);
294f1b34e8dSMatthias Ringwald 
295f1b34e8dSMatthias Ringwald     return 0;
296f1b34e8dSMatthias Ringwald }
29715de8206SMilanka Ringwald /* EXAMPLE_END */
298