daasiot_python
pydaasiot.cpp
Go to the documentation of this file.
1
15
16#include <pybind11/pybind11.h>
17#include <pybind11/stl.h>
18#include "daaswrapper.h"
19
20namespace py = pybind11;
21using namespace daas::api;
22
30class PyIDaasApiEvent : public IDaasApiEvent {
31public:
32 using IDaasApiEvent::IDaasApiEvent;
33
35 void dinAcceptedEvent(din_t din) override {
36 PYBIND11_OVERRIDE_PURE(void, IDaasApiEvent, dinAcceptedEvent, din);
37 }
38
40 void ddoReceivedEvent(int payload_size, typeset_t t, din_t din) override {
41 PYBIND11_OVERRIDE_PURE(void, IDaasApiEvent, ddoReceivedEvent, payload_size, t, din);
42 }
43
45 void frisbeeReceivedEvent(din_t din) override {
46 PYBIND11_OVERRIDE_PURE(void, IDaasApiEvent, frisbeeReceivedEvent, din);
47 }
48
50 void nodeStateReceivedEvent(din_t din) override {
51 PYBIND11_OVERRIDE_PURE(void, IDaasApiEvent, nodeStateReceivedEvent, din);
52 }
53
55 void atsSyncCompleted(din_t din) override {
56 PYBIND11_OVERRIDE_PURE(void, IDaasApiEvent, atsSyncCompleted, din);
57 }
58
60 void frisbeeDperfCompleted(din_t din, uint32_t packets_sent, uint32_t block_size) override {
61 PYBIND11_OVERRIDE_PURE(void, IDaasApiEvent, frisbeeDperfCompleted, din, packets_sent, block_size);
62 }
63};
64
75PYBIND11_MODULE(pydaasiot, m) {
76 m.doc() = "Python bindings for libdaas.a (DaaS-IoT SDK)";
77
82 py::enum_<daas_error_t>(m, "daas_error_t")
83 .value("ERROR_NONE", ERROR_NONE)
84 .value("ERROR_CORE_ALREADY_INITIALIZED", ERROR_CORE_ALREADY_INITIALIZED)
85 .value("ERROR_CORE_STOPPED", ERROR_CORE_STOPPED)
86 .value("ERROR_CANNOT_INITIALIZE", ERROR_CANNOT_INITIALIZE)
87 .value("ERROR_CANNOT_CREATE_NODE", ERROR_CANNOT_CREATE_NODE)
88 .value("ERROR_DIN_ALREADY_EXIST", ERROR_DIN_ALREADY_EXIST)
89 .value("ERROR_CANNOT_MAP_NODE", ERROR_CANNOT_MAP_NODE)
90 .value("ERROR_INVALID_USER_TYPESET", ERROR_INVALID_USER_TYPESET)
91 .value("ERROR_SEND_DDO", ERROR_SEND_DDO)
92 .value("ERROR_NO_DDO_PRESENT", ERROR_NO_DDO_PRESENT)
93 .value("ERROR_DIN_UNKNOWN", ERROR_DIN_UNKNOWN)
94 .value("ERROR_CHANNEL_FAILURE", ERROR_CHANNEL_FAILURE)
95 .value("ERROR_ATS_NOT_SYNCED", ERROR_ATS_NOT_SYNCED)
96 .value("ERROR_INVALID_DME", ERROR_INVALID_DME)
97 .value("ERROR_THREADS_ALREADY_STARTED", ERROR_THREADS_ALREADY_STARTED)
98 .value("ERROR_NOT_IMPLEMENTED", ERROR_NOT_IMPLEMENTED)
99 .value("ERROR_UNKNOWN", ERROR_UNKNOWN)
100 .export_values();
101
106 py::enum_<link_t>(m, "link_t")
107 .value("_LINK_NONE", _LINK_NONE)
108 .value("_LINK_DAAS", _LINK_DAAS)
109 .value("_LINK_INET4", _LINK_INET4)
110 .value("_LINK_BT", _LINK_BT)
111 .value("_LINK_MQTT5", _LINK_MQTT5)
112 .value("_LINK_UART", _LINK_UART)
113 .export_values();
114
119 py::enum_<performs_mode_t>(m, "performs_mode_t")
120 .value("PERFORM_CORE_THREAD", PERFORM_CORE_THREAD)
121 .value("PERFORM_CORE_NO_THREAD", PERFORM_CORE_NO_THREAD)
122 .export_values();
123
128 py::class_<DDO>(m, "DDO")
129 .def(py::init<>(), "Default constructor")
130 .def(py::init<typeset_t>(), "Constructor with a typeset")
131 .def(py::init<typeset_t, stime_t>(), "Constructor with typeset and timestamp")
132 .def("clearPayload", &DDO::clearPayload, "Clear the payload buffer")
133 .def("setOrigin", &DDO::setOrigin, "Set the origin device ID")
134 .def("setTypeset", &DDO::setTypeset, "Set the typeset field")
135 .def("setTimestamp", &DDO::setTimestamp, "Set the timestamp value")
136 .def("getOrigin", &DDO::getOrigin, "Get the origin device ID")
137 .def("getTypeset", &DDO::getTypeset, "Get the typeset")
138 .def("getTimestamp", &DDO::getTimestamp, "Get the timestamp")
139 .def("allocatePayload", &DDO::allocatePayload, "Allocate a payload buffer")
140 .def("appendPayloadData",
141 [](DDO &self, py::bytes data) {
142 std::string buffer = data;
143 return self.appendPayloadData(buffer.data(), (uint32_t)buffer.size());
144 },
145 "Append binary data to the payload")
146 .def("getPayloadSize", &DDO::getPayloadSize, "Return the payload size in bytes")
147 .def("getPayloadAsBinary",
148 [](DDO &self) {
149 uint32_t size = self.getPayloadSize();
150 std::vector<uint8_t> buf(size);
151 self.getPayloadAsBinary(buf.data(), 0, size);
152 return py::bytes(reinterpret_cast<char*>(buf.data()), size);
153 },
154 "Return the payload as a Python bytes object");
155
160 py::class_<IDaasApiEvent, PyIDaasApiEvent>(m, "IDaasApiEvent")
161 .def(py::init<>(), "Base class for DaaS event handlers");
162
167 py::class_<DaasWrapper>(m, "DaasWrapper")
168 .def(py::init([]() {
169 return new DaasWrapper(nullptr, nullptr);
170 }), "Default constructor with no configuration and no event handler")
171
172 .def(py::init<const char*, IDaasApiEvent*>(),
173 py::arg("config") = nullptr,
174 py::arg("eventHandler") = nullptr,
175 "Construct a DaaS wrapper with configuration and optional event handler")
176
177 .def("doInit", &DaasWrapper::doInit,
178 py::arg("sid"), py::arg("din"),
179 "Initialize the DaaS node with SID and DIN")
180
181 .def("doPerform", &DaasWrapper::doPerform,
182 py::arg("mode"), "Start internal execution loop")
183
184 .def("doEnd", &DaasWrapper::doEnd, "Terminate node activities")
185 .def("doReset", &DaasWrapper::doReset, "Reset the node")
186
187 .def("getVersion", &DaasWrapper::getVersion, "Return the library version")
188 .def("getInfos", &DaasWrapper::getInfos, "Return version and build information")
189
190 .def("listAvailableDrivers", &DaasWrapper::listAvailableDrivers, "List available drivers")
191
192 .def("enableDriver", &DaasWrapper::enableDriver,
193 py::arg("link"), py::arg("driver"),
194 "Enable a driver on this node")
195
196 .def("setupNode",
197 py::overload_cast<din_t, din_t, link_t, const char*>(&DaasWrapper::setupNode),
198 py::arg("sid"), py::arg("din"), py::arg("link"), py::arg("uri"),
199 "Configure and start a node (direct parameters)")
200
201 .def("setupNode",
202 py::overload_cast<const char*>(&DaasWrapper::setupNode),
203 py::arg("configFilePath"),
204 "Configure and start a node using a JSON config file")
205
206 .def("getStatus", &DaasWrapper::getStatus, "Return local node status")
207 .def("status", &DaasWrapper::status, py::arg("din"), "Return status of a remote node")
208 .def("fetch", &DaasWrapper::fetch, py::arg("din"), py::arg("opts"),
209 "Fetch state from a remote node")
210 .def("listNodes", &DaasWrapper::listNodes, "List known nodes")
211
212 .def("storeConfiguration", &DaasWrapper::storeConfiguration, py::arg("storage_interface"),
213 "Store configuration (requires C++ IDepot)")
214
215 .def("loadConfiguration", &DaasWrapper::loadConfiguration, py::arg("storage_interface"),
216 "Load configuration (requires C++ IDepot)")
217
218 .def("map", py::overload_cast<din_t>(&DaasWrapper::map), py::arg("din"),
219 "Map a known node")
220
221 .def("map", py::overload_cast<din_t, link_t, const char*>(&DaasWrapper::map),
222 py::arg("din"), py::arg("link"), py::arg("uri"),
223 "Map a node with link and URI")
224
225 .def("map", py::overload_cast<din_t, link_t, const char*, const char*>(&DaasWrapper::map),
226 py::arg("din"), py::arg("link"), py::arg("uri"), py::arg("securityKey"),
227 "Map a node with link, URI and security key")
228
229 .def("remove", &DaasWrapper::remove, py::arg("din"), "Remove a mapped node")
230 .def("locate", &DaasWrapper::locate, py::arg("din"), "Locate a node in the network")
231
232 .def("availablesPull", &DaasWrapper::availablesPull,
233 py::arg("din"), py::arg("count"),
234 "Return the number of available packets from a remote node")
235
236 .def("pull", [](DaasWrapper &self, din_t din) {
237 DDO* ddo_ptr = nullptr;
238 daas_error_t err = self.pull(din, &ddo_ptr);
239 if (err != ERROR_NONE || ddo_ptr == nullptr) {
240 return py::make_tuple(err, py::none());
241 }
242 return py::make_tuple(err, ddo_ptr);
243 }, py::arg("din"),
244 "Pull a DDO from a remote node; returns (err, ddo)")
245
246 .def("push", [](DaasWrapper &self, din_t din, DDO* outboundDDO) {
247 return self.push(din, outboundDDO);
248 }, py::arg("din"), py::arg("outboundDDO"),
249 "Push a DDO to a remote node")
250
251 .def("frisbee", &DaasWrapper::frisbee,
252 py::arg("din"),
253 "Send a frisbee ping to a node")
254
255 .def("getActiveInterfaces", &DaasWrapper::getActiveInterfaces,
256 "Get active network interfaces")
257
258 .def("listSystemInterfaces", &DaasWrapper::listSystemInterfaces,
259 "List all network interfaces on the system");
260}
void setOrigin(din_t)
void setTimestamp(stime_t tstamp)
void clearPayload()
void setTypeset(typeset_t)
uint32_t getPayloadSize(void)
uint32_t allocatePayload(uint32_t size_)
din_t getOrigin()
uint32_t appendPayloadData(const void *data_, uint32_t size_)
stime_t getTimestamp()
uint32_t getPayloadAsBinary(uint8_t *pbuffer_, unsigned offset_, uint32_t maxSize_)
typeset_t getTypeset()
Classe wrapper che incapsula la logica di utilizzo della libreria libdaas.a.
Definition daaswrapper.h:46
std::vector< std::string > getActiveInterfaces()
daas_error_t pull(din_t din, DDO **inboundDDO)
Riceve un pacchetto DDO dal nodo remoto.
daas_error_t doPerform(performs_mode_t mode)
Attiva l'esecuzione interna del nodo.
daas_error_t enableDriver(link_t link, const char *driver)
Abilita un driver su uno specifico link.
const nodestate_t & fetch(din_t din, uint16_t opts)
daas_error_t doInit(din_t sid, din_t din)
Inizializza il nodo DaaS con SID e DIN.
daas_error_t setupNode(din_t sid, din_t din, link_t link, const char *uri)
daas_error_t map(din_t din)
Mappa un nodo con DIN noto nella rete (formato base)
daas_error_t remove(din_t din)
Rimuove un nodo precedentemente mappato.
daas_error_t doEnd()
Termina le attivitą del nodo.
bool loadConfiguration(IDepot *storage_interface)
Carica la configurazione usando un'interfaccia IDepot.
const nodestate_t & status(din_t din)
daas_error_t doReset()
Resetta lo stato del nodo.
bool storeConfiguration(IDepot *storage_interface)
Salva la configurazione corrente usando un'interfaccia IDepot.
const char * getVersion()
Restituisce la versione della libreria libdaas.a.
std::vector< InterfaceInfo > listSystemInterfaces()
daas_error_t availablesPull(din_t din, uint32_t &count)
Restituisce il numero di pacchetti disponibili da ricevere.
const char * listAvailableDrivers()
const char * getInfos()
daas_error_t push(din_t din, DDO *outboundDDO)
Invia un pacchetto DDO al nodo remoto.
daas_error_t locate(din_t din)
Localizza un nodo in rete.
daas_error_t frisbee(din_t din)
Esegue un invio rapido (fire-and-forget)
@ _LINK_MQTT5
@ _LINK_INET4
@ _LINK_NONE
@ _LINK_DAAS
@ _LINK_UART
@ _LINK_BT
uint16_t typeset_t
daas_error_t
@ ERROR_CORE_STOPPED
@ ERROR_INVALID_DME
@ ERROR_THREADS_ALREADY_STARTED
@ ERROR_CANNOT_INITIALIZE
@ ERROR_INVALID_USER_TYPESET
@ ERROR_ATS_NOT_SYNCED
@ ERROR_UNKNOWN
@ ERROR_NONE
@ ERROR_CANNOT_MAP_NODE
@ ERROR_NO_DDO_PRESENT
@ ERROR_DIN_UNKNOWN
@ ERROR_CHANNEL_FAILURE
@ ERROR_NOT_IMPLEMENTED
@ ERROR_CORE_ALREADY_INITIALIZED
@ ERROR_CANNOT_CREATE_NODE
@ ERROR_SEND_DDO
@ ERROR_DIN_ALREADY_EXIST
uint32_t din_t
@ PERFORM_CORE_NO_THREAD
@ PERFORM_CORE_THREAD