-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcan-mqtt-bridge.cpp
176 lines (136 loc) · 5.1 KB
/
can-mqtt-bridge.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <iostream>
#include <string>
#include <ctime>
#include <chrono>
#include <iomanip>
#include <unordered_map>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/can.h>
#include <linux/can/raw.h>
#include <fstream>
#include <Vector/DBC.h>
#include <mqtt/async_client.h>
void print_usage() {
std::cout << "can-mqtt-bridge: Decode and forward CAN bus signals to an MQTT broker.\n" << std::endl;
std::cout << "Usage: can-mqtt-bridge [options] <CAN interface>\n" << std::endl;
std::cout << "Options: -d FILE DBC file to decode CAN frames" << std::endl;
std::cout << " -f FREQUENCY Maximum frequency to forward to MQTT for each signal" << std::endl;
std::cout << " -v Verbose mode, prints all decoded values" << std::endl;
std::cout << " -H MQTT host to publish to" << std::endl;
std::cout << " -U MQTT username" << std::endl;
std::cout << " -P MQTT password" << std::endl;
std::cout << " -p MQTT port" << std::endl;
std::cout << " -h Display this help message\n" << std::endl;
std::cout << "Example: can-mqtt-bridge -d can.dbc -f 5 -H mqtt.example.com -U admin -P admin -p 1883 can0\n" << std::endl;
}
void log(std::string msg) {
std::time_t t = std::time(nullptr);
std::tm tm = *std::localtime(&t);
std::cout << '[' << std::put_time(&tm, "%F %T") << ']';
std::cout << ' ' << msg << std::endl;
}
int main(int argc, char **argv) {
int opt;
int period = 0;
int frequency = 0;
int verbose = 0;
std::string dbc_path = "can.dbc";
std::string mqtt_host = "";
std::string mqtt_username = "";
std::string mqtt_password = "";
int mqtt_port = 1883;
if (argc < 2) {
print_usage();
return 1;
}
while ((opt = getopt(argc, argv, "d:f:vH:U:P:p:h")) != -1) {
switch (opt) {
case 'd':
dbc_path = optarg;
break;
case 'f':
frequency = atoi(optarg);
if (frequency <= 0) {
frequency = 0;
period = 0;
}
else {
period = 1000000/frequency;
}
break;
case 'v':
verbose = 1;
break;
case 'H':
mqtt_host = optarg;
break;
case 'U':
mqtt_username = optarg;
break;
case 'P':
mqtt_password = optarg;
break;
case 'p':
mqtt_port = atoi(optarg);
break;
case 'h':
print_usage();
exit(1);
break;
}
}
std::string iface = argv[argc-1];
log("using DBC file " + dbc_path);
log("using frequency " + std::to_string(frequency) + " Hz");
log("using period " + std::to_string(period) + " us");
log("using MQTT host " + mqtt_host + ":" + std::to_string(mqtt_port));
int s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
struct sockaddr_can addr;
struct ifreq ifr;
struct can_frame frame;
int nbytes;
using clock = std::chrono::high_resolution_clock;
std::unordered_map< canid_t, clock::time_point > next_publish;
strcpy(ifr.ifr_name, iface.c_str());
ioctl(s, SIOCGIFINDEX, &ifr);
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
Vector::DBC::Network network;
std::ifstream dbc_file(dbc_path);
dbc_file >> network;
mqtt::async_client cli(mqtt_host+":"+std::to_string(mqtt_port), "");
mqtt::connect_options connopts;
connopts.set_user_name(mqtt_username);
connopts.set_password(mqtt_password);
connopts.set_automatic_reconnect(1,10);
cli.connect(connopts)->wait();
if (bind(s, (struct sockaddr *)&addr, sizeof(addr))) {
log("bind error");
return 1;
}
while (1) {
nbytes = read(s, &frame, sizeof(struct can_frame));
Vector::DBC::Message & message = network.messages[frame.can_id];
if (clock::now() < next_publish[frame.can_id]) {
if (verbose) log("dropped message from " + std::to_string(frame.can_id) + " because of frequency limit");
continue;
}
next_publish[frame.can_id] = clock::now() + std::chrono::microseconds(period);
std::vector<unsigned char> data(frame.data, frame.data + sizeof frame.data / sizeof frame.data[0]);
for (const auto & signal : message.signals) {
unsigned int rawValue = signal.second.decode(data);
double physicalValue = signal.second.rawToPhysicalValue(rawValue);
try {
cli.publish(signal.second.comment, std::to_string(physicalValue));
}
catch (const mqtt::exception& exc) {
log(exc.what());
}
if (verbose) log(signal.second.comment + " " + std::to_string(physicalValue));
}
}
}