void wifiServerSaveConfig() { Serial.println(F("Saving settings")); int fail = 0; while (strcmp_P(useLineBuffer(), PSTR("begin")) != 0) wifiBufferLoop(); while (1) { wifiBufferLoop(); if (hasNewLine()) { if (strcmp_P(useLineBuffer(), PSTR("end")) == 0) break; if (!configParseRequest(useLineBuffer())) { Serial.println(F("fail:")); Serial.println(useLineBuffer()); Serial.println(F("end")); wifiInitiateSend(linkID); wifiServerSendPGMP(http400, &stream); wifiEndSend(); fail = 1; break; } } } if (fail == 0) { configWrite(); Serial.println(linkID); wifiInitiateSend(linkID); wifiServerSendPGMP(http200json, &stream); wifiServerSendPGMP(success, &stream); wifiEndSend(); } }
void stage3() { static int buf2Ndx = 0, buf3Ndx = 0, elfLength = 4, lineNum = 1; int bigBufSize = 1000; long filePosOffset; char *bigBuf = (char *) malloc(sizeof(char) * bigBufSize); char *binFile = "\177ELF"; FILE *readFile; grepInfo *grepData = (grepInfo *) malloc(sizeof(grepInfo)); while (done2 == 0) { //critical section for consuming //decrement semaphores for buf2 sem_wait(&buf2Full); //lock mutexes for buf2 pthread_mutex_lock(&buf2Mutex); grepData->fileName = strdup(buffer2[buf2Ndx]); //unlock mutexes for buf2 pthread_mutex_unlock(&buf2Mutex); //incrememnt semaphores for buf2 sem_post(&buf2Empty); readFile = fopen(buffer2[buf2Ndx++], "r"); grepData->lineNum = lineNum++; while (fgets(bigBuf, bigBufSize, readFile) != NULL) { if (strncmp(bigBuf, binFile, sizeof(char) * elfLength) == 0) { //hopefully this skips all of the unreadable characters in a binary file. fseek(readFile, 0, SEEK_END); continue; } filePosOffset = ftell(readFile); while (hasNewLine(bigBuf) == EXIT_FAILURE) { //if the end of the current line was not reached, //double the size of the buffer. //and keep reading from the same position //don't skip characters. fseek(readFile, filePosOffset, SEEK_SET); bigBufSize *= 2; bigBuf = (char *) realloc(bigBuf, sizeof(char) * bigBufSize); if (fgets(bigBuf, bigBufSize, readFile) == NULL) { ferror(readFile); return; } } grepData->lineStr = strdup(bigBuf); //critical section for producing sem_wait(&buf3Empty); pthread_mutex_lock(&buf3Mutex); buffer3[buf3Ndx++] = grepData; pthread_mutex_unlock(&buf3Mutex); sem_post(&buf3Full); } if (bigBuf == NULL) { ferror(readFile); return; } buf2Ndx = buf2Ndx % buffSize; buf3Ndx = buf3Ndx % buffSize; } done3 = 1; }
void wifiServerListener() { while (1) { wifiBufferLoop(); if (hasNewLine()) { if (strstr_P(useLineBuffer(), PSTR("+IPD")) != NULL) { // now we parse the request Serial.println(F("Get something, parsing to see if it's http or not")); if (sscanf_P(useLineBuffer(), PSTR("+IPD,%d,%*d:%7s %15s %8s\r"), &linkID, httpMethod, httpPath, httpVer)!=4){ Serial.println(httpMethod); Serial.println(httpPath); Serial.println(httpVer); wifiInitiateSend(linkID); wifiServerSendPGMP(http400, &stream); wifiEndSend(); continue; } if ((!strcmp_P(httpVer, PSTR("HTTP/1.1"))) || (!strcmp_P(httpVer, PSTR("HTTP/1.0")))) { Serial.println(F("It's really a HTTP request")); while (strcmp_P(useLineBuffer(), PSTR("\r")) != 0) wifiBufferLoop(); if (strcmp_P(httpPath, PSTR("/")) == 0) { Serial.println(F("Sending index page")); wifiInitiateSend(linkID); wifiServerSendIndex(&stream); wifiEndSend(); } else if (strcmp_P(httpPath, PSTR("/save")) == 0 && strcmp_P(httpMethod, PSTR("POST")) == 0) { wifiServerSaveConfig(); } else if (strcmp_P(httpPath, PSTR("/currSetting")) == 0) { Serial.println(F("Sending curr setting")); wifiInitiateSend(linkID); wifiServerSendPGMP(http200json, &stream); wifiServerSendConfig(&stream); wifiEndSend(); } else if (strcmp_P(httpPath, PSTR("/restart"))==0){ Serial.println(F("Soft restarting")); wifiInitiateSend(linkID); wifiServerSendPGMP(http200json, &stream); wifiServerSendPGMP(success, &stream); wifiEndSend(); while (1); // just let the watchdog timeout } else { Serial.println(F("Sending 404")); wifiInitiateSend(linkID); wifiServerSendPGMP(http404, &stream); wifiEndSend(); } } else { wifiInitiateSend(linkID); wifiServerSendPGMP(http400, &stream); wifiEndSend(); } } else if (strcmp_P(useLineBuffer(), PSTR("ready\r"))==0){ Serial.println(F("ESP unexpectedly reseted, initializing")); wifiInit(); wifiServerInit(); } } } }