|
| 1 | +/**************************************************************************** |
| 2 | + * apps/examples/ble/ble.c |
| 3 | + * |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 5 | + * contributor license agreements. See the NOTICE file distributed with |
| 6 | + * this work for additional information regarding copyright ownership. The |
| 7 | + * ASF licenses this file to you under the Apache License, Version 2.0 (the |
| 8 | + * "License"); you may not use this file except in compliance with the |
| 9 | + * License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 | + * License for the specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + * |
| 19 | + ****************************************************************************/ |
| 20 | + |
| 21 | +/**************************************************************************** |
| 22 | + * Included Files |
| 23 | + ****************************************************************************/ |
| 24 | + |
| 25 | +#include <nuttx/wireless/bluetooth/bt_core.h> |
| 26 | +#include <nuttx/wireless/bluetooth/bt_gatt.h> |
| 27 | +#include <nuttx/wireless/bluetooth/bt_ioctl.h> |
| 28 | +#include <netpacket/bluetooth.h> |
| 29 | +#include "sensors.h" |
| 30 | + |
| 31 | +/**************************************************************************** |
| 32 | + * Pre-processor Definitions |
| 33 | + ****************************************************************************/ |
| 34 | + |
| 35 | +#define GASVC 0x0001 |
| 36 | +#define NAME_CHR (GASVC + 0x0002) |
| 37 | +#define NAME_DSC (GASVC + 0x0003) |
| 38 | +#define APPEARANCE_CHR (GASVC + 0x0004) |
| 39 | +#define APPEARANCE_DSC (GASVC + 0x0005) |
| 40 | +#define SENSOR_SVC (APPEARANCE_DSC + 0x0001) |
| 41 | +#define TEMP_CHR (SENSOR_SVC + 0x0001) |
| 42 | +#define TEMP_DSC (SENSOR_SVC + 0x0002) |
| 43 | +#define HUM_CHR (TEMP_DSC + 0x0001) |
| 44 | +#define HUM_DSC (TEMP_DSC + 0x0002) |
| 45 | +#define GAS_CHR (HUM_DSC + 0x0001) |
| 46 | +#define GAS_DSC (HUM_DSC + 0x0002) |
| 47 | +#define PRESS_CHR (GAS_DSC + 0x0001) |
| 48 | +#define PRESS_DSC (GAS_DSC + 0x0002) |
| 49 | + |
| 50 | +/* Bluetooth UUIDs */ |
| 51 | + |
| 52 | +static struct bt_uuid_s g_gap_uuid = |
| 53 | +{ |
| 54 | + .type = BT_UUID_16, |
| 55 | + .u.u16 = BT_UUID_GAP, |
| 56 | +}; |
| 57 | + |
| 58 | +static struct bt_uuid_s g_device_name_uuid = |
| 59 | +{ |
| 60 | + .type = BT_UUID_16, |
| 61 | + .u.u16 = BT_UUID_GAP_DEVICE_NAME, |
| 62 | +}; |
| 63 | + |
| 64 | +static struct bt_uuid_s g_appearance_uuid = |
| 65 | +{ |
| 66 | + .type = BT_UUID_16, |
| 67 | + .u.u16 = BT_UUID_GAP_APPEARANCE, |
| 68 | +}; |
| 69 | + |
| 70 | +static struct bt_uuid_s g_sensor_uuid = |
| 71 | +{ |
| 72 | + .type = BT_UUID_128, |
| 73 | + .u.u128 = |
| 74 | + { |
| 75 | + 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, |
| 76 | + 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 |
| 77 | + } |
| 78 | +}; |
| 79 | + |
| 80 | +static struct bt_uuid_s g_temp_uuid = |
| 81 | +{ |
| 82 | + .type = BT_UUID_16, |
| 83 | + .u.u16 = 0x2a6e, /* Temperature UUID */ |
| 84 | +}; |
| 85 | + |
| 86 | +static struct bt_uuid_s g_hum_uuid = |
| 87 | +{ |
| 88 | + .type = BT_UUID_16, |
| 89 | + .u.u16 = 0x2a6f, /* Humidity UUID */ |
| 90 | +}; |
| 91 | + |
| 92 | +static struct bt_uuid_s g_press_uuid = |
| 93 | +{ |
| 94 | + .type = BT_UUID_16, |
| 95 | + .u.u16 = 0x2a6d, /* Pressure UUID */ |
| 96 | +}; |
| 97 | + |
| 98 | +/* GATT characteristics */ |
| 99 | + |
| 100 | +static struct bt_gatt_chrc_s g_name_chrc = |
| 101 | +{ |
| 102 | + .properties = BT_GATT_CHRC_READ, |
| 103 | + .value_handle = NAME_DSC, |
| 104 | + .uuid = &g_device_name_uuid, |
| 105 | +}; |
| 106 | + |
| 107 | +static struct bt_gatt_chrc_s g_appearance_chrc = |
| 108 | +{ |
| 109 | + .properties = BT_GATT_CHRC_READ, |
| 110 | + .value_handle = APPEARANCE_DSC, |
| 111 | + .uuid = &g_appearance_uuid, |
| 112 | +}; |
| 113 | + |
| 114 | +static struct bt_gatt_chrc_s g_temp_chrc = |
| 115 | +{ |
| 116 | + .properties = BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY, |
| 117 | + .value_handle = TEMP_DSC, |
| 118 | + .uuid = &g_temp_uuid, |
| 119 | +}; |
| 120 | + |
| 121 | +static struct bt_gatt_chrc_s g_hum_chrc = |
| 122 | +{ |
| 123 | + .properties = BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY, |
| 124 | + .value_handle = HUM_DSC, |
| 125 | + .uuid = &g_hum_uuid, |
| 126 | +}; |
| 127 | + |
| 128 | +static struct bt_gatt_chrc_s g_press_chrc = |
| 129 | +{ |
| 130 | + .properties = BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY, |
| 131 | + .value_handle = PRESS_DSC, |
| 132 | + .uuid = &g_press_uuid, |
| 133 | +}; |
| 134 | + |
| 135 | +static int read_name(FAR struct bt_conn_s *conn, |
| 136 | + FAR const struct bt_gatt_attr_s *attr, |
| 137 | + FAR void *buf, uint8_t len, uint16_t offset) |
| 138 | +{ |
| 139 | + const char *name = CONFIG_DEVICE_NAME; |
| 140 | + return bt_gatt_attr_read(conn, attr, buf, len, offset, name, strlen(name)); |
| 141 | +} |
| 142 | + |
| 143 | +static int read_appearance(FAR struct bt_conn_s *conn, |
| 144 | + FAR const struct bt_gatt_attr_s *attr, |
| 145 | + FAR void *buf, uint8_t len, uint16_t offset) |
| 146 | +{ |
| 147 | + uint16_t appearance = 0; /* Set appropriate appearance value */ |
| 148 | + return bt_gatt_attr_read(conn, attr, buf, len, offset, |
| 149 | + &appearance, sizeof(appearance)); |
| 150 | +} |
| 151 | + |
| 152 | +static int read_temperature(FAR struct bt_conn_s *conn, |
| 153 | + FAR const struct bt_gatt_attr_s *attr, |
| 154 | + FAR void *buf, uint8_t len, uint16_t offset) |
| 155 | +{ |
| 156 | + uint16_t temp = (uint16_t) (get_temperature() * 100); |
| 157 | + return bt_gatt_attr_read(conn, attr, buf, len, offset, |
| 158 | + &temp, sizeof(uint16_t)); |
| 159 | +} |
| 160 | + |
| 161 | +static int read_humidity(FAR struct bt_conn_s *conn, |
| 162 | + FAR const struct bt_gatt_attr_s *attr, |
| 163 | + FAR void *buf, uint8_t len, uint16_t offset) |
| 164 | +{ |
| 165 | + uint16_t hum = (uint16_t) (get_humidity() * 100); |
| 166 | + return bt_gatt_attr_read(conn, attr, buf, len, offset, |
| 167 | + &hum, sizeof(uint16_t)); |
| 168 | +} |
| 169 | + |
| 170 | +static int read_gas(FAR struct bt_conn_s *conn, |
| 171 | + FAR const struct bt_gatt_attr_s *attr, |
| 172 | + FAR void *buf, uint8_t len, uint16_t offset) |
| 173 | +{ |
| 174 | + uint16_t gas = (uint16_t) (get_gas_resistance() * 100); |
| 175 | + return bt_gatt_attr_read(conn, attr, buf, len, offset, |
| 176 | + &gas, sizeof(uint16_t)); |
| 177 | +} |
| 178 | + |
| 179 | +static int read_pressure(FAR struct bt_conn_s *conn, |
| 180 | + FAR const struct bt_gatt_attr_s *attr, |
| 181 | + FAR void *buf, uint8_t len, uint16_t offset) |
| 182 | +{ |
| 183 | + uint32_t press = (uint32_t) (get_pressure() * 1000); |
| 184 | + return bt_gatt_attr_read(conn, attr, buf, len, offset, |
| 185 | + &press, sizeof(uint32_t)); |
| 186 | +} |
| 187 | + |
| 188 | +/* GATT attributes */ |
| 189 | + |
| 190 | +static const struct bt_gatt_attr_s attrs[] = |
| 191 | +{ |
| 192 | + BT_GATT_PRIMARY_SERVICE(GASVC, &g_gap_uuid), |
| 193 | + BT_GATT_CHARACTERISTIC(NAME_CHR, &g_name_chrc), |
| 194 | + BT_GATT_DESCRIPTOR(NAME_DSC, &g_device_name_uuid, BT_GATT_PERM_READ, |
| 195 | + read_name, NULL, NULL), |
| 196 | + BT_GATT_CHARACTERISTIC(APPEARANCE_CHR, &g_appearance_chrc), |
| 197 | + BT_GATT_DESCRIPTOR(APPEARANCE_DSC, &g_appearance_uuid, |
| 198 | + BT_GATT_PERM_READ, read_appearance, NULL, NULL), |
| 199 | + |
| 200 | + BT_GATT_PRIMARY_SERVICE(SENSOR_SVC, &g_sensor_uuid), |
| 201 | + BT_GATT_CHARACTERISTIC(TEMP_CHR, &g_temp_chrc), |
| 202 | + BT_GATT_DESCRIPTOR(TEMP_DSC, &g_temp_uuid, BT_GATT_PERM_READ, |
| 203 | + read_temperature, NULL, NULL), |
| 204 | + BT_GATT_CHARACTERISTIC(HUM_CHR, &g_hum_chrc), |
| 205 | + BT_GATT_DESCRIPTOR(HUM_DSC, &g_hum_uuid, BT_GATT_PERM_READ, |
| 206 | + read_humidity, NULL, NULL), |
| 207 | + BT_GATT_CHARACTERISTIC(PRESS_CHR, &g_press_chrc), |
| 208 | + BT_GATT_DESCRIPTOR(PRESS_DSC, &g_press_uuid, BT_GATT_PERM_READ, |
| 209 | + read_pressure, NULL, NULL), |
| 210 | +}; |
| 211 | + |
| 212 | +static void start_scanning(void) |
| 213 | +{ |
| 214 | + struct btreq_s btreq; |
| 215 | + memset(&btreq, 0, sizeof(struct btreq_s)); |
| 216 | + strlcpy(btreq.btr_name, "bnep0", 16); |
| 217 | + btreq.btr_dupenable = false; |
| 218 | + |
| 219 | + int sockfd = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_L2CAP); |
| 220 | + if (sockfd < 0) |
| 221 | + { |
| 222 | + fprintf(stderr, "ERROR: failed to create socket\n"); |
| 223 | + return; |
| 224 | + } |
| 225 | + int ret = ioctl(sockfd, SIOCBTSCANSTART, |
| 226 | + (unsigned long)((uintptr_t)&btreq)); |
| 227 | + if (ret < 0) |
| 228 | + { |
| 229 | + fprintf(stderr, "ERROR: ioctl(SIOCBTSCANSTART) failed\n"); |
| 230 | + } |
| 231 | + close(sockfd); |
| 232 | +} |
| 233 | + |
| 234 | +/**************************************************************************** |
| 235 | + * Public Functions |
| 236 | + ****************************************************************************/ |
| 237 | + |
| 238 | +/**************************************************************************** |
| 239 | + * setup_ble |
| 240 | + ****************************************************************************/ |
| 241 | + |
| 242 | +void setup_ble(void) |
| 243 | +{ |
| 244 | + /* Scanning is enabled here to ensure advertising packets are sent. */ |
| 245 | + |
| 246 | + start_scanning(); |
| 247 | + bt_gatt_register(attrs, nitems(attrs)); |
| 248 | +} |
0 commit comments