int joinNetwork(){ ///More Testing int retries; int offset; int result; if(inCommandMode == -1){ result = enterCommandMode(); if(result == -1) return -1; else inCommandMode = 1; } for(retries = 0; retries < RETRY_ATTEMPTS; retries++){ sendString(JOIN_BOONDOCKS, 15); longDelay(); sendString(SHOW_CONNECTION, 7); longDelay(); if(Txdata[0] != NULL){ if(Txdata[2] == '3'){ result = exitCommandMode(); while(result != 1){ result = exitCommandMode(); } return 1; } } } result = exitCommandMode(); return -1; }
boolean WiFlyDevice::join(const char *ssid) { /* */ // TODO: Handle other authentication methods // TODO: Handle escaping spaces/$ in SSID // TODO: Allow for timeout? // TODO: Do we want to set the passphrase/key to empty when they're // not required? (Probably not necessary as I think module // ignores them when they're not required.) bool joined = false; String command = "join "; command += ssid; char charBuf[command.length()+1]; if (commandModeFlag) { exitCommandMode(); } enterCommandMode(); command.toCharArray(charBuf, command.length()+1); joined=sendCommand(charBuf,false,"Associated!"); // TODO: Actually detect failure to associate // TODO: Handle connecting to Adhoc device exitCommandMode(); return joined; }
/** * @brief Checks the connection status * @returns Conn status as four chars, in this hex format: 8XYZ */ const char * WiFlyDevice::getConnectionStatus() { static char status[4]=""; char newChar; byte offset = 0; if (commandModeFlag) { exitCommandMode(); } enterCommandMode(); if(sendCommand("show c ",false,"8")) { while (offset < 4) { newChar = uart->read(); if (newChar != -1) { status[offset] = newChar; offset++; } } status[3]=0; } exitCommandMode(); return status; }
boolean WiFlyDevice::setWakeSleepTimers( int _wakeTimer, int _sleepTimer) { boolean timerSet=false; String cmd1="set sys wake "; String cmd2="set sys sleep "; if(_sleepTimer!=0 && _wakeTimer==0) { // This would make the wifly never wake up!!!!! timerSet=false; } else { if (commandModeFlag) { exitCommandMode(); } enterCommandMode(); cmd1+=_wakeTimer; char charBuf1[cmd1.length()+1]; cmd1.toCharArray(charBuf1, cmd1.length()+1); boolean wakeOk=sendCommand(charBuf1,false,"AOK"); if(wakeOk) { cmd2+= _sleepTimer; char charBuf2[cmd2.length()+1]; cmd2.toCharArray(charBuf2, cmd2.length()+1); timerSet=sendCommand(charBuf2,false,"AOK"); } } sendCommand("save",false,"AOK"); exitCommandMode(); return timerSet; }
void WiFlyDevice::sleepNow() { if (commandModeFlag) { exitCommandMode(); } enterCommandMode(); sendCommand("sleep",false,"AOK"); exitCommandMode(); // Just in case, as the WiFly will sleep before reaching here. }
void WiFlyDevice::useUDP() { if (commandModeFlag) { exitCommandMode(); } enterCommandMode(); sendCommand("set ip protocol 1", false); sendCommand("set ip localport 80", false); exitCommandMode(); }
const char * WiFlyDevice::getIp(boolean pauseRequired) { static char ip[16] = ""; byte offset = 0; if (enterCommandMode(pauseRequired) && sendCommand("get ip", "IP=")) { while (offset < 15) { char character = uart.read(); LOG_READ(character); if (character == ':') { ip[offset] = '\x00'; break; } else if (character != -1) { ip[offset] = character; offset++; } } } ip[offset] = '\x00'; waitForPrompt(); exitCommandMode(); return ip; }
/** * @brief Get the version of the WiFly firmware * @return Char with the version info */ const char* WiFlyDevice::getVersion() { const int versionLength=64; // Allocate one extra space for the zero terminator. static char version[versionLength+1] = { 0 }; enterCommandMode(); sendCommand("ver", false, VER_RESPONSE); int offset = 0; int readVal = 0; while (offset < versionLength && readVal!='\n') { readVal = uart->read(); if (readVal == -1) { // Data not available; try again after brief delay. delay(1); continue; } else { version[offset++] = readVal; } } waitForResponse("<"); // This char appears in the beginning of the prompt findInResponse(" "); // And with this we ignore the rest of the prompt exitCommandMode(); return version; }
boolean WiFlyDevice::join(const char *ssid, const char *passphrase, boolean isWPA) { String command=""; if (commandModeFlag) { exitCommandMode(); } enterCommandMode(); // TODO: Handle escaping spaces/$ in passphrase and SSID command="set wlan "; if (isWPA) { command += " passphrase "; } else { command += " key "; } command += passphrase; // There must be a better way to convert string -> char[] in arduino... ? char charBuf[command.length()+1]; command.toCharArray(charBuf, command.length()+1); sendCommand(charBuf); command="join "; command += ssid; char charBuf2[command.length()+1]; command.toCharArray(charBuf2, command.length()+1); return sendCommand(charBuf2,false,"Associated!"); }
bool JPWiFly::finishCommand() { DPRINT("finishCommand : "); DPRINTLN(exitCommand); if (exitCommand > 0 && --exitCommand == 0) return exitCommandMode(); return true; }
const char * WiFlyDevice::ip() { /* The return value is intended to be dropped directly into calls to 'print' or 'println' style methods. */ static char ip[IP_ADDRESS_BUFFER_SIZE] = ""; // TODO: Ensure we're not in a connection? enterCommandMode(); // Version 2.19 of the WiFly firmware has a "get ip a" command but // we can't use it because we want to work with 2.18 too. sendCommand(F("get ip"), false, "IP="); char newChar; byte offset = 0; // Copy the IP address from the response into our buffer while (offset < IP_ADDRESS_BUFFER_SIZE) { newChar = uart->read(); if (newChar == ':') { ip[offset] = '\x00'; break; } else if (newChar != -1) { ip[offset] = newChar; offset++; } } // This handles the case when we reach the end of the buffer // in the loop. (Which should never happen anyway.) // And hopefully this prevents us from failing completely if // there's a mistake above. ip[IP_ADDRESS_BUFFER_SIZE-1] = '\x00'; // This should skip the remainder of the output. // TODO: Handle this better? waitForResponse("<"); while (uart->read() != ' ') { // Skip the prompt } // For some reason the "sendCommand" approach leaves the system // in a state where it misses the first/next connection so for // now we don't check the response. // TODO: Fix this exitCommandMode(); //uart->println("exit"); //sendCommand("exit", false, "EXIT"); return ip; }
WiFlyDevice::Status WiFlyDevice::getStatus(boolean pauseRequired) { if (!enterCommandMode(pauseRequired) || !sendCommand("show c", "8")) { waitForPrompt(); exitCommandMode(); return StatusError; } char result[3] = {0, 0, 0}; for (int i=0; i<3;) { result[i] = uart.read(); LOG_READ(result[i]); if (result[i] <= 0 || result[i] > 128) continue; i++; } Status status; if (strchr("02468ACEace", result[1])) status = StatusNotAssociated; else if (result[2] == '3') status = StatusNoIp; else if (result[2] == '4') status = StatusConnecting; else if (result[2] == '1') status = StatusConnected; else status = StatusDisconnected; waitForPrompt(); exitCommandMode(); return status; }