|
1 /** |
|
2 This file is part of CWRT package ** |
|
3 |
|
4 Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). ** |
|
5 |
|
6 This program is free software: you can redistribute it and/or modify |
|
7 it under the terms of the GNU (Lesser) General Public License as |
|
8 published by the Free Software Foundation, version 2.1 of the License. |
|
9 This program is distributed in the hope that it will be useful, but |
|
10 WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
12 (Lesser) General Public License for more details. You should have |
|
13 received a copy of the GNU (Lesser) General Public License along |
|
14 with this program. If not, see <http://www.gnu.org/licenses/>. |
|
15 */ |
|
16 |
|
17 |
|
18 #include <clientinfo.h> |
|
19 #include "serviceipcobserver.h" |
|
20 #include "serviceipcrequest.h" |
|
21 #include "serviceipcserversymbiansession.h" |
|
22 |
|
23 namespace WRT |
|
24 { |
|
25 |
|
26 // Constants for the IPC operation id |
|
27 const TInt KIPCNewOperation = 0; |
|
28 const TInt KIPCGetData = 1; |
|
29 |
|
30 /*! |
|
31 \class CServiceSymbianSession |
|
32 Symbian Session class |
|
33 */ |
|
34 |
|
35 /*! |
|
36 Constructor |
|
37 @param aObserver observer to the server |
|
38 */ |
|
39 CServiceSymbianSession::CServiceSymbianSession(MServiceIPCObserver* aObserver) : |
|
40 ServiceIPCSession(aObserver) |
|
41 { |
|
42 } |
|
43 |
|
44 /*! |
|
45 2nd phased constructor |
|
46 */ |
|
47 void CServiceSymbianSession::ConstructL() |
|
48 { |
|
49 } |
|
50 |
|
51 void CServiceSymbianSession::CreateL() |
|
52 { |
|
53 Server().addSession(); |
|
54 } |
|
55 |
|
56 /*! |
|
57 Two-Phased Constructor |
|
58 @param aObserver observer to the server |
|
59 */ |
|
60 CServiceSymbianSession* CServiceSymbianSession::NewL(MServiceIPCObserver* aObserver) |
|
61 { |
|
62 CServiceSymbianSession* self = |
|
63 new (ELeave) CServiceSymbianSession(aObserver); |
|
64 CleanupStack::PushL(self); |
|
65 self->ConstructL(); |
|
66 CleanupStack::Pop(self); |
|
67 return self; |
|
68 } |
|
69 |
|
70 /*! |
|
71 Destructor |
|
72 */ |
|
73 CServiceSymbianSession::~CServiceSymbianSession() |
|
74 { |
|
75 delete m_curRequest; |
|
76 m_curRequest = NULL; |
|
77 if (m_clientInfo) { |
|
78 delete m_clientInfo; |
|
79 m_clientInfo = NULL; |
|
80 } |
|
81 } |
|
82 |
|
83 /*! |
|
84 Write some data in response to a request |
|
85 @param aData some data to write as response |
|
86 @return bool if write was successful |
|
87 */ |
|
88 bool CServiceSymbianSession::write(const QByteArray& aData) |
|
89 { |
|
90 // Implicitly shared |
|
91 m_data = aData; |
|
92 return false; |
|
93 } |
|
94 |
|
95 /*! |
|
96 Complete a Request |
|
97 @return bool if request completed |
|
98 */ |
|
99 bool CServiceSymbianSession::completeRequest() |
|
100 { |
|
101 |
|
102 m_message.Complete(m_data.count()); |
|
103 delete m_curRequest; |
|
104 m_curRequest = NULL; |
|
105 return true; |
|
106 } |
|
107 |
|
108 /*! |
|
109 Close a session and gracefully shutdown |
|
110 */ |
|
111 void CServiceSymbianSession::close() |
|
112 { |
|
113 // Symbian doesn't really do anything |
|
114 } |
|
115 |
|
116 /*! |
|
117 From CSession2 |
|
118 Service request |
|
119 @param aMessage message object |
|
120 */ |
|
121 void CServiceSymbianSession::ServiceL(const RMessage2& aMessage) |
|
122 { |
|
123 // Default ServiceErrorL() will complete the message if this method leaves |
|
124 TInt operation(aMessage.Function()); |
|
125 switch (operation) { |
|
126 case KIPCNewOperation: { |
|
127 handleRequestL(aMessage); |
|
128 break; |
|
129 } |
|
130 case KIPCGetData: { |
|
131 handleGetBufferL(aMessage); |
|
132 break; |
|
133 } |
|
134 default: { |
|
135 aMessage.Complete(KErrNotFound); |
|
136 break; |
|
137 } |
|
138 } |
|
139 } |
|
140 |
|
141 /*! |
|
142 From CSession2 |
|
143 Handle any disconnection from the client |
|
144 @param aMessage message Object |
|
145 */ |
|
146 void CServiceSymbianSession::Disconnect(const RMessage2 &/*aMessage*/) |
|
147 { |
|
148 if (m_appendToBList) { |
|
149 Server().removeBroadcastList(m_clientInfo->sessionId()); |
|
150 } |
|
151 m_observer->handleClientDisconnect(m_clientInfo); |
|
152 Server().releaseSessionId(m_clientInfo->sessionId()); //release sessionId |
|
153 if (m_curRequest) { |
|
154 completeRequest(); |
|
155 } |
|
156 Server().closeSession(); |
|
157 } |
|
158 |
|
159 /*! |
|
160 Handle an IPC request |
|
161 @param aMessage message Object |
|
162 */ |
|
163 void CServiceSymbianSession::handleRequestL(const RMessage2& aMessage) |
|
164 { |
|
165 // Store the message |
|
166 m_message = aMessage; |
|
167 |
|
168 // Convert from Symbian to QT |
|
169 HBufC* request = ReadDesLC(aMessage, 0); |
|
170 HBufC8* data = ReadDes8LC(aMessage, 1); |
|
171 |
|
172 // Shallow copy only, we want a deep copy |
|
173 QString d = QString::fromUtf16(request->Ptr(), request->Length()); |
|
174 QString operation; |
|
175 operation += d; |
|
176 |
|
177 QByteArray convertData( reinterpret_cast<const char*>(data->Ptr()), data->Length() ); |
|
178 |
|
179 // New request |
|
180 Q_ASSERT(!m_curRequest); |
|
181 m_curRequest = new ServiceIPCRequest(this, 0, operation); |
|
182 m_data.clear(); |
|
183 |
|
184 // Get client info |
|
185 ClientInfo *client = new ClientInfo(); |
|
186 TSecureId sid(aMessage.Identity()); |
|
187 client->setProcessId(sid.iId); |
|
188 client->setVendorId(aMessage.VendorId().iId); |
|
189 RThread clientThread; |
|
190 aMessage.ClientL(clientThread); |
|
191 RProcess clientProc; |
|
192 clientThread.Process(clientProc); |
|
193 client->setName(QString::fromUtf16(clientProc.Name().Ptr(), |
|
194 clientProc.Name().Length())); |
|
195 client->setSessionId(m_clientInfo->sessionId()); |
|
196 // Add data and callback to the observer |
|
197 // |
|
198 m_curRequest->addRequestdata(convertData); |
|
199 m_curRequest->setClientInfo(client); // ownership passed |
|
200 //m_observer->handleRequest(m_curRequest); |
|
201 handleReq(); |
|
202 |
|
203 CleanupStack::PopAndDestroy(2, request); |
|
204 } |
|
205 |
|
206 /*! |
|
207 Handle getting the result buffer |
|
208 */ |
|
209 void CServiceSymbianSession::handleGetBufferL(const RMessage2& aMessage) |
|
210 { |
|
211 TPtrC8 data(reinterpret_cast<const TUint8*> (m_data.constData()), m_data.count()); |
|
212 aMessage.Write(0, data); |
|
213 aMessage.Complete(KErrNone); |
|
214 } |
|
215 |
|
216 /*! |
|
217 Read a 16 bit descriptor from the message |
|
218 @param aMessage message to read from, |
|
219 @param aMsgSlot slot to read from |
|
220 */ |
|
221 HBufC* CServiceSymbianSession::ReadDesLC(const RMessage2& aMessage, |
|
222 TInt aMsgSlot) |
|
223 { |
|
224 TInt length = aMessage.GetDesLengthL(aMsgSlot); |
|
225 HBufC* des = HBufC::NewLC(length); |
|
226 TPtr ptr = des->Des(); |
|
227 aMessage.ReadL(aMsgSlot, ptr); |
|
228 return des; |
|
229 } |
|
230 |
|
231 /*! |
|
232 Read a 8 bit descriptor from the message |
|
233 @param aMessage message to read from, |
|
234 @param aMsgSlot slot to read from |
|
235 */ |
|
236 HBufC8* CServiceSymbianSession::ReadDes8LC(const RMessage2& aMessage, |
|
237 TInt aMsgSlot) |
|
238 { |
|
239 TInt length = aMessage.GetDesLengthL(aMsgSlot); |
|
240 HBufC8* des = HBufC8::NewLC(length); |
|
241 TPtr8 ptr = des->Des(); |
|
242 aMessage.ReadL(aMsgSlot, ptr); |
|
243 return des; |
|
244 } |
|
245 } |
|
246 |
|
247 // END OF FILE |
|
248 |
|
249 |
|
250 |