/**
* Copyright (c) @CompanyNameMagicTag 2023-2023. All rights reserved. \n
*
* Description: BLE RCU HID Service Source. \n
* Author: @CompanyNameTag \n
* History: \n
* 2023-9-10, Create file. \n
*/
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include "osal_addr.h"
#include "securec.h"
#include "errcode.h"
#include "osal_debug.h"
#include "bts_def.h"
#include "bts_gatt_stru.h"
#include "bts_gatt_server.h"
#include "bts_le_gap.h"
#include "ble_hid_rcu_server.h"
/* HID information flag remote wakeup */
#define BLE_HID_INFO_FLAG_REMOTE_WAKE_UP_MSK 0x01
/* HID information flag normally connectable */
#define BLE_HID_INFO_FLAG_NORMALLY_CONNECTABLE_MSK 0x02
/* HID information country code */
#define BLE_HID_INFO_COUNTRY_CODE 0x00
/* HID spec version 1.11 */
#define BLE_HID_VERSION 0x0101
/* HID keyboard input report id */
#define BLE_HID_KEYBOARD_REPORT_ID 1
/* HID mouse input report id */
#define BLE_HID_MOUSE_REPORT_ID 4
/* HID consumer input report id */
#define BLE_HID_CONSUMER_REPORT_ID 3
/* HID power input report id */
#define BLE_HID_POWER_REPORT_ID 2
/* HID input report type */
#define BLE_REPORT_REFERENCE_REPORT_TYPE_INPUT_REPORT 1
/* HID output report type */
#define BLE_REPORT_REFERENCE_REPORT_TYPE_OUTPUT_REPORT 2
/* HID gatt server id */
#define BLE_HID_SERVER_ID 1
/* HID ble connect id */
#define BLE_SINGLE_LINK_CONNECT_ID 0
/* octets of 16 bits uuid */
#define UUID16_LEN 2
/* invalid attribute handle */
#define INVALID_ATT_HDL 0
/* invalid server ID */
#define INVALID_SERVER_ID 0
/* input count */
#define INPUT_KEYBOARD 0
#define INPUT_MOUSE 2
#define INPUT_CONSUMER 4
#define INPUT_POWER 6
#define input(size) (0x80 | (size))
#define output(size) (0x90 | (size))
#define feature(size) (0xb0 | (size))
#define collection(size) (0xa0 | (size))
#define end_collection(size) (0xc0 | (size))
/* Global items */
#define usage_page(size) (0x04 | (size))
#define logical_minimum(size) (0x14 | (size))
#define logical_maximum(size) (0x24 | (size))
#define physical_minimum(size) (0x34 | (size))
#define physical_maximum(size) (0x44 | (size))
#define uint_exponent(size) (0x54 | (size))
#define uint(size) (0x64 | (size))
#define report_size(size) (0x74 | (size))
#define report_id(size) (0x84 | (size))
#define report_count(size) (0x94 | (size))
#define push(size) (0xa4 | (size))
#define pop(size) (0xb4 | (size))
/* Local items */
#define usage(size) (0x08 | (size))
#define usage_minimum(size) (0x18 | (size))
#define usage_maximum(size) (0x28 | (size))
#define designator_index(size) (0x38 | (size))
#define designator_minimum(size) (0x48 | (size))
#define designator_maximum(size) (0x58 | (size))
#define string_index(size) (0x78 | (size))
#define string_minimum(size) (0x88 | (size))
#define string_maximum(size) (0x98 | (size))
#define delimiter(size) (0xa8 | (size))
#define uint16_to_byte(n) ((uint8_t)(n)), ((uint8_t)((n) >> 8))
enum {
/* HID information characteristic properties */
HID_INFORMATION_PROPERTIES = GATT_CHARACTER_PROPERTY_BIT_READ,
/* HID protocol mode characteristic properties */
HID_PROTOCOL_MODE_PROPERTIES = GATT_CHARACTER_PROPERTY_BIT_READ | GATT_CHARACTER_PROPERTY_BIT_WRITE_NO_RSP,
/* HID report map characteristic properties */
HID_REPORT_MAP_PROPERTIES = GATT_CHARACTER_PROPERTY_BIT_READ,
/* HID input report characteristic properties */
HID_INPUT_REPORT_PROPERTIES = GATT_CHARACTER_PROPERTY_BIT_READ | GATT_CHARACTER_PROPERTY_BIT_NOTIFY |
GATT_CHARACTER_PROPERTY_BIT_WRITE,
/* HID output report characteristic properties */
HID_OUTPUT_REPORT_PROPERTIES = GATT_CHARACTER_PROPERTY_BIT_READ | GATT_CHARACTER_PROPERTY_BIT_WRITE |
GATT_CHARACTER_PROPERTY_BIT_WRITE_NO_RSP,
/* HID control point characteristic properties */
HID_CONTROL_POINT_PROPERTIES = GATT_CHARACTER_PROPERTY_BIT_WRITE_NO_RSP,
};
static uint16_t g_device_rcu_appearance_value = GAP_BLE_APPEARANCE_TYPE_KEYBOARD;
static uint8_t g_device_rcu_name_value[32] = {'R', 'c', 'u', '6', 'x', '\0'};
static uint8_t const g_device_rcu_name_len = 6;
/* HID information value for test */
static uint8_t g_hid_information_val[] = { uint16_to_byte(BLE_HID_VERSION), BLE_HID_INFO_COUNTRY_CODE,
BLE_HID_INFO_FLAG_REMOTE_WAKE_UP_MSK | BLE_HID_INFO_FLAG_NORMALLY_CONNECTABLE_MSK };
/* HID control point value for test */
static uint8_t g_control_point_val[] = {0x00, 0x00};
/* HID client characteristic configuration value for test */
static uint8_t g_ccc_val[] = {0x00, 0x00};
/* HID keyboard input report reference value for test [report id 1, input] */
static uint8_t g_keyboard_report_reference_val_input[] = {BLE_HID_KEYBOARD_REPORT_ID,
BLE_REPORT_REFERENCE_REPORT_TYPE_INPUT_REPORT};
/* HID keyboard output report reference value for test [report id 1, output] */
static uint8_t g_keyboard_report_reference_val_output[] = {BLE_HID_KEYBOARD_REPORT_ID,
BLE_REPORT_REFERENCE_REPORT_TYPE_OUTPUT_REPORT};
/* HID mouse input report reference value for test [report id 4, input] */
static uint8_t g_mouse_report_reference_val_input[] = {BLE_HID_MOUSE_REPORT_ID,
BLE_REPORT_REFERENCE_REPORT_TYPE_INPUT_REPORT};
/* HID mouse output report reference value for test [report id 4, output] */
static uint8_t g_mouse_report_reference_val_output[] = {BLE_HID_MOUSE_REPORT_ID,
BLE_REPORT_REFERENCE_REPORT_TYPE_OUTPUT_REPORT};
/* HID consumer input report reference value for test [report id 3, input] */
static uint8_t g_consumer_report_reference_val_input[] = {BLE_HID_CONSUMER_REPORT_ID,
BLE_REPORT_REFERENCE_REPORT_TYPE_INPUT_REPORT};
/* HID consumer output report reference value for test [report id 3, output] */
static uint8_t g_consumer_report_reference_val_output[] = {BLE_HID_CONSUMER_REPORT_ID,
BLE_REPORT_REFERENCE_REPORT_TYPE_OUTPUT_REPORT};
/* HID power input report reference value for test [report id 2, input] */
static uint8_t g_power_report_reference_val_input[] = {BLE_HID_POWER_REPORT_ID,
BLE_REPORT_REFERENCE_REPORT_TYPE_INPUT_REPORT};
/* HID power output report reference value for test [report id 2, output] */
static uin