Aes encryption code

Dependencies:   Crypto USBDevice mbed

Fork of larada by MZJ

Revision:
6:634306947b58
Parent:
5:7214d56ee5ae
Child:
7:ed473cf0afaf
--- a/main.cpp	Wed Mar 09 20:50:13 2016 +0000
+++ b/main.cpp	Sat Mar 12 10:20:20 2016 +0000
@@ -4,7 +4,7 @@
 #include "Crypto.h"
 
 #define USE_CIPHER
-//#define FAKE_HW
+#define FAKE_HW
 
 #define SERIAL //comment for USB operation, uncomment for serial
 #define CALIBRATE 0
@@ -44,6 +44,7 @@
 #define BYTES_PER_100_MS 8 // the number of bytes to send per 100 ms
 #define TIP_UPDATE_INTERVAL_S 3 // Update the tip remaining time every x seconds
 #define TIP_READ_BLANK_TIME_MS 70
+#define TIP_TIME_BETWEEN_MESSAGES_MS 500 // Wait at least xms between sending messages
 
 float filter = DEFAULT_FILTER;
 uint8_t initial_duty = DEFAULT_INITIAL_DUTY;
@@ -144,7 +145,8 @@
 unsigned long current_time = 0L;
 uint32_t tip_start_value_s = 0L; // The number of seconds remaing on the tip at the start of the cycle
 uint32_t current_cycle_on_time_s = 0; // The number of seconds this cycle has been operational
-uint32_t last_serial_send = 0;
+uint32_t last_serial_send = 0; // Last time something was sent on the tip serial connection
+uint32_t last_message_send = 0; // Last time a message was sent on te tip serial connection
 bool connected = false;
 volatile uint32_t millis = 0;
 
@@ -248,10 +250,11 @@
  * Sends a message to the tip. This accounts for the
  * max number of bytes which can be sent per 100ms.
  *
- * @param *tm - the message to send
+ * @param *tm  - the message to send
+ * @param block - when true wait for TIP_TIME_BETWEEN_MESSAGES_MS to elapse, else return
  * @returns true if sent successfully
  */
-bool send_message_to_tip(struct tip_message *tm)
+bool send_message_to_tip(struct tip_message *tm, bool block=false)
 {
     char buff[CRYPT_BUFF_SZ] = {0};
     char send_buff[BYTES_PER_100_MS] = {0};
@@ -260,6 +263,18 @@
     
     int bytes_sent = 0;
     
+    if (!block && (get_time() - last_message_send) < TIP_TIME_BETWEEN_MESSAGES_MS)
+    {
+        return false;
+    }
+    else
+    {
+        while (get_time() - last_message_send < TIP_TIME_BETWEEN_MESSAGES_MS)
+        {
+            
+        }    
+    }
+    
     pack_message(buff, sizeof(buff), tm);
     
     #ifdef USE_CIPHER
@@ -302,6 +317,7 @@
     wait_until_can_send_tip_message(last_send);
     pc.printf("\r\n");
     last_serial_send = get_time();
+    last_message_send = last_serial_send;
     
     return true; 
 }
@@ -751,8 +767,8 @@
 
 void init(void){
 #ifdef SERIAL
-  pc.baud(4800);
-  connected = true;
+  pc.baud(9600);
+  //connected = true;
 #else
   pc.connect();
   connected = pc.vbusDetected();
@@ -881,10 +897,6 @@
                 _state = 2;
                 sent_time = get_time();
             }
-            else
-            {
-                _state = -1;    
-            }
             break;
         }
         case 2:
@@ -988,8 +1000,10 @@
     tm.command = WRITE_TIME_REMAINING;
     tm.value   = tip_start_value_s - (current_time / 1000);
     
-    send_message_to_tip(&tm);
-    last_tip_update = current_time;
+    if (send_message_to_tip(&tm))
+    {
+        last_tip_update = current_time;
+    }
   }
   
   // check if we're done
@@ -1007,7 +1021,7 @@
       tm.command = WRITE_TIME_REMAINING;
       tm.value   = tip_start_value_s - (current_time / 1000);
     
-      send_message_to_tip(&tm);
+      send_message_to_tip(&tm, true);
       
       // The tip has been removed or is out of juice
       current_cycle_on_time_s += current_time / 1000L;
@@ -1107,9 +1121,9 @@
   
   while(1){
     
-    getInput();
+    //getInput();
     
-    check_on_off();
+    //check_on_off();
     
     bool state_change = false;
     if(state != last_state){