gisi: Simplify client interface.

Use send functions without explicit timeout. Return booleans instead
of pointers - nobody really used GIsiPending but relied on client
doing the cleanup.

Add g_isi_client_set_timeout(), g_isi_client_(v)send_with_timeout().
This commit is contained in:
Pekka Pessi 2011-01-03 22:00:33 +02:00 committed by Aki Niemi
parent 340a6d8725
commit 593e74f793
3 changed files with 89 additions and 37 deletions

View File

@ -41,6 +41,7 @@ struct pending_data {
struct _GIsiClient { struct _GIsiClient {
GIsiModem *modem; GIsiModem *modem;
unsigned timeout;
uint8_t resource; uint8_t resource;
GSList *pending; GSList *pending;
}; };
@ -108,6 +109,7 @@ GIsiClient *g_isi_client_create(GIsiModem *modem, uint8_t resource)
return NULL; return NULL;
} }
client->timeout = G_ISI_CLIENT_DEFAULT_TIMEOUT;
client->resource = resource; client->resource = resource;
client->modem = modem; client->modem = modem;
client->pending = NULL; client->pending = NULL;
@ -146,6 +148,14 @@ void g_isi_client_destroy(GIsiClient *client)
g_free(client); g_free(client);
} }
void g_isi_client_set_timeout(GIsiClient *client, unsigned timeout)
{
if (client == NULL)
return;
client->timeout = timeout;
}
static struct pending_data *pending_data_create(GIsiClient *client, static struct pending_data *pending_data_create(GIsiClient *client,
GIsiNotifyFunc notify, GIsiNotifyFunc notify,
void *data, void *data,
@ -172,8 +182,22 @@ static struct pending_data *pending_data_create(GIsiClient *client,
return pd; return pd;
} }
GIsiPending *g_isi_client_send(GIsiClient *client, const void *__restrict buf, gboolean g_isi_client_send(GIsiClient *client,
size_t len, unsigned timeout, const void *__restrict msg, size_t len,
GIsiNotifyFunc notify, void *data,
GDestroyNotify destroy)
{
if (client == NULL)
return FALSE;
return g_isi_client_send_with_timeout(client, msg, len,
client->timeout,
notify, data, destroy);
}
gboolean g_isi_client_send_with_timeout(GIsiClient *client,
const void *__restrict buf, size_t len,
unsigned timeout,
GIsiNotifyFunc notify, void *data, GIsiNotifyFunc notify, void *data,
GDestroyNotify destroy) GDestroyNotify destroy)
{ {
@ -182,21 +206,34 @@ GIsiPending *g_isi_client_send(GIsiClient *client, const void *__restrict buf,
pd = pending_data_create(client, notify, data, destroy); pd = pending_data_create(client, notify, data, destroy);
if (pd == NULL) if (pd == NULL)
return NULL; return FALSE;
op = g_isi_request_send(client->modem, client->resource, buf, len, op = g_isi_request_send(client->modem, client->resource, buf, len,
timeout, pending_resp_notify, pd, timeout, pending_resp_notify, pd,
pending_destroy); pending_destroy);
if (op == NULL) { if (op == NULL) {
g_free(pd); g_free(pd);
return NULL; return FALSE;
} }
client->pending = g_slist_append(client->pending, op); client->pending = g_slist_append(client->pending, op);
return op; return TRUE;
} }
GIsiPending *g_isi_client_vsend(GIsiClient *client, gboolean g_isi_client_vsend(GIsiClient *client,
const struct iovec *iov, size_t iovlen,
GIsiNotifyFunc notify, void *data,
GDestroyNotify destroy)
{
if (client == NULL)
return FALSE;
return g_isi_client_vsend_with_timeout(client, iov, iovlen,
client->timeout,
notify, data, destroy);
}
gboolean g_isi_client_vsend_with_timeout(GIsiClient *client,
const struct iovec *__restrict iov, const struct iovec *__restrict iov,
size_t iovlen, unsigned timeout, size_t iovlen, unsigned timeout,
GIsiNotifyFunc notify, void *data, GIsiNotifyFunc notify, void *data,
@ -207,21 +244,21 @@ GIsiPending *g_isi_client_vsend(GIsiClient *client,
pd = pending_data_create(client, notify, data, destroy); pd = pending_data_create(client, notify, data, destroy);
if (pd == NULL) if (pd == NULL)
return NULL; return FALSE;
op = g_isi_request_vsend(client->modem, client->resource, iov, iovlen, op = g_isi_request_vsend(client->modem, client->resource, iov, iovlen,
timeout, pending_resp_notify, pd, timeout, pending_resp_notify, pd,
pending_destroy); pending_destroy);
if (op == NULL) { if (op == NULL) {
g_free(pd); g_free(pd);
return NULL; return FALSE;
} }
client->pending = g_slist_append(client->pending, op); client->pending = g_slist_append(client->pending, op);
return op; return TRUE;
} }
GIsiPending *g_isi_client_ind_subscribe(GIsiClient *client, uint8_t type, gboolean g_isi_client_ind_subscribe(GIsiClient *client, uint8_t type,
GIsiNotifyFunc notify, void *data) GIsiNotifyFunc notify, void *data)
{ {
struct pending_data *pd; struct pending_data *pd;
@ -229,20 +266,20 @@ GIsiPending *g_isi_client_ind_subscribe(GIsiClient *client, uint8_t type,
pd = pending_data_create(client, notify, data, NULL); pd = pending_data_create(client, notify, data, NULL);
if (pd == NULL) if (pd == NULL)
return NULL; return FALSE;
op = g_isi_ind_subscribe(client->modem, client->resource, type, op = g_isi_ind_subscribe(client->modem, client->resource, type,
pending_notify, pd, pending_destroy); pending_notify, pd, pending_destroy);
if (op == NULL) { if (op == NULL) {
g_free(pd); g_free(pd);
return NULL; return FALSE;
} }
client->pending = g_slist_append(client->pending, op); client->pending = g_slist_append(client->pending, op);
return op; return TRUE;
} }
GIsiPending *g_isi_client_ntf_subscribe(GIsiClient *client, uint8_t type, gboolean g_isi_client_ntf_subscribe(GIsiClient *client, uint8_t type,
GIsiNotifyFunc notify, void *data) GIsiNotifyFunc notify, void *data)
{ {
struct pending_data *pd; struct pending_data *pd;
@ -250,20 +287,20 @@ GIsiPending *g_isi_client_ntf_subscribe(GIsiClient *client, uint8_t type,
pd = pending_data_create(client, notify, data, NULL); pd = pending_data_create(client, notify, data, NULL);
if (pd == NULL) if (pd == NULL)
return NULL; return FALSE;
op = g_isi_ntf_subscribe(client->modem, client->resource, type, op = g_isi_ntf_subscribe(client->modem, client->resource, type,
pending_notify, pd, pending_destroy); pending_notify, pd, pending_destroy);
if (op == NULL) { if (op == NULL) {
g_free(pd); g_free(pd);
return NULL; return FALSE;
} }
client->pending = g_slist_append(client->pending, op); client->pending = g_slist_append(client->pending, op);
return op; return TRUE;
} }
GIsiPending *g_isi_client_verify(GIsiClient *client, GIsiNotifyFunc notify, gboolean g_isi_client_verify(GIsiClient *client, GIsiNotifyFunc notify,
void *data, GDestroyNotify destroy) void *data, GDestroyNotify destroy)
{ {
struct pending_data *pd; struct pending_data *pd;
@ -271,16 +308,16 @@ GIsiPending *g_isi_client_verify(GIsiClient *client, GIsiNotifyFunc notify,
pd = pending_data_create(client, notify, data, destroy); pd = pending_data_create(client, notify, data, destroy);
if (pd == NULL) if (pd == NULL)
return NULL; return FALSE;
op = g_isi_resource_ping(client->modem, client->resource, op = g_isi_resource_ping(client->modem, client->resource,
pending_resp_notify, pd, pending_resp_notify, pd,
pending_destroy); pending_destroy);
if (op == NULL) { if (op == NULL) {
g_free(pd); g_free(pd);
return NULL; return FALSE;
} }
client->pending = g_slist_append(client->pending, op); client->pending = g_slist_append(client->pending, op);
return op; return TRUE;
} }

View File

@ -31,6 +31,8 @@ extern "C" {
#include "modem.h" #include "modem.h"
#define G_ISI_CLIENT_DEFAULT_TIMEOUT (5)
struct _GIsiClient; struct _GIsiClient;
typedef struct _GIsiClient GIsiClient; typedef struct _GIsiClient GIsiClient;
@ -40,23 +42,35 @@ uint8_t g_isi_client_resource(GIsiClient *client);
void g_isi_client_reset(GIsiClient *client); void g_isi_client_reset(GIsiClient *client);
void g_isi_client_destroy(GIsiClient *client); void g_isi_client_destroy(GIsiClient *client);
GIsiPending *g_isi_client_send(GIsiClient *client, const void *__restrict msg, void g_isi_client_set_timeout(GIsiClient *client, unsigned timeout);
gboolean g_isi_client_send(GIsiClient *client,
const void *__restrict msg, size_t len,
GIsiNotifyFunc notify, void *data,
GDestroyNotify destroy);
gboolean g_isi_client_vsend(GIsiClient *client,
const struct iovec *iov, size_t iovlen,
GIsiNotifyFunc notify, void *data,
GDestroyNotify destroy);
gboolean g_isi_client_send_with_timeout(GIsiClient *client, const void *__restrict msg,
size_t len, unsigned timeout, size_t len, unsigned timeout,
GIsiNotifyFunc notify, void *data, GIsiNotifyFunc notify, void *data,
GDestroyNotify destroy); GDestroyNotify destroy);
GIsiPending *g_isi_client_vsend(GIsiClient *client, const struct iovec *iov, gboolean g_isi_client_vsend_with_timeout(GIsiClient *client, const struct iovec *iov,
size_t iovlen, unsigned timeout, size_t iovlen, unsigned timeout,
GIsiNotifyFunc notify, void *data, GIsiNotifyFunc notify, void *data,
GDestroyNotify destroy); GDestroyNotify destroy);
GIsiPending *g_isi_client_ind_subscribe(GIsiClient *client, uint8_t type, gboolean g_isi_client_ind_subscribe(GIsiClient *client, uint8_t type,
GIsiNotifyFunc notify, void *data); GIsiNotifyFunc notify, void *data);
GIsiPending *g_isi_client_ntf_subscribe(GIsiClient *client, uint8_t type, gboolean g_isi_client_ntf_subscribe(GIsiClient *client, uint8_t type,
GIsiNotifyFunc notify, void *data); GIsiNotifyFunc notify, void *data);
GIsiPending *g_isi_client_verify(GIsiClient *client, GIsiNotifyFunc notify, gboolean g_isi_client_verify(GIsiClient *client, GIsiNotifyFunc notify,
void *data, GDestroyNotify destroy); void *data, GDestroyNotify destroy);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -224,6 +224,7 @@ GIsiPipe *g_isi_pipe_create(GIsiModem *modem, GIsiPipeHandler cb, uint16_t obj1,
.type2 = type2, .type2 = type2,
.n_sb = 0, .n_sb = 0,
}; };
size_t len = sizeof(msg);
GIsiPipe *pipe; GIsiPipe *pipe;
pipe = g_try_new0(GIsiPipe, 1); pipe = g_try_new0(GIsiPipe, 1);
@ -246,8 +247,8 @@ GIsiPipe *g_isi_pipe_create(GIsiModem *modem, GIsiPipeHandler cb, uint16_t obj1,
pipe->enabled = FALSE; pipe->enabled = FALSE;
pipe->handle = PN_PIPE_INVALID_HANDLE; pipe->handle = PN_PIPE_INVALID_HANDLE;
if (g_isi_client_send(pipe->client, &msg, sizeof(msg), 3, if (g_isi_client_send(pipe->client, &msg, len,
g_isi_pipe_created, pipe, NULL)) g_isi_pipe_created, pipe, NULL))
return pipe; return pipe;
g_isi_client_destroy(pipe->client); g_isi_client_destroy(pipe->client);
@ -284,16 +285,16 @@ static void g_isi_pipe_enabled(const GIsiMessage *msg, void *data)
pipe->enabled = TRUE; pipe->enabled = TRUE;
} }
static GIsiPending *g_isi_pipe_enable(GIsiPipe *pipe) static void g_isi_pipe_enable(GIsiPipe *pipe)
{ {
struct isi_pipe_enable_req msg = { struct isi_pipe_enable_req msg = {
.cmd = PNS_PIPE_ENABLE_REQ, .cmd = PNS_PIPE_ENABLE_REQ,
.pipe_handle = pipe->handle, .pipe_handle = pipe->handle,
}; };
size_t len = sizeof(struct isi_pipe_enable_req); size_t len = sizeof(msg);
return g_isi_client_send(pipe->client, &msg, len, 5, g_isi_pipe_enabled, g_isi_client_send(pipe->client, &msg, len,
pipe, NULL); g_isi_pipe_enabled, pipe, NULL);
} }
/** /**
@ -343,16 +344,16 @@ static void g_isi_pipe_removed(const GIsiMessage *msg, void *data)
} }
static GIsiPending *g_isi_pipe_remove(GIsiPipe *pipe) static void g_isi_pipe_remove(GIsiPipe *pipe)
{ {
struct isi_pipe_remove_req msg = { struct isi_pipe_remove_req msg = {
.cmd = PNS_PIPE_REMOVE_REQ, .cmd = PNS_PIPE_REMOVE_REQ,
.pipe_handle = pipe->handle, .pipe_handle = pipe->handle,
}; };
size_t len = sizeof(struct isi_pipe_remove_req); size_t len = sizeof(msg);
return g_isi_client_send(pipe->client, &msg, len, 5, g_isi_pipe_removed, g_isi_client_send(pipe->client, &msg, len,
pipe, NULL); g_isi_pipe_removed, pipe, NULL);
} }
/** /**