Issue 22: Convert remaining code to use the log module
[remote/remote-mci.git] / protocols / client_server / MsgClientRequest.cc
blobe11b2a54590b2c2f8f19bded378937b9dad82689
1 #include "MsgClientRequest.h"
3 namespace remote { namespace protocols { namespace client_server {
5 MsgClientRequest::MsgClientRequest(uint8_t command)
6 : command(command), msgMoteIdList(), moteMsg()
10 MsgClientRequest::MsgClientRequest(uint8_t*& buffer, uint32_t& buflen) : msgMoteIdList(), moteMsg()
12 buffer = this->read(buffer,buflen);
15 uint32_t MsgClientRequest::getLength()
17 uint32_t length = sizeof(command);
18 length += msgMoteIdList.getLength();
19 switch (command)
21 case MSGCLIENTCOMMAND_MOTEMESSAGE:
22 length += moteMsg.getLength();
23 break;
24 case MSGCLIENTCOMMAND_GETMOTECONTROL:
25 case MSGCLIENTCOMMAND_DROPMOTECONTROL:
26 break;
27 default:
28 __THROW__ ("Invalid command");
29 break;
31 return length;
34 uint8_t* MsgClientRequest::write(uint8_t* buffer, uint32_t& buflen)
36 buffer = writevalue(command,buffer,buflen);
37 buffer = msgMoteIdList.write(buffer,buflen);
38 switch (command)
40 case MSGCLIENTCOMMAND_MOTEMESSAGE:
41 buffer = moteMsg.write(buffer,buflen);
42 break;
43 case MSGCLIENTCOMMAND_GETMOTECONTROL:
44 case MSGCLIENTCOMMAND_DROPMOTECONTROL:
45 break;
46 default:
47 __THROW__ ("Invalid command");
48 break;
50 return buffer;
53 uint8_t* MsgClientRequest::read(uint8_t* buffer, uint32_t& buflen)
55 buffer = readvalue(command,buffer,buflen);
56 buffer = msgMoteIdList.read(buffer,buflen);
57 switch (command)
59 case MSGCLIENTCOMMAND_MOTEMESSAGE:
60 buffer = moteMsg.read(buffer,buflen);
61 break;
62 case MSGCLIENTCOMMAND_GETMOTECONTROL:
63 break;
64 case MSGCLIENTCOMMAND_DROPMOTECONTROL:
65 break;
66 default:
67 __THROW__ ("Invalid command");
68 break;
70 return buffer;
73 void MsgClientRequest::print(FILE* s)
75 fprintf(s,"MESSAGE MsgServerRequest\n");
76 fprintf(s,"command: ");
77 switch (command)
79 case MSGCLIENTCOMMAND_MOTEMESSAGE:
80 fprintf(s,"MSGSERVERCOMMAND_MOTEMESSAGE\n");
81 moteMsg.print(s);
82 break;
83 case MSGCLIENTCOMMAND_GETMOTECONTROL:
84 fprintf(s,"MSGSERVERCOMMAND_GETMOTECONTROL\n");
85 break;
86 case MSGCLIENTCOMMAND_DROPMOTECONTROL:
87 fprintf(s,"MSGSERVERCOMMAND_DROPMOTECONTROL\n");
88 break;
89 default:
90 __THROW__ ("Invalid command");
91 break;
93 msgMoteIdList.print(s);
96 uint8_t MsgClientRequest::getCommand()
98 return command;
101 MsgMoteIdList& MsgClientRequest::getMoteIdList()
103 if ( command != MSGCLIENTCOMMAND_GETMOTECONTROL
104 && command != MSGCLIENTCOMMAND_DROPMOTECONTROL
105 && command != MSGCLIENTCOMMAND_MOTEMESSAGE)
106 __THROW__ ("Cannot getMoteIdList when command is not set correctly.");
107 return msgMoteIdList;
110 MsgPayload& MsgClientRequest::getMoteMsg()
112 if (command != MSGCLIENTCOMMAND_MOTEMESSAGE)
113 __THROW__ ("Cannot get motemsg when type is not MSGSERVERCOMMAND_MOTEMESSAGE!");
114 return moteMsg;