https://developer.mbed.org/users/ds4279/code/WiflySocket/

Committer:
ds4279
Date:
Wed Feb 22 18:49:17 2017 +0000
Revision:
0:374a8a31f262
Added modified library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ds4279 0:374a8a31f262 1 /* Copyright (C) 2012 mbed.org, MIT License
ds4279 0:374a8a31f262 2 *
ds4279 0:374a8a31f262 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
ds4279 0:374a8a31f262 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
ds4279 0:374a8a31f262 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
ds4279 0:374a8a31f262 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
ds4279 0:374a8a31f262 7 * furnished to do so, subject to the following conditions:
ds4279 0:374a8a31f262 8 *
ds4279 0:374a8a31f262 9 * The above copyright notice and this permission notice shall be included in all copies or
ds4279 0:374a8a31f262 10 * substantial portions of the Software.
ds4279 0:374a8a31f262 11 *
ds4279 0:374a8a31f262 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
ds4279 0:374a8a31f262 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
ds4279 0:374a8a31f262 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
ds4279 0:374a8a31f262 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ds4279 0:374a8a31f262 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ds4279 0:374a8a31f262 17 */
ds4279 0:374a8a31f262 18
ds4279 0:374a8a31f262 19 #include "mbed.h"
ds4279 0:374a8a31f262 20
ds4279 0:374a8a31f262 21 #include "headerStuff.h"
ds4279 0:374a8a31f262 22 #include "Wifly.h"
ds4279 0:374a8a31f262 23 #include <string>
ds4279 0:374a8a31f262 24 #include <algorithm>
ds4279 0:374a8a31f262 25
ds4279 0:374a8a31f262 26 //extern void sendToDisplay(int cmd,int canID,char *canMsg);
ds4279 0:374a8a31f262 27 extern Serial myDebug;
ds4279 0:374a8a31f262 28
ds4279 0:374a8a31f262 29 //Debug is disabled by default
ds4279 0:374a8a31f262 30 #if (0 && !defined(TARGET_LPC11U24))
ds4279 0:374a8a31f262 31 #define DBG(x, ...) std::printf("[Wifly : DBG]"x"\r\n", ##__VA_ARGS__);
ds4279 0:374a8a31f262 32 #define WARN(x, ...) std::printf("[Wifly : WARN]"x"\r\n", ##__VA_ARGS__);
ds4279 0:374a8a31f262 33 #define ERR(x, ...) std::printf("[Wifly : ERR]"x"\r\n", ##__VA_ARGS__);
ds4279 0:374a8a31f262 34 #else
ds4279 0:374a8a31f262 35 #define DBG(x, ...)
ds4279 0:374a8a31f262 36 #define WARN(x, ...)
ds4279 0:374a8a31f262 37 #define ERR(x, ...)
ds4279 0:374a8a31f262 38 #endif
ds4279 0:374a8a31f262 39
ds4279 0:374a8a31f262 40 #if !defined(TARGET_LPC11U24)
ds4279 0:374a8a31f262 41 #define INFO(x, ...) printf("[Wifly : INFO]"x"\r\n", ##__VA_ARGS__);
ds4279 0:374a8a31f262 42 #else
ds4279 0:374a8a31f262 43 #define INFO(x, ...)
ds4279 0:374a8a31f262 44 #endif
ds4279 0:374a8a31f262 45
ds4279 0:374a8a31f262 46 #define MAX_TRY_JOIN 3
ds4279 0:374a8a31f262 47
ds4279 0:374a8a31f262 48 /////////////////////////////////////////
ds4279 0:374a8a31f262 49 ///Make this object visible to the other
ds4279 0:374a8a31f262 50 ///classes. UDPSocket, etc...
ds4279 0:374a8a31f262 51 Wifly * Wifly::inst;
ds4279 0:374a8a31f262 52
ds4279 0:374a8a31f262 53 // PB_4/D5/GPIO6 short pad P6 on shield
ds4279 0:374a8a31f262 54 Wifly::Wifly( PinName tx, PinName rx, PinName _reset, PinName tcp_status, const char * ssid, const char * phrase, Security sec):
ds4279 0:374a8a31f262 55 wifiSer(tx, rx,512,2), reset_pin(_reset), tcp_status(tcp_status)//, buf_wifly(256)
ds4279 0:374a8a31f262 56 //PinName tx, PinName rx, uint32_t buf_size = 512, uint32_t tx_multiple = 4
ds4279 0:374a8a31f262 57 {
ds4279 0:374a8a31f262 58 memset(&state, 0, sizeof(state));
ds4279 0:374a8a31f262 59 state.sec = sec;
ds4279 0:374a8a31f262 60
ds4279 0:374a8a31f262 61 ///////////////////////////////////////////
ds4279 0:374a8a31f262 62 ///The default baud rate 9600. Once change
ds4279 0:374a8a31f262 63 ///It will never have to be changed again.
ds4279 0:374a8a31f262 64 wifiSer.baud(19200);
ds4279 0:374a8a31f262 65 wifiSer.format(8,Serial::None,1);
ds4279 0:374a8a31f262 66
ds4279 0:374a8a31f262 67 // change all ' ' in '$' in the ssid and the passphrase
ds4279 0:374a8a31f262 68 strcpy(this->ssid, ssid);
ds4279 0:374a8a31f262 69 for (int i = 0; i < strlen(ssid); i++)
ds4279 0:374a8a31f262 70 {
ds4279 0:374a8a31f262 71 if (this->ssid[i] == ' ')
ds4279 0:374a8a31f262 72 this->ssid[i] = '$';
ds4279 0:374a8a31f262 73 }
ds4279 0:374a8a31f262 74
ds4279 0:374a8a31f262 75 strcpy(this->phrase, phrase);
ds4279 0:374a8a31f262 76 for (int i = 0; i < strlen(phrase); i++)
ds4279 0:374a8a31f262 77 {
ds4279 0:374a8a31f262 78 if (this->phrase[i] == ' ')
ds4279 0:374a8a31f262 79 this->phrase[i] = '$';
ds4279 0:374a8a31f262 80 }
ds4279 0:374a8a31f262 81
ds4279 0:374a8a31f262 82 strcpy(this->staticIP,"0.0.0.0");
ds4279 0:374a8a31f262 83 inst = this; //make this class visible to other classes
ds4279 0:374a8a31f262 84 // attach_rx(false);
ds4279 0:374a8a31f262 85 state.cmd_mode = false;
ds4279 0:374a8a31f262 86 state.dhcp = true; //assume initial connection DHCP
ds4279 0:374a8a31f262 87 }
ds4279 0:374a8a31f262 88
ds4279 0:374a8a31f262 89 void Wifly::getDHCPhostInfo(char *SSID, char *passPhrase, int &auth)
ds4279 0:374a8a31f262 90 {
ds4279 0:374a8a31f262 91 auth = state.sec;
ds4279 0:374a8a31f262 92 strcpy(SSID,this->ssid);
ds4279 0:374a8a31f262 93 strcpy(passPhrase,this->phrase);
ds4279 0:374a8a31f262 94 }
ds4279 0:374a8a31f262 95
ds4279 0:374a8a31f262 96 bool Wifly::init()
ds4279 0:374a8a31f262 97 {
ds4279 0:374a8a31f262 98
ds4279 0:374a8a31f262 99 //set uart mode <value>
ds4279 0:374a8a31f262 100 //Bit Position Function
ds4279 0:374a8a31f262 101 //0 NOECHO - disables echo of RX data while in
ds4279 0:374a8a31f262 102 // command mode
ds4279 0:374a8a31f262 103 //1 DATA TRIGGER makes connection on RX data
ds4279 0:374a8a31f262 104 //2 Reserved for future use
ds4279 0:374a8a31f262 105 //3 Enable Sleep on RX BREAK signal
ds4279 0:374a8a31f262 106 //4 UART RX data buffer. See note below for details*
ds4279 0:374a8a31f262 107
ds4279 0:374a8a31f262 108 //no echo: set uart mode <value>
ds4279 0:374a8a31f262 109 //myDebug.printf("set u m 1\r");
ds4279 0:374a8a31f262 110 if (!sendCommand("set u m 1\r", "AOK",NULL,1000))
ds4279 0:374a8a31f262 111 {
ds4279 0:374a8a31f262 112 myDebug.printf("Error: set u m 1\n");
ds4279 0:374a8a31f262 113 } //
ds4279 0:374a8a31f262 114 //continue;
ds4279 0:374a8a31f262 115 wait_ms(10);
ds4279 0:374a8a31f262 116 //myDebug.printf("set c t 30\r");
ds4279 0:374a8a31f262 117 // set time: set comm timer <30ms>
ds4279 0:374a8a31f262 118 if (!sendCommand("set c t 30\r", "AOK",NULL,1000)) //
ds4279 0:374a8a31f262 119 {
ds4279 0:374a8a31f262 120 myDebug.printf("Error: set c t 30\n");
ds4279 0:374a8a31f262 121 }
ds4279 0:374a8a31f262 122
ds4279 0:374a8a31f262 123 wait_ms(10);
ds4279 0:374a8a31f262 124 //myDebug.printf("set c s 1024\r");
ds4279 0:374a8a31f262 125 // set size
ds4279 0:374a8a31f262 126 if (!sendCommand("set c s 1024\r", "AOK",NULL,1000))
ds4279 0:374a8a31f262 127 {
ds4279 0:374a8a31f262 128 myDebug.printf("Error: set c s 1024\n");
ds4279 0:374a8a31f262 129 }
ds4279 0:374a8a31f262 130
ds4279 0:374a8a31f262 131
ds4279 0:374a8a31f262 132 wait_ms(10);
ds4279 0:374a8a31f262 133 //myDebug.printf("set s i 0x40\r");
ds4279 0:374a8a31f262 134 // red led on when tcp connection active
ds4279 0:374a8a31f262 135 if (!sendCommand("set s i 0x40\r", "AOK",NULL,1000))
ds4279 0:374a8a31f262 136 {
ds4279 0:374a8a31f262 137 myDebug.printf("Error: set s i 0x40\n");
ds4279 0:374a8a31f262 138 }
ds4279 0:374a8a31f262 139
ds4279 0:374a8a31f262 140 wait_ms(100);
ds4279 0:374a8a31f262 141 // Do not auto auto
ds4279 0:374a8a31f262 142
ds4279 0:374a8a31f262 143 if (!sendCommand("set w j 0\r", "AOK",NULL,500) )
ds4279 0:374a8a31f262 144 {
ds4279 0:374a8a31f262 145 myDebug.printf("Error: set w j 0\n");
ds4279 0:374a8a31f262 146 }
ds4279 0:374a8a31f262 147 //continue;
ds4279 0:374a8a31f262 148 wait_ms(10);
ds4279 0:374a8a31f262 149 //For the RN module to accept both TCP and UDP, set bits 1 and 2 (value = 3).
ds4279 0:374a8a31f262 150 if (!sendCommand("set ip p 3\r", "AOK",NULL,500) )
ds4279 0:374a8a31f262 151 {
ds4279 0:374a8a31f262 152 myDebug.printf("Error: set w j 0\n");
ds4279 0:374a8a31f262 153 }
ds4279 0:374a8a31f262 154
ds4279 0:374a8a31f262 155 /*
ds4279 0:374a8a31f262 156 wait_ms(10);
ds4279 0:374a8a31f262 157 //uart baudrate
ds4279 0:374a8a31f262 158 if (!sendCommand("set uart baud 19200\r", "AOK",NULL,500) )
ds4279 0:374a8a31f262 159 {
ds4279 0:374a8a31f262 160 myDebug.printf("Error: set w j 0\n");
ds4279 0:374a8a31f262 161 }
ds4279 0:374a8a31f262 162 */
ds4279 0:374a8a31f262 163 if (!sendCommand("save\r", "Stor"))
ds4279 0:374a8a31f262 164 {
ds4279 0:374a8a31f262 165 // sendToDisplay(RS485Cmd,0,"Error: save, Stor\n");
ds4279 0:374a8a31f262 166 }
ds4279 0:374a8a31f262 167
ds4279 0:374a8a31f262 168
ds4279 0:374a8a31f262 169 wait_ms(10);
ds4279 0:374a8a31f262 170
ds4279 0:374a8a31f262 171
ds4279 0:374a8a31f262 172 // sendCommand("reboot\r", "ree") ;
ds4279 0:374a8a31f262 173 // exitCmdMode(); //leave command mode
ds4279 0:374a8a31f262 174
ds4279 0:374a8a31f262 175 myDebug.printf("sending Reboot\n");
ds4279 0:374a8a31f262 176 //cmd, ack, res, int timeout
ds4279 0:374a8a31f262 177 if(sendCommand("reboot\r", "Reboot",NULL,5000,false))
ds4279 0:374a8a31f262 178 {
ds4279 0:374a8a31f262 179 //myDebug.printf("DHCP=ON\n");
ds4279 0:374a8a31f262 180 }
ds4279 0:374a8a31f262 181 else
ds4279 0:374a8a31f262 182 {
ds4279 0:374a8a31f262 183 //myDebug.printf("DHCP Error\n");
ds4279 0:374a8a31f262 184
ds4279 0:374a8a31f262 185 }
ds4279 0:374a8a31f262 186 state.cmd_mode = false;
ds4279 0:374a8a31f262 187 return true;
ds4279 0:374a8a31f262 188 }
ds4279 0:374a8a31f262 189
ds4279 0:374a8a31f262 190 void Wifly::setConnectionType(bool DHCP_STATIC)
ds4279 0:374a8a31f262 191 {
ds4279 0:374a8a31f262 192 state.dhcp = DHCP_STATIC;
ds4279 0:374a8a31f262 193 }
ds4279 0:374a8a31f262 194
ds4279 0:374a8a31f262 195 bool Wifly::join(const char *ssid, const char *phrase, int auth)
ds4279 0:374a8a31f262 196 {
ds4279 0:374a8a31f262 197 char cmd[200];
ds4279 0:374a8a31f262 198 unsigned char retries = 3;
ds4279 0:374a8a31f262 199
ds4279 0:374a8a31f262 200 //////////////////////////////////////
ds4279 0:374a8a31f262 201 ///auth set wlan auth <value>
ds4279 0:374a8a31f262 202 sprintf(cmd, "set w a %d\r", auth);
ds4279 0:374a8a31f262 203 sendCommand(cmd, "AOK");
ds4279 0:374a8a31f262 204
ds4279 0:374a8a31f262 205
ds4279 0:374a8a31f262 206 wait_ms(10);
ds4279 0:374a8a31f262 207 //////////////////////////////////////
ds4279 0:374a8a31f262 208 ///Pass Phrase
ds4279 0:374a8a31f262 209 sprintf(cmd, "set w p %s\r", phrase); //Returns 450ms
ds4279 0:374a8a31f262 210 sendCommand(cmd, "AOK");
ds4279 0:374a8a31f262 211
ds4279 0:374a8a31f262 212
ds4279 0:374a8a31f262 213
ds4279 0:374a8a31f262 214
ds4279 0:374a8a31f262 215 //key step
ds4279 0:374a8a31f262 216 /* if (state.sec != NONE)
ds4279 0:374a8a31f262 217 {
ds4279 0:374a8a31f262 218 myDebug.printf("state.sec != NONE\n");
ds4279 0:374a8a31f262 219 //Create command string
ds4279 0:374a8a31f262 220 if (state.sec == WPA)
ds4279 0:374a8a31f262 221 {
ds4279 0:374a8a31f262 222 ///////////////////////////////////////////
ds4279 0:374a8a31f262 223 ///set wlan phrase <string>
ds4279 0:374a8a31f262 224 myDebug.printf("set WPA\n");
ds4279 0:374a8a31f262 225 sprintf(cmd, "set w p %s\r", phrase);
ds4279 0:374a8a31f262 226 }
ds4279 0:374a8a31f262 227 else if (state.sec == WEP_128)
ds4279 0:374a8a31f262 228 {
ds4279 0:374a8a31f262 229 myDebug.printf("set WEP\n");
ds4279 0:374a8a31f262 230 sprintf(cmd, "set w k %s\r", phrase);
ds4279 0:374a8a31f262 231 }
ds4279 0:374a8a31f262 232
ds4279 0:374a8a31f262 233 if (!sendCommand(cmd, "AOK")) //returns 500ms
ds4279 0:374a8a31f262 234 {
ds4279 0:374a8a31f262 235 }
ds4279 0:374a8a31f262 236 }
ds4279 0:374a8a31f262 237 */
ds4279 0:374a8a31f262 238
ds4279 0:374a8a31f262 239 wait_ms(10);
ds4279 0:374a8a31f262 240
ds4279 0:374a8a31f262 241 if(state.dhcp)
ds4279 0:374a8a31f262 242 {
ds4279 0:374a8a31f262 243 state.dhcp = true;
ds4279 0:374a8a31f262 244 myDebug.printf("Connect DHCP...\n");
ds4279 0:374a8a31f262 245 sendCommand("set ip dhcp 1", "AOK",NULL,1000);
ds4279 0:374a8a31f262 246 wait_ms(10);
ds4279 0:374a8a31f262 247 sprintf(cmd, "join %s\r", ssid); //join the SSID network
ds4279 0:374a8a31f262 248
ds4279 0:374a8a31f262 249 while(retries)
ds4279 0:374a8a31f262 250 {
ds4279 0:374a8a31f262 251 if( sendCommand(cmd, "DHCP=ON",NULL,10000,true) )
ds4279 0:374a8a31f262 252 {
ds4279 0:374a8a31f262 253 exitCmdMode(); //exit command mode
ds4279 0:374a8a31f262 254 return true;
ds4279 0:374a8a31f262 255 }
ds4279 0:374a8a31f262 256 else{
ds4279 0:374a8a31f262 257 retries--;
ds4279 0:374a8a31f262 258 //myDebug.printf("Trying to JOIN...\n");
ds4279 0:374a8a31f262 259 }
ds4279 0:374a8a31f262 260
ds4279 0:374a8a31f262 261 };
ds4279 0:374a8a31f262 262 }
ds4279 0:374a8a31f262 263 else //setup for static IP
ds4279 0:374a8a31f262 264 {
ds4279 0:374a8a31f262 265 //myDebug.printf("Connect Static IP\n");
ds4279 0:374a8a31f262 266 sprintf(cmd, "set ip a %s\r", this->staticIP);
ds4279 0:374a8a31f262 267 sendCommand(cmd, "AOK",NULL,2000);
ds4279 0:374a8a31f262 268
ds4279 0:374a8a31f262 269 sendCommand("set ip dhcp 0", "AOK",NULL,2000);
ds4279 0:374a8a31f262 270
ds4279 0:374a8a31f262 271 state.dhcp = false;
ds4279 0:374a8a31f262 272 retries = 3;
ds4279 0:374a8a31f262 273
ds4279 0:374a8a31f262 274 sprintf(cmd, "join %s\r", ssid); //join the SSID network
ds4279 0:374a8a31f262 275 while(retries)
ds4279 0:374a8a31f262 276 {
ds4279 0:374a8a31f262 277 if( sendCommand(cmd, "Associated",NULL,10000,true) )
ds4279 0:374a8a31f262 278 {
ds4279 0:374a8a31f262 279 exitCmdMode(); //leave command mode
ds4279 0:374a8a31f262 280 return true;
ds4279 0:374a8a31f262 281 }
ds4279 0:374a8a31f262 282 else{
ds4279 0:374a8a31f262 283 retries--;
ds4279 0:374a8a31f262 284 myDebug.printf("Trying to JOIN Static...\n");
ds4279 0:374a8a31f262 285 }
ds4279 0:374a8a31f262 286 };
ds4279 0:374a8a31f262 287 }
ds4279 0:374a8a31f262 288
ds4279 0:374a8a31f262 289 exitCmdMode(); //leave command mode
ds4279 0:374a8a31f262 290
ds4279 0:374a8a31f262 291 return false; //could not connect
ds4279 0:374a8a31f262 292 }
ds4279 0:374a8a31f262 293 /////////////////////////////////////////
ds4279 0:374a8a31f262 294 //::join()
ds4279 0:374a8a31f262 295 //
ds4279 0:374a8a31f262 296 //The SSID and pass pharse are saved
ds4279 0:374a8a31f262 297 //when the constructor is called
ds4279 0:374a8a31f262 298 ////////////////////////////////////////
ds4279 0:374a8a31f262 299 bool Wifly::join()
ds4279 0:374a8a31f262 300 {
ds4279 0:374a8a31f262 301 char cmd[200];
ds4279 0:374a8a31f262 302 unsigned char retries = 3;
ds4279 0:374a8a31f262 303
ds4279 0:374a8a31f262 304 //////////////////////////////////////
ds4279 0:374a8a31f262 305 ///auth set wlan auth <value>
ds4279 0:374a8a31f262 306 sprintf(cmd, "set w a %d\r", state.sec);
ds4279 0:374a8a31f262 307 sendCommand(cmd, "AOK");
ds4279 0:374a8a31f262 308 wait_ms(10);
ds4279 0:374a8a31f262 309
ds4279 0:374a8a31f262 310 sprintf(cmd, "set w p %s\r", this->phrase); //Returns 450ms
ds4279 0:374a8a31f262 311 myDebug.printf(cmd);
ds4279 0:374a8a31f262 312 sendCommand(cmd, "AOK");
ds4279 0:374a8a31f262 313 wait_ms(10);
ds4279 0:374a8a31f262 314 sendCommand("set ip dhcp 1", "AOK");
ds4279 0:374a8a31f262 315 wait_ms(10);
ds4279 0:374a8a31f262 316 //key step
ds4279 0:374a8a31f262 317 /* if (state.sec != NONE)
ds4279 0:374a8a31f262 318 {
ds4279 0:374a8a31f262 319 myDebug.printf("state.sec != NONE\n");
ds4279 0:374a8a31f262 320 //Create command string
ds4279 0:374a8a31f262 321 if (state.sec == WPA)
ds4279 0:374a8a31f262 322 {
ds4279 0:374a8a31f262 323 ///////////////////////////////////////////
ds4279 0:374a8a31f262 324 ///set wlan phrase <string>
ds4279 0:374a8a31f262 325 myDebug.printf("set WPA\n");
ds4279 0:374a8a31f262 326 sprintf(cmd, "set w p %s\r", phrase);
ds4279 0:374a8a31f262 327 }
ds4279 0:374a8a31f262 328 else if (state.sec == WEP_128)
ds4279 0:374a8a31f262 329 {
ds4279 0:374a8a31f262 330 myDebug.printf("set WEP\n");
ds4279 0:374a8a31f262 331 sprintf(cmd, "set w k %s\r", phrase);
ds4279 0:374a8a31f262 332 }
ds4279 0:374a8a31f262 333
ds4279 0:374a8a31f262 334 if (!sendCommand(cmd, "AOK")) //returns 500ms
ds4279 0:374a8a31f262 335 {
ds4279 0:374a8a31f262 336 }
ds4279 0:374a8a31f262 337 }
ds4279 0:374a8a31f262 338 */
ds4279 0:374a8a31f262 339
ds4279 0:374a8a31f262 340 wait_ms(10);
ds4279 0:374a8a31f262 341
ds4279 0:374a8a31f262 342 if(state.dhcp)
ds4279 0:374a8a31f262 343 {
ds4279 0:374a8a31f262 344 myDebug.printf("Connect DHCP...\n");
ds4279 0:374a8a31f262 345 sprintf(cmd, "join %s\r", this->ssid);
ds4279 0:374a8a31f262 346
ds4279 0:374a8a31f262 347 state.dhcp = true;
ds4279 0:374a8a31f262 348 while(retries)
ds4279 0:374a8a31f262 349 {
ds4279 0:374a8a31f262 350 if( sendCommand(cmd, "DHCP=ON",NULL,10000,true) )
ds4279 0:374a8a31f262 351 {
ds4279 0:374a8a31f262 352 exitCmdMode(); //leave command mode
ds4279 0:374a8a31f262 353 return true;
ds4279 0:374a8a31f262 354 }
ds4279 0:374a8a31f262 355 else{
ds4279 0:374a8a31f262 356 retries--;
ds4279 0:374a8a31f262 357 myDebug.printf("Trying to JOIN...\n");
ds4279 0:374a8a31f262 358 }
ds4279 0:374a8a31f262 359
ds4279 0:374a8a31f262 360 };
ds4279 0:374a8a31f262 361 }
ds4279 0:374a8a31f262 362 else //setup for static IP
ds4279 0:374a8a31f262 363 {
ds4279 0:374a8a31f262 364 //myDebug.printf("Connect Static IP\n");
ds4279 0:374a8a31f262 365 sprintf(cmd, "set ip a %s\r", this->staticIP);
ds4279 0:374a8a31f262 366 sendCommand(cmd, "AOK",NULL,2000);
ds4279 0:374a8a31f262 367
ds4279 0:374a8a31f262 368 sendCommand("set ip dhcp 0", "AOK",NULL,2000);
ds4279 0:374a8a31f262 369
ds4279 0:374a8a31f262 370 state.dhcp = false;
ds4279 0:374a8a31f262 371 retries = 3;
ds4279 0:374a8a31f262 372
ds4279 0:374a8a31f262 373 sprintf(cmd, "join %s\r", ssid); //join the SSID network
ds4279 0:374a8a31f262 374 while(retries)
ds4279 0:374a8a31f262 375 {
ds4279 0:374a8a31f262 376 if( sendCommand(cmd, "Associated",NULL,10000,true) )
ds4279 0:374a8a31f262 377 {
ds4279 0:374a8a31f262 378 exitCmdMode(); //leave command mode
ds4279 0:374a8a31f262 379 return true;
ds4279 0:374a8a31f262 380 }
ds4279 0:374a8a31f262 381 else{
ds4279 0:374a8a31f262 382 retries--;
ds4279 0:374a8a31f262 383 myDebug.printf("Trying to JOIN Static...\n");
ds4279 0:374a8a31f262 384 }
ds4279 0:374a8a31f262 385 };
ds4279 0:374a8a31f262 386 }
ds4279 0:374a8a31f262 387
ds4279 0:374a8a31f262 388 exitCmdMode(); //leave command mode
ds4279 0:374a8a31f262 389
ds4279 0:374a8a31f262 390 return false;
ds4279 0:374a8a31f262 391 }
ds4279 0:374a8a31f262 392
ds4279 0:374a8a31f262 393 void Wifly::enableDHCP(bool DHCP_STATIC)
ds4279 0:374a8a31f262 394 {
ds4279 0:374a8a31f262 395 state.dhcp = DHCP_STATIC;
ds4279 0:374a8a31f262 396 }
ds4279 0:374a8a31f262 397
ds4279 0:374a8a31f262 398 void Wifly::setStaticIP(const char *staticIP)
ds4279 0:374a8a31f262 399 {
ds4279 0:374a8a31f262 400 char buffer[100];
ds4279 0:374a8a31f262 401 strcpy(this->staticIP,staticIP);
ds4279 0:374a8a31f262 402 sprintf(buffer,"IPset---: %s\n",this->staticIP);
ds4279 0:374a8a31f262 403 myDebug.printf(buffer);
ds4279 0:374a8a31f262 404
ds4279 0:374a8a31f262 405 }
ds4279 0:374a8a31f262 406
ds4279 0:374a8a31f262 407 /////////////////////////////////////////
ds4279 0:374a8a31f262 408 //::setBaudRate()
ds4279 0:374a8a31f262 409 //
ds4279 0:374a8a31f262 410 //Baudrate can be change on the fly
ds4279 0:374a8a31f262 411 //Once the command is sent the RN-171
ds4279 0:374a8a31f262 412 //will exit the command mode
ds4279 0:374a8a31f262 413 bool Wifly::setBaudRate(int baud)
ds4279 0:374a8a31f262 414 {
ds4279 0:374a8a31f262 415 char cmd[60];
ds4279 0:374a8a31f262 416 sprintf(cmd, "set u b %i\r", baud);
ds4279 0:374a8a31f262 417 if ( !sendCommand(cmd, "AOK") )
ds4279 0:374a8a31f262 418 {
ds4279 0:374a8a31f262 419 return false;
ds4279 0:374a8a31f262 420 }
ds4279 0:374a8a31f262 421
ds4279 0:374a8a31f262 422 //Automatically exits command mode
ds4279 0:374a8a31f262 423 state.cmd_mode = false;
ds4279 0:374a8a31f262 424 return true;
ds4279 0:374a8a31f262 425 }
ds4279 0:374a8a31f262 426
ds4279 0:374a8a31f262 427 bool Wifly::setProtocol(Protocol p)
ds4279 0:374a8a31f262 428 {
ds4279 0:374a8a31f262 429 // use udp auto pairing
ds4279 0:374a8a31f262 430 char cmd[20];
ds4279 0:374a8a31f262 431 sprintf(cmd, "set i p %d\r", p);
ds4279 0:374a8a31f262 432 if (!sendCommand(cmd, "AOK"))
ds4279 0:374a8a31f262 433 return false;
ds4279 0:374a8a31f262 434
ds4279 0:374a8a31f262 435 switch(p) {
ds4279 0:374a8a31f262 436 case TCP:
ds4279 0:374a8a31f262 437 // set ip flags: tcp retry enabled
ds4279 0:374a8a31f262 438 if (!sendCommand("set i f 0x07\r", "AOK"))
ds4279 0:374a8a31f262 439 return false;
ds4279 0:374a8a31f262 440 break;
ds4279 0:374a8a31f262 441 case UDP:
ds4279 0:374a8a31f262 442 // set ip flags: udp auto pairing enabled
ds4279 0:374a8a31f262 443 if (!sendCommand("set i h 0.0.0.0\r", "AOK"))
ds4279 0:374a8a31f262 444 return false;
ds4279 0:374a8a31f262 445 if (!sendCommand("set i f 0x40\r", "AOK"))
ds4279 0:374a8a31f262 446 return false;
ds4279 0:374a8a31f262 447 break;
ds4279 0:374a8a31f262 448 }
ds4279 0:374a8a31f262 449 state.proto = p;
ds4279 0:374a8a31f262 450 return true;
ds4279 0:374a8a31f262 451 }
ds4279 0:374a8a31f262 452
ds4279 0:374a8a31f262 453 char * Wifly::getStringSecurity()
ds4279 0:374a8a31f262 454 {
ds4279 0:374a8a31f262 455 switch(state.sec) {
ds4279 0:374a8a31f262 456 case NONE:
ds4279 0:374a8a31f262 457 return "NONE";
ds4279 0:374a8a31f262 458 case WEP_128:
ds4279 0:374a8a31f262 459 return "WEP_128";
ds4279 0:374a8a31f262 460 case WPA:
ds4279 0:374a8a31f262 461 return "WPA";
ds4279 0:374a8a31f262 462 }
ds4279 0:374a8a31f262 463 return "UNKNOWN";
ds4279 0:374a8a31f262 464 }
ds4279 0:374a8a31f262 465
ds4279 0:374a8a31f262 466 bool Wifly::connect(const char * host, int port)
ds4279 0:374a8a31f262 467 {
ds4279 0:374a8a31f262 468 char rcv[20];
ds4279 0:374a8a31f262 469 char cmd[20];
ds4279 0:374a8a31f262 470
ds4279 0:374a8a31f262 471 // try to open
ds4279 0:374a8a31f262 472 sprintf(cmd, "open %s %d\r", host, port);
ds4279 0:374a8a31f262 473 if (sendCommand(cmd, "OPEN", NULL, 10000)) {
ds4279 0:374a8a31f262 474 state.tcp = true;
ds4279 0:374a8a31f262 475 state.cmd_mode = false;
ds4279 0:374a8a31f262 476 return true;
ds4279 0:374a8a31f262 477 }
ds4279 0:374a8a31f262 478
ds4279 0:374a8a31f262 479 // if failed, retry and parse the response
ds4279 0:374a8a31f262 480 if (sendCommand(cmd, NULL, rcv, 5000)) {
ds4279 0:374a8a31f262 481 if (strstr(rcv, "OPEN") == NULL) {
ds4279 0:374a8a31f262 482 if (strstr(rcv, "Connected") != NULL) {
ds4279 0:374a8a31f262 483 wait(0.25);
ds4279 0:374a8a31f262 484 if (!sendCommand("close\r", "CLOS"))
ds4279 0:374a8a31f262 485 return false;
ds4279 0:374a8a31f262 486 wait(0.25);
ds4279 0:374a8a31f262 487 if (!sendCommand(cmd, "OPEN", NULL, 10000))
ds4279 0:374a8a31f262 488 return false;
ds4279 0:374a8a31f262 489 } else {
ds4279 0:374a8a31f262 490 return false;
ds4279 0:374a8a31f262 491 }
ds4279 0:374a8a31f262 492 }
ds4279 0:374a8a31f262 493 } else {
ds4279 0:374a8a31f262 494 return false;
ds4279 0:374a8a31f262 495 }
ds4279 0:374a8a31f262 496
ds4279 0:374a8a31f262 497 state.tcp = true;
ds4279 0:374a8a31f262 498 state.cmd_mode = false;
ds4279 0:374a8a31f262 499
ds4279 0:374a8a31f262 500 return true;
ds4279 0:374a8a31f262 501 }
ds4279 0:374a8a31f262 502
ds4279 0:374a8a31f262 503
ds4279 0:374a8a31f262 504 bool Wifly::gethostbyname(const char * host, char * ip)
ds4279 0:374a8a31f262 505 {
ds4279 0:374a8a31f262 506 string h = host;
ds4279 0:374a8a31f262 507 char cmd[30], rcv[100];
ds4279 0:374a8a31f262 508 int l = 0;
ds4279 0:374a8a31f262 509 char * point;
ds4279 0:374a8a31f262 510 int nb_digits = 0;
ds4279 0:374a8a31f262 511
ds4279 0:374a8a31f262 512 // no dns needed
ds4279 0:374a8a31f262 513 int pos = h.find(".");
ds4279 0:374a8a31f262 514 if (pos != string::npos) {
ds4279 0:374a8a31f262 515 string sub = h.substr(0, h.find("."));
ds4279 0:374a8a31f262 516 nb_digits = atoi(sub.c_str());
ds4279 0:374a8a31f262 517 }
ds4279 0:374a8a31f262 518 //printf("substrL %s\r\n", sub.c_str());
ds4279 0:374a8a31f262 519 if (count(h.begin(), h.end(), '.') == 3 && nb_digits > 0) {
ds4279 0:374a8a31f262 520 strcpy(ip, host);
ds4279 0:374a8a31f262 521 }
ds4279 0:374a8a31f262 522 // dns needed
ds4279 0:374a8a31f262 523 else {
ds4279 0:374a8a31f262 524 nb_digits = 0;
ds4279 0:374a8a31f262 525 sprintf(cmd, "lookup %s\r", host);
ds4279 0:374a8a31f262 526 if (!sendCommand(cmd, NULL, rcv))
ds4279 0:374a8a31f262 527 return false;
ds4279 0:374a8a31f262 528
ds4279 0:374a8a31f262 529 // look for the ip address
ds4279 0:374a8a31f262 530 char * begin = strstr(rcv, "=") + 1;
ds4279 0:374a8a31f262 531 for (int i = 0; i < 3; i++) {
ds4279 0:374a8a31f262 532 point = strstr(begin + l, ".");
ds4279 0:374a8a31f262 533 DBG("str: %s", begin + l);
ds4279 0:374a8a31f262 534 l += point - (begin + l) + 1;
ds4279 0:374a8a31f262 535 }
ds4279 0:374a8a31f262 536 DBG("str: %s", begin + l);
ds4279 0:374a8a31f262 537 while(*(begin + l + nb_digits) >= '0' && *(begin + l + nb_digits) <= '9') {
ds4279 0:374a8a31f262 538 DBG("digit: %c", *(begin + l + nb_digits));
ds4279 0:374a8a31f262 539 nb_digits++;
ds4279 0:374a8a31f262 540 }
ds4279 0:374a8a31f262 541 memcpy(ip, begin, l + nb_digits);
ds4279 0:374a8a31f262 542 ip[l+nb_digits] = 0;
ds4279 0:374a8a31f262 543 DBG("ip from dns: %s", ip);
ds4279 0:374a8a31f262 544 }
ds4279 0:374a8a31f262 545 return true;
ds4279 0:374a8a31f262 546 }
ds4279 0:374a8a31f262 547
ds4279 0:374a8a31f262 548
ds4279 0:374a8a31f262 549 void Wifly::flush()
ds4279 0:374a8a31f262 550 {
ds4279 0:374a8a31f262 551 while (wifiSer.readable())
ds4279 0:374a8a31f262 552 wifiSer.getc();
ds4279 0:374a8a31f262 553 }
ds4279 0:374a8a31f262 554
ds4279 0:374a8a31f262 555 ////////////////////////////////////////////////////////////
ds4279 0:374a8a31f262 556 //sendCommand()
ds4279 0:374a8a31f262 557 //
ds4279 0:374a8a31f262 558 //
ds4279 0:374a8a31f262 559 //
ds4279 0:374a8a31f262 560 //
ds4279 0:374a8a31f262 561 //
ds4279 0:374a8a31f262 562 //
ds4279 0:374a8a31f262 563 ////////////////////////////////////////////////////////////
ds4279 0:374a8a31f262 564 bool Wifly::sendCommand(const char * cmd, const char * ack, char * res, int timeout, bool DHCPconn)
ds4279 0:374a8a31f262 565 {
ds4279 0:374a8a31f262 566 if (!state.cmd_mode) {
ds4279 0:374a8a31f262 567 cmdMode();
ds4279 0:374a8a31f262 568 }
ds4279 0:374a8a31f262 569 // Expected res strRet
ds4279 0:374a8a31f262 570 switch( this->sendString(cmd, strlen(cmd), ack, res, timeout, DHCPconn) )
ds4279 0:374a8a31f262 571 {
ds4279 0:374a8a31f262 572 case 0:
ds4279 0:374a8a31f262 573 //myDebug.printf("...OK\n");
ds4279 0:374a8a31f262 574 return true;
ds4279 0:374a8a31f262 575
ds4279 0:374a8a31f262 576 case 1:
ds4279 0:374a8a31f262 577 // myDebug.printf("Time out\n"); //do a retry
ds4279 0:374a8a31f262 578 // exitCmdMode();
ds4279 0:374a8a31f262 579 return false;
ds4279 0:374a8a31f262 580 case 2: //DHCP connected
ds4279 0:374a8a31f262 581 state.associated = true;
ds4279 0:374a8a31f262 582 myDebug.printf("DHCP CONNECTED\n");
ds4279 0:374a8a31f262 583 exitCmdMode();
ds4279 0:374a8a31f262 584 return true;
ds4279 0:374a8a31f262 585
ds4279 0:374a8a31f262 586 case 3:
ds4279 0:374a8a31f262 587 myDebug.printf("DHCP Failed\n"); //do a retry
ds4279 0:374a8a31f262 588 // exitCmdMode();
ds4279 0:374a8a31f262 589 return false;
ds4279 0:374a8a31f262 590
ds4279 0:374a8a31f262 591 default:
ds4279 0:374a8a31f262 592 exitCmdMode();
ds4279 0:374a8a31f262 593 return false;
ds4279 0:374a8a31f262 594
ds4279 0:374a8a31f262 595
ds4279 0:374a8a31f262 596 };
ds4279 0:374a8a31f262 597 return false;
ds4279 0:374a8a31f262 598 }
ds4279 0:374a8a31f262 599
ds4279 0:374a8a31f262 600 bool Wifly::cmdMode()
ds4279 0:374a8a31f262 601 {
ds4279 0:374a8a31f262 602 // if already in cmd mode, return
ds4279 0:374a8a31f262 603 if (state.cmd_mode)
ds4279 0:374a8a31f262 604 return true;
ds4279 0:374a8a31f262 605
ds4279 0:374a8a31f262 606 if ( this->sendString("$$$", 3, "CMD") == -1)
ds4279 0:374a8a31f262 607 {
ds4279 0:374a8a31f262 608 ERR("cannot enter in cmd mode\r\n");
ds4279 0:374a8a31f262 609 exitCmdMode();
ds4279 0:374a8a31f262 610 return false;
ds4279 0:374a8a31f262 611 }
ds4279 0:374a8a31f262 612 state.cmd_mode = true;
ds4279 0:374a8a31f262 613 return true;
ds4279 0:374a8a31f262 614 }
ds4279 0:374a8a31f262 615
ds4279 0:374a8a31f262 616 bool Wifly::disconnect()
ds4279 0:374a8a31f262 617 {
ds4279 0:374a8a31f262 618 // if already disconnected, return
ds4279 0:374a8a31f262 619 if (!state.associated)
ds4279 0:374a8a31f262 620 return true;
ds4279 0:374a8a31f262 621
ds4279 0:374a8a31f262 622 if (!sendCommand("leave\r", "DeAuth"))
ds4279 0:374a8a31f262 623 return false;
ds4279 0:374a8a31f262 624 exitCmdMode();
ds4279 0:374a8a31f262 625
ds4279 0:374a8a31f262 626 state.associated = false;
ds4279 0:374a8a31f262 627 return true;
ds4279 0:374a8a31f262 628
ds4279 0:374a8a31f262 629 }
ds4279 0:374a8a31f262 630
ds4279 0:374a8a31f262 631 bool Wifly::is_connected()
ds4279 0:374a8a31f262 632 {
ds4279 0:374a8a31f262 633 return (tcp_status.read() == 1) ? true : false;
ds4279 0:374a8a31f262 634 }
ds4279 0:374a8a31f262 635
ds4279 0:374a8a31f262 636
ds4279 0:374a8a31f262 637 bool Wifly::reset(bool howIreset)
ds4279 0:374a8a31f262 638 {
ds4279 0:374a8a31f262 639 if(howIreset)
ds4279 0:374a8a31f262 640 {
ds4279 0:374a8a31f262 641 reset_pin = 0;
ds4279 0:374a8a31f262 642 wait(0.2);
ds4279 0:374a8a31f262 643 reset_pin = 1;
ds4279 0:374a8a31f262 644 wait(0.2);
ds4279 0:374a8a31f262 645 }
ds4279 0:374a8a31f262 646 else
ds4279 0:374a8a31f262 647 {
ds4279 0:374a8a31f262 648 // sendToDisplay(RS485Cmd,0,"software Reset\n");
ds4279 0:374a8a31f262 649 sendCommand("factory R\r", "Defaults");
ds4279 0:374a8a31f262 650 wait_ms(10);
ds4279 0:374a8a31f262 651 sendCommand("save\r", "Stor");
ds4279 0:374a8a31f262 652
ds4279 0:374a8a31f262 653 return true;
ds4279 0:374a8a31f262 654 }
ds4279 0:374a8a31f262 655
ds4279 0:374a8a31f262 656 return true;
ds4279 0:374a8a31f262 657
ds4279 0:374a8a31f262 658 }
ds4279 0:374a8a31f262 659
ds4279 0:374a8a31f262 660 bool Wifly::reboot()
ds4279 0:374a8a31f262 661 {
ds4279 0:374a8a31f262 662 // if already in cmd mode, return
ds4279 0:374a8a31f262 663 if ( !sendCommand("reboot\r", "Reboot",NULL,5000,false) )
ds4279 0:374a8a31f262 664 return false;
ds4279 0:374a8a31f262 665
ds4279 0:374a8a31f262 666 wait(0.3);
ds4279 0:374a8a31f262 667
ds4279 0:374a8a31f262 668 state.cmd_mode = false;
ds4279 0:374a8a31f262 669 return true;
ds4279 0:374a8a31f262 670 }
ds4279 0:374a8a31f262 671
ds4279 0:374a8a31f262 672
ds4279 0:374a8a31f262 673 /////////////////////////////////////////
ds4279 0:374a8a31f262 674 //::close()
ds4279 0:374a8a31f262 675 //
ds4279 0:374a8a31f262 676 //disconnect a TCP connection.
ds4279 0:374a8a31f262 677 ////////////////////////////////////////
ds4279 0:374a8a31f262 678 bool Wifly::close()
ds4279 0:374a8a31f262 679 {
ds4279 0:374a8a31f262 680 // if not connected, return
ds4279 0:374a8a31f262 681 if (!state.tcp)
ds4279 0:374a8a31f262 682 return true;
ds4279 0:374a8a31f262 683
ds4279 0:374a8a31f262 684 wait(0.25);
ds4279 0:374a8a31f262 685 if (!sendCommand("close\r", "CLOS"))
ds4279 0:374a8a31f262 686 return false;
ds4279 0:374a8a31f262 687 exitCmdMode();
ds4279 0:374a8a31f262 688
ds4279 0:374a8a31f262 689 state.tcp = false;
ds4279 0:374a8a31f262 690 return true;
ds4279 0:374a8a31f262 691 }
ds4279 0:374a8a31f262 692
ds4279 0:374a8a31f262 693 /////////////////////////////////////////
ds4279 0:374a8a31f262 694 //::putc()
ds4279 0:374a8a31f262 695 //
ds4279 0:374a8a31f262 696 //Wrapper for wiFiSerial putc()
ds4279 0:374a8a31f262 697 int Wifly::putc(char c)
ds4279 0:374a8a31f262 698 {
ds4279 0:374a8a31f262 699 return wifiSer.putc(c);
ds4279 0:374a8a31f262 700 }
ds4279 0:374a8a31f262 701
ds4279 0:374a8a31f262 702 /////////////////////////////////////////
ds4279 0:374a8a31f262 703 //exitCmdMode()
ds4279 0:374a8a31f262 704 //
ds4279 0:374a8a31f262 705 //Tell WiFi module to exit command mode
ds4279 0:374a8a31f262 706 bool Wifly::exitCmdMode()
ds4279 0:374a8a31f262 707 {
ds4279 0:374a8a31f262 708 flush();
ds4279 0:374a8a31f262 709 //////////////////////////
ds4279 0:374a8a31f262 710 ///We flush the buffer
ds4279 0:374a8a31f262 711 while (wifiSer.readable())
ds4279 0:374a8a31f262 712 wifiSer.getc();
ds4279 0:374a8a31f262 713
ds4279 0:374a8a31f262 714 if (!state.cmd_mode)
ds4279 0:374a8a31f262 715 return true;
ds4279 0:374a8a31f262 716
ds4279 0:374a8a31f262 717 if (!sendCommand("exit\r", "EXIT"))
ds4279 0:374a8a31f262 718 return false;
ds4279 0:374a8a31f262 719
ds4279 0:374a8a31f262 720 state.cmd_mode = false;
ds4279 0:374a8a31f262 721 //////////////////////////
ds4279 0:374a8a31f262 722 ///We flush the buffer
ds4279 0:374a8a31f262 723 while (wifiSer.readable())
ds4279 0:374a8a31f262 724 wifiSer.getc();
ds4279 0:374a8a31f262 725
ds4279 0:374a8a31f262 726 return true;
ds4279 0:374a8a31f262 727 }
ds4279 0:374a8a31f262 728
ds4279 0:374a8a31f262 729 /////////////////////////////////////////
ds4279 0:374a8a31f262 730 //readable()
ds4279 0:374a8a31f262 731 //
ds4279 0:374a8a31f262 732 //BufferedSerial buffer has data?
ds4279 0:374a8a31f262 733 int Wifly::readable()
ds4279 0:374a8a31f262 734 {
ds4279 0:374a8a31f262 735 return wifiSer.readable(); //// note: look if things are in the buffer;
ds4279 0:374a8a31f262 736 }
ds4279 0:374a8a31f262 737
ds4279 0:374a8a31f262 738
ds4279 0:374a8a31f262 739 /////////////////////////////////////////
ds4279 0:374a8a31f262 740 //writeable()
ds4279 0:374a8a31f262 741 //
ds4279 0:374a8a31f262 742 //BufferedSerial This call will always
ds4279 0:374a8a31f262 743 //return true. the cicular queue will
ds4279 0:374a8a31f262 744 //always allow data
ds4279 0:374a8a31f262 745 int Wifly::writeable()
ds4279 0:374a8a31f262 746 {
ds4279 0:374a8a31f262 747 return wifiSer.writeable();
ds4279 0:374a8a31f262 748 }
ds4279 0:374a8a31f262 749
ds4279 0:374a8a31f262 750 char Wifly::getc()
ds4279 0:374a8a31f262 751 {
ds4279 0:374a8a31f262 752 char c;
ds4279 0:374a8a31f262 753
ds4279 0:374a8a31f262 754 c = wifiSer.getc();
ds4279 0:374a8a31f262 755
ds4279 0:374a8a31f262 756 // while (!buf_wifly.available());
ds4279 0:374a8a31f262 757 // buf_wifly.dequeue(&c);
ds4279 0:374a8a31f262 758 return c;
ds4279 0:374a8a31f262 759 }
ds4279 0:374a8a31f262 760
ds4279 0:374a8a31f262 761 /////////////////////////////////////////////////////
ds4279 0:374a8a31f262 762 ///These two functions are no longer needed since
ds4279 0:374a8a31f262 763 ///The BufferedSerial class takes care of this
ds4279 0:374a8a31f262 764 void Wifly::handler_rx(void)
ds4279 0:374a8a31f262 765 {
ds4279 0:374a8a31f262 766 //read characters
ds4279 0:374a8a31f262 767 // while (wifiSer.readable())
ds4279 0:374a8a31f262 768 // buf_wifly.queue(wifiSer.getc());
ds4279 0:374a8a31f262 769 }
ds4279 0:374a8a31f262 770
ds4279 0:374a8a31f262 771 void Wifly::attach_rx(bool callback)
ds4279 0:374a8a31f262 772 {
ds4279 0:374a8a31f262 773 /*
ds4279 0:374a8a31f262 774 if (!callback)
ds4279 0:374a8a31f262 775 wifiSer.attach(NULL);
ds4279 0:374a8a31f262 776 else
ds4279 0:374a8a31f262 777 wifiSer.attach(this, &Wifly::handler_rx);*/
ds4279 0:374a8a31f262 778 }
ds4279 0:374a8a31f262 779
ds4279 0:374a8a31f262 780 ////////////////////////////////////////////////////////////
ds4279 0:374a8a31f262 781 //sendString()
ds4279 0:374a8a31f262 782 //
ds4279 0:374a8a31f262 783 //
ds4279 0:374a8a31f262 784 //
ds4279 0:374a8a31f262 785 //
ds4279 0:374a8a31f262 786 //
ds4279 0:374a8a31f262 787 //
ds4279 0:374a8a31f262 788 //////////////////////////////////////////////////////////`
ds4279 0:374a8a31f262 789 int Wifly::sendString(const char * str, int len, const char * ACK, char * res, int timeout,bool DHCPconn)
ds4279 0:374a8a31f262 790 {
ds4279 0:374a8a31f262 791 char read;
ds4279 0:374a8a31f262 792 size_t found = string::npos; //same as -1 substring not found else position of string
ds4279 0:374a8a31f262 793 size_t DHCPnotCONN = string::npos;
ds4279 0:374a8a31f262 794 string AUTH_ERR = "AUTH-ERR"; //Error string on DHCP invalid SSID or PAss Phrase
ds4279 0:374a8a31f262 795 string FAILED = "NONE FAILED";
ds4279 0:374a8a31f262 796 string DHCP_CONN = "DHCP=ON";
ds4279 0:374a8a31f262 797 string checking;
ds4279 0:374a8a31f262 798 Timer tmr;
ds4279 0:374a8a31f262 799 int result = 0;
ds4279 0:374a8a31f262 800 char buffer[280];
ds4279 0:374a8a31f262 801 int pos = 0;
ds4279 0:374a8a31f262 802 int DHCPconnect = 0;
ds4279 0:374a8a31f262 803
ds4279 0:374a8a31f262 804 // DBG("will send: %s\r\n","checka: %s\r\n",);
ds4279 0:374a8a31f262 805
ds4279 0:374a8a31f262 806 // attach_rx(false);
ds4279 0:374a8a31f262 807 //sprintf(buffer,"will send: %s\r\n", str);
ds4279 0:374a8a31f262 808 //myDebug.printf(buffer);
ds4279 0:374a8a31f262 809
ds4279 0:374a8a31f262 810 //We flush the buffer
ds4279 0:374a8a31f262 811 while (wifiSer.readable())
ds4279 0:374a8a31f262 812 wifiSer.getc();
ds4279 0:374a8a31f262 813
ds4279 0:374a8a31f262 814
ds4279 0:374a8a31f262 815 //////////////////////////////////////////
ds4279 0:374a8a31f262 816 ///This is probably a get command
ds4279 0:374a8a31f262 817 if (!ACK || !strcmp(ACK, "NO"))
ds4279 0:374a8a31f262 818 {
ds4279 0:374a8a31f262 819 for (int i = 0; i < len; i++)
ds4279 0:374a8a31f262 820 {
ds4279 0:374a8a31f262 821 result = (putc(str[i]) == str[i]) ? result + 1 : result;
ds4279 0:374a8a31f262 822 }
ds4279 0:374a8a31f262 823 }
ds4279 0:374a8a31f262 824 else
ds4279 0:374a8a31f262 825 {
ds4279 0:374a8a31f262 826 // myDebug.printf("here1\n");
ds4279 0:374a8a31f262 827 //We flush the buffer
ds4279 0:374a8a31f262 828 while (wifiSer.readable())
ds4279 0:374a8a31f262 829 wifiSer.getc();
ds4279 0:374a8a31f262 830
ds4279 0:374a8a31f262 831 tmr.start();
ds4279 0:374a8a31f262 832 ////////////////////////////////////////
ds4279 0:374a8a31f262 833 ///Send command to wiFly transciver
ds4279 0:374a8a31f262 834 for (int i = 0; i < len; i++)
ds4279 0:374a8a31f262 835 {
ds4279 0:374a8a31f262 836 result = (putc(str[i]) == str[i]) ? result + 1 : result;
ds4279 0:374a8a31f262 837 }
ds4279 0:374a8a31f262 838
ds4279 0:374a8a31f262 839 ////////////////////////////////////////
ds4279 0:374a8a31f262 840 ///Now get expected results
ds4279 0:374a8a31f262 841 while (1)
ds4279 0:374a8a31f262 842 {
ds4279 0:374a8a31f262 843 //////////////////////////////
ds4279 0:374a8a31f262 844 ///Time has expired
ds4279 0:374a8a31f262 845 if (tmr.read_ms() > timeout)
ds4279 0:374a8a31f262 846 {
ds4279 0:374a8a31f262 847 // myDebug.printf("timeout!\n");
ds4279 0:374a8a31f262 848 //We flush the buffer
ds4279 0:374a8a31f262 849 while (wifiSer.readable())
ds4279 0:374a8a31f262 850 wifiSer.getc();
ds4279 0:374a8a31f262 851
ds4279 0:374a8a31f262 852
ds4279 0:374a8a31f262 853 sprintf(buffer,"timeout: %s\r\n", checking.c_str());
ds4279 0:374a8a31f262 854 myDebug.printf(buffer);
ds4279 0:374a8a31f262 855 //DBG("check: %s\r\n", checking.c_str());
ds4279 0:374a8a31f262 856
ds4279 0:374a8a31f262 857 // attach_rx(true);
ds4279 0:374a8a31f262 858 return 1;
ds4279 0:374a8a31f262 859 }
ds4279 0:374a8a31f262 860 else if (wifiSer.readable())
ds4279 0:374a8a31f262 861 {
ds4279 0:374a8a31f262 862 read = wifiSer.getc();
ds4279 0:374a8a31f262 863 if ( read != '\r' && read != '\n')
ds4279 0:374a8a31f262 864 {
ds4279 0:374a8a31f262 865 ////////////////////////////////////////
ds4279 0:374a8a31f262 866 ///This method is ineficient!!!
ds4279 0:374a8a31f262 867 checking += read;
ds4279 0:374a8a31f262 868 found = checking.find(ACK);
ds4279 0:374a8a31f262 869
ds4279 0:374a8a31f262 870
ds4279 0:374a8a31f262 871 ////////////////////////////////////////
ds4279 0:374a8a31f262 872 ///if a + value string found
ds4279 0:374a8a31f262 873 if (found != string::npos)
ds4279 0:374a8a31f262 874 {
ds4279 0:374a8a31f262 875 wait(0.01);
ds4279 0:374a8a31f262 876 // sprintf(buffer,"found: %s\n",checking.c_str() );
ds4279 0:374a8a31f262 877 // myDebug.printf(buffer);
ds4279 0:374a8a31f262 878
ds4279 0:374a8a31f262 879 //////////////////////////
ds4279 0:374a8a31f262 880 ///We flush the buffer
ds4279 0:374a8a31f262 881 while (wifiSer.readable())
ds4279 0:374a8a31f262 882 wifiSer.getc();
ds4279 0:374a8a31f262 883
ds4279 0:374a8a31f262 884 if( DHCP_CONN.find(ACK) != string::npos)
ds4279 0:374a8a31f262 885 {
ds4279 0:374a8a31f262 886 return 2; //successful DHCP
ds4279 0:374a8a31f262 887 }
ds4279 0:374a8a31f262 888 return 0; //found string 0 good
ds4279 0:374a8a31f262 889 // break;
ds4279 0:374a8a31f262 890 }
ds4279 0:374a8a31f262 891
ds4279 0:374a8a31f262 892 ///////////////////////////////////////////////////
ds4279 0:374a8a31f262 893 ///Special case check for DHCP attempt connection
ds4279 0:374a8a31f262 894 if(DHCPconn)
ds4279 0:374a8a31f262 895 {
ds4279 0:374a8a31f262 896 DHCPnotCONN = checking.find(AUTH_ERR);
ds4279 0:374a8a31f262 897 if (DHCPnotCONN != string::npos)
ds4279 0:374a8a31f262 898 {
ds4279 0:374a8a31f262 899 wait(0.01);
ds4279 0:374a8a31f262 900 sprintf(buffer,"AUTH-ERR: %s\n",checking.c_str() );
ds4279 0:374a8a31f262 901 myDebug.printf(buffer);
ds4279 0:374a8a31f262 902
ds4279 0:374a8a31f262 903 //////////////////////////////
ds4279 0:374a8a31f262 904 ///We flush the buffer
ds4279 0:374a8a31f262 905 while (wifiSer.readable())
ds4279 0:374a8a31f262 906 wifiSer.getc();
ds4279 0:374a8a31f262 907
ds4279 0:374a8a31f262 908 return 3; //DHCP Error
ds4279 0:374a8a31f262 909 // break;
ds4279 0:374a8a31f262 910 }
ds4279 0:374a8a31f262 911
ds4279 0:374a8a31f262 912 DHCPnotCONN = checking.find(FAILED);
ds4279 0:374a8a31f262 913 if (DHCPnotCONN != string::npos)
ds4279 0:374a8a31f262 914 {
ds4279 0:374a8a31f262 915 wait(0.01);
ds4279 0:374a8a31f262 916 sprintf(buffer,"NONE FAILED: %s\n",checking.c_str() );
ds4279 0:374a8a31f262 917 myDebug.printf(buffer);
ds4279 0:374a8a31f262 918
ds4279 0:374a8a31f262 919 //We flush the buffer
ds4279 0:374a8a31f262 920 while (wifiSer.readable())
ds4279 0:374a8a31f262 921 wifiSer.getc();
ds4279 0:374a8a31f262 922
ds4279 0:374a8a31f262 923 return 3;
ds4279 0:374a8a31f262 924 // break;
ds4279 0:374a8a31f262 925 }
ds4279 0:374a8a31f262 926 }//if(DHCPconn}
ds4279 0:374a8a31f262 927
ds4279 0:374a8a31f262 928 }//if ( read != '\r' && read != '\n')
ds4279 0:374a8a31f262 929 }//else if (wifiSer.readable())
ds4279 0:374a8a31f262 930 } //while (1)
ds4279 0:374a8a31f262 931 // sprintf(buffer,"check2: %s\r\n", checking.c_str());
ds4279 0:374a8a31f262 932 // myDebug.printf(buffer);
ds4279 0:374a8a31f262 933 // DBG("check: %s\r\n", checking.c_str());
ds4279 0:374a8a31f262 934
ds4279 0:374a8a31f262 935 // attach_rx(true);
ds4279 0:374a8a31f262 936 return result;
ds4279 0:374a8a31f262 937 }
ds4279 0:374a8a31f262 938
ds4279 0:374a8a31f262 939 //the user wants the result from the command (ACK == NULL, res != NULL)
ds4279 0:374a8a31f262 940 if ( res != NULL)
ds4279 0:374a8a31f262 941 {
ds4279 0:374a8a31f262 942 int i = 0;
ds4279 0:374a8a31f262 943 Timer timeout;
ds4279 0:374a8a31f262 944 timeout.start();
ds4279 0:374a8a31f262 945 tmr.reset();
ds4279 0:374a8a31f262 946 while (1) {
ds4279 0:374a8a31f262 947 if (timeout.read() > 2) {
ds4279 0:374a8a31f262 948 if (i == 0) {
ds4279 0:374a8a31f262 949 res = NULL;
ds4279 0:374a8a31f262 950 break;
ds4279 0:374a8a31f262 951 }
ds4279 0:374a8a31f262 952 res[i] = '\0';
ds4279 0:374a8a31f262 953 // DBG("user str 1: %s\r\n", res);
ds4279 0:374a8a31f262 954
ds4279 0:374a8a31f262 955 break;
ds4279 0:374a8a31f262 956 } else {
ds4279 0:374a8a31f262 957 if (tmr.read_ms() > 300) {
ds4279 0:374a8a31f262 958 res[i] = '\0';
ds4279 0:374a8a31f262 959 // DBG("user str: %s\r\n", res);
ds4279 0:374a8a31f262 960
ds4279 0:374a8a31f262 961 break;
ds4279 0:374a8a31f262 962 }
ds4279 0:374a8a31f262 963 if (wifiSer.readable()) {
ds4279 0:374a8a31f262 964 tmr.start();
ds4279 0:374a8a31f262 965 read = wifiSer.getc();
ds4279 0:374a8a31f262 966
ds4279 0:374a8a31f262 967 // we drop \r and \n
ds4279 0:374a8a31f262 968 if ( read != '\r' && read != '\n') {
ds4279 0:374a8a31f262 969 res[i++] = read;
ds4279 0:374a8a31f262 970 }
ds4279 0:374a8a31f262 971 }
ds4279 0:374a8a31f262 972 }
ds4279 0:374a8a31f262 973 }
ds4279 0:374a8a31f262 974 // DBG("user str: %s\r\n", res);
ds4279 0:374a8a31f262 975 return 0;
ds4279 0:374a8a31f262 976 }
ds4279 0:374a8a31f262 977
ds4279 0:374a8a31f262 978 // //We flush the buffer
ds4279 0:374a8a31f262 979 // while (wifiSer.readable())
ds4279 0:374a8a31f262 980 // wifiSer.getc();
ds4279 0:374a8a31f262 981
ds4279 0:374a8a31f262 982 // attach_rx(true);
ds4279 0:374a8a31f262 983 //DBG("result: %d\r\n", result)
ds4279 0:374a8a31f262 984 return 1;
ds4279 0:374a8a31f262 985 }
ds4279 0:374a8a31f262 986
ds4279 0:374a8a31f262 987 /* if( read == ACK[pos] )
ds4279 0:374a8a31f262 988 {
ds4279 0:374a8a31f262 989 pos++;
ds4279 0:374a8a31f262 990 sprintf(buffer,"cmp: %d\n",pos);
ds4279 0:374a8a31f262 991 myDebug.printf(buffer);
ds4279 0:374a8a31f262 992
ds4279 0:374a8a31f262 993 if(pos == len)
ds4279 0:374a8a31f262 994 {
ds4279 0:374a8a31f262 995 myDebug.printf("myCompare-Break\n");
ds4279 0:374a8a31f262 996 ///////////////////////////
ds4279 0:374a8a31f262 997 ///We have a compare
ds4279 0:374a8a31f262 998 //We flush the buffer
ds4279 0:374a8a31f262 999 while (wifiSer.readable())
ds4279 0:374a8a31f262 1000 wifiSer.getc();
ds4279 0:374a8a31f262 1001
ds4279 0:374a8a31f262 1002 break;
ds4279 0:374a8a31f262 1003 }
ds4279 0:374a8a31f262 1004 }//if( read == ACK[pos] )
ds4279 0:374a8a31f262 1005 else
ds4279 0:374a8a31f262 1006 {
ds4279 0:374a8a31f262 1007 if(pos == len)
ds4279 0:374a8a31f262 1008 {
ds4279 0:374a8a31f262 1009 myDebug.printf("myCompare2\n");
ds4279 0:374a8a31f262 1010 }
ds4279 0:374a8a31f262 1011 else
ds4279 0:374a8a31f262 1012 pos = 0;
ds4279 0:374a8a31f262 1013 }
ds4279 0:374a8a31f262 1014 */