Merge pull request #14 from monich/registration

Support for service registration notifications
This commit is contained in:
Slava Monich
2018-11-17 01:47:28 +02:00
committed by GitHub
7 changed files with 556 additions and 90 deletions

View File

@@ -13,9 +13,9 @@
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Jolla Ltd nor the names of its contributors may
* be used to endorse or promote products derived from this software
* without specific prior written permission.
* 3. Neither the names of the copyright holders nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
@@ -61,6 +61,13 @@ void
int status,
void* user_data);
typedef
void
(*GBinderServiceManagerRegistrationFunc)(
GBinderServiceManager* sm,
const char* name,
void* user_data);
GBinderServiceManager*
gbinder_servicemanager_new(
const char* dev);
@@ -130,6 +137,18 @@ gbinder_servicemanager_cancel(
GBinderServiceManager* sm,
gulong id);
gulong
gbinder_servicemanager_add_registration_handler(
GBinderServiceManager* sm,
const char* name,
GBinderServiceManagerRegistrationFunc func,
void* user_data); /* Since 1.0.13 */
void
gbinder_servicemanager_remove_handler(
GBinderServiceManager* sm,
gulong id); /* Since 1.0.13 */
G_END_DECLS
#endif /* GBINDER_SERVICEMANAGER_H */

View File

@@ -6,9 +6,9 @@ Group: Development/Libraries
License: BSD
URL: https://github.com/mer-hybris/libgbinder
Source: %{name}-%{version}.tar.bz2
Requires: libglibutil >= 1.0.29
Requires: libglibutil >= 1.0.34
BuildRequires: pkgconfig(glib-2.0)
BuildRequires: pkgconfig(libglibutil) >= 1.0.29
BuildRequires: pkgconfig(libglibutil) >= 1.0.34
Requires(post): /sbin/ldconfig
Requires(postun): /sbin/ldconfig

View File

@@ -151,6 +151,17 @@ gbinder_defaultservicemanager_add_service(
return status;
}
static
GBINDER_SERVICEMANAGER_NAME_CHECK
gbinder_defaultservicemanager_check_name(
GBinderServiceManager* self,
const char* name)
{
/* Old servicemanager doesn't support notifications, those would
* have to be emulated with polling. Is it necessary though? */
return GBINDER_SERVICEMANAGER_NAME_INVALID;
}
static
void
gbinder_defaultservicemanager_init(
@@ -171,6 +182,8 @@ gbinder_defaultservicemanager_class_init(
klass->list = gbinder_defaultservicemanager_list;
klass->get_service = gbinder_defaultservicemanager_get_service;
klass->add_service = gbinder_defaultservicemanager_add_service;
klass->check_name = gbinder_defaultservicemanager_check_name;
/* No need for other watch callbacks */
}
/*

View File

@@ -35,20 +35,36 @@
#include "gbinder_log.h"
#include <gbinder_client.h>
#include <gbinder_local_object.h>
#include <gbinder_local_request.h>
#include <gbinder_remote_reply.h>
#include <gbinder_remote_request.h>
#include <gbinder_reader.h>
#include <errno.h>
#include <pthread.h>
typedef GBinderServiceManager GBinderHwServiceManager;
typedef struct gbinder_hwservicemanager_watch {
char* name;
GBinderLocalObject* callback;
} GBinderHwServiceManagerWatch;
typedef GBinderServiceManagerClass GBinderHwServiceManagerClass;
typedef struct gbinder_hwservicemanager {
GBinderServiceManager manager;
GHashTable* watch_table;
} GBinderHwServiceManager;
G_DEFINE_TYPE(GBinderHwServiceManager,
gbinder_hwservicemanager,
GBINDER_TYPE_SERVICEMANAGER)
#define PARENT_CLASS gbinder_hwservicemanager_parent_class
#define GBINDER_TYPE_HWSERVICEMANAGER (gbinder_hwservicemanager_get_type())
#define GBINDER_HWSERVICEMANAGER(obj) \
G_TYPE_CHECK_INSTANCE_CAST((obj), GBINDER_TYPE_HWSERVICEMANAGER, \
GBinderHwServiceManager)
enum gbinder_hwservicemanager_calls {
GET_TRANSACTION = GBINDER_FIRST_CALL_TRANSACTION,
ADD_TRANSACTION,
@@ -60,9 +76,76 @@ enum gbinder_hwservicemanager_calls {
REGISTER_PASSTHROUGH_CLIENT_TRANSACTION
};
enum gbinder_hwservicemanager_notifications {
ON_REGISTRATION_TRANSACTION = GBINDER_FIRST_CALL_TRANSACTION
};
/* As a special case, ServiceManager's handle is zero */
#define HWSERVICEMANAGER_HANDLE (0)
#define HWSERVICEMANAGER_IFACE "android.hidl.manager@1.0::IServiceManager"
#define HWSERVICEMANAGER_NOTIFICATION_IFACE \
"android.hidl.manager@1.0::IServiceNotification"
static
void
gbinder_hwservicemanager_handle_registration(
GBinderHwServiceManager* self,
GBinderReader* reader)
{
char* fqname = gbinder_reader_read_hidl_string(reader);
char* name = gbinder_reader_read_hidl_string(reader);
gboolean preexisting;
/* (string fqName, string name, bool preexisting) */
if (fqname && name && gbinder_reader_read_bool(reader, &preexisting) &&
gbinder_reader_at_end(reader)) {
char* full_name = g_strconcat(fqname, "/", name, NULL);
GDEBUG("%s %s", full_name, preexisting ? "true" : "false");
gbinder_servicemanager_service_registered(&self->manager, full_name);
g_free(full_name);
} else {
GWARN("Failed to parse IServiceNotification::onRegistration payload");
}
g_free(fqname);
g_free(name);
}
static
GBinderLocalReply*
gbinder_hwservicemanager_notification(
GBinderLocalObject* obj,
GBinderRemoteRequest* req,
guint code,
guint flags,
int* status,
void* user_data)
{
GBinderHwServiceManager* self = GBINDER_HWSERVICEMANAGER(user_data);
const char* iface = gbinder_remote_request_interface(req);
if (!g_strcmp0(iface, HWSERVICEMANAGER_NOTIFICATION_IFACE)) {
GBinderReader reader;
gbinder_remote_request_init_reader(req, &reader);
switch (code) {
case ON_REGISTRATION_TRANSACTION:
GDEBUG(HWSERVICEMANAGER_NOTIFICATION_IFACE " %u onRegistration",
code);
gbinder_hwservicemanager_handle_registration(self, &reader);
*status = GBINDER_STATUS_OK;
break;
default:
GDEBUG(HWSERVICEMANAGER_NOTIFICATION_IFACE " %u", code);
*status = GBINDER_STATUS_FAILED;
break;
}
} else {
GDEBUG("%s %u", iface, code);
*status = GBINDER_STATUS_FAILED;
}
return NULL;
}
GBinderServiceManager*
gbinder_hwservicemanager_new(
@@ -75,7 +158,7 @@ gbinder_hwservicemanager_new(
static
char**
gbinder_hwservicemanager_list(
GBinderHwServiceManager* self)
GBinderServiceManager* self)
{
GBinderLocalRequest* req = gbinder_client_new_request(self->client);
GBinderRemoteReply* reply = gbinder_client_transact_sync_reply
@@ -111,6 +194,7 @@ gbinder_hwservicemanager_get_service(
/* e.g. "android.hardware.radio@1.1::IRadio/slot1" */
const char* sep = strchr(fqinstance, '/');
GBinderRemoteObject* obj = NULL;
if (sep) {
GBinderRemoteReply* reply;
GBinderLocalRequest* req = gbinder_client_new_request(self->client);
@@ -170,11 +254,123 @@ gbinder_hwservicemanager_add_service(
return status;
}
static
void
gbinder_hwservicemanager_watch_free(
gpointer data)
{
GBinderHwServiceManagerWatch* watch = data;
g_free(watch->name);
gbinder_local_object_drop(watch->callback);
g_free(watch);
}
static
GBINDER_SERVICEMANAGER_NAME_CHECK
gbinder_hwservicemanager_check_name(
GBinderServiceManager* self,
const char* name)
{
if (name) {
const gsize len = strlen(name);
static const char allowed_chars[] = "./0123456789:@"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
if (len && strspn(name, allowed_chars) == len) {
return strchr(name, '/') ?
GBINDER_SERVICEMANAGER_NAME_NORMALIZE :
GBINDER_SERVICEMANAGER_NAME_OK;
}
}
return GBINDER_SERVICEMANAGER_NAME_INVALID;
}
static
char*
gbinder_hwservicemanager_normalize_name(
GBinderServiceManager* self,
const char* name)
{
/* Slash must be there, see gbinder_hwservicemanager_check_name() above */
return g_strndup(name, strchr(name, '/') - name);
}
static
gboolean
gbinder_hwservicemanager_watch(
GBinderServiceManager* manager,
const char* name)
{
GBinderHwServiceManager* self = GBINDER_HWSERVICEMANAGER(manager);
GBinderLocalRequest* req = gbinder_client_new_request(manager->client);
GBinderRemoteReply* reply;
GBinderHwServiceManagerWatch* watch =
g_new0(GBinderHwServiceManagerWatch, 1);
gboolean success = FALSE;
int status;
watch->name = g_strdup(name);
watch->callback = gbinder_servicemanager_new_local_object(manager,
HWSERVICEMANAGER_NOTIFICATION_IFACE,
gbinder_hwservicemanager_notification, self);
g_hash_table_replace(self->watch_table, watch->name, watch);
/* registerForNotifications(string fqName, string name,
* IServiceNotification callback) generates (bool success); */
gbinder_local_request_append_hidl_string(req, name);
gbinder_local_request_append_hidl_string(req, "");
gbinder_local_request_append_local_object(req, watch->callback);
reply = gbinder_client_transact_sync_reply(manager->client,
REGISTER_FOR_NOTIFICATIONS_TRANSACTION, req, &status);
if (status == GBINDER_STATUS_OK && reply) {
GBinderReader reader;
gbinder_remote_reply_init_reader(reply, &reader);
if (gbinder_reader_read_int32(&reader, &status) &&
status == GBINDER_STATUS_OK) {
gbinder_reader_read_bool(&reader, &success);
}
}
gbinder_remote_reply_unref(reply);
gbinder_local_request_unref(req);
if (!success) {
/* unwatch() won't be called if we return FALSE */
g_hash_table_remove(self->watch_table, watch->name);
}
return success;
}
static
void
gbinder_hwservicemanager_unwatch(
GBinderServiceManager* manager,
const char* name)
{
g_hash_table_remove(GBINDER_HWSERVICEMANAGER(manager)->watch_table, name);
}
static
void
gbinder_hwservicemanager_init(
GBinderHwServiceManager* self)
{
self->watch_table = g_hash_table_new_full(g_str_hash, g_str_equal,
NULL, gbinder_hwservicemanager_watch_free);
}
static
void
gbinder_hwservicemanager_finalize(
GObject* object)
{
GBinderHwServiceManager* self = GBINDER_HWSERVICEMANAGER(object);
g_hash_table_destroy(self->watch_table);
G_OBJECT_CLASS(PARENT_CLASS)->finalize(object);
}
static
@@ -190,6 +386,11 @@ gbinder_hwservicemanager_class_init(
klass->list = gbinder_hwservicemanager_list;
klass->get_service = gbinder_hwservicemanager_get_service;
klass->add_service = gbinder_hwservicemanager_add_service;
klass->check_name = gbinder_hwservicemanager_check_name;
klass->normalize_name = gbinder_hwservicemanager_normalize_name;
klass->watch = gbinder_hwservicemanager_watch;
klass->unwatch = gbinder_hwservicemanager_unwatch;
G_OBJECT_CLASS(klass)->finalize = gbinder_hwservicemanager_finalize;
}
/*

View File

@@ -44,6 +44,17 @@
#include <errno.h>
typedef struct gbinder_servicemanager_watch {
char* name;
char* detail;
GQuark quark;
gboolean watched;
} GBinderServiceManagerWatch;
struct gbinder_servicemanager_priv {
GHashTable* watch_table;
};
G_DEFINE_ABSTRACT_TYPE(GBinderServiceManager, gbinder_servicemanager,
G_TYPE_OBJECT)
@@ -60,6 +71,16 @@ G_DEFINE_ABSTRACT_TYPE(GBinderServiceManager, gbinder_servicemanager,
#define GBINDER_IS_SERVICEMANAGER_TYPE(klass) \
G_TYPE_CHECK_CLASS_TYPE(klass, GBINDER_TYPE_SERVICEMANAGER)
enum gbinder_servicemanager_signal {
SIGNAL_REGISTRATION,
SIGNAL_COUNT
};
static const char SIGNAL_REGISTRATION_NAME[] = "servicemanager-registration";
#define DETAIL_LEN 32
static guint gbinder_servicemanager_signals[SIGNAL_COUNT] = { 0 };
/*==========================================================================*
* Implementation
*==========================================================================*/
@@ -81,53 +102,29 @@ gbinder_servicemanager_class_ref(
return NULL;
}
GBinderServiceManager*
gbinder_servicemanager_new_with_type(
GType type,
const char* dev)
static
GBinderServiceManagerWatch*
gbinder_servicemanager_watch_new(
const char* name)
{
GBinderServiceManager* self = NULL;
GBinderServiceManagerClass* klass = gbinder_servicemanager_class_ref(type);
GBinderServiceManagerWatch* watch = g_new0(GBinderServiceManagerWatch, 1);
if (klass) {
GBinderIpc* ipc;
watch->name = g_strdup(name);
watch->detail = g_compute_checksum_for_string(G_CHECKSUM_MD5, name, -1);
watch->quark = g_quark_from_string(watch->detail);
return watch;
}
if (!dev) dev = klass->default_device;
ipc = gbinder_ipc_new(dev, klass->rpc_protocol);
if (ipc) {
GBinderRemoteObject* object = gbinder_ipc_get_remote_object
(ipc, klass->handle);
static
void
gbinder_servicemanager_watch_free(
gpointer data)
{
GBinderServiceManagerWatch* watch = data;
if (object) {
/* Lock */
g_mutex_lock(&klass->mutex);
if (klass->table) {
self = g_hash_table_lookup(klass->table, dev);
}
if (self) {
gbinder_servicemanager_ref(self);
} else {
char* key = g_strdup(dev); /* Owned by the hashtable */
GVERBOSE_("%s", dev);
self = g_object_new(type, NULL);
self->client = gbinder_client_new(object, klass->iface);
self->dev = gbinder_remote_object_dev(object);
if (!klass->table) {
klass->table = g_hash_table_new_full(g_str_hash,
g_str_equal, g_free, NULL);
}
g_hash_table_replace(klass->table, key, self);
}
g_mutex_unlock(&klass->mutex);
/* Unlock */
gbinder_remote_object_unref(object);
}
gbinder_ipc_unref(ipc);
}
g_type_class_unref(klass);
}
return self;
g_free(watch->name);
g_free(watch->detail);
g_free(watch);
}
typedef struct gbinder_servicemanager_list_tx_data {
@@ -256,6 +253,89 @@ gbinder_servicemanager_add_service_tx_free(
g_slice_free(GBinderServiceManagerAddServiceTxData, data);
}
/*==========================================================================*
* Internal interface
*==========================================================================*/
GBinderServiceManager*
gbinder_servicemanager_new_with_type(
GType type,
const char* dev)
{
GBinderServiceManager* self = NULL;
GBinderServiceManagerClass* klass = gbinder_servicemanager_class_ref(type);
if (klass) {
GBinderIpc* ipc;
if (!dev) dev = klass->default_device;
ipc = gbinder_ipc_new(dev, klass->rpc_protocol);
if (ipc) {
GBinderRemoteObject* object = gbinder_ipc_get_remote_object
(ipc, klass->handle);
if (object) {
/* Lock */
g_mutex_lock(&klass->mutex);
if (klass->table) {
self = g_hash_table_lookup(klass->table, dev);
}
if (self) {
gbinder_servicemanager_ref(self);
} else {
char* key = g_strdup(dev); /* Owned by the hashtable */
GVERBOSE_("%s", dev);
self = g_object_new(type, NULL);
self->client = gbinder_client_new(object, klass->iface);
self->dev = gbinder_remote_object_dev(object);
if (!klass->table) {
klass->table = g_hash_table_new_full(g_str_hash,
g_str_equal, g_free, NULL);
}
g_hash_table_replace(klass->table, key, self);
}
g_mutex_unlock(&klass->mutex);
/* Unlock */
gbinder_remote_object_unref(object);
}
gbinder_ipc_unref(ipc);
}
g_type_class_unref(klass);
}
return self;
}
void
gbinder_servicemanager_service_registered(
GBinderServiceManager* self,
const char* name)
{
GBinderServiceManagerClass* klass = GBINDER_SERVICEMANAGER_GET_CLASS(self);
GBinderServiceManagerPriv* priv = self->priv;
GBinderServiceManagerWatch* watch = NULL;
const char* normalized_name;
char* tmp_name = NULL;
switch (klass->check_name(self, name)) {
case GBINDER_SERVICEMANAGER_NAME_OK:
normalized_name = name;
break;
case GBINDER_SERVICEMANAGER_NAME_NORMALIZE:
normalized_name = tmp_name = klass->normalize_name(self, name);
break;
default:
normalized_name = NULL;
break;
}
if (normalized_name) {
watch = g_hash_table_lookup(priv->watch_table, normalized_name);
}
g_free(tmp_name);
g_signal_emit(self, gbinder_servicemanager_signals[SIGNAL_REGISTRATION],
watch ? watch->quark : 0, name);
}
/*==========================================================================*
* Interface
*==========================================================================*/
@@ -433,6 +513,88 @@ gbinder_servicemanager_cancel(
}
}
gulong
gbinder_servicemanager_add_registration_handler(
GBinderServiceManager* self,
const char* name,
GBinderServiceManagerRegistrationFunc func,
void* data) /* Since 1.0.13 */
{
gulong id = 0;
if (G_LIKELY(self) && G_LIKELY(func)) {
char* tmp_name = NULL;
GBinderServiceManagerClass* klass =
GBINDER_SERVICEMANAGER_GET_CLASS(self);
switch (klass->check_name(self, name)) {
case GBINDER_SERVICEMANAGER_NAME_OK:
break;
case GBINDER_SERVICEMANAGER_NAME_NORMALIZE:
name = tmp_name = klass->normalize_name(self, name);
break;
default:
name = NULL;
break;
}
if (name) {
GBinderServiceManagerPriv* priv = self->priv;
GBinderServiceManagerWatch* watch = NULL;
watch = g_hash_table_lookup(priv->watch_table, name);
if (!watch) {
watch = gbinder_servicemanager_watch_new(name);
g_hash_table_insert(priv->watch_table, watch->name, watch);
}
if (!watch->watched) {
watch->watched = klass->watch(self, name);
if (watch->watched) {
GDEBUG("Watching %s", watch->name);
} else {
GWARN("Failed to watch %s", watch->name);
}
}
id = g_signal_connect_closure_by_id(self,
gbinder_servicemanager_signals[SIGNAL_REGISTRATION],
watch->quark, g_cclosure_new(G_CALLBACK(func), data, NULL),
FALSE);
}
g_free(tmp_name);
}
return id;
}
void
gbinder_servicemanager_remove_handler(
GBinderServiceManager* self,
gulong id) /* Since 1.0.13 */
{
if (G_LIKELY(self) && G_LIKELY(id)) {
GBinderServiceManagerClass* klass =
GBINDER_SERVICEMANAGER_GET_CLASS(self);
GBinderServiceManagerPriv* priv = self->priv;
GHashTableIter it;
gpointer value;
g_signal_handler_disconnect(self, id);
g_hash_table_iter_init(&it, priv->watch_table);
while (g_hash_table_iter_next(&it, NULL, &value)) {
GBinderServiceManagerWatch* watch = value;
if (watch->watched && !g_signal_has_handler_pending(self,
gbinder_servicemanager_signals[SIGNAL_REGISTRATION],
watch->quark, TRUE)) {
/* This must be the one we have just removed */
GDEBUG("Unwatching %s", watch->name);
watch->watched = FALSE;
klass->unwatch(self, watch->name);
break;
}
}
}
}
/*==========================================================================*
* Internals
*==========================================================================*/
@@ -442,6 +604,12 @@ void
gbinder_servicemanager_init(
GBinderServiceManager* self)
{
GBinderServiceManagerPriv* priv = G_TYPE_INSTANCE_GET_PRIVATE(self,
GBINDER_TYPE_SERVICEMANAGER, GBinderServiceManagerPriv);
self->priv = priv;
priv->watch_table = g_hash_table_new_full(g_str_hash, g_str_equal,
NULL, gbinder_servicemanager_watch_free);
}
static
@@ -450,8 +618,7 @@ gbinder_servicemanager_dispose(
GObject* object)
{
GBinderServiceManager* self = GBINDER_SERVICEMANAGER(object);
GBinderServiceManagerClass* klass =
GBINDER_SERVICEMANAGER_GET_CLASS(self);
GBinderServiceManagerClass* klass = GBINDER_SERVICEMANAGER_GET_CLASS(self);
GVERBOSE_("%s", self->dev);
/* Lock */
@@ -493,9 +660,10 @@ gbinder_servicemanager_finalize(
GObject* object)
{
GBinderServiceManager* self = GBINDER_SERVICEMANAGER(object);
GBinderServiceManagerPriv* priv = self->priv;
gutil_idle_pool_drain(self->pool);
gutil_idle_pool_unref(self->pool);
g_hash_table_destroy(priv->watch_table);
gutil_idle_pool_destroy(self->pool);
gbinder_client_unref(self->client);
G_OBJECT_CLASS(PARENT_CLASS)->finalize(object);
}
@@ -508,8 +676,13 @@ gbinder_servicemanager_class_init(
GObjectClass* object_class = G_OBJECT_CLASS(klass);
g_mutex_init(&klass->mutex);
g_type_class_add_private(klass, sizeof(GBinderServiceManagerPriv));
object_class->dispose = gbinder_servicemanager_dispose;
object_class->finalize = gbinder_servicemanager_finalize;
gbinder_servicemanager_signals[SIGNAL_REGISTRATION] =
g_signal_new(SIGNAL_REGISTRATION_NAME, G_OBJECT_CLASS_TYPE(klass),
G_SIGNAL_RUN_FIRST | G_SIGNAL_DETAILED, 0, NULL, NULL, NULL,
G_TYPE_NONE, 1, G_TYPE_STRING);
}
/*

View File

@@ -39,13 +39,22 @@
#include <glib-object.h>
typedef struct gbinder_servicemanager_priv GBinderServiceManagerPriv;
typedef struct gbinder_servicemanager {
GObject parent;
GBinderServiceManagerPriv* priv;
const char* dev;
GBinderClient* client;
GUtilIdlePool* pool;
} GBinderServiceManager;
typedef enum gbinder_servicemanager_name_check {
GBINDER_SERVICEMANAGER_NAME_OK,
GBINDER_SERVICEMANAGER_NAME_NORMALIZE,
GBINDER_SERVICEMANAGER_NAME_INVALID,
} GBINDER_SERVICEMANAGER_NAME_CHECK;
typedef struct gbinder_servicemanager_class {
GObjectClass parent;
GMutex mutex;
@@ -63,6 +72,15 @@ typedef struct gbinder_servicemanager_class {
int (*add_service)
(GBinderServiceManager* self, const char* name,
GBinderLocalObject* obj);
/* Checking/normalizing watch names */
GBINDER_SERVICEMANAGER_NAME_CHECK (*check_name)
(GBinderServiceManager* self, const char* name);
char* (*normalize_name)(GBinderServiceManager* self, const char* name);
/* If watch() returns FALSE, unwatch() is not called */
gboolean (*watch)(GBinderServiceManager* self, const char* name);
void (*unwatch)(GBinderServiceManager* self, const char* name);
} GBinderServiceManagerClass;
GType gbinder_servicemanager_get_type(void);
@@ -73,6 +91,11 @@ gbinder_servicemanager_new_with_type(
GType type,
const char* dev);
void
gbinder_servicemanager_service_registered(
GBinderServiceManager* self,
const char* name);
#endif /* GBINDER_SERVICEMANAGER_PRIVATE_H */
/*

View File

@@ -1,6 +1,6 @@
/*
* Copyright (C) 2018 Jolla Ltd.
* Contact: Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2018 Slava Monich <slava.monich@jolla.com>
*
* You may use this file under the terms of BSD license as follows:
*
@@ -13,9 +13,9 @@
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Jolla Ltd nor the names of its contributors may
* be used to endorse or promote products derived from this software
* without specific prior written permission.
* 3. Neither the names of the copyright holders nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
@@ -54,10 +54,15 @@ typedef struct app_options {
typedef struct app {
const AppOptions* opt;
char* fqname;
GMainLoop* loop;
GBinderServiceManager* sm;
GBinderLocalObject* local;
GBinderRemoteObject* remote;
gulong wait_id;
gulong death_id;
GBinderClient* client;
GThread* thread;
int ret;
} App;
@@ -171,46 +176,80 @@ app_input_thread(
return NULL;
}
static
gboolean
app_connect_remote(
App* app)
{
app->remote = gbinder_servicemanager_get_service_sync(app->sm,
app->fqname, NULL); /* autoreleased pointer */
if (app->remote) {
const AppOptions* opt = app->opt;
GINFO("Connected to %s", app->fqname);
gbinder_remote_object_ref(app->remote);
app->client = gbinder_client_new(app->remote, opt->iface);
app->death_id = gbinder_remote_object_add_death_handler(app->remote,
app_remote_died, app);
app->thread = g_thread_new("input", app_input_thread, app);
return TRUE;
}
return FALSE;
}
static
void
app_registration_handler(
GBinderServiceManager* sm,
const char* name,
void* user_data)
{
App* app = user_data;
GDEBUG("\"%s\" appeared", name);
if (!strcmp(name, app->fqname) && app_connect_remote(app)) {
gbinder_servicemanager_remove_handler(app->sm, app->wait_id);
app->wait_id = 0;
}
}
static
void
app_run(
App* app)
{
const AppOptions* opt = app->opt;
char* fqname = opt->fqname ? g_strdup(opt->fqname) :
guint sigtrm = g_unix_signal_add(SIGTERM, app_signal, app);
guint sigint = g_unix_signal_add(SIGINT, app_signal, app);
app->fqname = opt->fqname ? g_strdup(opt->fqname) :
strchr(opt->name, '/') ? g_strdup(opt->name) :
g_strconcat(opt->iface, "/", opt->name, NULL);
int status = 0;
GBinderRemoteObject* remote = gbinder_remote_object_ref
(gbinder_servicemanager_get_service_sync(app->sm, fqname, &status));
if (remote) {
guint sigtrm = g_unix_signal_add(SIGTERM, app_signal, app);
guint sigint = g_unix_signal_add(SIGINT, app_signal, app);
gulong death_id = gbinder_remote_object_add_death_handler
(remote, app_remote_died, app);
GThread* thread = g_thread_new("input", app_input_thread, app);
GINFO("Connected to %s\n", fqname);
app->client = gbinder_client_new(remote, opt->iface);
app->ret = RET_OK;
app->loop = g_main_loop_new(NULL, TRUE);
g_main_loop_run(app->loop);
g_source_remove(sigtrm);
g_source_remove(sigint);
g_main_loop_unref(app->loop);
gbinder_remote_object_remove_handler(remote, death_id);
gbinder_remote_object_unref(remote);
/* Not the cleanest exit, just dropping the thread... */
g_thread_unref(thread);
app->loop = NULL;
} else {
GERR("No such service: %s (%d)", fqname, status);
if (!app_connect_remote(app)) {
GINFO("Waiting for %s", app->fqname);
app->wait_id = gbinder_servicemanager_add_registration_handler(app->sm,
app->fqname, app_registration_handler, app);
}
g_free(fqname);
app->loop = g_main_loop_new(NULL, TRUE);
app->ret = RET_OK;
g_main_loop_run(app->loop);
g_source_remove(sigtrm);
g_source_remove(sigint);
g_main_loop_unref(app->loop);
if (app->thread) {
/* Not the cleanest of exits, just dropping the thread... */
g_thread_unref(app->thread);
}
gbinder_remote_object_remove_handler(app->remote, app->death_id);
gbinder_remote_object_unref(app->remote);
gbinder_local_object_drop(app->local);
gbinder_client_unref(app->client);
g_free(app->fqname);
}
static
@@ -311,8 +350,6 @@ int main(int argc, char* argv[])
app.local = gbinder_servicemanager_new_local_object(app.sm,
NULL, NULL, NULL);
app_run(&app);
gbinder_local_object_unref(app.local);
gbinder_client_unref(app.client);
gbinder_servicemanager_unref(app.sm);
}
}