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-2012, 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__
33 namespace Barry
{ class Data
; }
37 // forward declarations
50 friend class SocketZero
;
51 friend class SocketBase
;
57 Data
& GetSend() { return m_send
; }
58 Data
& GetReceive() { return *m_receive
; }
61 Packet(Data
&send
, Data
&receive
)
62 : m_send(send
), m_receive(&receive
)
66 // allow user to override the receive buffer for
67 // optimization purposes, to reduce copies... be
68 // careful with this, since this new Data object
69 // must outlive any usage of it via this Packet class
70 void SetNewReceive(Data
&receive
) { m_receive
= &receive
; }
72 //////////////////////////////////
73 // common response analysis
75 unsigned int Command() const; // throws Error if receive isn't big enough
81 /// Provides an API for building and analyzing socket-0 protocol packets.
82 /// This class relies on 2 external objects: a send and receive Data buffer.
84 /// Note that the receive buffer may be modified
85 /// during a packet send, and this class provides API helpers
86 /// to analyze the results.
88 class ZeroPacket
: public Packet
93 ZeroPacket(Data
&send
, Data
&receive
);
96 //////////////////////////////////
99 //////////////////////////////////
102 void GetAttribute(unsigned int object
, unsigned int attribute
);
103 void Echo(uint64_t us_ticks
);
107 //////////////////////////////////
110 unsigned int ObjectID() const;
111 unsigned int AttributeID() const;
112 uint32_t ChallengeSeed() const;
113 unsigned int RemainingTries() const;
114 unsigned int SocketResponse() const;
115 unsigned char SocketSequence() const;
116 uint8_t CommandResponse() const;
123 /// Provides an API for building and analyzing raw DB protocol packets.
124 /// This class relies on 3 external objects: a Mode::Desktop object,
125 /// a send Data buffer, and a receive data buffer. Socket and
126 /// connection details are retrieved on a readonly basis from the
127 /// Mode::Desktop object, but both send and receive buffers can be
130 /// Note that the receive buffer may be modified
131 /// during a packet send, and this DBPacket class provides API helpers
132 /// to analyze the results.
134 class DBPacket
: public Packet
139 Mode::Desktop
&m_con
;
140 unsigned int m_last_dbop
; // last database operation
145 DBPacket(Mode::Desktop
&con
, Data
&send
, Data
&receive
);
148 //////////////////////////////////
151 //////////////////////////////////
154 // commands that correspond to the DB operation
155 // constants in protocol.h
156 void ClearDatabase(unsigned int dbId
);
158 void GetRecordStateTable(unsigned int dbId
);
159 void SetRecordFlags(unsigned int dbId
, unsigned int stateTableIndex
, uint8_t flag1
);
160 void DeleteRecordByIndex(unsigned int dbId
, unsigned int stateTableIndex
);
161 void GetRecordByIndex(unsigned int dbId
, unsigned int stateTableIndex
);
162 bool SetRecordByIndex(unsigned int dbId
, unsigned int stateTableIndex
, Builder
&build
, const IConverter
*ic
);
163 void GetRecords(unsigned int dbId
);
164 bool AddRecord(unsigned int dbId
, Builder
&build
, const IConverter
*ic
);
167 //////////////////////////////////
170 // DB command response functions
171 unsigned int ReturnCode() const; // throws FIXME if packet doesn't support it
172 unsigned int DBOperation() const; // throws Error on size trouble
174 bool Parse(Parser
&parser
, const std::string
&dbname
,
175 const IConverter
*ic
); // switches based on last m_send command
176 bool ParseMeta(DBData
&data
);
185 /// Provides an API for building and analyzing raw Javaloader protocol packets.
186 /// This class relies on 3 external objects:
187 /// a command send Data buffer (which can be fairly small), a data
188 /// or argument send Data buffer, and a receive data buffer. Socket and
189 /// connection details are retrieved on a readonly basis from the
190 /// Mode::JavaLoader object, but all buffers can be modified.
192 /// Note that the receive buffer may be modified
193 /// during a packet send, and this JLPacket class provides API helpers
194 /// to analyze the results.
196 class JLPacket
: public Packet
198 friend class SocketBase
;
201 Data
&m_cmd
, &m_data
;
205 JLPacket(Data
&cmd
, Data
&send
, Data
&receive
);
208 //////////////////////////////////
211 bool HasData() const { return m_last_set_size
== 2; }
212 Data
& GetReceive() { return *m_receive
; }
214 //////////////////////////////////
217 // commands that correspond to the operation
218 // constants in protocol.h
220 // returns 1 or 2 depending on whether cmd or cmd+send are available
221 int SimpleCmd(uint8_t cmd
, uint8_t unknown
= 0, uint16_t size
= 0);
222 int SimpleData(const void *data
, uint16_t size
);
223 int BigEndianData(uint16_t value
);
224 int BigEndianData(uint32_t value
);
226 int Hello() { return SimpleCmd(SB_COMMAND_JL_HELLO
); }
227 int Goodbye() { return SimpleCmd(SB_COMMAND_JL_GOODBYE
); }
229 int SetCodFilename(const std::string
&filename
);
230 int SetCodSize(off_t size
);
231 int SetTime(time_t when
);
233 int GetData() { return SimpleCmd(SB_COMMAND_JL_SEND_DATA
); }
234 int DeviceInfo() { return SimpleCmd(SB_COMMAND_JL_DEVICE_INFO
); }
235 int OsMetrics() { return SimpleCmd(SB_COMMAND_JL_OS_METRICS
); }
236 int BootromMetrics() { return SimpleCmd(SB_COMMAND_JL_BOOTROM_METRICS
); }
237 int GetDirectory() { return SimpleCmd(SB_COMMAND_JL_GET_DIRECTORY
); }
238 int GetSubDir(uint16_t id
);
239 int GetDirEntry(uint8_t entry_cmd
, uint16_t id
);
240 int Erase(uint16_t cmd
, uint16_t id
);
241 int GetEventlog() { return SimpleCmd(SB_COMMAND_JL_GET_LOG
); }
242 int GetEventlogEntry(uint16_t entry_num
);
243 int ClearEventlog() { return SimpleCmd(SB_COMMAND_JL_CLEAR_LOG
); }
244 int SaveModule(uint16_t id
);
245 int PutData(const void *data
, uint16_t size
);
246 int WipeApps() { return SimpleCmd(SB_COMMAND_JL_WIPE_APPS
); }
247 int WipeFs() { return SimpleCmd(SB_COMMAND_JL_WIPE_FS
); }
248 int LogStackTraces() { return SimpleCmd(SB_COMMAND_JL_LOG_STRACES
); }
249 int ResetToFactory() { return SimpleCmd(SB_COMMAND_JL_RESET_FACTORY
); }
251 //////////////////////////////////
260 /// Provides an API for building and analyzing raw JavaDebug protocol packets.
261 /// This class relies on 3 external objects:
262 /// a command send Data buffer (which can be fairly small), a data
263 /// or argument send Data buffer, and a receive data buffer. Socket and
264 /// connection details are retrieved on a readonly basis from the
265 /// Mode::JavaDebug object, but all buffers can be modified.
267 /// Note that the receive buffer may be modified
268 /// during a packet send, and this JVMPacket class provides API helpers
269 /// to analyze the results.
271 class JVMPacket
: public Packet
273 friend class SocketBase
;
279 JVMPacket(Data
&cmd
, Data
&receive
);
282 //////////////////////////////////
285 Data
& GetReceive() { return *m_receive
; }
287 //////////////////////////////////
290 // commands that correspond to the operation
291 // constants in protocol.h
293 // returns 1 or 2 depending on whether cmd or cmd+send are available
294 void SimpleCmd(uint8_t cmd
);
295 void ComplexCmd(uint8_t cmd
, const void *param
, uint16_t size
= 0);
297 void Unknown01(); // Command 0x53
298 void Unknown02(); // Command 0x01
299 void Unknown03(); // Command 0x6f
300 void Unknown04(); // Command 0x8a
301 void Unknown05(); // Command 0x90
302 void Unknown06(); // Command 0x44
303 void Unknown07(); // Command 0x45
304 void Unknown08(); // Command 0x54
305 void Unknown09(); // Command 0x33
306 void Unknown10(); // Command 0x46
307 void Unknown11(uint32_t id
); // Command 0x0e
308 void Unknown12(uint32_t id
); // Command 0x50
309 void Unknown13(uint32_t id
); // Command 0x0d
310 void Unknown14(uint32_t id
); // Command 0x85
311 void Unknown15(uint32_t id
); // Command 0x84
312 void GetModulesList(uint32_t id
); // Command 0x8d
313 void GetThreadsList(); // Command 0x08
314 void GetConsoleMessage();
315 void Go(); // Command mal formed :)
316 void Stop(); // Command 0x02
317 void GetStatus(); // Command 0x06
319 //////////////////////////////////