Refactored packet.cc to make JLPacket even tighter
[barry.git] / src / packet.h
blobd04aed9e07f7865f1f45ff040c44e5fe2d070f71
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-2009, 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);
96 //////////////////////////////////
97 // response analysis
99 unsigned int ObjectID() const;
100 unsigned int AttributeID() const;
101 uint32_t ChallengeSeed() const;
102 unsigned int RemainingTries() const;
103 unsigned int SocketResponse() const;
104 unsigned char SocketSequence() const;
109 // DBPacket class
111 /// Provides an API for building and analyzing raw DB protocol packets.
112 /// This class relies on 3 external objects: a Mode::Desktop object,
113 /// a send Data buffer, and a receive data buffer. Socket and
114 /// connection details are retrieved on a readonly basis from the
115 /// Mode::Desktop object, but both send and receive buffers can be
116 /// modified.
118 /// Note that the receive buffer may be modified
119 /// during a packet send, and this DBPacket class provides API helpers
120 /// to analyze the results.
122 class DBPacket : public Packet
124 friend class Socket;
126 private:
127 Mode::Desktop &m_con;
128 unsigned int m_last_dbop; // last database operation
130 protected:
132 public:
133 DBPacket(Mode::Desktop &con, Data &send, Data &receive);
134 ~DBPacket();
136 //////////////////////////////////
137 // meta access
139 //////////////////////////////////
140 // packet building
142 // commands that correspond to the DB operation
143 // constants in protocol.h
144 void ClearDatabase(unsigned int dbId);
145 void GetDBDB();
146 void GetRecordStateTable(unsigned int dbId);
147 void SetRecordFlags(unsigned int dbId, unsigned int stateTableIndex, uint8_t flag1);
148 void DeleteRecordByIndex(unsigned int dbId, unsigned int stateTableIndex);
149 void GetRecordByIndex(unsigned int dbId, unsigned int stateTableIndex);
150 bool SetRecordByIndex(unsigned int dbId, unsigned int stateTableIndex, Builder &build, const IConverter *ic);
151 void GetRecords(unsigned int dbId);
152 bool SetRecord(unsigned int dbId, Builder &build, const IConverter *ic);
155 //////////////////////////////////
156 // response analysis
158 // DB command response functions
159 unsigned int ReturnCode() const; // throws FIXME if packet doesn't support it
160 unsigned int DBOperation() const; // throws Error on size trouble
162 bool Parse(Parser &parser, const IConverter *ic); // switches based on last m_send command
164 // response parsers
169 // JLPacket class
171 /// Provides an API for building and analyzing raw Javaloader protocol packets.
172 /// This class relies on 3 external objects:
173 /// a command send Data buffer (which can be fairly small), a data
174 /// or argument send Data buffer, and a receive data buffer. Socket and
175 /// connection details are retrieved on a readonly basis from the
176 /// Mode::JavaLoader object, but all buffers can be modified.
178 /// Note that the receive buffer may be modified
179 /// during a packet send, and this JLPacket class provides API helpers
180 /// to analyze the results.
182 class JLPacket : public Packet
184 friend class Socket;
186 private:
187 Data &m_cmd, &m_data;
188 int m_last_set_size;
190 public:
191 JLPacket(Data &cmd, Data &send, Data &receive);
192 ~JLPacket();
194 //////////////////////////////////
195 // meta access
197 bool HasData() const { return m_last_set_size == 2; }
199 //////////////////////////////////
200 // packet building
202 // commands that correspond to the operation
203 // constants in protocol.h
205 // returns 1 or 2 depending on whether cmd or cmd+send are available
206 int SimpleCmd(uint8_t cmd, uint8_t unknown = 0, uint16_t size = 0);
207 int SimpleData(const void *data, uint16_t size);
208 int BigEndianData(uint32_t value);
210 int Hello() { return SimpleCmd(SB_COMMAND_JL_HELLO); }
211 int Goodbye() { return SimpleCmd(SB_COMMAND_JL_GOODBYE); }
212 int SetUnknown1();
213 int SetCodFilename(const std::string &filename);
214 int SetCodSize(off_t size);
215 int SetTime(time_t when);
216 int GetScreenshot() { return SimpleCmd(SB_COMMAND_JL_GET_SCREENSHOT); }
217 int DeviceInfo() { return SimpleCmd(SB_COMMAND_JL_DEVICE_INFO); }
218 int OsMetrics() { return SimpleCmd(SB_COMMAND_JL_OS_METRICS); }
219 int BootromMetrics() { return SimpleCmd(SB_COMMAND_JL_BOOTROM_METRICS); }
220 int GetDirectory() { return SimpleCmd(SB_COMMAND_JL_GET_DIRECTORY); }
222 //////////////////////////////////
223 // response analysis
225 // JL command response functions
226 unsigned int ExpectedSize() const; // throws
230 } // namespace Barry
232 #endif