iotio

Fork of Nucleo_BLE_DemoApp by Cortex Challenge Team

Committer:
berlingeradam
Date:
Wed May 27 18:58:49 2015 +0000
Revision:
2:510cac0a0250
Parent:
1:fd7adeffebbe
Child:
3:b1f65162c95b
Copyright update

Who changed what in which revision?

UserRevisionLine numberNew contents of line
berlingeradam 2:510cac0a0250 1 /* Demo BLE service
berlingeradam 2:510cac0a0250 2 * Copyright (c) 2006-2013 ARM Limited
berlingeradam 2:510cac0a0250 3 * Copyright (c) 2015 Adam Berlinger
berlingeradam 2:510cac0a0250 4 *
berlingeradam 2:510cac0a0250 5 * Licensed under the Apache License, Version 2.0 (the "License");
berlingeradam 2:510cac0a0250 6 * you may not use this file except in compliance with the License.
berlingeradam 2:510cac0a0250 7 * You may obtain a copy of the License at
berlingeradam 2:510cac0a0250 8 *
berlingeradam 2:510cac0a0250 9 * http://www.apache.org/licenses/LICENSE-2.0
berlingeradam 2:510cac0a0250 10 *
berlingeradam 2:510cac0a0250 11 * Unless required by applicable law or agreed to in writing, software
berlingeradam 2:510cac0a0250 12 * distributed under the License is distributed on an "AS IS" BASIS,
berlingeradam 2:510cac0a0250 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
berlingeradam 2:510cac0a0250 14 * See the License for the specific language governing permissions and
berlingeradam 2:510cac0a0250 15 * limitations under the License.
berlingeradam 2:510cac0a0250 16 */
berlingeradam 2:510cac0a0250 17
berlingeradam 0:866f2c528c01 18 #ifndef __BLE_DEMOAPP_SERVICE_H__
berlingeradam 0:866f2c528c01 19 #define __BLE_DEMOAPP_SERVICE_H__
berlingeradam 0:866f2c528c01 20
berlingeradam 0:866f2c528c01 21 #include "Stream.h"
berlingeradam 0:866f2c528c01 22
berlingeradam 0:866f2c528c01 23 #include "UUID.h"
berlingeradam 0:866f2c528c01 24 #include "BLEDevice.h"
berlingeradam 0:866f2c528c01 25 #include "Utils.h"
berlingeradam 0:866f2c528c01 26
berlingeradam 1:fd7adeffebbe 27 typedef void (*DemoAppCallback)(uint8_t event);
berlingeradam 1:fd7adeffebbe 28
berlingeradam 0:866f2c528c01 29 class DemoAppService {
berlingeradam 0:866f2c528c01 30 public:
berlingeradam 1:fd7adeffebbe 31 static const uint8_t EVENT_SLIDER1_CHANGED = 0x01;
berlingeradam 1:fd7adeffebbe 32 static const uint8_t EVENT_SLIDER2_CHANGED = 0x02;
berlingeradam 1:fd7adeffebbe 33 static const uint8_t EVENT_BUTTON_CHANGED = 0x04;
berlingeradam 1:fd7adeffebbe 34
berlingeradam 0:866f2c528c01 35 static const uint8_t ServiceUUID[LENGTH_OF_LONG_UUID];
berlingeradam 0:866f2c528c01 36 static const uint8_t slider1CharacteristicUUID[LENGTH_OF_LONG_UUID];
berlingeradam 0:866f2c528c01 37 static const uint8_t slider2CharacteristicUUID[LENGTH_OF_LONG_UUID];
berlingeradam 0:866f2c528c01 38 static const uint8_t buttonCharacteristicUUID[LENGTH_OF_LONG_UUID];
berlingeradam 0:866f2c528c01 39 protected:
berlingeradam 0:866f2c528c01 40 BLEDevice &ble;
berlingeradam 0:866f2c528c01 41
berlingeradam 1:fd7adeffebbe 42 DemoAppCallback eventCallback;
berlingeradam 0:866f2c528c01 43 GattCharacteristic slider1Characteristic;
berlingeradam 0:866f2c528c01 44 GattCharacteristic slider2Characteristic;
berlingeradam 0:866f2c528c01 45 GattCharacteristic buttonCharacteristic;
berlingeradam 0:866f2c528c01 46
berlingeradam 0:866f2c528c01 47 uint16_t lastButtonPressed;
berlingeradam 0:866f2c528c01 48 uint16_t slider1Value;
berlingeradam 0:866f2c528c01 49 uint16_t slider2Value;
berlingeradam 0:866f2c528c01 50 public:
berlingeradam 0:866f2c528c01 51 DemoAppService(BLEDevice &_ble) :
berlingeradam 0:866f2c528c01 52 ble(_ble),
berlingeradam 1:fd7adeffebbe 53 eventCallback(NULL),
berlingeradam 1:fd7adeffebbe 54 slider1Characteristic(slider1CharacteristicUUID, (uint8_t*)&slider1Value, 2, 2,
berlingeradam 0:866f2c528c01 55 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE),
berlingeradam 1:fd7adeffebbe 56 slider2Characteristic(slider2CharacteristicUUID, (uint8_t*)&slider2Value, 2, 2,
berlingeradam 0:866f2c528c01 57 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE),
berlingeradam 1:fd7adeffebbe 58 buttonCharacteristic(buttonCharacteristicUUID, (uint8_t*)&lastButtonPressed, 2, 2,
berlingeradam 0:866f2c528c01 59 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE){
berlingeradam 0:866f2c528c01 60
berlingeradam 0:866f2c528c01 61 GattCharacteristic *charTable[] = {&slider1Characteristic, &slider2Characteristic, &buttonCharacteristic};
berlingeradam 0:866f2c528c01 62 GattService demoService(ServiceUUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
berlingeradam 0:866f2c528c01 63 ble.addService(demoService);
berlingeradam 0:866f2c528c01 64 ble.onDataWritten(this, &DemoAppService::onDataWritten);
berlingeradam 0:866f2c528c01 65 }
berlingeradam 0:866f2c528c01 66
berlingeradam 1:fd7adeffebbe 67 void setCallback(DemoAppCallback callback){eventCallback = callback;}
berlingeradam 1:fd7adeffebbe 68
berlingeradam 1:fd7adeffebbe 69 void waitForEvent(void){ble.waitForEvent(); }
berlingeradam 1:fd7adeffebbe 70
berlingeradam 0:866f2c528c01 71 virtual void onDataWritten(const GattCharacteristicWriteCBParams *params) {
berlingeradam 1:fd7adeffebbe 72 uint8_t event = 0;
berlingeradam 0:866f2c528c01 73 DEBUG("Demo service onWrite\n\r");
berlingeradam 0:866f2c528c01 74 if (params->charHandle == slider1Characteristic.getValueAttribute().getHandle()) {
berlingeradam 0:866f2c528c01 75 uint16_t bytesRead = params->len;
berlingeradam 0:866f2c528c01 76 if (bytesRead == 2) {
berlingeradam 0:866f2c528c01 77 memcpy(&slider1Value, params->data, 2);
berlingeradam 0:866f2c528c01 78 DEBUG("Slider1: %d\n\r", slider1Value);
berlingeradam 1:fd7adeffebbe 79 event |= EVENT_SLIDER1_CHANGED;
berlingeradam 0:866f2c528c01 80 }
berlingeradam 0:866f2c528c01 81 }
berlingeradam 0:866f2c528c01 82 else if (params->charHandle == slider2Characteristic.getValueAttribute().getHandle()) {
berlingeradam 0:866f2c528c01 83 uint16_t bytesRead = params->len;
berlingeradam 0:866f2c528c01 84 if (bytesRead == 2) {
berlingeradam 0:866f2c528c01 85 memcpy(&slider2Value, params->data, 2);
berlingeradam 0:866f2c528c01 86 DEBUG("Slider2: %d\n\r", slider2Value);
berlingeradam 1:fd7adeffebbe 87 event |= EVENT_SLIDER2_CHANGED;
berlingeradam 0:866f2c528c01 88 }
berlingeradam 0:866f2c528c01 89 }
berlingeradam 0:866f2c528c01 90 else if (params->charHandle == buttonCharacteristic.getValueAttribute().getHandle()) {
berlingeradam 0:866f2c528c01 91 uint16_t bytesRead = params->len;
berlingeradam 0:866f2c528c01 92 if (bytesRead == 2) {
berlingeradam 0:866f2c528c01 93 memcpy(&lastButtonPressed, params->data, 2);
berlingeradam 1:fd7adeffebbe 94 event |= EVENT_BUTTON_CHANGED;
berlingeradam 0:866f2c528c01 95 }
berlingeradam 0:866f2c528c01 96 }
berlingeradam 1:fd7adeffebbe 97 if(event && eventCallback){
berlingeradam 1:fd7adeffebbe 98 eventCallback(event);
berlingeradam 1:fd7adeffebbe 99 }
berlingeradam 0:866f2c528c01 100 }
berlingeradam 0:866f2c528c01 101
berlingeradam 0:866f2c528c01 102 uint16_t getSlider1Value()const{return slider1Value;}
berlingeradam 0:866f2c528c01 103 uint16_t getSlider2Value()const{return slider2Value;}
berlingeradam 0:866f2c528c01 104 };
berlingeradam 0:866f2c528c01 105
berlingeradam 1:fd7adeffebbe 106 DemoAppService *startDemoBLE(const char* name);
berlingeradam 1:fd7adeffebbe 107
berlingeradam 0:866f2c528c01 108 #endif