implementation of the Device Information Service

This commit is contained in:
Stephane D'Alu
2018-04-02 21:31:52 +02:00
parent 758de7ab8c
commit 6adc6d6172
4 changed files with 531 additions and 0 deletions
@@ -0,0 +1,105 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#ifndef H_BLE_SVC_DIS_
#define H_BLE_SVC_DIS_
/**
* Example:
*
* char firmware_revision[20] = '?.?.?';
* struct image_version iv;
* if (!imgr_my_version(&iv)) {
* snprintf(firmware_revision, sizeof(firmware_revision),
* "%u.%u.%u", iv.iv_major, iv.iv_minor, iv.iv_revision);
* }
* ble_svc_dis_manufacturer_name_set("MyNewt");
* ble_svc_dis_firmware_revision_set(firmware_revision);
*
*/
#define BLE_SVC_DIS_UUID16 0x180A
#define BLE_SVC_DIS_CHR_UUID16_MODEL_NUMBER 0x2A24
#define BLE_SVC_DIS_CHR_UUID16_SERIAL_NUMBER 0x2A25
#define BLE_SVC_DIS_CHR_UUID16_FIRMWARE_REVISION 0x2A26
#define BLE_SVC_DIS_CHR_UUID16_HARDWARE_REVISION 0x2A27
#define BLE_SVC_DIS_CHR_UUID16_SOFTWARE_REVISION 0x2A28
#define BLE_SVC_DIS_CHR_UUID16_MANUFACTURER_NAME 0x2A29
/**
* Structure holding data for the main characteristics
*/
struct ble_svc_dis_data {
/**
* Model number.
* Represent the model number that is assigned by the device vendor.
*/
const char *model_number;
/**
* Serial number.
* Represent the serial number for a particular instance of the device.
*/
const char *serial_number;
/**
* Firmware revision.
* Represent the firmware revision for the firmware within the device.
*/
const char *firmware_revision;
/**
* Hardware revision.
* Represent the hardware revision for the hardware within the device.
*/
const char *hardware_revision;
/**
* Software revision.
* Represent the software revision for the software within the device.
*/
const char *software_revision;
/**
* Manufacturer name.
* Represent the name of the manufacturer of the device.
*/
const char *manufacturer_name;
};
/**
* Variable holding data for the main characteristics.
*/
extern struct ble_svc_dis_data ble_svc_dis_data;
/**
* Service initialisation.
* Automatically called during package initialisation.
*/
void ble_svc_dis_init(void);
const char *ble_svc_dis_model_number(void);
int ble_svc_dis_model_number_set(const char *value);
const char *ble_svc_dis_serial_number(void);
int ble_svc_dis_serial_number_set(const char *value);
const char *ble_svc_dis_firmware_revision(void);
int ble_svc_dis_firmware_revision_set(const char *value);
const char *ble_svc_dis_hardware_revision(void);
int ble_svc_dis_hardware_revision_set(const char *value);
const char *ble_svc_dis_software_revision(void);
int ble_svc_dis_software_revision_set(const char *value);
const char *ble_svc_dis_manufacturer_name(void);
int ble_svc_dis_manufacturer_name_set(const char *value);
#endif
+34
View File
@@ -0,0 +1,34 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
pkg.name: nimble/host/services/dis
pkg.description: Device Information Service Implementation.
pkg.author: "Apache Mynewt <[email protected]>"
pkg.homepage: "http://mynewt.apache.org/"
pkg.keywords:
- ble
- bluetooth
- dis
- nimble
pkg.deps:
- nimble/host
pkg.init:
ble_svc_dis_init: 303
+296
View File
@@ -0,0 +1,296 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include <assert.h>
#include <string.h>
#include "sysinit/sysinit.h"
#include "host/ble_hs.h"
#include "services/dis/ble_svc_dis.h"
/* Device information */
struct ble_svc_dis_data ble_svc_dis_data = {
.model_number = MYNEWT_VAL(BLE_SVC_DIS_MODEL_NUMBER_DEFAULT),
.serial_number = MYNEWT_VAL(BLE_SVC_DIS_SERIAL_NUMBER_DEFAULT),
.firmware_revision = MYNEWT_VAL(BLE_SVC_DIS_FIRMWARE_REVISION_DEFAULT),
.hardware_revision = MYNEWT_VAL(BLE_SVC_DIS_HARDWARE_REVISION_DEFAULT),
.software_revision = MYNEWT_VAL(BLE_SVC_DIS_SOFTWARE_REVISION_DEFAULT),
.manufacturer_name = MYNEWT_VAL(BLE_SVC_DIS_MANUFACTURER_NAME_DEFAULT),
};
/* Access function */
#if (MYNEWT_VAL(BLE_SVC_DIS_MODEL_NUMBER_READ_PERM) >= 0) || \
(MYNEWT_VAL(BLE_SVC_DIS_SERIAL_NUMBER_READ_PERM) >= 0) || \
(MYNEWT_VAL(BLE_SVC_DIS_HARDWARE_REVISION_READ_PERM) >= 0) || \
(MYNEWT_VAL(BLE_SVC_DIS_FIRMWARE_REVISION_READ_PERM) >= 0) || \
(MYNEWT_VAL(BLE_SVC_DIS_SOFTWARE_REVISION_READ_PERM) >= 0) || \
(MYNEWT_VAL(BLE_SVC_DIS_MANUFACTURER_NAME_READ_PERM) >= 0)
static int
ble_svc_dis_access(uint16_t conn_handle, uint16_t attr_handle,
struct ble_gatt_access_ctxt *ctxt, void *arg);
#endif
static const struct ble_gatt_svc_def ble_svc_dis_defs[] = {
{ /*** Service: Device Information Service (DIS). */
.type = BLE_GATT_SVC_TYPE_PRIMARY,
.uuid = BLE_UUID16_DECLARE(BLE_SVC_DIS_UUID16),
.characteristics = (struct ble_gatt_chr_def[]) { {
#if (MYNEWT_VAL(BLE_SVC_DIS_MODEL_NUMBER_READ_PERM) >= 0)
/*** Characteristic: Model Number String */
.uuid = BLE_UUID16_DECLARE(BLE_SVC_DIS_CHR_UUID16_MODEL_NUMBER),
.access_cb = ble_svc_dis_access,
.flags = BLE_GATT_CHR_F_READ |
MYNEWT_VAL(BLE_SVC_DIS_MODEL_NUMBER_READ_PERM),
}, {
#endif
#if (MYNEWT_VAL(BLE_SVC_DIS_SERIAL_NUMBER_READ_PERM) >= 0)
/*** Characteristic: Serial Number String */
.uuid = BLE_UUID16_DECLARE(BLE_SVC_DIS_CHR_UUID16_SERIAL_NUMBER),
.access_cb = ble_svc_dis_access,
.flags = BLE_GATT_CHR_F_READ |
MYNEWT_VAL(BLE_SVC_DIS_SERIAL_NUMBER_READ_PERM),
}, {
#endif
#if (MYNEWT_VAL(BLE_SVC_DIS_HARDWARE_REVISION_READ_PERM) >= 0)
/*** Characteristic: Hardware Revision String */
.uuid = BLE_UUID16_DECLARE(BLE_SVC_DIS_CHR_UUID16_HARDWARE_REVISION),
.access_cb = ble_svc_dis_access,
.flags = BLE_GATT_CHR_F_READ |
MYNEWT_VAL(BLE_SVC_DIS_HARDWARE_REVISION_READ_PERM),
}, {
#endif
#if (MYNEWT_VAL(BLE_SVC_DIS_FIRMWARE_REVISION_READ_PERM) >= 0)
/*** Characteristic: Firmware Revision String */
.uuid = BLE_UUID16_DECLARE(BLE_SVC_DIS_CHR_UUID16_FIRMWARE_REVISION),
.access_cb = ble_svc_dis_access,
.flags = BLE_GATT_CHR_F_READ |
MYNEWT_VAL(BLE_SVC_DIS_FIRMWARE_REVISION_READ_PERM),
}, {
#endif
#if (MYNEWT_VAL(BLE_SVC_DIS_SOFTWARE_REVISION_READ_PERM) >= 0)
/*** Characteristic: Software Revision String */
.uuid = BLE_UUID16_DECLARE(BLE_SVC_DIS_CHR_UUID16_SOFTWARE_REVISION),
.access_cb = ble_svc_dis_access,
.flags = BLE_GATT_CHR_F_READ |
MYNEWT_VAL(BLE_SVC_DIS_SOFTWARE_REVISION_READ_PERM),
}, {
#endif
#if (MYNEWT_VAL(BLE_SVC_DIS_MANUFACTURER_NAME_READ_PERM) >= 0)
/*** Characteristic: Manufacturer Name */
.uuid = BLE_UUID16_DECLARE(BLE_SVC_DIS_CHR_UUID16_MANUFACTURER_NAME),
.access_cb = ble_svc_dis_access,
.flags = BLE_GATT_CHR_F_READ |
MYNEWT_VAL(BLE_SVC_DIS_MANUFACTURER_NAME_READ_PERM),
}, {
#endif
0, /* No more characteristics in this service */
}, }
},
{
0, /* No more services. */
},
};
/**
* Simple read access callback for the device information service
* characteristic.
*/
#if (MYNEWT_VAL(BLE_SVC_DIS_MODEL_NUMBER_READ_PERM) >= 0) || \
(MYNEWT_VAL(BLE_SVC_DIS_SERIAL_NUMBER_READ_PERM) >= 0) || \
(MYNEWT_VAL(BLE_SVC_DIS_HARDWARE_REVISION_READ_PERM) >= 0) || \
(MYNEWT_VAL(BLE_SVC_DIS_FIRMWARE_REVISION_READ_PERM) >= 0) || \
(MYNEWT_VAL(BLE_SVC_DIS_SOFTWARE_REVISION_READ_PERM) >= 0) || \
(MYNEWT_VAL(BLE_SVC_DIS_MANUFACTURER_NAME_READ_PERM) >= 0)
static int
ble_svc_dis_access(uint16_t conn_handle, uint16_t attr_handle,
struct ble_gatt_access_ctxt *ctxt, void *arg)
{
uint16_t uuid = ble_uuid_u16(ctxt->chr->uuid);
const char *info = NULL;
switch(uuid) {
#if (MYNEWT_VAL(BLE_SVC_DIS_MODEL_NUMBER_READ_PERM) >= 0)
case BLE_SVC_DIS_CHR_UUID16_MODEL_NUMBER:
info = ble_svc_dis_data.model_number;
#ifdef MYNEWT_VAL_BLE_SVC_DIS_MODEL_NUMBER_NAME_DEFAULT
if (info == NULL) {
info = MYNEWT_VAL(BLE_SVC_DIS_MODEL_NUMBER_DEFAULT);
}
#endif
break;
#endif
#if (MYNEWT_VAL(BLE_SVC_DIS_SERIAL_NUMBER_READ_PERM) >= 0)
case BLE_SVC_DIS_CHR_UUID16_SERIAL_NUMBER:
info = ble_svc_dis_data.serial_number;
#ifdef MYNEWT_VAL_BLE_SVC_DIS_SERIAL_NUMBER_DEFAULT
if (info == NULL) {
info = MYNEWT_VAL(BLE_SVC_DIS_SERIAL_NUMBER_DEFAULT);
}
#endif
break;
#endif
#if (MYNEWT_VAL(BLE_SVC_DIS_FIRMWARE_REVISION_READ_PERM) >= 0)
case BLE_SVC_DIS_CHR_UUID16_FIRMWARE_REVISION:
info = ble_svc_dis_data.firmware_revision;
#ifdef MYNEWT_VAL_BLE_SVC_DIS_FIRMWARE_REVISION_DEFAULT
if (info == NULL) {
info = MYNEWT_VAL(BLE_SVC_DIS_FIRMWARE_REVISION_DEFAULT);
}
#endif
break;
#endif
#if (MYNEWT_VAL(BLE_SVC_DIS_HARDWARE_REVISION_READ_PERM) >= 0)
case BLE_SVC_DIS_CHR_UUID16_HARDWARE_REVISION:
info = ble_svc_dis_data.hardware_revision;
#ifdef MYNEWT_VAL_BLE_SVC_DIS_HARDWARE_REVISION_DEFAULT
if (info == NULL) {
info = MYNEWT_VAL(BLE_SVC_DIS_HARDWARE_REVISION_DEFAULT);
}
#endif
break;
#endif
#if (MYNEWT_VAL(BLE_SVC_DIS_SOFTWARE_REVISION_READ_PERM) >= 0)
case BLE_SVC_DIS_CHR_UUID16_SOFTWARE_REVISION:
info = ble_svc_dis_data.software_revision;
#ifdef MYNEWT_VAL_BLE_SVC_DIS_SOFTWARE_REVISION_DEFAULT
if (info == NULL) {
info = MYNEWT_VAL(BLE_SVC_DIS_SOFTWARE_REVISION_DEFAULT);
}
#endif
break;
#endif
#if (MYNEWT_VAL(BLE_SVC_DIS_MANUFACTURER_NAME_READ_PERM) >= 0)
case BLE_SVC_DIS_CHR_UUID16_MANUFACTURER_NAME:
info = ble_svc_dis_data.manufacturer_name;
#ifdef MYNEWT_VAL_BLE_SVC_DIS_MANUFACTURER_NAME_DEFAULT
if (info == NULL) {
info = MYNEWT_VAL(BLE_SVC_DIS_MANUFACTURER_NAME_DEFAULT);
}
#endif
break;
#endif
default:
assert(0);
return BLE_ATT_ERR_UNLIKELY;
}
if (info != NULL) {
int rc = os_mbuf_append(ctxt->om, info, strlen(info));
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
}
return 0;
}
#endif
const char *
ble_svc_dis_model_number(void)
{
return ble_svc_dis_data.model_number;
}
int
ble_svc_dis_model_number_set(const char *value)
{
ble_svc_dis_data.model_number = value;
return 0;
}
const char *
ble_svc_dis_serial_number(void)
{
return ble_svc_dis_data.serial_number;
}
int
ble_svc_dis_serial_number_set(const char *value)
{
ble_svc_dis_data.serial_number = value;
return 0;
}
const char *
ble_svc_dis_firmware_revision(void)
{
return ble_svc_dis_data.firmware_revision;
}
int
ble_svc_dis_firmware_revision_set(const char *value)
{
ble_svc_dis_data.firmware_revision = value;
return 0;
}
const char *
ble_svc_dis_hardware_revision(void)
{
return ble_svc_dis_data.hardware_revision;
}
int
ble_svc_dis_hardware_revision_set(const char *value)
{
ble_svc_dis_data.hardware_revision = value;
return 0;
}
const char *
ble_svc_dis_software_revision(void)
{
return ble_svc_dis_data.software_revision;
}
int
ble_svc_dis_software_revision_set(const char *value)
{
ble_svc_dis_data.software_revision = value;
return 0;
}
const char *
ble_svc_dis_manufacturer_name(void)
{
return ble_svc_dis_data.manufacturer_name;
}
int
ble_svc_dis_manufacturer_name_set(const char *value)
{
ble_svc_dis_data.manufacturer_name = value;
return 0;
}
/**
* Initialize the DIS package.
*/
void
ble_svc_dis_init(void)
{
int rc;
/* Ensure this function only gets called by sysinit. */
SYSINIT_ASSERT_ACTIVE();
rc = ble_gatts_count_cfg(ble_svc_dis_defs);
SYSINIT_PANIC_ASSERT(rc == 0);
rc = ble_gatts_add_svcs(ble_svc_dis_defs);
SYSINIT_PANIC_ASSERT(rc == 0);
}
+96
View File
@@ -0,0 +1,96 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
# Package: nimble/host/services/dis
syscfg.defs:
BLE_SVC_DIS_DEFAULT_READ_PERM:
description: >
Defines default permissions for reading characteristics. Can be
zero to allow read without extra permissions or combination of:
BLE_GATT_CHR_F_READ_ENC
BLE_GATT_CHR_F_READ_AUTHEN
BLE_GATT_CHR_F_READ_AUTHOR
Set to '-1' to remove this characteristic.
value: -1
BLE_SVC_DIS_MODEL_NUMBER_READ_PERM:
description: >
Defines permissions for reading "Model Number" characteristics.
Can be set to MYNEWT_VAL_BLE_SVC_DIS_DEFAULT_READ_PERM or use
the possible values defined in BLE_SVC_DIS_DEFAULT_READ_PERM.
value: 0
BLE_SVC_DIS_MODEL_NUMBER_DEFAULT:
description: >
Defines a default value for "Model number" if not set with
'ble_svc_dis_model_number_set'.
value: '"Apache Mynewt NimBLE"'
BLE_SVC_DIS_SERIAL_NUMBER_READ_PERM:
description: >
Defines permissions for reading "Serial Number" characteristics.
Can be set to MYNEWT_VAL_BLE_SVC_DIS_DEFAULT_READ_PERM or use
the possible values defined in BLE_SVC_DIS_DEFAULT_READ_PERM.
value: MYNEWT_VAL_BLE_SVC_DIS_DEFAULT_READ_PERM
BLE_SVC_DIS_SERIAL_NUMBER_DEFAULT:
description: >
Defines a default value for "Serial number" if not set with
'ble_svc_dis_serial_number_set'.
value: NULL
BLE_SVC_DIS_HARDWARE_REVISION_READ_PERM:
description: >
Defines permissions for reading "Hardware Revision" characteristics.
Can be set to MYNEWT_VAL_BLE_SVC_DIS_DEFAULT_READ_PERM or use
the possible values defined in BLE_SVC_DIS_DEFAULT_READ_PERM.
value: MYNEWT_VAL_BLE_SVC_DIS_DEFAULT_READ_PERM
BLE_SVC_DIS_HARDWARE_REVISION_DEFAULT:
description: >
Defines a default value for "Hardware revision" if not set with
'ble_svc_dis_hardware_revision_set'.
value: NULL
BLE_SVC_DIS_FIRMWARE_REVISION_READ_PERM:
description: >
Defines permissions for reading "Firmware Revision" characteristics.
Can be set to MYNEWT_VAL_BLE_SVC_DIS_DEFAULT_READ_PERM or use
the possible values defined in BLE_SVC_DIS_DEFAULT_READ_PERM.
value: MYNEWT_VAL_BLE_SVC_DIS_DEFAULT_READ_PERM
BLE_SVC_DIS_FIRMWARE_REVISION_DEFAULT:
description: >
Defines a default value for "Software revision" if not set with
'ble_svc_dis_firmware_revision_set'.
value: NULL
BLE_SVC_DIS_SOFTWARE_REVISION_READ_PERM:
description: >
Defines permissions for reading "Software Revision" characteristics.
Can be set to MYNEWT_VAL_BLE_SVC_DIS_DEFAULT_READ_PERM or use
the possible values defined in BLE_SVC_DIS_DEFAULT_READ_PERM.
value: MYNEWT_VAL_BLE_SVC_DIS_DEFAULT_READ_PERM
BLE_SVC_DIS_SOFTWARE_REVISION_DEFAULT:
description: >
Defines a default value for "Software revision" if not set with
'ble_svc_dis_software_revision_set'.
value: NULL
BLE_SVC_DIS_MANUFACTURER_NAME_READ_PERM:
description: >
Defines permissions for reading "Manufacturer name" characteristics.
Can be set to MYNEWT_VAL_BLE_SVC_DIS_DEFAULT_READ_PERM or use
the possible values defined in BLE_SVC_DIS_DEFAULT_READ_PERM.
value: MYNEWT_VAL_BLE_SVC_DIS_DEFAULT_READ_PERM
BLE_SVC_DIS_MANUFACTURER_NAME_DEFAULT:
description: >
Defines a default value for "Manufacturer name" if not set with
'ble_svc_dis_manufacturer_name_set'.
value: NULL