Skip to content

Commit 1f79afe

Browse files
committed
Add getTimestampsForAllReceivedMessages() function
1 parent a6feef6 commit 1f79afe

File tree

5 files changed

+44
-23
lines changed

5 files changed

+44
-23
lines changed

lib/binding.ts

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ export class CanBridge {
7474
startRevCommonHeartbeat: (descriptor: string) => void;
7575
stopHeartbeats: (descriptor: string, sendDisabledHeartbeatsFirst: boolean) => void;
7676
ackHeartbeats: () => void;
77+
/**
78+
* @return Object that maps arbitration IDs to the timestamp a message with that ID was last received at
79+
*/
80+
getTimestampsForAllReceivedMessages: (descriptor: string) => Record<number, number>;
7781

7882
constructor() {
7983
try {
@@ -103,30 +107,9 @@ export class CanBridge {
103107
this.startRevCommonHeartbeat = addon.startRevCommonHeartbeat;
104108
this.ackHeartbeats = addon.ackHeartbeats;
105109
this.stopHeartbeats = addon.stopHeartbeats;
110+
this.getTimestampsForAllReceivedMessages = addon.getTimestampsForAllReceivedMessages;
106111
} catch (e: any) {
107112
throw new CanBridgeInitializationError(e);
108113
}
109114
}
110115
}
111-
112-
113-
114-
115-
116-
117-
118-
119-
120-
121-
122-
123-
124-
125-
126-
127-
128-
129-
130-
131-
132-

scripts/download-CanBridge.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as path from "path";
33
import axios from 'axios';
44
import AdmZip from 'adm-zip';
55

6-
const canBridgeTag = "v2.5.1";
6+
const canBridgeTag = "v2.6.0";
77
const canBridgeReleaseAssetUrlPrefix = `https://github.com/REVrobotics/CANBridge/releases/download/${canBridgeTag}`;
88

99
const externalCompileTimeDepsPath = 'externalCompileTimeDeps';

src/addon.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) {
5050
Napi::Function::New(env, stopHeartbeats));
5151
exports.Set(Napi::String::New(env, "ackHeartbeats"),
5252
Napi::Function::New(env, ackHeartbeats));
53+
exports.Set(Napi::String::New(env, "getTimestampsForAllReceivedMessages"),
54+
Napi::Function::New(env, getTimestampsForAllReceivedMessages));
5355
return exports;
5456
}
5557

src/canWrapper.cc

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,41 @@ Napi::Array getImageElements(const Napi::CallbackInfo& info) {
724724
return elements;
725725
}
726726

727+
Napi::Object getTimestampsForAllReceivedMessages(const Napi::CallbackInfo& info) {
728+
Napi::Env env = info.Env();
729+
std::string descriptor = info[0].As<Napi::String>().Utf8Value();
730+
731+
std::shared_ptr<rev::usb::CANDevice> device;
732+
733+
{ // This block exists to define how long we hold canDevicesMtx
734+
std::scoped_lock lock{canDevicesMtx};
735+
auto deviceIterator = canDeviceMap.find(descriptor);
736+
if (deviceIterator == canDeviceMap.end()) {
737+
if (devicesRegisteredToHal.find(descriptor) != devicesRegisteredToHal.end()) return receiveHalMessage(info);
738+
Napi::Error::New(env, DEVICE_NOT_FOUND_ERROR).ThrowAsJavaScriptException();
739+
return Napi::Object::New(env);
740+
}
741+
device = deviceIterator->second;
742+
}
743+
744+
std::map<uint32_t, std::shared_ptr<rev::usb::CANMessage>> messages;
745+
bool success = device->CopyReceivedMessagesMap(messages);
746+
if (!success) {
747+
Napi::Error::New(env, "Failed to copy the map of received messages").ThrowAsJavaScriptException();
748+
return Napi::Object::New(env);
749+
}
750+
751+
Napi::Object result = Napi::Object::New(env);
752+
for (auto& m: messages) {
753+
uint32_t arbId = m.first;
754+
// GetTimestampUs() actually returns timestamps in milliseconds
755+
uint32_t timestampMs = m.second->GetTimestampUs();
756+
result.Set(arbId, timestampMs);
757+
}
758+
759+
return result;
760+
}
761+
727762
void cleanupHeartbeatsRunning() {
728763
// Erase removed CAN buses from heartbeatsRunning
729764
std::scoped_lock lock{watchdogMtx, canDevicesMtx};

src/canWrapper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ void setSparkMaxHeartbeatData(const Napi::CallbackInfo& info);
2828
void startRevCommonHeartbeat(const Napi::CallbackInfo& info);
2929
void stopHeartbeats(const Napi::CallbackInfo& info);
3030
void ackHeartbeats(const Napi::CallbackInfo& info);
31+
Napi::Object getTimestampsForAllReceivedMessages(const Napi::CallbackInfo& info);
3132
#endif

0 commit comments

Comments
 (0)