OpenCPN Partial API docs
Loading...
Searching...
No Matches
comm_drv_n2k_socketcan.cpp
1/***************************************************************************
2 *
3 * Project: OpenCPN
4 * Purpose: Implement comm_drv_socketcan.h -- socketcan driver.
5 * Author: David Register, Alec Leamas
6 *
7 ***************************************************************************
8 * Copyright (C) 2022 by David Register, Alec Leamas *
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 * This program is distributed in the hope that it will be useful, *
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18 * GNU General Public License for more details. *
19 * *
20 * You should have received a copy of the GNU General Public License *
21 * along with this program; if not, write to the *
22 * Free Software Foundation, Inc., *
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
24 **************************************************************************/
25
26#if !defined(__linux__) || defined(__ANDROID__)
27#error "This file can only be compiled on Linux"
28#endif
29
30#include <algorithm>
31#include <atomic>
32#include <chrono>
33#include <mutex>
34#include <thread>
35#include <vector>
36#include <future>
37
38#include "config.h"
39
40#include <net/if.h>
41#include <serial/serial.h>
42#include <sys/ioctl.h>
43#include <sys/socket.h>
44#include <sys/time.h>
45
46#include <wx/log.h>
47#include <wx/string.h>
48#include <wx/utils.h>
49#include <wx/thread.h>
50
51#include "model/comm_can_util.h"
52#include "model/comm_drv_n2k_socketcan.h"
55#include "model/config_vars.h"
56
57#define DEFAULT_N2K_SOURCE_ADDRESS 72
58
59wxDEFINE_EVENT(EVT_N2K_59904, ObservedEvt);
60
61static const int kNotFound = -1;
62
64static const int kSocketTimeoutSeconds = 2;
65
66typedef struct can_frame CanFrame;
67
69
94class Worker {
95public:
96 Worker(CommDriverN2KSocketCAN* parent, const wxString& PortName);
97
98 bool StartThread();
99 void StopThread();
100 int GetSocket() { return m_socket; }
101
102private:
103 void Entry();
104
105 void ThreadMessage(const std::string& msg, wxLogLevel l = wxLOG_Message);
106
107 int InitSocket(const std::string port_name);
108 void SocketMessage(const std::string& msg, const std::string& device);
109 void HandleInput(CanFrame frame);
110 void ProcessRxMessages(std::shared_ptr<const Nmea2000Msg> n2k_msg);
111
112 std::vector<unsigned char> PushCompleteMsg(const CanHeader header,
113 int position,
114 const CanFrame frame);
115 std::vector<unsigned char> PushFastMsgFragment(const CanHeader& header,
116 int position);
117
118 CommDriverN2KSocketCanImpl* const m_parent_driver;
119 const wxString m_port_name;
120 std::atomic<int> m_run_flag;
121 FastMessageMap fast_messages;
122 int m_socket;
123};
124
127 friend class Worker;
128
129public:
132 m_worker(this, p->socketCAN_port),
133 m_source_address(-1),
134 m_last_TX_sequence(0) {
135 SetN2K_Name();
136 Open();
137 }
138
139 ~CommDriverN2KSocketCanImpl() { Close(); }
140
141 bool Open();
142 void Close();
143 void SetN2K_Name();
144
145 bool SendMessage(std::shared_ptr<const NavMsg> msg,
146 std::shared_ptr<const NavAddr> addr);
147
148 int DoAddressClaim();
149 bool SendAddressClaim(int proposed_source_address);
150 bool SendProductInfo();
151
152 Worker& GetWorker() { return m_worker; }
153 void UpdateAttrCanAddress();
154
155private:
156 N2kName node_name;
157 Worker m_worker;
158 int m_source_address;
159 int m_last_TX_sequence;
160 std::future<int> m_AddressClaimFuture;
161 wxMutex m_TX_mutex;
162 int m_unique_number;
163
164 ObservableListener listener_N2K_59904;
165 bool HandleN2K_59904(std::shared_ptr<const Nmea2000Msg> n2k_msg);
166};
167
168// Static CommDriverN2KSocketCAN factory implementation.
169
170std::unique_ptr<CommDriverN2KSocketCAN> CommDriverN2KSocketCAN::Create(
171 const ConnectionParams* params, DriverListener& listener) {
172 return std::unique_ptr<CommDriverN2KSocketCAN>(
173 new CommDriverN2KSocketCanImpl(params, listener));
174}
175
176// CommDriverN2KSocketCanImpl implementation
177
178void CommDriverN2KSocketCanImpl::SetN2K_Name() {
179 // We choose some "benign" values for OCPN socketCan interface
180 node_name.value.Name = 0;
181
182 m_unique_number = 1;
183 // Build a simple 16 bit hash of g_hostname, to use as unique "serial number"
184 int hash = 0;
185 std::string str(g_hostname.mb_str());
186 int len = str.size();
187 const char* ch = str.data();
188 for (int i = 0; i < len; i++)
189 hash = hash + ((hash) << 5) + *(ch + i) + ((*(ch + i)) << 7);
190 m_unique_number = ((hash) ^ (hash >> 16)) & 0xffff;
191
192 node_name.SetManufacturerCode(2046);
193 node_name.SetUniqueNumber(m_unique_number);
194 node_name.SetDeviceFunction(130); // Display
195 node_name.SetDeviceClass(120); // Display
196 node_name.SetIndustryGroup(4); // Marine
197 node_name.SetSystemInstance(0);
198}
199
200void CommDriverN2KSocketCanImpl::UpdateAttrCanAddress() {
201 this->attributes["canAddress"] = std::to_string(m_source_address);
202}
203
204bool CommDriverN2KSocketCanImpl::Open() {
205 // Start the RX worker thread
206 bool bws = m_worker.StartThread();
207 return bws;
208}
209
210void CommDriverN2KSocketCanImpl::Close() {
211 wxLogMessage("Closing N2K socketCAN: %s", m_params.socketCAN_port.c_str());
212 m_worker.StopThread();
213
214 // We cannot use shared_from_this() since we might be in the destructor.
215 auto& registry = CommDriverRegistry::GetInstance();
216 auto& me = FindDriver(registry.GetDrivers(), iface, bus);
217 registry.Deactivate(me);
218}
219
220bool CommDriverN2KSocketCanImpl::SendAddressClaim(int proposed_source_address) {
221 wxMutexLocker lock(m_TX_mutex);
222
223 int socket = GetWorker().GetSocket();
224
225 if (socket < 0) return false;
226
227 CanFrame frame;
228 memset(&frame, 0, sizeof(frame));
229
230 uint64_t _pgn = 60928;
231 unsigned long canId = BuildCanID(6, proposed_source_address, 255, _pgn);
232 frame.can_id = canId | CAN_EFF_FLAG;
233
234 // Load the data
235 uint32_t b32_0 = node_name.value.UnicNumberAndManCode;
236 memcpy(&frame.data, &b32_0, 4);
237
238 unsigned char b81 = node_name.value.DeviceInstance;
239 memcpy(&frame.data[4], &b81, 1);
240
241 b81 = node_name.value.DeviceFunction;
242 memcpy(&frame.data[5], &b81, 1);
243
244 b81 = (node_name.value.DeviceClass);
245 memcpy(&frame.data[6], &b81, 1);
246
247 b81 = node_name.value.IndustryGroupAndSystemInstance;
248 memcpy(&frame.data[7], &b81, 1);
249
250 frame.can_dlc = 8; // data length
251
252 int sentbytes = write(socket, &frame, sizeof(frame));
253
254 return (sentbytes == 16);
255}
256
257void AddStr(std::vector<uint8_t>& vec, std::string str, size_t max_len) {
258 size_t i;
259 for (i = 0; i < str.size(); i++) {
260 vec.push_back(str[i]);
261 ;
262 }
263 for (; i < max_len; i++) {
264 vec.push_back(0);
265 }
266}
267
268bool CommDriverN2KSocketCanImpl::SendProductInfo() {
269 // Create the payload
270 std::vector<uint8_t> payload;
271
272 payload.push_back(2100 & 0xFF); // N2KVersion
273 payload.push_back(2100 >> 8);
274 payload.push_back(0xEC); // Product Code, 1772
275 payload.push_back(0x06);
276
277 std::string ModelID("OpenCPN"); // Model ID
278 AddStr(payload, ModelID, 32);
279
280 std::string ModelSWCode(PACKAGE_VERSION); // SwCode
281 AddStr(payload, ModelSWCode, 32);
282
283 std::string ModelVersion(PACKAGE_VERSION); // Model Version
284 AddStr(payload, ModelVersion, 32);
285
286 std::string ModelSerialCode(
287 std::to_string(m_unique_number)); // Model Serial Code
288 AddStr(payload, ModelSerialCode, 32);
289
290 payload.push_back(0); // CertificationLevel
291 payload.push_back(0); // LoadEquivalency
292
293 auto dest_addr = std::make_shared<const NavAddr2000>(iface, 255);
294 uint64_t _PGN;
295 _PGN = 126996;
296
297 auto msg = std::make_shared<const Nmea2000Msg>(_PGN, payload, dest_addr);
298 SendMessage(msg, dest_addr);
299
300 return true;
301}
302
303bool CommDriverN2KSocketCanImpl::SendMessage(
304 std::shared_ptr<const NavMsg> msg, std::shared_ptr<const NavAddr> addr) {
305 wxMutexLocker lock(m_TX_mutex);
306
307 // Verify claimed address is useable
308 if (m_source_address < 0) return false;
309
310 if (m_source_address > 253) // Could not claim...
311 return false;
312
313 int socket = GetWorker().GetSocket();
314
315 if (socket < 0) return false;
316
317 CanFrame frame;
318 memset(&frame, 0, sizeof(frame));
319
320 auto msg_n2k = std::dynamic_pointer_cast<const Nmea2000Msg>(msg);
321 std::vector<uint8_t> load = msg_n2k->payload;
322
323 uint64_t _pgn = msg_n2k->PGN.pgn;
324 auto destination_address = std::static_pointer_cast<const NavAddr2000>(addr);
325
326 unsigned long canId = BuildCanID(msg_n2k->priority, m_source_address,
327 destination_address->address, _pgn);
328
329 frame.can_id = canId | CAN_EFF_FLAG;
330
331 if (load.size() <= 8) {
332 frame.can_dlc = load.size();
333 if (load.size() > 0) memcpy(&frame.data, load.data(), load.size());
334
335 int sentbytes = write(socket, &frame, sizeof(frame));
336 } else { // Fast Packet
337 int sequence = (m_last_TX_sequence + 0x20) & 0xE0;
338 m_last_TX_sequence = sequence;
339 unsigned char* data_ptr = load.data();
340 int n_remaining = load.size();
341
342 // First packet
343 frame.can_dlc = 8;
344 frame.data[0] = sequence;
345 frame.data[1] = load.size();
346 int data_len_0 = wxMin(load.size(), 6);
347 memcpy(&frame.data[2], load.data(), data_len_0);
348
349 int sentbytes0 = write(socket, &frame, sizeof(frame));
350
351 data_ptr += data_len_0;
352 n_remaining -= data_len_0;
353 sequence++;
354
355 // The rest of the bytes
356 while (n_remaining > 0) {
357 wxMilliSleep(10);
358 frame.data[0] = sequence;
359 int data_len_n = wxMin(n_remaining, 7);
360 memcpy(&frame.data[1], data_ptr, data_len_n);
361
362 int sentbytesn = write(socket, &frame, sizeof(frame));
363
364 data_ptr += data_len_n;
365 n_remaining -= data_len_n;
366 sequence++;
367 }
368 }
369
370 return true;
371}
372
373// CommDriverN2KSocketCAN implementation
374
375CommDriverN2KSocketCAN::CommDriverN2KSocketCAN(const ConnectionParams* params,
376 DriverListener& listener)
377 : CommDriverN2K(params->GetStrippedDSPort()),
378 m_params(*params),
379 m_listener(listener),
380 m_ok(false),
381 m_portstring(params->GetDSPort()),
382 m_baudrate(wxString::Format("%i", params->Baudrate)) {
383 this->attributes["canPort"] = params->socketCAN_port.ToStdString();
384 this->attributes["canAddress"] = std::to_string(DEFAULT_N2K_SOURCE_ADDRESS);
385 this->attributes["userComment"] = params->UserComment.ToStdString();
386 this->attributes["ioDirection"] = std::string("IN/OUT");
387}
388
389CommDriverN2KSocketCAN::~CommDriverN2KSocketCAN() {}
390
391// Worker implementation
392
393Worker::Worker(CommDriverN2KSocketCAN* parent, const wxString& port_name)
394 : m_parent_driver(dynamic_cast<CommDriverN2KSocketCanImpl*>(parent)),
395 m_port_name(port_name.Clone()),
396 m_run_flag(-1),
397 m_socket(-1) {
398 assert(m_parent_driver != 0);
399}
400
401std::vector<unsigned char> Worker::PushCompleteMsg(const CanHeader header,
402 int position,
403 const CanFrame frame) {
404 std::vector<unsigned char> data;
405 data.push_back(0x93);
406 data.push_back(0x13);
407 data.push_back(header.priority);
408 data.push_back(header.pgn & 0xFF);
409 data.push_back((header.pgn >> 8) & 0xFF);
410 data.push_back((header.pgn >> 16) & 0xFF);
411 data.push_back(header.destination);
412 data.push_back(header.source);
413 data.push_back(0xFF); // FIXME (dave) generate the time fields
414 data.push_back(0xFF);
415 data.push_back(0xFF);
416 data.push_back(0xFF);
417 data.push_back(CAN_MAX_DLEN); // nominally 8
418 for (size_t n = 0; n < CAN_MAX_DLEN; n++) data.push_back(frame.data[n]);
419 data.push_back(0x55); // CRC dummy, not checked
420 return data;
421}
422
423std::vector<unsigned char> Worker::PushFastMsgFragment(const CanHeader& header,
424 int position) {
425 std::vector<unsigned char> data;
426 data.push_back(0x93);
427 data.push_back(fast_messages[position].expected_length + 11);
428 data.push_back(header.priority);
429 data.push_back(header.pgn & 0xFF);
430 data.push_back((header.pgn >> 8) & 0xFF);
431 data.push_back((header.pgn >> 16) & 0xFF);
432 data.push_back(header.destination);
433 data.push_back(header.source);
434 data.push_back(0xFF); // FIXME (dave) Could generate the time fields
435 data.push_back(0xFF);
436 data.push_back(0xFF);
437 data.push_back(0xFF);
438 data.push_back(fast_messages[position].expected_length);
439 for (size_t n = 0; n < fast_messages[position].expected_length; n++)
440 data.push_back(fast_messages[position].data[n]);
441 data.push_back(0x55); // CRC dummy
442 fast_messages.Remove(position);
443 return data;
444}
445
446void Worker::ThreadMessage(const std::string& msg, wxLogLevel level) {
447 wxLogGeneric(level, wxString(msg.c_str()));
448 auto s = std::string("CommDriverN2KSocketCAN: ") + msg;
449 CommDriverRegistry::GetInstance().evt_driver_msg.Notify(level, s);
450}
451
452void Worker::SocketMessage(const std::string& msg, const std::string& device) {
453 std::stringstream ss;
454 ss << msg << device << ": " << strerror(errno);
455 ThreadMessage(ss.str());
456}
457
464int Worker::InitSocket(const std::string port_name) {
465 int sock = socket(PF_CAN, SOCK_RAW, CAN_RAW);
466 if (sock < 0) {
467 SocketMessage("SocketCAN socket create failed: ", port_name);
468 return -1;
469 }
470
471 // Get the interface index
472 struct ifreq if_request;
473 strcpy(if_request.ifr_name, port_name.c_str());
474 if (ioctl(sock, SIOCGIFINDEX, &if_request) < 0) {
475 SocketMessage("SocketCAN ioctl (SIOCGIFINDEX) failed: ", port_name);
476 return -1;
477 }
478
479 // Check if interface is UP
480 struct sockaddr_can can_address;
481 can_address.can_family = AF_CAN;
482 can_address.can_ifindex = if_request.ifr_ifindex;
483 if (ioctl(sock, SIOCGIFFLAGS, &if_request) < 0) {
484 SocketMessage("SocketCAN socket IOCTL (SIOCGIFFLAGS) failed: ", port_name);
485 return -1;
486 }
487 if (if_request.ifr_flags & IFF_UP) {
488 ThreadMessage("socketCan interface is UP");
489 } else {
490 ThreadMessage("socketCan interface is NOT UP");
491 return -1;
492 }
493
494 // Set timeout and bind
495 struct timeval tv;
496 tv.tv_sec = kSocketTimeoutSeconds;
497 tv.tv_usec = 0;
498 int r =
499 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv);
500 if (r < 0) {
501 SocketMessage("SocketCAN setsockopt SO_RCVTIMEO failed on device: ",
502 port_name);
503 return -1;
504 }
505 r = bind(sock, (struct sockaddr*)&can_address, sizeof(can_address));
506 if (r < 0) {
507 SocketMessage("SocketCAN socket bind() failed: ", port_name);
508 return -1;
509 }
510 return sock;
511}
512
519void Worker::HandleInput(CanFrame frame) {
520 int position = -1;
521 bool ready = true;
522
523 CanHeader header(frame);
524 if (header.IsFastMessage()) {
525 position = fast_messages.FindMatchingEntry(header, frame.data[0]);
526 if (position == kNotFound) {
527 // Not an existing fast message: create new entry and insert first frame
528 position = fast_messages.AddNewEntry();
529 ready = fast_messages.InsertEntry(header, frame.data, position);
530 } else {
531 // An existing fast message entry is present, append the frame
532 ready = fast_messages.AppendEntry(header, frame.data, position);
533 }
534 }
535 if (ready) {
536 std::vector<unsigned char> vec;
537 if (position >= 0) {
538 // Re-assembled fast message
539 vec = PushFastMsgFragment(header, position);
540 } else {
541 // Single frame message
542 vec = PushCompleteMsg(header, position, frame);
543 }
544 // auto name = N2kName(static_cast<uint64_t>(header.pgn));
545 auto src_addr = m_parent_driver->GetAddress(m_parent_driver->node_name);
546 auto msg = std::make_shared<const Nmea2000Msg>(header.pgn, vec, src_addr);
547 auto msg_all = std::make_shared<const Nmea2000Msg>(1, vec, src_addr);
548
549 ProcessRxMessages(msg);
550 m_parent_driver->m_listener.Notify(std::move(msg));
551 m_parent_driver->m_listener.Notify(std::move(msg_all));
552 }
553}
554
556void Worker::ProcessRxMessages(std::shared_ptr<const Nmea2000Msg> n2k_msg) {
557 if (n2k_msg->PGN.pgn == 59904) {
558 unsigned long RequestedPGN = 0;
559 RequestedPGN = n2k_msg->payload.at(15) << 16;
560 RequestedPGN += n2k_msg->payload.at(14) << 8;
561 RequestedPGN += n2k_msg->payload.at(13);
562
563 switch (RequestedPGN) {
564 case 60928:
565 m_parent_driver->SendAddressClaim(m_parent_driver->m_source_address);
566 break;
567 case 126996:
568 m_parent_driver->SendProductInfo();
569 break;
570 default:
571 break;
572 }
573 }
574
575 else if (n2k_msg->PGN.pgn == 60928) {
576 // Watch for conflicting source address
577 if (n2k_msg->payload.at(7) == m_parent_driver->m_source_address) {
578 // My name
579 uint64_t my_name = m_parent_driver->node_name.GetName();
580
581 // His name
582 uint64_t his_name = 0;
583 unsigned char* p = (unsigned char*)&his_name;
584 for (unsigned int i = 0; i < 8; i++) *p++ = n2k_msg->payload.at(13 + i);
585
586 // Compare literally the NAME values
587 if (his_name < my_name) {
588 // I lose, so select a new address
589 m_parent_driver->m_source_address++;
590 if (m_parent_driver->m_source_address > 253)
591 // Could not claim an address
592 m_parent_driver->m_source_address = 254;
593 m_parent_driver->UpdateAttrCanAddress();
594 }
595
596 // Claim the existing or modified address
597 m_parent_driver->SendAddressClaim(m_parent_driver->m_source_address);
598 }
599 }
600}
601
603void Worker::Entry() {
604 int recvbytes;
605 int socket;
606 CanFrame frame;
607
608 socket = InitSocket(m_port_name.ToStdString());
609 if (socket < 0) {
610 std::string msg("SocketCAN socket create failed: ");
611 ThreadMessage(msg + m_port_name.ToStdString());
612 m_run_flag = -1;
613 return;
614 }
615 m_socket = socket;
616
617 // Claim our default address
618 if (m_parent_driver->SendAddressClaim(DEFAULT_N2K_SOURCE_ADDRESS)) {
619 m_parent_driver->m_source_address = DEFAULT_N2K_SOURCE_ADDRESS;
620 m_parent_driver->UpdateAttrCanAddress();
621 }
622
623 // The main loop
624 while (m_run_flag > 0) {
625 recvbytes = read(socket, &frame, sizeof(frame));
626 if (recvbytes == -1) {
627 if (errno == EAGAIN || errno == EWOULDBLOCK) continue; // timeout
628
629 wxLogWarning("can socket %s: fatal error %s", m_port_name.c_str(),
630 strerror(errno));
631 break;
632 }
633 if (recvbytes != 16) {
634 wxLogWarning("can socket %s: bad frame size: %d (ignored)",
635 m_port_name.c_str(), recvbytes);
636 sleep(1);
637 continue;
638 }
639 HandleInput(frame);
640 }
641 m_run_flag = -1;
642 return;
643}
644
645bool Worker::StartThread() {
646 m_run_flag = 1;
647 std::thread t(&Worker::Entry, this);
648 t.detach();
649 return true;
650}
651
652void Worker::StopThread() {
653 if (m_run_flag < 0) {
654 wxLogMessage("Attempt to stop already dead thread (ignored).");
655 return;
656 }
657 wxLogMessage("Stopping Worker Thread");
658
659 m_run_flag = 0;
660 int tsec = 10;
661 while ((m_run_flag >= 0) && (tsec--)) wxSleep(1);
662
663 if (m_run_flag < 0)
664 wxLogMessage("StopThread: Stopped in %d sec.", 10 - tsec);
665 else
666 wxLogWarning("StopThread: Not Stopped after 10 sec.");
667}
const std::string iface
Physical device for 0183, else a unique string.
Definition comm_driver.h:88
CAN v2.0 29 bit header as used by NMEA 2000.
bool IsFastMessage() const
Return true if header reflects a multipart fast message.
Local driver implementation, not visible outside this file.
EventVar evt_driver_msg
Notified for messages from drivers.
Interface implemented by transport layer and possible other parties like test code which should handl...
Definition comm_driver.h:48
virtual void Notify(std::shared_ptr< const NavMsg > message)=0
Handle a received message.
const void Notify()
Notify all listeners, no data supplied.
Track fast message fragments eventually forming complete messages.
int AddNewEntry(void)
Allocate a new, fresh entry and return index to it.
void Remove(int pos)
Remove entry at pos.
bool AppendEntry(const CanHeader hdr, const unsigned char *data, int index)
Append fragment to existing multipart message.
int FindMatchingEntry(const CanHeader header, const unsigned char sid)
Setter.
bool InsertEntry(const CanHeader header, const unsigned char *data, int index)
Insert a new entry, first part of a multipart message.
Keeps listening over it's lifespan, removes itself on destruction.
Definition observable.h:131
Adds a std::shared<void> element to wxCommandEvent.
Manages reading the N2K data stream provided by some N2K gateways from the declared serial port.
DriverPtr & FindDriver(const std::vector< DriverPtr > &drivers, const std::string &iface, const NavAddr::Bus _bus)
Search list of drivers for a driver with given interface string.
Driver registration container, a singleton.
Raw messages layer, supports sending and recieving navmsg messages.
N2k uses CAN which defines the basic properties of messages.
Definition comm_navmsg.h:66