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