3 /// Low level protocol packet builder class.
4 /// Has knowledge of specific protocol commands in order
5 /// to hide protocol details behind an API.
9 Copyright (C) 2005-2010, Net Direct Inc. (http://www.netdirect.ca/)
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 See the GNU General Public License in the COPYING file at the
21 root directory of this project for more details.
24 #ifndef __BARRY_PACKET_H__
25 #define __BARRY_PACKET_H__
32 namespace Barry
{ class Data
; }
36 // forward declarations
49 friend class SocketZero
;
53 Data
&m_send
, &m_receive
;
55 Data
& GetSend() { return m_send
; }
56 Data
& GetReceive() { return m_receive
; }
59 Packet(Data
&send
, Data
&receive
)
60 : m_send(send
), m_receive(receive
)
64 //////////////////////////////////
65 // common response analysis
67 unsigned int Command() const; // throws Error if receive isn't big enough
73 /// Provides an API for building and analyzing socket-0 protocol packets.
74 /// This class relies on 2 external objects: a send and receive Data buffer.
76 /// Note that the receive buffer may be modified
77 /// during a packet send, and this class provides API helpers
78 /// to analyze the results.
80 class ZeroPacket
: public Packet
85 ZeroPacket(Data
&send
, Data
&receive
);
88 //////////////////////////////////
91 //////////////////////////////////
94 void GetAttribute(unsigned int object
, unsigned int attribute
);
95 void Echo(uint64_t us_ticks
);
99 //////////////////////////////////
102 unsigned int ObjectID() const;
103 unsigned int AttributeID() const;
104 uint32_t ChallengeSeed() const;
105 unsigned int RemainingTries() const;
106 unsigned int SocketResponse() const;
107 unsigned char SocketSequence() const;
108 uint8_t CommandResponse() const;
115 /// Provides an API for building and analyzing raw DB protocol packets.
116 /// This class relies on 3 external objects: a Mode::Desktop object,
117 /// a send Data buffer, and a receive data buffer. Socket and
118 /// connection details are retrieved on a readonly basis from the
119 /// Mode::Desktop object, but both send and receive buffers can be
122 /// Note that the receive buffer may be modified
123 /// during a packet send, and this DBPacket class provides API helpers
124 /// to analyze the results.
126 class DBPacket
: public Packet
131 Mode::Desktop
&m_con
;
132 unsigned int m_last_dbop
; // last database operation
137 DBPacket(Mode::Desktop
&con
, Data
&send
, Data
&receive
);
140 //////////////////////////////////
143 //////////////////////////////////
146 // commands that correspond to the DB operation
147 // constants in protocol.h
148 void ClearDatabase(unsigned int dbId
);
150 void GetRecordStateTable(unsigned int dbId
);
151 void SetRecordFlags(unsigned int dbId
, unsigned int stateTableIndex
, uint8_t flag1
);
152 void DeleteRecordByIndex(unsigned int dbId
, unsigned int stateTableIndex
);
153 void GetRecordByIndex(unsigned int dbId
, unsigned int stateTableIndex
);
154 bool SetRecordByIndex(unsigned int dbId
, unsigned int stateTableIndex
, Builder
&build
, const IConverter
*ic
);
155 void GetRecords(unsigned int dbId
);
156 bool SetRecord(unsigned int dbId
, Builder
&build
, const IConverter
*ic
);
159 //////////////////////////////////
162 // DB command response functions
163 unsigned int ReturnCode() const; // throws FIXME if packet doesn't support it
164 unsigned int DBOperation() const; // throws Error on size trouble
166 bool Parse(Parser
&parser
, const std::string
&dbname
,
167 const IConverter
*ic
); // switches based on last m_send command
168 bool ParseMeta(DBData
&data
);
177 /// Provides an API for building and analyzing raw Javaloader protocol packets.
178 /// This class relies on 3 external objects:
179 /// a command send Data buffer (which can be fairly small), a data
180 /// or argument send Data buffer, and a receive data buffer. Socket and
181 /// connection details are retrieved on a readonly basis from the
182 /// Mode::JavaLoader object, but all buffers can be modified.
184 /// Note that the receive buffer may be modified
185 /// during a packet send, and this JLPacket class provides API helpers
186 /// to analyze the results.
188 class JLPacket
: public Packet
193 Data
&m_cmd
, &m_data
;
197 JLPacket(Data
&cmd
, Data
&send
, Data
&receive
);
200 //////////////////////////////////
203 bool HasData() const { return m_last_set_size
== 2; }
204 Data
& GetReceive() { return m_receive
; }
206 //////////////////////////////////
209 // commands that correspond to the operation
210 // constants in protocol.h
212 // returns 1 or 2 depending on whether cmd or cmd+send are available
213 int SimpleCmd(uint8_t cmd
, uint8_t unknown
= 0, uint16_t size
= 0);
214 int SimpleData(const void *data
, uint16_t size
);
215 int BigEndianData(uint16_t value
);
216 int BigEndianData(uint32_t value
);
218 int Hello() { return SimpleCmd(SB_COMMAND_JL_HELLO
); }
219 int Goodbye() { return SimpleCmd(SB_COMMAND_JL_GOODBYE
); }
221 int SetCodFilename(const std::string
&filename
);
222 int SetCodSize(off_t size
);
223 int SetTime(time_t when
);
225 int GetData() { return SimpleCmd(SB_COMMAND_JL_SEND_DATA
); }
226 int DeviceInfo() { return SimpleCmd(SB_COMMAND_JL_DEVICE_INFO
); }
227 int OsMetrics() { return SimpleCmd(SB_COMMAND_JL_OS_METRICS
); }
228 int BootromMetrics() { return SimpleCmd(SB_COMMAND_JL_BOOTROM_METRICS
); }
229 int GetDirectory() { return SimpleCmd(SB_COMMAND_JL_GET_DIRECTORY
); }
230 int GetSubDir(uint16_t id
);
231 int GetDirEntry(uint8_t entry_cmd
, uint16_t id
);
232 int Erase(uint16_t cmd
, uint16_t id
);
233 int GetEventlog() { return SimpleCmd(SB_COMMAND_JL_GET_LOG
); }
234 int GetEventlogEntry(uint16_t entry_num
);
235 int ClearEventlog() { return SimpleCmd(SB_COMMAND_JL_CLEAR_LOG
); }
236 int SaveModule(uint16_t id
);
237 int PutData(const void *data
, uint16_t size
);
238 int WipeApps() { return SimpleCmd(SB_COMMAND_JL_WIPE_APPS
); }
239 int WipeFs() { return SimpleCmd(SB_COMMAND_JL_WIPE_FS
); }
240 int LogStackTraces() { return SimpleCmd(SB_COMMAND_JL_LOG_STRACES
); }
241 int ResetToFactory() { return SimpleCmd(SB_COMMAND_JL_RESET_FACTORY
); }
243 //////////////////////////////////
252 /// Provides an API for building and analyzing raw JavaDebug protocol packets.
253 /// This class relies on 3 external objects:
254 /// a command send Data buffer (which can be fairly small), a data
255 /// or argument send Data buffer, and a receive data buffer. Socket and
256 /// connection details are retrieved on a readonly basis from the
257 /// Mode::JavaDebug object, but all buffers can be modified.
259 /// Note that the receive buffer may be modified
260 /// during a packet send, and this JVMPacket class provides API helpers
261 /// to analyze the results.
263 class JVMPacket
: public Packet
271 JVMPacket(Data
&cmd
, Data
&receive
);
274 //////////////////////////////////
277 Data
& GetReceive() { return m_receive
; }
279 //////////////////////////////////
282 // commands that correspond to the operation
283 // constants in protocol.h
285 // returns 1 or 2 depending on whether cmd or cmd+send are available
286 void SimpleCmd(uint8_t cmd
);
287 void ComplexCmd(uint8_t cmd
, const void *param
, uint16_t size
= 0);
289 void Unknown01(); // Command 0x53
290 void Unknown02(); // Command 0x01
291 void Unknown03(); // Command 0x6f
292 void Unknown04(); // Command 0x8a
293 void Unknown05(); // Command 0x90
294 void Unknown06(); // Command 0x44
295 void Unknown07(); // Command 0x45
296 void Unknown08(); // Command 0x54
297 void Unknown09(); // Command 0x33
298 void Unknown10(); // Command 0x46
299 void Unknown11(uint32_t id
); // Command 0x0e
300 void Unknown12(uint32_t id
); // Command 0x50
301 void Unknown13(uint32_t id
); // Command 0x0d
302 void Unknown14(uint32_t id
); // Command 0x85
303 void Unknown15(uint32_t id
); // Command 0x84
304 void GetModulesList(uint32_t id
); // Command 0x8d
305 void GetThreadsList(); // Command 0x08
306 void GetConsoleMessage();
307 void Go(); // Command mal formed :)
308 void Stop(); // Command 0x02
309 void GetStatus(); // Command 0x06
311 //////////////////////////////////