Generic Pelion Device Management example for various Advantech modules.

This example is known to work great on the following platforms:

Example Functionality

This example showcases the following device functionality:

  • On timer button increment, simulate Pelion LWM2M button resource change

Use this example with Mbed CLI

1. Import the application into your desktop:

mbed import https://os.mbed.com/teams/Advantech/code/pelion-example-common
cd pelion-example-common

2. Download your developer certificate from pelion portal

3. Compile the program

mbed compile -t <toolchain> -m <TARGET_BOARD>

(supported toolchains : GCC_ARM / ARM / IAR)

4. Copy the binary file pelion-example-common.bin to your mbed device.

Committer:
chuanga
Date:
Tue Mar 12 13:48:39 2019 +0800
Revision:
0:43ff9e3bc244
copying sources from github repository

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chuanga 0:43ff9e3bc244 1 /* Copyright (c) 2015 ARM Limited
chuanga 0:43ff9e3bc244 2 *
chuanga 0:43ff9e3bc244 3 * Licensed under the Apache License, Version 2.0 (the "License");
chuanga 0:43ff9e3bc244 4 * you may not use this file except in compliance with the License.
chuanga 0:43ff9e3bc244 5 * You may obtain a copy of the License at
chuanga 0:43ff9e3bc244 6 *
chuanga 0:43ff9e3bc244 7 * http://www.apache.org/licenses/LICENSE-2.0
chuanga 0:43ff9e3bc244 8 *
chuanga 0:43ff9e3bc244 9 * Unless required by applicable law or agreed to in writing, software
chuanga 0:43ff9e3bc244 10 * distributed under the License is distributed on an "AS IS" BASIS,
chuanga 0:43ff9e3bc244 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
chuanga 0:43ff9e3bc244 12 * See the License for the specific language governing permissions and
chuanga 0:43ff9e3bc244 13 * limitations under the License.
chuanga 0:43ff9e3bc244 14 *
chuanga 0:43ff9e3bc244 15 * @section DESCRIPTION
chuanga 0:43ff9e3bc244 16 *
chuanga 0:43ff9e3bc244 17 * Parser for the AT command syntax
chuanga 0:43ff9e3bc244 18 *
chuanga 0:43ff9e3bc244 19 */
chuanga 0:43ff9e3bc244 20 #ifndef AT_PARSER_H
chuanga 0:43ff9e3bc244 21 #define AT_PARSER_H
chuanga 0:43ff9e3bc244 22
chuanga 0:43ff9e3bc244 23 #include "mbed.h"
chuanga 0:43ff9e3bc244 24 #include <cstdarg>
chuanga 0:43ff9e3bc244 25 #include <vector>
chuanga 0:43ff9e3bc244 26 #include "BufferedSpi.h"
chuanga 0:43ff9e3bc244 27 #include "Callback.h"
chuanga 0:43ff9e3bc244 28
chuanga 0:43ff9e3bc244 29 #define DEFAULT_SPI_TIMEOUT 60000 /* 1 minute */
chuanga 0:43ff9e3bc244 30
chuanga 0:43ff9e3bc244 31 /**
chuanga 0:43ff9e3bc244 32 * Parser class for parsing AT commands
chuanga 0:43ff9e3bc244 33 *
chuanga 0:43ff9e3bc244 34 * Here are some examples:
chuanga 0:43ff9e3bc244 35 * @code
chuanga 0:43ff9e3bc244 36 * ATParser at = ATParser(serial, "\r\n");
chuanga 0:43ff9e3bc244 37 * int value;
chuanga 0:43ff9e3bc244 38 * char buffer[100];
chuanga 0:43ff9e3bc244 39 *
chuanga 0:43ff9e3bc244 40 * at.send("AT") && at.recv("OK");
chuanga 0:43ff9e3bc244 41 * at.send("AT+CWMODE=%d", 3) && at.recv("OK");
chuanga 0:43ff9e3bc244 42 * at.send("AT+CWMODE?") && at.recv("+CWMODE:%d\r\nOK", &value);
chuanga 0:43ff9e3bc244 43 * at.recv("+IPD,%d:", &value);
chuanga 0:43ff9e3bc244 44 * at.read(buffer, value);
chuanga 0:43ff9e3bc244 45 * at.recv("OK");
chuanga 0:43ff9e3bc244 46 * @endcode
chuanga 0:43ff9e3bc244 47 */
chuanga 0:43ff9e3bc244 48 class ATParser {
chuanga 0:43ff9e3bc244 49 private:
chuanga 0:43ff9e3bc244 50 // Serial information
chuanga 0:43ff9e3bc244 51 BufferedSpi *_serial_spi;
chuanga 0:43ff9e3bc244 52 int _buffer_size;
chuanga 0:43ff9e3bc244 53 char *_buffer;
chuanga 0:43ff9e3bc244 54 Mutex _bufferMutex;
chuanga 0:43ff9e3bc244 55
chuanga 0:43ff9e3bc244 56 // Parsing information
chuanga 0:43ff9e3bc244 57 const char *_delimiter;
chuanga 0:43ff9e3bc244 58 int _delim_size;
chuanga 0:43ff9e3bc244 59 char _in_prev;
chuanga 0:43ff9e3bc244 60 volatile bool _aborted;
chuanga 0:43ff9e3bc244 61
chuanga 0:43ff9e3bc244 62 struct oob {
chuanga 0:43ff9e3bc244 63 unsigned len;
chuanga 0:43ff9e3bc244 64 const char *prefix;
chuanga 0:43ff9e3bc244 65 mbed::Callback<void()> cb;
chuanga 0:43ff9e3bc244 66 oob *next;
chuanga 0:43ff9e3bc244 67 };
chuanga 0:43ff9e3bc244 68 oob *_oobs;
chuanga 0:43ff9e3bc244 69
chuanga 0:43ff9e3bc244 70 public:
chuanga 0:43ff9e3bc244 71 /**
chuanga 0:43ff9e3bc244 72 * Constructor
chuanga 0:43ff9e3bc244 73 *
chuanga 0:43ff9e3bc244 74 * @param serial spi interface to use for AT commands
chuanga 0:43ff9e3bc244 75 * @param buffer_size size of internal buffer for transaction
chuanga 0:43ff9e3bc244 76 * @param timeout timeout of the connection
chuanga 0:43ff9e3bc244 77 * @param delimiter string of characters to use as line delimiters
chuanga 0:43ff9e3bc244 78 */
chuanga 0:43ff9e3bc244 79 ATParser(BufferedSpi &serial_spi, const char *delimiter = "\r\n", int buffer_size = 1440, int timeout = DEFAULT_SPI_TIMEOUT);
chuanga 0:43ff9e3bc244 80
chuanga 0:43ff9e3bc244 81 /**
chuanga 0:43ff9e3bc244 82 * Destructor
chuanga 0:43ff9e3bc244 83 */
chuanga 0:43ff9e3bc244 84 ~ATParser()
chuanga 0:43ff9e3bc244 85 {
chuanga 0:43ff9e3bc244 86 while (_oobs) {
chuanga 0:43ff9e3bc244 87 struct oob *oob = _oobs;
chuanga 0:43ff9e3bc244 88 _oobs = oob->next;
chuanga 0:43ff9e3bc244 89 delete oob;
chuanga 0:43ff9e3bc244 90 }
chuanga 0:43ff9e3bc244 91 delete[] _buffer;
chuanga 0:43ff9e3bc244 92 }
chuanga 0:43ff9e3bc244 93
chuanga 0:43ff9e3bc244 94 /**
chuanga 0:43ff9e3bc244 95 * Allows timeout to be changed between commands
chuanga 0:43ff9e3bc244 96 *
chuanga 0:43ff9e3bc244 97 * @param timeout timeout of the connection
chuanga 0:43ff9e3bc244 98 */
chuanga 0:43ff9e3bc244 99 void setTimeout(int timeout)
chuanga 0:43ff9e3bc244 100 {
chuanga 0:43ff9e3bc244 101 _serial_spi->setTimeout(timeout);
chuanga 0:43ff9e3bc244 102 }
chuanga 0:43ff9e3bc244 103
chuanga 0:43ff9e3bc244 104 /**
chuanga 0:43ff9e3bc244 105 * Sets string of characters to use as line delimiters
chuanga 0:43ff9e3bc244 106 *
chuanga 0:43ff9e3bc244 107 * @param delimiter string of characters to use as line delimiters
chuanga 0:43ff9e3bc244 108 */
chuanga 0:43ff9e3bc244 109 void setDelimiter(const char *delimiter)
chuanga 0:43ff9e3bc244 110 {
chuanga 0:43ff9e3bc244 111 _delimiter = delimiter;
chuanga 0:43ff9e3bc244 112 _delim_size = strlen(delimiter);
chuanga 0:43ff9e3bc244 113 }
chuanga 0:43ff9e3bc244 114
chuanga 0:43ff9e3bc244 115 /**
chuanga 0:43ff9e3bc244 116 * Sends an AT command
chuanga 0:43ff9e3bc244 117 *
chuanga 0:43ff9e3bc244 118 * Sends a formatted command using printf style formatting
chuanga 0:43ff9e3bc244 119 * @see printf
chuanga 0:43ff9e3bc244 120 *
chuanga 0:43ff9e3bc244 121 * @param command printf-like format string of command to send which
chuanga 0:43ff9e3bc244 122 * is appended with a newline
chuanga 0:43ff9e3bc244 123 * @param ... all printf-like arguments to insert into command
chuanga 0:43ff9e3bc244 124 * @return true only if command is successfully sent
chuanga 0:43ff9e3bc244 125 */
chuanga 0:43ff9e3bc244 126 bool send(const char *command, ...);
chuanga 0:43ff9e3bc244 127
chuanga 0:43ff9e3bc244 128 bool vsend(const char *command, va_list args);
chuanga 0:43ff9e3bc244 129
chuanga 0:43ff9e3bc244 130 /**
chuanga 0:43ff9e3bc244 131 * Receive an AT response
chuanga 0:43ff9e3bc244 132 *
chuanga 0:43ff9e3bc244 133 * Receives a formatted response using scanf style formatting
chuanga 0:43ff9e3bc244 134 * @see scanf
chuanga 0:43ff9e3bc244 135 *
chuanga 0:43ff9e3bc244 136 * Responses are parsed line at a time.
chuanga 0:43ff9e3bc244 137 * Any received data that does not match the response is ignored until
chuanga 0:43ff9e3bc244 138 * a timeout occurs.
chuanga 0:43ff9e3bc244 139 *
chuanga 0:43ff9e3bc244 140 * @param response scanf-like format string of response to expect
chuanga 0:43ff9e3bc244 141 * @param ... all scanf-like arguments to extract from response
chuanga 0:43ff9e3bc244 142 * @return true only if response is successfully matched
chuanga 0:43ff9e3bc244 143 */
chuanga 0:43ff9e3bc244 144 bool recv(const char *response, ...);
chuanga 0:43ff9e3bc244 145 bool vrecv(const char *response, va_list args);
chuanga 0:43ff9e3bc244 146
chuanga 0:43ff9e3bc244 147
chuanga 0:43ff9e3bc244 148 /**
chuanga 0:43ff9e3bc244 149 * Write a single byte to the underlying stream
chuanga 0:43ff9e3bc244 150 *
chuanga 0:43ff9e3bc244 151 * @param c The byte to write
chuanga 0:43ff9e3bc244 152 * @return The byte that was written or -1 during a timeout
chuanga 0:43ff9e3bc244 153 */
chuanga 0:43ff9e3bc244 154 int putc(char c);
chuanga 0:43ff9e3bc244 155
chuanga 0:43ff9e3bc244 156 /**
chuanga 0:43ff9e3bc244 157 * Get a single byte from the underlying stream
chuanga 0:43ff9e3bc244 158 *
chuanga 0:43ff9e3bc244 159 * @return The byte that was read or -1 during a timeout
chuanga 0:43ff9e3bc244 160 */
chuanga 0:43ff9e3bc244 161 int getc();
chuanga 0:43ff9e3bc244 162
chuanga 0:43ff9e3bc244 163 /**
chuanga 0:43ff9e3bc244 164 * Write an array of bytes to the underlying stream
chuanga 0:43ff9e3bc244 165 * assuming the header of the command is already in _txbuffer
chuanga 0:43ff9e3bc244 166 *
chuanga 0:43ff9e3bc244 167 * @param data the array of bytes to write
chuanga 0:43ff9e3bc244 168 * @param size_of_data number of bytes in data array
chuanga 0:43ff9e3bc244 169 * @param size_in_buff number of bytes already in the internal buff
chuanga 0:43ff9e3bc244 170 * @return number of bytes written or -1 on failure
chuanga 0:43ff9e3bc244 171 */
chuanga 0:43ff9e3bc244 172 int write(const char *data, int size_of_data, int size_in_buff);
chuanga 0:43ff9e3bc244 173
chuanga 0:43ff9e3bc244 174 /**
chuanga 0:43ff9e3bc244 175 * Read an array of bytes from the underlying stream
chuanga 0:43ff9e3bc244 176 *
chuanga 0:43ff9e3bc244 177 * @param data the destination for the read bytes
chuanga 0:43ff9e3bc244 178 * @param size number of bytes to read
chuanga 0:43ff9e3bc244 179 * @return number of bytes read or -1 on failure
chuanga 0:43ff9e3bc244 180 */
chuanga 0:43ff9e3bc244 181 int read(char *data);
chuanga 0:43ff9e3bc244 182
chuanga 0:43ff9e3bc244 183 /**
chuanga 0:43ff9e3bc244 184 * Direct printf to underlying stream
chuanga 0:43ff9e3bc244 185 * @see printf
chuanga 0:43ff9e3bc244 186 *
chuanga 0:43ff9e3bc244 187 * @param format format string to pass to printf
chuanga 0:43ff9e3bc244 188 * @param ... arguments to printf
chuanga 0:43ff9e3bc244 189 * @return number of bytes written or -1 on failure
chuanga 0:43ff9e3bc244 190 */
chuanga 0:43ff9e3bc244 191 int printf(const char *format, ...);
chuanga 0:43ff9e3bc244 192 int vprintf(const char *format, va_list args);
chuanga 0:43ff9e3bc244 193
chuanga 0:43ff9e3bc244 194 /**
chuanga 0:43ff9e3bc244 195 * Direct scanf on underlying stream
chuanga 0:43ff9e3bc244 196 * @see ::scanf
chuanga 0:43ff9e3bc244 197 *
chuanga 0:43ff9e3bc244 198 * @param format format string to pass to scanf
chuanga 0:43ff9e3bc244 199 * @param ... arguments to scanf
chuanga 0:43ff9e3bc244 200 * @return number of bytes read or -1 on failure
chuanga 0:43ff9e3bc244 201 */
chuanga 0:43ff9e3bc244 202 int scanf(const char *format, ...);
chuanga 0:43ff9e3bc244 203
chuanga 0:43ff9e3bc244 204 int vscanf(const char *format, va_list args);
chuanga 0:43ff9e3bc244 205
chuanga 0:43ff9e3bc244 206 /**
chuanga 0:43ff9e3bc244 207 * Attach a callback for out-of-band data
chuanga 0:43ff9e3bc244 208 *
chuanga 0:43ff9e3bc244 209 * @param prefix string on when to initiate callback
chuanga 0:43ff9e3bc244 210 * @param func callback to call when string is read
chuanga 0:43ff9e3bc244 211 * @note out-of-band data is only processed during a scanf call
chuanga 0:43ff9e3bc244 212 */
chuanga 0:43ff9e3bc244 213 void oob(const char *prefix, mbed::Callback<void()> func);
chuanga 0:43ff9e3bc244 214
chuanga 0:43ff9e3bc244 215 /**
chuanga 0:43ff9e3bc244 216 * Flushes the underlying stream
chuanga 0:43ff9e3bc244 217 */
chuanga 0:43ff9e3bc244 218 void flush();
chuanga 0:43ff9e3bc244 219
chuanga 0:43ff9e3bc244 220 /**
chuanga 0:43ff9e3bc244 221 * Abort current recv
chuanga 0:43ff9e3bc244 222 *
chuanga 0:43ff9e3bc244 223 * Can be called from oob handler to interrupt the current
chuanga 0:43ff9e3bc244 224 * recv operation.
chuanga 0:43ff9e3bc244 225 */
chuanga 0:43ff9e3bc244 226 void abort();
chuanga 0:43ff9e3bc244 227
chuanga 0:43ff9e3bc244 228 /**
chuanga 0:43ff9e3bc244 229 * Process out-of-band data
chuanga 0:43ff9e3bc244 230 *
chuanga 0:43ff9e3bc244 231 * Process out-of-band data in the receive buffer. This function
chuanga 0:43ff9e3bc244 232 * returns immediately if there is no data to process.
chuanga 0:43ff9e3bc244 233 *
chuanga 0:43ff9e3bc244 234 * @return true if oob data processed, false otherwise
chuanga 0:43ff9e3bc244 235 */
chuanga 0:43ff9e3bc244 236 bool process_oob(void);
chuanga 0:43ff9e3bc244 237 /**
chuanga 0:43ff9e3bc244 238 * Get buffer_size
chuanga 0:43ff9e3bc244 239 */
chuanga 0:43ff9e3bc244 240 int get_size(void)
chuanga 0:43ff9e3bc244 241 {
chuanga 0:43ff9e3bc244 242 return _buffer_size;
chuanga 0:43ff9e3bc244 243 }
chuanga 0:43ff9e3bc244 244
chuanga 0:43ff9e3bc244 245 };
chuanga 0:43ff9e3bc244 246 #endif