//..............................................................................
//
// TutoProto structs & constants
//

enum TutoProtoCommand: uint8_t
{
	[
		getReplyDigest = getTutoProtoReplyDigest_GetVersion,
		representReply = representTutoProtoReply_GetVersion
		// command has no params
	]
	GetVersion,

	[
		getCommandDigest = getTutoProtoCommandDigest_Read,
		getReplyDigest = getTutoProtoReplyDigest_Read,
		representCommand = representTutoProtoCommand_Read,
		representReply = representTutoProtoReply_Read
	]
	Read,

	[
		getCommandDigest = getTutoProtoCommandDigest_Write,
		representCommand = representTutoProtoCommand_Write
	]
	Write,
}

bitflag enum TutoProtoFlags: uint8_t
{
	Reply,
	Error
}

// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

struct TutoProtoHdr
{
	[
		displayName = "STX",
		formatSpec = "0x%02x"
	]
	uint8_t m_stx;

	[
		displayName = "Command",
		displayType = typeof(TutoProtoCommand)
	]
	uint8_t m_command : 4;

	[
		displayName = "Flags",
		displayType = typeof(TutoProtoFlags)
	]
	uint8_t m_flags   : 4;

	[
		displayName = "ID",
		formatSpec = "0x%04x"
	]
	bigendian uint16_t m_id;

	[
		displayName = "Size"
	]
	bigendian uint16_t m_size;
}

// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

struct TutoProtoVersion
{
	[ displayName = "Major" ]
	uint8_t m_major;

	[ displayName = "Minor" ]
	uint8_t m_minor;

	[ displayName = "Patch" ]
	uint8_t m_patch;
}

struct TutoProtoRange
{
	[
		displayName = "Offset",
		formatSpec = "0x%04x"
	]

	bigendian uint32_t m_offset;

	[ displayName = "Length" ]
	bigendian uint32_t m_length;
}

struct TutoProtoError
{
	[
		displayName = "Errno",
		formatFunc = formatErrno
	]
	bigendian int32_t m_errno;
}

char const* formatErrno(void const* p)
{
	std.Error error;
	error.m_guid = std.g_errnoGuid;
	error.m_code = *(bigendian int const*)p;
	return error.m_description;
}

//..............................................................................
