1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "BluetoothRilListener.h"
9 #include "BluetoothHfpManager.h"
10 #include "nsIMobileConnectionInfo.h"
11 #include "nsIRadioInterfaceLayer.h"
12 #include "nsRadioInterfaceLayer.h"
13 #include "nsServiceManagerUtils.h"
16 USING_BLUETOOTH_NAMESPACE
21 NS_IMPL_ISUPPORTS(IccListener
, nsIIccListener
)
24 IccListener::NotifyIccInfoChanged()
26 // mOwner would be set to nullptr only in the dtor of BluetoothRilListener
27 NS_ENSURE_TRUE(mOwner
, NS_ERROR_FAILURE
);
29 BluetoothHfpManager
* hfp
= BluetoothHfpManager::Get();
30 NS_ENSURE_TRUE(hfp
, NS_ERROR_FAILURE
);
32 hfp
->HandleIccInfoChanged(mOwner
->mClientId
);
38 IccListener::NotifyStkCommand(const nsAString
& aMessage
)
44 IccListener::NotifyStkSessionEnd()
50 IccListener::NotifyCardStateChanged()
56 IccListener::Listen(bool aStart
)
58 NS_ENSURE_TRUE(mOwner
, false);
60 nsCOMPtr
<nsIIccProvider
> provider
=
61 do_GetService(NS_RILCONTENTHELPER_CONTRACTID
);
62 NS_ENSURE_TRUE(provider
, false);
66 rv
= provider
->RegisterIccMsg(mOwner
->mClientId
, this);
68 rv
= provider
->UnregisterIccMsg(mOwner
->mClientId
, this);
71 return NS_SUCCEEDED(rv
);
75 IccListener::SetOwner(BluetoothRilListener
*aOwner
)
81 * MobileConnectionListener
83 NS_IMPL_ISUPPORTS(MobileConnectionListener
, nsIMobileConnectionListener
)
86 MobileConnectionListener::NotifyVoiceChanged()
88 BluetoothHfpManager
* hfp
= BluetoothHfpManager::Get();
89 NS_ENSURE_TRUE(hfp
, NS_OK
);
91 hfp
->HandleVoiceConnectionChanged(mClientId
);
97 MobileConnectionListener::NotifyDataChanged()
103 MobileConnectionListener::NotifyUssdReceived(const nsAString
& message
,
110 MobileConnectionListener::NotifyDataError(const nsAString
& message
)
116 MobileConnectionListener::NotifyCFStateChange(bool success
,
119 const nsAString
& number
,
120 uint16_t timeSeconds
,
121 uint16_t serviceClass
)
127 MobileConnectionListener::NotifyEmergencyCbModeChanged(bool active
,
134 MobileConnectionListener::NotifyOtaStatusChanged(const nsAString
& status
)
140 MobileConnectionListener::NotifyIccChanged()
146 MobileConnectionListener::NotifyRadioStateChanged()
152 MobileConnectionListener::NotifyClirModeChanged(uint32_t aMode
)
158 MobileConnectionListener::Listen(bool aStart
)
160 nsCOMPtr
<nsIMobileConnectionProvider
> provider
=
161 do_GetService(NS_RILCONTENTHELPER_CONTRACTID
);
162 NS_ENSURE_TRUE(provider
, false);
166 rv
= provider
->RegisterMobileConnectionMsg(mClientId
, this);
168 rv
= provider
->UnregisterMobileConnectionMsg(mClientId
, this);
171 return NS_SUCCEEDED(rv
);
175 * TelephonyListener Implementation
177 NS_IMPL_ISUPPORTS(TelephonyListener
, nsITelephonyListener
)
180 TelephonyListener::CallStateChanged(uint32_t aServiceId
,
183 const nsAString
& aNumber
,
184 uint16_t aNumberPresentation
,
185 const nsAString
& aName
,
186 uint16_t aNamePresentation
,
193 BluetoothHfpManager
* hfp
= BluetoothHfpManager::Get();
194 NS_ENSURE_TRUE(hfp
, NS_ERROR_FAILURE
);
196 hfp
->HandleCallStateChanged(aCallIndex
, aCallState
, EmptyString(), aNumber
,
197 aIsOutgoing
, aIsConference
, true);
202 TelephonyListener::EnumerateCallState(uint32_t aServiceId
,
205 const nsAString_internal
& aNumber
,
206 uint16_t aNumberPresentation
,
207 const nsAString
& aName
,
208 uint16_t aNamePresentation
,
215 BluetoothHfpManager
* hfp
= BluetoothHfpManager::Get();
216 NS_ENSURE_TRUE(hfp
, NS_ERROR_FAILURE
);
218 hfp
->HandleCallStateChanged(aCallIndex
, aCallState
, EmptyString(), aNumber
,
219 aIsOutgoing
, aIsConference
, false);
224 TelephonyListener::NotifyError(uint32_t aServiceId
,
226 const nsAString
& aError
)
228 BluetoothHfpManager
* hfp
= BluetoothHfpManager::Get();
229 NS_ENSURE_TRUE(hfp
, NS_ERROR_FAILURE
);
231 if (aCallIndex
> 0) {
232 // In order to not miss any related call state transition.
233 // It's possible that 3G network signal lost for unknown reason.
234 // If a call is released abnormally, NotifyError() will be called,
235 // instead of CallStateChanged(). We need to reset the call array state
236 // via setting CALL_STATE_DISCONNECTED
237 hfp
->HandleCallStateChanged(aCallIndex
,
238 nsITelephonyService::CALL_STATE_DISCONNECTED
,
239 aError
, EmptyString(), false, false, true);
240 BT_WARNING("Reset the call state due to call transition ends abnormally");
243 BT_WARNING(NS_ConvertUTF16toUTF8(aError
).get());
248 TelephonyListener::ConferenceCallStateChanged(uint16_t aCallState
)
254 TelephonyListener::EnumerateCallStateComplete()
260 TelephonyListener::SupplementaryServiceNotification(uint32_t aServiceId
,
262 uint16_t aNotification
)
268 TelephonyListener::NotifyConferenceError(const nsAString
& aName
,
269 const nsAString
& aMessage
)
271 BT_WARNING(NS_ConvertUTF16toUTF8(aName
).get());
272 BT_WARNING(NS_ConvertUTF16toUTF8(aMessage
).get());
278 TelephonyListener::NotifyCdmaCallWaiting(uint32_t aServiceId
,
279 const nsAString
& aNumber
,
280 uint16_t aNumberPresentation
,
281 const nsAString
& aName
,
282 uint16_t aNamePresentation
)
284 BluetoothHfpManager
* hfp
= BluetoothHfpManager::Get();
285 NS_ENSURE_TRUE(hfp
, NS_ERROR_FAILURE
);
287 hfp
->UpdateSecondNumber(aNumber
);
293 TelephonyListener::Listen(bool aStart
)
295 nsCOMPtr
<nsITelephonyService
> service
=
296 do_GetService(TELEPHONY_SERVICE_CONTRACTID
);
297 NS_ENSURE_TRUE(service
, false);
301 rv
= service
->RegisterListener(this);
303 rv
= service
->UnregisterListener(this);
306 return NS_SUCCEEDED(rv
);
310 * BluetoothRilListener
312 BluetoothRilListener::BluetoothRilListener()
314 // Query number of total clients (sim slots)
315 uint32_t numOfClients
;
316 nsCOMPtr
<nsIRadioInterfaceLayer
> radioInterfaceLayer
=
317 do_GetService(NS_RADIOINTERFACELAYER_CONTRACTID
);
318 NS_ENSURE_TRUE_VOID(radioInterfaceLayer
);
320 radioInterfaceLayer
->GetNumRadioInterfaces(&numOfClients
);
322 // Init MobileConnectionListener array and IccInfoListener
323 for (uint32_t i
= 0; i
< numOfClients
; i
++) {
324 mMobileConnListeners
.AppendElement(new MobileConnectionListener(i
));
327 mTelephonyListener
= new TelephonyListener();
328 mIccListener
= new IccListener();
329 mIccListener
->SetOwner(this);
331 // Probe for available client
335 BluetoothRilListener::~BluetoothRilListener()
337 mIccListener
->SetOwner(nullptr);
341 BluetoothRilListener::Listen(bool aStart
)
343 NS_ENSURE_TRUE(ListenMobileConnAndIccInfo(aStart
), false);
344 NS_ENSURE_TRUE(mTelephonyListener
->Listen(aStart
), false);
350 BluetoothRilListener::SelectClient()
353 mClientId
= mMobileConnListeners
.Length();
355 nsCOMPtr
<nsIMobileConnectionProvider
> connection
=
356 do_GetService(NS_RILCONTENTHELPER_CONTRACTID
);
357 NS_ENSURE_TRUE_VOID(connection
);
359 for (uint32_t i
= 0; i
< mMobileConnListeners
.Length(); i
++) {
360 nsCOMPtr
<nsIMobileConnectionInfo
> voiceInfo
;
361 connection
->GetVoiceConnectionInfo(i
, getter_AddRefs(voiceInfo
));
363 BT_WARNING("%s: Failed to get voice connection info", __FUNCTION__
);
368 voiceInfo
->GetState(regState
);
369 if (regState
.EqualsLiteral("registered")) {
370 // Found available client
378 BluetoothRilListener::ServiceChanged(uint32_t aClientId
, bool aRegistered
)
381 ListenMobileConnAndIccInfo(false);
385 * - TRUE: service becomes registered. We were listening to all clients
386 * and one of them becomes available. Select it to listen.
387 * - FALSE: service becomes un-registered. The client we were listening
388 * becomes unavailable. Select another registered one to listen.
391 mClientId
= aClientId
;
397 ListenMobileConnAndIccInfo(true);
399 BT_LOGR("%d client %d. new mClientId %d", aRegistered
, aClientId
,
400 (mClientId
< mMobileConnListeners
.Length()) ? mClientId
: -1);
404 BluetoothRilListener::EnumerateCalls()
406 nsCOMPtr
<nsITelephonyService
> service
=
407 do_GetService(TELEPHONY_SERVICE_CONTRACTID
);
408 NS_ENSURE_TRUE_VOID(service
);
410 nsCOMPtr
<nsITelephonyListener
> listener(
411 do_QueryObject(mTelephonyListener
));
413 service
->EnumerateCalls(listener
);
417 BluetoothRilListener::ListenMobileConnAndIccInfo(bool aStart
)
420 * mClientId < number of total clients:
421 * The client with mClientId is available. Start/Stop listening
422 * mobile connection and icc info of this client only.
424 * mClientId >= number of total clients:
425 * All clients are unavailable. Start/Stop listening mobile
426 * connections of all clients.
428 if (mClientId
< mMobileConnListeners
.Length()) {
429 NS_ENSURE_TRUE(mMobileConnListeners
[mClientId
]->Listen(aStart
), false);
430 NS_ENSURE_TRUE(mIccListener
->Listen(aStart
), false);
432 for (uint32_t i
= 0; i
< mMobileConnListeners
.Length(); i
++) {
433 NS_ENSURE_TRUE(mMobileConnListeners
[i
]->Listen(aStart
), false);