Weimen Li / Mbed 2 deprecated MAE433_Library

Dependencies:   mbed-dsp mbed-rtos mbed

Dependents:   MAE433_Library_Tester RobotBalancerv2

Files at this revision

API Documentation at this revision

Comitter:
Electrotiger
Date:
Tue Jun 28 01:24:36 2016 +0000
Parent:
4:2d38ad348e0d
Child:
6:076fcae78bac
Commit message:
Added checks to ensure that buffer overflow will not occur in the HC-06 Bluetooth module when receiving data.

Changed in this revision

HC06Bluetooth.cpp Show annotated file Show diff for this revision Revisions of this file
HC06Bluetooth.hpp Show annotated file Show diff for this revision Revisions of this file
QuadEnc.hpp Show annotated file Show diff for this revision Revisions of this file
--- a/HC06Bluetooth.cpp	Sat Jun 25 00:37:31 2016 +0000
+++ b/HC06Bluetooth.cpp	Tue Jun 28 01:24:36 2016 +0000
@@ -105,24 +105,28 @@
             charCallbackFunc(receivedChar);
         }
 
-        // If the character is a newline or carriage return, then call the line callback function.
-        if ((receivedChar == '\n') || (receivedChar == '\r')) {
-            // Terminate the buffer with a null character, since that is what strings end with.
-            receivedChar = '\0';
-            dataReceivedBuffer[dataReceivedBufferPos] = receivedChar;
-            // Copy data from the buffer to a copy.
-            std::copy(dataReceivedBuffer, dataReceivedBuffer + dataReceivedBufferPos, dataReceivedBufferCopy);
-            // Reset the buffer position.
-            dataReceivedBufferPos = 0;
-            // Call the callback function.
-            if (lineCallbackFunc != NULL) {
-                lineCallbackFunc((const char*)dataReceivedBuffer);
+        if (lineCallbackFunc != NULL) {
+            // If the character is a newline or carriage return, then call the line callback function.
+            if ((receivedChar == '\n') || (receivedChar == '\r')) {
+                // Terminate the buffer with a null character, since that is what strings end with.
+                receivedChar = '\0';
+                dataReceivedBuffer[dataReceivedBufferPos] = receivedChar;
+                // Copy data from the buffer to a copy.
+                std::copy(dataReceivedBuffer, dataReceivedBuffer + dataReceivedBufferPos, dataReceivedBufferCopy);
+                // Reset the buffer position.
+                dataReceivedBufferPos = 0;
+                // Call the callback function.
+                if (lineCallbackFunc != NULL) {
+                    lineCallbackFunc((const char*)dataReceivedBuffer);
+                }
             }
-        }
-        // Otherwise, just place it in the buffer and move on.
-        else {
-            dataReceivedBuffer[dataReceivedBufferPos] = receivedChar;
-            dataReceivedBufferPos++;
+            // Otherwise, just place it in the buffer and move on.
+            else {
+                if (dataReceivedBufferPos < dataBufferSize) {
+                    dataReceivedBuffer[dataReceivedBufferPos] = receivedChar;
+                    dataReceivedBufferPos++;
+                }
+            }
         }
     }
 }
--- a/HC06Bluetooth.hpp	Sat Jun 25 00:37:31 2016 +0000
+++ b/HC06Bluetooth.hpp	Tue Jun 28 01:24:36 2016 +0000
@@ -59,6 +59,8 @@
 #include "mbed.h"
 #include <string>
 
+    const int dataBufferSize = 256;
+
 class HC06Bluetooth {
 public: // Public methods.
     /**
@@ -101,9 +103,9 @@
     void (*lineCallbackFunc) (const char*);
     /// Pointer to a callback function the client provides when a character is received.
     void (*charCallbackFunc) (char);
-    char dataReceivedBuffer[256];
+    char dataReceivedBuffer[dataBufferSize];
     int32_t dataReceivedBufferPos;
-    char dataReceivedBufferCopy[256];
+    char dataReceivedBufferCopy[dataBufferSize];
 };
 
 #endif /* HC06BLUETOOTH_H_ */
--- a/QuadEnc.hpp	Sat Jun 25 00:37:31 2016 +0000
+++ b/QuadEnc.hpp	Tue Jun 28 01:24:36 2016 +0000
@@ -3,8 +3,8 @@
  * @date June 2nd, 2016
  * @author Weimen Li
  * @brief Header file for a class which encapsulates a quadrature encoder.
- * @remark The current implementation does not give the Quadrature Encoder maximum
- * priority, but it really should.
+ * @remark The algorith used to read from the encoder may be found at:
+ * http://makeatronics.blogspot.com/2013/02/efficiently-reading-quadrature-with.html
  */
 
 #ifndef QUADENC_H_