lib: add prefixes to enums to avoid name clashes on Windows (like ERROR)
[barry.git] / src / packet.h
blobe788e5ac1186a3a2a79d1dd321c00eed5e3578d6
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-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__
27 #include <string>
28 #include <stdint.h>
29 #include "protocol.h"
31 namespace Barry { class Data; }
33 namespace Barry {
35 // forward declarations
36 class Parser;
37 class Builder;
38 class SocketZero;
39 class Socket;
40 class IConverter;
41 namespace Mode {
42 class Desktop;
43 class JavaLoader;
46 class Packet
48 friend class SocketZero;
49 friend class Socket;
51 protected:
52 Data &m_send, &m_receive;
54 Data& GetSend() { return m_send; }
55 Data& GetReceive() { return m_receive; }
57 public:
58 Packet(Data &send, Data &receive)
59 : m_send(send), m_receive(receive)
61 virtual ~Packet() {}
63 //////////////////////////////////
64 // common response analysis
66 unsigned int Command() const; // throws Error if receive isn't big enough
70 // ZeroPacket class
72 /// Provides an API for building and analyzing socket-0 protocol packets.
73 /// This class relies on 2 external objects: a send and receive Data buffer.
74 ///
75 /// Note that the receive buffer may be modified
76 /// during a packet send, and this class provides API helpers
77 /// to analyze the results.
78 ///
79 class ZeroPacket : public Packet
81 friend class Socket;
83 public:
84 ZeroPacket(Data &send, Data &receive);
85 ~ZeroPacket();
87 //////////////////////////////////
88 // meta access
90 //////////////////////////////////
91 // packet building
93 void GetAttribute(unsigned int object, unsigned int attribute);
94 void Echo(uint64_t us_ticks);
95 void Reset();
98 //////////////////////////////////
99 // response analysis
101 unsigned int ObjectID() const;
102 unsigned int AttributeID() const;
103 uint32_t ChallengeSeed() const;
104 unsigned int RemainingTries() const;
105 unsigned int SocketResponse() const;
106 unsigned char SocketSequence() const;
107 uint8_t CommandResponse() const;
112 // DBPacket class
114 /// Provides an API for building and analyzing raw DB protocol packets.
115 /// This class relies on 3 external objects: a Mode::Desktop object,
116 /// a send Data buffer, and a receive data buffer. Socket and
117 /// connection details are retrieved on a readonly basis from the
118 /// Mode::Desktop object, but both send and receive buffers can be
119 /// modified.
121 /// Note that the receive buffer may be modified
122 /// during a packet send, and this DBPacket class provides API helpers
123 /// to analyze the results.
125 class DBPacket : public Packet
127 friend class Socket;
129 private:
130 Mode::Desktop &m_con;
131 unsigned int m_last_dbop; // last database operation
133 protected:
135 public:
136 DBPacket(Mode::Desktop &con, Data &send, Data &receive);
137 ~DBPacket();
139 //////////////////////////////////
140 // meta access
142 //////////////////////////////////
143 // packet building
145 // commands that correspond to the DB operation
146 // constants in protocol.h
147 void ClearDatabase(unsigned int dbId);
148 void GetDBDB();
149 void GetRecordStateTable(unsigned int dbId);
150 void SetRecordFlags(unsigned int dbId, unsigned int stateTableIndex, uint8_t flag1);
151 void DeleteRecordByIndex(unsigned int dbId, unsigned int stateTableIndex);
152 void GetRecordByIndex(unsigned int dbId, unsigned int stateTableIndex);
153 bool SetRecordByIndex(unsigned int dbId, unsigned int stateTableIndex, Builder &build, const IConverter *ic);
154 void GetRecords(unsigned int dbId);
155 bool SetRecord(unsigned int dbId, Builder &build, const IConverter *ic);
158 //////////////////////////////////
159 // response analysis
161 // DB command response functions
162 unsigned int ReturnCode() const; // throws FIXME if packet doesn't support it
163 unsigned int DBOperation() const; // throws Error on size trouble
165 bool Parse(Parser &parser, const std::string &dbname,
166 const IConverter *ic); // switches based on last m_send command
168 // response parsers
173 // JLPacket class
175 /// Provides an API for building and analyzing raw Javaloader protocol packets.
176 /// This class relies on 3 external objects:
177 /// a command send Data buffer (which can be fairly small), a data
178 /// or argument send Data buffer, and a receive data buffer. Socket and
179 /// connection details are retrieved on a readonly basis from the
180 /// Mode::JavaLoader object, but all buffers can be modified.
182 /// Note that the receive buffer may be modified
183 /// during a packet send, and this JLPacket class provides API helpers
184 /// to analyze the results.
186 class JLPacket : public Packet
188 friend class Socket;
190 private:
191 Data &m_cmd, &m_data;
192 int m_last_set_size;
194 public:
195 JLPacket(Data &cmd, Data &send, Data &receive);
196 ~JLPacket();
198 //////////////////////////////////
199 // meta access
201 bool HasData() const { return m_last_set_size == 2; }
202 Data& GetReceive() { return m_receive; }
204 //////////////////////////////////
205 // packet building
207 // commands that correspond to the operation
208 // constants in protocol.h
210 // returns 1 or 2 depending on whether cmd or cmd+send are available
211 int SimpleCmd(uint8_t cmd, uint8_t unknown = 0, uint16_t size = 0);
212 int SimpleData(const void *data, uint16_t size);
213 int BigEndianData(uint16_t value);
214 int BigEndianData(uint32_t value);
216 int Hello() { return SimpleCmd(SB_COMMAND_JL_HELLO); }
217 int Goodbye() { return SimpleCmd(SB_COMMAND_JL_GOODBYE); }
218 int SetUnknown1();
219 int SetCodFilename(const std::string &filename);
220 int SetCodSize(off_t size);
221 int SetTime(time_t when);
222 int GetScreenshot();
223 int GetData() { return SimpleCmd(SB_COMMAND_JL_SEND_DATA); }
224 int DeviceInfo() { return SimpleCmd(SB_COMMAND_JL_DEVICE_INFO); }
225 int OsMetrics() { return SimpleCmd(SB_COMMAND_JL_OS_METRICS); }
226 int BootromMetrics() { return SimpleCmd(SB_COMMAND_JL_BOOTROM_METRICS); }
227 int GetDirectory() { return SimpleCmd(SB_COMMAND_JL_GET_DIRECTORY); }
228 int GetSubDir(uint16_t id);
229 int GetDirEntry(uint8_t entry_cmd, uint16_t id);
230 int Erase(uint16_t cmd, uint16_t id);
231 int GetEventlog() { return SimpleCmd(SB_COMMAND_JL_GET_LOG); }
232 int GetEventlogEntry(uint16_t entry_num);
233 int ClearEventlog() { return SimpleCmd(SB_COMMAND_JL_CLEAR_LOG); }
234 int SaveModule(uint16_t id);
235 int PutData(const void *data, uint16_t size);
236 int WipeApps() { return SimpleCmd(SB_COMMAND_JL_WIPE_APPS); }
237 int WipeFs() { return SimpleCmd(SB_COMMAND_JL_WIPE_FS); }
238 int LogStackTraces() { return SimpleCmd(SB_COMMAND_JL_LOG_STRACES); }
239 int ResetToFactory() { return SimpleCmd(SB_COMMAND_JL_RESET_FACTORY); }
241 //////////////////////////////////
242 // response analysis
243 unsigned int Size();
248 // JVMPacket class
250 /// Provides an API for building and analyzing raw JavaDebug protocol packets.
251 /// This class relies on 3 external objects:
252 /// a command send Data buffer (which can be fairly small), a data
253 /// or argument send Data buffer, and a receive data buffer. Socket and
254 /// connection details are retrieved on a readonly basis from the
255 /// Mode::JavaDebug object, but all buffers can be modified.
257 /// Note that the receive buffer may be modified
258 /// during a packet send, and this JVMPacket class provides API helpers
259 /// to analyze the results.
261 class JVMPacket : public Packet
263 friend class Socket;
265 private:
266 Data &m_cmd;
268 public:
269 JVMPacket(Data &cmd, Data &receive);
270 ~JVMPacket();
272 //////////////////////////////////
273 // meta access
275 Data& GetReceive() { return m_receive; }
277 //////////////////////////////////
278 // packet building
280 // commands that correspond to the operation
281 // constants in protocol.h
283 // returns 1 or 2 depending on whether cmd or cmd+send are available
284 void SimpleCmd(uint8_t cmd);
285 void ComplexCmd(uint8_t cmd, const void *param, uint16_t size = 0);
287 void Unknown01(); // Command 0x53
288 void Unknown02(); // Command 0x01
289 void Unknown03(); // Command 0x6f
290 void Unknown04(); // Command 0x8a
291 void Unknown05(); // Command 0x90
292 void Unknown06(); // Command 0x44
293 void Unknown07(); // Command 0x45
294 void Unknown08(); // Command 0x54
295 void Unknown09(); // Command 0x33
296 void Unknown10(); // Command 0x46
297 void Unknown11(uint32_t id); // Command 0x0e
298 void Unknown12(uint32_t id); // Command 0x50
299 void Unknown13(uint32_t id); // Command 0x0d
300 void Unknown14(uint32_t id); // Command 0x85
301 void Unknown15(uint32_t id); // Command 0x84
302 void GetModulesList(uint32_t id); // Command 0x8d
303 void GetThreadsList(); // Command 0x08
304 void GetConsoleMessage();
305 void Go(); // Command mal formed :)
306 void Stop(); // Command 0x02
307 void GetStatus(); // Command 0x06
309 //////////////////////////////////
310 // response analysis
311 unsigned int Size();
315 } // namespace Barry
317 #endif