|
1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 #include <bluetooth/eirpublisher.h> |
|
17 #include <e32uid.h> |
|
18 #include <e32base.h> |
|
19 #include <bluetooth/eirclient.h> |
|
20 #include <bluetooth/eirmanshared.h> |
|
21 #include "eirmanclientlogger.h" |
|
22 |
|
23 |
|
24 void Panic(TBTEirPublisherPanic aPanic) |
|
25 { |
|
26 User::Panic(KBTEirPublisherPanic, aPanic); |
|
27 } |
|
28 |
|
29 EXPORT_C CEirPublisher* CEirPublisher::NewL(TEirTag aEirTag, MEirPublisherNotifier& aNotifier) |
|
30 { |
|
31 LOG1(_L8("CEirPublisher::NewL creating publisher with tag %d"), aEirTag); |
|
32 CEirPublisher* self = new(ELeave) CEirPublisher(aEirTag, aNotifier); |
|
33 CleanupStack::PushL(self); |
|
34 self->ConstructL(); |
|
35 CleanupStack::Pop(self); |
|
36 return self; |
|
37 } |
|
38 |
|
39 CEirPublisher::CEirPublisher(TEirTag aEirTag, MEirPublisherNotifier& aNotifier) |
|
40 : CActive(CActive::EPriorityStandard) |
|
41 , iEirTag(aEirTag) |
|
42 , iNotifier(aNotifier) |
|
43 , iPublishOutstanding(EFalse) |
|
44 , iRequiredBytes(0) |
|
45 { |
|
46 CActiveScheduler::Add(this); |
|
47 } |
|
48 |
|
49 CEirPublisher::~CEirPublisher() |
|
50 { |
|
51 LOG_FUNC |
|
52 iEirMan.Close(); // this should handle everything! |
|
53 Cancel(); |
|
54 delete iNotifierAo; |
|
55 } |
|
56 |
|
57 void CEirPublisher::ConstructL() |
|
58 { |
|
59 LOG_FUNC |
|
60 iNotifierAo = new(ELeave) CEirPublisherNotifier(*this); |
|
61 iEirMan.Connect(iStatus); |
|
62 iState = EConnecting; |
|
63 SetActive(); |
|
64 } |
|
65 |
|
66 /** |
|
67 Notify server the required bytes from this client for the Extended Inquiry Response packet. |
|
68 @param aRequiredBytes for the required bytes from this client |
|
69 */ |
|
70 EXPORT_C void CEirPublisher::PublishData(TInt aRequiredBytes) |
|
71 { |
|
72 LOG_FUNC |
|
73 __ASSERT_DEBUG(iState != EInvalid, Panic(EUnexpectedPublisherState)); |
|
74 if(iState == ENoConnection) |
|
75 { |
|
76 // If there is no connection to server currently, create one and indicate a request is outstanding. |
|
77 iEirMan.Connect(iStatus); |
|
78 iState = EConnecting; |
|
79 iPublishOutstanding = ETrue; |
|
80 iRequiredBytes = aRequiredBytes; |
|
81 SetActive(); |
|
82 } |
|
83 else if(iState != EIdle) |
|
84 { |
|
85 iPublishOutstanding = ETrue; |
|
86 iRequiredBytes = aRequiredBytes; |
|
87 } |
|
88 else |
|
89 { |
|
90 iState = ENewData; |
|
91 iEirMan.NewData(aRequiredBytes, iStatus); |
|
92 SetActive(); |
|
93 } |
|
94 } |
|
95 |
|
96 /** |
|
97 Send data and its type to server to update the Extended Inquiry Response packet. |
|
98 @param aData for the data to be added into Extended Inquiry Response |
|
99 @param aDataMode for the type of data from this client (partial or complete) |
|
100 */ |
|
101 EXPORT_C void CEirPublisher::SetData(const TDesC8& aData, TEirDataMode aDataMode) |
|
102 { |
|
103 LOG_FUNC |
|
104 // Make sure that client has an invitation to the party |
|
105 __ASSERT_ALWAYS(iState == EDataCallback, PANIC(KEirManCliPncCat, EEirManPanicSetDataUninvited)); |
|
106 iState = ESettingData; |
|
107 iEirMan.SetData(aData, aDataMode, iStatus); |
|
108 SetActive(); |
|
109 } |
|
110 |
|
111 void CEirPublisher::RunL() |
|
112 { |
|
113 LOG_FUNC |
|
114 LEAVEIFERRORL(iStatus.Int()); |
|
115 |
|
116 switch(iState) |
|
117 { |
|
118 case EConnecting: |
|
119 iEirMan.RegisterTag(iEirTag, iStatus); |
|
120 iState = ERegisterTag; |
|
121 SetActive(); |
|
122 break; |
|
123 case ERegisterTag: |
|
124 iNotifierAo->Start(); |
|
125 // Fall through... |
|
126 case ENewData: |
|
127 // Fall through... |
|
128 case ESettingData: |
|
129 if(iPublishOutstanding) |
|
130 { |
|
131 iPublishOutstanding = EFalse; |
|
132 iState = ENewData; |
|
133 iEirMan.NewData(iRequiredBytes, iStatus); |
|
134 SetActive(); |
|
135 } |
|
136 else if(iCallbackOutstanding) |
|
137 { |
|
138 iCallbackOutstanding = EFalse; |
|
139 iState = EDataCallback; |
|
140 LOG1(_L8("CEirPublisher::RunL: About to call back client with bytes available: %d"), iAvailableBytes); |
|
141 iNotifier.MepnSpaceAvailable(iAvailableBytes); |
|
142 } |
|
143 else |
|
144 { |
|
145 iState = EIdle; |
|
146 } |
|
147 break; |
|
148 default: |
|
149 // Unexpected |
|
150 __ASSERT_DEBUG(EFalse, Panic(EUnexpectedPublisherState)); |
|
151 break; |
|
152 } |
|
153 } |
|
154 |
|
155 void CEirPublisher::DoCancel() |
|
156 { |
|
157 LOG_FUNC |
|
158 iNotifierAo->Cancel(); |
|
159 // Because of asynchronous shutdown - we close the session and tidy up |
|
160 // the active objects locally. |
|
161 __ASSERT_DEBUG(!iEirMan.Handle(), Panic(EInvalidEirManHandle)); |
|
162 TRequestStatus* status = &iStatus; |
|
163 User::RequestComplete(status, KErrCancel); |
|
164 iState = EInvalid; |
|
165 } |
|
166 |
|
167 TInt CEirPublisher::RunError(TInt aError) |
|
168 { |
|
169 LOG_FUNC |
|
170 LOG1(_L8("CEirPublisher::RunError: %d"), aError); |
|
171 // report error to client |
|
172 iNotifier.MepnSetDataError(aError); |
|
173 if (iState == EConnecting) |
|
174 { |
|
175 // We have a problem on Connecting to server, set the state to No Connection |
|
176 iState = ENoConnection; |
|
177 } |
|
178 return KErrNone; |
|
179 } |
|
180 |
|
181 void CEirPublisher::SpaceAvailable(TUint aSpace) |
|
182 { |
|
183 LOG_FUNC |
|
184 |
|
185 if (aSpace > KEirLengthTagLength) |
|
186 { |
|
187 iAvailableBytes = aSpace - KEirLengthTagLength; |
|
188 } |
|
189 else |
|
190 { |
|
191 iAvailableBytes = 0; |
|
192 } |
|
193 if(iState == EIdle) |
|
194 { |
|
195 iState = EDataCallback; |
|
196 LOG1(_L8("CEirPublisher::RunL: About to call back client with bytes available: %d"), iAvailableBytes); |
|
197 iNotifier.MepnSpaceAvailable(iAvailableBytes); |
|
198 } |
|
199 else |
|
200 { |
|
201 iCallbackOutstanding = ETrue; |
|
202 } |
|
203 } |
|
204 |
|
205 void CEirPublisher::NotifierError(TInt aError) |
|
206 { |
|
207 static_cast<void>(RunError(aError)); |
|
208 } |
|
209 |
|
210 REirMan& CEirPublisher::EirMan() |
|
211 { |
|
212 LOG_FUNC |
|
213 return iEirMan; |
|
214 } |
|
215 |
|
216 |
|
217 |
|
218 CEirPublisherNotifier::CEirPublisherNotifier(CEirPublisher& aPublisher) |
|
219 : CActive(EPriorityStandard) |
|
220 , iPublisher(aPublisher) |
|
221 { |
|
222 LOG_FUNC |
|
223 CActiveScheduler::Add(this); |
|
224 } |
|
225 |
|
226 CEirPublisherNotifier::~CEirPublisherNotifier() |
|
227 { |
|
228 LOG_FUNC |
|
229 Cancel(); |
|
230 } |
|
231 |
|
232 void CEirPublisherNotifier::Start() |
|
233 { |
|
234 LOG_FUNC |
|
235 iPublisher.EirMan().SpaceAvailableNotification(iSpaceAvailable, iStatus); |
|
236 SetActive(); |
|
237 } |
|
238 |
|
239 void CEirPublisherNotifier::RunL() |
|
240 { |
|
241 LOG_FUNC |
|
242 LEAVEIFERRORL(iStatus.Int()); |
|
243 TUint spaceAvailable = iSpaceAvailable; |
|
244 iPublisher.EirMan().SpaceAvailableNotification(iSpaceAvailable, iStatus); |
|
245 SetActive(); |
|
246 iPublisher.SpaceAvailable(spaceAvailable); |
|
247 } |
|
248 |
|
249 void CEirPublisherNotifier::DoCancel() |
|
250 { |
|
251 LOG_FUNC |
|
252 // Because of asynchronous shutdown - we close the session and tidy up |
|
253 // the active objects locally. |
|
254 __ASSERT_DEBUG(!(iPublisher.EirMan().Handle()), Panic(EInvalidEirManHandle)); |
|
255 TRequestStatus* status = &iStatus; |
|
256 User::RequestComplete(status, KErrCancel); |
|
257 } |
|
258 |
|
259 TInt CEirPublisherNotifier::RunError(TInt aError) |
|
260 { |
|
261 LOG_FUNC |
|
262 iPublisher.NotifierError(aError); |
|
263 return KErrNone; |
|
264 } |