xref: /btstack/example/ancs_client_demo.c (revision bcf00d8fa453bd3a5c441c3b99e6e4b685f0d8f0)
1*bcf00d8fSMatthias Ringwald /*
2*bcf00d8fSMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3*bcf00d8fSMatthias Ringwald  *
4*bcf00d8fSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5*bcf00d8fSMatthias Ringwald  * modification, are permitted provided that the following conditions
6*bcf00d8fSMatthias Ringwald  * are met:
7*bcf00d8fSMatthias Ringwald  *
8*bcf00d8fSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9*bcf00d8fSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10*bcf00d8fSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11*bcf00d8fSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12*bcf00d8fSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13*bcf00d8fSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14*bcf00d8fSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15*bcf00d8fSMatthias Ringwald  *    from this software without specific prior written permission.
16*bcf00d8fSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17*bcf00d8fSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18*bcf00d8fSMatthias Ringwald  *    monetary gain.
19*bcf00d8fSMatthias Ringwald  *
20*bcf00d8fSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21*bcf00d8fSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*bcf00d8fSMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*bcf00d8fSMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24*bcf00d8fSMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25*bcf00d8fSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26*bcf00d8fSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27*bcf00d8fSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28*bcf00d8fSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29*bcf00d8fSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30*bcf00d8fSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*bcf00d8fSMatthias Ringwald  * SUCH DAMAGE.
32*bcf00d8fSMatthias Ringwald  *
33*bcf00d8fSMatthias Ringwald  * Please inquire about commercial licensing options at
34*bcf00d8fSMatthias Ringwald  * [email protected]
35*bcf00d8fSMatthias Ringwald  *
36*bcf00d8fSMatthias Ringwald  */
37*bcf00d8fSMatthias Ringwald 
38*bcf00d8fSMatthias Ringwald // *****************************************************************************
39*bcf00d8fSMatthias Ringwald //
40*bcf00d8fSMatthias Ringwald // ANCS Client Demo
41*bcf00d8fSMatthias Ringwald //
42*bcf00d8fSMatthias Ringwald // TODO: query full text upon notification using control point
43*bcf00d8fSMatthias Ringwald // TODO: present notifications in human readable form
44*bcf00d8fSMatthias Ringwald //
45*bcf00d8fSMatthias Ringwald // *****************************************************************************
46*bcf00d8fSMatthias Ringwald 
47*bcf00d8fSMatthias Ringwald #include <stdint.h>
48*bcf00d8fSMatthias Ringwald #include <stdio.h>
49*bcf00d8fSMatthias Ringwald #include <stdlib.h>
50*bcf00d8fSMatthias Ringwald #include <string.h>
51*bcf00d8fSMatthias Ringwald #include <unistd.h>
52*bcf00d8fSMatthias Ringwald 
53*bcf00d8fSMatthias Ringwald #include "btstack_config.h"
54*bcf00d8fSMatthias Ringwald 
55*bcf00d8fSMatthias Ringwald #include "btstack_run_loop.h"
56*bcf00d8fSMatthias Ringwald 
57*bcf00d8fSMatthias Ringwald #include "btstack_debug.h"
58*bcf00d8fSMatthias Ringwald #include "btstack_event.h"
59*bcf00d8fSMatthias Ringwald #include "btstack_memory.h"
60*bcf00d8fSMatthias Ringwald #include "gap.h"
61*bcf00d8fSMatthias Ringwald #include "hci.h"
62*bcf00d8fSMatthias Ringwald #include "hci_dump.h"
63*bcf00d8fSMatthias Ringwald #include "l2cap.h"
64*bcf00d8fSMatthias Ringwald 
65*bcf00d8fSMatthias Ringwald #include "ble/ancs_client.h"
66*bcf00d8fSMatthias Ringwald #include "ble/att_db.h"
67*bcf00d8fSMatthias Ringwald #include "ble/att_server.h"
68*bcf00d8fSMatthias Ringwald #include "ble/gatt_client.h"
69*bcf00d8fSMatthias Ringwald #include "ble/le_device_db.h"
70*bcf00d8fSMatthias Ringwald #include "ble/sm.h"
71*bcf00d8fSMatthias Ringwald 
72*bcf00d8fSMatthias Ringwald // ancs client demo profile
73*bcf00d8fSMatthias Ringwald #include "ancs_client_demo.h"
74*bcf00d8fSMatthias Ringwald 
75*bcf00d8fSMatthias Ringwald static const uint8_t adv_data[] = {
76*bcf00d8fSMatthias Ringwald     // Flags general discoverable
77*bcf00d8fSMatthias Ringwald     0x02, 0x01, 0x02,
78*bcf00d8fSMatthias Ringwald     // Name
79*bcf00d8fSMatthias Ringwald     0x05, 0x09, 'A', 'N', 'C', 'S',
80*bcf00d8fSMatthias Ringwald     // Service Solicitation, 128-bit UUIDs - ANCS (little endian)
81*bcf00d8fSMatthias Ringwald     0x11,0x15,0xD0,0x00,0x2D,0x12,0x1E,0x4B,0x0F,0xA4,0x99,0x4E,0xCE,0xB5,0x31,0xF4,0x05,0x79
82*bcf00d8fSMatthias Ringwald };
83*bcf00d8fSMatthias Ringwald static uint8_t adv_data_len = sizeof(adv_data);
84*bcf00d8fSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
85*bcf00d8fSMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration;
86*bcf00d8fSMatthias Ringwald 
87*bcf00d8fSMatthias Ringwald static void app_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
88*bcf00d8fSMatthias Ringwald     switch (packet_type) {
89*bcf00d8fSMatthias Ringwald         case HCI_EVENT_PACKET:
90*bcf00d8fSMatthias Ringwald             switch (packet[0]) {
91*bcf00d8fSMatthias Ringwald                 case BTSTACK_EVENT_STATE:
92*bcf00d8fSMatthias Ringwald                     if (packet[2] == HCI_STATE_WORKING) {
93*bcf00d8fSMatthias Ringwald                         printf("ANCS Client Demo ready.\n");
94*bcf00d8fSMatthias Ringwald                     }
95*bcf00d8fSMatthias Ringwald                     break;
96*bcf00d8fSMatthias Ringwald                 case SM_EVENT_JUST_WORKS_REQUEST:
97*bcf00d8fSMatthias Ringwald                     sm_just_works_confirm(sm_event_just_works_request_get_handle(packet));
98*bcf00d8fSMatthias Ringwald                     printf("Just Works Confirmed.\n");
99*bcf00d8fSMatthias Ringwald                     break;
100*bcf00d8fSMatthias Ringwald                 case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
101*bcf00d8fSMatthias Ringwald                     printf("Passkey display: %06u.\n", sm_event_passkey_display_number_get_passkey(packet));
102*bcf00d8fSMatthias Ringwald                     break;
103*bcf00d8fSMatthias Ringwald             }
104*bcf00d8fSMatthias Ringwald             break;
105*bcf00d8fSMatthias Ringwald     }
106*bcf00d8fSMatthias Ringwald }
107*bcf00d8fSMatthias Ringwald 
108*bcf00d8fSMatthias Ringwald static void ancs_callback(uint8_t packet_type, uint8_t *packet, uint16_t size){
109*bcf00d8fSMatthias Ringwald     const char * attribute_name;
110*bcf00d8fSMatthias Ringwald     if (packet[0] != HCI_EVENT_ANCS_META) return;
111*bcf00d8fSMatthias Ringwald     switch (packet[2]){
112*bcf00d8fSMatthias Ringwald         case ANCS_SUBEVENT_CLIENT_CONNECTED:
113*bcf00d8fSMatthias Ringwald             printf("ANCS Client: Connected\n");
114*bcf00d8fSMatthias Ringwald             break;
115*bcf00d8fSMatthias Ringwald         case ANCS_SUBEVENT_CLIENT_DISCONNECTED:
116*bcf00d8fSMatthias Ringwald             printf("ANCS Client: Disconnected\n");
117*bcf00d8fSMatthias Ringwald             break;
118*bcf00d8fSMatthias Ringwald         case ANCS_SUBEVENT_CLIENT_NOTIFICATION:
119*bcf00d8fSMatthias Ringwald             attribute_name = ancs_client_attribute_name_for_id(ancs_subevent_client_notification_get_attribute_id(packet));
120*bcf00d8fSMatthias Ringwald             if (!attribute_name) break;
121*bcf00d8fSMatthias Ringwald             printf("Notification: %s - %s\n", attribute_name, ancs_subevent_client_notification_get_text(packet));
122*bcf00d8fSMatthias Ringwald             break;
123*bcf00d8fSMatthias Ringwald         default:
124*bcf00d8fSMatthias Ringwald             break;
125*bcf00d8fSMatthias Ringwald     }
126*bcf00d8fSMatthias Ringwald }
127*bcf00d8fSMatthias Ringwald 
128*bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]);
129*bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]){
130*bcf00d8fSMatthias Ringwald 
131*bcf00d8fSMatthias Ringwald     printf("BTstack ANCS Client starting up...\n");
132*bcf00d8fSMatthias Ringwald 
133*bcf00d8fSMatthias Ringwald     // register for HCI events
134*bcf00d8fSMatthias Ringwald     hci_event_callback_registration.callback = &app_packet_handler;
135*bcf00d8fSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
136*bcf00d8fSMatthias Ringwald 
137*bcf00d8fSMatthias Ringwald     // set up l2cap_le
138*bcf00d8fSMatthias Ringwald     l2cap_init();
139*bcf00d8fSMatthias Ringwald 
140*bcf00d8fSMatthias Ringwald     // setup le device db
141*bcf00d8fSMatthias Ringwald     le_device_db_init();
142*bcf00d8fSMatthias Ringwald 
143*bcf00d8fSMatthias Ringwald     // setup SM: Display only
144*bcf00d8fSMatthias Ringwald     sm_init();
145*bcf00d8fSMatthias Ringwald     sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY);
146*bcf00d8fSMatthias Ringwald     sm_set_authentication_requirements( SM_AUTHREQ_BONDING );
147*bcf00d8fSMatthias Ringwald 
148*bcf00d8fSMatthias Ringwald     // register for SM events
149*bcf00d8fSMatthias Ringwald     sm_event_callback_registration.callback = &app_packet_handler;
150*bcf00d8fSMatthias Ringwald     hci_add_event_handler(&sm_event_callback_registration);
151*bcf00d8fSMatthias Ringwald 
152*bcf00d8fSMatthias Ringwald     // setup ATT server
153*bcf00d8fSMatthias Ringwald     att_server_init(profile_data, NULL, NULL);
154*bcf00d8fSMatthias Ringwald     att_server_register_packet_handler(app_packet_handler);
155*bcf00d8fSMatthias Ringwald 
156*bcf00d8fSMatthias Ringwald     // setup GATT client
157*bcf00d8fSMatthias Ringwald     gatt_client_init();
158*bcf00d8fSMatthias Ringwald 
159*bcf00d8fSMatthias Ringwald     // setup ANCS Client
160*bcf00d8fSMatthias Ringwald     ancs_client_init();
161*bcf00d8fSMatthias Ringwald     ancs_client_register_callback(&ancs_callback);
162*bcf00d8fSMatthias Ringwald 
163*bcf00d8fSMatthias Ringwald     // setup advertisements
164*bcf00d8fSMatthias Ringwald     uint16_t adv_int_min = 0x0030;
165*bcf00d8fSMatthias Ringwald     uint16_t adv_int_max = 0x0030;
166*bcf00d8fSMatthias Ringwald     uint8_t adv_type = 0;
167*bcf00d8fSMatthias Ringwald     bd_addr_t null_addr;
168*bcf00d8fSMatthias Ringwald     memset(null_addr, 0, 6);
169*bcf00d8fSMatthias Ringwald     gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
170*bcf00d8fSMatthias Ringwald     gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
171*bcf00d8fSMatthias Ringwald     gap_advertisements_enable(1);
172*bcf00d8fSMatthias Ringwald 
173*bcf00d8fSMatthias Ringwald     // turn on!
174*bcf00d8fSMatthias Ringwald     hci_power_control(HCI_POWER_ON);
175*bcf00d8fSMatthias Ringwald 
176*bcf00d8fSMatthias Ringwald     return 0;
177*bcf00d8fSMatthias Ringwald }
178