Issue 22: Convert remaining code to use the log module
[remote/remote-mci.git] / protocols / motecontrol / MoteMsg.cc
blobbe2260c26f8f2ecbbc37de020c6cd83b9c92b336
1 #include "MoteMsg.h"
3 namespace remote { namespace protocols { namespace motecontrol {
5 MoteMsg::MoteMsg(uint8_t*& buffer, uint32_t& buflen)
7 buffer = this->read(buffer,buflen);
8 deleteMsg = true;
11 MoteMsg::MoteMsg(MsgRequest& message)
12 : type(MOTEMSGTYPE_REQUEST),
13 message(&message),
14 deleteMsg(false)
17 MoteMsg::MoteMsg(MsgConfirm& message)
18 : type(MOTEMSGTYPE_CONFIRM),
19 message(&message),
20 deleteMsg(false)
23 MoteMsg::MoteMsg(MsgPayload& message)
24 : type(MOTEMSGTYPE_DATA),
25 message(&message),
26 deleteMsg(false)
29 MoteMsg::~MoteMsg()
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);
45 return buffer;
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);
53 switch (type)
55 case MOTEMSGTYPE_REQUEST:
56 message = new MsgRequest(buffer,buflen);
57 break;
58 case MOTEMSGTYPE_CONFIRM:
59 message = new MsgConfirm(buffer,buflen);
60 break;
61 case MOTEMSGTYPE_DATA:
62 message = new MsgPayload(buffer,buflen);
63 break;
65 return buffer;
68 void MoteMsg::print(FILE* s)
70 fprintf(s,"MESSAGE MoteMsg\n");
71 fprintf(s,"protocolVersion: %u\n",protocolVersion);
72 fprintf(s,"type: ");
73 switch (type)
75 case MOTEMSGTYPE_REQUEST:
76 fprintf(s,"MOTEMSGTYPE_REQUEST\n");
77 break;
78 case MOTEMSGTYPE_CONFIRM:
79 fprintf(s,"MOTEMSGTYPE_CONFIRM\n");
80 break;
81 case MOTEMSGTYPE_DATA:
82 fprintf(s,"MOTEMSGTYPE_DATA\n");
83 break;
84 default:
85 fprintf(s,"Invalid type!\n");
87 message->print(s);
90 uint32_t MoteMsg::getProtocolVersion()
92 return protocolVersion;
95 uint8_t MoteMsg::getType()
97 return type;
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);