2012-04-22 22:09:22 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-07-20 13:10:44 +04:00
|
|
|
/* Copyright 2012 Mozilla Foundation and Mozilla contributors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2012-04-22 22:09:22 +04:00
|
|
|
|
|
|
|
#include <android/log.h>
|
2012-08-01 18:58:58 +04:00
|
|
|
#include <fcntl.h>
|
2012-04-22 22:09:22 +04:00
|
|
|
#include <sysutils/NetlinkEvent.h>
|
|
|
|
|
|
|
|
#include "base/message_loop.h"
|
|
|
|
|
|
|
|
#include "Hal.h"
|
2012-08-01 18:58:58 +04:00
|
|
|
#include "mozilla/FileUtils.h"
|
2012-05-04 11:16:27 +04:00
|
|
|
#include "mozilla/Monitor.h"
|
2012-08-01 18:58:58 +04:00
|
|
|
#include "nsPrintfCString.h"
|
2012-04-22 22:09:22 +04:00
|
|
|
#include "nsXULAppAPI.h"
|
|
|
|
#include "UeventPoller.h"
|
|
|
|
|
|
|
|
using namespace mozilla::hal;
|
|
|
|
|
|
|
|
#define LOG(args...) __android_log_print(ANDROID_LOG_INFO, "GonkSwitch" , ## args)
|
|
|
|
|
2012-08-01 18:58:58 +04:00
|
|
|
#define SWITCH_HEADSET_DEVPATH "/devices/virtual/switch/h2w"
|
|
|
|
#define SWITCH_USB_DEVPATH_GB "/devices/virtual/switch/usb_configuration"
|
|
|
|
#define SWITCH_USB_DEVPATH_ICS "/devices/virtual/android_usb/android0"
|
|
|
|
|
2012-04-22 22:09:22 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace hal_impl {
|
2012-08-01 18:58:58 +04:00
|
|
|
/**
|
2012-09-25 15:03:13 +04:00
|
|
|
* The uevent for a usb on GB insertion looks like:
|
|
|
|
*
|
2012-08-01 18:58:58 +04:00
|
|
|
* change@/devices/virtual/switch/usb_configuration
|
|
|
|
* ACTION=change
|
|
|
|
* DEVPATH=/devices/virtual/switch/usb_configuration
|
|
|
|
* SUBSYSTEM=switch
|
|
|
|
* SWITCH_NAME=usb_configuration
|
|
|
|
* SWITCH_STATE=0
|
|
|
|
* SEQNUM=5038
|
2012-09-25 15:03:13 +04:00
|
|
|
*/
|
2012-08-01 18:58:58 +04:00
|
|
|
class SwitchHandler : public RefCounted<SwitchHandler>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SwitchHandler(const char* aDevPath, SwitchDevice aDevice)
|
|
|
|
: mDevPath(aDevPath),
|
|
|
|
mState(SWITCH_STATE_UNKNOWN),
|
|
|
|
mDevice(aDevice)
|
|
|
|
{
|
|
|
|
GetInitialState();
|
|
|
|
}
|
2012-04-22 22:09:22 +04:00
|
|
|
|
2012-08-01 18:58:58 +04:00
|
|
|
virtual ~SwitchHandler()
|
|
|
|
{
|
|
|
|
}
|
2012-04-22 22:09:22 +04:00
|
|
|
|
2012-08-01 18:58:58 +04:00
|
|
|
bool CheckEvent(NetlinkEvent* aEvent)
|
|
|
|
{
|
|
|
|
if (strcmp(GetSubsystem(), aEvent->getSubsystem()) ||
|
|
|
|
strcmp(mDevPath, aEvent->findParam("DEVPATH"))) {
|
|
|
|
return false;
|
2012-04-22 22:09:22 +04:00
|
|
|
}
|
2012-08-01 18:58:58 +04:00
|
|
|
|
|
|
|
mState = ConvertState(GetStateString(aEvent));
|
|
|
|
return mState != SWITCH_STATE_UNKNOWN;
|
2012-04-22 22:09:22 +04:00
|
|
|
}
|
2012-08-01 18:58:58 +04:00
|
|
|
|
|
|
|
SwitchState GetState()
|
|
|
|
{
|
|
|
|
return mState;
|
|
|
|
}
|
|
|
|
|
|
|
|
SwitchDevice GetType()
|
|
|
|
{
|
|
|
|
return mDevice;
|
|
|
|
}
|
|
|
|
protected:
|
|
|
|
virtual const char* GetSubsystem()
|
|
|
|
{
|
|
|
|
return "switch";
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const char* GetStateString(NetlinkEvent* aEvent)
|
|
|
|
{
|
|
|
|
return aEvent->findParam("SWITCH_STATE");
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetInitialState()
|
|
|
|
{
|
|
|
|
nsPrintfCString statePath("/sys%s/state", mDevPath);
|
|
|
|
int fd = open(statePath.get(), O_RDONLY);
|
|
|
|
if (fd <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScopedClose autoClose(fd);
|
|
|
|
char state[16];
|
|
|
|
ssize_t bytesRead = read(fd, state, sizeof(state));
|
|
|
|
if (bytesRead < 0) {
|
|
|
|
LOG("Read data from %s fails", statePath.get());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state[bytesRead - 1] == '\n') {
|
|
|
|
bytesRead--;
|
|
|
|
}
|
|
|
|
|
|
|
|
state[bytesRead] = '\0';
|
|
|
|
mState = ConvertState(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual SwitchState ConvertState(const char* aState)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aState);
|
|
|
|
return aState[0] == '0' ? SWITCH_STATE_OFF : SWITCH_STATE_ON;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* mDevPath;
|
|
|
|
SwitchState mState;
|
|
|
|
SwitchDevice mDevice;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The uevent delivered for the USB configuration under ICS looks like,
|
|
|
|
*
|
|
|
|
* change@/devices/virtual/android_usb/android0
|
|
|
|
* ACTION=change
|
|
|
|
* DEVPATH=/devices/virtual/android_usb/android0
|
|
|
|
* SUBSYSTEM=android_usb
|
|
|
|
* USB_STATE=CONFIGURED
|
|
|
|
* SEQNUM=1802
|
|
|
|
*/
|
|
|
|
class SwitchHandlerUsbIcs: public SwitchHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SwitchHandlerUsbIcs(const char* aDevPath) : SwitchHandler(aDevPath, SWITCH_USB)
|
|
|
|
{
|
|
|
|
SwitchHandler::GetInitialState();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~SwitchHandlerUsbIcs() { }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual const char* GetSubsystem()
|
|
|
|
{
|
|
|
|
return "android_usb";
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const char* GetStateString(NetlinkEvent* aEvent)
|
|
|
|
{
|
|
|
|
return aEvent->findParam("USB_STATE");
|
|
|
|
}
|
|
|
|
|
|
|
|
SwitchState ConvertState(const char* aState)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aState);
|
|
|
|
return strcmp(aState, "CONFIGURED") == 0 ? SWITCH_STATE_ON : SWITCH_STATE_OFF;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-09-25 15:03:13 +04:00
|
|
|
/**
|
|
|
|
* The uevent delivered for the headset under ICS looks like,
|
|
|
|
*
|
|
|
|
* change@/devices/virtual/switch/h2w
|
|
|
|
* ACTION=change
|
|
|
|
* DEVPATH=/devices/virtual/switch/h2w
|
|
|
|
* SUBSYSTEM=switch
|
|
|
|
* SWITCH_NAME=h2w
|
|
|
|
* SWITCH_STATE=2 // Headset with no mic
|
|
|
|
* SEQNUM=2581
|
|
|
|
* On Otoro, SWITCH_NAME could be Headset/No Device when plug/unplug.
|
|
|
|
* change@/devices/virtual/switch/h2w
|
|
|
|
* ACTION=change
|
|
|
|
* DEVPATH=/devices/virtual/switch/h2w
|
|
|
|
* SUBSYSTEM=switch
|
|
|
|
* SWITCH_NAME=Headset
|
|
|
|
* SWITCH_STATE=1 // Headset with mic
|
|
|
|
* SEQNUM=1602
|
|
|
|
*/
|
|
|
|
class SwitchHandlerHeadphone: public SwitchHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SwitchHandlerHeadphone(const char* aDevPath) :
|
|
|
|
SwitchHandler(aDevPath, SWITCH_HEADPHONES)
|
|
|
|
{
|
|
|
|
SwitchHandler::GetInitialState();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~SwitchHandlerHeadphone() { }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SwitchState ConvertState(const char* aState)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aState);
|
|
|
|
|
|
|
|
return aState[0] == '0' ? SWITCH_STATE_OFF :
|
|
|
|
(aState[0] == '1' ? SWITCH_STATE_HEADSET : SWITCH_STATE_HEADPHONE);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-08-01 18:58:58 +04:00
|
|
|
typedef nsTArray<RefPtr<SwitchHandler> > SwitchHandlerArray;
|
2012-04-22 22:09:22 +04:00
|
|
|
|
|
|
|
class SwitchEventRunnable : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2012-08-01 18:58:58 +04:00
|
|
|
SwitchEventRunnable(SwitchEvent& aEvent) : mEvent(aEvent)
|
|
|
|
{
|
|
|
|
}
|
2012-04-22 22:09:22 +04:00
|
|
|
|
2012-08-01 18:58:58 +04:00
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2012-04-22 22:09:22 +04:00
|
|
|
NotifySwitchChange(mEvent);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
SwitchEvent mEvent;
|
|
|
|
};
|
|
|
|
|
2012-08-01 18:58:58 +04:00
|
|
|
class SwitchEventObserver : public IUeventObserver,
|
|
|
|
public RefCounted<SwitchEventObserver>
|
2012-04-22 22:09:22 +04:00
|
|
|
{
|
|
|
|
public:
|
2012-08-01 18:58:58 +04:00
|
|
|
SwitchEventObserver() : mEnableCount(0)
|
|
|
|
{
|
|
|
|
Init();
|
2012-04-22 22:09:22 +04:00
|
|
|
}
|
|
|
|
|
2012-08-01 18:58:58 +04:00
|
|
|
~SwitchEventObserver()
|
|
|
|
{
|
|
|
|
mHandler.Clear();
|
2012-04-22 22:09:22 +04:00
|
|
|
}
|
|
|
|
|
2012-08-01 18:58:58 +04:00
|
|
|
int GetEnableCount()
|
|
|
|
{
|
|
|
|
return mEnableCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EnableSwitch(SwitchDevice aDevice)
|
|
|
|
{
|
|
|
|
mEventInfo[aDevice].mEnabled = true;
|
|
|
|
mEnableCount++;
|
2012-04-22 22:09:22 +04:00
|
|
|
}
|
|
|
|
|
2012-08-01 18:58:58 +04:00
|
|
|
void DisableSwitch(SwitchDevice aDevice)
|
|
|
|
{
|
|
|
|
mEventInfo[aDevice].mEnabled = false;
|
|
|
|
mEnableCount--;
|
2012-04-22 22:09:22 +04:00
|
|
|
}
|
|
|
|
|
2012-08-01 18:58:58 +04:00
|
|
|
void Notify(const NetlinkEvent& aEvent)
|
|
|
|
{
|
|
|
|
SwitchState currState;
|
|
|
|
|
|
|
|
SwitchDevice device = GetEventInfo(aEvent, currState);
|
|
|
|
if (device == SWITCH_DEVICE_UNKNOWN) {
|
2012-04-22 22:09:22 +04:00
|
|
|
return;
|
2012-08-01 18:58:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
EventInfo& info = mEventInfo[device];
|
|
|
|
if (currState == info.mEvent.status()) {
|
|
|
|
return;
|
|
|
|
}
|
2012-04-22 22:09:22 +04:00
|
|
|
|
2012-08-01 18:58:58 +04:00
|
|
|
info.mEvent.status() = currState;
|
|
|
|
|
|
|
|
if (info.mEnabled) {
|
|
|
|
NS_DispatchToMainThread(new SwitchEventRunnable(info.mEvent));
|
|
|
|
}
|
2012-04-22 22:09:22 +04:00
|
|
|
}
|
|
|
|
|
2012-08-01 18:58:58 +04:00
|
|
|
SwitchState GetCurrentInformation(SwitchDevice aDevice)
|
|
|
|
{
|
2012-04-22 22:09:22 +04:00
|
|
|
return mEventInfo[aDevice].mEvent.status();
|
|
|
|
}
|
|
|
|
|
2012-08-01 18:58:58 +04:00
|
|
|
void NotifyAnEvent(SwitchDevice aDevice)
|
|
|
|
{
|
|
|
|
EventInfo& info = mEventInfo[aDevice];
|
|
|
|
if (info.mEvent.status() != SWITCH_STATE_UNKNOWN) {
|
|
|
|
NS_DispatchToMainThread(new SwitchEventRunnable(info.mEvent));
|
|
|
|
}
|
|
|
|
}
|
2012-04-22 22:09:22 +04:00
|
|
|
private:
|
2012-08-01 18:58:58 +04:00
|
|
|
class EventInfo
|
|
|
|
{
|
2012-04-22 22:09:22 +04:00
|
|
|
public:
|
2012-08-01 18:58:58 +04:00
|
|
|
EventInfo() : mEnabled(false)
|
|
|
|
{
|
|
|
|
mEvent.status() = SWITCH_STATE_UNKNOWN;
|
|
|
|
mEvent.device() = SWITCH_DEVICE_UNKNOWN;
|
|
|
|
}
|
2012-04-22 22:09:22 +04:00
|
|
|
SwitchEvent mEvent;
|
2012-08-01 18:58:58 +04:00
|
|
|
bool mEnabled;
|
2012-04-22 22:09:22 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
EventInfo mEventInfo[NUM_SWITCH_DEVICE];
|
2012-08-01 18:58:58 +04:00
|
|
|
size_t mEnableCount;
|
|
|
|
SwitchHandlerArray mHandler;
|
2012-04-22 22:09:22 +04:00
|
|
|
|
2012-08-01 18:58:58 +04:00
|
|
|
void Init()
|
|
|
|
{
|
2012-09-25 15:03:13 +04:00
|
|
|
mHandler.AppendElement(new SwitchHandlerHeadphone(SWITCH_HEADSET_DEVPATH));
|
2012-08-01 18:58:58 +04:00
|
|
|
mHandler.AppendElement(new SwitchHandler(SWITCH_USB_DEVPATH_GB, SWITCH_USB));
|
|
|
|
mHandler.AppendElement(new SwitchHandlerUsbIcs(SWITCH_USB_DEVPATH_ICS));
|
2012-09-25 15:03:13 +04:00
|
|
|
|
2012-08-01 18:58:58 +04:00
|
|
|
SwitchHandlerArray::index_type handlerIndex;
|
|
|
|
SwitchHandlerArray::size_type numHandlers = mHandler.Length();
|
|
|
|
|
|
|
|
for (handlerIndex = 0; handlerIndex < numHandlers; handlerIndex++) {
|
|
|
|
SwitchState state = mHandler[handlerIndex]->GetState();
|
|
|
|
if (state == SWITCH_STATE_UNKNOWN) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
SwitchDevice device = mHandler[handlerIndex]->GetType();
|
|
|
|
mEventInfo[device].mEvent.device() = device;
|
|
|
|
mEventInfo[device].mEvent.status() = state;
|
2012-04-22 22:09:22 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-01 18:58:58 +04:00
|
|
|
SwitchDevice GetEventInfo(const NetlinkEvent& aEvent, SwitchState& aState)
|
|
|
|
{
|
2012-04-22 22:09:22 +04:00
|
|
|
//working around the android code not being const-correct
|
2012-08-01 18:58:58 +04:00
|
|
|
NetlinkEvent *e = const_cast<NetlinkEvent*>(&aEvent);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < mHandler.Length(); i++) {
|
|
|
|
if (mHandler[i]->CheckEvent(e)) {
|
|
|
|
aState = mHandler[i]->GetState();
|
|
|
|
return mHandler[i]->GetType();
|
2012-05-25 08:03:34 +04:00
|
|
|
}
|
2012-04-22 22:09:22 +04:00
|
|
|
}
|
2012-08-01 18:58:58 +04:00
|
|
|
return SWITCH_DEVICE_UNKNOWN;
|
2012-04-22 22:09:22 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-08-01 18:58:58 +04:00
|
|
|
static RefPtr<SwitchEventObserver> sSwitchObserver;
|
2012-04-22 22:09:22 +04:00
|
|
|
|
|
|
|
static void
|
|
|
|
InitializeResourceIfNeed()
|
|
|
|
{
|
|
|
|
if (!sSwitchObserver) {
|
|
|
|
sSwitchObserver = new SwitchEventObserver();
|
|
|
|
RegisterUeventListener(sSwitchObserver);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ReleaseResourceIfNeed()
|
|
|
|
{
|
|
|
|
if (sSwitchObserver->GetEnableCount() == 0) {
|
|
|
|
UnregisterUeventListener(sSwitchObserver);
|
|
|
|
sSwitchObserver = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-05-04 11:16:27 +04:00
|
|
|
EnableSwitchNotificationsIOThread(SwitchDevice aDevice, Monitor *aMonitor)
|
2012-04-22 22:09:22 +04:00
|
|
|
{
|
|
|
|
InitializeResourceIfNeed();
|
|
|
|
sSwitchObserver->EnableSwitch(aDevice);
|
2012-05-04 11:16:27 +04:00
|
|
|
{
|
|
|
|
MonitorAutoLock lock(*aMonitor);
|
|
|
|
lock.Notify();
|
|
|
|
}
|
2012-08-01 18:58:58 +04:00
|
|
|
|
|
|
|
// Notify the latest state if IO thread has the information.
|
|
|
|
if (sSwitchObserver->GetEnableCount() > 1) {
|
|
|
|
sSwitchObserver->NotifyAnEvent(aDevice);
|
|
|
|
}
|
2012-04-22 22:09:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
EnableSwitchNotifications(SwitchDevice aDevice)
|
|
|
|
{
|
2012-05-04 11:16:27 +04:00
|
|
|
Monitor monitor("EnableSwitch.monitor");
|
|
|
|
{
|
|
|
|
MonitorAutoLock lock(monitor);
|
|
|
|
XRE_GetIOMessageLoop()->PostTask(
|
|
|
|
FROM_HERE,
|
|
|
|
NewRunnableFunction(EnableSwitchNotificationsIOThread, aDevice, &monitor));
|
|
|
|
lock.Wait();
|
|
|
|
}
|
2012-04-22 22:09:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
DisableSwitchNotificationsIOThread(SwitchDevice aDevice)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(sSwitchObserver->GetEnableCount());
|
|
|
|
sSwitchObserver->DisableSwitch(aDevice);
|
|
|
|
ReleaseResourceIfNeed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DisableSwitchNotifications(SwitchDevice aDevice)
|
|
|
|
{
|
|
|
|
XRE_GetIOMessageLoop()->PostTask(
|
|
|
|
FROM_HERE,
|
|
|
|
NewRunnableFunction(DisableSwitchNotificationsIOThread, aDevice));
|
|
|
|
}
|
|
|
|
|
|
|
|
SwitchState
|
|
|
|
GetCurrentSwitchState(SwitchDevice aDevice)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(sSwitchObserver && sSwitchObserver->GetEnableCount());
|
|
|
|
return sSwitchObserver->GetCurrentInformation(aDevice);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // hal_impl
|
|
|
|
} //mozilla
|