mbed.org local branch of microbit-dal. The real version lives in git at https://github.com/lancaster-university/microbit-dal

Dependencies:   BLE_API nRF51822 mbed-dev-bin

Dependents:   microbit Microbit IoTChallenge1 microbit ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MicroBitEventService.h Source File

MicroBitEventService.h

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 #ifndef MICROBIT_EVENT_SERVICE_H
00027 #define MICROBIT_EVENT_SERVICE_H
00028 
00029 #include "MicroBitConfig.h"
00030 #include "ble/BLE.h"
00031 #include "MicroBitEvent.h"
00032 #include "EventModel.h"
00033 
00034 // UUIDs for our service and characteristics
00035 extern const uint8_t  MicroBitEventServiceUUID[];
00036 extern const uint8_t  MicroBitEventServiceMicroBitEventCharacteristicUUID[];
00037 extern const uint8_t  MicroBitEventServiceClientEventCharacteristicUUID[];
00038 extern const uint8_t  MicroBitEventServiceMicroBitRequirementsCharacteristicUUID[];
00039 extern const uint8_t  MicroBitEventServiceClientRequirementsCharacteristicUUID[];
00040 
00041 struct EventServiceEvent
00042 {
00043     uint16_t    type;
00044     uint16_t    reason;
00045 };
00046 
00047 
00048 /**
00049   * Class definition for a MicroBit BLE Event Service.
00050   * Provides a BLE gateway onto an Event Model.
00051   */
00052 class MicroBitEventService : public MicroBitComponent
00053 {
00054     public:
00055 
00056     /**
00057       * Constructor.
00058       * Create a representation of the EventService
00059       * @param _ble The instance of a BLE device that we're running on.
00060       * @param _messageBus An instance of an EventModel which events will be mirrored from.
00061       */
00062     MicroBitEventService(BLEDevice &_ble, EventModel &_messageBus);
00063 
00064     /**
00065      * Periodic callback from MicroBit scheduler.
00066      * If we're no longer connected, remove any registered Message Bus listeners.
00067      */
00068     virtual void idleTick();
00069 
00070     /**
00071       * Callback. Invoked when any of our attributes are written via BLE.
00072       */
00073     void onDataWritten(const GattWriteCallbackParams *params);
00074 
00075     /**
00076       * Callback. Invoked when any events are sent on the microBit message bus.
00077       */
00078     void onMicroBitEvent(MicroBitEvent evt);
00079 
00080     /**
00081       * Read callback on microBitRequirements characteristic.
00082       *
00083       * Used to iterate through the events that the code on this micro:bit is interested in.
00084       */
00085     void onRequirementsRead(GattReadAuthCallbackParams *params);
00086 
00087     private:
00088 
00089     // Bluetooth stack we're running on.
00090     BLEDevice           &ble;
00091     EventModel          &messageBus;
00092 
00093     // memory for our event characteristics.
00094     EventServiceEvent   clientEventBuffer;
00095     EventServiceEvent   microBitEventBuffer;
00096     EventServiceEvent   microBitRequirementsBuffer;
00097     EventServiceEvent   clientRequirementsBuffer;
00098 
00099     // handles on this service's characterisitics.
00100     GattAttribute::Handle_t microBitEventCharacteristicHandle;
00101     GattAttribute::Handle_t clientRequirementsCharacteristicHandle;
00102     GattAttribute::Handle_t clientEventCharacteristicHandle;
00103     GattCharacteristic *microBitRequirementsCharacteristic;
00104 
00105     // Message bus offset last sent to the client...
00106     uint16_t messageBusListenerOffset;
00107 
00108 };
00109 
00110 
00111 #endif