-
Notifications
You must be signed in to change notification settings - Fork 4
/
TapiLine.cpp
1773 lines (1655 loc) · 59.9 KB
/
TapiLine.cpp
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
// (c): Klaus Darilion (IPCom GmbH, www.ipcom.at)
// License: GNU General Public License 2.0
//#include <WinDNS.h>
#include <windows.h>
#include <windns.h>
#include <eXosip2/eXosip.h>
#include ".\tapiline.h"
#include "WaveTsp.h"
//Function call for asyncrounous functions
ASYNC_COMPLETION g_pfnCompletionProc = 0;
TapiLine::TapiLine(void)
{
this->initialized = false;
this->htLine = 0;
this->hdLine = 0;
this->htCall = 0;
this->hdCall = 0;
this->status = TapiLine::IDLE;
this->reg_status = TapiLine::SIP_UNREGISTERED;
this->cid = 0;
this->did = 0;
this->tid = 0;
this->rid = 0;
this->line = NULL;
this->from = "";
this->to = "";
this->referto = "";
this->markedforshutdown = false;
this->isIncoming = false;
this->tapiCallback = NULL;
this->deviceId = 0; // bad because 0 is a valid deviceId - but do not know how to solve this
this->shutdownRequestId = 0;
this->transferRequestId = 0;
this->redirectRequestId = 0;
this->incomingFromDisplayName = NULL;
this->incomingFromUriUser = NULL;
this->incomingToDisplayName = NULL;
this->incomingToUriUser = NULL;
this->enableRealmCheck = 0;
this->transportProtocol = 0; //UDP
this->send180Ringing = 0;
this->lastReportedLineEvent = LINECALLSTATE_UNKNOWN;
}
TapiLine::~TapiLine(void)
{
if (this->line) {
TSPTRACE("TapiLine::~TapiLine: freeing old line parameter");
free(this->line);
}
}
// set the SIP proxy
void TapiLine::setProxy(std::string strdata)
{
this->proxy=strdata;
}
// set the SIP outbound proxy
void TapiLine::setObProxy(std::string strdata)
{
this->obproxy=strdata;
}
// set the SIP AoR username and authentication user name if no authusername is supplied
void TapiLine::setUsername(std::string strdata)
{
this->username=strdata;
}
// get the SIP AoR username
const char * TapiLine::getUsername()
{
return this->username.c_str();
}
// set the authentication user name
void TapiLine::setAuthusername(std::string strdata)
{
this->authusername=strdata;
}
// set the phhone's (the party which gets called to get redirected) user name
void TapiLine::setPhoneusername(std::string strdata)
{
this->phoneusername=strdata;
}
// set the SIP authentication password
void TapiLine::setPassword(std::string strdata)
{
this->password=strdata;
}
// set auto-answer
void TapiLine::setAutoanswer(int temp)
{
this->autoanswer=temp;
}
// set if realmcheck is disabled
void TapiLine::setEnableRealmCheck(int disable)
{
this->enableRealmCheck = disable;
}
// transport protocol
void TapiLine::setTransportProtocol(int protocol)
{
this->transportProtocol = protocol;
}
// transport protocol
int TapiLine::getTransportProtocol(void)
{
return this->transportProtocol;
}
// send 180 ringing
void TapiLine::setSend180Ringing(int ringing)
{
this->send180Ringing = ringing;
}
// send 180 ringing
int TapiLine::getSend180Ringing(void)
{
return this->send180Ringing;
}
// get/set reverse-mode
void TapiLine::setReverseMode(int mode)
{
this->reverseMode = mode;
}
int TapiLine::getReverseMode()
{
return this->reverseMode;
}
// initialize the Line, sets the username, password, proxy... for the SIP call
int TapiLine::initialize(void)
{
int i;
// add authentication info for this line
//i = eXosip_clear_authentication_info(); // do not clear as this clears alos authentication info of other lines
if (this->authusername == "") {
this->authusername = this->username;
TSPTRACE("TapiLine::initialize(): authusername = %s\n",this->authusername.c_str());
}
if (this->enableRealmCheck ) {
TSPTRACE("TapiLine::initialize(): adding credentials with realm\n");
i = eXosip_add_authentication_info(ctx, this->username.c_str(), this->authusername.c_str(),
this->password.c_str(), "", this->proxy.c_str());
} else {
TSPTRACE("TapiLine::initialize(): adding credentials without realm\n");
i = eXosip_add_authentication_info(ctx, this->username.c_str(), this->authusername.c_str(),
this->password.c_str(), "", 0);
}
this->initialized = true;
return 0;
}
bool TapiLine::isInitialized(void)
{
return this->initialized;
}
//REGISTER to the SIP proxy
int TapiLine::register_to_sipproxy()
{
int rid, i;
std::string from_uri, registrar_uri, route_set;
osip_message_t *request;
TSPTRACE("TapiLine::register_to_sipproxy(): ...\n");
//i = eXosip_clear_authentication_info(); // ToDo: this may influence concurrent calls and REGISTERs
//if (this->authusername == "") {
// // use SIP AoR username as authentication username
// i = eXosip_add_authentication_info(ctx, this->username.c_str(), this->username.c_str(),
// this->password.c_str(), "", "");
//} else {
// i = eXosip_add_authentication_info(ctx, this->username.c_str(), this->authusername.c_str(),
// this->password.c_str(), "", "");
//}
from_uri = ("sip:") + this->username + ("@") + this->proxy;
registrar_uri = ("sip:") + this->proxy;
TSPTRACE("TapiLine::register_to_sipproxy(): from_uri.c_str()=%s...\n",from_uri.c_str());
TSPTRACE("TapiLine::register_to_sipproxy(): registrar_uri.c_str()=%s...\n",registrar_uri.c_str());
rid = eXosip_register_build_initial_register(ctx, from_uri.c_str(), registrar_uri.c_str(), NULL, 300, &request) ;
if (rid < 0)
{
TSPTRACE("TapiLine::register_to_sipproxy: eXosip_register_build_initial_register failed: (bad arguments?)\n");
this->reg_status = SIP_UNREGISTERED;
return -1;
}
TSPTRACE("TapiLine::register_to_sipproxy: eXosip_register_build_initial_register succeeded\n");
if (this->obproxy != "") {
// add outboundproxy as route set
// check if the obproxy contains the lr parameter
if (this->obproxy.find(";lr") == std::string::npos) {
// not found
route_set = ("<sip:") + this->obproxy + (";lr>");
} else {
// found
route_set = ("<sip:") + this->obproxy + (">");
}
char *header = osip_strdup ("route");
char *route = osip_strdup (route_set.c_str());
osip_message_set_multiple_header (request, header, route);
osip_free (route);
osip_free (header);
}
TspSipTrace("TapiLine::register_to_sipproxy: sending message\n", request);
eXosip_lock(ctx);
this->reg_status = SIP_REGISTERING;
i = eXosip_register_send_register(ctx, rid, request);
if (i == -1) {
TSPTRACE("TapiLine::register_to_sipproxy: eXosip_register_send_register failed: (bad arguments?)\n");
this->reg_status = SIP_UNREGISTERED;
eXosip_unlock(ctx);
return -1;
}
TSPTRACE("TapiLine::register_to_sipproxy: eXosip_register_send_register returned: %d\n",i);
this->rid = rid;
eXosip_unlock(ctx);
TSPTRACE("TapiLine::register_to_sipproxy: REGISTER sent ... further processing must be done in the thread\n");
return rid;
}
//un-REGISTER from the SIP proxy
int TapiLine::unregister_from_sipproxy()
{
int i;
std::string from_uri, registrar_uri, route_set;
osip_message_t *request;
TSPTRACE("TapiLine::un-register_from_sipproxy(): unregister rid=%d...\n",this->rid);
i = eXosip_register_build_register(ctx, this->rid, 0, &request);
if (i < 0)
{
TSPTRACE("TapiLine::unregister_from_sipproxy: eXosip_register_build_register failed: (bad arguments?)\n");
this->reg_status = SIP_UNREGISTERED;
return -1;
}
TSPTRACE("TapiLine::unregister_from_sipproxy: eXosip_register_build_register succeeded\n");
TspSipTrace("TapiLine::unregister_from_sipproxy: sending message\n", request);
eXosip_lock(ctx);
this->reg_status = SIP_DEREGISTERING;
i = eXosip_register_send_register(ctx, this->rid, request);
if (i == -1) {
TSPTRACE("TapiLine::unregister_from_sipproxy: eXosip_register_send_register failed: (bad arguments?)\n");
this->reg_status = SIP_UNREGISTERED;
eXosip_unlock(ctx);
return -1;
}
TSPTRACE("TapiLine::unregister_from_sipproxy: eXosip_register_send_register returned: %d\n",i);
eXosip_unlock(ctx);
TSPTRACE("TapiLine::unregister_from_sipproxy: REGISTER sent ... further processing must be done in the thread\n");
return 0;
}
// store TAPI Line handle inside the TapiLine object
void TapiLine::setTapiLine(HTAPILINE lineHandle)
{
this->htLine = lineHandle;
}
// retrieve TAPI Line handle from the TapiLine object
HTAPILINE TapiLine::getTapiLine()
{
return (this->htLine);
}
// store the callback function used for reporting line status to TAPI
void TapiLine::setLineEventCallback(LINEEVENT callback)
{
this->tapiCallback = callback;
}
// retrieve the callback function used for reporting line status to TAPI
LINEEVENT TapiLine::getLineEventCallback() {
return this->tapiCallback;
}
// store the call handle (a handle for a certain call on this line)
// we only allow one call per line, thus this is a "singleton"
void TapiLine::setTapiCall(HTAPICALL call)
{
this->htCall = call;
}
// make the call: initiate the click2dial feature
int TapiLine::makeCall(LPCWSTR pszDestAddress, DWORD dwCountryCode, DWORD dwRequestID)
{
std::string destAddress;
TSPTRACE("TapiLine::makeCall: ...\n");
//if( mbrlen(pszDestAddress,100) <= 0 ) {
// //what type of length address is that!
// TSPTRACE("TapiLine::makeCall: number to long\n");
// return;
//}
//if( *pszDestAddress == L'T' || *pszDestAddress == L'P' ) {
// pszDestAddress++;
//}
// convert lpcwstr to string
mbstate_t mbs;
mbsinit(&mbs);
char feld[202];
wcsrtombs(feld, &pszDestAddress,100, &mbs);
destAddress = std::string(feld);
// i = eXosip_clear_authentication_info(); // ToDo: XXXX this may influence concurrent calls
// i = eXosip_add_authentication_info(ctx, this->username.c_str(), this->username.c_str(), this->password.c_str(), "", this->proxy.c_str());
//asterisk sends "asterisk" as realm, thus do not apply realm to allow interworking with asterisk
// update: in sip.conf this can bechanged with realm=..., thus we should be strict and set credentials for a realm
//if (this->authusername == "") {
// // use SIP AoR username as authentication username
// i = eXosip_add_authentication_info(ctx, this->username.c_str(), this->username.c_str(),
// this->password.c_str(), "", "");
//} else {
// i = eXosip_add_authentication_info(ctx, this->username.c_str(), this->authusername.c_str(),
// this->password.c_str(), "", "");
//}
TSPTRACE("TapiLine::makeCall: removing spaces and special characters from phone number...\n");
TSPTRACE("TapiLine::makeCall: original number: %s\n",destAddress.c_str());
std::string::size_type pos;
while ( (pos = destAddress.find_first_not_of("0123456789")) != std::string::npos) {
destAddress.erase(pos,1);
TSPTRACE("TapiLine::makeCall: bad digit found, new number is: %s\n",destAddress.c_str());
}
TSPTRACE("TapiLine::makeCall: trimmed number: %s\n",destAddress.c_str());
from = ("sip:") + this->username + ("@") + this->proxy;
referto = ("sip:") + destAddress + ("@") + this->proxy;
// using openser it is possible to REGISTER multiple times and call yourself
// this does not work with Asterisk. Thus, it is possible to
// specify a "phone username" which gets called and gets refered
if (this->phoneusername == "") {
// use SIP AoR username as destination
to = ("sip:") + this->username + ("@") + this->proxy;
} else {
// use specified phone username as destination
if (this->phoneusername.find("@") == std::string::npos) {
to = ("sip:") + this->phoneusername + ("@") + this->proxy;
} else {
to = ("sip:") + this->phoneusername;
}
}
if (this->reverseMode) {
// reuse destAddress to change refertarget and calltarget
destAddress = to;
to = referto;
referto = destAddress;
}
return this->sendInvite();
// return this->sendOptions();
}
// retrieve the status of the state machine
TapiLine::StatusType TapiLine::getStatus(void)
{
return this->status;
}
// set the status of the state machine
void TapiLine::setStatus(TapiLine::StatusType newStatus)
{
this->status = newStatus;
}
// retrieve the status of the state machine using the TAPI LINECALLSTATE_ Constants
DWORD TapiLine::getTapiStatus(void)
{
TSPTRACE("TapiLine::getTapiStatus: ...\n");
switch(this->status) {
case TapiLine::IDLE:
return LINECALLSTATE_IDLE;
case TapiLine::RESERVED:
return LINECALLSTATE_DIALTONE;
case TapiLine::LEARNING:
return LINECALLSTATE_DIALTONE;
case TapiLine::CALLING:
return LINECALLSTATE_DIALING;
case TapiLine::CALLING_RINGING:
return LINECALLSTATE_RINGBACK;
case TapiLine::ESTABLISHED:
LINECALLSTATE_CONNECTED;
case TapiLine::ESTABLISHED_TRANSFERRING:
return LINECALLSTATE_ONHOLDPENDTRANSFER;
case TapiLine::ESTABLISHED_TRANSFER_ACCEPTED:
return LINECALLSTATE_ONHOLDPENDTRANSFER;
case TapiLine::ESTABLISHED_TRANSFERRED:
return LINECALLSTATE_CONNECTED;
case TapiLine::INCOMING:
return LINECALLSTATE_OFFERING;
case TapiLine::INCOMING_CANCELED:
return LINECALLSTATE_IDLE;
}
TSPTRACE("TapiLine::getTapiStatus: ERROR: undefined call state\n");
return LINECALLSTATE_UNKNOWN;
}
// mark the line to beeing shutdown by thread (async shutodwn tests for dialer.exe)
void TapiLine::markForShutdown(bool shut)
{
this->markedforshutdown=shut;
}
// terminate all ongoing calls and set status to idle
int TapiLine::shutdown(DRV_REQUESTID dwRequestID)
{
TSPTRACE("TapiLine::shutdown: ...\n");
switch(this->status) {
case TapiLine::IDLE:
case TapiLine::RESERVED:
case TapiLine::INCOMING_CANCELED:
TSPTRACE("TapiLine::shutdown: line is idle, shutdown done\n");
this->status = TapiLine::IDLE;
// ASYNC_COMPLETE
if (g_pfnCompletionProc) {
TSPTRACE("TapiLine::shutdown: sending async_complete ...\n");
g_pfnCompletionProc(dwRequestID,0);
}
this->reportStatus();
return 0;
break;
default:
if (!this->cid) {
TSPTRACE("TapiLine::shutdown: ERROR: line not idle, but no cid...strange\n");
this->status = TapiLine::IDLE;
// ASYNC_COMPLETE
if (g_pfnCompletionProc) {
TSPTRACE("TapiLine::shutdown: sending async_complete ...\n");
g_pfnCompletionProc(dwRequestID,0);
}
this->reportStatus();
return 0;
} else {
if (this->shutdown_nowait() != 0) {
if (g_pfnCompletionProc) {
TSPTRACE("TapiLine::shutdown: sending async_complete after shutdown_nowait() failed ...\n");
g_pfnCompletionProc(dwRequestID,0);
}
return 0;
}
}
}
return 1;
//int i;
//i=0; //counter
//while (this->status != TapiLine::IDLE) {
// Sleep(50);
// i++;
// if (i > 20*40) {
// TSPTRACE("TapiLine::shutdown: line still not idle after 40 seconds, force shutdown...\n");
// this->status = TapiLine::IDLE;
// this->cid=0;
// this->did=0;
// this->reportStatus();
// break;
// }
//}
TSPTRACE("TapiLine::shutdown: ... done\n");
}
// return the eXosip internal call id for this TapiLine
int TapiLine::getCid(void)
{
return this->cid;
}
// return the eXosip internal transaction id for this TapiLine
int TapiLine::getTid(void)
{
return this->tid;
}
// return the eXosip internal registration id for this TapiLine
int TapiLine::getRid(void)
{
return this->rid;
}
// return the eXosip internal registration line for this TapiLine
char *TapiLine::getLine(void)
{
return this->line;
}
// return the eXosip internal dialog id for this TapiLine
int TapiLine::getDid(void)
{
return this->did;
}
// handle the SIP events associated with this particular TapiLine
void TapiLine::handleEvent(eXosip_event_t *je)
{
int i;
TSPTRACE("TapiLine::handleEvent: ... \n");
switch(this->status) {
case IDLE:
TSPTRACE("TapiLine::handleEvent: current status: IDLE\n");
if (je->type == EXOSIP_CALL_INVITE) {
// save ids
this->cid = je->cid;
this->did = je->did;
this->tid = je->tid;
// set new status
this->status = INCOMING;
this->isIncoming = true;
// report call to TAPI and reuse hdLine as hdCall
this->hdCall = (HDRVCALL) this->hdLine;
// fetch Caller-ID: extract it from From header
osip_from_t *from;
char * temp;
size_t l;
from=osip_message_get_from (je->request);
if (from) {
temp = osip_from_get_displayname(from);
if (temp) {
l=strlen(temp); // excluding \0
this->incomingFromDisplayName = (char *) malloc(l+1);
if (this->incomingFromDisplayName) {
strncpy(this->incomingFromDisplayName, osip_from_get_displayname(from), l+1);
TSPTRACE("TapiLine::handleEvent: From display name = '%s'\n", this->incomingFromDisplayName);
} else {
TSPTRACE("TapiLine::handleEvent: Failed to allocate memory for From display name!\n");
}
}
temp = osip_uri_get_username(osip_from_get_url(from));
l=strlen(temp);
this->incomingFromUriUser = (char *) malloc(l+1);
if (this->incomingFromUriUser) {
strncpy(this->incomingFromUriUser, osip_uri_get_username(osip_from_get_url(from)), l+1);
TSPTRACE("TapiLine::handleEvent: From uri user = '%s'\n", this->incomingFromUriUser);
} else {
TSPTRACE("TapiLine::handleEvent: Failed to allocate memory for From uri user!\n");
}
}
// fetch Called-ID: extract it from To header
from=osip_message_get_to(je->request);
if (from) {
temp = osip_from_get_displayname(from);
if (temp) {
l=strlen(temp); // excluding \0
this->incomingToDisplayName = (char *) malloc(l+1);
if (this->incomingToDisplayName) {
strncpy(this->incomingToDisplayName, osip_from_get_displayname(from), l+1);
TSPTRACE("TapiLine::handleEvent: To display name = '%s'\n", this->incomingToDisplayName);
} else {
TSPTRACE("TapiLine::handleEvent: Failed to allocate memory for To display name!\n");
}
}
temp = osip_uri_get_username(osip_from_get_url(from));
l=strlen(temp);
this->incomingToUriUser = (char *) malloc(l+1);
if (this->incomingToUriUser) {
strncpy(this->incomingToUriUser, osip_uri_get_username(osip_from_get_url(from)), l+1);
TSPTRACE("TapiLine::handleEvent: To uri user = '%s'\n", this->incomingToUriUser);
} else {
TSPTRACE("TapiLine::handleEvent: Failed to allocate memory for To uri user!\n");
}
}
// signal incoming call to TAPI
TSPTRACE("TapiLine::handleEvent: new status: INCOMING\n");
TSPTRACE("TapiLine::handleEvent: sending LINE_NEWCALL ...\n");
TSPTRACE("TapiLine::handleEvent: htLine = %d\n",this->htLine);
TSPTRACE("TapiLine::handleEvent: htCall = %d\n",this->htCall);
TSPTRACE("TapiLine::handleEvent: hdCall = %d\n",this->hdCall);
this->reportLineEvent( this->htLine,
0,
LINE_NEWCALL,
(DWORD_PTR)(HDRVCALL) this->hdCall,
(DWORD_PTR)(LPHTAPICALL)&(this->htCall),
0);
TSPTRACE("TapiLine::handleEvent: sending LINE_NEWCALL ... done\n");
TSPTRACE("TapiLine::handleEvent: htLine = %d\n",this->htLine);
TSPTRACE("TapiLine::handleEvent: htCall = %d\n",this->htCall);
TSPTRACE("TapiLine::handleEvent: hdCall = %d\n",this->hdCall);
if (this->htCall == NULL) {
TSPTRACE("TapiLine::handleEvent: ERROR on incoming call - did not get htCall, reject call...\n");
int i;
eXosip_lock(ctx);
i=eXosip_call_send_answer(ctx, je->tid, 500, NULL);
eXosip_unlock(ctx);
if (i) {
TSPTRACE("SipStack::processSipMessages: eXosip_call_send_answer(ctx, %d,500,NULL) failed\n", je->tid);
} else {
TSPTRACE("SipStack::processSipMessages: eXosip_call_send_answer(ctx, %d,500,NULL) succeeded\n", je->tid);
}
}
}
break;
case INCOMING:
TSPTRACE("TapiLine::handleEvent: current status: INCOMING, je->type=%i\n", je->type);
if (je->type == EXOSIP_CALL_CANCELLED) {
TSPTRACE("TapiLine::handleEvent:INCOMING: EXOSIP_CALL_CANCELLED received: (cid=%i did=%i) '%s'\n",je->cid,je->did,je->textinfo);
this->cid=0;
this->did=0;
this->tid=0;
this->status=INCOMING_CANCELED;
//support for Reason header:
// if CANCEL request, check for Reason header
if ( je->request ) {
if (strcmp(je->request->sip_method, "CANCEL")) {
TSPTRACE("TapiLine::handleEvent: error, no CANCEL request, but %s received ...\n", je->request->sip_method);
} else {
// check for Reason header value
int res;
osip_header_t *reason_header;
TSPTRACE("TapiLine::handleEvent: CANCEL request ...\n");
res = osip_message_header_get_byname(je->request, "reason", 0, &reason_header);
if (reason_header) {
if (strstr(reason_header->hvalue, "cause=200")) {
TSPTRACE("TapiLine::handleEvent: incoming CANCEL request with Reason cause 200 ...\n");
// call was answered elsewhere, signal answer and then go on
// signaling IDLE as before
TSPTRACE("TapiLine::handleEvent: sending LINECALLSTATE_CONNECTED ...\n");
this->reportLineEvent( this->htLine, this->htCall,
LINE_CALLSTATE, LINECALLSTATE_CONNECTED, 0, 0);
} else {
TSPTRACE("TapiLine::handleEvent: CANCEL with Reason header, but cause!=200...\n");
}
} else {
TSPTRACE("TapiLine::handleEvent: CANCEL without Reason header ...\n");
}
}
}
checkForShutdown("incoming call closed");
}
if (je->type == EXOSIP_CALL_CLOSED) {
TSPTRACE("TapiLine::handleEvent:INCOMING: EXOSIP_CALL_CLOSED received: (cid=%i did=%i) '%s'\n",je->cid,je->did,je->textinfo);
this->cid=0;
this->did=0;
this->tid=0;
this->status=INCOMING_CANCELED;
checkForShutdown("incoming call closed");
}
if (je->type == EXOSIP_CALL_RELEASED) {
TSPTRACE("TapiLine::handleEvent: EXOSIP_CALL_RELEASED received: (cid=%i did=%i) '%s'\n",je->cid,je->did,je->textinfo);
this->cid=0;
this->did=0;
this->tid=0;
this->status=IDLE;
checkForShutdown("incoming call released");
break;
}
break;
case RESERVED:
TSPTRACE("TapiLine::handleEvent: current status: RESERVED\n");
break;
case LEARNING:
TSPTRACE("TapiLine::handleEvent: current status: LEARNING\n");
break;
case CALLING:
TSPTRACE("TapiLine::handleEvent: current status: CALLING\n");
case CALLING_RINGING:
TSPTRACE("TapiLine::handleEvent: current status: CALLING_RINGING\n");
if (je->type == EXOSIP_CALL_RELEASED) {
TSPTRACE("TapiLine::handleEvent: EXOSIP_CALL_RELEASED received: (cid=%i did=%i) '%s'\n",je->cid,je->did,je->textinfo);
this->cid=0;
this->did=0;
this->tid=0;
this->status=IDLE;
checkForShutdown("ringing call released");
break;
}
if (je->type == EXOSIP_CALL_RINGING) {
TSPTRACE("TapiLine::handleEvent: EXOSIP_CALL_RINGING received: (cid=%i did=%i) '%s'\n",je->cid,je->did,je->textinfo);
this->status=CALLING_RINGING;
break;
}
if (je->type == EXOSIP_CALL_REQUESTFAILURE) {
TSPTRACE("TapiLine::handleEvent: EXOSIP_CALL_REQUESTFAILURE received: (cid=%i did=%i) '%s'\n",je->cid,je->did,je->textinfo);
if ( (je->response != NULL) && (je->response->status_code == 487) ) {
TSPTRACE("TapiLine::handleEvent:: Call cancelled 487 ...\n");
this->cid=0;
this->did=0;
this->tid=0;
this->status=IDLE;
} else if ( (je->response != NULL) && (je->response->status_code == 403) ) {
TSPTRACE("TapiLine::handleEvent:: Call forbidden 403 ...\n");
this->cid=0;
this->did=0;
this->tid=0;
this->status=IDLE;
} else {
TSPTRACE("TapiLine::handleEvent:: Call failed for unknown reason ...\n");
this->cid=0;
this->did=0;
this->tid=0;
this->status=IDLE;
}
checkForShutdown("ringing call requestfailure");
break;
}
if (je->type == EXOSIP_CALL_SERVERFAILURE) {
TSPTRACE("TapiLine::handleEvent: EXOSIP_CALL_SERVERFAILURE received: (cid=%i did=%i) '%s'\n",je->cid,je->did,je->textinfo);
this->cid=0;
this->did=0;
this->tid=0;
this->status=IDLE;
checkForShutdown("ringing call serverfailure");
break;
}
if (je->type == EXOSIP_CALL_GLOBALFAILURE) {
TSPTRACE("TapiLine::handleEvent: EXOSIP_CALL_GLOBALFAILURE received: (cid=%i did=%i) '%s'\n",je->cid,je->did,je->textinfo);
this->cid=0;
this->did=0;
this->tid=0;
this->status=IDLE;
checkForShutdown("ringing call globalfailure");
break;
}
if (je->type == EXOSIP_CALL_ANSWERED) {
TSPTRACE("TapiLine::handleEvent: EXOSIP_CALL_ANSWERED received: (cid=%i did=%i) '%s'\n",je->cid,je->did,je->textinfo);
this->did = je->did;
osip_message_t *ack;
eXosip_lock(ctx);
i = eXosip_call_build_ack(ctx, je->did, &ack);
if (i != 0) {
TSPTRACE("TapiLine::handleEvent: eXosip_call_build_ack failed...\n");
eXosip_unlock(ctx);
this->shutdown_nowait();
break;
}
TSPTRACE("TapiLine::handleEvent: eXosip_call_build_ack succeeded...\n");
TspSipTrace("TapiLine::handleEvent: sending ACK\n",ack);
i = eXosip_call_send_ack(ctx, je->did, ack);
if (i != 0) {
TSPTRACE("TapiLine::handleEvent: eXosip_call_send_ack failed...\n");
eXosip_unlock(ctx);
this->shutdown_nowait();
break;
}
TSPTRACE("TapiLine::handleEvent: eXosip_call_send_ack succeeded...\n");
eXosip_unlock(ctx);
this->status=ESTABLISHED;
this->reportStatus();
// if we are in ACD mode we do not initiate the REFER automatically, but we
// wait for the TAPI application to execute the blind transfer
if (reverseMode == 1) {
TSPTRACE("TapiLine::handleEvent: ACD mode is activated, wait for blind transfer ...\n");
break;
}
// wait some time before sending the REFER reqeuest
Sleep(50);
osip_message_t *refer;
eXosip_lock(ctx);
TSPTRACE("TapiLine::handleEvent: building REFER ...\n");
i = eXosip_call_build_refer(ctx, this->did, this->referto.c_str(), &refer);
if (i != 0) {
TSPTRACE("TapiLine::handleEvent: eXosip_call_build_refer failed...\n");
eXosip_unlock(ctx);
this->shutdown_nowait();
break;
}
TSPTRACE("TapiLine::handleEvent: eXosip_call_build_refer succeeded...\n");
i = osip_message_set_header(refer, "Referred-By", this->from.c_str());
if (i != 0) {
TSPTRACE("TapiLine::handleEvent: osip_message_set_header failed...\n");
eXosip_unlock(ctx);
this->shutdown_nowait();
break;
}
TSPTRACE("TapiLine::handleEvent: osip_message_set_header succeeded...\n");
TspSipTrace("TapiLine::handleEvent: sending REFER\n",refer);
i = eXosip_call_send_request(ctx, this->did, refer);
if (i != 0) {
TSPTRACE("TapiLine::handleEvent: eXosip_call_send_request failed...\n");
eXosip_unlock(ctx);
this->shutdown_nowait();
break;
}
TSPTRACE("TapiLine::handleEvent: eXosip_call_send_request succeeded...\n");
this->status = ESTABLISHED_TRANSFERRING;
eXosip_unlock(ctx);
TSPTRACE("TapiLine::handleEvent: sending REFER ... done\n");
}
break;
case ESTABLISHED:
TSPTRACE("TapiLine::handleEvent: current status: ESTABLISHED\n");
if (je->type == EXOSIP_CALL_RELEASED) {
TSPTRACE("TapiLine::handleEvent: EXOSIP_CALL_RELEASED received: (cid=%i did=%i) '%s'\n",je->cid,je->did,je->textinfo);
this->cid=0;
this->did=0;
this->tid=0;
this->status=IDLE;
checkForShutdown("established call released");
break;
}
break;
case ESTABLISHED_TRANSFERRING:
TSPTRACE("TapiLine::handleEvent: current status: ESTABLISHED_TRANSFERRING\n");
if (je->type == EXOSIP_CALL_RELEASED) {
TSPTRACE("TapiLine::handleEvent: EXOSIP_CALL_RELEASED received: (cid=%i did=%i) '%s'\n",je->cid,je->did,je->textinfo);
this->cid=0;
this->did=0;
this->tid=0;
this->status=IDLE;
checkForAsyncRefer("established_transferring call released", LINEERR_OPERATIONFAILED);
checkForShutdown("established_transfering call released");
break;
}
if (je->type == EXOSIP_CALL_MESSAGE_REQUESTFAILURE) {
TSPTRACE("TapiLine::handleEvent: EXOSIP_CALL_REQUESTFAILURE received: (cid=%i did=%i) '%s'\n",je->cid,je->did,je->textinfo);
if ( (je->response != NULL) && (je->response->status_code == 407) ) {
TSPTRACE("TapiLine::handleEvent:: 407 received, exosip should handle this...\n");
} else if ( (je->response != NULL) && (je->response->status_code == 401) ) {
TSPTRACE("TapiLine::handleEvent:: 401 received, exosip should handle this...\n");
} else {
TSPTRACE("TapiLine::handleEvent:: Call failed with status code %d, shuting down ...\n", je->response->status_code);
checkForAsyncRefer("established_transferring call requestfailure", LINEERR_OPERATIONFAILED);
this->shutdown_nowait();
}
checkForShutdown("established call requestfailure");
break;
}
if (je->type == EXOSIP_CALL_MESSAGE_ANSWERED) {
TSPTRACE("TapiLine::handleEvent: EXOSIP_CALL_MESSAGE_ANSWERED received: (cid=%i did=%i) '%s'\n",je->cid,je->did,je->textinfo);
this->status=ESTABLISHED_TRANSFER_ACCEPTED;
checkForAsyncRefer("established_transferring call messageanswered", 0);
break;
}
break;
case ESTABLISHED_TRANSFER_ACCEPTED:
TSPTRACE("TapiLine::handleEvent: current status: ESTABLISHED_TRANSFER_ACCEPTED\n");
if (je->type == EXOSIP_CALL_RELEASED) {
this->cid=0;
this->did=0;
this->tid=0;
this->status=IDLE;
checkForShutdown("established_transfer call released");
break;
}
if (je->type == EXOSIP_CALL_MESSAGE_NEW) {
TSPTRACE("TapiLine::handleEvent: EXOSIP_CALL_MESSAGE_NEW received: (cid=%i did=%i) '%s'\n",je->cid,je->did,je->textinfo);
// check if request is a NOTIFY
if (0 != osip_strcasecmp (je->request->sip_method, "NOTIFY")) {
TSPTRACE("TapiLine::handleEvent: non NOTIFY received, sending 403 ...\n");
eXosip_lock(ctx);
TSPTRACE("TapiLine::handleEvent: sending answer 403 ...\n");
i = eXosip_call_send_answer(ctx, je->tid, 403, NULL);
if (i != 0) {
TSPTRACE("TapiLine::handleEvent: eXosip_call_send_answer failed...\n");
eXosip_unlock(ctx);
this->shutdown_nowait();
break;
}
TSPTRACE("TapiLine::handleEvent: eXosip_call_send_answer succeeded...\n");
eXosip_unlock(ctx);
TSPTRACE("TapiLine::handleEvent: sending answer ... done\n");
break;
}
// compare NOTIFY did with previous did
if (je->did != this->did) {
TSPTRACE("TapiLine::handleEvent: NOTIFY does not match the INVITE/REFER dialog, send 486\n");
eXosip_lock(ctx);
TSPTRACE("TapiLine::handleEvent: sending answer 481 ...\n");
i = eXosip_call_send_answer(ctx, je->tid, 481, NULL);
if (i != 0) {
TSPTRACE("TapiLine::handleEvent: eXosip_call_send_answer failed...\n");
eXosip_unlock(ctx);
this->shutdown_nowait();
break;
}
TSPTRACE("TapiLine::handleEvent: eXosip_call_send_answer succeeded...\n");
eXosip_unlock(ctx);
TSPTRACE("TapiLine::handleEvent: sending answer ... done\n");
break;
}
//Respond to every NOTIFY with 200 OK
osip_message_t *answer;
eXosip_lock(ctx);
TSPTRACE("TapiLine::handleEvent: building answer to NOTIFY ...\n");
i = eXosip_call_build_answer(ctx, je->tid, 200, &answer);
if (i != 0) {
TSPTRACE("TapiLine::handleEvent: eXosip_call_build_answer failed...\n");
eXosip_unlock(ctx);
this->shutdown_nowait();
break;
}
TSPTRACE("TapiLine::handleEvent: eXosip_call_build_answer succeeded...\n");
i = eXosip_call_send_answer(ctx, je->tid, 200, answer);
if (i != 0) {
TSPTRACE("TapiLine::handleEvent: eXosip_call_send_answer failed...\n");
eXosip_unlock(ctx);
this->shutdown_nowait();
break;
}
TSPTRACE("TapiLine::handleEvent: eXosip_call_send_answer succeeded...\n");
TSPTRACE("TapiLine::handleEvent: sending answer ... done\n");
// check sipfrag response code, >=200 is final response which will cause a BYE
osip_body_t *body = NULL;
i = osip_message_get_body(je->request, 0, &body);
if (i != 0) {
TSPTRACE("TapiLine::handleEvent: osip_message_get_body failed...\n");
eXosip_unlock(ctx);
this->shutdown_nowait();
break;
}
TSPTRACE("TapiLine::handleEvent: osip_message_get_body succeeded...\n");
if (body == NULL) {
TSPTRACE("TapiLine::handleEvent: no body in NOTIFY ... ignore this NOTIFY and wait for next NOTIFY\n");
eXosip_unlock(ctx);
break;
}
TSPTRACE("TapiLine::handleEvent: bodylen=%d, body is: %.*s\n", body->length, body->length, body->body);
if ( !strncmp(body->body, "SIP/2.0 1", min(body->length, 9)) ) {
TSPTRACE("TapiLine::handleEvent: provisional response in NOTIFY body ... ignore this NOTIFY and wait for next NOTIFY\n");
eXosip_unlock(ctx);
break;
}
TSPTRACE("TapiLine::handleEvent: final response (or garbage) in NOTIFY body ... terminate call\n");
this->status = TapiLine::ESTABLISHED_TRANSFERRED;
eXosip_unlock(ctx);
this->reportStatus();
//sending BYE request
TSPTRACE("TapiLine::handleEvent: sending BYE (shutting down)\n");
this->shutdown_nowait();
// we do not wait for CALL_RELEASED from eXosip as this takes 20 seconds. Thus we delete the cid/did
// and then the CALL_RELEASED event will be ignored in TapiLine. We immediately report idle.
TSPTRACE("TapiLine::handleEvent: eXosip_call_terminate succeeded, set status to idle ...\n");
this->status = TapiLine::IDLE;
this->cid=0;
this->did=0;
this->tid=0;
// XXXX: not sure if this is really needed.
asyncCompletion("call transfered");
this->reportStatus();
//TSPTRACE("TapiLine::handleEvent: sending BYE\n");
//eXosip_lock;
//i = eXosip_call_terminate(ctx, je->cid, je->did);
//if (i != 0) {
// TSPTRACE("TapiLine::handleEvent: eXosip_call_terminate failed...\n");
// eXosip_unlock(ctx);
// this->shutdown();
// break;
//}
//eXosip_unlock(ctx);
//TSPTRACE("TapiLine::handleEvent: eXosip_call_terminate succeeded...\n");
break;
}
break;
case ESTABLISHED_TRANSFERRED:
TSPTRACE("TapiLine::handleEvent: current status: ESTABLISHED_TRANSFERRED\n");
if (je->type == EXOSIP_CALL_RELEASED) {
TSPTRACE("TapiLine::handleEvent: EXOSIP_CALL_RELEASED received: (cid=%i did=%i) '%s'\n",je->cid,je->did,je->textinfo);
this->cid=0;
this->did=0;
this->tid=0;
this->status=IDLE;
checkForShutdown("established_transferred call released");
break;
}
break;
default:
TSPTRACE("TapiLine::handleEvent: current status: unknown --> ERROR\n");
break;