This source file includes following definitions.
- channel_run
- FIO_SIGNAL_CB_FUNCTION
- handle_SIGINT
- main
1 #include "freetdm.h"
2 #include <stdio.h>
3 #include <unistd.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <signal.h>
8 #include <stdlib.h>
9
10 static int THREADS[4][31] = { {0} };
11 static int R = 0;
12 static int T = 0;
13 static ftdm_mutex_t *mutex = NULL;
14
15
16 static void *channel_run(ftdm_thread_t *me, void *obj)
17 {
18 ftdm_channel_t *ftdmchan = obj;
19 int fd = -1;
20 short buf[160];
21 int spanid = ftdm_channel_get_span_id(ftdmchan);
22 int chanid = ftdm_channel_get_id(ftdmchan);
23
24 ftdm_mutex_lock(mutex);
25 T++;
26 ftdm_mutex_unlock(mutex);
27
28 ftdm_channel_call_answer(ftdmchan);
29
30 if ((fd = open("test.raw", O_RDONLY, 0)) < 0) {
31 goto end;
32 }
33
34 while(R == 1 && THREADS[spanid][chanid] == 1) {
35 ssize_t bytes = read(fd, buf, sizeof(buf));
36 size_t bbytes;
37
38 if (bytes <= 0) {
39 break;
40 }
41
42 bbytes = (size_t) bytes;
43
44 fio_slin2alaw(buf, sizeof(buf), &bbytes);
45
46 if (ftdm_channel_write(ftdmchan, buf, sizeof(buf), &bbytes) != FTDM_SUCCESS) {
47 break;
48 }
49 }
50
51 close(fd);
52
53 end:
54
55 ftdm_channel_call_hangup(ftdmchan);
56
57 THREADS[spanid][chanid] = 0;
58
59 ftdm_mutex_lock(mutex);
60 T = 0;
61 ftdm_mutex_unlock(mutex);
62
63 return NULL;
64 }
65
66 static FIO_SIGNAL_CB_FUNCTION(on_signal)
67 {
68 int spanid = ftdm_channel_get_span_id(sigmsg->channel);
69 int chanid = ftdm_channel_get_id(sigmsg->channel);
70 ftdm_log(FTDM_LOG_DEBUG, "got sig %d:%d [%s]\n", spanid, chanid, ftdm_signal_event2str(sigmsg->event_id));
71
72 switch(sigmsg->event_id) {
73
74 case FTDM_SIGEVENT_STOP:
75 THREADS[spanid][chanid] = -1;
76 break;
77
78 case FTDM_SIGEVENT_START:
79 if (!THREADS[spanid][chanid]) {
80 THREADS[spanid][chanid] = 1;
81 ftdm_thread_create_detached(channel_run, sigmsg->channel);
82 }
83
84 break;
85 default:
86 break;
87 }
88
89 return FTDM_SUCCESS;
90 }
91
92
93 static void handle_SIGINT(int sig)
94 {
95 if (sig);
96
97 ftdm_mutex_lock(mutex);
98 R = 0;
99 ftdm_mutex_unlock(mutex);
100
101 return;
102 }
103
104 int main(int argc, char *argv[])
105 {
106 ftdm_span_t *span;
107 ftdm_mutex_create(&mutex);
108
109 ftdm_global_set_default_logger(FTDM_LOG_LEVEL_DEBUG);
110
111 if (argc < 2) {
112 printf("umm no\n");
113 exit(-1);
114 }
115
116 if (ftdm_global_init() != FTDM_SUCCESS) {
117 fprintf(stderr, "Error loading FreeTDM\n");
118 exit(-1);
119 }
120
121 printf("FreeTDM loaded\n");
122
123 if (ftdm_span_find(atoi(argv[1]), &span) != FTDM_SUCCESS) {
124 fprintf(stderr, "Error finding FreeTDM span\n");
125 goto done;
126 }
127
128
129
130 if (ftdm_configure_span(
131 span, "libpri", on_signal,
132 "node", "cpe",
133 "switch", "euroisdn",
134 "dp", "unknown",
135 "l1", "alaw",
136 "debug", NULL,
137 "opts", 0,
138 FTDM_TAG_END) == FTDM_SUCCESS) {
139
140
141 ftdm_span_start(span);
142 } else {
143 fprintf(stderr, "Error starting ISDN D-Channel\n");
144 goto done;
145 }
146
147 signal(SIGINT, handle_SIGINT);
148 ftdm_mutex_lock(mutex);
149 R = 1;
150 ftdm_mutex_unlock(mutex);
151 while(R || T) {
152 ftdm_sleep(1 * 1000);
153 }
154
155 done:
156
157 ftdm_global_destroy();
158
159 return 1;
160
161 }
162
163
164
165
166
167
168
169
170
171
172