res_rtp_asterisk.c: Fix rtp source address learning for broken clients
[asterisk/asterisk.git] / addons / ooh323c / src / ooCalls.c
1 /*
2  * Copyright (C) 2004-2005 by Objective Systems, Inc.
3  *
4  * This software is furnished under an open source license and may be 
5  * used and copied only in accordance with the terms of this license. 
6  * The text of the license may generally be found in the root 
7  * directory of this installation in the COPYING file.  It 
8  * can also be viewed online at the following URL:
9  *
10  *   http://www.obj-sys.com/open/license.html
11  *
12  * Any redistributions of this file including modified versions must 
13  * maintain this copyright notice.
14  *
15  *****************************************************************************/
16
17 #include "asterisk.h"
18 #include "asterisk/lock.h"
19 #include "asterisk/utils.h"
20
21 #include "ootrace.h"
22 #include "ootypes.h"
23 #include "ooCalls.h"
24 #include "ooUtils.h"
25 #include "ooports.h"
26 #include "oochannels.h"
27 #include "ooh245.h"
28 #include "ooCapability.h"
29 #include "ooGkClient.h"
30 #include "ooh323ep.h"
31 #include "ooCalls.h"
32
33 /** Global endpoint structure */
34 extern OOH323EndPoint gH323ep;
35 extern ast_mutex_t callListLock;
36 extern ast_mutex_t newCallLock;
37
38 OOH323CallData* ooCreateCall(char* type, char*callToken)
39 {
40    OOH323CallData *call=NULL;
41    OOCTXT *pctxt=NULL;
42    OOCTXT *msgctxt=NULL;
43
44    pctxt = newContext();
45    if(!pctxt)
46    {
47       OOTRACEERR1("ERROR:Failed to create OOCTXT for new call\n");
48       return NULL;
49    }
50    msgctxt = newContext();
51    if(!msgctxt)
52    {
53       OOTRACEERR1("ERROR:Failed to create OOCTXT for new call\n");
54       return NULL;
55    }
56    ast_mutex_lock(&newCallLock);
57    /* call = (OOH323CallData*)memAlloc(&gH323ep.ctxt, sizeof(OOH323CallData));  */
58    call = (OOH323CallData*)memAlloc(pctxt, sizeof(OOH323CallData));
59    ast_mutex_unlock(&newCallLock);
60    if(!call)
61    {
62       OOTRACEERR1("ERROR:Memory - ooCreateCall - call\n");
63       return NULL;
64    } 
65    memset(call, 0, sizeof(OOH323CallData));
66    ast_cond_init(&call->gkWait, NULL);
67    ast_mutex_init(&call->GkLock);
68    ast_mutex_init(&call->Lock);
69    call->pctxt = pctxt;
70    call->msgctxt = msgctxt;
71    call->callMode = gH323ep.callMode;
72    sprintf(call->callToken, "%s", callToken);
73    sprintf(call->callType, "%s", type);
74    call->callReference = 0;
75    if(gH323ep.callerid) {
76      strncpy(call->ourCallerId, gH323ep.callerid, sizeof(call->ourCallerId)-1);
77      call->ourCallerId[sizeof(call->ourCallerId)-1] = '\0';
78    }
79    else {
80       call->ourCallerId[0] = '\0';
81    }
82    
83    memset(&call->callIdentifier, 0, sizeof(H225CallIdentifier));
84    memset(&call->confIdentifier, 0, sizeof(H225ConferenceIdentifier));
85
86    call->flags = 0;
87    if (OO_TESTFLAG(gH323ep.flags, OO_M_TUNNELING))
88       OO_SETFLAG (call->flags, OO_M_TUNNELING);
89
90    if(gH323ep.gkClient)
91    {
92       if(OO_TESTFLAG(gH323ep.flags, OO_M_GKROUTED))
93       {
94          OO_SETFLAG(call->flags, OO_M_GKROUTED);
95       }
96    }
97
98    if (OO_TESTFLAG(gH323ep.flags, OO_M_FASTSTART))
99       OO_SETFLAG (call->flags, OO_M_FASTSTART);
100
101    if (OO_TESTFLAG(gH323ep.flags, OO_M_MEDIAWAITFORCONN))
102       OO_SETFLAG (call->flags, OO_M_MEDIAWAITFORCONN);
103
104    call->fsSent = FALSE;
105
106 // May 20090713. Fix it for Video session
107
108    OO_SETFLAG(call->flags, OO_M_AUDIOSESSION);
109    
110    call->callState = OO_CALL_CREATED;
111    call->callEndReason = OO_REASON_UNKNOWN;
112    call->pCallFwdData = NULL;
113
114    if(!strcmp(call->callType, "incoming"))
115    {
116       call->callingPartyNumber = NULL;
117    }
118    else{      
119       if(ooUtilsIsStrEmpty(gH323ep.callingPartyNumber))
120       {
121          call->callingPartyNumber = NULL;
122       }
123       else{
124          call->callingPartyNumber = (char*) memAlloc(call->pctxt, 
125                                          strlen(gH323ep.callingPartyNumber)+1);
126          if(call->callingPartyNumber)
127          {
128             strcpy(call->callingPartyNumber, gH323ep.callingPartyNumber);
129          }
130          else{
131             OOTRACEERR3("Error:Memory - ooCreateCall - callingPartyNumber"
132                         ".(%s, %s)\n", call->callType, call->callToken);
133             freeContext(pctxt);
134             return NULL;
135          }
136       }
137    }
138
139    call->calledPartyNumber = NULL;
140    call->h245ConnectionAttempts = 0;
141    call->h245SessionState = OO_H245SESSION_IDLE;
142    call->dtmfmode = gH323ep.dtmfmode;
143    call->mediaInfo = NULL;
144    strcpy(call->localIP, gH323ep.signallingIP);
145    call->pH225Channel = NULL;
146    call->pH245Channel = NULL;
147    call->h245listener = NULL;
148    call->h245listenport = NULL;
149    call->remoteIP[0] = '\0';
150    call->remotePort = 0;
151    call->remoteH245Port = 0;
152    call->remoteDisplayName = NULL;
153    call->remoteAliases = NULL;
154    call->ourAliases = NULL;
155    call->masterSlaveState = OO_MasterSlave_Idle;
156    call->statusDeterminationNumber = 0;
157    call->localTermCapState = OO_LocalTermCapExchange_Idle;
158    call->remoteTermCapState = OO_RemoteTermCapExchange_Idle; 
159    call->ourCaps = NULL;
160    call->remoteCaps = NULL;
161    call->jointCaps = NULL;
162    dListInit(&call->remoteFastStartOLCs);
163    call->remoteTermCapSeqNo =0;
164    call->localTermCapSeqNo = 0;
165    memcpy(&call->capPrefs, &gH323ep.capPrefs, sizeof(OOCapPrefs));    
166    call->logicalChans = NULL;
167    call->noOfLogicalChannels = 0;
168    call->logicalChanNoBase = 1001;
169    call->logicalChanNoMax = 1100;
170    call->logicalChanNoCur = 1001;
171    call->nextSessionID = 4; /* 1,2,3 are reserved for audio, video and data */
172    dListInit(&call->timerList);
173    call->msdRetries = 0;
174    call->pFastStartRes = NULL;
175    call->usrData = NULL;
176    OOTRACEINFO3("Created a new call (%s, %s)\n", call->callType, 
177                  call->callToken);
178    /* Add new call to calllist */
179    ooAddCallToList (call);
180    if(gH323ep.h323Callbacks.onNewCallCreated)
181      gH323ep.h323Callbacks.onNewCallCreated(call);
182    return call;
183 }
184
185 int ooAddCallToList(OOH323CallData *call)
186 {
187    ast_mutex_lock(&callListLock);
188
189    if(!gH323ep.callList)
190    {
191       gH323ep.callList = call;
192       call->next = NULL;
193       call->prev = NULL;
194    }
195    else{
196       call->next = gH323ep.callList;
197       call->prev = NULL;
198       gH323ep.callList->prev = call;
199       gH323ep.callList = call;
200    }
201
202    ast_mutex_unlock(&callListLock);
203
204    return OO_OK;
205 }
206
207
208 int ooEndCall(OOH323CallData *call)
209 {
210    OOTRACEDBGA4("In ooEndCall call state is - %s (%s, %s)\n", 
211                  ooGetCallStateText(call->callState), call->callType, 
212                  call->callToken);
213
214    if(call->callState == OO_CALL_REMOVED) {
215       OOTRACEINFO2("Call already removed %s\n",
216                     call->callToken);
217      return OO_OK;
218    }
219
220    if (call->callIdentifier.guid.numocts == 0) call->callState = OO_CALL_CLEARED;
221
222    if(!call->pH225Channel || call->pH225Channel->sock ==0)
223    {
224       call->callState = OO_CALL_CLEARED;
225    }
226
227    if(call->callState == OO_CALL_CLEARED || ((strcmp(call->callType, "incoming")) &&
228      call->callState == OO_CALL_CLEAR_RELEASESENT))
229    {
230       ooCleanCall(call); 
231       call->callState = OO_CALL_REMOVED;
232       return OO_OK;
233    }
234
235    if(call->logicalChans)
236    {
237       OOTRACEINFO3("Clearing all logical channels. (%s, %s)\n", call->callType,
238                     call->callToken);
239       ooClearAllLogicalChannels(call);
240    }
241
242    if(!OO_TESTFLAG(call->flags, OO_M_ENDSESSION_BUILT))
243    {
244       if(call->h245SessionState == OO_H245SESSION_ACTIVE ||
245          call->h245SessionState == OO_H245SESSION_ENDRECVD)
246       {
247          ooSendEndSessionCommand(call);
248          OO_SETFLAG(call->flags, OO_M_ENDSESSION_BUILT);
249       }
250    }
251
252
253    if(!OO_TESTFLAG(call->flags, OO_M_RELEASE_BUILT))   
254    {
255      if(call->callState == OO_CALL_CLEAR || 
256         call->callState == OO_CALL_CLEAR_RELEASERECVD)
257      {
258         ooSendReleaseComplete(call);
259         OO_SETFLAG(call->flags, OO_M_RELEASE_BUILT);
260      }
261    }
262       
263    return OO_OK;
264 }
265
266
267
268 int ooRemoveCallFromList (OOH323CallData *call)
269 {
270    if(!call || !gH323ep.callList)
271       return OO_OK;
272
273    ast_mutex_lock(&callListLock);
274
275    OOTRACEINFO3("Removing call %lx: %s\n", call, call->callToken);
276
277    if(call == gH323ep.callList)
278    {
279       if(!call->next)
280          gH323ep.callList = NULL;
281       else{
282          call->next->prev = NULL;
283          gH323ep.callList = call->next;
284       }
285    }
286    else{
287       call->prev->next = call->next;
288       if(call->next)
289          call->next->prev = call->prev;
290    }
291
292    ast_mutex_unlock(&callListLock);
293
294    return OO_OK;
295 }
296
297 int ooCleanCall(OOH323CallData *call)
298 {
299    OOCTXT *pctxt;
300
301    OOTRACEWARN4 ("Cleaning Call (%s, %s)- reason:%s\n", 
302                  call->callType, call->callToken, 
303                  ooGetReasonCodeText (call->callEndReason));
304
305    /* First clean all the logical channels, if not already cleaned. */
306    if(call->logicalChans)
307       ooClearAllLogicalChannels(call);
308
309    /* Close H.245 connection, if not already closed */
310    if(call->h245SessionState != OO_H245SESSION_CLOSED)
311       ooCloseH245Connection(call);
312    else{
313       if(call->pH245Channel && call->pH245Channel->outQueue.count > 0)
314       {
315          dListFreeAll(call->pctxt, &(call->pH245Channel->outQueue));
316          memFreePtr(call->pctxt, call->pH245Channel);
317       }
318    }
319
320    /* Close H.245 listener, if not already closed */
321    if(call->h245listener)
322    {
323       ooCloseH245Listener(call);
324    }
325    
326    /* Close H225 connection, if not already closed. */
327    if (0 != call->pH225Channel && 0 != call->pH225Channel->sock)
328    {
329       ooCloseH225Connection(call);
330    }
331
332    /* Clean timers */
333    if(call->timerList.count > 0)
334    {
335       dListFreeAll(call->pctxt, &(call->timerList));
336    }
337
338    if(gH323ep.gkClient && !OO_TESTFLAG(call->flags, OO_M_DISABLEGK))
339    {
340       ooGkClientCleanCall(gH323ep.gkClient, call);
341    }
342
343    ooRemoveCallFromList (call);
344    OOTRACEINFO3("Removed call (%s, %s) from list\n", call->callType, 
345                  call->callToken);
346
347    if(call->pCallFwdData && call->pCallFwdData->fwdedByRemote)
348    {
349
350       if(gH323ep.h323Callbacks.onCallForwarded)
351          gH323ep.h323Callbacks.onCallForwarded(call);
352
353       if(ooH323HandleCallFwdRequest(call)!= OO_OK)
354       {
355          OOTRACEERR3("Error:Failed to forward call (%s, %s)\n", call->callType,
356                      call->callToken);
357       }
358    }
359    else {
360       if(gH323ep.h323Callbacks.onCallCleared)
361          gH323ep.h323Callbacks.onCallCleared(call);
362    }
363
364    if (call->rtpMask) {
365         ast_mutex_lock(&call->rtpMask->lock);
366         call->rtpMask->inuse--;
367         ast_mutex_unlock(&call->rtpMask->lock);
368         if ((call->rtpMask->inuse) == 0) {
369                 regfree(&call->rtpMask->regex);
370                 ast_mutex_destroy(&call->rtpMask->lock);
371                 ast_free(call->rtpMask);
372         }
373    }
374
375    if ((pctxt = call->msgctxt) != NULL) {
376         freeContext(pctxt);
377         ast_free(pctxt);
378         call->msgctxt = NULL;
379    }
380 /* May !!!! Fix it !! */
381    /* free(pctxt); */
382
383    return OO_OK;
384 }
385
386
387 int ooCallSetCallerId(OOH323CallData* call, const char* callerid)
388 {
389    if(!call || !callerid) return OO_FAILED;
390    strncpy(call->ourCallerId, callerid, sizeof(call->ourCallerId)-1);
391    call->ourCallerId[sizeof(call->ourCallerId)-1]='\0';
392    return OO_OK;
393 }
394
395 int ooCallSetCallingPartyNumber(OOH323CallData *call, const char *number)
396 {
397    if(call->callingPartyNumber) 
398       memFreePtr(call->pctxt, call->callingPartyNumber);
399
400    call->callingPartyNumber = (char*) memAlloc(call->pctxt, strlen(number)+1);
401    if(call->callingPartyNumber)
402    {
403      strcpy(call->callingPartyNumber, number);
404    }
405    else{
406       OOTRACEERR3("Error:Memory - ooCallSetCallingPartyNumber - "
407                   "callingPartyNumber.(%s, %s)\n", call->callType, 
408                   call->callToken);
409       return OO_FAILED;
410    }
411    /* Set dialed digits alias */
412    /*   if(!strcmp(call->callType, "outgoing"))
413    {
414       ooCallAddAliasDialedDigits(call, number);
415    }*/
416    return OO_OK;
417 }
418
419 int ooCallGetCallingPartyNumber(OOH323CallData *call, char *buffer, int len)
420 {
421    if(call->callingPartyNumber)
422    {
423       if(len>(int)strlen(call->callingPartyNumber))
424       {
425          strcpy(buffer, call->callingPartyNumber);
426          return OO_OK;
427       }
428    }
429    
430    return OO_FAILED;
431 }
432
433
434 int ooCallSetCalledPartyNumber(OOH323CallData *call, const char *number)
435 {
436    if(call->calledPartyNumber) 
437       memFreePtr(call->pctxt, call->calledPartyNumber);
438
439    call->calledPartyNumber = (char*) memAlloc(call->pctxt, strlen(number)+1);
440    if(call->calledPartyNumber)
441    {
442      strcpy(call->calledPartyNumber, number);
443    }
444    else{
445       OOTRACEERR3("Error:Memory - ooCallSetCalledPartyNumber - "
446                   "calledPartyNumber.(%s, %s)\n", call->callType, 
447                   call->callToken);
448       return OO_FAILED;
449    }
450    return OO_OK;
451 }
452
453 int ooCallGetCalledPartyNumber(OOH323CallData *call, char *buffer, int len)
454 {
455    if(call->calledPartyNumber)
456    {
457       if(len>(int)strlen(call->calledPartyNumber))
458       {
459          strcpy(buffer, call->calledPartyNumber);
460          return OO_OK;
461       }
462    }
463    
464    return OO_FAILED;
465 }
466
467 int ooCallClearAliases(OOH323CallData *call)
468 {
469    if(call->ourAliases)
470       memFreePtr(call->pctxt, call->ourAliases);
471    call->ourAliases = NULL;
472    return OO_OK;
473 }
474
475
476 int ooCallAddAlias
477    (OOH323CallData *call, int aliasType, const char *value, OOBOOL local)
478 {
479    ooAliases * psNewAlias=NULL;
480    psNewAlias = (ooAliases*)memAlloc(call->pctxt, sizeof(ooAliases));
481    if(!psNewAlias)
482    {
483       OOTRACEERR3("Error:Memory - ooCallAddAlias - psNewAlias"
484                   "(%s, %s)\n", call->callType, call->callToken);
485       return OO_FAILED;
486    }
487    psNewAlias->type = aliasType;
488    psNewAlias->value = (char*) memAlloc(call->pctxt, strlen(value)+1);
489    if(!psNewAlias->value)
490    {
491       OOTRACEERR3("Error:Memory - ooCallAddAlias - psNewAlias->value"
492                   " (%s, %s)\n", call->callType, call->callToken);
493       memFreePtr(call->pctxt, psNewAlias);
494       return OO_FAILED;
495    }
496    strcpy(psNewAlias->value, value);
497
498    if(local)
499    {
500       psNewAlias->next = call->ourAliases;
501       call->ourAliases = psNewAlias;
502    }
503    else {
504      psNewAlias->next = call->remoteAliases;
505      call->remoteAliases = psNewAlias;
506    }
507
508    OOTRACEDBGC5("Added %s alias %s to call. (%s, %s)\n", 
509               local?"local":"remote", value, call->callType, call->callToken);
510    return OO_OK;
511 }
512
513 int ooCallAddAliasH323ID(OOH323CallData *call, const char* h323id)
514 {
515    return ooCallAddAlias(call, T_H225AliasAddress_h323_ID, h323id, TRUE);
516 }
517
518
519 int ooCallAddAliasDialedDigits(OOH323CallData *call, const char* dialedDigits)
520 {
521    return ooCallAddAlias
522                (call, T_H225AliasAddress_dialedDigits, dialedDigits, TRUE);
523 }
524
525
526 int ooCallAddAliasEmailID(OOH323CallData *call, const char* email)
527 {
528    return ooCallAddAlias(call, T_H225AliasAddress_email_ID, email, TRUE);
529 }
530
531
532 int ooCallAddAliasURLID(OOH323CallData *call, const char* url)
533 {
534    return ooCallAddAlias(call, T_H225AliasAddress_url_ID, url, TRUE);
535
536  
537
538 int ooCallAddRemoteAliasH323ID(OOH323CallData *call, const char* h323id)
539 {
540    return ooCallAddAlias(call, T_H225AliasAddress_h323_ID, h323id, FALSE);
541 }
542
543 int ooCallAddRemoteAliasDialedDigits
544    (OOH323CallData *call, const char* dialedDigits)
545 {
546    return ooCallAddAlias
547               (call, T_H225AliasAddress_dialedDigits, dialedDigits, FALSE);
548 }
549
550
551
552 /* Used to override global end point capabilities and add call specific 
553    capabilities */
554
555 int ooCallAddG726Capability(OOH323CallData *call, int cap, int txframes, 
556                             int rxframes, OOBOOL silenceSuppression, int dir,
557                             cb_StartReceiveChannel startReceiveChannel,
558                             cb_StartTransmitChannel startTransmitChannel,
559                             cb_StopReceiveChannel stopReceiveChannel,
560                             cb_StopTransmitChannel stopTransmitChannel)
561 {
562    return ooCapabilityAddSimpleCapability(call, cap, txframes, rxframes, 
563                                 silenceSuppression, dir, startReceiveChannel, 
564                                 startTransmitChannel, stopReceiveChannel, 
565                                 stopTransmitChannel, FALSE);
566 }
567 int ooCallAddAMRNBCapability(OOH323CallData *call, int cap, int txframes, 
568                             int rxframes, OOBOOL silenceSuppression, int dir,
569                             cb_StartReceiveChannel startReceiveChannel,
570                             cb_StartTransmitChannel startTransmitChannel,
571                             cb_StopReceiveChannel stopReceiveChannel,
572                             cb_StopTransmitChannel stopTransmitChannel)
573 {
574    return ooCapabilityAddSimpleCapability(call, cap, txframes, rxframes, 
575                                 silenceSuppression, dir, startReceiveChannel, 
576                                 startTransmitChannel, stopReceiveChannel, 
577                                 stopTransmitChannel, FALSE);
578 }
579
580 int ooCallAddSpeexCapability(OOH323CallData *call, int cap, int txframes, 
581                             int rxframes, OOBOOL silenceSuppression, int dir,
582                             cb_StartReceiveChannel startReceiveChannel,
583                             cb_StartTransmitChannel startTransmitChannel,
584                             cb_StopReceiveChannel stopReceiveChannel,
585                             cb_StopTransmitChannel stopTransmitChannel)
586 {
587    return ooCapabilityAddSimpleCapability(call, cap, txframes, rxframes, 
588                                 silenceSuppression, dir, startReceiveChannel, 
589                                 startTransmitChannel, stopReceiveChannel, 
590                                 stopTransmitChannel, FALSE);
591 }
592
593 int ooCallAddG7231Capability(OOH323CallData *call, int cap, int txframes, 
594                             int rxframes, OOBOOL silenceSuppression, int dir,
595                             cb_StartReceiveChannel startReceiveChannel,
596                             cb_StartTransmitChannel startTransmitChannel,
597                             cb_StopReceiveChannel stopReceiveChannel,
598                             cb_StopTransmitChannel stopTransmitChannel)
599 {
600    return ooCapabilityAddSimpleCapability(call, cap, txframes, rxframes, 
601                                 silenceSuppression, dir, startReceiveChannel, 
602                                 startTransmitChannel, stopReceiveChannel, 
603                                 stopTransmitChannel, FALSE);
604 }
605
606
607
608 int ooCallAddG729Capability(OOH323CallData *call, int cap, int txframes, 
609                             int rxframes, int dir,
610                             cb_StartReceiveChannel startReceiveChannel,
611                             cb_StartTransmitChannel startTransmitChannel,
612                             cb_StopReceiveChannel stopReceiveChannel,
613                             cb_StopTransmitChannel stopTransmitChannel)
614 {
615    return ooCapabilityAddSimpleCapability(call, cap, txframes, rxframes, FALSE,
616                           dir, startReceiveChannel, startTransmitChannel, 
617                           stopReceiveChannel, stopTransmitChannel, FALSE);
618 }
619
620 /*
621 int ooCallAddG726Capability(OOH323CallData *call, int cap, int txframes, 
622                             int rxframes, int dir,
623                             cb_StartReceiveChannel startReceiveChannel,
624                             cb_StartTransmitChannel startTransmitChannel,
625                             cb_StopReceiveChannel stopReceiveChannel,
626                             cb_StopTransmitChannel stopTransmitChannel)
627 {
628    return ooCapabilityAddSimpleCapability(call, cap, txframes, rxframes, FALSE,
629                           dir, startReceiveChannel, startTransmitChannel, 
630                           stopReceiveChannel, stopTransmitChannel, FALSE);
631 }
632 */
633
634 int ooCallAddG728Capability(OOH323CallData *call, int cap, int txframes, 
635                             int rxframes, int dir,
636                             cb_StartReceiveChannel startReceiveChannel,
637                             cb_StartTransmitChannel startTransmitChannel,
638                             cb_StopReceiveChannel stopReceiveChannel,
639                             cb_StopTransmitChannel stopTransmitChannel)
640 {
641    return ooCapabilityAddSimpleCapability(call, cap, txframes, rxframes, FALSE,
642                           dir, startReceiveChannel, startTransmitChannel, 
643                           stopReceiveChannel, stopTransmitChannel, FALSE);
644 }
645
646 int ooCallAddG711Capability(OOH323CallData *call, int cap, int txframes, 
647                             int rxframes, int dir,
648                             cb_StartReceiveChannel startReceiveChannel,
649                             cb_StartTransmitChannel startTransmitChannel,
650                             cb_StopReceiveChannel stopReceiveChannel,
651                             cb_StopTransmitChannel stopTransmitChannel)
652 {
653    return ooCapabilityAddSimpleCapability(call, cap, txframes, rxframes, FALSE,
654                             dir, startReceiveChannel, startTransmitChannel, 
655                             stopReceiveChannel, stopTransmitChannel, FALSE);
656 }
657
658 int ooCallAddGSMCapability
659    (OOH323CallData* call, int cap, ASN1USINT framesPerPkt, 
660     OOBOOL comfortNoise, OOBOOL scrambled, int dir,
661     cb_StartReceiveChannel startReceiveChannel,
662     cb_StartTransmitChannel startTransmitChannel,
663     cb_StopReceiveChannel stopReceiveChannel,
664     cb_StopTransmitChannel stopTransmitChannel)
665 {
666    return ooCapabilityAddGSMCapability(call, cap, framesPerPkt, comfortNoise, 
667                                      scrambled, dir, startReceiveChannel, 
668                                      startTransmitChannel, stopReceiveChannel,
669                                      stopTransmitChannel, FALSE);
670 }
671
672
673 int ooCallAddH263VideoCapability
674    (OOH323CallData *call, int cap, unsigned sqcifMPI, unsigned qcifMPI, 
675     unsigned cifMPI, unsigned cif4MPI, unsigned cif16MPI, unsigned maxBitRate, 
676     int dir, cb_StartReceiveChannel startReceiveChannel,
677     cb_StartTransmitChannel startTransmitChannel,
678     cb_StopReceiveChannel stopReceiveChannel,
679     cb_StopTransmitChannel stopTransmitChannel)
680 {
681
682    return ooCapabilityAddH263VideoCapability(call, sqcifMPI, qcifMPI, cifMPI,
683                                      cif4MPI, cif16MPI, maxBitRate,dir,
684                                      startReceiveChannel, startTransmitChannel,
685                                      stopReceiveChannel, stopTransmitChannel, 
686                                      FALSE);
687
688 }
689
690 int ooCallEnableDTMFRFC2833(OOH323CallData *call, int dynamicRTPPayloadType)
691 {
692    return ooCapabilityEnableDTMFRFC2833(call, dynamicRTPPayloadType);
693 }
694
695 int ooCallDisableDTMFRFC2833(OOH323CallData *call)
696 {
697   return ooCapabilityDisableDTMFRFC2833(call);
698 }
699
700 int ooCallEnableDTMFCISCO(OOH323CallData *call, int dynamicRTPPayloadType)
701 {
702    return ooCapabilityEnableDTMFCISCO(call, dynamicRTPPayloadType);
703 }
704
705 int ooCallDisableDTMFCISCO(OOH323CallData *call)
706 {
707   return ooCapabilityDisableDTMFCISCO(call);
708 }
709
710
711 int ooCallEnableDTMFH245Alphanumeric(OOH323CallData *call)
712 {
713    return ooCapabilityEnableDTMFH245Alphanumeric(call);
714 }
715
716 int ooCallDisableDTMFH245Alphanumeric(OOH323CallData *call)
717 {
718    return ooCapabilityDisableDTMFH245Alphanumeric(call);
719 }
720
721 int ooCallEnableDTMFH245Signal(OOH323CallData *call)
722 {
723    return ooCapabilityEnableDTMFH245Signal(call);
724 }
725
726 int ooCallDisableDTMFH245Signal(OOH323CallData *call)
727 {
728    return ooCapabilityDisableDTMFH245Signal(call);
729 }
730
731 int ooCallEnableDTMFQ931Keypad(OOH323CallData *call)
732 {
733    return ooCapabilityEnableDTMFQ931Keypad(call);
734 }
735
736 int ooCallDisableDTMFQ931Keypad(OOH323CallData *call)
737 {
738    return ooCapabilityDisableDTMFQ931Keypad(call);
739 }
740
741
742 OOH323CallData* ooFindCallByToken(const char *callToken)
743 {
744    OOH323CallData *call;
745    if(!callToken)
746    {
747       OOTRACEERR1("ERROR:Invalid call token passed - ooFindCallByToken\n");
748       return NULL;
749    }
750
751    ast_mutex_lock(&callListLock);
752
753    if(!gH323ep.callList)
754    {
755       OOTRACEERR1("ERROR: Empty calllist - ooFindCallByToken failed\n");
756       ast_mutex_unlock(&callListLock);
757       return NULL;
758    }
759    call = gH323ep.callList;
760    while(call)
761    {
762       if(!strcmp(call->callToken, callToken))
763          break;
764       else
765          call = call->next;
766    }
767    
768    if(!call)
769    {
770       OOTRACEERR2("ERROR:Call with token %s not found\n", callToken);
771       ast_mutex_unlock(&callListLock);
772       return NULL;
773    }
774
775    ast_mutex_unlock(&callListLock);
776
777    OOTRACEINFO3("INFO: FinCall returned %lx for call: %s\n", call, callToken);
778
779    return call;
780 }
781
782
783
784 /* Checks whether session with suplied ID and direction is already active*/
785 ASN1BOOL ooIsSessionEstablished(OOH323CallData *call, int sessionID, char* dir)
786 {
787    OOLogicalChannel * temp = NULL;
788    temp = call->logicalChans;
789    while(temp)
790    {
791       if(temp->sessionID == sessionID              &&
792          temp->state == OO_LOGICALCHAN_ESTABLISHED && 
793          !strcmp(temp->dir, dir)                     )
794          return TRUE;
795       temp = temp->next;
796    }
797    return FALSE;
798 }
799
800 int ooAddMediaInfo(OOH323CallData *call, OOMediaInfo mediaInfo)
801 {
802    OOMediaInfo *newMediaInfo=NULL;
803
804    if(!call)
805    {
806       OOTRACEERR3("Error:Invalid 'call' param for ooAddMediaInfo.(%s, %s)\n",
807                    call->callType, call->callToken);
808       return OO_FAILED;
809    }
810    newMediaInfo = (OOMediaInfo*) memAlloc(call->pctxt, sizeof(OOMediaInfo));
811    if(!newMediaInfo)
812    {
813       OOTRACEERR3("Error:Memory - ooAddMediaInfo - newMediaInfo. "
814                   "(%s, %s)\n", call->callType, call->callToken);
815       return OO_FAILED;
816    }
817
818    memcpy (newMediaInfo, &mediaInfo, sizeof(OOMediaInfo));
819
820    OOTRACEDBGC4("Configured mediainfo for cap %s (%s, %s)\n", 
821                 ooGetCapTypeText(mediaInfo.cap),
822                 call->callType, call->callToken);
823    if(!call->mediaInfo) {
824       newMediaInfo->next = NULL;
825       call->mediaInfo = newMediaInfo;
826    }
827    else {
828       newMediaInfo->next = call->mediaInfo;
829       call->mediaInfo = newMediaInfo;
830    }
831    return OO_OK;
832 }
833
834 unsigned ooCallGenerateSessionID
835    (OOH323CallData *call, OOCapType type, char *dir)
836 {
837    unsigned sessionID=0;
838
839    if(type == OO_CAP_TYPE_AUDIO)
840    {
841       if(!ooGetLogicalChannel(call, 1, dir))
842       {
843          sessionID = 1;
844       }
845       else{
846          if(call->masterSlaveState == OO_MasterSlave_Master)
847             sessionID = call->nextSessionID++;
848          else{
849             OOTRACEDBGC4("Session id for %s channel of type audio has to be "
850                         "provided by remote.(%s, %s)\n", dir, call->callType, 
851                          call->callToken);
852             sessionID = 0; /* Will be assigned by remote */
853          }
854       }
855    }
856
857    if(type == OO_CAP_TYPE_VIDEO)
858    {
859       if(!ooGetLogicalChannel(call, 2, dir))
860       {
861          sessionID = 2;
862       }
863       else{
864          if(call->masterSlaveState == OO_MasterSlave_Master)
865             sessionID = call->nextSessionID++;
866          else{
867             sessionID = 0; /* Will be assigned by remote */
868             OOTRACEDBGC4("Session id for %s channel of type video has to be "
869                         "provided by remote.(%s, %s)\n", dir, call->callType, 
870                          call->callToken);
871          }
872       }
873    }
874    if(type == OO_CAP_TYPE_DATA)
875    {
876       if(!ooGetLogicalChannel(call, 3, dir))
877       {
878          sessionID = 3;
879       }
880       else{
881          if(call->masterSlaveState == OO_MasterSlave_Master)
882             sessionID = call->nextSessionID++;
883          else{
884             sessionID = 0; /* Will be assigned by remote */
885             OOTRACEDBGC4("Session id for %s channel of type data has to be "
886                         "provided by remote.(%s, %s)\n", dir, call->callType, 
887                          call->callToken);
888          }
889       }
890    }
891    return sessionID;
892
893 }
894
895
896 int ooCallH245ConnectionRetryTimerExpired(void *data)
897 {
898    ooTimerCallback *cbData = (ooTimerCallback*) data;
899    OOH323CallData *call = cbData->call;
900
901    OOTRACEINFO3("H245 connection retry timer expired. (%s, %s)\n", 
902                                             call->callType, call->callToken); 
903    memFreePtr(call->pctxt, cbData);
904
905    call->h245ConnectionAttempts++;
906
907    ooCreateH245Connection(call);
908
909    return OO_OK;
910 }
911
912 const char* ooGetReasonCodeText (OOUINT32 code)
913 {
914    static const char* reasonCodeText[] = {
915       "OO_REASON_UNKNOWN", 
916       "OO_REASON_INVALIDMESSAGE",
917       "OO_REASON_TRANSPORTFAILURE", 
918       "OO_REASON_NOROUTE",
919       "OO_REASON_NOUSER",
920       "OO_REASON_NOBW",
921       "OO_REASON_GK_NOCALLEDUSER",
922       "OO_REASON_GK_NOCALLERUSER",
923       "OO_REASON_GK_NORESOURCES",
924       "OO_REASON_GK_UNREACHABLE",
925       "OO_REASON_GK_CLEARED",
926       "OO_REASON_NOCOMMON_CAPABILITIES",
927       "OO_REASON_REMOTE_FWDED",   
928       "OO_REASON_LOCAL_FWDED",
929       "OO_REASON_REMOTE_CLEARED", 
930       "OO_REASON_LOCAL_CLEARED", 
931       "OO_REASON_REMOTE_BUSY",
932       "OO_REASON_LOCAL_BUSY",
933       "OO_REASON_REMOTE_NOANSWER",
934       "OO_REASON_LOCAL_NOTANSWERED",
935       "OO_REASON_REMOTE_REJECTED",
936       "OO_REASON_LOCAL_REJECTED",
937       "OO_REASON_REMOTE_CONGESTED",
938       "OO_REASON_LOCAL_CONGESTED"
939    };
940    return ooUtilsGetText (code, reasonCodeText, OONUMBEROF(reasonCodeText));
941 }
942
943 const char* ooGetCallStateText (OOCallState callState)
944 {
945    static const char* callStateText[] = {
946       "OO_CALL_CREATED",
947       "OO_CALL_WAITING_ADMISSION",
948       "OO_CALL_CONNECTING",
949       "OO_CALL_CONNECTED",
950       "OO_CALL_PAUSED",
951       "OO_CALL_CLEAR",
952       "OO_CALL_CLEAR_RELEASERECVD",
953       "OO_CALL_CLEAR_RELEASESENT",
954       "OO_CALL_CLEARED"
955    };
956    return ooUtilsGetText (callState, callStateText, OONUMBEROF(callStateText));
957 }
958