1 /*
2  * Copyright (c) 2007-2014, Anthony Minessale II
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of the original author; nor the names of any contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER
25  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Contributors:
34  *
35  * Moises Silva <moy@sangoma.com>
36  *
37  */
38 
39 #ifndef FTDM_TYPES_H
40 #define FTDM_TYPES_H
41 
42 #include "freetdm.h"
43 
44 #include "fsk.h"
45 
46 #ifdef WIN32
47 typedef intptr_t ftdm_ssize_t;
48 typedef int ftdm_filehandle_t;
49 #else
50 #include <sys/types.h>
51 #include <sys/ioctl.h>
52 #include <stdarg.h>
53 typedef ssize_t ftdm_ssize_t;
54 typedef int ftdm_filehandle_t;
55 #endif
56 
57 #ifdef __cplusplus
58 extern "C" {
59 #endif
60 
61 #define FTDM_COMMAND_OBJ_SIZE *((ftdm_size_t *)obj)
62 #define FTDM_COMMAND_OBJ_INT *((int *)obj)
63 #define FTDM_COMMAND_OBJ_CHAR_P (char *)obj
64 #define FTDM_COMMAND_OBJ_FLOAT *(float *)obj
65 #define FTDM_FSK_MOD_FACTOR 0x10000
66 #define FTDM_DEFAULT_DTMF_ON 250
67 #define FTDM_DEFAULT_DTMF_OFF 50
68 
69 typedef enum {
70 	FTDM_ENDIAN_BIG = 1,
71 	FTDM_ENDIAN_LITTLE = -1
72 } ftdm_endian_t;
73 
74 typedef enum {
75 	FTDM_CID_TYPE_SDMF = 0x04,
76 	FTDM_CID_TYPE_MDMF = 0x80
77 } ftdm_cid_type_t;
78 
79 typedef enum {
80 	MDMF_DATETIME = 1,
81 	MDMF_PHONE_NUM = 2,
82 	MDMF_DDN = 3,
83 	MDMF_NO_NUM = 4,
84 	MDMF_PHONE_NAME = 7,
85 	MDMF_NO_NAME = 8,
86 	MDMF_ALT_ROUTE = 9,
87 	MDMF_INVALID = 10
88 } ftdm_mdmf_type_t;
89 #define MDMF_STRINGS "X", "DATETIME", "PHONE_NUM", "DDN", "NO_NUM", "X", "X", "PHONE_NAME", "NO_NAME", "ALT_ROUTE", "INVALID"
90 FTDM_STR2ENUM_P(ftdm_str2ftdm_mdmf_type, ftdm_mdmf_type2str, ftdm_mdmf_type_t)
91 
92 #define FTDM_TONEMAP_LEN 128
93 typedef enum {
94 	FTDM_TONEMAP_NONE,
95 	FTDM_TONEMAP_DIAL,
96 	FTDM_TONEMAP_RING,
97 	FTDM_TONEMAP_BUSY,
98 	FTDM_TONEMAP_FAIL1,
99 	FTDM_TONEMAP_FAIL2,
100 	FTDM_TONEMAP_FAIL3,
101 	FTDM_TONEMAP_ATTN,
102 	FTDM_TONEMAP_CALLWAITING_CAS,
103 	FTDM_TONEMAP_CALLWAITING_SAS,
104 	FTDM_TONEMAP_CALLWAITING_ACK,
105 	FTDM_TONEMAP_INVALID
106 } ftdm_tonemap_t;
107 #define TONEMAP_STRINGS "NONE", "DIAL", "RING", "BUSY", "FAIL1", "FAIL2", "FAIL3", "ATTN", "CALLWAITING-CAS", "CALLWAITING-SAS", "CALLWAITING-ACK", "INVALID"
108 FTDM_STR2ENUM_P(ftdm_str2ftdm_tonemap, ftdm_tonemap2str, ftdm_tonemap_t)
109 
110 typedef enum {
111 	FTDM_ANALOG_START_KEWL,
112 	FTDM_ANALOG_START_LOOP,
113 	FTDM_ANALOG_START_GROUND,
114 	FTDM_ANALOG_START_WINK,
115 	FTDM_ANALOG_START_NA
116 } ftdm_analog_start_type_t;
117 #define START_TYPE_STRINGS "KEWL", "LOOP", "GROUND", "WINK", "NA"
118 FTDM_STR2ENUM_P(ftdm_str2ftdm_analog_start_type, ftdm_analog_start_type2str, ftdm_analog_start_type_t)
119 
120 typedef enum {
121 	FTDM_OOB_NOOP,
122 	FTDM_OOB_ONHOOK,
123 	FTDM_OOB_OFFHOOK,
124 	FTDM_OOB_WINK,
125 	FTDM_OOB_FLASH,
126 	FTDM_OOB_RING_START,
127 	FTDM_OOB_RING_STOP,
128 	FTDM_OOB_ALARM_TRAP,
129 	FTDM_OOB_ALARM_CLEAR,
130 	FTDM_OOB_CAS_BITS_CHANGE,
131 	FTDM_OOB_POLARITY_REVERSE,
132 	FTDM_OOB_INVALID
133 } ftdm_oob_event_t;
134 #define OOB_STRINGS "NOOP", "ONHOOK", "OFFHOOK", "WINK", "FLASH", "RING_START", "RING_STOP", "ALARM_TRAP", "ALARM_CLEAR", "CAS_BITS_CHANGE", "POLARITY_REVERSE", "INVALID"
135 FTDM_STR2ENUM_P(ftdm_str2ftdm_oob_event, ftdm_oob_event2str, ftdm_oob_event_t)
136 
137 /*! \brief Event types */
138 typedef enum {
139 	FTDM_EVENT_NONE,
140 	/* DTMF digit was just detected */
141 	FTDM_EVENT_DTMF,
142 	/* Out of band event */
143 	FTDM_EVENT_OOB,
144 	FTDM_EVENT_COUNT
145 } ftdm_event_type_t;
146 
147 /*! \brief Generic event data type */
148 struct ftdm_event {
149 	ftdm_event_type_t e_type;
150 	uint32_t enum_id;
151 	ftdm_channel_t *channel;
152 	void *data;
153 };
154 
155 typedef enum {
156 	FTDM_SIGTYPE_NONE,
157 	FTDM_SIGTYPE_ISDN,
158 	FTDM_SIGTYPE_RBS,
159 	FTDM_SIGTYPE_ANALOG,
160 	FTDM_SIGTYPE_SANGOMABOOST,
161 	FTDM_SIGTYPE_M3UA,
162 	FTDM_SIGTYPE_M2UA,
163 	FTDM_SIGTYPE_R2,
164 	FTDM_SIGTYPE_SS7,
165 	FTDM_SIGTYPE_GSM
166 } ftdm_signal_type_t;
167 
168 typedef enum {
169 	FTDM_SPAN_CONFIGURED = (1 << 0),
170 	FTDM_SPAN_STARTED = (1 << 1),
171 	FTDM_SPAN_STATE_CHANGE = (1 << 2),
172 	FTDM_SPAN_SUSPENDED = (1 << 3),
173 	FTDM_SPAN_IN_THREAD = (1 << 4),
174 	FTDM_SPAN_STOP_THREAD = (1 << 5),
175 	/*! Signaling modules set this flag to use fchan->pendingchans queue instead
176 	 *  of the FTDM_SPAN_STATE_CHANGE flag to detect when there is channels with
177 	 *  a state change pending in the span. If you set this member you can't rely
178 	 *  on FTDM_SPAN_STATE_CHANGE anymore and must use the queue only instead. This
179 	 *  is the new way of detecting state changes, new modules should always set this
180 	 *  flag, the old modules still relying on FTDM_SPAN_STATE_CHANGE should be updated */
181 	FTDM_SPAN_USE_CHAN_QUEUE = (1 << 6),
182 	FTDM_SPAN_SUGGEST_CHAN_ID = (1 << 7),
183 	FTDM_SPAN_USE_AV_RATE = (1 << 8),
184 	FTDM_SPAN_PWR_SAVING = (1 << 9),
185 	/* If you use this flag, you MUST call ftdm_span_trigger_signals to deliver the user signals
186 	 * after having called ftdm_send_span_signal(), which with this flag it will just enqueue the signal
187 	 * for later delivery */
188 	FTDM_SPAN_USE_SIGNALS_QUEUE = (1 << 10),
189 	/* If this flag is set, channel will be moved to proceed state when calls goes to routing */
190 	FTDM_SPAN_USE_PROCEED_STATE = (1 << 11),
191 	/* If this flag is set, the signalling module supports jumping directly to state up, without
192 		going through PROGRESS/PROGRESS_MEDIA */
193 	FTDM_SPAN_USE_SKIP_STATES = (1 << 12),
194 	/* If this flag is set, then this span cannot be stopped individually, it can only be stopped
195 		on freetdm unload */
196 	FTDM_SPAN_NON_STOPPABLE = (1 << 13),
197 	/* If this flag is set, then this span supports TRANSFER state */
198 	FTDM_SPAN_USE_TRANSFER = (1 << 14),
199 	/* This is the last flag, no more flags bigger than this */
200 	FTDM_SPAN_MAX_FLAG = (1 << 15),
201 } ftdm_span_flag_t;
202 
203 /*! \brief Channel supported features */
204 typedef enum {
205 	FTDM_CHANNEL_FEATURE_DTMF_DETECT = (1 << 0), /*!< Channel can detect DTMF (read-only) */
206 	FTDM_CHANNEL_FEATURE_DTMF_GENERATE = (1 << 1), /*!< Channel can generate DTMF (read-only) */
207 	FTDM_CHANNEL_FEATURE_CODECS = (1 << 2), /*!< Channel can do transcoding (read-only) */
208 	FTDM_CHANNEL_FEATURE_INTERVAL = (1 << 3), /*!< Channel support i/o interval configuration (read-only) */
209 	FTDM_CHANNEL_FEATURE_CALLERID = (1 << 4), /*!< Channel can detect caller id (read-only) */
210 	FTDM_CHANNEL_FEATURE_PROGRESS = (1 << 5), /*!< Channel can detect inband progress (read-only) */
211 	FTDM_CHANNEL_FEATURE_CALLWAITING = (1 << 6), /*!< Channel will allow call waiting (ie: FXS devices) (read/write) */
212 	FTDM_CHANNEL_FEATURE_HWEC = (1<<7), /*!< Channel has a hardware echo canceller */
213 	FTDM_CHANNEL_FEATURE_HWEC_DISABLED_ON_IDLE  = (1<<8), /*!< hardware echo canceller is disabled when there are no calls on this channel */
214 	FTDM_CHANNEL_FEATURE_IO_STATS = (1<<9), /*!< Channel supports IO statistics (HDLC channels only) */
215 	FTDM_CHANNEL_FEATURE_MF_GENERATE = (1<<10), /*!< Channel can generate R2 MF tones (read-only) */
216 } ftdm_channel_feature_t;
217 
218 /*! \brief Channel IO pending flags */
219 typedef enum {
220 	FTDM_CHANNEL_IO_EVENT = (1 << 0),
221 	FTDM_CHANNEL_IO_READ = (1 << 1),
222 	FTDM_CHANNEL_IO_WRITE = (1 << 2),
223 } ftdm_channel_io_flags_t;
224 
225 /*!< Channel flags. This used to be an enum but we reached the 32bit limit for enums, is safer this way */
226 #define FTDM_CHANNEL_CONFIGURED    (1ULL << 0)
227 #define FTDM_CHANNEL_READY         (1ULL << 1)
228 #define FTDM_CHANNEL_OPEN          (1ULL << 2)
229 #define FTDM_CHANNEL_DTMF_DETECT   (1ULL << 3)
230 #define FTDM_CHANNEL_SUPRESS_DTMF  (1ULL << 4)
231 #define FTDM_CHANNEL_TRANSCODE     (1ULL << 5)
232 #define FTDM_CHANNEL_BUFFER        (1ULL << 6)
233 #define FTDM_CHANNEL_INTHREAD      (1ULL << 8)
234 #define FTDM_CHANNEL_WINK          (1ULL << 9)
235 #define FTDM_CHANNEL_FLASH         (1ULL << 10)
236 #define FTDM_CHANNEL_STATE_CHANGE  (1ULL << 11)
237 #define FTDM_CHANNEL_HOLD          (1ULL << 12)
238 #define FTDM_CHANNEL_INUSE         (1ULL << 13)
239 #define FTDM_CHANNEL_OFFHOOK       (1ULL << 14)
240 #define FTDM_CHANNEL_RINGING       (1ULL << 15)
241 #define FTDM_CHANNEL_PROGRESS_DETECT (1ULL << 16)
242 #define FTDM_CHANNEL_CALLERID_DETECT (1ULL << 17)
243 #define FTDM_CHANNEL_OUTBOUND        (1ULL << 18)
244 #define FTDM_CHANNEL_SUSPENDED       (1ULL << 19)
245 #define FTDM_CHANNEL_3WAY            (1ULL << 20)
246 #define FTDM_CHANNEL_PROGRESS        (1ULL << 21)
247 /*!< There is media on the channel already */
248 #define FTDM_CHANNEL_MEDIA           (1ULL << 22)
249 /*!< The channel was answered */
250 #define FTDM_CHANNEL_ANSWERED        (1ULL << 23)
251 #define FTDM_CHANNEL_MUTE            (1ULL << 24)
252 #define FTDM_CHANNEL_USE_RX_GAIN     (1ULL << 25)
253 #define FTDM_CHANNEL_USE_TX_GAIN     (1ULL << 26)
254 #define FTDM_CHANNEL_IN_ALARM        (1ULL << 27)
255 #define FTDM_CHANNEL_SIG_UP          (1ULL << 28)
256 #define FTDM_CHANNEL_USER_HANGUP     (1ULL << 29)
257 #define FTDM_CHANNEL_RX_DISABLED     (1ULL << 30)
258 #define FTDM_CHANNEL_TX_DISABLED     (1ULL << 31)
259 /*!< The user knows about a call in this channel */
260 #define FTDM_CHANNEL_CALL_STARTED    (1ULL << 32)
261 /*!< The user wants non-blocking operations in the channel */
262 #define FTDM_CHANNEL_NONBLOCK        (1ULL << 33)
263 /*!< There is a pending acknowledge for an indication */
264 #define FTDM_CHANNEL_IND_ACK_PENDING (1ULL << 34)
265 /*!< There is someone blocking in the channel waiting for state completion */
266 #define FTDM_CHANNEL_BLOCKING        (1ULL << 35)
267 /*!< Media is digital */
268 #define FTDM_CHANNEL_DIGITAL_MEDIA   (1ULL << 36)
269 /*!< Native signaling bridge is enabled */
270 #define FTDM_CHANNEL_NATIVE_SIGBRIDGE (1ULL << 37)
271 /*!< Native signaling DTMF detection */
272 #define FTDM_CHANNEL_SIG_DTMF_DETECTION (1ULL << 38)
273 
274 /*!< This no more flags after this flag */
275 #define FTDM_CHANNEL_MAX_FLAG 	     (1ULL << 39)
276 /*!<When adding a new flag, need to update ftdm_io.c:channel_flag_strs */
277 
278 #include "ftdm_state.h"
279 
280 typedef enum ftdm_channel_hw_link_status {
281 	FTDM_HW_LINK_DISCONNECTED = 0,
282 	FTDM_HW_LINK_CONNECTED
283 } ftdm_channel_hw_link_status_t;
284 
285 typedef ftdm_status_t (*ftdm_stream_handle_raw_write_function_t) (ftdm_stream_handle_t *handle, uint8_t *data, ftdm_size_t datalen);
286 typedef ftdm_status_t (*ftdm_stream_handle_write_function_t) (ftdm_stream_handle_t *handle, const char *fmt, ...);
287 
288 #include "ftdm_dso.h"
289 
290 #define FTDM_NODE_NAME_SIZE 50
291 struct ftdm_conf_node {
292 	/* node name */
293 	char name[FTDM_NODE_NAME_SIZE];
294 
295 	/* total slots for parameters */
296 	unsigned int t_parameters;
297 
298 	/* current number of parameters */
299 	unsigned int n_parameters;
300 
301 	/* array of parameters */
302 	ftdm_conf_parameter_t *parameters;
303 
304 	/* first node child */
305 	struct ftdm_conf_node *child;
306 
307 	/* last node child */
308 	struct ftdm_conf_node *last;
309 
310 	/* next node sibling */
311 	struct ftdm_conf_node *next;
312 
313 	/* prev node sibling */
314 	struct ftdm_conf_node *prev;
315 
316 	/* my parent if any */
317 	struct ftdm_conf_node *parent;
318 };
319 
320 typedef struct ftdm_module {
321 	char name[256];
322 	fio_io_load_t io_load;
323 	fio_io_unload_t io_unload;
324 	fio_sig_load_t sig_load;
325 	fio_sig_configure_t sig_configure;
326 	fio_sig_unload_t sig_unload;
327 	/*!
328 	  \brief configure a given span signaling
329 	  \see sig_configure
330 	  This is just like sig_configure but receives
331 	  an an ftdm_conf_node_t instead
332 	  I'd like to deprecate sig_configure and move
333 	  all modules to use configure_span_signaling
334 	 */
335 	fio_configure_span_signaling_t configure_span_signaling;
336 	ftdm_dso_lib_t lib;
337 	char path[256];
338 } ftdm_module_t;
339 
340 typedef struct ftdm_fsk_data_state ftdm_fsk_data_state_t;
341 typedef int (*ftdm_fsk_data_decoder_t)(ftdm_fsk_data_state_t *state);
342 typedef ftdm_status_t (*ftdm_fsk_write_sample_t)(int16_t *buf, ftdm_size_t buflen, void *user_data);
343 typedef struct hashtable ftdm_hash_t;
344 typedef struct hashtable_iterator ftdm_hash_iterator_t;
345 typedef struct key ftdm_hash_key_t;
346 typedef struct value ftdm_hash_val_t;
347 typedef struct ftdm_bitstream ftdm_bitstream_t;
348 typedef struct ftdm_fsk_modulator ftdm_fsk_modulator_t;
349 typedef ftdm_status_t (*ftdm_span_start_t)(ftdm_span_t *span);
350 typedef ftdm_status_t (*ftdm_span_stop_t)(ftdm_span_t *span);
351 typedef ftdm_status_t (*ftdm_span_destroy_t)(ftdm_span_t *span);
352 typedef ftdm_status_t (*ftdm_channel_sig_read_t)(ftdm_channel_t *ftdmchan, void *data, ftdm_size_t size);
353 typedef ftdm_status_t (*ftdm_channel_sig_write_t)(ftdm_channel_t *ftdmchan, void *data, ftdm_size_t size);
354 typedef ftdm_status_t (*ftdm_channel_sig_dtmf_t)(ftdm_channel_t *ftdmchan, const char *dtmf);
355 
356 typedef enum {
357 	FTDM_ITERATOR_VARS = 1,
358 	FTDM_ITERATOR_CHANS,
359 	FTDM_ITERATOR_SPANS,
360 } ftdm_iterator_type_t;
361 
362 struct ftdm_iterator {
363 	ftdm_iterator_type_t type;
364 	unsigned int allocated:1;
365 	union {
366 		struct {
367 			uint32_t index;
368 			const ftdm_span_t *span;
369 		} chaniter;
370 		ftdm_hash_iterator_t *hashiter;
371 	} pvt;
372 };
373 
374 #ifdef __cplusplus
375 }
376 #endif
377 
378 #endif
379 
380 /* For Emacs:
381  * Local Variables:
382  * mode:c
383  * indent-tabs-mode:t
384  * tab-width:4
385  * c-basic-offset:4
386  * End:
387  * For VIM:
388  * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
389  */
390 
391