RTS API Documentation  1.10.11
switch_cpp.h
Go to the documentation of this file.
1 #ifndef SWITCH_CPP_H
2 #define SWITCH_CPP_H
3 
4 
5 #ifdef __cplusplus
6 extern "C" {
7 #endif
8 #ifdef DOH
9 }
10 #endif
11 #include <switch.h>
12 #define this_check(x) do { if (!this) { switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "object is not initalized\n"); return x;}} while(0)
13 #define this_check_void() do { if (!this) { switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "object is not initalized\n"); return;}} while(0)
14 #define sanity_check(x) do { if (!(session && allocated)) { switch_log_printf(SWITCH_CHANNEL_UUID_LOG(uuid),SWITCH_LOG_ERROR, "session is not initalized\n"); return x;}} while(0)
15 #define sanity_check_noreturn do { if (!(session && allocated)) { switch_log_printf(SWITCH_CHANNEL_UUID_LOG(uuid),SWITCH_LOG_ERROR, "session is not initalized\n"); return;}} while(0)
16 #define init_vars() allocated = 0; \
17  session = NULL; \
18  channel = NULL; \
19  uuid = NULL; \
20  tts_name = NULL; \
21  voice_name = NULL; \
22  xml_cdr_text = NULL; \
23  memset(&args, 0, sizeof(args)); \
24  ap = NULL; \
25  flags = 0; \
26  on_hangup = NULL; \
27  memset(&cb_state, 0, sizeof(cb_state)); \
28  hook_state = CS_NEW; \
29  fhp = NULL; \
30  cause = SWITCH_CAUSE_NONE
31 
32 //// C++ Interface: switch_to_cpp_mempool//// Description: This class allows for overloading the new operator to allocate from a switch_memory_pool_t//// Author: Yossi Neiman <freeswitch@cartissolutions.com>, (C) 2007//// Copyright: See COPYING file that comes with this distribution//
33 #if 0
34 #ifndef SWITCHTOMEMPOOL
35 #define SWITCHTOMEMPOOL
36 class SwitchToMempool {
37  public:
38  SwitchToMempool() {
39  } SwitchToMempool(switch_memory_pool_t *mem) {
40  memorypool = mem;
41  }
42  void *operator new(switch_size_t num_bytes, switch_memory_pool_t *mem) {
43  void *ptr = switch_core_alloc(mem, (switch_size_t) num_bytes);
44  return ptr;
45  }
46  protected:
47  switch_memory_pool_t *memorypool;
48 };
49 #endif
50 #endif
51 
52 /*
53 
54 Overview: once you create an object that inherits this class, since
55  the memory pool is then a class data member, you can continue to
56  allocate objects from the memory pool.
57  objects from within the class
58 
59 Notes on usage:
60 
61 1. The derived class will need to also overload the ctor so that it accepts a memory pool object as a parameter.
62 2. Instantiation of a class would then look something like this: Foo *bar = new(memory_pool) Foo(memory_pool);
63 
64 Note that the first parameter to the new operator is implicitly handled by c++... not sure I like that but it's how it is...
65 
66 */
67 
68 
69 SWITCH_DECLARE(void) setGlobalVariable(char *var_name, char *var_val);
70 SWITCH_DECLARE(char *) getGlobalVariable(char *var_name);
71 
72 SWITCH_DECLARE(void) consoleLog(char *level_str, char *msg);
73 SWITCH_DECLARE(void) consoleLog2(char *level_str, char *file, char *func, int line, char *msg);
74 SWITCH_DECLARE(void) consoleCleanLog(char *msg);
75 SWITCH_DECLARE(bool) running(void);
76 
77 SWITCH_DECLARE(bool) email(char *to, char *from, char *headers = NULL, char *body = NULL,
78  char *file = NULL, char *convert_cmd = NULL, char *convert_ext = NULL);
79 
80 
81  class CoreSession;
82 
83  class IVRMenu {
84  protected:
87  public:
89  const char *name,
90  const char *greeting_sound,
91  const char *short_greeting_sound,
92  const char *invalid_sound,
93  const char *exit_sound,
94  const char *transfer_sound,
95  const char *confirm_macro,
96  const char *confirm_key,
97  const char *tts_engine,
98  const char *tts_voice,
99  int confirm_attempts, int inter_timeout, int digit_len,
100  int timeout, int max_failures, int max_timeouts);
102  SWITCH_DECLARE(void) bindAction(char *action, const char *arg, const char *bind);
103  SWITCH_DECLARE(void) execute(CoreSession * session, const char *name);
104  };
105 
106 
107  class API {
108  protected:
109  char time_buf[64];
111  public:
113  virtual SWITCH_DECLARE_CONSTRUCTOR ~ API();
114  SWITCH_DECLARE(const char *) execute(const char *command, const char *data = NULL);
115  SWITCH_DECLARE(const char *) executeString(const char *command);
116  SWITCH_DECLARE(char *) getTime(void);
117  };
118 
119 
120  typedef struct input_callback_state {
121  void *function; // pointer to the language specific callback function
122  // eg, PyObject *pyfunc
123  void *threadState; // pointer to the language specific thread state
124  // eg, PyThreadState *threadState
125  void *extra; // currently used to store a switch_file_handle_t
126  char *funcargs; // extra string that will be passed to callback function
128 
129  typedef enum {
130  S_HUP = (1 << 0),
131  S_FREE = (1 << 1),
132  S_RDLOCK = (1 << 2)
133  } session_flag_t;
134 
135  class DTMF {
136  public:
137  char digit;
138  uint32_t duration;
139  SWITCH_DECLARE_CONSTRUCTOR DTMF(char idigit, uint32_t iduration = SWITCH_DEFAULT_DTMF_DURATION);
140  virtual SWITCH_DECLARE_CONSTRUCTOR ~DTMF();
141  };
142 
143  class Stream {
144  protected:
147  int mine;
148  public:
152  SWITCH_DECLARE(const char *) read(int *len);
153  SWITCH_DECLARE(void) write(const char *data);
154  SWITCH_DECLARE(void) raw_write(const char *data, int len);
155  SWITCH_DECLARE(const char *) get_data(void);
156  };
157 
158  class Event {
159  protected:
160  public:
163  int mine;
164 
165  SWITCH_DECLARE_CONSTRUCTOR Event(const char *type, const char *subclass_name = NULL);
166  SWITCH_DECLARE_CONSTRUCTOR Event(switch_event_t *wrap_me, int free_me = 0);
168  SWITCH_DECLARE(int) chat_execute(const char *app, const char *data = NULL);
169  SWITCH_DECLARE(int) chat_send(const char *dest_proto = NULL);
170  SWITCH_DECLARE(const char *) serialize(const char *format = NULL);
171  SWITCH_DECLARE(bool) setPriority(switch_priority_t priority = SWITCH_PRIORITY_NORMAL);
172  SWITCH_DECLARE(const char *) getHeader(const char *header_name);
173  SWITCH_DECLARE(char *) getBody(void);
174  SWITCH_DECLARE(const char *) getType(void);
175  SWITCH_DECLARE(bool) addBody(const char *value);
176  SWITCH_DECLARE(bool) addHeader(const char *header_name, const char *value);
177  SWITCH_DECLARE(bool) delHeader(const char *header_name);
178  SWITCH_DECLARE(bool) fire(void);
179  SWITCH_DECLARE(bool) merge(Event *to_merge);
180 
181  };
182 
184  protected:
186  int ready;
187  public:
190  char *e_callback;
192  char *e_cb_arg;
194  uint32_t node_index;
195 
196  SWITCH_DECLARE_CONSTRUCTOR EventConsumer(const char *event_name = NULL, const char *subclass_name = "", int len = 5000);
198  SWITCH_DECLARE(int) bind(const char *event_name, const char *subclass_name = "");
199  SWITCH_DECLARE(Event *) pop(int block = 0, int timeout = 0);
200  SWITCH_DECLARE(void) cleanup();
201  };
202 
203 #ifdef SWIG
204  class CoreSession {
205 #else
207 #endif
208  protected:
209  switch_input_args_t args; // holds ptr to cb function and input_callback_state struct
210  // which has a language specific callback function
211  switch_input_args_t *ap; // ptr to args .. (is this really needed?)
212  // instead set them here first
214  void store_file_handle(switch_file_handle_t *fh);
215  void *on_hangup; // language specific callback function, cast as void *
217  char dtmf_buf[512];
218 
219  public:
221  SWITCH_DECLARE_CONSTRUCTOR CoreSession(char *nuuid, CoreSession *a_leg = NULL);
226  unsigned int flags;
228  input_callback_state cb_state; // callback state, always pointed to by the buf
229  // field in this->args
230  switch_channel_state_t hook_state; // store hookstate for on_hangup callback
232 
233  char *uuid;
234  char *tts_name;
235  char *voice_name;
236 
237  SWITCH_DECLARE(int) insertFile(const char *file, const char *insert_file, int sample_point);
238  SWITCH_DECLARE(int) answer();
239  SWITCH_DECLARE(int) print(char *txt);
240  SWITCH_DECLARE(int) preAnswer();
241  SWITCH_DECLARE(void) hangup(const char *cause = "normal_clearing");
242  SWITCH_DECLARE(void) hangupState(void);
243  SWITCH_DECLARE(void) setVariable(char *var, char *val);
244  SWITCH_DECLARE(void) setPrivate(char *var, void *val);
245  SWITCH_DECLARE(void *) getPrivate(char *var);
246  SWITCH_DECLARE(const char *) getVariable(char *var);
247  SWITCH_DECLARE(switch_status_t) process_callback_result(char *result);
248  SWITCH_DECLARE(void) say(const char *tosay, const char *module_name, const char *say_type, const char *say_method,
249  const char *say_gender = NULL);
250  SWITCH_DECLARE(void) sayPhrase(const char *phrase_name, const char *phrase_data = "", const char *phrase_lang = NULL);
251  SWITCH_DECLARE(const char *) hangupCause();
252  SWITCH_DECLARE(const char *) getState();
253 
254  /** \brief Record to a file
255  * \param file_name
256  * \param <[max_len]> maximum length of the recording in seconds
257  * \param <[silence_threshold]> energy level audio must fall below
258  * to be considered silence (500 is a good starting point).
259  * \param <[silence_secs]> seconds of silence to interrupt the record.
260  */
261  SWITCH_DECLARE(int) recordFile(char *file_name, int time_limit = 0, int silence_threshold = 0, int silence_hits = 0);
262 
263  /** \brief Set attributes of caller data for purposes of outgoing calls
264  * \param var - the variable name, eg, "caller_id_name"
265  * \param val - the data to set, eg, "bob"
266  */
267 
268  /** \brief Originate a call to a destination
269  *
270  * \param a_leg_session - the session where the call is originating from
271  * and also the session in which _this_ session was
272  * created
273  * \param dest - a string representing destination, eg, sofia/mydomain.com/foo\@bar.com
274  * \param timeout - time to wait for call to be answered
275  * \return an int status code indicating success or failure
276  *
277  */
278  SWITCH_DECLARE(int) originate(CoreSession * a_leg_session, char *dest, int timeout = 60, switch_state_handler_table_t *handlers = NULL);
279 
280 
281  SWITCH_DECLARE(virtual void) destroy(void);
282 
283  /** \brief set a DTMF callback function
284  *
285  * The DTMF callback function will be set and persist
286  * for the life of the session, and be called when a dtmf
287  * is pressed by user during streamfile(), collectDigits(), and
288  * certain other methods are executing.
289  *
290  */
291  SWITCH_DECLARE(void) setDTMFCallback(void *cbfunc, char *funcargs);
292 
293  SWITCH_DECLARE(int) speak(char *text);
294  SWITCH_DECLARE(void) set_tts_parms(char *tts_name, char *voice_name);
295  SWITCH_DECLARE(void) set_tts_params(char *tts_name, char *voice_name);
296 
297  /**
298  * For timeout milliseconds, call the dtmf function set previously
299  * by setDTMFCallback whenever a dtmf or event is received
300  */
301  SWITCH_DECLARE(int) collectDigits(int abs_timeout);
302  SWITCH_DECLARE(int) collectDigits(int digit_timeout, int abs_timeout);
303 
304  /**
305  * Collect up to maxdigits digits worth of digits
306  * and store them in dtmf_buf. In the case of mod_python, the
307  * dtmf_buf parameter is configured to act as a _return_ value,
308  * (see mod_python.i). This does NOT call any callbacks upon
309  * receiving dtmf digits. For that, use collectDigits.
310  */
311  SWITCH_DECLARE(char *) getDigits(int maxdigits, char *terminators, int timeout);
312  SWITCH_DECLARE(char *) getDigits(int maxdigits, char *terminators, int timeout, int interdigit);
313  SWITCH_DECLARE(char *) getDigits(int maxdigits, char *terminators, int timeout, int interdigit, int abstimeout);
314  SWITCH_DECLARE(int) transfer(char *extension, char *dialplan = NULL, char *context = NULL);
315 
316 
317  SWITCH_DECLARE(char *) read(int min_digits, int max_digits,
318  const char *prompt_audio_file, int timeout, const char *valid_terminators, int digit_timeout = 0);
319 
320 
321  SWITCH_DECLARE(void) detectSpeech(char *arg0, char *arg1 = NULL, char *arg2 = NULL, char *arg3 = NULL);
322 
323 
324  /** \brief Play a file into channel and collect dtmfs
325  *
326  * See API docs in switch_ivr.h: switch_play_and_get_digits(..)
327  *
328  * NOTE: this does not call any dtmf callbacks set by
329  * setDTMFCallback(..) as it uses its own internal callback
330  * handler.
331  */
332  SWITCH_DECLARE(char *) playAndGetDigits(int min_digits,
333  int max_digits,
334  int max_tries,
335  int timeout, char *terminators, char *audio_files, char *bad_input_audio_files,
336  char *digits_regex, const char *var_name = NULL, int digit_timeout = 0,
337  const char *transfer_on_failure = NULL);
338 
339 
340  /** \brief Play a file and detect speech
341  * See API docs switch_ivr_play_and_detect_speech(...)
342  */
343 
344  SWITCH_DECLARE(char *) playAndDetectSpeech(char *file, char *engine, char *grammar);
345 
346 
347  /** \brief Play a file that resides on disk into the channel
348  *
349  * \param file - the path to the .wav/.mp3 to be played
350  * \param starting_sample_count - the index of the sample to
351  * start playing from
352  * \return an int status code indicating success or failure
353  *
354  */
355  SWITCH_DECLARE(int) streamFile(char *file, int starting_sample_count = 0);
356  SWITCH_DECLARE(int) sleep(int ms, int sync=0);
357 
358  /** \brief flush any pending events
359  */
360  SWITCH_DECLARE(int) flushEvents();
361 
362  /** \brief flush any pending digits
363  */
364  SWITCH_DECLARE(int) flushDigits();
365 
366  SWITCH_DECLARE(int) setAutoHangup(bool val);
367 
368  /** \brief Set the hangup callback function
369  * \param hangup_func - language specific function ptr cast into void *
370  */
371  SWITCH_DECLARE(void) setHangupHook(void *hangup_func);
372 
373  SWITCH_DECLARE(bool) ready();
374  SWITCH_DECLARE(bool) bridged();
375  SWITCH_DECLARE(bool) answered();
376  SWITCH_DECLARE(bool) mediaReady();
377 
378  SWITCH_DECLARE(void) waitForAnswer(CoreSession *calling_session);
379 
380  SWITCH_DECLARE(void) execute(const char *app, const char *data = NULL);
381 
382  SWITCH_DECLARE(void) sendEvent(Event * sendME);
383 
384  SWITCH_DECLARE(void) setEventData(Event * e);
385  SWITCH_DECLARE(char *) getXMLCDR();
386 
387  virtual bool begin_allow_threads() = 0;
388  virtual bool end_allow_threads() = 0;
389 
390  /** \brief Get the uuid of this session
391  * \return the uuid of this session
392  */
393  const char *get_uuid() const {
394  return uuid ? uuid : (char *) "uninitialized";
395  };
396 
397  /** \brief Get the callback function arguments associated with this session
398  * \return a const reference to the callback function arguments
399  */
401  return args;
402  };
403 
404  /** \brief Callback to the language specific hangup callback
405  */
406  virtual void check_hangup_hook() = 0;
407 
408  virtual switch_status_t run_dtmf_callback(void *input, switch_input_type_t itype) = 0;
409 
410  SWITCH_DECLARE(void) consoleLog(char *level_str, char *msg);
411  SWITCH_DECLARE(void) consoleLog2(char *level_str, char *file, char *func, int line, char *msg);
412  };
413 
414 
415 /* ---- functions not bound to CoreSession instance ----- */
416 
417 SWITCH_DECLARE(void) console_log(char *level_str, char *msg);
418 SWITCH_DECLARE(void) console_log2(char *level_str, char *file, char *func, int line, char *msg);
419 SWITCH_DECLARE(void) console_clean_log(char *msg);
420 SWITCH_DECLARE(void) switch_msleep(unsigned ms);
421 
422 /** \brief bridge the audio of session_b into session_a
423  *
424  * NOTE: the stuff regarding the dtmf callback might be completely
425  * wrong and has not been reviewed or tested
426  */
427 SWITCH_DECLARE(void) bridge(CoreSession & session_a, CoreSession & session_b);
428 
429 
430 /** \brief the actual hangup hook called back by freeswitch core
431  * which in turn gets the session and calls the appropriate
432  * instance method to complete the callback.
433  */
435 
437  void *input, switch_input_type_t itype, void *buf, unsigned int buflen);
438 
439 
440 
441 #ifdef __cplusplus
442 }
443 #endif
444 
445 #endif
446 /* For Emacs:
447  * Local Variables:
448  * mode:c
449  * indent-tabs-mode:t
450  * tab-width:4
451  * c-basic-offset:4
452  * End:
453  * For VIM:
454  * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
455  */
switch_event_types_t e_event_id
Definition: switch_cpp.h:189
int mine
Definition: switch_cpp.h:163
char * e_callback
Definition: switch_cpp.h:190
void console_clean_log(char *msg)
void console_log2(char *level_str, char *file, char *func, int line, char *msg)
switch_input_args_t * ap
Definition: switch_cpp.h:211
switch_event_types_t
Built-in Events.
cJSON *const to
switch_priority_t
Priority Indication.
Representation of an event.
Definition: switch_event.h:80
char * e_cb_arg
Definition: switch_cpp.h:192
switch_channel_t * channel
Definition: switch_cpp.h:225
const char *const const char *const const cJSON *const value
switch_status_t dtmf_callback(switch_core_session_t *session, void *input, switch_input_type_t itype, void *buf, unsigned int buflen)
switch_stream_handle_t mystream
Definition: switch_cpp.h:145
struct input_callback_state input_callback_state_t
#define SWITCH_DEFAULT_DTMF_DURATION
Definition: switch_types.h:116
char const int const cJSON_bool format
Definition: switch_cJSON.h:153
A node to store binded events.
Definition: switch_event.c:48
switch_stream_handle_t * stream_p
Definition: switch_cpp.h:146
unsigned int flags
Definition: switch_cpp.h:226
switch_input_type_t
switch_core_session_t * session
Definition: switch_cpp.h:224
int mine
Definition: switch_cpp.h:147
const char * get_uuid() const
Get the uuid of this session.
Definition: switch_cpp.h:393
switch_event_t * event
Definition: switch_cpp.h:161
int main(int argc, char *argv[])
Definition: fs_encode.c:56
char * getGlobalVariable(char *var_name)
void console_log(char *level_str, char *msg)
switch_byte_t switch_byte_t * buf
switch_status_t hanguphook(switch_core_session_t *session)
the actual hangup hook called back by freeswitch core which in turn gets the session and calls the ap...
char * uuid
Definition: switch_cpp.h:233
void * on_hangup
Definition: switch_cpp.h:215
void consoleCleanLog(char *msg)
void consoleLog(char *level_str, char *msg)
#define SWITCH_DECLARE_CLASS
char * e_subclass_name
Definition: switch_cpp.h:191
switch_file_handle_t * fhp
Definition: switch_cpp.h:216
#define switch_core_alloc(_pool, _mem)
Allocate memory directly from a memory pool.
Definition: switch_core.h:684
char * xml_cdr_text
Definition: switch_cpp.h:213
void setGlobalVariable(char *var_name, char *var_val)
#define SWITCH_DECLARE_CONSTRUCTOR
session_flag_t
Definition: switch_cpp.h:129
static unsigned char * print(const cJSON *const item, cJSON_bool format, const internal_hooks *const hooks)
Definition: cJSON.c:1094
switch_memory_pool_t * pool
Definition: switch_cpp.h:86
uintptr_t switch_size_t
char digit
Definition: switch_cpp.h:137
void bridge(CoreSession &session_a, CoreSession &session_b)
bridge the audio of session_b into session_a
char * serialized_string
Definition: switch_cpp.h:162
switch_byte_t switch_byte_t uint32_t buflen
switch_call_cause_t cause
Definition: switch_cpp.h:231
switch_channel_state_t hook_state
Definition: switch_cpp.h:230
uint32_t node_index
Definition: switch_cpp.h:194
switch_call_cause_t
#define SWITCH_DECLARE_NONSTD(type)
bool email(char *to, char *from, char *headers=NULL, char *body=NULL, char *file=NULL, char *convert_cmd=NULL, char *convert_ext=NULL)
switch_channel_state_t
Channel States (these are the defaults, CS_SOFT_EXECUTE, CS_EXCHANGE_MEDIA, and CS_CONSUME_MEDIA are ...
uint32_t duration
Definition: switch_cpp.h:138
void consoleLog2(char *level_str, char *file, char *func, int line, char *msg)
switch_status_t
Common return values.
switch_ivr_menu_t * menu
Definition: switch_cpp.h:85
switch_queue_t * events
Definition: switch_cpp.h:188
const switch_input_args_t & get_cb_args() const
Get the callback function arguments associated with this session.
Definition: switch_cpp.h:400
char * voice_name
Definition: switch_cpp.h:235
Main Library Header.
switch_core_session_t * session
Definition: switch_cpp.h:110
#define SWITCH_DECLARE(type)
bool running(void)
switch_input_args_t args
Definition: switch_cpp.h:209
switch_memory_pool_t * pool
Definition: switch_cpp.h:185
struct fspr_pool_t switch_memory_pool_t
const char *const name
Definition: switch_cJSON.h:250
void switch_msleep(unsigned ms)
input_callback_state cb_state
Definition: switch_cpp.h:228
char * tts_name
Definition: switch_cpp.h:234