Skip to content

Commit 43f697d

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

File tree

4 files changed

+312
-0
lines changed

4 files changed

+312
-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: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
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 "sysinit/sysinit.h"
23+
#include "syscfg/syscfg.h"
24+
#include "host/ble_hs.h"
25+
#include "services/hid/ble_svc_hid.h"
26+
27+
28+
uint16_t ble_svc_hid_val_handle;
29+
30+
/* Access function */
31+
static int
32+
ble_svc_hid_access(uint16_t conn_handle, uint16_t attr_handle,
33+
struct ble_gatt_access_ctxt *ctxt, void *arg);
34+
35+
/* Service and characteristics definition */
36+
/* TODO: Confirm details of characteristic `Report` */
37+
static const struct ble_gatt_svc_def ble_svc_hid_defs[] = {
38+
{
39+
/*** Service: Tx Power Service. */
40+
.type = BLE_GATT_SVC_TYPE_PRIMARY,
41+
.uuid = BLE_UUID16_DECLARE(BLE_SVC_HID_UUID16),
42+
.characteristics = (struct ble_gatt_chr_def[]) { {
43+
/*** Characteristic: Protocol Mode. */
44+
.uuid = BLE_UUID16_DECLARE(BLE_SVC_HID_CHR_UUID16_PROTOCOL_MODE),
45+
.access_cb = ble_svc_hid_access,
46+
.val_handle = &ble_svc_hid_val_handle,
47+
.flags = BLE_GATT_CHR_F_READ|BLE_GATT_CHR_F_WRITE_NO_RSP,
48+
}, {
49+
/*** Characteristic: Report. */
50+
.uuid = BLE_UUID16_DECLARE(BLE_SVC_HID_CHR_UUID16_REPORT),
51+
.access_cb = ble_svc_hid_access,
52+
.val_handle = &ble_svc_hid_val_handle,
53+
.flags = BLE_GATT_CHR_F_READ|BLE_GATT_CHR_F_NOTIFY,
54+
}, {
55+
/*** Characteristic: Report Map. */
56+
.uuid = BLE_UUID16_DECLARE(BLE_SVC_HID_CHR_UUID16_REPORT_MAP),
57+
.access_cb = ble_svc_hid_access,
58+
.val_handle = &ble_svc_hid_val_handle,
59+
.flags = BLE_GATT_CHR_F_READ,
60+
}, {
61+
/*** Characteristic: Boot Keyboard Input Report. */
62+
.uuid = BLE_UUID16_DECLARE(BLE_SVC_HID_CHR_UUID16_BOOT_KEYBOARD_INPUT_REPORT),
63+
.access_cb = ble_svc_hid_access,
64+
.val_handle = &ble_svc_hid_val_handle,
65+
.flags = BLE_GATT_CHR_F_READ|BLE_GATT_CHR_F_NOTIFY,
66+
}, {
67+
/*** Characteristic: Boot Keyboard Output Report. */
68+
.uuid = BLE_UUID16_DECLARE(BLE_SVC_HID_CHR_UUID16_BOOT_KEYBOARD_OUTPUT_REPORT),
69+
.access_cb = ble_svc_hid_access,
70+
.val_handle = &ble_svc_hid_val_handle,
71+
.flags = BLE_GATT_CHR_F_READ|BLE_GATT_CHR_F_WRITE|BLE_GATT_CHR_F_WRITE_NO_RSP,
72+
}, {
73+
/*** Characteristic: Boot Mouse Input Report. */
74+
.uuid = BLE_UUID16_DECLARE(BLE_SVC_HID_CHR_UUID16_BOOT_MOUSE_INPUT_REPORT),
75+
.access_cb = ble_svc_hid_access,
76+
.val_handle = &ble_svc_hid_val_handle,
77+
.flags = BLE_GATT_CHR_F_READ|BLE_GATT_CHR_F_NOTIFY,
78+
}, {
79+
/*** Characteristic: Hid Information. */
80+
.uuid = BLE_UUID16_DECLARE(BLE_SVC_HID_CHR_UUID16_HID_INFORMATION),
81+
.access_cb = ble_svc_hid_access,
82+
.val_handle = &ble_svc_hid_val_handle,
83+
.flags = BLE_GATT_CHR_F_READ,
84+
}, {
85+
/*** Characteristic: Hid Control Point. */
86+
.uuid = BLE_UUID16_DECLARE(BLE_SVC_HID_CHR_UUID16_HID_CONTROL_POINT),
87+
.access_cb = ble_svc_hid_access,
88+
.val_handle = &ble_svc_hid_val_handle,
89+
.flags = BLE_GATT_CHR_F_WRITE_NO_RSP,
90+
}, {
91+
0, /* No more characteristics in this service. */
92+
} },
93+
},
94+
95+
{
96+
0, /* No more services. */
97+
},
98+
};
99+
100+
/* {
101+
/ ** Alert Notification Control Point
102+
*
103+
* This characteristic allows the peer device to
104+
* enable/disable the alert notification of new alert
105+
* and unread event more selectively than can be done
106+
* by setting or clearing the notification bit in the
107+
* Client Characteristic Configuration for each alert
108+
* characteristic.
109+
* /
110+
.uuid = BLE_UUID16_DECLARE(BLE_SVC_ANS_CHR_UUID16_ALERT_NOT_CTRL_PT),
111+
.access_cb = ble_svc_ans_access,
112+
.flags = BLE_GATT_CHR_F_WRITE,
113+
}
114+
*/
115+
116+
/**
117+
* HID access function
118+
*/
119+
static int
120+
ble_svc_hid_access(uint16_t conn_handle, uint16_t attr_handle,
121+
struct ble_gatt_access_ctxt *ctxt,
122+
void *arg)
123+
{
124+
uint16_t uuid16;
125+
int rc=0;
126+
127+
/* ANS Control point command and catagory variables */
128+
//uint8_t cmd_id;
129+
//int i;
130+
131+
uuid16 = ble_uuid_u16(ctxt->chr->uuid);
132+
assert(uuid16 != 0);
133+
134+
switch (uuid16) {
135+
case BLE_SVC_HID_CHR_UUID16_PROTOCOL_MODE:
136+
if (ctxt->op == BLE_GATT_CHR_F_READ) {
137+
} else if (ctxt->op == BLE_GATT_CHR_F_WRITE_NO_RSP) {
138+
}
139+
return rc;
140+
141+
case BLE_SVC_HID_CHR_UUID16_REPORT:
142+
if (ctxt->op == BLE_GATT_CHR_F_READ) {
143+
} else if (ctxt->op == BLE_GATT_CHR_F_NOTIFY) {
144+
}
145+
return rc;
146+
147+
case BLE_SVC_HID_CHR_UUID16_REPORT_MAP:
148+
assert(ctxt->op == BLE_GATT_CHR_F_READ);
149+
return rc;
150+
151+
case BLE_SVC_HID_CHR_UUID16_BOOT_KEYBOARD_INPUT_REPORT:
152+
if (ctxt->op == BLE_GATT_CHR_F_READ) {
153+
} else if (ctxt->op == BLE_GATT_CHR_F_NOTIFY) {
154+
}
155+
return rc;
156+
157+
case BLE_SVC_HID_CHR_UUID16_BOOT_KEYBOARD_OUTPUT_REPORT:
158+
if (ctxt->op == BLE_GATT_CHR_F_READ) {
159+
} else if (ctxt->op == BLE_GATT_CHR_F_WRITE) {
160+
} else if (ctxt->op == BLE_GATT_CHR_F_WRITE_NO_RSP) {
161+
}
162+
return rc;
163+
164+
case BLE_SVC_HID_CHR_UUID16_BOOT_MOUSE_INPUT_REPORT:
165+
if (ctxt->op == BLE_GATT_CHR_F_READ) {
166+
} else if (ctxt->op == BLE_GATT_CHR_F_NOTIFY) {
167+
}
168+
return rc;
169+
170+
case BLE_SVC_HID_CHR_UUID16_HID_INFORMATION:
171+
assert(ctxt->op == BLE_GATT_CHR_F_READ);
172+
return rc;
173+
174+
case BLE_SVC_HID_CHR_UUID16_HID_CONTROL_POINT:
175+
assert(ctxt->op == BLE_GATT_CHR_F_WRITE_NO_RSP);
176+
return rc;
177+
178+
default:
179+
assert(0);
180+
return BLE_ATT_ERR_UNLIKELY;
181+
}
182+
}
183+
184+
/**
185+
* Initialize the HID service
186+
*/
187+
void
188+
ble_svc_hid_init(void)
189+
{
190+
int rc;
191+
192+
/* Ensure this function only gets called by sysinit. */
193+
SYSINIT_ASSERT_ACTIVE();
194+
195+
rc = ble_gatts_count_cfg(ble_svc_hid_defs);
196+
SYSINIT_PANIC_ASSERT(rc == 0);
197+
198+
rc = ble_gatts_add_svcs(ble_svc_hid_defs);
199+
SYSINIT_PANIC_ASSERT(rc == 0);
200+
}

nimble/host/services/hid/syscfg.yml

Whitespace-only changes.

0 commit comments

Comments
 (0)