2011-12-07 02:58:56 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* vim: set sw=4 ts=8 et ft=cpp: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-12-07 02:58:56 -08:00
|
|
|
|
2013-10-29 01:12:45 -07:00
|
|
|
#include "mozilla/ipc/Ril.h"
|
|
|
|
|
2011-12-07 02:58:56 -08:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
2013-02-04 02:56:12 -08:00
|
|
|
#include <netdb.h> // For gethostbyname.
|
2011-12-07 02:58:56 -08:00
|
|
|
|
2013-12-17 10:26:45 -08:00
|
|
|
#undef CHROMIUM_LOG
|
2011-12-07 02:58:56 -08:00
|
|
|
#if defined(MOZ_WIDGET_GONK)
|
|
|
|
#include <android/log.h>
|
2013-12-17 10:26:45 -08:00
|
|
|
#define CHROMIUM_LOG(args...) __android_log_print(ANDROID_LOG_INFO, "Gonk", args)
|
2011-12-07 02:58:56 -08:00
|
|
|
#else
|
2013-12-17 10:26:45 -08:00
|
|
|
#define CHROMIUM_LOG(args...) printf(args);
|
2011-12-07 02:58:56 -08:00
|
|
|
#endif
|
|
|
|
|
2013-02-01 04:28:22 -08:00
|
|
|
#include "jsfriendapi.h"
|
2014-01-30 10:26:54 -08:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2013-10-29 01:12:45 -07:00
|
|
|
#include "nsTArray.h"
|
2013-02-01 04:28:22 -08:00
|
|
|
#include "nsThreadUtils.h" // For NS_IsMainThread.
|
2011-12-12 12:39:17 -08:00
|
|
|
|
2013-02-01 04:28:22 -08:00
|
|
|
USING_WORKERS_NAMESPACE
|
|
|
|
using namespace mozilla::ipc;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
const char* RIL_SOCKET_NAME = "/dev/socket/rilproxy";
|
2011-12-07 02:58:56 -08:00
|
|
|
|
2011-12-08 23:12:32 -08:00
|
|
|
// Network port to connect to for adb forwarded sockets when doing
|
|
|
|
// desktop development.
|
|
|
|
const uint32_t RIL_TEST_PORT = 6200;
|
|
|
|
|
2013-10-29 01:12:45 -07:00
|
|
|
nsTArray<nsRefPtr<mozilla::ipc::RilConsumer> > sRilConsumers;
|
|
|
|
|
|
|
|
class ConnectWorkerToRIL : public WorkerTask
|
2011-12-07 02:58:56 -08:00
|
|
|
{
|
2012-04-01 01:57:21 -07:00
|
|
|
public:
|
2013-10-29 01:12:45 -07:00
|
|
|
ConnectWorkerToRIL()
|
2013-02-01 04:28:22 -08:00
|
|
|
{ }
|
2012-04-01 01:57:21 -07:00
|
|
|
|
2013-02-01 04:28:22 -08:00
|
|
|
virtual bool RunTask(JSContext *aCx);
|
2013-10-29 01:12:45 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
class SendRilSocketDataTask : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SendRilSocketDataTask(unsigned long aClientId,
|
|
|
|
UnixSocketRawData *aRawData)
|
|
|
|
: mRawData(aRawData)
|
|
|
|
, mClientId(aClientId)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (sRilConsumers.Length() <= mClientId ||
|
|
|
|
!sRilConsumers[mClientId] ||
|
|
|
|
sRilConsumers[mClientId]->GetConnectionStatus() != SOCKET_CONNECTED) {
|
|
|
|
// Probably shuting down.
|
|
|
|
delete mRawData;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
sRilConsumers[mClientId]->SendSocketData(mRawData);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
UnixSocketRawData *mRawData;
|
|
|
|
unsigned long mClientId;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool
|
|
|
|
PostToRIL(JSContext *aCx,
|
|
|
|
unsigned aArgc,
|
|
|
|
JS::Value *aArgv)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Expecting to be on the worker thread");
|
|
|
|
|
|
|
|
if (aArgc != 2) {
|
|
|
|
JS_ReportError(aCx, "Expecting two arguments with the RIL message");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Value cv = JS_ARGV(aCx, aArgv)[0];
|
|
|
|
int clientId = cv.toInt32();
|
|
|
|
|
|
|
|
JS::Value v = JS_ARGV(aCx, aArgv)[1];
|
|
|
|
|
|
|
|
JSAutoByteString abs;
|
|
|
|
void *data;
|
|
|
|
size_t size;
|
|
|
|
if (JSVAL_IS_STRING(v)) {
|
|
|
|
JSString *str = JSVAL_TO_STRING(v);
|
|
|
|
if (!abs.encodeUtf8(aCx, str)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
data = abs.ptr();
|
|
|
|
size = abs.length();
|
|
|
|
} else if (!JSVAL_IS_PRIMITIVE(v)) {
|
|
|
|
JSObject *obj = JSVAL_TO_OBJECT(v);
|
|
|
|
if (!JS_IsTypedArrayObject(obj)) {
|
|
|
|
JS_ReportError(aCx, "Object passed in wasn't a typed array");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t type = JS_GetArrayBufferViewType(obj);
|
|
|
|
if (type != js::ArrayBufferView::TYPE_INT8 &&
|
|
|
|
type != js::ArrayBufferView::TYPE_UINT8 &&
|
|
|
|
type != js::ArrayBufferView::TYPE_UINT8_CLAMPED) {
|
|
|
|
JS_ReportError(aCx, "Typed array data is not octets");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = JS_GetTypedArrayByteLength(obj);
|
|
|
|
data = JS_GetArrayBufferViewData(obj);
|
|
|
|
} else {
|
|
|
|
JS_ReportError(aCx,
|
|
|
|
"Incorrect argument. Expecting a string or a typed array");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnixSocketRawData* raw = new UnixSocketRawData(data, size);
|
|
|
|
|
|
|
|
nsRefPtr<SendRilSocketDataTask> task =
|
|
|
|
new SendRilSocketDataTask(clientId, raw);
|
|
|
|
NS_DispatchToMainThread(task);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ConnectWorkerToRIL::RunTask(JSContext *aCx)
|
|
|
|
{
|
|
|
|
// Set up the postRILMessage on the function for worker -> RIL thread
|
|
|
|
// communication.
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Expecting to be on the worker thread");
|
|
|
|
NS_ASSERTION(!JS_IsRunning(aCx), "Are we being called somehow?");
|
2014-01-17 10:10:17 -08:00
|
|
|
JS::Rooted<JSObject*> workerGlobal(aCx, JS::CurrentGlobalOrNull(aCx));
|
2013-10-29 01:12:45 -07:00
|
|
|
|
|
|
|
return !!JS_DefineFunction(aCx, workerGlobal,
|
2013-11-05 06:16:39 -08:00
|
|
|
"postRILMessage", PostToRIL, 2, 0);
|
2013-10-29 01:12:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
class DispatchRILEvent : public WorkerTask
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DispatchRILEvent(UnixSocketRawData* aMessage)
|
|
|
|
: mMessage(aMessage)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual bool RunTask(JSContext *aCx);
|
2012-04-01 01:57:21 -07:00
|
|
|
|
|
|
|
private:
|
2013-10-29 01:12:45 -07:00
|
|
|
nsAutoPtr<UnixSocketRawData> mMessage;
|
2011-12-08 23:12:32 -08:00
|
|
|
};
|
2012-09-25 21:07:39 -07:00
|
|
|
|
2013-02-01 04:28:22 -08:00
|
|
|
bool
|
|
|
|
DispatchRILEvent::RunTask(JSContext *aCx)
|
|
|
|
{
|
2014-02-11 02:59:16 -08:00
|
|
|
JS::Rooted<JSObject*> obj(aCx, JS::CurrentGlobalOrNull(aCx));
|
2011-12-07 02:58:56 -08:00
|
|
|
|
2013-02-01 04:28:22 -08:00
|
|
|
JSObject *array = JS_NewUint8Array(aCx, mMessage->mSize);
|
|
|
|
if (!array) {
|
|
|
|
return false;
|
2012-09-30 22:03:43 -07:00
|
|
|
}
|
2014-02-11 02:59:16 -08:00
|
|
|
JS::Rooted<JS::Value> arrayVal(aCx, JS::ObjectValue(*array));
|
2013-02-01 04:28:22 -08:00
|
|
|
|
|
|
|
memcpy(JS_GetArrayBufferViewData(array), mMessage->mData, mMessage->mSize);
|
2014-02-11 02:59:16 -08:00
|
|
|
JS::Rooted<JS::Value> rval(aCx);
|
2014-02-13 07:33:04 -08:00
|
|
|
return JS_CallFunctionName(aCx, obj, "onRILMessage", arrayVal, &rval);
|
2011-12-07 02:58:56 -08:00
|
|
|
}
|
|
|
|
|
2013-02-01 04:28:22 -08:00
|
|
|
class RilConnector : public mozilla::ipc::UnixSocketConnector
|
2011-12-07 02:58:56 -08:00
|
|
|
{
|
2013-02-01 04:28:22 -08:00
|
|
|
public:
|
2013-02-04 05:26:43 -08:00
|
|
|
RilConnector(unsigned long aClientId) : mClientId(aClientId)
|
|
|
|
{}
|
|
|
|
|
2013-02-01 04:28:22 -08:00
|
|
|
virtual ~RilConnector()
|
|
|
|
{}
|
|
|
|
|
|
|
|
virtual int Create();
|
2013-03-19 10:23:47 -07:00
|
|
|
virtual bool CreateAddr(bool aIsServer,
|
2013-02-01 04:28:22 -08:00
|
|
|
socklen_t& aAddrSize,
|
2013-03-19 10:23:47 -07:00
|
|
|
sockaddr_any& aAddr,
|
2013-02-01 04:28:22 -08:00
|
|
|
const char* aAddress);
|
|
|
|
virtual bool SetUp(int aFd);
|
2013-10-24 19:00:22 -07:00
|
|
|
virtual bool SetUpListenSocket(int aFd);
|
2013-03-19 10:23:47 -07:00
|
|
|
virtual void GetSocketAddr(const sockaddr_any& aAddr,
|
2013-02-01 04:28:22 -08:00
|
|
|
nsAString& aAddrStr);
|
2013-02-04 05:26:43 -08:00
|
|
|
|
|
|
|
private:
|
|
|
|
unsigned long mClientId;
|
2013-02-01 04:28:22 -08:00
|
|
|
};
|
2011-12-07 02:58:56 -08:00
|
|
|
|
2013-02-01 04:28:22 -08:00
|
|
|
int
|
|
|
|
RilConnector::Create()
|
2011-12-07 02:58:56 -08:00
|
|
|
{
|
2013-02-01 04:28:22 -08:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
|
|
|
|
int fd = -1;
|
2012-09-30 22:03:43 -07:00
|
|
|
|
2011-12-07 02:58:56 -08:00
|
|
|
#if defined(MOZ_WIDGET_GONK)
|
2013-02-01 04:28:22 -08:00
|
|
|
fd = socket(AF_LOCAL, SOCK_STREAM, 0);
|
2011-12-07 02:58:56 -08:00
|
|
|
#else
|
2013-03-19 10:23:47 -07:00
|
|
|
// If we can't hit a local loopback, fail later in connect.
|
|
|
|
fd = socket(AF_INET, SOCK_STREAM, 0);
|
2011-12-07 02:58:56 -08:00
|
|
|
#endif
|
|
|
|
|
2013-02-01 04:28:22 -08:00
|
|
|
if (fd < 0) {
|
|
|
|
NS_WARNING("Could not open ril socket!");
|
|
|
|
return -1;
|
2011-12-07 02:58:56 -08:00
|
|
|
}
|
|
|
|
|
2013-02-01 04:28:22 -08:00
|
|
|
if (!SetUp(fd)) {
|
|
|
|
NS_WARNING("Could not set up socket!");
|
2012-12-11 05:56:36 -08:00
|
|
|
}
|
2013-02-01 04:28:22 -08:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2013-03-19 10:23:47 -07:00
|
|
|
bool
|
2013-02-01 04:28:22 -08:00
|
|
|
RilConnector::CreateAddr(bool aIsServer,
|
|
|
|
socklen_t& aAddrSize,
|
2013-03-19 10:23:47 -07:00
|
|
|
sockaddr_any& aAddr,
|
2013-02-01 04:28:22 -08:00
|
|
|
const char* aAddress)
|
|
|
|
{
|
|
|
|
// We never open ril socket as server.
|
|
|
|
MOZ_ASSERT(!aIsServer);
|
2013-03-19 10:23:47 -07:00
|
|
|
uint32_t af;
|
2012-04-11 10:05:35 -07:00
|
|
|
#if defined(MOZ_WIDGET_GONK)
|
2013-03-19 10:23:47 -07:00
|
|
|
af = AF_LOCAL;
|
2013-02-01 04:28:22 -08:00
|
|
|
#else
|
2013-03-19 10:23:47 -07:00
|
|
|
af = AF_INET;
|
2013-03-15 21:35:25 -07:00
|
|
|
#endif
|
2013-03-19 10:23:47 -07:00
|
|
|
switch (af) {
|
|
|
|
case AF_LOCAL:
|
|
|
|
aAddr.un.sun_family = af;
|
|
|
|
if(strlen(aAddress) > sizeof(aAddr.un.sun_path)) {
|
|
|
|
NS_WARNING("Address too long for socket struct!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
strcpy((char*)&aAddr.un.sun_path, aAddress);
|
|
|
|
aAddrSize = strlen(aAddress) + offsetof(struct sockaddr_un, sun_path) + 1;
|
|
|
|
break;
|
|
|
|
case AF_INET:
|
|
|
|
aAddr.in.sin_family = af;
|
|
|
|
aAddr.in.sin_port = htons(RIL_TEST_PORT + mClientId);
|
2014-01-10 03:40:57 -08:00
|
|
|
aAddr.in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
2013-03-19 10:23:47 -07:00
|
|
|
aAddrSize = sizeof(sockaddr_in);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_WARNING("Socket type not handled by connector!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2011-12-07 02:58:56 -08:00
|
|
|
}
|
|
|
|
|
2013-02-01 04:28:22 -08:00
|
|
|
bool
|
|
|
|
RilConnector::SetUp(int aFd)
|
2011-12-07 02:58:56 -08:00
|
|
|
{
|
2013-02-01 04:28:22 -08:00
|
|
|
// Nothing to do here.
|
|
|
|
return true;
|
2011-12-07 02:58:56 -08:00
|
|
|
}
|
|
|
|
|
2013-10-24 19:00:22 -07:00
|
|
|
bool
|
|
|
|
RilConnector::SetUpListenSocket(int aFd)
|
|
|
|
{
|
|
|
|
// Nothing to do here.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-12-07 02:58:56 -08:00
|
|
|
void
|
2013-03-19 10:23:47 -07:00
|
|
|
RilConnector::GetSocketAddr(const sockaddr_any& aAddr,
|
2013-02-01 04:28:22 -08:00
|
|
|
nsAString& aAddrStr)
|
2011-12-07 02:58:56 -08:00
|
|
|
{
|
2013-06-28 18:38:30 -07:00
|
|
|
MOZ_CRASH("This should never be called!");
|
2011-12-07 02:58:56 -08:00
|
|
|
}
|
|
|
|
|
2013-02-01 04:28:22 -08:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
2011-12-07 02:58:56 -08:00
|
|
|
|
2013-02-04 05:26:43 -08:00
|
|
|
RilConsumer::RilConsumer(unsigned long aClientId,
|
|
|
|
WorkerCrossThreadDispatcher* aDispatcher)
|
2013-02-01 04:28:22 -08:00
|
|
|
: mDispatcher(aDispatcher)
|
2013-02-04 05:26:43 -08:00
|
|
|
, mClientId(aClientId)
|
2013-02-01 04:28:22 -08:00
|
|
|
, mShutdown(false)
|
2011-12-07 02:58:56 -08:00
|
|
|
{
|
2013-02-04 05:26:43 -08:00
|
|
|
// Only append client id after RIL_SOCKET_NAME when it's not connected to
|
|
|
|
// the first(0) rilproxy for compatibility.
|
|
|
|
if (!aClientId) {
|
|
|
|
mAddress = RIL_SOCKET_NAME;
|
|
|
|
} else {
|
|
|
|
struct sockaddr_un addr_un;
|
|
|
|
snprintf(addr_un.sun_path, sizeof addr_un.sun_path, "%s%lu",
|
|
|
|
RIL_SOCKET_NAME, aClientId);
|
|
|
|
mAddress = addr_un.sun_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConnectSocket(new RilConnector(mClientId), mAddress.get());
|
2011-12-07 02:58:56 -08:00
|
|
|
}
|
|
|
|
|
2013-10-29 01:12:45 -07:00
|
|
|
nsresult
|
|
|
|
RilConsumer::Register(unsigned int aClientId,
|
|
|
|
WorkerCrossThreadDispatcher* aDispatcher)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
sRilConsumers.EnsureLengthAtLeast(aClientId + 1);
|
|
|
|
|
|
|
|
if (sRilConsumers[aClientId]) {
|
|
|
|
NS_WARNING("RilConsumer already registered");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<ConnectWorkerToRIL> connection = new ConnectWorkerToRIL();
|
|
|
|
if (!aDispatcher->PostTask(connection)) {
|
|
|
|
NS_WARNING("Failed to connect worker to ril");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we're set up, connect ourselves to the RIL thread.
|
|
|
|
sRilConsumers[aClientId] = new RilConsumer(aClientId, aDispatcher);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-02-01 04:28:22 -08:00
|
|
|
void
|
|
|
|
RilConsumer::Shutdown()
|
2011-12-07 02:58:56 -08:00
|
|
|
{
|
2013-10-29 01:12:45 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
for (unsigned long i = 0; i < sRilConsumers.Length(); i++) {
|
|
|
|
nsRefPtr<RilConsumer>& instance = sRilConsumers[i];
|
|
|
|
if (!instance) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
instance->mShutdown = true;
|
|
|
|
instance->CloseSocket();
|
|
|
|
instance = nullptr;
|
|
|
|
}
|
2011-12-07 02:58:56 -08:00
|
|
|
}
|
|
|
|
|
2013-02-01 04:28:22 -08:00
|
|
|
void
|
|
|
|
RilConsumer::ReceiveSocketData(nsAutoPtr<UnixSocketRawData>& aMessage)
|
2011-12-07 02:58:56 -08:00
|
|
|
{
|
2013-02-01 04:28:22 -08:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2011-12-07 02:58:56 -08:00
|
|
|
|
2013-02-01 04:28:22 -08:00
|
|
|
nsRefPtr<DispatchRILEvent> dre(new DispatchRILEvent(aMessage.forget()));
|
|
|
|
mDispatcher->PostTask(dre);
|
2011-12-07 02:58:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-02-01 04:28:22 -08:00
|
|
|
RilConsumer::OnConnectSuccess()
|
2011-12-07 02:58:56 -08:00
|
|
|
{
|
2013-02-01 04:28:22 -08:00
|
|
|
// Nothing to do here.
|
2013-12-17 10:26:45 -08:00
|
|
|
CHROMIUM_LOG("RIL[%lu]: %s\n", mClientId, __FUNCTION__);
|
2013-02-01 04:28:22 -08:00
|
|
|
}
|
2011-12-07 02:58:56 -08:00
|
|
|
|
2013-02-01 04:28:22 -08:00
|
|
|
void
|
|
|
|
RilConsumer::OnConnectError()
|
|
|
|
{
|
2013-12-17 10:26:45 -08:00
|
|
|
CHROMIUM_LOG("RIL[%lu]: %s\n", mClientId, __FUNCTION__);
|
2013-02-01 04:28:22 -08:00
|
|
|
CloseSocket();
|
|
|
|
}
|
2011-12-07 02:58:56 -08:00
|
|
|
|
2013-02-01 04:28:22 -08:00
|
|
|
void
|
|
|
|
RilConsumer::OnDisconnect()
|
|
|
|
{
|
2013-12-17 10:26:45 -08:00
|
|
|
CHROMIUM_LOG("RIL[%lu]: %s\n", mClientId, __FUNCTION__);
|
2013-02-01 04:28:22 -08:00
|
|
|
if (!mShutdown) {
|
2013-02-04 05:26:43 -08:00
|
|
|
ConnectSocket(new RilConnector(mClientId), mAddress.get(), 1000);
|
2011-12-07 02:58:56 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace ipc
|
|
|
|
} // namespace mozilla
|