teste coragem

Dependencies:   Si1133

Committer:
brunnobbco
Date:
Thu Jul 11 13:13:06 2019 +0000
Revision:
22:f3af2192b3ff
Parent:
21:6cf50085f9f3
Com envio de codigo do sensor

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 14:de95c96e3305 1 /* mbed Microcontroller Library
mbed_official 14:de95c96e3305 2 * Copyright (c) 2017 ARM Limited
mbed_official 14:de95c96e3305 3 *
mbed_official 14:de95c96e3305 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 14:de95c96e3305 5 * you may not use this file except in compliance with the License.
mbed_official 14:de95c96e3305 6 * You may obtain a copy of the License at
mbed_official 14:de95c96e3305 7 *
mbed_official 14:de95c96e3305 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 14:de95c96e3305 9 *
mbed_official 14:de95c96e3305 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 14:de95c96e3305 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 14:de95c96e3305 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 14:de95c96e3305 13 * See the License for the specific language governing permissions and
mbed_official 14:de95c96e3305 14 * limitations under the License.
mbed_official 14:de95c96e3305 15 */
pancotinho 20:86417f956e22 16
pancotinho 20:86417f956e22 17 #include <events/mbed_events.h>
mbed_official 14:de95c96e3305 18 #include <stdio.h>
pancotinho 20:86417f956e22 19 #include "mbed.h"
mbed_official 14:de95c96e3305 20
mbed_official 14:de95c96e3305 21 #include "platform/Callback.h"
mbed_official 14:de95c96e3305 22 #include "events/EventQueue.h"
mbed_official 14:de95c96e3305 23 #include "platform/NonCopyable.h"
mbed_official 14:de95c96e3305 24
mbed_official 14:de95c96e3305 25 #include "ble/BLE.h"
mbed_official 14:de95c96e3305 26 #include "ble/Gap.h"
mbed_official 14:de95c96e3305 27 #include "ble/GattClient.h"
mbed_official 14:de95c96e3305 28 #include "ble/GapAdvertisingParams.h"
mbed_official 14:de95c96e3305 29 #include "ble/GapAdvertisingData.h"
mbed_official 14:de95c96e3305 30 #include "ble/GattServer.h"
mbed_official 14:de95c96e3305 31 #include "BLEProcess.h"
pancotinho 20:86417f956e22 32 #include "Si1133.h"
pancotinho 20:86417f956e22 33
mbed_official 14:de95c96e3305 34
mbed_official 14:de95c96e3305 35 using mbed::callback;
pancotinho 20:86417f956e22 36 Si1133 sensor_light(P0_13, P0_15);
pancotinho 20:86417f956e22 37
mbed_official 14:de95c96e3305 38
mbed_official 14:de95c96e3305 39 /**
pancotinho 20:86417f956e22 40 * A My service that demonstrate the GattServer features.
mbed_official 14:de95c96e3305 41 *
pancotinho 20:86417f956e22 42 * The My service host three characteristics that model the current hour,
pancotinho 20:86417f956e22 43 * led and count of the My. The value of the count characteristic is
mbed_official 14:de95c96e3305 44 * incremented automatically by the system.
mbed_official 14:de95c96e3305 45 *
pancotinho 20:86417f956e22 46 * A client can subscribe to updates of the My characteristics and get
mbed_official 14:de95c96e3305 47 * notified when one of the value is changed. Clients can also change value of
pancotinho 20:86417f956e22 48 * the count, led and hour characteristric.
mbed_official 14:de95c96e3305 49 */
pancotinho 20:86417f956e22 50 class MyService {
pancotinho 20:86417f956e22 51 typedef MyService Self;
mbed_official 14:de95c96e3305 52
mbed_official 14:de95c96e3305 53 public:
pancotinho 20:86417f956e22 54 MyService() :
pancotinho 20:86417f956e22 55 // _hour_char("485f4145-52b9-4644-af1f-7a6b9322490f", 0),
pancotinho 20:86417f956e22 56 _led_char("0a924ca7-87cd-4699-a3bd-abdcd9cf126a", 0),
pancotinho 20:86417f956e22 57 _count_char("8dd6a1b7-bc75-4741-8a26-264af75807de", 0),
pancotinho 20:86417f956e22 58 _My_service(
mbed_official 14:de95c96e3305 59 /* uuid */ "51311102-030e-485f-b122-f8f381aa84ed",
pancotinho 20:86417f956e22 60 /* characteristics */ _My_characteristics,
pancotinho 20:86417f956e22 61 /* numCharacteristics */ sizeof(_My_characteristics) /
pancotinho 20:86417f956e22 62 sizeof(_My_characteristics[0])
mbed_official 14:de95c96e3305 63 ),
mbed_official 14:de95c96e3305 64 _server(NULL),
pancotinho 20:86417f956e22 65 _event_queue(NULL),
pancotinho 20:86417f956e22 66 _alive_led(P1_13, 1),
pancotinho 20:86417f956e22 67 _actuated_led(P1_14,0)
pancotinho 20:86417f956e22 68
mbed_official 14:de95c96e3305 69 {
mbed_official 14:de95c96e3305 70 // update internal pointers (value, descriptors and characteristics array)
pancotinho 20:86417f956e22 71 // _My_characteristics[0] = &_hour_char;
pancotinho 20:86417f956e22 72 _My_characteristics[1] = &_led_char;
pancotinho 20:86417f956e22 73 _My_characteristics[0] = &_count_char;
mbed_official 14:de95c96e3305 74
mbed_official 14:de95c96e3305 75 // setup authorization handlers
pancotinho 20:86417f956e22 76 //_hour_char.setWriteAuthorizationCallback(this, &Self::authorize_client_write);
pancotinho 20:86417f956e22 77 _led_char.setWriteAuthorizationCallback(this, &Self::authorize_client_write);
pancotinho 20:86417f956e22 78 _count_char.setWriteAuthorizationCallback(this, &Self::authorize_client_write);
mbed_official 14:de95c96e3305 79 }
mbed_official 14:de95c96e3305 80
mbed_official 14:de95c96e3305 81
mbed_official 14:de95c96e3305 82
mbed_official 14:de95c96e3305 83 void start(BLE &ble_interface, events::EventQueue &event_queue)
mbed_official 14:de95c96e3305 84 {
mbed_official 14:de95c96e3305 85 if (_event_queue) {
mbed_official 14:de95c96e3305 86 return;
mbed_official 14:de95c96e3305 87 }
mbed_official 14:de95c96e3305 88
mbed_official 14:de95c96e3305 89 _server = &ble_interface.gattServer();
mbed_official 14:de95c96e3305 90 _event_queue = &event_queue;
mbed_official 14:de95c96e3305 91
pancotinho 20:86417f956e22 92
mbed_official 14:de95c96e3305 93 // register the service
mbed_official 14:de95c96e3305 94 printf("Adding demo service\r\n");
pancotinho 20:86417f956e22 95 ble_error_t err = _server->addService(_My_service);
mbed_official 14:de95c96e3305 96
mbed_official 14:de95c96e3305 97 if (err) {
mbed_official 14:de95c96e3305 98 printf("Error %u during demo service registration.\r\n", err);
mbed_official 14:de95c96e3305 99 return;
mbed_official 14:de95c96e3305 100 }
mbed_official 14:de95c96e3305 101
mbed_official 14:de95c96e3305 102 // read write handler
mbed_official 14:de95c96e3305 103 _server->onDataSent(as_cb(&Self::when_data_sent));
mbed_official 14:de95c96e3305 104 _server->onDataWritten(as_cb(&Self::when_data_written));
mbed_official 14:de95c96e3305 105 _server->onDataRead(as_cb(&Self::when_data_read));
mbed_official 14:de95c96e3305 106
mbed_official 14:de95c96e3305 107 // updates subscribtion handlers
mbed_official 14:de95c96e3305 108 _server->onUpdatesEnabled(as_cb(&Self::when_update_enabled));
mbed_official 14:de95c96e3305 109 _server->onUpdatesDisabled(as_cb(&Self::when_update_disabled));
mbed_official 14:de95c96e3305 110 _server->onConfirmationReceived(as_cb(&Self::when_confirmation_received));
mbed_official 14:de95c96e3305 111
mbed_official 14:de95c96e3305 112 // print the handles
pancotinho 20:86417f956e22 113 printf("My service registered\r\n");
pancotinho 20:86417f956e22 114 printf("service handle: %u\r\n", _My_service.getHandle());
pancotinho 20:86417f956e22 115 //printf("\thour characteristic value handle %u\r\n", _hour_char.getValueHandle());
pancotinho 20:86417f956e22 116 // printf("\tled characteristic value handle %u\r\n", _led_char.getValueHandle());
pancotinho 20:86417f956e22 117 printf("\tcount characteristic value handle %u\r\n", _count_char.getValueHandle());
mbed_official 14:de95c96e3305 118
pancotinho 20:86417f956e22 119 _event_queue->call_every(1000 /* ms */, callback(this, &Self::increment_count));
pancotinho 20:86417f956e22 120 _event_queue->call_every(500 /* ms */, callback(this, &Self::blink));
pancotinho 20:86417f956e22 121
mbed_official 14:de95c96e3305 122 }
mbed_official 14:de95c96e3305 123
mbed_official 14:de95c96e3305 124 private:
mbed_official 14:de95c96e3305 125
mbed_official 14:de95c96e3305 126 /**
mbed_official 14:de95c96e3305 127 * Handler called when a notification or an indication has been sent.
mbed_official 14:de95c96e3305 128 */
mbed_official 14:de95c96e3305 129 void when_data_sent(unsigned count)
mbed_official 14:de95c96e3305 130 {
mbed_official 14:de95c96e3305 131 printf("sent %u updates\r\n", count);
mbed_official 14:de95c96e3305 132 }
mbed_official 14:de95c96e3305 133
mbed_official 14:de95c96e3305 134 /**
mbed_official 14:de95c96e3305 135 * Handler called after an attribute has been written.
mbed_official 14:de95c96e3305 136 */
mbed_official 14:de95c96e3305 137 void when_data_written(const GattWriteCallbackParams *e)
mbed_official 14:de95c96e3305 138 {
mbed_official 14:de95c96e3305 139 printf("data written:\r\n");
mbed_official 14:de95c96e3305 140 printf("\tconnection handle: %u\r\n", e->connHandle);
mbed_official 14:de95c96e3305 141 printf("\tattribute handle: %u", e->handle);
pancotinho 20:86417f956e22 142 //if (e->handle == _hour_char.getValueHandle()) {
pancotinho 20:86417f956e22 143 // printf(" (hour characteristic)\r\n");
pancotinho 20:86417f956e22 144 // } else
pancotinho 20:86417f956e22 145 if (e->handle == _led_char.getValueHandle()) {
pancotinho 20:86417f956e22 146 printf(" (led characteristic)\r\n");
pancotinho 20:86417f956e22 147 _actuated_led = *(e->data);
pancotinho 20:86417f956e22 148 } else if (e->handle == _count_char.getValueHandle()) {
pancotinho 20:86417f956e22 149 printf(" (count characteristic)\r\n");
mbed_official 14:de95c96e3305 150 } else {
mbed_official 14:de95c96e3305 151 printf("\r\n");
mbed_official 14:de95c96e3305 152 }
mbed_official 14:de95c96e3305 153 printf("\twrite operation: %u\r\n", e->writeOp);
mbed_official 14:de95c96e3305 154 printf("\toffset: %u\r\n", e->offset);
mbed_official 14:de95c96e3305 155 printf("\tlength: %u\r\n", e->len);
mbed_official 14:de95c96e3305 156 printf("\t data: ");
mbed_official 14:de95c96e3305 157
mbed_official 14:de95c96e3305 158 for (size_t i = 0; i < e->len; ++i) {
mbed_official 14:de95c96e3305 159 printf("%02X", e->data[i]);
mbed_official 14:de95c96e3305 160 }
mbed_official 14:de95c96e3305 161
mbed_official 14:de95c96e3305 162 printf("\r\n");
mbed_official 14:de95c96e3305 163 }
mbed_official 14:de95c96e3305 164
mbed_official 14:de95c96e3305 165 /**
mbed_official 14:de95c96e3305 166 * Handler called after an attribute has been read.
mbed_official 14:de95c96e3305 167 */
mbed_official 14:de95c96e3305 168 void when_data_read(const GattReadCallbackParams *e)
mbed_official 14:de95c96e3305 169 {
mbed_official 14:de95c96e3305 170 printf("data read:\r\n");
mbed_official 14:de95c96e3305 171 printf("\tconnection handle: %u\r\n", e->connHandle);
mbed_official 14:de95c96e3305 172 printf("\tattribute handle: %u", e->handle);
pancotinho 20:86417f956e22 173 // if (e->handle == _hour_char.getValueHandle()) {
pancotinho 20:86417f956e22 174 // printf(" (hour characteristic)\r\n");
pancotinho 20:86417f956e22 175 // } else
pancotinho 20:86417f956e22 176 if (e->handle == _led_char.getValueHandle()) {
pancotinho 20:86417f956e22 177 printf(" (led characteristic)\r\n");
pancotinho 20:86417f956e22 178 } else if (e->handle == _count_char.getValueHandle()) {
pancotinho 20:86417f956e22 179 printf(" (count characteristic)\r\n");
mbed_official 14:de95c96e3305 180 } else {
mbed_official 14:de95c96e3305 181 printf("\r\n");
mbed_official 14:de95c96e3305 182 }
mbed_official 14:de95c96e3305 183 }
mbed_official 14:de95c96e3305 184
mbed_official 14:de95c96e3305 185 /**
mbed_official 14:de95c96e3305 186 * Handler called after a client has subscribed to notification or indication.
mbed_official 14:de95c96e3305 187 *
mbed_official 14:de95c96e3305 188 * @param handle Handle of the characteristic value affected by the change.
mbed_official 14:de95c96e3305 189 */
mbed_official 14:de95c96e3305 190 void when_update_enabled(GattAttribute::Handle_t handle)
mbed_official 14:de95c96e3305 191 {
mbed_official 14:de95c96e3305 192 printf("update enabled on handle %d\r\n", handle);
mbed_official 14:de95c96e3305 193 }
mbed_official 14:de95c96e3305 194
mbed_official 14:de95c96e3305 195 /**
mbed_official 14:de95c96e3305 196 * Handler called after a client has cancelled his subscription from
mbed_official 14:de95c96e3305 197 * notification or indication.
mbed_official 14:de95c96e3305 198 *
mbed_official 14:de95c96e3305 199 * @param handle Handle of the characteristic value affected by the change.
mbed_official 14:de95c96e3305 200 */
mbed_official 14:de95c96e3305 201 void when_update_disabled(GattAttribute::Handle_t handle)
mbed_official 14:de95c96e3305 202 {
mbed_official 14:de95c96e3305 203 printf("update disabled on handle %d\r\n", handle);
mbed_official 14:de95c96e3305 204 }
mbed_official 14:de95c96e3305 205
mbed_official 14:de95c96e3305 206 /**
mbed_official 14:de95c96e3305 207 * Handler called when an indication confirmation has been received.
mbed_official 14:de95c96e3305 208 *
mbed_official 14:de95c96e3305 209 * @param handle Handle of the characteristic value that has emitted the
mbed_official 14:de95c96e3305 210 * indication.
mbed_official 14:de95c96e3305 211 */
mbed_official 14:de95c96e3305 212 void when_confirmation_received(GattAttribute::Handle_t handle)
mbed_official 14:de95c96e3305 213 {
mbed_official 14:de95c96e3305 214 printf("confirmation received on handle %d\r\n", handle);
mbed_official 14:de95c96e3305 215 }
mbed_official 14:de95c96e3305 216
mbed_official 14:de95c96e3305 217 /**
mbed_official 14:de95c96e3305 218 * Handler called when a write request is received.
mbed_official 14:de95c96e3305 219 *
mbed_official 14:de95c96e3305 220 * This handler verify that the value submitted by the client is valid before
mbed_official 14:de95c96e3305 221 * authorizing the operation.
mbed_official 14:de95c96e3305 222 */
mbed_official 14:de95c96e3305 223 void authorize_client_write(GattWriteAuthCallbackParams *e)
mbed_official 14:de95c96e3305 224 {
mbed_official 14:de95c96e3305 225 printf("characteristic %u write authorization\r\n", e->handle);
mbed_official 14:de95c96e3305 226
mbed_official 14:de95c96e3305 227 if (e->offset != 0) {
mbed_official 14:de95c96e3305 228 printf("Error invalid offset\r\n");
mbed_official 14:de95c96e3305 229 e->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INVALID_OFFSET;
mbed_official 14:de95c96e3305 230 return;
mbed_official 14:de95c96e3305 231 }
mbed_official 14:de95c96e3305 232
mbed_official 14:de95c96e3305 233 if (e->len != 1) {
mbed_official 14:de95c96e3305 234 printf("Error invalid len\r\n");
mbed_official 14:de95c96e3305 235 e->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INVALID_ATT_VAL_LENGTH;
mbed_official 14:de95c96e3305 236 return;
mbed_official 14:de95c96e3305 237 }
mbed_official 14:de95c96e3305 238
pancotinho 20:86417f956e22 239 //if ((e->data[0] >= 60) ||
pancotinho 20:86417f956e22 240 // ((e->data[0] >= 24) && (e->handle == _hour_char.getValueHandle()))) {
pancotinho 20:86417f956e22 241 // printf("Error invalid data\r\n");
pancotinho 20:86417f956e22 242 // e->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_WRITE_NOT_PERMITTED;
pancotinho 20:86417f956e22 243 // return;
pancotinho 20:86417f956e22 244 // }
mbed_official 14:de95c96e3305 245
mbed_official 14:de95c96e3305 246 e->authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS;
mbed_official 14:de95c96e3305 247 }
mbed_official 14:de95c96e3305 248
mbed_official 14:de95c96e3305 249 /**
pancotinho 20:86417f956e22 250 * Increment the count counter.
mbed_official 14:de95c96e3305 251 */
pancotinho 20:86417f956e22 252 void increment_count(void)
mbed_official 14:de95c96e3305 253 {
brunnobbco 22:f3af2192b3ff 254 uint8_t count =0;
brunnobbco 22:f3af2192b3ff 255 float float32;
brunnobbco 22:f3af2192b3ff 256 uint8_t count_v[4];
brunnobbco 22:f3af2192b3ff 257 uint8_t *vp;
brunnobbco 22:f3af2192b3ff 258 uint8_t temp;
brunnobbco 22:f3af2192b3ff 259
pancotinho 20:86417f956e22 260
pancotinho 20:86417f956e22 261 ble_error_t err = _count_char.get(*_server, count);
mbed_official 14:de95c96e3305 262 if (err) {
pancotinho 20:86417f956e22 263 printf("read of the count value returned error %u\r\n", err);
pancotinho 20:86417f956e22 264 return;
pancotinho 20:86417f956e22 265 }
brunnobbco 22:f3af2192b3ff 266 // count ++;
pancotinho 20:86417f956e22 267
pancotinho 20:86417f956e22 268
pancotinho 20:86417f956e22 269 if (sensor_light.open()) {
pancotinho 20:86417f956e22 270 printf("Device detected!\n");
pancotinho 20:86417f956e22 271 //Print the current light level
pancotinho 20:86417f956e22 272 printf("Lux = %.3f\n", (float)sensor_light.get_light_level());
pancotinho 20:86417f956e22 273 //Print the current UV index
pancotinho 20:86417f956e22 274 printf("UV index = %.3f\n", (float)sensor_light.get_uv_index());
pancotinho 20:86417f956e22 275 //Sleep for 0.5 seconds
brunnobbco 22:f3af2192b3ff 276 float32= sensor_light.get_light_level();
brunnobbco 22:f3af2192b3ff 277 count = (uint8_t)float32;
brunnobbco 22:f3af2192b3ff 278 //printf("Lux = %.2f\n", float32);
brunnobbco 22:f3af2192b3ff 279 vp = (uint8_t*)&float32;
brunnobbco 22:f3af2192b3ff 280 count_v[0] = vp[0];
brunnobbco 22:f3af2192b3ff 281 count_v[1] = vp[1];
brunnobbco 22:f3af2192b3ff 282 count_v[2] = vp[2];
brunnobbco 22:f3af2192b3ff 283 count_v[3] = vp[3];
brunnobbco 22:f3af2192b3ff 284 *(float*)count_v = float32;
brunnobbco 22:f3af2192b3ff 285 count = count_v[2];
pancotinho 20:86417f956e22 286
pancotinho 20:86417f956e22 287 _actuated_led = !_actuated_led;
pancotinho 20:86417f956e22 288 }
pancotinho 20:86417f956e22 289
pancotinho 20:86417f956e22 290 err = _count_char.set(*_server, count);
pancotinho 20:86417f956e22 291 if (err) {
pancotinho 20:86417f956e22 292 printf("write of the count value returned error %u\r\n", err);
mbed_official 14:de95c96e3305 293 return;
mbed_official 14:de95c96e3305 294 }
mbed_official 14:de95c96e3305 295
pancotinho 20:86417f956e22 296 }
pancotinho 20:86417f956e22 297
pancotinho 20:86417f956e22 298 void blink() {
pancotinho 20:86417f956e22 299 _alive_led = !_alive_led;
mbed_official 14:de95c96e3305 300 }
mbed_official 14:de95c96e3305 301
mbed_official 14:de95c96e3305 302 /**
pancotinho 20:86417f956e22 303 * Change led status.
mbed_official 14:de95c96e3305 304 */
pancotinho 20:86417f956e22 305 void change_led(void)
mbed_official 14:de95c96e3305 306 {
pancotinho 20:86417f956e22 307 uint8_t led = 0;
pancotinho 20:86417f956e22 308 ble_error_t err = _led_char.get(*_server, led);
mbed_official 14:de95c96e3305 309 if (err) {
pancotinho 20:86417f956e22 310 printf("read of the led value returned error %u\r\n", err);
mbed_official 14:de95c96e3305 311 return;
mbed_official 14:de95c96e3305 312 }
mbed_official 14:de95c96e3305 313 }
mbed_official 14:de95c96e3305 314
pancotinho 20:86417f956e22 315 /* * Increment the hour counter.
pancotinho 20:86417f956e22 316
mbed_official 14:de95c96e3305 317 void increment_hour(void)
mbed_official 14:de95c96e3305 318 {
mbed_official 14:de95c96e3305 319 uint8_t hour = 0;
mbed_official 14:de95c96e3305 320 ble_error_t err = _hour_char.get(*_server, hour);
mbed_official 14:de95c96e3305 321 if (err) {
mbed_official 14:de95c96e3305 322 printf("read of the hour value returned error %u\r\n", err);
mbed_official 14:de95c96e3305 323 return;
mbed_official 14:de95c96e3305 324 }
mbed_official 14:de95c96e3305 325
pancotinho 20:86417f956e22 326 hour = (hour + 1) % 24;
mbed_official 14:de95c96e3305 327
mbed_official 14:de95c96e3305 328 err = _hour_char.set(*_server, hour);
mbed_official 14:de95c96e3305 329 if (err) {
mbed_official 14:de95c96e3305 330 printf("write of the hour value returned error %u\r\n", err);
mbed_official 14:de95c96e3305 331 return;
mbed_official 14:de95c96e3305 332 }
mbed_official 14:de95c96e3305 333 }
pancotinho 20:86417f956e22 334 */
mbed_official 14:de95c96e3305 335 private:
mbed_official 14:de95c96e3305 336 /**
mbed_official 14:de95c96e3305 337 * Helper that construct an event handler from a member function of this
mbed_official 14:de95c96e3305 338 * instance.
mbed_official 14:de95c96e3305 339 */
mbed_official 14:de95c96e3305 340 template<typename Arg>
mbed_official 14:de95c96e3305 341 FunctionPointerWithContext<Arg> as_cb(void (Self::*member)(Arg))
mbed_official 14:de95c96e3305 342 {
mbed_official 14:de95c96e3305 343 return makeFunctionPointer(this, member);
mbed_official 14:de95c96e3305 344 }
mbed_official 14:de95c96e3305 345
mbed_official 14:de95c96e3305 346 /**
mbed_official 14:de95c96e3305 347 * Read, Write, Notify, Indicate Characteristic declaration helper.
mbed_official 14:de95c96e3305 348 *
mbed_official 14:de95c96e3305 349 * @tparam T type of data held by the characteristic.
mbed_official 14:de95c96e3305 350 */
mbed_official 14:de95c96e3305 351 template<typename T>
mbed_official 14:de95c96e3305 352 class ReadWriteNotifyIndicateCharacteristic : public GattCharacteristic {
mbed_official 14:de95c96e3305 353 public:
mbed_official 14:de95c96e3305 354 /**
mbed_official 14:de95c96e3305 355 * Construct a characteristic that can be read or written and emit
mbed_official 14:de95c96e3305 356 * notification or indication.
mbed_official 14:de95c96e3305 357 *
mbed_official 14:de95c96e3305 358 * @param[in] uuid The UUID of the characteristic.
mbed_official 14:de95c96e3305 359 * @param[in] initial_value Initial value contained by the characteristic.
mbed_official 14:de95c96e3305 360 */
mbed_official 14:de95c96e3305 361 ReadWriteNotifyIndicateCharacteristic(const UUID & uuid, const T& initial_value) :
mbed_official 14:de95c96e3305 362 GattCharacteristic(
mbed_official 14:de95c96e3305 363 /* UUID */ uuid,
mbed_official 14:de95c96e3305 364 /* Initial value */ &_value,
mbed_official 14:de95c96e3305 365 /* Value size */ sizeof(_value),
mbed_official 14:de95c96e3305 366 /* Value capacity */ sizeof(_value),
mbed_official 14:de95c96e3305 367 /* Properties */ GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ |
mbed_official 14:de95c96e3305 368 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE |
mbed_official 14:de95c96e3305 369 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY |
mbed_official 14:de95c96e3305 370 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE,
mbed_official 14:de95c96e3305 371 /* Descriptors */ NULL,
mbed_official 14:de95c96e3305 372 /* Num descriptors */ 0,
mbed_official 14:de95c96e3305 373 /* variable len */ false
mbed_official 14:de95c96e3305 374 ),
mbed_official 14:de95c96e3305 375 _value(initial_value) {
mbed_official 14:de95c96e3305 376 }
mbed_official 14:de95c96e3305 377
mbed_official 14:de95c96e3305 378 /**
pancotinho 20:86417f956e22 379 * Get the value of this characteristic.
mbed_official 14:de95c96e3305 380 *
mbed_official 14:de95c96e3305 381 * @param[in] server GattServer instance that contain the characteristic
mbed_official 14:de95c96e3305 382 * value.
mbed_official 14:de95c96e3305 383 * @param[in] dst Variable that will receive the characteristic value.
mbed_official 14:de95c96e3305 384 *
mbed_official 14:de95c96e3305 385 * @return BLE_ERROR_NONE in case of success or an appropriate error code.
mbed_official 14:de95c96e3305 386 */
mbed_official 14:de95c96e3305 387 ble_error_t get(GattServer &server, T& dst) const
mbed_official 14:de95c96e3305 388 {
mbed_official 14:de95c96e3305 389 uint16_t value_length = sizeof(dst);
mbed_official 14:de95c96e3305 390 return server.read(getValueHandle(), &dst, &value_length);
mbed_official 14:de95c96e3305 391 }
mbed_official 14:de95c96e3305 392
mbed_official 14:de95c96e3305 393 /**
mbed_official 14:de95c96e3305 394 * Assign a new value to this characteristic.
mbed_official 14:de95c96e3305 395 *
mbed_official 14:de95c96e3305 396 * @param[in] server GattServer instance that will receive the new value.
mbed_official 14:de95c96e3305 397 * @param[in] value The new value to set.
mbed_official 14:de95c96e3305 398 * @param[in] local_only Flag that determine if the change should be kept
mbed_official 14:de95c96e3305 399 * locally or forwarded to subscribed clients.
mbed_official 14:de95c96e3305 400 */
pancotinho 20:86417f956e22 401 ble_error_t set(
pancotinho 20:86417f956e22 402 GattServer &server, const uint8_t &value, bool local_only = false
mbed_official 14:de95c96e3305 403 ) const {
mbed_official 14:de95c96e3305 404 return server.write(getValueHandle(), &value, sizeof(value), local_only);
mbed_official 14:de95c96e3305 405 }
mbed_official 14:de95c96e3305 406
mbed_official 14:de95c96e3305 407 private:
mbed_official 14:de95c96e3305 408 uint8_t _value;
mbed_official 14:de95c96e3305 409 };
mbed_official 14:de95c96e3305 410
pancotinho 20:86417f956e22 411 //ReadWriteNotifyIndicateCharacteristic<uint8_t> _hour_char;
pancotinho 20:86417f956e22 412 ReadWriteNotifyIndicateCharacteristic<uint8_t> _led_char;
pancotinho 20:86417f956e22 413 ReadWriteNotifyIndicateCharacteristic<uint8_t> _count_char;
mbed_official 14:de95c96e3305 414
pancotinho 20:86417f956e22 415 // list of the characteristics of the My service
pancotinho 20:86417f956e22 416 GattCharacteristic* _My_characteristics[2];
mbed_official 14:de95c96e3305 417
mbed_official 14:de95c96e3305 418 // demo service
pancotinho 20:86417f956e22 419 GattService _My_service;
mbed_official 14:de95c96e3305 420
mbed_official 14:de95c96e3305 421 GattServer* _server;
mbed_official 14:de95c96e3305 422 events::EventQueue *_event_queue;
pancotinho 20:86417f956e22 423
pancotinho 20:86417f956e22 424 DigitalOut _alive_led;
pancotinho 20:86417f956e22 425 DigitalOut _actuated_led;
mbed_official 14:de95c96e3305 426 };
mbed_official 14:de95c96e3305 427
mbed_official 14:de95c96e3305 428 int main() {
mbed_official 14:de95c96e3305 429 BLE &ble_interface = BLE::Instance();
mbed_official 14:de95c96e3305 430 events::EventQueue event_queue;
pancotinho 20:86417f956e22 431 MyService demo_service;
mbed_official 14:de95c96e3305 432 BLEProcess ble_process(event_queue, ble_interface);
pancotinho 20:86417f956e22 433
mbed_official 14:de95c96e3305 434
pancotinho 20:86417f956e22 435 ble_process.on_init(callback(&demo_service, &MyService::start));
pancotinho 20:86417f956e22 436
mbed_official 14:de95c96e3305 437
mbed_official 14:de95c96e3305 438 // bind the event queue to the ble interface, initialize the interface
mbed_official 14:de95c96e3305 439 // and start advertising
mbed_official 14:de95c96e3305 440 ble_process.start();
mbed_official 14:de95c96e3305 441
mbed_official 14:de95c96e3305 442 // Process the event queue.
mbed_official 14:de95c96e3305 443 event_queue.dispatch_forever();
mbed_official 14:de95c96e3305 444
mbed_official 14:de95c96e3305 445 return 0;
mbed_official 14:de95c96e3305 446 }