16bdecec7SMatthias Ringwald /* 26bdecec7SMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 36bdecec7SMatthias Ringwald * 46bdecec7SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 56bdecec7SMatthias Ringwald * modification, are permitted provided that the following conditions 66bdecec7SMatthias Ringwald * are met: 76bdecec7SMatthias Ringwald * 86bdecec7SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 96bdecec7SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 106bdecec7SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 116bdecec7SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 126bdecec7SMatthias Ringwald * documentation and/or other materials provided with the distribution. 136bdecec7SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 146bdecec7SMatthias Ringwald * contributors may be used to endorse or promote products derived 156bdecec7SMatthias Ringwald * from this software without specific prior written permission. 166bdecec7SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 176bdecec7SMatthias Ringwald * personal benefit and not for any commercial purpose or for 186bdecec7SMatthias Ringwald * monetary gain. 196bdecec7SMatthias Ringwald * 206bdecec7SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 216bdecec7SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 226bdecec7SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 236bdecec7SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 246bdecec7SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 256bdecec7SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 266bdecec7SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 276bdecec7SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 286bdecec7SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 296bdecec7SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 306bdecec7SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 316bdecec7SMatthias Ringwald * SUCH DAMAGE. 326bdecec7SMatthias Ringwald * 336bdecec7SMatthias Ringwald * Please inquire about commercial licensing options at 346bdecec7SMatthias Ringwald * [email protected] 356bdecec7SMatthias Ringwald * 366bdecec7SMatthias Ringwald */ 376bdecec7SMatthias Ringwald 386bdecec7SMatthias Ringwald #define BTSTACK_FILE__ "ancs_client.c" 396bdecec7SMatthias Ringwald 406bdecec7SMatthias Ringwald #include "btstack_config.h" 416bdecec7SMatthias Ringwald 426bdecec7SMatthias Ringwald #include <stdint.h> 436bdecec7SMatthias Ringwald #include <string.h> 446bdecec7SMatthias Ringwald 456bdecec7SMatthias Ringwald #include "ble/gatt-service/ancs_client.h" 466bdecec7SMatthias Ringwald 476bdecec7SMatthias Ringwald #include "ble/core.h" 486bdecec7SMatthias Ringwald #include "ble/gatt_client.h" 496bdecec7SMatthias Ringwald #include "ble/sm.h" 506bdecec7SMatthias Ringwald #include "btstack_debug.h" 516bdecec7SMatthias Ringwald #include "btstack_event.h" 526bdecec7SMatthias Ringwald #include "gap.h" 536bdecec7SMatthias Ringwald 546bdecec7SMatthias Ringwald // ancs_client.h Start 556bdecec7SMatthias Ringwald typedef enum ancs_chunk_parser_state { 566bdecec7SMatthias Ringwald W4_ATTRIBUTE_ID, 576bdecec7SMatthias Ringwald W4_ATTRIBUTE_LEN, 586bdecec7SMatthias Ringwald W4_ATTRIBUTE_COMPLETE, 596bdecec7SMatthias Ringwald } ancs_chunk_parser_state_t; 606bdecec7SMatthias Ringwald 616bdecec7SMatthias Ringwald typedef enum { 626bdecec7SMatthias Ringwald TC_IDLE, 636bdecec7SMatthias Ringwald TC_W4_ENCRYPTED_CONNECTION, 646bdecec7SMatthias Ringwald TC_W4_SERVICE_RESULT, 656bdecec7SMatthias Ringwald TC_W4_CHARACTERISTIC_RESULT, 666bdecec7SMatthias Ringwald TC_W4_DATA_SOURCE_SUBSCRIBED, 676bdecec7SMatthias Ringwald TC_W4_NOTIFICATION_SOURCE_SUBSCRIBED, 686bdecec7SMatthias Ringwald TC_SUBSCRIBED, 696bdecec7SMatthias Ringwald TC_W4_DISCONNECT 706bdecec7SMatthias Ringwald } tc_state_t; 716bdecec7SMatthias Ringwald 726bdecec7SMatthias Ringwald static uint32_t ancs_notification_uid; 736bdecec7SMatthias Ringwald static uint16_t gc_handle; 746bdecec7SMatthias Ringwald static gatt_client_notification_t ancs_notification_source_notification; 756bdecec7SMatthias Ringwald static gatt_client_notification_t ancs_data_source_notification; 766bdecec7SMatthias Ringwald static int ancs_service_found; 776bdecec7SMatthias Ringwald static gatt_client_service_t ancs_service; 786bdecec7SMatthias Ringwald static gatt_client_characteristic_t ancs_notification_source_characteristic; 796bdecec7SMatthias Ringwald static gatt_client_characteristic_t ancs_control_point_characteristic; 806bdecec7SMatthias Ringwald static gatt_client_characteristic_t ancs_data_source_characteristic; 816bdecec7SMatthias Ringwald static int ancs_characteristcs; 826bdecec7SMatthias Ringwald static tc_state_t tc_state = TC_IDLE; 836bdecec7SMatthias Ringwald 846bdecec7SMatthias Ringwald static ancs_chunk_parser_state_t chunk_parser_state; 856bdecec7SMatthias Ringwald static uint8_t ancs_notification_buffer[50]; 866bdecec7SMatthias Ringwald static uint16_t ancs_bytes_received; 876bdecec7SMatthias Ringwald static uint16_t ancs_bytes_needed; 886bdecec7SMatthias Ringwald static uint8_t ancs_attribute_id; 896bdecec7SMatthias Ringwald static uint16_t ancs_attribute_len; 906bdecec7SMatthias Ringwald 916bdecec7SMatthias Ringwald static btstack_packet_handler_t client_handler; 926bdecec7SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 936bdecec7SMatthias Ringwald 94*e115a09fSMatthias Ringwald static void ancs_client_handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 95*e115a09fSMatthias Ringwald 966bdecec7SMatthias Ringwald void ancs_client_register_callback(btstack_packet_handler_t handler){ 976bdecec7SMatthias Ringwald client_handler = handler; 986bdecec7SMatthias Ringwald } 996bdecec7SMatthias Ringwald 1006bdecec7SMatthias Ringwald static void notify_client_text(int event_type){ 1016bdecec7SMatthias Ringwald if (!client_handler) return; 1026bdecec7SMatthias Ringwald uint8_t event[7 + sizeof(ancs_notification_buffer) + 1]; 1036bdecec7SMatthias Ringwald event[0] = HCI_EVENT_ANCS_META; 1046bdecec7SMatthias Ringwald event[1] = 5u + ancs_attribute_len; 1056bdecec7SMatthias Ringwald event[2] = event_type; 1066bdecec7SMatthias Ringwald little_endian_store_16(event, 3, gc_handle); 1076bdecec7SMatthias Ringwald little_endian_store_16(event, 5, ancs_attribute_id); 1086bdecec7SMatthias Ringwald (void)memcpy(&event[7], ancs_notification_buffer, ancs_attribute_len); 1096bdecec7SMatthias Ringwald // we're nice 1106bdecec7SMatthias Ringwald event[7u+ancs_attribute_len] = 0u; 1116bdecec7SMatthias Ringwald (*client_handler)(HCI_EVENT_PACKET, 0u, event, event[1u] + 2u); 1126bdecec7SMatthias Ringwald } 1136bdecec7SMatthias Ringwald 1146bdecec7SMatthias Ringwald static void notify_client_simple(int event_type){ 1156bdecec7SMatthias Ringwald if (!client_handler) return; 1166bdecec7SMatthias Ringwald uint8_t event[5]; 1176bdecec7SMatthias Ringwald event[0] = HCI_EVENT_ANCS_META; 1186bdecec7SMatthias Ringwald event[1] = 3; 1196bdecec7SMatthias Ringwald event[2] = event_type; 1206bdecec7SMatthias Ringwald little_endian_store_16(event, 3, gc_handle); 1216bdecec7SMatthias Ringwald (*client_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 1226bdecec7SMatthias Ringwald } 1236bdecec7SMatthias Ringwald 1246bdecec7SMatthias Ringwald static void ancs_chunk_parser_init(void){ 12541866fd3SMatthias Ringwald // skip comand id and notification uid 1266bdecec7SMatthias Ringwald chunk_parser_state = W4_ATTRIBUTE_ID; 1276bdecec7SMatthias Ringwald ancs_bytes_received = 0; 1286bdecec7SMatthias Ringwald ancs_bytes_needed = 6; 1296bdecec7SMatthias Ringwald } 1306bdecec7SMatthias Ringwald 1316bdecec7SMatthias Ringwald const char * ancs_client_attribute_name_for_id(int id){ 1326bdecec7SMatthias Ringwald static const char * ancs_attribute_names[] = { 1336bdecec7SMatthias Ringwald "AppIdentifier", 1346bdecec7SMatthias Ringwald "IDTitle", 1356bdecec7SMatthias Ringwald "IDSubtitle", 1366bdecec7SMatthias Ringwald "IDMessage", 1376bdecec7SMatthias Ringwald "IDMessageSize", 1386bdecec7SMatthias Ringwald "IDDate" 1396bdecec7SMatthias Ringwald }; 1406bdecec7SMatthias Ringwald 14141866fd3SMatthias Ringwald static const uint16_t ANCS_ATTRIBUTE_NAMES_COUNT = sizeof(ancs_attribute_names) / sizeof(char *); 1426bdecec7SMatthias Ringwald 14341866fd3SMatthias Ringwald if (id >= ANCS_ATTRIBUTE_NAMES_COUNT) return NULL; 1446bdecec7SMatthias Ringwald return ancs_attribute_names[id]; 1456bdecec7SMatthias Ringwald } 1466bdecec7SMatthias Ringwald 1476bdecec7SMatthias Ringwald static void ancs_chunk_parser_handle_byte(uint8_t data){ 1486bdecec7SMatthias Ringwald ancs_notification_buffer[ancs_bytes_received++] = data; 1496bdecec7SMatthias Ringwald if (ancs_bytes_received < ancs_bytes_needed) return; 1506bdecec7SMatthias Ringwald switch (chunk_parser_state){ 1516bdecec7SMatthias Ringwald case W4_ATTRIBUTE_ID: 1526bdecec7SMatthias Ringwald ancs_attribute_id = ancs_notification_buffer[ancs_bytes_received-1u]; 1536bdecec7SMatthias Ringwald ancs_bytes_received = 0; 1546bdecec7SMatthias Ringwald ancs_bytes_needed = 2; 1556bdecec7SMatthias Ringwald chunk_parser_state = W4_ATTRIBUTE_LEN; 1566bdecec7SMatthias Ringwald break; 1576bdecec7SMatthias Ringwald case W4_ATTRIBUTE_LEN: 1586bdecec7SMatthias Ringwald ancs_attribute_len = little_endian_read_16(ancs_notification_buffer, ancs_bytes_received-2u); 1596bdecec7SMatthias Ringwald ancs_bytes_received = 0; 1606bdecec7SMatthias Ringwald ancs_bytes_needed = ancs_attribute_len; 1616bdecec7SMatthias Ringwald if (ancs_attribute_len == 0u) { 1626bdecec7SMatthias Ringwald ancs_bytes_needed = 1; 1636bdecec7SMatthias Ringwald chunk_parser_state = W4_ATTRIBUTE_ID; 1646bdecec7SMatthias Ringwald break; 1656bdecec7SMatthias Ringwald } 1666bdecec7SMatthias Ringwald chunk_parser_state = W4_ATTRIBUTE_COMPLETE; 1676bdecec7SMatthias Ringwald break; 1686bdecec7SMatthias Ringwald case W4_ATTRIBUTE_COMPLETE: 1696bdecec7SMatthias Ringwald ancs_notification_buffer[ancs_bytes_received] = 0; 1706bdecec7SMatthias Ringwald notify_client_text(ANCS_SUBEVENT_CLIENT_NOTIFICATION); 1716bdecec7SMatthias Ringwald ancs_bytes_received = 0; 1726bdecec7SMatthias Ringwald ancs_bytes_needed = 1; 1736bdecec7SMatthias Ringwald chunk_parser_state = W4_ATTRIBUTE_ID; 1746bdecec7SMatthias Ringwald break; 1756bdecec7SMatthias Ringwald default: 17641866fd3SMatthias Ringwald btstack_unreachable(); 1776bdecec7SMatthias Ringwald break; 1786bdecec7SMatthias Ringwald } 1796bdecec7SMatthias Ringwald } 1806bdecec7SMatthias Ringwald 181*e115a09fSMatthias Ringwald static void ancs_client_handle_notification(uint8_t * packet, uint16_t size){ 182*e115a09fSMatthias Ringwald uint16_t value_handle = little_endian_read_16(packet, 4); 183*e115a09fSMatthias Ringwald uint16_t value_length = little_endian_read_16(packet, 6); 184*e115a09fSMatthias Ringwald uint8_t * value = &packet[8]; 1856bdecec7SMatthias Ringwald 186*e115a09fSMatthias Ringwald log_info("ANCS Notification, value handle %u", value_handle); 1876bdecec7SMatthias Ringwald 188*e115a09fSMatthias Ringwald if (value_handle == ancs_data_source_characteristic.value_handle){ 189*e115a09fSMatthias Ringwald int i; 190*e115a09fSMatthias Ringwald for (i=0;i<value_length;i++) { 191*e115a09fSMatthias Ringwald ancs_chunk_parser_handle_byte(value[i]); 192*e115a09fSMatthias Ringwald } 193*e115a09fSMatthias Ringwald } else if (value_handle == ancs_notification_source_characteristic.value_handle){ 194*e115a09fSMatthias Ringwald ancs_notification_uid = little_endian_read_32(value, 4); 195*e115a09fSMatthias Ringwald log_info("Notification received: EventID %02x, EventFlags %02x, CategoryID %02x, CategoryCount %u, UID %04x", 196*e115a09fSMatthias Ringwald value[0], value[1], value[2], value[3], (int) ancs_notification_uid); 197*e115a09fSMatthias Ringwald static uint8_t get_notification_attributes[] = {0, 0,0,0,0, 0, 1,32,0, 2,32,0, 3,32,0, 4, 5}; 198*e115a09fSMatthias Ringwald little_endian_store_32(get_notification_attributes, 1, ancs_notification_uid); 199*e115a09fSMatthias Ringwald ancs_notification_uid = 0; 200*e115a09fSMatthias Ringwald ancs_chunk_parser_init(); 201*e115a09fSMatthias Ringwald gatt_client_write_value_of_characteristic(ancs_client_handle_gatt_client_event, gc_handle, ancs_control_point_characteristic.value_handle, 202*e115a09fSMatthias Ringwald sizeof(get_notification_attributes), get_notification_attributes); 203*e115a09fSMatthias Ringwald } else { 204*e115a09fSMatthias Ringwald log_info("Unknown Source: "); 205*e115a09fSMatthias Ringwald log_info_hexdump(value , value_length); 206*e115a09fSMatthias Ringwald } 207*e115a09fSMatthias Ringwald } 208*e115a09fSMatthias Ringwald 209*e115a09fSMatthias Ringwald static void ancs_client_handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 210*e115a09fSMatthias Ringwald 2116bdecec7SMatthias Ringwald static const uint8_t ancs_notification_source_uuid[] = {0x9F,0xBF,0x12,0x0D,0x63,0x01,0x42,0xD9,0x8C,0x58,0x25,0xE6,0x99,0xA2,0x1D,0xBD}; 2126bdecec7SMatthias Ringwald static const uint8_t ancs_control_point_uuid[] = {0x69,0xD1,0xD8,0xF3,0x45,0xE1,0x49,0xA8,0x98,0x21,0x9B,0xBD,0xFD,0xAA,0xD9,0xD9}; 2136bdecec7SMatthias Ringwald static const uint8_t ancs_data_source_uuid[] = {0x22,0xEA,0xC6,0xE9,0x24,0xD6,0x4B,0xB5,0xBE,0x44,0xB3,0x6A,0xCE,0x7C,0x7B,0xFB}; 2146bdecec7SMatthias Ringwald 2156bdecec7SMatthias Ringwald gatt_client_characteristic_t characteristic; 2166bdecec7SMatthias Ringwald 2176bdecec7SMatthias Ringwald switch(tc_state){ 2186bdecec7SMatthias Ringwald case TC_W4_SERVICE_RESULT: 2196bdecec7SMatthias Ringwald switch(hci_event_packet_get_type(packet)){ 2206bdecec7SMatthias Ringwald case GATT_EVENT_SERVICE_QUERY_RESULT: 2216bdecec7SMatthias Ringwald gatt_event_service_query_result_get_service(packet, &ancs_service); 2226bdecec7SMatthias Ringwald ancs_service_found = 1; 2236bdecec7SMatthias Ringwald break; 2246bdecec7SMatthias Ringwald case GATT_EVENT_QUERY_COMPLETE: 2256bdecec7SMatthias Ringwald if (!ancs_service_found){ 2266bdecec7SMatthias Ringwald log_info("ANCS Service not found"); 2276bdecec7SMatthias Ringwald tc_state = TC_IDLE; 2286bdecec7SMatthias Ringwald break; 2296bdecec7SMatthias Ringwald } 2306bdecec7SMatthias Ringwald tc_state = TC_W4_CHARACTERISTIC_RESULT; 2316bdecec7SMatthias Ringwald log_info("ANCS Client - Discover characteristics for ANCS SERVICE "); 232*e115a09fSMatthias Ringwald gatt_client_discover_characteristics_for_service(ancs_client_handle_gatt_client_event, gc_handle, &ancs_service); 2336bdecec7SMatthias Ringwald break; 2346bdecec7SMatthias Ringwald default: 2356bdecec7SMatthias Ringwald break; 2366bdecec7SMatthias Ringwald } 2376bdecec7SMatthias Ringwald break; 2386bdecec7SMatthias Ringwald 2396bdecec7SMatthias Ringwald case TC_W4_CHARACTERISTIC_RESULT: 2406bdecec7SMatthias Ringwald switch(hci_event_packet_get_type(packet)){ 2416bdecec7SMatthias Ringwald case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT: 2426bdecec7SMatthias Ringwald gatt_event_characteristic_query_result_get_characteristic(packet, &characteristic); 2436bdecec7SMatthias Ringwald if (memcmp(characteristic.uuid128, ancs_notification_source_uuid, 16) == 0){ 2446bdecec7SMatthias Ringwald log_info("ANCS Notification Source found, attribute handle %u", characteristic.value_handle); 2456bdecec7SMatthias Ringwald ancs_notification_source_characteristic = characteristic; 2466bdecec7SMatthias Ringwald ancs_characteristcs++; 2476bdecec7SMatthias Ringwald break; 2486bdecec7SMatthias Ringwald } 2496bdecec7SMatthias Ringwald if (memcmp(characteristic.uuid128, ancs_control_point_uuid, 16) == 0){ 2506bdecec7SMatthias Ringwald log_info("ANCS Control Point found, attribute handle %u", characteristic.value_handle); 2516bdecec7SMatthias Ringwald ancs_control_point_characteristic = characteristic; 2526bdecec7SMatthias Ringwald ancs_characteristcs++; 2536bdecec7SMatthias Ringwald break; 2546bdecec7SMatthias Ringwald } 2556bdecec7SMatthias Ringwald if (memcmp(characteristic.uuid128, ancs_data_source_uuid, 16) == 0){ 2566bdecec7SMatthias Ringwald log_info("ANCS Data Source found, attribute handle %u", characteristic.value_handle); 2576bdecec7SMatthias Ringwald ancs_data_source_characteristic = characteristic; 2586bdecec7SMatthias Ringwald ancs_characteristcs++; 2596bdecec7SMatthias Ringwald break; 2606bdecec7SMatthias Ringwald } 2616bdecec7SMatthias Ringwald break; 2626bdecec7SMatthias Ringwald case GATT_EVENT_QUERY_COMPLETE: 2636bdecec7SMatthias Ringwald log_info("ANCS Characteristcs count %u", ancs_characteristcs); 2646bdecec7SMatthias Ringwald tc_state = TC_W4_NOTIFICATION_SOURCE_SUBSCRIBED; 265*e115a09fSMatthias Ringwald gatt_client_listen_for_characteristic_value_updates(&ancs_notification_source_notification, &ancs_client_handle_gatt_client_event, gc_handle, &ancs_notification_source_characteristic); 266*e115a09fSMatthias Ringwald gatt_client_write_client_characteristic_configuration(ancs_client_handle_gatt_client_event, gc_handle, &ancs_notification_source_characteristic, 2676bdecec7SMatthias Ringwald GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION); 2686bdecec7SMatthias Ringwald break; 2696bdecec7SMatthias Ringwald default: 2706bdecec7SMatthias Ringwald break; 2716bdecec7SMatthias Ringwald } 2726bdecec7SMatthias Ringwald break; 2736bdecec7SMatthias Ringwald case TC_W4_NOTIFICATION_SOURCE_SUBSCRIBED: 2746bdecec7SMatthias Ringwald switch(hci_event_packet_get_type(packet)){ 2756bdecec7SMatthias Ringwald case GATT_EVENT_QUERY_COMPLETE: 2766bdecec7SMatthias Ringwald log_info("ANCS Notification Source subscribed"); 2776bdecec7SMatthias Ringwald tc_state = TC_W4_DATA_SOURCE_SUBSCRIBED; 278*e115a09fSMatthias Ringwald gatt_client_listen_for_characteristic_value_updates(&ancs_data_source_notification, &ancs_client_handle_gatt_client_event, gc_handle, &ancs_data_source_characteristic); 279*e115a09fSMatthias Ringwald gatt_client_write_client_characteristic_configuration(ancs_client_handle_gatt_client_event, gc_handle, &ancs_data_source_characteristic, 2806bdecec7SMatthias Ringwald GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION); 2816bdecec7SMatthias Ringwald break; 2826bdecec7SMatthias Ringwald default: 2836bdecec7SMatthias Ringwald break; 2846bdecec7SMatthias Ringwald } 2856bdecec7SMatthias Ringwald break; 2866bdecec7SMatthias Ringwald case TC_W4_DATA_SOURCE_SUBSCRIBED: 2876bdecec7SMatthias Ringwald switch(hci_event_packet_get_type(packet)){ 2886bdecec7SMatthias Ringwald case GATT_EVENT_QUERY_COMPLETE: 2896bdecec7SMatthias Ringwald log_info("ANCS Data Source subscribed"); 2906bdecec7SMatthias Ringwald tc_state = TC_SUBSCRIBED; 2916bdecec7SMatthias Ringwald notify_client_simple(ANCS_SUBEVENT_CLIENT_CONNECTED); 2926bdecec7SMatthias Ringwald break; 2936bdecec7SMatthias Ringwald default: 2946bdecec7SMatthias Ringwald break; 2956bdecec7SMatthias Ringwald } 2966bdecec7SMatthias Ringwald break; 2976bdecec7SMatthias Ringwald case TC_SUBSCRIBED: 29841866fd3SMatthias Ringwald switch(hci_event_packet_get_type(packet)){ 29941866fd3SMatthias Ringwald case GATT_EVENT_NOTIFICATION: 30041866fd3SMatthias Ringwald case GATT_EVENT_INDICATION: 301*e115a09fSMatthias Ringwald ancs_client_handle_notification(packet, size); 3026bdecec7SMatthias Ringwald break; 3036bdecec7SMatthias Ringwald default: 3046bdecec7SMatthias Ringwald break; 3056bdecec7SMatthias Ringwald } 30641866fd3SMatthias Ringwald break; 30741866fd3SMatthias Ringwald default: 30841866fd3SMatthias Ringwald break; 30941866fd3SMatthias Ringwald } 310*e115a09fSMatthias Ringwald } 311*e115a09fSMatthias Ringwald 312*e115a09fSMatthias Ringwald static void handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 313*e115a09fSMatthias Ringwald 314*e115a09fSMatthias Ringwald UNUSED(packet_type); // ok: only hci events 315*e115a09fSMatthias Ringwald UNUSED(channel); // ok: there is no channel 316*e115a09fSMatthias Ringwald UNUSED(size); // ok: fixed format events read from HCI buffer 317*e115a09fSMatthias Ringwald 318*e115a09fSMatthias Ringwald static const uint8_t ancs_service_uuid[] = {0x79,0x05,0xF4,0x31,0xB5,0xCE,0x4E,0x99,0xA4,0x0F,0x4B,0x1E,0x12,0x2D,0x00,0xD0}; 319*e115a09fSMatthias Ringwald 320*e115a09fSMatthias Ringwald int connection_encrypted; 321*e115a09fSMatthias Ringwald 322*e115a09fSMatthias Ringwald // handle connect / disconncet events first 323*e115a09fSMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 324*e115a09fSMatthias Ringwald case HCI_EVENT_LE_META: 325*e115a09fSMatthias Ringwald switch (packet[2]) { 326*e115a09fSMatthias Ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 327*e115a09fSMatthias Ringwald gc_handle = little_endian_read_16(packet, 4); 328*e115a09fSMatthias Ringwald log_info("Connection handle 0x%04x, request encryption", gc_handle); 329*e115a09fSMatthias Ringwald 330*e115a09fSMatthias Ringwald // we need to be paired to enable notifications 331*e115a09fSMatthias Ringwald tc_state = TC_W4_ENCRYPTED_CONNECTION; 332*e115a09fSMatthias Ringwald ancs_service_found = false; 333*e115a09fSMatthias Ringwald sm_request_pairing(gc_handle); 334*e115a09fSMatthias Ringwald break; 335*e115a09fSMatthias Ringwald default: 336*e115a09fSMatthias Ringwald break; 337*e115a09fSMatthias Ringwald } 338*e115a09fSMatthias Ringwald return; 339*e115a09fSMatthias Ringwald 340*e115a09fSMatthias Ringwald case HCI_EVENT_ENCRYPTION_CHANGE: 341*e115a09fSMatthias Ringwald if (gc_handle != little_endian_read_16(packet, 3)) return; 342*e115a09fSMatthias Ringwald connection_encrypted = packet[5]; 343*e115a09fSMatthias Ringwald log_info("Encryption state change: %u", connection_encrypted); 344*e115a09fSMatthias Ringwald if (!connection_encrypted) return; 345*e115a09fSMatthias Ringwald if (tc_state != TC_W4_ENCRYPTED_CONNECTION) return; 346*e115a09fSMatthias Ringwald 347*e115a09fSMatthias Ringwald // let's start 348*e115a09fSMatthias Ringwald log_info("\nANCS Client - CONNECTED, discover ANCS service"); 349*e115a09fSMatthias Ringwald tc_state = TC_W4_SERVICE_RESULT; 350*e115a09fSMatthias Ringwald gatt_client_discover_primary_services_by_uuid128(ancs_client_handle_gatt_client_event, gc_handle, ancs_service_uuid); 351*e115a09fSMatthias Ringwald return; 352*e115a09fSMatthias Ringwald 353*e115a09fSMatthias Ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 354*e115a09fSMatthias Ringwald if (hci_event_disconnection_complete_get_connection_handle(packet) != gc_handle) break; 355*e115a09fSMatthias Ringwald if (tc_state == TC_SUBSCRIBED){ 356*e115a09fSMatthias Ringwald notify_client_simple(ANCS_SUBEVENT_CLIENT_DISCONNECTED); 357*e115a09fSMatthias Ringwald } 358*e115a09fSMatthias Ringwald tc_state = TC_IDLE; 359*e115a09fSMatthias Ringwald gc_handle = 0; 360*e115a09fSMatthias Ringwald return; 361*e115a09fSMatthias Ringwald 362*e115a09fSMatthias Ringwald default: 363*e115a09fSMatthias Ringwald break; 364*e115a09fSMatthias Ringwald } 3656bdecec7SMatthias Ringwald } 3666bdecec7SMatthias Ringwald 3676bdecec7SMatthias Ringwald void ancs_client_init(void){ 3686bdecec7SMatthias Ringwald hci_event_callback_registration.callback = &handle_hci_event; 3696bdecec7SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 3706bdecec7SMatthias Ringwald } 37141866fd3SMatthias Ringwald 37241866fd3SMatthias Ringwald // unit test only 37341866fd3SMatthias Ringwald #if defined __cplusplus 37441866fd3SMatthias Ringwald extern "C" 37541866fd3SMatthias Ringwald #endif 37641866fd3SMatthias Ringwald void ancs_client_set_invalid_parser_state(void); 37741866fd3SMatthias Ringwald void ancs_client_set_invalid_parser_state(void){ 37841866fd3SMatthias Ringwald chunk_parser_state = (ancs_chunk_parser_state_t) 0x17; 37941866fd3SMatthias Ringwald } 380