-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathasttapi.cpp
More file actions
3052 lines (2580 loc) · 77 KB
/
asttapi.cpp
File metadata and controls
3052 lines (2580 loc) · 77 KB
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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Name: asttapi.cpp
Copyright: Under the GNU General Public License Version 2 or later (the "GPL")
Author: Nick Knight
Klaus Darilion
Description:
*/
/* ***** BEGIN LICENSE BLOCK *****
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Asttapi.
*
* The Initial Developer of the Original Code is
* Nick Knight.
* Portions created by the Initial Developer are Copyright (C) 2005
* the Initial Developer. All Rights Reserved.
*
* Contributor(s): Klaus Darilion (enum.at)
*
* ***** END LICENSE BLOCK ***** */
// The debugger can't handle symbols more than 255 characters long.
// STL often creates symbols longer than that.
// When symbols are longer than 255 characters, the warning is issued.
#pragma warning(disable:4786)
#include "dll.h"
#include <windows.h>
#include "tapiastmanager.h"
#include "asttspglue.h"
#include <stdio.h>
#include <wchar.h>
#include <atlconv.h>
//For debug
#include "WaveTsp.h"
//our resources
#include "resource.h"
//misc functions
#include "utilities.h"
#include <map>
// #include <boost/regex.hpp>
////////////////////////////////////////////////////////////////////////////////
//
// Globals
//
// Keep track of all the globals we require
//
////////////////////////////////////////////////////////////////////////////////
// TODO Need to make these members of a class so that we can have multiple lines.
//Function call for asyncrounous functions
ASYNC_COMPLETION g_pfnCompletionProc = 0;
//LINEEVENT g_pfnEventProc = 0;
//HTAPILINE g_htLine = 0;
//HTAPICALL g_htCall = 0;
DWORD g_dwPermanentProviderID = 0;
DWORD g_dwLineDeviceIDBase = 0;
HPROVIDER g_hProvider = 0;
//For our windows...
HINSTANCE g_hinst = 0;
//Keep track of our lines which we have opened (aka sockets to the server)
typedef std::map<HDRVLINE, tapiAstManager*> mapLine;
mapLine trackLines;
HDRVLINE lastValue = 0;
Mutex lineMut;
// { dwDialPause, dwDialSpeed, dwDigitDuration, dwWaitForDialtone }
LINEDIALPARAMS g_dpMin = { 100, 50, 100, 100 };
LINEDIALPARAMS g_dpDef = { 250, 50, 250, 500 };
LINEDIALPARAMS g_dpMax = { 1000, 50, 1000, 1000 };
////////////////////////////////////////////////////////////////////////////////
// Function DllMain
//
// Dll entry
//
////////////////////////////////////////////////////////////////////////////////
BOOL WINAPI DllMain(
HINSTANCE hinst,
DWORD dwReason,
void* /*pReserved*/)
{
if( dwReason == DLL_PROCESS_ATTACH )
{
g_hinst = hinst;
}
return TRUE;
}
LONG TSPIAPI TSPI_providerInit(
DWORD dwTSPIVersion,
DWORD dwPermanentProviderID,
DWORD dwLineDeviceIDBase,
DWORD dwPhoneDeviceIDBase,
DWORD_PTR dwNumLines,
DWORD_PTR dwNumPhones,
ASYNC_COMPLETION lpfnCompletionProc,
LPDWORD lpdwTSPIOptions // TSPI v2.0
)
{
BEGIN_PARAM_TABLE("TSPI_providerInit")
DWORD_IN_ENTRY(dwTSPIVersion)
DWORD_IN_ENTRY(dwPermanentProviderID)
DWORD_IN_ENTRY(dwLineDeviceIDBase)
DWORD_IN_ENTRY(dwPhoneDeviceIDBase)
DWORD_OUT_ENTRY(dwNumLines)
DWORD_OUT_ENTRY(dwNumPhones)
END_PARAM_TABLE()
WSADATA info;
//TSPTRACE("Setting up sockets");
// //First off initialize sockets - which we have to do under Win32
// if (WSAStartup(MAKELONG(1, 1), &info) == SOCKET_ERROR) {
// TSPTRACE("Failed to setup sockets");
// WSACleanup();
// return EPILOG(1);
// }
//TSPTRACE("Sockets setup sucsesfully");
//Record all of the globals we need
g_pfnCompletionProc = lpfnCompletionProc;
//other params we need to track
g_dwPermanentProviderID = dwPermanentProviderID;
g_dwLineDeviceIDBase = dwLineDeviceIDBase;
return (0);
}
////////////////////////////////////////////////////////////////////////////////
// Function TSPI_providerShutdown
//
// Shutdown and clean up.
//
////////////////////////////////////////////////////////////////////////////////
LONG TSPIAPI TSPI_providerShutdown(
DWORD dwTSPIVersion,
DWORD dwPermanentProviderID // TSPI v2.0
)
{
BEGIN_PARAM_TABLE("TSPI_providerShutdown")
DWORD_IN_ENTRY(dwTSPIVersion)
DWORD_IN_ENTRY(dwPermanentProviderID)
END_PARAM_TABLE()
//Clean up our sockets
WSACleanup();
return EPILOG(0);
}
////////////////////////////////////////////////////////////////////////////////
//
// Capabilities
//
// TAPI will ask us what our capabilities are
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Function TSPI_lineNegotiateTSPIVersion
//
//
//
////////////////////////////////////////////////////////////////////////////////
LONG TSPIAPI TSPI_lineNegotiateTSPIVersion(
DWORD dwDeviceID,
DWORD dwLowVersion,
DWORD dwHighVersion,
LPDWORD lpdwTSPIVersion)
{
BEGIN_PARAM_TABLE("TSPI_lineNegotiateTSPIVersion")
DWORD_IN_ENTRY(dwDeviceID)
DWORD_IN_ENTRY(dwLowVersion)
DWORD_IN_ENTRY(dwHighVersion)
DWORD_OUT_ENTRY(lpdwTSPIVersion)
END_PARAM_TABLE()
LONG tr = 0;
if ( dwLowVersion <= TAPI_CURRENT_VERSION )
{
#define MIN(a, b) (a < b ? a : b)
*lpdwTSPIVersion = MIN(TAPI_CURRENT_VERSION,dwHighVersion);
}
else
{
tr = LINEERR_INCOMPATIBLEAPIVERSION;
}
return EPILOG(tr);
}
////////////////////////////////////////////////////////////////////////////////
// Function TSPI_providerEnumDevices
//
//
//
////////////////////////////////////////////////////////////////////////////////
LONG TSPIAPI TSPI_providerEnumDevices(
DWORD dwPermanentProviderID,
LPDWORD lpdwNumLines,
LPDWORD lpdwNumPhones,
HPROVIDER hProvider,
LINEEVENT lpfnLineCreateProc,
PHONEEVENT lpfnPhoneCreateProc)
{
BEGIN_PARAM_TABLE("TSPI_providerEnumDevices")
DWORD_IN_ENTRY(dwPermanentProviderID)
DWORD_OUT_ENTRY(lpdwNumLines)
DWORD_OUT_ENTRY(lpdwNumPhones)
DWORD_IN_ENTRY(hProvider)
DWORD_IN_ENTRY(lpfnLineCreateProc)
DWORD_IN_ENTRY(lpfnPhoneCreateProc)
END_PARAM_TABLE()
g_hProvider = hProvider;
*lpdwNumLines = 1;
/* // fix for Windows 8
*lpdwNumPhones = 0;
*/
*lpdwNumPhones = 1;
return EPILOG(0);
}
////////////////////////////////////////////////////////////////////////////////
// Function TSPI_lineGetDevCaps
//
// Allows TAPI to check our line capabilities before placing a call
//
////////////////////////////////////////////////////////////////////////////////
LONG TSPIAPI TSPI_lineGetDevCaps(
DWORD dwDeviceID,
DWORD dwTSPIVersion,
DWORD dwExtVersion,
LPLINEDEVCAPS pldc
)
{
BEGIN_PARAM_TABLE("TSPI_lineGetDevCaps")
DWORD_IN_ENTRY(dwDeviceID)
DWORD_IN_ENTRY(dwTSPIVersion)
DWORD_IN_ENTRY(dwExtVersion)
DWORD_IN_ENTRY(pldc)
END_PARAM_TABLE()
LONG tr = 0;
const wchar_t szProviderInfo[] = L"SIPTAPI, a SIP TAPI provider for click2dial";
const wchar_t szLineName[] = L"SIPTAPI " SIPTAPI_VERSION_W;
pldc->dwNeededSize = sizeof(LINEDEVCAPS) +
sizeof(szProviderInfo) +
sizeof(szLineName);
if( pldc->dwNeededSize <= pldc->dwTotalSize )
{
pldc->dwUsedSize = pldc->dwNeededSize;
pldc->dwProviderInfoSize = sizeof(szProviderInfo);
pldc->dwProviderInfoOffset = sizeof(LINEDEVCAPS) + 0;
wchar_t* pszProviderInfo = (wchar_t*)((BYTE*)pldc + pldc->dwProviderInfoOffset);
wcscpy(pszProviderInfo, szProviderInfo);
pldc->dwLineNameSize = sizeof(szLineName);
pldc->dwLineNameOffset = sizeof(LINEDEVCAPS) + sizeof(szProviderInfo);
wchar_t* pszLineName = (wchar_t*)((BYTE*)pldc + pldc->dwLineNameOffset);
wcscpy(pszLineName, szLineName);
}
else
{
pldc->dwUsedSize = sizeof(LINEDEVCAPS);
}
pldc->dwStringFormat = STRINGFORMAT_ASCII;
// Microsoft recommended algorithm for
// calculating the permanent line ID
#define MAKEPERMLINEID(dwPermProviderID, dwDeviceID) \
((LOWORD(dwPermProviderID) << 16) | dwDeviceID)
pldc->dwPermanentLineID = MAKEPERMLINEID(g_dwPermanentProviderID, dwDeviceID - g_dwLineDeviceIDBase);
pldc->dwAddressModes = LINEADDRESSMODE_ADDRESSID;
pldc->dwNumAddresses = 1;
pldc->dwBearerModes = LINEBEARERMODE_VOICE;
pldc->dwMediaModes = LINEMEDIAMODE_INTERACTIVEVOICE;
pldc->dwGenerateDigitModes= LINEDIGITMODE_DTMF;
pldc->dwDevCapFlags = LINEDEVCAPFLAGS_CLOSEDROP;
pldc->dwMaxNumActiveCalls = 1;
pldc->dwLineFeatures = LINEFEATURE_MAKECALL;
// DialParams
pldc->MinDialParams = g_dpMin;
pldc->MaxDialParams = g_dpMax;
pldc->DefaultDialParams = g_dpDef;
return EPILOG(tr);
}
////////////////////////////////////////////////////////////////////////////////
// Function TSPI_lineGetAddressCaps
//
// Allows TAPI to check our line capabilities before placing a call
//
////////////////////////////////////////////////////////////////////////////////
LONG TSPIAPI TSPI_lineGetAddressCaps(
DWORD dwDeviceID,
DWORD dwAddressID,
DWORD dwTSPIVersion,
DWORD dwExtVersion,
LPLINEADDRESSCAPS pac)
{
BEGIN_PARAM_TABLE("TSPI_lineGetAddressCaps")
DWORD_IN_ENTRY(dwDeviceID)
DWORD_IN_ENTRY(dwAddressID)
DWORD_IN_ENTRY(dwTSPIVersion)
DWORD_OUT_ENTRY(dwExtVersion)
END_PARAM_TABLE()
/* TODO (Nick#1#): Most of this function has been taken from an example
and will need to be modified in more detail */
//pac->dwNeededSize = sizeof(LPLINEADDRESSCAPS);
//pac->dwUsedSize = sizeof(LPLINEADDRESSCAPS);
pac->dwNeededSize = sizeof(LINEADDRESSCAPS);
pac->dwUsedSize = sizeof(LINEADDRESSCAPS);
pac->dwLineDeviceID = dwDeviceID;
pac->dwAddressSharing = LINEADDRESSSHARING_PRIVATE;
pac->dwCallInfoStates = LINECALLINFOSTATE_MEDIAMODE | LINECALLINFOSTATE_APPSPECIFIC;
pac->dwCallerIDFlags = LINECALLPARTYID_ADDRESS | LINECALLPARTYID_UNKNOWN;
pac->dwCalledIDFlags = LINECALLPARTYID_ADDRESS | LINECALLPARTYID_UNKNOWN;
pac->dwRedirectionIDFlags = LINECALLPARTYID_ADDRESS | LINECALLPARTYID_UNKNOWN ;
pac->dwCallStates = LINECALLSTATE_IDLE | LINECALLSTATE_OFFERING | LINECALLSTATE_ACCEPTED | LINECALLSTATE_DIALING | LINECALLSTATE_CONNECTED;
pac->dwDialToneModes = LINEDIALTONEMODE_UNAVAIL;
pac->dwBusyModes = LINEDIALTONEMODE_UNAVAIL;
pac->dwSpecialInfo = LINESPECIALINFO_UNAVAIL;
pac->dwDisconnectModes = LINEDISCONNECTMODE_UNAVAIL;
/* TODO (Nick#1#): This needs to be taken from the UI */
pac->dwMaxNumActiveCalls = 1;
pac->dwAddrCapFlags = LINEADDRCAPFLAGS_DIALED;
pac->dwCallFeatures = LINECALLFEATURE_DIAL | LINECALLFEATURE_DROP | LINECALLFEATURE_GENERATEDIGITS;
pac->dwAddressFeatures = LINEADDRFEATURE_MAKECALL | LINEADDRFEATURE_PICKUP;
return EPILOG(0);
}
////////////////////////////////////////////////////////////////////////////////
//
// Lines
//
// After a suitable line has been found it will be opened with lineOpen
// which TAPI will forward onto TSPI_lineOpen
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Function ThreadProc
//
// this is the thread for a line which listens for messages and processes them
//
////////////////////////////////////////////////////////////////////////////////
DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
BEGIN_PARAM_TABLE("ThreadProc")
DWORD_IN_ENTRY(lpParameter)
END_PARAM_TABLE()
((tapiAstManager*)lpParameter)->processMessages();
return EPILOG(0);
}
////////////////////////////////////////////////////////////////////////////////
// Function TSPI_lineOpen
//
// This function is typically called where the software needs to reserve some
// hardware, you can assign any 32bit value to the *phdLine, and it will be sent
// back to any future calls to functions about that line.
//
// Becuase this is sockets not hardware we can set-up the sockets required in this
// functions, and perhaps get the thread going to read the output of the manager.
//
////////////////////////////////////////////////////////////////////////////////
LONG TSPIAPI TSPI_lineOpen(
DWORD dwDeviceID,
HTAPILINE htLine,
LPHDRVLINE phdLine,
DWORD dwTSPIVersion,
LINEEVENT pfnEventProc
)
{
BEGIN_PARAM_TABLE("TSPI_lineOpen")
DWORD_IN_ENTRY(dwDeviceID)
DWORD_IN_ENTRY(htLine)
DWORD_IN_ENTRY(phdLine)
DWORD_IN_ENTRY(dwTSPIVersion)
DWORD_IN_ENTRY(pfnEventProc)
END_PARAM_TABLE()
// g_pfnEventProc = pfnEventProc;
/* TODO (Nick#1#): the syncrounous call back needs to be managed
somehow. For the time being we don't use them but in
the future perhaps have a child class to handle this? */
tapiAstManager *ourConnection;
ourConnection = new tapiAstManager;
DWORD tempInt;
if ( ourConnection )
{
std::string strData,strPass,strExten,strAuthuser;
DWORD intData;
//get our config parameters
// SIP Proxy
if ( false == readConfigString("host", strData) ) {
TspTrace("Error: no SIP proxy configured");
return EPILOG(LINEERR_CALLUNAVAIL);
}
ourConnection->setHost(strData);
// port not used for SIP
//if ( false == readConfigInt("port", intData) )
// return EPILOG(LINEERR_CALLUNAVAIL);
//ourConnection->setPort(intData);
TspTrace("Host is '%s'",strData.c_str());
/* if ( false == readConfigString("ochan", strData) )
return EPILOG(LINEERR_CALLUNAVAIL);
ourConnection->setOutgoingChannel(strData);
*/
// SIP user = auth user
if ( false == readConfigString("user", strData) ) {
TspTrace("Error: no SIP user configured");
return EPILOG(LINEERR_CALLUNAVAIL);
}
// SIP user's extension (for asterisk dummies)
if ( false == readConfigString("userexten", strExten) ) {
TspTrace("Info: no SIP user extension configured, using username...");
strExten = strData;
}
// SIP password
if ( false == readConfigString("pass", strPass) ) {
TspTrace("Error: no SIP password");
return EPILOG(LINEERR_CALLUNAVAIL);
}
// SIP authentication username
if ( false == readConfigString("authuser", strAuthuser) ) {
TspTrace("Info: no authentication username");
}
ourConnection->setUsernamePassword(strData,strExten,strPass,strAuthuser);
// SIP Domain
if ( false == readConfigString("uchan", strData) ) {
TspTrace("Error: no SIP user configured");
return EPILOG(LINEERR_CALLUNAVAIL);
}
ourConnection->setOriginator(strData);
if ( false == readConfigInt("reversemode", tempInt) ) {
TspTrace("Info: failed reading reversemode, use 'off' ...");
ourConnection->reverseMode = 0;
} else {
ourConnection->reverseMode = tempInt;
}
if (tempInt) {
TspTrace("TSPI_lineOpen: dwDeviceID 0x%X reverse-mode activated", dwDeviceID);
} else {
TspTrace("TSPI_lineOpen: dwDeviceID 0x%X reverse-mode deactivated", dwDeviceID);
}
if ( false == readConfigInt("dontsendbye", tempInt) ) {
TspTrace("Info: failed reading dontsendbye, use 'off' ...");
ourConnection->dontSendBye = 0;
} else {
ourConnection->dontSendBye = tempInt;
}
if (tempInt) {
TspTrace("TSPI_lineOpen: dwDeviceID 0x%X dontsendbye activated", dwDeviceID);
} else {
TspTrace("TSPI_lineOpen: dwDeviceID 0x%X dontsendbye deactivated", dwDeviceID);
}
if ( false == readConfigInt("immediatesendbye", tempInt) ) {
TspTrace("Info: failed reading immediatesendbye, use 'off' ...");
ourConnection->immediateSendBye = 0;
} else {
ourConnection->immediateSendBye = tempInt;
}
if (tempInt) {
TspTrace("TSPI_lineOpen: dwDeviceID 0x%X immediatesendbye activated", dwDeviceID);
} else {
TspTrace("TSPI_lineOpen: dwDeviceID 0x%X immediatesendbye deactivated", dwDeviceID);
}
if ( false == readConfigInt("autoanswer", tempInt) ) {
TspTrace("Info: failed reading autoanswer, use 'off' ...");
ourConnection->autoAnswer = 0;
} else {
ourConnection->autoAnswer = tempInt;
}
if (tempInt) {
TspTrace("TSPI_lineOpen: dwDeviceID 0x%X auto-answer activated", dwDeviceID);
} else {
TspTrace("TSPI_lineOpen: dwDeviceID 0x%X auto-answer deactivated", dwDeviceID);
}
if ( false == readConfigInt("autoanswer2", tempInt) ) {
TspTrace("Info: failed reading autoanswer2, use 'off' ...");
ourConnection->autoAnswer2 = 0;
} else {
ourConnection->autoAnswer2 = tempInt;
}
if (tempInt) {
TspTrace("TSPI_lineOpen: dwDeviceID 0x%X auto-answer2 activated", dwDeviceID);
} else {
TspTrace("TSPI_lineOpen: dwDeviceID 0x%X auto-answer2 deactivated", dwDeviceID);
}
// initialize SIP stack
if ( ourConnection->astConnect() == 0)
{
TspTrace("Couldn't initialize SIP stack");
return EPILOG(LINEERR_CALLUNAVAIL);
}
/* if ( false == readConfigString("contextorchan", strData) )
return EPILOG(LINEERR_CALLUNAVAIL);
if ( strData == "context" )
{
if ( false == readConfigString("context", strData) )
return EPILOG(LINEERR_CALLUNAVAIL);
ourConnection->setContext(strData);
}
if ( false == readConfigString("setcallerid", strData) )
return EPILOG(LINEERR_CALLUNAVAIL);
if ( strData == "true" )
{
if ( false == readConfigString("callerid", strData) )
return EPILOG(LINEERR_CALLUNAVAIL);
ourConnection->setCallerID(strData);
}
if ( false == readConfigString("ichan", strData) )
return EPILOG(LINEERR_CALLUNAVAIL);
ourConnection->setInBoundChannel(strData);
if ( false == readConfigString("ichanregex", strData) )
return EPILOG(LINEERR_CALLUNAVAIL);
if ( strData == "true" )
{
ourConnection->useInBoundRegex(true);
}
else
{
ourConnection->useInBoundRegex(false);
}
*/
//log the connection in
ourConnection->login();
ourConnection->setTapiLine(htLine);
ourConnection->setLineEvent(pfnEventProc);
//create the background thread we use to monitor monitor the asterisk manager
//for events
HANDLE thrHandle = CreateThread(NULL,0,ThreadProc,ourConnection,0,NULL);
//keep track of it for our other functions
lineMut.Lock();
lastValue++;
trackLines[lastValue] = ourConnection;
*phdLine = lastValue;
lineMut.Unlock();
//return 0 - signal success
return EPILOG(0);
}
return EPILOG(LINEERR_NOMEM);
}
////////////////////////////////////////////////////////////////////////////////
// Function TSPI_lineClose
//
// Called by TAPI when a line is no longer required.
//
////////////////////////////////////////////////////////////////////////////////
LONG TSPIAPI TSPI_lineClose(HDRVLINE hdLine)
{
BEGIN_PARAM_TABLE("TSPI_lineClose")
DWORD_IN_ENTRY(hdLine)
END_PARAM_TABLE()
//HDRVLINE is a pointer to our asterisk manager object.
//this should free any resources, such as the socket,
lineMut.Lock();
//no find it and remove it from the list
mapLine::iterator it = trackLines.find(hdLine);
if ( it != trackLines.end() )
{
//clean up
//which in turn our thread should exit nicley.
delete ((astManager *)(*it).second);
trackLines.erase(it);
}
lineMut.Unlock();
return EPILOG(0);
}
////////////////////////////////////////////////////////////////////////////////
// Function TSPI_lineMakeCall
//
// This function is called by TAPI to initialize a new outgoing call. This will
// initiate the call and return, when the call is made (but not necasarily connected)
// we should then signal TAPI via the asyncrounous completion function.
//
////////////////////////////////////////////////////////////////////////////////
LONG TSPIAPI TSPI_lineMakeCall(
DRV_REQUESTID dwRequestID,
HDRVLINE hdLine,
HTAPICALL htCall,
LPHDRVCALL phdCall,
LPCWSTR pszDestAddress,
DWORD dwCountryCode,
LPLINECALLPARAMS const pCallParams
)
{
BEGIN_PARAM_TABLE("TSPI_lineMakeCall")
DWORD_IN_ENTRY(hdLine)
DWORD_IN_ENTRY(htCall)
DWORD_IN_ENTRY(phdCall)
DWORD_IN_ENTRY(dwCountryCode)
// DWORD_IN_ENTRY(g_pfnEventProc)
// DWORD_IN_ENTRY(g_pfnCompletionProc)
END_PARAM_TABLE()
mbstate_t mbs;
LONG tr = 0;
lineMut.Lock();
mapLine::iterator it;
it = trackLines.find(hdLine);
if ( it == trackLines.end() )
{
lineMut.Unlock();
//TODO - more error reporting
return EPILOG(LINEERR_INVALLINEHANDLE);
}
tapiAstManager *ourConnection = (tapiAstManager*) (*it).second;
// check if call is busy
if (ourConnection->ongoingcall != 0) {
// line is busy, return proper error value
lineMut.Unlock();
TspTrace("Line busy ... stop.");
return EPILOG(LINEERR_INUSE);
}
ourConnection->ongoingcall = 4;
lineMut.Unlock();
astTspGlue call;
char charString[100];
//if( mbrlen(pszDestAddress,100) <= 0 ) {
//what type of length address is that!
// return EPILOG(0);
//}
if( *pszDestAddress == L'T' || *pszDestAddress == L'P' )
{
pszDestAddress++;
}
mbsinit(&mbs);
wcsrtombs(&charString[0], &pszDestAddress,100, &mbs);
if (ourConnection->originate(&charString[0]) == LINEERR_CALLUNAVAIL) {
TspTrace("Call failed, some SIP problems...? stop.");
return EPILOG(LINEERR_CALLUNAVAIL);
}
if ( g_pfnCompletionProc != 0 )
{
g_pfnCompletionProc(dwRequestID,0);
tr = dwRequestID;
}
//todo - this needs to be made more interactive - i.e. wait for events from Asterisk?
//if ( g_pfnEventProc != 0 )
//{
// g_pfnEventProc(g_htLine,htCall,LINE_CALLSTATE,LINECALLSTATE_DIALING,0,0);
//}
//g_htCall = htCall;
call.setTapiCall(htCall);
call.setDest(&charString[0]);
ourConnection->addCall(call);
//klaus
ourConnection->setTapiCall(htCall);
return EPILOG(tr);
}
////////////////////////////////////////////////////////////////////////////////
// Function TSPI_lineDrop
//
// This function is called by TAPI to signal the end of a call. The status
// information for the call should be retained until the function TSPI_lineCloseCall
// is called.
//
////////////////////////////////////////////////////////////////////////////////
LONG TSPIAPI TSPI_lineDrop(
DRV_REQUESTID dwRequestID,
HDRVCALL hdCall,
LPCSTR lpsUserInfo,
DWORD dwSize
)
{
BEGIN_PARAM_TABLE("TSPI_lineDrop")
DWORD_IN_ENTRY(dwRequestID)
DWORD_IN_ENTRY(hdCall)
DWORD_OUT_ENTRY(lpsUserInfo)
DWORD_IN_ENTRY(dwSize)
END_PARAM_TABLE()
lineMut.Lock();
mapLine::iterator it;
for ( it = trackLines.begin() ; it != trackLines.end() ; it++ )
{
TspTrace("Dropping Call...");
(*it).second->dropCall(hdCall);
}
lineMut.Unlock();
//if ( g_pfnEventProc != 0 )
//{
// g_pfnEventProc(g_htLine,g_htCall,LINE_CALLSTATE,LINECALLSTATE_IDLE,0,0);
//}
//Lets pretend to be asyncrounous for the time being!
//in the furture we should make this so that it waits
//for asterisk to send its results back to us.
/* TODO (Nick#1#): Make this part a true asyncronous call */
if ( g_pfnCompletionProc )
{
TSPTRACE("Sending ASYNC completion ...");
g_pfnCompletionProc(dwRequestID, 0);
}
return EPILOG(dwRequestID);
}
////////////////////////////////////////////////////////////////////////////////
// Function TSPI_lineCloseCall
//
// This function should deallocate all of the calls resources, TSPI_lineDrop
// may not be called before this one - so we also have to check the call
// is dropped as well.
//
////////////////////////////////////////////////////////////////////////////////
LONG TSPIAPI TSPI_lineCloseCall(
HDRVCALL hdCall
)
{
BEGIN_PARAM_TABLE("TSPI_lineCloseCall")
DWORD_IN_ENTRY(hdCall)
END_PARAM_TABLE()
lineMut.Lock();
mapLine::iterator it;
for ( it = trackLines.begin() ; it != trackLines.end() ; it++ )
{
(*it).second->dropCall(hdCall);
}
lineMut.Unlock();
return EPILOG(0);
}
////////////////////////////////////////////////////////////////////////////////
//
// Status
//
// if TAPI requires to find out the status of our lines then it can call
// the following functions.
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Function TSPI_lineGetLineDevStatus
//
// As the function says.
//
////////////////////////////////////////////////////////////////////////////////
LONG TSPIAPI TSPI_lineGetLineDevStatus(
HDRVLINE hdLine,
LPLINEDEVSTATUS plds
)
{
BEGIN_PARAM_TABLE("TSPI_lineGetLineDevStatus")
DWORD_IN_ENTRY(hdLine)
END_PARAM_TABLE()
return EPILOG(0);
}
////////////////////////////////////////////////////////////////////////////////
// Function TSPI_lineGetAdressStatus
//
// As the function says.
//
////////////////////////////////////////////////////////////////////////////////
LONG TSPI_lineGetAdressStatus(
HDRVLINE hdLine,
DWORD dwAddressID,
LPLINEADDRESSSTATUS pas)
{
BEGIN_PARAM_TABLE("TSPI_lineGetAdressStatus")
DWORD_IN_ENTRY(dwAddressID)
END_PARAM_TABLE()
return EPILOG(0);
}
////////////////////////////////////////////////////////////////////////////////
// Function TSPI_lineGetCallStatus
//
// As the function says.
//
////////////////////////////////////////////////////////////////////////////////
LONG TSPIAPI TSPI_lineGetCallStatus(
HDRVCALL hdCall,
LPLINECALLSTATUS pls
)
{
//TODO finish this offf....
BEGIN_PARAM_TABLE("TSPI_lineGetCallStatus")
DWORD_IN_ENTRY(hdCall)
END_PARAM_TABLE()
TSPTRACE("TSPI_lineGetCallStatus: ERROR: pls->dwTotalSize = %d",pls->dwTotalSize);
TSPTRACE("TSPI_lineGetCallStatus: ERROR: sizeof(LINECALLSTATUS)= %d",sizeof(LINECALLSTATUS));
if (sizeof(LINECALLSTATUS) > pls->dwTotalSize) {
TSPTRACE("TSPI_lineGetCallStatus: ERROR: sizeof(LINECALLSTATUS) > dwTotalSize");
return EPILOG(LINEERR_NOMEM);
}
// TAPI Service Provider MUST NOT write this member!
// http://msdn2.microsoft.com/en-us/library/ms725567.aspx
// pls->dwTotalSize = sizeof(LINECALLSTATUS);
// we use all the fixed size members, thus we need at least the size of the fixed size members
pls->dwNeededSize = sizeof(LINECALLSTATUS);
pls->dwUsedSize = sizeof(LINECALLSTATUS);
pls->dwCallStateMode = 0;
// TAPI Service PRovider MUST NOT write this member!
// http://msdn2.microsoft.com/en-us/library/ms725567.aspx
// pls->dwCallPrivilege = LINECALLPRIVILEGE_MONITOR | LINECALLFEATURE_ACCEPT | LINECALLFEATURE_ANSWER | LINECALLFEATURE_COMPLETECALL | LINECALLFEATURE_DIAL | LINECALLFEATURE_DROP;
//LINECALLPRIVILEGE_NONE
//LINECALLPRIVILEGE_OWNER
pls->dwCallFeatures = LINECALLFEATURE_DROP;
//and more...
lineMut.Lock();
mapLine::iterator it;
int found=0;
for ( it = trackLines.begin() ; it != trackLines.end(); it++ )
{
astTspGlue *ourCall;
if ( ( ourCall = (*it).second->findCall(hdCall)) != NULL )
{
TSPTRACE("Found call - getting state");
found = 1;
// richtiger callstate geht nicht, weil im astTspGlue das nicht drinnen steht!
//pls->dwCallState = ourCall->dwCallState;
//pls->dwCallStateMode = ourCall->dwCallStateMode;
pls->dwCallState = ourCall->getState();
//LINECALLSTATE_OFFERING;
//LINECALLSTATE_CONNECTED;
//LINECALLSTATE_OFFERING
//LINECALLSTATE_DISCONNECTED
}
}
lineMut.Unlock();
if (found) {
return EPILOG(0);
} else {
return EPILOG(LINEERR_INVALCALLHANDLE);
}
}
//Required (maybe) by lineGetCallInfo
//Thanks to the poster!
//http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=114501c32a84%24a337f930%24a101280a%40phx.gbl&rnum=3&prev=/groups%3Fq%3DTSPI_lineGetCallInfo%26ie%3DUTF-8%26oe%3DUTF-8%26hl%3Den%26btnG%3DGoogle%2BSearch
void TackOnData(void* pData, const char* pStr, DWORD* pSize)
{
USES_CONVERSION;
// Convert the string to Unicode
LPCWSTR pWStr = A2CW(pStr);
size_t cbStr = (strlen(pStr) + 1) * 2;
LPLINECALLINFO pDH = (LPLINECALLINFO)pData;
// If this isn't an empty string then tack it on
if (cbStr > 2)
{
// Increase the needed size to reflect this string whether we are
// successful or not.
pDH->dwNeededSize += cbStr;
// Do we have space to tack on the string?
if (pDH->dwTotalSize >= pDH->dwUsedSize + cbStr)
{
// YES, tack it on
memcpy((char *)pDH + pDH->dwUsedSize, pWStr, cbStr);
// Now adjust size and offset in message and used
// size in the header
DWORD* pOffset = pSize + 1;
*pSize = cbStr;
*pOffset = pDH->dwUsedSize;
pDH->dwUsedSize += cbStr;
}
}
}
////////////////////////////////////////////////////////////////////////////////
// Function TSPI_lineGetCallInfo
//