/* * Sends the AT command and stops if any of the TAGS is found. * Extract the string enclosed in the passed tags and returns it in the outStr buffer. * Returns true if the string is extracted, false if tags are not found of timed out. */ bool EspDrv::sendCmdGet(const __FlashStringHelper* cmd, const char* startTag, const char* endTag, char* outStr, int outStrLen) { int idx; bool ret = false; outStr[0] = 0; espEmptyBuf(); LOGDEBUG(F("----------------------------------------------")); LOGDEBUG1(F(">>"), cmd); // send AT command to ESP espSerial->println(cmd); // read result until the startTag is found idx = readUntil(1000, startTag); if(idx==NUMESPTAGS) { // clean the buffer to get a clean string ringBuf.init(); // start tag found, search the endTag idx = readUntil(500, endTag); if(idx==NUMESPTAGS) { // end tag found // copy result to output buffer avoiding overflow ringBuf.getStrN(outStr, strlen(endTag), outStrLen-1); // read the remaining part of the response readUntil(2000); ret = true; } else { LOGWARN(F("End tag not found")); } } else if(idx>=0 and idx<NUMESPTAGS) { // the command has returned but no start tag is found LOGDEBUG1(F("No start tag found:"), idx); } else { // the command has returned but no tag is found LOGWARN(F("No tag found")); } LOGDEBUG1(F("---------------------------------------------- >"), outStr); LOGDEBUG(); return ret; }
/* * Sends the AT command and returns the id of the TAG. * Return -1 if no tag is found. */ int EspDrv::sendCmd(const __FlashStringHelper* cmd, int timeout) { espEmptyBuf(); LOGDEBUG(F("----------------------------------------------")); LOGDEBUG1(F(">>"), cmd); espSerial->println(cmd); int idx = readUntil(timeout); LOGDEBUG1(F("---------------------------------------------- >"), idx); LOGDEBUG(); return idx; }
static int firestep_writeCore(const char *buf, size_t bufsize) { char message[WRITEBUFMAX+4]; if (bufsize > WRITEBUFMAX) { memcpy(message, buf, WRITEBUFMAX); message[WRITEBUFMAX] = '.'; message[WRITEBUFMAX+1] = '.'; message[WRITEBUFMAX+2] = '.'; message[WRITEBUFMAX+3] = 0; } else { memcpy(message, buf, bufsize); message[bufsize] = 0; } char *s; for (s = message; *s; s++) { switch (*s) { case '\n': case '\r': *s = ' '; break; } } LOGDEBUG1("firestep_write %s start", message); ssize_t rc = write(fdwTinyG, buf, bufsize); if (rc == bufsize) { LOGINFO2("firestep_write %s (%ldB)", message, bufsize); } else { LOGERROR2("firestep_write %s -> [%ld]", message, rc); } return rc < 0 ? rc : 0; }
static int firefuse_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi) { LOGTRACE1("firefuse_readdir(%s)", path); if (is_cv_path(path) || is_cnc_path(path) || 0==strcmp(path, FIREREST_SYNC)) { return firerest_readdir(path, buf, filler, offset, fi); } (void) offset; (void) fi; LOGDEBUG1("firefuse_readdir(%s)", path); if (strcmp(path, "/") == 0) { filler(buf, ".", NULL, 0); filler(buf, "..", NULL, 0); filler(buf, STATUS_PATH + 1, NULL, 0); filler(buf, CONFIG_PATH + 1, NULL, 0); filler(buf, HOLES_PATH + 1, NULL, 0); filler(buf, FIRELOG_PATH + 1, NULL, 0); filler(buf, ECHO_PATH + 1, NULL, 0); filler(buf, FIRESTEP_PATH + 1, NULL, 0); filler(buf, FIREREST_CV + 1, NULL, 0); filler(buf, FIREREST_CNC + 1, NULL, 0); filler(buf, FIREREST_SYNC + 1, NULL, 0); } else { LOGERROR1("firefuse_readdir(%s) Unknown path", path); return -ENOENT; } return 0; }
static int firefuse_truncate(const char *path, off_t size) { LOGTRACE2("firefuse_truncate(%s,%ld)", path, size); if (is_cv_path(path)) { return cve_truncate(path, size); } if (is_cnc_path(path)) { return cnc_truncate(path, size); } (void) size; if (strcmp(path, "/") == 0) { // directory } else if (strcmp(path, ECHO_PATH) == 0) { // NOP } else if (strcmp(path, FIRELOG_PATH) == 0) { // NOP } else if (strcmp(path, FIRESTEP_PATH) == 0) { // NOP } else { LOGERROR1("firefuse_truncate(%s) -> ENOENT", path); return -ENOENT; } LOGDEBUG1("firefuse_truncate %s", path); return 0; }
int firefuse_open(const char *path, struct fuse_file_info *fi) { if (is_cv_path(path)) { return cve_open(path, fi); } if (is_cnc_path(path)) { return cnc_open(path, fi); } LOGDEBUG1("firefuse_open(%s)", path); int result = 0; if (strcmp(path, STATUS_PATH) == 0) // "/status" { verifyOpenR_(path, fi, &result); } else if (strcmp(path, CONFIG_PATH) == 0) // "/config.json" { verifyOpenR_(path, fi, &result); } else if (strcmp(path, HOLES_PATH) == 0) // "/holes" { verifyOpenR_(path, fi, &result); fi->fh = (uint64_t) (size_t) fopen("/var/firefuse/config.json", "r"); if (!fi->fh) { result = -ENOENT; } } else if (strcmp(path, ECHO_PATH) == 0) // "/echo" { verifyOpenRW(path, fi, &result); } else if (strcmp(path, FIRELOG_PATH) == 0) // "/firelog" { verifyOpenRW(path, fi, &result); } else if (strcmp(path, FIRESTEP_PATH) == 0) // "/firestep" { verifyOpenRW(path, fi, &result); } else { result = -ENOENT; } switch (-result) { case ENOENT: LOGERROR1("firefuse_open(%s) error ENOENT", path); break; case EACCES: LOGERROR1("firefuse_open(%s) error EACCES", path); break; default: if (fi->fh) { fi->direct_io = 1; } LOGDEBUG2("firefuse_open(%s) OK flags:%0x", path, fi->flags); break; } return result; }
// Start server TCP on port specified bool EspDrv::startServer(uint16_t port) { LOGDEBUG1(F("> startServer"), port); int ret = sendCmd(F("AT+CIPSERVER=1,%d"), 1000, port); return ret==TAG_OK; }
uint8_t EspDrv::getScanNetworks() { uint8_t ssidListNum = 0; int idx; bool ret = false; espEmptyBuf(); LOGDEBUG(F("----------------------------------------------")); LOGDEBUG(F(">> AT+CWLAP")); espSerial->println(F("AT+CWLAP")); char buf[100]; idx = readUntil(10000, "+CWLAP:("); while (idx == NUMESPTAGS) { _networkEncr[ssidListNum] = espSerial->parseInt(); //LOGDEBUG1("enc:", _networkEncr[ssidListNum]); // discard , and " characters readUntil(1000, "\""); idx = readUntil(1000, "\"", false); if(idx==NUMESPTAGS) { ringBuf.getStr(_networkSsid[ssidListNum], 1); // 1 = strlen ("\"") } //LOGDEBUG1("ssid:", _networkSsid[ssidListNum]); // discard , character readUntil(1000, ","); _networkRssi[ssidListNum] = espSerial->parseInt(); //LOGDEBUG1("rssi:", _networkRssi[ssidListNum]); idx = readUntil(1000, "+CWLAP:("); if(ssidListNum==WL_NETWORKS_LIST_MAXNUM-1) break; ssidListNum++; } if (idx==-1) return -1; LOGDEBUG1(F("---------------------------------------------- >"), ssidListNum); LOGDEBUG(); return ssidListNum; }
/* * Sends the AT command and returns the id of the TAG. * The additional arguments are formatted into the command using sprintf. * Return -1 if no tag is found. */ int EspDrv::sendCmd(const __FlashStringHelper* cmd, int timeout, ...) { char cmdBuf[CMD_BUFFER_SIZE]; va_list args; va_start (args, timeout); vsnprintf_P (cmdBuf, CMD_BUFFER_SIZE, (char*)cmd, args); va_end (args); espEmptyBuf(); LOGDEBUG(F("----------------------------------------------")); LOGDEBUG1(F(">>"), cmdBuf); espSerial->println(cmdBuf); int idx = readUntil(timeout); LOGDEBUG1(F("---------------------------------------------- >"), idx); LOGDEBUG(); return idx; }
uint8_t EspDrv::getClientState(uint8_t sock) { LOGDEBUG1(F("> getClientState"), sock); char findBuf[20]; sprintf_P(findBuf, PSTR("+CIPSTATUS:%d,"), sock); char buf[10]; if (sendCmdGet(F("AT+CIPSTATUS"), findBuf, ",", buf, sizeof(buf))) { LOGDEBUG(F("Connected")); return true; } LOGDEBUG(F("Not connected")); return false; }
void EspDrv::espEmptyBuf(bool warn) { char c; int i=0; while(espSerial->available() > 0) { c = espSerial->read(); if (i>0 and warn==true) LOGDEBUG0(c); i++; } if (i>0 and warn==true) { LOGDEBUG(F("")); LOGDEBUG1(F("Dirty characters in the serial buffer! >"), i); } }
const char * firestep_json() { int wait = 0; while (jsonDepth > 0) { LOGDEBUG1("firestep_json() waiting for JSON %d", wait++); sched_yield(); // wait for completion if (wait > 10) { LOGERROR("firestep_json() unterminated JSON"); return "{\"error\":\"unterminated JSON\"}"; } } jsonBuf[jsonLen] = 0; if (jsonLen > 0) { jsonBuf[jsonLen++] = '\n'; jsonBuf[jsonLen++] = 0; } return jsonBuf; }
// Start server TCP on port specified void EspDrv::stopClient(uint8_t sock) { LOGDEBUG1(F("> stopClient"), sock); int ret = sendCmd(F("AT+CIPCLOSE=%d"), 4000, sock); }
int firefuse_unlink(const char *path) { LOGDEBUG1("firefuse_unlink(%s)", path); return 0; }