Files CCore/inc/net/PTPConBase.h CCore/src/net/PTPConBase.cpp
Files CCore/inc/net/PTPConDevice.h CCore/src/net/PTPConDevice.cpp
PTP Console is a PTP console service with 4 functions. The service id is 3. Function ids are 1-4. Server may support single or multiple consoles. Each console has an id.
const ServiceIdType ServiceId = 3 ; const FunctionIdType FunctionId_Open = 1 ; const FunctionIdType FunctionId_Read = 2 ; const FunctionIdType FunctionId_Write = 3 ; const FunctionIdType FunctionId_Close = 4 ;
Protocol types and constants:
struct Name { LenType len : len<=MaxNameLen ; uint8 name[len]; }; struct ConId { uint32 slot; uint64 number; uint64 clock; }; const unsigned MaxNameLen = 128 ; const unsigned DeltaReadLen = 20 ; const unsigned MaxReadDataLen = 1420 ; // MaxInfoLen-DeltaReadLen const unsigned DeltaWriteLen = 36 ; const unsigned MaxWriteDataLen = 1404 ; // MaxInfoLen-DeltaWriteLen
Function Open has the following input/output:
struct OpenInput { uint32 write_timeout_msec; uint32 read_timeout_msec; uint32 trigger_mask[8]; void setbit(uint8 ch) { trigger_mask[ch>>5]|=(uint32(1)<<(ch&31u)); } Name name; }; struct OpenOutput { ConId con_id; };
This function opens a console with the given name and parameters. The console id is returned.
write_timeout_msec is a time in milliseconds. The write character frame print may be delayed on this timeout to catch a previously sent frames.
read_timeout_msec is a time in milliseconds. The read operation completion may be delayed on this timeout if there is no trigger character in the input stream.
trigger_mask is a 256-bit character mask. The masked character is a trigger character. I.e. the appearance of these characters in the input stream causes a read operation completion.
Function Read has the following input/output:
struct ReadInput { ConId con_id; uint32 number; LenType len; }; struct ReadOutput { uint32 number; LenType len : len<=MaxReadDataLen ; uint8 data[len]; };
This function reads the input character stream. The number is any number and sent back by the operation. len is a maximum read length. The operation completion is delayed until a trigger character appears in the input stream or the read timeout is expired.
Function Write has the following input/output:
struct WriteInput { ConId con_id; uint32 number; LenType len : len<=MaxWriteDataLen ; uint8 data[len]; }; struct WriteOutput { };
This function writes a character frame to the given console. The number must be incremented with each write. It starts from 0.
Function Close has the following input/output:
struct CloseInput { ConId con_id; }; struct CloseOutput { };
This function closes the given console.
The file txt/cpp/PTPCon.txt.cpp contains this service definition.
The file net/PTPConBase.h contains this service definition in C++ in the namespace PTPCon.
There is a client class ClientDevice to work with PTP console. The class is located in the namespace PTPCon.
class ClientDevice : public ObjBase
{
....
public:
// constructors
explicit ClientDevice(StrLen ptp_dev_name);
virtual ~ClientDevice();
// write
bool getWriteErrorFlag() const;
PacketFormat getWriteFormat() const;
void write(Packet<uint8> packet);
// read
struct InputProc
{
virtual void input(PacketBuf &pbuf,PtrLen<const uint8> str)=0;
virtual void stop()=0;
};
void start_read(InputProc *proc);
void stop_read();
// open/close
class OpenClose : NoCopy
{
....
public:
OpenClose(ClientDevice &obj,StrLen name,const Cfg &cfg=DefaultValue());
~OpenClose();
};
};
The constructor argument is a PTP ClientDevice object name.
To perform Open and Close operations the inner class OpenClose is provided. Its constructor does Open and destructor Close. name is the console name and cfg is the configuration parameters (see below).
To read the method start_read() is called. The argument is a pointer to the abstract interface. The method input() is called when input data arrives. The first argument refers the packet buffer, the second is the input string, it is stored in the buffer. The method stop() is called to signal about console input is shutdown by any reason. These methods are call in the "unknown task context".
stop_read() stops reading. After this method has returned, the input processor may be destroyed.
getWriteErrorFlag() returns true, if there were some write errors.
getWriteFormat() returns the packet format for the writing.
write() writes the string from the given packet.
The structure Cfg stores configuration properties for a PTP console. The field timeout is the timeout for Open and Close operations.
struct Cfg
{
uint32 write_timeout_msec;
uint32 read_timeout_msec;
TriggerMask trigger_mask;
MSec timeout;
// defaults
void setDefaultTimeouts()
{
write_timeout_msec=1'000; // 1 sec
read_timeout_msec=10'000; // 10 sec
}
// constructors
explicit Cfg(MSec timeout_=DefaultTimeout)
: trigger_mask(TriggerDefault),
timeout(timeout_)
{
setDefaultTimeouts();
}
explicit Cfg(const char *trigger,MSec timeout_=DefaultTimeout)
: trigger_mask(trigger),
timeout(timeout_)
{
setDefaultTimeouts();
}
Cfg(Trigger trigger,MSec timeout_=DefaultTimeout)
: trigger_mask(trigger),
timeout(timeout_)
{
setDefaultTimeouts();
}
};
The structure TriggerMask is the character trigger mask.
enum Trigger { TriggerNone, TriggerDefault, // \r \n \t \b TriggerAll }; struct TriggerMask { uint32 mask[8]; // constructors TriggerMask(); explicit TriggerMask(const char *zstr); explicit TriggerMask(Trigger t); // methods void setNone(); void setDefault(); void setAll(); void set(uint8 ch); void set(const char *zstr); uint32 test(uint8 ch) const; };
TriggerMask can be initialize by default (which means the null trigger mask), or from the given zero-terminated string, or using the Trigger enumeration.
setNone() sets the mask to null.
setDefault() sets the mask to the default set.
setAll() sets the mask to the all one.
set(char ch) sets only the given character bit to one. Other bits remain unchanged.
set(const char *zstr) sets only the given character set bits to ones. Other bits remain unchanged.
test() can be used to test the particular character bit.