Created
January 5, 2020 21:21
-
-
Save jonasbits/e032fdaef12519791340969da749fc38 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifdef ARDUINO | |
#include <Arduino.h> | |
#else | |
#include <iostream> | |
#endif | |
#include <string.h> | |
#define YES 1 | |
#define NO 2 | |
// Function that printf and related will use to print | |
int serial_putchar(char c, FILE* f) { | |
if (c == '\n') serial_putchar('\r', f); | |
return Serial.write(c) == 1? 0 : 1; | |
} | |
FILE serial_stdout; | |
//stub | |
int mainStart(); | |
void setup(){ | |
Serial.begin(9600); | |
// Redirect stdout | |
fdev_setup_stream(&serial_stdout, serial_putchar, NULL, _FDEV_SETUP_WRITE); | |
stdout = &serial_stdout; | |
printf("This now goes to stdout: %d\n", 3); | |
mainStart(); | |
} | |
void loop(){ | |
delay(1000); | |
} | |
enum cmds | |
{ | |
STAT, | |
LIST, | |
RETR, | |
TOP, | |
DELE, | |
UIDL, | |
NOOP, | |
RSET, | |
CAPA, | |
QUIT | |
}; | |
const char *cmdQuery[QUIT + 1] = | |
{ | |
"STAT", "LIST", "RETR", "TOP", "DELE", "UIDL", "NOOP", "RSET", "CAPA", "QUIT"}; | |
const char *cmdReply[QUIT + 1] = | |
{ | |
"+OK 0 0 STAT", "+OK 0 messages (0 octets) LIST", "-ERR No such message RETR", "-ERR No such message TOP", "-ERR No such message TOP", | |
"-ERR No such message UIDL", "+OK NOOP", "+OK 0 messages (0 octets) RSET", "+OK Capability list follows.", "+OK goodbye QUIT"}; | |
int mainStart() | |
{ | |
int valid_command = NO; | |
char buff[] = "CAPA"; | |
int activeCmd = QUIT; | |
for (int i = 0; i <= QUIT; i++) | |
{ | |
if (strncasecmp(buff, cmdQuery[i], 3) == 0) | |
{ | |
valid_command = YES; | |
activeCmd = i; | |
} | |
} | |
if (valid_command == YES) | |
{ | |
switch (activeCmd) | |
{ | |
case STAT: | |
//std::cout << cmdReply[activeCmd] << std::endl; break; | |
case LIST: | |
//std::cout << cmdReply[activeCmd] << std::endl; break; | |
case RETR: | |
//std::cout << cmdReply[activeCmd] << std::endl; break; | |
case TOP: | |
//std::cout << cmdReply[activeCmd] << std::endl; break; | |
case DELE: | |
//std::cout << cmdReply[activeCmd] << std::endl; break; | |
case UIDL: | |
//std::cout << cmdReply[activeCmd] << std::endl; break; | |
case NOOP: | |
//std::cout << cmdReply[activeCmd] << std::endl; break; | |
case RSET: | |
//std::cout << cmdReply[activeCmd] << std::endl; break; | |
case CAPA: | |
/*std::cout << cmdReply[activeCmd] << std::endl; | |
for (int i = 0; i <= QUIT; i++) | |
std::cout << cmdQuery[i] << std::endl; | |
std::cout << "." << std::endl; | |
*/break; | |
case QUIT: | |
Serial.println( cmdReply[activeCmd] ); | |
//std::cout << cmdReply[activeCmd] << std::endl; break; | |
default: | |
break; | |
} | |
} | |
valid_command = NO; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment