MODIFIED from mbed official WiflyInterface (interface for Roving Networks Wifly modules). Numerous performance and reliability improvements (see the detailed documentation). Also, tracking changes in mbed official version to retain functional parity.
Dependents: Smart-WiFly-WebServer PUB_WiflyInterface_Demo
Fork of WiflyInterface by
EthernetInterface.cpp
00001 #include "EthernetInterface.h" 00002 00003 00004 //#define DEBUG "WFLY" //Debug is disabled by default 00005 00006 // How to use this debug macro 00007 // 00008 // ... 00009 // INFO("Stuff to show %d", var); // new-line is automatically appended 00010 // [I myfile 23] Stuff to show 23\r\n 00011 // 00012 #if (defined(DEBUG) && !defined(TARGET_LPC11U24)) 00013 #define INFO(x, ...) std::printf("[INF %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); 00014 #define WARN(x, ...) std::printf("[WRN %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); 00015 #define ERR(x, ...) std::printf("[ERR %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); 00016 #else 00017 #define INFO(x, ...) 00018 #define WARN(x, ...) 00019 #define ERR(x, ...) 00020 #endif 00021 00022 static char myName[33]; // name cannot exceed 32 chars 00023 00024 EthernetInterface::EthernetInterface( PinName tx, PinName rx, PinName reset, PinName tcp_status, 00025 const char * ssid, const char * phrase, Security sec) : 00026 Wifly(tx, rx, reset, tcp_status, ssid, phrase, sec) 00027 { 00028 ip_set = false; 00029 mac_set = false; 00030 myName[0] = '\0'; 00031 } 00032 00033 int EthernetInterface::init() 00034 { 00035 state.dhcp = true; 00036 ip_set = false; 00037 reset(); 00038 configure(); 00039 return 0; 00040 } 00041 00042 int EthernetInterface::init(const char* ip, const char* mask, const char* gateway) 00043 { 00044 state.dhcp = false; 00045 this->ip = ip; 00046 strcpy(ip_string, ip); 00047 ip_set = true; 00048 this->netmask = mask; 00049 this->gateway = gateway; 00050 reset(); 00051 configure(); 00052 return 0; 00053 } 00054 00055 void EthernetInterface::setSecurity(const char * ssid, const char * phrase, Security sec) 00056 { 00057 Wifly::SetSecurity(ssid, phrase, sec); 00058 } 00059 00060 int EthernetInterface::connect() 00061 { 00062 return (join()) ? 0 : -1; 00063 } 00064 00065 int EthernetInterface::disconnect() 00066 { 00067 ip_set = false; 00068 return Wifly::disconnect(); 00069 } 00070 00071 // command "get mac\r" 00072 // response "MAC Addr=##:##:##:##:##:##\n<4.40> " 00073 char * EthernetInterface::getMACAddress() 00074 { 00075 char buffer[50]; 00076 char * match = buffer; 00077 char * p; 00078 if (!mac_set) { 00079 mac_string[0] = '\0'; 00080 if (!sendCommand("get mac\r", NULL, buffer)) 00081 return NULL; 00082 exit(); 00083 flush(); 00084 INFO("buffer is '%s'", buffer); 00085 // revised to find the mac address string, following a small 00086 // change where \r are permitted in the captured 00087 // reply to a command. 00088 while (*match && (*match != '=')) 00089 match++; 00090 if (*match) { 00091 match++; // Advance past '=' 00092 p = match; 00093 while (strchr("0123456789abcdefABCDEF:", *p)) 00094 p++; 00095 *p = '\0'; 00096 if (strlen(match) <= 17) { // 192.168.101.202 00097 strcpy(mac_string, match); 00098 mac_set = true; 00099 } 00100 } 00101 } 00102 return mac_string; 00103 } 00104 00105 // typical response might be 00106 // \r192.168.43.107\r 00107 // <4.40> 00108 char * EthernetInterface::getIPAddress() 00109 { 00110 char buffer[50]; 00111 char * match = buffer; 00112 char * p; 00113 if (!ip_set) { 00114 if (!sendCommand("get ip a\r", NULL, buffer)) 00115 return NULL; 00116 exit(); 00117 flush(); 00118 // revised to find the IP string, following a small 00119 // change where \r are permitted in the captured 00120 // reply to a command. 00121 while (*match && (*match < '0' || *match > '9')) 00122 match++; 00123 p = match; 00124 while (strchr("0123456789.", *p)) 00125 p++; 00126 *p = '\0'; 00127 if (strlen(match) <= 15) { // 192.168.101.202 00128 strcpy(ip_string, match); 00129 ip_set = true; 00130 } 00131 } 00132 return ip_string; 00133 } 00134 00135 static bool inRange(char testChar, char minChar, char maxChar) 00136 { 00137 if (testChar >= minChar && testChar <= maxChar) 00138 return true; 00139 else 00140 return false; 00141 } 00142 00143 int EthernetInterface::setName(const char * myname) 00144 { 00145 char buffer[60]; // hold the whole command 00146 int i; 00147 00148 strncpy(myName, myname, 32); 00149 myName[32] = '\0'; // be sure it is NULL terminated. 00150 // make the name 'safe' 00151 for (i=0; i<32 && myName[i]; i++) { 00152 if (!inRange(myName[i], '0', '9') 00153 && !inRange(myName[i], 'A', 'Z') 00154 && !inRange(myName[i], 'a', 'z')) 00155 myName[i] = '-'; 00156 } 00157 sprintf(buffer, "set opt deviceid %s\r", myname); 00158 if (!sendCommand(buffer, "OK")) 00159 return -1; // fail 00160 return 0; // indicate success... 00161 } 00162 00163 const char * EthernetInterface::getName(void) 00164 { 00165 return myName; 00166 } 00167 00168 int EthernetInterface::get_connection_speed(void) 00169 { 00170 char bigbuf[200]; // big enough? 00171 int res = 0; 00172 00173 if (sendCommand("get wlan\r", NULL, bigbuf)) { 00174 // "Rate=15, 54 Mb\r" 00175 char * p = strstr(bigbuf, "Rate="); 00176 00177 if (p) { 00178 p += 5; // strlen("Rate="); 00179 while (*p && *p != ' ') 00180 p++; 00181 while (*p && *p == ' ') 00182 p++; 00183 res = atoi(p); 00184 00185 } 00186 } 00187 return res; 00188 }
Generated on Tue Jul 12 2022 16:14:58 by 1.7.2