156 lines
4.6 KiB
C
156 lines
4.6 KiB
C
/*
|
|
* 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 <stdio.h>
|
|
#include <string.h>
|
|
#include "host/ble_hs.h"
|
|
#include "host/ble_uuid.h"
|
|
// #include "blehr_sens.h"
|
|
|
|
#include "services/dis/ble_svc_dis.h"
|
|
#include "services/bas/ble_svc_bas.h"
|
|
|
|
#define GATT_DEVICE_INFO_UUID 0x180A
|
|
#define GATT_MANUFACTURER_NAME_UUID 0x2A29
|
|
#define GATT_MODEL_NUMBER_UUID 0x2A24
|
|
|
|
#define REPLY_BUFFER_SIZE 100
|
|
static char reply[REPLY_BUFFER_SIZE];
|
|
|
|
static int gatt_svr_chr_access_device_info_manufacturer(
|
|
uint16_t conn_handle, uint16_t attr_handle,
|
|
struct ble_gatt_access_ctxt *ctxt, void *arg);
|
|
|
|
static int gatt_svr_chr_access_device_info_model(
|
|
uint16_t conn_handle, uint16_t attr_handle,
|
|
struct ble_gatt_access_ctxt *ctxt, void *arg);
|
|
|
|
/* define several bluetooth services for our device */
|
|
static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
|
|
/*
|
|
* access_cb defines a callback for read and write access events on
|
|
* given characteristics
|
|
*/
|
|
{
|
|
/* Service: Device Information */
|
|
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
|
.uuid = BLE_UUID16_DECLARE(GATT_DEVICE_INFO_UUID),
|
|
.characteristics = (struct ble_gatt_chr_def[]) { {
|
|
/* Characteristic: * Manufacturer name */
|
|
.uuid = BLE_UUID16_DECLARE(GATT_MANUFACTURER_NAME_UUID),
|
|
.access_cb = gatt_svr_chr_access_device_info_manufacturer,
|
|
.flags = BLE_GATT_CHR_F_READ,
|
|
}, {
|
|
/* Characteristic: Model number string */
|
|
.uuid = BLE_UUID16_DECLARE(GATT_MODEL_NUMBER_UUID),
|
|
.access_cb = gatt_svr_chr_access_device_info_model,
|
|
.flags = BLE_GATT_CHR_F_READ,
|
|
}, {
|
|
0, /* No more characteristics in this service */
|
|
}, }
|
|
},
|
|
{
|
|
0, /* No more services */
|
|
},
|
|
};
|
|
|
|
static int gatt_svr_chr_access_device_info_manufacturer(
|
|
uint16_t conn_handle, uint16_t attr_handle,
|
|
struct ble_gatt_access_ctxt *ctxt, void *arg)
|
|
{
|
|
printf("service 'device info: manufacturer' callback triggered\n");
|
|
|
|
snprintf(reply, REPLY_BUFFER_SIZE, "This is TencentOS tiny\n");
|
|
printf("reply: %s\n", reply);
|
|
|
|
int rc = os_mbuf_append(ctxt->om, reply, strlen(reply));
|
|
|
|
printf("\n");
|
|
|
|
return rc;
|
|
}
|
|
|
|
static int gatt_svr_chr_access_device_info_model(
|
|
uint16_t conn_handle, uint16_t attr_handle,
|
|
struct ble_gatt_access_ctxt *ctxt, void *arg)
|
|
{
|
|
printf("service 'device info: model' callback triggered\n");
|
|
|
|
snprintf(reply, REPLY_BUFFER_SIZE, "running TencentOS tiny on nordic ble board");
|
|
printf("reply: %s\n", reply);
|
|
|
|
int rc = os_mbuf_append(ctxt->om, reply, strlen(reply));
|
|
|
|
printf("\n");
|
|
|
|
return rc;
|
|
}
|
|
|
|
void
|
|
gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg)
|
|
{
|
|
char buf[BLE_UUID_STR_LEN];
|
|
|
|
switch (ctxt->op) {
|
|
case BLE_GATT_REGISTER_OP_SVC:
|
|
MODLOG_DFLT(DEBUG, "registered service %s with handle=%d\n",
|
|
ble_uuid_to_str(ctxt->svc.svc_def->uuid, buf),
|
|
ctxt->svc.handle);
|
|
break;
|
|
|
|
case BLE_GATT_REGISTER_OP_CHR:
|
|
MODLOG_DFLT(DEBUG, "registering characteristic %s with "
|
|
"def_handle=%d val_handle=%d\n",
|
|
ble_uuid_to_str(ctxt->chr.chr_def->uuid, buf),
|
|
ctxt->chr.def_handle,
|
|
ctxt->chr.val_handle);
|
|
break;
|
|
|
|
case BLE_GATT_REGISTER_OP_DSC:
|
|
MODLOG_DFLT(DEBUG, "registering descriptor %s with handle=%d\n",
|
|
ble_uuid_to_str(ctxt->dsc.dsc_def->uuid, buf),
|
|
ctxt->dsc.handle);
|
|
break;
|
|
|
|
default:
|
|
assert(0);
|
|
break;
|
|
}
|
|
}
|
|
|
|
int
|
|
gatt_svr_init(void)
|
|
{
|
|
int rc;
|
|
|
|
rc = ble_gatts_count_cfg(gatt_svr_svcs);
|
|
if (rc != 0) {
|
|
return rc;
|
|
}
|
|
|
|
rc = ble_gatts_add_svcs(gatt_svr_svcs);
|
|
if (rc != 0) {
|
|
return rc;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|