Azure IoT / serializer

Dependents:   sht15_remote_monitoring f767zi_mqtt remote_monitoring simplesample_amqp ... more

Revision:
16:ef107f3f230c
Parent:
15:370c47516bd4
Child:
21:6d3dea1abd9c
diff -r 370c47516bd4 -r ef107f3f230c agenttypesystem.c
--- a/agenttypesystem.c	Fri Aug 26 12:59:27 2016 -0700
+++ b/agenttypesystem.c	Thu Oct 20 17:08:06 2016 -0700
@@ -85,27 +85,55 @@
 }
 
 
-static const char base64char[64] = { 
-    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 
-    'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 
-    'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 
-    'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 
-    'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 
-    'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', 
-    '8', '9', '-', '_'
-};
+#define IS_DIGIT(a) (('0'<=(a)) &&((a)<='9'))
+#define splitInt(intVal, bytePos)   (char)((intVal >> (bytePos << 3)) & 0xFF)
+#define joinChars(a, b, c, d) (uint32_t)( (uint32_t)a + ((uint32_t)b << 8) + ((uint32_t)c << 16) + ((uint32_t)d << 24))
+
+static char base64char(unsigned char val)
+{
+    char result;
 
-static const char base64b16[16] = { 
-    'A', 'E', 'I', 'M', 'Q', 'U', 'Y', 'c', 'g', 'k', 
-    'o', 's', 'w', '0', '4', '8'
-};
+    if (val < 26)
+    {
+        result = 'A' + (char)val;
+    }
+    else if (val < 52)
+    {
+        result = 'a' + ((char)val - 26);
+    }
+    else if (val < 62)
+    {
+        result = '0' + ((char)val - 52);
+    }
+    else if (val == 62)
+    {
+        result = '-';
+    }
+    else
+    {
+        result = '_';
+    }
 
-static const char base64b8[4] = {
-    'A' , 'Q' , 'g' , 'w'
+    return result;
+}
+
+static char base64b16(unsigned char val)
+{
+    const uint32_t base64b16values[4] = {
+        joinChars('A', 'E', 'I', 'M'),
+        joinChars('Q', 'U', 'Y', 'c'),
+        joinChars('g', 'k', 'o', 's'),
+        joinChars('w', '0', '4', '8')
+    };
+    return splitInt(base64b16values[val >> 2], (val & 0x03));
+}
+
+static char base64b8(unsigned char val)
+{
+    const uint32_t base64b8values = joinChars('A', 'Q', 'g', 'w');
+    return splitInt(base64b8values, val);
 };
 
-#define IS_DIGIT(a) (('0'<=(a)) &&((a)<='9'))
-
 /*creates an AGENT_DATA_TYPE containing a EDM_BOOLEAN from a int*/
 AGENT_DATA_TYPES_RESULT Create_EDM_BOOLEAN_from_int(AGENT_DATA_TYPE* agentData, int v)
 {
@@ -559,7 +587,7 @@
     unsigned char i;
     for (i = 0; i <= 15; i++)
     {
-        if (base64b16[i] == source)
+        if (base64b16(i) == source)
         {
             *destination = i;
             return 0;
@@ -574,7 +602,7 @@
     unsigned char i;
     for (i = 0; i <= 3; i++)
     {
-        if (base64b8[i] == source)
+        if (base64b8(i) == source)
         {
             *destination = i;
             return 0;
@@ -2220,18 +2248,18 @@
                     temp[destinationPointer++] = '"';
                     while (value->value.edmBinary.size - currentPosition >= 3)
                     {
-                        char c1 = base64char[value->value.edmBinary.data[currentPosition] >> 2];
-                        char c2 = base64char[
+                        char c1 = base64char(value->value.edmBinary.data[currentPosition] >> 2);
+                        char c2 = base64char(
                             ((value->value.edmBinary.data[currentPosition] & 3) << 4) |
                                 (value->value.edmBinary.data[currentPosition + 1] >> 4)
-                        ];
-                        char c3 = base64char[
+                        );
+                        char c3 = base64char(
                             ((value->value.edmBinary.data[currentPosition + 1] & 0x0F) << 2) |
                                 ((value->value.edmBinary.data[currentPosition + 2] >> 6) & 3)
-                        ];
-                        char c4 = base64char[
+                        );
+                        char c4 = base64char(
                             value->value.edmBinary.data[currentPosition + 2] & 0x3F
-                        ];
+                        );
                         currentPosition += 3;
                         temp[destinationPointer++] = c1;
                         temp[destinationPointer++] = c2;
@@ -2241,12 +2269,12 @@
                     }
                     if (value->value.edmBinary.size - currentPosition == 2)
                     {
-                        char c1 = base64char[value->value.edmBinary.data[currentPosition] >> 2];
-                        char c2 = base64char[
+                        char c1 = base64char(value->value.edmBinary.data[currentPosition] >> 2);
+                        char c2 = base64char(
                             ((value->value.edmBinary.data[currentPosition] & 0x03) << 4) |
                                 (value->value.edmBinary.data[currentPosition + 1] >> 4)
-                        ];
-                        char c3 = base64b16[value->value.edmBinary.data[currentPosition + 1] & 0x0F];
+                        );
+                        char c3 = base64b16(value->value.edmBinary.data[currentPosition + 1] & 0x0F);
                         temp[destinationPointer++] = c1;
                         temp[destinationPointer++] = c2;
                         temp[destinationPointer++] = c3;
@@ -2254,8 +2282,8 @@
                     }
                     else if (value->value.edmBinary.size - currentPosition == 1)
                     {
-                        char c1 = base64char[value->value.edmBinary.data[currentPosition] >> 2];
-                        char c2 = base64b8[value->value.edmBinary.data[currentPosition] & 0x03];
+                        char c1 = base64char(value->value.edmBinary.data[currentPosition] >> 2);
+                        char c2 = base64b8(value->value.edmBinary.data[currentPosition] & 0x03);
                         temp[destinationPointer++] = c1;
                         temp[destinationPointer++] = c2;
                         temp[destinationPointer++] = '=';
@@ -2923,20 +2951,30 @@
 
 #define isLeapYear(y) ((((y) % 400) == 0) || (((y)%4==0)&&(!((y)%100==0))))
 
-const int daysInAllPreviousMonths[12] = {
-    0,
-    31,
-    31 + 28,
-    31 + 28 + 31,
-    31 + 28 + 31 + 30,
-    31 + 28 + 31 + 30 + 31,
-    31 + 28 + 31 + 30 + 31 + 30,
-    31 + 28 + 31 + 30 + 31 + 30 + 31,
-    31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
-    31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
-    31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
-    31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
-};
+static int daysInAllPreviousMonths(int month)
+{
+    int result = 0;
+    for (int i = 0; i < month; i++)
+    {
+        switch (i)
+        {
+        case 0:
+        case 2:
+        case 4:
+        case 6:
+        case 7:
+        case 9:
+            result += 31;
+            break;
+        case 1:
+            result += 28;
+            break;
+        default:
+            result += 30;
+        }
+    }
+    return result;
+}
 
 /*this function assumes a correctly filled in tm_year, tm_mon and tm_mday and will fill in tm_yday and tm_wday*/
 static void fill_tm_yday_and_tm_wday(struct tm* source)
@@ -2946,7 +2984,7 @@
     /*1st of Jan is day "0" in a year*/
     int year = source->tm_year + 1900 + 10000;
     int nLeapYearsSinceYearMinus9999 = ((year - 1) / 4) - ((year - 1) / 100) + ((year - 1) / 400);
-    source->tm_yday = (daysInAllPreviousMonths[source->tm_mon]) + (source->tm_mday - 1) + ((source->tm_mon > 1 /*1 is Feb*/) && isLeapYear(year));
+    source->tm_yday = (daysInAllPreviousMonths(source->tm_mon)) + (source->tm_mday - 1) + ((source->tm_mon > 1 /*1 is Feb*/) && isLeapYear(year));
     source->tm_wday = ((365 * year + nLeapYearsSinceYearMinus9999) + source->tm_yday) % 7;
     /*day "0" is 1st jan -9999 (this is how much odata can span*/
     /*the function shall count days */