xref: /btstack/example/pbap_client_demo.c (revision ab2c6ae4b737d5e801d3defe4117331eb244ebb7)
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 #include "btstack_config.h"
41 
42 #include <stdint.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 
47 #include "btstack_run_loop.h"
48 #include "l2cap.h"
49 #include "rfcomm.h"
50 #include "btstack_event.h"
51 #include "classic/goep_client.h"
52 #include "classic/pbap_client.h"
53 
54 #ifdef HAVE_POSIX_STDIN
55 #include <unistd.h>
56 #include "stdin_support.h"
57 #endif
58 
59 #ifndef HAVE_POSIX_STDIN
60 static int step = 0;
61 #endif
62 
63 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
64 
65 static bd_addr_t    remote_addr;
66 // MBP2016 "F4-0F-24-3B-1B-E1"
67 // Nexus 7 "30-85-A9-54-2E-78"
68 static const char * remote_addr_string = "30-85-A9-54-2E-78";
69 
70 static btstack_packet_callback_registration_t hci_event_callback_registration;
71 static uint16_t pbap_cid;
72 
73 #ifdef HAVE_POSIX_STDIN
74 
75 // Testig User Interface
76 static void show_usage(void){
77     bd_addr_t iut_address;
78     gap_local_bd_addr(iut_address);
79 
80     printf("\n--- Bluetooth PBAP Client (HF) Test Console %s ---\n", bd_addr_to_str(iut_address));
81     printf("\n");
82     printf("a - establish PBAP connection to %s\n", bd_addr_to_str(remote_addr));
83     printf("b - set phonebook '/telecom/pb'\n");
84     printf("c - set phonebook '/SIM1/telecom/pb'\n");
85     printf("d - pull phonebook\n");
86     printf("e - disconnnect\n");
87     printf("---\n");
88     printf("Ctrl-c - exit\n");
89     printf("---\n");
90 }
91 
92 static void stdin_process(btstack_data_source_t *ds, btstack_data_source_callback_type_t callback_type){
93     UNUSED(ds);
94     UNUSED(callback_type);
95 
96     char cmd = btstack_stdin_read();
97 
98     switch (cmd){
99         case 'a':
100             printf("[+] Connecting to %s...\n", bd_addr_to_str(remote_addr));
101             pbap_connect(&packet_handler, remote_addr, &pbap_cid);
102             break;
103         case 'b':
104             printf("[+] Set Phonebook 'telecom/pb'\n");
105             pbap_set_phonebook(pbap_cid, "telecom/pb");
106             break;
107         case 'c':
108             printf("[+] Set Phonebook 'SIM1/telecom/pb'\n");
109             pbap_set_phonebook(pbap_cid, "SIM1/telecom/pb");
110             break;
111         case 'd':
112             pbap_pull_phonebook(pbap_cid);
113             break;
114         case 'e':
115             pbap_disconnect(pbap_cid);
116             break;
117         default:
118             show_usage();
119             break;
120     }
121 }
122 
123 // packet handler for interactive console
124 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
125     UNUSED(channel);
126     UNUSED(size);
127     int i;
128     switch (packet_type){
129         case HCI_EVENT_PACKET:
130             switch (hci_event_packet_get_type(packet)) {
131                 case BTSTACK_EVENT_STATE:
132                     // BTstack activated, get started
133                     if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
134                         show_usage();
135                     }
136                     break;
137                 case HCI_EVENT_PBAP_META:
138                     switch (hci_event_pbap_meta_get_subevent_code(packet)){
139                         case PBAP_SUBEVENT_CONNECTION_OPENED:
140                             printf("[+] Connected\n");
141                             break;
142                         case PBAP_SUBEVENT_CONNECTION_CLOSED:
143                             printf("[+] Connection closed\n");
144                             break;
145                         case PBAP_SUBEVENT_OPERATION_COMPLETED:
146                             printf("[+] Operation complete\n");
147                             break;
148                         default:
149                             break;
150                     }
151                     break;
152                 default:
153                     break;
154             }
155             break;
156         case PBAP_DATA_PACKET:
157             for (i=0;i<size;i++){
158                 printf("%c", packet[i]);
159             }
160             break;
161         default:
162             break;
163     }
164 }
165 #else
166 
167 // packet handler for emdded system with fixed operation sequence
168 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
169     UNUSED(channel);
170     UNUSED(size);
171     int i;
172     switch (packet_type){
173         case HCI_EVENT_PACKET:
174             switch (hci_event_packet_get_type(packet)) {
175                 case BTSTACK_EVENT_STATE:
176                     // BTstack activated, get started
177                     if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
178                         printf("[+] Connect to Phone Book Server on %s\n", bd_addr_to_str(remote_addr));
179                         pbap_connect(&packet_handler, remote_addr, &pbap_cid);
180                     }
181                     break;
182                 case HCI_EVENT_PBAP_META:
183                     switch (hci_event_pbap_meta_get_subevent_code(packet)){
184                         case PBAP_SUBEVENT_CONNECTION_OPENED:
185                             printf("[+] Connected\n");
186                             printf("[+] Pull phonebook\n");
187                             pbap_pull_phonebook(pbap_cid);
188                             break;
189                         case PBAP_SUBEVENT_CONNECTION_CLOSED:
190                             printf("[+] Connection closed\n");
191                             break;
192                         case PBAP_SUBEVENT_OPERATION_COMPLETED:
193                             printf("[+] Operation complete\n");
194                             printf("[+] Pull Phonebook complete\n");
195                             pbap_disconnect(pbap_cid);
196                             break;
197                         default:
198                             break;
199                     }
200                     break;
201                 default:
202                     break;
203             }
204             break;
205         case PBAP_DATA_PACKET:
206             for (i=0;i<size;i++){
207                 printf("%c", packet[i]);
208             }
209             break;
210         default:
211             break;
212     }
213 }
214 #endif
215 
216 int btstack_main(int argc, const char * argv[]);
217 int btstack_main(int argc, const char * argv[]){
218 
219     (void)argc;
220     (void)argv;
221 
222     sscanf_bd_addr(remote_addr_string, remote_addr);
223 
224     // register for HCI events
225     hci_event_callback_registration.callback = &packet_handler;
226     hci_add_event_handler(&hci_event_callback_registration);
227 
228     // init L2CAP
229     l2cap_init();
230 
231     // init RFCOM
232     rfcomm_init();
233 
234     // init GOEP Client
235     goep_client_init();
236 
237     // init PBAP Client
238     pbap_client_init();
239 
240 #ifdef HAVE_POSIX_STDIN
241     btstack_stdin_setup(stdin_process);
242 #endif
243 
244     // turn on!
245     hci_power_control(HCI_POWER_ON);
246 
247     return 0;
248 }
249