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