Bayonne

bayonne.h

Go to the documentation of this file.
00001 // Copyright (C) 2005 Open Source Telecom Corp.
00002 //  
00003 // This program is free software; you can redistribute it and/or modify
00004 // it under the terms of the GNU General Public License as published by
00005 // the Free Software Foundation; either version 2 of the License, or
00006 // (at your option) any later version.
00007 // 
00008 // This program is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, write to the Free Software 
00015 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00016 // 
00017 // As a special exception, you may use this file as part of a free software
00018 // library without restriction.  Specifically, if other files instantiate
00019 // templates or use macros or inline functions from this file, or you compile
00020 // this file and link it with other files to produce an executable, this
00021 // file does not by itself cause the resulting executable to be covered by
00022 // the GNU General Public License.  This exception does not however
00023 // invalidate any other reasons why the executable file might be covered by
00024 // the GNU General Public License.
00025 //
00026 // This exception applies only to the code released under the name GNU
00027 // ccScript.  If you copy code from other releases into a copy of GNU
00028 // ccScript, as the General Public License permits, the exception does
00029 // not apply to the code that you add in this way.  To avoid misleading
00030 // anyone as to the status of such modified files, you must delete
00031 // this exception notice from them.
00032 //
00033 // If you write modifications of your own for GNU ccScript, it is your choice
00034 // whether to permit this exception to apply to your modifications.
00035 // If you do not wish that, delete this exception notice.
00036 //
00037 
00038 #ifndef CCXX_BAYONNE_H_
00039 #define CCXX_BAYONNE_H_
00040 
00041 #ifndef CCXX_SCRIPT3_H_
00042 #include <cc++/script3.h>
00043 #endif
00044 
00045 #ifndef CCXX_CCAUDIO2_H_
00046 #include <cc++/audio2.h>
00047 #endif
00048 
00049 #ifndef CCXX_SOCKET_H_
00050 #include <cc++/socket.h>
00051 #endif
00052 
00053 #define BAYONNE_RELEASE 1               // release check sequence
00054 #define NO_TIMESLOT     0xffff
00055 #define MAX_DTMF        32
00056 #define MAX_LIST        256
00057 #define MAX_LIBINPUT    256
00058 #define MAX_PATHNAME    256
00059 #define MIN_AUDIOFEED   (120 * 8)       // 120 millisecond starting
00060 #define MAX_AUDIOFEED   (600 * 8)       // 600 millisecond buffer
00061 
00062 #ifdef  WIN32
00063 #define PFD_INVALID     INVALID_HANDLE_VALUE
00064 #else
00065 #define PFD_INVALID     -1
00066 #endif
00067 
00068 #if SCRIPT_RIPPLE_LEVEL < 2
00069 #error "ccscript 3 0.8.0 or later required"
00070 #endif
00071 
00072 namespace ost {
00073 
00074 class __EXPORT BayonneMsgport;
00075 class __EXPORT BayonneDriver;
00076 class __EXPORT BayonneSession;
00077 class __EXPORT BayonneSpan;
00078 class __EXPORT BayonneService;
00079 class __EXPORT BayonneTranslator;
00080           
00088 class __EXPORT Bayonne : public Script
00089 {
00090 public:
00091 #ifdef  WIN32
00092         typedef WORD    timeslot_t;
00093 #else
00094         typedef uint16_t timeslot_t;
00095 #endif
00096 
00097 protected:
00098         static BayonneSession **timeslots;
00099         static ScriptImage **localimages;
00100         static char *status;
00101         static ScriptCommand *server;
00102         static timeslot_t ts_limit;
00103         static timeslot_t ts_count;
00104         static timeslot_t ts_used;
00105         static std::ostream *logging;
00106         static const char *path_prompts;
00107         static const char *path_tmpfs;
00108         static const char *path_tmp;
00109         static unsigned idle_count;
00110         static unsigned idle_limit;
00111         static bool shutdown_flag;
00112         static char sla[];
00113         static time_t start_time;
00114 
00115 public:
00116         static timeout_t step_timer;
00117         static timeout_t reset_timer;
00118         static timeout_t exec_timer;
00119         static unsigned compile_count;
00120         static volatile bool image_loaded;
00121 
00122         static BayonneTranslator *init_translator;
00123         static const char *init_voicelib;
00124         static AtomicCounter libexec_count;
00125 
00129         typedef enum
00130         {
00131                 IF_PSTN,
00132                 IF_SPAN,
00133                 IF_ISDN,
00134                 IF_SS7,
00135                 IF_INET,
00136                 IF_NONE,
00137                 IF_POTS=IF_PSTN
00138         }       interface_t;
00139 
00143         typedef enum
00144         {
00145                 NONE,           /* idle */
00146                 INCOMING,       /* inbound call */
00147                 OUTGOING,       /* outbound call */
00148                 PICKUP,         /* station pickup */
00149                 FORWARDED,      /* inbound forwarded call */
00150                 RECALL,         /* inbound hold recall */
00151                 DIRECT,         /* node/endpoint direct */
00152                 RINGING,        /* ringing incoming join */
00153                 VIRTUAL         /* virtual channel driver */
00154         }       calltype_t;
00155 
00159         typedef enum
00160         {
00161                 BR_TDM,
00162                 BR_INET,
00163                 BR_SOFT,
00164                 BR_GATE,
00165                 BR_NONE
00166         }       bridge_t;
00167 
00173         typedef enum
00174         {
00175                 STATE_INITIAL = 0,
00176                 STATE_IDLE,
00177                 STATE_RESET,
00178                 STATE_RELEASE,
00179                 STATE_BUSY,
00180                 STATE_DOWN,
00181                 STATE_RING,
00182                 STATE_PICKUP,
00183                 STATE_SEIZE,
00184                 STATE_STEP,
00185                 STATE_EXEC,
00186                 STATE_THREAD,
00187                 STATE_CLEAR,
00188                 STATE_INKEY,
00189                 STATE_INPUT,
00190                 STATE_READ,
00191                 STATE_COLLECT,
00192                 STATE_DIAL,
00193                 STATE_XFER,
00194                 STATE_HOLD,
00195                 STATE_RECALL,
00196                 STATE_TONE,
00197                 STATE_DTMF,
00198                 STATE_PLAY,
00199                 STATE_RECORD,
00200                 STATE_JOIN,
00201                 STATE_WAIT,
00202                 STATE_CONNECT,
00203                 STATE_HUNTING,
00204                 STATE_SLEEP,
00205                 STATE_START,
00206                 STATE_HANGUP,
00207                 STATE_LIBRESET,
00208                 STATE_WAITKEY,
00209                 STATE_LIBWAIT,
00210                 STATE_IRESET,
00211                 STATE_FINAL,
00212 
00213                 STATE_STANDBY = STATE_DOWN,
00214                 STATE_LIBEXEC = STATE_EXEC,
00215                 STATE_RINGING = STATE_RING,
00216                 STATE_RUNNING = STATE_STEP,
00217                 STATE_THREADING = STATE_THREAD
00218         } state_t;
00219 
00224         typedef enum
00225         {
00226                 SIGNAL_EXIT = 0,
00227                 SIGNAL_ERROR,
00228                 SIGNAL_TIMEOUT,
00229                 SIGNAL_DTMF,
00230         
00231                 SIGNAL_0,
00232                 SIGNAL_1,
00233                 SIGNAL_2,
00234                 SIGNAL_3,
00235                 SIGNAL_4,
00236                 SIGNAL_5,
00237                 SIGNAL_6,
00238                 SIGNAL_7,
00239                 SIGNAL_8,
00240                 SIGNAL_9,
00241                 SIGNAL_STAR,
00242                 SIGNAL_POUND,
00243                 SIGNAL_A,
00244                 SIGNAL_OVERRIDE = SIGNAL_A,
00245                 SIGNAL_B,
00246                 SIGNAL_FLASH = SIGNAL_B,
00247                 SIGNAL_C,
00248                 SIGNAL_IMMEDIATE = SIGNAL_C,
00249                 SIGNAL_D,
00250                 SIGNAL_PRIORITY = SIGNAL_D,
00251 
00252                 SIGNAL_RING,
00253                 SIGNAL_TONE,
00254                 SIGNAL_EVENT,
00255                 SIGNAL_WINK,
00256 
00257                 SIGNAL_CHILD,
00258                 SIGNAL_FAIL,
00259                 SIGNAL_PICKUP,
00260                 SIGNAL_PART,
00261 
00262                 SIGNAL_INVALID,
00263                 SIGNAL_PARENT,
00264                 SIGNAL_WAIT,
00265 
00266                 SIGNAL_HANGUP = SIGNAL_EXIT
00267         } signal_t;
00268 
00274         typedef enum 
00275         {
00276                 // msgport management events, server control...
00277 
00278                 MSGPORT_WAKEUP = 0,     // wakeup a waiting message port
00279                 MSGPORT_SHUTDOWN,       // notify msgport of server shutdown
00280                 MSGPORT_LOGGING,        // enable driver event logging
00281                 MSGPORT_REGISTER,       // update registrations
00282 
00283                 // primary event identifiers
00284 
00285                 ENTER_STATE = 100,      // newly entered state handler
00286                 EXIT_STATE,
00287                 EXIT_THREAD,            // thread death notify
00288                 EXIT_TIMER,             // timer notified exit
00289                 EXIT_PARTING,           // exit a join...
00290                 NULL_EVENT,             // generic null event pusher
00291                 ERROR_STATE,            // generic enter with error
00292                 ENTER_HUNTING,
00293                 EXIT_HUNTING,
00294 
00295                 // primary session control events
00296 
00297                 START_DIRECT = 200,     // start a script, directed
00298                 START_INCOMING,         // start an incoming script
00299                 START_OUTGOING,         // start selected script, outbound
00300                 START_RECALL,           // start a recalled call
00301                 START_FORWARDED,        // start a forwarded call
00302                 START_RINGING,          // start a ringing call
00303                 START_HUNTING,          // start a hunting call
00304                 STOP_SCRIPT,
00305                 STOP_DISCONNECT,
00306                 STOP_PARENT,
00307                 CANCEL_CHILD,
00308                 DETACH_CHILD,
00309                 CHILD_RUNNING,
00310                 CHILD_FAILED,
00311                 CHILD_INVALID,
00312                 CHILD_EXPIRED,
00313                 CHILD_BUSY,
00314                 CHILD_FAX,
00315                 CHILD_DND,
00316                 CHILD_AWAY,
00317                 CHILD_NOCODEC,
00318                 CHILD_OFFLINE,
00319 
00320                 START_SCRIPT = START_INCOMING,
00321                 START_SELECTED = START_OUTGOING,
00322 
00323                 // libexec specific events
00324 
00325                 ENTER_LIBEXEC = 300,
00326                 EXIT_LIBEXEC,
00327                 HEAD_LIBEXEC,
00328                 ARGS_LIBEXEC,
00329                 GOT_LIBEXEC,
00330                 READ_LIBEXEC,
00331                 DROP_LIBEXEC,
00332                 STAT_LIBEXEC,
00333                 PROMPT_LIBEXEC,
00334                 CLEAR_LIBEXEC,
00335                 WAIT_LIBEXEC,
00336                 RECORD_LIBEXEC,
00337                 REPLAY_LIBEXEC,
00338                 RESTART_LIBEXEC,
00339                 TONE_LIBEXEC,
00340                 XFER_LIBEXEC,
00341                 POST_LIBEXEC,
00342                 ERROR_LIBEXEC,
00343 
00344                 // primary driver events
00345 
00346                 TIMER_EXPIRED = 400,    // trunk timer expired
00347                 LINE_WINK,
00348                 LINE_PICKUP,
00349                 LINE_HANGUP,
00350                 LINE_DISCONNECT,
00351                 LINE_ON_HOOK,
00352                 LINE_OFF_HOOK,
00353                 RING_ON,
00354                 RING_OFF,
00355                 RING_STOP,
00356                 LINE_CALLER_ID,
00357                 RINGING_DID,
00358                 DEVICE_BLOCKED,
00359                 DEVICE_UNBLOCKED,
00360                 DEVICE_OPEN,
00361                 DEVICE_CLOSE,
00362                 DSP_READY,
00363 
00364                 // primary call processing events
00365 
00366                 CALL_DETECT = 500,
00367                 CALL_CONNECTED,
00368                 CALL_RELEASED,
00369                 CALL_ACCEPTED,
00370                 CALL_ANSWERED,
00371                 CALL_HOLD,
00372                 CALL_HOLDING=CALL_HOLD,
00373                 CALL_NOHOLD,
00374                 CALL_DIGITS,
00375                 CALL_OFFERED,
00376                 CALL_ANI,
00377                 CALL_ACTIVE,
00378                 CALL_NOACTIVE,
00379                 CALL_BILLING,
00380                 CALL_RESTART,
00381                 CALL_SETSTATE,
00382                 CALL_FAILURE,
00383                 CALL_ALERTING,
00384                 CALL_INFO,
00385                 CALL_BUSY,
00386                 CALL_DIVERT,
00387                 CALL_FACILITY,
00388                 CALL_FRAME,
00389                 CALL_NOTIFY,
00390                 CALL_NSI,
00391                 CALL_RINGING,
00392                 CALL_DISCONNECT,
00393                 CALL_CLEARED,
00394                 CALL_PROCEEDING,
00395                 RESTART_FAILED,
00396                 RELEASE_FAILED,
00397 
00398                 // some timeslot specific control and dial events
00399 
00400                 START_RING = 600,
00401                 STOP_RING,
00402                 CLEAR_TIMESLOT, // garbage collect
00403                 START_FLASH,
00404                 STOP_FLASH,
00405                 DIAL_CONNECT,
00406                 DIAL_TIMEOUT,
00407                 DIAL_FAILED,
00408                 DIAL_INVALID,
00409                 DIAL_BUSY,
00410                 DIAL_FAX,
00411                 DIAL_PAM,
00412                 DIAL_DND,
00413                 DIAL_AWAY,
00414                 DIAL_OFFLINE,
00415                 DIAL_NOCODEC,
00416 
00417                 DIAL_MACHINE = DIAL_PAM,
00418 
00419                 // basic audio stuff
00420 
00421                 AUDIO_IDLE = 700,
00422                 AUDIO_ACTIVE,
00423                 AUDIO_EXPIRED,
00424                 INPUT_PENDING,
00425                 OUTPUT_PENDING,
00426                 AUDIO_BUFFER,
00427                 TONE_IDLE,
00428                 DTMF_KEYDOWN,
00429                 DTMF_KEYSYNC,   /* timing sync event */
00430                 DTMF_KEYUP,
00431                 TONE_START,
00432                 TONE_STOP,
00433                 AUDIO_START,
00434                 AUDIO_STOP,
00435                 DTMF_GENDOWN,   /* peer */
00436                 DTMF_GENUP,     /* peer */
00437                 AUDIO_SYNC,     /* peer */
00438                 AUDIO_RECONNECT,        /* re-invite event... */
00439                 AUDIO_DISCONNECT,       /* audio holding re-invite */
00440                 PEER_RECONNECT,         /* peer re-invite notice */
00441                 PEER_DISCONNECT,        /* peer holding notice */
00442                 DTMF_GENTONE = DTMF_GENUP,
00443 
00444                 // make modes and special timeslot stuff
00445 
00446                 MAKE_TEST = 800,
00447                 MAKE_BUSY,
00448                 MAKE_IDLE,
00449                 MAKE_DOWN,
00450                 MAKE_UP,
00451                 MAKE_EXPIRED,
00452                 ENABLE_LOGGING,
00453                 DISABLE_LOGGING,
00454                 PART_EXPIRED,
00455                 PART_EXITING,
00456                 PART_DISCONNECT,
00457                 JOIN_PEER,
00458                 PEER_WAITING,
00459                 RELOCATE_REQUEST,       // queued event
00460                 RELOCATE_ACCEPT,        // hunting reply
00461                 RELOCATE_REJECT,        // a kind of disconnect
00462                 START_RELOCATE,         // start timeslot for relocation
00463 
00464                 // master control events
00465 
00466                 SYSTEM_DOWN = 900,      
00467 
00468                 // driver specific events and anomalies
00469 
00470                 DRIVER_SPECIFIC = 1000  // oddball events
00471 
00472         } event_t;
00473 
00474         typedef enum
00475         {
00476                 RESULT_SUCCESS = 0,
00477                 RESULT_TIMEOUT,
00478                 RESULT_INVALID,
00479                 RESULT_PENDING,
00480                 RESULT_COMPLETE,
00481                 RESULT_FAILED,
00482                 RESULT_BADPATH = 254,
00483                 RESULT_OFFLINE = 255
00484         }       result_t;
00485 
00486         typedef struct
00487         {
00488                 Line line;
00489                 char text[MAX_LIBINPUT];
00490                 const char *list[MAX_LIBINPUT / 2];
00491         }       libaudio_t;
00492 
00493         typedef struct
00494         {
00495                 const char *remote;
00496                 const char *userid;
00497                 const char *type;
00498                 const char *status;
00499                 unsigned short active_calls, call_limit;
00500                 unsigned long attempts_iCount, attempts_oCount;
00501                 unsigned long complete_iCount, complete_oCount;
00502                 time_t updated;
00503         }       regauth_t;
00504 
00512         typedef struct
00513         {
00514                 event_t id;
00515                 timeslot_t timeslot;
00516                 uint16 seq;
00517 
00518                 union
00519                 {
00520                         // used to invoke attach.  The image refcount
00521                         // is assumed to already be inc'd!  If no
00522                         // scr is passed, then uses default or search
00523                         // vars passed in sym, value pairs
00524                         struct
00525                         {
00526                                 ScriptImage *img;
00527                                 Script::Name *scr;
00528                                 BayonneSession *parent;
00529                                 const char *dialing;
00530                         }       start;
00531 
00532                         struct
00533                         {
00534                                 ScriptImage *img;
00535                                 Script::Name *scr;
00536                                 BayonneSession *parent;
00537                                 Line *select;
00538                         }       hunt;
00539 
00540                         struct
00541                         {
00542                                 BayonneSession *current;
00543                                 BayonneSession *replace;
00544                         }       relocate;
00545 
00546                         struct
00547                         {
00548                                 const char *tid;
00549 #ifdef  WIN32
00550                                 HANDLE  pfd;
00551 #else
00552                                 const char *fname;
00553 #endif
00554                                 int pid, result;
00555                         }       libexec;
00556 
00557                         struct
00558                         {
00559                                 const char *tid;
00560                                 const char *errmsg;
00561                         }       liberror;
00562 
00563                         struct
00564                         {
00565                                 timeout_t duration;
00566                                 int digit;
00567                         }       dtmf;
00568 
00569                         struct
00570                         {
00571                                 const char *err;
00572                                 const char *msg;
00573                         }       cpa;
00574 
00575                         struct
00576                         {
00577                                 const char *name;
00578                                 bool exit;
00579                         }       tone;
00580 
00581                         struct
00582                         {
00583                                 std::ostream *output;
00584                                 const char *logstate;
00585                         }       debug;
00586 
00587                         const char *name;
00588                         const char *errmsg;
00589                         BayonneSession *pid;
00590                         BayonneSession *peer;
00591                         void *data;
00592                 };
00593 
00594         }       Event;
00595 
00600         class __EXPORT Traffic
00601         {
00602         private:
00603                 static unsigned long stamp;
00604 
00605         public:
00606                 Traffic();
00607 
00608                 static inline unsigned long getStamp(void)
00609                         {return stamp;};
00610 
00611                 volatile unsigned long iCount, oCount;
00612         };
00613 
00619         typedef bool (BayonneSession::*Handler)(Event *event);
00620 
00625         typedef struct
00626         {
00627                 const char *name;
00628                 Handler handler;
00629                 char flag;
00630         }       statetab;
00631 
00639         typedef struct
00640         {
00641                 Handler handler, logstate;
00642                 const char *name;
00643                 timeout_t timeout;
00644                 Name *menu;
00645                 unsigned stack;
00646                 Line *lib;
00647 #ifdef  WIN32
00648                 HANDLE pfd;
00649 #else
00650                 int pfd;
00651 #endif
00652                 result_t result;
00653                 int pid;
00654                 libaudio_t *libaudio;
00655 
00656                 union
00657                 {
00658                         struct
00659                         {
00660                                 unsigned count;
00661                                 timeout_t interval;
00662                         }       wait;
00663 
00664                         struct
00665                         {
00666                                 Audio::Mode mode;
00667                                 Audio::Level level;
00668                                 timeout_t total, silence, intersilence;
00669                                 long lastnum;
00670                                 bool exitkey;
00671                                 bool compress;
00672                                 bool trigger;
00673                                 const char *pos;
00674                                 const char *exit;
00675                                 const char *menu;
00676                                 const char *note;
00677                                 const char *list[MAX_LIST];
00678                         }       audio;
00679                                 
00680                         struct
00681                         {
00682                                 timeout_t interdigit;
00683                                 timeout_t lastdigit;
00684                                 const char *var;
00685                                 const char *exit;
00686                                 const char *format;
00687                                 const char *ignore;
00688                                 const char *route;
00689                                 unsigned count, size, required;
00690                         }       input;
00691 
00692                         struct
00693                         {
00694                                 const char *var;
00695                                 const char *menu;
00696                         }       inkey;
00697 
00698                         struct
00699                         {
00700                                 const char *sequence;
00701                                 bool flashing;
00702                                 bool dialing;
00703                                 bool exiting;
00704                                 bool hangup;
00705                                 bool dtmf;
00706                                 char *syncdigit;
00707                                 timeout_t synctimer;
00708                                 timeout_t duration;
00709                                 char digits[64];
00710                                 char sessionid[16];
00711                         }       tone;
00712 
00713                         struct
00714                         {
00715                                 timeout_t on, off, interdigit;
00716                                 unsigned pos;
00717                                 bool flashing;
00718                                 bool dialing;
00719                                 unsigned char digits[64];
00720                         }       pulse;  
00721 
00722                         struct
00723                         {
00724                                 const char *dial;
00725                                 const char *exit;
00726                                 bool dtmf, drop, hangup;
00727                                 BayonneSession *peer;
00728                                 timeout_t answer_timer, hunt_timer;
00729                                 Line *select;
00730                                 unsigned index;
00731                                 char digits[64];
00732                         }       join;
00733 
00734                         struct
00735                         {
00736                                 const char *ref;
00737                                 char buf[MAX_LIST * sizeof(char *)];
00738                         }       url;
00739                 };
00740                 
00741         }       State;
00742         
00746         static statetab states[];
00747 
00752         static Mutex serialize;
00753 
00757         static ThreadLock reloading;
00758 
00762         static Traffic total_call_attempts;
00763         static Traffic total_call_complete;
00764         static volatile unsigned short total_active_calls;
00765 
00773         static void allocate(timeslot_t timeslots, ScriptCommand *pointer = NULL, timeslot_t overdraft = 0);
00774 
00778         static void allocateLocal(void);
00779 
00786         void md5_hash(char *out, const char *source);
00787 
00791         static void waitLoaded(void);
00792         
00798         static unsigned long uptime(void);
00799 
00805         static ScriptCompiler *reload(void);
00806 
00810         static void down(void);
00811 
00818         static bool service(const char *service);
00819 
00827         static BayonneSession *getSession(timeslot_t timeslot);
00828 
00835         static ScriptImage **getLocalImage(timeslot_t timeslot);
00836 
00848         static BayonneSession *startDialing(const char *dial, 
00849                 const char *name, const char *caller, const char *display, BayonneSession *parent = NULL);
00850 
00859         static BayonneSession *getSid(const char *id);
00860 
00868         static timeslot_t toTimeslot(const char *id);
00869 
00875         static inline timeslot_t getTimeslotsUsed(void)
00876                 {return Bayonne::ts_used;};
00877 
00883         static inline timeslot_t getTimeslotCount(void)
00884                 {return Bayonne::ts_count;};    
00885 
00892         static inline timeslot_t getAvailTimeslots(void)
00893                 {return Bayonne::ts_count - Bayonne::ts_used;};
00894 
00902         static Handler getState(const char *name);
00903 
00910         static int getDigit(char dtmf);
00911 
00918         static char getChar(int dtmf);
00919 
00930         static bool matchDigits(const char *digits, const char *match, bool partial = false);
00931 
00937         static ScriptImage *useImage(void);
00938 
00945         static void endImage(ScriptImage *image);
00946 
00953         static bool loadPlugin(const char *path);
00954 
00961         static bool loadMonitor(const char *path);  
00962 
00969         static bool loadAudio(const char *path);
00970 
00971         static void errlog(const char *level, const char *fmt, ...); 
00972 
00973         static bool getUserdata(void);
00974 };
00975 
00983 class __EXPORT BayonneConfig : public Keydata
00984 {
00985 private:
00986         static BayonneConfig *first;
00987 
00988         BayonneConfig *next;
00989         const char *id;
00990 
00991 public:
00992         BayonneConfig(const char *id, Define *def, const char *path);
00993         BayonneConfig(const char *id, const char *path);
00994         void setEnv(const char *id);
00995         static BayonneConfig *get(const char *id);
00996         static void rebuild(ScriptImage *img);
00997 };
00998 
01006 class __EXPORT BayonneTranslator : public Bayonne
01007 {
01008 protected:
01009         static BayonneTranslator *first;
01010         BayonneTranslator *next;
01011         const char *id;
01012 
01013         static const char *getToken(BayonneSession *s, Line *l, unsigned *idx);
01014         static unsigned addItem(BayonneSession *s, unsigned count, const char *text);
01015         static const char *getLast(BayonneSession *s, unsigned count);
01016 
01017 public:
01025         BayonneTranslator(const char *iso);
01026 
01027         virtual ~BayonneTranslator();
01028 
01035         static BayonneTranslator *get(const char *name);
01036 
01041         static inline BayonneTranslator *getFirst(void)
01042                 {return first;};
01043 
01049         inline BayonneTranslator *getNext()
01050                 {return next;};
01051         
01059         static BayonneTranslator *loadTranslator(const char *iso);
01060 
01069         virtual unsigned digits(BayonneSession *sessiob, unsigned count, const char *string);
01070 
01079         virtual unsigned spell(BayonneSession *session, unsigned count, const char *string);
01080 
01089         virtual unsigned sayorder(BayonneSession *session, unsigned count, const char *string);
01090 
01099         virtual unsigned number(BayonneSession *session, unsigned count, const char *string);
01100 
01110         virtual unsigned saynumber(BayonneSession *session, unsigned count, const char *string);
01111 
01120         virtual unsigned saycount(BayonneSession *session, unsigned count, const char *string);
01121 
01130         virtual unsigned sayhour(BayonneSession *session, unsigned count, const char *string);
01131 
01140         virtual unsigned saytime(BayonneSession *session, unsigned count, const char *string);
01141 
01150         virtual unsigned weekday(BayonneSession *session, unsigned count, const char *string);
01151 
01152         /*
01153          * Translate a string with a date into spoken date without year.
01154          *
01155          * @param session to save list of prompts.
01156          * @param count of current prompts used.
01157          * @param string to be processed.
01158          * @return new count of prompts used.
01159          */
01160         virtual unsigned sayday(BayonneSession *session, unsigned count, const char *string);
01161 
01170         virtual unsigned saydate(BayonneSession *session, unsigned count, const char *string);
01171 
01180         virtual unsigned saybool(BayonneSession *session, unsigned count, const char *string);
01181 
01190         virtual unsigned phone(BayonneSession *session, unsigned count, const char *string);
01191 
01200         virtual unsigned extension(BayonneSession *session, unsigned count, const char *string);
01201 
01209         virtual const char *speak(BayonneSession *session, Line *line = NULL);
01210 
01216         inline const char *getId(void)
01217                 {return id;};
01218 };
01219 
01227 class __EXPORT BayonneAudio : public AudioStream, public Bayonne
01228 {
01229 protected:
01230         char filename[MAX_PATHNAME];
01231         const char **list;
01232         char *getContinuation(void);
01233 
01234 public:
01239         AudioTone *tone;
01240 
01245         BayonneTranslator *translator;
01246 
01250         char vlib[60];
01251 
01252         const char *extension, *voicelib, *libext, *prefixdir, *offset;
01253         Encoding encoding;
01254         timeout_t framing;
01255         char var_position[14];
01256 
01260         BayonneAudio();
01261 
01269         const char *getFilename(const char *name, bool write = false);
01270 
01275         void cleanup(void);
01276         
01283         void play(const char **list, Mode mode = modeRead);
01284 
01292         void record(const char *name, Mode mode = modeCreate, const char *annotation = NULL);
01293 
01300         const char *getVoicelib(const char *iso);
01301 
01307         inline AudioCodec *getCodec(void)
01308                 {return codec;};
01309 };              
01310 
01321 class __EXPORT BayonneMsgport : public Thread, public Buffer, public Bayonne
01322 {
01323 public:
01327         virtual ~BayonneMsgport();
01328 
01334         BayonneMsgport(BayonneDriver *driver);
01335 
01341         void update(void); 
01342 
01347         void initial(void);
01348 
01349 protected:
01350         BayonneDriver *msgdriver;
01351         Event *msglist;
01352         unsigned msgsize, msghead, msgtail;
01353         timeslot_t tsfirst, tscount;
01354         char msgname[16];
01355 
01359         void shutdown(void);
01360 
01368         virtual timeout_t getTimeout(Event *event);
01369         
01370         void run(void);
01371 
01372         size_t onWait(void *buf);
01373         size_t onPost(void *buf);
01374         size_t onPeek(void *buf);
01375 };
01376 
01384 class __EXPORT BayonneDriver : public Bayonne, public Keydata, public Mutex
01385 {
01386 protected:
01387         friend class __EXPORT BayonneSession;
01388         friend class __EXPORT BayonneMsgport;
01389         static BayonneDriver *firstDriver;
01390         static BayonneDriver *lastDriver;
01391         static BayonneDriver *trunkDriver;
01392         static BayonneDriver *protoDriver;
01393         static Semaphore oink;  // the pig has been kicked!
01394         static bool protocols;
01395 
01396         BayonneSession *firstIdle, *lastIdle, *highIdle;
01397         BayonneMsgport *msgport;
01398         BayonneDriver *nextDriver;
01399         const char *name;
01400         timeslot_t timeslot, count;
01401         unsigned avail;
01402         unsigned span, spans;   
01403         bool running;
01404         static bool stopping;
01405         std::ostream *logevents;
01406 
01407         int audio_priority;
01408         size_t audio_stack;
01409         Audio::Level audio_level;
01410 
01411         timeout_t pickup_timer, hangup_timer, seize_timer, ring_timer, hunt_timer;
01412         timeout_t reset_timer, release_timer, flash_timer, interdigit_timer;
01413         unsigned answer_count;
01414 
01419         virtual void reloadDriver(void);
01420 
01425         virtual void startDriver(void);
01426 
01430         virtual void stopDriver(void);
01431 
01436         void relistIdle(void);
01437 
01438 public:
01439         Traffic call_attempts, call_complete;
01440         volatile unsigned short active_calls;
01441 
01450         BayonneDriver(Keydata::Define *pairs, const char *key, const char *id, bool virt = false);
01451 
01455         ~BayonneDriver();
01456 
01460         static inline bool useProtocols(void)
01461                 {return protocols;}
01462 
01466         static bool isStopping(void)
01467                 {return stopping;};
01468 
01472         static inline BayonneDriver *getTrunking(void)
01473                 {return trunkDriver;}
01474 
01478         static inline BayonneDriver *getPrimary(void)
01479                 {return firstDriver;}
01480         
01484         inline BayonneDriver *getNext(void)
01485                 {return nextDriver;};
01486 
01487 
01491         static inline BayonneDriver *getProtocol(void)
01492                 {return protoDriver;}
01493 
01499         BayonneSession *getIdle(void);
01500 
01508         virtual const char *registerScript(ScriptImage *image, Line *line);
01509 
01517         virtual const char *assignScript(ScriptImage *image, Line *line);
01518 
01525         static BayonneDriver *get(const char *id);
01526 
01533         static BayonneDriver *loadDriver(const char *id);
01534 
01541         static BayonneDriver *loadTrunking(const char *id);
01542 
01550         static BayonneDriver *loadProtocol(const char *id, unsigned timeslots = 0);
01551 
01559         static unsigned list(char **items, unsigned max);
01560 
01564         static void start(void);
01565         
01569         static void stop(void);
01570 
01574         static void reload(void);
01575 
01582         static void add(BayonneSession *session);
01583 
01590         static void del(BayonneSession *session);
01591 
01597         inline timeslot_t getFirst(void)
01598                 {return timeslot;};
01599 
01605         inline timeslot_t getCount(void)
01606                 {return count;};
01607 
01613         inline unsigned getSpanFirst(void)
01614                 {return span;};
01615 
01621         inline unsigned getSpansUsed(void)
01622                 {return spans;};
01623 
01629         inline const char *getName(void)
01630                 {return name;};
01631 
01638         inline timeout_t getResetTimer(void)
01639                 {return reset_timer;};
01640 
01646         inline timeout_t getReleaseTimer(void)
01647                 {return release_timer;};
01648 
01654         inline timeout_t getHangupTimer(void)
01655                 {return hangup_timer;};
01656 
01662         inline timeout_t getPickupTimer(void)
01663                 {return pickup_timer;};
01664 
01670         inline timeout_t getSeizeTimer(void)
01671                 {return seize_timer;};
01672 
01678         inline timeout_t getHuntTimer(void)
01679                 {return hunt_timer;};
01680 
01686         inline timeout_t getFlashTimer(void)
01687                 {return flash_timer;};
01688 
01694         inline timeout_t getInterdigit(void)
01695                 {return interdigit_timer;};
01696 
01704         inline timeout_t getRingTimer(void)
01705                 {return ring_timer;};
01706 
01712         inline unsigned getAnswerCount(void)
01713                 {return answer_count;};
01714 
01721         BayonneSpan *getSpan(unsigned id);
01722 
01730         BayonneSession *getTimeslot(timeslot_t id);
01731 
01737         inline BayonneMsgport *getMsgport(void)
01738                 {return msgport;};
01739 
01745         inline size_t getAudioStack(void)
01746                 {return audio_stack;};
01747 
01753         inline int getAudioPriority(void)
01754                 {return audio_priority;};
01755 
01761         inline Audio::Level getAudioLevel(void)
01762                 {return audio_level;};
01763 
01769         inline void setLogging(std::ostream *output)
01770                 {logevents = output;};
01771 
01778         inline bool isSpanable(unsigned span);
01779 
01790         virtual bool getDestination(const char *target, const char *dial, char *output, size_t size);
01791 
01797         inline unsigned getAvail(void)
01798                 {return avail;}
01799 
01807         virtual bool isExternal(const char *dest);
01808 
01815         virtual bool isRegistered(const char *dest);
01816 
01823         virtual bool isAvailable(const char *dest);
01824 
01832         virtual bool isReachable(const char *proxy);
01833 
01841         virtual unsigned getRegistration(regauth_t *data, unsigned count);
01842 };      
01843 
01851 class __EXPORT BayonneSpan : public Bayonne, public Keydata
01852 {
01853 protected:
01854         friend class __EXPORT BayonneSession;
01855         friend class __EXPORT BayonneDriver;
01856 
01857         static BayonneSpan *first;
01858         static BayonneSpan *last;
01859         static unsigned spans;
01860         static BayonneSpan **index;
01861         
01862         unsigned id;
01863         BayonneDriver *driver;
01864         BayonneSpan *next;
01865         timeslot_t timeslot, count, used;       // timeslots
01866 
01867 public:
01868         Traffic call_attempts, call_complete;
01869         volatile unsigned short active_calls;
01870 
01877         BayonneSpan(BayonneDriver *driver, timeslot_t timeslots);
01878 
01885         static BayonneSpan *get(unsigned id);
01886         
01893         BayonneSession *getTimeslot(timeslot_t id);
01894 
01900         static void allocate(unsigned total = 0);
01901 
01907         static inline unsigned getSpans(void)
01908                 {return spans;};
01909 
01915         inline timeslot_t getFirst(void)
01916                 {return timeslot;};
01917 
01923         inline timeslot_t getCount(void)
01924                 {return count;};
01925 
01931         inline unsigned getId(void)
01932                 {return id;};
01933 
01939         inline BayonneDriver *getDriver(void)
01940                 {return driver;};
01941 
01947         inline unsigned getAvail(void)
01948                 {return count - used;}
01949 };
01950 
01957 class __EXPORT BayonneBinder : public ScriptBinder, public Bayonne
01958 {
01959 private:
01960         static BayonneBinder *binder;
01961 
01962 protected:
01963         virtual const char *submit(const char **data);
01964 
01965         virtual ScriptCompiler *compiler(void);
01966 
01967         BayonneSession *session(ScriptInterp *interp);
01968 
01969         bool scriptEvent(ScriptInterp *interp, const char *evt);
01970 
01971         bool digitEvent(ScriptInterp *interp, const char *evt);
01972 
01973         BayonneBinder(const char *id);
01974 
01975 public:
01976         static const char *submitRequest(const char **data);
01977 
01978         static ScriptCompiler *getCompiler(void);
01979 };
01980 
01988 class __EXPORT BayonneSession : public ScriptInterp, public Bayonne
01989 {
01990 private:
01991         friend class __EXPORT BayonneMsgport;
01992         friend class __EXPORT BayonneTranslator;
01993         friend class __EXPORT BayonneDriver;
01994         friend class __EXPORT Bayonne;
01995 
01996         BayonneSession() {};
01997 
01998         BayonneSession *nextIdle, *prevIdle;
01999         bool isAvail;
02000 
02001 protected:
02002         static BayonneTranslator langNone;
02003         static ScriptSymbols *globalSyms;
02004         static Mutex globalLock;        
02005 
02006         std::ostream *logevents, *logtrace;
02007         BayonneDriver *driver;
02008         BayonneMsgport *msgport;
02009         BayonneSession *peer;
02010         BayonneSpan *span;
02011         timeslot_t timeslot;
02012         uint8 seq;
02013         uint16 evseq;
02014         uint32 tseq;
02015         bool offhook, dtmf, answered, starting, holding, connecting;
02016         time_t audiotimer, starttime;
02017         interface_t iface;
02018         bridge_t bridge;
02019         calltype_t type;
02020         event_t seizure;
02021 
02022 //      Name *getScript(const char *scr);
02023 
02031         bool requiresDTMF(void);
02032 
02038         virtual bool enableDTMF(void);
02039 
02043         virtual void disableDTMF(void);
02044 
02053         virtual const char *checkAudio(bool live);
02054 
02061         virtual bool filterPosting(Event *event);
02062 
02063         virtual bool enterCommon(Event *event);
02064         virtual bool enterInitial(Event *event);
02065         virtual bool enterFinal(Event *event);
02066         virtual bool enterIdle(Event *event);
02067         virtual bool enterReset(Event *event);
02068         virtual bool enterRelease(Event *event);
02069         virtual bool enterRinging(Event *event);
02070         virtual bool enterPickup(Event *event);
02071         virtual bool enterSeize(Event *event);
02072         virtual bool enterHunting(Event *event);
02073         virtual bool enterHangup(Event *event);
02074         virtual bool enterTone(Event *event);
02075         virtual bool enterDTMF(Event *event);
02076         virtual bool enterPlay(Event *event);
02077         virtual bool enterRecord(Event *event);
02078         virtual bool enterJoin(Event *event);
02079         virtual bool enterWait(Event *event);
02080         virtual bool enterDial(Event *event);
02081         virtual bool enterBusy(Event *event);
02082         virtual bool enterStandby(Event *event);
02083         virtual bool enterXfer(Event *event);
02084         virtual bool enterHold(Event *event);
02085         virtual bool enterRecall(Event *event);
02086 
02091         void check(void);
02092 
02093         void renameRecord(void);
02094         bool stateInitial(Event *event);
02095         bool stateFinal(Event *event);
02096         bool stateIdle(Event *event);
02097         bool stateIdleReset(Event *event);
02098         bool stateReset(Event *event);
02099         bool stateRelease(Event *event);
02100         bool stateBusy(Event *event);
02101         bool stateStandby(Event *event);
02102         bool stateRinging(Event *event);
02103         bool statePickup(Event *event);
02104         bool stateSeize(Event *event);
02105         bool stateHunting(Event *event);
02106         bool stateRunning(Event *event);
02107         bool stateLibexec(Event *event);
02108         bool stateLibreset(Event *event);
02109         bool stateLibwait(Event *event);
02110         bool stateWaitkey(Event *event);
02111         bool stateThreading(Event *event);
02112         bool stateHangup(Event *event);
02113         bool stateCollect(Event *event);
02114         bool stateSleep(Event *event);
02115         bool stateStart(Event *event);
02116         bool stateClear(Event *event);
02117         bool stateInkey(Event *event);
02118         bool stateInput(Event *event);
02119         bool stateRead(Event *event);
02120         bool stateDial(Event *event);
02121         bool stateXfer(Event *event);
02122         bool stateHold(Event *event);
02123         bool stateRecall(Event *event);
02124         bool stateTone(Event *event);
02125         bool stateDTMF(Event *event);
02126         bool statePlay(Event *event);
02127         bool stateRecord(Event *event);
02128         bool stateJoin(Event *event);
02129         bool stateWait(Event *event);
02130         bool stateConnect(Event *event);
02131 
02138         bool putEvent(Event *event);
02139 
02145         void libWrite(const char *string);
02146 
02147         void libClose(const char *string);
02148 
02149         bool isLibexec(const char *tsid);
02150 
02151         timeout_t getLibexecTimeout(void);
02152 
02153         const char *getWritepath(char *buf = NULL, size_t len = 0);
02154 
02155         void incIncomingAttempts(void);
02156 
02157         void incOutgoingAttempts(void);
02158 
02159         void incIncomingComplete(void);
02160 
02161         void incOutgoingComplete(void);
02162 
02163         void incActiveCalls(void);
02164 
02165         void decActiveCalls(void);
02166 
02167 public:
02168         Traffic call_attempts, call_complete;
02169 
02176         const char *getExternal(const char *option);
02177 
02183         inline uint16 getEventSequence(void)
02184                 {return evseq;};
02185 
02186         static const char *getGlobal(const char *id);
02187         static bool setGlobal(const char *id, const char *value);
02188         static bool sizeGlobal(const char *id, unsigned size);
02189         static bool addGlobal(const char *id, const char *value);
02190         static bool clearGlobal(const char *id);
02191 
02192 protected:
02199         ScriptInterp *getInterp(const char *id);
02200 
02207         ScriptSymbols *getSymbols(const char *id);
02208 
02212         BayonneTranslator *translator;
02213 
02221         Name *attachStart(Event *event);
02222 
02226         unsigned getId(void);
02227 
02232         void setSid(void);
02233 
02237         void setState(state_t);
02238 
02243         void setRunning(void);
02244 
02249         void setConnecting(const char *evname = NULL);
02250 
02259         bool setLibexec(result_t result);
02260 
02269         bool setLibreset(result_t result);
02270 
02271 public:
02277         inline BayonneTranslator *getTranslator(void)
02278                 {return translator;};
02279 
02283         inline void startConnecting(void)
02284                 {connecting = true;};
02285 
02286 protected:
02293         inline libaudio_t *getLibaudio(void)
02294                 {return state.libaudio;};
02295 
02299         void finalize(void);
02300 
02306         bool exit(void);
02307 
02308         char var_date[12];
02309         char var_time[12];
02310         char var_duration[12];
02311         char var_callid[12];
02312         char var_tid[14];
02313         char var_sid[16];
02314         char var_pid[16];
02315         char var_joined[16];
02316         char var_rings[4];
02317         char var_timeslot[8];
02318         char var_spanid[8];
02319         char var_bankid[4];
02320         char var_spantsid[12];
02321         const char *voicelib;
02322         char *dtmf_digits;              // dtmf sym space;
02323         unsigned digit_count, ring_count;
02324 
02325         State state;
02326 
02327 public:
02335         BayonneSession(BayonneDriver *driver, timeslot_t timeslot, BayonneSpan *span = NULL);
02336 
02340         virtual ~BayonneSession();
02341 
02342         inline const char *defVoicelib(void)
02343                 {return voicelib;}
02344 
02348         void initialevent(void);
02349 
02353         void initialize(void);
02354 
02358         void detach(void);
02359 
02365         inline BayonneDriver *getDriver(void)
02366                 {return driver;}
02367 
02374         virtual timeout_t getRemaining(void) = 0;
02375 
02382         virtual void startTimer(timeout_t timer) = 0;
02383 
02388         virtual void stopTimer(void) = 0;
02389 
02395         virtual void setOffhook(bool state);
02396 
02400         virtual void makeIdle(void);
02401 
02407         void part(event_t reason);
02408 
02415         virtual bool postEvent(Event *event);
02416 
02417 
02418         bool matchLine(Line *line);
02419 
02425         virtual void queEvent(Event *event);
02426 
02430         virtual void startThread(void);
02431 
02435         virtual void enterThread(ScriptThread *thr);
02436 
02440         virtual void exitThread(const char *msg);
02441 
02445         virtual void clrAudio(void);
02446 
02450         virtual timeout_t getToneFraming(void);
02451 
02455         virtual const char *audioEncoding(void);
02456 
02460         virtual const char *audioExtension(void);
02461         
02465         virtual timeout_t audioFraming(void);
02466 
02473         const char *getAudio(bool live = true);
02474 
02479         void branching(void);
02480 
02486         inline bool isOffhook(void)
02487                 {return offhook;};
02488 
02494         inline interface_t getInterface(void)
02495                 {return iface;};
02496 
02502         inline bridge_t getBridge(void)
02503                 {return bridge;};
02504 
02510         inline calltype_t getType(void)
02511                 {return type;};
02512 
02518         inline timeslot_t getSlot(void)
02519                 {return timeslot;};
02520 
02526         inline bool isIdle(void)
02527                 {return isAvail;};
02528 
02534         bool isJoined(void);
02535 
02541         timeout_t getJoinTimer(void);
02542 
02549         inline bool signalScript(signal_t signal)
02550                 {return ScriptInterp::signal(signal);};
02551 
02557         virtual bool peerLinear(void);
02558 
02568         virtual bool peerAudio(Audio::Encoded encoded);
02569 
02579         virtual bool setPeering(Audio::Encoding encoding, timeout_t framing);
02580 
02581         const char *getKeyString(const char *id);
02582         bool getKeyBool(const char *id);
02583         long getKeyValue(const char *id);
02584         timeout_t getSecTimeout(const char *id);
02585         timeout_t getMSecTimeout(const char *id);
02586         timeout_t getTimeoutValue(const char *opt = NULL);
02587         timeout_t getTimeoutKeyword(const char *kw);
02588         const char *getExitKeyword(const char *def);
02589         const char *getMenuKeyword(const char *def);
02590 
02591         unsigned getInputCount(const char *digits, unsigned max);
02592 
02600         uint32 newTid(void);
02601 
02607         inline const char *getTid(void)
02608                 {return var_tid;};
02609 
02616         bool digitEvent(const char *event);
02617 
02618         inline bool stringEvent(const char *evt)
02619                 {return scriptEvent(evt);}
02620 
02626         char getDigit(void);
02627 
02628         BayonneAudio audio;
02629 };
02630 
02639 class __EXPORT BayonneService : public Thread                
02640 {          
02641 private:            
02642         static BayonneService *first;
02643         static BayonneService *last;
02644         BayonneService *next;
02645         friend void startServices(void);        
02646         friend void stopServices(void);
02647 
02648 protected:
02649         BayonneService(int pri, size_t stack);
02650 
02654         virtual void stopService(void);
02655 
02659         virtual void startService(void);
02660 
02664         virtual void detachCall(BayonneSession *s);
02665 
02669         virtual void activeCall(BayonneSession *s);
02670 
02674         virtual void notifyCall(BayonneSession *s);
02675 
02676 public:
02677         static void start(void);
02678         static void stop(void); 
02679         static void detach(BayonneSession *s);
02680         static void active(BayonneSession *s);
02681         static void notify(BayonneSession *s);
02682 };
02683 
02684 } // namespace
02685 
02686 #endif