First Commit

Dependencies:   mbed Crypto_light mbed-rtos

Spin it 2 win it

Revision:
4:e1141c1d8b19
Parent:
3:2e32d7974962
Child:
5:fe9b21ba2e33
--- a/main.cpp	Mon Mar 12 11:58:08 2018 +0000
+++ b/main.cpp	Mon Mar 12 14:41:46 2018 +0000
@@ -22,6 +22,10 @@
 //Enum for putMessage message types
 #define MSG_HASHCOUNT 0
 #define MSG_NONCE_OK 1
+#define MSG_OVERFLOW 2
+//FIFO constant definitions
+#define MAX_ARRAY_SIZE 256 //check the variable fifo position if this is changed
+
 
 //Mapping from sequential drive states to motor phase outputs
 /*
@@ -45,17 +49,104 @@
 //Phase lead to make motor spin
 const int8_t lead = 2;  //2 for forwards, -2 for backwards
  
-//Instantiate the serial port
-Serial pc(SERIAL_TX, SERIAL_RX); 
+//Instantiate the serial port, using RawSerial to deal with 
+//serial's undocumented buffering behaviour 
+RawSerial pc(SERIAL_TX, SERIAL_RX); 
+
+//serial port ISR to take individual chars
+void serialISR(){
+ uint8_t newChar = pc.getc();
+ inCharQ.put((void*)newChar);
+ }
+//decode commands
+Thread decodeT;
 
+void decodeFn(){
+    pc.attach(&serialISR);
+    char charArray[MAX_ARRAY_SIZE] = "";
+    uint32_t bufferPosition = 0;                //change this variable type if the max buffer/fifio size is found to be different
+    bool exit = false;
+    while(!exit) { 
+    
+        //get new char
+        osEvent newEvent = inCharQ.get();
+        uint8_t newChar = (uint8_t)newEvent.value.p;
+        
+        //------error for overflow-------------------------// 
+        if(bufferPosition >= MAX_ARRAY_SIZE - 1){ 
+            exit = true; 
+            putMessage(MSG_OVERFLOW, bufferPositon); //
+        } 
+        //-------------------------------------------------//
+        
+        
+        
+        //check for carriage return "\r"
+        if(newChar == 'r'){
+            if(bufferpostion != 0){
+                if(charArray[bufferposition - 1] == '\\'){
+                    //carriage found 
+                    newChar = '0';  //replace character
+                    
+                    //add to array
+                    charArray[bufferPosition] = newChar; 
+                    
+                    //reset buffer
+                    bufferPosition = 0;
+                    //send char array to decoder ***
+                }
+            }
+        }
+        //Add new char to array
+        else{
+            //add character at current position 
+            charArray[bufferPosition] = newChar; 
+            bufferPosition ++;
+        }
+      
+     }//end of : while(!exit){} 
+        
+        
+        
+    //Place it on the end of a char[] array. Make the array large enough to contain the
+    //longest possible command. You will need to keep an index of the current buffer
+    //position. This would be easier with the C++ std::string class but there is not
+    //enough memory available for this. Using a C-style array is also faster and does not
+    //involve the uncertainty of dynamic memory allocation.
+    
+    //Include a test to make sure you do not write past the end of the buffer if the
+    //incoming string is too long. Buffer overflows have historically been the source of
+    //many software crashes and vulnerabilities since you may be able to alter the return
+    //address of a function if you write past the end of a buffer.
+    
+    
+    
+    
+    //If the incoming character is a carriage return ‘\r’ it indicates the end of a
+    //command. At this point:
+        //i. Place a string termination character ‘\0’ at the end of the command. This
+        //is used by functions like printf() and sscanf() to detect the end of the
+        //string.
+        //ii. Reset the buffer position index back to 0 ready to record the next command.
+        //iii. Test the first character to determine which command was sent.
+        //iv. Decode the rest of the command
 
+        
+
+    
+    
+    
+} 
+//structure for Mail Class
 typedef struct {
      uint8_t code;
      uint32_t data;
 } message_t ;
 
+//Mail class allowing 16 messages to be stored up in the FIFO
 Mail<message_t,16> outMessages;
 
+//Replacement for printf so that notification shortcodes can be sent 
 void putMessage(uint8_t code, uint32_t data)
 {
     message_t *pMessage = outMessages.alloc();
@@ -166,6 +257,8 @@
     
     commOutT.start(&commOutFn);
     
+    decodeT.start(&decodeFN); 
+    
     uint8_t sequence[] = {0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64,
                             0x20,0x53,0x79,0x73,0x74,0x65,0x6D,0x73,
                             0x20,0x61,0x72,0x65,0x20,0x66,0x75,0x6E,