fork
Dependencies: BLE_API mbed-dev-bin nRF51822
MicroBitValueService.cpp
00001 /* 00002 The MIT License (MIT) 00003 00004 Copyright (c) 2016 British Broadcasting Corporation. 00005 This software is provided by Lancaster University by arrangement with the BBC. 00006 00007 Permission is hereby granted, free of charge, to any person obtaining a 00008 copy of this software and associated documentation files (the "Software"), 00009 to deal in the Software without restriction, including without limitation 00010 the rights to use, copy, modify, merge, publish, distribute, sublicense, 00011 and/or sell copies of the Software, and to permit persons to whom the 00012 Software is furnished to do so, subject to the following conditions: 00013 00014 The above copyright notice and this permission notice shall be included in 00015 all copies or substantial portions of the Software. 00016 00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 00020 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00021 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00022 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00023 DEALINGS IN THE SOFTWARE. 00024 */ 00025 00026 /** 00027 * Class definition for the MicroBit BLE Magnetometer Service. 00028 * Provides access to live magnetometer data via BLE, and provides basic configuration options. 00029 */ 00030 #include "MicroBitConfig.h" 00031 #include "ble/UUID.h" 00032 00033 #include "MicroBitValueService.h" 00034 00035 /** 00036 * Constructor. 00037 * Create a representation of the MagnetometerService. 00038 * @param _ble The instance of a BLE device that we're running on. 00039 * @param _compass An instance of MicroBitCompass to use as our Magnetometer source. 00040 */ 00041 MicroBitValueService::MicroBitValueService(BLEDevice &_ble, uint16_t *_value) : 00042 ble(_ble), value(_value) 00043 { 00044 #if CONFIG_ENABLED(MICROBIT_DBG) 00045 if(SERIAL_DEBUG) SERIAL_DEBUG->printf("MicroBitValueService::MicroBitValueService value = %i\r\n", *value); 00046 #endif 00047 // Create the data structures that represent each of our characteristics in Soft Device. 00048 00049 GattCharacteristic valueCharacteristic(MicroBitValueServiceDataUUID, (uint8_t *)&valueCharacteristicBuffer, 0, 00050 sizeof(valueCharacteristicBuffer), 00051 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); 00052 00053 // Initialise our characteristic values. 00054 valueCharacteristicBuffer = *value; 00055 00056 // Set default security requirements 00057 valueCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL); 00058 00059 GattCharacteristic *characteristics[] = {&valueCharacteristic}; 00060 GattService service(MicroBitValueServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); 00061 00062 ble.addService(service); 00063 00064 valueCharacteristicHandle = valueCharacteristic.getValueHandle(); 00065 00066 ble.gattServer().write(valueCharacteristicHandle, (const uint8_t *)&valueCharacteristicBuffer, sizeof(valueCharacteristicBuffer)); 00067 00068 ble.gattServer().onDataWritten(this, &MicroBitValueService::onDataWritten); 00069 00070 if (EventModel::defaultEventBus) 00071 { 00072 EventModel::defaultEventBus->listen(MICROBIT_ID_VALUE, MICROBIT_VALUE_EVT_UPDATE, this, &MicroBitValueService::valueUpdate, MESSAGE_BUS_LISTENER_IMMEDIATE); 00073 } 00074 } 00075 00076 /** 00077 * Callback. Invoked when any of our attributes are written via BLE. 00078 */ 00079 void MicroBitValueService::onDataWritten(const GattWriteCallbackParams *params) 00080 { 00081 00082 if (params->handle == valueCharacteristicHandle && params->len >= sizeof(valueCharacteristicBuffer)) 00083 { 00084 valueCharacteristicBuffer = *((uint16_t *)params->data); 00085 *value = valueCharacteristicBuffer; 00086 //MicroBitEvent e(41, MICROBIT_VALUE_EVT_UPDATE); 00087 } 00088 } 00089 00090 /** 00091 * Magnetometer update callback 00092 */ 00093 void MicroBitValueService::valueUpdate(MicroBitEvent) 00094 { 00095 00096 #if CONFIG_ENABLED(MICROBIT_DBG) 00097 if(SERIAL_DEBUG) SERIAL_DEBUG->printf("MicroBitValueService::valueUpdate\r\n"); 00098 #endif 00099 00100 if (ble.getGapState().connected) 00101 { 00102 valueCharacteristicBuffer = *value; 00103 ble.gattServer().write(valueCharacteristicHandle, (const uint8_t *)&valueCharacteristicBuffer, sizeof(valueCharacteristicBuffer)); 00104 } 00105 00106 } 00107 00108 const uint8_t MicroBitValueServiceUUID[] = { 00109 0xab,0x05,0xf2,0xd8,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 00110 }; 00111 00112 const uint8_t MicroBitValueServiceDataUUID[] = { 00113 0xab,0x05,0xfb,0x11,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 00114 }; 00115 00116 const uint8_t MicroBitValueServicePeriodUUID[] = { 00117 0xab,0x05,0x38,0x6c,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 00118 }; 00119 00120 const uint8_t MicroBitValueServiceBearingUUID[] = { 00121 0xab,0x05,0x97,0x15,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 00122 };
Generated on Fri Nov 4 2022 18:32:46 by
1.7.2