From 23c3cc376324a9782cde9454ba8f53ba973fba98 Mon Sep 17 00:00:00 2001 From: David Rebbe Date: Fri, 5 Apr 2019 19:04:49 -0400 Subject: [PATCH] Implemented legacy icsneoGetTimeStampForMsg() function call. Signed-off-by: David Rebbe --- api/icsneoc/icsneoc.cpp | 13 ++++++++- api/icsneolegacy/icsneolegacy.cpp | 28 +++++++++++++++++-- communication/decoder.cpp | 2 ++ communication/message/neomessage.cpp | 1 + device/device.cpp | 4 +++ .../icsneo/communication/message/message.h | 1 + .../icsneo/communication/message/neomessage.h | 3 +- include/icsneo/device/device.h | 1 + include/icsneo/icsneoc.h | 12 ++++++++ 9 files changed, 61 insertions(+), 4 deletions(-) diff --git a/api/icsneoc/icsneoc.cpp b/api/icsneoc/icsneoc.cpp index 93ac522..f57bd78 100644 --- a/api/icsneoc/icsneoc.cpp +++ b/api/icsneoc/icsneoc.cpp @@ -597,4 +597,15 @@ bool icsneo_getSupportedDevices(devicetype_t* devices, size_t* count) { *count = len; return true; -} \ No newline at end of file +} + +extern bool DLLExport icsneo_getTimestampMultiplier(const neodevice_t* device, int* multiplier) +{ + if (!icsneo_isValidNeoDevice(device)) { + ErrorManager::GetInstance().add(APIError::InvalidNeoDevice); + return false; + } + + *multiplier = device->device->getTimestampMultiplier(); + return true; +} diff --git a/api/icsneolegacy/icsneolegacy.cpp b/api/icsneolegacy/icsneolegacy.cpp index fbcf679..25a17d6 100644 --- a/api/icsneolegacy/icsneolegacy.cpp +++ b/api/icsneolegacy/icsneolegacy.cpp @@ -60,6 +60,12 @@ static void NeoMessageToSpyMessage(const neomessage_t& newmsg, icsSpyMessage& ol oldmsg.Protocol = SPY_PROTOCOL_ETHERNET; break; } + // Timestamp - epoch = 1/1/2007 - 25ns per tick most of the time + uint64_t t = newmsg.timestamp; + if (newmsg.timestampMultiplier) + t /= newmsg.timestampMultiplier; + oldmsg.TimeHardware2 = (unsigned long)(t >> 32); + oldmsg.TimeHardware = (unsigned long)(t & 0xFFFFFFFF); } //Basic Functions @@ -241,8 +247,26 @@ int icsneoEnableNetworkRXQueue(void* hObject, int iEnable) { } int icsneoGetTimeStampForMsg(void* hObject, icsSpyMessage* pMsg, double* pTimeStamp) { - // TODO Implement - return false; + if(!icsneoValidateHObject(hObject)) + return false; + neodevice_t* device = (neodevice_t*)hObject; + + int multiplier = 0; + if (!icsneo_getTimestampMultiplier(device, &multiplier)) + return false; + + // Convert back to ticks + uint64_t ticks = pMsg->TimeHardware2; + ticks <<= 32; + ticks += pMsg->TimeHardware; + + // convert to ns + ticks *= multiplier; + + // icsneoGetTimeStampForMsg() expects pTimeStamp to be in seconds + *pTimeStamp = ticks / (double)1000000000; + + return true; } void icsneoGetISO15765Status(void* hObject, int lNetwork, int lClearTxStatus, int lClearRxStatus, int*lTxStatus, int*lRxStatus) { diff --git a/communication/decoder.cpp b/communication/decoder.cpp index 863d2fa..15c0062 100644 --- a/communication/decoder.cpp +++ b/communication/decoder.cpp @@ -28,6 +28,7 @@ bool Decoder::decode(std::shared_ptr& result, const std::shared_ptrtimestamp *= timestampMultiplier; + result->timestampMultiplier = timestampMultiplier; result->network = packet->network; return true; case Network::Type::CAN: @@ -43,6 +44,7 @@ bool Decoder::decode(std::shared_ptr& result, const std::shared_ptrtimestamp *= timestampMultiplier; + result->timestampMultiplier = timestampMultiplier; result->network = packet->network; return true; } diff --git a/communication/message/neomessage.cpp b/communication/message/neomessage.cpp index 1774997..d189983 100644 --- a/communication/message/neomessage.cpp +++ b/communication/message/neomessage.cpp @@ -14,6 +14,7 @@ neomessage_t icsneo::CreateNeoMessage(const std::shared_ptr message) { neomsg.length = message->data.size(); neomsg.data = message->data.data(); neomsg.timestamp = message->timestamp; + neomsg.timestampMultiplier = message->timestampMultiplier; neomsg.status.globalError = message->error; neomsg.status.transmitMessage = message->transmitted; diff --git a/device/device.cpp b/device/device.cpp index 04ec6e8..34cb678 100644 --- a/device/device.cpp +++ b/device/device.cpp @@ -67,6 +67,10 @@ bool Device::SerialStringIsNumeric(const std::string& serial) { return isdigit(serial[0]) && isdigit(serial[1]); } +int Device::getTimestampMultiplier() const { + return com->decoder->timestampMultiplier; +} + std::string Device::describe() const { std::stringstream ss; ss << getType() << ' ' << getSerial(); diff --git a/include/icsneo/communication/message/message.h b/include/icsneo/communication/message/message.h index 3ed805c..6aa2522 100644 --- a/include/icsneo/communication/message/message.h +++ b/include/icsneo/communication/message/message.h @@ -12,6 +12,7 @@ public: Network network; std::vector data; uint64_t timestamp = 0; + int timestampMultiplier = 0; uint16_t description = 0; bool transmitted = false; bool error = false; diff --git a/include/icsneo/communication/message/neomessage.h b/include/icsneo/communication/message/neomessage.h index 28892c1..e093a76 100644 --- a/include/icsneo/communication/message/neomessage.h +++ b/include/icsneo/communication/message/neomessage.h @@ -108,7 +108,8 @@ typedef struct { uint8_t header[4]; uint16_t netid; uint8_t type; - uint8_t reserved[17]; + uint8_t reserved[15]; + uint16_t timestampMultiplier; } neomessage_t; // 72 bytes total // Any time you add another neomessage_*_t type, make sure to add it to the static_asserts below! diff --git a/include/icsneo/device/device.h b/include/icsneo/device/device.h index 5a1a252..8910f06 100644 --- a/include/icsneo/device/device.h +++ b/include/icsneo/device/device.h @@ -29,6 +29,7 @@ public: static uint32_t SerialStringToNum(const std::string& serial); static bool SerialStringIsNumeric(const std::string& serial); + int getTimestampMultiplier() const; DeviceType getType() const { return DeviceType(data.type); } uint16_t getProductId() const { return productId; } std::string getSerial() const { return data.serial; } diff --git a/include/icsneo/icsneoc.h b/include/icsneo/icsneoc.h index 3a5e7f2..b7380a1 100644 --- a/include/icsneo/icsneoc.h +++ b/include/icsneo/icsneoc.h @@ -626,6 +626,18 @@ extern size_t DLLExport icsneo_getErrorLimit(void); */ extern bool DLLExport icsneo_getSupportedDevices(devicetype_t* devices, size_t* count); +/** + * \brief Get the devices timestamp multiplier + * \param[out] device A pointer to a buffer of devicetype_t structures which will be written to. + * \param[inout] multiplier A pointer to an int, for legacy tick rate to ns + + * \returns True unless device is invalid + */ +extern bool DLLExport icsneo_getTimestampMultiplier(const neodevice_t* device, int* multiplier); + + + + #ifdef __cplusplus } // extern "C" #endif