|
| 1 | +// canbusfdtime.cpp |
| 2 | +// |
| 3 | +// Linux SocketCAN FD writer example. |
| 4 | +// |
| 5 | +// This writes the current time to the CAN bus 10 times per second as a |
| 6 | +// 12-byte CAN FD frame with nanosecond resolution. The payload format is: |
| 7 | +// |
| 8 | +// Bytes 0-7: int64_t seconds since the Unix epoch (big-endian host order) |
| 9 | +// Bytes 8-11: uint32_t nanosecond fraction |
| 10 | +// |
| 11 | +// This is a simple (though not overly precise) way to synchronize the time |
| 12 | +// for nodes on the CAN bus. |
| 13 | +// |
| 14 | +// -------------------------------------------------------------------------- |
| 15 | +// This file is part of the "sockpp" C++ socket library. |
| 16 | +// |
| 17 | +// Copyright (c) 2026 Frank Pagliughi |
| 18 | +// All rights reserved. |
| 19 | +// |
| 20 | +// Redistribution and use in source and binary forms, with or without |
| 21 | +// modification, are permitted provided that the following conditions are |
| 22 | +// met: |
| 23 | +// |
| 24 | +// 1. Redistributions of source code must retain the above copyright notice, |
| 25 | +// this list of conditions and the following disclaimer. |
| 26 | +// |
| 27 | +// 2. Redistributions in binary form must reproduce the above copyright |
| 28 | +// notice, this list of conditions and the following disclaimer in the |
| 29 | +// documentation and/or other materials provided with the distribution. |
| 30 | +// |
| 31 | +// 3. Neither the name of the copyright holder nor the names of its |
| 32 | +// contributors may be used to endorse or promote products derived from this |
| 33 | +// software without specific prior written permission. |
| 34 | +// |
| 35 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 36 | +// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 37 | +// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 38 | +// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 39 | +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 40 | +// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 41 | +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 42 | +// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 43 | +// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 44 | +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 45 | +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 46 | +// -------------------------------------------------------------------------- |
| 47 | + |
| 48 | +#include <time.h> |
| 49 | + |
| 50 | +#include <chrono> |
| 51 | +#include <cstdint> |
| 52 | +#include <cstring> |
| 53 | +#include <iostream> |
| 54 | +#include <string> |
| 55 | +#include <thread> |
| 56 | + |
| 57 | +#include "sockpp/canbus/canbus_socket.h" |
| 58 | +#include "sockpp/version.h" |
| 59 | + |
| 60 | +using namespace std; |
| 61 | + |
| 62 | +// The clock to use to pace the app. |
| 63 | +using sysclock = chrono::system_clock; |
| 64 | + |
| 65 | +// -------------------------------------------------------------------------- |
| 66 | + |
| 67 | +int main(int argc, char* argv[]) { |
| 68 | + cout << "Sample SocketCAN FD time writer for 'sockpp' " << sockpp::SOCKPP_VERSION << endl; |
| 69 | + |
| 70 | + string canIface = (argc > 1) ? argv[1] : "can0"; |
| 71 | + canid_t canID = (argc > 2) ? canid_t(strtoul(argv[2], nullptr, 0)) : 0x20; |
| 72 | + |
| 73 | + sockpp::initialize(); |
| 74 | + |
| 75 | + error_code ec{}; |
| 76 | + sockpp::canbus_address addr(canIface, ec); |
| 77 | + |
| 78 | + if (ec) { |
| 79 | + cerr << "Error finding the CAN interface: " << canIface << "\n\t" << ec.message() |
| 80 | + << endl; |
| 81 | + return 1; |
| 82 | + } |
| 83 | + |
| 84 | + sockpp::canbusfd_socket sock(addr, ec); |
| 85 | + if (ec) { |
| 86 | + cerr << "Error opening CAN FD socket on " << canIface << "\n\t" << ec.message() |
| 87 | + << endl; |
| 88 | + return 1; |
| 89 | + } |
| 90 | + |
| 91 | + cout << "Created CAN FD socket on " << sock.address() << endl; |
| 92 | + |
| 93 | + // Schedule the first send at the next 100ms boundary. |
| 94 | + auto next = sysclock::now(); |
| 95 | + |
| 96 | + while (true) { |
| 97 | + next += chrono::milliseconds(100); |
| 98 | + this_thread::sleep_until(next); |
| 99 | + |
| 100 | + // Get the current time with nanosecond resolution |
| 101 | + timespec ts{}; |
| 102 | + clock_gettime(CLOCK_REALTIME, &ts); |
| 103 | + |
| 104 | + // Pack into a 12-byte payload: 8-byte int64_t seconds + 4-byte uint32_t ns |
| 105 | + int64_t secs = int64_t(ts.tv_sec); |
| 106 | + uint32_t nsec = uint32_t(ts.tv_nsec); |
| 107 | + |
| 108 | + uint8_t payload[12]; |
| 109 | + memcpy(payload, &secs, sizeof(secs)); |
| 110 | + memcpy(payload + sizeof(secs), &nsec, sizeof(nsec)); |
| 111 | + |
| 112 | + sockpp::canbusfd_frame frame{canID, payload, sizeof(payload)}; |
| 113 | + if (auto res = sock.send(frame); !res) { |
| 114 | + cerr << "Error sending frame: " << res << endl; |
| 115 | + return 1; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return 0; |
| 120 | +} |
0 commit comments