21
21
#include " canWrapper.h"
22
22
#include " DfuSeFile.h"
23
23
24
- #define DEVICE_NOT_FOUND_ERROR " Device not found. Make sure to run getDevices()"
25
-
26
24
#define REV_COMMON_HEARTBEAT_ID 0x00502C0
27
25
#define SPARK_HEARTBEAT_ID 0x2052C80
28
26
#define HEARTBEAT_PERIOD_MS 20
@@ -50,6 +48,12 @@ std::map<std::string, std::array<uint8_t, REV_COMMON_HEARTBEAT_LENGTH>> revCommo
50
48
std::map<std::string, std::array<uint8_t , SPARK_HEARTBEAT_LENGTH>> sparkHeartbeatMap;
51
49
auto latestHeartbeatAck = std::chrono::time_point<std::chrono::steady_clock>();
52
50
51
+ void throwDeviceNotFoundError (Napi::Env env) {
52
+ Napi::Error error = Napi::Error::New (env, " CAN bridge device not found. Make sure to run getDevices()" );
53
+ error.Set (" canBridgeDeviceNotFound" , Napi::Boolean::New (env, true ));
54
+ error.ThrowAsJavaScriptException ();
55
+ }
56
+
53
57
// Only call when holding canDevicesMtx
54
58
void removeExtraDevicesFromDeviceMap (std::vector<std::string> descriptors) {
55
59
for (auto itr = canDeviceMap.begin (); itr != canDeviceMap.end (); ++itr) {
@@ -226,7 +230,7 @@ Napi::Object receiveMessage(const Napi::CallbackInfo& info) {
226
230
auto deviceIterator = canDeviceMap.find (descriptor);
227
231
if (deviceIterator == canDeviceMap.end ()) {
228
232
if (devicesRegisteredToHal.find (descriptor) != devicesRegisteredToHal.end ()) return receiveHalMessage (info);
229
- Napi::Error::New (env, DEVICE_NOT_FOUND_ERROR). ThrowAsJavaScriptException ( );
233
+ throwDeviceNotFoundError (env);
230
234
return Napi::Object::New (env);
231
235
}
232
236
@@ -325,7 +329,7 @@ Napi::Number openStreamSession(const Napi::CallbackInfo& info) {
325
329
std::scoped_lock lock{canDevicesMtx};
326
330
auto deviceIterator = canDeviceMap.find (descriptor);
327
331
if (deviceIterator == canDeviceMap.end ()) {
328
- Napi::Error::New (env, DEVICE_NOT_FOUND_ERROR). ThrowAsJavaScriptException ( );
332
+ throwDeviceNotFoundError (env);
329
333
return Napi::Number::New (env, 0 );
330
334
}
331
335
@@ -366,7 +370,7 @@ Napi::Array readStreamSession(const Napi::CallbackInfo& info) {
366
370
std::scoped_lock lock{canDevicesMtx};
367
371
auto deviceIterator = canDeviceMap.find (descriptor);
368
372
if (deviceIterator == canDeviceMap.end ()) {
369
- Napi::Error::New (env, DEVICE_NOT_FOUND_ERROR). ThrowAsJavaScriptException ( );
373
+ throwDeviceNotFoundError (env);
370
374
return Napi::Array::New (env);
371
375
}
372
376
@@ -415,7 +419,7 @@ Napi::Number closeStreamSession(const Napi::CallbackInfo& info) {
415
419
std::scoped_lock lock{canDevicesMtx};
416
420
auto deviceIterator = canDeviceMap.find (descriptor);
417
421
if (deviceIterator == canDeviceMap.end ()) {
418
- Napi::Error::New (env, DEVICE_NOT_FOUND_ERROR). ThrowAsJavaScriptException ( );
422
+ throwDeviceNotFoundError (env);
419
423
return Napi::Number::New (env, 0 );
420
424
}
421
425
@@ -438,7 +442,7 @@ Napi::Object getCANDetailStatus(const Napi::CallbackInfo& info) {
438
442
std::scoped_lock lock{canDevicesMtx};
439
443
auto deviceIterator = canDeviceMap.find (descriptor);
440
444
if (deviceIterator == canDeviceMap.end ()) {
441
- Napi::Error::New (env, DEVICE_NOT_FOUND_ERROR). ThrowAsJavaScriptException ( );
445
+ throwDeviceNotFoundError (env);
442
446
return Napi::Object::New (env);
443
447
}
444
448
@@ -510,7 +514,7 @@ Napi::Number sendCANMessage(const Napi::CallbackInfo& info) {
510
514
}
511
515
int status = _sendCANMessage (descriptor, messageId, messageData, dataParam.Length (), repeatPeriodMs);
512
516
if (status < 0 ) {
513
- Napi::Error::New (env, DEVICE_NOT_FOUND_ERROR). ThrowAsJavaScriptException ( );
517
+ throwDeviceNotFoundError (env);
514
518
}
515
519
return Napi::Number::New (env, status);
516
520
}
@@ -538,7 +542,7 @@ Napi::Number sendRtrMessage(const Napi::CallbackInfo& info) {
538
542
}
539
543
int status = _sendCANMessage (descriptor, messageId, messageData, dataParam.Length (), repeatPeriodMs);
540
544
if (status < 0 ) {
541
- Napi::Error::New (env, DEVICE_NOT_FOUND_ERROR). ThrowAsJavaScriptException ( );
545
+ throwDeviceNotFoundError (env);
542
546
}
543
547
return Napi::Number::New (env, status);
544
548
}
@@ -736,7 +740,7 @@ Napi::Object getLatestMessageOfEveryReceivedArbId(const Napi::CallbackInfo& info
736
740
auto deviceIterator = canDeviceMap.find (descriptor);
737
741
if (deviceIterator == canDeviceMap.end ()) {
738
742
if (devicesRegisteredToHal.find (descriptor) != devicesRegisteredToHal.end ()) return receiveHalMessage (info);
739
- Napi::Error::New (env, DEVICE_NOT_FOUND_ERROR). ThrowAsJavaScriptException ( );
743
+ throwDeviceNotFoundError (env);
740
744
return Napi::Object::New (env);
741
745
}
742
746
device = deviceIterator->second ;
0 commit comments