Helium / helium
Committer:
Marc Nijdam
Date:
Tue Aug 22 09:43:11 2017 -0700
Revision:
22:062758492f31
Parent:
20:d55e9eb828d4
Child:
23:cc2c1d1ed159
Remove spurious channel_id from channel docs

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 14:af7682f4e610 9 #include "BufferedSerial.h"
Marc Nijdam 3:475fae677f2d 10 #include "helium-client/helium-client.h"
Marc Nijdam 17:f38730193da3 11 #include "mbed.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 17:f38730193da3 68 int connect(struct connection * connection = NULL,
Marc Nijdam 17:f38730193da3 69 uint32_t retries = HELIUM_POLL_RETRIES_5S);
Marc Nijdam 3:475fae677f2d 70
Marc Nijdam 3:475fae677f2d 71 /** Check if the Atom is connected to the network
Marc Nijdam 3:475fae677f2d 72 *
Marc Nijdam 3:475fae677f2d 73 * Checks whether the Atom is connected to the network.
Marc Nijdam 3:475fae677f2d 74 *
Marc Nijdam 3:475fae677f2d 75 * @returns true if connected, false if not connected
Marc Nijdam 3:475fae677f2d 76 */
Marc Nijdam 3:475fae677f2d 77 bool connected();
Marc Nijdam 3:475fae677f2d 78
Marc Nijdam 3:475fae677f2d 79 /** Disconnects the Atom from the network and puts it in sleep mode.
Marc Nijdam 3:475fae677f2d 80 *
Marc Nijdam 3:475fae677f2d 81 * This disconnects the Helium Atom from the network and puts it
Marc Nijdam 3:475fae677f2d 82 * in sleep mode. If a connection structure is given, it is filled
Marc Nijdam 3:475fae677f2d 83 * with sleep state which can be used in a sub-sequent connect
Marc Nijdam 3:475fae677f2d 84 * call to quick connect to the network.
Marc Nijdam 3:475fae677f2d 85 *
Marc Nijdam 3:475fae677f2d 86 * @param connection Sleep connection buffer to fill in.
Marc Nijdam 3:475fae677f2d 87 */
Marc Nijdam 17:f38730193da3 88 int sleep(struct connection * connection = NULL);
Marc Nijdam 3:475fae677f2d 89
Marc Nijdam 19:be9553194a4e 90 /** Checks if the Atom needs resetting.
Marc Nijdam 19:be9553194a4e 91 *
Marc Nijdam 19:be9553194a4e 92 * Checks if the Atom needs a reset. This is set over the air by
Marc Nijdam 19:be9553194a4e 93 * the Helium network when the Helium Atom has a firmware update
Marc Nijdam 19:be9553194a4e 94 * ready to apply.
Marc Nijdam 19:be9553194a4e 95 *
Marc Nijdam 19:be9553194a4e 96 * @returns true if the Atom needs to be reset, false otherwise.
Marc Nijdam 19:be9553194a4e 97 */
Marc Nijdam 19:be9553194a4e 98 bool needs_reset();
Marc Nijdam 19:be9553194a4e 99
Marc Nijdam 18:c735140666d1 100 /** Resets the Atom.
Marc Nijdam 18:c735140666d1 101 *
Marc Nijdam 18:c735140666d1 102 * This resets the Atom. Use this method when `needs_reset`
Marc Nijdam 18:c735140666d1 103 * returns true to apply any pending firmware updates to the Atom.
Marc Nijdam 18:c735140666d1 104 */
Marc Nijdam 18:c735140666d1 105 int reset();
Marc Nijdam 18:c735140666d1 106
Marc Nijdam 3:475fae677f2d 107
Marc Nijdam 3:475fae677f2d 108 private:
Marc Nijdam 3:475fae677f2d 109 struct helium_ctx _ctx;
Marc Nijdam 17:f38730193da3 110 BufferedSerial serial;
Marc Nijdam 3:475fae677f2d 111
Marc Nijdam 3:475fae677f2d 112 friend class Channel;
Marc Nijdam 3:475fae677f2d 113 };
Marc Nijdam 3:475fae677f2d 114
Marc Nijdam 3:475fae677f2d 115 /**
Marc Nijdam 3:475fae677f2d 116 * \class Channel
Marc Nijdam 3:475fae677f2d 117 *
Marc Nijdam 3:475fae677f2d 118 * \brief A Channel to an IoT platform.
Marc Nijdam 3:475fae677f2d 119 *
Marc Nijdam 3:475fae677f2d 120 * Channels represent a delivery mechanism from the device to a number
Marc Nijdam 3:475fae677f2d 121 * of supported back-end IoT platforms.
Marc Nijdam 3:475fae677f2d 122 *
Marc Nijdam 3:475fae677f2d 123 * To use a channel, make sure you have it set up in your Helium
Marc Nijdam 3:475fae677f2d 124 * Dashboard and then call Channel#begin with the channel name as you
Marc Nijdam 3:475fae677f2d 125 * configured it on the Helium Dashboard. This will automatically
Marc Nijdam 3:475fae677f2d 126 * ensure that the device is securely authenticated and registered
Marc Nijdam 3:475fae677f2d 127 * with the channel.
Marc Nijdam 3:475fae677f2d 128 *
Marc Nijdam 3:475fae677f2d 129 */
Marc Nijdam 3:475fae677f2d 130 class Channel
Marc Nijdam 3:475fae677f2d 131 {
Marc Nijdam 3:475fae677f2d 132 public:
Marc Nijdam 3:475fae677f2d 133 /** Construct a channel.
Marc Nijdam 3:475fae677f2d 134 *
Marc Nijdam 3:475fae677f2d 135 * @param helium The Helium Atom to communicate with
Marc Nijdam 3:475fae677f2d 136 */
Marc Nijdam 3:475fae677f2d 137 Channel(Helium * helium);
Marc Nijdam 3:475fae677f2d 138
Marc Nijdam 3:475fae677f2d 139 /** Begin communicating over a channel
Marc Nijdam 3:475fae677f2d 140 *
Marc Nijdam 3:475fae677f2d 141 * Always call this method before calling Channel#send. Beginning
Marc Nijdam 3:475fae677f2d 142 * communication will ensure that the device is authenticated and
Marc Nijdam 3:475fae677f2d 143 * registered with the channel with the given name as configured
Marc Nijdam 3:475fae677f2d 144 * in the Helium Dashboard.
Marc Nijdam 3:475fae677f2d 145 *
Marc Nijdam 3:475fae677f2d 146 * The `result` value will be 0 if the channel was created
Marc Nijdam 3:475fae677f2d 147 * successfully and non-`0` otherwise.
Marc Nijdam 3:475fae677f2d 148 *
Marc Nijdam 3:475fae677f2d 149 * @param name The name of the channel to authenticate with.
Marc Nijdam 3:475fae677f2d 150 * @param result An out parameter for the result of the request.
Marc Nijdam 3:475fae677f2d 151 */
Marc Nijdam 3:475fae677f2d 152 int begin(const char * name, int8_t * result);
Marc Nijdam 3:475fae677f2d 153
Marc Nijdam 3:475fae677f2d 154 /** Send data to this channel.
Marc Nijdam 3:475fae677f2d 155 *
Marc Nijdam 3:475fae677f2d 156 * Send data to a given channel. The given data is sent to the
Marc Nijdam 3:475fae677f2d 157 * configured channel and the result as returned by the channel is
Marc Nijdam 3:475fae677f2d 158 * put in the `result` output variable. The result value is `0`
Marc Nijdam 3:475fae677f2d 159 * for success and non-`0` if an error occurred.
Marc Nijdam 3:475fae677f2d 160 *
Marc Nijdam 3:475fae677f2d 161 * @note The maximum number of bytes that can be transmitted is
Marc Nijdam 3:475fae677f2d 162 * limited to HELIUM_MAX_DATA_SIZE (about 100 bytes).
Marc Nijdam 3:475fae677f2d 163 *
Marc Nijdam 3:475fae677f2d 164 * @param data The data bytes to send
Marc Nijdam 3:475fae677f2d 165 * @param len The number of bytes in data to send
Marc Nijdam 3:475fae677f2d 166 * @param result An out parameter for the result returned by the channel
Marc Nijdam 3:475fae677f2d 167 */
Marc Nijdam 3:475fae677f2d 168 int send(void const * data, size_t len, int8_t * result);
Marc Nijdam 3:475fae677f2d 169
Marc Nijdam 3:475fae677f2d 170 /** Asynchronous begin method.
Marc Nijdam 3:475fae677f2d 171 *
Marc Nijdam 3:475fae677f2d 172 * The Channel#begin(const char *) method is a synchronous version
Marc Nijdam 3:475fae677f2d 173 * of this method. Sending a request to a channel returns a
Marc Nijdam 3:475fae677f2d 174 * `token` which can be used in a subsequent call to #poll() to
Marc Nijdam 3:475fae677f2d 175 * check for results from the remote channel.
Marc Nijdam 3:475fae677f2d 176 *
Marc Nijdam 3:475fae677f2d 177 * @param name The name of the channel to authenticate with.
Marc Nijdam 3:475fae677f2d 178 * @param token The output parameter for the pending result token.
Marc Nijdam 3:475fae677f2d 179 */
Marc Nijdam 3:475fae677f2d 180 int begin(const char * name, uint16_t * token);
Marc Nijdam 3:475fae677f2d 181
Marc Nijdam 3:475fae677f2d 182 /** Asynchornous send method.
Marc Nijdam 3:475fae677f2d 183 *
Marc Nijdam 3:475fae677f2d 184 * This is the asynchronous version of the #send(void const *data,
Marc Nijdam 3:475fae677f2d 185 * size_t int8_t *) method.
Marc Nijdam 3:475fae677f2d 186 *
Marc Nijdam 3:475fae677f2d 187 * @param data The data bytes to send
Marc Nijdam 3:475fae677f2d 188 * @param len The number of bytes in data to send
Marc Nijdam 3:475fae677f2d 189 * @param token The output parameter for the pending result token
Marc Nijdam 3:475fae677f2d 190 */
Marc Nijdam 3:475fae677f2d 191 int send(void const * data, size_t len, uint16_t * token);
Marc Nijdam 3:475fae677f2d 192
Marc Nijdam 3:475fae677f2d 193 /** Poll for a result token.
Marc Nijdam 3:475fae677f2d 194 *
Marc Nijdam 3:475fae677f2d 195 * This polls the Helium Atom for the given number of retries for
Marc Nijdam 3:475fae677f2d 196 * the result of a given `token`. Use HELIUM_POLL_RETRIES_5S for
Marc Nijdam 3:475fae677f2d 197 * the recommended number of retries.
Marc Nijdam 3:475fae677f2d 198 *
Marc Nijdam 3:475fae677f2d 199 * If succcessful the result will be helium_status_OK and the
Marc Nijdam 3:475fae677f2d 200 * result value will be set to the result of the original request
Marc Nijdam 3:475fae677f2d 201 * that the token represents.
Marc Nijdam 3:475fae677f2d 202 *
Marc Nijdam 3:475fae677f2d 203 * @param token The token to check for
Marc Nijdam 17:f38730193da3 204 * @param result[out] The result of the given request token
Marc Nijdam 3:475fae677f2d 205 * @param retries The number of times to retry
Marc Nijdam 3:475fae677f2d 206 */
Marc Nijdam 17:f38730193da3 207 int poll_result(uint16_t token,
Marc Nijdam 17:f38730193da3 208 int8_t * result,
Marc Nijdam 17:f38730193da3 209 uint32_t retries = HELIUM_POLL_RETRIES_5S);
Marc Nijdam 17:f38730193da3 210
Marc Nijdam 17:f38730193da3 211 /** Poll for data on a channel.
Marc Nijdam 17:f38730193da3 212 *
Marc Nijdam 17:f38730193da3 213 * This polls the Helium Atom for the given number of retries for
Marc Nijdam 20:d55e9eb828d4 214 * any data on the channel.
Marc Nijdam 17:f38730193da3 215 *
Marc Nijdam 17:f38730193da3 216 * If successful the result will be helium_status_OK and the given
Marc Nijdam 17:f38730193da3 217 * `data` buffer will be filled with the received data. The `used`
Marc Nijdam 17:f38730193da3 218 * out parameter will be set to the number of bytes read. Note
Marc Nijdam 17:f38730193da3 219 * that the maximum number of bytes that can be sent is set by
Marc Nijdam 17:f38730193da3 220 * HELIUM_MAX_DATA_SIZE.
Marc Nijdam 17:f38730193da3 221 *
Marc Nijdam 17:f38730193da3 222 * @param[out] data The data buffer to fill with received data
Marc Nijdam 17:f38730193da3 223 * @param len The available length of the data buffer
Marc Nijdam 17:f38730193da3 224 * @param[out] used On success the number of bytes used up in data
Marc Nijdam 17:f38730193da3 225 * @param retries The number of times to retry
Marc Nijdam 17:f38730193da3 226 */
Marc Nijdam 20:d55e9eb828d4 227 int poll_data(void * data,
Marc Nijdam 17:f38730193da3 228 size_t len,
Marc Nijdam 17:f38730193da3 229 size_t * used,
Marc Nijdam 17:f38730193da3 230 uint32_t retries = HELIUM_POLL_RETRIES_5S);
Marc Nijdam 3:475fae677f2d 231
Marc Nijdam 3:475fae677f2d 232 private:
Marc Nijdam 3:475fae677f2d 233 Helium * _helium;
Marc Nijdam 3:475fae677f2d 234 int8_t _channel_id;
Marc Nijdam 3:475fae677f2d 235 };
Marc Nijdam 3:475fae677f2d 236
Marc Nijdam 3:475fae677f2d 237 #endif // HELIUM_H