File: vpbtoned.cpp

package info (click to toggle)
vpb-driver 4.2.55-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 3,528 kB
  • sloc: cpp: 26,247; ansic: 19,041; sh: 3,210; perl: 1,132; makefile: 752; asm: 182
file content (746 lines) | stat: -rw-r--r-- 18,288 bytes parent folder | download | duplicates (7)
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
/*---------------------------------------------------------------------------*\

    FILE....: VPBTONED.CPP
    TYPE....: C++ Module
    AUTHOR..: David Rowe
    DATE....: 26/2/98

    This file contains the implementation of the programmable tone
    detection API functions, vpb_settonedet and vpb_gettonedet.


         Voicetronix Voice Processing Board (VPB) Software
         Copyright (C) 1999-2008 Voicetronix www.voicetronix.com.au

         This library is free software; you can redistribute it and/or
         modify it under the terms of the GNU Lesser General Public
         License as published by the Free Software Foundation; either
         version 2.1 of the License, or (at your option) any later version.

         This library 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
         Lesser General Public License for more details.

         You should have received a copy of the GNU Lesser General Public
         License along with this library; if not, write to the Free Software
         Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
         MA  02110-1301  USA

\*---------------------------------------------------------------------------*/

#include "vpbtoned.h"
#include "apifunc.h"
#include "mapdev.h"
#include "objtrack.h"
//#include "mess.h"

#include <cstdio>
#include <cstring>
#include <cassert>
#include <climits>


#define	FMIN		100	// minimum tone detection frequency
#define	FMAX		3900	// maximum tone detection frequency
#define	LMAX		0	// maximum tone level in dB
#define	SNRMIN		0	// maximum SNR in dB
#define	TWISTMIN	0	// minimum twist in dB
#define	TSAM		125E-6	// sampling period

#define	SIZEOFHEADERINWORDS	15
#define	STARTNSTATES		12
#define	SIZEOFVPB_STRANINWORDS	4


static VPB_DETECT	*td;	// record of each tone detector for each ch
static unsigned short	*numtd;	// num detector for each ch

static unsigned short	logging = 0;
static unsigned short	logch;
static long		logframes;
static long		logcnt;
static FILE*		logfile;


void check_freq(float f, float bw);
void check_level(float l);
void check_snr(float snr);
void check_twist(float twist);


/*--------------------------------------------------------------------------*\

	FUNCTION.: vpbtoned_open()
	AUTHOR...: David Rowe
	DATE.....: 26/2/98

	Initialises the programmable tone detector module.  This function 
	should be called when the API is initialised.  This function loads
	allocates memory for the tone detector params mirror store in PC
	memory.

\*--------------------------------------------------------------------------*/

void vpbtoned_open(int numdev)
//  int    numdev	total number of channel devices (and hence tone dets)
{
	td = new VPB_DETECT[numdev*VPB_MD];
	numtd = new unsigned short[numdev*VPB_MD];
	for(int i=0; i<numdev; ++i) numtd[i] = 0;
}

/*--------------------------------------------------------------------------*\

	FUNCTION.: vpbtoned_close()
	AUTHOR...: David Rowe
	DATE.....: 26/2/98

	Frees memory used for storing tone detector info.

\*--------------------------------------------------------------------------*/

void vpbtoned_close()
//  int    numdev	total number of channel devices (and hence tone dets)
{
	delete [] td;
	delete [] numtd;
}

/*--------------------------------------------------------------------------*\

	FUNCTION.: check_states()
	AUTHOR...: David Rowe
	DATE.....: 27/2/98

	Validation function for cadence states, also scales ton and toff for
	each state to 10ms increments rather than API level 1ms increments.

\*--------------------------------------------------------------------------*/

static void check_states(const VPB_DETECT &d)
{
	if(d.nstates > VPB_MS)
		throw VpbException("settonedet: too many cadence states (%d)",
				   d.nstates);
	if(d.nstates == 0)
		throw VpbException("settonedet: no cadence states");

	for(int i=0; i < d.nstates; ++i)
		switch( d.stran[i].type )
		{
		    case VPB_TIMER:
		    case VPB_RISING:
		    case VPB_FALLING:
		    case VPB_DELAY:
			break;

		    default:
			throw VpbException("settonedet: invalid cadence state"
					   " (%d = %d)", i, d.stran[i].type);
		}
}

/*--------------------------------------------------------------------------*\

	FUNCTION.: settonedet()
	AUTHOR...: David Rowe
	DATE.....: 26/2/98

	Enables user to set a user programmed tone, or replace an exisiting 
	tone.  Performs translation of user parameters to VPB message.  The
	tones are defined on a per channel basis.

	This version should only be called when the config manager for this
	VPB is not running, use vpb_settonedet() if the config manager is
	running.

\*--------------------------------------------------------------------------*/

void settonedet(int chdev, const VPB_DETECT &d)
//  int		chdev		channel device to add tone to
//  VPB_DETECT	*d		structure containing detector parameters
{
	int	i;
	unsigned short	b,ch;
	int	vpbmodel=-1;

	int id = d.tone_id;

	// determine if a detector with this identifier already exists

	int exists = 0;
	int index = 0;

	for(i=0; i < numtd[chdev]; ++i) {
		if (td[chdev*VPB_MD+i].tone_id == id) {
			exists = 1;
			index = i;
			break;
		}
	}

	// if ident doesn't exist, make new tone

	if( ! exists ) {
		if(numtd[chdev] >= VPB_MD)
			throw VpbException("settonedet: too many detectors");

		index = numtd[chdev]++;
	}

	// validate tone parameters

	if(d.ntones != 1 && d.ntones != 2)
		throw VpbException("settonedet: invalid number of tones (%d)",
				   d.ntones);

	check_freq(d.freq1, d.bandwidth1);
	check_level(d.minlevel1);
	if(d.ntones == 2) {
		check_freq(d.freq2, d.bandwidth2);
		check_level(d.minlevel2);
		check_twist(d.twist);
	}
	check_snr(d.snr);
	check_states(d);

	// OK, translate params into message and send to VPB

	maphndletodev(chdev, &b, &ch);
	uint16_t	mess[COMM_MAX_MESSPCDSP];
	assert(COMM_MAX_MESSPCDSP > (SIZEOFHEADERINWORDS+VPB_MS* SIZEOFVPB_STRANINWORDS));

	mess[0] = (uint16_t)(SIZEOFHEADERINWORDS+d.nstates* SIZEOFVPB_STRANINWORDS);
	mess[1] = (uint16_t)PC_TONED_ST;
	vpbmodel=vpb_c->vpbreg(b)->model;
//	mprintf("settonedet: Model [%d]\n",vpbmodel);
	switch(vpbmodel)
	{
	    case VPB_OPCI:
	    case VPB_OSW:
		mess[2] = (uint16_t)ch;
		break;

	  #ifdef _OPENPRI
	    case VPB_PRI:
                // This may throw...
                mess[2] = get_idx_from_ch(vpb_c->vpbreg(b),ch);
		break;
	  #endif

	    default:
		mess[2] = (uint16_t)objtrack_handle_to_id(CPOBJ,chdev);
	}
	memcpy(&mess[3], &d, sizeof(VPB_DETECT));
//	mprintf("settonedet: Sending message to bd[%d]\n",b);
	vpb_c->PutMessageVPB(b, mess);
//	mprintf("settonedet: Checking for assert on bd[%d]\n",b);

	// keep record of tone detector in PC
	memcpy(&td[chdev*VPB_MD+index], &d, sizeof(VPB_DETECT));
}

/*--------------------------------------------------------------------------*\

	FUNCTION.: deltonedet()
	AUTHOR...: Ben Kramer
	DATE.....: 14/10/04

	Enables user to delete a user programmed tone, only implemented in the
	OpenSwitch cards.

\*--------------------------------------------------------------------------*/

void deltonedet(int chdev, const VPB_DETECT &d)
//  int		chdev		channel device to add tone to
//  VPB_DETECT	*d		structure containing detector parameters
{
	int	i;
	unsigned short	b,ch;

	int id = d.tone_id;

	// determine if a detector with this identifier already exists

	int exists = 0;
//	int index = 0;

	for(i=0; i < numtd[chdev]; ++i) {
		if (td[chdev*VPB_MD+i].tone_id == id) {
			exists = 1;
//			index = i;
			break;
		}
	}

	// if ident doesn't exist, giveup

	if (!exists)
		return;


	// OK, translate params into message and send to VPB

	uint16_t	mess[COMM_MAX_MESSPCDSP];
	assert(COMM_MAX_MESSPCDSP > (SIZEOFHEADERINWORDS+VPB_MS*
				SIZEOFVPB_STRANINWORDS));

	mess[0] = (uint16_t)(SIZEOFHEADERINWORDS+d.nstates*
			 SIZEOFVPB_STRANINWORDS);
	mess[1] = (uint16_t)PC_TONED_ST_DEL;
	mess[2] = (uint16_t)objtrack_handle_to_id(CPOBJ,chdev);
	memcpy(&mess[3], &d, sizeof(VPB_DETECT));
	maphndletodev(chdev, &b, &ch);
	vpb_c->PutMessageVPB(b, mess);
//	//mprintf(" settonedet half way\n");

	// delete record of tone detector in PC
/*
	if (!exists) {
		memcpy(&td[chdev*VPB_MD+index], d, sizeof(VPB_DETECT));
	}
*/
}

void WINAPI vpb_settonedet(VPBPortHandle handle, const VPB_DETECT &d)
{
	CheckHandle(handle, "vpb_settonedet");
	settonedet(handle, d);
}

/*--------------------------------------------------------------------------*\

	FUNCTION.: vpb_gettonedet()
	AUTHOR...: David Rowe
	DATE.....: 27/2/98

	Enables user to extract a tone detectors parameters.

\*--------------------------------------------------------------------------*/

void WINAPI vpb_gettonedet(VPBPortHandle handle, int id, VPB_DETECT *d)
//  int		handle channel device holding tone detector
//  int		id     tone identifier
//  VPB_DETECT  *d     structure containing detector parameters
{
	CheckHandle(handle, "vpb_gettonedet");

	// determine if a detector with this identifier already exists

	int exists = 0;
	int index = 0;

	for(int i=0; i < numtd[handle]; ++i) {
		if (td[handle * VPB_MD + i].tone_id == id) {
			exists = 1;
			index = i;
			break;
		}
	}

	if( ! exists )
		throw VpbException("vpb_gettonedet: invalid id (%d)", id);

	// copy tone params from PC mirror

	memcpy(d, &td[handle * VPB_MD + index], sizeof(VPB_DETECT));
}

/*--------------------------------------------------------------------------*\

	FUNCTION.: vpb_deltonedet()
	AUTHOR...: Ben Kramer
	DATE.....: 14/10/04

	Enables user to delete a user programmed tone, this is only
	implemented for use with the host side tone detector.

    int		chdev	channel device to add tone to
    VPB_DETECT	*d      structure containing detector parameters

\*--------------------------------------------------------------------------*/

void WINAPI vpb_deltonedet(VPBPortHandle handle, const VPB_DETECT &d)
{
	CheckHandle(handle, "vpb_deltonedet");
	deltonedet(handle, d);
}

/*--------------------------------------------------------------------------*\

	FUNCTION.: check_freq()
	AUTHOR...: David Rowe
	DATE.....: 27/2/98

	Frequency validation function.

\*--------------------------------------------------------------------------*/

void check_freq(float f, float bw)
//  float f
//  float bw
{
	bw /= 2;
	if ( ((f-bw) < FMIN) || ((f+bw) > FMAX) )
		throw Wobbly(VPBAPI_DETECT_INVALID_FREQUENCY);
}

/*--------------------------------------------------------------------------*\

	FUNCTION.: check_level()
	AUTHOR...: David Rowe
	DATE.....: 27/2/98

	Level validation function.

\*--------------------------------------------------------------------------*/

void check_level(float l)
//	float	l	Level in dB with maximum of LMAX dB
{
	if (l > LMAX)
		throw Wobbly(VPBAPI_DETECT_INVALID_LEVEL);
}

/*--------------------------------------------------------------------------*\

	FUNCTION.: check_snr()
	AUTHOR...: David Rowe
	DATE.....: 27/2/98

	SNR validation function.

\*--------------------------------------------------------------------------*/

void check_snr(float snr)
//	float	snr		SNR in dB with minimum of SNRMIN dB
{
	if (snr < SNRMIN)
		throw Wobbly(VPBAPI_DETECT_INVALID_SNR);
}

/*--------------------------------------------------------------------------*\

	FUNCTION.: check_twist()
	AUTHOR...: David Rowe
	DATE.....: 27/2/98

	Twist validation function.

\*--------------------------------------------------------------------------*/

void check_twist(float twist)
//	float	twist		twist in dB with minimum of TWISTMIN dB
{
	if (twist < TWISTMIN)
		throw Wobbly(VPBAPI_DETECT_INVALID_TWIST);
}

/*--------------------------------------------------------------------------*\

	FUNCTION.: vpb_debug_tonedet()
	AUTHOR...: David Rowe
	DATE.....: 29/4/98

	Instructs API to produce a debug file of tone detector parameters for
	one channel. 

\*--------------------------------------------------------------------------*/

int WINAPI vpb_debug_tonedet(int chdev, int id, char file_name[], int sec)
//	int	chdev		channel to log debug info for
//	int	id		ID of tone detector to log
//	char	file_name[]	name of file where debug info is to be written
//	int	sec		number of seconds of info to log
{

	try {
		if (logging)
			throw Wobbly(VPBAPI_DETECT_ALREADY_LOGGING);
		logging = 1;

		logfile = fopen(file_name, "wt");
		if (logfile == NULL)
			throw Wobbly(VPBAPI_DETECT_BAD_LOG_FILE_NAME);

		// set up statics

		unsigned short b,ch;
		maphndletodev(chdev, &b, &ch);

		logch = chdev;
		VPBREG* pvpbreg = vpb_c->vpbreg(b);
		logframes = (long)sec*8000l/(pvpbreg->lsf);
		if (logframes > SHRT_MAX) logframes = SHRT_MAX;
		logcnt = 0;

		// tell DSP to switch debugging on

		uint16_t	mess[PC_LTONED_DEBUG];
		mess[0] = PC_LTONED_DEBUG;
		mess[1] = PC_TONED_DEBUG;
		mess[2] = objtrack_handle_to_id(CPOBJ, chdev);
		mess[3] = id;
		mess[4] = (uint16_t)logframes;
		vpb_c->PutMessageVPB(b, mess);
	}
	catch(const Wobbly &w){
		return(RunTimeError(w,"vpb_debug_tonedet"));
	}
	return VPB_OK;
}

/*--------------------------------------------------------------------------*\

	FUNCTION.: toned_debug_log
	AUTHOR...: David Rowe
	DATE.....: 29/4/98

	This function is called from the MMQ to iterate the debug logging
	data.

\*--------------------------------------------------------------------------*/

void toned_debug_log(unsigned short board, uint16_t mess[])
//	unsigned short	board
//	uint16_t	mess[]		messge from DSP
{
	unsigned short	chdev;

	// validate

	assert(logging);
	assert(mess[1] == DSP_TONED_LOG);
	assert(logfile != NULL);
	assert(logframes);
	assert(mess[4] == logcnt);

	// extract params from message and print to text file

	chdev = objtrack_id_to_handle(CPOBJ, mess[2], board);
	assert(chdev == logch);

	fprintf(logfile,"[%06ld] ch:%02d id:%02d t1:%d t2:%d tw:%d snr:%d t:%d "
		"ton:%5d toff:%5d r:%d f:%d s:%02d ns:%02d\n",
					logcnt,
					chdev,
					mess[3],	// tone id
					mess[5],	// tone1
					mess[6],	// tone2
					mess[7],	// twist
					mess[8],	// snr
					mess[9],	// tone

					mess[10],	// tone_on
					mess[11],	// tone_off
					mess[12],	// rising
					mess[13],	// falling
					mess[14],	// state
					mess[15]	// next_state
			);

	// if we have finished logging close file and send event to API

	logcnt++;
	if (logframes == logcnt) {
		logging = 0;
		fclose(logfile);

		VPB_EVENT e;
		e.type = VPB_TONE_DEBUG_END;
		e.handle = logch;
		e.data = 0;
		putevt(&e, 0);
	}
}



//XXX All this lot is completely unused now right?
#if 0
//{{{
// message describing state transitions for Studio 308 ring 
static short toned_ring[] ={
    0,				// work out length at run time	
    PC_TONED_ST,
    0,				// ID				

    3,				// number of cadence states	
    1,				// event code			
    1,				// number of tones		
    425,			// f1:  centre frequency	
    100,			// bw1: bandwidth		
    0,				// f2: N/A			
    0,				// bw2: N/A			
    -20,			// A1: -10 dbm0			
    0,				// A2: N/A 			
    0,				// twist: N/A			
    10,				// SNR: 10dB			

    RISING,			// state 0			
    0,
    0,
    0,

    FALLING,		// state 1			
    0,
    80,				// 800ms ton min		
    120,			// 1200ms ton max		

    RISING,			// state 2			
    0,
    360,			// 3800ms toff min		
    440,			// 4200ms toff max		
};


static short toned_busy[] ={
    0,				// work out length at run time	
    PC_TONED_ST,
    0,				// ID				

    3,				// number of states		
    2,				// event code			
    1,				// number of tones		
    425,			// f1:  centre frequency	
    100,			// bw1: bandwidth		
    0,				// f2: N/A			
    0,				// bw2: N/A			
    -20,			// A1: -10 dbm0			
    0,				// A2: N/A 			
    0,				// twist: N/A			
    10,				// SNR: 10dB			

    RISING,			// state 0			
    0,
    0,
    0,

    FALLING,		// state 1			
    0,
    45,				// 450ms ton min		
    55,				// 550ms ton max		

    RISING,			// state 2			
    0,
    45,				// 450ms toff min		
    55,				// 550ms toff max		
};

// message describing state transitions for DTMF tone 

static short toned_dtmf[] ={
    0,				// work out length at run time	
    PC_TONED_ST,
    0,				// ID				

    2,				// number of states		
    3,				// event code			
    2,				// number of tones		
    697,			// f1:  697Hz			
    100,			// bw1: 100 Hz			
    1209,			// f2: 1209Hz			
    100,			// bw2: 100 Hz			
    -20,			// A1: -10 dbm0			
    -20,			// A2: -10 dbm0 		
    10,				// allowable twist: 10dB	
    10,				// SNR: 10dB			

    RISING,			// state 0			
    0,
    0,
    0,

    FALLING,		// state 1			
    0,
    5,				// 50ms ton min			
    15,				// 150ms ton max		
};
#endif

#ifdef AUSTEL

// message describing state transitions for AUSTEL ring 

static short toned_ring[] ={
    0,				// work out length at run time	
    PC_TONED_ST,
    0,				// ID				

    4,				// number of cadence states	
    1,				// event code			
    1,				// number of tones		
    425,			// f1:  centre frequency	
    100,			// bw1: bandwidth		
    0,				// f2: N/A			
    0,				// bw2: N/A			
    -20,			// A1: -10 dbm0			
    0,				// A2: N/A 			
    0,				// twist: N/A			
    10,				// SNR: 10dB			

    RISING,			// state 0			
    0,
    0,
    0,

    FALLING,		// state 1			
    0,
    32,				// 320ms ton min		
    48,				// 480ms ton max		

    RISING,			// state 2			
    0,
    16,				// 160ms toff min		
    24,				// 240ms toff max		

    FALLING,			// state 3			
    0,
    32,				// 320ms ton min		
    48				// 480ms ton max		

};

// message describing state transitions for AUSTEL busy 

static short toned_busy[] ={
    0,				// work out length at run time	
    PC_TONED_ST,
    0,				// tone detector object ID				

    4,				// number of states		
    2,				// event code			
    1,				// number of tones		
    425,			// f1:  centre frequency	
    100,			// bw1: bandwidth		
    0,				// f2: N/A			
    0,				// bw2: N/A			
    -10,			// A1: -10 dbm0			
    0,				// A2: N/A 			
    0,				// twist: N/A			
    10,				// SNR: 10dB			

    RISING,			// state 0			
    0,
    0,
    0,

    FALLING,		// state 1			
    0,
    30,				// 300ms ton min		
    45,				// 450ms ton max		

    RISING,			// state 2			
    0,
    30,				// 300ms toff min		
    45,				// 450ms toff max		

    FALLING,		// state 3			
    0,
    30,				// 300ms ton min		
    45,				// 450ms ton max		

};
//}}}
#endif