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