implement transfer and call forwarding. Bug #221
[asterisk/asterisk.git] / channels / h323 / ast_h323.cpp
1 /*
2  * ast_h323.cpp
3  *
4  * OpenH323 Channel Driver for ASTERISK PBX.
5  *                      By  Jeremy McNamara
6  *                      For The NuFone Network
7  * 
8  * This code has been derived from code created by
9  *              Michael Manousos and Mark Spencer
10  *
11  * This file is part of the chan_h323 driver for Asterisk
12  *
13  * chan_h323 is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version. 
17  *
18  * chan_h323 is distributed WITHOUT ANY WARRANTY; without even 
19  * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
20  * PURPOSE. See the GNU General Public License for more details. 
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
25  *
26  * Version Info: $Id$
27  */
28 #include "ast_h323.h"
29 #include "h323t38.h"
30
31
32 /* PWlib Required Components  */
33 #define MAJOR_VERSION 0
34 #define MINOR_VERSION 1
35 #define BUILD_TYPE    ReleaseCode
36 #define BUILD_NUMBER  0
37
38 /** Counter for the number of connections */
39 int channelsOpen;
40
41 /* DTMF Mode */
42 int mode = H323_DTMF_RFC2833;
43
44 /** Options for connections creation */
45 BOOL    noFastStart;
46 BOOL    noH245Tunnelling;
47 BOOL    noSilenceSuppression;
48
49 /**
50  * We assume that only one endPoint should exist.
51  * The application cannot run the h323_end_point_create() more than once
52  * FIXME: Singleton this, for safety
53  */
54 MyH323EndPoint *endPoint = NULL;
55
56 /** PWLib entry point */
57 MyProcess *localProcess = NULL;
58
59 /** H.323 listener */
60 H323ListenerTCP *tcpListener;
61
62 MyProcess::MyProcess(): PProcess("The NuFone Network's", "H.323 Channel Driver for Asterisk",
63              MAJOR_VERSION, MINOR_VERSION, BUILD_TYPE, BUILD_NUMBER)
64 {
65         Resume();
66 }
67
68 MyProcess::~MyProcess()
69 {
70         cout << " == PWLib proces going down." << endl;
71         delete endPoint;
72         endPoint = NULL;
73 }
74
75 void MyProcess::Main()
76 {
77         cout << "  == Creating H.323 Endpoint" << endl;
78         endPoint = new MyH323EndPoint();
79         PTrace::Initialise(0, NULL, PTrace::Timestamp | PTrace::Thread | PTrace::FileAndLine);
80 }
81
82 ClearCallThread::ClearCallThread(const char *tc) : PThread(10000, PThread::NoAutoDeleteThread)
83
84         token = tc;
85         Resume(); 
86 }
87
88 ClearCallThread::~ClearCallThread()
89 {
90         if (h323debug)
91                 cout <<  " == ClearCall thread going down." << endl;
92         return;
93 }
94     
95 void ClearCallThread::Main()
96 {
97         endPoint->ClearCall(token);
98         return;
99 }
100
101
102 #define H323_NAME OPAL_G7231_6k3"{sw}"
103
104 H323_REGISTER_CAPABILITY(H323_G7231Capability, H323_NAME);
105
106 H323_G7231Capability::H323_G7231Capability(BOOL annexA_)
107   : H323AudioCapability(7, 4)
108 {
109   annexA = annexA_;
110 }
111
112 PObject::Comparison H323_G7231Capability::Compare(const PObject & obj) const
113 {
114   Comparison result = H323AudioCapability::Compare(obj);
115   if (result != EqualTo)
116     return result;
117
118   PINDEX otherAnnexA = ((const H323_G7231Capability &)obj).annexA;
119   if (annexA < otherAnnexA)
120     return LessThan;
121   if (annexA > otherAnnexA)
122     return GreaterThan;
123   return EqualTo;
124 }
125
126 PObject * H323_G7231Capability::Clone() const
127 {
128   return new H323_G7231Capability(*this);
129 }
130
131
132 PString H323_G7231Capability::GetFormatName() const
133 {
134   return H323_NAME;
135 }
136
137
138 unsigned H323_G7231Capability::GetSubType() const
139 {
140   return H245_AudioCapability::e_g7231;
141 }
142
143
144 BOOL H323_G7231Capability::OnSendingPDU(H245_AudioCapability & cap,
145                                           unsigned packetSize) const
146 {
147   cap.SetTag(H245_AudioCapability::e_g7231);
148
149   H245_AudioCapability_g7231 & g7231 = cap;
150   g7231.m_maxAl_sduAudioFrames = packetSize;
151   g7231.m_silenceSuppression = annexA;
152
153   return TRUE;
154 }
155
156
157 BOOL H323_G7231Capability::OnReceivedPDU(const H245_AudioCapability & cap,
158                                            unsigned & packetSize)
159 {
160   if (cap.GetTag() != H245_AudioCapability::e_g7231)
161     return FALSE;
162
163   const H245_AudioCapability_g7231 & g7231 = cap;
164   packetSize = g7231.m_maxAl_sduAudioFrames;
165   annexA = g7231.m_silenceSuppression;
166
167   return TRUE;
168 }
169
170
171 H323Codec * H323_G7231Capability::CreateCodec(H323Codec::Direction direction) const
172 {
173   return NULL;
174 }
175
176 /** MyH323EndPoint 
177   * The fullAddress parameter is used directly in the MakeCall method so
178   * the General form for the fullAddress argument is :
179   * [alias@][transport$]host[:port]
180   * default values:     alias = the same value as host.
181   *                                     transport = ip.
182   *                                     port = 1720.
183   */
184 int MyH323EndPoint::MakeCall(const PString & dest, PString & token, 
185                                                                                 unsigned int *callReference, unsigned int port, char *callerid)
186 {
187         PString fullAddress;
188         MyH323Connection * connection;
189
190         /* Determine whether we are using a gatekeeper or not. */
191         if (GetGatekeeper() != NULL) {
192                 fullAddress = dest;
193                 if (h323debug)
194                         cout << " -- Making call to " << fullAddress << " using gatekeeper." << endl;
195         } else {
196                         fullAddress = dest; /* host */
197                         if (h323debug)
198                                 cout << " -- Making call to " << fullAddress << "." << endl;
199         }
200
201         if (!(connection = (MyH323Connection *)H323EndPoint::MakeCallLocked(fullAddress, token))) {
202                 if (h323debug)
203                         cout << "Error making call to \"" << fullAddress << '"' << endl;
204                 return 1;
205         }
206         
207         *callReference = connection->GetCallReference();
208         
209         if (callerid)
210                 connection->SetLocalPartyName(PString(callerid));
211
212         connection->Unlock();   
213
214         if (h323debug) {
215                 cout << "       -- " << GetLocalUserName() << " is calling host " << fullAddress << endl;
216                 cout << "       -- " << "Call token is " << (const char *)token << endl;
217                 cout << "       -- Call reference is " << *callReference << endl;
218         }
219         return 0;
220 }
221
222 void MyH323EndPoint::SetEndpointTypeInfo( H225_EndpointType & info ) const
223 {
224   H323EndPoint::SetEndpointTypeInfo(info);
225   info.m_gateway.IncludeOptionalField(H225_GatewayInfo::e_protocol);
226   info.m_gateway.m_protocol.SetSize(1);
227   H225_SupportedProtocols &protocol=info.m_gateway.m_protocol[0];
228   protocol.SetTag(H225_SupportedProtocols::e_voice);
229   PINDEX as=SupportedPrefixes.GetSize();
230   ((H225_VoiceCaps &)protocol).m_supportedPrefixes.SetSize(as);
231   for (PINDEX p=0; p<as; p++) {
232     H323SetAliasAddress(SupportedPrefixes[p], ((H225_VoiceCaps &)protocol).m_supportedPrefixes[p].m_prefix);
233   }
234 }
235
236 void MyH323EndPoint::SetGateway(void)
237 {
238         terminalType = e_GatewayOnly;
239 }
240
241 H323Capabilities MyH323EndPoint::GetCapabilities(void)
242 {
243         return capabilities;
244 }
245
246 BOOL MyH323EndPoint::ClearCall(const PString & token)
247 {
248         if (h323debug)
249                 cout << "       -- ClearCall: Request to clear call with token " << token << endl;
250         return H323EndPoint::ClearCall(token);
251 }
252
253 void MyH323EndPoint::SendUserTone(const PString &token, char tone)
254 {
255         H323Connection *connection = NULL;
256                 
257         connection = FindConnectionWithLock(token);
258         if (connection != NULL) {
259                 connection->SendUserInputTone(tone, 500);
260                 connection->Unlock();
261         }
262 }
263
264 void MyH323EndPoint::OnClosedLogicalChannel(H323Connection & connection, const H323Channel & channel)
265 {
266         channelsOpen--;
267         if (h323debug)
268                 cout << "               channelsOpen = " << channelsOpen << endl;
269         H323EndPoint::OnClosedLogicalChannel(connection, channel);
270 }
271
272 BOOL MyH323EndPoint::OnConnectionForwarded(H323Connection & connection,
273                 const PString & forwardParty,
274                 const H323SignalPDU & pdu)
275  {
276         if (h323debug)
277         cout << "       -- Call Forwarded to " << forwardParty << endl;
278         return FALSE;
279  }
280  
281 BOOL MyH323EndPoint::ForwardConnection(H323Connection & connection,
282                 const PString & forwardParty,
283                 const H323SignalPDU & pdu)
284 {
285         if (h323debug)
286                 cout << "       -- Forwarding call to " << forwardParty << endl;
287         return H323EndPoint::ForwardConnection(connection, forwardParty, pdu);
288 }
289
290 void MyH323EndPoint::OnConnectionEstablished(H323Connection & connection, const PString & estCallToken)
291 {
292         if (h323debug)
293                 cout << "       -- Connection Established with \"" << connection.GetRemotePartyName() << "\"" << endl;
294         on_connection_established(connection.GetCallReference());
295 }
296
297 /** OnConnectionCleared callback function is called upon the dropping of an established
298   * H323 connection. 
299   */
300 void MyH323EndPoint::OnConnectionCleared(H323Connection & connection, const PString & clearedCallToken)
301 {
302         PString remoteName = connection.GetRemotePartyName();
303         
304         call_details_t cd;
305
306         cd.call_reference = connection.GetCallReference();
307         cd.call_token = (const char *)connection.GetCallToken();
308         cd.call_source_aliases = (const char *)connection.GetRemotePartyName();
309         
310         /* Convert complex strings */
311         char *s;
312     if ((s = strchr(cd.call_source_aliases, ' ')) != NULL)
313                 *s = '\0';
314
315         /* Invoke the PBX application registered callback */
316         on_connection_cleared(cd);
317
318         switch (connection.GetCallEndReason()) {
319                 case H323Connection::EndedByCallForwarded :
320                         if (h323debug)
321                                 cout << " -- " << remoteName << " has forwarded the call" << endl;
322                         break;
323                 case H323Connection::EndedByRemoteUser :
324                         if (h323debug)
325                                 cout << " -- " << remoteName << " has cleared the call" << endl;
326                         break;
327                 case H323Connection::EndedByCallerAbort :
328                         if (h323debug)
329                                 cout << " -- " << remoteName << " has stopped calling" << endl;
330                         break;
331                 case H323Connection::EndedByRefusal :
332                         if (h323debug)
333                                 cout << " -- " << remoteName << " did not accept your call" << endl;
334                         break;
335                 case H323Connection::EndedByRemoteBusy :
336                         if (h323debug)
337                         cout << " -- " << remoteName << " was busy" << endl;
338                         break;
339                 case H323Connection::EndedByRemoteCongestion :
340                         if (h323debug)
341                                 cout << " -- Congested link to " << remoteName << endl;
342                         break;
343                 case H323Connection::EndedByNoAnswer :
344                         if (h323debug)
345                                 cout << " -- " << remoteName << " did not answer your call" << endl;
346                         break;
347                 case H323Connection::EndedByTransportFail :
348                         if (h323debug)
349                                 cout << " -- Call with " << remoteName << " ended abnormally" << endl;
350                         break;
351                 case H323Connection::EndedByCapabilityExchange :
352                         if (h323debug)
353                                 cout << " -- Could not find common codec with " << remoteName << endl;
354                         break;
355                 case H323Connection::EndedByNoAccept :
356                         if (h323debug)
357                                 cout << " -- Did not accept incoming call from " << remoteName << endl;
358                         break;
359                 case H323Connection::EndedByAnswerDenied :
360                         if (h323debug)
361                                 cout << " -- Refused incoming call from " << remoteName << endl;
362                         break;
363                 case H323Connection::EndedByNoUser :
364                         if (h323debug)
365                                 cout << " -- Remote endpoint could not find user: " << remoteName << endl;
366                         break;
367                 case H323Connection::EndedByNoBandwidth :
368                         if (h323debug)
369                                 cout << " -- Call to " << remoteName << " aborted, insufficient bandwidth." << endl;
370                         break;
371                 case H323Connection::EndedByUnreachable :
372                         if (h323debug)
373                                 cout << " -- " << remoteName << " could not be reached." << endl;
374                         break;
375                 case H323Connection::EndedByHostOffline :
376                         if (h323debug)
377                                 cout << " -- " << remoteName << " is not online." << endl;
378                         break;
379                 case H323Connection::EndedByNoEndPoint :
380                         if (h323debug)
381                                 cout << " -- No phone running for " << remoteName << endl;
382                         break;
383                 case H323Connection::EndedByConnectFail :
384                         if (h323debug)
385                                 cout << " -- Transport error calling " << remoteName << endl;
386                         break;
387                 default :
388                         if (h323debug)
389                                 cout << " -- Call with " << remoteName << " completed" << endl;
390         }
391         if(connection.IsEstablished()) 
392                 if (h323debug)
393                         cout << "        -- Call duration " << setprecision(0) << setw(5) << (PTime() - connection.GetConnectionStartTime()) << endl;
394 }
395
396
397 H323Connection * MyH323EndPoint::CreateConnection(unsigned callReference, void *outbound)
398 {
399         unsigned options = 0;
400
401         if (noFastStart)
402                 options |= H323Connection::FastStartOptionDisable;
403
404         if (noH245Tunnelling)
405                 options |= H323Connection::H245TunnelingOptionDisable;
406
407         return new MyH323Connection(*this, callReference, options);
408 }
409
410 H323Connection * MyH323EndPoint::SetupTransfer(const PString & token,
411                                                                                            const PString & callIdentity,
412                                                                                            const PString & remoteParty,
413                                                                                            PString & newToken,
414                                                                                            void * userData)
415 {
416         PString alias;
417         H323TransportAddress address;
418         
419         H323Connection * connection; 
420  
421         if (h323debug) {
422                 cout << " -- Setup transfer of  " << callIdentity << ":" << endl;
423                 cout << "       -- Call from    " << token << endl;
424                 cout << "       -- Remote Party " << remoteParty << endl;
425         }
426  
427         connection = FindConnectionWithLock(token);
428  
429         if (connection != NULL) {
430                 unsigned int old_call_reference = connection->GetCallReference();
431  
432                 if (h323debug)
433                         cout << " -- Old call reference " << old_call_reference << endl;
434                 connection->Unlock();
435  
436                 if (on_transfer_call(old_call_reference, remoteParty)) {
437                         if (h323debug)
438                                 cout << "       -- Transfer succeded " << endl;
439                         if (connection->ClearCall(H323Connection::EndedByCallForwarded))
440                                         return NULL;
441                         return NULL;
442                 }
443         }
444         if (h323debug)
445                 cout << "       -- Transfer failed " << endl;
446  
447         if (connection != NULL) {
448                 return connection;
449         }
450         return NULL;
451 }
452
453 /* MyH323Connection */    
454 MyH323Connection::MyH323Connection(MyH323EndPoint & ep,
455                                                                                    unsigned callReference,
456                                                                                unsigned options)
457                                                                                    : H323Connection(ep, 
458                                                                                                                         callReference, 
459                                                                                                                         options)
460 {
461     remoteIpAddress = 0;        // IP Address of remote endpoint
462         remotePort              = 0;    // remote endpoint Data port (control is dataPort+1)
463
464         if (h323debug)
465                 cout << "       == New H.323 Connection created." << endl;
466         return;
467 }
468
469 MyH323Connection::~MyH323Connection()
470 {
471         if (h323debug)
472                 cout << "       == H.323 Connection deleted." << endl;
473         return;
474 }
475
476 H323Connection::AnswerCallResponse      MyH323Connection::OnAnswerCall(const PString & caller,
477                                                                                                                                    const H323SignalPDU & /*setupPDU*/,
478                                                                                                                                    H323SignalPDU & /*connectPDU*/)
479 {
480         /* The call will be answered later with "AnsweringCall()" function.
481          */ 
482         return H323Connection::AnswerCallAlertWithMedia;
483 }
484
485 BOOL  MyH323Connection::OnAlerting(const H323SignalPDU & /*alertingPDU*/, const PString & username)
486 {
487         
488         if (h323debug)
489                 cout << "       -- Ringing phone for \"" << username << "\"" << endl;
490         return TRUE;
491 }
492
493 BOOL MyH323Connection::OnReceivedSignalSetup(const H323SignalPDU & setupPDU)
494 {
495         
496         if (h323debug)
497                 cout << "       -- Received SETUP message..." << endl;
498
499         call_details_t cd;
500         
501         PString sourceE164;
502         PString destE164;
503         PString sourceAliases;  
504         PString destAliases;
505         PString sourceIp;
506                 
507         PIPSocket::Address Ip;
508         WORD                       sourcePort;
509
510         sourceAliases = setupPDU.GetSourceAliases();
511         destAliases = setupPDU.GetDestinationAlias();
512                         
513         sourceE164 = "";
514         setupPDU.GetSourceE164(sourceE164);
515         destE164 = "";
516         setupPDU.GetDestinationE164(destE164);
517
518         /* Convert complex strings */
519         //  FIXME: deal more than one source alias 
520         char *s; 
521     if ((s = strchr(sourceAliases, ' ')) != NULL)
522                 *s = '\0';
523     if ((s = strchr(sourceAliases, '\t')) != NULL)
524                 *s = '\0';
525         char *s1;
526     if ((s1 = strchr(destAliases, ' ')) != NULL)
527          *s1 = '\0';
528         if ((s1 = strchr(destAliases, '\t')) != NULL)
529          *s1 = '\0';
530
531         GetSignallingChannel()->GetRemoteAddress().GetIpAndPort(Ip, sourcePort);
532
533         sourceIp = Ip.AsString();
534
535         cd.call_reference               = GetCallReference();
536         cd.call_token                   = (const char *)GetCallToken();
537         cd.call_source_aliases  = (const char *)sourceAliases;
538         cd.call_dest_alias              = (const char *)destAliases;
539         cd.call_source_e164             = (const char *)sourceE164;
540         cd.call_dest_e164               = (const char *)destE164;
541         cd.sourceIp                             = (const char *)sourceIp;
542         
543         /* Notify Asterisk of the request */
544         int res = on_incoming_call(cd); 
545
546         if (!res) {
547                 if (h323debug)
548                         cout << "       -- Call Failed" << endl;
549                 return FALSE;
550         }
551         
552         return H323Connection::OnReceivedSignalSetup(setupPDU);
553 }
554
555 BOOL MyH323Connection::OnSendSignalSetup(H323SignalPDU & setupPDU)
556 {
557         call_details_t cd;
558         
559         if (h323debug) 
560                 cout << "       -- Sending SETUP message" << endl;
561         
562         sourceAliases = setupPDU.GetSourceAliases();
563         destAliases = setupPDU.GetDestinationAlias();
564
565         sourceE164 = "";
566         setupPDU.GetSourceE164(sourceE164);
567         destE164 = "";
568         setupPDU.GetDestinationE164(destE164);
569
570         /* Convert complex strings */
571         //  FIXME: deal more than one source alias 
572         char *s; 
573     if ((s = strchr(sourceAliases, ' ')) != NULL)
574                 *s = '\0';
575     if ((s = strchr(sourceAliases, '\t')) != NULL)
576                 *s = '\0';
577         char *s1;
578     if ((s1 = strchr(destAliases, ' ')) != NULL)
579          *s1 = '\0';
580         if ((s1 = strchr(destAliases, '\t')) != NULL)
581          *s1 = '\0';
582
583         cd.call_reference               = GetCallReference();
584         cd.call_token                   = (const char *)GetCallToken();
585         cd.call_source_aliases  = (const char *)sourceAliases;
586         cd.call_dest_alias              = (const char *)destAliases;
587         cd.call_source_e164             = (const char *)sourceE164;
588         cd.call_dest_e164               = (const char *)destE164;
589
590         int res = on_outgoing_call(cd); 
591                 
592         if (!res) {
593                 if (h323debug)
594                         cout << "       -- Call Failed" << endl;
595                 return FALSE;
596         }
597
598         return H323Connection::OnSendSignalSetup(setupPDU);
599 }
600
601 BOOL MyH323Connection::OnSendReleaseComplete(H323SignalPDU & releaseCompletePDU)
602 {
603         if (h323debug)
604                 cout << "       -- Sending RELEASE COMPLETE" << endl;
605         return H323Connection::OnSendReleaseComplete(releaseCompletePDU);
606 }
607
608 BOOL MyH323Connection::OnReceivedFacility(const H323SignalPDU & pdu)
609 {
610         if (h323debug)
611                 cout << "       -- Received Facility message... " << endl;
612         return H323Connection::OnReceivedFacility(pdu);
613 }
614
615 void MyH323Connection::OnReceivedReleaseComplete(const H323SignalPDU & pdu)
616 {
617         if (h323debug)
618                 cout <<  "      -- Received RELEASE COMPLETE message..." << endl;
619         return H323Connection::OnReceivedReleaseComplete(pdu);
620
621 }
622
623 BOOL MyH323Connection::OnClosingLogicalChannel(H323Channel & channel)
624 {
625         if (h323debug)
626                 cout << "       -- Closing logical channel..." << endl;
627         return H323Connection::OnClosingLogicalChannel(channel);
628 }
629
630
631 void MyH323Connection::SendUserInputTone(char tone, unsigned duration)
632 {
633         if (h323debug)
634                 cout << "       -- Sending user input tone (" << tone << ") to remote" << endl;
635
636         on_send_digit(GetCallReference(), tone);
637                 
638         H323Connection::SendUserInputTone(tone, duration);
639 }
640
641 void MyH323Connection::OnUserInputTone(char tone, unsigned duration, unsigned logicalChannel, unsigned rtpTimestamp)
642 {
643         if (mode == H323_DTMF_INBAND) {
644                 if (h323debug)
645                         cout << "       -- Received user input tone (" << tone << ") from remote" << endl;
646                 on_send_digit(GetCallReference(), tone);
647         }
648         H323Connection::OnUserInputTone(tone, duration, logicalChannel, rtpTimestamp);
649 }
650
651 void MyH323Connection::OnUserInputString(const PString &value)
652 {
653         if (mode == H323_DTMF_RFC2833) {
654                 if (h323debug)
655                         cout <<  "      -- Received user input string (" << value << ") from remote." << endl;
656                 on_send_digit(GetCallReference(), value[0]);
657         }       
658 }
659
660 H323Channel * MyH323Connection::CreateRealTimeLogicalChannel(const H323Capability & capability,
661                                                                                                                      H323Channel::Directions dir,
662                                                                                                                      unsigned sessionID,
663                                                                                                                      const H245_H2250LogicalChannelParameters * /*param*/)
664 {
665         struct rtp_info *info;
666         WORD port;
667
668         /* Determine the Local (A side) IP Address and port */
669         info = on_create_connection(GetCallReference()); 
670         
671 //      if (bridging) {
672 //              externalIpAddress = PIPSocket::Address(info->addr);
673 //      } else {
674         GetControlChannel().GetLocalAddress().GetIpAndPort(externalIpAddress, port);
675 //      }
676
677 //      externalIpAddress = PIPSocket::Address("192.168.1.50");
678
679         externalPort = info->port;
680         
681         if (h323debug) {
682                 cout << "       =*= In CreateRealTimeLogicalChannel for call " << GetCallReference() << endl;
683                 cout << "               -- externalIpAddress: " << externalIpAddress << endl;
684                 cout << "               -- externalPort: " << externalPort << endl;
685                 cout << "               -- SessionID: " << sessionID << endl;
686                 cout << "               -- Direction: " << dir << endl;
687         }
688         return new H323_ExternalRTPChannel(*this, capability, dir, sessionID, externalIpAddress, externalPort);
689 }
690
691 /** This callback function is invoked once upon creation of each
692   * channel for an H323 session 
693   */
694 BOOL MyH323Connection::OnStartLogicalChannel(H323Channel & channel)
695 {    
696         if (h323debug) {
697                 cout << "        -- Started logical channel: "; 
698                 cout << ((channel.GetDirection()==H323Channel::IsTransmitter)?"sending ":((channel.GetDirection()==H323Channel::IsReceiver)?"receiving ":" ")); 
699                 cout << (const char *)(channel.GetCapability()).GetFormatName() << endl;
700         }
701         // adjust the count of channels we have open
702         channelsOpen++;
703         if (h323debug)
704                 cout <<  "              -- channelsOpen = " << channelsOpen << endl;
705         
706         H323_ExternalRTPChannel & external = (H323_ExternalRTPChannel &)channel;
707     external.GetRemoteAddress(remoteIpAddress, remotePort); 
708
709         if (h323debug) {
710                 cout << "               -- remoteIpAddress: " << remoteIpAddress << endl;
711                 cout << "               -- remotePort: " << remotePort << endl;
712                 cout << "               -- ExternalIpAddress: " << externalIpAddress << endl;
713                 cout << "               -- ExternalPort: " << externalPort << endl;
714         }
715         /* Notify Asterisk of remote RTP information */
716         on_start_logical_channel(GetCallReference(), (const char *)remoteIpAddress.AsString(), remotePort);
717
718         return TRUE;    
719 }
720
721 #if 0
722 MyGatekeeperServer::MyGatekeeperServer(MyH323EndPoint & ep)
723   : H323GatekeeperServer(ep),
724     endpoint(ep)
725 {
726 }
727
728
729 BOOL MyGatekeeperServer::Initialise()
730 {
731   PINDEX i;
732
733   PWaitAndSignal mutex(reconfigurationMutex);
734   
735   SetGatekeeperIdentifier("TESTIES");
736
737     // Interfaces to listen on
738   H323TransportAddressArray interfaces;
739   interfaces.Append(new H323TransportAddress(0.0.0.0);
740   AddListeners(interfaces);
741
742   // lots more to come
743   
744   return TRUE;
745
746 }
747
748 #endif
749
750 /** IMPLEMENTATION OF C FUNCTIONS */
751
752 /**
753  * The extern "C" directive takes care for 
754  * the ANSI-C representation of linkable symbols 
755  */
756 extern "C" {
757
758 int end_point_exist(void)
759 {
760         if (!endPoint) {
761                 return 0;
762         }
763         return 1;
764 }
765     
766 void h323_end_point_create(void)
767 {
768         channelsOpen = 0;
769         localProcess = new MyProcess(); 
770         localProcess->Main();
771 }
772
773 void h323_gk_urq(void)
774 {
775         if (!end_point_exist()) {
776                 cout << " ERROR: [h323_gk_urq] No Endpoint, this is bad" << endl;
777                 return;
778         }
779         
780         endPoint->RemoveGatekeeper();
781 }
782
783 void h323_end_process(void)
784 {       
785         delete localProcess;
786 }
787
788 void h323_debug(int flag, unsigned level)
789 {
790         if (flag) 
791                 PTrace:: SetLevel(level); 
792         else 
793                 PTrace:: SetLevel(0); 
794 }
795         
796 /** Installs the callback functions on behalf of the PBX application  */
797 void h323_callback_register(setup_incoming_cb  ifunc,
798                                                         setup_outbound_cb  sfunc,
799                                                         setup_transfer_cb  tfunc,
800                                                         on_connection_cb   confunc,
801                                                         start_logchan_cb   lfunc,
802                                                         clear_con_cb       clfunc,
803                                                         con_established_cb efunc,
804                                                         send_digit_cb      dfunc)
805 {
806         on_incoming_call = ifunc;
807         on_outgoing_call = sfunc;
808         on_transfer_call = tfunc;
809         on_create_connection = confunc;
810         on_start_logical_channel = lfunc;
811         on_connection_cleared = clfunc;
812         on_connection_established = efunc;
813         on_send_digit = dfunc;
814 }
815
816 /**
817  * Add capability to the capability table of the end point. 
818  */
819 int h323_set_capability(int cap, int dtmfMode)
820 {
821         int g711Frames = 30;
822     int gsmFrames  = 4;
823
824         
825         if (!end_point_exist()) {
826                 cout << " ERROR: [h323_set_capablity] No Endpoint, this is bad" << endl;
827                 return 1;
828         }
829
830         mode = dtmfMode;
831         if (dtmfMode == H323_DTMF_INBAND)
832             endPoint->SetSendUserInputMode(H323Connection::SendUserInputAsTone);
833         else
834                 endPoint->SetSendUserInputMode(H323Connection::SendUserInputAsInlineRFC2833);
835          
836
837         /* Hardcode this for now (Someone tell me if T.38 works now 
838            or provide me with some debug so we can make this work */
839
840 //      endPoint->SetCapability(0, 0, new H323_T38Capability(H323_T38Capability::e_UDP));
841         
842         if (cap & AST_FORMAT_SPEEX) {
843                 /* Not real sure if Asterisk acutally supports all
844                    of the various different bit rates so add them 
845                    all and figure it out later*/
846
847                 endPoint->SetCapability(0, 0, new SpeexNarrow2AudioCapability());
848                 endPoint->SetCapability(0, 0, new SpeexNarrow3AudioCapability());
849                 endPoint->SetCapability(0, 0, new SpeexNarrow4AudioCapability());
850                 endPoint->SetCapability(0, 0, new SpeexNarrow5AudioCapability());
851                 endPoint->SetCapability(0, 0, new SpeexNarrow6AudioCapability());
852         }
853
854 #if WANT_G729
855         if (cap & AST_FORMAT_G729A) {
856                 H323_G729ACapability *g729aCap;
857                 endPoint->SetCapability(0, 0, g729aCap = new H323_G729ACapability);
858         }
859 #endif
860         
861         if (cap & AST_FORMAT_G723_1) {
862                 H323_G7231Capability *g7231Cap;
863                 endPoint->SetCapability(0, 0, g7231Cap = new H323_G7231Capability);
864         } 
865
866         if (cap & AST_FORMAT_GSM) {
867                 H323_GSM0610Capability *gsmCap;
868             endPoint->SetCapability(0, 0, gsmCap = new H323_GSM0610Capability);
869             gsmCap->SetTxFramesInPacket(gsmFrames);
870         } 
871
872         if (cap & AST_FORMAT_ULAW) {
873
874                 H323_G711Capability *g711uCap;
875             endPoint->SetCapability(0, 0, g711uCap = new H323_G711Capability(H323_G711Capability::muLaw));
876                 g711uCap->SetTxFramesInPacket(g711Frames);
877         } 
878
879         if (cap & AST_FORMAT_ALAW) {
880                 H323_G711Capability *g711aCap;
881                 endPoint->SetCapability(0, 0, g711aCap = new H323_G711Capability(H323_G711Capability::ALaw));
882                 g711aCap->SetTxFramesInPacket(g711Frames);
883         } 
884
885         return 0;
886 }
887
888 /** Start the H.323 listener */
889 int h323_start_listener(int listenPort, struct sockaddr_in bindaddr)
890 {
891         
892         if (!end_point_exist()) {
893                 cout << "ERROR: [h323_start_listener] No Endpoint, this is bad!" << endl;
894                 return 1;
895         }
896         
897         PIPSocket::Address interfaceAddress(bindaddr.sin_addr);
898
899         if (!listenPort)
900                 listenPort = 1720;
901         
902         tcpListener = new H323ListenerTCP(*endPoint, interfaceAddress, (WORD)listenPort);
903
904         if (!endPoint->StartListener(tcpListener)) {
905                 cout << "ERROR: Could not open H.323 listener port on " << ((H323ListenerTCP *) tcpListener)->GetListenerPort() << endl;
906                 delete tcpListener;
907                 return 1;
908                 
909         }
910                 
911 //      cout << "  == H.323 listener started on " << ((H323ListenerTCP *) tcpListener)->GetTransportAddress() << endl;
912         cout << "  == H.323 listener started" << endl;
913
914         return 0;
915 };
916  
917    
918 int h323_set_alias(struct oh323_alias *alias)
919 {
920         char *p;
921         char *num;
922         PString h323id(alias->name);
923         PString e164(alias->e164);
924         
925         if (!end_point_exist()) {
926                 cout << "ERROR: [h323_set_alias] No Endpoint, this is bad!" << endl;
927                 return 1;
928         }
929
930         cout << "  == Adding alias \"" << h323id << "\" to endpoint" << endl;
931         endPoint->AddAliasName(h323id); 
932
933         endPoint->RemoveAliasName(localProcess->GetUserName());
934
935         if (!e164.IsEmpty()) {
936                 cout << "  == Adding E.164 \"" << e164 << "\" to endpoint" << endl;
937                 endPoint->AddAliasName(e164);
938         }
939         if (strlen(alias->prefix)) {
940                 p = alias->prefix;
941                 num = strsep(&p, ",");
942                 while(num) {
943                 cout << "  == Adding Prefix \"" << num << "\" to endpoint" << endl;
944                         endPoint->SupportedPrefixes += PString(num);
945                         endPoint->SetGateway();
946                 num = strsep(&p, ",");          
947                 }
948         }
949
950         return 0;
951 }
952
953
954 void h323_set_id(char *id)
955 {
956         PString h323id(id);
957
958         if (h323debug) {
959                 cout << "  == Using '" << h323id << "' as our H.323ID for this call" << endl;
960         }
961
962         /* EVIL HACK */
963         endPoint->SetLocalUserName(h323id);
964 }
965
966 /** Establish Gatekeeper communiations, if so configured, 
967   *     register aliases for the H.323 endpoint to respond to.
968   */
969 int h323_set_gk(int gatekeeper_discover, char *gatekeeper, char *secret)
970 {
971         PString gkName = PString(gatekeeper);
972         PString pass   = PString(secret);
973
974         if (!end_point_exist()) {
975                 cout << "ERROR: [h323_set_gk] No Endpoint, this is bad!" << endl;
976                 return 1;
977         }
978
979         if (!gatekeeper) {
980                 cout << "Error: Gatekeeper cannot be NULL" << endl;
981                 return 1;
982         }
983
984         if (strlen(secret)) {
985                 endPoint->SetGatekeeperPassword(pass);
986         }
987                 
988
989         if (gatekeeper_discover) {
990                 /* discover the gk using multicast */
991                 if (endPoint->DiscoverGatekeeper(new H323TransportUDP(*endPoint))) {
992                         cout << "  == Using " << (endPoint->GetGatekeeper())->GetName() << " as our Gatekeeper." << endl;
993                 } else {
994                         cout << "  *** Could not find a gatekeeper." << endl;
995                         return 1;
996                 }       
997         } else {
998                 /* Gatekeeper operations */
999                 H323TransportUDP *rasChannel = new H323TransportUDP(*endPoint);
1000         
1001                 if (!rasChannel) {
1002                         cout << "       *** No RAS Channel, this is bad" << endl;
1003                         return 1;
1004                 }
1005                 if (endPoint->SetGatekeeper(gkName, rasChannel)) {
1006                         cout << "  == Using " << (endPoint->GetGatekeeper())->GetName() << " as our Gatekeeper." << endl;
1007                 } else {
1008                         cout << "  *** Error registering with gatekeeper \"" << gkName << "\". " << endl;
1009                         
1010                         /* XXX Maybe we should fire a new thread to attempt to re-register later and not kill asterisk here? */
1011
1012                 //      delete rasChannel;
1013                         return 1;
1014                 }
1015         }
1016         
1017         return 0;
1018 }
1019
1020 /** Send a DTMF tone over the H323Connection with the
1021   * specified token.
1022   */
1023 void h323_send_tone(const char *call_token, char tone)
1024 {
1025         if (!end_point_exist()) {
1026                 cout << "ERROR: [h323_send_tone] No Endpoint, this is bad!" << endl;
1027                 return;
1028         }
1029
1030         PString token = PString(call_token);
1031         endPoint->SendUserTone(token, tone);
1032 }
1033
1034 /** Make a call to the remote endpoint.
1035   */
1036 int h323_make_call(char *host, call_details_t *cd, call_options_t call_options)
1037 {
1038         int res;
1039         PString token;
1040
1041         if (!end_point_exist()) {
1042                 return 1;
1043         }
1044         
1045         PString dest(host);
1046
1047         res = endPoint->MakeCall(dest, token, &cd->call_reference, call_options.port, call_options.callerid);
1048         memcpy((char *)(cd->call_token), (const unsigned char *)token, token.GetLength());
1049         
1050         return res;
1051 };
1052
1053 int h323_clear_call(const char *call_token)
1054 {
1055         if (!end_point_exist()) {
1056                 return 1;
1057         }
1058
1059         ClearCallThread *clearCallThread = new ClearCallThread(call_token);
1060         clearCallThread->WaitForTermination();
1061         
1062         return 0;
1063 };
1064
1065 /** This function tells the h.323 stack to either 
1066     answer or deny an incoming call  */
1067 int h323_answering_call(const char *token, int busy) 
1068 {
1069         const PString currentToken(token);
1070
1071         H323Connection * connection;
1072         
1073         connection = endPoint->FindConnectionWithLock(currentToken);
1074         
1075         if (connection == NULL) {
1076                 cout << "No connection found for " << token << endl;
1077                 return -1;
1078         }
1079
1080         if (!busy){
1081                 connection->AnsweringCall(H323Connection::AnswerCallNow);
1082                 connection->Unlock();
1083
1084         } else {
1085                 connection->AnsweringCall(H323Connection::AnswerCallDenied);
1086                 connection->Unlock();
1087         };
1088
1089         return 0;
1090 }
1091
1092
1093 int h323_show_codec(int fd, int argc, char *argv[])
1094 {
1095         cout <<  "Allowed Codecs:\n\t" << setprecision(2) << endPoint->GetCapabilities() << endl;
1096
1097         return 0;
1098 }
1099
1100
1101 /* alas, this doesn't work :(   */
1102 void h323_native_bridge(const char *token, char *them, char *capability)
1103 {
1104         H323Channel *channel;
1105         MyH323Connection *connection = (MyH323Connection *)endPoint->FindConnectionWithLock(token);
1106         PString mode(capability);
1107         
1108         if (!mode) {
1109                 return;
1110         }
1111
1112         if (!connection){
1113                 cout << "ERROR: No connection found, this is bad\n";
1114                 return;
1115         }
1116
1117         cout << "Native Bridge:  them [" << them << "]" << endl; 
1118
1119         channel = connection->FindChannel(connection->sessionId, TRUE);
1120         connection->bridging = TRUE;
1121         connection->CloseLogicalChannelNumber(channel->GetNumber());
1122         connection->RequestModeChange(mode);    
1123         connection->Unlock();
1124         return;
1125
1126 }
1127
1128 } /* extern "C" */
1129
1130
1131