-
Notifications
You must be signed in to change notification settings - Fork 3
/
extern.c
660 lines (491 loc) · 19.4 KB
/
extern.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
#include "menue.h"
#include "main.h"
#include "extern.h"
#include "gpio.h"
#include "signals.h"
#include "dial.h"
#include "dtmf.h"
pthread_mutex_t dial_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond_dial = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond_dtmf_dial = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond_dialcomplete = PTHREAD_COND_INITIALIZER;
pthread_mutex_t dtmf_mutex = PTHREAD_MUTEX_INITIALIZER;
sem_t sem_dtmf;
extern uint8_t number_dialed;
uint32_t binary_lines;
uint8_t first_number;
uint8_t second_number;
uint8_t complete_number;
uint8_t dialed_number;
uint8_t loop_bits;
extern bool debounce_flag;
ext_fsm_state_t next_state;
ext_fsm_state_t ext_state = st_idle ;
uint32_t ring_timer = 0;
uint8_t dial_status = 0;
extern uint8_t mfv_buffer[32];
uint8_t i = 0;
bool invalid_flag = true;
void *tf_main_fsm()
{
struct sched_param para_main;
para_main.sched_priority = 40;
sched_setscheduler(0,SCHED_RR, ¶_main);
sched_setscheduler(0,SCHED_OTHER, NULL);
sem_init(&sem_signal,0,0);
while(1) {
usleep(10000);
if (test_mode == true)
{
test_menue();
}
else
{
ext_timer();
next_state = ext_state;
main_fsm();
if (ext_state != next_state){
printf("State changed: '%x' Origin: '%x' \n", ext_state, origin_number);
};
}
}
}
void ext_timer()
{
//An active signal on ring indicator sets the debounce flag after 20ms
if ((gpio_read(RING_INDICATOR_N) == 0) & (debounce_flag == false) ){
usleep(20000);
debounce_flag = true;
}
// If ring indicator goes high while debounce flag is true, there was a glitch
// and debounce flag is cleared
else if ((gpio_read(RING_INDICATOR_N) == 1) & (debounce_flag == true)){
debounce_flag = false;
}
//if the debounce flag is still true, while the indicator is still active
// it is assumed that there is no glitch and MAX_RING is loaded
else if ((gpio_read(RING_INDICATOR_N) == 0) & (debounce_flag == true)){
ring_timer = MAX_RING;
debounce_flag = false;
}
//count down delayed by usleep
else if (ring_timer > 0) {
ring_timer-- ;
}
else{
ring_timer = 0;
}
}
void main_fsm()
{
//uint8_t iac;
/******************************************************************************
* State IDLE
******************************************************************************/
switch (ext_state) {
case st_idle:
origin_number = line_requesting();
//>>>>> GOTO st_ext_ring
if (ring_timer > 0){
melody = gb_ring;
//On external call, let all phones ring at once
sem_post(&sem_signal);
write_mcp_bit(CONNECT_CTRL, MCP_OLAT, RINGER_ENABLE, 1, 5071);
ac_on(9);
ext_state = st_ext_ring;
}
// Put Dail tone on requesting line
else if (origin_number != 0xff) {
melody = ger_dial ;
sem_post(&sem_signal);
//set matrix to output dial tone
write_ctrl_register(MATRIX_FROM, MCP_OLAT, 0x00, 1111);
write_ctrl_register(MATRIX_TO, MCP_OLAT, 0x00, 1112);
write_mcp_bit(DTMF_READ, MCP_OLAT, SIGNAL_B_FROM, 1, 3097);
write_mcp_bit(MATRIX_FROM, MCP_OLAT, origin_number, 1, 3098);
connection_check();
//Arm rotary dial receiver
pthread_mutex_lock(&dial_mutex);
pthread_cond_signal(&cond_dial);
pthread_cond_signal(&cond_dtmf_dial);
pthread_mutex_unlock(&dial_mutex);
ext_state = st_offhook;
}
else ext_state = st_idle;
break;
/******************************************************************************
* State EXTERNAL RING
******************************************************************************/
case st_ext_ring:
if (mmap_gpio_test(PICK_UP_N) == true){
//>>>>>>>> Go TO ACCEPTED >>>>>>
//Turn thyristors for AC off first
ac_off(9);
// Wait for signals to settle
usleep(20000);
// Turn off the AC generator
sem_init(&sem_signal,0,0);
write_mcp_bit(CONNECT_CTRL, MCP_OLAT, RINGER_ENABLE, 0, 5071);
//Pick up external line
write_mcp_bit(CONNECT_CTRL, MCP_OLAT, EXT_LINE_RELAY, 1, 4057);
//Switch all phones to DC
//expire ring timer
ring_timer = 0;
//Connect to devic which lifted receiver
usleep(10000);
loop_bits = read_ctrl_register(LOOP_DETECT,MCP_GPIO, 373);
origin_number = line_requesting();
set_ext_connect(origin_number);
connection_check();
ext_state = st_ext_accepted;
}
else if (ring_timer == 0){
//>>>>>>>>>>>>>>>>>GO TO IDLE >>>>>>>>>>>>>>>>>>>>
//turn ringing of if it goes to idle state
sem_init(&sem_signal,0,0);
write_mcp_bit(CONNECT_CTRL, MCP_OLAT, RINGER_ENABLE, 0, 5071);
ext_state = st_idle;
}
else {
ext_state = st_ext_ring;
}
break;
/******************************************************************************
* State External Call Accepted
******************************************************************************/
case st_ext_accepted:
//If loop is opened, hang up external line
if (line_requesting() == 0xff){
// //If loop is opened, hang up external line
// >>>>>>>>>> GO TO IDLE >>>>>>>>>>>>>>>>>>>>
return_to_idle();
ext_state = st_idle;
}
else {
ext_state = st_ext_accepted;
}
break;
/******************************************************************************
* State OFF HOOK
******************************************************************************/
case st_offhook:
// Wait for dial complete
pthread_mutex_lock(&dial_mutex);
pthread_cond_wait(&cond_dialcomplete, &dial_mutex);
pthread_mutex_unlock(&dial_mutex);
first_number = number_dialed;
//Evaluate return Message from rotary encoder
switch (dial_status) {
//Rotary decoder detected, a rotary phone is connected
case stat_compl_rotary:
//REQUESTING PHONE IS DIALING OPERATOR TO GET OUTSIDE LINE
if(number_dialed == 10){
//Clear the Dial Buffer Indexes
dtmf_rd_idx = 0;
dtmf_wr_idx = 0;
//Withdraw tokens from Tone-Signal Generation
// after dialing 0, no tone should be audible
sem_init(&sem_signal,0,0);
{gst_element_set_state (tone_pipeline, GST_STATE_NULL);
}
// Clear the connection Matrix
write_ctrl_register(MATRIX_FROM, MCP_OLAT, 0x00, 1111);
write_ctrl_register(MATRIX_TO, MCP_OLAT, 0x00, 1112);
write_mcp_bit(DTMF_READ, MCP_OLAT, SIGNAL_B_TO, 0, 3097);
write_mcp_bit(DTMF_READ, MCP_OLAT, SIGNAL_B_FROM, 0, 3097);
write_mcp_bit(CONNECT_CTRL, MCP_OLAT, EXT_TO_ENABLE, 0, 4057);
write_mcp_bit(CONNECT_CTRL, MCP_OLAT, EXT_FROM_ENABLE, 0, 4057);
// Connects the Matrix for the speach channel
origin_number = line_requesting();
set_ext_connect(origin_number);
//Allow DTMF Signaling
//write_mcp_bit(DTMF_READ, MCP_OLAT, SIGNAL_B_TO, 1, 3097);
//write_mcp_bit(CONNECT_CTRL, MCP_OLAT, EXT_TO_ENABLE, 1, 4057);
connection_check();
//The Tone Generator is connected directly to the EXT-output by the amplifier
// Enableing the Matrix is therefore not required
//Turn on Relay
write_mcp_bit(CONNECT_CTRL, MCP_OLAT, EXT_LINE_RELAY, 1, 4057);
//The write pointer for the DTMF signals is set to 0 when outside_line is entered
dtmf_wr_idx = 0;
dtmf_rd_idx = 0;
ext_state = st_outsideline_rotary;
}
else {
//>>>>GOTO st_second_number
//Turn Tones off by removing semaphore and tone pipeline zero
sem_init(&sem_signal,0,0);
gst_element_set_state (tone_pipeline, GST_STATE_NULL);
pthread_mutex_lock(&dial_mutex);
pthread_cond_signal(&cond_dial);
pthread_cond_signal(&cond_dtmf_dial);
pthread_mutex_unlock(&dial_mutex);
ext_state = st_second_number;
}
break;
case stat_compl_dtmf:
//REQUESTING PHONE IS DIALING OPERATOR TO GET OUTSIDE LINE
if(number_dialed == 10){
// after dialing 0, no "ready tone" should be audible
sem_init(&sem_signal,0,0);
{gst_element_set_state (tone_pipeline, GST_STATE_NULL);
}
// Clear the connection Matrix
write_ctrl_register(MATRIX_FROM, MCP_OLAT, 0x00, 1111);
write_ctrl_register(MATRIX_TO, MCP_OLAT, 0x00, 1112);
write_mcp_bit(DTMF_READ, MCP_OLAT, SIGNAL_B_TO, 0, 3097);
write_mcp_bit(DTMF_READ, MCP_OLAT, SIGNAL_B_FROM, 0, 3097);
write_mcp_bit(CONNECT_CTRL, MCP_OLAT, EXT_TO_ENABLE, 0, 4057);
write_mcp_bit(CONNECT_CTRL, MCP_OLAT, EXT_FROM_ENABLE, 0, 4057);
// Connects the Matrix for the speach channel
set_ext_connect(origin_number);
connection_check();
//Turn on Relay
write_mcp_bit(CONNECT_CTRL, MCP_OLAT, EXT_LINE_RELAY, 1, 4057);
ext_state = st_outsideline_dtmf;
}
else {
//>>>>GOTO st_second_number
//Turn Tones off
sem_init(&sem_signal,0,0);
pthread_mutex_lock(&dial_mutex);
pthread_cond_signal(&cond_dial);
pthread_cond_signal(&cond_dtmf_dial);
pthread_cond_signal(&cond_dtmf_dial);
pthread_mutex_unlock(&dial_mutex);
ext_state = st_second_number;
}
break;
case stat_nodial:
return_to_idle();
ext_state = st_idle;
break;
case stat_hangup:
return_to_idle();
ext_state = st_idle;
break;
}
break;
/******************************************************************************
* State Second Number
******************************************************************************/
case st_second_number:
pthread_mutex_lock(&dial_mutex);
pthread_cond_wait(&cond_dialcomplete, &dial_mutex);
pthread_mutex_unlock(&dial_mutex);
//Check for invalid numbers
second_number = number_dialed;
complete_number = (first_number *10) + second_number;
invalid_flag = false;
for (i = 0; i <= 100; i++)
{
if (invalid_no[i] == complete_number) {
invalid_flag = true;
}
}
// If an invalid number is dialed
if (invalid_flag == true) {
ext_state = st_unobtainable;
}
//If caller is not calling himself
else if ((number_dialed-1) != origin_number) {
switch(country_set[number_dialed-1]) {
case us:
melody = gb_ring;
break;
case gb:
melody = gb_ring;
break;
default:
melody = ger_ring;
}
write_mcp_bit(CONNECT_CTRL, MCP_OLAT, RINGER_ENABLE, 1, 5071);
sem_post(&sem_signal);
ac_on(number_dialed);
ext_state = st_int_ring;
}
//In case same number is dialed
else {
switch(country_set[number_dialed-1]) {
case us:
melody = ger_enga;
break;
case gb:
melody = ger_enga;
break;
default:
melody = ger_enga;
}
sem_post(&sem_signal);
ext_state = st_engaged;
}
break;
/******************************************************************************
* State OUTSIDE LINE Rotary
******************************************************************************/
case st_outsideline_rotary:
//Arm rotary dial receiver
pthread_mutex_lock(&dial_mutex);
pthread_cond_signal(&cond_dial);
pthread_cond_signal(&cond_dtmf_dial);
pthread_mutex_unlock(&dial_mutex);
//Wait for dial complete
pthread_mutex_lock(&dial_mutex);
pthread_cond_wait(&cond_dialcomplete, &dial_mutex);
pthread_mutex_unlock(&dial_mutex);
switch (dial_status) {
//Normal completion of dial
case stat_compl_rotary:
dialed_number = number_dialed;
//Ready for the next number
//When a number is dialed in st_ousideline, the number is stored in a buffer
// The buffer is incremented for each number
// ine tone.c the numbers are taken out of the buffer with dtmf_rd_idx
// Post a semaphore sem_dtmf to unblock dtmf generation
pthread_mutex_lock(&dtmf_mutex);
mfv_buffer[dtmf_wr_idx] = number_dialed;
dtmf_wr_idx++;
// Post a token, which triggers to play a dtmf tone in *tf_play_dtmf (tones.c)
sem_post(&sem_dtmf);
pthread_mutex_unlock(&dtmf_mutex);
ext_state = st_outsideline_rotary;
break;
case stat_nodial:
//User waits more than 30s to dial
//Here no Error, FSM stays in same state
ext_state = st_outsideline_rotary;
break;
case stat_hangup:
//User hung up instead of dialing
// Turn off relay, open all connections
return_to_idle();
//Remove DTMF before leaving rotary state
write_mcp_bit(DTMF_READ, MCP_OLAT, SIGNAL_B_TO, 0, 3097);
ext_state = st_idle;
break;
}
break;
//>>>>> GOTO st_int_ring
//if phone is hang up go to idle
if (line_requesting() == 0xff){
//>>>>>>>>>> GO TO st_idle >>>>>>>>>>>>>>>>>>>>
//clear all connections
return_to_idle();
//Remove DTMF before leaving rotary state
write_mcp_bit(DTMF_READ, MCP_OLAT, SIGNAL_B_TO, 0, 3097);
ext_state = st_idle;
}
else {
ext_state = st_outsideline_rotary;
}
break;
/******************************************************************************
* State OUTSIDE LINE DTMF
******************************************************************************/
case st_outsideline_dtmf:
//If the terminal is dtmf, no rotary to dtmf conversion needs to be done
// Wait only until the receiver is hang up
//if phone is hang up go to idle
if (line_requesting() == 0xff){
//>>>>>>>>>> GO TO st_idle >>>>>>>>>>>>>>>>>>>>
//clear all connections
return_to_idle();
ext_state = st_idle;
}
else {
ext_state = st_outsideline_dtmf;
}
break;
/***************************************************/
// STATE HANG UP
/****************************************************/
case st_hang_up:
usleep(1000);
return_to_idle();
ext_state = st_idle;
break;
/***************************************************/
// STATE INTERN RING
/****************************************************/
case st_int_ring:
origin_number = line_requesting();
if (mmap_gpio_test(PICK_UP_N) == true){
//>>>> GOTO st_int_accepted
//Turn of AC Ringing Thyristors for all phones
ac_off(9);
//Wait for Signal to thyristor to settle
usleep(20000);
sem_init(&sem_signal,0,0);
write_mcp_bit(CONNECT_CTRL, MCP_OLAT, RINGER_ENABLE, 0, 5071);
//Clear Matrix
write_ctrl_register(MATRIX_FROM, MCP_OLAT, 0x00, 1139);
write_ctrl_register(MATRIX_TO, MCP_OLAT, 0x00, 1140);
write_mcp_bit(DTMF_READ, MCP_OLAT, SIGNAL_B_FROM, 0, 3097);
//Sets the connection matrix FROM TO
//Print settings to screen for debug
set_connections(origin_number, number_dialed);
connection_check();
ext_state = st_int_accepted;
}
else if (origin_number == 0xff){
//>>>> GOTO st_idle
sem_init(&sem_signal,0,0);
write_mcp_bit(CONNECT_CTRL, MCP_OLAT, RINGER_ENABLE, 0, 5071);
//Turn all lines back to DC mode
return_to_idle();
ext_state = st_idle;
}
else {
ext_state = st_int_ring;
}
break;
/***************************************************/
// Inetrnal Call Accepted
/****************************************************/
case st_int_accepted:
//if all participants are leaving call go to idle
if (line_requesting() == 0xff){
//>>>>>>>>>> GO TO st_idle >>>>>>>>>>>>>>>>>>>>
return_to_idle();
ext_state = st_idle;
}
else {
ext_state = st_int_accepted;
}
break;
/***************************************************/
// Engaged
/****************************************************/
case st_engaged:
//if all participants are leaving call go to idle
if (line_requesting() == 0xff){
//>>>>>>>>>> GO TO st_idle >>>>>>>>>>>>>>>>>>>>
sem_init(&sem_signal,0,0);
return_to_idle();
ext_state = st_idle;
}
else {
ext_state = st_engaged;
}
break;
/***************************************************/
// STATE UNOBTAINABLE
/****************************************************/
case st_unobtainable:
g_object_set (G_OBJECT (announcement_src), "location", "./announcement/kein_anschluss.wav", NULL);
//While receiver is still lifted play the anouncement
while(line_requesting() != 0xff) {
gst_element_set_state (announcement_pipeline, GST_STATE_PLAYING);
}
gst_element_set_state (announcement_pipeline, GST_STATE_NULL);
return_to_idle();
ext_state = st_idle;
break;
/***************************************************/
// SWITCH DEFAULT STATE
/****************************************************/
default:
return_to_idle();
ext_state = st_idle;
}
}