Dawn Framework 1.0
Universal data acquisition framework for embedded systems
simple.hxx
1// dawn/include/dawn/proto/udp/simple.hxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#pragma once
7
8#include <cstdint>
9#include <mutex>
10
11#include <netinet/in.h>
12#include <poll.h>
13
14#include "dawn/common/thread.hxx"
15#include "dawn/porting/config.hxx"
16#include "dawn/proto/simplebase.hxx"
17
18namespace dawn
19{
28 : public CProtoSimpleBase
29 , protected CThreadedObject
30{
31public:
32 enum
33 {
34 PROTO_UDP_CFG_FIRST = 0,
35 PROTO_UDP_CFG_IOBIND = 1,
36 PROTO_UDP_CFG_PORT = 2,
37 PROTO_UDP_CFG_BIND_ADDR = 3,
38 PROTO_UDP_CFG_LAST = 31
39 };
40
42
43 explicit CProtoUdp(CDescObject &desc)
44 : CProtoSimpleBase(desc)
45 , port(CONFIG_DAWN_PROTO_UDP_PORT)
46 , fd(-1)
47 , sender()
48 {
49 }
50
51 ~CProtoUdp() override;
52
53#ifdef CONFIG_DAWN_OBJECT_HAS_NAME
54 const char *getClassNameStr() const override
55 {
56 return "udp";
57 }
58#endif
59
60 int configure() override;
61 int init() override;
62 int deinit() override;
63 int doStart() override;
64 int doStop() override;
65 bool hasThread() const override;
66
67 constexpr static SObjectId::ObjectId objectId(uint16_t id)
68 {
71 }
72
73 constexpr static SObjectCfg::ObjectCfgId cfgId(bool rw, uint8_t dtype, uint8_t size, uint8_t id)
74 {
77 }
78
79 constexpr static SObjectCfg::ObjectCfgId cfgIdIOBind(uint16_t size)
80 {
81 return CProtoUdp::cfgId(false, SObjectId::DTYPE_ANY, size, PROTO_UDP_CFG_IOBIND);
82 }
83
84 constexpr static SObjectCfg::ObjectCfgId cfgIdPort()
85 {
86 return CProtoUdp::cfgId(true, SObjectId::DTYPE_UINT16, 1, PROTO_UDP_CFG_PORT);
87 }
88
89private:
90 uint16_t port;
91 int fd;
92 struct sockaddr_in sender;
93 std::mutex senderMutex;
94
95 int sendFrame(uint8_t cmd, const uint8_t *payload, size_t len) override;
96
97 int configureDesc(const CDescObject &desc);
98 int udpInit();
99 void thread();
100 int pollBefore(struct pollfd *pfds, nfds_t nfds);
101 void pollAfter(int ret);
102 int pollOnReady(struct pollfd *pfds, nfds_t nfds, int pollRet);
103
104 static int cbPollBefore(void *priv, struct pollfd *pfds, nfds_t nfds);
105 static void cbPollAfter(void *priv, struct pollfd *pfds, nfds_t nfds, int ret);
106 static int cbPollOnReady(void *priv, struct pollfd *pfds, nfds_t nfds, int pollRet);
107};
108
109} // Namespace dawn
Descriptor wrapper for individual object configuration.
@ PROTO_CLASS_UDP
UDP-based protocol.
Definition common.hxx:79
Shared base for simple framed protocols.
Simple binary UDP protocol for device communication.
Definition simple.hxx:30
bool hasThread() const
Check if a background thread is active.
Definition simple.cxx:368
int deinit()
De-initialize object.
Definition simple.cxx:322
int init()
One-time initialize object after bindings are resolved.
Definition simple.cxx:294
int configure()
Configure object from descriptor data.
Definition simple.cxx:280
int doStart()
Start implementation hook.
Definition simple.cxx:339
int doStop()
Stop implementation hook.
Definition simple.cxx:355
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_UINT16
Unsigned 16-bit integer (0 to 65535).
Definition objectid.hxx:88
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