From 1f79afe22d0803d6b18145e2fe7f4443a0259368 Mon Sep 17 00:00:00 2001 From: NoahAndrews <10224994+NoahAndrews@users.noreply.github.com.> Date: Wed, 6 Nov 2024 19:23:47 -0600 Subject: [PATCH] Add getTimestampsForAllReceivedMessages() function --- lib/binding.ts | 27 +++++--------------------- scripts/download-CanBridge.mjs | 2 +- src/addon.cc | 2 ++ src/canWrapper.cc | 35 ++++++++++++++++++++++++++++++++++ src/canWrapper.h | 1 + 5 files changed, 44 insertions(+), 23 deletions(-) diff --git a/lib/binding.ts b/lib/binding.ts index 56de42c..dd7e912 100644 --- a/lib/binding.ts +++ b/lib/binding.ts @@ -74,6 +74,10 @@ export class CanBridge { startRevCommonHeartbeat: (descriptor: string) => void; stopHeartbeats: (descriptor: string, sendDisabledHeartbeatsFirst: boolean) => void; ackHeartbeats: () => void; + /** + * @return Object that maps arbitration IDs to the timestamp a message with that ID was last received at + */ + getTimestampsForAllReceivedMessages: (descriptor: string) => Record; constructor() { try { @@ -103,30 +107,9 @@ export class CanBridge { this.startRevCommonHeartbeat = addon.startRevCommonHeartbeat; this.ackHeartbeats = addon.ackHeartbeats; this.stopHeartbeats = addon.stopHeartbeats; + this.getTimestampsForAllReceivedMessages = addon.getTimestampsForAllReceivedMessages; } catch (e: any) { throw new CanBridgeInitializationError(e); } } } - - - - - - - - - - - - - - - - - - - - - - diff --git a/scripts/download-CanBridge.mjs b/scripts/download-CanBridge.mjs index c26898a..7530a91 100644 --- a/scripts/download-CanBridge.mjs +++ b/scripts/download-CanBridge.mjs @@ -3,7 +3,7 @@ import * as path from "path"; import axios from 'axios'; import AdmZip from 'adm-zip'; -const canBridgeTag = "v2.5.1"; +const canBridgeTag = "v2.6.0"; const canBridgeReleaseAssetUrlPrefix = `https://github.com/REVrobotics/CANBridge/releases/download/${canBridgeTag}`; const externalCompileTimeDepsPath = 'externalCompileTimeDeps'; diff --git a/src/addon.cc b/src/addon.cc index 10bc0bf..6ecc3e0 100644 --- a/src/addon.cc +++ b/src/addon.cc @@ -50,6 +50,8 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) { Napi::Function::New(env, stopHeartbeats)); exports.Set(Napi::String::New(env, "ackHeartbeats"), Napi::Function::New(env, ackHeartbeats)); + exports.Set(Napi::String::New(env, "getTimestampsForAllReceivedMessages"), + Napi::Function::New(env, getTimestampsForAllReceivedMessages)); return exports; } diff --git a/src/canWrapper.cc b/src/canWrapper.cc index c8fb1ae..6c75d50 100644 --- a/src/canWrapper.cc +++ b/src/canWrapper.cc @@ -724,6 +724,41 @@ Napi::Array getImageElements(const Napi::CallbackInfo& info) { return elements; } +Napi::Object getTimestampsForAllReceivedMessages(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + std::string descriptor = info[0].As().Utf8Value(); + + std::shared_ptr device; + + { // This block exists to define how long we hold canDevicesMtx + std::scoped_lock lock{canDevicesMtx}; + auto deviceIterator = canDeviceMap.find(descriptor); + if (deviceIterator == canDeviceMap.end()) { + if (devicesRegisteredToHal.find(descriptor) != devicesRegisteredToHal.end()) return receiveHalMessage(info); + Napi::Error::New(env, DEVICE_NOT_FOUND_ERROR).ThrowAsJavaScriptException(); + return Napi::Object::New(env); + } + device = deviceIterator->second; + } + + std::map> messages; + bool success = device->CopyReceivedMessagesMap(messages); + if (!success) { + Napi::Error::New(env, "Failed to copy the map of received messages").ThrowAsJavaScriptException(); + return Napi::Object::New(env); + } + + Napi::Object result = Napi::Object::New(env); + for (auto& m: messages) { + uint32_t arbId = m.first; + // GetTimestampUs() actually returns timestamps in milliseconds + uint32_t timestampMs = m.second->GetTimestampUs(); + result.Set(arbId, timestampMs); + } + + return result; +} + void cleanupHeartbeatsRunning() { // Erase removed CAN buses from heartbeatsRunning std::scoped_lock lock{watchdogMtx, canDevicesMtx}; diff --git a/src/canWrapper.h b/src/canWrapper.h index 6629980..6de1b79 100644 --- a/src/canWrapper.h +++ b/src/canWrapper.h @@ -28,4 +28,5 @@ void setSparkMaxHeartbeatData(const Napi::CallbackInfo& info); void startRevCommonHeartbeat(const Napi::CallbackInfo& info); void stopHeartbeats(const Napi::CallbackInfo& info); void ackHeartbeats(const Napi::CallbackInfo& info); +Napi::Object getTimestampsForAllReceivedMessages(const Napi::CallbackInfo& info); #endif