~ubuntu-branches/ubuntu/quantal/zaptel/quantal

« back to all changes in this revision

Viewing changes to kernel/wctc4xxp/codec_test.c

  • Committer: Bazaar Package Importer
  • Author(s): Tzafrir Cohen
  • Date: 2008-08-28 22:58:23 UTC
  • mfrom: (11.1.11 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080828225823-r8bdunirm8hmc76m
Tags: 1:1.4.11~dfsg-2
* Patch xpp_fxs_power: Fixed an issue with hook detection of the Astribank
  FXS module.
* Don't fail init.d script if fxotune fails. This may happen if running it
  when Asterisk is already running.
* Bump standards version to 3.8.0.0 .
* Ignore false lintian warning ("m-a a-i" has "a a").
* Patch xpp_fxo_cid_always: do always pass PCM if that's what the user
  asked.
* Patch vzaphfc_proc_root_dir: fix vzaphfc on 2.6.26.
* Patch wcte12xp_flags: Proper time for irq save flags.
* Patch headers_2627: Fix location of semaphore.h for 2.6.27 .
* Patch xpp_fxs_dtmf_leak: Don't play DTMFs to the wrong channel.
* Patch wctdm_fix_alarm: Fix sending channel alarms.
* Patch device_class_2626: Fix building 2.6.26 (Closes: #493397).
* Using dh_lintian for lintian overrides, hence requiring debhelper 6.0.7.
* Lintian: we know we have direct changes. Too bad we're half-upstream :-(
* Fix doc-base section names. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Wilcard TC400B Digium Transcoder Engine Interface Driver for Zapata Telephony interface test tool.
 
3
 *
 
4
 * Written by Matt O'Gorman <mogorman@digium.com>
 
5
 *
 
6
 * Copyright (C) 2006-2007, Digium, Inc.
 
7
 *
 
8
 * All rights reserved.
 
9
 *
 
10
 * This program is free software; you can redistribute it and/or modify
 
11
 * it under the terms of the GNU General Public License as published by
 
12
 * the Free Software Foundation; either version 2 of the License, or
 
13
 * (at your option) any later version.
 
14
 * 
 
15
 * This program is distributed in the hope that it will be useful,
 
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
 * GNU General Public License for more details.
 
19
 * 
 
20
 * You should have received a copy of the GNU General Public License
 
21
 * along with this program; if not, write to the Free Software
 
22
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
 
23
 *
 
24
 */
 
25
 
 
26
 
 
27
 
 
28
#include <fcntl.h>
 
29
#include <stdlib.h>
 
30
#include <unistd.h>
 
31
#include <netinet/in.h>
 
32
#include <string.h>
 
33
#include <stdio.h>
 
34
#include <sys/ioctl.h>
 
35
#include <errno.h>
 
36
#include <sys/mman.h>
 
37
#include <unistd.h>
 
38
#ifdef STANDALONE_ZAPATA
 
39
#include "zaptel.h"
 
40
#else
 
41
#include <linux/zaptel.h>
 
42
#endif
 
43
 
 
44
#define MAX_CARDS_TO_TEST       6
 
45
#define MAX_CHANNELS_PER_CARD   96
 
46
 
 
47
#define AST_FORMAT_ULAW         (1 << 2)
 
48
#define AST_FORMAT_G729A        (1 << 8)
 
49
 
 
50
#define AST_FRIENDLY_OFFSET     64
 
51
 
 
52
static int debug = 0;
 
53
 
 
54
static unsigned char ulaw_slin_ex[] = {
 
55
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 
56
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 
57
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 
58
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 
59
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 
60
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 
61
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 
62
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 
63
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 
64
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
 
65
};
 
66
 
 
67
static unsigned char g729a_expected[] = {
 
68
        0xE9, 0x88, 0x4C, 0xA0, 0x00, 0xFA, 0xDD, 0xA2, 0x06, 0x2D,
 
69
        0x69, 0x88, 0x00, 0x60, 0x68, 0xD5, 0x9E, 0x20, 0x80, 0x50
 
70
};
 
71
 
 
72
 
 
73
struct format_map {
 
74
        unsigned int map[32][32];
 
75
};
 
76
 
 
77
 
 
78
struct tcpvt {
 
79
        int fd;
 
80
        int fake;
 
81
        int inuse;
 
82
        struct zt_transcode_header *hdr;
 
83
//      struct ast_frame f;
 
84
};
 
85
 
 
86
struct tctest_info {
 
87
        int numcards;
 
88
        int numchans[MAX_CARDS_TO_TEST];
 
89
        int total_chans;
 
90
        int errors;
 
91
        int overcnt_error;      /* Too many cards found */
 
92
        int undercnt_error;     /* Too few cards found */
 
93
        int timeout_error[MAX_CARDS_TO_TEST];
 
94
        int data_error[MAX_CARDS_TO_TEST];
 
95
        int numcards_werrors;
 
96
};
 
97
 
 
98
 
 
99
static int find_transcoders(struct tctest_info *tctest_info)
 
100
{
 
101
        struct zt_transcode_info info = { 0, };
 
102
        int fd, res;
 
103
 
 
104
        if ((fd = open("/dev/zap/transcode", O_RDWR)) < 0) {
 
105
                printf("Warning: No Zaptel transcoder support!\n");
 
106
                return 0;
 
107
        }
 
108
 
 
109
        tctest_info->total_chans = 0;
 
110
        info.op = ZT_TCOP_GETINFO;
 
111
        for (info.tcnum = 0; !(res = ioctl(fd, ZT_TRANSCODE_OP, &info)); info.tcnum++) {
 
112
                if (debug)
 
113
                        printf("Found transcoder %d, '%s' with %d channels.\n", info.tcnum, info.name, info.numchannels);
 
114
                if ((info.tcnum % 2) == 0)
 
115
                {
 
116
                        tctest_info->numchans[info.tcnum/2] = info.numchannels;
 
117
                        tctest_info->total_chans += info.numchannels;
 
118
                }
 
119
        }
 
120
        tctest_info->numcards = info.tcnum / 2;
 
121
 
 
122
        close(fd);
 
123
        if (!info.tcnum)
 
124
                printf("No hardware transcoders found.\n");
 
125
        return 0;
 
126
}
 
127
 
 
128
 
 
129
static int open_transcoder(struct tcpvt *ztp, int dest, int source)
 
130
{
 
131
        int fd;
 
132
        unsigned int x = ZT_TCOP_ALLOCATE;
 
133
        struct zt_transcode_header *hdr;
 
134
        int flags;
 
135
 
 
136
        if ((fd = open("/dev/zap/transcode", O_RDWR)) < 0)
 
137
                return -1;
 
138
        flags = fcntl(fd, F_GETFL);
 
139
        if (flags > - 1) {
 
140
                if (fcntl(fd, F_SETFL, flags | O_NONBLOCK))
 
141
                        printf("Could not set non-block mode!\n");
 
142
        }
 
143
 
 
144
        if ((hdr = mmap(NULL, sizeof(*hdr), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) {
 
145
                printf("Memory Map failed for transcoding (%s)\n", strerror(errno));
 
146
                close(fd);
 
147
 
 
148
                return -1;
 
149
        }
 
150
 
 
151
        if (hdr->magic != ZT_TRANSCODE_MAGIC) {
 
152
                printf("Transcoder header (%08x) wasn't magic.  Abandoning\n", hdr->magic);
 
153
                munmap(hdr, sizeof(*hdr));
 
154
                close(fd);
 
155
 
 
156
                return -1;
 
157
        }
 
158
 
 
159
        hdr->srcfmt = source;
 
160
        hdr->dstfmt = dest;
 
161
 
 
162
        if (ioctl(fd, ZT_TRANSCODE_OP, &x)) {
 
163
                printf("Unable to attach transcoder: %s\n", strerror(errno));
 
164
                munmap(hdr, sizeof(*hdr));
 
165
                close(fd);
 
166
 
 
167
                return -1;
 
168
        }
 
169
 
 
170
        ztp->fd = fd;
 
171
        ztp->hdr = hdr;
 
172
 
 
173
        return 0;
 
174
}
 
175
 
 
176
 
 
177
static void close_transcoder(struct tcpvt *ztp)
 
178
{
 
179
        unsigned int x;
 
180
 
 
181
        x = ZT_TCOP_RELEASE;
 
182
        if (ioctl(ztp->fd, ZT_TRANSCODE_OP, &x))
 
183
                printf("Failed to release transcoder channel: %s\n", strerror(errno));
 
184
                                
 
185
        munmap(ztp->hdr, sizeof(*ztp->hdr));
 
186
        close(ztp->fd);
 
187
}
 
188
 
 
189
 
 
190
static int encode_packet(struct tcpvt *ztp, unsigned char *packet_in, unsigned char *packet_out)
 
191
{
 
192
        struct zt_transcode_header *hdr = ztp->hdr;
 
193
        unsigned int x;
 
194
 
 
195
        hdr->srcoffset = 0;
 
196
 
 
197
        memcpy(hdr->srcdata + hdr->srcoffset + hdr->srclen, packet_in, 160);
 
198
        hdr->srclen += 160;
 
199
 
 
200
 
 
201
        hdr->dstoffset = AST_FRIENDLY_OFFSET;
 
202
        x = ZT_TCOP_TRANSCODE;
 
203
        if (ioctl(ztp->fd, ZT_TRANSCODE_OP, &x))
 
204
                printf("Failed to transcode: %s\n", strerror(errno));
 
205
 
 
206
        usleep(20000);
 
207
        if (hdr->dstlen)
 
208
        {
 
209
                memcpy(packet_out, hdr->dstdata + hdr->dstoffset, hdr->dstlen);
 
210
                return 0;
 
211
        }
 
212
        else
 
213
                return -1;
 
214
}
 
215
 
 
216
 
 
217
static void print_failed()
 
218
{
 
219
        printf("______    ___    _____   _       _____  ______\n");
 
220
        printf("|  ___|  / _ \\  |_   _| | |     |  ___| |  _  \\\n");
 
221
        printf("| |_    / /_\\ \\   | |   | |     | |__   | | | |\n");
 
222
        printf("|  _|   |  _  |   | |   | |     |  __|  | | | |\n");
 
223
        printf("| |     | | | |  _| |_  | |____ | |___  | |/ /\n");
 
224
        printf("\\_|     \\_| |_/  \\___/  \\_____/ \\____/  |___/ \n");
 
225
}
 
226
 
 
227
 
 
228
int main(int argc, char *argv[])
 
229
{
 
230
        int arg1, arg2, i, j, card_testing, chan_testing;
 
231
        struct tcpvt ztp[MAX_CHANNELS_PER_CARD * MAX_CARDS_TO_TEST];
 
232
        unsigned char packet_out[200];
 
233
        struct tctest_info tctest_info;
 
234
 
 
235
        memset(&tctest_info, 0, sizeof(tctest_info));
 
236
 
 
237
        if ((argc < 2) || (argc > 3))
 
238
        {
 
239
                printf("codec_test requires one argument.\n");
 
240
                printf("     arg1 = number of cards to test\n");
 
241
                return -1;
 
242
        }
 
243
 
 
244
        if (argc == 2)
 
245
                sscanf(argv[1], "%d", &arg1);
 
246
        else if (argc == 3)
 
247
        {
 
248
                sscanf(argv[1], "%d %d", &arg1, &arg2);
 
249
                debug = arg2;
 
250
        }
 
251
 
 
252
        printf("Beginning test of %d TC400B cards\n", arg1);
 
253
 
 
254
 
 
255
        /* Search for TC400Bs */
 
256
        find_transcoders(&tctest_info);
 
257
 
 
258
        if (tctest_info.numcards > arg1)
 
259
        {
 
260
                tctest_info.errors++;
 
261
                tctest_info.overcnt_error = 1;
 
262
        }
 
263
        if (tctest_info.numcards < arg1)
 
264
        {
 
265
                tctest_info.errors++;
 
266
                tctest_info.undercnt_error = 1;
 
267
        }
 
268
 
 
269
        if (tctest_info.errors == 0)
 
270
        {
 
271
                /* Begin testing transcoder channels */
 
272
                for (card_testing = 0; card_testing < tctest_info.numcards; card_testing++)
 
273
                {
 
274
                        tctest_info.data_error[card_testing] = 0;
 
275
                        tctest_info.timeout_error[card_testing] = 0;
 
276
                        for (chan_testing = 0; chan_testing < tctest_info.numchans[card_testing]; chan_testing++)
 
277
                        {
 
278
                                i = chan_testing;
 
279
                                for(j = 0; j < card_testing; j++)
 
280
                                        i += tctest_info.numchans[j];
 
281
 
 
282
                                open_transcoder(&ztp[i], AST_FORMAT_G729A, AST_FORMAT_ULAW);
 
283
 
 
284
                                if ((tctest_info.timeout_error[card_testing] = encode_packet(&ztp[i], ulaw_slin_ex, packet_out) == -1))
 
285
                                        tctest_info.errors++;
 
286
 
 
287
                                if (memcmp(g729a_expected, packet_out, 20) != 0)
 
288
                                {
 
289
                                        tctest_info.errors++;
 
290
                                        tctest_info.data_error[card_testing] += 1;
 
291
                                }
 
292
                        }
 
293
                        if ( (tctest_info.data_error[card_testing]) || (tctest_info.timeout_error[card_testing]) )
 
294
                                tctest_info.numcards_werrors++;
 
295
                }
 
296
 
 
297
                for (i = 0; i < tctest_info.total_chans; i++)
 
298
                        close_transcoder(&ztp[i]);
 
299
        }
 
300
 
 
301
        if (debug)
 
302
        {
 
303
                printf("\n\n");
 
304
                printf("tctest_info.errors = %d\n", tctest_info.errors);
 
305
                printf("tctest_info.overcnt_error = %d\n", tctest_info.overcnt_error);
 
306
                printf("tctest_info.undercnt_error = %d\n", tctest_info.undercnt_error);
 
307
                printf("tctest_info.numcards_werrors = %d\n", tctest_info.numcards_werrors);
 
308
 
 
309
                for (i = 0; i < tctest_info.numcards; i++)
 
310
                {
 
311
                        printf("tctest_info.data_error[%d] = %d\n", i, tctest_info.data_error[i]);
 
312
                        printf("tctest_info.timeout_error[%d] = %d\n", i, tctest_info.timeout_error[i]);
 
313
                }
 
314
        }
 
315
 
 
316
        if (tctest_info.errors)
 
317
        {
 
318
                printf("\n\n\n");
 
319
                if (tctest_info.numcards_werrors)
 
320
                        printf("%d of %d cards\n", tctest_info.numcards_werrors, tctest_info.numcards); 
 
321
                print_failed();
 
322
                if (tctest_info.overcnt_error)
 
323
                        printf("\n%d cards found, %d expected\n", tctest_info.numcards, arg1);
 
324
                if (tctest_info.undercnt_error)
 
325
                        printf("\n%d cards found, %d expected\n", tctest_info.numcards, arg1);
 
326
                printf("\n\n\n");
 
327
        }
 
328
        else
 
329
                printf("%d of %d cards PASSED\n", tctest_info.numcards - tctest_info.numcards_werrors, tctest_info.numcards);   
 
330
 
 
331
        return 0;
 
332
}