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 MicroBitRadioDatagram.h Source File

MicroBitRadioDatagram.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_RADIO_DATAGRAM_H
00027 #define MICROBIT_RADIO_DATAGRAM_H
00028 
00029 #include "mbed.h"
00030 #include "MicroBitConfig.h"
00031 #include "MicroBitRadio.h"
00032 #include "ManagedString.h"
00033 
00034 /**
00035   * Provides a simple broadcast radio abstraction, built upon the raw nrf51822 RADIO module.
00036   *
00037   * This class provides the ability to broadcast simple text or binary messages to other micro:bits in the vicinity
00038   * It is envisaged that this would provide the basis for children to experiment with building their own, simple,
00039   * custom protocols.
00040   *
00041   * @note This API does not contain any form of encryption, authentication or authorisation. Its purpose is solely for use as a
00042   * teaching aid to demonstrate how simple communications operates, and to provide a sandpit through which learning can take place.
00043   * For serious applications, BLE should be considered a substantially more secure alternative.
00044   */
00045 class MicroBitRadioDatagram
00046 {
00047     MicroBitRadio   &radio;     // The underlying radio module used to send and receive data.
00048     FrameBuffer     *rxQueue;   // A linear list of incoming packets, queued awaiting processing.
00049 
00050     public:
00051 
00052     /**
00053       * Constructor.
00054       *
00055       * Creates an instance of a MicroBitRadioDatagram which offers the ability
00056       * to broadcast simple text or binary messages to other micro:bits in the vicinity
00057       *
00058       * @param r The underlying radio module used to send and receive data.
00059       */
00060     MicroBitRadioDatagram(MicroBitRadio &r);
00061 
00062     /**
00063       * Retrieves packet payload data into the given buffer.
00064       *
00065       * If a data packet is already available, then it will be returned immediately to the caller.
00066       * If no data is available then MICROBIT_INVALID_PARAMETER is returned.
00067       *
00068       * @param buf A pointer to a valid memory location where the received data is to be stored
00069       *
00070       * @param len The maximum amount of data that can safely be stored in 'buf'
00071       *
00072       * @return The length of the data stored, or MICROBIT_INVALID_PARAMETER if no data is available, or the memory regions provided are invalid.
00073       */
00074     int recv(uint8_t *buf, int len);
00075 
00076     /**
00077       * Retreives packet payload data into the given buffer.
00078       *
00079       * If a data packet is already available, then it will be returned immediately to the caller
00080       * in the form of a PacketBuffer.
00081       *
00082       * @return the data received, or an empty PacketBuffer if no data is available.
00083       */
00084     PacketBuffer recv();
00085 
00086     /**
00087       * Transmits the given buffer onto the broadcast radio.
00088       *
00089       * This is a synchronous call that will wait until the transmission of the packet
00090       * has completed before returning.
00091       *
00092       * @param buffer The packet contents to transmit.
00093       *
00094       * @param len The number of bytes to transmit.
00095       *
00096       * @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER if the buffer is invalid,
00097       *         or the number of bytes to transmit is greater than `MICROBIT_RADIO_MAX_PACKET_SIZE + MICROBIT_RADIO_HEADER_SIZE`.
00098       */
00099     int send(uint8_t *buffer, int len);
00100 
00101     /**
00102       * Transmits the given string onto the broadcast radio.
00103       *
00104       * This is a synchronous call that will wait until the transmission of the packet
00105       * has completed before returning.
00106       *
00107       * @param data The packet contents to transmit.
00108       *
00109       * @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER if the buffer is invalid,
00110       *         or the number of bytes to transmit is greater than `MICROBIT_RADIO_MAX_PACKET_SIZE + MICROBIT_RADIO_HEADER_SIZE`.
00111       */
00112     int send(PacketBuffer data);
00113 
00114     /**
00115       * Transmits the given string onto the broadcast radio.
00116       *
00117       * This is a synchronous call that will wait until the transmission of the packet
00118       * has completed before returning.
00119       *
00120       * @param data The packet contents to transmit.
00121       *
00122       * @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER if the buffer is invalid,
00123       *         or the number of bytes to transmit is greater than `MICROBIT_RADIO_MAX_PACKET_SIZE + MICROBIT_RADIO_HEADER_SIZE`.
00124       */
00125     int send(ManagedString data);
00126 
00127     /**
00128       * Protocol handler callback. This is called when the radio receives a packet marked as a datagram.
00129       *
00130       * This function process this packet, and queues it for user reception.
00131       */
00132     void packetReceived();
00133 };
00134 
00135 #endif