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 /* ISM43362 Example
chuanga 0:43ff9e3bc244 2 *
chuanga 0:43ff9e3bc244 3 * Copyright (c) STMicroelectronics 2018
chuanga 0:43ff9e3bc244 4 *
chuanga 0:43ff9e3bc244 5 * Licensed under the Apache License, Version 2.0 (the "License");
chuanga 0:43ff9e3bc244 6 * you may not use this file except in compliance with the License.
chuanga 0:43ff9e3bc244 7 * You may obtain a copy of the License at
chuanga 0:43ff9e3bc244 8 *
chuanga 0:43ff9e3bc244 9 * http://www.apache.org/licenses/LICENSE-2.0
chuanga 0:43ff9e3bc244 10 *
chuanga 0:43ff9e3bc244 11 * Unless required by applicable law or agreed to in writing, software
chuanga 0:43ff9e3bc244 12 * distributed under the License is distributed on an "AS IS" BASIS,
chuanga 0:43ff9e3bc244 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
chuanga 0:43ff9e3bc244 14 * See the License for the specific language governing permissions and
chuanga 0:43ff9e3bc244 15 * limitations under the License.
chuanga 0:43ff9e3bc244 16 */
chuanga 0:43ff9e3bc244 17 #include <string.h>
chuanga 0:43ff9e3bc244 18 #include "ISM43362.h"
chuanga 0:43ff9e3bc244 19 #include "mbed_debug.h"
chuanga 0:43ff9e3bc244 20
chuanga 0:43ff9e3bc244 21 // activate / de-activate debug
chuanga 0:43ff9e3bc244 22 #define ism_debug 0
chuanga 0:43ff9e3bc244 23
chuanga 0:43ff9e3bc244 24 ISM43362::ISM43362(PinName mosi, PinName miso, PinName sclk, PinName nss, PinName resetpin, PinName datareadypin, PinName wakeup, bool debug)
chuanga 0:43ff9e3bc244 25 : _bufferspi(mosi, miso, sclk, nss, datareadypin),
chuanga 0:43ff9e3bc244 26 _parser(_bufferspi),
chuanga 0:43ff9e3bc244 27 _resetpin(resetpin),
chuanga 0:43ff9e3bc244 28 _packets(0), _packets_end(&_packets)
chuanga 0:43ff9e3bc244 29 {
chuanga 0:43ff9e3bc244 30 DigitalOut wakeup_pin(wakeup);
chuanga 0:43ff9e3bc244 31 _bufferspi.format(16, 0); /* 16bits, ploarity low, phase 1Edge, master mode */
chuanga 0:43ff9e3bc244 32 _bufferspi.frequency(20000000); /* up to 20 MHz */
chuanga 0:43ff9e3bc244 33 _active_id = 0xFF;
chuanga 0:43ff9e3bc244 34 _FwVersionId = 0;
chuanga 0:43ff9e3bc244 35
chuanga 0:43ff9e3bc244 36 _ism_debug = debug || ism_debug;
chuanga 0:43ff9e3bc244 37 reset();
chuanga 0:43ff9e3bc244 38 }
chuanga 0:43ff9e3bc244 39
chuanga 0:43ff9e3bc244 40 /**
chuanga 0:43ff9e3bc244 41 * @brief Parses and returns number from string.
chuanga 0:43ff9e3bc244 42 * @param ptr: pointer to string
chuanga 0:43ff9e3bc244 43 * @param cnt: pointer to the number of parsed digit
chuanga 0:43ff9e3bc244 44 * @retval integer value.
chuanga 0:43ff9e3bc244 45 */
chuanga 0:43ff9e3bc244 46 #define CHARISHEXNUM(x) (((x) >= '0' && (x) <= '9') || \
chuanga 0:43ff9e3bc244 47 ((x) >= 'a' && (x) <= 'f') || \
chuanga 0:43ff9e3bc244 48 ((x) >= 'A' && (x) <= 'F'))
chuanga 0:43ff9e3bc244 49 #define CHARISNUM(x) ((x) >= '0' && (x) <= '9')
chuanga 0:43ff9e3bc244 50 #define CHAR2NUM(x) ((x) - '0')
chuanga 0:43ff9e3bc244 51
chuanga 0:43ff9e3bc244 52
chuanga 0:43ff9e3bc244 53 extern "C" int32_t ParseNumber(char *ptr, uint8_t *cnt)
chuanga 0:43ff9e3bc244 54 {
chuanga 0:43ff9e3bc244 55 uint8_t minus = 0, i = 0;
chuanga 0:43ff9e3bc244 56 int32_t sum = 0;
chuanga 0:43ff9e3bc244 57
chuanga 0:43ff9e3bc244 58 if (*ptr == '-') { /* Check for minus character */
chuanga 0:43ff9e3bc244 59 minus = 1;
chuanga 0:43ff9e3bc244 60 ptr++;
chuanga 0:43ff9e3bc244 61 i++;
chuanga 0:43ff9e3bc244 62 }
chuanga 0:43ff9e3bc244 63 if (*ptr == 'C') { /* input string from get_firmware_version is Cx.x.x.x */
chuanga 0:43ff9e3bc244 64 ptr++;
chuanga 0:43ff9e3bc244 65 }
chuanga 0:43ff9e3bc244 66
chuanga 0:43ff9e3bc244 67 while (CHARISNUM(*ptr) || (*ptr == '.')) { /* Parse number */
chuanga 0:43ff9e3bc244 68 if (*ptr == '.') {
chuanga 0:43ff9e3bc244 69 ptr++; // next char
chuanga 0:43ff9e3bc244 70 } else {
chuanga 0:43ff9e3bc244 71 sum = 10 * sum + CHAR2NUM(*ptr);
chuanga 0:43ff9e3bc244 72 ptr++;
chuanga 0:43ff9e3bc244 73 i++;
chuanga 0:43ff9e3bc244 74 }
chuanga 0:43ff9e3bc244 75 }
chuanga 0:43ff9e3bc244 76
chuanga 0:43ff9e3bc244 77 if (cnt != NULL) { /* Save number of characters used for number */
chuanga 0:43ff9e3bc244 78 *cnt = i;
chuanga 0:43ff9e3bc244 79 }
chuanga 0:43ff9e3bc244 80 if (minus) { /* Minus detected */
chuanga 0:43ff9e3bc244 81 return 0 - sum;
chuanga 0:43ff9e3bc244 82 }
chuanga 0:43ff9e3bc244 83 return sum; /* Return number */
chuanga 0:43ff9e3bc244 84 }
chuanga 0:43ff9e3bc244 85
chuanga 0:43ff9e3bc244 86 uint32_t ISM43362::get_firmware_version(void)
chuanga 0:43ff9e3bc244 87 {
chuanga 0:43ff9e3bc244 88 char tmp_buffer[250];
chuanga 0:43ff9e3bc244 89 char *ptr, *ptr2;
chuanga 0:43ff9e3bc244 90 char _fw_version[16];
chuanga 0:43ff9e3bc244 91
chuanga 0:43ff9e3bc244 92 /* Use %[^\n] instead of %s to allow having spaces in the string */
chuanga 0:43ff9e3bc244 93 if (!(_parser.send("I?") && _parser.recv("%[^\n^\r]\r\n", tmp_buffer) && check_response())) {
chuanga 0:43ff9e3bc244 94 debug_if(_ism_debug, "ISM43362: get_firmware_version is FAIL\r\n");
chuanga 0:43ff9e3bc244 95 return 0;
chuanga 0:43ff9e3bc244 96 }
chuanga 0:43ff9e3bc244 97 debug_if(_ism_debug, "ISM43362: get_firmware_version = %s\r\n", tmp_buffer);
chuanga 0:43ff9e3bc244 98
chuanga 0:43ff9e3bc244 99 // Get the first version in the string
chuanga 0:43ff9e3bc244 100 ptr = strtok((char *)tmp_buffer, ",");
chuanga 0:43ff9e3bc244 101 ptr = strtok(NULL, ",");
chuanga 0:43ff9e3bc244 102 ptr2 = strtok(NULL, ",");
chuanga 0:43ff9e3bc244 103 if (ptr == NULL) {
chuanga 0:43ff9e3bc244 104 debug_if(_ism_debug, "ISM43362: get_firmware_version decoding is FAIL\r\n");
chuanga 0:43ff9e3bc244 105 return 0;
chuanga 0:43ff9e3bc244 106 }
chuanga 0:43ff9e3bc244 107 strncpy(_fw_version, ptr, ptr2 - ptr);
chuanga 0:43ff9e3bc244 108 _FwVersionId = ParseNumber(_fw_version, NULL);
chuanga 0:43ff9e3bc244 109
chuanga 0:43ff9e3bc244 110 return _FwVersionId;
chuanga 0:43ff9e3bc244 111 }
chuanga 0:43ff9e3bc244 112
chuanga 0:43ff9e3bc244 113 bool ISM43362::reset(void)
chuanga 0:43ff9e3bc244 114 {
chuanga 0:43ff9e3bc244 115 char tmp_buffer[100];
chuanga 0:43ff9e3bc244 116 debug_if(_ism_debug, "ISM43362: Reset Module\r\n");
chuanga 0:43ff9e3bc244 117 _resetpin = 0;
chuanga 0:43ff9e3bc244 118 wait_ms(10);
chuanga 0:43ff9e3bc244 119 _resetpin = 1;
chuanga 0:43ff9e3bc244 120 wait_ms(500);
chuanga 0:43ff9e3bc244 121
chuanga 0:43ff9e3bc244 122 /* Wait for prompt line : the string is "> ". */
chuanga 0:43ff9e3bc244 123 /* As the space char is not detected by sscanf function in parser.recv, */
chuanga 0:43ff9e3bc244 124 /* we need to use %[\n] */
chuanga 0:43ff9e3bc244 125 if (!_parser.recv(">%[^\n]", tmp_buffer)) {
chuanga 0:43ff9e3bc244 126 debug_if(_ism_debug, "ISM43362: Reset Module failed\r\n");
chuanga 0:43ff9e3bc244 127 return false;
chuanga 0:43ff9e3bc244 128 }
chuanga 0:43ff9e3bc244 129 return true;
chuanga 0:43ff9e3bc244 130 }
chuanga 0:43ff9e3bc244 131
chuanga 0:43ff9e3bc244 132 void ISM43362::print_rx_buff(void)
chuanga 0:43ff9e3bc244 133 {
chuanga 0:43ff9e3bc244 134 char tmp[150] = {0};
chuanga 0:43ff9e3bc244 135 uint16_t i = 0;
chuanga 0:43ff9e3bc244 136 debug_if(_ism_debug, "ISM43362: ");
chuanga 0:43ff9e3bc244 137 while (i < 150) {
chuanga 0:43ff9e3bc244 138 int c = _parser.getc();
chuanga 0:43ff9e3bc244 139 if (c < 0) {
chuanga 0:43ff9e3bc244 140 break;
chuanga 0:43ff9e3bc244 141 }
chuanga 0:43ff9e3bc244 142 tmp[i] = c;
chuanga 0:43ff9e3bc244 143 debug_if(_ism_debug, "0x%2X ", c);
chuanga 0:43ff9e3bc244 144 i++;
chuanga 0:43ff9e3bc244 145 }
chuanga 0:43ff9e3bc244 146 debug_if(_ism_debug, "\n");
chuanga 0:43ff9e3bc244 147 debug_if(_ism_debug, "ISM43362: Buffer content =====%s=====\r\n", tmp);
chuanga 0:43ff9e3bc244 148 }
chuanga 0:43ff9e3bc244 149
chuanga 0:43ff9e3bc244 150 /* checks the standard OK response of the WIFI module, shouldbe:
chuanga 0:43ff9e3bc244 151 * \r\nDATA\r\nOK\r\n>sp
chuanga 0:43ff9e3bc244 152 * or
chuanga 0:43ff9e3bc244 153 * \r\nERROR\r\nUSAGE\r\n>sp
chuanga 0:43ff9e3bc244 154 * function returns true if OK, false otherwise. In case of error,
chuanga 0:43ff9e3bc244 155 * print error content then flush buffer */
chuanga 0:43ff9e3bc244 156 bool ISM43362::check_response(void)
chuanga 0:43ff9e3bc244 157 {
chuanga 0:43ff9e3bc244 158 char tmp_buffer[100];
chuanga 0:43ff9e3bc244 159 if (!_parser.recv("OK\r\n")) {
chuanga 0:43ff9e3bc244 160 print_rx_buff();
chuanga 0:43ff9e3bc244 161 _parser.flush();
chuanga 0:43ff9e3bc244 162 return false;
chuanga 0:43ff9e3bc244 163 }
chuanga 0:43ff9e3bc244 164
chuanga 0:43ff9e3bc244 165 /* Then we should get the prompt: "> " */
chuanga 0:43ff9e3bc244 166 /* As the space char is not detected by sscanf function in parser.recv, */
chuanga 0:43ff9e3bc244 167 /* we need to use %[\n] */
chuanga 0:43ff9e3bc244 168 if (!_parser.recv(">%[^\n]", tmp_buffer)) {
chuanga 0:43ff9e3bc244 169 debug_if(_ism_debug, "ISM43362: Missing prompt in WIFI resp\r\n");
chuanga 0:43ff9e3bc244 170 print_rx_buff();
chuanga 0:43ff9e3bc244 171 _parser.flush();
chuanga 0:43ff9e3bc244 172 return false;
chuanga 0:43ff9e3bc244 173 }
chuanga 0:43ff9e3bc244 174
chuanga 0:43ff9e3bc244 175 /* Inventek module do stuffing / padding of data with 0x15,
chuanga 0:43ff9e3bc244 176 * in case buffer contains such */
chuanga 0:43ff9e3bc244 177 while (1) {
chuanga 0:43ff9e3bc244 178 int c = _parser.getc();
chuanga 0:43ff9e3bc244 179 if (c == 0x15) {
chuanga 0:43ff9e3bc244 180 // debug_if(_ism_debug, "ISM43362: Flush char 0x%x\n", c);
chuanga 0:43ff9e3bc244 181 continue;
chuanga 0:43ff9e3bc244 182 } else {
chuanga 0:43ff9e3bc244 183 /* How to put it back if needed ? */
chuanga 0:43ff9e3bc244 184 break;
chuanga 0:43ff9e3bc244 185 }
chuanga 0:43ff9e3bc244 186 }
chuanga 0:43ff9e3bc244 187 return true;
chuanga 0:43ff9e3bc244 188 }
chuanga 0:43ff9e3bc244 189
chuanga 0:43ff9e3bc244 190 bool ISM43362::dhcp(bool enabled)
chuanga 0:43ff9e3bc244 191 {
chuanga 0:43ff9e3bc244 192 return (_parser.send("C4=%d", enabled ? 1 : 0) && check_response());
chuanga 0:43ff9e3bc244 193 }
chuanga 0:43ff9e3bc244 194
chuanga 0:43ff9e3bc244 195 int ISM43362::connect(const char *ap, const char *passPhrase, ism_security_t ap_sec)
chuanga 0:43ff9e3bc244 196 {
chuanga 0:43ff9e3bc244 197 char tmp[256];
chuanga 0:43ff9e3bc244 198
chuanga 0:43ff9e3bc244 199 if (!(_parser.send("C1=%s", ap) && check_response())) {
chuanga 0:43ff9e3bc244 200 return NSAPI_ERROR_PARAMETER;
chuanga 0:43ff9e3bc244 201 }
chuanga 0:43ff9e3bc244 202
chuanga 0:43ff9e3bc244 203 if (!(_parser.send("C2=%s", passPhrase) && check_response())) {
chuanga 0:43ff9e3bc244 204 return NSAPI_ERROR_PARAMETER;
chuanga 0:43ff9e3bc244 205 }
chuanga 0:43ff9e3bc244 206
chuanga 0:43ff9e3bc244 207 /* Check security level is acceptable */
chuanga 0:43ff9e3bc244 208 if (ap_sec > ISM_SECURITY_WPA_WPA2) {
chuanga 0:43ff9e3bc244 209 debug_if(_ism_debug, "ISM43362: Unsupported security level %d\n", ap_sec);
chuanga 0:43ff9e3bc244 210 return NSAPI_ERROR_UNSUPPORTED;
chuanga 0:43ff9e3bc244 211 }
chuanga 0:43ff9e3bc244 212
chuanga 0:43ff9e3bc244 213 if (!(_parser.send("C3=%d", ap_sec) && check_response())) {
chuanga 0:43ff9e3bc244 214 return NSAPI_ERROR_PARAMETER;
chuanga 0:43ff9e3bc244 215 }
chuanga 0:43ff9e3bc244 216
chuanga 0:43ff9e3bc244 217 if (_parser.send("C0")) {
chuanga 0:43ff9e3bc244 218 while (_parser.recv("%[^\n]\n", tmp)) {
chuanga 0:43ff9e3bc244 219 if (strstr(tmp, "OK")) {
chuanga 0:43ff9e3bc244 220 _parser.flush();
chuanga 0:43ff9e3bc244 221 _conn_status = NSAPI_STATUS_GLOBAL_UP;
chuanga 0:43ff9e3bc244 222 _conn_stat_cb();
chuanga 0:43ff9e3bc244 223 return NSAPI_ERROR_OK;
chuanga 0:43ff9e3bc244 224 }
chuanga 0:43ff9e3bc244 225 if (strstr(tmp, "JOIN")) {
chuanga 0:43ff9e3bc244 226 _conn_status = NSAPI_STATUS_CONNECTING;
chuanga 0:43ff9e3bc244 227 _conn_stat_cb();
chuanga 0:43ff9e3bc244 228 if (strstr(tmp, "Failed")) {
chuanga 0:43ff9e3bc244 229 _parser.flush();
chuanga 0:43ff9e3bc244 230 return NSAPI_ERROR_AUTH_FAILURE;
chuanga 0:43ff9e3bc244 231 }
chuanga 0:43ff9e3bc244 232 }
chuanga 0:43ff9e3bc244 233 }
chuanga 0:43ff9e3bc244 234 }
chuanga 0:43ff9e3bc244 235
chuanga 0:43ff9e3bc244 236 return NSAPI_ERROR_NO_CONNECTION;
chuanga 0:43ff9e3bc244 237 }
chuanga 0:43ff9e3bc244 238
chuanga 0:43ff9e3bc244 239 bool ISM43362::disconnect(void)
chuanga 0:43ff9e3bc244 240 {
chuanga 0:43ff9e3bc244 241 _conn_status = NSAPI_STATUS_DISCONNECTED;
chuanga 0:43ff9e3bc244 242 _conn_stat_cb();
chuanga 0:43ff9e3bc244 243 return (_parser.send("CD") && check_response());
chuanga 0:43ff9e3bc244 244 }
chuanga 0:43ff9e3bc244 245
chuanga 0:43ff9e3bc244 246 const char *ISM43362::getIPAddress(void)
chuanga 0:43ff9e3bc244 247 {
chuanga 0:43ff9e3bc244 248 char tmp_ip_buffer[250];
chuanga 0:43ff9e3bc244 249 char *ptr, *ptr2;
chuanga 0:43ff9e3bc244 250
chuanga 0:43ff9e3bc244 251 /* Use %[^\n] instead of %s to allow having spaces in the string */
chuanga 0:43ff9e3bc244 252 if (!(_parser.send("C?")
chuanga 0:43ff9e3bc244 253 && _parser.recv("%[^\n^\r]\r\n", tmp_ip_buffer)
chuanga 0:43ff9e3bc244 254 && check_response())) {
chuanga 0:43ff9e3bc244 255 debug_if(_ism_debug, "ISM43362: getIPAddress LINE KO: %s\n", tmp_ip_buffer);
chuanga 0:43ff9e3bc244 256 return 0;
chuanga 0:43ff9e3bc244 257 }
chuanga 0:43ff9e3bc244 258
chuanga 0:43ff9e3bc244 259 /* Get the IP address in the result */
chuanga 0:43ff9e3bc244 260 /* TODO : check if the begining of the string is always = "eS-WiFi_AP_C47F51011231," */
chuanga 0:43ff9e3bc244 261 ptr = strtok((char *)tmp_ip_buffer, ",");
chuanga 0:43ff9e3bc244 262 ptr = strtok(NULL, ",");
chuanga 0:43ff9e3bc244 263 ptr = strtok(NULL, ",");
chuanga 0:43ff9e3bc244 264 ptr = strtok(NULL, ",");
chuanga 0:43ff9e3bc244 265 ptr = strtok(NULL, ",");
chuanga 0:43ff9e3bc244 266 ptr = strtok(NULL, ",");
chuanga 0:43ff9e3bc244 267 ptr2 = strtok(NULL, ",");
chuanga 0:43ff9e3bc244 268 if (ptr == NULL) {
chuanga 0:43ff9e3bc244 269 return 0;
chuanga 0:43ff9e3bc244 270 }
chuanga 0:43ff9e3bc244 271 strncpy(_ip_buffer, ptr, ptr2 - ptr);
chuanga 0:43ff9e3bc244 272
chuanga 0:43ff9e3bc244 273 tmp_ip_buffer[59] = 0;
chuanga 0:43ff9e3bc244 274 debug_if(_ism_debug, "ISM43362: receivedIPAddress: %s\n", _ip_buffer);
chuanga 0:43ff9e3bc244 275
chuanga 0:43ff9e3bc244 276 return _ip_buffer;
chuanga 0:43ff9e3bc244 277 }
chuanga 0:43ff9e3bc244 278
chuanga 0:43ff9e3bc244 279 const char *ISM43362::getMACAddress(void)
chuanga 0:43ff9e3bc244 280 {
chuanga 0:43ff9e3bc244 281 if (!(_parser.send("Z5") && _parser.recv("%s\r\n", _mac_buffer) && check_response())) {
chuanga 0:43ff9e3bc244 282 debug_if(_ism_debug, "ISM43362: receivedMacAddress LINE KO: %s\n", _mac_buffer);
chuanga 0:43ff9e3bc244 283 return 0;
chuanga 0:43ff9e3bc244 284 }
chuanga 0:43ff9e3bc244 285
chuanga 0:43ff9e3bc244 286 debug_if(_ism_debug, "ISM43362: receivedMacAddress:%s, size=%d\r\n", _mac_buffer, sizeof(_mac_buffer));
chuanga 0:43ff9e3bc244 287
chuanga 0:43ff9e3bc244 288 return _mac_buffer;
chuanga 0:43ff9e3bc244 289 }
chuanga 0:43ff9e3bc244 290
chuanga 0:43ff9e3bc244 291 const char *ISM43362::getGateway()
chuanga 0:43ff9e3bc244 292 {
chuanga 0:43ff9e3bc244 293 char tmp[250];
chuanga 0:43ff9e3bc244 294 /* Use %[^\n] instead of %s to allow having spaces in the string */
chuanga 0:43ff9e3bc244 295 if (!(_parser.send("C?") && _parser.recv("%[^\n^\r]\r\n", tmp) && check_response())) {
chuanga 0:43ff9e3bc244 296 debug_if(_ism_debug, "ISM43362: getGateway LINE KO: %s\r\n", tmp);
chuanga 0:43ff9e3bc244 297 return 0;
chuanga 0:43ff9e3bc244 298 }
chuanga 0:43ff9e3bc244 299
chuanga 0:43ff9e3bc244 300 /* Extract the Gateway in the received buffer */
chuanga 0:43ff9e3bc244 301 char *ptr;
chuanga 0:43ff9e3bc244 302 ptr = strtok(tmp, ",");
chuanga 0:43ff9e3bc244 303 for (int i = 0; i < 7; i++) {
chuanga 0:43ff9e3bc244 304 if (ptr == NULL) {
chuanga 0:43ff9e3bc244 305 break;
chuanga 0:43ff9e3bc244 306 }
chuanga 0:43ff9e3bc244 307 ptr = strtok(NULL, ",");
chuanga 0:43ff9e3bc244 308 }
chuanga 0:43ff9e3bc244 309
chuanga 0:43ff9e3bc244 310 strncpy(_gateway_buffer, ptr, sizeof(_gateway_buffer));
chuanga 0:43ff9e3bc244 311
chuanga 0:43ff9e3bc244 312 debug_if(_ism_debug, "ISM43362: getGateway: %s\r\n", _gateway_buffer);
chuanga 0:43ff9e3bc244 313
chuanga 0:43ff9e3bc244 314 return _gateway_buffer;
chuanga 0:43ff9e3bc244 315 }
chuanga 0:43ff9e3bc244 316
chuanga 0:43ff9e3bc244 317 const char *ISM43362::getNetmask()
chuanga 0:43ff9e3bc244 318 {
chuanga 0:43ff9e3bc244 319 char tmp[250];
chuanga 0:43ff9e3bc244 320 /* Use %[^\n] instead of %s to allow having spaces in the string */
chuanga 0:43ff9e3bc244 321 if (!(_parser.send("C?") && _parser.recv("%[^\n^\r]\r\n", tmp) && check_response())) {
chuanga 0:43ff9e3bc244 322 debug_if(_ism_debug, "ISM43362: getNetmask LINE KO: %s\n", tmp);
chuanga 0:43ff9e3bc244 323 return 0;
chuanga 0:43ff9e3bc244 324 }
chuanga 0:43ff9e3bc244 325
chuanga 0:43ff9e3bc244 326 /* Extract Netmask in the received buffer */
chuanga 0:43ff9e3bc244 327 char *ptr;
chuanga 0:43ff9e3bc244 328 ptr = strtok(tmp, ",");
chuanga 0:43ff9e3bc244 329 for (int i = 0; i < 6; i++) {
chuanga 0:43ff9e3bc244 330 if (ptr == NULL) {
chuanga 0:43ff9e3bc244 331 break;
chuanga 0:43ff9e3bc244 332 }
chuanga 0:43ff9e3bc244 333 ptr = strtok(NULL, ",");
chuanga 0:43ff9e3bc244 334 }
chuanga 0:43ff9e3bc244 335
chuanga 0:43ff9e3bc244 336 strncpy(_netmask_buffer, ptr, sizeof(_netmask_buffer));
chuanga 0:43ff9e3bc244 337
chuanga 0:43ff9e3bc244 338 debug_if(_ism_debug, "ISM43362: getNetmask: %s\r\n", _netmask_buffer);
chuanga 0:43ff9e3bc244 339
chuanga 0:43ff9e3bc244 340 return _netmask_buffer;
chuanga 0:43ff9e3bc244 341 }
chuanga 0:43ff9e3bc244 342
chuanga 0:43ff9e3bc244 343 int8_t ISM43362::getRSSI()
chuanga 0:43ff9e3bc244 344 {
chuanga 0:43ff9e3bc244 345 int8_t rssi;
chuanga 0:43ff9e3bc244 346 char tmp[25];
chuanga 0:43ff9e3bc244 347
chuanga 0:43ff9e3bc244 348 if (!(_parser.send("CR") && _parser.recv("%s\r\n", tmp) && check_response())) {
chuanga 0:43ff9e3bc244 349 debug_if(_ism_debug, "ISM43362: getRSSI LINE KO: %s\r\n", tmp);
chuanga 0:43ff9e3bc244 350 return 0;
chuanga 0:43ff9e3bc244 351 }
chuanga 0:43ff9e3bc244 352
chuanga 0:43ff9e3bc244 353 rssi = ParseNumber(tmp, NULL);
chuanga 0:43ff9e3bc244 354
chuanga 0:43ff9e3bc244 355 debug_if(_ism_debug, "ISM43362: getRSSI: %d\r\n", rssi);
chuanga 0:43ff9e3bc244 356
chuanga 0:43ff9e3bc244 357 return rssi;
chuanga 0:43ff9e3bc244 358 }
chuanga 0:43ff9e3bc244 359 /**
chuanga 0:43ff9e3bc244 360 * @brief Parses Security type.
chuanga 0:43ff9e3bc244 361 * @param ptr: pointer to string
chuanga 0:43ff9e3bc244 362 * @retval Encryption type.
chuanga 0:43ff9e3bc244 363 */
chuanga 0:43ff9e3bc244 364 extern "C" nsapi_security_t ParseSecurity(char *ptr)
chuanga 0:43ff9e3bc244 365 {
chuanga 0:43ff9e3bc244 366 if (strstr(ptr, "Open")) {
chuanga 0:43ff9e3bc244 367 return NSAPI_SECURITY_NONE;
chuanga 0:43ff9e3bc244 368 } else if (strstr(ptr, "WEP")) {
chuanga 0:43ff9e3bc244 369 return NSAPI_SECURITY_WEP;
chuanga 0:43ff9e3bc244 370 } else if (strstr(ptr, "WPA2 AES")) {
chuanga 0:43ff9e3bc244 371 return NSAPI_SECURITY_WPA2;
chuanga 0:43ff9e3bc244 372 } else if (strstr(ptr, "WPA WPA2")) {
chuanga 0:43ff9e3bc244 373 return NSAPI_SECURITY_WPA_WPA2;
chuanga 0:43ff9e3bc244 374 } else if (strstr(ptr, "WPA2 TKIP")) {
chuanga 0:43ff9e3bc244 375 return NSAPI_SECURITY_UNKNOWN; // no match in mbed
chuanga 0:43ff9e3bc244 376 } else if (strstr(ptr, "WPA2")) {
chuanga 0:43ff9e3bc244 377 return NSAPI_SECURITY_WPA2; // catch any other WPA2 formula
chuanga 0:43ff9e3bc244 378 } else if (strstr(ptr, "WPA")) {
chuanga 0:43ff9e3bc244 379 return NSAPI_SECURITY_WPA;
chuanga 0:43ff9e3bc244 380 } else {
chuanga 0:43ff9e3bc244 381 return NSAPI_SECURITY_UNKNOWN;
chuanga 0:43ff9e3bc244 382 }
chuanga 0:43ff9e3bc244 383 }
chuanga 0:43ff9e3bc244 384
chuanga 0:43ff9e3bc244 385 /**
chuanga 0:43ff9e3bc244 386 * @brief Convert char in Hex format to integer.
chuanga 0:43ff9e3bc244 387 * @param a: character to convert
chuanga 0:43ff9e3bc244 388 * @retval integer value.
chuanga 0:43ff9e3bc244 389 */
chuanga 0:43ff9e3bc244 390 extern "C" uint8_t Hex2Num(char a)
chuanga 0:43ff9e3bc244 391 {
chuanga 0:43ff9e3bc244 392 if (a >= '0' && a <= '9') { /* Char is num */
chuanga 0:43ff9e3bc244 393 return a - '0';
chuanga 0:43ff9e3bc244 394 } else if (a >= 'a' && a <= 'f') { /* Char is lowercase character A - Z (hex) */
chuanga 0:43ff9e3bc244 395 return (a - 'a') + 10;
chuanga 0:43ff9e3bc244 396 } else if (a >= 'A' && a <= 'F') { /* Char is uppercase character A - Z (hex) */
chuanga 0:43ff9e3bc244 397 return (a - 'A') + 10;
chuanga 0:43ff9e3bc244 398 }
chuanga 0:43ff9e3bc244 399
chuanga 0:43ff9e3bc244 400 return 0;
chuanga 0:43ff9e3bc244 401 }
chuanga 0:43ff9e3bc244 402
chuanga 0:43ff9e3bc244 403 /**
chuanga 0:43ff9e3bc244 404 * @brief Extract a hex number from a string.
chuanga 0:43ff9e3bc244 405 * @param ptr: pointer to string
chuanga 0:43ff9e3bc244 406 * @param cnt: pointer to the number of parsed digit
chuanga 0:43ff9e3bc244 407 * @retval Hex value.
chuanga 0:43ff9e3bc244 408 */
chuanga 0:43ff9e3bc244 409 extern "C" uint32_t ParseHexNumber(char *ptr, uint8_t *cnt)
chuanga 0:43ff9e3bc244 410 {
chuanga 0:43ff9e3bc244 411 uint32_t sum = 0;
chuanga 0:43ff9e3bc244 412 uint8_t i = 0;
chuanga 0:43ff9e3bc244 413
chuanga 0:43ff9e3bc244 414 while (CHARISHEXNUM(*ptr)) { /* Parse number */
chuanga 0:43ff9e3bc244 415 sum <<= 4;
chuanga 0:43ff9e3bc244 416 sum += Hex2Num(*ptr);
chuanga 0:43ff9e3bc244 417 ptr++;
chuanga 0:43ff9e3bc244 418 i++;
chuanga 0:43ff9e3bc244 419 }
chuanga 0:43ff9e3bc244 420
chuanga 0:43ff9e3bc244 421 if (cnt != NULL) { /* Save number of characters used for number */
chuanga 0:43ff9e3bc244 422 *cnt = i;
chuanga 0:43ff9e3bc244 423 }
chuanga 0:43ff9e3bc244 424 return sum; /* Return number */
chuanga 0:43ff9e3bc244 425 }
chuanga 0:43ff9e3bc244 426
chuanga 0:43ff9e3bc244 427 bool ISM43362::isConnected(void)
chuanga 0:43ff9e3bc244 428 {
chuanga 0:43ff9e3bc244 429 return getIPAddress() != 0;
chuanga 0:43ff9e3bc244 430 }
chuanga 0:43ff9e3bc244 431
chuanga 0:43ff9e3bc244 432 int ISM43362::scan(WiFiAccessPoint *res, unsigned limit)
chuanga 0:43ff9e3bc244 433 {
chuanga 0:43ff9e3bc244 434 unsigned cnt = 0, num = 0;
chuanga 0:43ff9e3bc244 435 char *ptr;
chuanga 0:43ff9e3bc244 436 char tmp[256];
chuanga 0:43ff9e3bc244 437
chuanga 0:43ff9e3bc244 438 if (!(_parser.send("F0"))) {
chuanga 0:43ff9e3bc244 439 debug_if(_ism_debug, "ISM43362: scan error\r\n");
chuanga 0:43ff9e3bc244 440 return 0;
chuanga 0:43ff9e3bc244 441 }
chuanga 0:43ff9e3bc244 442
chuanga 0:43ff9e3bc244 443 /* Parse the received buffer and fill AP buffer */
chuanga 0:43ff9e3bc244 444 /* Use %[^\n] instead of %s to allow having spaces in the string */
chuanga 0:43ff9e3bc244 445 while (_parser.recv("#%[^\n]\n", tmp)) {
chuanga 0:43ff9e3bc244 446 if (limit != 0 && cnt >= limit) {
chuanga 0:43ff9e3bc244 447 /* reached end */
chuanga 0:43ff9e3bc244 448 break;
chuanga 0:43ff9e3bc244 449 }
chuanga 0:43ff9e3bc244 450 nsapi_wifi_ap_t ap = {0};
chuanga 0:43ff9e3bc244 451 debug_if(_ism_debug, "ISM43362: received:%s\n", tmp);
chuanga 0:43ff9e3bc244 452 ptr = strtok(tmp, ",");
chuanga 0:43ff9e3bc244 453 num = 0;
chuanga 0:43ff9e3bc244 454 while (ptr != NULL) {
chuanga 0:43ff9e3bc244 455 switch (num++) {
chuanga 0:43ff9e3bc244 456 case 0: /* Ignore index */
chuanga 0:43ff9e3bc244 457 case 4: /* Ignore Max Rate */
chuanga 0:43ff9e3bc244 458 case 5: /* Ignore Network Type */
chuanga 0:43ff9e3bc244 459 case 7: /* Ignore Radio Band */
chuanga 0:43ff9e3bc244 460 break;
chuanga 0:43ff9e3bc244 461 case 1:
chuanga 0:43ff9e3bc244 462 ptr[strlen(ptr) - 1] = 0;
chuanga 0:43ff9e3bc244 463 strncpy((char *)ap.ssid, ptr + 1, 32);
chuanga 0:43ff9e3bc244 464 break;
chuanga 0:43ff9e3bc244 465 case 2:
chuanga 0:43ff9e3bc244 466 for (int i = 0; i < 6; i++) {
chuanga 0:43ff9e3bc244 467 ap.bssid[i] = ParseHexNumber(ptr + (i * 3), NULL);
chuanga 0:43ff9e3bc244 468 }
chuanga 0:43ff9e3bc244 469 break;
chuanga 0:43ff9e3bc244 470 case 3:
chuanga 0:43ff9e3bc244 471 ap.rssi = ParseNumber(ptr, NULL);
chuanga 0:43ff9e3bc244 472 break;
chuanga 0:43ff9e3bc244 473 case 6:
chuanga 0:43ff9e3bc244 474 ap.security = ParseSecurity(ptr);
chuanga 0:43ff9e3bc244 475 break;
chuanga 0:43ff9e3bc244 476 case 8:
chuanga 0:43ff9e3bc244 477 ap.channel = ParseNumber(ptr, NULL);
chuanga 0:43ff9e3bc244 478 num = 1;
chuanga 0:43ff9e3bc244 479 break;
chuanga 0:43ff9e3bc244 480 default:
chuanga 0:43ff9e3bc244 481 break;
chuanga 0:43ff9e3bc244 482 }
chuanga 0:43ff9e3bc244 483 ptr = strtok(NULL, ",");
chuanga 0:43ff9e3bc244 484 }
chuanga 0:43ff9e3bc244 485 if (res != NULL) {
chuanga 0:43ff9e3bc244 486 res[cnt] = WiFiAccessPoint(ap);
chuanga 0:43ff9e3bc244 487 }
chuanga 0:43ff9e3bc244 488 cnt++;
chuanga 0:43ff9e3bc244 489 }
chuanga 0:43ff9e3bc244 490
chuanga 0:43ff9e3bc244 491 /* We may stop before having read all the APs list, so flush the rest of
chuanga 0:43ff9e3bc244 492 * it as well as OK commands */
chuanga 0:43ff9e3bc244 493 _parser.flush();
chuanga 0:43ff9e3bc244 494
chuanga 0:43ff9e3bc244 495 debug_if(_ism_debug, "ISM43362: End of Scan: cnt=%d\n", cnt);
chuanga 0:43ff9e3bc244 496
chuanga 0:43ff9e3bc244 497 return cnt;
chuanga 0:43ff9e3bc244 498
chuanga 0:43ff9e3bc244 499 }
chuanga 0:43ff9e3bc244 500
chuanga 0:43ff9e3bc244 501 bool ISM43362::open(const char *type, int id, const char *addr, int port)
chuanga 0:43ff9e3bc244 502 {
chuanga 0:43ff9e3bc244 503 /* TODO : This is the implementation for the client socket, need to check if need to create openserver too */
chuanga 0:43ff9e3bc244 504 //IDs only 0-3
chuanga 0:43ff9e3bc244 505 if ((id < 0) || (id > 3)) {
chuanga 0:43ff9e3bc244 506 debug_if(_ism_debug, "ISM43362: open: wrong id\n");
chuanga 0:43ff9e3bc244 507 return false;
chuanga 0:43ff9e3bc244 508 }
chuanga 0:43ff9e3bc244 509 /* Set communication socket */
chuanga 0:43ff9e3bc244 510 _active_id = id;
chuanga 0:43ff9e3bc244 511 if (!(_parser.send("P0=%d", id) && check_response())) {
chuanga 0:43ff9e3bc244 512 debug_if(_ism_debug, "ISM43362: open: P0 issue\n");
chuanga 0:43ff9e3bc244 513 return false;
chuanga 0:43ff9e3bc244 514 }
chuanga 0:43ff9e3bc244 515 /* Set protocol */
chuanga 0:43ff9e3bc244 516 if (!(_parser.send("P1=%s", type) && check_response())) {
chuanga 0:43ff9e3bc244 517 debug_if(_ism_debug, "ISM43362: open: P1 issue\n");
chuanga 0:43ff9e3bc244 518 return false;
chuanga 0:43ff9e3bc244 519 }
chuanga 0:43ff9e3bc244 520 /* Set address */
chuanga 0:43ff9e3bc244 521 if (!(_parser.send("P3=%s", addr) && check_response())) {
chuanga 0:43ff9e3bc244 522 debug_if(_ism_debug, "ISM43362: open: P3 issue\n");
chuanga 0:43ff9e3bc244 523 return false;
chuanga 0:43ff9e3bc244 524 }
chuanga 0:43ff9e3bc244 525 if (!(_parser.send("P4=%d", port) && check_response())) {
chuanga 0:43ff9e3bc244 526 debug_if(_ism_debug, "ISM43362: open: P4 issue\n");
chuanga 0:43ff9e3bc244 527 return false;
chuanga 0:43ff9e3bc244 528 }
chuanga 0:43ff9e3bc244 529 /* Start client */
chuanga 0:43ff9e3bc244 530 if (!(_parser.send("P6=1") && check_response())) {
chuanga 0:43ff9e3bc244 531 debug_if(_ism_debug, "ISM43362: open: P6 issue\n");
chuanga 0:43ff9e3bc244 532 return false;
chuanga 0:43ff9e3bc244 533 }
chuanga 0:43ff9e3bc244 534
chuanga 0:43ff9e3bc244 535 /* request as much data as possible - i.e. module max size */
chuanga 0:43ff9e3bc244 536 if (!(_parser.send("R1=%d", ES_WIFI_MAX_RX_PACKET_SIZE) && check_response())) {
chuanga 0:43ff9e3bc244 537 debug_if(_ism_debug, "ISM43362: open: R1 issue\n");
chuanga 0:43ff9e3bc244 538 return -1;
chuanga 0:43ff9e3bc244 539 }
chuanga 0:43ff9e3bc244 540
chuanga 0:43ff9e3bc244 541 /* Non blocking mode : set Read Transport Timeout to 1ms */
chuanga 0:43ff9e3bc244 542 if (!(_parser.send("R2=1") && check_response())) {
chuanga 0:43ff9e3bc244 543 debug_if(_ism_debug, "ISM43362: open: R2 issue\n");
chuanga 0:43ff9e3bc244 544 return -1;
chuanga 0:43ff9e3bc244 545 }
chuanga 0:43ff9e3bc244 546
chuanga 0:43ff9e3bc244 547 debug_if(_ism_debug, "ISM43362: open ok with id %d type %s addr %s port %d\n", id, type, addr, port);
chuanga 0:43ff9e3bc244 548
chuanga 0:43ff9e3bc244 549 return true;
chuanga 0:43ff9e3bc244 550 }
chuanga 0:43ff9e3bc244 551
chuanga 0:43ff9e3bc244 552 bool ISM43362::dns_lookup(const char *name, char *ip)
chuanga 0:43ff9e3bc244 553 {
chuanga 0:43ff9e3bc244 554 char tmp[30];
chuanga 0:43ff9e3bc244 555
chuanga 0:43ff9e3bc244 556 if (!(_parser.send("D0=%s", name) && _parser.recv("%s\r\n", tmp)
chuanga 0:43ff9e3bc244 557 && check_response())) {
chuanga 0:43ff9e3bc244 558 debug_if(_ism_debug, "ISM43362 dns_lookup: D0 issue: %s\n", tmp);
chuanga 0:43ff9e3bc244 559 return 0;
chuanga 0:43ff9e3bc244 560 }
chuanga 0:43ff9e3bc244 561
chuanga 0:43ff9e3bc244 562 strncpy(ip, tmp, sizeof(tmp));
chuanga 0:43ff9e3bc244 563
chuanga 0:43ff9e3bc244 564 debug_if(_ism_debug, "ISM43362 dns_lookup: %s ok\n", ip);
chuanga 0:43ff9e3bc244 565 return 1;
chuanga 0:43ff9e3bc244 566 }
chuanga 0:43ff9e3bc244 567
chuanga 0:43ff9e3bc244 568 bool ISM43362::send(int id, const void *data, uint32_t amount)
chuanga 0:43ff9e3bc244 569 {
chuanga 0:43ff9e3bc244 570 // The Size limit has to be checked on caller side.
chuanga 0:43ff9e3bc244 571 if (amount > ES_WIFI_MAX_TX_PACKET_SIZE) {
chuanga 0:43ff9e3bc244 572 debug_if(_ism_debug, "ISM43362 send: max issue\n");
chuanga 0:43ff9e3bc244 573 return false;
chuanga 0:43ff9e3bc244 574 }
chuanga 0:43ff9e3bc244 575
chuanga 0:43ff9e3bc244 576 /* Activate the socket id in the wifi module */
chuanga 0:43ff9e3bc244 577 if ((id < 0) || (id > 3)) {
chuanga 0:43ff9e3bc244 578 return false;
chuanga 0:43ff9e3bc244 579 }
chuanga 0:43ff9e3bc244 580 if (_active_id != id) {
chuanga 0:43ff9e3bc244 581 _active_id = id;
chuanga 0:43ff9e3bc244 582 if (!(_parser.send("P0=%d", id) && check_response())) {
chuanga 0:43ff9e3bc244 583 debug_if(_ism_debug, "ISM43362 send: P0 issue\n");
chuanga 0:43ff9e3bc244 584 return false;
chuanga 0:43ff9e3bc244 585 }
chuanga 0:43ff9e3bc244 586 }
chuanga 0:43ff9e3bc244 587
chuanga 0:43ff9e3bc244 588 /* set Write Transport Packet Size */
chuanga 0:43ff9e3bc244 589 int i = _parser.printf("S3=%d\r", (int)amount);
chuanga 0:43ff9e3bc244 590 if (i < 0) {
chuanga 0:43ff9e3bc244 591 debug_if(_ism_debug, "ISM43362 send: S3 issue\n");
chuanga 0:43ff9e3bc244 592 return false;
chuanga 0:43ff9e3bc244 593 }
chuanga 0:43ff9e3bc244 594 i = _parser.write((const char *)data, amount, i);
chuanga 0:43ff9e3bc244 595 if (i < 0) {
chuanga 0:43ff9e3bc244 596 return false;
chuanga 0:43ff9e3bc244 597 }
chuanga 0:43ff9e3bc244 598
chuanga 0:43ff9e3bc244 599 if (!check_response()) {
chuanga 0:43ff9e3bc244 600 return false;
chuanga 0:43ff9e3bc244 601 }
chuanga 0:43ff9e3bc244 602
chuanga 0:43ff9e3bc244 603 debug_if(_ism_debug, "ISM43362 send: id %d amount %d\n", id, amount);
chuanga 0:43ff9e3bc244 604 return true;
chuanga 0:43ff9e3bc244 605 }
chuanga 0:43ff9e3bc244 606
chuanga 0:43ff9e3bc244 607 int ISM43362::check_recv_status(int id, void *data)
chuanga 0:43ff9e3bc244 608 {
chuanga 0:43ff9e3bc244 609 int read_amount;
chuanga 0:43ff9e3bc244 610
chuanga 0:43ff9e3bc244 611 debug_if(_ism_debug, "ISM43362 check_recv_status: id %d\r\n", id);
chuanga 0:43ff9e3bc244 612
chuanga 0:43ff9e3bc244 613 /* Activate the socket id in the wifi module */
chuanga 0:43ff9e3bc244 614 if ((id < 0) || (id > 3)) {
chuanga 0:43ff9e3bc244 615 debug_if(_ism_debug, "ISM43362 check_recv_status: ERROR with id %d\r\n", id);
chuanga 0:43ff9e3bc244 616 return -1;
chuanga 0:43ff9e3bc244 617 }
chuanga 0:43ff9e3bc244 618
chuanga 0:43ff9e3bc244 619 if (_active_id != id) {
chuanga 0:43ff9e3bc244 620 _active_id = id;
chuanga 0:43ff9e3bc244 621 if (!(_parser.send("P0=%d", id) && check_response())) {
chuanga 0:43ff9e3bc244 622 return -1;
chuanga 0:43ff9e3bc244 623 }
chuanga 0:43ff9e3bc244 624 }
chuanga 0:43ff9e3bc244 625
chuanga 0:43ff9e3bc244 626
chuanga 0:43ff9e3bc244 627 if (!_parser.send("R0")) {
chuanga 0:43ff9e3bc244 628 return -1;
chuanga 0:43ff9e3bc244 629 }
chuanga 0:43ff9e3bc244 630 read_amount = _parser.read((char *)data);
chuanga 0:43ff9e3bc244 631
chuanga 0:43ff9e3bc244 632 if (read_amount < 0) {
chuanga 0:43ff9e3bc244 633 debug_if(_ism_debug, "ISM43362 check_recv_status: ERROR in data RECV, timeout?\r\n");
chuanga 0:43ff9e3bc244 634 return -1; /* nothing to read */
chuanga 0:43ff9e3bc244 635 }
chuanga 0:43ff9e3bc244 636
chuanga 0:43ff9e3bc244 637 /* If there are spurious 0x15 at the end of the data, this is an error
chuanga 0:43ff9e3bc244 638 * we hall can get rid off of them :-(
chuanga 0:43ff9e3bc244 639 * This should not happen, but let's try to clean-up anyway
chuanga 0:43ff9e3bc244 640 */
chuanga 0:43ff9e3bc244 641 char *cleanup = (char *) data;
chuanga 0:43ff9e3bc244 642 while ((read_amount > 0) && (cleanup[read_amount - 1] == 0x15)) {
chuanga 0:43ff9e3bc244 643 // debug_if(_ism_debug, "ISM43362 check_recv_status: spurious 0X15 trashed\r\n");
chuanga 0:43ff9e3bc244 644 /* Remove the trailling char then search again */
chuanga 0:43ff9e3bc244 645 read_amount--;
chuanga 0:43ff9e3bc244 646 }
chuanga 0:43ff9e3bc244 647
chuanga 0:43ff9e3bc244 648 if ((read_amount >= 6) && (strncmp("OK\r\n> ", (char *)data, 6) == 0)) {
chuanga 0:43ff9e3bc244 649 // debug_if(_ism_debug, "ISM43362 check_recv_status: recv 2 nothing to read=%d\r\n", read_amount);
chuanga 0:43ff9e3bc244 650 // read_amount -= 6;
chuanga 0:43ff9e3bc244 651 return 0; /* nothing to read */
chuanga 0:43ff9e3bc244 652 } else if ((read_amount >= 8) && (strncmp((char *)((uint32_t) data + read_amount - 8), "\r\nOK\r\n> ", 8)) == 0) {
chuanga 0:43ff9e3bc244 653 /* bypass ""\r\nOK\r\n> " if present at the end of the chain */
chuanga 0:43ff9e3bc244 654 read_amount -= 8;
chuanga 0:43ff9e3bc244 655 } else {
chuanga 0:43ff9e3bc244 656 debug_if(_ism_debug, "ISM43362 check_recv_status: ERROR, flushing %d bytes: ", read_amount);
chuanga 0:43ff9e3bc244 657 // for (int i = 0; i < read_amount; i++) {
chuanga 0:43ff9e3bc244 658 // debug_if(_ism_debug, "%2X ", cleanup[i]);
chuanga 0:43ff9e3bc244 659 // }
chuanga 0:43ff9e3bc244 660 // debug_if(_ism_debug, "\r\n (ASCII)", cleanup);
chuanga 0:43ff9e3bc244 661 cleanup[read_amount] = 0;
chuanga 0:43ff9e3bc244 662 debug_if(_ism_debug, "%s\r\n", cleanup);
chuanga 0:43ff9e3bc244 663 return -1; /* nothing to read */
chuanga 0:43ff9e3bc244 664 }
chuanga 0:43ff9e3bc244 665
chuanga 0:43ff9e3bc244 666 debug_if(_ism_debug, "ISM43362 check_recv_status: id %d read_amount=%d\r\n", id, read_amount);
chuanga 0:43ff9e3bc244 667 return read_amount;
chuanga 0:43ff9e3bc244 668 }
chuanga 0:43ff9e3bc244 669
chuanga 0:43ff9e3bc244 670 bool ISM43362::close(int id)
chuanga 0:43ff9e3bc244 671 {
chuanga 0:43ff9e3bc244 672 if ((id < 0) || (id > 3)) {
chuanga 0:43ff9e3bc244 673 debug_if(_ism_debug, "ISM43362: Wrong socket number\n");
chuanga 0:43ff9e3bc244 674 return false;
chuanga 0:43ff9e3bc244 675 }
chuanga 0:43ff9e3bc244 676 /* Set connection on this socket */
chuanga 0:43ff9e3bc244 677 debug_if(_ism_debug, "ISM43362: CLOSE socket id=%d\n", id);
chuanga 0:43ff9e3bc244 678 _active_id = id;
chuanga 0:43ff9e3bc244 679 if (!(_parser.send("P0=%d", id) && check_response())) {
chuanga 0:43ff9e3bc244 680 return false;
chuanga 0:43ff9e3bc244 681 }
chuanga 0:43ff9e3bc244 682 /* close this socket */
chuanga 0:43ff9e3bc244 683 if (!(_parser.send("P6=0") && check_response())) {
chuanga 0:43ff9e3bc244 684 return false;
chuanga 0:43ff9e3bc244 685 }
chuanga 0:43ff9e3bc244 686 return true;
chuanga 0:43ff9e3bc244 687 }
chuanga 0:43ff9e3bc244 688
chuanga 0:43ff9e3bc244 689 bool ISM43362::readable()
chuanga 0:43ff9e3bc244 690 {
chuanga 0:43ff9e3bc244 691 /* not applicable with SPI api */
chuanga 0:43ff9e3bc244 692 return true;
chuanga 0:43ff9e3bc244 693 }
chuanga 0:43ff9e3bc244 694
chuanga 0:43ff9e3bc244 695 bool ISM43362::writeable()
chuanga 0:43ff9e3bc244 696 {
chuanga 0:43ff9e3bc244 697 /* not applicable with SPI api */
chuanga 0:43ff9e3bc244 698 return true;
chuanga 0:43ff9e3bc244 699 }
chuanga 0:43ff9e3bc244 700
chuanga 0:43ff9e3bc244 701 void ISM43362::attach(Callback<void()> status_cb)
chuanga 0:43ff9e3bc244 702 {
chuanga 0:43ff9e3bc244 703 _conn_stat_cb = status_cb;
chuanga 0:43ff9e3bc244 704 }
chuanga 0:43ff9e3bc244 705
chuanga 0:43ff9e3bc244 706 nsapi_connection_status_t ISM43362::connection_status() const
chuanga 0:43ff9e3bc244 707 {
chuanga 0:43ff9e3bc244 708 debug_if(_ism_debug, "ISM43362: connection_status %d\n", _conn_status);
chuanga 0:43ff9e3bc244 709 return _conn_status;
chuanga 0:43ff9e3bc244 710 }