2015-01-07 13:19:59 +03:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04: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 14:58:56 +04:00
|
|
|
|
2013-10-29 12:12:45 +04:00
|
|
|
#include "mozilla/ipc/Ril.h"
|
|
|
|
|
2011-12-07 14:58:56 +04:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
2013-02-04 14:56:12 +04:00
|
|
|
#include <netdb.h> // For gethostbyname.
|
2011-12-07 14:58:56 +04:00
|
|
|
|
2013-12-17 22:26:45 +04:00
|
|
|
#undef CHROMIUM_LOG
|
2011-12-07 14:58:56 +04:00
|
|
|
#if defined(MOZ_WIDGET_GONK)
|
|
|
|
#include <android/log.h>
|
2013-12-17 22:26:45 +04:00
|
|
|
#define CHROMIUM_LOG(args...) __android_log_print(ANDROID_LOG_INFO, "Gonk", args)
|
2011-12-07 14:58:56 +04:00
|
|
|
#else
|
2013-12-17 22:26:45 +04:00
|
|
|
#define CHROMIUM_LOG(args...) printf(args);
|
2011-12-07 14:58:56 +04:00
|
|
|
#endif
|
|
|
|
|
2013-02-01 16:28:22 +04:00
|
|
|
#include "jsfriendapi.h"
|
2014-01-30 22:26:54 +04:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2013-10-29 12:12:45 +04:00
|
|
|
#include "nsTArray.h"
|
2013-02-01 16:28:22 +04:00
|
|
|
#include "nsThreadUtils.h" // For NS_IsMainThread.
|
2015-05-19 14:28:46 +03:00
|
|
|
#include "RilConnector.h"
|
2011-12-13 00:39:17 +04:00
|
|
|
|
2013-02-01 16:28:22 +04:00
|
|
|
USING_WORKERS_NAMESPACE
|
|
|
|
using namespace mozilla::ipc;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2015-01-07 13:19:59 +03:00
|
|
|
static const char RIL_SOCKET_NAME[] = "/dev/socket/rilproxy";
|
2011-12-07 14:58:56 +04:00
|
|
|
|
2015-05-26 14:24:20 +03:00
|
|
|
static nsTArray<nsAutoPtr<mozilla::ipc::RilConsumer>> sRilConsumers;
|
2013-10-29 12:12:45 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class ConnectWorkerToRIL final : public WorkerTask
|
2011-12-07 14:58:56 +04:00
|
|
|
{
|
2012-04-01 12:57:21 +04:00
|
|
|
public:
|
2015-03-21 19:28:04 +03:00
|
|
|
bool RunTask(JSContext* aCx) override;
|
2013-10-29 12:12:45 +04:00
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class SendRilSocketDataTask final : public nsRunnable
|
2013-10-29 12:12:45 +04:00
|
|
|
{
|
|
|
|
public:
|
2015-01-07 13:19:59 +03:00
|
|
|
SendRilSocketDataTask(unsigned long aClientId,
|
|
|
|
UnixSocketRawData* aRawData)
|
|
|
|
: mRawData(aRawData)
|
|
|
|
, mClientId(aClientId)
|
|
|
|
{ }
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD Run() override
|
2015-01-07 13:19:59 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-05-26 14:24:20 +03:00
|
|
|
if (sRilConsumers.Length() <= mClientId || !sRilConsumers[mClientId]) {
|
|
|
|
// Probably shutting down.
|
2015-01-07 13:19:59 +03:00
|
|
|
delete mRawData;
|
|
|
|
return NS_OK;
|
2013-10-29 12:12:45 +04:00
|
|
|
}
|
|
|
|
|
2015-05-26 14:24:20 +03:00
|
|
|
sRilConsumers[mClientId]->Send(mRawData);
|
2015-01-07 13:19:59 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-10-29 12:12:45 +04:00
|
|
|
private:
|
2015-01-07 13:19:59 +03:00
|
|
|
UnixSocketRawData* mRawData;
|
|
|
|
unsigned long mClientId;
|
2013-10-29 12:12:45 +04:00
|
|
|
};
|
|
|
|
|
2015-01-07 13:19:59 +03:00
|
|
|
static bool
|
2015-01-07 13:19:59 +03:00
|
|
|
PostToRIL(JSContext* aCx, unsigned aArgc, JS::Value* aVp)
|
2013-10-29 12:12:45 +04:00
|
|
|
{
|
2015-01-07 13:19:59 +03:00
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(aArgc, aVp);
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Expecting to be on the worker thread");
|
|
|
|
|
|
|
|
if (args.length() != 2) {
|
|
|
|
JS_ReportError(aCx, "Expecting two arguments with the RIL message");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int clientId = args[0].toInt32();
|
|
|
|
JS::Value v = args[1];
|
|
|
|
|
|
|
|
UnixSocketRawData* raw = nullptr;
|
2013-10-29 12:12:45 +04:00
|
|
|
|
2015-01-07 13:19:59 +03:00
|
|
|
if (v.isString()) {
|
|
|
|
JSAutoByteString abs;
|
|
|
|
JS::Rooted<JSString*> str(aCx, v.toString());
|
|
|
|
if (!abs.encodeUtf8(aCx, str)) {
|
|
|
|
return false;
|
2013-10-29 12:12:45 +04:00
|
|
|
}
|
|
|
|
|
2015-01-07 13:19:59 +03:00
|
|
|
raw = new UnixSocketRawData(abs.ptr(), abs.length());
|
|
|
|
} else if (!v.isPrimitive()) {
|
|
|
|
JSObject* obj = v.toObjectOrNull();
|
|
|
|
if (!JS_IsTypedArrayObject(obj)) {
|
|
|
|
JS_ReportError(aCx, "Object passed in wasn't a typed array");
|
|
|
|
return false;
|
2013-10-29 12:12:45 +04:00
|
|
|
}
|
|
|
|
|
2015-01-07 13:19:59 +03:00
|
|
|
uint32_t type = JS_GetArrayBufferViewType(obj);
|
|
|
|
if (type != js::Scalar::Int8 &&
|
|
|
|
type != js::Scalar::Uint8 &&
|
|
|
|
type != js::Scalar::Uint8Clamped) {
|
|
|
|
JS_ReportError(aCx, "Typed array data is not octets");
|
|
|
|
return false;
|
2014-10-07 21:44:07 +04:00
|
|
|
}
|
2013-10-29 12:12:45 +04:00
|
|
|
|
2015-01-07 13:19:59 +03:00
|
|
|
JS::AutoCheckCannotGC nogc;
|
|
|
|
size_t size = JS_GetTypedArrayByteLength(obj);
|
|
|
|
void* data = JS_GetArrayBufferViewData(obj, nogc);
|
|
|
|
raw = new UnixSocketRawData(data, size);
|
|
|
|
} else {
|
|
|
|
JS_ReportError(
|
|
|
|
aCx, "Incorrect argument. Expecting a string or a typed array");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!raw) {
|
|
|
|
JS_ReportError(aCx, "Unable to post to RIL");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<SendRilSocketDataTask> task = new SendRilSocketDataTask(clientId,
|
|
|
|
raw);
|
|
|
|
NS_DispatchToMainThread(task);
|
|
|
|
return true;
|
2013-10-29 12:12:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-01-07 13:19:59 +03:00
|
|
|
ConnectWorkerToRIL::RunTask(JSContext* aCx)
|
2013-10-29 12:12:45 +04:00
|
|
|
{
|
2015-01-07 13:19:59 +03:00
|
|
|
// 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?");
|
|
|
|
JS::Rooted<JSObject*> workerGlobal(aCx, JS::CurrentGlobalOrNull(aCx));
|
|
|
|
|
|
|
|
// Check whether |postRILMessage| has been defined. No one but this class
|
|
|
|
// should ever define |postRILMessage| in a RIL worker.
|
|
|
|
JS::Rooted<JS::Value> val(aCx);
|
|
|
|
if (!JS_GetProperty(aCx, workerGlobal, "postRILMessage", &val)) {
|
|
|
|
JS_ReportPendingException(aCx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that |postRILMessage| is a function.
|
|
|
|
if (JSTYPE_FUNCTION == JS_TypeOfValue(aCx, val)) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-02-17 15:35:01 +04:00
|
|
|
|
2015-01-07 13:19:59 +03:00
|
|
|
return !!JS_DefineFunction(aCx, workerGlobal, "postRILMessage",
|
|
|
|
PostToRIL, 2, 0);
|
2013-10-29 12:12:45 +04:00
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class DispatchRILEvent final : public WorkerTask
|
2013-10-29 12:12:45 +04:00
|
|
|
{
|
|
|
|
public:
|
2015-04-23 14:48:48 +03:00
|
|
|
DispatchRILEvent(unsigned long aClient, UnixSocketBuffer* aBuffer)
|
2015-01-07 13:19:59 +03:00
|
|
|
: mClientId(aClient)
|
2015-04-23 14:48:48 +03:00
|
|
|
, mBuffer(aBuffer)
|
2015-01-07 13:19:59 +03:00
|
|
|
{ }
|
2013-10-29 12:12:45 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
bool RunTask(JSContext* aCx) override;
|
2012-04-01 12:57:21 +04:00
|
|
|
|
|
|
|
private:
|
2015-01-07 13:19:59 +03:00
|
|
|
unsigned long mClientId;
|
2015-04-23 14:48:48 +03:00
|
|
|
nsAutoPtr<UnixSocketBuffer> mBuffer;
|
2011-12-09 11:12:32 +04:00
|
|
|
};
|
2012-09-26 08:07:39 +04:00
|
|
|
|
2013-02-01 16:28:22 +04:00
|
|
|
bool
|
2015-01-07 13:19:59 +03:00
|
|
|
DispatchRILEvent::RunTask(JSContext* aCx)
|
2013-02-01 16:28:22 +04:00
|
|
|
{
|
2015-01-07 13:19:59 +03:00
|
|
|
JS::Rooted<JSObject*> obj(aCx, JS::CurrentGlobalOrNull(aCx));
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> array(aCx,
|
2015-04-23 14:48:48 +03:00
|
|
|
JS_NewUint8Array(aCx, mBuffer->GetSize()));
|
2015-01-07 13:19:59 +03:00
|
|
|
if (!array) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
JS::AutoCheckCannotGC nogc;
|
|
|
|
memcpy(JS_GetArrayBufferViewData(array, nogc),
|
2015-04-23 14:48:48 +03:00
|
|
|
mBuffer->GetData(), mBuffer->GetSize());
|
2015-01-07 13:19:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
JS::AutoValueArray<2> args(aCx);
|
|
|
|
args[0].setNumber((uint32_t)mClientId);
|
|
|
|
args[1].setObject(*array);
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> rval(aCx);
|
|
|
|
return JS_CallFunctionName(aCx, obj, "onRILMessage", args, &rval);
|
2011-12-07 14:58:56 +04:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2013-02-01 16:28:22 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
2011-12-07 14:58:56 +04:00
|
|
|
|
2013-02-04 17:26:43 +04:00
|
|
|
RilConsumer::RilConsumer(unsigned long aClientId,
|
|
|
|
WorkerCrossThreadDispatcher* aDispatcher)
|
2015-01-07 13:19:59 +03:00
|
|
|
: mDispatcher(aDispatcher)
|
|
|
|
, mShutdown(false)
|
2011-12-07 14:58:56 +04:00
|
|
|
{
|
2015-01-07 13:19:59 +03: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;
|
|
|
|
}
|
|
|
|
|
2015-05-26 14:24:20 +03:00
|
|
|
mSocket = new StreamSocket(this, aClientId);
|
|
|
|
mSocket->Connect(new RilConnector(mAddress, aClientId));
|
2011-12-07 14:58:56 +04:00
|
|
|
}
|
|
|
|
|
2013-10-29 12:12:45 +04:00
|
|
|
nsresult
|
|
|
|
RilConsumer::Register(unsigned int aClientId,
|
|
|
|
WorkerCrossThreadDispatcher* aDispatcher)
|
|
|
|
{
|
2015-01-07 13:19:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-10-29 12:12:45 +04:00
|
|
|
|
2015-01-07 13:19:59 +03:00
|
|
|
sRilConsumers.EnsureLengthAtLeast(aClientId + 1);
|
2013-10-29 12:12:45 +04:00
|
|
|
|
2015-01-07 13:19:59 +03:00
|
|
|
if (sRilConsumers[aClientId]) {
|
|
|
|
NS_WARNING("RilConsumer already registered");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2013-10-29 12:12:45 +04:00
|
|
|
|
2015-01-07 13:19:59 +03:00
|
|
|
nsRefPtr<ConnectWorkerToRIL> connection = new ConnectWorkerToRIL();
|
|
|
|
if (!aDispatcher->PostTask(connection)) {
|
|
|
|
NS_WARNING("Failed to connect worker to ril");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2013-10-29 12:12:45 +04:00
|
|
|
|
2015-01-07 13:19:59 +03:00
|
|
|
// Now that we're set up, connect ourselves to the RIL thread.
|
|
|
|
sRilConsumers[aClientId] = new RilConsumer(aClientId, aDispatcher);
|
|
|
|
return NS_OK;
|
2013-10-29 12:12:45 +04:00
|
|
|
}
|
|
|
|
|
2013-02-01 16:28:22 +04:00
|
|
|
void
|
|
|
|
RilConsumer::Shutdown()
|
2011-12-07 14:58:56 +04:00
|
|
|
{
|
2015-01-07 13:19:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-10-29 12:12:45 +04:00
|
|
|
|
2015-01-07 13:19:59 +03:00
|
|
|
for (unsigned long i = 0; i < sRilConsumers.Length(); i++) {
|
2015-05-26 14:24:20 +03:00
|
|
|
nsAutoPtr<RilConsumer> instance(sRilConsumers[i]);
|
2015-01-07 13:19:59 +03:00
|
|
|
if (!instance) {
|
|
|
|
continue;
|
2013-10-29 12:12:45 +04:00
|
|
|
}
|
2015-01-07 13:19:59 +03:00
|
|
|
|
|
|
|
instance->mShutdown = true;
|
2015-01-07 13:19:59 +03:00
|
|
|
instance->Close();
|
2015-01-07 13:19:59 +03:00
|
|
|
instance = nullptr;
|
|
|
|
}
|
2011-12-07 14:58:56 +04:00
|
|
|
}
|
|
|
|
|
2013-02-01 16:28:22 +04:00
|
|
|
void
|
2015-05-26 14:24:20 +03:00
|
|
|
RilConsumer::Send(UnixSocketRawData* aRawData)
|
|
|
|
{
|
|
|
|
if (!mSocket || mSocket->GetConnectionStatus() != SOCKET_CONNECTED) {
|
|
|
|
// Probably shutting down.
|
|
|
|
delete aRawData;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mSocket->SendSocketData(aRawData);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RilConsumer::Close()
|
|
|
|
{
|
2015-06-05 10:18:07 +03:00
|
|
|
if (mSocket) {
|
|
|
|
mSocket->Close();
|
|
|
|
mSocket = nullptr;
|
|
|
|
}
|
2015-05-26 14:24:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// |StreamSocketConnector|
|
|
|
|
|
|
|
|
void
|
|
|
|
RilConsumer::ReceiveSocketData(int aIndex,
|
|
|
|
nsAutoPtr<UnixSocketBuffer>& aBuffer)
|
2011-12-07 14:58:56 +04:00
|
|
|
{
|
2015-01-07 13:19:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2011-12-07 14:58:56 +04:00
|
|
|
|
2015-05-26 14:24:20 +03:00
|
|
|
nsRefPtr<DispatchRILEvent> dre(new DispatchRILEvent(aIndex, aBuffer.forget()));
|
2015-01-07 13:19:59 +03:00
|
|
|
mDispatcher->PostTask(dre);
|
2011-12-07 14:58:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-05-26 14:24:20 +03:00
|
|
|
RilConsumer::OnConnectSuccess(int aIndex)
|
2011-12-07 14:58:56 +04:00
|
|
|
{
|
2015-01-07 13:19:59 +03:00
|
|
|
// Nothing to do here.
|
2015-05-26 14:24:20 +03:00
|
|
|
CHROMIUM_LOG("RIL[%d]: %s\n", aIndex, __FUNCTION__);
|
2013-02-01 16:28:22 +04:00
|
|
|
}
|
2011-12-07 14:58:56 +04:00
|
|
|
|
2013-02-01 16:28:22 +04:00
|
|
|
void
|
2015-05-26 14:24:20 +03:00
|
|
|
RilConsumer::OnConnectError(int aIndex)
|
2013-02-01 16:28:22 +04:00
|
|
|
{
|
2015-05-26 14:24:20 +03:00
|
|
|
CHROMIUM_LOG("RIL[%d]: %s\n", aIndex, __FUNCTION__);
|
2015-01-07 13:19:59 +03:00
|
|
|
Close();
|
2013-02-01 16:28:22 +04:00
|
|
|
}
|
2011-12-07 14:58:56 +04:00
|
|
|
|
2013-02-01 16:28:22 +04:00
|
|
|
void
|
2015-05-26 14:24:20 +03:00
|
|
|
RilConsumer::OnDisconnect(int aIndex)
|
2013-02-01 16:28:22 +04:00
|
|
|
{
|
2015-05-26 14:24:20 +03:00
|
|
|
CHROMIUM_LOG("RIL[%d]: %s\n", aIndex, __FUNCTION__);
|
2015-05-20 11:50:43 +03:00
|
|
|
if (mShutdown) {
|
|
|
|
return;
|
2015-01-07 13:19:59 +03:00
|
|
|
}
|
2015-05-26 14:24:20 +03:00
|
|
|
mSocket->Connect(new RilConnector(mAddress, aIndex),
|
|
|
|
mSocket->GetSuggestedConnectDelayMs());
|
2011-12-07 14:58:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace ipc
|
|
|
|
} // namespace mozilla
|