added support for geolocation encoded as integers; code refactoring
authorVictor Kirhenshtein <victor@netxms.org>
Thu, 13 Mar 2014 22:17:08 +0000 (00:17 +0200)
committerVictor Kirhenshtein <victor@netxms.org>
Thu, 13 Mar 2014 22:17:08 +0000 (00:17 +0200)
doc/misc/embedded_nxcp.c
include/nxcpapi.h
src/libnetxms/geolocation.cpp
src/libnetxms/message.cpp
src/libnxcl/objects.cpp
src/server/core/mobile.cpp
src/server/core/netmap.cpp
src/server/core/netobj.cpp

index 42c8cf6..fe2963d 100644 (file)
@@ -223,7 +223,7 @@ uint32_t nxcp_preprocess_message()
    ((NXCP_MESSAGE_HEADER *)nxcp_message)->code = ntohs(((NXCP_MESSAGE_HEADER *)nxcp_message)->code);
    ((NXCP_MESSAGE_HEADER *)nxcp_message)->size = ntohl(((NXCP_MESSAGE_HEADER *)nxcp_message)->size);
    ((NXCP_MESSAGE_HEADER *)nxcp_message)->numFields = ntohl(((NXCP_MESSAGE_HEADER *)nxcp_message)->numFields);
-   ((NXCP_MESSAGE_HEADER *)nxcp_message)->id = htonl(((NXCP_MESSAGE_HEADER *)nxcp_message)->id);
+   ((NXCP_MESSAGE_HEADER *)nxcp_message)->id = ntohl(((NXCP_MESSAGE_HEADER *)nxcp_message)->id);
    return ((NXCP_MESSAGE_HEADER *)nxcp_message)->id;
 }
 
@@ -232,10 +232,10 @@ uint32_t nxcp_preprocess_message()
  */
 uint32_t nxcp_get_rcc()
 {
-   CSCP_DF *field = (CSCP_DF *)(nxcp_message + 16);
+   NXCP_DF *field = (NXCP_DF *)(nxcp_message + 16);
    if ((field->type == CSCP_DT_INTEGER) && (field->id == htonl(28)))
    {
-      return ntohl(df->df_int32);
+      return ntohl(field->df_int32);
    }
    return 0;
 }
index 49a5ea2..3a4a16a 100644 (file)
@@ -80,10 +80,12 @@ public:
    UINT32 GetId() { return m_id; }
    void SetId(UINT32 id) { m_id = id; }
 
-   bool isFieldExist(UINT32 varId) { return find(varId) != NULL; }
    bool isEndOfSequence() { return (m_flags & MF_END_OF_SEQUENCE) ? true : false; }
    bool isReverseOrder() { return (m_flags & MF_REVERSE_ORDER) ? true : false; }
 
+   bool isFieldExist(UINT32 fieldId) { return find(fieldId) != NULL; }
+   int getFieldType(UINT32 fieldId);
+
    void SetVariable(UINT32 dwVarId, INT16 wValue) { set(dwVarId, CSCP_DT_INT16, &wValue); }
    void SetVariable(UINT32 dwVarId, UINT16 wValue) { set(dwVarId, CSCP_DT_INT16, &wValue); }
    void SetVariable(UINT32 dwVarId, INT32 dwValue) { set(dwVarId, CSCP_DT_INTEGER, &dwValue); }
@@ -102,11 +104,14 @@ public:
    void SetVariableToInt32Array(UINT32 dwVarId, UINT32 dwNumElements, const UINT32 *pdwData);
    BOOL SetVariableFromFile(UINT32 dwVarId, const TCHAR *pszFileName);
 
+   INT16 getFieldAsInt16(UINT32 fieldId);
+   INT32 getFieldAsInt32(UINT32 fieldId);
+   INT64 getFieldAsInt64(UINT32 fieldId);
+   double getFieldAsDouble(UINT32 fieldId);
+
    UINT32 GetVariableLong(UINT32 dwVarId);
    UINT64 GetVariableInt64(UINT32 dwVarId);
    UINT16 GetVariableShort(UINT32 dwVarId);
-   INT32 GetVariableShortAsInt32(UINT32 dwVarId);
-   double GetVariableDouble(UINT32 dwVarId);
    TCHAR *GetVariableStr(UINT32 dwVarId, TCHAR *szBuffer = NULL, UINT32 dwBufSize = 0);
        char *GetVariableStrA(UINT32 dwVarId, char *pszBuffer = NULL, UINT32 dwBufSize = 0);
        char *GetVariableStrUTF8(UINT32 dwVarId, char *pszBuffer = NULL, UINT32 dwBufSize = 0);
index 481d995..3c13f4b 100644 (file)
@@ -98,8 +98,17 @@ GeoLocation::GeoLocation(const GeoLocation &src)
 GeoLocation::GeoLocation(CSCPMessage &msg)
 {
        m_type = (int)msg.GetVariableShort(VID_GEOLOCATION_TYPE);
-       m_lat = msg.GetVariableDouble(VID_LATITUDE);
-       m_lon = msg.GetVariableDouble(VID_LONGITUDE);
+
+   if (msg.getFieldType(VID_LATITUDE) == CSCP_DT_INTEGER)
+          m_lat = (double)msg.getFieldAsInt32(VID_LATITUDE) / 1000000;
+   else
+          m_lat = msg.getFieldAsDouble(VID_LATITUDE);
+
+   if (msg.getFieldType(VID_LONGITUDE) == CSCP_DT_INTEGER)
+          m_lon = (double)msg.getFieldAsInt32(VID_LONGITUDE) / 1000000;
+   else
+       m_lon = msg.getFieldAsDouble(VID_LONGITUDE);
+
        m_accuracy = (int)msg.GetVariableShort(VID_ACCURACY);
        m_timestamp = (time_t)msg.GetVariableInt64(VID_GEOLOCATION_TIMESTAMP);
        posToString(true, m_lat);
index 00a59be..e18c308 100644 (file)
@@ -1,7 +1,7 @@
 /* 
 ** NetXMS - Network Management System
 ** NetXMS Foundation Library
-** Copyright (C) 2003-2013 Victor Kirhenshtein
+** Copyright (C) 2003-2014 Victor Kirhenshtein
 **
 ** This program is free software; you can redistribute it and/or modify
 ** it under the terms of the GNU Lesser General Public License as published
@@ -510,7 +510,27 @@ void *CSCPMessage::get(UINT32 fieldId, BYTE type)
 }
 
 /**
- * get integer field
+ * Get data type of message field.
+ *
+ * @return field type or -1 if field with given ID does not exist
+ */
+int CSCPMessage::getFieldType(UINT32 fieldId)
+{
+   CSCP_DF *field = find(fieldId);
+   return (field != NULL) ? (int)field->bType : -1;
+}
+
+/**
+ * get signed integer field
+ */
+INT32 CSCPMessage::getFieldAsInt32(UINT32 fieldId)
+{
+   char *value = (char *)get(fieldId, CSCP_DT_INTEGER);
+   return (value != NULL) ? *((INT32 *)value) : 0;
+}
+
+/**
+ * get unsigned integer field
  */
 UINT32 CSCPMessage::GetVariableLong(UINT32 dwVarId)
 {
@@ -521,7 +541,16 @@ UINT32 CSCPMessage::GetVariableLong(UINT32 dwVarId)
 }
 
 /**
- * get 16-bit integer variable
+ * get signed 16-bit integer field
+ */
+INT16 CSCPMessage::getFieldAsInt16(UINT32 fieldId)
+{
+   char *value = (char *)get(fieldId, CSCP_DT_INT16);
+   return (value != NULL) ? *((INT16 *)value) : 0;
+}
+
+/**
+ * get unsigned 16-bit integer variable
  */
 UINT16 CSCPMessage::GetVariableShort(UINT32 dwVarId)
 {
@@ -532,18 +561,16 @@ UINT16 CSCPMessage::GetVariableShort(UINT32 dwVarId)
 }
 
 /**
- * get 16-bit integer variable as signel 32-bit integer
+ * get signed 64-bit integer field
  */
-INT32 CSCPMessage::GetVariableShortAsInt32(UINT32 dwVarId)
+INT64 CSCPMessage::getFieldAsInt64(UINT32 fieldId)
 {
-   void *pValue;
-
-   pValue = get(dwVarId, CSCP_DT_INT16);
-   return pValue ? *((short *)pValue) : 0;
+   char *value = (char *)get(fieldId, CSCP_DT_INT64);
+   return (value != NULL) ? *((INT64 *)value) : 0;
 }
 
 /**
- * get 64-bit integer variable
+ * get unsigned 64-bit integer field
  */
 UINT64 CSCPMessage::GetVariableInt64(UINT32 dwVarId)
 {
@@ -556,12 +583,10 @@ UINT64 CSCPMessage::GetVariableInt64(UINT32 dwVarId)
 /**
  * get 64-bit floating point variable
  */
-double CSCPMessage::GetVariableDouble(UINT32 dwVarId)
+double CSCPMessage::getFieldAsDouble(UINT32 fieldId)
 {
-   char *pValue;
-
-   pValue = (char *)get(dwVarId, CSCP_DT_FLOAT);
-   return pValue ? *((double *)pValue) : 0;
+   char *value = (char *)get(fieldId, CSCP_DT_FLOAT);
+   return (value != NULL) ? *((double *)value) : 0;
 }
 
 /**
index bd6272f..a51f538 100644 (file)
@@ -224,8 +224,8 @@ static NXC_OBJECT *NewObjectFromMsg(CSCPMessage *pMsg)
    pObject->pszComments = pMsg->GetVariableStr(VID_COMMENTS);
 
        pObject->geolocation.type = (int)pMsg->GetVariableShort(VID_GEOLOCATION_TYPE);
-       pObject->geolocation.latitude = pMsg->GetVariableDouble(VID_LATITUDE);
-       pObject->geolocation.longitude = pMsg->GetVariableDouble(VID_LONGITUDE);
+       pObject->geolocation.latitude = pMsg->getFieldAsDouble(VID_LATITUDE);
+       pObject->geolocation.longitude = pMsg->getFieldAsDouble(VID_LONGITUDE);
 
        pObject->dwNumTrustedNodes = pMsg->GetVariableLong(VID_NUM_TRUSTED_NODES);
        if (pObject->dwNumTrustedNodes > 0)
index 7a882f3..060f193 100644 (file)
@@ -242,8 +242,11 @@ void MobileDevice::updateStatus(CSCPMessage *msg)
 
        m_lastReportTime = time(NULL);
 
-       if (msg->isFieldExist(VID_BATTERY_LEVEL))
+   int type = msg->getFieldType(VID_BATTERY_LEVEL);
+   if (type == CSCP_DT_INTEGER)
                m_batteryLevel = (int)msg->GetVariableLong(VID_BATTERY_LEVEL);
+   else if (type == CSCP_DT_INT16)
+               m_batteryLevel = (int)msg->GetVariableShort(VID_BATTERY_LEVEL);
        else
                m_batteryLevel = -1;
 
index c5083c3..dc2409b 100644 (file)
@@ -539,8 +539,8 @@ UINT32 NetworkMap::ModifyFromMessage(CSCPMessage *request, BOOL bAlreadyLocked)
        if (request->isFieldExist(VID_BACKGROUND))
        {
                request->GetVariableBinary(VID_BACKGROUND, m_background, UUID_LENGTH);
-               m_backgroundLatitude = request->GetVariableDouble(VID_BACKGROUND_LATITUDE);
-               m_backgroundLongitude = request->GetVariableDouble(VID_BACKGROUND_LONGITUDE);
+               m_backgroundLatitude = request->getFieldAsDouble(VID_BACKGROUND_LATITUDE);
+               m_backgroundLongitude = request->getFieldAsDouble(VID_BACKGROUND_LONGITUDE);
                m_backgroundZoom = (int)request->GetVariableShort(VID_BACKGROUND_ZOOM);
        }
 
index 8ff2c7f..341fef2 100644 (file)
@@ -930,19 +930,19 @@ UINT32 NetObj::ModifyFromMessage(CSCPMessage *pRequest, BOOL bAlreadyLocked)
    // Change object's status calculation/propagation algorithms
    if (pRequest->isFieldExist(VID_STATUS_CALCULATION_ALG))
    {
-      m_iStatusCalcAlg = (int)pRequest->GetVariableShort(VID_STATUS_CALCULATION_ALG);
-      m_iStatusPropAlg = (int)pRequest->GetVariableShort(VID_STATUS_PROPAGATION_ALG);
-      m_iFixedStatus = (int)pRequest->GetVariableShort(VID_FIXED_STATUS);
-      m_iStatusShift = pRequest->GetVariableShortAsInt32(VID_STATUS_SHIFT);
-      m_iStatusTranslation[0] = (int)pRequest->GetVariableShort(VID_STATUS_TRANSLATION_1);
-      m_iStatusTranslation[1] = (int)pRequest->GetVariableShort(VID_STATUS_TRANSLATION_2);
-      m_iStatusTranslation[2] = (int)pRequest->GetVariableShort(VID_STATUS_TRANSLATION_3);
-      m_iStatusTranslation[3] = (int)pRequest->GetVariableShort(VID_STATUS_TRANSLATION_4);
-      m_iStatusSingleThreshold = (int)pRequest->GetVariableShort(VID_STATUS_SINGLE_THRESHOLD);
-      m_iStatusThresholds[0] = (int)pRequest->GetVariableShort(VID_STATUS_THRESHOLD_1);
-      m_iStatusThresholds[1] = (int)pRequest->GetVariableShort(VID_STATUS_THRESHOLD_2);
-      m_iStatusThresholds[2] = (int)pRequest->GetVariableShort(VID_STATUS_THRESHOLD_3);
-      m_iStatusThresholds[3] = (int)pRequest->GetVariableShort(VID_STATUS_THRESHOLD_4);
+      m_iStatusCalcAlg = pRequest->getFieldAsInt16(VID_STATUS_CALCULATION_ALG);
+      m_iStatusPropAlg = pRequest->getFieldAsInt16(VID_STATUS_PROPAGATION_ALG);
+      m_iFixedStatus = pRequest->getFieldAsInt16(VID_FIXED_STATUS);
+      m_iStatusShift = pRequest->getFieldAsInt16(VID_STATUS_SHIFT);
+      m_iStatusTranslation[0] = pRequest->getFieldAsInt16(VID_STATUS_TRANSLATION_1);
+      m_iStatusTranslation[1] = pRequest->getFieldAsInt16(VID_STATUS_TRANSLATION_2);
+      m_iStatusTranslation[2] = pRequest->getFieldAsInt16(VID_STATUS_TRANSLATION_3);
+      m_iStatusTranslation[3] = pRequest->getFieldAsInt16(VID_STATUS_TRANSLATION_4);
+      m_iStatusSingleThreshold = pRequest->getFieldAsInt16(VID_STATUS_SINGLE_THRESHOLD);
+      m_iStatusThresholds[0] = pRequest->getFieldAsInt16(VID_STATUS_THRESHOLD_1);
+      m_iStatusThresholds[1] = pRequest->getFieldAsInt16(VID_STATUS_THRESHOLD_2);
+      m_iStatusThresholds[2] = pRequest->getFieldAsInt16(VID_STATUS_THRESHOLD_3);
+      m_iStatusThresholds[3] = pRequest->getFieldAsInt16(VID_STATUS_THRESHOLD_4);
       bRecalcStatus = TRUE;
    }