lib: fixed RecurBase class's Dump output, to use decimal numbers
[barry/progweb.git] / src / packet.h
blob5aa5c3daa638a37bc0ac3d10839b0a950540f5cb
1 ///
2 /// \file packet.h
3 /// Low level protocol packet builder class.
4 /// Has knowledge of specific protocol commands in order
5 /// to hide protocol details behind an API.
6 ///
8 /*
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__
27 #include <string>
28 #include <stdint.h>
29 #include <unistd.h>
30 #include "protocol.h"
31 #include "data.h"
33 namespace Barry { class Data; }
35 namespace Barry {
37 // forward declarations
38 class Parser;
39 class Builder;
40 class SocketZero;
41 class Socket;
42 class IConverter;
43 namespace Mode {
44 class Desktop;
45 class JavaLoader;
48 class Packet
50 friend class SocketZero;
51 friend class SocketBase;
53 protected:
54 Data &m_send;
55 Data *m_receive;
57 Data& GetSend() { return m_send; }
58 Data& GetReceive() { return *m_receive; }
60 public:
61 Packet(Data &send, Data &receive)
62 : m_send(send), m_receive(&receive)
64 virtual ~Packet() {}
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
79 // ZeroPacket class
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.
83 ///
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.
87 ///
88 class ZeroPacket : public Packet
90 friend class Socket;
92 public:
93 ZeroPacket(Data &send, Data &receive);
94 ~ZeroPacket();
96 //////////////////////////////////
97 // meta access
99 //////////////////////////////////
100 // packet building
102 void GetAttribute(unsigned int object, unsigned int attribute);
103 void Echo(uint64_t us_ticks);
104 void Reset();
107 //////////////////////////////////
108 // response analysis
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;
121 // DBPacket class
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
128 /// modified.
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
136 friend class Socket;
138 private:
139 Mode::Desktop &m_con;
140 unsigned int m_last_dbop; // last database operation
142 protected:
144 public:
145 DBPacket(Mode::Desktop &con, Data &send, Data &receive);
146 ~DBPacket();
148 //////////////////////////////////
149 // meta access
151 //////////////////////////////////
152 // packet building
154 // commands that correspond to the DB operation
155 // constants in protocol.h
156 void ClearDatabase(unsigned int dbId);
157 void GetDBDB();
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 //////////////////////////////////
168 // response analysis
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);
178 // response parsers
183 // JLPacket class
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;
200 private:
201 Data &m_cmd, &m_data;
202 int m_last_set_size;
204 public:
205 JLPacket(Data &cmd, Data &send, Data &receive);
206 ~JLPacket();
208 //////////////////////////////////
209 // meta access
211 bool HasData() const { return m_last_set_size == 2; }
212 Data& GetReceive() { return *m_receive; }
214 //////////////////////////////////
215 // packet building
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); }
228 int SetUnknown1();
229 int SetCodFilename(const std::string &filename);
230 int SetCodSize(off_t size);
231 int SetTime(time_t when);
232 int GetScreenshot();
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 //////////////////////////////////
252 // response analysis
253 unsigned int Size();
258 // JVMPacket class
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;
275 private:
276 Data &m_cmd;
278 public:
279 JVMPacket(Data &cmd, Data &receive);
280 ~JVMPacket();
282 //////////////////////////////////
283 // meta access
285 Data& GetReceive() { return *m_receive; }
287 //////////////////////////////////
288 // packet building
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 //////////////////////////////////
320 // response analysis
321 unsigned int Size();
325 } // namespace Barry
327 #endif