Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
WizFi310_static_members.cpp
00001 #include "WizFi310.h" 00002 #include "mbed_trace.h" 00003 #include "greentea_serial.h" 00004 #include <cctype> 00005 00006 #define TRACE_GROUP "WZFI" 00007 00008 using namespace mbed; 00009 00010 // ================================================================================================= 00011 // Utility function 00012 static void print(char c, char *&out) 00013 { 00014 if (isprint((int)c)) { 00015 *out = c; 00016 out++; 00017 } else if (c == '\r') { 00018 *out = '\\'; 00019 out++; 00020 *out = 'r'; 00021 out++; 00022 } else if (c == '\n') { 00023 *out = '\\'; 00024 out++; 00025 *out = 'n'; 00026 out++; 00027 } else { 00028 out += sprintf(out, "\\%02x", c); 00029 } 00030 } 00031 00032 static bool isolate_token(char *&next, char *&start, uint32_t &len) 00033 { 00034 start = next; 00035 char *end = (char *)memchr(start, '/', len); 00036 bool res = end != NULL; 00037 if (res) { 00038 *end = '\0'; 00039 next = end + 1; 00040 len -= (end - start) + 1; 00041 } 00042 return res; 00043 } 00044 00045 // ================================================================================================= 00046 // Static members 00047 bool WizFi310::parse_greeting(const char *buf, uint32_t len, char fw_ver[8]) 00048 { 00049 int count = -1; 00050 sscanf(buf, "WizFi310 Version %8s (WIZnet Co.Ltd)%n", fw_ver, &count); 00051 return (count == (int)len); 00052 } 00053 00054 bool WizFi310::parse_error(const char *buf, uint32_t len, volatile cmd_resp_t &rsp) 00055 { 00056 int count = -1; 00057 char tmp[20] = {0}; 00058 if ((len < 7) || strncmp(buf, "[ERROR", 6) != 0) { 00059 return false; 00060 } 00061 if (buf[6] == ']') { 00062 rsp = CmdRspError; 00063 return true; 00064 } 00065 00066 sscanf(buf, "[ERROR: %19[^]]]%n", tmp, &count); 00067 if (count == (int)len) { 00068 tr_debug("error: %s", tmp); 00069 // error to cmd_resp_t 00070 if (strcmp("INVALID INPUT", tmp) == 0) { 00071 rsp = CmdRspErrorInvalidInput; 00072 } else if (strcmp("INVALID SCID", tmp) == 0) { 00073 rsp = CmdRspErrorInvalidScid; 00074 } else if (strcmp("WiFi Status", tmp) == 0) { 00075 rsp = CmdRspErrorWifiStatus; 00076 } else if (strcmp("MODE STATUS", tmp) == 0) { 00077 rsp = CmdRspErrorModeStatus; 00078 } else { 00079 tr_warn("Unkown error type: %s", tmp); 00080 rsp = CmdRspError; 00081 } 00082 } 00083 00084 return (count == (int)len); 00085 } 00086 00087 bool WizFi310::parse_linkup_ip(const char *buf, uint32_t len, char ip_buffer[16]) 00088 { 00089 int count = -1; 00090 sscanf(buf, " IP Addr : %15[^ ]%n", ip_buffer, &count); 00091 //tr_debug("count: %u==%lu? ip: %s", count, len, ip_buffer); 00092 return (count == (int)len); 00093 } 00094 00095 bool WizFi310::parse_linkup_gw(const char *buf, uint32_t len, char ip_buffer[16]) 00096 { 00097 int count = -1; 00098 sscanf(buf, " Gateway : %15[^ ]%n", ip_buffer, &count); 00099 //tr_debug("count: %u==%lu? ip: %s", count, len, ip_buffer); 00100 return (count == (int)len); 00101 } 00102 00103 bool WizFi310::parse_status(char *buf, uint32_t len, char ip_buf[16], char gw_buf[16], char mac_buf[18], int32_t &rssi) 00104 { 00105 // STA/VM8118528/192.168.0.61/192.168.0.1/00:08:DC:52:A0:42//57 00106 // <mode>/<ssid>/<ip>/<gw>/<mac>/<txpower>/<rssi> 00107 // sscanf("STA/%*[^/]/%15[^/]/%15[^/]/%17[^/]/%*[^/]/%d", ip_buf, gw_buf, mac_buf, &rssi); // sscanf requires %[^/] to be non-empty 00108 char *start = NULL, *next = buf, *end; 00109 if (!isolate_token(next, start, len)) { 00110 return false; 00111 } 00112 // Mode 00113 00114 if (!isolate_token(next, start, len)) { 00115 return false; 00116 } 00117 // ssid 00118 00119 if (!isolate_token(next, start, len)) { 00120 return false; 00121 } 00122 // ip 00123 strncpy(ip_buf, start, 16); 00124 00125 if (!isolate_token(next, start, len)) { 00126 return false; 00127 } 00128 // gw 00129 strncpy(gw_buf, start, 16); 00130 00131 if (!isolate_token(next, start, len)) { 00132 return false; 00133 } 00134 // mac 00135 strncpy(mac_buf, start, 18); 00136 00137 if (!isolate_token(next, start, len)) { 00138 return false; 00139 } 00140 // txpower 00141 00142 start = next; 00143 // rssi 00144 rssi = strtol(start, &end, 10); 00145 return (end - start) == (int)len; 00146 } 00147 00148 bool WizFi310::parse_connect(const char *buf, uint32_t len, int &id) 00149 { 00150 int count = -1; 00151 sscanf(buf, "[CONNECT %d]%n", &id, &count); 00152 return (count == (int)len); 00153 } 00154 00155 bool WizFi310::parse_disconnect(const char *buf, uint32_t len, int &id) 00156 { 00157 int count = -1; 00158 sscanf(buf, "[DISCONNECT %d]%n", &id, &count); 00159 return (count == (int)len); 00160 } 00161 00162 bool WizFi310::parse_send_rdy(const char *buf, uint32_t len, int &id, uint32_t &plen) 00163 { 00164 int count = -1; 00165 sscanf(buf, "[%d,,,%lu]%n", &id, &plen, &count); 00166 return (count == (int)len); 00167 } 00168 00169 bool WizFi310::parse_recv(const char *buf, uint32_t len, int &id, char ip_buf[16], uint16_t &port, uint32_t &plen) 00170 { 00171 int count = -1; 00172 if (buf[0] == '{') { 00173 sscanf(buf, "{%d,%15[^,],%hu,%lu}%n", &id, ip_buf, &port, &plen, &count); 00174 } 00175 00176 return (count == (int)len); 00177 } 00178 00179 bool WizFi310::parse_mac(const char *buf, uint32_t len, char mac_buf[18]) 00180 { 00181 int count = -1; 00182 sscanf(buf, "%*[0-9A-F]:%*[0-9A-F]:%*[0-9A-F]:%*[0-9A-F]:%*[0-9A-F]:%*[0-9A-F]%n", &count); 00183 if ((count == ((int)len)) && (len < 18)) { 00184 strcpy(mac_buf, buf); 00185 } 00186 return (count == ((int)len)) && (len < 18); 00187 } 00188 00189 bool WizFi310::parse_ip(const char *buf, uint32_t len, char ip_buf[16]) 00190 { 00191 int count = -1; 00192 sscanf(buf, "%*3d.%*3d.%*3d.%*3d%n", &count); 00193 if ((count == ((int)len)) && (len < 16)) { 00194 strcpy(ip_buf, buf); 00195 } 00196 return (count == ((int)len)) && (len < 16); 00197 } 00198 00199 00200 bool WizFi310::parse_ap(char *buf, uint32_t len, nsapi_wifi_ap_t *ap) 00201 { 00202 char *start = NULL, *next = buf, *end; 00203 if (!isolate_token(next, start, len)) { 00204 return false; 00205 } 00206 // index 00207 00208 // ssid 00209 if (!isolate_token(next, start, len)) { 00210 return false; 00211 } 00212 strncpy(ap->ssid, start, 32); 00213 00214 // bssid 00215 if (!isolate_token(next, start, len)) { 00216 return false; 00217 } 00218 sscanf(start, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", 00219 &ap->bssid[0], &ap->bssid[1], &ap->bssid[2], &ap->bssid[3], &ap->bssid[4], &ap->bssid[5]); 00220 00221 // rssi 00222 if (!isolate_token(next, start, len)) { 00223 return false; 00224 } 00225 ap->rssi = strtol(start, &end, 10); 00226 MBED_ASSERT((end + 1) == next); 00227 00228 // datarate 00229 if (!isolate_token(next, start, len)) { 00230 return false; 00231 } 00232 00233 // security 00234 if (!isolate_token(next, start, len)) { 00235 return false; 00236 } 00237 ap->security = str2sec(start); 00238 00239 // band 00240 if (!isolate_token(next, start, len)) { 00241 return false; 00242 } 00243 00244 start = next; 00245 // channel 00246 ap->channel = strtoul(start, &end, 10); 00247 00248 bool res = (end - start) == (int)len; 00249 if (!res) { 00250 for (char *ptr = buf; ptr < end; ptr++) { 00251 if (*ptr == 0) { 00252 *ptr = '/'; 00253 } 00254 } 00255 } 00256 return res; 00257 } 00258 00259 const char *WizFi310::print_buf(const char *ptr, uint32_t len) 00260 { 00261 static char output[1024] = {0}; 00262 char *optr = output; 00263 uint32_t i = 0; 00264 for (; (i < len) && ((optr - output) < 1020); i++) { 00265 print(ptr[i], optr); 00266 } 00267 if (((optr - output) >= 1020) && (i < len)) { 00268 *(optr - 3) = '.'; 00269 *(optr - 2) = '.'; 00270 *(optr - 1) = '.'; 00271 } 00272 *(optr) = '\0'; 00273 return output; 00274 } 00275 00276 nsapi_security_t WizFi310::str2sec(const char *str_sec) 00277 { 00278 if (strcmp(str_sec, "Open") == 0) { 00279 return NSAPI_SECURITY_NONE; 00280 } else if (strcmp(str_sec, "WEP") == 0) { 00281 return NSAPI_SECURITY_WEP; 00282 } else if (strcmp(str_sec, "WPA") == 0) { 00283 return NSAPI_SECURITY_WPA; 00284 } else if (strcmp(str_sec, "WPA2") == 0) { 00285 return NSAPI_SECURITY_WPA2; 00286 } else if (strcmp(str_sec, "WPAWPA2") == 0) { 00287 return NSAPI_SECURITY_WPA_WPA2; 00288 } 00289 00290 return NSAPI_SECURITY_UNKNOWN; 00291 } 00292
Generated on Mon Aug 29 2022 19:53:42 by
