leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ISM43362.cpp Source File

ISM43362.cpp

00001 /* ISM43362 Example
00002 *
00003 * Copyright (c) STMicroelectronics 2018
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #include <string.h>
00018 #include "ISM43362.h"
00019 #include "mbed_debug.h"
00020 
00021 // activate / de-activate debug
00022 #define ism_debug 0
00023 
00024 ISM43362::ISM43362(PinName mosi, PinName miso, PinName sclk, PinName nss, PinName resetpin, PinName datareadypin, PinName wakeup, bool debug)
00025     : _bufferspi(mosi, miso, sclk, nss, datareadypin),
00026       _parser(_bufferspi),
00027       _resetpin(resetpin),
00028       _packets(0), _packets_end(&_packets)
00029 {
00030     DigitalOut wakeup_pin(wakeup);
00031     _bufferspi.format(16, 0); /* 16bits, ploarity low, phase 1Edge, master mode */
00032     _bufferspi.frequency(20000000); /* up to 20 MHz */
00033     _active_id = 0xFF;
00034     _FwVersionId = 0;
00035 
00036     _ism_debug = debug || ism_debug;
00037     reset();
00038 }
00039 
00040 /**
00041   * @brief  Parses and returns number from string.
00042   * @param  ptr: pointer to string
00043   * @param  cnt: pointer to the number of parsed digit
00044   * @retval integer value.
00045   */
00046 #define CHARISHEXNUM(x)                 (((x) >= '0' && (x) <= '9') || \
00047                                          ((x) >= 'a' && (x) <= 'f') || \
00048                                          ((x) >= 'A' && (x) <= 'F'))
00049 #define CHARISNUM(x)                    ((x) >= '0' && (x) <= '9')
00050 #define CHAR2NUM(x)                     ((x) - '0')
00051 
00052 
00053 extern "C" int32_t ParseNumber(char *ptr, uint8_t *cnt)
00054 {
00055     uint8_t minus = 0, i = 0;
00056     int32_t sum = 0;
00057 
00058     if (*ptr == '-') {                                      /* Check for minus character */
00059         minus = 1;
00060         ptr++;
00061         i++;
00062     }
00063     if (*ptr == 'C') {  /* input string from get_firmware_version is Cx.x.x.x */
00064         ptr++;
00065     }
00066 
00067     while (CHARISNUM(*ptr) || (*ptr == '.')) { /* Parse number */
00068         if (*ptr == '.') {
00069             ptr++; // next char
00070         } else {
00071             sum = 10 * sum + CHAR2NUM(*ptr);
00072             ptr++;
00073             i++;
00074         }
00075     }
00076 
00077     if (cnt != NULL) {                   /* Save number of characters used for number */
00078         *cnt = i;
00079     }
00080     if (minus) {                         /* Minus detected */
00081         return 0 - sum;
00082     }
00083     return sum;                          /* Return number */
00084 }
00085 
00086 uint32_t ISM43362::get_firmware_version(void)
00087 {
00088     char tmp_buffer[250];
00089     char *ptr, *ptr2;
00090     char _fw_version[16];
00091 
00092     /* Use %[^\n] instead of %s to allow having spaces in the string */
00093     if (!(_parser.send("I?") && _parser.recv("%[^\n^\r]\r\n", tmp_buffer) && check_response())) {
00094         debug_if(_ism_debug, "ISM43362: get_firmware_version is FAIL\r\n");
00095         return 0;
00096     }
00097     debug_if(_ism_debug, "ISM43362: get_firmware_version = %s\r\n", tmp_buffer);
00098 
00099     // Get the first version in the string
00100     ptr = strtok((char *)tmp_buffer, ",");
00101     ptr = strtok(NULL, ",");
00102     ptr2 = strtok(NULL, ",");
00103     if (ptr == NULL) {
00104         debug_if(_ism_debug, "ISM43362: get_firmware_version decoding is FAIL\r\n");
00105         return 0;
00106     }
00107     strncpy(_fw_version, ptr, ptr2 - ptr);
00108     _FwVersionId = ParseNumber(_fw_version, NULL);
00109 
00110     return _FwVersionId;
00111 }
00112 
00113 bool ISM43362::reset(void)
00114 {
00115     char tmp_buffer[100];
00116     debug_if(_ism_debug, "ISM43362: Reset Module\r\n");
00117     _resetpin = 0;
00118     wait_ms(10);
00119     _resetpin = 1;
00120     wait_ms(500);
00121 
00122     /* Wait for prompt line : the string is "> ". */
00123     /* As the space char is not detected by sscanf function in parser.recv, */
00124     /* we need to use %[\n] */
00125     if (!_parser.recv(">%[^\n]", tmp_buffer)) {
00126         debug_if(_ism_debug, "ISM43362: Reset Module failed\r\n");
00127         return false;
00128     }
00129     return true;
00130 }
00131 
00132 void ISM43362::print_rx_buff(void)
00133 {
00134     char tmp[150] = {0};
00135     uint16_t i = 0;
00136     debug_if(_ism_debug, "ISM43362: ");
00137     while (i  < 150) {
00138         int c = _parser.getc();
00139         if (c < 0) {
00140             break;
00141         }
00142         tmp[i] = c;
00143         debug_if(_ism_debug, "0x%2X ", c);
00144         i++;
00145     }
00146     debug_if(_ism_debug, "\n");
00147     debug_if(_ism_debug, "ISM43362: Buffer content =====%s=====\r\n", tmp);
00148 }
00149 
00150 /*  checks the standard OK response of the WIFI module, shouldbe:
00151  *  \r\nDATA\r\nOK\r\n>sp
00152  *  or
00153  *  \r\nERROR\r\nUSAGE\r\n>sp
00154  *  function returns true if OK, false otherwise. In case of error,
00155  *  print error content then flush buffer */
00156 bool ISM43362::check_response(void)
00157 {
00158     char tmp_buffer[100];
00159     if (!_parser.recv("OK\r\n")) {
00160         print_rx_buff();
00161         _parser.flush();
00162         return false;
00163     }
00164 
00165     /*  Then we should get the prompt: "> " */
00166     /* As the space char is not detected by sscanf function in parser.recv, */
00167     /* we need to use %[\n] */
00168     if (!_parser.recv(">%[^\n]", tmp_buffer)) {
00169         debug_if(_ism_debug, "ISM43362: Missing prompt in WIFI resp\r\n");
00170         print_rx_buff();
00171         _parser.flush();
00172         return false;
00173     }
00174 
00175     /*  Inventek module do stuffing / padding of data with 0x15,
00176      *  in case buffer contains such */
00177     while (1) {
00178         int c = _parser.getc();
00179         if (c == 0x15) {
00180             // debug_if(_ism_debug, "ISM43362: Flush char 0x%x\n", c);
00181             continue;
00182         } else {
00183             /*  How to put it back if needed ? */
00184             break;
00185         }
00186     }
00187     return true;
00188 }
00189 
00190 bool ISM43362::dhcp(bool enabled)
00191 {
00192     return (_parser.send("C4=%d", enabled ? 1 : 0) && check_response());
00193 }
00194 
00195 int ISM43362::connect(const char *ap, const char *passPhrase, ism_security_t ap_sec)
00196 {
00197     char tmp[256];
00198 
00199     if (!(_parser.send("C1=%s", ap) && check_response())) {
00200         return NSAPI_ERROR_PARAMETER;
00201     }
00202 
00203     if (!(_parser.send("C2=%s", passPhrase) && check_response())) {
00204         return NSAPI_ERROR_PARAMETER;
00205     }
00206 
00207     /* Check security level is acceptable */
00208     if (ap_sec > ISM_SECURITY_WPA_WPA2) {
00209         debug_if(_ism_debug, "ISM43362: Unsupported security level %d\n", ap_sec);
00210         return NSAPI_ERROR_UNSUPPORTED;
00211     }
00212 
00213     if (!(_parser.send("C3=%d", ap_sec) && check_response())) {
00214         return NSAPI_ERROR_PARAMETER;
00215     }
00216 
00217     if (_parser.send("C0")) {
00218         while (_parser.recv("%[^\n]\n", tmp)) {
00219             if (strstr(tmp, "OK")) {
00220                 _parser.flush();
00221                 _conn_status = NSAPI_STATUS_GLOBAL_UP;
00222                 _conn_stat_cb();
00223                 return NSAPI_ERROR_OK;
00224             }
00225             if (strstr(tmp, "JOIN")) {
00226                 _conn_status = NSAPI_STATUS_CONNECTING;
00227                 _conn_stat_cb();
00228                 if (strstr(tmp, "Failed")) {
00229                     _parser.flush();
00230                     return NSAPI_ERROR_AUTH_FAILURE;
00231                 }
00232             }
00233         }
00234     }
00235 
00236     return NSAPI_ERROR_NO_CONNECTION;
00237 }
00238 
00239 bool ISM43362::disconnect(void)
00240 {
00241     _conn_status = NSAPI_STATUS_DISCONNECTED;
00242     _conn_stat_cb();
00243     return (_parser.send("CD") && check_response());
00244 }
00245 
00246 const char *ISM43362::getIPAddress(void)
00247 {
00248     char tmp_ip_buffer[250];
00249     char *ptr, *ptr2;
00250 
00251     /* Use %[^\n] instead of %s to allow having spaces in the string */
00252     if (!(_parser.send("C?")
00253             && _parser.recv("%[^\n^\r]\r\n", tmp_ip_buffer)
00254             && check_response())) {
00255         debug_if(_ism_debug, "ISM43362: getIPAddress LINE KO: %s\n", tmp_ip_buffer);
00256         return 0;
00257     }
00258 
00259     /* Get the IP address in the result */
00260     /* TODO : check if the begining of the string is always = "eS-WiFi_AP_C47F51011231," */
00261     ptr = strtok((char *)tmp_ip_buffer, ",");
00262     ptr = strtok(NULL, ",");
00263     ptr = strtok(NULL, ",");
00264     ptr = strtok(NULL, ",");
00265     ptr = strtok(NULL, ",");
00266     ptr = strtok(NULL, ",");
00267     ptr2 = strtok(NULL, ",");
00268     if (ptr == NULL) {
00269         return 0;
00270     }
00271     strncpy(_ip_buffer, ptr, ptr2 - ptr);
00272 
00273     tmp_ip_buffer[59] = 0;
00274     debug_if(_ism_debug, "ISM43362: receivedIPAddress: %s\n", _ip_buffer);
00275 
00276     return _ip_buffer;
00277 }
00278 
00279 const char *ISM43362::getMACAddress(void)
00280 {
00281     if (!(_parser.send("Z5") && _parser.recv("%s\r\n", _mac_buffer) && check_response())) {
00282         debug_if(_ism_debug, "ISM43362: receivedMacAddress LINE KO: %s\n", _mac_buffer);
00283         return 0;
00284     }
00285 
00286     debug_if(_ism_debug, "ISM43362: receivedMacAddress:%s, size=%d\r\n", _mac_buffer, sizeof(_mac_buffer));
00287 
00288     return _mac_buffer;
00289 }
00290 
00291 const char *ISM43362::getGateway()
00292 {
00293     char tmp[250];
00294     /* Use %[^\n] instead of %s to allow having spaces in the string */
00295     if (!(_parser.send("C?") && _parser.recv("%[^\n^\r]\r\n", tmp) && check_response())) {
00296         debug_if(_ism_debug, "ISM43362: getGateway LINE KO: %s\r\n", tmp);
00297         return 0;
00298     }
00299 
00300     /* Extract the Gateway in the received buffer */
00301     char *ptr;
00302     ptr = strtok(tmp, ",");
00303     for (int i = 0; i < 7; i++) {
00304         if (ptr == NULL) {
00305             break;
00306         }
00307         ptr = strtok(NULL, ",");
00308     }
00309 
00310     strncpy(_gateway_buffer, ptr, sizeof(_gateway_buffer));
00311 
00312     debug_if(_ism_debug, "ISM43362: getGateway: %s\r\n", _gateway_buffer);
00313 
00314     return _gateway_buffer;
00315 }
00316 
00317 const char *ISM43362::getNetmask()
00318 {
00319     char tmp[250];
00320     /* Use %[^\n] instead of %s to allow having spaces in the string */
00321     if (!(_parser.send("C?") && _parser.recv("%[^\n^\r]\r\n", tmp) && check_response())) {
00322         debug_if(_ism_debug, "ISM43362: getNetmask LINE KO: %s\n", tmp);
00323         return 0;
00324     }
00325 
00326     /* Extract Netmask in the received buffer */
00327     char *ptr;
00328     ptr = strtok(tmp, ",");
00329     for (int i = 0; i < 6; i++) {
00330         if (ptr == NULL) {
00331             break;
00332         }
00333         ptr = strtok(NULL, ",");
00334     }
00335 
00336     strncpy(_netmask_buffer, ptr, sizeof(_netmask_buffer));
00337 
00338     debug_if(_ism_debug, "ISM43362: getNetmask: %s\r\n", _netmask_buffer);
00339 
00340     return _netmask_buffer;
00341 }
00342 
00343 int8_t ISM43362::getRSSI()
00344 {
00345     int8_t rssi;
00346     char tmp[25];
00347 
00348     if (!(_parser.send("CR") && _parser.recv("%s\r\n", tmp) && check_response())) {
00349         debug_if(_ism_debug, "ISM43362: getRSSI LINE KO: %s\r\n", tmp);
00350         return 0;
00351     }
00352 
00353     rssi = ParseNumber(tmp, NULL);
00354 
00355     debug_if(_ism_debug, "ISM43362: getRSSI: %d\r\n", rssi);
00356 
00357     return rssi;
00358 }
00359 /**
00360   * @brief  Parses Security type.
00361   * @param  ptr: pointer to string
00362   * @retval Encryption type.
00363   */
00364 extern "C" nsapi_security_t ParseSecurity(char *ptr)
00365 {
00366     if (strstr(ptr, "Open")) {
00367         return NSAPI_SECURITY_NONE;
00368     } else if (strstr(ptr, "WEP")) {
00369         return NSAPI_SECURITY_WEP;
00370     } else if (strstr(ptr, "WPA2 AES")) {
00371         return NSAPI_SECURITY_WPA2;
00372     } else if (strstr(ptr, "WPA WPA2")) {
00373         return NSAPI_SECURITY_WPA_WPA2;
00374     } else if (strstr(ptr, "WPA2 TKIP")) {
00375         return NSAPI_SECURITY_UNKNOWN;    // no match in mbed
00376     } else if (strstr(ptr, "WPA2")) {
00377         return NSAPI_SECURITY_WPA2;    // catch any other WPA2 formula
00378     } else if (strstr(ptr, "WPA")) {
00379         return NSAPI_SECURITY_WPA;
00380     } else {
00381         return NSAPI_SECURITY_UNKNOWN;
00382     }
00383 }
00384 
00385 /**
00386   * @brief  Convert char in Hex format to integer.
00387   * @param  a: character to convert
00388   * @retval integer value.
00389   */
00390 extern "C"  uint8_t Hex2Num(char a)
00391 {
00392     if (a >= '0' && a <= '9') {                             /* Char is num */
00393         return a - '0';
00394     } else if (a >= 'a' && a <= 'f') {                      /* Char is lowercase character A - Z (hex) */
00395         return (a - 'a') + 10;
00396     } else if (a >= 'A' && a <= 'F') {                      /* Char is uppercase character A - Z (hex) */
00397         return (a - 'A') + 10;
00398     }
00399 
00400     return 0;
00401 }
00402 
00403 /**
00404   * @brief  Extract a hex number from a string.
00405   * @param  ptr: pointer to string
00406   * @param  cnt: pointer to the number of parsed digit
00407   * @retval Hex value.
00408   */
00409 extern "C" uint32_t ParseHexNumber(char *ptr, uint8_t *cnt)
00410 {
00411     uint32_t sum = 0;
00412     uint8_t i = 0;
00413 
00414     while (CHARISHEXNUM(*ptr)) {         /* Parse number */
00415         sum <<= 4;
00416         sum += Hex2Num(*ptr);
00417         ptr++;
00418         i++;
00419     }
00420 
00421     if (cnt != NULL) {                  /* Save number of characters used for number */
00422         *cnt = i;
00423     }
00424     return sum;                         /* Return number */
00425 }
00426 
00427 bool ISM43362::isConnected(void)
00428 {
00429     return getIPAddress() != 0;
00430 }
00431 
00432 int ISM43362::scan(WiFiAccessPoint *res, unsigned limit)
00433 {
00434     unsigned cnt = 0, num = 0;
00435     char *ptr;
00436     char tmp[256];
00437 
00438     if (!(_parser.send("F0"))) {
00439         debug_if(_ism_debug, "ISM43362: scan error\r\n");
00440         return 0;
00441     }
00442 
00443     /* Parse the received buffer and fill AP buffer */
00444     /* Use %[^\n] instead of %s to allow having spaces in the string */
00445     while (_parser.recv("#%[^\n]\n", tmp)) {
00446         if (limit != 0 && cnt >= limit) {
00447             /* reached end */
00448             break;
00449         }
00450         nsapi_wifi_ap_t ap = {0};
00451         debug_if(_ism_debug, "ISM43362: received:%s\n", tmp);
00452         ptr = strtok(tmp, ",");
00453         num = 0;
00454         while (ptr != NULL) {
00455             switch (num++) {
00456                 case 0: /* Ignore index */
00457                 case 4: /* Ignore Max Rate */
00458                 case 5: /* Ignore Network Type */
00459                 case 7: /* Ignore Radio Band */
00460                     break;
00461                 case 1:
00462                     ptr[strlen(ptr) - 1] = 0;
00463                     strncpy((char *)ap.ssid,  ptr + 1, 32);
00464                     break;
00465                 case 2:
00466                     for (int i = 0; i < 6; i++) {
00467                         ap.bssid[i] = ParseHexNumber(ptr + (i * 3), NULL);
00468                     }
00469                     break;
00470                 case 3:
00471                     ap.rssi = ParseNumber(ptr, NULL);
00472                     break;
00473                 case 6:
00474                     ap.security = ParseSecurity(ptr);
00475                     break;
00476                 case 8:
00477                     ap.channel = ParseNumber(ptr, NULL);
00478                     num = 1;
00479                     break;
00480                 default:
00481                     break;
00482             }
00483             ptr = strtok(NULL, ",");
00484         }
00485         if (res != NULL) {
00486             res[cnt] = WiFiAccessPoint(ap);
00487         }
00488         cnt++;
00489     }
00490 
00491     /* We may stop before having read all the APs list, so flush the rest of
00492      * it as well as OK commands */
00493     _parser.flush();
00494 
00495     debug_if(_ism_debug, "ISM43362: End of Scan: cnt=%d\n", cnt);
00496 
00497     return cnt;
00498 
00499 }
00500 
00501 bool ISM43362::open(const char *type, int id, const char *addr, int port)
00502 {
00503     /* TODO : This is the implementation for the client socket, need to check if need to create openserver too */
00504     //IDs only 0-3
00505     if ((id < 0) || (id > 3)) {
00506         debug_if(_ism_debug, "ISM43362: open: wrong id\n");
00507         return false;
00508     }
00509     /* Set communication socket */
00510     _active_id = id;
00511     if (!(_parser.send("P0=%d", id) && check_response())) {
00512         debug_if(_ism_debug, "ISM43362: open: P0 issue\n");
00513         return false;
00514     }
00515     /* Set protocol */
00516     if (!(_parser.send("P1=%s", type) && check_response())) {
00517         debug_if(_ism_debug, "ISM43362: open: P1 issue\n");
00518         return false;
00519     }
00520     /* Set address */
00521     if (!(_parser.send("P3=%s", addr) && check_response())) {
00522         debug_if(_ism_debug, "ISM43362: open: P3 issue\n");
00523         return false;
00524     }
00525     if (!(_parser.send("P4=%d", port) && check_response())) {
00526         debug_if(_ism_debug, "ISM43362: open: P4 issue\n");
00527         return false;
00528     }
00529     /* Start client */
00530     if (!(_parser.send("P6=1") && check_response())) {
00531         debug_if(_ism_debug, "ISM43362: open: P6 issue\n");
00532         return false;
00533     }
00534 
00535     /* request as much data as possible - i.e. module max size */
00536     if (!(_parser.send("R1=%d", ES_WIFI_MAX_RX_PACKET_SIZE) && check_response())) {
00537         debug_if(_ism_debug, "ISM43362: open: R1 issue\n");
00538         return -1;
00539     }
00540 
00541     /* Non blocking mode : set Read Transport Timeout to 1ms */
00542     if (!(_parser.send("R2=1") && check_response())) {
00543         debug_if(_ism_debug, "ISM43362: open: R2 issue\n");
00544         return -1;
00545     }
00546 
00547     debug_if(_ism_debug, "ISM43362: open ok with id %d type %s addr %s port %d\n", id, type, addr, port);
00548 
00549     return true;
00550 }
00551 
00552 bool ISM43362::dns_lookup(const char *name, char *ip)
00553 {
00554     char tmp[30];
00555 
00556     if (!(_parser.send("D0=%s", name) && _parser.recv("%s\r\n", tmp)
00557             && check_response())) {
00558         debug_if(_ism_debug, "ISM43362 dns_lookup: D0 issue: %s\n", tmp);
00559         return 0;
00560     }
00561 
00562     strncpy(ip, tmp, sizeof(tmp));
00563 
00564     debug_if(_ism_debug, "ISM43362 dns_lookup: %s ok\n", ip);
00565     return 1;
00566 }
00567 
00568 bool ISM43362::send(int id, const void *data, uint32_t amount)
00569 {
00570     // The Size limit has to be checked on caller side.
00571     if (amount > ES_WIFI_MAX_TX_PACKET_SIZE) {
00572         debug_if(_ism_debug, "ISM43362 send: max issue\n");
00573         return false;
00574     }
00575 
00576     /* Activate the socket id in the wifi module */
00577     if ((id < 0) || (id > 3)) {
00578         return false;
00579     }
00580     if (_active_id != id) {
00581         _active_id = id;
00582         if (!(_parser.send("P0=%d", id) && check_response())) {
00583             debug_if(_ism_debug, "ISM43362 send: P0 issue\n");
00584             return false;
00585         }
00586     }
00587 
00588     /* set Write Transport Packet Size */
00589     int i = _parser.printf("S3=%d\r", (int)amount);
00590     if (i < 0) {
00591         debug_if(_ism_debug, "ISM43362 send: S3 issue\n");
00592         return false;
00593     }
00594     i = _parser.write((const char *)data, amount, i);
00595     if (i < 0) {
00596         return false;
00597     }
00598 
00599     if (!check_response()) {
00600         return false;
00601     }
00602 
00603     debug_if(_ism_debug, "ISM43362 send: id %d amount %d\n", id, amount);
00604     return true;
00605 }
00606 
00607 int ISM43362::check_recv_status(int id, void *data)
00608 {
00609     int read_amount;
00610 
00611     debug_if(_ism_debug, "ISM43362 check_recv_status: id %d\r\n", id);
00612 
00613     /* Activate the socket id in the wifi module */
00614     if ((id < 0) || (id > 3)) {
00615         debug_if(_ism_debug, "ISM43362 check_recv_status: ERROR with id %d\r\n", id);
00616         return -1;
00617     }
00618 
00619     if (_active_id != id) {
00620         _active_id = id;
00621         if (!(_parser.send("P0=%d", id) && check_response())) {
00622             return -1;
00623         }
00624     }
00625 
00626 
00627     if (!_parser.send("R0")) {
00628         return -1;
00629     }
00630     read_amount = _parser.read((char *)data);
00631 
00632     if (read_amount < 0) {
00633         debug_if(_ism_debug, "ISM43362 check_recv_status: ERROR in data RECV, timeout?\r\n");
00634         return -1; /* nothing to read */
00635     }
00636 
00637     /*  If there are spurious 0x15 at the end of the data, this is an error
00638      *  we hall can get rid off of them :-(
00639      *  This should not happen, but let's try to clean-up anyway
00640      */
00641     char *cleanup = (char *) data;
00642     while ((read_amount > 0) && (cleanup[read_amount - 1] == 0x15)) {
00643         // debug_if(_ism_debug, "ISM43362 check_recv_status: spurious 0X15 trashed\r\n");
00644         /* Remove the trailling char then search again */
00645         read_amount--;
00646     }
00647 
00648     if ((read_amount >= 6) && (strncmp("OK\r\n> ", (char *)data, 6) == 0)) {
00649         // debug_if(_ism_debug, "ISM43362 check_recv_status: recv 2 nothing to read=%d\r\n", read_amount);
00650         // read_amount -= 6;
00651         return 0; /* nothing to read */
00652     } else if ((read_amount >= 8) && (strncmp((char *)((uint32_t) data + read_amount - 8), "\r\nOK\r\n> ", 8)) == 0) {
00653         /* bypass ""\r\nOK\r\n> " if present at the end of the chain */
00654         read_amount -= 8;
00655     } else {
00656         debug_if(_ism_debug, "ISM43362 check_recv_status: ERROR, flushing %d bytes: ", read_amount);
00657         // for (int i = 0; i < read_amount; i++) {
00658         //      debug_if(_ism_debug, "%2X ", cleanup[i]);
00659         // }
00660         // debug_if(_ism_debug, "\r\n (ASCII)", cleanup);
00661         cleanup[read_amount] = 0;
00662         debug_if(_ism_debug, "%s\r\n", cleanup);
00663         return -1; /* nothing to read */
00664     }
00665 
00666     debug_if(_ism_debug, "ISM43362 check_recv_status: id %d read_amount=%d\r\n", id, read_amount);
00667     return read_amount;
00668 }
00669 
00670 bool ISM43362::close(int id)
00671 {
00672     if ((id < 0) || (id > 3)) {
00673         debug_if(_ism_debug, "ISM43362: Wrong socket number\n");
00674         return false;
00675     }
00676     /* Set connection on this socket */
00677     debug_if(_ism_debug, "ISM43362: CLOSE socket id=%d\n", id);
00678     _active_id = id;
00679     if (!(_parser.send("P0=%d", id) && check_response())) {
00680         return false;
00681     }
00682     /* close this socket */
00683     if (!(_parser.send("P6=0") && check_response())) {
00684         return false;
00685     }
00686     return true;
00687 }
00688 
00689 bool ISM43362::readable()
00690 {
00691     /* not applicable with SPI api */
00692     return true;
00693 }
00694 
00695 bool ISM43362::writeable()
00696 {
00697     /* not applicable with SPI api */
00698     return true;
00699 }
00700 
00701 void ISM43362::attach(Callback<void()> status_cb)
00702 {
00703     _conn_stat_cb = status_cb;
00704 }
00705 
00706 nsapi_connection_status_t ISM43362::connection_status() const
00707 {
00708     debug_if(_ism_debug, "ISM43362: connection_status %d\n", _conn_status);
00709     return _conn_status;
00710 }