w11 - cpp 0.794
Backend server for Rlink and w11
Loading...
Searching...
No Matches
RlinkConnect.hpp
Go to the documentation of this file.
1// $Id: RlinkConnect.hpp 1198 2019-07-27 19:08:31Z mueller $
2// SPDX-License-Identifier: GPL-3.0-or-later
3// Copyright 2011-2019 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
4//
5// Revision History:
6// Date Rev Version Comment
7// 2019-07-27 1198 2.8.5 add Nak handling
8// 2019-06-07 1160 2.8.4 *Stats() not longer const
9// 2018-12-23 1091 2.8.3 add BadPort()
10// 2018-12-17 1085 2.8.2 use std::recursive_mutex instead of boost
11// 2018-12-16 1084 2.8.1 use =delete for noncopyable instead of boost
12// 2018-12-08 1079 2.8 add HasPort(); return ref for Port()
13// 2018-12-07 1078 2.7.1 use std::shared_ptr instead of boost
14// 2018-12-01 1076 2.7 use unique_ptr instead of scoped_ptr
15// 2017-04-22 883 2.6.3 add rbus monitor probe, add HasRbmon()
16// 2017-04-09 871 2.6.2 LogFileName(): returns now const std::string&
17// 2017-04-07 868 2.6.1 Dump(): add detail arg
18// 2017-02-20 854 2.6 use Rtime, drop TimeOfDayAsDouble
19// 2016-04-02 758 2.5 add USR_ACCESS register support (RLUA0/RLUA1)
20// 2016-03-20 748 2.4 add fTimeout,(Set)Timeout();
21// 2015-04-12 666 2.3 add LinkInit,LinkInitDone; transfer xon
22// 2015-04-02 661 2.2 expect logic: stat expect in Command, invert mask
23// 2015-01-06 631 2.1 full rlink v4 implementation
24// 2014-12-25 621 2.0.2 Reorganize packet send/revd stats
25// 2014-12-20 616 2.0.1 add BlockDone expect checks
26// 2014-12-10 611 2.0 re-organize for rlink v4
27// 2013-04-21 509 1.3.3 add SndAttn() method
28// 2013-03-05 495 1.3.2 add Exec() without emsg (will send emsg to LogFile)
29// 2013-03-01 493 1.3.1 add Server(Active..|SignalAttn)() methods
30// 2013-02-23 492 1.3 use scoped_ptr for Port; Close allways allowed
31// use RlinkContext, add Context(), Exec(..., cntx)
32// 2013-02-22 491 1.2 use new RlogFile/RlogMsg interfaces
33// 2013-02-03 481 1.1.3 add SetServer(),Server()
34// 2013-01-13 474 1.1.2 add PollAttn() method
35// 2011-11-28 434 1.1.1 struct LogOpts: use uint32_t for lp64 compatibility
36// 2011-04-24 380 1.1 use boost::noncopyable (instead of private dcl's);
37// use boost::(mutex&lock), implement Lockable IF
38// 2011-04-22 379 1.0.1 add Lock(), Unlock()
39// 2011-04-02 375 1.0 Initial version
40// 2011-01-15 356 0.1 First draft
41// ---------------------------------------------------------------------------
42
47#ifndef included_Retro_RlinkConnect
48#define included_Retro_RlinkConnect 1
49
50#include <cstdint>
51#include <string>
52#include <vector>
53#include <memory>
54#include <ostream>
55#include <mutex>
56
57#include "librtools/RerrMsg.hpp"
58#include "librtools/Rtime.hpp"
59#include "librtools/Rstats.hpp"
62
63#include "RlinkPort.hpp"
64#include "RlinkCommandList.hpp"
65#include "RlinkPacketBufSnd.hpp"
66#include "RlinkPacketBufRcv.hpp"
67#include "RlinkAddrMap.hpp"
68#include "RlinkContext.hpp"
69
70#include "librtools/Rbits.hpp"
71
72namespace Retro {
73
74 class RlinkServer; // forw decl to avoid circular incl
75
76 class RlinkConnect : public Rbits {
77 public:
78
81
82 RlinkConnect(const RlinkConnect&) = delete; // noncopyable
83 RlinkConnect& operator=(const RlinkConnect&) = delete; // noncopyable
84
85 bool Open(const std::string& name, RerrMsg& emsg);
86 void Close();
87 bool IsOpen() const;
88 bool HasPort() const;
89 RlinkPort& Port();
90 const RlinkPort& Port() const;
91
92 bool LinkInit(RerrMsg& emsg);
93 bool LinkInitDone() const;
94
96
97 void SetServer(RlinkServer* pserv);
98 RlinkServer* Server() const;
99 bool ServerActive() const;
100 bool ServerActiveInside() const;
101 bool ServerActiveOutside() const;
102
103 // provide Lockable interface
104 void lock();
105 bool try_lock();
106 void unlock();
107
108 bool Exec(RlinkCommandList& clist, RerrMsg& emsg);
109 bool Exec(RlinkCommandList& clist, RlinkContext& cntx,
110 RerrMsg& emsg);
111 void Exec(RlinkCommandList& clist);
112 void Exec(RlinkCommandList& clist, RlinkContext& cntx);
113
114 int WaitAttn(const Rtime& timeout, Rtime& twait, uint16_t& apat,
115 RerrMsg& emsg);
116 bool SndOob(uint16_t addr, uint16_t data, RerrMsg& emsg);
117 bool SndAttn(RerrMsg& emsg);
118
119 uint32_t SysId() const;
120 uint32_t UsrAcc() const;
121 size_t RbufSize() const;
122 size_t BlockSizeMax() const;
123 size_t BlockSizePrudent() const;
124 bool HasRbmon() const;
125
126 bool AddrMapInsert(const std::string& name, uint16_t addr);
127 bool AddrMapErase(const std::string& name);
128 bool AddrMapErase(uint16_t addr);
129 void AddrMapClear();
130 const RlinkAddrMap& AddrMap() const;
131
132 Rstats& Stats();
133 Rstats& SndStats();
134 Rstats& RcvStats();
135
136 void SetLogBaseAddr(uint32_t base);
137 void SetLogBaseData(uint32_t base);
138 void SetLogBaseStat(uint32_t base);
139 void SetPrintLevel(uint32_t lvl);
140 void SetDumpLevel(uint32_t lvl);
141 void SetTraceLevel(uint32_t lvl);
142 void SetTimeout(const Rtime& timeout);
143
144 uint32_t LogBaseAddr() const;
145 uint32_t LogBaseData() const;
146 uint32_t LogBaseStat() const;
147 uint32_t PrintLevel() const;
148 uint32_t DumpLevel() const;
149 uint32_t TraceLevel() const;
150 const Rtime& Timeout() const;
151
152 bool LogOpen(const std::string& name, RerrMsg& emsg);
153 void LogUseStream(std::ostream* pstr,
154 const std::string& name = "");
155 RlogFile& LogFile() const;
156 const std::shared_ptr<RlogFile>& LogFileSPtr() const;
157
158 void SetLogFileName(const std::string& name);
159 const std::string& LogFileName() const;
160
161 void Print(std::ostream& os) const;
162 void Dump(std::ostream& os, int ind=0, const char* text=0,
163 int detail=0) const;
164
166
167 // some constants (also defined in cpp)
168 static const uint16_t kRbaddr_RLCNTL = 0xffff;
169 static const uint16_t kRbaddr_RLSTAT = 0xfffe;
170 static const uint16_t kRbaddr_RLID1 = 0xfffd;
171 static const uint16_t kRbaddr_RLID0 = 0xfffc;
172 static const uint16_t kRbaddr_RLUA1 = 0xfffb;
173 static const uint16_t kRbaddr_RLUA0 = 0xfffa;
174 static const uint16_t kRbaddr_RMBASE = 0xffe8;
175
176 static const uint16_t kRLCNTL_M_AnEna = kWBit15;
177 static const uint16_t kRLCNTL_M_AtoEna= kWBit14;
178 static const uint16_t kRLCNTL_M_AtoVal= 0x00ff;
179
180 static const uint16_t kRLSTAT_V_LCmd = 8;
181 static const uint16_t kRLSTAT_B_LCmd = 0x00ff;
182 static const uint16_t kRLSTAT_M_BAbo = kWBit07;
183 static const uint16_t kRLSTAT_M_RBSize= 0x0007;
184
185 static const uint16_t kSBCNTL_V_RLMON = 15;
186 static const uint16_t kSBCNTL_V_RLBMON= 14;
187 static const uint16_t kSBCNTL_V_RBMON = 13;
188
189 // space beyond data for rblk = 8 :cmd(1) cnt(2) dcnt(2) stat(1) crc(2)
190 // and wblk = 3 :cmd(1) cnt(2)
191 static const uint16_t kRbufBlkDelta=16;
192 // 512 byte are enough space for a prudent amount of non-blk commands
193 static const uint16_t kRbufPrudentDelta=512;
194
195 // statistics counter indices
196 enum stats {
223 };
224
225 protected:
226 bool ExecPart(RlinkCommandList& clist, size_t ibeg, size_t iend,
227 RerrMsg& emsg);
228
229 void EncodeRequest(RlinkCommandList& clist, size_t ibeg,
230 size_t iend);
231 int DecodeResponse(RlinkCommandList& clist, size_t ibeg,
232 size_t iend);
233 bool DecodeAttnNotify(uint16_t& apat);
234 bool ReadResponse(const Rtime& timeout, RerrMsg& emsg);
235 void AcceptResponse();
237 void ProcessAttnNotify();
238 [[noreturn]] void BadPort(const char* meth);
239
240 protected:
245 uint8_t fSeqNumber[8];
251 uint32_t fLogBaseAddr;
252 uint32_t fLogBaseData;
253 uint32_t fLogBaseStat;
254 uint32_t fPrintLevel;
255 uint32_t fDumpLevel;
256 uint32_t fTraceLevel;
258 std::shared_ptr<RlogFile> fspLog;
259 std::recursive_mutex fConnectMutex;
260 uint16_t fAttnNotiPatt;
262 uint32_t fSysId;
263 uint32_t fUsrAcc;
264 size_t fRbufSize;
266 };
267
268} // end namespace Retro
269
270#include "RlinkConnect.ipp"
271
272#endif
static const uint16_t kWBit14
Definition: Rbits.hpp:46
static const uint16_t kWBit15
Definition: Rbits.hpp:47
static const uint16_t kWBit07
Definition: Rbits.hpp:39
FIXME_docs.
Definition: RerrMsg.hpp:25
static const uint16_t kRLCNTL_M_AnEna
RLCNTL: an enable.
std::recursive_mutex fConnectMutex
mutex to lock whole connect
static const uint16_t kRbaddr_RLCNTL
rlink core reg RLCNTL
void SetTimeout(const Rtime &timeout)
FIXME_docs.
void AddrMapClear()
FIXME_docs.
int WaitAttn(const Rtime &timeout, Rtime &twait, uint16_t &apat, RerrMsg &emsg)
Wait for an attention notify.
void SetLogFileName(const std::string &name)
FIXME_docs.
uint32_t LogBaseData() const
FIXME_docs.
static const uint16_t kRLSTAT_B_LCmd
RLSTAT: lcmd.
bool LogOpen(const std::string &name, RerrMsg &emsg)
FIXME_docs.
void Close()
FIXME_docs.
bool SndAttn(RerrMsg &emsg)
FIXME_docs.
static const uint16_t kRbaddr_RLID1
rlink core reg RLID1
void SetLogBaseAddr(uint32_t base)
FIXME_docs.
uint32_t LogBaseAddr() const
FIXME_docs.
void HandleUnsolicitedData()
Handle unsolicited data from port.
uint16_t fAttnNotiPatt
attn notifier pattern
bool AddrMapInsert(const std::string &name, uint16_t addr)
FIXME_docs.
bool AddrMapErase(const std::string &name)
FIXME_docs.
RlinkPacketBufSnd fSndPkt
send packet buffer
static const uint16_t kSBCNTL_V_RLMON
SBCNTL: rlmon enable bit.
void unlock()
FIXME_docs.
uint32_t SysId() const
FIXME_docs.
uint32_t fTraceLevel
trace 0=off,1=buf,2=char
uint32_t fDumpLevel
dump 0=off,1=err,2=chk,3=all
void SetLogBaseStat(uint32_t base)
FIXME_docs.
bool ServerActiveInside() const
Indicates whether server is active and caller is inside server thread.
static const uint16_t kRbufBlkDelta
rbuf needed for rblk or wblk
RlinkPort::port_uptr_t fupPort
uptr to port
bool Exec(RlinkCommandList &clist, RerrMsg &emsg)
FIXME_docs.
uint32_t fLogBaseData
log: base for data
Rtime fTimeout
response timeout
bool LinkInit(RerrMsg &emsg)
FIXME_docs.
@ kStatNInit
init commands
@ kStatNErrCrc
decode: crc mismatch
@ kStatNWblk
wblk commands
@ kStatNExecPart
ExecPart() calls.
@ kStatNErrMiss
decode: missing data
@ kStatNSndOob
SndOob() calls.
@ kStatNErrLen
decode: length mismatch
@ kStatNAttn
attn commands
@ kStatNChkData
expect data failed
@ kStatNExpStat
expect for stat explicit
@ kStatNNoExpStat
no expect for stat
@ kStatNExpDone
expect for done defined
@ kStatNErrCmd
decode: command mismatch
@ kStatNErrNak
decode: nak seen
@ kStatNExpData
expect for data defined
@ kStatNRblkWord
words rcvd with rblk
@ kStatNChkDone
expect done failed
@ kStatNCmd
commands executed
@ kStatNWreg
wreg commands
@ kStatNExec
Exec() calls.
@ kStatNLabo
labo commands
@ kStatNRreg
rreg commands
@ kStatNChkStat
expect stat failed
@ kStatNRblk
rblk commands
@ kStatNWblkWord
words send with wblk
uint32_t fUsrAcc
USR_ACCESS of connected device.
bool ServerActive() const
Indicates whether server is active.
Rstats & RcvStats()
FIXME_docs.
uint32_t PrintLevel() const
FIXME_docs.
void lock()
FIXME_docs.
static const uint16_t kRLCNTL_M_AtoVal
RLCNTL: ato value.
size_t RbufSize() const
FIXME_docs.
bool fLinkInitDeferred
noinit attr seen on Open
void EncodeRequest(RlinkCommandList &clist, size_t ibeg, size_t iend)
FIXME_docs.
const Rtime & Timeout() const
FIXME_docs.
~RlinkConnect()
Destructor.
void ProcessUnsolicitedData()
Process data still pending in the input buffer.
static const uint16_t kRLSTAT_V_LCmd
RLSTAT: lcmd.
void SetDumpLevel(uint32_t lvl)
FIXME_docs.
void LogUseStream(std::ostream *pstr, const std::string &name="")
FIXME_docs.
uint8_t fSeqNumber[8]
command sequence number
RlogFile & LogFile() const
FIXME_docs.
bool try_lock()
FIXME_docs.
static const uint16_t kRLSTAT_M_BAbo
RLSTAT: babo.
uint32_t LogBaseStat() const
FIXME_docs.
static const uint16_t kRbufPrudentDelta
Rbuf space reserve.
void Print(std::ostream &os) const
FIXME_docs.
Rtime fTsLastAttnNoti
time stamp last attn notify
RlinkContext & Context()
FIXME_docs.
static const uint16_t kSBCNTL_V_RLBMON
SBCNTL: rlbmon enable bit.
RlinkConnect()
Default constructor.
bool SndOob(uint16_t addr, uint16_t data, RerrMsg &emsg)
FIXME_docs.
uint32_t fSysId
SYSID of connected device.
void ProcessAttnNotify()
Process attention notify packets.
static const uint16_t kRLSTAT_M_RBSize
RLSTAT: rbuf size.
bool ReadResponse(const Rtime &timeout, RerrMsg &emsg)
Read data from port until complete response packet seen.
void BadPort(const char *meth)
Port not connected or not open abort.
RlinkContext fContext
default context
static const uint16_t kSBCNTL_V_RBMON
SBCNTL: rbmon enable bit.
static const uint16_t kRbaddr_RLUA1
rlink opt. reg RLUA1
size_t BlockSizeMax() const
FIXME_docs.
bool IsOpen() const
FIXME_docs.
uint32_t DumpLevel() const
FIXME_docs.
static const uint16_t kRbaddr_RLID0
rlink core reg RLID0
RlinkPacketBufRcv fRcvPkt
receive packet buffer
uint32_t fLogBaseAddr
log: base for addr
RlinkServer * Server() const
FIXME_docs.
RlinkConnect(const RlinkConnect &)=delete
const std::shared_ptr< RlogFile > & LogFileSPtr() const
FIXME_docs.
static const uint16_t kRbaddr_RMBASE
rlink opt. rbd_rbmon
void SetLogBaseData(uint32_t base)
FIXME_docs.
bool ServerActiveOutside() const
Indicates whether server is active and caller is outside server thread.
bool ExecPart(RlinkCommandList &clist, size_t ibeg, size_t iend, RerrMsg &emsg)
FIXME_docs.
static const uint16_t kRbaddr_RLSTAT
rlink core reg RLSTAT
uint32_t TraceLevel() const
FIXME_docs.
RlinkConnect & operator=(const RlinkConnect &)=delete
Rstats & Stats()
FIXME_docs.
RlinkPort & Port()
FIXME_docs.
void SetPrintLevel(uint32_t lvl)
FIXME_docs.
uint32_t fPrintLevel
print 0=off,1=err,2=chk,3=all
Rstats fStats
statistics
bool HasPort() const
FIXME_docs.
void Dump(std::ostream &os, int ind=0, const char *text=0, int detail=0) const
FIXME_docs.
size_t BlockSizePrudent() const
FIXME_docs.
RlinkAddrMap fAddrMap
name<->address mapping
bool fHasRbmon
has rbd_rbmon (rbus monitor)
static const uint16_t kRLCNTL_M_AtoEna
RLCNTL: ato enable.
uint32_t fLogBaseStat
log: base for stat
bool HasRbmon() const
FIXME_docs.
uint32_t UsrAcc() const
FIXME_docs.
RlinkServer * fpServ
ptr to server (optional)
size_t fRbufSize
Rbuf size (in bytes)
Rstats & SndStats()
FIXME_docs.
void SetServer(RlinkServer *pserv)
FIXME_docs.
void AcceptResponse()
Accept response packet received with ReadResponse().
const RlinkAddrMap & AddrMap() const
FIXME_docs.
static const uint16_t kRbaddr_RLUA0
rlink opt. reg RLUA0
bool fLinkInitDone
LinkInit done.
void SetTraceLevel(uint32_t lvl)
FIXME_docs.
int DecodeResponse(RlinkCommandList &clist, size_t ibeg, size_t iend)
FIXME_docs.
bool LinkInitDone() const
FIXME_docs.
bool Open(const std::string &name, RerrMsg &emsg)
FIXME_docs.
std::shared_ptr< RlogFile > fspLog
log file ptr
bool DecodeAttnNotify(uint16_t &apat)
Decodes an attention notify packet.
const std::string & LogFileName() const
FIXME_docs.
FIXME_docs.
Definition: RlinkPort.hpp:45
std::unique_ptr< RlinkPort > port_uptr_t
Definition: RlinkPort.hpp:47
FIXME_docs.
Definition: RlogFile.hpp:34
FIXME_docs.
Definition: Rstats.hpp:28
FIXME_docs.
Definition: Rtime.hpp:25
Declaration of class ReventLoop.
Definition: ReventLoop.cpp:47