aboutsummaryrefslogtreecommitdiffstats
path: root/addons/ooh323c/src/ooCmdChannel.c
blob: ff3988273788aec6b95c13f68d1fb30194d28563 (plain)
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
/*
 * Copyright (C) 2004-2005 by Objective Systems, Inc.
 *
 * This software is furnished under an open source license and may be 
 * used and copied only in accordance with the terms of this license. 
 * The text of the license may generally be found in the root 
 * directory of this installation in the COPYING file.  It 
 * can also be viewed online at the following URL:
 *
 *   http://www.obj-sys.com/open/license.html
 *
 * Any redistributions of this file including modified versions must 
 * maintain this copyright notice.
 *
 *****************************************************************************/

#include "asterisk.h"
#include "asterisk/lock.h"
#include "ooStackCmds.h"
#include "ootrace.h"
#include "ooq931.h"
#include "ooh245.h"
#include "ooh323ep.h"
#include "oochannels.h"
#include "ooCalls.h"
#include "ooCmdChannel.h"


/** Global endpoint structure */
extern OOH323EndPoint gH323ep;

OOSOCKET gCmdChan = 0;
ast_mutex_t gCmdChanLock;

int ooCreateCmdConnection()
{
   int ret = 0;
   int thePipe[2];

   if ((ret = pipe(thePipe)) == -1) {
      return OO_FAILED;
   }
   ast_mutex_init(&gCmdChanLock);

   gH323ep.cmdSock = dup(thePipe[0]);
   close(thePipe[0]);
   gCmdChan = dup(thePipe[1]);
   close(thePipe[1]);
   return OO_OK;
}

int ooCreateCallCmdConnection(OOH323CallData* call)
{
   int ret = 0;
   int thePipe[2];

    OOTRACEINFO2("INFO: create cmd connect for call: %lx\n", call);

   call->CmdChanLock = ast_calloc(1, sizeof(ast_mutex_t));
   ast_mutex_init(call->CmdChanLock);


   if ((ret = socketpair(PF_LOCAL, SOCK_STREAM, 0, thePipe)) == -1) {
      ast_mutex_destroy(call->CmdChanLock);
      free(call->CmdChanLock);
      call->CmdChanLock = NULL;
      return OO_FAILED;
   }
   ast_mutex_lock(call->CmdChanLock);
   call->cmdSock = thePipe[0];
   call->CmdChan = thePipe[1];
   ast_mutex_unlock(call->CmdChanLock);
   return OO_OK;
}


int ooCloseCmdConnection()
{
   close(gH323ep.cmdSock);
   gH323ep.cmdSock = 0;
   close(gCmdChan);
   gCmdChan = 0;
   ast_mutex_destroy(&gCmdChanLock);

   return OO_OK;
}
int ooCloseCallCmdConnection(OOH323CallData* call)
{
   ast_mutex_lock(call->CmdChanLock);
   close(call->cmdSock);
   call->cmdSock = 0;
   close(call->CmdChan);
   call->CmdChan = 0;
   ast_mutex_unlock(call->CmdChanLock);
   ast_mutex_destroy(call->CmdChanLock);
   free(call->CmdChanLock);
   call->CmdChanLock = NULL;

   return OO_OK;
}

int ooWriteStackCommand(OOStackCommand *cmd)
{

	ast_mutex_lock(&gCmdChanLock);
   	if (write(gCmdChan, (char*)cmd, sizeof(OOStackCommand)) == -1) {
		ast_mutex_unlock(&gCmdChanLock);
		return OO_FAILED;
	}
	ast_mutex_unlock(&gCmdChanLock);
   
   	return OO_OK;
}
int ooWriteCallStackCommand(OOH323CallData* call, OOStackCommand *cmd)
{
	unsigned char buffer[MAXMSGLEN];
	unsigned char* bPoint;

	memcpy(buffer, cmd,  sizeof(OOStackCommand));
	bPoint = buffer +  sizeof(OOStackCommand);
	if (cmd->param1 && cmd->plen1 > 0) {
		if (bPoint + cmd->plen1 >= buffer + MAXMSGLEN)
			return OO_FAILED;
		memcpy(bPoint, cmd->param1, cmd->plen1);
		bPoint += cmd->plen1;
	}
	if (cmd->param2 && cmd->plen2 > 0) {
		if (bPoint + cmd->plen2 >= buffer + MAXMSGLEN)
			return OO_FAILED;
		memcpy(bPoint, cmd->param2, cmd->plen2);
		bPoint += cmd->plen2;
	}
	if (cmd->param3 && cmd->plen3 > 0) {
		if (bPoint + cmd->plen3 >= buffer + MAXMSGLEN)
			return OO_FAILED;
		memcpy(bPoint, cmd->param3, cmd->plen3);
		bPoint += cmd->plen3;
	}

	ast_mutex_lock(call->CmdChanLock);
   	if (write(call->CmdChan, buffer, bPoint - buffer) == -1) {
		ast_mutex_unlock(call->CmdChanLock);
		return OO_FAILED;
	}
	ast_mutex_unlock(call->CmdChanLock);
   
   	return OO_OK;
}


int ooReadAndProcessStackCommand()
{
   OOH323CallData *pCall = NULL;   
   unsigned char buffer[MAXMSGLEN];
   int i, recvLen = 0;
   OOStackCommand cmd;
   memset(&cmd, 0, sizeof(OOStackCommand));
   ast_mutex_lock(&gCmdChanLock);
   recvLen = read(gH323ep.cmdSock, buffer, MAXMSGLEN);
   ast_mutex_unlock(&gCmdChanLock);
   if(recvLen <= 0)
   {
      OOTRACEERR1("Error:Failed to read CMD message\n");
      return OO_FAILED;
   }

   for(i=0; (int)(i+sizeof(OOStackCommand)) <= recvLen; i += sizeof(OOStackCommand))
   {
      memcpy(&cmd, buffer+i, sizeof(OOStackCommand));

      if(cmd.type == OO_CMD_NOOP)
         continue;

      if(gH323ep.gkClient && gH323ep.gkClient->state != GkClientRegistered)
      {
         OOTRACEINFO1("Ignoring stack command as Gk Client is not registered"
                      " yet\n");
      }
      else {
         switch(cmd.type) {
            case OO_CMD_MAKECALL: 
               OOTRACEINFO2("Processing MakeCall command %s\n", 
                                    (char*)cmd.param2);
 
               ooH323NewCall ((char*)cmd.param2);
               break;

            case OO_CMD_MANUALPROGRESS:
                pCall = ooFindCallByToken((char*)cmd.param1);
                if(!pCall) {
                   OOTRACEINFO2("Call \"%s\" does not exist\n",
                                (char*)cmd.param1);
                   OOTRACEINFO1("Call migth be cleared/closed\n");
                }
                else {
                     ooSendProgress(ooFindCallByToken((char*)cmd.param1));
                }
               break;

            case OO_CMD_MANUALRINGBACK:
               if(OO_TESTFLAG(gH323ep.flags, OO_M_MANUALRINGBACK))
               {
                  pCall = ooFindCallByToken((char*)cmd.param1);
                  if(!pCall) {
                     OOTRACEINFO2("Call \"%s\" does not exist\n",
                                  (char*)cmd.param1);
                     OOTRACEINFO1("Call migth be cleared/closed\n");
                  }
                  else {
                     ooSendAlerting(ooFindCallByToken((char*)cmd.param1));
                     if(OO_TESTFLAG(gH323ep.flags, OO_M_AUTOANSWER)) {
                        ooSendConnect(ooFindCallByToken((char*)cmd.param1));
                     }
                  }
               }
               break;
 
            case OO_CMD_ANSCALL:
               pCall = ooFindCallByToken((char*)cmd.param1);
               if(!pCall) {
                  OOTRACEINFO2("Call \"%s\" does not exist\n",
                               (char*)cmd.param1);
                  OOTRACEINFO1("Call might be cleared/closed\n");
               }
               else {
                  OOTRACEINFO2("Processing Answer Call command for %s\n",
                               (char*)cmd.param1);
                  ooSendConnect(pCall);
               }
               break;

            case OO_CMD_FWDCALL:
               OOTRACEINFO3("Forwarding call %s to %s\n", (char*)cmd.param1,
                                                          (char*)cmd.param2);
               ooH323ForwardCall((char*)cmd.param1, (char*)cmd.param2);
               break;

            case OO_CMD_HANGCALL: 
               OOTRACEINFO3("Processing Hang call command %s with q931 cause %d\n", 
                             (char*)cmd.param1, *(int *) cmd.param3);
               ooH323HangCall((char*)cmd.param1, 
                              *(OOCallClearReason*)cmd.param2, *(int *) cmd.param3);
               break;
          
            case OO_CMD_SENDDIGIT:
               pCall = ooFindCallByToken((char*)cmd.param1);
               if(!pCall) {
                  OOTRACEERR2("ERROR:Invalid calltoken %s\n",
                              (char*)cmd.param1);
                  break;
               }
               if(pCall->jointDtmfMode & OO_CAP_DTMF_H245_alphanumeric) {
                  ooSendH245UserInputIndication_alphanumeric(
                     pCall, (const char*)cmd.param2);
               }
               else if(pCall->jointDtmfMode & OO_CAP_DTMF_H245_signal) {
                  ooSendH245UserInputIndication_signal(
                     pCall, (const char*)cmd.param2);
               }
               else {
                  ooQ931SendDTMFAsKeyPadIE(pCall, (const char*)cmd.param2);
               }

               break;

            case OO_CMD_STOPMONITOR: 
               OOTRACEINFO1("Processing StopMonitor command\n");
               ooStopMonitorCalls();
               break;

            default: OOTRACEERR1("ERROR:Unknown command\n");
         }
      }
      if(cmd.param1) free(cmd.param1);
      if(cmd.param2) free(cmd.param2);
      if(cmd.param3) free(cmd.param3);
   }


   return OO_OK;
}
int ooReadAndProcessCallStackCommand(OOH323CallData* call)
{
   unsigned char buffer[MAXMSGLEN];
   unsigned char *bPoint;
   int recvLen = 0;
   OOStackCommand cmd;
   memset(&cmd, 0, sizeof(OOStackCommand));
   if (call->CmdChanLock) {
    ast_mutex_lock(call->CmdChanLock);
    recvLen = read(call->cmdSock, buffer, MAXMSGLEN);
    ast_mutex_unlock(call->CmdChanLock);
   } else {
    recvLen = read(call->cmdSock, buffer, MAXMSGLEN);
   }
   if(recvLen <= 0)
   {
      OOTRACEERR1("Error:Failed to read CMD message\n");
      return OO_FAILED;
   }

   bPoint = buffer;
   while (bPoint  < buffer + recvLen - sizeof(OOStackCommand)) {

      memcpy(&cmd, bPoint, sizeof(OOStackCommand));
      bPoint +=  sizeof(OOStackCommand);

      if (cmd.plen1 > 0) {
	cmd.param1 = malloc(cmd.plen1 + 1);
	if (!cmd.param1) 
		return OO_FAILED;
	memset(cmd.param1, 0, cmd.plen1 + 1);
	memcpy(cmd.param1, bPoint, cmd.plen1);
	bPoint += cmd.plen1;
      }

      if (cmd.plen2 > 0) {
	cmd.param2 = malloc(cmd.plen2 + 1);
	if (!cmd.param2) 
		return OO_FAILED;
	memset(cmd.param2, 0, cmd.plen2 + 1);
	memcpy(cmd.param2, bPoint, cmd.plen2);
	bPoint += cmd.plen2;
      }

      if (cmd.plen3 > 0) {
	cmd.param3 = malloc(cmd.plen3 + 1);
	if (!cmd.param3) 
		return OO_FAILED;
	memset(cmd.param3, 0, cmd.plen3 + 1);
	memcpy(cmd.param3, bPoint, cmd.plen3);
	bPoint += cmd.plen3;
      }

      if(cmd.type == OO_CMD_NOOP)
         continue;

      if(gH323ep.gkClient && gH323ep.gkClient->state != GkClientRegistered)
      {
         OOTRACEINFO1("Ignoring stack command as Gk Client is not registered"
                      " yet\n");
      }
      else {
         switch(cmd.type) {
            case OO_CMD_MAKECALL: 
               OOTRACEINFO2("Processing MakeCall command %s\n", 
                                    (char*)cmd.param2);
 
               ooH323MakeCall ((char*)cmd.param1, (char*)cmd.param2, 
                               (ooCallOptions*)cmd.param3);
               break;

            case OO_CMD_MANUALPROGRESS:
               ooSendProgress(call);
               break;

            case OO_CMD_MANUALRINGBACK:
               if(OO_TESTFLAG(gH323ep.flags, OO_M_MANUALRINGBACK))
               {
                 ooSendAlerting(call);
                 if(OO_TESTFLAG(gH323ep.flags, OO_M_AUTOANSWER)) {
                   ooSendConnect(call);
                 }
               }
               break;
 
            case OO_CMD_ANSCALL:
                  ooSendConnect(call);
               break;

            case OO_CMD_FWDCALL:
               OOTRACEINFO3("Forwarding call %s to %s\n", (char*)cmd.param1,
                                                          (char*)cmd.param2);
               ooH323ForwardCall((char*)cmd.param1, (char*)cmd.param2);
               break;

            case OO_CMD_HANGCALL: 
               OOTRACEINFO2("Processing Hang call command %s with q931 cause %d\n", 
                             (char*)cmd.param1);
               ooH323HangCall((char*)cmd.param1, 
                              *(OOCallClearReason*)cmd.param2, *(int *) cmd.param3);
               break;
          
            case OO_CMD_SENDDIGIT:
               if(call->jointDtmfMode & OO_CAP_DTMF_H245_alphanumeric) {
                  ooSendH245UserInputIndication_alphanumeric(
                     call, (const char*)cmd.param2);
               }
               else if(call->jointDtmfMode & OO_CAP_DTMF_H245_signal) {
                  ooSendH245UserInputIndication_signal(
                     call, (const char*)cmd.param2);
               }
               else {
                  ooQ931SendDTMFAsKeyPadIE(call, (const char*)cmd.param2);
               }

               break;

	    case OO_CMD_REQMODE:
	       OOTRACEINFO3("Processing RequestMode command %s, requested mode is %d\n",
				(char *)cmd.param1, *(int *)cmd.param2);
	       ooSendRequestMode(call, *(int *)cmd.param2);
	       break;

	    case OO_CMD_SETANI:
		OOTRACEINFO3("Processing SetANI command %s, ani is %s\n",
				(char *)cmd.param1, (char *)cmd.param2);
   		if(cmd.param2) {
     			strncpy(call->ourCallerId, cmd.param2, sizeof(call->ourCallerId)-1);
     			call->ourCallerId[sizeof(call->ourCallerId)-1] = '\0';
   		}
		break;

            default: OOTRACEERR1("ERROR:Unknown command\n");
         }
      }
      if(cmd.param1) free(cmd.param1);
      if(cmd.param2) free(cmd.param2);
      if(cmd.param3) free(cmd.param3);
   }


   return OO_OK;
}