Dawn Framework 1.0
Universal data acquisition framework for embedded systems
nxscope.cxx
1// dawn/src/proto/nxscope/nxscope.cxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#include "dawn/proto/nxscope/nxscope.hxx"
7
8#include <algorithm>
9#include <cstring>
10#include <new>
11#include <time.h>
12#include <unistd.h>
13
14#include "dawn/io/common.hxx"
15#include "dawn/io/ddata.hxx"
16
17using namespace dawn;
18
19#if defined(CONFIG_DAWN_PROTO_NXSCOPE_SAMPLE_THREAD) + defined(CONFIG_DAWN_IO_NOTIFY) != 1
20# error one sampling method can be supported
21#endif
22
23// doStop() stops threadRecv() before the transport - needs non-blocking recv
24
25static_assert(NXSCOPE_RECV_NONBLOCK == 1, "doStop() relies on non-blocking recv");
26
27// Interval in us for sample thread
28
29#define NXSCOPE_SAMPLE_INTERVAL (1000000)
30
31static inline uint16_t nxscopeU16Le(const uint8_t *p)
32{
33 return (uint16_t)p[0] | ((uint16_t)p[1] << 8);
34}
35
36static inline uint32_t nxscopeU32Le(const uint8_t *p)
37{
38 return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
39}
40
41#ifdef CONFIG_DAWN_IO_NOTIFY
42int CProtoNxscope::ioNotifierCb(void *priv, io_ddata_t *data)
43{
44 SProtoNxscopeIochan *chan = (SProtoNxscopeIochan *)priv;
45 int ret;
46
47 if (!chan || !chan->obj || !data || !chan->put)
48 {
49 return -EINVAL;
50 }
51
52 {
53 std::lock_guard<std::recursive_mutex> lock(chan->obj->streamLock);
54
55 ret = chan->put(&chan->obj->nxs, chan->chan, data, chan->dim);
56 }
57
58 // Wake threadRecv to flush this batch (cheap signal, not a flush).
59
60 sem_post(&chan->obj->streamSem);
61
62 return ret;
63}
64#endif
65
66int CProtoNxscope::sendAck(int ret)
67{
68 size_t len;
69
70 if (nxs.proto_cmd == nullptr || nxs.intf_cmd == nullptr)
71 {
72 return -EINVAL;
73 }
74
75 if (nxs.proto_cmd->ops == nullptr || nxs.intf_cmd->ops == nullptr)
76 {
77 return -EINVAL;
78 }
79
80 if (nxs.proto_cmd->ops->frame_final == nullptr || nxs.intf_cmd->ops->send == nullptr)
81 {
82 return -EINVAL;
83 }
84
85 if (nxs.txbuf == nullptr)
86 {
87 return -EINVAL;
88 }
89
90 if (nxs.txbuf_len < nxs.proto_cmd->hdrlen + sizeof(ret) + nxs.proto_cmd->footlen)
91 {
92 return -ENOBUFS;
93 }
94
95 len = nxs.proto_cmd->hdrlen;
96 std::memcpy(&nxs.txbuf[len], &ret, sizeof(ret));
97 len += sizeof(ret);
98
99 ret = nxs.proto_cmd->ops->frame_final(nxs.proto_cmd, NXSCOPE_HDRID_ACK, nxs.txbuf, &len);
100 if (ret < 0)
101 {
102 return ret;
103 }
104
105 return nxs.intf_cmd->ops->send(nxs.intf_cmd, nxs.txbuf, (int)len);
106}
107
108int CProtoNxscope::userIdCb(void *priv, uint8_t id, uint8_t *buff)
109{
110 CProtoNxscope *obj;
111 int ret;
112 bool handled;
113
114 obj = static_cast<CProtoNxscope *>(priv);
115 if (obj == nullptr || buff == nullptr)
116 {
117 return -EINVAL;
118 }
119
120 handled = (id == NXSCOPE_USER_SET_IO || id == NXSCOPE_USER_SET_IO_SEEK ||
121 id == NXSCOPE_USER_GET_IO || id == NXSCOPE_USER_GET_IO_SEEK);
122 if (!handled)
123 {
124 /* Ignore non-extension IDs routed through userid callback.
125 * In particular, ACK frames may be seen here and must not be ACKed again.
126 */
127 return OK;
128 }
129
130 ret = obj->handleUserCommand(id, buff);
131
132#ifdef CONFIG_LOGGING_NXSCOPE_ACKFRAMES
133 {
134 int ack;
135
136 ack = obj->sendAck(ret);
137 if (ack < 0)
138 {
139 DAWNERR("nxscope user ack failed: %d\n", ack);
140 return ack;
141 }
142 }
143#endif
144
145 return ret;
146}
147
148int CProtoNxscope::handleUserCommand(uint8_t id, uint8_t *buff)
149{
150 switch (id)
151 {
152 case NXSCOPE_USER_SET_IO:
153 {
154 return userSetIO(buff);
155 }
156
157 case NXSCOPE_USER_SET_IO_SEEK:
158 {
159 return userSetIOSeek(buff);
160 }
161
162 case NXSCOPE_USER_GET_IO:
163 {
164 return userGetIO(buff);
165 }
166
167 case NXSCOPE_USER_GET_IO_SEEK:
168 {
169 return userGetIOSeek(buff);
170 }
171
172 default:
173 {
174 return -ENOTSUP;
175 }
176 }
177}
178
179CProtoNxscope::SProtoNxscopeIochan *CProtoNxscope::findIochan(SObjectId::ObjectId objid)
180{
181 for (auto &iochan : vio)
182 {
183 if (iochan.io != nullptr && iochan.io->getIdV() == objid)
184 {
185 return &iochan;
186 }
187 }
188
189 return nullptr;
190}
191
192int CProtoNxscope::userSetIO(uint8_t *buff)
193{
195 uint16_t size;
196 SProtoNxscopeIochan *iochan;
197 CIOCommon *io;
198 io_ddata_t *iodata;
199 size_t oldSize;
200 int ret;
201
202 objid = nxscopeU32Le(buff);
203 size = nxscopeU16Le(&buff[4]);
204
205 iochan = findIochan(objid);
206 if (iochan == nullptr || iochan->io == nullptr)
207 {
208 return -ENOENT;
209 }
210
211 io = iochan->io;
212 if (!io->isWrite())
213 {
214 return -EPERM;
215 }
216
217 iodata = iochan->setData;
218 if (iodata == nullptr)
219 {
220 return -ENOMEM;
221 }
222
223 if (!io->isSeekable())
224 {
225 if (size != io->getDataSize())
226 {
227 return -EINVAL;
228 }
229
230 std::memcpy(iodata->getDataPtr(), &buff[6], size);
231 ret = io->setData(*iodata);
232 }
233 else
234 {
235 if (size > iodata->getDataSize())
236 {
237 return -EINVAL;
238 }
239
240 std::memcpy(iodata->getDataPtr(), &buff[6], size);
241
242 oldSize = iodata->N;
243 iodata->N = size;
244 ret = io->setData(*iodata);
245 iodata->N = oldSize;
246 }
247
248 return ret;
249}
250
251int CProtoNxscope::userSetIOSeek(uint8_t *buff)
252{
254 size_t offset;
255 uint16_t size;
256 SProtoNxscopeIochan *iochan;
257 CIOCommon *io;
258 io_ddata_t *iodata;
259 size_t oldSize;
260 int ret;
261
262 objid = nxscopeU32Le(buff);
263 offset = nxscopeU32Le(&buff[4]);
264 size = nxscopeU16Le(&buff[8]);
265
266 iochan = findIochan(objid);
267 if (iochan == nullptr || iochan->io == nullptr)
268 {
269 return -ENOENT;
270 }
271
272 io = iochan->io;
273 if (!io->isWrite())
274 {
275 return -EPERM;
276 }
277
278 if (!io->isSeekable())
279 {
280 return -ENOTSUP;
281 }
282
283 iodata = iochan->setData;
284 if (iodata == nullptr)
285 {
286 return -ENOMEM;
287 }
288
289 if (size > iodata->getDataSize())
290 {
291 return -EINVAL;
292 }
293
294 std::memcpy(iodata->getDataPtr(), &buff[10], size);
295
296 oldSize = iodata->N;
297 iodata->N = size;
298 ret = io->setData(*iodata, offset);
299 iodata->N = oldSize;
300
301 return ret;
302}
303
304int CProtoNxscope::userGetIO(uint8_t *buff)
305{
307 SProtoNxscopeIochan *iochan;
308 CIOCommon *io;
309 io_ddata_t *iodata;
310 int ret;
311
312 objid = nxscopeU32Le(buff);
313
314 iochan = findIochan(objid);
315 if (iochan == nullptr || iochan->io == nullptr)
316 {
317 return -ENOENT;
318 }
319
320 io = iochan->io;
321 if (!io->isRead())
322 {
323 return -EPERM;
324 }
325
326 if (io->isSeekable())
327 {
328 return -ENOTSUP;
329 }
330
331 if (iochan->getData == nullptr)
332 {
333 iochan->getData = io->ddata_alloc(1);
334 if (iochan->getData == nullptr)
335 {
336 return -ENOMEM;
337 }
338 }
339
340 iodata = iochan->getData;
341 ret = io->getData(*iodata, 1);
342 if (ret < 0)
343 {
344 return ret;
345 }
346
347 ret = sendUserData(objid, iodata->getDataPtr(), (uint16_t)io->getDataSize());
348 if (ret < 0)
349 {
350 return ret;
351 }
352
353 // A positive value is the intf send byte count - the user command ACK
354 // must carry a plain status.
355
356 return OK;
357}
358
359int CProtoNxscope::userGetIOSeek(uint8_t *buff)
360{
362 size_t offset;
363 uint16_t size;
364 SProtoNxscopeIochan *iochan;
365 CIOCommon *io;
366 io_ddata_t *iodata;
367 int ret;
368
369 objid = nxscopeU32Le(buff);
370 offset = nxscopeU32Le(&buff[4]);
371 size = nxscopeU16Le(&buff[8]);
372
373 iochan = findIochan(objid);
374 if (iochan == nullptr || iochan->io == nullptr)
375 {
376 return -ENOENT;
377 }
378
379 io = iochan->io;
380 if (!io->isRead())
381 {
382 return -EPERM;
383 }
384
385 if (!io->isSeekable())
386 {
387 return -ENOTSUP;
388 }
389
390 if (iochan->getData == nullptr)
391 {
392 iochan->getData = io->ddata_alloc(1, CONFIG_DAWN_PROTO_NXSCOPE_RXBUF_LEN);
393 if (iochan->getData == nullptr)
394 {
395 return -ENOMEM;
396 }
397 }
398
399 iodata = iochan->getData;
400 if (size > iodata->getDataSize())
401 {
402 return -EINVAL;
403 }
404
405 // A window past the end would ship a stale tail (short reads are silent)
406
407 if (io->getDataSize() != 0 && offset + size > io->getDataSize())
408 {
409 return -EINVAL;
410 }
411
412 ret = io->getData(*iodata, 1, offset);
413 if (ret < 0)
414 {
415 return ret;
416 }
417
418 ret = sendUserData(objid, iodata->getDataPtr(), size);
419 if (ret < 0)
420 {
421 return ret;
422 }
423
424 return OK;
425}
426
429int CProtoNxscope::sendUserData(SObjectId::ObjectId objid, const void *data, uint16_t size)
430{
431 size_t len;
432 int ret;
433
434 if (nxs.proto_cmd == nullptr || nxs.intf_cmd == nullptr || nxs.proto_cmd->ops == nullptr ||
435 nxs.intf_cmd->ops == nullptr || nxs.proto_cmd->ops->frame_final == nullptr ||
436 nxs.intf_cmd->ops->send == nullptr || userTxBuf == nullptr)
437 {
438 return -EINVAL;
439 }
440
441 if (userTxLen < nxs.proto_cmd->hdrlen + NXSCOPE_USER_DATA_HDR + size + nxs.proto_cmd->footlen)
442 {
443 return -ENOBUFS;
444 }
445
446 len = nxs.proto_cmd->hdrlen;
447 userTxBuf[len++] = (uint8_t)(objid & 0xff);
448 userTxBuf[len++] = (uint8_t)((objid >> 8) & 0xff);
449 userTxBuf[len++] = (uint8_t)((objid >> 16) & 0xff);
450 userTxBuf[len++] = (uint8_t)((objid >> 24) & 0xff);
451 userTxBuf[len++] = (uint8_t)(size & 0xff);
452 userTxBuf[len++] = (uint8_t)((size >> 8) & 0xff);
453 std::memcpy(&userTxBuf[len], data, size);
454 len += size;
455
456 ret = nxs.proto_cmd->ops->frame_final(nxs.proto_cmd, NXSCOPE_USER_GET_IO, userTxBuf, &len);
457 if (ret < 0)
458 {
459 return ret;
460 }
461
462 return nxs.intf_cmd->ops->send(nxs.intf_cmd, userTxBuf, (int)len);
463}
464
465int CProtoNxscope::configureNxscope()
466{
467 int ret;
468
469 // Default serial protocol implementation
470
471 ret = nxscope_proto_ser_init(&nxsProto, nullptr);
472 if (ret != OK)
473 {
474 DAWNERR("nxscope_proto_ser_init failed: %d\n", ret);
475 return ret;
476 }
477
478 // Connect callbacks
479
480 nxsCbs.userid_priv = this;
481 nxsCbs.userid = userIdCb;
482 nxsCbs.start_priv = nullptr;
483 nxsCbs.start = nullptr;
484
485 // Initialize nxscope
486
487 nxsCfg.streambuf_len = CONFIG_DAWN_PROTO_NXSCOPE_STREAMBUF_LEN;
488 nxsCfg.rxbuf_len = CONFIG_DAWN_PROTO_NXSCOPE_RXBUF_LEN;
489 nxsCfg.rx_padding = CONFIG_DAWN_PROTO_NXSCOPE_RX_PADDING;
490#ifdef CONFIG_LOGGING_NXSCOPE_CRICHANNELS
491 nxsCfg.cribuf_len = CONFIG_DAWN_PROTO_NXSCOPE_CRIBUF_LEN;
492#endif
493
494 nxsCfg.intf_cmd = &nxsIntf;
495 nxsCfg.intf_stream = &nxsIntf;
496 nxsCfg.proto_cmd = &nxsProto;
497 nxsCfg.proto_stream = &nxsProto;
498 nxsCfg.callbacks = &nxsCbs;
499 nxsCfg.channels = vchannels.size();
500
501 ret = nxscope_init(&nxs, &nxsCfg);
502 if (ret != OK)
503 {
504 DAWNERR("nxscope_init failed: %d\n", ret);
505 return ret;
506 }
507
508 return OK;
509}
510
511int CProtoNxscope::allocObject(SProtoNxscopeIOBind *alloc)
512{
513 DAWNINFO("allocate object 0x%" PRIx32 "\n", alloc->objid.v);
514
515 // Allocate object in map
516
517 setObjectMapItem(alloc->objid.v, nullptr);
518
519 // Store reference to chanel configuration
520
521 vchannels.push_back(alloc);
522
523 return OK;
524};
525
526int CProtoNxscope::allocNames(size_t j, SProtoNxscopeNames *alloc)
527{
528 DAWNINFO("channel %zu with name %s\n", j, alloc->name);
529
530 // Store reference to chanel configuration
531
532 vnames.push_back(alloc);
533
534 return OK;
535};
536
537uint8_t CProtoNxscope::getChannelDim(const CIOCommon &io)
538{
539 return io.getDataDim();
540}
541
542uint8_t CProtoNxscope::getChannelDtype(const CIOCommon &io)
543{
544 uint8_t dtype = io.getDtype();
545
546 switch (dtype)
547 {
548#ifdef CONFIG_DAWN_DTYPE_UINT8
550 {
551 return NXSCOPE_TYPE_UINT8;
552 }
553#endif
554
555#ifdef CONFIG_DAWN_DTYPE_INT8
557 {
558 return NXSCOPE_TYPE_INT8;
559 }
560#endif
561
562#ifdef CONFIG_DAWN_DTYPE_UINT16
564 {
565 return NXSCOPE_TYPE_UINT16;
566 }
567#endif
568
569#ifdef CONFIG_DAWN_DTYPE_INT16
571 {
572 return NXSCOPE_TYPE_INT16;
573 }
574#endif
575
576#ifdef CONFIG_DAWN_DTYPE_INT32
578 {
579 return NXSCOPE_TYPE_INT32;
580 }
581#endif
582
583#ifdef CONFIG_DAWN_DTYPE_UINT32
585 {
586 return NXSCOPE_TYPE_UINT32;
587 }
588#endif
589
590#ifdef CONFIG_DAWN_DTYPE_UINT64
592 {
593 return NXSCOPE_TYPE_UINT64;
594 }
595#endif
596
597#ifdef CONFIG_DAWN_DTYPE_FLOAT
599 {
600 return NXSCOPE_TYPE_FLOAT;
601 }
602#endif
603
604#ifdef CONFIG_DAWN_DTYPE_B16
606 {
607 return NXSCOPE_TYPE_B16;
608 }
609#endif
610
611#ifdef CONFIG_DAWN_DTYPE_UB16
613 {
614 return NXSCOPE_TYPE_UB16;
615 }
616#endif
617
618#ifdef CONFIG_DAWN_DTYPE_CHAR
620 {
621 return NXSCOPE_TYPE_CHAR;
622 }
623#endif
624
625 default:
626 {
627 DAWNERR("unsuported nxscope type %d\n", dtype);
628 return NXSCOPE_TYPE_NONE;
629 }
630 }
631}
632
633int CProtoNxscope::nxscopeChannelsCreate()
634{
635 union nxscope_chinfo_type_u u;
636 const SProtoNxscopeIOBind *alloc;
637 size_t bindidx;
638 size_t getmax = 0;
639 int chanid = 0;
640 int ret;
641
642 hasStreamChannels = false;
643
644 for (bindidx = 0; bindidx < vchannels.size(); bindidx++)
645 {
646 alloc = vchannels[bindidx];
647 const CIOCommon *io = getIO(alloc->objid.v);
648
649 if (io != nullptr)
650 {
651 SProtoNxscopeIochan iochan;
652 const char *name;
653
654 if (bindidx < vnames.size())
655 {
656 name = vnames[bindidx]->name;
657 }
658 else
659 {
660 name = "no-name";
661 }
662
663 DAWNINFO("nxscope channel %s %p 0x%x\n", name, io, alloc->objid.v);
664
665 // Store IO to nxscope chan map
666
667 iochan.chan = chanid;
668 iochan.dim = getChannelDim(*io);
669 iochan.stream = io->isRead();
670 iochan.io = (CIOCommon *)io;
671 iochan.obj = this;
672 iochan.setData = nullptr;
673 iochan.getData = nullptr;
674#ifdef CONFIG_DAWN_IO_NOTIFY
675 iochan.put = nullptr;
676#endif
677#ifdef CONFIG_DAWN_PROTO_NXSCOPE_SAMPLE_THREAD
678 iochan.sample = nullptr;
679 iochan.data = nullptr;
680#endif
681
682 if (io->isRead() == false && io->isWrite() == false)
683 {
684 DAWNERR("IO 0x%" PRIx32 " has neither read nor write support\n", alloc->objid.v);
685 return -EPERM;
686 }
687
688 // GET_IO returns the whole IO, GET_IO_SEEK at most one RXBUF chunk
689
690 if (io->isRead())
691 {
692 size_t n = io->getDataSize();
693
694 if (io->isSeekable())
695 {
696 n = std::min(n, (size_t)CONFIG_DAWN_PROTO_NXSCOPE_RXBUF_LEN);
697 }
698
699 getmax = std::max(getmax, n);
700 }
701
702 if (iochan.io->isWrite())
703 {
704 if (iochan.io->isSeekable())
705 {
706 iochan.setData = iochan.io->ddata_alloc(1, CONFIG_DAWN_PROTO_NXSCOPE_RXBUF_LEN);
707 }
708 else
709 {
710 iochan.setData = iochan.io->ddata_alloc(1);
711 }
712
713 if (iochan.setData == nullptr)
714 {
715 DAWNERR("failed to allocate set buffer for objid=0x%" PRIx32 "\n",
716 alloc->objid.v);
717#ifdef CONFIG_DAWN_PROTO_NXSCOPE_SAMPLE_THREAD
718 delete iochan.data;
719 iochan.data = nullptr;
720#endif
721 return -ENOMEM;
722 }
723 }
724
725 if (iochan.stream)
726 {
727 ret = bindChannelCallbacks(iochan, io->getDtype());
728 if (ret < 0)
729 {
730 if (iochan.io->isWrite())
731 {
732 DAWNINFO("set-only nxscope channel for objid=0x%" PRIx32 "\n",
733 alloc->objid.v);
734 iochan.stream = false;
735 }
736 else
737 {
738 DAWNERR("Unsupported data type %d for objid=0x%" PRIx32 "\n",
739 io->getDtype(),
740 alloc->objid.v);
741#ifdef CONFIG_DAWN_PROTO_NXSCOPE_SAMPLE_THREAD
742 delete iochan.data;
743 iochan.data = nullptr;
744#endif
745 delete iochan.setData;
746 iochan.setData = nullptr;
747 return ret;
748 }
749 }
750 }
751
752#ifdef CONFIG_DAWN_IO_NOTIFY
753 if (iochan.stream && iochan.io->isNotify() == false)
754 {
755 // Not streamable - still reachable with SET_IO / GET_IO
756
757 DAWNWARN("%s-only nxscope channel (no notify) for objid=0x%" PRIx32 "\n",
758 iochan.io->isWrite() ? "set" : "get",
759 alloc->objid.v);
760 iochan.stream = false;
761 }
762#endif
763
764#ifdef CONFIG_DAWN_PROTO_NXSCOPE_SAMPLE_THREAD
765 if (iochan.stream && iochan.io->isSeekable())
766 {
767 if (iochan.io->isWrite())
768 {
769 DAWNINFO("set-only nxscope channel (seekable) for "
770 "objid=0x%" PRIx32 "\n",
771 alloc->objid.v);
772 iochan.stream = false;
773 }
774 else
775 {
776 DAWNERR("seekable IO not supported by NXScope "
777 "(objid=0x%08" PRIx32 ")\n",
778 iochan.io->getIdV());
779 delete iochan.setData;
780 iochan.setData = nullptr;
781 return -ENOTSUP;
782 }
783 }
784
785 if (iochan.stream)
786 {
787 iochan.data = iochan.io->ddata_alloc(1);
788 if (iochan.data == nullptr)
789 {
790 DAWNERR("failed to allocate sample buffer for objid=0x%" PRIx32 "\n",
791 alloc->objid.v);
792 delete iochan.setData;
793 iochan.setData = nullptr;
794 return -ENOMEM;
795 }
796 }
797#endif
798
799 if (iochan.stream)
800 {
801 // Initialize nxscope stream channel
802 u.s.dtype = getChannelDtype(*io);
803 u.s._res = 0;
804 u.s.cri = 0;
805 // NOTE: nxscope_chan_init declares name as char* (non-const)
806 // but never modifies it. The char* in the struct is only
807 // stored and compared, so casting away const is safe here.
808 nxscope_chan_init(&nxs, chanid, const_cast<char *>(name), u.u8, iochan.dim, 0);
809 iochan.chan = chanid;
810 chanid += 1;
811 hasStreamChannels = true;
812 }
813 else
814 {
815 iochan.chan = 0;
816 }
817
818 vio.push_back(iochan);
819 }
820 else
821 {
822 DAWNERR("IO not found for objid=0x%" PRIx32 "\n", alloc->objid.v);
823 return -EIO;
824 }
825 }
826
827 // GET_IO response buffer: the lib txbuf only fits a CHINFO reply
828
829 delete[] userTxBuf;
830 userTxLen = nxsProto.hdrlen + NXSCOPE_USER_DATA_HDR + getmax + nxsProto.footlen;
831 userTxBuf = new (std::nothrow) uint8_t[userTxLen];
832 if (userTxBuf == nullptr)
833 {
834 userTxLen = 0;
835 return -ENOMEM;
836 }
837
838 return OK;
839};
840
841#ifdef CONFIG_DAWN_IO_NOTIFY
842template<uint8_t Type>
843int CProtoNxscope::putBatch(struct nxscope_s *nxs, uint8_t chan, io_ddata_t *data, uint8_t dim)
844{
845 // One put per batch; the batch offset includes timestamp padding
846
847 return nxscope_put_samples(
848 nxs, Type, chan, data->getDataPtr(0), dim, data->getBatch(), data->off);
849}
850#endif
851
852#ifdef CONFIG_DAWN_PROTO_NXSCOPE_SAMPLE_THREAD
853template<typename T, CProtoNxscope::nxscope_put_t<T> Put>
854int CProtoNxscope::sampleTyped(CProtoNxscope *obj, SProtoNxscopeIochan *iochan)
855{
856 int ret;
857
858 if (!iochan || !iochan->data)
859 {
860 return -EINVAL;
861 }
862
863 ret = iochan->io->getData(*iochan->data, 1);
864 if (ret == OK)
865 {
866 ret = Put(&obj->nxs, iochan->chan, static_cast<T *>(iochan->data->getDataPtr()), iochan->dim);
867 }
868
869 return ret;
870}
871#endif
872
873template<typename T, CProtoNxscope::nxscope_put_t<T> Put, uint8_t Type>
874void CProtoNxscope::bindTyped(SProtoNxscopeIochan &iochan)
875{
876#ifdef CONFIG_DAWN_IO_NOTIFY
877 iochan.put = &putBatch<Type>;
878#endif
879#ifdef CONFIG_DAWN_PROTO_NXSCOPE_SAMPLE_THREAD
880 iochan.sample = &sampleTyped<T, Put>;
881#endif
882}
883
884int CProtoNxscope::bindChannelCallbacks(SProtoNxscopeIochan &iochan, uint8_t dtype)
885{
886 switch (dtype)
887 {
888#ifdef CONFIG_DAWN_DTYPE_UINT8
890 {
891 bindTyped<uint8_t, nxscope_put_vuint8, NXSCOPE_TYPE_UINT8>(iochan);
892 break;
893 }
894#endif
895
896#ifdef CONFIG_DAWN_DTYPE_INT8
898 {
899 bindTyped<int8_t, nxscope_put_vint8, NXSCOPE_TYPE_INT8>(iochan);
900 break;
901 }
902#endif
903
904#ifdef CONFIG_DAWN_DTYPE_UINT16
906 {
907 bindTyped<uint16_t, nxscope_put_vuint16, NXSCOPE_TYPE_UINT16>(iochan);
908 break;
909 }
910#endif
911
912#ifdef CONFIG_DAWN_DTYPE_INT16
914 {
915 bindTyped<int16_t, nxscope_put_vint16, NXSCOPE_TYPE_INT16>(iochan);
916 break;
917 }
918#endif
919
920#ifdef CONFIG_DAWN_DTYPE_INT32
922 {
923 bindTyped<int32_t, nxscope_put_vint32, NXSCOPE_TYPE_INT32>(iochan);
924 break;
925 }
926#endif
927
928#ifdef CONFIG_DAWN_DTYPE_UINT32
930 {
931 bindTyped<uint32_t, nxscope_put_vuint32, NXSCOPE_TYPE_UINT32>(iochan);
932 break;
933 }
934#endif
935
936#ifdef CONFIG_DAWN_DTYPE_UINT64
938 {
939 bindTyped<uint64_t, nxscope_put_vuint64, NXSCOPE_TYPE_UINT64>(iochan);
940 break;
941 }
942#endif
943
944#ifdef CONFIG_DAWN_DTYPE_FLOAT
946 {
947 bindTyped<float, nxscope_put_vfloat, NXSCOPE_TYPE_FLOAT>(iochan);
948 break;
949 }
950#endif
951
952 default:
953 {
954 return -ENOTSUP;
955 }
956 }
957
958 return OK;
959}
960
961#ifdef CONFIG_DAWN_PROTO_NXSCOPE_SAMPLE_THREAD
962void CProtoNxscope::getAndPut(SProtoNxscopeIochan *iochan)
963{
964 int ret;
965
966 if (!iochan || !iochan->sample)
967 {
968 return;
969 }
970
971 std::lock_guard<std::recursive_mutex> lock(streamLock);
972
973 ret = iochan->sample(this, iochan);
974 if (ret < 0 && ret != -EAGAIN)
975 {
976 DAWNERR("sampling failed for chan=%d ret=%d\n", iochan->chan, ret);
977 }
978}
979#endif
980
981void CProtoNxscope::threadRecv()
982{
983 // Loop until stop called
984
985 do
986 {
987 bool waited = false;
988 int ret;
989
990 if (hasStreamChannels)
991 {
992#ifdef CONFIG_DAWN_IO_NOTIFY
993 struct timespec ts;
994
995 /* Wait until the notifier signals a batch is ready, or a short
996 * timeout elapses so client commands are still serviced when idle.
997 * This paces flushing at the data rate instead of a fixed tick.
998 */
999
1000 clock_gettime(CLOCK_MONOTONIC, &ts);
1001 ts.tv_nsec += CONFIG_DAWN_PROTO_NXSCOPE_RECV_INTERVAL * 1000L;
1002 ts.tv_sec += ts.tv_nsec / 1000000000L;
1003 ts.tv_nsec %= 1000000000L;
1004
1005 sem_clockwait(&streamSem, CLOCK_MONOTONIC, &ts);
1006 waited = true;
1007
1008 /* Coalesce any further pending batches into this one flush */
1009
1010 while (sem_trywait(&streamSem) == 0)
1011 {
1012 }
1013#endif
1014
1015 /* Flush stream data */
1016
1017 std::lock_guard<std::recursive_mutex> lock(streamLock);
1018
1019 ret = nxscope_stream(&nxs);
1020 if (ret < 0)
1021 {
1022 DAWNERR("ERROR: nxscope_stream failed %d\n", ret);
1023 }
1024 }
1025
1026 /* Handle recv data - command handlers share streamLock with the
1027 * producers (see nxscope.hxx), which is only safe with non-blocking recv
1028 */
1029
1030 {
1031 std::lock_guard<std::recursive_mutex> lock(streamLock);
1032
1033 ret = nxscope_recv(&nxs);
1034 }
1035
1036 if (ret < 0)
1037 {
1038 DAWNERR("ERROR: nxscope_recv failed %d\n", ret);
1039 }
1040
1041 // Pace command polling when there is no stream to wait on
1042
1043 if (!waited)
1044 {
1045 usleep(CONFIG_DAWN_PROTO_NXSCOPE_RECV_INTERVAL);
1046 }
1047 }
1048 while (!threadRecvMember.shouldQuit());
1049
1050 // Mark thread quit done
1051
1052 threadRecvMember.markThreadFinished();
1053}
1054
1055#ifdef CONFIG_DAWN_PROTO_NXSCOPE_SAMPLE_THREAD
1056void CProtoNxscope::threadSample()
1057{
1058 // Loop until stop called
1059
1060 do
1061 {
1062 for (auto &io : vio)
1063 {
1064 if (!io.stream)
1065 {
1066 continue;
1067 }
1068 getAndPut(&io);
1069 }
1070
1071 usleep(NXSCOPE_SAMPLE_INTERVAL);
1072 }
1073 while (!threadSampleMember.shouldQuit());
1074
1075 // Mark thread quit done
1076
1077 threadSampleMember.markThreadFinished();
1078}
1079
1080#endif // CONFIG_DAWN_PROTO_NXSCOPE_SAMPLE_THREAD
1081
1082CProtoNxscope::~CProtoNxscope()
1083{
1084 for (auto &io : vio)
1085 {
1086#ifdef CONFIG_DAWN_PROTO_NXSCOPE_SAMPLE_THREAD
1087 delete io.data;
1088 io.data = nullptr;
1089#endif
1090 delete io.setData;
1091 io.setData = nullptr;
1092 delete io.getData;
1093 io.getData = nullptr;
1094 }
1095
1096 vchannels.clear();
1097 vnames.clear();
1098 vio.clear();
1099
1100 delete[] userTxBuf;
1101 userTxBuf = nullptr;
1102
1103#ifdef CONFIG_DAWN_IO_NOTIFY
1104 sem_destroy(&streamSem);
1105#endif
1106
1107 // Deinit nxscope
1108
1109 nxscope_deinit(&nxs);
1110}
1111
1113{
1114 int ret;
1115
1116 ret = initPriv();
1117 if (ret != OK)
1118 {
1119 return ret;
1120 }
1121
1122 return OK;
1123}
1124
1126{
1127 // Allocated objects are mapped after handler bind, so create channels here
1128 return nxscopeChannelsCreate();
1129}
1130
1132{
1133 int ret;
1134
1135 ret = deinitPriv();
1136 if (ret != OK)
1137 {
1138 return ret;
1139 }
1140
1141 return OK;
1142}
1143
1145{
1146 int ret;
1147
1148#ifdef CONFIG_DAWN_IO_NOTIFY
1149 // Register notifications first - an advertised stream channel that cannot
1150 // notify is a start failure, not a silent downgrade
1151
1152 for (auto &io : vio)
1153 {
1154 if (!io.stream)
1155 {
1156 continue;
1157 }
1158 ret = io.io->setNotifier(ioNotifierCb, 0, (void *)&io);
1159 if (ret < 0)
1160 {
1161 DAWNERR("set notifier failed for objId = 0x%" PRIx32 " (%d)\n", io.io->getIdV(), ret);
1162 return ret;
1163 }
1164 }
1165#endif
1166
1167 ret = startPriv();
1168 if (ret != OK)
1169 {
1170 return ret;
1171 }
1172
1173 // Start recv thread
1174
1175 threadRecvMember.setThreadFunc([this]() { threadRecv(); });
1176 ret = threadRecvMember.threadStart();
1177 if (ret < 0)
1178 {
1179 return ret;
1180 }
1181
1182#ifdef CONFIG_DAWN_PROTO_NXSCOPE_SAMPLE_THREAD
1183 // Start sample thread
1184
1185 threadSampleMember.setThreadFunc([this]() { threadSample(); });
1186 ret = threadSampleMember.threadStart();
1187 if (ret < 0)
1188 {
1189 return ret;
1190 }
1191#endif
1192
1193 return OK;
1194};
1195
1197{
1198 int ret;
1199
1200 // Stop recv thread first - recv is non-blocking, so the thread quits on
1201 // its own and can't touch the transport after it's torn down
1202
1203 threadRecvMember.threadStop();
1204
1205#ifdef CONFIG_DAWN_PROTO_NXSCOPE_SAMPLE_THREAD
1206 // Stop sample thread
1207
1208 threadSampleMember.threadStop();
1209#endif
1210
1211 // Now the transport is unused - tear it down
1212
1213 ret = stopPriv();
1214
1215 return ret;
1216};
1217
1219{
1220#ifdef CONFIG_DAWN_PROTO_NXSCOPE_SAMPLE_THREAD
1221 return !(threadRecvMember.isStopped() & threadSampleMember.isStopped());
1222#else
1223 return !threadRecvMember.isStopped();
1224#endif
1225}
CIOCommon * getIO(SObjectId::ObjectId id)
Get an I/O object by ID.
Definition bindable.cxx:41
void setObjectMapItem(SObjectId::ObjectId id, CObject *obj)
Set an item in the object map.
Definition bindable.cxx:23
Base class for all I/O objects.
Definition common.hxx:30
virtual bool isWrite() const =0
Check if IO supports write operations.
int setData(IODataCmn &data, size_t offset=0)
Set data for I/O (public interface with stats tracking).
Definition common.hxx:421
int getData(IODataCmn &data, size_t len, size_t offset=0)
Get data from I/O (public interface with stats tracking).
Definition common.hxx:382
virtual bool isSeekable() const
Check if IO supports partial (seekable) access.
Definition common.hxx:531
virtual size_t getDataDim() const =0
Get data vector dimension.
io_ddata_t * ddata_alloc(size_t batch, size_t chunk_size=0)
Allocate data buffer for this I/O.
Definition common.cxx:249
virtual size_t getDataSize() const =0
Get data size in bytes.
virtual bool isRead() const =0
Check if IO supports read operations.
uint8_t getDtype() const
Get data type field.
Definition object.cxx:175
Real-time data visualization protocol (base class).
Definition nxscope.hxx:43
int doStop()
Stop implementation hook.
Definition nxscope.cxx:1196
bool hasThread() const
Check if a background thread is active.
Definition nxscope.cxx:1218
struct nxscope_intf_s nxsIntf
NXScope interface structure (from logging library).
Definition nxscope.hxx:90
int configure()
Configure object from descriptor data.
Definition nxscope.cxx:1112
int doStart()
Start implementation hook.
Definition nxscope.cxx:1144
int init()
One-time initialize object after bindings are resolved.
Definition nxscope.cxx:1125
int deinit()
De-initialize object.
Definition nxscope.cxx:1131
Out-of-tree user-extension hooks for Dawn.
Definition bindable.hxx:13
@ DTYPE_FLOAT
IEEE 754 single-precision floating point (32-bit).
Definition objectid.hxx:112
@ DTYPE_INT32
Signed 32-bit integer (-2147483648 to 2147483647).
Definition objectid.hxx:92
@ DTYPE_UINT8
Unsigned 8-bit integer (0 to 255).
Definition objectid.hxx:80
@ DTYPE_INT16
Signed 16-bit integer (-32768 to 32767).
Definition objectid.hxx:84
@ DTYPE_INT8
Signed 8-bit integer (-128 to 127).
Definition objectid.hxx:76
@ DTYPE_UINT64
Unsigned 64-bit integer.
Definition objectid.hxx:104
@ DTYPE_UINT16
Unsigned 16-bit integer (0 to 65535).
Definition objectid.hxx:88
@ DTYPE_UB16
Unsigned 16.16 fixed-point (32-bit).
Definition objectid.hxx:132
@ 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
@ DTYPE_B16
Signed 16.16 fixed-point (32-bit).
Definition objectid.hxx:128
uint32_t ObjectId
ObjectID type - single 32-bit value.
Definition objectid.hxx:44
Heap-allocated dynamic I/O data buffer.
Definition ddata.hxx:21
size_t getDataSize()
Get data size in bytes.
Definition ddata.hxx:153
void * getDataPtr(size_t batch=0)
Get pointer to data only (skips timestamp if present).
Definition ddata.hxx:180
size_t getBatch()
Get number of batches.
Definition ddata.hxx:135