1 /* 2 * Copyright (C) 2021 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 MATTHIAS 24 * RINGWALD 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 #ifndef HIDS_CLIENT_H 39 #define HIDS_CLIENT_H 40 41 #if defined __cplusplus 42 extern "C" { 43 #endif 44 45 #include <stdint.h> 46 #include "hid.h" 47 #include "btstack_defines.h" 48 #include "bluetooth.h" 49 #include "btstack_linked_list.h" 50 #include "ble/gatt_client.h" 51 52 #ifndef MAX_NUM_HID_SERVICES 53 #define MAX_NUM_HID_SERVICES 3 54 #endif 55 #define HIDS_CLIENT_INVALID_REPORT_INDEX 0xFF 56 57 #define HIDS_CLIENT_NUM_REPORTS 15 58 59 typedef enum { 60 HIDS_CLIENT_STATE_IDLE, 61 62 // get all HID services 63 HIDS_CLIENT_STATE_W2_QUERY_SERVICE, 64 HIDS_CLIENT_STATE_W4_SERVICE_RESULT, 65 66 // for each service, discover all characteristics 67 HIDS_CLIENT_STATE_W2_QUERY_CHARACTERISTIC, 68 HIDS_CLIENT_STATE_W4_CHARACTERISTIC_RESULT, 69 70 // for each REPORT_MAP characteristic, read HID Descriptor (Report Map Characteristic Value) 71 HIDS_CLIENT_STATE_W2_READ_REPORT_MAP_HID_DESCRIPTOR, 72 HIDS_CLIENT_STATE_W4_REPORT_MAP_HID_DESCRIPTOR, 73 74 // for REPORT_MAP characteristic, discover descriptor 75 HIDS_CLIENT_STATE_W2_REPORT_MAP_DISCOVER_CHARACTERISTIC_DESCRIPTORS, 76 HIDS_CLIENT_STATE_W4_REPORT_MAP_CHARACTERISTIC_DESCRIPTORS_RESULT, 77 78 // for REPORT_MAP characteristic, read External Report Reference Characteristic Descriptor 79 HIDS_CLIENT_STATE_W2_REPORT_MAP_READ_EXTERNAL_REPORT_REFERENCE_UUID, 80 HIDS_CLIENT_STATE_W4_REPORT_MAP_EXTERNAL_REPORT_REFERENCE_UUID, 81 82 // for every external report reference uuid, discover external Report characteristic 83 HIDS_CLIENT_STATE_W2_DISCOVER_EXTERNAL_REPORT_CHARACTERISTIC, 84 HIDS_CLIENT_STATE_W4_EXTERNAL_REPORT_CHARACTERISTIC_RESULT, 85 86 // for each Report characteristics, discover Report characteristic descriptor 87 HIDS_CLIENT_STATE_W2_FIND_REPORT, 88 HIDS_CLIENT_STATE_W4_REPORT_FOUND, 89 90 // then read value of Report characteristic descriptor to get report ID and type 91 HIDS_CLIENT_STATE_W2_READ_REPORT_ID_AND_TYPE, 92 HIDS_CLIENT_STATE_W4_REPORT_ID_AND_TYPE, 93 94 // Boot Mode 95 HIDS_CLIENT_STATE_W2_ENABLE_KEYBOARD, 96 HIDS_CLIENT_STATE_W4_KEYBOARD_ENABLED, 97 HIDS_CLIENT_STATE_W2_ENABLE_MOUSE, 98 HIDS_CLIENT_STATE_W4_MOUSE_ENABLED, 99 100 101 HIDS_CLIENT_STATE_W2_SET_PROTOCOL_MODE, 102 HIDS_CLIENT_STATE_W4_SET_PROTOCOL_MODE, 103 104 HIDS_CLIENT_STATE_CONNECTED, 105 HIDS_CLIENT_W2_SEND_REPORT 106 } hid_service_client_state_t; 107 108 109 typedef struct { 110 // GATT cache 111 112 //reuse as descriptor_handle 113 uint16_t value_handle; 114 uint16_t end_handle; 115 uint16_t properties; 116 117 // UUID of external Report characteristic, stored in Report Map descriptor EXTERNAL_REPORT_REFERENCE 118 uint16_t external_report_reference_uuid; 119 120 // service mapping 121 uint8_t service_index; 122 uint8_t report_id; 123 hid_report_type_t report_type; 124 gatt_client_notification_t notifications; 125 } hids_client_report_t; 126 127 typedef struct { 128 uint16_t start_handle; 129 uint16_t end_handle; 130 131 uint16_t report_map_value_handle; 132 uint16_t report_map_end_handle; 133 } hid_service_t; 134 135 typedef struct { 136 btstack_linked_item_t item; 137 138 hci_con_handle_t con_handle; 139 uint16_t cid; 140 hid_protocol_mode_t protocol_mode; 141 hid_service_client_state_t state; 142 btstack_packet_handler_t client_handler; 143 144 uint8_t num_instances; 145 hid_service_t services[MAX_NUM_HID_SERVICES]; 146 147 // used for discovering characteristics 148 uint8_t service_index; 149 hid_protocol_mode_t required_protocol_mode; 150 151 uint16_t protocol_mode_value_handle; 152 153 // send report 154 hids_client_report_t reports[HIDS_CLIENT_NUM_REPORTS]; 155 uint8_t num_reports; 156 157 // index used for report and report map search 158 uint8_t active_index; 159 uint16_t descriptor_handle; 160 uint16_t report_len; 161 const uint8_t * report; 162 } hids_client_t; 163 164 /* API_START */ 165 166 /** 167 * @brief Initialize Battery Service. 168 */ 169 void hids_client_init(void); 170 171 /* @brief Connect to HID Services of remote device. 172 * 173 * @param con_handle 174 * @param packet_handler 175 * @param protocol_mode see hid_protocol_mode_t in hid.h 176 * @param hids_cid 177 * @return status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_COMMAND_DISALLOWED if there is already a client associated with con_handle, or BTSTACK_MEMORY_ALLOC_FAILED 178 */ 179 uint8_t hids_client_connect(hci_con_handle_t con_handle, btstack_packet_handler_t packet_handler, hid_protocol_mode_t protocol_mode, uint16_t * hids_cid); 180 181 /** 182 * @brief Send HID output report. 183 * @param hids_cid 184 * @param report_id 185 * @param report 186 * @param report_len 187 * @result status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, ERROR_CODE_COMMAND_DISALLOWED 188 */ 189 uint8_t hids_client_send_report(uint16_t hids_cid, uint8_t report_id, const uint8_t * report, uint8_t report_len); 190 191 /** 192 * @brief Disconnect from Battery Service. 193 * @param hids_cid 194 * @return status 195 */ 196 uint8_t hids_client_disconnect(uint16_t hids_cid); 197 198 /** 199 * @brief De-initialize Battery Service. 200 */ 201 void hids_client_deinit(void); 202 203 /* API_END */ 204 205 #if defined __cplusplus 206 } 207 #endif 208 209 #endif 210