38 #ifndef CCXX_BAYONNE_H_
39 #define CCXX_BAYONNE_H_
41 #ifndef CCXX_SCRIPT3_H_
42 #include <cc++/script3.h>
45 #ifndef CCXX_CCAUDIO2_H_
46 #include <cc++/audio2.h>
49 #ifndef CCXX_SOCKET_H_
50 #include <cc++/socket.h>
53 #define BAYONNE_RELEASE 1 // release check sequence
54 #define NO_TIMESLOT 0xffff
57 #define MAX_LIBINPUT 256
58 #define MAX_PATHNAME 256
59 #define MIN_AUDIOFEED (120 * 8) // 120 millisecond starting
60 #define MAX_AUDIOFEED (600 * 8) // 600 millisecond buffer
61 #define RPC_MAX_PARAMS 96
64 #define PFD_INVALID INVALID_HANDLE_VALUE
66 #define PFD_INVALID -1
69 #if SCRIPT_RIPPLE_LEVEL < 2
70 #error "ccscript 3 0.8.0 or later required"
74 #define SLOG_DEBUG(x, ...) slog.debug(x, __VA_ARGS__)
76 #define SLOG_DEBUG(x, ...)
110 StreamingBuffer(
const char *
id, timeout_t size = 600, Rate rate = rate8khz);
113 virtual Linear putBuffer(timeout_t duration);
114 virtual void clearBuffer(timeout_t duration);
131 virtual bool isActive(
void);
139 virtual unsigned long getPosition(timeout_t framing);
148 virtual Linear getBuffer(
unsigned long *mark, timeout_t duration);
162 StaticKeydata(
const char *path, Keydata::Define *defkeys = NULL,
const char *homepath = NULL);
164 inline const char *getString(
const char *
id)
165 {
return getLast(
id);};
167 inline long getValue(
const char *
id)
168 {
return getLong(
id);};
170 inline bool getBoolean(
const char *
id)
171 {
return getBool(
id);};
191 const char *homepath;
193 Keydata::Define *defkeys;
194 void loadConfig(
void);
197 virtual void updateConfig(Keydata *keydata);
200 DynamicKeydata(
const char *keypath, Keydata::Define *def = NULL,
const char *homepath = NULL);
202 const char *getString(
const char *key,
char *buf,
size_t size);
203 long getValue(
const char *key);
204 bool isKey(
const char *key);
205 bool getBoolean(
const char *key);
207 static void reload(
void);
222 const char *updatedString(
const char *
id);
223 long updatedValue(
const char *
id);
224 timeout_t updatedSecTimer(
const char *
id);
225 timeout_t updatedMsecTimer(
const char *
id);
226 bool updatedBoolean(
const char *
id);
229 inline const char *getInitial(
const char *
id)
231 inline void setInitial(
const char *
id,
const char *val)
232 {StaticKeydata::setValue(
id, val);};
236 const char *getString(
const char *key,
char *buf,
size_t size);
237 timeout_t getSecTimer(
const char *key);
238 timeout_t getMsecTimer(
const char *key);
239 long getValue(
const char *key);
240 bool isKey(
const char *key);
241 bool getBoolean(
const char *key);
254 static SOCKET trap_so4;
255 static unsigned trap_count4;
256 static struct sockaddr_in trap_addr4[8];
259 static SOCKET trap_so6;
260 static unsigned trap_count6;
261 static struct sockaddr_in6 trap_addr6[8];
265 static char dtmf_keymap[256];
267 static void snmptrap(
unsigned id,
const char *descr = NULL);
402 STATE_SUSPEND = STATE_DOWN,
403 STATE_STANDBY = STATE_DOWN,
404 STATE_LIBEXEC = STATE_EXEC,
405 STATE_RINGING = STATE_RING,
406 STATE_RUNNING = STATE_STEP,
407 STATE_THREADING = STATE_THREAD
434 SIGNAL_OVERRIDE = SIGNAL_A,
436 SIGNAL_FLASH = SIGNAL_B,
438 SIGNAL_IMMEDIATE = SIGNAL_C,
440 SIGNAL_PRIORITY = SIGNAL_D,
456 SIGNAL_HANGUP = SIGNAL_EXIT
516 START_SCRIPT = START_INCOMING,
517 START_SELECTED = START_OUTGOING,
518 START_TRANSFER = START_REFER,
570 CALL_HOLDING=CALL_HOLD,
615 DIAL_MACHINE = DIAL_PAM,
641 DTMF_GENTONE = DTMF_GENUP,
670 ENTER_RESUME = MAKE_UP,
671 ENTER_SUSPEND = MAKE_DOWN,
679 DRIVER_SPECIFIC = 1000
691 RESULT_BADPATH = 254,
832 static void detach(
Ring *list);
844 static unsigned long stamp;
849 static inline unsigned long getStamp(
void)
852 volatile unsigned long iCount, oCount;
880 static unsigned count;
892 inline const char *getPrefix(
void)
898 static inline unsigned getCount(
void)
960 timeout_t
total, silence, intersilence;
972 const char *pathv[4];
989 unsigned count,
size, required;
1012 timeout_t
on, off, interdigit;
1016 unsigned char digits[64];
1070 static void allocate(
timeslot_t timeslots, ScriptCommand *pointer = NULL,
timeslot_t overdraft = 0);
1072 static const char *getRegistryId(
const char *
id);
1076 static Audio::Encoding getEncoding(
const char *cp);
1081 static void allocateLocal(
void);
1086 static void addConfig(
const char *cfgfile);
1094 void md5_hash(
char *out,
const char *source);
1099 static void waitLoaded(
void);
1106 static unsigned long uptime(
void);
1113 static ScriptCompiler *reload(
void);
1118 static void down(
void);
1126 static bool service(
const char *service);
1133 static const char *getRunLevel(
void);
1150 static ScriptImage **getLocalImage(
timeslot_t timeslot);
1164 const char *name,
const char *caller,
const char *display,
1166 const char *secret = NULL);
1185 static timeslot_t toTimeslot(
const char *
id);
1219 static Handler getState(
const char *name);
1227 static int getDigit(
char dtmf);
1235 static char getChar(
int dtmf);
1247 static bool matchDigits(
const char *digits,
const char *match,
bool partial =
false);
1254 static ScriptImage *useImage(
void);
1262 static void endImage(ScriptImage *image);
1270 static bool loadPlugin(
const char *path);
1278 static bool loadMonitor(
const char *path);
1286 static bool loadAudio(
const char *path);
1288 static void errlog(
const char *level,
const char *fmt, ...);
1290 static bool getUserdata(
void);
1292 static void addTrap4(
const char *addr);
1294 static void addTrap6(
const char *addr);
1319 } zeroconf_family_t;
1333 {
return zeroconf_first;};
1341 {
return zeroconf_next;};
1348 inline const char *getType(
void)
1349 {
return zeroconf_type;};
1356 inline tpport_t getPort(
void)
1357 {
return zeroconf_port;};
1360 {
return zeroconf_family;};
1379 BayonneConfig(
const char *
id, Keydata::Define *def,
const char *path);
1381 void setEnv(
const char *
id);
1383 static void rebuild(ScriptImage *img);
1400 static const char *getToken(
BayonneSession *s, Line *l,
unsigned *idx);
1401 static unsigned addItem(
BayonneSession *s,
unsigned count,
const char *text);
1456 virtual unsigned digits(
BayonneSession *sessiob,
unsigned count,
const char *
string);
1466 virtual unsigned spell(
BayonneSession *session,
unsigned count,
const char *
string);
1476 virtual unsigned sayorder(
BayonneSession *session,
unsigned count,
const char *
string);
1486 virtual unsigned number(
BayonneSession *session,
unsigned count,
const char *
string);
1497 virtual unsigned saynumber(
BayonneSession *session,
unsigned count,
const char *
string);
1507 virtual unsigned saycount(
BayonneSession *session,
unsigned count,
const char *
string);
1517 virtual unsigned sayhour(
BayonneSession *session,
unsigned count,
const char *
string);
1527 virtual unsigned saytime(
BayonneSession *session,
unsigned count,
const char *
string);
1537 virtual unsigned weekday(
BayonneSession *session,
unsigned count,
const char *
string);
1547 virtual unsigned sayday(
BayonneSession *session,
unsigned count,
const char *
string);
1557 virtual unsigned saydate(
BayonneSession *session,
unsigned count,
const char *
string);
1567 virtual unsigned saybool(
BayonneSession *session,
unsigned count,
const char *
string);
1577 virtual unsigned phone(
BayonneSession *session,
unsigned count,
const char *
string);
1587 virtual unsigned extension(
BayonneSession *session,
unsigned count,
const char *
string);
1596 virtual const char *speak(
BayonneSession *session, Line *line = NULL);
1603 inline const char *getId(
void)
1620 char *getContinuation(
void);
1640 const char *extension, *
voicelib, *libext, *prefixdir, *offset;
1643 char var_position[14];
1657 const char *getFilename(
const char *name,
bool write =
false);
1671 void play(
const char **list, Mode mode = modeRead);
1680 void record(
const char *name, Mode mode = modeCreate,
const char *annotation = NULL);
1688 const char *getVoicelib(
const char *iso);
1695 inline AudioCodec *getCodec(
void)
1747 void shutdown(
void);
1756 virtual timeout_t getTimeout(Event *event);
1760 size_t onWait(
void *buf);
1761 size_t onPost(
void *buf);
1762 size_t onPeek(
void *buf);
1799 timeout_t pickup_timer, hangup_timer,
seize_timer, ring_timer, hunt_timer;
1807 virtual void reloadDriver(
void);
1813 virtual void startDriver(
void);
1818 virtual void stopDriver(
void);
1824 void relistIdle(
void);
1835 virtual bool isAuthorized(
const char *userid,
const char *secret);
1837 virtual bool deregister(
const char *
id);
1838 virtual bool reregister(
const char *
id,
const char *uri,
const char *secret, timeout_t expires);
1851 BayonneDriver(Keydata::Define *pairs,
const char *key,
const char *
id,
bool virt =
false);
1861 static inline bool useProtocols(
void)
1867 static bool isStopping(
void)
1874 {
return trunkDriver;}
1880 {
return firstDriver;}
1889 static BayonneDriver *authorize(
const char *userid,
const char *secret);
1895 {
return nextDriver;};
1898 {
return firstDriver;};
1904 {
return protoDriver;}
1918 virtual bool suspend(
void);
1925 virtual bool resume(
void);
1930 virtual void reregister(
void);
1939 virtual const char *registerScript(ScriptImage *image, Line *line);
1948 virtual const char *assignScript(ScriptImage *image, Line *line);
1981 static BayonneDriver *loadProtocol(
const char *
id,
unsigned timeslots = 0);
1990 static unsigned list(
char **items,
unsigned max);
1995 static void start(
void);
2000 static void stop(
void);
2005 static void reload(
void);
2044 inline unsigned getSpanFirst(
void)
2052 inline unsigned getSpansUsed(
void)
2060 inline const char *getName(
void)
2069 inline timeout_t getResetTimer(
void)
2070 {
return reset_timer;};
2077 inline timeout_t getReleaseTimer(
void)
2078 {
return release_timer;};
2085 inline timeout_t getHangupTimer(
void)
2086 {
return hangup_timer;};
2093 inline timeout_t getPickupTimer(
void)
2094 {
return pickup_timer;};
2101 inline timeout_t getSeizeTimer(
void)
2102 {
return seize_timer;};
2109 inline timeout_t getHuntTimer(
void)
2110 {
return hunt_timer;};
2117 inline timeout_t getFlashTimer(
void)
2118 {
return flash_timer;};
2125 inline timeout_t getInterdigit(
void)
2126 {
return interdigit_timer;};
2135 inline timeout_t getRingTimer(
void)
2136 {
return ring_timer;};
2143 inline unsigned getAnswerCount(
void)
2144 {
return answer_count;};
2176 inline size_t getAudioStack(
void)
2177 {
return audio_stack;};
2184 inline int getAudioPriority(
void)
2185 {
return audio_priority;};
2192 inline Audio::Level getAudioLevel(
void)
2193 {
return audio_level;};
2200 inline void setLogging(std::ostream *output)
2201 {logevents = output;};
2209 inline bool isSpanable(
unsigned span);
2221 virtual bool getDestination(
const char *target,
const char *dial,
char *output,
size_t size);
2228 inline unsigned getAvail(
void)
2238 virtual bool isExternal(
const char *dest);
2246 virtual bool isRegistered(
const char *dest);
2254 virtual bool isAvailable(
const char *dest);
2263 virtual bool isReachable(
const char *proxy);
2274 virtual unsigned getRegistration(regauth_t *data,
unsigned count,
const char *
id = NULL);
2333 static void allocate(
unsigned total = 0);
2340 static inline unsigned getSpans(
void)
2364 inline unsigned getId(
void)
2380 inline unsigned getAvail(
void)
2381 {
return count - used;}
2388 virtual bool suspend(
void);
2395 virtual bool resume(
void);
2402 virtual bool reset(
void);
2417 class Image :
public ScriptImage
2420 inline Image() : ScriptImage(NULL, NULL) {};
2421 unsigned gatherPrefix(
const char *prefix,
const char **list,
unsigned max);
2425 virtual const char *submit(
const char **data);
2427 virtual ScriptCompiler *compiler(
void);
2429 virtual unsigned destinations(Image *img,
const char **array,
unsigned max);
2431 virtual bool isDestination(Image *img,
const char *name);
2435 bool scriptEvent(ScriptInterp *interp,
const char *evt);
2437 bool digitEvent(ScriptInterp *interp,
const char *evt);
2439 BayonneBinder(
const char *
id);
2445 virtual Name *getIncoming(ScriptImage *img,
BayonneSession *s, Event *event);
2448 static const char *submitRequest(
const char **data);
2450 static ScriptCompiler *getCompiler(
void);
2452 static unsigned gatherDestinations(ScriptImage *img,
const char **index,
unsigned max);
2454 static bool isDestination(
const char *name);
2493 bool offhook, dtmf, answered,
starting, holding, connecting, referring;
2510 bool requiresDTMF(
void);
2516 void enterCall(
void);
2525 void exitCall(
const char *reason);
2532 virtual bool enableDTMF(
void);
2537 virtual void disableDTMF(
void);
2547 virtual const char *checkAudio(
bool live);
2555 virtual bool filterPosting(
Event *event);
2557 virtual bool enterCommon(
Event *event);
2558 virtual bool enterInitial(
Event *event);
2559 virtual bool enterFinal(
Event *event);
2560 virtual bool enterIdle(
Event *event);
2561 virtual bool enterReset(
Event *event);
2562 virtual bool enterRelease(
Event *event);
2563 virtual bool enterRinging(
Event *event);
2564 virtual bool enterPickup(
Event *event);
2565 virtual bool enterAnswer(
Event *event);
2566 virtual bool enterSeize(
Event *event);
2567 virtual bool enterHunting(
Event *event);
2568 virtual bool enterHangup(
Event *event);
2569 virtual bool enterTone(
Event *event);
2570 virtual bool enterReconnect(
Event *event);
2571 virtual bool enterDTMF(
Event *event);
2572 virtual bool enterPlay(
Event *event);
2573 virtual bool enterRecord(
Event *event);
2574 virtual bool enterJoin(
Event *event);
2575 virtual bool enterWait(
Event *event);
2576 virtual bool enterDial(
Event *event);
2577 virtual bool enterBusy(
Event *event);
2578 virtual bool enterStandby(
Event *event);
2579 virtual bool enterXfer(
Event *event);
2580 virtual bool enterRefer(
Event *event);
2581 virtual bool enterHold(
Event *event);
2582 virtual bool enterRecall(
Event *event);
2590 void renameRecord(
void);
2591 bool stateInitial(
Event *event);
2592 bool stateFinal(
Event *event);
2593 bool stateIdle(
Event *event);
2594 bool stateIdleReset(
Event *event);
2595 bool stateReset(
Event *event);
2596 bool stateRelease(
Event *event);
2597 bool stateBusy(
Event *event);
2598 bool stateStandby(
Event *event);
2599 bool stateRinging(
Event *event);
2600 bool statePickup(
Event *event);
2601 bool stateAnswer(
Event *event);
2602 bool stateSeize(
Event *event);
2603 bool stateHunting(
Event *event);
2604 bool stateRunning(
Event *event);
2605 bool stateLibexec(
Event *event);
2606 bool stateLibreset(
Event *event);
2607 bool stateLibwait(
Event *event);
2608 bool stateWaitkey(
Event *event);
2609 bool stateThreading(
Event *event);
2610 bool stateHangup(
Event *event);
2611 bool stateCollect(
Event *event);
2612 bool stateSleep(
Event *event);
2613 bool stateStart(
Event *event);
2614 bool stateClear(
Event *event);
2615 bool stateInkey(
Event *event);
2616 bool stateInput(
Event *event);
2617 bool stateRead(
Event *event);
2618 bool stateDial(
Event *event);
2619 bool stateXfer(
Event *event);
2620 bool stateRefer(
Event *event);
2621 bool stateHold(
Event *event);
2622 bool stateRecall(
Event *event);
2623 bool stateTone(
Event *event);
2624 bool stateDTMF(
Event *event);
2625 bool statePlay(
Event *event);
2626 bool stateRecord(
Event *event);
2627 bool stateJoin(
Event *event);
2628 bool stateWait(
Event *event);
2629 bool stateConnect(
Event *event);
2630 bool stateReconnect(
Event *event);
2631 bool stateCalling(
Event *event);
2639 bool putEvent(
Event *event);
2646 void libWrite(
const char *
string);
2648 void libClose(
const char *
string);
2650 bool isLibexec(
const char *tsid);
2652 timeout_t getLibexecTimeout(
void);
2654 const char *getWritepath(
char *buf = NULL,
size_t len = 0);
2656 void incIncomingAttempts(
void);
2658 void incOutgoingAttempts(
void);
2660 void incIncomingComplete(
void);
2662 void incOutgoingComplete(
void);
2664 void incActiveCalls(
void);
2666 void decActiveCalls(
void);
2671 inline bool isDTMF(
void)
2674 inline bool isPeering(
void)
2675 {
return state.peering;};
2680 inline timeslot_t getTimeslot(
void)
2692 const char *getExternal(
const char *option);
2701 bool addSymbol(
const char *
id,
const char *value);
2709 bool clearSymbol(
const char *
id);
2716 inline uint16 getEventSequence(
void)
2719 static const char *getGlobal(
const char *
id);
2720 static bool setGlobal(
const char *
id,
const char *value);
2721 static bool sizeGlobal(
const char *
id,
unsigned size);
2722 static bool addGlobal(
const char *
id,
const char *value);
2723 static bool clearGlobal(
const char *
id);
2732 ScriptInterp *getInterp(
const char *
id);
2740 ScriptSymbols *getSymbols(
const char *
id);
2754 Name *attachStart(
Event *event);
2759 unsigned getId(
void);
2776 void setRunning(
void);
2784 bool setReconnect(
const char *enc, timeout_t framing);
2790 bool recallReconnect(
void);
2796 void setConnecting(
const char *evname = NULL);
2825 {
return translator;};
2830 inline void startConnecting(
void)
2831 {connecting =
true;};
2841 {
return state.libaudio;};
2846 void finalize(
void);
2857 char var_duration[12];
2858 char var_callid[12];
2862 char var_recall[16];
2863 char var_joined[16];
2865 char var_timeslot[8];
2868 char var_spantsid[12];
2891 const char *getDigits(
void);
2893 inline const char *defVoicelib(
void)
2896 inline const char *getSessionId(
void)
2899 inline const char *getSessionParent(
void)
2902 inline const char *getSessionJoined(
void)
2903 {
return var_joined;};
2905 inline time_t getSessionStart(
void)
2906 {
return starttime;};
2911 void initialevent(
void);
2916 void initialize(
void);
2926 inline time_t getJoined(
void)
2927 {
return time_joined;};
2943 virtual timeout_t getRemaining(
void) = 0;
2951 virtual void startTimer(timeout_t timer) = 0;
2957 virtual void stopTimer(
void) = 0;
2964 virtual void setOffhook(
bool state);
2969 virtual void makeIdle(
void);
2976 void part(event_t reason);
2984 virtual bool postEvent(Event *event);
2987 bool matchLine(Line *line);
2994 virtual void queEvent(Event *event);
2999 virtual void startThread(
void);
3004 virtual void enterThread(ScriptThread *thr);
3009 virtual void exitThread(
const char *msg);
3014 virtual void clrAudio(
void);
3019 virtual timeout_t getToneFraming(
void);
3024 virtual const char *audioEncoding(
void);
3029 virtual const char *audioExtension(
void);
3034 virtual timeout_t audioFraming(
void);
3042 const char *getAudio(
bool live =
true);
3048 void branching(
void);
3055 inline bool isOffhook(
void)
3063 inline interface_t getInterface(
void)
3071 inline bridge_t getBridge(
void)
3079 inline calltype_t getType(
void)
3087 inline timeslot_t getSlot(
void)
3095 inline bool isIdle(
void)
3110 bool isJoined(
void);
3117 bool isAssociated(
void);
3126 inline bool isHolding(
void)
3132 bool isDisconnecting(
void);
3139 timeout_t getJoinTimer(
void);
3147 bool signalScript(signal_t signal);
3154 virtual bool peerLinear(
void);
3165 virtual bool peerAudio(Audio::Encoded encoded);
3176 virtual bool setPeering(Audio::Encoding encoding, timeout_t framing);
3178 const char *getKeyString(
const char *
id);
3179 bool getKeyBool(
const char *
id);
3180 long getKeyValue(
const char *
id);
3181 timeout_t getSecTimeout(
const char *
id);
3182 timeout_t getMSecTimeout(
const char *
id);
3183 timeout_t getTimeoutValue(
const char *opt = NULL);
3184 timeout_t getTimeoutKeyword(
const char *kw);
3185 const char *getExitKeyword(
const char *def);
3186 const char *getMenuKeyword(
const char *def);
3188 unsigned getInputCount(
const char *digits,
unsigned max);
3197 uint32 newTid(
void);
3204 inline const char *getTid(
void)
3213 bool digitEvent(
const char *event);
3215 inline bool stringEvent(
const char *evt)
3216 {
return scriptEvent(evt);}
3223 char getDigit(
void);
3251 bool parseCall(
char *cp);
3280 inline unsigned getCount(
void)
3283 const char *getParamId(
unsigned short param,
unsigned short offset);
3284 const char *getIndexed(
unsigned short param,
unsigned short offset = 0);
3285 const char *getNamed(
unsigned short param,
const char *member);
3286 const char *getMapped(
const char *map,
const char *member);
3288 friend size_t xmlwrite(
char **buf,
size_t *max,
const char *fmt, ...);
3289 bool buildResponse(
const char *fmt, ...);
3290 void sendSuccess(
void);
3291 void sendFault(
int code,
const char *
string);
3293 inline void transportFault(
unsigned code,
const char *
string)
3294 {result.code = code; result.string = string;};
3296 bool invokeXMLRPC(
void);
3299 size_t xmlwrite(
char **buf,
size_t *max,
const char *fmt, ...);
3317 friend void startServices(
void);
3318 friend void stopServices(
void);
3326 virtual void stopService(
void);
3331 virtual void startService(
void);
3357 static void start(
void);
3362 static void stop(
void);
3390 virtual void releaseEngine(
void) = 0;
3396 virtual void disconnectEngine(
void) = 0;
3406 virtual bool stepEngine(
void) = 0;