-
Notifications
You must be signed in to change notification settings - Fork 0
/
dstccallback.cpp
56 lines (45 loc) · 1.87 KB
/
dstccallback.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright (C) 2019, Jaguar Land Rover
// This program is licensed under the terms and conditions of the
// Mozilla Public License, version 2.0. The full text of the
// Mozilla Public License is at https://www.mozilla.org/MPL/2.0/
//
// Author:Steven Martin ([email protected])
#include "dstccallback.hpp"
#include <iostream>
namespace dstc {
CallbackHandler _callback_handler;
void dstcCallbackHandlerRoutine(dstc_callback_t callback_ref,
rmc_node_id_t node_id,
uint8_t *name,
uint8_t* payload,
uint16_t payload_len) {
_callback_handler.execute(callback_ref, payload, payload_len);
}
CallbackHandler::CallbackHandler()
: _next_id(1)
{}
void CallbackHandler::execute(dstc_callback_t callback_ref, uint8_t* payload, uint16_t payload_len) {
std::shared_ptr<CallbackFunctionBase> function_ptr;
{ // lock scope
std::lock_guard<std::mutex> lock(_callback_mtx);
auto function_iter = _callbacks.find(callback_ref);
if (function_iter == _callbacks.end()) {
std::runtime_error("CallbackHandler received a callback_ref that has not been registered");
}
else {
function_ptr = function_iter->second;
_callbacks.erase(function_iter);
}
} // end scope lock
if (function_ptr) {
return function_ptr->execute(payload, payload_len);
}
}
dstc_callback_t CallbackHandler::_getUniqueCallbackID() {
if (_next_id == 0) ++_next_id;
while (_callbacks.count(_next_id) > 0) {
++_next_id;
}
return _next_id++;
}
}