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