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 /* SPWFSA01 Device
chuanga 0:43ff9e3bc244 2 * Copyright (c) 2015 ARM Limited
chuanga 0:43ff9e3bc244 3 *
chuanga 0:43ff9e3bc244 4 * Licensed under the Apache License, Version 2.0 (the "License");
chuanga 0:43ff9e3bc244 5 * you may not use this file except in compliance with the License.
chuanga 0:43ff9e3bc244 6 * You may obtain a copy of the License at
chuanga 0:43ff9e3bc244 7 *
chuanga 0:43ff9e3bc244 8 * http://www.apache.org/licenses/LICENSE-2.0
chuanga 0:43ff9e3bc244 9 *
chuanga 0:43ff9e3bc244 10 * Unless required by applicable law or agreed to in writing, software
chuanga 0:43ff9e3bc244 11 * distributed under the License is distributed on an "AS IS" BASIS,
chuanga 0:43ff9e3bc244 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
chuanga 0:43ff9e3bc244 13 * See the License for the specific language governing permissions and
chuanga 0:43ff9e3bc244 14 * limitations under the License.
chuanga 0:43ff9e3bc244 15 */
chuanga 0:43ff9e3bc244 16
chuanga 0:43ff9e3bc244 17 #include "SPWFSA01.h"
chuanga 0:43ff9e3bc244 18 #include "SpwfSAInterface.h"
chuanga 0:43ff9e3bc244 19 #include "mbed_debug.h"
chuanga 0:43ff9e3bc244 20
chuanga 0:43ff9e3bc244 21 #if MBED_CONF_IDW0XX1_EXPANSION_BOARD == IDW01M1
chuanga 0:43ff9e3bc244 22
chuanga 0:43ff9e3bc244 23 SPWFSA01::SPWFSA01(PinName tx, PinName rx,
chuanga 0:43ff9e3bc244 24 PinName rts, PinName cts,
chuanga 0:43ff9e3bc244 25 SpwfSAInterface &ifce, bool debug,
chuanga 0:43ff9e3bc244 26 PinName wakeup, PinName reset)
chuanga 0:43ff9e3bc244 27 : SPWFSAxx(tx, rx, rts, cts, ifce, debug, wakeup, reset) {
chuanga 0:43ff9e3bc244 28 }
chuanga 0:43ff9e3bc244 29
chuanga 0:43ff9e3bc244 30 bool SPWFSA01::open(const char *type, int* spwf_id, const char* addr, int port)
chuanga 0:43ff9e3bc244 31 {
chuanga 0:43ff9e3bc244 32 int socket_id;
chuanga 0:43ff9e3bc244 33 int value;
chuanga 0:43ff9e3bc244 34 int trials;
chuanga 0:43ff9e3bc244 35
chuanga 0:43ff9e3bc244 36 if(!_parser.send("AT+S.SOCKON=%s,%d,%s,ind", addr, port, type))
chuanga 0:43ff9e3bc244 37 {
chuanga 0:43ff9e3bc244 38 debug_if(_dbg_on, "\r\nSPWF> `SPWFSA01::open`: error opening socket (%d)\r\n", __LINE__);
chuanga 0:43ff9e3bc244 39 return false;
chuanga 0:43ff9e3bc244 40 }
chuanga 0:43ff9e3bc244 41
chuanga 0:43ff9e3bc244 42 /* handle both response possibilities here before returning
chuanga 0:43ff9e3bc244 43 * otherwise module seems to remain in inconsistent state.
chuanga 0:43ff9e3bc244 44 */
chuanga 0:43ff9e3bc244 45
chuanga 0:43ff9e3bc244 46 /* wait for first character */
chuanga 0:43ff9e3bc244 47 trials = 0;
chuanga 0:43ff9e3bc244 48 while((value = _parser.getc()) < 0) {
chuanga 0:43ff9e3bc244 49 if(trials++ > SPWFXX_MAX_TRIALS) {
chuanga 0:43ff9e3bc244 50 debug("\r\nSPWF> error opening socket (%d)\r\n", __LINE__);
chuanga 0:43ff9e3bc244 51 empty_rx_buffer();
chuanga 0:43ff9e3bc244 52 return false;
chuanga 0:43ff9e3bc244 53 }
chuanga 0:43ff9e3bc244 54 }
chuanga 0:43ff9e3bc244 55
chuanga 0:43ff9e3bc244 56 if(value != _cr_) { // Note: this is different to what the spec exactly says
chuanga 0:43ff9e3bc244 57 debug_if(_dbg_on, "\r\nSPWF> error opening socket (%d)\r\n", __LINE__);
chuanga 0:43ff9e3bc244 58 empty_rx_buffer();
chuanga 0:43ff9e3bc244 59 return false;
chuanga 0:43ff9e3bc244 60 }
chuanga 0:43ff9e3bc244 61
chuanga 0:43ff9e3bc244 62 if(!_recv_delim_lf()) { // Note: this is different to what the spec exactly says
chuanga 0:43ff9e3bc244 63 debug_if(_dbg_on, "\r\nSPWF> error opening socket (%d)\r\n", __LINE__);
chuanga 0:43ff9e3bc244 64 empty_rx_buffer();
chuanga 0:43ff9e3bc244 65 return false;
chuanga 0:43ff9e3bc244 66 }
chuanga 0:43ff9e3bc244 67
chuanga 0:43ff9e3bc244 68 value = _parser.getc();
chuanga 0:43ff9e3bc244 69 switch(value) {
chuanga 0:43ff9e3bc244 70 case ' ':
chuanga 0:43ff9e3bc244 71 if(_parser.recv("ID: %d\n", &socket_id)
chuanga 0:43ff9e3bc244 72 && _recv_ok()) {
chuanga 0:43ff9e3bc244 73 debug_if(_dbg_on, "AT^ ID: %d\r\n", socket_id);
chuanga 0:43ff9e3bc244 74
chuanga 0:43ff9e3bc244 75 *spwf_id = socket_id;
chuanga 0:43ff9e3bc244 76 return true;
chuanga 0:43ff9e3bc244 77 } else {
chuanga 0:43ff9e3bc244 78 empty_rx_buffer();
chuanga 0:43ff9e3bc244 79 }
chuanga 0:43ff9e3bc244 80 break;
chuanga 0:43ff9e3bc244 81 case 'E':
chuanga 0:43ff9e3bc244 82 if(_parser.recv("RROR: %255[^\n]\n", _msg_buffer) && _recv_delim_lf()) {
chuanga 0:43ff9e3bc244 83 debug_if(_dbg_on, "AT^ ERROR: %s (%d)\r\n", _msg_buffer, __LINE__);
chuanga 0:43ff9e3bc244 84 } else {
chuanga 0:43ff9e3bc244 85 debug_if(_dbg_on, "\r\nSPWF> error opening socket (%d)\r\n", __LINE__);
chuanga 0:43ff9e3bc244 86 empty_rx_buffer();
chuanga 0:43ff9e3bc244 87 }
chuanga 0:43ff9e3bc244 88 break;
chuanga 0:43ff9e3bc244 89 default:
chuanga 0:43ff9e3bc244 90 debug_if(_dbg_on, "\r\nSPWF> error opening socket (value=%d, %d)\r\n", value, __LINE__);
chuanga 0:43ff9e3bc244 91 break;
chuanga 0:43ff9e3bc244 92 }
chuanga 0:43ff9e3bc244 93
chuanga 0:43ff9e3bc244 94 return false;
chuanga 0:43ff9e3bc244 95 }
chuanga 0:43ff9e3bc244 96
chuanga 0:43ff9e3bc244 97 int SPWFSA01::_read_in(char* buffer, int spwf_id, uint32_t amount) {
chuanga 0:43ff9e3bc244 98 int ret = -1;
chuanga 0:43ff9e3bc244 99
chuanga 0:43ff9e3bc244 100 MBED_ASSERT(buffer != NULL);
chuanga 0:43ff9e3bc244 101
chuanga 0:43ff9e3bc244 102 /* block asynchronous indications */
chuanga 0:43ff9e3bc244 103 if(!_winds_off()) {
chuanga 0:43ff9e3bc244 104 return -1;
chuanga 0:43ff9e3bc244 105 }
chuanga 0:43ff9e3bc244 106
chuanga 0:43ff9e3bc244 107 /* read in data */
chuanga 0:43ff9e3bc244 108 if(_parser.send("AT+S.SOCKR=%d,%u", spwf_id, (unsigned int)amount)) {
chuanga 0:43ff9e3bc244 109 /* set high timeout */
chuanga 0:43ff9e3bc244 110 _parser.set_timeout(SPWF_READ_BIN_TIMEOUT);
chuanga 0:43ff9e3bc244 111 /* read in binary data */
chuanga 0:43ff9e3bc244 112 int read = _parser.read(buffer, amount);
chuanga 0:43ff9e3bc244 113 /* reset timeout value */
chuanga 0:43ff9e3bc244 114 _parser.set_timeout(_timeout);
chuanga 0:43ff9e3bc244 115 if(read > 0) {
chuanga 0:43ff9e3bc244 116 if(_recv_ok()) {
chuanga 0:43ff9e3bc244 117 ret = amount;
chuanga 0:43ff9e3bc244 118
chuanga 0:43ff9e3bc244 119 /* remove from pending sizes
chuanga 0:43ff9e3bc244 120 * (MUST be done before next async indications handling (e.g. `_winds_on()`)) */
chuanga 0:43ff9e3bc244 121 _remove_pending_pkt_size(spwf_id, amount);
chuanga 0:43ff9e3bc244 122 } else {
chuanga 0:43ff9e3bc244 123 debug_if(_dbg_on, "\r\nSPWF> failed to receive OK (%s, %d)\r\n", __func__, __LINE__);
chuanga 0:43ff9e3bc244 124 empty_rx_buffer();
chuanga 0:43ff9e3bc244 125 }
chuanga 0:43ff9e3bc244 126 } else {
chuanga 0:43ff9e3bc244 127 debug_if(_dbg_on, "\r\nSPWF> failed to read binary data (%u:%d), (%s, %d)\r\n", amount, read, __func__, __LINE__);
chuanga 0:43ff9e3bc244 128 empty_rx_buffer();
chuanga 0:43ff9e3bc244 129 }
chuanga 0:43ff9e3bc244 130 } else {
chuanga 0:43ff9e3bc244 131 debug_if(_dbg_on, "\r\nSPWF> failed to send SOCKR (%s, %d)\r\n", __func__, __LINE__);
chuanga 0:43ff9e3bc244 132 }
chuanga 0:43ff9e3bc244 133
chuanga 0:43ff9e3bc244 134 debug_if(_dbg_on, "\r\nSPWF> %s():\t%d:%d\r\n", __func__, spwf_id, amount);
chuanga 0:43ff9e3bc244 135
chuanga 0:43ff9e3bc244 136 /* unblock asynchronous indications */
chuanga 0:43ff9e3bc244 137 _winds_on();
chuanga 0:43ff9e3bc244 138
chuanga 0:43ff9e3bc244 139 return ret;
chuanga 0:43ff9e3bc244 140 }
chuanga 0:43ff9e3bc244 141
chuanga 0:43ff9e3bc244 142 /* betzw - TODO: improve performance! */
chuanga 0:43ff9e3bc244 143 bool SPWFSA01::_recv_ap(nsapi_wifi_ap_t *ap)
chuanga 0:43ff9e3bc244 144 {
chuanga 0:43ff9e3bc244 145 bool ret;
chuanga 0:43ff9e3bc244 146 unsigned int channel;
chuanga 0:43ff9e3bc244 147 int trials;
chuanga 0:43ff9e3bc244 148
chuanga 0:43ff9e3bc244 149 ap->security = NSAPI_SECURITY_UNKNOWN;
chuanga 0:43ff9e3bc244 150
chuanga 0:43ff9e3bc244 151 /* check for end of list */
chuanga 0:43ff9e3bc244 152 if(_recv_delim_cr_lf()) {
chuanga 0:43ff9e3bc244 153 return false;
chuanga 0:43ff9e3bc244 154 }
chuanga 0:43ff9e3bc244 155
chuanga 0:43ff9e3bc244 156 /* run to 'horizontal tab' */
chuanga 0:43ff9e3bc244 157 trials = 0;
chuanga 0:43ff9e3bc244 158 while(_parser.getc() != '\x09') {
chuanga 0:43ff9e3bc244 159 if(trials++ > SPWFXX_MAX_TRIALS) {
chuanga 0:43ff9e3bc244 160 debug("\r\nSPWF> WARNING: might happen in case of RX buffer overflow! (%s, %d)\r\n", __func__, __LINE__);
chuanga 0:43ff9e3bc244 161 return false;
chuanga 0:43ff9e3bc244 162 }
chuanga 0:43ff9e3bc244 163 }
chuanga 0:43ff9e3bc244 164
chuanga 0:43ff9e3bc244 165 /* read in next line */
chuanga 0:43ff9e3bc244 166 ret = _parser.recv("%255[^\n]\n", _msg_buffer) && _recv_delim_lf();
chuanga 0:43ff9e3bc244 167
chuanga 0:43ff9e3bc244 168 /* parse line - first phase */
chuanga 0:43ff9e3bc244 169 if(ret) {
chuanga 0:43ff9e3bc244 170 int val = sscanf(_msg_buffer,
chuanga 0:43ff9e3bc244 171 " %*s %hhx:%hhx:%hhx:%hhx:%hhx:%hhx CHAN: %u RSSI: %hhd SSID: \'%*255[^\']\'",
chuanga 0:43ff9e3bc244 172 &ap->bssid[0], &ap->bssid[1], &ap->bssid[2], &ap->bssid[3], &ap->bssid[4], &ap->bssid[5],
chuanga 0:43ff9e3bc244 173 &channel, &ap->rssi);
chuanga 0:43ff9e3bc244 174 if(val < 8) {
chuanga 0:43ff9e3bc244 175 ret = false;
chuanga 0:43ff9e3bc244 176 }
chuanga 0:43ff9e3bc244 177 }
chuanga 0:43ff9e3bc244 178
chuanga 0:43ff9e3bc244 179 /* parse line - second phase */
chuanga 0:43ff9e3bc244 180 if(ret) { // ret == true
chuanga 0:43ff9e3bc244 181 char value;
chuanga 0:43ff9e3bc244 182 char *rest, *first, *last;
chuanga 0:43ff9e3bc244 183
chuanga 0:43ff9e3bc244 184 /* decide about position of `CAPS:` */
chuanga 0:43ff9e3bc244 185 first = strchr(_msg_buffer, '\'');
chuanga 0:43ff9e3bc244 186 if(first == NULL) {
chuanga 0:43ff9e3bc244 187 debug("\r\nSPWF> WARNING: might happen in case of RX buffer overflow! (%s, %d)\r\n", __func__, __LINE__);
chuanga 0:43ff9e3bc244 188 return false;
chuanga 0:43ff9e3bc244 189 }
chuanga 0:43ff9e3bc244 190 last = strrchr(_msg_buffer, '\'');
chuanga 0:43ff9e3bc244 191 if((last == NULL) || (last < (first+1))) {
chuanga 0:43ff9e3bc244 192 debug("\r\nSPWF> WARNING: might happen in case of RX buffer overflow! (%s, %d)\r\n", __func__, __LINE__);
chuanga 0:43ff9e3bc244 193 return false;
chuanga 0:43ff9e3bc244 194 }
chuanga 0:43ff9e3bc244 195 rest = strstr(last, "CAPS:");
chuanga 0:43ff9e3bc244 196 if(rest == NULL) {
chuanga 0:43ff9e3bc244 197 debug("\r\nSPWF> WARNING: might happen in case of RX buffer overflow! (%s, %d)\r\n", __func__, __LINE__);
chuanga 0:43ff9e3bc244 198 return false;
chuanga 0:43ff9e3bc244 199 }
chuanga 0:43ff9e3bc244 200
chuanga 0:43ff9e3bc244 201 /* substitute '\'' with '\0' */
chuanga 0:43ff9e3bc244 202 *last = '\0';
chuanga 0:43ff9e3bc244 203
chuanga 0:43ff9e3bc244 204 /* copy values */
chuanga 0:43ff9e3bc244 205 memcpy(&ap->ssid, first+1, sizeof(ap->ssid)-1);
chuanga 0:43ff9e3bc244 206 ap->ssid[sizeof(ap->ssid)-1] = '\0';
chuanga 0:43ff9e3bc244 207 ap->channel = channel;
chuanga 0:43ff9e3bc244 208
chuanga 0:43ff9e3bc244 209 /* skip `CAPS: 0421 ` */
chuanga 0:43ff9e3bc244 210 if(strlen(rest) < 11) {
chuanga 0:43ff9e3bc244 211 debug("\r\nSPWF> WARNING: might happen in case of RX buffer overflow! (%s, %d)\r\n", __func__, __LINE__);
chuanga 0:43ff9e3bc244 212 return false;
chuanga 0:43ff9e3bc244 213 }
chuanga 0:43ff9e3bc244 214 rest += 11;
chuanga 0:43ff9e3bc244 215
chuanga 0:43ff9e3bc244 216 /* get next character */
chuanga 0:43ff9e3bc244 217 value = *rest++;
chuanga 0:43ff9e3bc244 218 if(value != 'W') { // no security
chuanga 0:43ff9e3bc244 219 ap->security = NSAPI_SECURITY_NONE;
chuanga 0:43ff9e3bc244 220 return true;
chuanga 0:43ff9e3bc244 221 }
chuanga 0:43ff9e3bc244 222
chuanga 0:43ff9e3bc244 223 /* determine security */
chuanga 0:43ff9e3bc244 224 {
chuanga 0:43ff9e3bc244 225 char buffer[10];
chuanga 0:43ff9e3bc244 226
chuanga 0:43ff9e3bc244 227 if(!(sscanf(rest, "%s%*[\x20]", (char*)&buffer) > 0)) { // '\0x20' == <space>
chuanga 0:43ff9e3bc244 228 return true;
chuanga 0:43ff9e3bc244 229 } else if(strncmp("EP", buffer, 10) == 0) {
chuanga 0:43ff9e3bc244 230 ap->security = NSAPI_SECURITY_WEP;
chuanga 0:43ff9e3bc244 231 return true;
chuanga 0:43ff9e3bc244 232 } else if(strncmp("PA2", buffer, 10) == 0) {
chuanga 0:43ff9e3bc244 233 ap->security = NSAPI_SECURITY_WPA2;
chuanga 0:43ff9e3bc244 234 return true;
chuanga 0:43ff9e3bc244 235 } else if(strncmp("PA", buffer, 10) != 0) {
chuanga 0:43ff9e3bc244 236 return true;
chuanga 0:43ff9e3bc244 237 }
chuanga 0:43ff9e3bc244 238
chuanga 0:43ff9e3bc244 239 /* got a "WPA", check for "WPA2" */
chuanga 0:43ff9e3bc244 240 rest += strlen(buffer);
chuanga 0:43ff9e3bc244 241 value = *rest++;
chuanga 0:43ff9e3bc244 242 if(value == '\0') { // no further protocol
chuanga 0:43ff9e3bc244 243 ap->security = NSAPI_SECURITY_WPA;
chuanga 0:43ff9e3bc244 244 return true;
chuanga 0:43ff9e3bc244 245 } else { // assume "WPA2"
chuanga 0:43ff9e3bc244 246 ap->security = NSAPI_SECURITY_WPA_WPA2;
chuanga 0:43ff9e3bc244 247 return true;
chuanga 0:43ff9e3bc244 248 }
chuanga 0:43ff9e3bc244 249 }
chuanga 0:43ff9e3bc244 250 } else { // ret == false
chuanga 0:43ff9e3bc244 251 debug("\r\nSPWF> WARNING: might happen in case of RX buffer overflow! (%s, %d)\r\n", __func__, __LINE__);
chuanga 0:43ff9e3bc244 252 }
chuanga 0:43ff9e3bc244 253
chuanga 0:43ff9e3bc244 254 return ret;
chuanga 0:43ff9e3bc244 255 }
chuanga 0:43ff9e3bc244 256
chuanga 0:43ff9e3bc244 257 int SPWFSA01::scan(WiFiAccessPoint *res, unsigned limit)
chuanga 0:43ff9e3bc244 258 {
chuanga 0:43ff9e3bc244 259 unsigned cnt = 0;
chuanga 0:43ff9e3bc244 260 nsapi_wifi_ap_t ap;
chuanga 0:43ff9e3bc244 261
chuanga 0:43ff9e3bc244 262 if (!_parser.send("AT+S.SCAN=a,s")) {
chuanga 0:43ff9e3bc244 263 return NSAPI_ERROR_DEVICE_ERROR;
chuanga 0:43ff9e3bc244 264 }
chuanga 0:43ff9e3bc244 265
chuanga 0:43ff9e3bc244 266 while (_recv_ap(&ap)) {
chuanga 0:43ff9e3bc244 267 if (cnt < limit) {
chuanga 0:43ff9e3bc244 268 res[cnt] = WiFiAccessPoint(ap);
chuanga 0:43ff9e3bc244 269 }
chuanga 0:43ff9e3bc244 270
chuanga 0:43ff9e3bc244 271 cnt++;
chuanga 0:43ff9e3bc244 272 if (limit != 0 && cnt >= limit) {
chuanga 0:43ff9e3bc244 273 break;
chuanga 0:43ff9e3bc244 274 }
chuanga 0:43ff9e3bc244 275 }
chuanga 0:43ff9e3bc244 276
chuanga 0:43ff9e3bc244 277 if(!_recv_ok()) {
chuanga 0:43ff9e3bc244 278 empty_rx_buffer();
chuanga 0:43ff9e3bc244 279 }
chuanga 0:43ff9e3bc244 280
chuanga 0:43ff9e3bc244 281 return cnt;
chuanga 0:43ff9e3bc244 282 }
chuanga 0:43ff9e3bc244 283
chuanga 0:43ff9e3bc244 284 #endif // MBED_CONF_IDW0XX1_EXPANSION_BOARD