The Ethernet-Board

Dependencies:   EthernetInterface HTTPClient MODSERIAL mbed-dev mbed-rtos wolfSSL

Fork of sose2016_tr_oauth_pop by niko gillen

Files at this revision

API Documentation at this revision

Comitter:
Cataract
Date:
Tue Jul 12 15:25:26 2016 +0000
Parent:
6:e844935142d5
Commit message:
final commit

Changed in this revision

base64.cpp Show annotated file Show diff for this revision Revisions of this file
base64.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r e844935142d5 -r 5d116f2e5587 base64.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/base64.cpp	Tue Jul 12 15:25:26 2016 +0000
@@ -0,0 +1,93 @@
+#include <stdint.h>
+#include <stdlib.h>
+
+
+static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
+                                'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
+                                'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
+                                'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
+                                'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
+                                'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
+                                'w', 'x', 'y', 'z', '0', '1', '2', '3',
+                                '4', '5', '6', '7', '8', '9', '-', '_'};
+static char *decoding_table = NULL;
+static int mod_table[] = {0, 2, 1};
+
+
+void build_decoding_table() {
+
+    decoding_table = (char*) malloc(256);
+
+    for (int i = 0; i < 64; i++)
+        decoding_table[(unsigned char) encoding_table[i]] = i;
+}
+
+
+void base64_cleanup() {
+    free(decoding_table);
+}
+
+char *base64_encode(const unsigned char *data,
+                    size_t input_length,
+                    size_t *output_length) {
+
+    *output_length = 4 * ((input_length +2) / 3);
+
+    char *encoded_data = (char*) malloc(*output_length);
+    if (encoded_data == NULL) return NULL;
+
+    for (int i = 0, j = 0; i < input_length;) {
+
+        uint32_t octet_a = i < input_length ? (unsigned char)data[i++] : 0;
+        uint32_t octet_b = i < input_length ? (unsigned char)data[i++] : 0;
+        uint32_t octet_c = i < input_length ? (unsigned char)data[i++] : 0;
+
+        uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
+
+        encoded_data[j++] = encoding_table[(triple >> 3 * 6) & 0x3F];
+        encoded_data[j++] = encoding_table[(triple >> 2 * 6) & 0x3F];
+        encoded_data[j++] = encoding_table[(triple >> 1 * 6) & 0x3F];
+        encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F];
+    }
+
+    for (int i = 0; i < mod_table[input_length % 3]; i++)
+        encoded_data[*output_length - 1 - i] = 0x00;
+
+    return encoded_data;
+}
+
+
+unsigned char *base64_decode(const char *data,
+                             size_t input_length,
+                             size_t *output_length) {
+
+    if (decoding_table == NULL) build_decoding_table();
+
+    if (input_length % 4 != 0) return NULL;
+
+    *output_length = input_length / 4 * 3;
+    if (data[input_length - 1] == '=') (*output_length)--;
+    if (data[input_length - 2] == '=') (*output_length)--;
+
+    unsigned char *decoded_data = (unsigned char*) malloc(*output_length);
+    if (decoded_data == NULL) return NULL;
+
+    for (int i = 0, j = 0; i < input_length;) {
+
+        uint32_t sextet_a = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
+        uint32_t sextet_b = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
+        uint32_t sextet_c = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
+        uint32_t sextet_d = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
+
+        uint32_t triple = (sextet_a << 3 * 6)
+        + (sextet_b << 2 * 6)
+        + (sextet_c << 1 * 6)
+        + (sextet_d << 0 * 6);
+
+        if (j < *output_length) decoded_data[j++] = (triple >> 2 * 8) & 0xFF;
+        if (j < *output_length) decoded_data[j++] = (triple >> 1 * 8) & 0xFF;
+        if (j < *output_length) decoded_data[j++] = (triple >> 0 * 8) & 0xFF;
+    }
+
+    return decoded_data;
+}
\ No newline at end of file
diff -r e844935142d5 -r 5d116f2e5587 base64.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/base64.h	Tue Jul 12 15:25:26 2016 +0000
@@ -0,0 +1,9 @@
+char *base64_encode(const unsigned char *data,
+                    size_t input_length,
+                    size_t *output_length);
+                    
+unsigned char *base64_decode(const char *data,
+                             size_t input_length,
+                             size_t *output_length);
+                             
+void base64_cleanup();
\ No newline at end of file
diff -r e844935142d5 -r 5d116f2e5587 main.cpp
--- 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();
     }