/
grilio_channel.c
2314 lines (2098 loc) · 68.4 KB
/
grilio_channel.c
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
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2015-2020 Jolla Ltd.
* Copyright (C) 2015-2020 Slava Monich <slava.monich@jolla.com>
*
* You may use this file under the terms of BSD license as follows:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 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 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
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#define GLIB_DISABLE_DEPRECATION_WARNINGS
#include "grilio_p.h"
#include "grilio_parser.h"
#include "grilio_transport_p.h"
#include "grilio_log.h"
#include <gutil_misc.h>
#include <gutil_macros.h>
#define GRILIO_MAX_PACKET_LEN (0x8000)
#define GRILIO_SUB_LEN (4)
typedef struct grilio_channel_event GrilIoChannelEvent;
/* Requests are considered pending for no longer than pending_timeout
* because pending requests prevent blocking requests from being
* submitted and we don't want to get stuck forever. */
#define GRILIO_DEFAULT_PENDING_TIMEOUT_MS (30000)
/* Miliseconds to microseconds */
#define MICROSEC(ms) (((gint64)(ms)) * 1000)
/* Log module */
GLOG_MODULE_DEFINE("grilio");
#define LOG_PREFIX(priv) ((priv)->transport->log_prefix)
enum grilio_transport_events {
TRANSPORT_EVENT_CONNECTED,
TRANSPORT_EVENT_DISCONNECTED,
TRANSPORT_EVENT_REQUEST_SENT,
TRANSPORT_EVENT_RESPONSE,
TRANSPORT_EVENT_INDICATION,
TRANSPORT_EVENT_READ_ERROR,
TRANSPORT_EVENT_WRITE_ERROR,
TRANSPORT_EVENT_COUNT
};
/* Object definition */
struct grilio_channel_priv {
GRilIoTransport* transport;
gulong transport_event_ids[TRANSPORT_EVENT_COUNT];
GRilIoRequest* send_req;
guint last_id;
GHashTable* req_table;
GHashTable* pending;
gboolean last_pending;
int pending_timeout;
guint pending_timeout_id;
gint64 next_pending_deadline;
GSList* log_list;
GHashTable* gen_ids;
/* Serialization */
GHashTable* block_ids;
GRilIoRequest* block_req;
GRilIoQueue* owner;
GSList* owner_queue;
/* Timeouts */
int timeout;
guint timeout_id;
gint64 next_deadline;
/* Retry queue (sorted) */
GRilIoRequest* retry_req;
/* Send queue */
GRilIoRequest* first_req;
GRilIoRequest* last_req;
/* Injected events */
gboolean processing_injects;
guint process_injects_id;
GrilIoChannelEvent* first_inject;
GrilIoChannelEvent* last_inject;
};
typedef GObjectClass GRilIoChannelClass;
G_DEFINE_TYPE(GRilIoChannel, grilio_channel, G_TYPE_OBJECT)
#define PARENT_CLASS grilio_channel_parent_class
#define GRILIO_CHANNEL_TYPE (grilio_channel_get_type())
#define GRILIO_CHANNEL(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), \
GRILIO_CHANNEL_TYPE, GRilIoChannel))
enum grilio_channel_signal {
SIGNAL_CONNECTED,
SIGNAL_UNSOL_EVENT,
SIGNAL_ERROR,
SIGNAL_EOF,
SIGNAL_OWNER,
SIGNAL_PENDING,
SIGNAL_ENABLED,
SIGNAL_COUNT
};
#define SIGNAL_CONNECTED_NAME "grilio-connected"
#define SIGNAL_UNSOL_EVENT_NAME "grilio-unsol-event"
#define SIGNAL_ERROR_NAME "grilio-error"
#define SIGNAL_EOF_NAME "grilio-eof"
#define SIGNAL_OWNER_NAME "grilio-owner"
#define SIGNAL_PENDING_NAME "grilio-pending"
#define SIGNAL_ENABLED_NAME "grilio-enabled"
#define SIGNAL_UNSOL_EVENT_DETAIL_FORMAT "%x"
#define SIGNAL_UNSOL_EVENT_DETAIL_MAX_LENGTH (8)
static guint grilio_channel_signals[SIGNAL_COUNT] = { 0 };
static GHashTable* grilio_channel_table = NULL;
#define NEW_SIGNAL_NO_ARGS(type,X) \
grilio_channel_signals[SIGNAL_##X] = g_signal_new(SIGNAL_##X##_NAME, \
type, G_SIGNAL_RUN_FIRST, 0, NULL, NULL, NULL, G_TYPE_NONE, 0)
struct grilio_channel_event {
GrilIoChannelEvent* next;
guint code;
void* data;
guint len;
};
typedef struct grilio_channel_logger {
int id;
GrilIoChannelLogFunc log;
void* user_data;
gboolean legacy;
} GrilIoChannelLogger;
typedef struct grilio_channel_gen_id_data {
GRilIoChannelPriv* priv;
guint id;
guint timeout_id;
GRilIoChannelIdCleanupFunc cleanup;
gpointer user_data;
} GRilIoChannelGenIdData;
static
void
grilio_channel_process_injects(
GRilIoChannel* self);
static
void
grilio_channel_reset_timeout(
GRilIoChannel* self);
static
void
grilio_channel_reset_pending_timeout(
GRilIoChannel* self);
static
void
grilio_channel_schedule_write(
GRilIoChannel* self);
/*==========================================================================*
* Implementation
*==========================================================================*/
static
void
grilio_channel_destroyed(
gpointer key,
GObject* channel)
{
GASSERT(grilio_channel_table);
GDEBUG("Channel \"%s\" has been destroyed", (char*) key);
if (grilio_channel_table) {
GASSERT(g_hash_table_lookup(grilio_channel_table, key) == channel);
g_hash_table_remove(grilio_channel_table, key);
if (g_hash_table_size(grilio_channel_table) == 0) {
g_hash_table_unref(grilio_channel_table);
grilio_channel_table = NULL;
}
}
}
static
gboolean
grilio_channel_generic_id_timeout(
gpointer user_data)
{
GRilIoChannelGenIdData* data = user_data;
GRilIoChannelPriv* priv = data->priv;
GDEBUG("id 0x%08x timed out", data->id);
data->timeout_id = 0;
g_hash_table_remove(priv->gen_ids, GINT_TO_POINTER(data->id));
return G_SOURCE_REMOVE;
}
static
GRilIoChannelGenIdData*
grilio_channel_generic_id_data_new(
GRilIoChannelPriv* priv,
guint id,
guint timeout_ms,
GRilIoChannelIdCleanupFunc cleanup,
gpointer user_data)
{
GRilIoChannelGenIdData* data = g_slice_new(GRilIoChannelGenIdData);
data->priv = priv;
data->id = id;
data->cleanup = cleanup;
data->user_data = user_data;
data->timeout_id = g_timeout_add(timeout_ms,
grilio_channel_generic_id_timeout, data);
return data;
}
static
void
grilio_channel_generic_id_destroy(
gpointer value)
{
if (value) {
GRilIoChannelGenIdData* data = value;
if (data->timeout_id) {
g_source_remove(data->timeout_id);
}
if (data->cleanup) {
/* data->timeout_id gets zeroed on timeout */
data->cleanup(data->id, !data->timeout_id, data->user_data);
}
g_slice_free1(sizeof(*data), data);
}
}
static
guint
grilio_channel_generate_id(
GRilIoChannelPriv* priv)
{
gconstpointer key;
do {
(priv->last_id)++;
key = GINT_TO_POINTER(priv->last_id);
} while (!priv->last_id || g_hash_table_contains(priv->req_table, key) ||
(priv->block_ids && g_hash_table_contains(priv->block_ids, key)) ||
g_hash_table_contains(priv->gen_ids, key));
return priv->last_id;
}
static
guint
grilio_channel_get_generic_id(
GRilIoChannelPriv* priv)
{
const guint id = grilio_channel_generate_id(priv);
g_hash_table_insert(priv->gen_ids, GINT_TO_POINTER(id), NULL);
return id;
}
static
gboolean
grilio_channel_serialized(
GRilIoChannelPriv* priv)
{
return priv->block_ids && g_hash_table_size(priv->block_ids);
}
static
void
grilio_channel_log(
GRilIoChannel* self,
GRILIO_PACKET_TYPE type,
guint id,
guint code,
const void* data,
gsize len)
{
GRilIoChannelPriv* priv = self->priv;
GSList* link = priv->log_list;
guint8* legacy_data = NULL;
gsize legacy_len = 0;
while (link) {
GSList* next = link->next;
GrilIoChannelLogger* logger = link->data;
if (logger->legacy) {
if (!legacy_data) {
guint32* header;
guint header_len;
guint ril_code;
switch (type) {
case GRILIO_PACKET_REQ:
header_len = RIL_REQUEST_HEADER_SIZE;
ril_code = code;
break;
default:
case GRILIO_PACKET_RESP:
header_len = RIL_RESPONSE_HEADER_SIZE;
ril_code = RIL_PACKET_TYPE_SOLICITED;
break;
case GRILIO_PACKET_RESP_ACK_EXP:
header_len = RIL_RESPONSE_HEADER_SIZE;
ril_code = RIL_PACKET_TYPE_SOLICITED_ACK_EXP;
break;
case GRILIO_PACKET_UNSOL:
header_len = RIL_UNSOL_HEADER_SIZE;
ril_code = RIL_PACKET_TYPE_UNSOLICITED;
break;
case GRILIO_PACKET_UNSOL_ACK_EXP:
header_len = RIL_UNSOL_HEADER_SIZE;
ril_code = RIL_PACKET_TYPE_UNSOLICITED_ACK_EXP;
break;
case GRILIO_PACKET_ACK:
header_len = RIL_ACK_HEADER_SIZE;
ril_code = RIL_PACKET_TYPE_SOLICITED_ACK;
break;
}
legacy_len = header_len + len;
legacy_data = g_malloc(legacy_len);
memcpy(legacy_data + header_len, data, len);
header = (guint32*)legacy_data;
header[0] = GUINT32_TO_RIL(ril_code);
switch (type) {
default:
case GRILIO_PACKET_RESP:
case GRILIO_PACKET_RESP_ACK_EXP:
header[2] = GUINT32_TO_RIL(code); /* status */
/* no break */
case GRILIO_PACKET_REQ:
case GRILIO_PACKET_ACK:
header[1] = GUINT32_TO_RIL(id);
break;
case GRILIO_PACKET_UNSOL_ACK_EXP:
case GRILIO_PACKET_UNSOL:
header[1] = GUINT32_TO_RIL(code);
break;
}
}
logger->log(self, type, id, code, legacy_data, legacy_len,
logger->user_data);
} else {
logger->log(self, type, id, code, data, len, logger->user_data);
}
link = next;
}
g_free(legacy_data);
}
static
guint
grilio_channel_logger_add(
GRilIoChannel* self,
gboolean legacy,
GrilIoChannelLogFunc log,
void* user_data)
{
if (G_LIKELY(self && log)) {
GRilIoChannelPriv* priv = self->priv;
GrilIoChannelLogger* logger = g_slice_new(GrilIoChannelLogger);
logger->id = grilio_channel_get_generic_id(priv);
logger->log = log;
logger->user_data = user_data;
logger->legacy = legacy;
priv->log_list = g_slist_append(priv->log_list, logger);
return logger->id;
} else {
return 0;
}
}
static
void
grilio_channel_logger_free(
GrilIoChannelLogger* logger)
{
g_slice_free(GrilIoChannelLogger, logger);
}
static
void
grilio_channel_logger_free1(
gpointer logger)
{
grilio_channel_logger_free(logger);
}
static
void
grilio_channel_update_pending(
GRilIoChannel* self)
{
const gboolean has_pending = grilio_channel_has_pending_requests(self);
GRilIoChannelPriv* priv = self->priv;
if (priv->last_pending != has_pending) {
priv->last_pending = has_pending;
g_signal_emit(self, grilio_channel_signals[SIGNAL_PENDING], 0);
}
}
static
void
grilio_channel_queue_request(
GRilIoChannelPriv* priv,
GRilIoRequest* req)
{
GASSERT(!req->next);
GASSERT(req->status == GRILIO_REQUEST_NEW ||
req->status == GRILIO_REQUEST_RETRY);
req->status = GRILIO_REQUEST_QUEUED;
if (priv->last_req) {
priv->last_req->next = req;
priv->last_req = req;
} else {
GASSERT(!priv->first_req);
priv->first_req = priv->last_req = req;
}
GVERBOSE("Queued %srequest %u (%08x/%08x)", LOG_PREFIX(priv),
req->code, req->id, req->current_id);
}
static
void
grilio_channel_requeue_request(
GRilIoChannel* self,
GRilIoRequest* req)
{
GRilIoChannelPriv* priv = self->priv;
GASSERT(!g_hash_table_contains(priv->req_table,
GINT_TO_POINTER(req->id)));
GASSERT(!g_hash_table_contains(priv->req_table,
GINT_TO_POINTER(req->current_id)));
GASSERT(!g_hash_table_contains(priv->pending,
GINT_TO_POINTER(req->current_id)));
/* Generate new request id. The first one is kept around because
* it was returned to the caller. */
req->current_id = grilio_channel_generate_id(priv);
GASSERT(req->id != req->current_id);
req->deadline = 0;
req->retry_count++;
/* Stick both public and private ids into the table (for cancel) */
g_hash_table_insert(priv->req_table,
GINT_TO_POINTER(req->id),
grilio_request_ref(req));
g_hash_table_insert(priv->req_table,
GINT_TO_POINTER(req->current_id),
grilio_request_ref(req));
GVERBOSE("Queued retry #%d for request %08x", req->retry_count, req->id);
grilio_channel_queue_request(priv, req);
grilio_channel_schedule_write(self);
}
static
GRilIoRequest*
grilio_channel_dequeue_request(
GRilIoChannel* self,
gboolean internal_only)
{
GRilIoChannelPriv* priv = self->priv;
GRilIoRequest* req = priv->first_req;
GRilIoRequest* prev = NULL;
if (req && !(req->flags & GRILIO_REQUEST_FLAG_INTERNAL)) {
if (internal_only) {
/* This one is not */
req = NULL;
} else {
/* Handle the special (serialized) cases */
if (priv->owner) {
if (g_hash_table_size(priv->pending)) {
GHashTableIter iter;
gpointer key, value;
g_hash_table_iter_init(&iter, priv->pending);
while (g_hash_table_iter_next(&iter, &key, &value)) {
GRilIoRequest* pending = value;
if (pending->queue != priv->owner) {
/* This request is not associated with the queue
* which owns the channel. Wait until all such
* requests complete, before we start submiting
* requests associated with the owner queue. */
req = NULL;
break;
}
}
}
/* If a transaction is in progress, pick the first request
* that belongs to the transaction */
while (req && req->queue != priv->owner) {
prev = req;
req = req->next;
}
} else if (grilio_channel_serialized(priv) ||
(req->flags & GRILIO_REQUEST_FLAG_BLOCKING)) {
if (g_hash_table_size(priv->pending)) {
/* We need to wait for the currently pending request(s)
* to complete or time out before we can submit a blocking
* request. */
req = NULL;
}
}
}
}
if (!req) {
/* Check if we have any internal requests queued */
req = priv->first_req;
while (req && !(req->flags & GRILIO_REQUEST_FLAG_INTERNAL)) {
prev = req;
req = req->next;
}
}
if (req) {
if (prev) {
prev->next = req->next;
} else {
priv->first_req = req->next;
}
if (req->next) {
req->next = NULL;
GASSERT(priv->last_req);
GASSERT(priv->last_req != req);
} else {
GASSERT(priv->last_req == req);
priv->last_req = prev;
}
GASSERT(req->status == GRILIO_REQUEST_QUEUED);
req->status = GRILIO_REQUEST_SENDING;
req->submitted = g_get_monotonic_time();
GVERBOSE("Sending %srequest %u (%08x/%08x)", LOG_PREFIX(priv),
req->code, req->id, req->current_id);
/* Keep track of pending requests, except for those which
* don't expect any reply */
if (!(req->flags & GRILIO_REQUEST_FLAG_NO_REPLY)) {
g_hash_table_insert(priv->pending,
GINT_TO_POINTER(req->current_id),
grilio_request_ref(req));
grilio_channel_reset_pending_timeout(self);
grilio_channel_update_pending(self);
}
}
return req;
}
static
void
grilio_channel_remove_request(
GRilIoChannelPriv* priv,
GRilIoRequest* req)
{
grilio_queue_remove(req);
g_hash_table_remove(priv->req_table, GINT_TO_POINTER(req->current_id));
if (req->id != req->current_id) {
g_hash_table_remove(priv->req_table, GINT_TO_POINTER(req->id));
}
}
static
void
grilio_channel_handle_error(
GRilIoTransport* transport,
const GError* error,
void* user_data)
{
GRilIoChannel* self = GRILIO_CHANNEL(user_data);
GRilIoChannelPriv* priv = self->priv;
grilio_transport_shutdown(priv->transport, FALSE);
g_signal_emit(self, grilio_channel_signals[SIGNAL_ERROR], 0, error);
}
static
void
grilio_channel_handle_connected(
GRilIoTransport* transport,
void* user_data)
{
GRilIoChannel* self = GRILIO_CHANNEL(user_data);
self->connected = TRUE;
self->ril_version = transport->ril_version +
grilio_transport_version_offset(transport);
GVERBOSE("Public RIL version %u", self->ril_version);
g_signal_emit(self, grilio_channel_signals[SIGNAL_CONNECTED], 0);
grilio_channel_process_injects(self);
grilio_channel_schedule_write(self);
}
static
void
grilio_channel_handle_disconnected(
GRilIoTransport* transport,
void* user_data)
{
GRilIoChannel* self = GRILIO_CHANNEL(user_data);
self->connected = FALSE;
g_signal_emit(self, grilio_channel_signals[SIGNAL_EOF], 0);
}
static
void
grilio_channel_schedule_retry(
GRilIoChannelPriv* priv,
GRilIoRequest* req)
{
GASSERT(!req->next);
req->deadline = g_get_monotonic_time() + MICROSEC(req->retry_period);
req->status = GRILIO_REQUEST_RETRY;
/* Remove the request from the request table while it's waiting
* for its turn to retry. It means that it gets completed during
* this time, we will miss the reply. */
grilio_request_ref(req);
g_hash_table_remove(priv->req_table, GINT_TO_POINTER(req->current_id));
if (req->id != req->current_id) {
g_hash_table_remove(priv->req_table, GINT_TO_POINTER(req->id));
}
GVERBOSE("Retry #%d for request %08x in %u ms", req->retry_count+1,
req->id, req->retry_period);
/* Keep the retry queue sorted by deadline */
if (priv->retry_req && priv->retry_req->deadline < req->deadline) {
GRilIoRequest* prev = priv->retry_req;
GRilIoRequest* next = prev->next;
while (next && next->deadline < req->deadline) {
prev = next;
next = prev->next;
}
prev->next = req;
req->next = next;
} else {
req->next = priv->retry_req;
priv->retry_req = req;
}
}
static
gint64
grilio_channel_pending_deadline(
GRilIoChannelPriv* priv,
GRilIoRequest* req)
{
const int req_timeout = (req->timeout > 0) ? req->timeout :
priv->pending_timeout;
GASSERT(req->submitted);
return req->submitted + MICROSEC(req_timeout);
}
static
gboolean
grilio_channel_pending_timeout(
gpointer user_data)
{
GRilIoChannel* self = GRILIO_CHANNEL(user_data);
GRilIoChannelPriv* priv = self->priv;
const gint64 now = g_get_monotonic_time();
GHashTableIter iter;
gpointer value;
/*
* To prevent request completion from releasing the last reference
* to GRilIoChannel, temporarily bump the reference count.
*/
grilio_channel_ref(self);
priv->pending_timeout_id = 0;
g_hash_table_iter_init(&iter, priv->pending);
while (g_hash_table_iter_next(&iter, NULL, &value)) {
GRilIoRequest* req = value;
const gint64 t = grilio_channel_pending_deadline(priv, req);
if (t <= now) {
GDEBUG("Pending %srequest %u (%08x/%08x) expired",
LOG_PREFIX(priv), req->code, req->id, req->current_id);
if (req == priv->block_req) {
/* Let the life continue */
grilio_request_unref(priv->block_req);
priv->block_req = NULL;
}
g_hash_table_iter_remove(&iter);
}
}
grilio_channel_reset_pending_timeout(self);
grilio_channel_schedule_write(self);
grilio_channel_update_pending(self);
grilio_channel_unref(self);
return G_SOURCE_REMOVE;
}
static
void
grilio_channel_reset_pending_timeout(
GRilIoChannel* self)
{
GRilIoChannelPriv* priv = self->priv;
if (g_hash_table_size(priv->pending)) {
GHashTableIter iter;
const gint64 now = g_get_monotonic_time();
gint64 deadline = 0;
gpointer value;
/* Calculate the new deadline */
g_hash_table_iter_init(&iter, priv->pending);
while (g_hash_table_iter_next(&iter, NULL, &value)) {
GRilIoRequest* req = value;
const gint64 t = grilio_channel_pending_deadline(priv, req);
if (!deadline || deadline > t) {
deadline = t;
}
}
if (priv->next_pending_deadline != deadline) {
/* Reset the timer */
if (priv->pending_timeout_id) {
g_source_remove(priv->pending_timeout_id);
}
priv->next_pending_deadline = deadline;
if (deadline > now) {
priv->pending_timeout_id =
g_timeout_add(((deadline - now) + 999)/1000,
grilio_channel_pending_timeout, self);
} else {
priv->pending_timeout_id =
g_idle_add(grilio_channel_pending_timeout, self);
}
}
} else {
/* No more pending requests, cancel the timeout */
if (priv->pending_timeout_id) {
g_source_remove(priv->pending_timeout_id);
priv->pending_timeout_id = 0;
}
}
}
static
gboolean
grilio_channel_timeout(
gpointer user_data)
{
GRilIoChannel* self = GRILIO_CHANNEL(user_data);
GRilIoChannelPriv* priv = self->priv;
GRilIoRequest* expired = NULL;
const gint64 now = g_get_monotonic_time();
gboolean pending_expired = FALSE;
GHashTableIter iter;
gpointer key, value;
priv->timeout_id = 0;
priv->next_deadline = 0;
/* Expired requests */
g_hash_table_iter_init(&iter, priv->req_table);
while (g_hash_table_iter_next(&iter, &key, &value)) {
GRilIoRequest* req = value;
if (key == GINT_TO_POINTER(req->current_id) &&
req->deadline && req->deadline <= now) {
GASSERT(!req->next);
req->next = expired;
req->deadline = 0;
GDEBUG("%s%srequest %u (%08x/%08x) timed out",
(priv->block_req == req) ? "Blocking " : "",
LOG_PREFIX(priv), req->code, req->id, req->current_id);
if (priv->block_req == req) {
expired = priv->block_req;
priv->block_req = NULL;
} else {
expired = grilio_request_ref(req);
}
}
}
while (expired) {
GRilIoRequest* req = expired;
expired = req->next;
req->next = NULL;
if (g_hash_table_remove(priv->pending,
GINT_TO_POINTER(req->current_id))) {
grilio_channel_update_pending(self);
pending_expired = TRUE;
}
g_hash_table_remove(priv->req_table, GINT_TO_POINTER(req->id));
if (grilio_request_can_retry(req)) {
grilio_channel_schedule_retry(priv, req);
} else {
grilio_channel_remove_request(priv, req);
req->status = GRILIO_REQUEST_DONE;
if (req->response) {
req->response(self, GRILIO_STATUS_TIMEOUT, NULL, 0,
req->user_data);
}
}
grilio_request_unref(req);
}
/* Expired retry timeouts */
if (priv->retry_req && priv->retry_req->deadline <= now) {
/* Slip expired timeout to preserve the order */
GRilIoRequest* last = priv->retry_req;
while (last->next && last->next->deadline <= now) {
last = last->next;
}
expired = priv->retry_req;
priv->retry_req = last->next;
last->next = NULL;
}
while (expired) {
GRilIoRequest* req = expired;
expired = req->next;
req->next = NULL;
grilio_channel_requeue_request(self, req);
}
if (pending_expired) {
grilio_channel_reset_pending_timeout(self);
}
grilio_channel_reset_timeout(self);
grilio_channel_schedule_write(self);
grilio_channel_update_pending(self);
return G_SOURCE_REMOVE;
}
static
void
grilio_channel_reset_timeout(
GRilIoChannel* self)
{
GRilIoChannelPriv* priv = self->priv;
GHashTableIter iter;
const gint64 now = g_get_monotonic_time();
gint64 deadline = 0;
gpointer value;
if (priv->block_req && priv->block_req->deadline) {
deadline = priv->block_req->deadline;
}
/* This loop shouldn't impact the performance because the hash table
* typically contains very few entries */
g_hash_table_iter_init(&iter, priv->req_table);
while (g_hash_table_iter_next(&iter, NULL, &value)) {
GRilIoRequest* req = value;
if (req->deadline) {
if (!deadline || deadline > req->deadline) {
deadline = req->deadline;
}
}
}
/* Retry requests are sorted */
if (priv->retry_req) {
if (!deadline || deadline > priv->retry_req->deadline) {
deadline = priv->retry_req->deadline;
}
}
if (deadline) {
if (!priv->next_deadline || priv->next_deadline > deadline) {
if (priv->timeout_id) {
g_source_remove(priv->timeout_id);
}
priv->next_deadline = deadline;
priv->timeout_id = (deadline <= now) ?
g_idle_add(grilio_channel_timeout, self) :
g_timeout_add(((deadline - now) + 999)/1000,
grilio_channel_timeout, self);
}
} else if (priv->timeout_id) {
g_source_remove(priv->timeout_id);
priv->timeout_id = 0;
priv->next_deadline = 0;
}
}
static
void
grilio_channel_request_sent(
GRilIoChannel* self,
GRilIoRequest* req)
{
GRilIoChannelPriv* priv = self->priv;
/* The request has been sent */
if (req->status == GRILIO_REQUEST_SENDING) {
req->status = GRILIO_REQUEST_SENT;
} else {
GASSERT(req->status == GRILIO_REQUEST_CANCELLED);
}
/* Log it */
grilio_channel_log(self, GRILIO_PACKET_REQ, req->current_id, req->code,
grilio_request_data(req), grilio_request_size(req));
/* If no reply is expected, remove it from req_table */
if (req->flags & GRILIO_REQUEST_FLAG_NO_REPLY) {
grilio_channel_remove_request(priv, req);
}
/* Submit the next request(s) */
if (priv->send_req == req) {
priv->send_req = NULL;
grilio_request_unref(req);
}
}
static
gboolean
grilio_channel_send_next_request(
GRilIoChannel* self)
{
int req_timeout;
GRilIoRequest* req;
GRilIoChannelPriv* priv = self->priv;
if (!self->connected) {
GVERBOSE("%s not connected yet", self->name);
return FALSE;
}
if (priv->send_req) {
/* Request is being sent */
return FALSE;
}
req = priv->send_req;
if (priv->block_req && !req) {
/* Try to dequeue an internal request */
req = priv->send_req = grilio_channel_dequeue_request(self, TRUE);
if (!req) {
GVERBOSE("%s waiting for request %08x to complete", self->name,
priv->block_req->current_id);
return FALSE;
}
}
if (!req) {
/* Nothing stops us from dequeueing any request */
req = priv->send_req = grilio_channel_dequeue_request(self, FALSE);
if (!req) {
/* There is nothing to send, remove the watch */
GVERBOSE("%s has nothing to send", self->name);
return FALSE;
}
}
req_timeout = req->timeout;
if (req_timeout == GRILIO_TIMEOUT_DEFAULT && priv->timeout > 0) {
req_timeout = priv->timeout;
}
if (!(req->flags & GRILIO_REQUEST_FLAG_INTERNAL) &&
((req->flags & GRILIO_REQUEST_FLAG_BLOCKING) ||
grilio_channel_serialized(priv))) {
/* Block next requests from being sent while this one is pending */
priv->block_req = grilio_request_ref(req);