-
Dependencies: EthernetInterface TCPSocket_HelloWorld mbed-rtos mbed
Fork of TCPSocket_HelloWorld by
Revision 16:22169975b5d8, committed 2016-02-09
- Comitter:
- offroad
- Date:
- Tue Feb 09 11:56:52 2016 +0000
- Parent:
- 15:10c1be621ad6
- Commit message:
- initial public release
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 10c1be621ad6 -r 22169975b5d8 main.cpp --- a/main.cpp Sat Nov 07 15:46:54 2015 +0000 +++ b/main.cpp Tue Feb 09 11:56:52 2016 +0000 @@ -10,9 +10,13 @@ static int FSM_state2 = 0; static char FSM_cmd = 'x'; -static void FSM_handleChar(DigitalOut* gpo, char inputChar, char* bufOut, int* nOut){ - // === reset reply === - *nOut = 0; +static void FSM_reset(){ + FSM_state1 = FSM_STATE_IDLE; + FSM_state2 = 0; + FSM_cmd = 'x'; +} + +static void FSM_handleChar(DigitalOut* gpo, char inputChar, char** bufOut){ // === handle incoming char === switch (FSM_state1){ @@ -50,9 +54,9 @@ goto reset; // success case '?': // === query GPO state (verify) === - bufOut[(*nOut)++] = *(gpo+ixGpio) ? '1' : '0'; - bufOut[(*nOut)++] = 13; - bufOut[(*nOut)++] = 10; + *((*bufOut)++) = *(gpo+ixGpio) ? '1' : '0'; + *((*bufOut)++) = 13; + *((*bufOut)++) = 10; goto reset; // success default: goto reset; // invalid command byte @@ -72,22 +76,20 @@ goto lockup; } // switch FSM_state1 - // === internal error. May never be reached === + // === internal error. Should never be reached. === lockup: goto lockup; // === reset state machine (on completion and any invalid input === reset: - FSM_state1 = FSM_STATE_IDLE; - FSM_state2 = 0; - FSM_cmd = 'x'; + FSM_reset(); doneCont: return; } int main() { - printf("\r\nGPIO server v2 20151106 mn\r\n"); + printf("\r\nLANGPIO server v2.1 20151107 mn\r\n"); fflush(stdout); static DigitalOut myGpo[NPORTS] = { DigitalOut(LED1), @@ -155,7 +157,8 @@ printf("Connection from: %s\n", client.get_address()); client.send_all(">\r\n", 3); - + FSM_reset(); + // === main loop === while (true) { @@ -164,19 +167,34 @@ // === detect connection break === if (n < 0) - break; + goto conn_exit; - // === handle incoming data === + // maximum length of a single reply. Anything longer will be split. + #define MAX_PACKETLEN (1024) + // maximum length of a single message to guarantee there is enough output buffer + #define MAX_MSGLEN (16) + char bufOut[MAX_PACKETLEN]; + char* pWriteStart = &bufOut[0]; + char* pWrite = pWriteStart; + + // === iterate over input characters === int ix; - char bufOut[255]; - int nOut = 0; - for (ix = 0; ix < n; ++ix) - FSM_handleChar(&myGpo[0], buffer[ix], bufOut, &nOut); + for (ix = 0; ix < n; ++ix){ + FSM_handleChar(&myGpo[0], buffer[ix], &pWrite); - // === send reply === - if (nOut > 0) - client.send_all(bufOut, nOut); + // === send reply, if there is any === + // - when processing last char of incoming packet + // - when output buffer is too full + int nInBuf = pWrite - pWriteStart; + if (nInBuf > 0) + if ((ix == n-1) || (nInBuf > MAX_PACKETLEN - MAX_MSGLEN)){ + client.send_all(bufOut, nInBuf); + pWrite = pWriteStart; + } + } // for input character } // while connection + + conn_exit: client.close(); } // eternal main loop } // void main