xref: /btstack/example/ancs_client_demo.c (revision 9d65c9925326973cebda440e26e0ed853739da70)
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 BLUEKITCHEN
24  * GMBH 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__ "ancs_client_demo.c"
39 
40 // *****************************************************************************
41 /* EXAMPLE_START(ancs_client_demo): LE ANCS Client - Apple Notification Service
42  *
43  */
44 #include <stdint.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <inttypes.h>
49 
50 #include "btstack.h"
51 
52 // TODO: query full text upon notification using control point
53 // TODO: present notifications in human readable form
54 
55 // ancs_client_demo.gatt contains the declaration of the provided GATT Services + Characteristics
56 // ancs_client_demo.h    contains the binary representation of ancs_client_demo.gatt
57 // it is generated by the build system by calling: $BTSTACK_ROOT/tool/compile_gatt.py ancs_client_demo.gatt ancs_client_demo.h
58 // it needs to be regenerated when the GATT Database declared in ancs_client_demo.gatt file is modified
59 #include "ancs_client_demo.h"
60 
61 static const uint8_t adv_data[] = {
62     // Flags general discoverable, BR/EDR not supported
63     0x02, 0x01, 0x06,
64     // Name
65     0x05, 0x09, 'A', 'N', 'C', 'S',
66     // Service Solicitation, 128-bit UUIDs - ANCS (little endian)
67     0x11,0x15,0xD0,0x00,0x2D,0x12,0x1E,0x4B,0x0F,0xA4,0x99,0x4E,0xCE,0xB5,0x31,0xF4,0x05,0x79
68 };
69 static uint8_t adv_data_len = sizeof(adv_data);
70 static btstack_packet_callback_registration_t hci_event_callback_registration;
71 static btstack_packet_callback_registration_t sm_event_callback_registration;
72 
app_packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)73 static void app_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
74     UNUSED(channel);
75     UNUSED(size);
76 
77     if (packet_type != HCI_EVENT_PACKET) return;
78 
79     switch (hci_event_packet_get_type(packet)) {
80         case SM_EVENT_JUST_WORKS_REQUEST:
81             sm_just_works_confirm(sm_event_just_works_request_get_handle(packet));
82             printf("Just Works Confirmed.\n");
83             break;
84         case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
85             printf("Passkey display: %"PRIu32"\n", sm_event_passkey_display_number_get_passkey(packet));
86             break;
87         default:
88             break;
89     }
90 }
91 
ancs_callback(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)92 static void ancs_callback(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
93     UNUSED(packet_type);
94     UNUSED(channel);
95     UNUSED(size);
96 
97     const char * attribute_name;
98     if (hci_event_packet_get_type(packet) != HCI_EVENT_ANCS_META) return;
99     switch (hci_event_ancs_meta_get_subevent_code(packet)){
100         case ANCS_SUBEVENT_CLIENT_CONNECTED:
101             printf("ANCS Client: Connected\n");
102             break;
103         case ANCS_SUBEVENT_CLIENT_DISCONNECTED:
104             printf("ANCS Client: Disconnected\n");
105             break;
106         case ANCS_SUBEVENT_CLIENT_NOTIFICATION:
107             attribute_name = ancs_client_attribute_name_for_id(ancs_subevent_client_notification_get_attribute_id(packet));
108             if (!attribute_name) break;
109             printf("Notification: %s - %s\n", attribute_name, ancs_subevent_client_notification_get_text(packet));
110             break;
111         default:
112             break;
113     }
114 }
115 
116 int btstack_main(int argc, const char * argv[]);
btstack_main(int argc,const char * argv[])117 int btstack_main(int argc, const char * argv[]){
118     (void)argc;
119     (void)argv;
120 
121     printf("BTstack ANCS Client starting up...\n");
122 
123 
124     // set up l2cap_le
125     l2cap_init();
126 
127     // setup SM: Display only
128     sm_init();
129     sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY);
130     sm_set_authentication_requirements( SM_AUTHREQ_BONDING );
131 
132     // register for HCI events
133     hci_event_callback_registration.callback = &app_packet_handler;
134     hci_add_event_handler(&hci_event_callback_registration);
135 
136     // register for SM events
137     sm_event_callback_registration.callback = &app_packet_handler;
138     sm_add_event_handler(&sm_event_callback_registration);
139 
140     // setup ATT server
141     att_server_init(profile_data, NULL, NULL);
142 
143     // setup ANCS Client
144     ancs_client_init();
145 
146 
147     // register for ATT Serer events
148     att_server_register_packet_handler(app_packet_handler);
149 
150     // setup GATT client
151     gatt_client_init();
152 
153     // register for ancs events
154     ancs_client_register_callback(&ancs_callback);
155 
156     // setup advertisements
157     uint16_t adv_int_min = 0x0030;
158     uint16_t adv_int_max = 0x0030;
159     uint8_t adv_type = 0;
160     bd_addr_t null_addr;
161     memset(null_addr, 0, 6);
162     gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
163     gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
164     gap_advertisements_enable(1);
165 
166     // turn on!
167     hci_power_control(HCI_POWER_ON);
168 
169     return 0;
170 }
171 
172 /* EXAMPLE_END */
173