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