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