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