tracker.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Local imports
  2. #include "crypto.h"
  3. #include "rng.h"
  4. #include <WiFi.h>
  5. #include <WiFiClient.h>
  6. // Energia constructors
  7. #include "Arduino.h"
  8. #define MLEN 32
  9. void setup();
  10. void loop();
  11. char ssid[255] = {0};
  12. char pass[255] = {0};
  13. uint16_t ID = 1050; // Device ID
  14. uint16_t port = 9000;
  15. IPAddress server(192,168,0,10);
  16. WiFiClient wclient;
  17. uint8_t aes_key[16] = {
  18. 0x03, 0x02, 0x01, 0x00,
  19. 0x07, 0x06, 0x05, 0x04,
  20. 0x0b, 0x0a, 0x09, 0x08,
  21. 0x0f, 0x0e, 0x0d, 0x0c
  22. };
  23. typedef struct {
  24. uint16_t ID;
  25. uint16_t LEN;
  26. } header;
  27. uint8_t AESData[MLEN], *AESResult, AESResult2[MLEN];
  28. uint32_t AESDataLength=MLEN;
  29. void setupWifi() {
  30. if(ssid[0] != 0x00) return;
  31. int32_t pnt = 0;
  32. bool typingp = false;
  33. Serial.print("SSID: ");
  34. while (1) {
  35. if(Serial.available() > 0) {
  36. if(!typingp) {
  37. ssid[pnt] = Serial.read();
  38. if(ssid[pnt] == 13) {
  39. ssid[pnt] = 0x00;
  40. typingp = true;
  41. pnt = -1;
  42. Serial.println();
  43. Serial.print("PASS: ");
  44. }
  45. else Serial.write(ssid[pnt]);
  46. } else {
  47. pass[pnt] = Serial.read();
  48. if(pass[pnt] == 13) {
  49. pass[pnt] = 0x00;
  50. break;
  51. }
  52. else Serial.write(pass[pnt]);
  53. }
  54. pnt ++;
  55. }
  56. }
  57. Serial.println();
  58. }
  59. void setup() {
  60. // initialising
  61. Serial.begin(9600);
  62. Serial.print("Setup RNG ..");
  63. RNGSetup();
  64. Serial.println("OK");
  65. Serial.print("Setup AES ..");
  66. AESSetup(AES_KEY_128BIT, aes_key);
  67. Serial.println("OK");
  68. setupWifi();
  69. Serial.print("Setup WiFi ..");
  70. WiFi.begin(ssid, pass);
  71. while (WiFi.status() != WL_CONNECTED) {delay(200);}
  72. Serial.println("OK");
  73. Serial.print("Setup DHCP ..");
  74. while (WiFi.localIP() == INADDR_NONE) {delay(200);}
  75. Serial.print("OK ");
  76. Serial.println(WiFi.localIP());
  77. Serial.print("Setup Socket ..");
  78. while(wclient.connect(server, port) == false) {delay(200);}
  79. Serial.println("OK");
  80. //String msg = "This is secret!";
  81. //msg.toCharArray((char *)AESData, MLEN);
  82. //
  83. // Serial.print("KEY: ");
  84. // for(int i=0;i<16;i++) {
  85. // Serial.print(aes_key[i], HEX);
  86. // Serial.print(' ');
  87. // }
  88. // Serial.println();
  89. //
  90. // Serial.print("IV: ");
  91. // for(int i=0;i<16;i++) {
  92. // Serial.print(aes_iv[i], HEX);
  93. // Serial.print(' ');
  94. // }
  95. // Serial.println();
  96. //
  97. // Serial.print("Message: ");
  98. // for(int i=0;i<MLEN;i++) {
  99. // Serial.print(AESData[i], HEX);
  100. // Serial.print(' ');
  101. // }
  102. // Serial.println();
  103. //
  104. //AESResult = AESEncrypt(AESData, AESDataLength);
  105. // AESCrypt(AES_DIR_ENCRYPT, aes_iv, AESData, AESResult, AESDataLength);
  106. //Serial.print("Encrypted: ");
  107. //for(uint32_t i=0;i<AESDataLength+getBlockSize();i++) {
  108. //Serial.print(AESResult[i], HEX);
  109. //Serial.print(' ');
  110. //}
  111. //Serial.println();
  112. // AESCrypt(AES_DIR_DECRYPT, &aesv, AESResult, AESResult2, AESDataLength);
  113. // Serial.print("Decrypted: ");
  114. // for(int i=0;i<MLEN;i++) {
  115. // Serial.print(AESResult2[i], HEX);
  116. // Serial.print(' ');
  117. // }
  118. // Serial.println();
  119. }
  120. uint8_t buffer[32] = {0};
  121. uint16_t bufferPnt = 0;
  122. void send() {
  123. uint32_t resLen = 0;
  124. Serial.print("encrypting..");
  125. uint8_t *res = AESEncrypt(buffer, 32, &resLen);
  126. Serial.println(resLen);
  127. header h = {ID, resLen};
  128. wclient.write((uint8_t *) &h, sizeof(h));
  129. wclient.write(res, (uint16_t) resLen);
  130. free(res);
  131. memset(buffer, 0x00, 32);
  132. bufferPnt = 0;
  133. }
  134. void loop() {
  135. while (Serial.available() > 0) {
  136. uint8_t val = Serial.read();
  137. if ((val == 8 || val == 127 || val == 27) && bufferPnt > 0) {
  138. bufferPnt--;
  139. Serial.write(val); // echo
  140. continue;
  141. }
  142. if (val == 26 && buffer[bufferPnt] != 0x00) {
  143. bufferPnt++;
  144. Serial.write(val); // echo
  145. continue;
  146. }
  147. if(val == 13) {
  148. send();
  149. Serial.println();
  150. continue;
  151. }
  152. Serial.write(val); // echo
  153. if(bufferPnt >= 32) send();
  154. buffer[bufferPnt] = val;
  155. bufferPnt++;
  156. }
  157. }