xref: /btstack/example/pbap_client_demo.c (revision d32b5a2f58fab7968b9e42e4cf359e00f4ee8a2b)
1f1b34e8dSMatthias Ringwald /*
2f1b34e8dSMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3f1b34e8dSMatthias Ringwald  *
4f1b34e8dSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5f1b34e8dSMatthias Ringwald  * modification, are permitted provided that the following conditions
6f1b34e8dSMatthias Ringwald  * are met:
7f1b34e8dSMatthias Ringwald  *
8f1b34e8dSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9f1b34e8dSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10f1b34e8dSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11f1b34e8dSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12f1b34e8dSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13f1b34e8dSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14f1b34e8dSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15f1b34e8dSMatthias Ringwald  *    from this software without specific prior written permission.
16f1b34e8dSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17f1b34e8dSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18f1b34e8dSMatthias Ringwald  *    monetary gain.
19f1b34e8dSMatthias Ringwald  *
20f1b34e8dSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21f1b34e8dSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22f1b34e8dSMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23f1b34e8dSMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24f1b34e8dSMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25f1b34e8dSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26f1b34e8dSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27f1b34e8dSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28f1b34e8dSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29f1b34e8dSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30f1b34e8dSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31f1b34e8dSMatthias Ringwald  * SUCH DAMAGE.
32f1b34e8dSMatthias Ringwald  *
33f1b34e8dSMatthias Ringwald  * Please inquire about commercial licensing options at
34f1b34e8dSMatthias Ringwald  * [email protected]
35f1b34e8dSMatthias Ringwald  *
36f1b34e8dSMatthias Ringwald  */
37ab2c6ae4SMatthias Ringwald 
38ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "pbap_client_demo.c"
39f1b34e8dSMatthias Ringwald 
4015de8206SMilanka Ringwald // *****************************************************************************
4115de8206SMilanka Ringwald /* EXAMPLE_START(pbap_client_demo): Connect to Phonebook Server and get contacts.
4258d7a529SMilanka Ringwald  *
4358d7a529SMilanka Ringwald  * @text Note: The Bluetooth address of the remote Phonbook server is hardcoded.
4458d7a529SMilanka Ringwald  * Change it before running example, then use the UI to connect to it, to set and
4558d7a529SMilanka Ringwald  * query contacts.
4615de8206SMilanka Ringwald  */
4715de8206SMilanka Ringwald // *****************************************************************************
4815de8206SMilanka Ringwald 
4915de8206SMilanka Ringwald 
50f1b34e8dSMatthias Ringwald #include "btstack_config.h"
51f1b34e8dSMatthias Ringwald 
52f1b34e8dSMatthias Ringwald #include <stdint.h>
53f1b34e8dSMatthias Ringwald #include <stdio.h>
54f1b34e8dSMatthias Ringwald #include <stdlib.h>
55f1b34e8dSMatthias Ringwald #include <string.h>
56f1b34e8dSMatthias Ringwald 
57f1b34e8dSMatthias Ringwald #include "btstack_run_loop.h"
58f1b34e8dSMatthias Ringwald #include "l2cap.h"
59cde6ece4SMatthias Ringwald #include "classic/rfcomm.h"
60f1b34e8dSMatthias Ringwald #include "btstack_event.h"
61f1b34e8dSMatthias Ringwald #include "classic/goep_client.h"
62f1b34e8dSMatthias Ringwald #include "classic/pbap_client.h"
63f1b34e8dSMatthias Ringwald 
647ea7688aSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN
657ea7688aSMatthias Ringwald #include "btstack_stdin.h"
66f1b34e8dSMatthias Ringwald #endif
67f1b34e8dSMatthias Ringwald 
68f1b34e8dSMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
69f1b34e8dSMatthias Ringwald 
70f1b34e8dSMatthias Ringwald static bd_addr_t    remote_addr;
71f1b34e8dSMatthias Ringwald // MBP2016 "F4-0F-24-3B-1B-E1"
72f1b34e8dSMatthias Ringwald // Nexus 7 "30-85-A9-54-2E-78"
73c1bc0b8eSMatthias Ringwald // iPhone SE "BC:EC:5D:E6:15:03"
74c1bc0b8eSMatthias Ringwald // PTS "001BDC080AA5"
758c94c044SMatthias Ringwald static  char * remote_addr_string = "001BDC080AA5";
76e03b79abSMatthias Ringwald 
77e03b79abSMatthias Ringwald static char * phone_number = "911";
78f1b34e8dSMatthias Ringwald 
79*d32b5a2fSMatthias Ringwald static const char * t_pb_path   = "telecom/pb.vcf";
80*d32b5a2fSMatthias Ringwald static const char * t_fav_path  = "telecom/fav.vcf";
81*d32b5a2fSMatthias Ringwald static const char * t_ich_path  = "telecom/ich.vcf";
82*d32b5a2fSMatthias Ringwald static const char * t_och_path  = "telecom/och.vcf";
83*d32b5a2fSMatthias Ringwald static const char * t_mch_path  = "telecom/mch.vcf";
84*d32b5a2fSMatthias Ringwald static const char * t_cch_path  = "telecom/cch.vcf";
85*d32b5a2fSMatthias Ringwald static const char * t_spd_path  = "telecom/spd.vcf";
86*d32b5a2fSMatthias Ringwald 
87*d32b5a2fSMatthias Ringwald static const char * st_pb_path  = "SIM1/telecom/pb.vcf";
88*d32b5a2fSMatthias Ringwald static const char * st_ich_path = "SIM1/telecom/ich.vcf";
89*d32b5a2fSMatthias Ringwald static const char * st_och_path = "SIM1/telecom/och.vcf";
90*d32b5a2fSMatthias Ringwald static const char * st_mch_path = "SIM1/telecom/mch.vcf";
91*d32b5a2fSMatthias Ringwald static const char * st_cch_path = "SIM1/telecom/cch.vcf";
928c94c044SMatthias Ringwald 
93f1b34e8dSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
94f1b34e8dSMatthias Ringwald static uint16_t pbap_cid;
95f1b34e8dSMatthias Ringwald 
967ea7688aSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN
97f1b34e8dSMatthias Ringwald 
98f1b34e8dSMatthias Ringwald // Testig User Interface
99f1b34e8dSMatthias Ringwald static void show_usage(void){
100f1b34e8dSMatthias Ringwald     bd_addr_t iut_address;
101f1b34e8dSMatthias Ringwald     gap_local_bd_addr(iut_address);
102f1b34e8dSMatthias Ringwald 
103f1b34e8dSMatthias Ringwald     printf("\n--- Bluetooth PBAP Client (HF) Test Console %s ---\n", bd_addr_to_str(iut_address));
104f1b34e8dSMatthias Ringwald     printf("\n");
105f1b34e8dSMatthias Ringwald     printf("a - establish PBAP connection to %s\n", bd_addr_to_str(remote_addr));
106f1b34e8dSMatthias Ringwald     printf("b - set phonebook  '/telecom/pb'\n");
107*d32b5a2fSMatthias Ringwald     // printf("c - set phonebook  '/SIM1/telecom/pb'\n");
108e650aceaSMatthias Ringwald     printf("r - set path to    '/root/telecom'\n");
109*d32b5a2fSMatthias Ringwald     printf("v - set vCardSelector to FN\n");
110*d32b5a2fSMatthias Ringwald     printf("V - set vCardSelectorOperator to AND\n");
111*d32b5a2fSMatthias Ringwald     printf("d - get size of    '%s'\n", t_pb_path);
112*d32b5a2fSMatthias Ringwald 
113*d32b5a2fSMatthias Ringwald     printf("e - pull phonebook '%s'\n", t_pb_path);
114*d32b5a2fSMatthias Ringwald     printf("f - pull phonebook '%s'\n", t_fav_path);
115*d32b5a2fSMatthias Ringwald     printf("i - pull phonebook '%s'\n", t_ich_path);
116*d32b5a2fSMatthias Ringwald     printf("o - pull phonebook '%s'\n", t_och_path);
117*d32b5a2fSMatthias Ringwald     printf("m - pull phonebook '%s'\n", t_mch_path);
118*d32b5a2fSMatthias Ringwald     printf("c - pull phonebook '%s'\n", t_cch_path);
119*d32b5a2fSMatthias Ringwald     printf("s - pull phonebook '%s'\n", t_spd_path);
120*d32b5a2fSMatthias Ringwald 
121*d32b5a2fSMatthias Ringwald     printf("E - pull phonebook '%s'\n", st_pb_path);
122*d32b5a2fSMatthias Ringwald     printf("I - pull phonebook '%s'\n", st_ich_path);
123*d32b5a2fSMatthias Ringwald     printf("O - pull phonebook '%s'\n", st_och_path);
124*d32b5a2fSMatthias Ringwald     printf("M - pull phonebook '%s'\n", st_mch_path);
125*d32b5a2fSMatthias Ringwald     printf("C - pull phonebook '%s'\n", st_cch_path);
126*d32b5a2fSMatthias Ringwald 
127*d32b5a2fSMatthias Ringwald     printf("t - disconnnect\n");
128e03b79abSMatthias Ringwald     printf("g - Lookup contact with number '%s'\n", phone_number);
129c1bc0b8eSMatthias Ringwald     printf("p - authenticate using password '0000'\n");
130e650aceaSMatthias Ringwald     printf("r - set path to 'telecom'\n");
131948889a2SMatthias Ringwald     printf("x - abort operation\n");
1328c0f3635SMilanka Ringwald     printf("\n");
133f1b34e8dSMatthias Ringwald }
134f1b34e8dSMatthias Ringwald 
13595a8ee01SMatthias Ringwald static void stdin_process(char c){
13695a8ee01SMatthias Ringwald     switch (c){
137f1b34e8dSMatthias Ringwald         case 'a':
138f1b34e8dSMatthias Ringwald             printf("[+] Connecting to %s...\n", bd_addr_to_str(remote_addr));
139f1b34e8dSMatthias Ringwald             pbap_connect(&packet_handler, remote_addr, &pbap_cid);
140f1b34e8dSMatthias Ringwald             break;
141f1b34e8dSMatthias Ringwald         case 'b':
142*d32b5a2fSMatthias Ringwald             printf("[+] Set phonebook 'telecom/pb'\n");
143f1b34e8dSMatthias Ringwald             pbap_set_phonebook(pbap_cid, "telecom/pb");
144f1b34e8dSMatthias Ringwald             break;
145*d32b5a2fSMatthias Ringwald         // case 'c':
146*d32b5a2fSMatthias Ringwald         //     printf("[+] Set phonebook 'SIM1/telecom/pb'\n");
147*d32b5a2fSMatthias Ringwald         //     pbap_set_phonebook(pbap_cid, "SIM1/telecom/pb");
148*d32b5a2fSMatthias Ringwald         //     break;
149f1b34e8dSMatthias Ringwald         case 'd':
150*d32b5a2fSMatthias Ringwald             printf("[+] Get size of phonebook '%s'\n", t_pb_path);
151*d32b5a2fSMatthias Ringwald             pbap_get_phonebook_size(pbap_cid, t_pb_path);
152f1b34e8dSMatthias Ringwald             break;
153e03b79abSMatthias Ringwald         case 'g':
154e03b79abSMatthias Ringwald             pbap_lookup_by_number(pbap_cid, phone_number);
155e03b79abSMatthias Ringwald             break;
156*d32b5a2fSMatthias Ringwald 
157*d32b5a2fSMatthias Ringwald         case 'c':
158*d32b5a2fSMatthias Ringwald             printf("[+] Get phonebook '%s'\n", t_cch_path);
159*d32b5a2fSMatthias Ringwald             pbap_pull_phonebook(pbap_cid, t_cch_path);
160*d32b5a2fSMatthias Ringwald             break;
161*d32b5a2fSMatthias Ringwald         case 'e':
162*d32b5a2fSMatthias Ringwald             printf("[+] Get phonebook '%s'\n", t_pb_path);
163*d32b5a2fSMatthias Ringwald             pbap_pull_phonebook(pbap_cid, t_pb_path);
164*d32b5a2fSMatthias Ringwald             break;
165*d32b5a2fSMatthias Ringwald         case 'f':
166*d32b5a2fSMatthias Ringwald             printf("[+] Get phonebook '%s'\n", t_fav_path);
167*d32b5a2fSMatthias Ringwald             pbap_pull_phonebook(pbap_cid, t_fav_path);
168*d32b5a2fSMatthias Ringwald             break;
169*d32b5a2fSMatthias Ringwald         case 'i':
170*d32b5a2fSMatthias Ringwald             printf("[+] Get phonebook '%s'\n", t_ich_path);
171*d32b5a2fSMatthias Ringwald             pbap_pull_phonebook(pbap_cid, t_ich_path);
172*d32b5a2fSMatthias Ringwald             break;
173*d32b5a2fSMatthias Ringwald         case 'm':
174*d32b5a2fSMatthias Ringwald             printf("[+] Get phonebook '%s'\n", t_mch_path);
175*d32b5a2fSMatthias Ringwald             pbap_pull_phonebook(pbap_cid, t_mch_path);
176*d32b5a2fSMatthias Ringwald             break;
177*d32b5a2fSMatthias Ringwald         case 'o':
178*d32b5a2fSMatthias Ringwald             printf("[+] Get phonebook '%s'\n", t_och_path);
179*d32b5a2fSMatthias Ringwald             pbap_pull_phonebook(pbap_cid, t_och_path);
180*d32b5a2fSMatthias Ringwald             break;
181*d32b5a2fSMatthias Ringwald         case 's':
182*d32b5a2fSMatthias Ringwald             printf("[+] Get phonebook '%s'\n", t_spd_path);
183*d32b5a2fSMatthias Ringwald             pbap_pull_phonebook(pbap_cid, t_spd_path);
184*d32b5a2fSMatthias Ringwald             break;
185*d32b5a2fSMatthias Ringwald 
186*d32b5a2fSMatthias Ringwald         case 'C':
187*d32b5a2fSMatthias Ringwald             printf("[+] Get phonebook '%s'\n", st_cch_path);
188*d32b5a2fSMatthias Ringwald             pbap_pull_phonebook(pbap_cid, st_cch_path);
189*d32b5a2fSMatthias Ringwald             break;
190*d32b5a2fSMatthias Ringwald         case 'E':
191*d32b5a2fSMatthias Ringwald             printf("[+] Get phonebook '%s'\n", st_pb_path);
192*d32b5a2fSMatthias Ringwald             pbap_pull_phonebook(pbap_cid, st_pb_path);
193*d32b5a2fSMatthias Ringwald             break;
194*d32b5a2fSMatthias Ringwald         case 'I':
195*d32b5a2fSMatthias Ringwald             printf("[+] Get phonebook '%s'\n", st_ich_path);
196*d32b5a2fSMatthias Ringwald             pbap_pull_phonebook(pbap_cid, st_ich_path);
197*d32b5a2fSMatthias Ringwald             break;
198*d32b5a2fSMatthias Ringwald         case 'M':
199*d32b5a2fSMatthias Ringwald             printf("[+] Get phonebook '%s'\n", st_mch_path);
200*d32b5a2fSMatthias Ringwald             pbap_pull_phonebook(pbap_cid, st_mch_path);
201*d32b5a2fSMatthias Ringwald             break;
202*d32b5a2fSMatthias Ringwald         case 'O':
203*d32b5a2fSMatthias Ringwald             printf("[+] Get phonebook '%s'\n", st_och_path);
204*d32b5a2fSMatthias Ringwald             pbap_pull_phonebook(pbap_cid, st_och_path);
205*d32b5a2fSMatthias Ringwald             break;
206*d32b5a2fSMatthias Ringwald 
207c1bc0b8eSMatthias Ringwald         case 'p':
208c1bc0b8eSMatthias Ringwald             pbap_authentication_password(pbap_cid, "0000");
209c1bc0b8eSMatthias Ringwald             break;
210*d32b5a2fSMatthias Ringwald         case 'v':
211*d32b5a2fSMatthias Ringwald             printf("[+] Set vCardSelector 'FN'\n");
212*d32b5a2fSMatthias Ringwald             pbap_set_vcard_selector(pbap_cid, PBAP_PROPERTY_MASK_FN);
213*d32b5a2fSMatthias Ringwald             break;
214*d32b5a2fSMatthias Ringwald         case 'V':
215*d32b5a2fSMatthias Ringwald             printf("[+] Set vCardSelectorOperator 'AND'\n");
216*d32b5a2fSMatthias Ringwald             pbap_set_vcard_selector_operator(pbap_cid, PBAP_VCARD_SELECTOR_OPERATOR_AND);
217*d32b5a2fSMatthias Ringwald             break;
218e650aceaSMatthias Ringwald         case 'r':
219e650aceaSMatthias Ringwald             printf("[+] Set path to 'telecom'\n");
220e650aceaSMatthias Ringwald             pbap_set_phonebook(pbap_cid, "telecom");
221e650aceaSMatthias Ringwald             break;
222948889a2SMatthias Ringwald         case 'x':
223948889a2SMatthias Ringwald             printf("[+] Abort'\n");
224948889a2SMatthias Ringwald             pbap_abort(pbap_cid);
225948889a2SMatthias Ringwald             break;
226*d32b5a2fSMatthias Ringwald         case 't':
227*d32b5a2fSMatthias Ringwald             pbap_disconnect(pbap_cid);
228*d32b5a2fSMatthias Ringwald             break;
229f1b34e8dSMatthias Ringwald         default:
230f1b34e8dSMatthias Ringwald             show_usage();
231f1b34e8dSMatthias Ringwald             break;
232f1b34e8dSMatthias Ringwald     }
233f1b34e8dSMatthias Ringwald }
234f1b34e8dSMatthias Ringwald 
235f1b34e8dSMatthias Ringwald // packet handler for interactive console
236f1b34e8dSMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
237f1b34e8dSMatthias Ringwald     UNUSED(channel);
238f1b34e8dSMatthias Ringwald     UNUSED(size);
239f1b34e8dSMatthias Ringwald     int i;
2406ff88f1fSMatthias Ringwald     uint8_t status;
24183f1bca0SMatthias Ringwald     char buffer[32];
242f1b34e8dSMatthias Ringwald     switch (packet_type){
243f1b34e8dSMatthias Ringwald         case HCI_EVENT_PACKET:
244f1b34e8dSMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
245f1b34e8dSMatthias Ringwald                 case BTSTACK_EVENT_STATE:
246f1b34e8dSMatthias Ringwald                     // BTstack activated, get started
247f1b34e8dSMatthias Ringwald                     if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
248f1b34e8dSMatthias Ringwald                         show_usage();
249f1b34e8dSMatthias Ringwald                     }
250f1b34e8dSMatthias Ringwald                     break;
251f1b34e8dSMatthias Ringwald                 case HCI_EVENT_PBAP_META:
252f1b34e8dSMatthias Ringwald                     switch (hci_event_pbap_meta_get_subevent_code(packet)){
253f1b34e8dSMatthias Ringwald                         case PBAP_SUBEVENT_CONNECTION_OPENED:
25483f1bca0SMatthias Ringwald                             status = pbap_subevent_connection_opened_get_status(packet);
25583f1bca0SMatthias Ringwald                             if (status){
25683f1bca0SMatthias Ringwald                                 printf("[!] Connection failed, status 0x%02x\n", status);
25783f1bca0SMatthias Ringwald                             } else {
258f1b34e8dSMatthias Ringwald                                 printf("[+] Connected\n");
25983f1bca0SMatthias Ringwald                             }
260f1b34e8dSMatthias Ringwald                             break;
261f1b34e8dSMatthias Ringwald                         case PBAP_SUBEVENT_CONNECTION_CLOSED:
262f1b34e8dSMatthias Ringwald                             printf("[+] Connection closed\n");
263f1b34e8dSMatthias Ringwald                             break;
264f1b34e8dSMatthias Ringwald                         case PBAP_SUBEVENT_OPERATION_COMPLETED:
265f1b34e8dSMatthias Ringwald                             printf("[+] Operation complete\n");
266f1b34e8dSMatthias Ringwald                             break;
267c1bc0b8eSMatthias Ringwald                         case PBAP_SUBEVENT_AUTHENTICATION_REQUEST:
268c1bc0b8eSMatthias Ringwald                             printf("[?] Authentication requested\n");
269c1bc0b8eSMatthias Ringwald                             break;
2706ff88f1fSMatthias Ringwald                         case PBAP_SUBEVENT_PHONEBOOK_SIZE:
2716ff88f1fSMatthias Ringwald                             status = pbap_subevent_phonebook_size_get_status(packet);
2726ff88f1fSMatthias Ringwald                             if (status){
27383f1bca0SMatthias Ringwald                                 printf("[!] Get Phonebook size error: 0x%x\n", status);
2746ff88f1fSMatthias Ringwald                             } else {
2756ff88f1fSMatthias Ringwald                                 printf("[+] Phonebook size: %u\n", pbap_subevent_phonebook_size_get_phoneboook_size(packet));
2766ff88f1fSMatthias Ringwald                             }
2776ff88f1fSMatthias Ringwald                             break;
27883f1bca0SMatthias Ringwald                         case PBAP_SUBEVENT_CARD_RESULT:
27983f1bca0SMatthias Ringwald                             memcpy(buffer, pbap_subevent_card_result_get_name(packet), pbap_subevent_card_result_get_name_len(packet));
28083f1bca0SMatthias Ringwald                             buffer[pbap_subevent_card_result_get_name_len(packet)] = 0;
28183f1bca0SMatthias Ringwald                             printf("[-] Name:   '%s'\n", buffer);
28283f1bca0SMatthias Ringwald                             memcpy(buffer, pbap_subevent_card_result_get_handle(packet), pbap_subevent_card_result_get_handle_len(packet));
28383f1bca0SMatthias Ringwald                             buffer[pbap_subevent_card_result_get_handle_len(packet)] = 0;
28483f1bca0SMatthias Ringwald                             printf("[-] Handle: '%s'\n", buffer);
28583f1bca0SMatthias Ringwald                             break;
286f1b34e8dSMatthias Ringwald                         default:
287f1b34e8dSMatthias Ringwald                             break;
288f1b34e8dSMatthias Ringwald                     }
289f1b34e8dSMatthias Ringwald                     break;
290f1b34e8dSMatthias Ringwald                 default:
291f1b34e8dSMatthias Ringwald                     break;
292f1b34e8dSMatthias Ringwald             }
293f1b34e8dSMatthias Ringwald             break;
294f1b34e8dSMatthias Ringwald         case PBAP_DATA_PACKET:
295f1b34e8dSMatthias Ringwald             for (i=0;i<size;i++){
296f1b34e8dSMatthias Ringwald                 printf("%c", packet[i]);
297f1b34e8dSMatthias Ringwald             }
298f1b34e8dSMatthias Ringwald             break;
299f1b34e8dSMatthias Ringwald         default:
300f1b34e8dSMatthias Ringwald             break;
301f1b34e8dSMatthias Ringwald     }
302f1b34e8dSMatthias Ringwald }
303f1b34e8dSMatthias Ringwald #else
304f1b34e8dSMatthias Ringwald 
305f1b34e8dSMatthias Ringwald // packet handler for emdded system with fixed operation sequence
306f1b34e8dSMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
307f1b34e8dSMatthias Ringwald     UNUSED(channel);
308f1b34e8dSMatthias Ringwald     UNUSED(size);
309f1b34e8dSMatthias Ringwald     int i;
310f1b34e8dSMatthias Ringwald     switch (packet_type){
311f1b34e8dSMatthias Ringwald         case HCI_EVENT_PACKET:
312f1b34e8dSMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
313f1b34e8dSMatthias Ringwald                 case BTSTACK_EVENT_STATE:
314f1b34e8dSMatthias Ringwald                     // BTstack activated, get started
315f1b34e8dSMatthias Ringwald                     if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
316f1b34e8dSMatthias Ringwald                         printf("[+] Connect to Phone Book Server on %s\n", bd_addr_to_str(remote_addr));
317f1b34e8dSMatthias Ringwald                         pbap_connect(&packet_handler, remote_addr, &pbap_cid);
318f1b34e8dSMatthias Ringwald                     }
319f1b34e8dSMatthias Ringwald                     break;
320f1b34e8dSMatthias Ringwald                 case HCI_EVENT_PBAP_META:
321f1b34e8dSMatthias Ringwald                     switch (hci_event_pbap_meta_get_subevent_code(packet)){
322f1b34e8dSMatthias Ringwald                         case PBAP_SUBEVENT_CONNECTION_OPENED:
323f1b34e8dSMatthias Ringwald                             printf("[+] Connected\n");
324f1b34e8dSMatthias Ringwald                             printf("[+] Pull phonebook\n");
325f1b34e8dSMatthias Ringwald                             pbap_pull_phonebook(pbap_cid);
326f1b34e8dSMatthias Ringwald                             break;
327f1b34e8dSMatthias Ringwald                         case PBAP_SUBEVENT_CONNECTION_CLOSED:
328f1b34e8dSMatthias Ringwald                             printf("[+] Connection closed\n");
329f1b34e8dSMatthias Ringwald                             break;
330f1b34e8dSMatthias Ringwald                         case PBAP_SUBEVENT_OPERATION_COMPLETED:
331f1b34e8dSMatthias Ringwald                             printf("[+] Operation complete\n");
332f1b34e8dSMatthias Ringwald                             printf("[+] Pull Phonebook complete\n");
333f1b34e8dSMatthias Ringwald                             pbap_disconnect(pbap_cid);
334f1b34e8dSMatthias Ringwald                             break;
335f1b34e8dSMatthias Ringwald                         default:
336f1b34e8dSMatthias Ringwald                             break;
337f1b34e8dSMatthias Ringwald                     }
338f1b34e8dSMatthias Ringwald                     break;
339f1b34e8dSMatthias Ringwald                 default:
340f1b34e8dSMatthias Ringwald                     break;
341f1b34e8dSMatthias Ringwald             }
342f1b34e8dSMatthias Ringwald             break;
343f1b34e8dSMatthias Ringwald         case PBAP_DATA_PACKET:
344f1b34e8dSMatthias Ringwald             for (i=0;i<size;i++){
345f1b34e8dSMatthias Ringwald                 printf("%c", packet[i]);
346f1b34e8dSMatthias Ringwald             }
347f1b34e8dSMatthias Ringwald             break;
348f1b34e8dSMatthias Ringwald         default:
349f1b34e8dSMatthias Ringwald             break;
350f1b34e8dSMatthias Ringwald     }
351f1b34e8dSMatthias Ringwald }
352f1b34e8dSMatthias Ringwald #endif
353f1b34e8dSMatthias Ringwald 
354f1b34e8dSMatthias Ringwald int btstack_main(int argc, const char * argv[]);
355f1b34e8dSMatthias Ringwald int btstack_main(int argc, const char * argv[]){
356f1b34e8dSMatthias Ringwald 
357f1b34e8dSMatthias Ringwald     (void)argc;
358f1b34e8dSMatthias Ringwald     (void)argv;
359f1b34e8dSMatthias Ringwald 
360f1b34e8dSMatthias Ringwald     // init L2CAP
361f1b34e8dSMatthias Ringwald     l2cap_init();
362f1b34e8dSMatthias Ringwald 
363f1b34e8dSMatthias Ringwald     // init RFCOM
364f1b34e8dSMatthias Ringwald     rfcomm_init();
365f1b34e8dSMatthias Ringwald 
366f1b34e8dSMatthias Ringwald     // init GOEP Client
367f1b34e8dSMatthias Ringwald     goep_client_init();
368f1b34e8dSMatthias Ringwald 
369f1b34e8dSMatthias Ringwald     // init PBAP Client
370f1b34e8dSMatthias Ringwald     pbap_client_init();
371f1b34e8dSMatthias Ringwald 
372a4fe6467SMatthias Ringwald     // register for HCI events
373a4fe6467SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
374a4fe6467SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
375a4fe6467SMatthias Ringwald 
376a4fe6467SMatthias Ringwald     sscanf_bd_addr(remote_addr_string, remote_addr);
377a4fe6467SMatthias Ringwald 
3787ea7688aSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN
379f1b34e8dSMatthias Ringwald     btstack_stdin_setup(stdin_process);
380f1b34e8dSMatthias Ringwald #endif
381f1b34e8dSMatthias Ringwald 
382f1b34e8dSMatthias Ringwald     // turn on!
383f1b34e8dSMatthias Ringwald     hci_power_control(HCI_POWER_ON);
384f1b34e8dSMatthias Ringwald 
385f1b34e8dSMatthias Ringwald     return 0;
386f1b34e8dSMatthias Ringwald }
38715de8206SMilanka Ringwald /* EXAMPLE_END */
388