The Ethernet-Board

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

Fork of sose2016_tr_oauth_pop by niko gillen

Committer:
Cataract
Date:
Tue Jul 12 15:25:26 2016 +0000
Revision:
7:5d116f2e5587
Parent:
5:a837161f959a
final commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
coolnik 0:64d0faaa4e6e 1 #include "mbed.h"
coolnik 0:64d0faaa4e6e 2 #include "EthernetInterface.h"
coolnik 0:64d0faaa4e6e 3 #include "HTTPClient.h"
coolnik 5:a837161f959a 4 #include "MODSERIAL.h"
Cataract 7:5d116f2e5587 5 #include "base64.h"
coolnik 5:a837161f959a 6
coolnik 5:a837161f959a 7 #define SEND_BUF_SIZE 4096
coolnik 0:64d0faaa4e6e 8
coolnik 3:e91a03086c72 9 DigitalOut R(LED_RED);
coolnik 3:e91a03086c72 10 DigitalOut G(LED_GREEN);
coolnik 3:e91a03086c72 11 DigitalOut B(LED_BLUE);
coolnik 1:5f0df6a77d62 12
coolnik 5:a837161f959a 13 MODSERIAL serial(USBTX, USBRX,4096); // tx, rx
coolnik 0:64d0faaa4e6e 14 EthernetInterface eth;
coolnik 0:64d0faaa4e6e 15 HTTPClient http;
coolnik 5:a837161f959a 16
Cataract 7:5d116f2e5587 17 SPISlave device(D11, D12, D13, D10);
Cataract 7:5d116f2e5587 18
coolnik 3:e91a03086c72 19 char str2[32000];
coolnik 0:64d0faaa4e6e 20
Cataract 7:5d116f2e5587 21 char * token;
Cataract 7:5d116f2e5587 22
Cataract 7:5d116f2e5587 23 int length_countdown = 4;
Cataract 7:5d116f2e5587 24
Cataract 7:5d116f2e5587 25 char length_bytes[4];
Cataract 7:5d116f2e5587 26 int length = 0;
Cataract 7:5d116f2e5587 27 int i = 0;
Cataract 7:5d116f2e5587 28
Cataract 7:5d116f2e5587 29 bool onRead(char ch)
Cataract 7:5d116f2e5587 30 {
Cataract 7:5d116f2e5587 31 if (ch == 0x11) NVIC_SystemReset(); // Reset
Cataract 7:5d116f2e5587 32
Cataract 7:5d116f2e5587 33 // If length didn't get read yet...
Cataract 7:5d116f2e5587 34 if (length_countdown > 0){
Cataract 7:5d116f2e5587 35 length_countdown--;
Cataract 7:5d116f2e5587 36 length_bytes[3-length_countdown] = ch;
Cataract 7:5d116f2e5587 37
Cataract 7:5d116f2e5587 38 // if last lengthbyte got read...
Cataract 7:5d116f2e5587 39 if (length_countdown == 0){
Cataract 7:5d116f2e5587 40
Cataract 7:5d116f2e5587 41 //Set length and malloc the token.
Cataract 7:5d116f2e5587 42 length = length_bytes[0] + (length_bytes[1] << 8) + (length_bytes[2] << 16) + (length_bytes[3] << 24);
Cataract 7:5d116f2e5587 43 i = length;
Cataract 7:5d116f2e5587 44 token = (char*) malloc(sizeof(char)*length+1);
Cataract 7:5d116f2e5587 45 }
Cataract 7:5d116f2e5587 46 return false;
Cataract 7:5d116f2e5587 47 }
Cataract 7:5d116f2e5587 48
Cataract 7:5d116f2e5587 49 //Read token
Cataract 7:5d116f2e5587 50 memset(token+(length-i),ch,1);
Cataract 7:5d116f2e5587 51 i--;
Cataract 7:5d116f2e5587 52
Cataract 7:5d116f2e5587 53 //If token got read...
Cataract 7:5d116f2e5587 54 if (i == 0 and length_countdown == 0){
Cataract 7:5d116f2e5587 55 //Set 0x00 byte to the end and return true.
Cataract 7:5d116f2e5587 56 memset(token+length,0x00,1);
Cataract 7:5d116f2e5587 57 length_countdown = 4;
Cataract 7:5d116f2e5587 58 return true;
Cataract 7:5d116f2e5587 59 }
Cataract 7:5d116f2e5587 60 return false;
Cataract 7:5d116f2e5587 61 }
Cataract 7:5d116f2e5587 62
Cataract 7:5d116f2e5587 63 void encodeToken(){
Cataract 7:5d116f2e5587 64 size_t len;
Cataract 7:5d116f2e5587 65 char * ptr = strtok(token,"|");
Cataract 7:5d116f2e5587 66
Cataract 7:5d116f2e5587 67 char * t1 = base64_encode((const unsigned char*)ptr,strlen(ptr),&len);
Cataract 7:5d116f2e5587 68 ptr = strtok(NULL,"|");
Cataract 7:5d116f2e5587 69 char * t2 = base64_encode((const unsigned char*)ptr,strlen(ptr),&len);
Cataract 7:5d116f2e5587 70 ptr = strtok(NULL,"|");
Cataract 7:5d116f2e5587 71
Cataract 7:5d116f2e5587 72 free(token);
Cataract 7:5d116f2e5587 73 token = (char *) malloc(strlen(t1)+strlen(t2)+strlen(ptr)+3);
Cataract 7:5d116f2e5587 74 sprintf(token,"%s.%s.%s",t1,t2,ptr);
Cataract 7:5d116f2e5587 75
Cataract 7:5d116f2e5587 76 free(t1);
Cataract 7:5d116f2e5587 77 free(t2);
Cataract 7:5d116f2e5587 78 free(ptr);
Cataract 7:5d116f2e5587 79 base64_cleanup();
Cataract 7:5d116f2e5587 80 }
Cataract 7:5d116f2e5587 81
coolnik 0:64d0faaa4e6e 82 int main() {
coolnik 0:64d0faaa4e6e 83 setbuf(stdout, NULL);
coolnik 4:00b8d595aa33 84 eth.init();
coolnik 0:64d0faaa4e6e 85 eth.connect();
coolnik 3:e91a03086c72 86 R=!R;G=!G;B=!B;
coolnik 3:e91a03086c72 87 wait(1);
coolnik 1:5f0df6a77d62 88
Cataract 7:5d116f2e5587 89 serial.printf("Start");
Cataract 7:5d116f2e5587 90
Cataract 7:5d116f2e5587 91 //Init SPI
Cataract 7:5d116f2e5587 92 device.format(8,3);
Cataract 7:5d116f2e5587 93 device.frequency(2000000);
Cataract 7:5d116f2e5587 94
Cataract 7:5d116f2e5587 95
Cataract 7:5d116f2e5587 96
coolnik 1:5f0df6a77d62 97 while(1){
Cataract 7:5d116f2e5587 98
Cataract 7:5d116f2e5587 99 // Recieve Token
Cataract 7:5d116f2e5587 100 while(1){
Cataract 7:5d116f2e5587 101 if(device.receive()) {
Cataract 7:5d116f2e5587 102 if (onRead(device.read())) break;
Cataract 7:5d116f2e5587 103 }
Cataract 7:5d116f2e5587 104 }
Cataract 7:5d116f2e5587 105
Cataract 7:5d116f2e5587 106 //serial.printf("%s\r\n\r\n",token);
Cataract 7:5d116f2e5587 107
Cataract 7:5d116f2e5587 108 // Encode the first two parts of the message to get the valid token
Cataract 7:5d116f2e5587 109 encodeToken();
Cataract 7:5d116f2e5587 110
coolnik 3:e91a03086c72 111 // Light Blue
coolnik 3:e91a03086c72 112 B=!B;
Cataract 7:5d116f2e5587 113
coolnik 3:e91a03086c72 114 // Token Read, light Yellow
coolnik 3:e91a03086c72 115 R=!R;G=!G;B=!B;
Cataract 7:5d116f2e5587 116
coolnik 1:5f0df6a77d62 117 // Send token to Server
coolnik 2:1ffd9e2ce7df 118 HTTPMap map;
coolnik 3:e91a03086c72 119 HTTPText inText(str2, 32000);
coolnik 3:e91a03086c72 120 map.put("client_id", "a1b2c3");
coolnik 3:e91a03086c72 121 map.put("client_secret", "drickyoughurt");
coolnik 4:00b8d595aa33 122 map.put("token_type_hint", "pop_token");
Cataract 7:5d116f2e5587 123 map.put("token", token);
Cataract 7:5d116f2e5587 124
Cataract 7:5d116f2e5587 125 int ret = http.post("https://kongsugar.de/introspection", map, &inText);
coolnik 3:e91a03086c72 126
Cataract 7:5d116f2e5587 127 free(token);
Cataract 7:5d116f2e5587 128
coolnik 1:5f0df6a77d62 129 if (!ret)
coolnik 1:5f0df6a77d62 130 {
Cataract 7:5d116f2e5587 131 // Unlock, Light Green 5s
Cataract 7:5d116f2e5587 132 R=!R;wait(5);G=!G;
coolnik 1:5f0df6a77d62 133 }
coolnik 1:5f0df6a77d62 134 else
coolnik 1:5f0df6a77d62 135 {
Cataract 7:5d116f2e5587 136 // Light Red 5s
Cataract 7:5d116f2e5587 137 G=!G;wait(5);R=!R;
coolnik 1:5f0df6a77d62 138 }
Cataract 7:5d116f2e5587 139
coolnik 5:a837161f959a 140 // Bugfix, nervous user send token multiple times!
coolnik 5:a837161f959a 141 memset(&str2[0], 0, sizeof(str2));
coolnik 5:a837161f959a 142 serial.rxBufferFlush();
coolnik 0:64d0faaa4e6e 143 }
coolnik 0:64d0faaa4e6e 144 }