3743b677135da88668d7ab196cdd809b95b6bddf
[asterisk/asterisk.git] / apps / app_test.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Applictions connected with CDR engine
5  * 
6  * Copyright (C) 2004, Digium, Inc.
7  *
8  * Mark Spencer <markster@digium.com>
9  * Russell Bryant <russelb@clemson.edu>
10  *
11  * This program is free software, distributed under the terms of
12  * the GNU General Public License
13  */
14
15 #include <sys/types.h>
16 #include <asterisk/channel.h>
17 #include <asterisk/module.h>
18 #include <asterisk/logger.h>
19 #include <asterisk/lock.h>
20 #include <asterisk/app.h>
21 #include <asterisk/pbx.h>
22 #include <asterisk/utils.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include "../astconf.h"
28
29
30 static char *tdesc = "Interface Test Application";
31
32 static char *tests_descrip = "TestServer(): Perform test server function and write call report"
33                                                          "Results stored in /var/log/asterisk/testreports/<testid>.txt";
34 static char *tests_app = "TestServer";
35 static char *tests_synopsis = "Execute Interface Test Server";
36
37 static char *testc_descrip = "TestClient(testid): Executes test client with given testid.\n"
38                                                          "Results stored in /var/log/asterisk/testreports/<testid>.txt";
39
40 static char *testc_app = "TestClient";
41 static char *testc_synopsis = "Execute Interface Test Client";
42
43 static int measurenoise(struct ast_channel *chan, int ms, char *who)
44 {
45         int res=0;
46         int mssofar;
47         int noise=0;
48         int samples=0;
49         int x;
50         short *foo;
51         struct timeval start, tv;
52         struct ast_frame *f;
53         int rformat;
54         rformat = chan->readformat;
55         if (ast_set_read_format(chan, AST_FORMAT_SLINEAR)) {
56                 ast_log(LOG_NOTICE, "Unable to set to linear mode!\n");
57                 return -1;
58         }
59         gettimeofday(&start, NULL);
60         for(;;) {
61                 gettimeofday(&tv, NULL);
62                 mssofar = (tv.tv_sec - start.tv_sec) * 1000;
63                 mssofar += (tv.tv_usec - start.tv_usec) / 1000;
64                 if (mssofar > ms)
65                         break;
66                 res = ast_waitfor(chan, ms - mssofar);
67                 if (res < 1)
68                         break;
69                 f = ast_read(chan);
70                 if (!f) {
71                         res = -1;
72                         break;
73                 }
74                 if ((f->frametype == AST_FRAME_VOICE) && (f->subclass == AST_FORMAT_SLINEAR)) {
75                         foo = (short *)f->data;
76                         for (x=0;x<f->samples;x++) {
77                                 noise += abs(foo[x]);
78                                 samples++;
79                         }
80                 }
81         }
82
83         if (rformat) {
84                 if (ast_set_read_format(chan, rformat)) {
85                         ast_log(LOG_NOTICE, "Unable to restore original format!\n");
86                         return -1;
87                 }
88         }
89         if (res < 0)
90                 return res;
91         if (!samples) {
92                 ast_log(LOG_NOTICE, "No samples were received from the other side!\n");
93                 return -1;
94         }
95         ast_log(LOG_DEBUG, "%s: Noise: %d, samples: %d, avg: %d\n", who, noise, samples, noise / samples);
96         return (noise / samples);
97 }
98
99 static int sendnoise(struct ast_channel *chan, int ms) 
100 {
101         int res;
102         res = ast_tonepair_start(chan, 1537, 2195, ms, 8192);
103         if (!res) {
104                 res = ast_waitfordigit(chan, ms);
105                 ast_tonepair_stop(chan);
106         }
107         return res;     
108 }
109
110 STANDARD_LOCAL_USER;
111
112 LOCAL_USER_DECL;
113
114 static int testclient_exec(struct ast_channel *chan, void *data)
115 {
116         struct localuser *u;
117         int res = 0;
118         char *testid=data;
119         char fn[80];
120         char serverver[80];
121         FILE *f;
122         LOCAL_USER_ADD(u);
123         
124         /* Check for test id */
125         if (!testid || ast_strlen_zero(testid)) {
126                 ast_log(LOG_WARNING, "TestClient requires an argument - the test id\n");
127                 return -1;
128         }
129         
130         if (chan->_state != AST_STATE_UP)
131                 res = ast_answer(chan);
132         
133         /* Wait a few just to be sure things get started */
134         res = ast_safe_sleep(chan, 3000);
135         /* Transmit client version */
136         if (!res)
137                 res = ast_dtmf_stream(chan, NULL, "8378*1#", 0);
138         ast_log(LOG_DEBUG, "Transmit client version\n");
139         
140         /* Read server version */
141         ast_log(LOG_DEBUG, "Read server version\n");
142         if (!res) 
143                 res = ast_app_getdata(chan, NULL, serverver, sizeof(serverver) - 1, 0);
144         if (res > 0)
145                 res = 0;
146         ast_log(LOG_DEBUG, "server version: %s\n", serverver);
147                 
148         if (res > 0)
149                 res = 0;
150
151         if (!res)
152                 res = ast_safe_sleep(chan, 1000);
153         /* Send test id */
154         if (!res) 
155                 res = ast_dtmf_stream(chan, NULL, testid, 0);           
156         if (!res) 
157                 res = ast_dtmf_stream(chan, NULL, "#", 0);              
158         ast_log(LOG_DEBUG, "send test identifier: %s\n", testid);
159
160         if ((res >=0) && (!ast_strlen_zero(testid))) {
161                 /* Make the directory to hold the test results in case it's not there */
162                 snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR);
163                 mkdir(fn, 0777);
164                 snprintf(fn, sizeof(fn), "%s/testresults/%s-client.txt", ast_config_AST_LOG_DIR, testid);
165                 if ((f = fopen(fn, "w+"))) {
166                         setlinebuf(f);
167                         fprintf(f, "CLIENTCHAN:    %s\n", chan->name);
168                         fprintf(f, "CLIENTTEST ID: %s\n", testid);
169                         fprintf(f, "ANSWER:        PASS\n");
170                         res = 0;
171                         
172                         if (!res) {
173                                 /* Step 1: Wait for "1" */
174                                 ast_log(LOG_DEBUG, "TestClient: 2.  Wait DTMF 1\n");
175                                 res = ast_waitfordigit(chan, 3000);
176                                 fprintf(f, "WAIT DTMF 1:   %s\n", (res != '1') ? "FAIL" : "PASS");
177                                 if (res == '1')
178                                         res = 0;
179                                 else
180                                         res = -1;
181                         }
182                         if (!res)
183                                 res = ast_safe_sleep(chan, 1000);
184                         if (!res) {
185                                 /* Step 2: Send "2" */
186                                 ast_log(LOG_DEBUG, "TestClient: 2.  Send DTMF 2\n");
187                                 res = ast_dtmf_stream(chan, NULL, "2", 0);
188                                 fprintf(f, "SEND DTMF 2:   %s\n", (res < 0) ? "FAIL" : "PASS");
189                                 if (res > 0)
190                                         res = 0;
191                         }
192                         if (!res) {
193                                 /* Step 3: Wait one second */
194                                 ast_log(LOG_DEBUG, "TestClient: 3.  Wait one second\n");
195                                 res = ast_safe_sleep(chan, 1000);
196                                 fprintf(f, "WAIT 1 SEC:    %s\n", (res < 0) ? "FAIL" : "PASS");
197                                 if (res > 0)
198                                         res = 0;
199                         }                       
200                         if (!res) {
201                                 /* Step 4: Measure noise */
202                                 ast_log(LOG_DEBUG, "TestClient: 4.  Measure noise\n");
203                                 res = measurenoise(chan, 5000, "TestClient");
204                                 fprintf(f, "MEASURENOISE:  %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
205                                 if (res > 0)
206                                         res = 0;
207                         }
208                         if (!res) {
209                                 /* Step 5: Wait for "4" */
210                                 ast_log(LOG_DEBUG, "TestClient: 5.  Wait DTMF 4\n");
211                                 res = ast_waitfordigit(chan, 3000);
212                                 fprintf(f, "WAIT DTMF 4:   %s\n", (res != '4') ? "FAIL" : "PASS");
213                                 if (res == '4')
214                                         res = 0;
215                                 else
216                                         res = -1;
217                         }
218                         if (!res) {
219                                 /* Step 6: Transmit tone noise */
220                                 ast_log(LOG_DEBUG, "TestClient: 6.  Transmit tone\n");
221                                 res = sendnoise(chan, 6000);
222                                 fprintf(f, "SENDTONE:      %s\n", (res < 0) ? "FAIL" : "PASS");
223                         }
224                         if (!res || (res == '5')) {
225                                 /* Step 7: Wait for "5" */
226                                 ast_log(LOG_DEBUG, "TestClient: 7.  Wait DTMF 5\n");
227                                 if (!res)
228                                         res = ast_waitfordigit(chan, 3000);
229                                 fprintf(f, "WAIT DTMF 5:   %s\n", (res != '5') ? "FAIL" : "PASS");
230                                 if (res == '5')
231                                         res = 0;
232                                 else
233                                         res = -1;
234                         }
235                         if (!res) {
236                                 /* Step 8: Wait one second */
237                                 ast_log(LOG_DEBUG, "TestClient: 8.  Wait one second\n");
238                                 res = ast_safe_sleep(chan, 1000);
239                                 fprintf(f, "WAIT 1 SEC:    %s\n", (res < 0) ? "FAIL" : "PASS");
240                                 if (res > 0)
241                                         res = 0;
242                         }
243                         if (!res) {
244                                 /* Step 9: Measure noise */
245                                 ast_log(LOG_DEBUG, "TestClient: 6.  Measure tone\n");
246                                 res = measurenoise(chan, 4000, "TestClient");
247                                 fprintf(f, "MEASURETONE:   %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
248                                 if (res > 0)
249                                         res = 0;
250                         }
251                         if (!res) {
252                                 /* Step 10: Send "7" */
253                                 ast_log(LOG_DEBUG, "TestClient: 7.  Send DTMF 7\n");
254                                 res = ast_dtmf_stream(chan, NULL, "7", 0);
255                                 fprintf(f, "SEND DTMF 7:   %s\n", (res < 0) ? "FAIL" : "PASS");
256                                 if (res > 0)
257                                         res =0;
258                         }
259                         if (!res) {
260                                 /* Step 11: Wait for "8" */
261                                 ast_log(LOG_DEBUG, "TestClient: 11.  Wait DTMF 8\n");
262                                 res = ast_waitfordigit(chan, 3000);
263                                 fprintf(f, "WAIT DTMF 8:   %s\n", (res != '8') ? "FAIL" : "PASS");
264                                 if (res == '8')
265                                         res = 0;
266                                 else
267                                         res = -1;
268                         }
269                         if (!res) {
270                                 /* Step 12: Hangup! */
271                                 ast_log(LOG_DEBUG, "TestClient: 12.  Hangup\n");
272                         }
273
274                         ast_log(LOG_DEBUG, "-- TEST COMPLETE--\n");
275                         fprintf(f, "-- END TEST--\n");
276                         fclose(f);
277                         res = -1;
278                 } else
279                         res = -1;
280         } else {
281                 ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", chan->name);
282                 res = -1;
283         }
284         LOCAL_USER_REMOVE(u);
285         return res;
286 }
287
288 static int testserver_exec(struct ast_channel *chan, void *data)
289 {
290         struct localuser *u;
291         int res = 0;
292         char testid[80]="";
293         char fn[80];
294         FILE *f;
295         LOCAL_USER_ADD(u);
296         if (chan->_state != AST_STATE_UP)
297                 res = ast_answer(chan);
298         /* Read version */
299         ast_log(LOG_DEBUG, "Read client version\n");
300         if (!res) 
301                 res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);
302         if (res > 0)
303                 res = 0;
304         ast_log(LOG_DEBUG, "client version: %s\n", testid);
305         ast_log(LOG_DEBUG, "Transmit server version\n");
306         res = ast_safe_sleep(chan, 1000);
307         if (!res)
308                 res = ast_dtmf_stream(chan, NULL, "8378*1#", 0);
309         if (res > 0)
310                 res = 0;
311
312         if (!res) 
313                 res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);               
314         ast_log(LOG_DEBUG, "read test identifier: %s\n", testid);
315         /* Check for sneakyness */
316         if (strchr(testid, '/'))
317                 res = -1;
318         if ((res >=0) && (!ast_strlen_zero(testid))) {
319                 /* Got a Test ID!  Whoo hoo! */
320                 /* Make the directory to hold the test results in case it's not there */
321                 snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR);
322                 mkdir(fn, 0777);
323                 snprintf(fn, sizeof(fn), "%s/testresults/%s-server.txt", ast_config_AST_LOG_DIR, testid);
324                 if ((f = fopen(fn, "w+"))) {
325                         setlinebuf(f);
326                         fprintf(f, "SERVERCHAN:    %s\n", chan->name);
327                         fprintf(f, "SERVERTEST ID: %s\n", testid);
328                         fprintf(f, "ANSWER:        PASS\n");
329                         ast_log(LOG_DEBUG, "Processing Test ID '%s'\n", testid);
330                         res = ast_safe_sleep(chan, 1000);
331                         if (!res) {
332                                 /* Step 1: Send "1" */
333                                 ast_log(LOG_DEBUG, "TestServer: 1.  Send DTMF 1\n");
334                                 res = ast_dtmf_stream(chan, NULL, "1", 0);
335                                 fprintf(f, "SEND DTMF 1:   %s\n", (res < 0) ? "FAIL" : "PASS");
336                                 if (res > 0)
337                                         res = 0;
338                         }
339                         if (!res) {
340                                 /* Step 2: Wait for "2" */
341                                 ast_log(LOG_DEBUG, "TestServer: 2.  Wait DTMF 2\n");
342                                 res = ast_waitfordigit(chan, 3000);
343                                 fprintf(f, "WAIT DTMF 2:   %s\n", (res != '2') ? "FAIL" : "PASS");
344                                 if (res == '2')
345                                         res = 0;
346                                 else
347                                         res = -1;
348                         }
349                         if (!res) {
350                                 /* Step 3: Measure noise */
351                                 ast_log(LOG_DEBUG, "TestServer: 3.  Measure noise\n");
352                                 res = measurenoise(chan, 6000, "TestServer");
353                                 fprintf(f, "MEASURENOISE:  %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
354                                 if (res > 0)
355                                         res = 0;
356                         }
357                         if (!res) {
358                                 /* Step 4: Send "4" */
359                                 ast_log(LOG_DEBUG, "TestServer: 4.  Send DTMF 4\n");
360                                 res = ast_dtmf_stream(chan, NULL, "4", 0);
361                                 fprintf(f, "SEND DTMF 4:   %s\n", (res < 0) ? "FAIL" : "PASS");
362                                 if (res > 0)
363                                         res = 0;
364                         }
365                 
366                         if (!res) {
367                                 /* Step 5: Wait one second */
368                                 ast_log(LOG_DEBUG, "TestServer: 5.  Wait one second\n");
369                                 res = ast_safe_sleep(chan, 1000);
370                                 fprintf(f, "WAIT 1 SEC:    %s\n", (res < 0) ? "FAIL" : "PASS");
371                                 if (res > 0)
372                                         res = 0;
373                         }
374                 
375                         if (!res) {
376                                 /* Step 6: Measure noise */
377                                 ast_log(LOG_DEBUG, "TestServer: 6.  Measure tone\n");
378                                 res = measurenoise(chan, 4000, "TestServer");
379                                 fprintf(f, "MEASURETONE:   %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
380                                 if (res > 0)
381                                         res = 0;
382                         }
383
384                         if (!res) {
385                                 /* Step 7: Send "5" */
386                                 ast_log(LOG_DEBUG, "TestServer: 7.  Send DTMF 5\n");
387                                 res = ast_dtmf_stream(chan, NULL, "5", 0);
388                                 fprintf(f, "SEND DTMF 5:   %s\n", (res < 0) ? "FAIL" : "PASS");
389                                 if (res > 0)
390                                         res = 0;
391                         }
392
393                         if (!res) {
394                                 /* Step 8: Transmit tone noise */
395                                 ast_log(LOG_DEBUG, "TestServer: 8.  Transmit tone\n");
396                                 res = sendnoise(chan, 6000);
397                                 fprintf(f, "SENDTONE:      %s\n", (res < 0) ? "FAIL" : "PASS");
398                         }
399                 
400                         if (!res || (res == '7')) {
401                                 /* Step 9: Wait for "7" */
402                                 ast_log(LOG_DEBUG, "TestServer: 9.  Wait DTMF 7\n");
403                                 if (!res)
404                                         res = ast_waitfordigit(chan, 3000);
405                                 fprintf(f, "WAIT DTMF 7:   %s\n", (res != '7') ? "FAIL" : "PASS");
406                                 if (res == '7')
407                                         res = 0;
408                                 else
409                                         res = -1;
410                         }
411                         if (!res)
412                                 res = ast_safe_sleep(chan, 1000);
413                         if (!res) {
414                                 /* Step 10: Send "8" */
415                                 ast_log(LOG_DEBUG, "TestServer: 10.  Send DTMF 8\n");
416                                 res = ast_dtmf_stream(chan, NULL, "8", 0);
417                                 fprintf(f, "SEND DTMF 8:   %s\n", (res < 0) ? "FAIL" : "PASS");
418                                 if (res > 0)
419                                         res = 0;
420                         }
421                         if (!res) {
422                                 /* Step 11: Wait for hangup to arrive! */
423                                 ast_log(LOG_DEBUG, "TestServer: 11.  Waiting for hangup\n");
424                                 res = ast_safe_sleep(chan, 10000);
425                                 fprintf(f, "WAIT HANGUP:   %s\n", (res < 0) ? "PASS" : "FAIL");
426                         }
427
428                         ast_log(LOG_DEBUG, "-- TEST COMPLETE--\n");
429                         fprintf(f, "-- END TEST--\n");
430                         fclose(f);
431                         res = -1;
432                 } else
433                         res = -1;
434         } else {
435                 ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", chan->name);
436                 res = -1;
437         }
438         LOCAL_USER_REMOVE(u);
439         return res;
440 }
441
442 int unload_module(void)
443 {
444         STANDARD_HANGUP_LOCALUSERS;
445         ast_unregister_application(testc_app);
446         return ast_unregister_application(tests_app);
447 }
448
449 int load_module(void)
450 {
451         ast_register_application(testc_app, testclient_exec, testc_synopsis, testc_descrip);
452         return ast_register_application(tests_app, testserver_exec, tests_synopsis, tests_descrip);
453 }
454
455 char *description(void)
456 {
457         return tdesc;
458 }
459
460 int usecount(void)
461 {
462         int res;
463         STANDARD_USECOUNT(res);
464         return res;
465 }
466
467 char *key(void)
468 {
469         return ASTERISK_GPL_KEY;
470 }