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 MicroBitRadioEvent.cpp Source File

MicroBitRadioEvent.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 #include "MicroBitConfig.h"
00027 #include "MicroBitRadio.h"
00028 
00029 /**
00030  * Provides a simple broadcast radio abstraction, built upon the raw nrf51822 RADIO module.
00031  *
00032  * This class provides the ability to extend the micro:bit's default EventModel to other micro:bits in the vicinity,
00033  * in a very similar way to the MicroBitEventService for BLE interfaces.
00034  *
00035  * It is envisaged that this would provide the basis for children to experiment with building their own, simple,
00036  * custom asynchronous events and actions.
00037  *
00038  * @note This API does not contain any form of encryption, authentication or authorisation. Its purpose is solely for use as a
00039  * teaching aid to demonstrate how simple communications operates, and to provide a sandpit through which learning can take place.
00040  * For serious applications, BLE should be considered a substantially more secure alternative.
00041  */
00042 
00043 /**
00044   * Constructor.
00045   *
00046   * Creates an instance of MicroBitRadioEvent which offers the ability to extend
00047   * the micro:bit's default EventModel to other micro:bits in the vicinity.
00048   *
00049   * @param r The underlying radio module used to send and receive data.
00050   */
00051 MicroBitRadioEvent::MicroBitRadioEvent(MicroBitRadio &r) : radio(r)
00052 {
00053     this->suppressForwarding = false;
00054 }
00055 
00056 /**
00057   * Associates the given event with the radio channel.
00058   *
00059   * Once registered, all events matching the given registration sent to this micro:bit's
00060   * default EventModel will be automatically retransmitted on the radio.
00061   *
00062   * @param id The id of the event to register.
00063   *
00064   * @param value the value of the event to register.
00065   *
00066   * @return MICROBIT_OK on success, or MICROBIT_NO_RESOURCES if no default EventModel is available.
00067   *
00068   * @note The wildcards MICROBIT_ID_ANY and MICROBIT_EVT_ANY can also be in place of the
00069   *       id and value fields.
00070   */
00071 int MicroBitRadioEvent::listen(uint16_t id, uint16_t value)
00072 {
00073     if (EventModel::defaultEventBus)
00074         return listen(id, value, *EventModel::defaultEventBus);
00075 
00076     return MICROBIT_NO_RESOURCES;
00077 }
00078 
00079 /**
00080   * Associates the given event with the radio channel.
00081   *
00082   * Once registered, all events matching the given registration sent to the given
00083   * EventModel will be automatically retransmitted on the radio.
00084   *
00085   * @param id The id of the events to register.
00086   *
00087   * @param value the value of the event to register.
00088   *
00089   * @param eventBus The EventModel to listen for events on.
00090   *
00091   * @return MICROBIT_OK on success.
00092   *
00093   * @note The wildcards MICROBIT_ID_ANY and MICROBIT_EVT_ANY can also be in place of the
00094   *       id and value fields.
00095   */
00096 int MicroBitRadioEvent::listen(uint16_t id, uint16_t value, EventModel &eventBus)
00097 {
00098     return eventBus.listen(id, value, this, &MicroBitRadioEvent::eventReceived, MESSAGE_BUS_LISTENER_IMMEDIATE);
00099 }
00100 
00101 /**
00102   * Disassociates the given event with the radio channel.
00103   *
00104   * @param id The id of the events to deregister.
00105   *
00106   * @param value The value of the event to deregister.
00107   *
00108   * @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER if the default message bus does not exist.
00109   *
00110   * @note MICROBIT_EVT_ANY can be used to deregister all event values matching the given id.
00111   */
00112 int MicroBitRadioEvent::ignore(uint16_t id, uint16_t value)
00113 {
00114     if (EventModel::defaultEventBus)
00115         return ignore(id, value, *EventModel::defaultEventBus);
00116 
00117     return MICROBIT_INVALID_PARAMETER;
00118 }
00119 
00120 /**
00121   * Disassociates the given events with the radio channel.
00122   *
00123   * @param id The id of the events to deregister.
00124   *
00125   * @param value The value of the event to deregister.
00126   *
00127   * @param eventBus The EventModel to deregister on.
00128   *
00129   * @return MICROBIT_OK on success.
00130   *
00131   * @note MICROBIT_EVT_ANY can be used to deregister all event values matching the given id.
00132   */
00133 int MicroBitRadioEvent::ignore(uint16_t id, uint16_t value, EventModel &eventBus)
00134 {
00135     return eventBus.ignore(id, value, this, &MicroBitRadioEvent::eventReceived);
00136 }
00137 
00138 
00139 /**
00140   * Protocol handler callback. This is called when the radio receives a packet marked as using the event protocol.
00141   *
00142   * This function process this packet, and fires the event contained inside onto the default EventModel.
00143   */
00144 void MicroBitRadioEvent::packetReceived()
00145 {
00146     FrameBuffer *p = radio.recv();
00147     MicroBitEvent *e = (MicroBitEvent *) p->payload;
00148 
00149     suppressForwarding = true;
00150     e->fire();
00151     suppressForwarding = false;
00152 
00153     delete p;
00154 }
00155 
00156 /**
00157   * Event handler callback. This is called whenever an event is received matching one of those registered through
00158   * the registerEvent() method described above. Upon receiving such an event, it is wrapped into
00159   * a radio packet and transmitted to any other micro:bits in the same group.
00160   */
00161 void MicroBitRadioEvent::eventReceived(MicroBitEvent e)
00162 {
00163     if(suppressForwarding)
00164         return;
00165 
00166     FrameBuffer buf;
00167 
00168     buf.length = sizeof(MicroBitEvent) + MICROBIT_RADIO_HEADER_SIZE - 1;
00169     buf.version = 1;
00170     buf.group = 0;
00171     buf.protocol = MICROBIT_RADIO_PROTOCOL_EVENTBUS;
00172     memcpy(buf.payload, (const uint8_t *)&e, sizeof(MicroBitEvent));
00173 
00174     radio.send(&buf);
00175 }