PUD: pull nodeIdBinary status information into type
authorFerry Huberts <ferry.huberts@pelagic.nl>
Fri, 3 Feb 2012 18:15:40 +0000 (19:15 +0100)
committerFerry Huberts <ferry.huberts@pelagic.nl>
Tue, 14 Feb 2012 13:32:59 +0000 (14:32 +0100)
Signed-off-by: Ferry Huberts <ferry.huberts@pelagic.nl>
lib/pud/src/configuration.c
lib/pud/src/configuration.h
lib/pud/src/gpsConversion.c
lib/pud/wireformat/include/OlsrdPudWireFormat/nodeIdConversion.h
lib/pud/wireformat/include/OlsrdPudWireFormat/wireFormat.h
lib/pud/wireformat/src/nodeIdConversion.c

index 3520e35..bf7c1bf 100644 (file)
@@ -200,15 +200,9 @@ static size_t nodeIdLength = 0;
 /** True when the nodeId is set */
 static bool nodeIdSet = false;
 
-/** The nodeId as a binary representation */
+/** The nodeId as a binary representation, with status */
 static nodeIdBinaryType nodeIdBinary;
 
-/** The length of the string in the nodeIdBinary buffer */
-static size_t nodeIdBinaryLength = 0;
-
-/** True when the nodeIdBinary is set */
-static bool nodeIdBinarySet = false;
-
 /**
  @return
  The node ID
@@ -240,25 +234,17 @@ unsigned char * getNodeIdWithLength(size_t *length) {
 }
 
 /**
- Get the nodeIdBinary and its length
-
- @param length
- a pointer to the variable in which to store the nodeIdBinary length (allowed to be
- NULL, in which case the length is not stored)
+ Get the nodeIdBinary
 
  @return
  The node ID in binary representation
  */
-unsigned char * getNodeIdBinaryWithLength(size_t *length) {
-       if (!nodeIdBinarySet) {
+nodeIdBinaryType * getNodeIdBinary(void) {
+       if (!nodeIdBinary.set) {
                setNodeId("", NULL, (set_plugin_parameter_addon) {.pc = NULL});
        }
 
-       if (length != NULL) {
-               *length = nodeIdBinaryLength;
-       }
-
-       return (unsigned char *)&nodeIdBinary;
+       return &nodeIdBinary;
 }
 
 /**
@@ -314,8 +300,7 @@ static bool intSetupNodeIdBinaryMAC(void) {
                return false;
        }
 
-       return setupNodeIdBinaryMAC(&nodeIdBinary, &nodeIdBinaryLength,
-                       &nodeIdBinarySet, mac);
+       return setupNodeIdBinaryMAC(&nodeIdBinary, mac);
 }
 
 /**
@@ -346,8 +331,7 @@ static bool intSetupNodeIdBinaryLongLong(unsigned long long min,
                return false;
        }
 
-       return setupNodeIdBinaryLongLong(&nodeIdBinary, &nodeIdBinaryLength,
-                               &nodeIdBinarySet, longValue, bytes);
+       return setupNodeIdBinaryLongLong(&nodeIdBinary, longValue, bytes);
 }
 
 /**
@@ -376,8 +360,7 @@ static bool intSetupNodeIdBinaryString(void) {
                return false;
        }
 
-       return setupNodeIdBinaryString(&nodeIdBinary, &nodeIdBinaryLength,
-                       &nodeIdBinarySet, nodeid, nodeidlength);
+       return setupNodeIdBinaryString(&nodeIdBinary, nodeid, nodeidlength);
 }
 
 /**
@@ -399,8 +382,7 @@ static bool intSetupNodeIdBinaryIp(void) {
                length = sizeof(struct in6_addr);
        }
 
-       return setupNodeIdBinaryIp(&nodeIdBinary, &nodeIdBinaryLength,
-                       &nodeIdBinarySet, src, length);
+       return setupNodeIdBinaryIp(&nodeIdBinary, src, length);
 }
 
 /**
index af4eea3..05b248a 100644 (file)
@@ -30,7 +30,7 @@ int setNodeIdType(const char *value, void *data,
 
 unsigned char * getNodeId(void);
 unsigned char * getNodeIdWithLength(size_t *length);
-unsigned char * getNodeIdBinaryWithLength(size_t *length);
+nodeIdBinaryType * getNodeIdBinary(void);
 int setNodeId(const char *value, void *data, set_plugin_parameter_addon addon);
 
 /*
index bed7720..f548a7b 100644 (file)
@@ -259,8 +259,7 @@ unsigned int gpsToOlsr(nmeaINFO *nmeaInfo, union olsr_message *olsrMessage,
        unsigned int aligned_size;
        unsigned int aligned_size_remainder;
        size_t nodeLength;
-       size_t nodeidbinarylength;
-       unsigned char * nodeidbinary = NULL;
+       nodeIdBinaryType * nodeIdBinary = NULL;
 
        PudOlsrPositionUpdate * olsrGpsMessage =
                        getOlsrMessagePayload(olsr_cnf->ip_version, olsrMessage);
@@ -316,9 +315,10 @@ unsigned int gpsToOlsr(nmeaINFO *nmeaInfo, union olsr_message *olsrMessage,
                setPositionUpdateHdop(olsrGpsMessage, PUD_HDOP_MAX);
        }
 
-       nodeidbinary = getNodeIdBinaryWithLength(&nodeidbinarylength);
+       nodeIdBinary = getNodeIdBinary();
        nodeLength = setPositionUpdateNodeInfo(olsr_cnf->ip_version, olsrGpsMessage,
-                       olsrMessageSize, getNodeIdTypeNumber(), nodeidbinary, nodeidbinarylength);
+                       olsrMessageSize, getNodeIdTypeNumber(),
+                       (unsigned char *) &nodeIdBinary->buffer, nodeIdBinary->length);
 
        /*
         * Messages in OLSR are 4-byte aligned: align
index 95c143d..ac39def 100644 (file)
@@ -14,20 +14,15 @@ void getNodeIdStringFromOlsr(int ipVersion, union olsr_message *olsrMessage,
                const char **nodeIdStr, char *nodeIdStrBuffer,
                unsigned int nodeIdStrBufferSize);
 
-bool setupNodeIdBinaryMAC(nodeIdBinaryType * nodeIdBinary,
-               size_t * nodeIdBinaryLength, bool * nodeIdBinarySet,
-               unsigned char * mac);
+bool setupNodeIdBinaryMAC(nodeIdBinaryType * nodeIdBinary, unsigned char * mac);
 
 bool setupNodeIdBinaryLongLong(nodeIdBinaryType * nodeIdBinary,
-               size_t * nodeIdBinaryLength, bool * nodeIdBinarySet,
                unsigned long long longValue, size_t bytes);
 
-bool setupNodeIdBinaryString(nodeIdBinaryType * nodeIdBinary,
-               size_t * nodeIdBinaryLength, bool * nodeIdBinarySet,
-               char * nodeId, size_t nodeIdLength);
+bool setupNodeIdBinaryString(nodeIdBinaryType * nodeIdBinary, char * nodeId,
+               size_t nodeIdLength);
 
-bool setupNodeIdBinaryIp(nodeIdBinaryType * nodeIdBinary,
-               size_t * nodeIdBinaryLength, bool * nodeIdBinarySet, void * ip,
+bool setupNodeIdBinaryIp(nodeIdBinaryType * nodeIdBinary, void * ip,
                size_t ipLength);
 
 #endif /* _PUD_NODEIDCONVERSION_H_ */
index f5d72ed..13ac241 100644 (file)
@@ -231,13 +231,18 @@ typedef enum _NodeIdType {
 #define PUD_TX_NODEID_BUFFERSIZE               1023
 
 /**
- The type that is used to store the nodeId as a binary representation
+ The type that is used to store the nodeId as a binary representation along
+ with its length and setup status
  */
-typedef union _nodeIdBinaryType {
-               unsigned long long longValue;
-               unsigned char stringValue[PUD_TX_NODEID_BUFFERSIZE + 1];
-               union olsr_ip_addr ip;
-               unsigned char mac[PUD_NODEIDTYPE_MAC_BYTES];
+typedef struct _nodeIdBinaryType {
+               bool set;
+               size_t length;
+               union _buffer {
+                               unsigned char mac[PUD_NODEIDTYPE_MAC_BYTES];
+                               union olsr_ip_addr ip;
+                               unsigned long long longValue;
+                               unsigned char stringValue[PUD_TX_NODEID_BUFFERSIZE + 1];
+               } buffer;
 } nodeIdBinaryType;
 
 /*
index a9027cb..6a7aa3e 100644 (file)
@@ -176,24 +176,16 @@ void getNodeIdStringFromOlsr(int ipVersion, union olsr_message *olsrMessage,
 
  @param nodeIdBinary
  a pointer to the buffer in which to store the binary/wireformat representation
- @param nodeIdBinaryLength
- a pointer to the variable in which to store the number of bytes used by the
- binary/wireformat representation
- @param nodeIdBinarySet
- a pointer to a variable in which to store the result of whether the binary/wireformat
- representation was stored
  @param mac
  a pointer to a buffer in which the MAC address is stored (in network byte-order)
  @return
  - true when ok
  - false on failure
  */
-bool setupNodeIdBinaryMAC(nodeIdBinaryType * nodeIdBinary,
-               size_t * nodeIdBinaryLength, bool * nodeIdBinarySet,
-               unsigned char * mac) {
-       memcpy(&nodeIdBinary->mac, mac, PUD_NODEIDTYPE_MAC_BYTES);
-       *nodeIdBinaryLength = PUD_NODEIDTYPE_MAC_BYTES;
-       *nodeIdBinarySet = true;
+bool setupNodeIdBinaryMAC(nodeIdBinaryType * nodeIdBinary, unsigned char * mac) {
+       memcpy(&nodeIdBinary->buffer.mac, mac, PUD_NODEIDTYPE_MAC_BYTES);
+       nodeIdBinary->length = PUD_NODEIDTYPE_MAC_BYTES;
+       nodeIdBinary->set = true;
        return true;
 }
 
@@ -202,12 +194,6 @@ bool setupNodeIdBinaryMAC(nodeIdBinaryType * nodeIdBinary,
 
  @param nodeIdBinary
  a pointer to the buffer in which to store the binary/wireformat representation
- @param nodeIdBinaryLength
- a pointer to the variable in which to store the number of bytes used by the
- binary/wireformat representation
- @param nodeIdBinarySet
- a pointer to a variable in which to store the result of whether the binary/wireformat
- representation was stored
  @param value
  the value to convert (in machine byte-order)
  @param bytes
@@ -218,21 +204,20 @@ bool setupNodeIdBinaryMAC(nodeIdBinaryType * nodeIdBinary,
  - false on failure
  */
 bool setupNodeIdBinaryLongLong(nodeIdBinaryType * nodeIdBinary,
-               size_t * nodeIdBinaryLength, bool * nodeIdBinarySet,
                unsigned long long value, size_t bytes) {
        unsigned long long longValue = value;
        int i = bytes - 1;
 
        while (i >= 0) {
-               ((unsigned char *)&nodeIdBinary->longValue)[i] = longValue & 0xff;
+               ((unsigned char *) &nodeIdBinary->buffer.longValue)[i] = longValue & 0xff;
                longValue >>= 8;
                i--;
        }
 
        assert(longValue == 0);
 
-       *nodeIdBinaryLength = bytes;
-       *nodeIdBinarySet = true;
+       nodeIdBinary->length = bytes;
+       nodeIdBinary->set = true;
        return true;
 }
 
@@ -241,12 +226,6 @@ bool setupNodeIdBinaryLongLong(nodeIdBinaryType * nodeIdBinary,
 
  @param nodeIdBinary
  a pointer to the buffer in which to store the binary/wireformat representation
- @param nodeIdBinaryLength
- a pointer to the variable in which to store the number of bytes used by the
- binary/wireformat representation
- @param nodeIdBinarySet
- a pointer to a variable in which to store the result of whether the binary/wireformat
- representation was stored
  @param nodeId
  a pointer to the nodeId string
  @param nodeIdLength
@@ -255,13 +234,12 @@ bool setupNodeIdBinaryLongLong(nodeIdBinaryType * nodeIdBinary,
  - true when ok
  - false on failure
  */
-bool setupNodeIdBinaryString(nodeIdBinaryType * nodeIdBinary,
-               size_t * nodeIdBinaryLength, bool * nodeIdBinarySet,
-               char * nodeId, size_t nodeIdLength) {
+bool setupNodeIdBinaryString(nodeIdBinaryType * nodeIdBinary, char * nodeId,
+               size_t nodeIdLength) {
        /* including trailing \0 */
-       memcpy(&nodeIdBinary->stringValue[0], &nodeId[0], nodeIdLength + 1);
-       *nodeIdBinaryLength = nodeIdLength + 1;
-       *nodeIdBinarySet = true;
+       memcpy(&nodeIdBinary->buffer.stringValue[0], &nodeId[0], nodeIdLength + 1);
+       nodeIdBinary->length = nodeIdLength + 1;
+       nodeIdBinary->set = true;
        return true;
 }
 
@@ -270,12 +248,6 @@ bool setupNodeIdBinaryString(nodeIdBinaryType * nodeIdBinary,
 
  @param nodeIdBinary
  a pointer to the buffer in which to store the binary/wireformat representation
- @param nodeIdBinaryLength
- a pointer to the variable in which to store the number of bytes used by the
- binary/wireformat representation
- @param nodeIdBinarySet
- a pointer to a variable in which to store the result of whether the binary/wireformat
- representation was stored
  @param ip
  a pointer to a buffer in which the IP address is stored (in network byte-order)
  @param ipLength
@@ -284,11 +256,10 @@ bool setupNodeIdBinaryString(nodeIdBinaryType * nodeIdBinary,
  - true when ok
  - false on failure
  */
-bool setupNodeIdBinaryIp(nodeIdBinaryType * nodeIdBinary,
-               size_t * nodeIdBinaryLength, bool * nodeIdBinarySet, void * ip,
+bool setupNodeIdBinaryIp(nodeIdBinaryType * nodeIdBinary, void * ip,
                size_t ipLength) {
-       memcpy(&nodeIdBinary->ip, ip, ipLength);
-       *nodeIdBinaryLength = ipLength;
-       *nodeIdBinarySet = true;
+       memcpy(&nodeIdBinary->buffer.ip, ip, ipLength);
+       nodeIdBinary->length = ipLength;
+       nodeIdBinary->set = true;
        return true;
 }