Skip to content

Commit fecd172

Browse files
committed
Initial commit: HID service
1 parent 4d90654 commit fecd172

File tree

4 files changed

+313
-0
lines changed

4 files changed

+313
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#ifndef H_BLE_SVC_HID_
21+
#define H_BLE_SVC_HID_
22+
23+
24+
25+
/* 16 Bit Human Interface Device Service UUID */
26+
#define BLE_SVC_HID_UUID16 0x1812
27+
28+
/* 16 Bit HID Service Characteristic UUIDs */
29+
#define BLE_SVC_HID_CHR_UUID16_PROTOCOL_MODE 0x2A4E
30+
#define BLE_SVC_HID_CHR_UUID16_REPORT 0x2A4D
31+
#define BLE_SVC_HID_CHR_UUID16_REPORT_MAP 0x2A4B
32+
#define BLE_SVC_HID_CHR_UUID16_BOOT_KEYBOARD_INPUT_REPORT 0x2A22
33+
#define BLE_SVC_HID_CHR_UUID16_BOOT_KEYBOARD_OUTPUT_REPORT 0x2A32
34+
#define BLE_SVC_HID_CHR_UUID16_BOOT_MOUSE_INPUT_REPORT 0x2A33
35+
#define BLE_SVC_HID_CHR_UUID16_HID_INFORMATION 0x2A4A
36+
#define BLE_SVC_HID_CHR_UUID16_HID_CONTROL_POINT 0x2A4C
37+
38+
/* HID Service Protocol Mode enum
39+
* The protocol mode of the HID Service
40+
* Format: uint8
41+
* key="0" value="Boot Protocol Mode"
42+
* key="1" value="Report Protocol Mode"
43+
* key="2-255" ReservedForFutureUse
44+
*/
45+
46+
typedef enum __attribute__((packed)) {
47+
BLE_SVC_HID_PROTOCOL_MODE_BOOT,
48+
BLE_SVC_HID_PROTOCOL_MODE_REPORT
49+
} ble_svc_hid_protocol_mode_value_t ;
50+
51+
/* HID Service HID Information Flags
52+
* Format: 8-bit BitField
53+
* Note:The fields are in the order of LSO to MSO
54+
* index="0" size="1" name="RemoteWake"
55+
* index="1" size="1" name="NormallyConnectable"
56+
* index="2" size="6" ReservedForFutureUse
57+
*/
58+
#define BLE_SVC_HID_HID_INFO_FLAG_REMOTE_WAKE 0x01
59+
#define BLE_SVC_HID_HID_INFO_FLAG_NORMALLY_CONNECTABLE 0x02
60+
61+
/* HID Service HID Control Point Command enum
62+
* The HID Control Point characteristic is a control-point attribute that defines the following HID Commands when written:
63+
* Format: uint8
64+
* key="0" value="Suspend"
65+
* key="1" value="Exit Suspend"
66+
* key="2-255" ReservedForFutureUse
67+
*/
68+
69+
typedef enum __attribute__((packed)) {
70+
BLE_SVC_HID_HID_CONTROL_POINT_SUSPEND,
71+
BLE_SVC_HID_HID_CONTROL_POINT_EXIT_SUSPEND
72+
} ble_svc_hid_hid_control_point_value_t;
73+
74+
/* Functions */
75+
76+
void ble_svc_hid_init(void);
77+
78+
#endif

nimble/host/services/hid/pkg.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
20+
pkg.name: nimble/host/services/hid
21+
pkg.description: Implements the Human Interface Device Service
22+
pkg.author: "Apache Mynewt <[email protected]>"
23+
pkg.homepage: "http://mynewt.apache.org/"
24+
pkg.keywords:
25+
- ble
26+
- bluetooth
27+
- nimble
28+
- hid
29+
30+
pkg.deps:
31+
- nimble/host
32+
33+
pkg.init:
34+
ble_svc_hid_init: 303
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#include <assert.h>
21+
22+
#include "services/hid/ble_svc_hid.h"
23+
24+
#include "sysinit/sysinit.h"
25+
#include "syscfg/syscfg.h"
26+
#include "host/ble_hs.h"
27+
28+
29+
uint16_t ble_svc_hid_val_handle;
30+
31+
/* Access function */
32+
static int
33+
ble_svc_hid_access(uint16_t conn_handle, uint16_t attr_handle,
34+
struct ble_gatt_access_ctxt *ctxt, void *arg);
35+
36+
/* Service and characteristics definition */
37+
/* TODO: Confirm details of characteristic `Report` */
38+
static const struct ble_gatt_svc_def ble_svc_hid_defs[] = {
39+
{
40+
/*** Service: Tx Power Service. */
41+
.type = BLE_GATT_SVC_TYPE_PRIMARY,
42+
.uuid = BLE_UUID16_DECLARE(BLE_SVC_HID_UUID16),
43+
.characteristics = (struct ble_gatt_chr_def[]) { {
44+
/*** Characteristic: Protocol Mode. */
45+
.uuid = BLE_UUID16_DECLARE(BLE_SVC_HID_CHR_UUID16_PROTOCOL_MODE),
46+
.access_cb = ble_svc_hid_access,
47+
.val_handle = &ble_svc_hid_val_handle,
48+
.flags = BLE_GATT_CHR_F_READ|BLE_GATT_CHR_F_WRITE_NO_RSP,
49+
}, {
50+
/*** Characteristic: Report. */
51+
.uuid = BLE_UUID16_DECLARE(BLE_SVC_HID_CHR_UUID16_REPORT),
52+
.access_cb = ble_svc_hid_access,
53+
.val_handle = &ble_svc_hid_val_handle,
54+
.flags = BLE_GATT_CHR_F_READ|BLE_GATT_CHR_F_NOTIFY,
55+
}, {
56+
/*** Characteristic: Report Map. */
57+
.uuid = BLE_UUID16_DECLARE(BLE_SVC_HID_CHR_UUID16_REPORT_MAP),
58+
.access_cb = ble_svc_hid_access,
59+
.val_handle = &ble_svc_hid_val_handle,
60+
.flags = BLE_GATT_CHR_F_READ,
61+
}, {
62+
/*** Characteristic: Boot Keyboard Input Report. */
63+
.uuid = BLE_UUID16_DECLARE(BLE_SVC_HID_CHR_UUID16_BOOT_KEYBOARD_INPUT_REPORT),
64+
.access_cb = ble_svc_hid_access,
65+
.val_handle = &ble_svc_hid_val_handle,
66+
.flags = BLE_GATT_CHR_F_READ|BLE_GATT_CHR_F_NOTIFY,
67+
}, {
68+
/*** Characteristic: Boot Keyboard Output Report. */
69+
.uuid = BLE_UUID16_DECLARE(BLE_SVC_HID_CHR_UUID16_BOOT_KEYBOARD_OUTPUT_REPORT),
70+
.access_cb = ble_svc_hid_access,
71+
.val_handle = &ble_svc_hid_val_handle,
72+
.flags = BLE_GATT_CHR_F_READ|BLE_GATT_CHR_F_WRITE|BLE_GATT_CHR_F_WRITE_NO_RSP,
73+
}, {
74+
/*** Characteristic: Boot Mouse Input Report. */
75+
.uuid = BLE_UUID16_DECLARE(BLE_SVC_HID_CHR_UUID16_BOOT_MOUSE_INPUT_REPORT),
76+
.access_cb = ble_svc_hid_access,
77+
.val_handle = &ble_svc_hid_val_handle,
78+
.flags = BLE_GATT_CHR_F_READ|BLE_GATT_CHR_F_NOTIFY,
79+
}, {
80+
/*** Characteristic: Hid Information. */
81+
.uuid = BLE_UUID16_DECLARE(BLE_SVC_HID_CHR_UUID16_HID_INFORMATION),
82+
.access_cb = ble_svc_hid_access,
83+
.val_handle = &ble_svc_hid_val_handle,
84+
.flags = BLE_GATT_CHR_F_READ,
85+
}, {
86+
/*** Characteristic: Hid Control Point. */
87+
.uuid = BLE_UUID16_DECLARE(BLE_SVC_HID_CHR_UUID16_HID_CONTROL_POINT),
88+
.access_cb = ble_svc_hid_access,
89+
.val_handle = &ble_svc_hid_val_handle,
90+
.flags = BLE_GATT_CHR_F_WRITE_NO_RSP,
91+
}, {
92+
0, /* No more characteristics in this service. */
93+
} },
94+
},
95+
96+
{
97+
0, /* No more services. */
98+
},
99+
};
100+
101+
/* {
102+
/ ** Alert Notification Control Point
103+
*
104+
* This characteristic allows the peer device to
105+
* enable/disable the alert notification of new alert
106+
* and unread event more selectively than can be done
107+
* by setting or clearing the notification bit in the
108+
* Client Characteristic Configuration for each alert
109+
* characteristic.
110+
* /
111+
.uuid = BLE_UUID16_DECLARE(BLE_SVC_ANS_CHR_UUID16_ALERT_NOT_CTRL_PT),
112+
.access_cb = ble_svc_ans_access,
113+
.flags = BLE_GATT_CHR_F_WRITE,
114+
}
115+
*/
116+
117+
/**
118+
* HID access function
119+
*/
120+
static int
121+
ble_svc_hid_access(uint16_t conn_handle, uint16_t attr_handle,
122+
struct ble_gatt_access_ctxt *ctxt,
123+
void *arg)
124+
{
125+
uint16_t uuid16;
126+
int rc=0;
127+
128+
/* ANS Control point command and catagory variables */
129+
//uint8_t cmd_id;
130+
//int i;
131+
132+
uuid16 = ble_uuid_u16(ctxt->chr->uuid);
133+
assert(uuid16 != 0);
134+
135+
switch (uuid16) {
136+
case BLE_SVC_HID_CHR_UUID16_PROTOCOL_MODE:
137+
if (ctxt->op == BLE_GATT_CHR_F_READ) {
138+
} else if (ctxt->op == BLE_GATT_CHR_F_WRITE_NO_RSP) {
139+
}
140+
return rc;
141+
142+
case BLE_SVC_HID_CHR_UUID16_REPORT:
143+
if (ctxt->op == BLE_GATT_CHR_F_READ) {
144+
} else if (ctxt->op == BLE_GATT_CHR_F_NOTIFY) {
145+
}
146+
return rc;
147+
148+
case BLE_SVC_HID_CHR_UUID16_REPORT_MAP:
149+
assert(ctxt->op == BLE_GATT_CHR_F_READ);
150+
return rc;
151+
152+
case BLE_SVC_HID_CHR_UUID16_BOOT_KEYBOARD_INPUT_REPORT:
153+
if (ctxt->op == BLE_GATT_CHR_F_READ) {
154+
} else if (ctxt->op == BLE_GATT_CHR_F_NOTIFY) {
155+
}
156+
return rc;
157+
158+
case BLE_SVC_HID_CHR_UUID16_BOOT_KEYBOARD_OUTPUT_REPORT:
159+
if (ctxt->op == BLE_GATT_CHR_F_READ) {
160+
} else if (ctxt->op == BLE_GATT_CHR_F_WRITE) {
161+
} else if (ctxt->op == BLE_GATT_CHR_F_WRITE_NO_RSP) {
162+
}
163+
return rc;
164+
165+
case BLE_SVC_HID_CHR_UUID16_BOOT_MOUSE_INPUT_REPORT:
166+
if (ctxt->op == BLE_GATT_CHR_F_READ) {
167+
} else if (ctxt->op == BLE_GATT_CHR_F_NOTIFY) {
168+
}
169+
return rc;
170+
171+
case BLE_SVC_HID_CHR_UUID16_HID_INFORMATION:
172+
assert(ctxt->op == BLE_GATT_CHR_F_READ);
173+
return rc;
174+
175+
case BLE_SVC_HID_CHR_UUID16_HID_CONTROL_POINT:
176+
assert(ctxt->op == BLE_GATT_CHR_F_WRITE_NO_RSP);
177+
return rc;
178+
179+
default:
180+
assert(0);
181+
return BLE_ATT_ERR_UNLIKELY;
182+
}
183+
}
184+
185+
/**
186+
* Initialize the HID service
187+
*/
188+
void
189+
ble_svc_hid_init(void)
190+
{
191+
int rc;
192+
193+
/* Ensure this function only gets called by sysinit. */
194+
SYSINIT_ASSERT_ACTIVE();
195+
196+
rc = ble_gatts_count_cfg(ble_svc_hid_defs);
197+
SYSINIT_PANIC_ASSERT(rc == 0);
198+
199+
rc = ble_gatts_add_svcs(ble_svc_hid_defs);
200+
SYSINIT_PANIC_ASSERT(rc == 0);
201+
}

nimble/host/services/hid/syscfg.yml

Whitespace-only changes.

0 commit comments

Comments
 (0)