3 namespace remote
{ namespace protocols
{ namespace motecontrol
{
5 MoteMsg::MoteMsg(uint8_t*& buffer
, uint32_t& buflen
)
7 buffer
= this->read(buffer
,buflen
);
11 MoteMsg::MoteMsg(MsgRequest
& message
)
12 : type(MOTEMSGTYPE_REQUEST
),
17 MoteMsg::MoteMsg(MsgConfirm
& message
)
18 : type(MOTEMSGTYPE_CONFIRM
),
23 MoteMsg::MoteMsg(MsgPayload
& message
)
24 : type(MOTEMSGTYPE_DATA
),
31 if (deleteMsg
) {delete this->message
;}
35 uint32_t MoteMsg::getLength()
37 return sizeof(protocolVersion
) + sizeof(type
) + message
->getLength();
40 uint8_t* MoteMsg::write(uint8_t* buffer
, uint32_t& buflen
)
42 buffer
= writevalue(protocolVersion
,buffer
,buflen
);
43 buffer
= writevalue(type
,buffer
,buflen
);
44 buffer
= message
->write(buffer
,buflen
);
48 uint8_t* MoteMsg::read(uint8_t* buffer
, uint32_t& buflen
)
50 buffer
= readvalue(protocolVersion
,buffer
,buflen
);
51 // TODO: check protocolVersion
52 buffer
= readvalue(type
,buffer
,buflen
);
55 case MOTEMSGTYPE_REQUEST
:
56 message
= new MsgRequest(buffer
,buflen
);
58 case MOTEMSGTYPE_CONFIRM
:
59 message
= new MsgConfirm(buffer
,buflen
);
61 case MOTEMSGTYPE_DATA
:
62 message
= new MsgPayload(buffer
,buflen
);
68 void MoteMsg::print(FILE* s
)
70 fprintf(s
,"MESSAGE MoteMsg\n");
71 fprintf(s
,"protocolVersion: %u\n",protocolVersion
);
75 case MOTEMSGTYPE_REQUEST
:
76 fprintf(s
,"MOTEMSGTYPE_REQUEST\n");
78 case MOTEMSGTYPE_CONFIRM
:
79 fprintf(s
,"MOTEMSGTYPE_CONFIRM\n");
81 case MOTEMSGTYPE_DATA
:
82 fprintf(s
,"MOTEMSGTYPE_DATA\n");
85 fprintf(s
,"Invalid type!\n");
90 uint32_t MoteMsg::getProtocolVersion()
92 return protocolVersion
;
95 uint8_t MoteMsg::getType()
100 MsgRequest
& MoteMsg::getRequest()
102 if (type
!= MOTEMSGTYPE_REQUEST
)
103 __THROW__ ("Cannot getRequest when type is not MOTEMSGTYPE_REQUEST!");
104 return *((MsgRequest
*)message
);
107 MsgConfirm
& MoteMsg::getConfirm()
109 if (type
!= MOTEMSGTYPE_CONFIRM
)
110 __THROW__ ("Cannot getConfirm when type is not MOTEMSGTYPE_CONFIRM!");
111 return *((MsgConfirm
*)message
);
114 MsgPayload
& MoteMsg::getData()
116 if (type
!= MOTEMSGTYPE_DATA
)
117 __THROW__ ("Cannot getData when type is not MOTEMSGTYPE_DATA!");
118 return *((MsgPayload
*)message
);