Skip to content

Commit a20aa53

Browse files
authored
Merge pull request #37 from REVrobotics/add-getTimestampsForAllReceivedMessages-function
2 parents a6feef6 + 11ed247 commit a20aa53

File tree

5 files changed

+63
-23
lines changed

5 files changed

+63
-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 last-received message with that ID
79+
*/
80+
getLatestMessageOfEveryReceivedArbId: (descriptor: string, maxAgeMs: number) => Record<number, CanMessage>;
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.getLatestMessageOfEveryReceivedArbId = addon.getLatestMessageOfEveryReceivedArbId;
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, "getLatestMessageOfEveryReceivedArbId"),
54+
Napi::Function::New(env, getLatestMessageOfEveryReceivedArbId));
5355
return exports;
5456
}
5557

src/canWrapper.cc

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

727+
Napi::Object getLatestMessageOfEveryReceivedArbId(const Napi::CallbackInfo& info) {
728+
Napi::Env env = info.Env();
729+
std::string descriptor = info[0].As<Napi::String>().Utf8Value();
730+
uint32_t maxAgeMs = info[1].As<Napi::Number>().Uint32Value();
731+
732+
std::shared_ptr<rev::usb::CANDevice> device;
733+
734+
{ // This block exists to define how long we hold canDevicesMtx
735+
std::scoped_lock lock{canDevicesMtx};
736+
auto deviceIterator = canDeviceMap.find(descriptor);
737+
if (deviceIterator == canDeviceMap.end()) {
738+
if (devicesRegisteredToHal.find(descriptor) != devicesRegisteredToHal.end()) return receiveHalMessage(info);
739+
Napi::Error::New(env, DEVICE_NOT_FOUND_ERROR).ThrowAsJavaScriptException();
740+
return Napi::Object::New(env);
741+
}
742+
device = deviceIterator->second;
743+
}
744+
745+
std::map<uint32_t, std::shared_ptr<rev::usb::CANMessage>> messages;
746+
bool success = device->CopyReceivedMessagesMap(messages);
747+
if (!success) {
748+
Napi::Error::New(env, "Failed to copy the map of received messages").ThrowAsJavaScriptException();
749+
return Napi::Object::New(env);
750+
}
751+
752+
// TODO(Harper): Use HAL clock
753+
const auto nowMs = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now()).time_since_epoch().count();
754+
755+
Napi::Object result = Napi::Object::New(env);
756+
for (auto& m: messages) {
757+
uint32_t arbId = m.first;
758+
auto message = m.second;
759+
uint32_t timestampMs = message->GetTimestampUs();
760+
761+
if (nowMs - timestampMs > maxAgeMs) {
762+
continue;
763+
}
764+
765+
size_t messageSize = message->GetSize();
766+
const uint8_t* messageData = message->GetData();
767+
Napi::Array napiMessage = Napi::Array::New(env, messageSize);
768+
for (int i = 0; i < messageSize; i++) {
769+
napiMessage[i] = messageData[i];
770+
}
771+
Napi::Object messageInfo = Napi::Object::New(env);
772+
messageInfo.Set("messageID", message->GetMessageId());
773+
messageInfo.Set("timeStamp", timestampMs);
774+
messageInfo.Set("data", napiMessage);
775+
result.Set(arbId, messageInfo);
776+
}
777+
778+
return result;
779+
}
780+
727781
void cleanupHeartbeatsRunning() {
728782
// Erase removed CAN buses from heartbeatsRunning
729783
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 getLatestMessageOfEveryReceivedArbId(const Napi::CallbackInfo& info);
3132
#endif

0 commit comments

Comments
 (0)