PUD: include compiler.h where needed
[olsrd.git] / lib / pud / src / gpsConversion.c
index ca11ca0..1404066 100644 (file)
@@ -4,18 +4,14 @@
 #include "pud.h"
 #include "configuration.h"
 #include "compiler.h"
-#include "networkInterfaces.h"
 
 /* OLSR includes */
 #include "olsr.h"
 
 /* System includes */
-#include <nmea/gmath.h>
 #include <nmea/tok.h>
-#include <nmea/info.h>
-#include <netinet/in.h>
-#include <stdio.h>
-#include <OlsrdPudWireFormat/wireFormat.h>
+#include <nmea/gmath.h>
+#include <arpa/inet.h>
 #include <OlsrdPudWireFormat/nodeIdConversion.h>
 
 /* ************************************************************************
@@ -50,10 +46,15 @@ unsigned int gpsFromOlsr(union olsr_message *olsrMessage,
        char trackString[PUD_TX_TRACK_DIGITS + 1];
        char hdopString[PUD_TX_HDOP_DIGITS + 1];
        uint8_t smask;
+       uint8_t flags;
+       char gateway[2] = { '0', '\0' };
 
        char nodeIdTypeString[PUD_TX_NODEIDTYPE_DIGITS + 1];
        char nodeIdString[PUD_TX_NODEID_BUFFERSIZE + 1];
        const char * nodeId;
+       const void * ipAddr;
+       char originatorBuffer[64];
+       const char * originator;
 
        unsigned int transmitStringLength;
 
@@ -68,10 +69,22 @@ unsigned int gpsFromOlsr(union olsr_message *olsrMessage,
                return 0;
        }
 
+       ipAddr = (olsr_cnf->ip_version == AF_INET) ?
+                               (void *) &olsrMessage->v4.originator :
+                               (void *) &olsrMessage->v6.originator;
+       originator = inet_ntop(olsr_cnf->ip_version, ipAddr, &originatorBuffer[0],
+                       sizeof(originatorBuffer));
+
        validityTime = getValidityTime(&olsrGpsMessage->validityTime);
 
        smask = getPositionUpdateSmask(olsrGpsMessage);
 
+       flags = getPositionUpdateSmask(olsrGpsMessage);
+
+       if (flags & PUD_FLAGS_GATEWAY) {
+               gateway[0] = '1';
+       }
+
        /* time is ALWAYS present so we can just use it */
        getPositionUpdateTime(olsrGpsMessage, time(NULL), &timeStruct);
 
@@ -180,6 +193,8 @@ unsigned int gpsFromOlsr(union olsr_message *olsrMessage,
        transmitStringLength = nmea_printf((char *) txGpsBuffer, txGpsBufferSize
                        - 1, "$P%s," /* prefix (always) */
                "%u," /* sentence version (always) */
+               "%s," /* gateway flag (always) */
+               "%s," /* OLSR originator (always) */
                "%s,%s," /* nodeIdType/nodeId (always) */
                "%02u%02u%02u," /* date (always) */
                "%02u%02u%02u," /* time (always) */
@@ -190,7 +205,8 @@ unsigned int gpsFromOlsr(union olsr_message *olsrMessage,
                "%s," /* speed (optional) */
                "%s," /* track (optional) */
                "%s" /* hdop (optional) */
-       , getTxNmeaMessagePrefix(), PUD_TX_SENTENCE_VERSION, &nodeIdTypeString[0],
+       , getTxNmeaMessagePrefix(), PUD_TX_SENTENCE_VERSION, &gateway[0],
+                       originator , &nodeIdTypeString[0],
                        nodeId, timeStruct.tm_mday, timeStruct.tm_mon + 1, (timeStruct.tm_year
                                        % 100), timeStruct.tm_hour, timeStruct.tm_min,
                        timeStruct.tm_sec, validityTime, &latitudeString[0],
@@ -237,10 +253,8 @@ unsigned int gpsToOlsr(nmeaINFO *nmeaInfo, union olsr_message *olsrMessage,
                unsigned int olsrMessageSize, unsigned long long validityTime) {
        unsigned int aligned_size;
        unsigned int aligned_size_remainder;
-       NodeIdType nodeIdType;
-       unsigned char * nodeId;
-       size_t nodeIdLength;
        size_t nodeLength;
+       nodeIdBinaryType * nodeIdBinary = NULL;
 
        PudOlsrPositionUpdate * olsrGpsMessage =
                        getOlsrMessagePayload(olsr_cnf->ip_version, olsrMessage);
@@ -253,6 +267,8 @@ unsigned int gpsToOlsr(nmeaINFO *nmeaInfo, union olsr_message *olsrMessage,
        setPositionUpdateVersion(olsrGpsMessage, PUD_WIRE_FORMAT_VERSION);
        setValidityTime(&olsrGpsMessage->validityTime, validityTime);
        setPositionUpdateSmask(olsrGpsMessage, nmeaInfo->smask);
+       setPositionUpdateFlags(olsrGpsMessage,
+                       getPositionUpdateFlags(olsrGpsMessage) & ~PUD_FLAGS_GATEWAY);
 
        /* utc is always present, we make sure of that elsewhere, so just use it */
        setPositionUpdateTime(olsrGpsMessage, nmeaInfo->utc.hour, nmeaInfo->utc.min,
@@ -294,17 +310,10 @@ unsigned int gpsToOlsr(nmeaINFO *nmeaInfo, union olsr_message *olsrMessage,
                setPositionUpdateHdop(olsrGpsMessage, PUD_HDOP_MAX);
        }
 
-       nodeIdType = getNodeIdTypeNumber();
-       if (unlikely(nodeIdType == PUD_NODEIDTYPE_MAC)) {
-               nodeLength = setPositionUpdateNodeInfo(olsr_cnf->ip_version,
-                               olsrGpsMessage, olsrMessageSize, nodeIdType,
-                               getMainIpMacAddress(), PUD_NODEIDTYPE_MAC_BYTES);
-       } else {
-               nodeId = getNodeIdWithLength(&nodeIdLength);
-               nodeLength = setPositionUpdateNodeInfo(olsr_cnf->ip_version,
-                               olsrGpsMessage, olsrMessageSize, nodeIdType, nodeId,
-                               nodeIdLength);
-       }
+       nodeIdBinary = getNodeIdBinary();
+       nodeLength = setPositionUpdateNodeInfo(olsr_cnf->ip_version, olsrGpsMessage,
+                       olsrMessageSize, getNodeIdTypeNumber(),
+                       (unsigned char *) &nodeIdBinary->buffer, nodeIdBinary->length);
 
        /*
         * Messages in OLSR are 4-byte aligned: align
@@ -328,8 +337,7 @@ unsigned int gpsToOlsr(nmeaINFO *nmeaInfo, union olsr_message *olsrMessage,
                olsrMessage->v4.olsr_msgtype = PUD_OLSR_MSG_TYPE;
                olsrMessage->v4.olsr_vtime = reltime_to_me(validityTime * 1000);
                /* message->v4.olsr_msgsize at the end */
-               memcpy(&olsrMessage->v4.originator, &olsr_cnf->main_addr,
-                               olsr_cnf->ipsize);
+               olsrMessage->v4.originator = olsr_cnf->main_addr.v4.s_addr;
                olsrMessage->v4.ttl = getOlsrTtl();
                olsrMessage->v4.hopcnt = 0;
                olsrMessage->v4.seqno = htons(get_msg_seqno());
@@ -344,8 +352,7 @@ unsigned int gpsToOlsr(nmeaINFO *nmeaInfo, union olsr_message *olsrMessage,
                olsrMessage->v6.olsr_msgtype = PUD_OLSR_MSG_TYPE;
                olsrMessage->v6.olsr_vtime = reltime_to_me(validityTime * 1000);
                /* message->v6.olsr_msgsize at the end */
-               memcpy(&olsrMessage->v6.originator, &olsr_cnf->main_addr,
-                               olsr_cnf->ipsize);
+               olsrMessage->v6.originator = olsr_cnf->main_addr.v6;
                olsrMessage->v6.ttl = getOlsrTtl();
                olsrMessage->v6.hopcnt = 0;
                olsrMessage->v6.seqno = htons(get_msg_seqno());