Qt 4.8
usbostcomm.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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:
68  {
85  };
86 };
87 
89 {
90 }
91 
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 
134 {
135  return SendReceive(EUsbOstCmdDisconnect);
136 }
137 
139 {
140  return SendReceive(EUsbOstCmdCodeOpen);
141 }
142 
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 
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
const TInt KVersionOrdinal
Definition: usbostcomm.h:174
TInt Open()
Definition: usbostcomm.h:138
TVersion Version() const
Definition: usbostcomm.h:176
int TOstProtIds
Definition: usbostcomm.h:50
TInt RegisterProtocolID(TOstProtIds aId, TBool aNeedHeader)
Definition: usbostcomm.h:150
static QAuServer & server()
Definition: qsound.cpp:79
void ReadMessage(TRequestStatus &aStatus, TDes8 &aDes)
Definition: usbostcomm.h:156
TInt Close()
Definition: usbostcomm.h:143
TInt Disconnect()
Definition: usbostcomm.h:133
TInt Connect()
Definition: usbostcomm.h:92
TVersion(* TVersionFunction)(const RUsbOstComm *)
Definition: usbostcomm.h:173
void WriteMessage(TRequestStatus &aStatus, const TDesC8 &aDes, TBool aHasHeader=EFalse)
Definition: usbostcomm.h:167
TInt ReadCancel()
Definition: usbostcomm.h:162