xref: /btstack/example/pbap_client_demo.c (revision 9d65c9925326973cebda440e26e0ed853739da70)
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
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH 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 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "pbap_client_demo.c"
39f1b34e8dSMatthias Ringwald 
4015de8206SMilanka Ringwald // *****************************************************************************
41ec8ae085SMilanka Ringwald /* EXAMPLE_START(pbap_client_demo): PBAP Client - Get Contacts from Phonebook Server
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"
75*9d65c992SMatthias Ringwald static  char * remote_addr_string = "DC:52:85:B4:AD:2B ";
76e03b79abSMatthias Ringwald 
77e03b79abSMatthias Ringwald static char * phone_number = "911";
78f1b34e8dSMatthias Ringwald 
790928fcb2SMatthias Ringwald static const char * pb_name   = "pb";
800928fcb2SMatthias Ringwald static const char * fav_name  = "fav";
810928fcb2SMatthias Ringwald static const char * ich_name  = "ich";
820928fcb2SMatthias Ringwald static const char * och_name  = "och";
830928fcb2SMatthias Ringwald static const char * mch_name  = "mch";
840928fcb2SMatthias Ringwald static const char * cch_name  = "cch";
850928fcb2SMatthias Ringwald static const char * spd_name  = "spd";
86d32b5a2fSMatthias Ringwald 
87c3d9e3b5SMatthias Ringwald static const char * phonebook_name;
88c3d9e3b5SMatthias Ringwald static char phonebook_folder[30];
890928fcb2SMatthias Ringwald static char phonebook_path[30];
908c94c044SMatthias Ringwald 
91f1b34e8dSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
92f1b34e8dSMatthias Ringwald static uint16_t pbap_cid;
93f1b34e8dSMatthias Ringwald 
940928fcb2SMatthias Ringwald static int sim1_selected;
950928fcb2SMatthias Ringwald 
96b43dabc8SMatthias Ringwald static void refresh_phonebook_folder_and_path(void){
97b43dabc8SMatthias Ringwald     sprintf(phonebook_path, "%s%s.vcf", sim1_selected ? "SIM1/telecom/" : "telecom/", phonebook_name);
98b43dabc8SMatthias Ringwald     sprintf(phonebook_folder, "%s%s",   sim1_selected ? "SIM1/telecom/" : "telecom/", phonebook_name);
99c3d9e3b5SMatthias Ringwald     printf("[-] Phonebook folder '%s'\n", phonebook_folder);
100c3d9e3b5SMatthias Ringwald     printf("[-] Phonebook path   '%s'\n", phonebook_path);
101c3d9e3b5SMatthias Ringwald }
102c3d9e3b5SMatthias Ringwald 
103b43dabc8SMatthias Ringwald static void select_phonebook(const char * phonebook){
104b43dabc8SMatthias Ringwald     phonebook_name = phonebook;
105b43dabc8SMatthias Ringwald     printf("[-] Phonebook name   '%s'\n", phonebook_name);
106b43dabc8SMatthias Ringwald     refresh_phonebook_folder_and_path();
107b43dabc8SMatthias Ringwald }
108b43dabc8SMatthias Ringwald 
109c15d10d5SMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN
110c15d10d5SMatthias Ringwald 
111b43dabc8SMatthias Ringwald // Testing User Interface
112f1b34e8dSMatthias Ringwald static void show_usage(void){
113f1b34e8dSMatthias Ringwald     bd_addr_t iut_address;
114f1b34e8dSMatthias Ringwald     gap_local_bd_addr(iut_address);
115f1b34e8dSMatthias Ringwald 
116f1b34e8dSMatthias Ringwald     printf("\n--- Bluetooth PBAP Client (HF) Test Console %s ---\n", bd_addr_to_str(iut_address));
1170928fcb2SMatthias Ringwald     printf("Phonebook:       '%s'\n", phonebook_folder);
1180928fcb2SMatthias Ringwald     // printf("Phonebook folder '%s'\n", phonebook_folder);
1190928fcb2SMatthias Ringwald     printf("Phonebook path   '%s'\n", phonebook_path);
120f1b34e8dSMatthias Ringwald     printf("\n");
121f1b34e8dSMatthias Ringwald     printf("a - establish PBAP connection to %s\n", bd_addr_to_str(remote_addr));
1220928fcb2SMatthias Ringwald     printf("b - select SIM1\n");
1230928fcb2SMatthias Ringwald     printf("r - set path to '/telecom'\n");
1240928fcb2SMatthias Ringwald     printf("R - set path to '/SIM1/telecom'\n");
125c3d9e3b5SMatthias Ringwald     printf("u - set path to '%s'\n", phonebook_folder);
1260928fcb2SMatthias Ringwald     printf("v - set vCardSelector to N and TEL\n");
127d32b5a2fSMatthias Ringwald     printf("V - set vCardSelectorOperator to AND\n");
128d32b5a2fSMatthias Ringwald 
1290928fcb2SMatthias Ringwald     printf("e - select phonebook '%s'\n", pb_name);
1300928fcb2SMatthias Ringwald     printf("f - select phonebook '%s'\n", fav_name);
1310928fcb2SMatthias Ringwald     printf("i - select phonebook '%s'\n", ich_name);
1320928fcb2SMatthias Ringwald     printf("o - select phonebook '%s'\n", och_name);
1330928fcb2SMatthias Ringwald     printf("m - select phonebook '%s'\n", mch_name);
1340928fcb2SMatthias Ringwald     printf("c - select phonebook '%s'\n", cch_name);
1350928fcb2SMatthias Ringwald     printf("s - select phonebook '%s'\n", spd_name);
136d32b5a2fSMatthias Ringwald 
1370928fcb2SMatthias Ringwald     printf("d - get size of    '%s'\n",             phonebook_path);
1380928fcb2SMatthias Ringwald     printf("g - pull phonebook '%s'\n",             phonebook_path);
139c904d4d5SMatthias Ringwald     printf("h - pull vCard listing '%s'\n",         phonebook_folder);
140c904d4d5SMatthias Ringwald     printf("l - get vCard 0.vcf\n");
141c904d4d5SMatthias Ringwald     printf("L - get vCard X-BT-UID::1234567890ABCDEF1234567890000001\n");
142c904d4d5SMatthias Ringwald     printf("j - Lookup contact with number '%s'\n", phone_number);
143d32b5a2fSMatthias Ringwald     printf("t - disconnnect\n");
144c1bc0b8eSMatthias Ringwald     printf("p - authenticate using password '0000'\n");
145e650aceaSMatthias Ringwald     printf("r - set path to 'telecom'\n");
146948889a2SMatthias Ringwald     printf("x - abort operation\n");
1478c0f3635SMilanka Ringwald     printf("\n");
148f1b34e8dSMatthias Ringwald }
149f1b34e8dSMatthias Ringwald 
15095a8ee01SMatthias Ringwald static void stdin_process(char c){
15195a8ee01SMatthias Ringwald     switch (c){
152f1b34e8dSMatthias Ringwald         case 'a':
153f1b34e8dSMatthias Ringwald             printf("[+] Connecting to %s...\n", bd_addr_to_str(remote_addr));
154f1b34e8dSMatthias Ringwald             pbap_connect(&packet_handler, remote_addr, &pbap_cid);
155f1b34e8dSMatthias Ringwald             break;
156f1b34e8dSMatthias Ringwald         case 'b':
1570928fcb2SMatthias Ringwald             printf("[+] SIM1 selected'\n");
1580928fcb2SMatthias Ringwald             sim1_selected = 1;
159b43dabc8SMatthias Ringwald             refresh_phonebook_folder_and_path();
160f1b34e8dSMatthias Ringwald             break;
1610928fcb2SMatthias Ringwald 
162f1b34e8dSMatthias Ringwald         case 'd':
1630928fcb2SMatthias Ringwald             printf("[+] Get size of phonebook '%s'\n", phonebook_path);
1640928fcb2SMatthias Ringwald             pbap_get_phonebook_size(pbap_cid, phonebook_path);
165f1b34e8dSMatthias Ringwald             break;
166e03b79abSMatthias Ringwald         case 'g':
1670928fcb2SMatthias Ringwald             printf("[+] Pull phonebook '%s'\n", phonebook_path);
1680928fcb2SMatthias Ringwald             pbap_pull_phonebook(pbap_cid, phonebook_path);
1690928fcb2SMatthias Ringwald             break;
1700928fcb2SMatthias Ringwald         case 'h':
1710928fcb2SMatthias Ringwald             printf("[+] Pull vCard list for '%s'\n", phonebook_folder);
172c3d9e3b5SMatthias Ringwald             pbap_pull_vcard_listing(pbap_cid, "");
1730928fcb2SMatthias Ringwald             break;
174c904d4d5SMatthias Ringwald         case 'j':
175c904d4d5SMatthias Ringwald             printf("[+] Lookup name for number '%s'\n", phone_number);
176c904d4d5SMatthias Ringwald             pbap_lookup_by_number(pbap_cid, phone_number);
177c904d4d5SMatthias Ringwald             break;
178c904d4d5SMatthias Ringwald         case 'l':
179eeba0155SMatthias Ringwald             printf("[+] Pull vCard '1.vcf'\n");
180eeba0155SMatthias Ringwald             pbap_pull_vcard_entry(pbap_cid, "1.vcf");
1816f968c62SMatthias Ringwald             break;
182c904d4d5SMatthias Ringwald         case 'L':
183c904d4d5SMatthias Ringwald             printf("[+] Pull vCard 'X-BT-UID:1234567890ABCDEF1234567890000001'\n");
184c904d4d5SMatthias Ringwald             pbap_pull_vcard_entry(pbap_cid, "X-BT-UID:1234567890ABCDEF1234567890000001");
185c904d4d5SMatthias Ringwald             break;
186d32b5a2fSMatthias Ringwald 
187d32b5a2fSMatthias Ringwald         case 'c':
1880928fcb2SMatthias Ringwald             printf("[+] Select phonebook '%s'\n", cch_name);
1890928fcb2SMatthias Ringwald             select_phonebook(cch_name);
190d32b5a2fSMatthias Ringwald             break;
191d32b5a2fSMatthias Ringwald         case 'e':
1920928fcb2SMatthias Ringwald             printf("[+] Select phonebook '%s'\n", pb_name);
1930928fcb2SMatthias Ringwald             select_phonebook(pb_name);
194d32b5a2fSMatthias Ringwald             break;
195d32b5a2fSMatthias Ringwald         case 'f':
1960928fcb2SMatthias Ringwald             printf("[+] Select phonebook '%s'\n", fav_name);
1970928fcb2SMatthias Ringwald             select_phonebook(fav_name);
198d32b5a2fSMatthias Ringwald             break;
199d32b5a2fSMatthias Ringwald         case 'i':
2000928fcb2SMatthias Ringwald             printf("[+] Select phonebook '%s'\n", ich_name);
2010928fcb2SMatthias Ringwald             select_phonebook(ich_name);
202d32b5a2fSMatthias Ringwald             break;
203d32b5a2fSMatthias Ringwald         case 'm':
2040928fcb2SMatthias Ringwald             printf("[+] Select phonebook '%s'\n", mch_name);
2050928fcb2SMatthias Ringwald             select_phonebook(mch_name);
206d32b5a2fSMatthias Ringwald             break;
207d32b5a2fSMatthias Ringwald         case 'o':
2080928fcb2SMatthias Ringwald             printf("[+] Select phonebook '%s'\n", och_name);
2090928fcb2SMatthias Ringwald             select_phonebook(och_name);
210d32b5a2fSMatthias Ringwald             break;
211d32b5a2fSMatthias Ringwald         case 's':
2120928fcb2SMatthias Ringwald             printf("[+] Select phonebook '%s'\n", spd_name);
2130928fcb2SMatthias Ringwald             select_phonebook(spd_name);
214d32b5a2fSMatthias Ringwald             break;
215d32b5a2fSMatthias Ringwald 
216c1bc0b8eSMatthias Ringwald         case 'p':
217c1bc0b8eSMatthias Ringwald             pbap_authentication_password(pbap_cid, "0000");
218c1bc0b8eSMatthias Ringwald             break;
219d32b5a2fSMatthias Ringwald         case 'v':
2200928fcb2SMatthias Ringwald             printf("[+] Set vCardSelector 'N' and 'TEL'\n");
2210928fcb2SMatthias Ringwald             pbap_set_vcard_selector(pbap_cid, PBAP_PROPERTY_MASK_N | PBAP_PROPERTY_MASK_TEL);
222d32b5a2fSMatthias Ringwald             break;
223d32b5a2fSMatthias Ringwald         case 'V':
224d32b5a2fSMatthias Ringwald             printf("[+] Set vCardSelectorOperator 'AND'\n");
225d32b5a2fSMatthias Ringwald             pbap_set_vcard_selector_operator(pbap_cid, PBAP_VCARD_SELECTOR_OPERATOR_AND);
226d32b5a2fSMatthias Ringwald             break;
227e650aceaSMatthias Ringwald         case 'r':
2280928fcb2SMatthias Ringwald             printf("[+] Set path to '/telecom'\n");
229e650aceaSMatthias Ringwald             pbap_set_phonebook(pbap_cid, "telecom");
230e650aceaSMatthias Ringwald             break;
2310928fcb2SMatthias Ringwald         case 'R':
2320928fcb2SMatthias Ringwald             printf("[+] Set path to '/SIM1/telecom'\n");
2330928fcb2SMatthias Ringwald             pbap_set_phonebook(pbap_cid, "SIM1/telecom");
2340928fcb2SMatthias Ringwald             break;
235c3d9e3b5SMatthias Ringwald         case 'u':
236c3d9e3b5SMatthias Ringwald             printf("[+] Set path to '%s'\n", phonebook_folder);
237c3d9e3b5SMatthias Ringwald             pbap_set_phonebook(pbap_cid, phonebook_folder);
238c3d9e3b5SMatthias Ringwald             break;
239948889a2SMatthias Ringwald         case 'x':
240948889a2SMatthias Ringwald             printf("[+] Abort'\n");
241948889a2SMatthias Ringwald             pbap_abort(pbap_cid);
242948889a2SMatthias Ringwald             break;
243d32b5a2fSMatthias Ringwald         case 't':
244d32b5a2fSMatthias Ringwald             pbap_disconnect(pbap_cid);
245d32b5a2fSMatthias Ringwald             break;
246f1b34e8dSMatthias Ringwald         default:
247f1b34e8dSMatthias Ringwald             show_usage();
248f1b34e8dSMatthias Ringwald             break;
249f1b34e8dSMatthias Ringwald     }
250f1b34e8dSMatthias Ringwald }
251f1b34e8dSMatthias Ringwald 
252f1b34e8dSMatthias Ringwald // packet handler for interactive console
253f1b34e8dSMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
254f1b34e8dSMatthias Ringwald     UNUSED(channel);
255f1b34e8dSMatthias Ringwald     UNUSED(size);
256f1b34e8dSMatthias Ringwald     int i;
2576ff88f1fSMatthias Ringwald     uint8_t status;
25883f1bca0SMatthias Ringwald     char buffer[32];
259f1b34e8dSMatthias Ringwald     switch (packet_type){
260f1b34e8dSMatthias Ringwald         case HCI_EVENT_PACKET:
261f1b34e8dSMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
262f1b34e8dSMatthias Ringwald                 case BTSTACK_EVENT_STATE:
263f1b34e8dSMatthias Ringwald                     // BTstack activated, get started
264f1b34e8dSMatthias Ringwald                     if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
265f1b34e8dSMatthias Ringwald                         show_usage();
266f1b34e8dSMatthias Ringwald                     }
267f1b34e8dSMatthias Ringwald                     break;
268f1b34e8dSMatthias Ringwald                 case HCI_EVENT_PBAP_META:
269f1b34e8dSMatthias Ringwald                     switch (hci_event_pbap_meta_get_subevent_code(packet)){
270f1b34e8dSMatthias Ringwald                         case PBAP_SUBEVENT_CONNECTION_OPENED:
27183f1bca0SMatthias Ringwald                             status = pbap_subevent_connection_opened_get_status(packet);
27283f1bca0SMatthias Ringwald                             if (status){
27383f1bca0SMatthias Ringwald                                 printf("[!] Connection failed, status 0x%02x\n", status);
27483f1bca0SMatthias Ringwald                             } else {
275f1b34e8dSMatthias Ringwald                                 printf("[+] Connected\n");
27683f1bca0SMatthias Ringwald                             }
277f1b34e8dSMatthias Ringwald                             break;
278f1b34e8dSMatthias Ringwald                         case PBAP_SUBEVENT_CONNECTION_CLOSED:
279f1b34e8dSMatthias Ringwald                             printf("[+] Connection closed\n");
280f1b34e8dSMatthias Ringwald                             break;
281f1b34e8dSMatthias Ringwald                         case PBAP_SUBEVENT_OPERATION_COMPLETED:
282f1b34e8dSMatthias Ringwald                             printf("[+] Operation complete\n");
283f1b34e8dSMatthias Ringwald                             break;
284c1bc0b8eSMatthias Ringwald                         case PBAP_SUBEVENT_AUTHENTICATION_REQUEST:
285c1bc0b8eSMatthias Ringwald                             printf("[?] Authentication requested\n");
286c1bc0b8eSMatthias Ringwald                             break;
2876ff88f1fSMatthias Ringwald                         case PBAP_SUBEVENT_PHONEBOOK_SIZE:
2886ff88f1fSMatthias Ringwald                             status = pbap_subevent_phonebook_size_get_status(packet);
2896ff88f1fSMatthias Ringwald                             if (status){
29083f1bca0SMatthias Ringwald                                 printf("[!] Get Phonebook size error: 0x%x\n", status);
2916ff88f1fSMatthias Ringwald                             } else {
292ac27f3f2SMilanka Ringwald                                 printf("[+] Phonebook size: %u\n", pbap_subevent_phonebook_size_get_phonebook_size(packet));
2936ff88f1fSMatthias Ringwald                             }
2946ff88f1fSMatthias Ringwald                             break;
29583f1bca0SMatthias Ringwald                         case PBAP_SUBEVENT_CARD_RESULT:
29683f1bca0SMatthias Ringwald                             memcpy(buffer, pbap_subevent_card_result_get_name(packet), pbap_subevent_card_result_get_name_len(packet));
29783f1bca0SMatthias Ringwald                             buffer[pbap_subevent_card_result_get_name_len(packet)] = 0;
29883f1bca0SMatthias Ringwald                             printf("[-] Name:   '%s'\n", buffer);
29983f1bca0SMatthias Ringwald                             memcpy(buffer, pbap_subevent_card_result_get_handle(packet), pbap_subevent_card_result_get_handle_len(packet));
30083f1bca0SMatthias Ringwald                             buffer[pbap_subevent_card_result_get_handle_len(packet)] = 0;
30183f1bca0SMatthias Ringwald                             printf("[-] Handle: '%s'\n", buffer);
30283f1bca0SMatthias Ringwald                             break;
303f1b34e8dSMatthias Ringwald                         default:
304f1b34e8dSMatthias Ringwald                             break;
305f1b34e8dSMatthias Ringwald                     }
306f1b34e8dSMatthias Ringwald                     break;
307f1b34e8dSMatthias Ringwald                 default:
308f1b34e8dSMatthias Ringwald                     break;
309f1b34e8dSMatthias Ringwald             }
310f1b34e8dSMatthias Ringwald             break;
311f1b34e8dSMatthias Ringwald         case PBAP_DATA_PACKET:
312f1b34e8dSMatthias Ringwald             for (i=0;i<size;i++){
313f1b34e8dSMatthias Ringwald                 printf("%c", packet[i]);
314f1b34e8dSMatthias Ringwald             }
315f1b34e8dSMatthias Ringwald             break;
316f1b34e8dSMatthias Ringwald         default:
317f1b34e8dSMatthias Ringwald             break;
318f1b34e8dSMatthias Ringwald     }
319f1b34e8dSMatthias Ringwald }
320f1b34e8dSMatthias Ringwald #else
321f1b34e8dSMatthias Ringwald 
322f1b34e8dSMatthias Ringwald // packet handler for emdded system with fixed operation sequence
323f1b34e8dSMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
324f1b34e8dSMatthias Ringwald     UNUSED(channel);
325f1b34e8dSMatthias Ringwald     UNUSED(size);
326f1b34e8dSMatthias Ringwald     int i;
327f1b34e8dSMatthias Ringwald     switch (packet_type){
328f1b34e8dSMatthias Ringwald         case HCI_EVENT_PACKET:
329f1b34e8dSMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
330f1b34e8dSMatthias Ringwald                 case BTSTACK_EVENT_STATE:
331f1b34e8dSMatthias Ringwald                     // BTstack activated, get started
332f1b34e8dSMatthias Ringwald                     if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
333f1b34e8dSMatthias Ringwald                         printf("[+] Connect to Phone Book Server on %s\n", bd_addr_to_str(remote_addr));
334f1b34e8dSMatthias Ringwald                         pbap_connect(&packet_handler, remote_addr, &pbap_cid);
335f1b34e8dSMatthias Ringwald                     }
336f1b34e8dSMatthias Ringwald                     break;
337f1b34e8dSMatthias Ringwald                 case HCI_EVENT_PBAP_META:
338f1b34e8dSMatthias Ringwald                     switch (hci_event_pbap_meta_get_subevent_code(packet)){
339f1b34e8dSMatthias Ringwald                         case PBAP_SUBEVENT_CONNECTION_OPENED:
340f1b34e8dSMatthias Ringwald                             printf("[+] Connected\n");
341f1b34e8dSMatthias Ringwald                             printf("[+] Pull phonebook\n");
342c15d10d5SMatthias Ringwald                             pbap_pull_phonebook(pbap_cid, phonebook_path);
343f1b34e8dSMatthias Ringwald                             break;
344f1b34e8dSMatthias Ringwald                         case PBAP_SUBEVENT_CONNECTION_CLOSED:
345f1b34e8dSMatthias Ringwald                             printf("[+] Connection closed\n");
346f1b34e8dSMatthias Ringwald                             break;
347f1b34e8dSMatthias Ringwald                         case PBAP_SUBEVENT_OPERATION_COMPLETED:
348f1b34e8dSMatthias Ringwald                             printf("[+] Operation complete\n");
349f1b34e8dSMatthias Ringwald                             printf("[+] Pull Phonebook complete\n");
350f1b34e8dSMatthias Ringwald                             pbap_disconnect(pbap_cid);
351f1b34e8dSMatthias Ringwald                             break;
352f1b34e8dSMatthias Ringwald                         default:
353f1b34e8dSMatthias Ringwald                             break;
354f1b34e8dSMatthias Ringwald                     }
355f1b34e8dSMatthias Ringwald                     break;
356f1b34e8dSMatthias Ringwald                 default:
357f1b34e8dSMatthias Ringwald                     break;
358f1b34e8dSMatthias Ringwald             }
359f1b34e8dSMatthias Ringwald             break;
360f1b34e8dSMatthias Ringwald         case PBAP_DATA_PACKET:
361f1b34e8dSMatthias Ringwald             for (i=0;i<size;i++){
362f1b34e8dSMatthias Ringwald                 printf("%c", packet[i]);
363f1b34e8dSMatthias Ringwald             }
364f1b34e8dSMatthias Ringwald             break;
365f1b34e8dSMatthias Ringwald         default:
366f1b34e8dSMatthias Ringwald             break;
367f1b34e8dSMatthias Ringwald     }
368f1b34e8dSMatthias Ringwald }
369f1b34e8dSMatthias Ringwald #endif
370f1b34e8dSMatthias Ringwald 
371f1b34e8dSMatthias Ringwald int btstack_main(int argc, const char * argv[]);
372f1b34e8dSMatthias Ringwald int btstack_main(int argc, const char * argv[]){
373f1b34e8dSMatthias Ringwald 
374f1b34e8dSMatthias Ringwald     (void)argc;
375f1b34e8dSMatthias Ringwald     (void)argv;
376f1b34e8dSMatthias Ringwald 
377f1b34e8dSMatthias Ringwald     // init L2CAP
378f1b34e8dSMatthias Ringwald     l2cap_init();
379f1b34e8dSMatthias Ringwald 
380f1b34e8dSMatthias Ringwald     // init RFCOM
381f1b34e8dSMatthias Ringwald     rfcomm_init();
382f1b34e8dSMatthias Ringwald 
383f1b34e8dSMatthias Ringwald     // init GOEP Client
384f1b34e8dSMatthias Ringwald     goep_client_init();
385f1b34e8dSMatthias Ringwald 
386f1b34e8dSMatthias Ringwald     // init PBAP Client
387f1b34e8dSMatthias Ringwald     pbap_client_init();
388f1b34e8dSMatthias Ringwald 
389a4fe6467SMatthias Ringwald     // register for HCI events
390a4fe6467SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
391a4fe6467SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
392a4fe6467SMatthias Ringwald 
393a4fe6467SMatthias Ringwald     sscanf_bd_addr(remote_addr_string, remote_addr);
394a4fe6467SMatthias Ringwald 
3950928fcb2SMatthias Ringwald     select_phonebook(pb_name);
3960928fcb2SMatthias Ringwald 
3977ea7688aSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN
398f1b34e8dSMatthias Ringwald     btstack_stdin_setup(stdin_process);
399f1b34e8dSMatthias Ringwald #endif
400f1b34e8dSMatthias Ringwald 
401f1b34e8dSMatthias Ringwald     // turn on!
402f1b34e8dSMatthias Ringwald     hci_power_control(HCI_POWER_ON);
403f1b34e8dSMatthias Ringwald 
404f1b34e8dSMatthias Ringwald     return 0;
405f1b34e8dSMatthias Ringwald }
40615de8206SMilanka Ringwald /* EXAMPLE_END */
407