6#include "can_internal.hxx"
8#include "dawn/common/poll_loop.hxx"
9#include "dawn/io/common.hxx"
10#include "dawn/porting/can.hxx"
14int CProtoCan::configureDesc(
const CDescObject &desc)
19 for (
size_t i = 0; i < desc.
getSize(); i++)
25 DAWNERR(
"Unsupported CAN config 0x%08" PRIx32
"\n", item->
cfgid.
v);
35 SProtoCanIOBind *tmp =
reinterpret_cast<SProtoCanIOBind *
>(item->
data + j);
39 j +=
sizeof(SProtoCanIOBind) / 4 + tmp->size;
47 devno =
static_cast<uint32_t
>(item->
data[0]);
53 nodeid =
static_cast<uint32_t
>(item->
data[0]);
59 DAWNERR(
"Unsupported CAN config 0x%08" PRIx32
"\n", item->
cfgid.
v);
68void CProtoCan::allocObject(SProtoCanIOBind *alloc)
70 for (
size_t i = 0; i < alloc->size; i++)
72 DAWNINFO(
"allocate object 0x%" PRIx32
"\n", alloc->objid[i]);
76 valloc.push_back(alloc);
79void CProtoCan::thread()
84 DAWNINFO(
"start CAN thread\n");
86 std::memset(fds, 0,
sizeof(fds));
89 fds[0].events = POLLIN;
91 callbacks.beforePoll = beforePoll;
92 callbacks.afterPoll = afterPoll;
93 callbacks.onPollReady = onPollReady;
98int CProtoCan::beforePoll(
void *priv,
struct pollfd *pfds, nfds_t nfds)
102 if (pfds ==
nullptr || nfds == 0)
111void CProtoCan::afterPoll(
void *priv,
struct pollfd *pfds, nfds_t nfds,
int ret)
119 DAWNERR(
"CAN poll failed %d\n", -errno);
123int CProtoCan::onPollReady(
void *priv,
struct pollfd *pfds, nfds_t nfds,
int pollRet)
129 if (priv ==
nullptr || pfds ==
nullptr || nfds == 0 || pollRet <= 0)
134 if ((pfds[0].revents & POLLIN) == 0)
141 ret = can_read(self->fd, &msg);
146 DAWNERR(
"CAN error frame id=0x%03" PRIx32
" len=%u\n",
147 static_cast<uint32_t
>(msg.id),
148 static_cast<unsigned>(msg.len));
153 ret = self->msgRecv(msg);
156 DAWNERR(
"Failed to handle msg %d\n", ret);
164int CProtoCan::canInitialize()
168 if (devno ==
static_cast<uint32_t
>(-1))
170 DAWNERR(
"Invalid CAN device number configuration\n");
174 snprintf(path,
sizeof(path), CAN_PATH_FMT, devno);
179 DAWNERR(
"can_open failed %d\n", -errno);
186 DAWNERR(
"can_init failed %d\n", ret);
193CProtoCan::~CProtoCan()
202 ret = configureDesc(
getDesc());
205 DAWNERR(
"CAN configure failed (error %d)\n", ret);
216 ret = canInitialize();
225 DAWNERR(
"failed to create registers %d\n", ret);
void setObjectMapItem(SObjectId::ObjectId id, CObject *obj)
Set an item in the object map.
Descriptor wrapper for individual object configuration.
size_t getSize() const
Get number of configuration items for this object.
SObjectCfg::SObjectCfgItem * objectCfgItemNext(size_t &offset) const
Get config item at current offset and advance past it.
CDescObject & getDesc()
Get descriptor object for this object.
static int run(CThreadedObject &threadCtl, struct pollfd *pfds, nfds_t nfds, int timeoutMs, const SPollLoopCallbacks &callbacks, void *priv)
Run poll loop until quit is requested.
CAN bus protocol for industrial automation.
int deinit()
De-initialize object.
int configure()
Configure object from descriptor data.
int init()
One-time initialize object after bindings are resolved.
int doStop()
Stop implementation hook.
bool hasThread() const
Check if a background thread is active.
int doStart()
Start implementation hook.
@ PROTO_CAN_CFG_DEVNO
CAN device number (0=can0)
@ PROTO_CAN_CFG_IOBIND
I/O object binding.
@ PROTO_CAN_CFG_NODEID
CAN node ID base address.
@ PROTO_CLASS_CAN
CAN bus protocol.
bool workerThreadRunning() const
Check if the worker thread is running.
int stopWorkerThread()
Stop the worker thread.
int startWorkerThread(Func &&func)
Start the worker thread with a given function.
CThreadedObject & workerThread()
Get a reference to this thread controller.
Out-of-tree user-extension hooks for Dawn.
Single configuration item within object.
ObjectCfgData_t data[]
Configuration data array (flexible, size from cfgid.s.size).
UObjectCfgId cfgid
Configuration ID header (type, class, id, size, rw, dtype).
Callback set for poll-based worker loops.
Common CAN message format for chardev and socketCAN.
ObjectCfgId v
Raw 32-bit ConfigID value (for storage, comparison).
uint32_t cls
Object class (bits 21-29, max 511).
uint32_t id
Configuration identifier (bits 0-4, max 31).
uint32_t size
Configuration data size in 32-bit words (bits 5-14, max 1023).
struct dawn::SObjectCfg::UObjectCfgId::@10 s
Bit-field structure for named member access.