6#include "dawn/proto/nxscope/nxscope.hxx"
14#include "dawn/io/common.hxx"
15#include "dawn/io/ddata.hxx"
19#if defined(CONFIG_DAWN_PROTO_NXSCOPE_SAMPLE_THREAD) + defined(CONFIG_DAWN_IO_NOTIFY) != 1
20# error one sampling method can be supported
25static_assert(NXSCOPE_RECV_NONBLOCK == 1,
"doStop() relies on non-blocking recv");
29#define NXSCOPE_SAMPLE_INTERVAL (1000000)
31static inline uint16_t nxscopeU16Le(
const uint8_t *p)
33 return (uint16_t)p[0] | ((uint16_t)p[1] << 8);
36static inline uint32_t nxscopeU32Le(
const uint8_t *p)
38 return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
41#ifdef CONFIG_DAWN_IO_NOTIFY
42int CProtoNxscope::ioNotifierCb(
void *priv,
io_ddata_t *data)
44 SProtoNxscopeIochan *chan = (SProtoNxscopeIochan *)priv;
47 if (!chan || !chan->obj || !data || !chan->put)
53 std::lock_guard<std::recursive_mutex> lock(chan->obj->streamLock);
55 ret = chan->put(&chan->obj->nxs, chan->chan, data, chan->dim);
60 sem_post(&chan->obj->streamSem);
66int CProtoNxscope::sendAck(
int ret)
70 if (nxs.proto_cmd ==
nullptr || nxs.intf_cmd ==
nullptr)
75 if (nxs.proto_cmd->ops ==
nullptr || nxs.intf_cmd->ops ==
nullptr)
80 if (nxs.proto_cmd->ops->frame_final ==
nullptr || nxs.intf_cmd->ops->send ==
nullptr)
85 if (nxs.txbuf ==
nullptr)
90 if (nxs.txbuf_len < nxs.proto_cmd->hdrlen +
sizeof(ret) + nxs.proto_cmd->footlen)
95 len = nxs.proto_cmd->hdrlen;
96 std::memcpy(&nxs.txbuf[len], &ret,
sizeof(ret));
99 ret = nxs.proto_cmd->ops->frame_final(nxs.proto_cmd, NXSCOPE_HDRID_ACK, nxs.txbuf, &len);
105 return nxs.intf_cmd->ops->send(nxs.intf_cmd, nxs.txbuf, (
int)len);
108int CProtoNxscope::userIdCb(
void *priv, uint8_t
id, uint8_t *buff)
114 obj =
static_cast<CProtoNxscope *
>(priv);
115 if (obj ==
nullptr || buff ==
nullptr)
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);
130 ret = obj->handleUserCommand(
id, buff);
132#ifdef CONFIG_LOGGING_NXSCOPE_ACKFRAMES
136 ack = obj->sendAck(ret);
139 DAWNERR(
"nxscope user ack failed: %d\n", ack);
148int CProtoNxscope::handleUserCommand(uint8_t
id, uint8_t *buff)
152 case NXSCOPE_USER_SET_IO:
154 return userSetIO(buff);
157 case NXSCOPE_USER_SET_IO_SEEK:
159 return userSetIOSeek(buff);
162 case NXSCOPE_USER_GET_IO:
164 return userGetIO(buff);
167 case NXSCOPE_USER_GET_IO_SEEK:
169 return userGetIOSeek(buff);
181 for (
auto &iochan : vio)
183 if (iochan.io !=
nullptr && iochan.io->getIdV() == objid)
192int CProtoNxscope::userSetIO(uint8_t *buff)
196 SProtoNxscopeIochan *iochan;
202 objid = nxscopeU32Le(buff);
203 size = nxscopeU16Le(&buff[4]);
205 iochan = findIochan(objid);
206 if (iochan ==
nullptr || iochan->io ==
nullptr)
217 iodata = iochan->setData;
218 if (iodata ==
nullptr)
230 std::memcpy(iodata->
getDataPtr(), &buff[6], size);
240 std::memcpy(iodata->
getDataPtr(), &buff[6], size);
251int CProtoNxscope::userSetIOSeek(uint8_t *buff)
256 SProtoNxscopeIochan *iochan;
262 objid = nxscopeU32Le(buff);
263 offset = nxscopeU32Le(&buff[4]);
264 size = nxscopeU16Le(&buff[8]);
266 iochan = findIochan(objid);
267 if (iochan ==
nullptr || iochan->io ==
nullptr)
283 iodata = iochan->setData;
284 if (iodata ==
nullptr)
294 std::memcpy(iodata->
getDataPtr(), &buff[10], size);
298 ret = io->
setData(*iodata, offset);
304int CProtoNxscope::userGetIO(uint8_t *buff)
307 SProtoNxscopeIochan *iochan;
312 objid = nxscopeU32Le(buff);
314 iochan = findIochan(objid);
315 if (iochan ==
nullptr || iochan->io ==
nullptr)
331 if (iochan->getData ==
nullptr)
334 if (iochan->getData ==
nullptr)
340 iodata = iochan->getData;
359int CProtoNxscope::userGetIOSeek(uint8_t *buff)
364 SProtoNxscopeIochan *iochan;
369 objid = nxscopeU32Le(buff);
370 offset = nxscopeU32Le(&buff[4]);
371 size = nxscopeU16Le(&buff[8]);
373 iochan = findIochan(objid);
374 if (iochan ==
nullptr || iochan->io ==
nullptr)
390 if (iochan->getData ==
nullptr)
392 iochan->getData = io->
ddata_alloc(1, CONFIG_DAWN_PROTO_NXSCOPE_RXBUF_LEN);
393 if (iochan->getData ==
nullptr)
399 iodata = iochan->getData;
412 ret = io->
getData(*iodata, 1, offset);
418 ret = sendUserData(objid, iodata->
getDataPtr(), size);
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)
441 if (userTxLen < nxs.proto_cmd->hdrlen + NXSCOPE_USER_DATA_HDR + size + nxs.proto_cmd->footlen)
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);
456 ret = nxs.proto_cmd->ops->frame_final(nxs.proto_cmd, NXSCOPE_USER_GET_IO, userTxBuf, &len);
462 return nxs.intf_cmd->ops->send(nxs.intf_cmd, userTxBuf, (
int)len);
465int CProtoNxscope::configureNxscope()
471 ret = nxscope_proto_ser_init(&nxsProto,
nullptr);
474 DAWNERR(
"nxscope_proto_ser_init failed: %d\n", ret);
480 nxsCbs.userid_priv =
this;
481 nxsCbs.userid = userIdCb;
482 nxsCbs.start_priv =
nullptr;
483 nxsCbs.start =
nullptr;
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;
496 nxsCfg.proto_cmd = &nxsProto;
497 nxsCfg.proto_stream = &nxsProto;
498 nxsCfg.callbacks = &nxsCbs;
499 nxsCfg.channels = vchannels.size();
501 ret = nxscope_init(&nxs, &nxsCfg);
504 DAWNERR(
"nxscope_init failed: %d\n", ret);
511int CProtoNxscope::allocObject(SProtoNxscopeIOBind *alloc)
513 DAWNINFO(
"allocate object 0x%" PRIx32
"\n", alloc->objid.v);
521 vchannels.push_back(alloc);
526int CProtoNxscope::allocNames(
size_t j, SProtoNxscopeNames *alloc)
528 DAWNINFO(
"channel %zu with name %s\n", j, alloc->name);
532 vnames.push_back(alloc);
537uint8_t CProtoNxscope::getChannelDim(
const CIOCommon &io)
542uint8_t CProtoNxscope::getChannelDtype(
const CIOCommon &io)
548#ifdef CONFIG_DAWN_DTYPE_UINT8
551 return NXSCOPE_TYPE_UINT8;
555#ifdef CONFIG_DAWN_DTYPE_INT8
558 return NXSCOPE_TYPE_INT8;
562#ifdef CONFIG_DAWN_DTYPE_UINT16
565 return NXSCOPE_TYPE_UINT16;
569#ifdef CONFIG_DAWN_DTYPE_INT16
572 return NXSCOPE_TYPE_INT16;
576#ifdef CONFIG_DAWN_DTYPE_INT32
579 return NXSCOPE_TYPE_INT32;
583#ifdef CONFIG_DAWN_DTYPE_UINT32
586 return NXSCOPE_TYPE_UINT32;
590#ifdef CONFIG_DAWN_DTYPE_UINT64
593 return NXSCOPE_TYPE_UINT64;
597#ifdef CONFIG_DAWN_DTYPE_FLOAT
600 return NXSCOPE_TYPE_FLOAT;
604#ifdef CONFIG_DAWN_DTYPE_B16
607 return NXSCOPE_TYPE_B16;
611#ifdef CONFIG_DAWN_DTYPE_UB16
614 return NXSCOPE_TYPE_UB16;
618#ifdef CONFIG_DAWN_DTYPE_CHAR
621 return NXSCOPE_TYPE_CHAR;
627 DAWNERR(
"unsuported nxscope type %d\n", dtype);
628 return NXSCOPE_TYPE_NONE;
633int CProtoNxscope::nxscopeChannelsCreate()
635 union nxscope_chinfo_type_u u;
636 const SProtoNxscopeIOBind *alloc;
642 hasStreamChannels =
false;
644 for (bindidx = 0; bindidx < vchannels.size(); bindidx++)
646 alloc = vchannels[bindidx];
647 const CIOCommon *io =
getIO(alloc->objid.v);
651 SProtoNxscopeIochan iochan;
654 if (bindidx < vnames.size())
656 name = vnames[bindidx]->name;
663 DAWNINFO(
"nxscope channel %s %p 0x%x\n", name, io, alloc->objid.v);
667 iochan.chan = chanid;
668 iochan.dim = getChannelDim(*io);
669 iochan.stream = io->
isRead();
670 iochan.io = (CIOCommon *)io;
672 iochan.setData =
nullptr;
673 iochan.getData =
nullptr;
674#ifdef CONFIG_DAWN_IO_NOTIFY
675 iochan.put =
nullptr;
677#ifdef CONFIG_DAWN_PROTO_NXSCOPE_SAMPLE_THREAD
678 iochan.sample =
nullptr;
679 iochan.data =
nullptr;
684 DAWNERR(
"IO 0x%" PRIx32
" has neither read nor write support\n", alloc->objid.v);
696 n = std::min(n, (
size_t)CONFIG_DAWN_PROTO_NXSCOPE_RXBUF_LEN);
699 getmax = std::max(getmax, n);
702 if (iochan.io->isWrite())
704 if (iochan.io->isSeekable())
706 iochan.setData = iochan.io->ddata_alloc(1, CONFIG_DAWN_PROTO_NXSCOPE_RXBUF_LEN);
710 iochan.setData = iochan.io->ddata_alloc(1);
713 if (iochan.setData ==
nullptr)
715 DAWNERR(
"failed to allocate set buffer for objid=0x%" PRIx32
"\n",
717#ifdef CONFIG_DAWN_PROTO_NXSCOPE_SAMPLE_THREAD
719 iochan.data =
nullptr;
727 ret = bindChannelCallbacks(iochan, io->
getDtype());
730 if (iochan.io->isWrite())
732 DAWNINFO(
"set-only nxscope channel for objid=0x%" PRIx32
"\n",
734 iochan.stream =
false;
738 DAWNERR(
"Unsupported data type %d for objid=0x%" PRIx32
"\n",
741#ifdef CONFIG_DAWN_PROTO_NXSCOPE_SAMPLE_THREAD
743 iochan.data =
nullptr;
745 delete iochan.setData;
746 iochan.setData =
nullptr;
752#ifdef CONFIG_DAWN_IO_NOTIFY
753 if (iochan.stream && iochan.io->isNotify() ==
false)
757 DAWNWARN(
"%s-only nxscope channel (no notify) for objid=0x%" PRIx32
"\n",
758 iochan.io->isWrite() ?
"set" :
"get",
760 iochan.stream =
false;
764#ifdef CONFIG_DAWN_PROTO_NXSCOPE_SAMPLE_THREAD
765 if (iochan.stream && iochan.io->isSeekable())
767 if (iochan.io->isWrite())
769 DAWNINFO(
"set-only nxscope channel (seekable) for "
770 "objid=0x%" PRIx32
"\n",
772 iochan.stream =
false;
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;
787 iochan.data = iochan.io->ddata_alloc(1);
788 if (iochan.data ==
nullptr)
790 DAWNERR(
"failed to allocate sample buffer for objid=0x%" PRIx32
"\n",
792 delete iochan.setData;
793 iochan.setData =
nullptr;
802 u.s.dtype = getChannelDtype(*io);
808 nxscope_chan_init(&nxs, chanid,
const_cast<char *
>(name), u.u8, iochan.dim, 0);
809 iochan.chan = chanid;
811 hasStreamChannels =
true;
818 vio.push_back(iochan);
822 DAWNERR(
"IO not found for objid=0x%" PRIx32
"\n", alloc->objid.v);
830 userTxLen = nxsProto.hdrlen + NXSCOPE_USER_DATA_HDR + getmax + nxsProto.footlen;
831 userTxBuf =
new (std::nothrow) uint8_t[userTxLen];
832 if (userTxBuf ==
nullptr)
841#ifdef CONFIG_DAWN_IO_NOTIFY
842template<u
int8_t Type>
843int CProtoNxscope::putBatch(
struct nxscope_s *nxs, uint8_t chan,
io_ddata_t *data, uint8_t dim)
847 return nxscope_put_samples(
852#ifdef CONFIG_DAWN_PROTO_NXSCOPE_SAMPLE_THREAD
853template<
typename T, CProtoNxscope::nxscope_put_t<T> Put>
854int CProtoNxscope::sampleTyped(
CProtoNxscope *obj, SProtoNxscopeIochan *iochan)
858 if (!iochan || !iochan->data)
863 ret = iochan->io->getData(*iochan->data, 1);
866 ret = Put(&obj->nxs, iochan->chan,
static_cast<T *
>(iochan->data->getDataPtr()), iochan->dim);
873template<
typename T, CProtoNxscope::nxscope_put_t<T> Put, u
int8_t Type>
874void CProtoNxscope::bindTyped(SProtoNxscopeIochan &iochan)
876#ifdef CONFIG_DAWN_IO_NOTIFY
877 iochan.put = &putBatch<Type>;
879#ifdef CONFIG_DAWN_PROTO_NXSCOPE_SAMPLE_THREAD
880 iochan.sample = &sampleTyped<T, Put>;
884int CProtoNxscope::bindChannelCallbacks(SProtoNxscopeIochan &iochan, uint8_t dtype)
888#ifdef CONFIG_DAWN_DTYPE_UINT8
891 bindTyped<uint8_t, nxscope_put_vuint8, NXSCOPE_TYPE_UINT8>(iochan);
896#ifdef CONFIG_DAWN_DTYPE_INT8
899 bindTyped<int8_t, nxscope_put_vint8, NXSCOPE_TYPE_INT8>(iochan);
904#ifdef CONFIG_DAWN_DTYPE_UINT16
907 bindTyped<uint16_t, nxscope_put_vuint16, NXSCOPE_TYPE_UINT16>(iochan);
912#ifdef CONFIG_DAWN_DTYPE_INT16
915 bindTyped<int16_t, nxscope_put_vint16, NXSCOPE_TYPE_INT16>(iochan);
920#ifdef CONFIG_DAWN_DTYPE_INT32
923 bindTyped<int32_t, nxscope_put_vint32, NXSCOPE_TYPE_INT32>(iochan);
928#ifdef CONFIG_DAWN_DTYPE_UINT32
931 bindTyped<uint32_t, nxscope_put_vuint32, NXSCOPE_TYPE_UINT32>(iochan);
936#ifdef CONFIG_DAWN_DTYPE_UINT64
939 bindTyped<uint64_t, nxscope_put_vuint64, NXSCOPE_TYPE_UINT64>(iochan);
944#ifdef CONFIG_DAWN_DTYPE_FLOAT
947 bindTyped<float, nxscope_put_vfloat, NXSCOPE_TYPE_FLOAT>(iochan);
961#ifdef CONFIG_DAWN_PROTO_NXSCOPE_SAMPLE_THREAD
962void CProtoNxscope::getAndPut(SProtoNxscopeIochan *iochan)
966 if (!iochan || !iochan->sample)
971 std::lock_guard<std::recursive_mutex> lock(streamLock);
973 ret = iochan->sample(
this, iochan);
974 if (ret < 0 && ret != -EAGAIN)
976 DAWNERR(
"sampling failed for chan=%d ret=%d\n", iochan->chan, ret);
981void CProtoNxscope::threadRecv()
990 if (hasStreamChannels)
992#ifdef CONFIG_DAWN_IO_NOTIFY
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;
1005 sem_clockwait(&streamSem, CLOCK_MONOTONIC, &ts);
1010 while (sem_trywait(&streamSem) == 0)
1017 std::lock_guard<std::recursive_mutex> lock(streamLock);
1019 ret = nxscope_stream(&nxs);
1022 DAWNERR(
"ERROR: nxscope_stream failed %d\n", ret);
1031 std::lock_guard<std::recursive_mutex> lock(streamLock);
1033 ret = nxscope_recv(&nxs);
1038 DAWNERR(
"ERROR: nxscope_recv failed %d\n", ret);
1045 usleep(CONFIG_DAWN_PROTO_NXSCOPE_RECV_INTERVAL);
1048 while (!threadRecvMember.shouldQuit());
1052 threadRecvMember.markThreadFinished();
1055#ifdef CONFIG_DAWN_PROTO_NXSCOPE_SAMPLE_THREAD
1056void CProtoNxscope::threadSample()
1062 for (
auto &io : vio)
1071 usleep(NXSCOPE_SAMPLE_INTERVAL);
1073 while (!threadSampleMember.shouldQuit());
1077 threadSampleMember.markThreadFinished();
1082CProtoNxscope::~CProtoNxscope()
1084 for (
auto &io : vio)
1086#ifdef CONFIG_DAWN_PROTO_NXSCOPE_SAMPLE_THREAD
1101 userTxBuf =
nullptr;
1103#ifdef CONFIG_DAWN_IO_NOTIFY
1104 sem_destroy(&streamSem);
1109 nxscope_deinit(&nxs);
1128 return nxscopeChannelsCreate();
1148#ifdef CONFIG_DAWN_IO_NOTIFY
1152 for (
auto &io : vio)
1158 ret = io.io->setNotifier(ioNotifierCb, 0, (
void *)&io);
1161 DAWNERR(
"set notifier failed for objId = 0x%" PRIx32
" (%d)\n", io.io->getIdV(), ret);
1175 threadRecvMember.setThreadFunc([
this]() { threadRecv(); });
1176 ret = threadRecvMember.threadStart();
1182#ifdef CONFIG_DAWN_PROTO_NXSCOPE_SAMPLE_THREAD
1185 threadSampleMember.setThreadFunc([
this]() { threadSample(); });
1186 ret = threadSampleMember.threadStart();
1203 threadRecvMember.threadStop();
1205#ifdef CONFIG_DAWN_PROTO_NXSCOPE_SAMPLE_THREAD
1208 threadSampleMember.threadStop();
1220#ifdef CONFIG_DAWN_PROTO_NXSCOPE_SAMPLE_THREAD
1221 return !(threadRecvMember.isStopped() & threadSampleMember.isStopped());
1223 return !threadRecvMember.isStopped();
CIOCommon * getIO(SObjectId::ObjectId id)
Get an I/O object by ID.
void setObjectMapItem(SObjectId::ObjectId id, CObject *obj)
Set an item in the object map.
Base class for all I/O objects.
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).
int getData(IODataCmn &data, size_t len, size_t offset=0)
Get data from I/O (public interface with stats tracking).
virtual bool isSeekable() const
Check if IO supports partial (seekable) access.
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.
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.
Real-time data visualization protocol (base class).
int doStop()
Stop implementation hook.
bool hasThread() const
Check if a background thread is active.
struct nxscope_intf_s nxsIntf
NXScope interface structure (from logging library).
int configure()
Configure object from descriptor data.
int doStart()
Start implementation hook.
int init()
One-time initialize object after bindings are resolved.
int deinit()
De-initialize object.
Out-of-tree user-extension hooks for Dawn.
@ DTYPE_FLOAT
IEEE 754 single-precision floating point (32-bit).
@ DTYPE_INT32
Signed 32-bit integer (-2147483648 to 2147483647).
@ DTYPE_UINT8
Unsigned 8-bit integer (0 to 255).
@ DTYPE_INT16
Signed 16-bit integer (-32768 to 32767).
@ DTYPE_INT8
Signed 8-bit integer (-128 to 127).
@ DTYPE_UINT64
Unsigned 64-bit integer.
@ DTYPE_UINT16
Unsigned 16-bit integer (0 to 65535).
@ DTYPE_UB16
Unsigned 16.16 fixed-point (32-bit).
@ DTYPE_CHAR
Character/string type (null-terminated, 4-byte aligned).
@ DTYPE_UINT32
Unsigned 32-bit integer (0 to 4294967295).
@ DTYPE_B16
Signed 16.16 fixed-point (32-bit).
uint32_t ObjectId
ObjectID type - single 32-bit value.
Heap-allocated dynamic I/O data buffer.
size_t getDataSize()
Get data size in bytes.
void * getDataPtr(size_t batch=0)
Get pointer to data only (skips timestamp if present).
size_t getBatch()
Get number of batches.