Release tarball for barry-0.9
[barry.git] / src / controller.h
blobdb4ba77ffeeb16c3121f6b8716d3a89017182333
1 ///
2 /// \file controller.h
3 /// High level BlackBerry API class
4 ///
6 /*
7 Copyright (C) 2005-2007, 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 #ifndef __BARRY_CONTROLLER_H__
23 #define __BARRY_CONTROLLER_H__
25 #include "usbwrap.h"
26 #include "probe.h"
27 #include "socket.h"
28 #include "record.h"
29 #include "data.h"
31 /// Project namespace, containing all related functions and classes.
32 /// This is the only namespace applications should be concerned with,
33 /// for now.
34 namespace Barry {
36 // forward declarations
37 class Parser;
38 class Builder;
39 class DBPacket;
42 // Controller class
44 /// The main interface class. This class coordinates the communication to
45 /// a single handheld.
46 ///
47 /// To use this class, use the following steps:
48 ///
49 /// - Probe the USB bus for matching devices with the Probe class
50 /// - Pass one of the probe results into the Controller constructor
51 /// to connect
52 /// - Call OpenMode() to select the desired mode. This will fill all
53 /// internal data structures for that mode, such as the
54 /// Database Database in Desktop mode.
55 /// NOTE: only Desktop mode is currently implemented.
56 /// - Call GetDBDB() to get the device's database database
57 /// - Call GetDBID() to get a database ID by name
58 /// - In Desktop mode, call LoadDatabase() to retrieve and store a database
59 ///
60 class Controller
62 friend class Barry::DBPacket;
64 public:
65 /// Handheld mode type
66 enum ModeType {
67 Unspecified, //< default on start up
68 Bypass, //< unsupported, unknown
69 Desktop, //< desktop mode required for database
70 //< operation
71 JavaLoader, //< unsupported
72 UsbSerData //< GPRS modem support over USB
74 enum CommandType { Unknown, DatabaseAccess };
76 private:
77 Usb::Device m_dev;
78 Usb::Interface *m_iface;
79 uint32_t m_pin;
81 Socket m_socket;
83 CommandTable m_commandTable;
84 DatabaseDatabase m_dbdb;
86 ModeType m_mode;
88 uint16_t m_modeSocket; // socket recommended by device
89 // when mode was selected
91 // UsbSerData cache
92 Data m_writeCache, m_readCache;
94 // tracking of open Desktop socket, and the need to reset
95 bool m_halfOpen;
97 protected:
98 void SelectMode(ModeType mode);
99 unsigned int GetCommand(CommandType ct);
101 void LoadCommandTable();
102 void LoadDBDB();
104 public:
105 Controller(const ProbeResult &device);
106 ~Controller();
108 //////////////////////////////////
109 // meta access
111 /// Returns DatabaseDatabase object for this connection.
112 /// Must call OpenMode() to select Desktop mode first
113 const DatabaseDatabase& GetDBDB() const { return m_dbdb; }
114 unsigned int GetDBID(const std::string &name) const;
116 //////////////////////////////////
117 // general operations
118 void OpenMode(ModeType mode, const char *password = 0);
119 void RetryPassword(const char *password);
121 //////////////////////////////////
122 // Desktop mode - database specific
124 // dirty flag related functions, for sync operations
125 void GetRecordStateTable(unsigned int dbId, RecordStateTable &result);
126 void AddRecord(unsigned int dbId, Builder &build); // RecordId is
127 // retrieved from build, and duplicate IDs are allowed,
128 // but *not* recommended!
129 void GetRecord(unsigned int dbId, unsigned int stateTableIndex, Parser &parser);
130 void SetRecord(unsigned int dbId, unsigned int stateTableIndex, Builder &build);
131 void ClearDirty(unsigned int dbId, unsigned int stateTableIndex);
132 void DeleteRecord(unsigned int dbId, unsigned int stateTableIndex);
134 // pure load/save operations
135 void LoadDatabase(unsigned int dbId, Parser &parser);
136 void SaveDatabase(unsigned int dbId, Builder &builder);
138 template <class RecordT, class StorageT> void LoadDatabaseByType(StorageT &store);
139 template <class RecordT, class StorageT> void SaveDatabaseByType(StorageT &store);
141 template <class StorageT> void LoadDatabaseByName(const std::string &name, StorageT &store);
142 template <class StorageT> void SaveDatabaseByName(const std::string &name, StorageT &store);
144 template <class RecordT> void AddRecordByType(uint32_t recordId, const RecordT &rec);
147 //////////////////////////////////
148 // UsbSerData mode - modem specific
150 void SerialRead(Data &data, int timeout); // can be called from separate thread
151 void SerialWrite(const Data &data);
154 } // namespace Barry
156 #endif