Bug 921991 - B2G BT: support multiple sim cards, r=echou, r=mrbkap

This commit is contained in:
Ben Tian 2013-11-26 14:11:06 +08:00
parent 03a8a8b6cf
commit 1ad0367958
7 changed files with 297 additions and 157 deletions

View File

@ -7,36 +7,29 @@
#include "BluetoothRilListener.h"
#include "BluetoothHfpManager.h"
#include "nsIIccProvider.h"
#include "nsIMobileConnectionProvider.h"
#include "nsITelephonyProvider.h"
#include "nsIDOMMobileConnection.h"
#include "nsIRadioInterfaceLayer.h"
#include "nsRadioInterfaceLayer.h"
#include "nsServiceManagerUtils.h"
#include "nsString.h"
USING_BLUETOOTH_NAMESPACE
namespace {
/**
* IccListener
*/
class IccListener : public nsIIccListener
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIICCLISTENER
IccListener() { }
};
NS_IMPL_ISUPPORTS1(IccListener, nsIIccListener)
NS_IMETHODIMP
IccListener::NotifyIccInfoChanged()
{
// mOwner would be set to nullptr only in the dtor of BluetoothRilListener
NS_ENSURE_TRUE(mOwner, NS_ERROR_FAILURE);
BluetoothHfpManager* hfp = BluetoothHfpManager::Get();
hfp->HandleIccInfoChanged();
NS_ENSURE_TRUE(hfp, NS_ERROR_FAILURE);
hfp->HandleIccInfoChanged(mOwner->mClientId);
return NS_OK;
}
@ -59,25 +52,43 @@ IccListener::NotifyCardStateChanged()
return NS_OK;
}
bool
IccListener::Listen(bool aStart)
{
NS_ENSURE_TRUE(mOwner, false);
nsCOMPtr<nsIIccProvider> provider =
do_GetService(NS_RILCONTENTHELPER_CONTRACTID);
NS_ENSURE_TRUE(provider, false);
nsresult rv;
if (aStart) {
rv = provider->RegisterIccMsg(mOwner->mClientId, this);
} else {
rv = provider->UnregisterIccMsg(mOwner->mClientId, this);
}
return NS_SUCCEEDED(rv);
}
void
IccListener::SetOwner(BluetoothRilListener *aOwner)
{
mOwner = aOwner;
}
/**
* MobileConnectionListener
*/
class MobileConnectionListener : public nsIMobileConnectionListener
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIMOBILECONNECTIONLISTENER
MobileConnectionListener() { }
};
NS_IMPL_ISUPPORTS1(MobileConnectionListener, nsIMobileConnectionListener)
NS_IMETHODIMP
MobileConnectionListener::NotifyVoiceChanged()
{
BluetoothHfpManager* hfp = BluetoothHfpManager::Get();
hfp->HandleVoiceConnectionChanged();
NS_ENSURE_TRUE(hfp, NS_OK);
hfp->HandleVoiceConnectionChanged(mClientId);
return NS_OK;
}
@ -137,20 +148,26 @@ MobileConnectionListener::NotifyRadioStateChanged()
return NS_OK;
}
bool
MobileConnectionListener::Listen(bool aStart)
{
nsCOMPtr<nsIMobileConnectionProvider> provider =
do_GetService(NS_RILCONTENTHELPER_CONTRACTID);
NS_ENSURE_TRUE(provider, false);
nsresult rv;
if (aStart) {
rv = provider->RegisterMobileConnectionMsg(mClientId, this);
} else {
rv = provider->UnregisterMobileConnectionMsg(mClientId, this);
}
return NS_SUCCEEDED(rv);
}
/**
* TelephonyListener Implementation
*
* TODO: Bug 921991 - B2G BT: support multiple sim cards
*/
class TelephonyListener : public nsITelephonyListener
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSITELEPHONYLISTENER
TelephonyListener() { }
};
NS_IMPL_ISUPPORTS1(TelephonyListener, nsITelephonyListener)
NS_IMETHODIMP
@ -164,9 +181,10 @@ TelephonyListener::CallStateChanged(uint32_t aServiceId,
bool aIsConference)
{
BluetoothHfpManager* hfp = BluetoothHfpManager::Get();
NS_ENSURE_TRUE(hfp, NS_ERROR_FAILURE);
hfp->HandleCallStateChanged(aCallIndex, aCallState, EmptyString(), aNumber,
aIsOutgoing, true);
return NS_OK;
}
@ -181,6 +199,8 @@ TelephonyListener::EnumerateCallState(uint32_t aServiceId,
bool aIsConference)
{
BluetoothHfpManager* hfp = BluetoothHfpManager::Get();
NS_ENSURE_TRUE(hfp, NS_ERROR_FAILURE);
hfp->HandleCallStateChanged(aCallIndex, aCallState, EmptyString(), aNumber,
aIsOutgoing, false);
return NS_OK;
@ -192,6 +212,7 @@ TelephonyListener::NotifyError(uint32_t aServiceId,
const nsAString& aError)
{
BluetoothHfpManager* hfp = BluetoothHfpManager::Get();
NS_ENSURE_TRUE(hfp, NS_ERROR_FAILURE);
if (aCallIndex > 0) {
// In order to not miss any related call state transition.
@ -244,41 +265,123 @@ TelephonyListener::NotifyCdmaCallWaiting(uint32_t aServiceId,
const nsAString& aNumber)
{
BluetoothHfpManager* hfp = BluetoothHfpManager::Get();
NS_ENSURE_TRUE(hfp, NS_ERROR_FAILURE);
hfp->UpdateSecondNumber(aNumber);
return NS_OK;
}
} // anonymous namespace
bool
TelephonyListener::Listen(bool aStart)
{
nsCOMPtr<nsITelephonyProvider> provider =
do_GetService(TELEPHONY_PROVIDER_CONTRACTID);
NS_ENSURE_TRUE(provider, false);
nsresult rv;
if (aStart) {
rv = provider->RegisterListener(this);
} else {
rv = provider->UnregisterListener(this);
}
return NS_SUCCEEDED(rv);
}
/**
* BluetoothRilListener
*/
BluetoothRilListener::BluetoothRilListener()
{
mIccListener = new IccListener();
mMobileConnectionListener = new MobileConnectionListener();
// Query number of total clients (sim slots)
uint32_t numOfClients;
nsCOMPtr<nsIRadioInterfaceLayer> radioInterfaceLayer =
do_GetService(NS_RADIOINTERFACELAYER_CONTRACTID);
NS_ENSURE_TRUE_VOID(radioInterfaceLayer);
radioInterfaceLayer->GetNumRadioInterfaces(&numOfClients);
// Init MobileConnectionListener array and IccInfoListener
for (uint32_t i = 0; i < numOfClients; i++) {
mMobileConnListeners.AppendElement(new MobileConnectionListener(i));
}
mTelephonyListener = new TelephonyListener();
mIccListener = new IccListener();
mIccListener->SetOwner(this);
// Probe for available client
SelectClient();
}
BluetoothRilListener::~BluetoothRilListener()
{
mIccListener->SetOwner(nullptr);
}
bool
BluetoothRilListener::StartListening()
BluetoothRilListener::Listen(bool aStart)
{
NS_ENSURE_TRUE(StartIccListening(), false);
NS_ENSURE_TRUE(StartMobileConnectionListening(), false);
NS_ENSURE_TRUE(StartTelephonyListening(), false);
NS_ENSURE_TRUE(ListenMobileConnAndIccInfo(aStart), false);
NS_ENSURE_TRUE(mTelephonyListener->Listen(aStart), false);
return true;
}
bool
BluetoothRilListener::StopListening()
void
BluetoothRilListener::SelectClient()
{
NS_ENSURE_TRUE(StopIccListening(), false);
NS_ENSURE_TRUE(StopMobileConnectionListening(), false);
NS_ENSURE_TRUE(StopTelephonyListening(), false);
// Reset mClientId
mClientId = mMobileConnListeners.Length();
return true;
nsCOMPtr<nsIMobileConnectionProvider> connection =
do_GetService(NS_RILCONTENTHELPER_CONTRACTID);
NS_ENSURE_TRUE_VOID(connection);
for (uint32_t i = 0; i < mMobileConnListeners.Length(); i++) {
nsCOMPtr<nsIDOMMozMobileConnectionInfo> voiceInfo;
connection->GetVoiceConnectionInfo(i, getter_AddRefs(voiceInfo));
if (!voiceInfo) {
BT_WARNING("%s: Failed to get voice connection info", __FUNCTION__);
continue;
}
nsString regState;
voiceInfo->GetState(regState);
if (regState.EqualsLiteral("registered")) {
// Found available client
mClientId = i;
return;
}
}
}
void
BluetoothRilListener::ServiceChanged(uint32_t aClientId, bool aRegistered)
{
// Stop listening
ListenMobileConnAndIccInfo(false);
/**
* aRegistered:
* - TRUE: service becomes registered. We were listening to all clients
* and one of them becomes available. Select it to listen.
* - FALSE: service becomes un-registered. The client we were listening
* becomes unavailable. Select another registered one to listen.
*/
if (aRegistered) {
mClientId = aClientId;
} else {
SelectClient();
}
// Restart listening
ListenMobileConnAndIccInfo(true);
BT_LOGR("%s: %d client %d. new mClientId %d",
__FUNCTION__, aRegistered, aClientId,
(mClientId < mMobileConnListeners.Length()) ? mClientId : -1);
}
void
@ -288,78 +391,32 @@ BluetoothRilListener::EnumerateCalls()
do_GetService(TELEPHONY_PROVIDER_CONTRACTID);
NS_ENSURE_TRUE_VOID(provider);
provider->EnumerateCalls(mTelephonyListener);
}
nsCOMPtr<nsITelephonyListener> listener(
do_QueryObject(mTelephonyListener));
// private
bool
BluetoothRilListener::StartIccListening()
{
nsCOMPtr<nsIIccProvider> provider =
do_GetService(NS_RILCONTENTHELPER_CONTRACTID);
NS_ENSURE_TRUE(provider, false);
// TODO: Bug 921991 - B2G BT: support multiple sim cards
nsresult rv = provider->RegisterIccMsg(0, mIccListener);
return NS_SUCCEEDED(rv);
provider->EnumerateCalls(listener);
}
bool
BluetoothRilListener::StopIccListening()
BluetoothRilListener::ListenMobileConnAndIccInfo(bool aStart)
{
nsCOMPtr<nsIIccProvider> provider =
do_GetService(NS_RILCONTENTHELPER_CONTRACTID);
NS_ENSURE_TRUE(provider, false);
/**
* mClientId < number of total clients:
* The client with mClientId is available. Start/Stop listening
* mobile connection and icc info of this client only.
*
* mClientId >= number of total clients:
* All clients are unavailable. Start/Stop listening mobile
* connections of all clients.
*/
if (mClientId < mMobileConnListeners.Length()) {
NS_ENSURE_TRUE(mMobileConnListeners[mClientId]->Listen(aStart), false);
NS_ENSURE_TRUE(mIccListener->Listen(aStart), false);
} else {
for (uint32_t i = 0; i < mMobileConnListeners.Length(); i++) {
NS_ENSURE_TRUE(mMobileConnListeners[i]->Listen(aStart), false);
}
}
// TODO: Bug 921991 - B2G BT: support multiple sim cards
nsresult rv = provider->UnregisterIccMsg(0, mIccListener);
return NS_SUCCEEDED(rv);
}
bool
BluetoothRilListener::StartMobileConnectionListening()
{
nsCOMPtr<nsIMobileConnectionProvider> provider =
do_GetService(NS_RILCONTENTHELPER_CONTRACTID);
NS_ENSURE_TRUE(provider, false);
// TODO: Bug 921991 - B2G BT: support multiple sim cards
nsresult rv = provider->
RegisterMobileConnectionMsg(0, mMobileConnectionListener);
return NS_SUCCEEDED(rv);
}
bool
BluetoothRilListener::StopMobileConnectionListening()
{
nsCOMPtr<nsIMobileConnectionProvider> provider =
do_GetService(NS_RILCONTENTHELPER_CONTRACTID);
NS_ENSURE_TRUE(provider, false);
// TODO: Bug 921991 - B2G BT: support multiple sim cards
nsresult rv = provider->
UnregisterMobileConnectionMsg(0, mMobileConnectionListener);
return NS_SUCCEEDED(rv);
}
bool
BluetoothRilListener::StartTelephonyListening()
{
nsCOMPtr<nsITelephonyProvider> provider =
do_GetService(TELEPHONY_PROVIDER_CONTRACTID);
NS_ENSURE_TRUE(provider, false);
nsresult rv = provider->RegisterListener(mTelephonyListener);
return NS_SUCCEEDED(rv);
}
bool
BluetoothRilListener::StopTelephonyListening()
{
nsCOMPtr<nsITelephonyProvider> provider =
do_GetService(TELEPHONY_PROVIDER_CONTRACTID);
NS_ENSURE_TRUE(provider, false);
nsresult rv = provider->UnregisterListener(mTelephonyListener);
return NS_SUCCEEDED(rv);
return true;
}

View File

@ -9,37 +9,117 @@
#include "BluetoothCommon.h"
#include "nsCOMPtr.h"
#include "nsAutoPtr.h"
class nsIIccListener;
class nsIMobileConnectionListener;
class nsITelephonyListener;
#include "nsIIccProvider.h"
#include "nsIMobileConnectionProvider.h"
#include "nsITelephonyProvider.h"
BEGIN_BLUETOOTH_NAMESPACE
class BluetoothRilListener;
class IccListener : public nsIIccListener
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIICCLISTENER
IccListener() { }
bool Listen(bool aStart);
void SetOwner(BluetoothRilListener *aOwner);
private:
BluetoothRilListener* mOwner;
};
class MobileConnectionListener : public nsIMobileConnectionListener
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIMOBILECONNECTIONLISTENER
MobileConnectionListener(uint32_t aClientId)
: mClientId(aClientId) { }
bool Listen(bool aStart);
private:
uint32_t mClientId;
};
class TelephonyListener : public nsITelephonyListener
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSITELEPHONYLISTENER
TelephonyListener() { }
bool Listen(bool aStart);
};
class BluetoothRilListener
{
public:
BluetoothRilListener();
~BluetoothRilListener();
bool StartListening();
bool StopListening();
/**
* Start/Stop listening.
*
* @param aStart [in] whether to start/stop listening
*/
bool Listen(bool aStart);
/**
* Be informed that certain client's service has changed.
*
* @param aClientId [in] the client id with service change
* @param aRegistered [in] whether changed service is registered
*/
void ServiceChanged(uint32_t aClientId, bool aRegistered);
/**
* Enumerate current calls.
*/
void EnumerateCalls();
/**
* The id of client that mobile connection and icc info listeners
* are listening to.
*
* mClientId equals to number of total clients (array length of
* mobile connection listeners) if there is no available client to listen.
*/
uint32_t mClientId;
private:
bool StartIccListening();
bool StopIccListening();
/**
* Start/Stop listening of mobile connection and icc info.
*
* @param aStart [in] whether to start/stop listening
*/
bool ListenMobileConnAndIccInfo(bool aStart);
bool StartMobileConnectionListening();
bool StopMobileConnectionListening();
/**
* Select available client to listen and assign mClientId.
*
* mClientId is assigned to number of total clients (array length of
* mobile connection listeners) if there is no available client to listen.
*/
void SelectClient();
bool StartTelephonyListening();
bool StopTelephonyListening();
/**
* Array of mobile connection listeners.
*
* The length equals to number of total clients.
*/
nsTArray<nsRefPtr<MobileConnectionListener> > mMobileConnListeners;
nsCOMPtr<nsIIccListener> mIccListener;
nsCOMPtr<nsIMobileConnectionListener> mMobileConnectionListener;
nsCOMPtr<nsITelephonyListener> mTelephonyListener;
nsRefPtr<IccListener> mIccListener;
nsRefPtr<TelephonyListener> mTelephonyListener;
};
END_BLUETOOTH_NAMESPACE

View File

@ -386,7 +386,7 @@ BluetoothHfpManager::Init()
hal::RegisterBatteryObserver(this);
mListener = new BluetoothRilListener();
NS_ENSURE_TRUE(mListener->StartListening(), false);
NS_ENSURE_TRUE(mListener->Listen(true), false);
nsCOMPtr<nsISettingsService> settings =
do_GetService("@mozilla.org/settingsService;1");
@ -427,7 +427,7 @@ BluetoothHfpManager::InitHfpInterface()
BluetoothHfpManager::~BluetoothHfpManager()
{
if (!mListener->StopListening()) {
if (!mListener->Listen(false)) {
BT_WARNING("Failed to stop listening RIL");
}
mListener = nullptr;
@ -815,17 +815,20 @@ BluetoothHfpManager::HandleVolumeChanged(const nsAString& aData)
}
void
BluetoothHfpManager::HandleVoiceConnectionChanged()
BluetoothHfpManager::HandleVoiceConnectionChanged(uint32_t aClientId)
{
nsCOMPtr<nsIMobileConnectionProvider> connection =
do_GetService(NS_RILCONTENTHELPER_CONTRACTID);
NS_ENSURE_TRUE_VOID(connection);
nsCOMPtr<nsIDOMMozMobileConnectionInfo> voiceInfo;
connection->GetVoiceConnectionInfo(0, getter_AddRefs(voiceInfo));
connection->GetVoiceConnectionInfo(aClientId, getter_AddRefs(voiceInfo));
NS_ENSURE_TRUE_VOID(voiceInfo);
// Roam
nsString type;
voiceInfo->GetType(type);
mPhoneType = GetPhoneType(type);
bool roaming;
voiceInfo->GetRoaming(&roaming);
mRoam = (roaming) ? 1 : 0;
@ -867,14 +870,14 @@ BluetoothHfpManager::HandleVoiceConnectionChanged()
}
void
BluetoothHfpManager::HandleIccInfoChanged()
BluetoothHfpManager::HandleIccInfoChanged(uint32_t aClientId)
{
nsCOMPtr<nsIIccProvider> icc =
do_GetService(NS_RILCONTENTHELPER_CONTRACTID);
NS_ENSURE_TRUE_VOID(icc);
nsCOMPtr<nsIDOMMozIccInfo> iccInfo;
icc->GetIccInfo(0, getter_AddRefs(iccInfo));
icc->GetIccInfo(aClientId, getter_AddRefs(iccInfo));
NS_ENSURE_TRUE_VOID(iccInfo);
nsCOMPtr<nsIDOMMozGsmIccInfo> gsmIccInfo = do_QueryInterface(iccInfo);

View File

@ -96,8 +96,8 @@ public:
void HandleCallStateChanged(uint32_t aCallIndex, uint16_t aCallState,
const nsAString& aError, const nsAString& aNumber,
const bool aIsOutgoing, bool aSend);
void HandleIccInfoChanged();
void HandleVoiceConnectionChanged();
void HandleIccInfoChanged(uint32_t aClientId);
void HandleVoiceConnectionChanged(uint32_t aClientId);
// Bluedroid hfp callback handlers
void ProcessConnectionState(bthf_connection_state_t aState, bt_bdaddr_t* aBdAddress);

View File

@ -416,7 +416,7 @@ BluetoothHfpManager::Init()
#ifdef MOZ_B2G_RIL
mListener = new BluetoothRilListener();
if (!mListener->StartListening()) {
if (!mListener->Listen(true)) {
BT_WARNING("Failed to start listening RIL");
return false;
}
@ -448,7 +448,7 @@ BluetoothHfpManager::Init()
BluetoothHfpManager::~BluetoothHfpManager()
{
#ifdef MOZ_B2G_RIL
if (!mListener->StopListening()) {
if (!mListener->Listen(false)) {
BT_WARNING("Failed to stop listening RIL");
}
mListener = nullptr;
@ -587,15 +587,14 @@ BluetoothHfpManager::HandleVolumeChanged(const nsAString& aData)
#ifdef MOZ_B2G_RIL
void
BluetoothHfpManager::HandleVoiceConnectionChanged()
BluetoothHfpManager::HandleVoiceConnectionChanged(uint32_t aClientId)
{
nsCOMPtr<nsIMobileConnectionProvider> connection =
do_GetService(NS_RILCONTENTHELPER_CONTRACTID);
NS_ENSURE_TRUE_VOID(connection);
nsCOMPtr<nsIDOMMozMobileConnectionInfo> voiceInfo;
// TODO: Bug 921991 - B2G BT: support multiple sim cards
connection->GetVoiceConnectionInfo(0, getter_AddRefs(voiceInfo));
connection->GetVoiceConnectionInfo(aClientId, getter_AddRefs(voiceInfo));
NS_ENSURE_TRUE_VOID(voiceInfo);
nsString type;
@ -606,11 +605,12 @@ BluetoothHfpManager::HandleVoiceConnectionChanged()
voiceInfo->GetRoaming(&roaming);
UpdateCIND(CINDType::ROAM, roaming);
bool service = false;
nsString regState;
voiceInfo->GetState(regState);
if (regState.EqualsLiteral("registered")) {
service = true;
bool service = regState.EqualsLiteral("registered");
if (service != sCINDItems[CINDType::SERVICE].value) {
// Notify BluetoothRilListener of service change
mListener->ServiceChanged(aClientId, service);
}
UpdateCIND(CINDType::SERVICE, service);
@ -631,8 +631,7 @@ BluetoothHfpManager::HandleVoiceConnectionChanged()
* - manual: set mNetworkSelectionMode to 1 (manual)
*/
nsString mode;
// TODO: Bug 921991 - B2G BT: support multiple sim cards
connection->GetNetworkSelectionMode(0, mode);
connection->GetNetworkSelectionMode(aClientId, mode);
if (mode.EqualsLiteral("manual")) {
mNetworkSelectionMode = 1;
} else {
@ -659,15 +658,14 @@ BluetoothHfpManager::HandleVoiceConnectionChanged()
}
void
BluetoothHfpManager::HandleIccInfoChanged()
BluetoothHfpManager::HandleIccInfoChanged(uint32_t aClientId)
{
nsCOMPtr<nsIIccProvider> icc =
do_GetService(NS_RILCONTENTHELPER_CONTRACTID);
NS_ENSURE_TRUE_VOID(icc);
nsCOMPtr<nsIDOMMozIccInfo> iccInfo;
// TODO: Bug 921991 - B2G BT: support multiple sim cards
icc->GetIccInfo(0, getter_AddRefs(iccInfo));
icc->GetIccInfo(aClientId, getter_AddRefs(iccInfo));
NS_ENSURE_TRUE_VOID(iccInfo);
nsCOMPtr<nsIDOMMozGsmIccInfo> gsmIccInfo = do_QueryInterface(iccInfo);

View File

@ -121,8 +121,8 @@ public:
void HandleCallStateChanged(uint32_t aCallIndex, uint16_t aCallState,
const nsAString& aError, const nsAString& aNumber,
const bool aIsOutgoing, bool aSend);
void HandleIccInfoChanged();
void HandleVoiceConnectionChanged();
void HandleIccInfoChanged(uint32_t aClientId);
void HandleVoiceConnectionChanged(uint32_t aClientId);
// CDMA-specific functions
void UpdateSecondNumber(const nsAString& aNumber);

View File

@ -11,4 +11,6 @@
#define NS_RILCONTENTHELPER_CID \
{ 0x472816e1, 0x1fd6, 0x4405, \
{ 0x99, 0x6c, 0x80, 0x6f, 0x9e, 0xa6, 0x81, 0x74 } }
#define NS_RADIOINTERFACELAYER_CONTRACTID "@mozilla.org/ril;1"
#define NS_RILCONTENTHELPER_CONTRACTID "@mozilla.org/ril/content-helper;1"