summaryrefslogtreecommitdiffstats
path: root/gisi/client.c
blob: 8c0cc72e24e48f748b283dc8ee788525cdf04adc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
/*
 *
 *  oFono - Open Source Telephony
 *
 *  Copyright (C) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#define _GNU_SOURCE
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <search.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <errno.h>
#include "phonet.h"
#include <glib.h>

#include "socket.h"
#include "client.h"

#define PN_COMMGR			0x10
#define PNS_SUBSCRIBED_RESOURCES_IND	0x10

static const struct sockaddr_pn commgr = {
	.spn_family = AF_PHONET,
	.spn_resource = PN_COMMGR,
};

struct _GIsiRequest {
	unsigned int id; /* don't move, see g_isi_cmp */
	GIsiClient *client;
	guint timeout;
	GIsiResponseFunc func;
	void *data;
	GDestroyNotify notify;
};

struct _GIsiIndication {
	unsigned int type; /* don't move, see g_isi_cmp */
	GIsiIndicationFunc func;
	void *data;
};
typedef struct _GIsiIndication GIsiIndication;

struct _GIsiClient {
	uint8_t resource;
	uint16_t server_obj;
	struct {
		int major;
		int minor;
	} version;
	GIsiModem *modem;
	int error;

	/* Requests */
	struct {
		int fd;
		guint source;
		unsigned int last; /* last used transaction ID */
		void *pending;
	} reqs;

	/* Indications */
	struct {
		int fd;
		guint source;
		unsigned int count;
		void *subs;
	} inds;

	/* Debugging */
	GIsiDebugFunc debug_func;
	void *debug_data;
};

static gboolean g_isi_callback(GIOChannel *channel, GIOCondition cond,
				gpointer data);
static gboolean g_isi_timeout(gpointer data);

static void g_isi_vdebug(const struct iovec *__restrict iov,
				size_t iovlen, size_t total_len,
				GIsiDebugFunc func, void *data)
{
	uint8_t debug[total_len];
	uint8_t *ptr = debug;
	size_t i;

	for (i = 0; i < iovlen; i++) {
		memcpy(ptr, iov[i].iov_base, iov[i].iov_len);
		ptr += iov[i].iov_len;
	}

	func(debug, total_len, data);
}


static int g_isi_cmp(const void *a, const void *b)
{
	const unsigned int *ua = (const unsigned int *)a;
	const unsigned int *ub = (const unsigned int *)b;

	return *ua - *ub;
}

/**
 * Create an ISI client.
 * @param resource PhoNet resource ID for the client
 * @return NULL on error (see errno), a GIsiClient pointer on success,
 */
GIsiClient *g_isi_client_create(GIsiModem *modem, uint8_t resource)
{
	GIsiClient *client;
	GIOChannel *channel;

	client  = g_try_new0(GIsiClient, 1);
	if (client == NULL) {
		errno = ENOMEM;
		return NULL;
	}

	client->resource = resource;
	client->version.major = -1;
	client->version.minor = -1;
	client->modem = modem;
	client->error = 0;
	client->debug_func = NULL;

	client->reqs.last = 0;
	client->reqs.pending = NULL;

	client->inds.count = 0;
	client->inds.subs = NULL;

	channel = phonet_new(modem, resource);
	if (channel == NULL) {
		g_free(client);
		return NULL;
	}
	client->reqs.fd = g_io_channel_unix_get_fd(channel);
	client->reqs.source = g_io_add_watch(channel,
					G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL,
					g_isi_callback, client);
	g_io_channel_unref(channel);

	return client;
}

/**
 * Set the ISI resource version of @a client.
 * @param client client for the resource
 * @param major ISI major version
 * @param minor ISI minor version
 */
void g_isi_version_set(GIsiClient *client, int major, int minor)
{
	if (client == NULL)
		return;

	client->version.major = major;
	client->version.minor = minor;
}

/**
 * Returns the ISI major version of the resource associated with @a
 * client.
 * @param client client for the resource
 * @return major version, -1 if not available
 */
int g_isi_version_major(GIsiClient *client)
{
	return client ? client->version.major : -1;
}

/**
 * Returns the ISI minor version of the resource associated with @a
 * client.
 * @param client client for the resource
 * @return minor version, -1 if not available
 */
int g_isi_version_minor(GIsiClient *client)
{
	return client ? client->version.minor : -1;
}

/**
 * Set the server object for the resource associated with @a
 * client.
 * @param client client for the resource
 * @param server object
 */
void g_isi_server_object_set(GIsiClient *client, uint16_t obj)
{
	if (client == NULL)
		return;

	client->server_obj = obj;
}

/**
 * Returns the server object for the the resource associated with @a
 * client.
 * @param client client for the resource
 * @return server object
 */
uint8_t g_isi_server_object(GIsiClient *client)
{
	return client ? client->server_obj : 0;
}

/**
 * Returns the resource associated with @a client
 * @param client client for the resource
 * @return PhoNet resource ID for the client
 */
uint8_t g_isi_client_resource(GIsiClient *client)
{
	return client ? client->resource : 0;
}

/**
 * Set a debugging function for @a client. This function will be
 * called whenever an ISI protocol message is sent or received.
 * @param client client to debug
 * @param func debug function
 * @param opaque user data
 */
void g_isi_client_set_debug(GIsiClient *client, GIsiDebugFunc func,
				void *opaque)
{
	if (client == NULL)
		return;

	client->debug_func = func;
	client->debug_data = opaque;
}

static void g_isi_cleanup_req(void *data)
{
	GIsiRequest *req = data;

	if (req == NULL)
		return;

	/* Finalize any pending requests */
	req->client->error = ESHUTDOWN;
	if (req->func)
		req->func(req->client, NULL, 0, 0, req->data);
	req->client->error = 0;

	if (req->notify)
		req->notify(req->data);

	if (req->timeout > 0)
		g_source_remove(req->timeout);

	g_free(req);
}

static void g_isi_cleanup_ind(void *data)
{
	GIsiIndication *ind = data;

	if (ind == NULL)
		return;

	g_free(ind);
}

/**
 * Destroys an ISI client, cancels all pending transactions and subscriptions.
 * @param client client to destroy (may be NULL)
 */
void g_isi_client_destroy(GIsiClient *client)
{
	if (client == NULL)
		return;

	tdestroy(client->reqs.pending, g_isi_cleanup_req);
	if (client->reqs.source > 0)
		g_source_remove(client->reqs.source);

	tdestroy(client->inds.subs, g_isi_cleanup_ind);
	client->inds.subs = NULL;
	client->inds.count = 0;
	g_isi_commit_subscriptions(client);
	if (client->inds.source > 0)
		g_source_remove(client->inds.source);

	g_free(client);
}

/**
 * Make an ISI request and register a callback to process the response(s) to
 * the resulting transaction.
 * @param cl ISI client (from g_isi_client_create())
 * @param buf pointer to request payload
 * @param len request payload byte length
 * @param timeout timeout in seconds
 * @param cb callback to process response(s)
 * @param opaque data for the callback
 */
GIsiRequest *g_isi_request_make(GIsiClient *client, const void *__restrict buf,
				size_t len, unsigned timeout,
				GIsiResponseFunc cb, void *opaque)
{
	return g_isi_send(client, buf, len, timeout, cb, opaque, NULL);
}

/**
 * Make an ISI request and register a callback to process the response(s) to
 * the resulting transaction.
 * @param cl ISI client (from g_isi_client_create())
 * @param iov scatter-gather array to the request payload
 * @param iovlen number of vectors in the scatter-gather array
 * @param timeout timeout in seconds
 * @param cb callback to process response(s)
 * @param opaque data for the callback
 */
GIsiRequest *g_isi_request_vmake(GIsiClient *client, const struct iovec *iov,
					size_t iovlen, unsigned timeout,
					GIsiResponseFunc func, void *opaque)
{
	return g_isi_vsend(client, iov, iovlen, timeout, func, opaque, NULL);
}

/**
 * Send an ISI request to a specific Phonet address and register a callback
 * to process the response(s) to the resulting transaction.
 *
 * @param client ISI client (from g_isi_client_create())
 * @param dst Phonet destination address
 * @param buf pointer to request payload
 * @param len request payload byte length
 * @param timeout timeout in seconds
 * @param cb callback to process response(s)
 * @param opaque data for the callback
 * @param notify finalizer function for the @a opaque data (may be NULL)
 *
 * @return
 * A pointer to a newly created GIsiRequest.
 *
 * @errors
 * If an error occurs, @a errno is set accordingly and a NULL pointer is
 * returned.
 */
GIsiRequest *g_isi_sendto(GIsiClient *client,
				struct sockaddr_pn *dst,
				const void *__restrict buf, size_t len,
				unsigned timeout,
				GIsiResponseFunc cb, void *opaque,
				GDestroyNotify notify)
{
	const struct iovec iov = {
		.iov_base = (void *)buf,
		.iov_len = len,
	};

	return g_isi_vsendto(client, dst, &iov, 1, timeout, cb, opaque, notify);
}


/**
 * Send an ISI request and register a callback to process the response(s) to
 * the resulting transaction.
 *
 * @param cl ISI client (from g_isi_client_create())
 * @param buf pointer to request payload
 * @param len request payload byte length
 * @param timeout timeout in seconds
 * @param cb callback to process response(s)
 * @param opaque data for the callback
 * @param notify finalizer function for the @a opaque data (may be NULL)
 *
 * @return
 * A pointer to a newly created GIsiRequest.
 *
 * @errors
 * If an error occurs, @a errno is set accordingly and a NULL pointer is
 * returned.
 */
GIsiRequest *g_isi_send(GIsiClient *client,
			const void *__restrict buf, size_t len,
			unsigned timeout,
			GIsiResponseFunc cb, void *opaque,
			GDestroyNotify notify)
{
	const struct iovec iov = {
		.iov_base = (void *)buf,
		.iov_len = len,
	};

	return g_isi_vsend(client, &iov, 1, timeout, cb, opaque, notify);
}


/**
 * Send an ISI request to a specific Phonet address and register a callback
 * to process the response(s) to the resulting transaction.
 *
 * @param client ISI client (from g_isi_client_create())
 * @param dst Phonet destination address
 * @param iov scatter-gather array to the request payload
 * @param iovlen number of vectors in the scatter-gather array
 * @param timeout timeout in seconds
 * @param cb callback to process response(s)
 * @param opaque data for the callback
 * @param notify finalizer function for the @a opaque data (may be NULL)
 *
 * @return
 * A pointer to a newly created GIsiRequest.
 *
 * @errors
 * If an error occurs, @a errno is set accordingly and a NULL pointer is
 * returned.
 */
GIsiRequest *g_isi_vsendto(GIsiClient *client,
				struct sockaddr_pn *dst,
				const struct iovec *__restrict iov,
				size_t iovlen, unsigned timeout,
				GIsiResponseFunc cb, void *opaque,
				GDestroyNotify notify)
{
	struct iovec _iov[1 + iovlen];
	struct msghdr msg = {
		.msg_name = (void *)dst,
		.msg_namelen = sizeof(*dst),
		.msg_iov = _iov,
		.msg_iovlen = 1 + iovlen,
		.msg_control = NULL,
		.msg_controllen = 0,
		.msg_flags = 0,
	};
	ssize_t ret;
	size_t i, len;
	unsigned int key;
	uint8_t id;

	GIsiRequest *req = NULL;
	GIsiRequest **old;

	if (client == NULL) {
		errno = EINVAL;
		return NULL;
	}

	key = 1 + ((client->reqs.last + 1) % 255);

	if (cb) {
		req = g_try_new0(GIsiRequest, 1);
		if (req == NULL) {
			errno = ENOMEM;
			return NULL;
		}

		req->client = client;
		req->id = key;
		req->func = cb;
		req->data = opaque;
		req->notify = notify;

		old = tsearch(req, &client->reqs.pending, g_isi_cmp);
		if (old == NULL) {
			errno = ENOMEM;
			goto error;
		}
		if (*old == req)
			old = NULL;

	} else
		old = tfind(&key, &client->reqs.pending, g_isi_cmp);

	if (old) {
		/* FIXME: perhaps retry with randomized access after
		 * initial miss. Although if the rate at which
		 * requests are sent is so high that the transaction
		 * ID wraps it's likely there is something wrong and
		 * we might as well fail here. */
		errno = EBUSY;
		goto error;
	}

	id = key;
	_iov[0].iov_base = &id;
	_iov[0].iov_len = 1;

	for (i = 0, len = 1; i < iovlen; i++) {
		_iov[1 + i] = iov[i];
		len += iov[i].iov_len;
	}

	if (client->debug_func)
		g_isi_vdebug(iov, iovlen, len - 1, client->debug_func,
				client->debug_data);

	ret = sendmsg(client->reqs.fd, &msg, MSG_NOSIGNAL);
	if (ret == -1)
		goto error;

	if (ret != (ssize_t)len) {
		errno = EMSGSIZE;
		goto error;
	}

	if (req && timeout)
		req->timeout = g_timeout_add_seconds(timeout, g_isi_timeout,
							req);
	client->reqs.last = key;
	return req;

error:
	tdelete(req, &client->reqs.pending, g_isi_cmp);
	g_free(req);

	return NULL;
}

/**
 * Send an ISI request and register a callback to process the response(s) to
 * the resulting transaction.
 *
 * @param cl ISI client (from g_isi_client_create())
 * @param iov scatter-gather array to the request payload
 * @param iovlen number of vectors in the scatter-gather array
 * @param timeout timeout in seconds
 * @param cb callback to process response(s)
 * @param opaque data for the callback
 * @param notify finalizer function for the @a opaque data (may be NULL)
 *
 * @return
 * A pointer to a newly created GIsiRequest.
 *
 * @errors
 * If an error occurs, @a errno is set accordingly and a NULL pointer is
 * returned.
 */
GIsiRequest *g_isi_vsend(GIsiClient *client,
				const struct iovec *__restrict iov,
				size_t iovlen, unsigned timeout,
				GIsiResponseFunc cb, void *opaque,
				GDestroyNotify notify)
{
	struct sockaddr_pn dst = {
		.spn_family = AF_PHONET,
	};

	if (client == NULL) {
		errno = EINVAL;
		return NULL;
	}

	dst.spn_resource = client->resource;

	return g_isi_vsendto(client, &dst, iov, iovlen, timeout,
				cb, opaque, notify);
}

/**
 * Cancels a pending request, i.e. stop waiting for responses and cancels the
 * timeout.
 * @param req request to cancel
 */
void g_isi_request_cancel(GIsiRequest *req)
{
	if (req == NULL)
		return;

	if (req->timeout > 0)
		g_source_remove(req->timeout);

	tdelete(req, &req->client->reqs.pending, g_isi_cmp);

	if (req->notify)
		req->notify(req->data);

	g_free(req);
}

static uint8_t *__msg;
static void build_subscribe_msg(const void *nodep,
				const VISIT which,
				const int depth)
{
	GIsiIndication *ind = *(GIsiIndication **)nodep;
	uint8_t res = ind->type >> 8;

	switch (which) {
	case postorder:
	case leaf:
		if (__msg[2] && res == __msg[2+__msg[2]])
			break;
		__msg[2]++;
		__msg[2+__msg[2]] = res;
		break;
	default:
		break;
	}
}

/**
 * Subscribe indications from the modem.
 * @param client ISI client (from g_isi_client_create())
 * @return 0 on success, a system error code otherwise.
 */
int g_isi_commit_subscriptions(GIsiClient *client)
{
	GIOChannel *channel;
	uint8_t msg[3+256] = {
		0, PNS_SUBSCRIBED_RESOURCES_IND,
		0,
	};

	if (client == NULL)
		return -EINVAL;

	if (!client->inds.source) {
		if (client->inds.count == 0)
			return 0;

		channel = phonet_new(client->modem, PN_COMMGR);
		if (channel == NULL)
			return -errno;

		client->inds.fd = g_io_channel_unix_get_fd(channel);

		client->inds.source = g_io_add_watch(channel,
						G_IO_IN|G_IO_ERR|
						G_IO_HUP|G_IO_NVAL,
						g_isi_callback, client);

		g_io_channel_unref(channel);
	}

	__msg = msg;
	twalk(client->inds.subs, build_subscribe_msg);

	/* Subscribe by sending an indication */
	sendto(client->inds.fd, msg, 3+msg[2], MSG_NOSIGNAL, (void *)&commgr,
		sizeof(commgr));
	return 0;
}

/**
 * Add subscription for a given indication type from the given resource.
 * If the same type was already subscribed, the old subscription
 * is overriden. Subscriptions for newly added resources do not become
 * effective until g_isi_commit_subscriptions() has been called.
 * @param client ISI client (from g_isi_client_create())
 * @param res resource id
 * @param type indication type
 * @param cb callback to process received indications
 * @param data data for the callback
 * @return 0 on success, a system error code otherwise.
 */
int g_isi_add_subscription(GIsiClient *client, uint8_t res, uint8_t type,
				GIsiIndicationFunc cb, void *data)
{
	GIsiIndication *ind;
	GIsiIndication **old;

	if (client == NULL || cb == NULL)
		return -EINVAL;

	ind = g_try_new0(GIsiIndication, 1);
	if (ind == NULL)
		return -ENOMEM;

	ind->type = (res << 8) | type;

	old = tsearch(ind, &client->inds.subs, g_isi_cmp);
	if (old == NULL) {
		g_free(ind);
		return -ENOMEM;
	}

	/* FIXME: This overrides any existing subscription. We should
	 * enable multiple subscriptions to a single indication in
	 * order to allow efficient client sharing. */
	if (*old != ind) {
		g_free(ind);
		ind = *old;
	} else
		client->inds.count++;

	ind->func = cb;
	ind->data = data;

	return 0;
}

/**
 * Subscribe to a given indication type for the resource that an ISI client
 * is associated with. If the same type was already subscribed, the old
 * subscription is overriden. For multiple subscriptions,
 * g_isi_add_subcription() and g_isi_commit_subscriptions() should be used
 * instead.
 * @param cl ISI client (from g_isi_client_create())
 * @param type indication type
 * @param cb callback to process received indications
 * @param data data for the callback
 * @return 0 on success, a system error code otherwise.
 */
int g_isi_subscribe(GIsiClient *client, uint8_t type,
			GIsiIndicationFunc cb, void *data)
{
	int ret;

	if (client == NULL)
		return -EINVAL;

	ret = g_isi_add_subscription(client, client->resource, type, cb, data);
	if (ret)
		return ret;

	return g_isi_commit_subscriptions(client);
}

/**
 * Remove subscription for a given indication type from the given resource.
 * g_isi_commit_subcsriptions() should be called after modifications to
 * cancel unnecessary resource subscriptions from the modem.
 * @param client ISI client (from g_isi_client_create())
 * @param res resource id
 * @param type indication type
 */
void g_isi_remove_subscription(GIsiClient *client, uint8_t res, uint8_t type)
{
	void *ret;
	GIsiIndication *ind;
	unsigned int id = (res << 8) | type;

	if (client == NULL)
		return;

	ret = tfind(&id, &client->inds.subs, g_isi_cmp);
	if (ret == NULL)
		return;

	ind = *(GIsiIndication **)ret;
	tdelete(ind, &client->inds.subs, g_isi_cmp);
	client->inds.count--;
	g_free(ind);
}

/**
 * Unsubscribe from a given indication type. For removing multiple
 * subscriptions, g_isi_remove_subcription() and
 * g_isi_commit_subscriptions() should be used instead.
 * @param client ISI client (from g_isi_client_create())
 * @param type indication type.
 */
void g_isi_unsubscribe(GIsiClient *client, uint8_t type)
{
	if (client == NULL)
		return;

	g_isi_remove_subscription(client, client->resource, type);
	g_isi_commit_subscriptions(client);
}

static void g_isi_dispatch_indication(GIsiClient *client, uint8_t res,
					uint16_t obj, uint8_t *msg,
					size_t len)
{
	void *ret;
	GIsiIndication *ind;
	unsigned type = (res << 8) | msg[0];

	ret = tfind(&type, &client->inds.subs, g_isi_cmp);
	if (ret == NULL)
		return;

	ind = *(GIsiIndication **)ret;

	if (ind->func)
		ind->func(client, msg, len, obj, ind->data);
}

static void g_isi_dispatch_response(GIsiClient *client, uint8_t res,
					uint16_t obj, uint8_t *msg,
					size_t len)
{
	void *ret;
	GIsiRequest *req;
	unsigned id = msg[0];

	ret = tfind(&id, &client->reqs.pending, g_isi_cmp);
	if (ret == NULL) {
		/* This could either be an unsolicited response, which
		 * we will ignore, or an incoming request, which we
		 * handle just like an incoming indication */
		g_isi_dispatch_indication(client, res, obj, msg + 1, len - 1);
		return;
	}

	req = *(GIsiRequest **)ret;

	if (!req->func || req->func(client, msg + 1, len - 1, obj, req->data))
		g_isi_request_cancel(req);
}

/* Data callback for both responses and indications */
static gboolean g_isi_callback(GIOChannel *channel, GIOCondition cond,
				gpointer data)
{
	GIsiClient *client = data;
	int fd = g_io_channel_unix_get_fd(channel);
	int len;

	if (cond & (G_IO_NVAL|G_IO_HUP)) {
		g_warning("Unexpected event on Phonet channel %p", channel);
		return FALSE;
	}

	len = phonet_peek_length(channel);

	if (len > 0) {
		uint32_t buf[(len + 3) / 4];
		uint8_t *msg;
		uint16_t obj;
		uint8_t res;

		len = phonet_read(channel, buf, len, &obj, &res);
		if (len < 2)
			return TRUE;

		msg = (uint8_t *)buf;

		if (client->debug_func)
			client->debug_func(msg + 1, len - 1,
						client->debug_data);

		if (fd == client->reqs.fd)
			g_isi_dispatch_response(client, res, obj, msg, len);
		else
			/* Transaction field at first byte is
			 * discarded with indications */
			g_isi_dispatch_indication(client, res, obj, msg + 1,
							len - 1);
	}
	return TRUE;
}

static gboolean g_isi_timeout(gpointer data)
{
	GIsiRequest *req = data;

	req->client->error = ETIMEDOUT;
	if (req->func)
		req->func(req->client, NULL, 0, 0, req->data);
	req->client->error = 0;

	g_isi_request_cancel(req);
	return FALSE;
}

int g_isi_client_error(const GIsiClient *client)
{
	return -client->error;
}