Dawn Framework 1.0
Universal data acquisition framework for embedded systems
simple.hxx
1// dawn/include/dawn/proto/serial/simple.hxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#pragma once
7
8#include <cstdint>
9
10#include <poll.h>
11
12#include "dawn/common/thread.hxx"
13#include "dawn/porting/config.hxx"
14#include "dawn/proto/simplebase.hxx"
15
16namespace dawn
17{
26 : public CProtoSimpleBase
27 , protected CThreadedObject
28{
29public:
30 enum
31 {
32 PROTO_SERIAL_CFG_FIRST = 0,
33 PROTO_SERIAL_CFG_IOBIND = 1,
34 PROTO_SERIAL_CFG_PATH = 2,
35 PROTO_SERIAL_CFG_BAUD = 3,
36 PROTO_SERIAL_CFG_LAST = 31
37 } typedef EProtoSerialCfg;
38
40
41 explicit CProtoSerial(CDescObject &desc)
42 : CProtoSimpleBase(desc)
43 , path(CONFIG_DAWN_PROTO_SERIAL_PATH)
44 , baud(CONFIG_DAWN_PROTO_SERIAL_BAUD)
45 , fd(-1)
46 , parserPos(0)
47 , parserLen(0)
48 , parserState(0)
49 {
50 }
51
52 ~CProtoSerial() override;
53
54#ifdef CONFIG_DAWN_OBJECT_HAS_NAME
55 const char *getClassNameStr() const override
56 {
57 return "serial";
58 }
59#endif
60
61 int configure() override;
62 int init() override;
63 int deinit() override;
64 int doStart() override;
65 int doStop() override;
66 bool hasThread() const override;
67
68 constexpr static SObjectId::ObjectId objectId(uint16_t id)
69 {
72 }
73
74 constexpr static SObjectCfg::ObjectCfgId cfgId(bool rw, uint8_t dtype, uint8_t size, uint8_t id)
75 {
78 }
79
80 constexpr static SObjectCfg::ObjectCfgId cfgIdIOBind(uint16_t size)
81 {
82 return CProtoSerial::cfgId(false, SObjectId::DTYPE_ANY, size, PROTO_SERIAL_CFG_IOBIND);
83 }
84
85 constexpr static SObjectCfg::ObjectCfgId cfgIdPath(uint16_t size)
86 {
87 return CProtoSerial::cfgId(true, SObjectId::DTYPE_CHAR, size, PROTO_SERIAL_CFG_PATH);
88 }
89
90 constexpr static SObjectCfg::ObjectCfgId cfgIdBaud()
91 {
92 return CProtoSerial::cfgId(true, SObjectId::DTYPE_UINT32, 1, PROTO_SERIAL_CFG_BAUD);
93 }
94
95private:
96 const char *path;
97 uint32_t baud;
98 int fd;
99 size_t parserPos;
100 uint16_t parserLen;
101 int parserState;
102
103 int sendFrame(uint8_t cmd, const uint8_t *payload, size_t len) override;
104
105 int configureDesc(const CDescObject &desc);
106 int serialInit();
107 void thread();
108 int pollBefore(struct pollfd *pfds, nfds_t nfds);
109 void pollAfter(int ret);
110 int pollOnReady(struct pollfd *pfds, nfds_t nfds, int pollRet);
111
112 static int cbPollBefore(void *priv, struct pollfd *pfds, nfds_t nfds);
113 static void cbPollAfter(void *priv, struct pollfd *pfds, nfds_t nfds, int ret);
114 static int cbPollOnReady(void *priv, struct pollfd *pfds, nfds_t nfds, int pollRet);
115};
116
117} // Namespace dawn
Descriptor wrapper for individual object configuration.
@ PROTO_CLASS_SERIAL
Compact binary protocol over serial port.
Definition common.hxx:63
Simple binary serial protocol for device communication.
Definition simple.hxx:28
int configure()
Configure object from descriptor data.
Definition simple.cxx:307
int init()
One-time initialize object after bindings are resolved.
Definition simple.cxx:321
int doStart()
Start implementation hook.
Definition simple.cxx:366
bool hasThread() const
Check if a background thread is active.
Definition simple.cxx:395
int doStop()
Stop implementation hook.
Definition simple.cxx:382
int deinit()
De-initialize object.
Definition simple.cxx:349
Shared base for simple framed protocols.
Portable thread owner abstraction for Dawn components.
Definition thread.hxx:32
static ObjectCfgId objectCfg(uint8_t type, uint16_t cls, uint8_t dtype, bool rw, uint16_t size, uint8_t id)
Construct 32-bit ConfigID from component fields.
uint32_t ObjectCfgId
ConfigID type - single 32-bit value.
Definition objectcfg.hxx:60
Out-of-tree user-extension hooks for Dawn.
Definition bindable.hxx:13
Configuration for binding an I/O object to a simple protocol.
@ OBJTYPE_PROTO
Protocol object type.
Definition objectid.hxx:193
@ DTYPE_ANY
Wildcard data type (matches any actual type).
Definition objectid.hxx:68
@ DTYPE_CHAR
Character/string type (null-terminated, 4-byte aligned).
Definition objectid.hxx:144
@ DTYPE_UINT32
Unsigned 32-bit integer (0 to 4294967295).
Definition objectid.hxx:96
uint32_t ObjectId
ObjectID type - single 32-bit value.
Definition objectid.hxx:44
static ObjectId objectId(uint8_t type, uint16_t cls, uint8_t dtype, uint8_t flags, uint16_t priv)
Construct 32-bit ObjectID from component fields.
Definition objectid.hxx:290