PUD: refactor setupNodeIdBinaryLongLong
authorFerry Huberts <ferry.huberts@pelagic.nl>
Fri, 3 Feb 2012 12:57:29 +0000 (13:57 +0100)
committerFerry Huberts <ferry.huberts@pelagic.nl>
Tue, 14 Feb 2012 13:32:58 +0000 (14:32 +0100)
Signed-off-by: Ferry Huberts <ferry.huberts@pelagic.nl>
lib/pud/src/configuration.c

index 4ad8ad9..75f4b4c 100644 (file)
@@ -333,21 +333,30 @@ static bool setupNodeIdBinaryMAC(void) {
  */
 static bool setupNodeIdBinaryLongLong(unsigned long long min,
                unsigned long long max, unsigned int bytes) {
-       if (!nodeIdBinarySet) {
-               if (!readULL(PUD_NODE_ID_NAME, (char *) getNodeId(),
-                               &nodeIdBinary.longValue)) {
-                       return false;
-               }
-               nodeIdBinaryLength = bytes;
-               nodeIdBinarySet = true;
+       unsigned long long longValue = 0;
+       int i = bytes - 1;
+
+       if (!readULL(PUD_NODE_ID_NAME, (char *) getNodeId(), &longValue)) {
+               return false;
        }
 
-       if ((nodeIdBinary.longValue < min) || (nodeIdBinary.longValue > max)) {
+       if ((longValue < min) || (longValue > max)) {
                pudError(false, "%s value %llu is out of range [%llu,%llu]",
-                               PUD_NODE_ID_NAME, nodeIdBinary.longValue, min, max);
+                               PUD_NODE_ID_NAME, longValue, min, max);
                return false;
        }
 
+       while (i >= 0) {
+               ((unsigned char *)&nodeIdBinary.longValue)[i] = longValue & 0xff;
+               longValue >>= 8;
+               i--;
+       }
+
+       assert(longValue == 0);
+
+       nodeIdBinaryLength = bytes;
+       nodeIdBinarySet = true;
+
        if (setupNodeIdBinaryLongForOlsrCache(nodeIdBinary.longValue, bytes)) {
                return true;
        }