An mbed wrapper around the helium-client to communicate with the Helium Atom

Helium for ARM mbed

This code repository exposes an mbed library for the Helium Atom module. The Helium Atom makes it easy to securely connect IoT devices and applications to back-end IoT services.

Getting Started

See a getting started guide on the Helium site.

Supported Boards

The Helium mbed client should work with any mbed board with an available serial port.

Example Setup

Example applications can be found in the mbed Helium team.

Getting Help

If you have any questions or ideas about how to use this code - or any part of Helium - head over to the Helium Community Slack. We're standing by to help.

Contributing

Want to contribute to helium-mbed? That's awesome!

Please see CONTRIBUTING.md in this repository for details.

Committer:
Marc Nijdam
Date:
Thu Jul 06 10:18:57 2017 -0700
Revision:
14:af7682f4e610
Parent:
12:07a4782fe9f2
Child:
17:f38730193da3
Fixing buffered serial again

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Marc Nijdam 3:475fae677f2d 1 /*
Marc Nijdam 3:475fae677f2d 2 * Copyright 2017, Helium Systems, Inc.
Marc Nijdam 12:07a4782fe9f2 3 * All Rights Reserved. See LICENSE.txt for license information
Marc Nijdam 3:475fae677f2d 4 */
Marc Nijdam 3:475fae677f2d 5
Marc Nijdam 3:475fae677f2d 6 #ifndef HELIUM_H
Marc Nijdam 3:475fae677f2d 7 #define HELIUM_H
Marc Nijdam 3:475fae677f2d 8
Marc Nijdam 3:475fae677f2d 9 #include "mbed.h"
Marc Nijdam 14:af7682f4e610 10 #include "BufferedSerial.h"
Marc Nijdam 3:475fae677f2d 11 #include "helium-client/helium-client.h"
Marc Nijdam 3:475fae677f2d 12
Marc Nijdam 3:475fae677f2d 13 /**
Marc Nijdam 3:475fae677f2d 14 * \class Helium
Marc Nijdam 3:475fae677f2d 15 *
Marc Nijdam 3:475fae677f2d 16 * \brief Enables communication with the Helium Atom
Marc Nijdam 3:475fae677f2d 17 *
Marc Nijdam 3:475fae677f2d 18 * This class offers the basic ability to communicate with the Helium
Marc Nijdam 3:475fae677f2d 19 * Atom module.
Marc Nijdam 3:475fae677f2d 20 */
Marc Nijdam 3:475fae677f2d 21 class Helium
Marc Nijdam 3:475fae677f2d 22 {
Marc Nijdam 3:475fae677f2d 23 public:
Marc Nijdam 3:475fae677f2d 24 /** Create a Helium instance on a hardware serial port.
Marc Nijdam 3:475fae677f2d 25 *
Marc Nijdam 3:475fae677f2d 26 * @param serial The hardware serial port to use
Marc Nijdam 3:475fae677f2d 27 */
Marc Nijdam 3:475fae677f2d 28 Helium(PinName tx, PinName rx);
Marc Nijdam 3:475fae677f2d 29
Marc Nijdam 3:475fae677f2d 30 /** Set communication speed with the Helium Atom.
Marc Nijdam 3:475fae677f2d 31 *
Marc Nijdam 3:475fae677f2d 32 * This sets communication speed with the Helium Atom to the given
Marc Nijdam 3:475fae677f2d 33 * value.
Marc Nijdam 3:475fae677f2d 34 *
Marc Nijdam 3:475fae677f2d 35 * @param baud The baud rate to use.
Marc Nijdam 3:475fae677f2d 36 */
Marc Nijdam 3:475fae677f2d 37 int baud(enum helium_baud baud);
Marc Nijdam 3:475fae677f2d 38
Marc Nijdam 3:475fae677f2d 39 /** Get information on the Helium atom
Marc Nijdam 3:475fae677f2d 40 *
Marc Nijdam 3:475fae677f2d 41 * Gets basic information about the Helium Atom such as it's MAC
Marc Nijdam 3:475fae677f2d 42 * address and the current network time, if connected.
Marc Nijdam 3:475fae677f2d 43 *
Marc Nijdam 3:475fae677f2d 44 * @param info The information structure to fill out
Marc Nijdam 3:475fae677f2d 45 */
Marc Nijdam 3:475fae677f2d 46 int info(struct helium_info * info);
Marc Nijdam 3:475fae677f2d 47
Marc Nijdam 3:475fae677f2d 48 /** Connect the Helium Atom to the network.
Marc Nijdam 3:475fae677f2d 49 *
Marc Nijdam 3:475fae677f2d 50 * This tries to connect the Helium Atom to a nearby Element and
Marc Nijdam 3:475fae677f2d 51 * the Helium Network. Connections can be quick or slow. A quick
Marc Nijdam 3:475fae677f2d 52 * connection attempt to re-connect to the network using revious
Marc Nijdam 3:475fae677f2d 53 * _connection_ state which was retrieved using %sleep. If the
Marc Nijdam 3:475fae677f2d 54 * quick connect was not requested or if it fails a normal, slower
Marc Nijdam 3:475fae677f2d 55 * connection attempt is made.
Marc Nijdam 3:475fae677f2d 56 *
Marc Nijdam 3:475fae677f2d 57 * Connect is an asynchronous operation. The given are a
Marc Nijdam 3:475fae677f2d 58 * convenience to attempt a number of retries before returning the
Marc Nijdam 3:475fae677f2d 59 * current connection state.
Marc Nijdam 3:475fae677f2d 60 *
Marc Nijdam 3:475fae677f2d 61 * @param connection A previously retrieved connection structure
Marc Nijdam 3:475fae677f2d 62 * that reflects sleep state.
Marc Nijdam 3:475fae677f2d 63 *
Marc Nijdam 3:475fae677f2d 64 * @param retries The number of retries to attempt to connect.
Marc Nijdam 3:475fae677f2d 65 *
Marc Nijdam 3:475fae677f2d 66 * @return helium_connect_status
Marc Nijdam 3:475fae677f2d 67 *
Marc Nijdam 3:475fae677f2d 68 * @see Helium#connect()
Marc Nijdam 3:475fae677f2d 69 */
Marc Nijdam 3:475fae677f2d 70 int connect(struct connection * connection, uint32_t retries);
Marc Nijdam 3:475fae677f2d 71
Marc Nijdam 3:475fae677f2d 72 /** Short form connect to the Helium Network.
Marc Nijdam 3:475fae677f2d 73 *
Marc Nijdam 3:475fae677f2d 74 * This is the recommended connect call. It assumes no connection
Marc Nijdam 3:475fae677f2d 75 * state and tries to connect for up to a 5 seconds.
Marc Nijdam 3:475fae677f2d 76 *
Marc Nijdam 3:475fae677f2d 77 */
Marc Nijdam 3:475fae677f2d 78 int connect()
Marc Nijdam 3:475fae677f2d 79 {
Marc Nijdam 3:475fae677f2d 80 return connect(NULL, HELIUM_POLL_RETRIES_5S);
Marc Nijdam 3:475fae677f2d 81 }
Marc Nijdam 3:475fae677f2d 82
Marc Nijdam 3:475fae677f2d 83 /** Check if the Atom is connected to the network
Marc Nijdam 3:475fae677f2d 84 *
Marc Nijdam 3:475fae677f2d 85 * Checks whether the Atom is connected to the network.
Marc Nijdam 3:475fae677f2d 86 *
Marc Nijdam 3:475fae677f2d 87 * @returns true if connected, false if not connected
Marc Nijdam 3:475fae677f2d 88 */
Marc Nijdam 3:475fae677f2d 89 bool connected();
Marc Nijdam 3:475fae677f2d 90
Marc Nijdam 3:475fae677f2d 91 /** Disconnects the Atom from the network and puts it in sleep mode.
Marc Nijdam 3:475fae677f2d 92 *
Marc Nijdam 3:475fae677f2d 93 * This disconnects the Helium Atom from the network and puts it
Marc Nijdam 3:475fae677f2d 94 * in sleep mode. If a connection structure is given, it is filled
Marc Nijdam 3:475fae677f2d 95 * with sleep state which can be used in a sub-sequent connect
Marc Nijdam 3:475fae677f2d 96 * call to quick connect to the network.
Marc Nijdam 3:475fae677f2d 97 *
Marc Nijdam 3:475fae677f2d 98 * @param connection Sleep connection buffer to fill in.
Marc Nijdam 3:475fae677f2d 99 *
Marc Nijdam 3:475fae677f2d 100 * @see Helium::sleep() for the easy version.
Marc Nijdam 3:475fae677f2d 101 */
Marc Nijdam 3:475fae677f2d 102 int sleep(struct connection * connection);
Marc Nijdam 3:475fae677f2d 103
Marc Nijdam 3:475fae677f2d 104 /** Disconnect the Atom and put it to sleep.
Marc Nijdam 3:475fae677f2d 105 *
Marc Nijdam 3:475fae677f2d 106 * This is the convenience version of sleep. A subsequent connect
Marc Nijdam 3:475fae677f2d 107 * call will go through a normal connect cycle.
Marc Nijdam 3:475fae677f2d 108 *
Marc Nijdam 3:475fae677f2d 109 */
Marc Nijdam 3:475fae677f2d 110 int sleep()
Marc Nijdam 3:475fae677f2d 111 {
Marc Nijdam 3:475fae677f2d 112 return sleep(NULL);
Marc Nijdam 3:475fae677f2d 113 }
Marc Nijdam 3:475fae677f2d 114
Marc Nijdam 3:475fae677f2d 115 private:
Marc Nijdam 3:475fae677f2d 116 struct helium_ctx _ctx;
Marc Nijdam 14:af7682f4e610 117 BufferedSerial serial;
Marc Nijdam 3:475fae677f2d 118
Marc Nijdam 3:475fae677f2d 119 friend class Channel;
Marc Nijdam 3:475fae677f2d 120 };
Marc Nijdam 3:475fae677f2d 121
Marc Nijdam 3:475fae677f2d 122 /**
Marc Nijdam 3:475fae677f2d 123 * \class Channel
Marc Nijdam 3:475fae677f2d 124 *
Marc Nijdam 3:475fae677f2d 125 * \brief A Channel to an IoT platform.
Marc Nijdam 3:475fae677f2d 126 *
Marc Nijdam 3:475fae677f2d 127 * Channels represent a delivery mechanism from the device to a number
Marc Nijdam 3:475fae677f2d 128 * of supported back-end IoT platforms.
Marc Nijdam 3:475fae677f2d 129 *
Marc Nijdam 3:475fae677f2d 130 * To use a channel, make sure you have it set up in your Helium
Marc Nijdam 3:475fae677f2d 131 * Dashboard and then call Channel#begin with the channel name as you
Marc Nijdam 3:475fae677f2d 132 * configured it on the Helium Dashboard. This will automatically
Marc Nijdam 3:475fae677f2d 133 * ensure that the device is securely authenticated and registered
Marc Nijdam 3:475fae677f2d 134 * with the channel.
Marc Nijdam 3:475fae677f2d 135 *
Marc Nijdam 3:475fae677f2d 136 */
Marc Nijdam 3:475fae677f2d 137 class Channel
Marc Nijdam 3:475fae677f2d 138 {
Marc Nijdam 3:475fae677f2d 139 public:
Marc Nijdam 3:475fae677f2d 140 /** Construct a channel.
Marc Nijdam 3:475fae677f2d 141 *
Marc Nijdam 3:475fae677f2d 142 * @param helium The Helium Atom to communicate with
Marc Nijdam 3:475fae677f2d 143 */
Marc Nijdam 3:475fae677f2d 144 Channel(Helium * helium);
Marc Nijdam 3:475fae677f2d 145
Marc Nijdam 3:475fae677f2d 146 /** Begin communicating over a channel
Marc Nijdam 3:475fae677f2d 147 *
Marc Nijdam 3:475fae677f2d 148 * Always call this method before calling Channel#send. Beginning
Marc Nijdam 3:475fae677f2d 149 * communication will ensure that the device is authenticated and
Marc Nijdam 3:475fae677f2d 150 * registered with the channel with the given name as configured
Marc Nijdam 3:475fae677f2d 151 * in the Helium Dashboard.
Marc Nijdam 3:475fae677f2d 152 *
Marc Nijdam 3:475fae677f2d 153 * The `result` value will be 0 if the channel was created
Marc Nijdam 3:475fae677f2d 154 * successfully and non-`0` otherwise.
Marc Nijdam 3:475fae677f2d 155 *
Marc Nijdam 3:475fae677f2d 156 * @param name The name of the channel to authenticate with.
Marc Nijdam 3:475fae677f2d 157 * @param result An out parameter for the result of the request.
Marc Nijdam 3:475fae677f2d 158 */
Marc Nijdam 3:475fae677f2d 159 int begin(const char * name, int8_t * result);
Marc Nijdam 3:475fae677f2d 160
Marc Nijdam 3:475fae677f2d 161 /** Send data to this channel.
Marc Nijdam 3:475fae677f2d 162 *
Marc Nijdam 3:475fae677f2d 163 * Send data to a given channel. The given data is sent to the
Marc Nijdam 3:475fae677f2d 164 * configured channel and the result as returned by the channel is
Marc Nijdam 3:475fae677f2d 165 * put in the `result` output variable. The result value is `0`
Marc Nijdam 3:475fae677f2d 166 * for success and non-`0` if an error occurred.
Marc Nijdam 3:475fae677f2d 167 *
Marc Nijdam 3:475fae677f2d 168 * @note The maximum number of bytes that can be transmitted is
Marc Nijdam 3:475fae677f2d 169 * limited to HELIUM_MAX_DATA_SIZE (about 100 bytes).
Marc Nijdam 3:475fae677f2d 170 *
Marc Nijdam 3:475fae677f2d 171 * @param data The data bytes to send
Marc Nijdam 3:475fae677f2d 172 * @param len The number of bytes in data to send
Marc Nijdam 3:475fae677f2d 173 * @param result An out parameter for the result returned by the channel
Marc Nijdam 3:475fae677f2d 174 */
Marc Nijdam 3:475fae677f2d 175 int send(void const * data, size_t len, int8_t * result);
Marc Nijdam 3:475fae677f2d 176
Marc Nijdam 3:475fae677f2d 177 /** Asynchronous begin method.
Marc Nijdam 3:475fae677f2d 178 *
Marc Nijdam 3:475fae677f2d 179 * The Channel#begin(const char *) method is a synchronous version
Marc Nijdam 3:475fae677f2d 180 * of this method. Sending a request to a channel returns a
Marc Nijdam 3:475fae677f2d 181 * `token` which can be used in a subsequent call to #poll() to
Marc Nijdam 3:475fae677f2d 182 * check for results from the remote channel.
Marc Nijdam 3:475fae677f2d 183 *
Marc Nijdam 3:475fae677f2d 184 * @param name The name of the channel to authenticate with.
Marc Nijdam 3:475fae677f2d 185 * @param token The output parameter for the pending result token.
Marc Nijdam 3:475fae677f2d 186 */
Marc Nijdam 3:475fae677f2d 187 int begin(const char * name, uint16_t * token);
Marc Nijdam 3:475fae677f2d 188
Marc Nijdam 3:475fae677f2d 189 /** Asynchornous send method.
Marc Nijdam 3:475fae677f2d 190 *
Marc Nijdam 3:475fae677f2d 191 * This is the asynchronous version of the #send(void const *data,
Marc Nijdam 3:475fae677f2d 192 * size_t int8_t *) method.
Marc Nijdam 3:475fae677f2d 193 *
Marc Nijdam 3:475fae677f2d 194 * @param data The data bytes to send
Marc Nijdam 3:475fae677f2d 195 * @param len The number of bytes in data to send
Marc Nijdam 3:475fae677f2d 196 * @param token The output parameter for the pending result token
Marc Nijdam 3:475fae677f2d 197 */
Marc Nijdam 3:475fae677f2d 198 int send(void const * data, size_t len, uint16_t * token);
Marc Nijdam 3:475fae677f2d 199
Marc Nijdam 3:475fae677f2d 200 /** Poll for a result token.
Marc Nijdam 3:475fae677f2d 201 *
Marc Nijdam 3:475fae677f2d 202 * This polls the Helium Atom for the given number of retries for
Marc Nijdam 3:475fae677f2d 203 * the result of a given `token`. Use HELIUM_POLL_RETRIES_5S for
Marc Nijdam 3:475fae677f2d 204 * the recommended number of retries.
Marc Nijdam 3:475fae677f2d 205 *
Marc Nijdam 3:475fae677f2d 206 * If succcessful the result will be helium_status_OK and the
Marc Nijdam 3:475fae677f2d 207 * result value will be set to the result of the original request
Marc Nijdam 3:475fae677f2d 208 * that the token represents.
Marc Nijdam 3:475fae677f2d 209 *
Marc Nijdam 3:475fae677f2d 210 * @param token The token to check for
Marc Nijdam 3:475fae677f2d 211 * @param result The out parameter for the result of the given request token
Marc Nijdam 3:475fae677f2d 212 * @param retries The number of times to retry
Marc Nijdam 3:475fae677f2d 213 */
Marc Nijdam 3:475fae677f2d 214 int poll(uint16_t token, int8_t * result, uint32_t retries);
Marc Nijdam 3:475fae677f2d 215
Marc Nijdam 3:475fae677f2d 216 private:
Marc Nijdam 3:475fae677f2d 217 Helium * _helium;
Marc Nijdam 3:475fae677f2d 218 int8_t _channel_id;
Marc Nijdam 3:475fae677f2d 219 };
Marc Nijdam 3:475fae677f2d 220
Marc Nijdam 3:475fae677f2d 221 #endif // HELIUM_H