00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "m_desktop.h"
00023 #include "data.h"
00024 #include "protocol.h"
00025 #include "protostructs.h"
00026 #include "packet.h"
00027 #include "endian.h"
00028 #include "error.h"
00029 #include "usbwrap.h"
00030 #include "controller.h"
00031 #include <stdexcept>
00032 #include <sstream>
00033
00034 #include "debug.h"
00035
00036 namespace Barry { namespace Mode {
00037
00038
00039
00040
00041
00042 Desktop::Desktop(Controller &con)
00043 : m_con(con)
00044 , m_ModeSocket(0)
00045 , m_ic(0)
00046 {
00047 }
00048
00049 Desktop::Desktop(Controller &con, const IConverter &ic)
00050 : m_con(con)
00051 , m_ModeSocket(0)
00052 , m_ic(&ic)
00053 {
00054 }
00055
00056 Desktop::~Desktop()
00057 {
00058 }
00059
00060
00061
00062
00063 void Desktop::LoadCommandTable()
00064 {
00065 char rawCommand[] = { 6, 0, 0x0a, 0, 0x40, 0, 0, 1, 0, 0 };
00066 *((uint16_t*) rawCommand) = htobs(m_socket->GetSocket());
00067
00068 Data command(rawCommand, sizeof(rawCommand));
00069 Data response;
00070
00071 try {
00072 m_socket->Packet(command, response);
00073
00074 MAKE_PACKET(rpack, response);
00075 while( rpack->command != SB_COMMAND_DB_DONE ) {
00076 m_socket->NextRecord(response);
00077
00078 rpack = (const Protocol::Packet *) response.GetData();
00079 if( rpack->command == SB_COMMAND_DB_DATA && btohs(rpack->size) > 10 ) {
00080
00081
00082 m_commandTable.Clear();
00083 m_commandTable.Parse(response, 6);
00084 }
00085 }
00086
00087 ddout(m_commandTable);
00088
00089 }
00090 catch( Usb::Error & ) {
00091 eout("Desktop: error getting command table");
00092 eeout(command, response);
00093 throw;
00094 }
00095 }
00096
00097 void Desktop::LoadDBDB()
00098 {
00099 Data command, response;
00100 DBPacket packet(*this, command, response);
00101 packet.GetDBDB();
00102
00103 m_socket->Packet(packet);
00104
00105 while( packet.Command() != SB_COMMAND_DB_DONE ) {
00106 if( packet.Command() == SB_COMMAND_DB_DATA ) {
00107 m_dbdb.Clear();
00108 m_dbdb.Parse(response);
00109 }
00110
00111
00112 m_socket->NextRecord(response);
00113 }
00114 }
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148 void Desktop::Open(const char *password)
00149 {
00150 if( m_ModeSocket ) {
00151 m_socket->Close();
00152 m_socket.reset();
00153 m_ModeSocket = 0;
00154 }
00155
00156 m_ModeSocket = m_con.SelectMode(Controller::Desktop);
00157 RetryPassword(password);
00158 }
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177 void Desktop::RetryPassword(const char *password)
00178 {
00179 if( m_socket.get() != 0 )
00180 throw std::logic_error("Socket alreay open in RetryPassword");
00181
00182 m_socket = m_con.m_zero.Open(m_ModeSocket, password);
00183
00184
00185 LoadCommandTable();
00186 LoadDBDB();
00187 }
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200 unsigned int Desktop::GetDBID(const std::string &name) const
00201 {
00202 unsigned int ID = 0;
00203
00204 if( !m_dbdb.GetDBNumber(name, ID) ) {
00205 throw Error("Desktop: database name not found: " + name);
00206 }
00207 return ID;
00208 }
00209
00210
00211
00212
00213
00214
00215
00216 unsigned int Desktop::GetDBCommand(CommandType ct)
00217 {
00218 unsigned int cmd = 0;
00219 const char *cmdName = "Unknown";
00220
00221 switch( ct )
00222 {
00223 case DatabaseAccess:
00224 cmdName = "Database Access";
00225 cmd = m_commandTable.GetCommand(cmdName);
00226 break;
00227 default:
00228 throw std::logic_error("Desktop: unknown command type");
00229 }
00230
00231 if( cmd == 0 ) {
00232 std::ostringstream oss;
00233 oss << "Desktop: unable to get command code: " << cmdName;
00234 throw Error(oss.str());
00235 }
00236
00237 return cmd;
00238 }
00239
00240 void Desktop::SetIConverter(const IConverter &ic)
00241 {
00242 m_ic = ⁣
00243 }
00244
00245
00246
00247
00248
00249
00250
00251
00252 void Desktop::GetRecordStateTable(unsigned int dbId, RecordStateTable &result)
00253 {
00254 dout("Database ID: " << dbId);
00255
00256
00257 result.Clear();
00258
00259 Data command, response;
00260 DBPacket packet(*this, command, response);
00261 packet.GetRecordStateTable(dbId);
00262
00263 m_socket->Packet(packet);
00264 result.Parse(response);
00265
00266
00267 while( packet.Command() != SB_COMMAND_DB_DONE )
00268 m_socket->NextRecord(response);
00269 }
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279 void Desktop::AddRecord(unsigned int dbId, Builder &build)
00280 {
00281 dout("Database ID: " << dbId);
00282
00283 Data command, response;
00284 DBPacket packet(*this, command, response);
00285
00286 if( packet.SetRecord(dbId, build, m_ic) ) {
00287
00288 std::ostringstream oss;
00289
00290 m_socket->Packet(packet);
00291
00292
00293 if( packet.Command() != SB_COMMAND_DB_DONE ) {
00294 oss << "Desktop: device responded with unexpected packet command code: "
00295 << "0x" << std::hex << packet.Command();
00296 throw Error(oss.str());
00297 }
00298
00299 if( packet.ReturnCode() != 0 ) {
00300 oss << "Desktop: device responded with error code (command: "
00301 << packet.Command() << ", code: "
00302 << packet.ReturnCode() << ")";
00303 throw Error(oss.str());
00304 }
00305 }
00306 }
00307
00308
00309
00310
00311
00312
00313
00314
00315 void Desktop::GetRecord(unsigned int dbId,
00316 unsigned int stateTableIndex,
00317 Parser &parser)
00318 {
00319 dout("Database ID: " << dbId);
00320
00321 Data command, response;
00322 DBPacket packet(*this, command, response);
00323 packet.GetRecordByIndex(dbId, stateTableIndex);
00324
00325 m_socket->Packet(packet);
00326
00327
00328 if( response.GetSize() < SB_PACKET_RESPONSE_HEADER_SIZE ) {
00329 eeout(command, response);
00330
00331 std::ostringstream oss;
00332 oss << "Desktop: invalid response packet size of "
00333 << std::dec << response.GetSize();
00334 eout(oss.str());
00335 throw Error(oss.str());
00336 }
00337 if( packet.Command() != SB_COMMAND_DB_DATA ) {
00338 eeout(command, response);
00339
00340 std::ostringstream oss;
00341 oss << "Desktop: unexpected command of 0x"
00342 << std::setbase(16) << packet.Command()
00343 << " instead of expected 0x"
00344 << std::setbase(16) << (unsigned int)SB_COMMAND_DB_DATA;
00345 eout(oss.str());
00346 throw Error(oss.str());
00347 }
00348
00349
00350 packet.Parse(parser, m_ic);
00351
00352
00353 while( packet.Command() != SB_COMMAND_DB_DONE )
00354 m_socket->NextRecord(response);
00355 }
00356
00357
00358
00359
00360
00361
00362
00363 void Desktop::SetRecord(unsigned int dbId, unsigned int stateTableIndex,
00364 Builder &build)
00365 {
00366 dout("Database ID: " << dbId << " Index: " << stateTableIndex);
00367
00368 Data command, response;
00369 DBPacket packet(*this, command, response);
00370
00371
00372 if( !packet.SetRecordByIndex(dbId, stateTableIndex, build, m_ic) ) {
00373 throw std::logic_error("Desktop: no data available in SetRecord");
00374 }
00375
00376 m_socket->Packet(packet);
00377
00378 std::ostringstream oss;
00379
00380
00381 if( packet.Command() != SB_COMMAND_DB_DONE ) {
00382 oss << "Desktop: device responded with unexpected packet command code: "
00383 << "0x" << std::hex << packet.Command();
00384 throw Error(oss.str());
00385 }
00386
00387 if( packet.ReturnCode() != 0 ) {
00388 oss << "Desktop: device responded with error code (command: "
00389 << packet.Command() << ", code: "
00390 << packet.ReturnCode() << ")";
00391 throw Error(oss.str());
00392 }
00393 }
00394
00395
00396
00397
00398
00399
00400 void Desktop::ClearDirty(unsigned int dbId, unsigned int stateTableIndex)
00401 {
00402 dout("Database ID: " << dbId);
00403
00404 Data command, response;
00405 DBPacket packet(*this, command, response);
00406 packet.SetRecordFlags(dbId, stateTableIndex, 0);
00407
00408 m_socket->Packet(packet);
00409
00410
00411 while( packet.Command() != SB_COMMAND_DB_DONE )
00412 m_socket->NextRecord(response);
00413 }
00414
00415
00416
00417
00418
00419
00420 void Desktop::DeleteRecord(unsigned int dbId, unsigned int stateTableIndex)
00421 {
00422 dout("Database ID: " << dbId);
00423
00424 Data command, response;
00425 DBPacket packet(*this, command, response);
00426 packet.DeleteRecordByIndex(dbId, stateTableIndex);
00427
00428 m_socket->Packet(packet);
00429
00430
00431 while( packet.Command() != SB_COMMAND_DB_DONE )
00432 m_socket->NextRecord(response);
00433 }
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458 void Desktop::LoadDatabase(unsigned int dbId, Parser &parser)
00459 {
00460 dout("Database ID: " << dbId);
00461
00462 Data command, response;
00463 DBPacket packet(*this, command, response);
00464 packet.GetRecords(dbId);
00465
00466 m_socket->Packet(packet);
00467
00468 while( packet.Command() != SB_COMMAND_DB_DONE ) {
00469 if( packet.Command() == SB_COMMAND_DB_DATA ) {
00470
00471
00472 packet.Parse(parser, m_ic);
00473 }
00474
00475
00476 m_socket->NextRecord(response);
00477 }
00478 }
00479
00480 void Desktop::SaveDatabase(unsigned int dbId, Builder &builder)
00481 {
00482 dout("Database ID: " << dbId);
00483
00484
00485
00486
00487
00488
00489
00490
00491 Data command, response;
00492 DBPacket packet(*this, command, response);
00493 packet.ClearDatabase(dbId);
00494
00495
00496 m_socket->Packet(packet, 60000);
00497 if( packet.ReturnCode() != 0 ) {
00498 std::ostringstream oss;
00499 oss << "Desktop: could not clear database: (command: "
00500 << "0x" << std::hex << packet.Command() << ", code: "
00501 << "0x" << std::hex << packet.ReturnCode() << ")";
00502 throw Error(oss.str());
00503 }
00504
00505
00506 if( packet.Command() != SB_COMMAND_DB_DONE ) {
00507 eeout(command, response);
00508 throw Error("Desktop: error clearing database, bad response");
00509 }
00510
00511
00512 bool first = true;
00513 while( packet.SetRecord(dbId, builder, m_ic) ) {
00514 dout("Database ID: " << dbId);
00515
00516 m_socket->Packet(packet, first ? 60000 : -1);
00517 first = false;
00518
00519 std::ostringstream oss;
00520
00521 if( packet.Command() != SB_COMMAND_DB_DONE ) {
00522 oss << "Desktop: device responded with unexpected packet command code: "
00523 << "0x" << std::hex << packet.Command();
00524 throw Error(oss.str());
00525 }
00526
00527 if( packet.ReturnCode() != 0 ) {
00528 oss << "Desktop: device responded with error code (command: "
00529 << packet.Command() << ", code: "
00530 << packet.ReturnCode() << ")";
00531 throw Error(oss.str());
00532 }
00533 }
00534 }
00535
00536 }}
00537