lib: added start of libbarrybackup, and copied tarfile support from gui
[barry.git] / src / m_desktop.cc
blobeecdc685c54fa3597bff4e7e7f36ecca1079b6c7
1 ///
2 /// \file m_desktop.cc
3 /// Mode class for the Desktop mode
4 ///
6 /*
7 Copyright (C) 2005-2010, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #include "m_desktop.h"
23 #include "data.h"
24 #include "protocol.h"
25 #include "protostructs.h"
26 #include "packet.h"
27 #include "endian.h"
28 #include "error.h"
29 #include "usbwrap.h"
30 #include "controller.h"
31 #include <stdexcept>
32 #include <sstream>
34 #include "debug.h"
36 namespace Barry { namespace Mode {
39 ///////////////////////////////////////////////////////////////////////////////
40 // Desktop Mode class
42 Desktop::Desktop(Controller &con)
43 : Mode(con, Controller::Desktop)
44 , m_ic(0)
48 Desktop::Desktop(Controller &con, const IConverter &ic)
49 : Mode(con, Controller::Desktop)
50 , m_ic(&ic)
54 Desktop::~Desktop()
58 ///////////////////////////////////////////////////////////////////////////////
59 // protected members
61 void Desktop::LoadCommandTable()
63 char rawCommand[] = { 6, 0, 0x0a, 0, 0x40, 0, 0, 1, 0, 0 };
64 *((uint16_t*) rawCommand) = htobs(m_socket->GetSocket());
66 Data command(rawCommand, sizeof(rawCommand));
67 Data response;
69 try {
70 m_socket->Packet(command, response);
72 MAKE_PACKET(rpack, response);
73 while( rpack->command != SB_COMMAND_DB_DONE ) {
74 m_socket->NextRecord(response);
76 rpack = (const Protocol::Packet *) response.GetData();
77 if( rpack->command == SB_COMMAND_DB_DATA && btohs(rpack->size) > 10 ) {
78 // second packet is generally large, and contains
79 // the command table
80 m_commandTable.Clear();
81 m_commandTable.Parse(response, 6);
85 ddout(m_commandTable);
88 catch( Usb::Error & ) {
89 eout("Desktop: error getting command table");
90 eeout(command, response);
91 throw;
95 void Desktop::LoadDBDB()
97 Data command, response;
98 DBPacket packet(*this, command, response);
99 packet.GetDBDB();
101 m_socket->Packet(packet);
103 while( packet.Command() != SB_COMMAND_DB_DONE ) {
104 if( packet.Command() == SB_COMMAND_DB_DATA ) {
105 m_dbdb.Clear();
106 m_dbdb.Parse(response);
109 // advance!
110 m_socket->NextRecord(response);
114 void Desktop::OnOpen()
116 // get command table and database database
117 LoadCommandTable();
118 LoadDBDB();
121 ///////////////////////////////////////////////////////////////////////////////
122 // public API
125 // GetDBID
127 /// Get numeric database ID by name.
129 /// \param[in] name Name of database, which matches one of the
130 /// names listed in GetDBDB()
132 /// \exception Barry::Error
133 /// Thrown if name not found.
135 unsigned int Desktop::GetDBID(const std::string &name) const
137 unsigned int ID = 0;
138 // FIXME - this needs a better error handler...
139 if( !m_dbdb.GetDBNumber(name, ID) ) {
140 throw Error("Desktop: database name not found: " + name);
142 return ID;
146 // GetDBCommand
148 /// Get database command from command table. Must call Open()
149 /// before this.
151 unsigned int Desktop::GetDBCommand(CommandType ct)
153 unsigned int cmd = 0;
154 const char *cmdName = "Unknown";
156 switch( ct )
158 case DatabaseAccess:
159 cmdName = "Database Access";
160 cmd = m_commandTable.GetCommand(cmdName);
161 break;
162 default:
163 throw std::logic_error("Desktop: unknown command type");
166 if( cmd == 0 ) {
167 std::ostringstream oss;
168 oss << "Desktop: unable to get command code: " << cmdName;
169 throw Error(oss.str());
172 return cmd;
175 void Desktop::SetIConverter(const IConverter &ic)
177 m_ic = &ic;
181 // GetRecordStateTable
183 /// Retrieve the record state table from the handheld device, using the given
184 /// database ID. Results will be stored in result, which will be cleared
185 /// before adding.
187 void Desktop::GetRecordStateTable(unsigned int dbId, RecordStateTable &result)
189 dout("Database ID: " << dbId);
191 // start fresh
192 result.Clear();
194 Data command, response;
195 DBPacket packet(*this, command, response);
196 packet.GetRecordStateTable(dbId);
198 m_socket->Packet(packet);
199 result.Parse(response);
201 // flush the command sequence
202 while( packet.Command() != SB_COMMAND_DB_DONE )
203 m_socket->NextRecord(response);
207 // AddRecord
209 /// Adds a record to the specified database. RecordId is
210 /// retrieved from build, and duplicate IDs are allowed by the device
211 /// (i.e. you can have two records with the same ID)
212 /// but *not* recommended!
214 void Desktop::AddRecord(unsigned int dbId, Builder &build)
216 dout("Database ID: " << dbId);
218 Data command, response;
219 DBPacket packet(*this, command, response);
221 if( packet.SetRecord(dbId, build, m_ic) ) {
223 std::ostringstream oss;
225 m_socket->Packet(packet);
227 // successful packet transfer, so check the network return code
228 if( packet.Command() != SB_COMMAND_DB_DONE ) {
229 oss << "Desktop: device responded with unexpected packet command code: "
230 << "0x" << std::hex << packet.Command();
231 throw Error(oss.str());
234 if( packet.ReturnCode() != 0 ) {
235 oss << "Desktop: device responded with error code (command: "
236 << packet.Command() << ", code: "
237 << packet.ReturnCode() << ")";
238 throw Error(oss.str());
244 // GetRecord
246 /// Retrieves a specific record from the specified database.
247 /// The stateTableIndex comes from the GetRecordStateTable()
248 /// function. GetRecord() does not clear the dirty flag.
250 void Desktop::GetRecord(unsigned int dbId,
251 unsigned int stateTableIndex,
252 Parser &parser)
254 dout("Database ID: " << dbId);
256 Data command, response;
257 DBPacket packet(*this, command, response);
258 packet.GetRecordByIndex(dbId, stateTableIndex);
260 m_socket->Packet(packet);
262 // perform copious packet checks
263 if( response.GetSize() < SB_PACKET_RESPONSE_HEADER_SIZE ) {
264 eeout(command, response);
266 std::ostringstream oss;
267 oss << "Desktop: invalid response packet size of "
268 << std::dec << response.GetSize();
269 eout(oss.str());
270 throw Error(oss.str());
272 if( packet.Command() != SB_COMMAND_DB_DATA ) {
273 eeout(command, response);
275 std::ostringstream oss;
276 oss << "Desktop: unexpected command of 0x"
277 << std::setbase(16) << packet.Command()
278 << " instead of expected 0x"
279 << std::setbase(16) << (unsigned int)SB_COMMAND_DB_DATA;
280 eout(oss.str());
281 throw Error(oss.str());
284 // grab that data
285 packet.Parse(parser, m_ic);
287 // flush the command sequence
288 while( packet.Command() != SB_COMMAND_DB_DONE )
289 m_socket->NextRecord(response);
293 // SetRecord
295 /// Overwrites a specific record in the device as identified by the
296 /// stateTableIndex.
298 void Desktop::SetRecord(unsigned int dbId, unsigned int stateTableIndex,
299 Builder &build)
301 dout("Database ID: " << dbId << " Index: " << stateTableIndex);
303 Data command, response;
304 DBPacket packet(*this, command, response);
306 // loop until builder object has no more data
307 if( !packet.SetRecordByIndex(dbId, stateTableIndex, build, m_ic) ) {
308 throw std::logic_error("Desktop: no data available in SetRecord");
311 m_socket->Packet(packet);
313 std::ostringstream oss;
315 // successful packet transfer, so check the network return code
316 if( packet.Command() != SB_COMMAND_DB_DONE ) {
317 oss << "Desktop: device responded with unexpected packet command code: "
318 << "0x" << std::hex << packet.Command();
319 throw Error(oss.str());
322 if( packet.ReturnCode() != 0 ) {
323 oss << "Desktop: device responded with error code (command: "
324 << packet.Command() << ", code: "
325 << packet.ReturnCode() << ")";
326 throw Error(oss.str());
331 // ClearDirty
333 /// Clears the dirty flag on the specified record in the specified database.
335 void Desktop::ClearDirty(unsigned int dbId, unsigned int stateTableIndex)
337 dout("Database ID: " << dbId);
339 Data command, response;
340 DBPacket packet(*this, command, response);
341 packet.SetRecordFlags(dbId, stateTableIndex, 0);
343 m_socket->Packet(packet);
345 // flush the command sequence
346 while( packet.Command() != SB_COMMAND_DB_DONE )
347 m_socket->NextRecord(response);
351 // DeleteRecord
353 /// Deletes the specified record in the specified database.
355 void Desktop::DeleteRecord(unsigned int dbId, unsigned int stateTableIndex)
357 dout("Database ID: " << dbId);
359 Data command, response;
360 DBPacket packet(*this, command, response);
361 packet.DeleteRecordByIndex(dbId, stateTableIndex);
363 m_socket->Packet(packet);
365 // flush the command sequence
366 while( packet.Command() != SB_COMMAND_DB_DONE )
367 m_socket->NextRecord(response);
371 // LoadDatabase
373 /// Retrieve a database from the handheld device, using the given parser
374 /// to parse the resulting data, and optionally store it.
376 /// See the RecordParser<> template to create a parser object. The
377 /// RecordParser<> template allows custom storage based on the type of
378 /// database record retrieved. The database ID and the parser Record
379 /// type must match.
381 /// \param[in] dbId Database Database ID - use GetDBID()
382 /// \param[out] parser Parser object which parses the resulting
383 /// protocol data, and optionally stores it in
384 /// a custom fashion. See the RecordParser<>
385 /// template.
387 /// \exception Barry::Error
388 /// Thrown on protocol error.
390 /// \exception std::logic_error
391 /// Thrown if not in Desktop mode.
393 void Desktop::LoadDatabase(unsigned int dbId, Parser &parser)
395 dout("Database ID: " << dbId);
397 Data command, response;
398 DBPacket packet(*this, command, response);
399 packet.GetRecords(dbId);
401 m_socket->Packet(packet);
403 while( packet.Command() != SB_COMMAND_DB_DONE ) {
404 if( packet.Command() == SB_COMMAND_DB_DATA ) {
405 // this size is the old header size, since using
406 // old command above
407 packet.Parse(parser, m_ic);
410 // advance!
411 m_socket->NextRecord(response);
415 void Desktop::ClearDatabase(unsigned int dbId)
417 dout("Database ID: " << dbId);
419 Data command, response;
420 DBPacket packet(*this, command, response);
421 packet.ClearDatabase(dbId);
423 // wait up to a minute here for old, slower devices with lots of data
424 m_socket->Packet(packet, 60000);
425 if( packet.ReturnCode() != 0 ) {
426 std::ostringstream oss;
427 oss << "Desktop: could not clear database: (command: "
428 << "0x" << std::hex << packet.Command() << ", code: "
429 << "0x" << std::hex << packet.ReturnCode() << ")";
430 throw Error(oss.str());
433 // check response to clear command was successful
434 if( packet.Command() != SB_COMMAND_DB_DONE ) {
435 eeout(command, response);
436 throw Error("Desktop: error clearing database, bad response");
440 void Desktop::SaveDatabase(unsigned int dbId, Builder &builder)
442 dout("Database ID: " << dbId);
444 // Protocol note: so far in testing, this CLEAR_DATABASE operation is
445 // required, since every record sent via SET_RECORD
446 // is treated like a hypothetical "ADD_RECORD" (perhaps
447 // SET_RECORD should be renamed)... I don't know if
448 // there is a real SET_RECORD... all I know is from
449 // the Windows USB captures, which uses this same
450 // technique.
451 ClearDatabase(dbId);
453 Data command, response;
454 DBPacket packet(*this, command, response);
456 // loop until builder object has no more data
457 bool first = true;
458 while( packet.SetRecord(dbId, builder, m_ic) ) {
459 dout("Database ID: " << dbId);
461 m_socket->Packet(packet, first ? 60000 : -1);
462 first = false;
464 std::ostringstream oss;
465 // successful packet transfer, so check the network return code
466 if( packet.Command() != SB_COMMAND_DB_DONE ) {
467 oss << "Desktop: device responded with unexpected packet command code: "
468 << "0x" << std::hex << packet.Command();
469 throw Error(oss.str());
472 if( packet.ReturnCode() != 0 ) {
473 oss << "Desktop: device responded with error code (command: "
474 << packet.Command() << ", code: "
475 << packet.ReturnCode() << ")";
476 throw Error(oss.str());
481 }} // namespace Barry::Mode