lib: added DBDataBuilder class
[barry.git] / src / packet.h
blob11b600da2ede9d45b4c104f43861f34eeaa04a83
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"
30 #include "data.h"
32 namespace Barry { class Data; }
34 namespace Barry {
36 // forward declarations
37 class Parser;
38 class Builder;
39 class SocketZero;
40 class Socket;
41 class IConverter;
42 namespace Mode {
43 class Desktop;
44 class JavaLoader;
47 class Packet
49 friend class SocketZero;
50 friend class Socket;
52 protected:
53 Data &m_send;
54 Data *m_receive;
56 Data& GetSend() { return m_send; }
57 Data& GetReceive() { return *m_receive; }
59 public:
60 Packet(Data &send, Data &receive)
61 : m_send(send), m_receive(&receive)
63 virtual ~Packet() {}
65 // allow user to override the receive buffer for
66 // optimization purposes, to reduce copies... be
67 // careful with this, since this new Data object
68 // must outlive any usage of it via this Packet class
69 void SetNewReceive(Data &receive) { m_receive = &receive; }
71 //////////////////////////////////
72 // common response analysis
74 unsigned int Command() const; // throws Error if receive isn't big enough
78 // ZeroPacket class
80 /// Provides an API for building and analyzing socket-0 protocol packets.
81 /// This class relies on 2 external objects: a send and receive Data buffer.
82 ///
83 /// Note that the receive buffer may be modified
84 /// during a packet send, and this class provides API helpers
85 /// to analyze the results.
86 ///
87 class ZeroPacket : public Packet
89 friend class Socket;
91 public:
92 ZeroPacket(Data &send, Data &receive);
93 ~ZeroPacket();
95 //////////////////////////////////
96 // meta access
98 //////////////////////////////////
99 // packet building
101 void GetAttribute(unsigned int object, unsigned int attribute);
102 void Echo(uint64_t us_ticks);
103 void Reset();
106 //////////////////////////////////
107 // response analysis
109 unsigned int ObjectID() const;
110 unsigned int AttributeID() const;
111 uint32_t ChallengeSeed() const;
112 unsigned int RemainingTries() const;
113 unsigned int SocketResponse() const;
114 unsigned char SocketSequence() const;
115 uint8_t CommandResponse() const;
120 // DBPacket class
122 /// Provides an API for building and analyzing raw DB protocol packets.
123 /// This class relies on 3 external objects: a Mode::Desktop object,
124 /// a send Data buffer, and a receive data buffer. Socket and
125 /// connection details are retrieved on a readonly basis from the
126 /// Mode::Desktop object, but both send and receive buffers can be
127 /// modified.
129 /// Note that the receive buffer may be modified
130 /// during a packet send, and this DBPacket class provides API helpers
131 /// to analyze the results.
133 class DBPacket : public Packet
135 friend class Socket;
137 private:
138 Mode::Desktop &m_con;
139 unsigned int m_last_dbop; // last database operation
141 protected:
143 public:
144 DBPacket(Mode::Desktop &con, Data &send, Data &receive);
145 ~DBPacket();
147 //////////////////////////////////
148 // meta access
150 //////////////////////////////////
151 // packet building
153 // commands that correspond to the DB operation
154 // constants in protocol.h
155 void ClearDatabase(unsigned int dbId);
156 void GetDBDB();
157 void GetRecordStateTable(unsigned int dbId);
158 void SetRecordFlags(unsigned int dbId, unsigned int stateTableIndex, uint8_t flag1);
159 void DeleteRecordByIndex(unsigned int dbId, unsigned int stateTableIndex);
160 void GetRecordByIndex(unsigned int dbId, unsigned int stateTableIndex);
161 bool SetRecordByIndex(unsigned int dbId, unsigned int stateTableIndex, Builder &build, const IConverter *ic);
162 void GetRecords(unsigned int dbId);
163 bool SetRecord(unsigned int dbId, Builder &build, const IConverter *ic);
166 //////////////////////////////////
167 // response analysis
169 // DB command response functions
170 unsigned int ReturnCode() const; // throws FIXME if packet doesn't support it
171 unsigned int DBOperation() const; // throws Error on size trouble
173 bool Parse(Parser &parser, const std::string &dbname,
174 const IConverter *ic); // switches based on last m_send command
175 bool ParseMeta(DBData &data);
177 // response parsers
182 // JLPacket class
184 /// Provides an API for building and analyzing raw Javaloader protocol packets.
185 /// This class relies on 3 external objects:
186 /// a command send Data buffer (which can be fairly small), a data
187 /// or argument send Data buffer, and a receive data buffer. Socket and
188 /// connection details are retrieved on a readonly basis from the
189 /// Mode::JavaLoader object, but all buffers can be modified.
191 /// Note that the receive buffer may be modified
192 /// during a packet send, and this JLPacket class provides API helpers
193 /// to analyze the results.
195 class JLPacket : public Packet
197 friend class Socket;
199 private:
200 Data &m_cmd, &m_data;
201 int m_last_set_size;
203 public:
204 JLPacket(Data &cmd, Data &send, Data &receive);
205 ~JLPacket();
207 //////////////////////////////////
208 // meta access
210 bool HasData() const { return m_last_set_size == 2; }
211 Data& GetReceive() { return *m_receive; }
213 //////////////////////////////////
214 // packet building
216 // commands that correspond to the operation
217 // constants in protocol.h
219 // returns 1 or 2 depending on whether cmd or cmd+send are available
220 int SimpleCmd(uint8_t cmd, uint8_t unknown = 0, uint16_t size = 0);
221 int SimpleData(const void *data, uint16_t size);
222 int BigEndianData(uint16_t value);
223 int BigEndianData(uint32_t value);
225 int Hello() { return SimpleCmd(SB_COMMAND_JL_HELLO); }
226 int Goodbye() { return SimpleCmd(SB_COMMAND_JL_GOODBYE); }
227 int SetUnknown1();
228 int SetCodFilename(const std::string &filename);
229 int SetCodSize(off_t size);
230 int SetTime(time_t when);
231 int GetScreenshot();
232 int GetData() { return SimpleCmd(SB_COMMAND_JL_SEND_DATA); }
233 int DeviceInfo() { return SimpleCmd(SB_COMMAND_JL_DEVICE_INFO); }
234 int OsMetrics() { return SimpleCmd(SB_COMMAND_JL_OS_METRICS); }
235 int BootromMetrics() { return SimpleCmd(SB_COMMAND_JL_BOOTROM_METRICS); }
236 int GetDirectory() { return SimpleCmd(SB_COMMAND_JL_GET_DIRECTORY); }
237 int GetSubDir(uint16_t id);
238 int GetDirEntry(uint8_t entry_cmd, uint16_t id);
239 int Erase(uint16_t cmd, uint16_t id);
240 int GetEventlog() { return SimpleCmd(SB_COMMAND_JL_GET_LOG); }
241 int GetEventlogEntry(uint16_t entry_num);
242 int ClearEventlog() { return SimpleCmd(SB_COMMAND_JL_CLEAR_LOG); }
243 int SaveModule(uint16_t id);
244 int PutData(const void *data, uint16_t size);
245 int WipeApps() { return SimpleCmd(SB_COMMAND_JL_WIPE_APPS); }
246 int WipeFs() { return SimpleCmd(SB_COMMAND_JL_WIPE_FS); }
247 int LogStackTraces() { return SimpleCmd(SB_COMMAND_JL_LOG_STRACES); }
248 int ResetToFactory() { return SimpleCmd(SB_COMMAND_JL_RESET_FACTORY); }
250 //////////////////////////////////
251 // response analysis
252 unsigned int Size();
257 // JVMPacket class
259 /// Provides an API for building and analyzing raw JavaDebug protocol packets.
260 /// This class relies on 3 external objects:
261 /// a command send Data buffer (which can be fairly small), a data
262 /// or argument send Data buffer, and a receive data buffer. Socket and
263 /// connection details are retrieved on a readonly basis from the
264 /// Mode::JavaDebug object, but all buffers can be modified.
266 /// Note that the receive buffer may be modified
267 /// during a packet send, and this JVMPacket class provides API helpers
268 /// to analyze the results.
270 class JVMPacket : public Packet
272 friend class Socket;
274 private:
275 Data &m_cmd;
277 public:
278 JVMPacket(Data &cmd, Data &receive);
279 ~JVMPacket();
281 //////////////////////////////////
282 // meta access
284 Data& GetReceive() { return *m_receive; }
286 //////////////////////////////////
287 // packet building
289 // commands that correspond to the operation
290 // constants in protocol.h
292 // returns 1 or 2 depending on whether cmd or cmd+send are available
293 void SimpleCmd(uint8_t cmd);
294 void ComplexCmd(uint8_t cmd, const void *param, uint16_t size = 0);
296 void Unknown01(); // Command 0x53
297 void Unknown02(); // Command 0x01
298 void Unknown03(); // Command 0x6f
299 void Unknown04(); // Command 0x8a
300 void Unknown05(); // Command 0x90
301 void Unknown06(); // Command 0x44
302 void Unknown07(); // Command 0x45
303 void Unknown08(); // Command 0x54
304 void Unknown09(); // Command 0x33
305 void Unknown10(); // Command 0x46
306 void Unknown11(uint32_t id); // Command 0x0e
307 void Unknown12(uint32_t id); // Command 0x50
308 void Unknown13(uint32_t id); // Command 0x0d
309 void Unknown14(uint32_t id); // Command 0x85
310 void Unknown15(uint32_t id); // Command 0x84
311 void GetModulesList(uint32_t id); // Command 0x8d
312 void GetThreadsList(); // Command 0x08
313 void GetConsoleMessage();
314 void Go(); // Command mal formed :)
315 void Stop(); // Command 0x02
316 void GetStatus(); // Command 0x06
318 //////////////////////////////////
319 // response analysis
320 unsigned int Size();
324 } // namespace Barry
326 #endif