Move Asterisk-addons modules into the main Asterisk source tree.
[asterisk/asterisk.git] / addons / ooh323c / src / ooCmdChannel.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 "ooStackCmds.h"
18 #include "ootrace.h"
19 #include "ooq931.h"
20 #include "ooh245.h"
21 #include "ooh323ep.h"
22 #include "oochannels.h"
23 #include "ooCalls.h"
24 #include "ooCmdChannel.h"
25
26 /** Global endpoint structure */
27 extern OOH323EndPoint gH323ep;
28
29 OOSOCKET gCmdChan = 0;
30 pthread_mutex_t gCmdChanLock;
31
32 int ooCreateCmdConnection()
33 {
34    int ret = 0;
35    int thePipe[2];
36
37    if ((ret = pipe(thePipe)) == -1) {
38       return OO_FAILED;
39    }
40    pthread_mutex_init(&gCmdChanLock, NULL);
41
42    gH323ep.cmdSock = dup(thePipe[0]);
43    close(thePipe[0]);
44    gCmdChan = dup(thePipe[1]);
45    close(thePipe[1]);
46    return OO_OK;
47 }
48
49
50 int ooCloseCmdConnection()
51 {
52    close(gH323ep.cmdSock);
53    gH323ep.cmdSock = 0;
54    close(gCmdChan);
55    gCmdChan = 0;
56    pthread_mutex_destroy(&gCmdChanLock);
57
58    return OO_OK;
59 }
60
61 int ooWriteStackCommand(OOStackCommand *cmd)
62 {
63
64         pthread_mutex_lock(&gCmdChanLock);
65         if (write(gCmdChan, (char*)cmd, sizeof(OOStackCommand)) == -1) {
66                 pthread_mutex_unlock(&gCmdChanLock);
67                 return OO_FAILED;
68         }
69         pthread_mutex_unlock(&gCmdChanLock);
70    
71         return OO_OK;
72 }
73
74
75 int ooReadAndProcessStackCommand()
76 {
77    OOH323CallData *pCall = NULL;   
78    unsigned char buffer[MAXMSGLEN];
79    int i, recvLen = 0;
80    OOStackCommand cmd;
81    memset(&cmd, 0, sizeof(OOStackCommand));
82    recvLen = read(gH323ep.cmdSock, buffer, MAXMSGLEN);
83    if(recvLen <= 0)
84    {
85       OOTRACEERR1("Error:Failed to read CMD message\n");
86       return OO_FAILED;
87    }
88
89    for(i=0; (int)(i+sizeof(OOStackCommand)) <= recvLen; i += sizeof(OOStackCommand))
90    {
91       memcpy(&cmd, buffer+i, sizeof(OOStackCommand));
92
93       if(cmd.type == OO_CMD_NOOP)
94          continue;
95
96       if(gH323ep.gkClient && gH323ep.gkClient->state != GkClientRegistered)
97       {
98          OOTRACEINFO1("Ignoring stack command as Gk Client is not registered"
99                       " yet\n");
100       }
101       else {
102          switch(cmd.type) {
103             case OO_CMD_MAKECALL: 
104                OOTRACEINFO2("Processing MakeCall command %s\n", 
105                                     (char*)cmd.param2);
106  
107                ooH323MakeCall ((char*)cmd.param1, (char*)cmd.param2, 
108                                (ooCallOptions*)cmd.param3);
109                break;
110
111             case OO_CMD_MANUALRINGBACK:
112                if(OO_TESTFLAG(gH323ep.flags, OO_M_MANUALRINGBACK))
113                {
114                   pCall = ooFindCallByToken((char*)cmd.param1);
115                   if(!pCall) {
116                      OOTRACEINFO2("Call \"%s\" does not exist\n",
117                                   (char*)cmd.param1);
118                      OOTRACEINFO1("Call migth be cleared/closed\n");
119                   }
120                   else {
121                      ooSendAlerting(ooFindCallByToken((char*)cmd.param1));
122                      if(OO_TESTFLAG(gH323ep.flags, OO_M_AUTOANSWER)) {
123                         ooSendConnect(ooFindCallByToken((char*)cmd.param1));
124                      }
125                   }
126                }
127                break;
128  
129             case OO_CMD_ANSCALL:
130                pCall = ooFindCallByToken((char*)cmd.param1);
131                if(!pCall) {
132                   OOTRACEINFO2("Call \"%s\" does not exist\n",
133                                (char*)cmd.param1);
134                   OOTRACEINFO1("Call might be cleared/closed\n");
135                }
136                else {
137                   OOTRACEINFO2("Processing Answer Call command for %s\n",
138                                (char*)cmd.param1);
139                   ooSendConnect(pCall);
140                }
141                break;
142
143             case OO_CMD_FWDCALL:
144                OOTRACEINFO3("Forwarding call %s to %s\n", (char*)cmd.param1,
145                                                           (char*)cmd.param2);
146                ooH323ForwardCall((char*)cmd.param1, (char*)cmd.param2);
147                break;
148
149             case OO_CMD_HANGCALL: 
150                OOTRACEINFO2("Processing Hang call command %s\n", 
151                              (char*)cmd.param1);
152                ooH323HangCall((char*)cmd.param1, 
153                               *(OOCallClearReason*)cmd.param2);
154                break;
155           
156             case OO_CMD_SENDDIGIT:
157                pCall = ooFindCallByToken((char*)cmd.param1);
158                if(!pCall) {
159                   OOTRACEERR2("ERROR:Invalid calltoken %s\n",
160                               (char*)cmd.param1);
161                   break;
162                }
163                if(pCall->jointDtmfMode & OO_CAP_DTMF_H245_alphanumeric) {
164                   ooSendH245UserInputIndication_alphanumeric(
165                      pCall, (const char*)cmd.param2);
166                }
167                else if(pCall->jointDtmfMode & OO_CAP_DTMF_H245_signal) {
168                   ooSendH245UserInputIndication_signal(
169                      pCall, (const char*)cmd.param2);
170                }
171                else {
172                   ooQ931SendDTMFAsKeyPadIE(pCall, (const char*)cmd.param2);
173                }
174
175                break;
176
177             case OO_CMD_STOPMONITOR: 
178                OOTRACEINFO1("Processing StopMonitor command\n");
179                ooStopMonitorCalls();
180                break;
181
182             default: OOTRACEERR1("ERROR:Unknown command\n");
183          }
184       }
185       if(cmd.param1) free(cmd.param1);
186       if(cmd.param2) free(cmd.param2);
187       if(cmd.param3) free(cmd.param3);
188    }
189
190
191    return OO_OK;
192 }
193