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