d253ef83ee1695522bd0c18b4c3da41c16049c15
[profile/ivi/qtdeclarative.git] / src / plugins / qmltooling / qmldbg_ost / usbostcomm.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef USBHOSTCOMM_H
43 #define USBHOSTCOMM_H
44
45 // Based on the official usbostrouter header, modified to remove dependancy on
46 // the client DLL
47
48 #include <e32base.h>
49
50 typedef int TOstProtIds;
51
52 class RUsbOstComm : public RSessionBase
53 {
54 public:
55     RUsbOstComm();
56     TInt Connect();
57     TInt Disconnect();
58     TInt Open();
59     TInt Close();
60     TInt RegisterProtocolID(TOstProtIds aId, TBool aNeedHeader);
61     void ReadMessage(TRequestStatus& aStatus, TDes8& aDes);
62     TInt ReadCancel();
63     void WriteMessage(TRequestStatus& aStatus, const TDesC8& aDes, TBool aHasHeader=EFalse);
64     TVersion Version() const;
65
66 private:
67     enum TUsbOstCmdCode
68     {
69         EUsbOstCmdCodeFirst,
70         EUsbOstCmdConnect,
71         EUsbOstCmdDisconnect,
72         EUsbOstCmdCodeGetAcmConfig,
73         EUsbOstCmdCodeSetAcmConfig,
74         EUsbOstCmdCodeOpen,
75         EUsbOstCmdCodeClose,
76         EUsbOstCmdCodeRegisterId,
77         EUsbOstCmdCodeRegisterIds,
78         EUsbOstCmdCodeUnRegisterId,
79         EUsbOstCmdCodeUnRegisterIds,
80         EUsbOstCmdCodeReadMsg,
81         EUsbOstCmdCodeReadCancel,
82         EUsbOstCmdCodeWriteMsg,
83         EUsbOstCmdCodeWriteCancel,
84         EUsbOstCmdCodeLast
85     };
86 };
87
88 RUsbOstComm::RUsbOstComm()
89 {
90 }
91
92 TInt RUsbOstComm::Connect()
93 {
94     _LIT(KUsbOstServerName, "!UsbOstRouter");
95     _LIT(KUsbOstServerImageName, "usbostrouter");
96     const TUid KUsbOstServerUid = { 0x200170BE };
97     TInt startupAttempts = 2;
98     for(;;) {
99         TInt ret = CreateSession(KUsbOstServerName, TVersion(1,0,0));
100         if (ret != KErrNotFound && ret != KErrServerTerminated) {
101             return ret;
102         }
103
104         if (startupAttempts-- == 0) {
105             return ret;
106         }
107
108         RProcess server;
109         ret = server.Create(KUsbOstServerImageName, KNullDesC, KUsbOstServerUid);
110         if (ret != KErrNone)
111             return ret;
112
113         TRequestStatus serverDiedRequestStatus;
114         server.Rendezvous(serverDiedRequestStatus);
115
116         if (serverDiedRequestStatus != KRequestPending) {
117             // Abort startup
118             server.Kill(KErrNone);
119         } else {
120             // Logon OK - start the server
121             server.Resume();
122         }
123         User::WaitForRequest(serverDiedRequestStatus);
124         ret = (server.ExitType() == EExitPanic) ? KErrGeneral : serverDiedRequestStatus.Int();
125         server.Close();
126
127         if (ret != KErrNone && ret != KErrAlreadyExists) {
128             return ret;
129         }
130     }
131 }
132
133 TInt RUsbOstComm::Disconnect()
134 {
135     return SendReceive(EUsbOstCmdDisconnect);
136 }
137
138 TInt RUsbOstComm::Open()
139 {
140     return SendReceive(EUsbOstCmdCodeOpen);
141 }
142
143 TInt RUsbOstComm::Close()
144 {
145     TInt err = SendReceive(EUsbOstCmdCodeClose);
146     RHandleBase::Close();
147     return err;
148 }
149
150 TInt RUsbOstComm::RegisterProtocolID(const TOstProtIds aId, TBool aNeedHeader)
151 {
152     TIpcArgs args(aId, aNeedHeader);
153     return SendReceive(EUsbOstCmdCodeRegisterId, args);
154 }
155
156 void RUsbOstComm::ReadMessage(TRequestStatus& aStatus, TDes8& aDes)
157 {
158     TIpcArgs args(aDes.MaxLength(), &aDes);
159     SendReceive(EUsbOstCmdCodeReadMsg, args, aStatus);
160 }
161
162 TInt RUsbOstComm::ReadCancel()
163 {
164     return SendReceive(EUsbOstCmdCodeReadCancel);
165 }
166
167 void RUsbOstComm::WriteMessage(TRequestStatus& aStatus, const TDesC8& aDes, TBool aHasHeader)
168 {
169     TIpcArgs args(aHasHeader, aDes.Length(), &aDes);
170     SendReceive(EUsbOstCmdCodeWriteMsg, args, aStatus);
171 }
172
173 typedef TVersion (*TVersionFunction)(const RUsbOstComm*);
174 const TInt KVersionOrdinal = 17;
175
176 TVersion RUsbOstComm::Version() const
177 {
178     // This function has to go to the DLL, unfortunately
179     TVersion result; // Return 0.0.0 on any error
180     RLibrary lib;
181     TInt err = lib.Load(_L("usbostcomm"));
182     if (err) return result;
183
184     TLibraryFunction fn = lib.Lookup(KVersionOrdinal);
185     if (fn)
186         result = ((TVersionFunction)fn)(this);
187     lib.Close();
188     return result;
189 }
190
191 #endif //USBHOSTCOMM_H