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
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
|
2012-04-01 01:57:21 -07:00
|
|
|
#undef LOG
|
2011-12-07 02:58:56 -08:00
|
|
|
#if defined(MOZ_WIDGET_GONK)
|
|
|
|
#include <android/log.h>
|
|
|
|
#define LOG(args...) __android_log_print(ANDROID_LOG_INFO, "Gonk", args)
|
|
|
|
#else
|
|
|
|
#define LOG(args...) printf(args);
|
|
|
|
#endif
|
|
|
|
|
2013-02-01 04:28:22 -08:00
|
|
|
#include "jsfriendapi.h"
|
|
|
|
#include "nsThreadUtils.h" // For NS_IsMainThread.
|
|
|
|
#include "Ril.h"
|
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-02-01 04:28:22 -08:00
|
|
|
class DispatchRILEvent : public WorkerTask
|
2011-12-07 02:58:56 -08:00
|
|
|
{
|
2012-04-01 01:57:21 -07:00
|
|
|
public:
|
2013-02-01 04:28:22 -08:00
|
|
|
DispatchRILEvent(UnixSocketRawData* aMessage)
|
|
|
|
: mMessage(aMessage)
|
|
|
|
{ }
|
2012-04-01 01:57:21 -07:00
|
|
|
|
2013-02-01 04:28:22 -08:00
|
|
|
virtual bool RunTask(JSContext *aCx);
|
2012-04-01 01:57:21 -07:00
|
|
|
|
|
|
|
private:
|
2013-02-01 04:28:22 -08: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)
|
|
|
|
{
|
|
|
|
JSObject *obj = JS_GetGlobalObject(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
|
|
|
}
|
2013-02-01 04:28:22 -08:00
|
|
|
|
|
|
|
memcpy(JS_GetArrayBufferViewData(array), mMessage->mData, mMessage->mSize);
|
|
|
|
jsval argv[] = { OBJECT_TO_JSVAL(array) };
|
|
|
|
return JS_CallFunctionName(aCx, obj, "onRILMessage", NS_ARRAY_LENGTH(argv),
|
|
|
|
argv, argv);
|
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:
|
|
|
|
virtual ~RilConnector()
|
|
|
|
{}
|
|
|
|
|
|
|
|
virtual int Create();
|
|
|
|
virtual void CreateAddr(bool aIsServer,
|
|
|
|
socklen_t& aAddrSize,
|
|
|
|
struct sockaddr *aAddr,
|
|
|
|
const char* aAddress);
|
|
|
|
virtual bool SetUp(int aFd);
|
|
|
|
virtual void GetSocketAddr(const sockaddr& aAddr,
|
|
|
|
nsAString& aAddrStr);
|
|
|
|
};
|
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
|
|
|
|
struct hostent *hp;
|
|
|
|
|
|
|
|
hp = gethostbyname("localhost");
|
2013-02-01 04:28:22 -08:00
|
|
|
if (hp) {
|
|
|
|
fd = socket(hp->h_addrtype, 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RilConnector::CreateAddr(bool aIsServer,
|
|
|
|
socklen_t& aAddrSize,
|
|
|
|
struct sockaddr *aAddr,
|
|
|
|
const char* aAddress)
|
|
|
|
{
|
|
|
|
// We never open ril socket as server.
|
|
|
|
MOZ_ASSERT(!aIsServer);
|
2012-12-11 05:56:36 -08:00
|
|
|
|
2012-04-11 10:05:35 -07:00
|
|
|
#if defined(MOZ_WIDGET_GONK)
|
2013-02-01 04:28:22 -08:00
|
|
|
struct sockaddr_un addr_un;
|
2011-12-07 02:58:56 -08:00
|
|
|
|
2013-02-01 04:28:22 -08:00
|
|
|
memset(&addr_un, 0, sizeof(addr_un));
|
|
|
|
strcpy(addr_un.sun_path, aAddress);
|
|
|
|
addr_un.sun_family = AF_LOCAL;
|
2011-12-07 02:58:56 -08:00
|
|
|
|
2013-02-01 04:28:22 -08:00
|
|
|
aAddrSize = strlen(aAddress) + offsetof(struct sockaddr_un, sun_path) + 1;
|
|
|
|
memcpy(aAddr, &addr_un, aAddrSize);
|
|
|
|
#else
|
|
|
|
struct hostent *hp;
|
|
|
|
struct sockaddr_in addr_in;
|
2011-12-07 02:58:56 -08:00
|
|
|
|
2013-02-01 04:28:22 -08:00
|
|
|
hp = gethostbyname("localhost");
|
|
|
|
if (!hp) {
|
|
|
|
return;
|
2011-12-07 02:58:56 -08:00
|
|
|
}
|
2013-02-01 04:28:22 -08:00
|
|
|
|
|
|
|
memset(&addr_in, 0, sizeof(addr_in));
|
|
|
|
addr_in.sin_family = hp->h_addrtype;
|
2013-02-02 00:01:25 -08:00
|
|
|
addr_in.sin_port = htons(RIL_TEST_PORT);
|
2013-02-01 04:28:22 -08:00
|
|
|
memcpy(&addr_in.sin_addr, hp->h_addr, hp->h_length);
|
|
|
|
|
|
|
|
aAddrSize = sizeof(addr_in);
|
|
|
|
memcpy(aAddr, &addr_in, aAddrSize);
|
|
|
|
#endif
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-02-01 04:28:22 -08:00
|
|
|
RilConnector::GetSocketAddr(const sockaddr& aAddr,
|
|
|
|
nsAString& aAddrStr)
|
2011-12-07 02:58:56 -08:00
|
|
|
{
|
2013-02-01 04:28:22 -08:00
|
|
|
// Unused.
|
|
|
|
MOZ_NOT_REACHED("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-02 00:01:25 -08:00
|
|
|
RilConsumer::RilConsumer(WorkerCrossThreadDispatcher* aDispatcher)
|
2013-02-01 04:28:22 -08:00
|
|
|
: mDispatcher(aDispatcher)
|
|
|
|
, mShutdown(false)
|
2011-12-07 02:58:56 -08:00
|
|
|
{
|
2013-02-02 00:01:25 -08:00
|
|
|
ConnectSocket(new RilConnector(), RIL_SOCKET_NAME);
|
2011-12-07 02:58:56 -08:00
|
|
|
}
|
|
|
|
|
2013-02-01 04:28:22 -08:00
|
|
|
void
|
|
|
|
RilConsumer::Shutdown()
|
2011-12-07 02:58:56 -08:00
|
|
|
{
|
2013-02-01 04:28:22 -08:00
|
|
|
mShutdown = true;
|
|
|
|
CloseSocket();
|
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.
|
|
|
|
LOG("Socket open for RIL\n");
|
|
|
|
}
|
2011-12-07 02:58:56 -08:00
|
|
|
|
2013-02-01 04:28:22 -08:00
|
|
|
void
|
|
|
|
RilConsumer::OnConnectError()
|
|
|
|
{
|
|
|
|
LOG("%s\n", __FUNCTION__);
|
|
|
|
CloseSocket();
|
|
|
|
}
|
2011-12-07 02:58:56 -08:00
|
|
|
|
2013-02-01 04:28:22 -08:00
|
|
|
void
|
|
|
|
RilConsumer::OnDisconnect()
|
|
|
|
{
|
|
|
|
LOG("%s\n", __FUNCTION__);
|
|
|
|
if (!mShutdown) {
|
2013-02-02 00:01:25 -08:00
|
|
|
ConnectSocket(new RilConnector(), RIL_SOCKET_NAME, 1000);
|
2011-12-07 02:58:56 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace ipc
|
|
|
|
} // namespace mozilla
|