|
1 /* |
|
2 * ============================================================================ |
|
3 * Name : btnotifwrapper.cpp |
|
4 * Part of : bluetoothengine / btnotif |
|
5 * Description : Wrapper for Bluetooth Notifiers |
|
6 * |
|
7 * Copyright © 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
8 * All rights reserved. |
|
9 * This component and the accompanying materials are made available |
|
10 * under the terms of "Eclipse Public License v1.0" |
|
11 * which accompanies this distribution, and is available |
|
12 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
13 * |
|
14 * Initial Contributors: |
|
15 * Nokia Corporation - initial contribution. |
|
16 * |
|
17 * Contributors: |
|
18 * Nokia Corporation |
|
19 * ============================================================================ |
|
20 * Template version: 4.1 |
|
21 */ |
|
22 |
|
23 #include "btnotifwrapper.h" |
|
24 #include <btnotif.h> |
|
25 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS |
|
26 #include <btextnotifierspartner.h> |
|
27 #endif |
|
28 |
|
29 /** Identifier of the active object. */ |
|
30 const TInt KWrapperActive = 10; |
|
31 |
|
32 // ======== MEMBER FUNCTIONS ======== |
|
33 |
|
34 |
|
35 // --------------------------------------------------------------------------- |
|
36 // ?description_if_needed |
|
37 // --------------------------------------------------------------------------- |
|
38 // |
|
39 CBTNotifWrapper::CBTNotifWrapper( const TUid& aUid, const TUid& aChannel ) |
|
40 : MEikSrvNotifierBase2(), |
|
41 iUid( aUid ), |
|
42 iChannel( aChannel ), |
|
43 iResponsePtr( NULL, 0 ) |
|
44 { |
|
45 } |
|
46 |
|
47 |
|
48 // --------------------------------------------------------------------------- |
|
49 // ?description_if_needed |
|
50 // --------------------------------------------------------------------------- |
|
51 // |
|
52 void CBTNotifWrapper::ConstructL() |
|
53 { |
|
54 // lazy initialization: members are created and destroyed when needed. |
|
55 } |
|
56 |
|
57 |
|
58 // --------------------------------------------------------------------------- |
|
59 // NewLC |
|
60 // --------------------------------------------------------------------------- |
|
61 // |
|
62 CBTNotifWrapper* CBTNotifWrapper::NewLC( const TUid& aUid, const TUid& aChannel ) |
|
63 { |
|
64 CBTNotifWrapper* self = new( ELeave ) CBTNotifWrapper( aUid, aChannel ); |
|
65 CleanupStack::PushL( self ); |
|
66 self->ConstructL(); |
|
67 return self; |
|
68 } |
|
69 |
|
70 // --------------------------------------------------------------------------- |
|
71 // ?description_if_needed |
|
72 // --------------------------------------------------------------------------- |
|
73 // |
|
74 CBTNotifWrapper::~CBTNotifWrapper() |
|
75 { |
|
76 Cancel(); // Cleans up buffers |
|
77 delete iActive; |
|
78 iBTNotif.Close(); |
|
79 } |
|
80 |
|
81 // --------------------------------------------------------------------------- |
|
82 // From class MEikSrvNotifierBase2. |
|
83 // Frees all the resources i.e. destruct ourselves. |
|
84 // --------------------------------------------------------------------------- |
|
85 // |
|
86 void CBTNotifWrapper::Release() |
|
87 { |
|
88 delete this; |
|
89 } |
|
90 |
|
91 // --------------------------------------------------------------------------- |
|
92 // From class MEikSrvNotifierBase2. |
|
93 // ?implementation_description |
|
94 // --------------------------------------------------------------------------- |
|
95 // |
|
96 MEikSrvNotifierBase2::TNotifierInfo CBTNotifWrapper::RegisterL() |
|
97 { |
|
98 return Info(); |
|
99 } |
|
100 |
|
101 |
|
102 // --------------------------------------------------------------------------- |
|
103 // From class MEikSrvNotifierBase2. |
|
104 // ?implementation_description |
|
105 // --------------------------------------------------------------------------- |
|
106 // |
|
107 MEikSrvNotifierBase2::TNotifierInfo CBTNotifWrapper::Info() const |
|
108 { |
|
109 MEikSrvNotifierBase2::TNotifierInfo info; |
|
110 info.iUid = iUid; |
|
111 info.iChannel = iChannel; |
|
112 info.iPriority = MEikSrvNotifierBase2::ENotifierPriorityVHigh; |
|
113 return info; |
|
114 } |
|
115 |
|
116 |
|
117 // --------------------------------------------------------------------------- |
|
118 // From class MEikSrvNotifierBase2. |
|
119 // ?implementation_description |
|
120 // --------------------------------------------------------------------------- |
|
121 // |
|
122 TPtrC8 CBTNotifWrapper::StartL(const TDesC8& aBuffer) |
|
123 { |
|
124 if( !IsSync() ) |
|
125 { |
|
126 // due to the charasteristics of Bluetooth and UI operations, |
|
127 // most of Bluetooth notifiers do not support synchronous version. |
|
128 User::Leave( KErrNotSupported ); |
|
129 } |
|
130 // Call notifier server and get response. |
|
131 if( !iBTNotif.Handle() ) |
|
132 { |
|
133 User::LeaveIfError( iBTNotif.Connect() ); |
|
134 } |
|
135 User::LeaveIfError( iBTNotif.StartNotifier( iUid, aBuffer ) ); |
|
136 iBTNotif.Close(); |
|
137 return KNullDesC8(); |
|
138 } |
|
139 |
|
140 // --------------------------------------------------------------------------- |
|
141 // From class MEikSrvNotifierBase2. |
|
142 // ?implementation_description |
|
143 // --------------------------------------------------------------------------- |
|
144 // |
|
145 void CBTNotifWrapper::StartL(const TDesC8& aBuffer, TInt aReplySlot, |
|
146 const RMessagePtr2& aMessage) |
|
147 { |
|
148 if( !iMessage.IsNull() ) |
|
149 { |
|
150 aMessage.Complete( KErrAlreadyExists ); |
|
151 return; |
|
152 } |
|
153 |
|
154 // Call notifier server and get response. |
|
155 if( !iBTNotif.Handle() ) |
|
156 { |
|
157 User::LeaveIfError( iBTNotif.Connect() ); |
|
158 } |
|
159 if( !iActive ) |
|
160 { |
|
161 iActive = CBtSimpleActive::NewL( *this, KWrapperActive ); |
|
162 } |
|
163 |
|
164 // We need to store the parameters locally, as aBuffer is destroyed after |
|
165 // returning from this call. We do it on the heap, so we do not permanently |
|
166 // consume memory for the buffer. |
|
167 |
|
168 iParamsBuf = HBufC8::NewL( aBuffer.Size() ); |
|
169 *iParamsBuf = aBuffer; |
|
170 |
|
171 TInt len = aMessage.GetDesMaxLength( aReplySlot ); |
|
172 iResponseBuf = HBufC8::NewL( len ); |
|
173 // Copy in the response, to get the right buffer size. |
|
174 iResponsePtr.Set( iResponseBuf->Des() ); |
|
175 aMessage.ReadL( aReplySlot, iResponsePtr ); |
|
176 |
|
177 iBTNotif.StartNotifierAndGetResponse( iActive->RequestStatus(), |
|
178 iUid, *iParamsBuf, iResponsePtr ); |
|
179 iActive->GoActive(); |
|
180 // record the request |
|
181 iReplySlot = aReplySlot; |
|
182 iMessage = aMessage; |
|
183 } |
|
184 |
|
185 // --------------------------------------------------------------------------- |
|
186 // From class MEikSrvNotifierBase2. |
|
187 // ?implementation_description |
|
188 // --------------------------------------------------------------------------- |
|
189 // |
|
190 void CBTNotifWrapper::Cancel() |
|
191 { |
|
192 // Call notifier server to cancel. |
|
193 if( iActive ) |
|
194 { |
|
195 iActive->Cancel(); |
|
196 } |
|
197 if( !iMessage.IsNull() ) |
|
198 { |
|
199 iMessage.Complete( KErrCancel ); |
|
200 } |
|
201 iReplySlot = 0; |
|
202 delete iParamsBuf; |
|
203 iParamsBuf = NULL; |
|
204 delete iResponseBuf; |
|
205 iResponseBuf = NULL; |
|
206 } |
|
207 |
|
208 // --------------------------------------------------------------------------- |
|
209 // From class MEikSrvNotifierBase2. |
|
210 // Synchronous notifier update. |
|
211 // --------------------------------------------------------------------------- |
|
212 // |
|
213 TPtrC8 CBTNotifWrapper::UpdateL(const TDesC8& aBuffer) |
|
214 { |
|
215 // Call notifier server and get response. |
|
216 TBuf8<256> response; |
|
217 if( !iBTNotif.Handle() ) |
|
218 { |
|
219 User::LeaveIfError( iBTNotif.Connect() ); |
|
220 } |
|
221 User::LeaveIfError( iBTNotif.UpdateNotifier( iUid, aBuffer, response ) ); |
|
222 return response; |
|
223 } |
|
224 |
|
225 // --------------------------------------------------------------------------- |
|
226 // From class MEikSrvNotifierBase2. |
|
227 // Asynchronous notifier update. |
|
228 // --------------------------------------------------------------------------- |
|
229 // |
|
230 void CBTNotifWrapper::UpdateL(const TDesC8& aBuffer, TInt aReplySlot, const RMessagePtr2& aMessage) |
|
231 { |
|
232 (void) aReplySlot; |
|
233 (void) aBuffer; |
|
234 if( iMessage.IsNull() ) |
|
235 { |
|
236 // There is no outstanding request, can't relate this to anything. |
|
237 aMessage.Complete( KErrNotFound ); |
|
238 return; |
|
239 } |
|
240 // Call notifier server and get response. |
|
241 // Async updates are just for updating parameters, so they are still |
|
242 // done synchronously between here and btnotif, as they don't |
|
243 // require any user feedback or other response with |
|
244 // asynchronous/long cycles. |
|
245 TPtrC8 response = UpdateL( aBuffer ); |
|
246 aMessage.WriteL( aReplySlot, response ); |
|
247 aMessage.Complete( KErrNone ); |
|
248 } |
|
249 |
|
250 // --------------------------------------------------------------------------- |
|
251 // From class MBtSimpleActiveObserver. |
|
252 // Callback to notify that an outstanding request has completed. |
|
253 // --------------------------------------------------------------------------- |
|
254 // |
|
255 void CBTNotifWrapper::RequestCompletedL( CBtSimpleActive* aActive, TInt aStatus ) |
|
256 { |
|
257 ASSERT( aActive->RequestId() == KWrapperActive ); |
|
258 (void) aActive; |
|
259 if( !iMessage.IsNull() ) |
|
260 { |
|
261 TInt err( aStatus ); |
|
262 if( !aStatus ) |
|
263 { |
|
264 // for testing: |
|
265 //TPckgBuf<TBTDeviceResponseParams> response; |
|
266 //response.Copy( *iResponseBuf ); |
|
267 //response().BDAddr(); |
|
268 err = iMessage.Write( iReplySlot, *iResponseBuf ); |
|
269 } |
|
270 iMessage.Complete( err ); |
|
271 } |
|
272 // Clean up after usage. |
|
273 iBTNotif.Close(); |
|
274 delete iParamsBuf; |
|
275 iParamsBuf = NULL; |
|
276 delete iResponseBuf; |
|
277 iResponseBuf = NULL; |
|
278 delete iActive; |
|
279 iActive = NULL; |
|
280 } |
|
281 |
|
282 |
|
283 // --------------------------------------------------------------------------- |
|
284 // From class MBtSimpleActiveObserver. |
|
285 // Cancel and clean up all requests related to the active object. |
|
286 // --------------------------------------------------------------------------- |
|
287 // |
|
288 void CBTNotifWrapper::CancelRequest( TInt aRequestId ) |
|
289 { |
|
290 ASSERT( aRequestId == KWrapperActive ); |
|
291 (void) aRequestId; |
|
292 iBTNotif.CancelNotifier( iUid ); |
|
293 } |
|
294 |
|
295 // --------------------------------------------------------------------------- |
|
296 // From class MBtSimpleActiveObserver. |
|
297 // |
|
298 // --------------------------------------------------------------------------- |
|
299 // |
|
300 void CBTNotifWrapper::HandleError( CBtSimpleActive* aActive, |
|
301 TInt aError ) |
|
302 { |
|
303 // RequestCompletedL() is not leavable. |
|
304 (void) aActive; |
|
305 (void) aError; |
|
306 } |
|
307 |
|
308 // --------------------------------------------------------------------------- |
|
309 // Check if this is a synchronous notifier or not. |
|
310 // --------------------------------------------------------------------------- |
|
311 // |
|
312 TBool CBTNotifWrapper::IsSync() const |
|
313 { |
|
314 return ( iUid == KBTGenericInfoNotifierUid ); |
|
315 } |