The Ethernet-Board
Dependencies: EthernetInterface HTTPClient MODSERIAL mbed-dev mbed-rtos wolfSSL
Fork of sose2016_tr_oauth_pop by
Diff: main.cpp
- Revision:
- 7:5d116f2e5587
- Parent:
- 5:a837161f959a
--- a/main.cpp Sun Jun 26 21:19:39 2016 +0000 +++ b/main.cpp Tue Jul 12 15:25:26 2016 +0000 @@ -2,6 +2,7 @@ #include "EthernetInterface.h" #include "HTTPClient.h" #include "MODSERIAL.h" +#include "base64.h" #define SEND_BUF_SIZE 4096 @@ -13,9 +14,71 @@ EthernetInterface eth; HTTPClient http; -char str[32000]; +SPISlave device(D11, D12, D13, D10); + char str2[32000]; +char * token; + +int length_countdown = 4; + +char length_bytes[4]; +int length = 0; +int i = 0; + +bool onRead(char ch) +{ + if (ch == 0x11) NVIC_SystemReset(); // Reset + + // If length didn't get read yet... + if (length_countdown > 0){ + length_countdown--; + length_bytes[3-length_countdown] = ch; + + // if last lengthbyte got read... + if (length_countdown == 0){ + + //Set length and malloc the token. + length = length_bytes[0] + (length_bytes[1] << 8) + (length_bytes[2] << 16) + (length_bytes[3] << 24); + i = length; + token = (char*) malloc(sizeof(char)*length+1); + } + return false; + } + + //Read token + memset(token+(length-i),ch,1); + i--; + + //If token got read... + if (i == 0 and length_countdown == 0){ + //Set 0x00 byte to the end and return true. + memset(token+length,0x00,1); + length_countdown = 4; + return true; + } + return false; +} + +void encodeToken(){ + size_t len; + char * ptr = strtok(token,"|"); + + char * t1 = base64_encode((const unsigned char*)ptr,strlen(ptr),&len); + ptr = strtok(NULL,"|"); + char * t2 = base64_encode((const unsigned char*)ptr,strlen(ptr),&len); + ptr = strtok(NULL,"|"); + + free(token); + token = (char *) malloc(strlen(t1)+strlen(t2)+strlen(ptr)+3); + sprintf(token,"%s.%s.%s",t1,t2,ptr); + + free(t1); + free(t2); + free(ptr); + base64_cleanup(); +} + int main() { setbuf(stdout, NULL); eth.init(); @@ -23,42 +86,58 @@ R=!R;G=!G;B=!B; wait(1); + serial.printf("Start"); + + //Init SPI + device.format(8,3); + device.frequency(2000000); + + + while(1){ - // Wait for Token on Serial and read to str + + // Recieve Token + while(1){ + if(device.receive()) { + if (onRead(device.read())) break; + } + } + + //serial.printf("%s\r\n\r\n",token); + + // Encode the first two parts of the message to get the valid token + encodeToken(); + // Light Blue B=!B; - while(1){ - if(serial.readable()){ - serial.scanf("%s",&str); - break; - } - } + // Token Read, light Yellow R=!R;G=!G;B=!B; - //printf(str); - + // Send token to Server HTTPMap map; HTTPText inText(str2, 32000); map.put("client_id", "a1b2c3"); map.put("client_secret", "drickyoughurt"); map.put("token_type_hint", "pop_token"); - map.put("token", str); + map.put("token", token); + + int ret = http.post("https://kongsugar.de/introspection", map, &inText); - int ret = http.post("https://kongsugar.de/introspection", map, &inText); + free(token); + if (!ret) { - // Unlock, Light Green 5s - R=!R;wait(5);G=!G; + // Unlock, Light Green 5s + R=!R;wait(5);G=!G; } else { - // Light Red 5s - G=!G;wait(5);R=!R; + // Light Red 5s + G=!G;wait(5);R=!R; } - + // Bugfix, nervous user send token multiple times! - memset(&str[0], 0, sizeof(str)); memset(&str2[0], 0, sizeof(str2)); serial.rxBufferFlush(); }