@@ -43,6 +43,7 @@ namespace {
4343// time to sleep after getting a CAN read error
4444constexpr std::chrono::milliseconds READ_ERR_SLEEP (100 );
4545constexpr std::chrono::milliseconds ACK_TIMEOUT (50 );
46+ constexpr std::chrono::milliseconds READ_TIMEOUT (100 );
4647// CAN26 11-bit ID layout: [priority:1][deviceUUID:7][peripheral:1][power:1][motor:1]
4748// Match on UUID field to filter for packets addressed to this device
4849constexpr uint32_t CAN_MASK = 0x3F8 ; // UUID field
@@ -66,7 +67,7 @@ std::unordered_map<
6667std::shared_mutex directReadMapMutex;
6768std::unordered_map<
6869 std::pair<CANDeviceUUID_t, uint16_t >,
69- std::function<void (CANMotorPacket_BLDC_DirectReadResult_Decoded_t)>> directReadCallbackMap;
70+ std::pair<std:: function<void (CANMotorPacket_BLDC_DirectReadResult_Decoded_t)>, std::thread >> directReadCallbackMap;
7071
7172// Endpoint JSONs
7273nlohmann::json pro_endpoints;
@@ -133,7 +134,7 @@ void handleDirectRead(CANPacket_t& packet) {
133134 // Unlock in case callback wants to modify the map?
134135 mapReadLock.unlock ();
135136 if (it != directReadCallbackMap.end ()) {
136- it->second (decoded);
137+ it->second . first (decoded);
137138 }
138139}
139140
@@ -362,7 +363,17 @@ void addDirectReadCallback(CANDevice_t device, uint16_t endpoint, const std::fun
362363 LOG_F (WARNING , " Callback already exists for 0x%x endpoint %d! Ignoring.." , device.deviceUUID , endpoint);
363364 return ;
364365 }
365- directReadCallbackMap.insert ({key, callback});
366+ std::thread timeout ([device, endpoint]() {
367+ std::this_thread::sleep_for (READ_TIMEOUT );
368+ LOG_F (ERROR , " 0x%x read of %d timed out!" , device.deviceUUID , endpoint);
369+ });
370+ auto element = std::make_pair<const std::function<void (CANMotorPacket_BLDC_DirectReadResult_Decoded_t)>, std::thread>(callback, timeout);
371+
372+ // std::unordered_map<
373+ // std::pair<CANDeviceUUID_t, uint16_t>,
374+ // std::pair<std::function<void(CANMotorPacket_BLDC_DirectReadResult_Decoded_t)>, std::thread>> directReadCallbackMap;
375+
376+ directReadCallbackMap.insert ({key, element});
366377}
367378
368379void removeDirectReadCallback (CANDevice_t device, uint16_t endpoint) {
0 commit comments