forked from wb2osz/direwolf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dlq.c
1206 lines (938 loc) · 29.9 KB
/
dlq.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
//
// This file is part of Dire Wolf, an amateur radio packet TNC.
//
// Copyright (C) 2014, 2015, 2016 John Langner, WB2OSZ
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// 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, see <http://www.gnu.org/licenses/>.
//
/*------------------------------------------------------------------
*
* Module: dlq.c
*
* Purpose: Received frame queue.
*
* Description: In earlier versions, the main thread read from the
* audio device and performed the receive demodulation/decoding.
*
* Since version 1.2 we have a separate receive thread
* for each audio device. This queue is used to collect
* received frames from all channels and process them
* serially.
*
* In version 1.4, other types of events also go into this
* queue and we use it to drive the data link state machine.
*
*---------------------------------------------------------------*/
#include "direwolf.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#if __WIN32__
#else
#include <errno.h>
#endif
#include "ax25_pad.h"
#include "textcolor.h"
#include "audio.h"
#include "dlq.h"
#include "dedupe.h"
#include "dtime_now.h"
/* The queue is a linked list of these. */
static struct dlq_item_s *queue_head = NULL; /* Head of linked list for queue. */
#if __WIN32__
// TODO1.2: use dw_mutex_t
static CRITICAL_SECTION dlq_cs; /* Critical section for updating queues. */
static HANDLE wake_up_event; /* Notify received packet processing thread when queue not empty. */
#else
static pthread_mutex_t dlq_mutex; /* Critical section for updating queues. */
static pthread_cond_t wake_up_cond; /* Notify received packet processing thread when queue not empty. */
static pthread_mutex_t wake_up_mutex; /* Required by cond_wait. */
static volatile int recv_thread_is_waiting = 0;
#endif
static int was_init = 0; /* was initialization performed? */
static void append_to_queue (struct dlq_item_s *pnew);
static volatile int s_new_count = 0; /* To detect memory leak for queue items. */
static volatile int s_delete_count = 0; // TODO: need to test.
static volatile int s_cdata_new_count = 0; /* To detect memory leak for connected mode data. */
static volatile int s_cdata_delete_count = 0; // TODO: need to test.
/*-------------------------------------------------------------------
*
* Name: dlq_init
*
* Purpose: Initialize the queue.
*
* Inputs: None.
*
* Outputs:
*
* Description: Initialize the queue to be empty and set up other
* mechanisms for sharing it between different threads.
*
*--------------------------------------------------------------------*/
void dlq_init (void)
{
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
dw_printf ("dlq_init ( )\n");
#endif
queue_head = NULL;
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
dw_printf ("dlq_init: pthread_mutex_init...\n");
#endif
#if __WIN32__
InitializeCriticalSection (&dlq_cs);
#else
int err;
err = pthread_mutex_init (&wake_up_mutex, NULL);
err = pthread_mutex_init (&dlq_mutex, NULL);
if (err != 0) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("dlq_init: pthread_mutex_init err=%d", err);
perror ("");
exit (1);
}
#endif
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
dw_printf ("dlq_init: pthread_cond_init...\n");
#endif
#if __WIN32__
wake_up_event = CreateEvent (NULL, 0, 0, NULL);
if (wake_up_event == NULL) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("dlq_init: pthread_cond_init: can't create receive wake up event");
exit (1);
}
#else
err = pthread_cond_init (&wake_up_cond, NULL);
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
dw_printf ("dlq_init: pthread_cond_init returns %d\n", err);
#endif
if (err != 0) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("dlq_init: pthread_cond_init err=%d", err);
perror ("");
exit (1);
}
recv_thread_is_waiting = 0;
#endif
was_init = 1;
} /* end dlq_init */
/*-------------------------------------------------------------------
*
* Name: dlq_rec_frame
*
* Purpose: Add a received packet to the end of the queue.
* Normally this was received over the radio but we can create
* our own from APRStt or beaconing.
*
* This would correspond to PH-DATA Indication in the AX.25 protocol spec.
*
* Inputs: chan - Channel, 0 is first.
*
* subchan - Which modem caught it.
* Special case -1 for APRStt gateway.
*
* slice - Which slice we picked.
*
* pp - Address of packet object.
* Caller should NOT make any references to
* it after this point because it could
* be deleted at any time.
*
* alevel - Audio level, range of 0 - 100.
* (Special case, use negative to skip
* display of audio level line.
* Use -2 to indicate DTMF message.)
*
* retries - Level of bit correction used.
*
* spectrum - Display of how well multiple decoders did.
*
*
* IMPORTANT! Don't make an further references to the packet object after
* giving it to dlq_append.
*
*--------------------------------------------------------------------*/
void dlq_rec_frame (int chan, int subchan, int slice, packet_t pp, alevel_t alevel, retry_t retries, char *spectrum)
{
struct dlq_item_s *pnew;
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
dw_printf ("dlq_rec_frame (chan=%d, pp=%p, ...)\n", chan, pp);
#endif
assert (chan >= 0 && chan < MAX_CHANS);
if (pp == NULL) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("INTERNAL ERROR: dlq_rec_frame NULL packet pointer. Please report this!\n");
return;
}
#if AX25MEMDEBUG
if (ax25memdebug_get()) {
text_color_set(DW_COLOR_DEBUG);
dw_printf ("dlq_rec_frame (chan=%d.%d, seq=%d, ...)\n", chan, subchan, ax25memdebug_seq(pp));
}
#endif
/* Allocate a new queue item. */
pnew = (struct dlq_item_s *) calloc (sizeof(struct dlq_item_s), 1);
s_new_count++;
if (s_new_count > s_delete_count + 50) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("INTERNAL ERROR: DLQ memory leak, new=%d, delete=%d\n", s_new_count, s_delete_count);
}
pnew->nextp = NULL;
pnew->type = DLQ_REC_FRAME;
pnew->chan = chan;
pnew->slice = slice;
pnew->subchan = subchan;
pnew->pp = pp;
pnew->alevel = alevel;
pnew->retries = retries;
if (spectrum == NULL)
strlcpy(pnew->spectrum, "", sizeof(pnew->spectrum));
else
strlcpy(pnew->spectrum, spectrum, sizeof(pnew->spectrum));
/* Put it into queue. */
append_to_queue (pnew);
} /* end dlq_rec_frame */
/*-------------------------------------------------------------------
*
* Name: append_to_queue
*
* Purpose: Append some type of event to queue.
* This includes frames received over the radio,
* requests from client applications, and notifications
* from the frame transmission process.
*
*
* Inputs: pnew - Pointer to queue element structure.
*
* Outputs: Information is appended to queue.
*
* Description: Add item to end of linked list.
* Signal the receive processing thread if the queue was formerly empty.
*
*--------------------------------------------------------------------*/
static void append_to_queue (struct dlq_item_s *pnew)
{
struct dlq_item_s *plast;
int queue_length = 0;
if ( ! was_init) {
dlq_init ();
}
pnew->nextp = NULL;
#if DEBUG1
text_color_set(DW_COLOR_DEBUG);
dw_printf ("dlq append_to_queue: enter critical section\n");
#endif
#if __WIN32__
EnterCriticalSection (&dlq_cs);
#else
int err;
err = pthread_mutex_lock (&dlq_mutex);
if (err != 0) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("dlq append_to_queue: pthread_mutex_lock err=%d", err);
perror ("");
exit (1);
}
#endif
if (queue_head == NULL) {
queue_head = pnew;
queue_length = 1;
}
else {
queue_length = 2; /* head + new one */
plast = queue_head;
while (plast->nextp != NULL) {
plast = plast->nextp;
queue_length++;
}
plast->nextp = pnew;
}
#if __WIN32__
LeaveCriticalSection (&dlq_cs);
#else
err = pthread_mutex_unlock (&dlq_mutex);
if (err != 0) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("dlq append_to_queue: pthread_mutex_unlock err=%d", err);
perror ("");
exit (1);
}
#endif
#if DEBUG1
text_color_set(DW_COLOR_DEBUG);
dw_printf ("dlq append_to_queue: left critical section\n");
dw_printf ("dlq append_to_queue (): about to wake up recv processing thread.\n");
#endif
/*
* Bug: June 2015, version 1.2
*
* It has long been known that we will eventually block trying to write to a
* pseudo terminal if nothing is reading from the other end. There is even
* a warning at start up time:
*
* Virtual KISS TNC is available on /dev/pts/2
* WARNING - Dire Wolf will hang eventually if nothing is reading from it.
* Created symlink /tmp/kisstnc -> /dev/pts/2
*
* In earlier versions, where the audio input and demodulation was in the main
* thread, that would stop and it was pretty obvious something was wrong.
* In version 1.2, the audio in / demodulating was moved to a device specific
* thread. Packet objects are appended to this queue.
*
* The main thread should wake up and process them which includes printing and
* forwarding to clients over multiple protocols and transport methods.
* Just before the 1.2 release someone reported a memory leak which only showed
* up after about 20 hours. It happened to be on a Cubie Board 2, which shouldn't
* make a difference unless there was some operating system difference.
* (cubieez 2.0 is based on Debian wheezy, just like Raspian.)
*
* The debug output revealed:
*
* It was using AX.25 for Linux (not APRS).
* The pseudo terminal KISS interface was being used.
* Transmitting was continuing fine. (So something must be writing to the other end.)
* Frames were being received and appended to this queue.
* They were not coming out of the queue.
*
* My theory is that writing to the the pseudo terminal is blocking so the
* main thread is stopped. It's not taking anything from this queue and we detect
* it as a memory leak.
*
* Add a new check here and complain if the queue is growing too large.
* That will get us a step closer to the root cause.
* This has been documented in the User Guide and the CHANGES.txt file which is
* a minimal version of Release Notes.
* The proper fix will be somehow avoiding or detecting the pseudo terminal filling up
* and blocking on a write.
*/
if (queue_length > 10) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Received frame queue is out of control. Length=%d.\n", queue_length);
dw_printf ("Reader thread is probably frozen.\n");
dw_printf ("This can be caused by using a pseudo terminal (direwolf -p) where another\n");
dw_printf ("application is not reading the frames from the other side.\n");
}
#if __WIN32__
SetEvent (wake_up_event);
#else
if (recv_thread_is_waiting) {
err = pthread_mutex_lock (&wake_up_mutex);
if (err != 0) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("dlq append_to_queue: pthread_mutex_lock wu err=%d", err);
perror ("");
exit (1);
}
err = pthread_cond_signal (&wake_up_cond);
if (err != 0) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("dlq append_to_queue: pthread_cond_signal err=%d", err);
perror ("");
exit (1);
}
err = pthread_mutex_unlock (&wake_up_mutex);
if (err != 0) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("dlq append_to_queue: pthread_mutex_unlock wu err=%d", err);
perror ("");
exit (1);
}
}
#endif
} /* end append_to_queue */
/*-------------------------------------------------------------------
*
* Name: dlq_connect_request
*
* Purpose: Client application has requested connection to another station.
*
* Inputs: addrs - Source (owncall), destination (peercall),
* and possibly digipeaters.
*
* num_addr - Number of addresses. 2 to 10.
*
* chan - Channel, 0 is first.
*
* client - Client application instance. We could have multiple
* applications, all on the same channel, connecting
* to different stations. We need to know which one
* should get the results.
*
* pid - Protocol ID for data. Normally 0xf0 but the API
* allows the client app to use something non-standard
* for special situations.
* TODO: remove this. PID is only for I and UI frames.
*
* Outputs: Request is appended to queue for processing by
* the data link state machine.
*
*--------------------------------------------------------------------*/
void dlq_connect_request (char addrs[AX25_MAX_ADDRS][AX25_MAX_ADDR_LEN], int num_addr, int chan, int client, int pid)
{
struct dlq_item_s *pnew;
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
dw_printf ("dlq_connect_request (...)\n");
#endif
assert (chan >= 0 && chan < MAX_CHANS);
/* Allocate a new queue item. */
pnew = (struct dlq_item_s *) calloc (sizeof(struct dlq_item_s), 1);
s_new_count++;
pnew->type = DLQ_CONNECT_REQUEST;
pnew->chan = chan;
memcpy (pnew->addrs, addrs, sizeof(pnew->addrs));
pnew->num_addr = num_addr;
pnew->client = client;
/* Put it into queue. */
append_to_queue (pnew);
} /* end dlq_connect_request */
/*-------------------------------------------------------------------
*
* Name: dlq_disconnect_request
*
* Purpose: Client application has requested to disconnect.
*
* Inputs: addrs - Source (owncall), destination (peercall),
* and possibly digipeaters.
*
* num_addr - Number of addresses. 2 to 10.
* Only first two matter in this case.
*
* chan - Channel, 0 is first.
*
* client - Client application instance. We could have multiple
* applications, all on the same channel, connecting
* to different stations. We need to know which one
* should get the results.
*
* Outputs: Request is appended to queue for processing by
* the data link state machine.
*
*--------------------------------------------------------------------*/
void dlq_disconnect_request (char addrs[AX25_MAX_ADDRS][AX25_MAX_ADDR_LEN], int num_addr, int chan, int client)
{
struct dlq_item_s *pnew;
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
dw_printf ("dlq_disconnect_request (...)\n");
#endif
assert (chan >= 0 && chan < MAX_CHANS);
/* Allocate a new queue item. */
pnew = (struct dlq_item_s *) calloc (sizeof(struct dlq_item_s), 1);
s_new_count++;
pnew->type = DLQ_DISCONNECT_REQUEST;
pnew->chan = chan;
memcpy (pnew->addrs, addrs, sizeof(pnew->addrs));
pnew->num_addr = num_addr;
pnew->client = client;
/* Put it into queue. */
append_to_queue (pnew);
} /* end dlq_connect_request */
/*-------------------------------------------------------------------
*
* Name: dlq_xmit_data_request
*
* Purpose: Client application has requested transmission of connected
* data over an established link.
*
* Inputs: addrs - Source (owncall), destination (peercall),
* and possibly digipeaters.
*
* num_addr - Number of addresses. 2 to 10.
* First two are used to uniquely identify link.
* Any digipeaters involved are remembered
* from when the link was established.
*
* chan - Channel, 0 is first.
*
* client - Client application instance.
*
* pid - Protocol ID for data. Normally 0xf0 but the API
* allows the client app to use something non-standard
* for special situations.
*
* xdata_ptr - Pointer to block of data.
*
* xdata_len - Length of data in bytes.
*
* Outputs: Request is appended to queue for processing by
* the data link state machine.
*
*--------------------------------------------------------------------*/
void dlq_xmit_data_request (char addrs[AX25_MAX_ADDRS][AX25_MAX_ADDR_LEN], int num_addr, int chan, int client, int pid, char *xdata_ptr, int xdata_len)
{
struct dlq_item_s *pnew;
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
dw_printf ("dlq_xmit_data_request (...)\n");
#endif
assert (chan >= 0 && chan < MAX_CHANS);
/* Allocate a new queue item. */
pnew = (struct dlq_item_s *) calloc (sizeof(struct dlq_item_s), 1);
s_new_count++;
pnew->type = DLQ_XMIT_DATA_REQUEST;
pnew->chan = chan;
memcpy (pnew->addrs, addrs, sizeof(pnew->addrs));
pnew->num_addr = num_addr;
pnew->client = client;
/* Attach the transmit data. */
pnew->txdata = cdata_new(pid,xdata_ptr,xdata_len);
/* Put it into queue. */
append_to_queue (pnew);
} /* end dlq_xmit_data_request */
/*-------------------------------------------------------------------
*
* Name: dlq_register_callsign
* dlq_unregister_callsign
*
* Purpose: Register callsigns that we will recognize for incoming connection requests.
*
* Inputs: addrs - Source (owncall), destination (peercall),
* and possibly digipeaters.
*
* chan - Channel, 0 is first.
*
* client - Client application instance.
*
* Outputs: Request is appended to queue for processing by
* the data link state machine.
*
* Description: The data link state machine does not use MYCALL from the APRS configuration.
* For outgoing frames, the client supplies the source callsign.
* For incoming connection requests, we need to know what address(es) to respond to.
*
* Note that one client application can register multiple callsigns for
* multiple channels.
* Different clients can register different different addresses on the same channel.
*
*--------------------------------------------------------------------*/
void dlq_register_callsign (char addr[AX25_MAX_ADDR_LEN], int chan, int client)
{
struct dlq_item_s *pnew;
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
dw_printf ("dlq_register_callsign (%s, chan=%d, client=%d)\n", addr, chan, client);
#endif
assert (chan >= 0 && chan < MAX_CHANS);
/* Allocate a new queue item. */
pnew = (struct dlq_item_s *) calloc (sizeof(struct dlq_item_s), 1);
s_new_count++;
pnew->type = DLQ_REGISTER_CALLSIGN;
pnew->chan = chan;
strlcpy (pnew->addrs[0], addr, AX25_MAX_ADDR_LEN);
pnew->num_addr = 1;
pnew->client = client;
/* Put it into queue. */
append_to_queue (pnew);
} /* end dlq_register_callsign */
void dlq_unregister_callsign (char addr[AX25_MAX_ADDR_LEN], int chan, int client)
{
struct dlq_item_s *pnew;
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
dw_printf ("dlq_unregister_callsign (%s, chan=%d, client=%d)\n", addr, chan, client);
#endif
assert (chan >= 0 && chan < MAX_CHANS);
/* Allocate a new queue item. */
pnew = (struct dlq_item_s *) calloc (sizeof(struct dlq_item_s), 1);
s_new_count++;
pnew->type = DLQ_UNREGISTER_CALLSIGN;
pnew->chan = chan;
strlcpy (pnew->addrs[0], addr, AX25_MAX_ADDR_LEN);
pnew->num_addr = 1;
pnew->client = client;
/* Put it into queue. */
append_to_queue (pnew);
} /* end dlq_unregister_callsign */
/*-------------------------------------------------------------------
*
* Name: dlq_channel_busy
*
* Purpose: Inform data link state machine about activity on the radio channel.
*
* Inputs: chan - Radio channel number.
*
* activity - OCTYPE_PTT or OCTYPE_DCD, as defined in audio.h.
* Other values will be discarded.
*
* status - 1 for active or 0 for quiet.
*
* Outputs: Request is appended to queue for processing by
* the data link state machine.
*
* Description: Notify the link state machine about changes in carrier detect
* and our transmitter.
* This is needed for pausing some of our timers. For example,
* if we transmit a frame and expect a response in 3 seconds, that
* might be delayed because someone else is using the channel.
*
*--------------------------------------------------------------------*/
void dlq_channel_busy (int chan, int activity, int status)
{
struct dlq_item_s *pnew;
if (activity == OCTYPE_PTT || activity == OCTYPE_DCD) {
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
dw_printf ("dlq_channel_busy (...)\n");
#endif
/* Allocate a new queue item. */
pnew = (struct dlq_item_s *) calloc (sizeof(struct dlq_item_s), 1);
s_new_count++;
pnew->type = DLQ_CHANNEL_BUSY;
pnew->chan = chan;
pnew->activity = activity;
pnew->status = status;
/* Put it into queue. */
append_to_queue (pnew);
}
} /* end dlq_channel_busy */
/*-------------------------------------------------------------------
*
* Name: dlq_seize_confirm
*
* Purpose: Inform data link state machine that the transmitter is on.
* This is in reponse to lm_seize_request.
*
* Inputs: chan - Radio channel number.
*
* Outputs: Request is appended to queue for processing by
* the data link state machine.
*
* Description: When removed from the data link state machine queue, this
* becomes lm_seize_confirm.
*
*--------------------------------------------------------------------*/
void dlq_seize_confirm (int chan)
{
struct dlq_item_s *pnew;
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
dw_printf ("dlq_seize_confirm (chan=%d)\n", chan);
#endif
/* Allocate a new queue item. */
pnew = (struct dlq_item_s *) calloc (sizeof(struct dlq_item_s), 1);
s_new_count++;
pnew->type = DLQ_SEIZE_CONFIRM;
pnew->chan = chan;
/* Put it into queue. */
append_to_queue (pnew);
} /* end dlq_seize_confirm */
/*-------------------------------------------------------------------
*
* Name: dlq_client_cleanup
*
* Purpose: Client application has disappeared.
* i.e. The TCP connection has been broken.
*
* Inputs: client - Client application instance.
*
* Outputs: Request is appended to queue for processing by
* the data link state machine.
*
* Description: Notify the link state machine that given client has gone away.
* Clean up all information related to that client application.
*
*--------------------------------------------------------------------*/
void dlq_client_cleanup (int client)
{
struct dlq_item_s *pnew;
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
dw_printf ("dlq_client_cleanup (...)\n");
#endif
// assert (client >= 0 && client < MAX_NET_CLIENTS);
/* Allocate a new queue item. */
pnew = (struct dlq_item_s *) calloc (sizeof(struct dlq_item_s), 1);
s_new_count++;
// All we care about is the client number.
pnew->type = DLQ_CLIENT_CLEANUP;
pnew->client = client;
/* Put it into queue. */
append_to_queue (pnew);
} /* end dlq_client_cleanup */
/*-------------------------------------------------------------------
*
* Name: dlq_wait_while_empty
*
* Purpose: Sleep while the received data queue is empty rather than
* polling periodically.
*
* Inputs: timeout - Return at this time even if queue is empty.
* Zero for no timeout.
*
* Returns: True if timed out before any event arrived.
*
* Description: In version 1.4, we add timeout option so we can continue after
* some amount of time even if no events are in the queue.
*
*--------------------------------------------------------------------*/
int dlq_wait_while_empty (double timeout)
{
int timed_out_result = 0;
#if DEBUG1
text_color_set(DW_COLOR_DEBUG);
dw_printf ("dlq_wait_while_empty (%.3f)\n", timeout);
#endif
if ( ! was_init) {
dlq_init ();
}
if (queue_head == NULL) {
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
dw_printf ("dlq_wait_while_empty (): prepare to SLEEP...\n");
#endif
#if __WIN32__
if (timeout != 0.0) {
DWORD ms = (timeout - dtime_now()) * 1000;
if (ms <= 0) ms = 1;
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
dw_printf ("WaitForSingleObject: timeout after %d ms\n", ms);
#endif
if (WaitForSingleObject (wake_up_event, ms) == WAIT_TIMEOUT) {
timed_out_result = 1;
}
}
else {
WaitForSingleObject (wake_up_event, INFINITE);
}
#else
int err;
err = pthread_mutex_lock (&wake_up_mutex);
if (err != 0) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("dlq_wait_while_empty: pthread_mutex_lock wu err=%d", err);
perror ("");
exit (1);
}
recv_thread_is_waiting = 1;
if (timeout != 0.0) {
struct timespec abstime;
abstime.tv_sec = (time_t)(long)timeout;
abstime.tv_nsec = (long)((timeout - (long)abstime.tv_sec) * 1000000000.0);
err = pthread_cond_timedwait (&wake_up_cond, &wake_up_mutex, &abstime);
if (err == ETIMEDOUT) {
timed_out_result = 1;
}
}
else {
err = pthread_cond_wait (&wake_up_cond, &wake_up_mutex);
}
recv_thread_is_waiting = 0;
err = pthread_mutex_unlock (&wake_up_mutex);
if (err != 0) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("dlq_wait_while_empty: pthread_mutex_unlock wu err=%d", err);
perror ("");
exit (1);
}
#endif
}
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
dw_printf ("dlq_wait_while_empty () returns timedout=%d\n", timed_out_result);
#endif
return (timed_out_result);
} /* end dlq_wait_while_empty */
/*-------------------------------------------------------------------
*
* Name: dlq_remove
*
* Purpose: Remove an item from the head of the queue.
*
* Inputs: None.
*
* Returns: Pointer to a queue item. Caller is responsible for deleting it.
* NULL if queue is empty.
*
*--------------------------------------------------------------------*/
struct dlq_item_s *dlq_remove (void)
{
struct dlq_item_s *result = NULL;
//int err;
#if DEBUG1
text_color_set(DW_COLOR_DEBUG);
dw_printf ("dlq_remove() enter critical section\n");
#endif
if ( ! was_init) {
dlq_init ();
}
#if __WIN32__
EnterCriticalSection (&dlq_cs);
#else