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