1 /*
2  * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3  * Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
4  *
5  * Version: MPL 1.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
18  *
19  * The Initial Developer of the Original Code is
20  * Anthony Minessale II <anthm@freeswitch.org>
21  * Portions created by the Initial Developer are Copyright (C)
22  * the Initial Developer. All Rights Reserved.
23  *
24  * Contributor(s):
25  *
26  * Anthony Minessale II <anthm@freeswitch.org>
27  * Neal Horman <neal at wanlink dot com>
28  *
29  *
30  * mod_skel.c -- Framework Demo Module
31  *
32  */
33 #include <switch.h>
34 
35 /* Prototypes */
36 SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_skel_shutdown);
37 SWITCH_MODULE_RUNTIME_FUNCTION(mod_skel_runtime);
38 SWITCH_MODULE_LOAD_FUNCTION(mod_skel_load);
39 
40 /* SWITCH_MODULE_DEFINITION(name, load, shutdown, runtime)
41  * Defines a switch_loadable_module_function_table_t and a static const char[] modname
42  */
43 SWITCH_MODULE_DEFINITION(mod_skel, mod_skel_load, mod_skel_shutdown, NULL);
44 
45 typedef enum {
46 	CODEC_NEGOTIATION_GREEDY = 1,
47 	CODEC_NEGOTIATION_GENEROUS = 2,
48 	CODEC_NEGOTIATION_EVIL = 3
49 } codec_negotiation_t;
50 
51 static struct {
52 	char *codec_negotiation_str;
53 	codec_negotiation_t codec_negotiation;
54 	switch_bool_t sip_trace;
55 	int integer;
56 } globals;
57 
config_callback_siptrace(switch_xml_config_item_t * data,switch_config_callback_type_t callback_type,switch_bool_t changed)58 static switch_status_t config_callback_siptrace(switch_xml_config_item_t *data, switch_config_callback_type_t callback_type, switch_bool_t changed)
59 {
60 	switch_bool_t value = *(switch_bool_t *) data->ptr;
61 	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "In siptrace callback: value %s changed %s\n",
62 					  value ? "true" : "false", changed ? "true" : "false");
63 
64 
65 	/*
66 	   if ((callback_type == CONFIG_LOG || callback_type == CONFIG_RELOAD) && changed) {
67 	   nua_set_params(((sofia_profile_t*)data->functiondata)->nua, TPTAG_LOG(value), TAG_END());
68 	   }
69 	 */
70 
71 	return SWITCH_STATUS_SUCCESS;
72 }
73 
74 static switch_xml_config_string_options_t config_opt_codec_negotiation = { NULL, 0, "greedy|generous|evil" };
75 
76 /* enforce_min, min, enforce_max, max */
77 static switch_xml_config_int_options_t config_opt_integer = { SWITCH_TRUE, 0, SWITCH_TRUE, 10 };
78 static switch_xml_config_enum_item_t config_opt_codec_negotiation_enum[] = {
79 	{"greedy", CODEC_NEGOTIATION_GREEDY},
80 	{"generous", CODEC_NEGOTIATION_GENEROUS},
81 	{"evil", CODEC_NEGOTIATION_EVIL},
82 	{NULL, 0}
83 };
84 
85 static switch_xml_config_item_t instructions[] = {
86 	/* parameter name        type                 reloadable   pointer                         default value     options structure */
87 	SWITCH_CONFIG_ITEM("codec-negotiation-str", SWITCH_CONFIG_STRING, CONFIG_RELOADABLE, &globals.codec_negotiation_str, "greedy",
88 					   &config_opt_codec_negotiation,
89 					   "greedy|generous|evil", "Specifies the codec negotiation scheme to be used."),
90 	SWITCH_CONFIG_ITEM("codec-negotiation", SWITCH_CONFIG_ENUM, CONFIG_RELOADABLE, &globals.codec_negotiation, (void *) CODEC_NEGOTIATION_GREEDY,
91 					   &config_opt_codec_negotiation_enum,
92 					   "greedy|generous|evil", "Specifies the codec negotiation scheme to be used."),
93 	SWITCH_CONFIG_ITEM_CALLBACK("sip-trace", SWITCH_CONFIG_BOOL, CONFIG_RELOADABLE, &globals.sip_trace, (void *) SWITCH_FALSE,
94 								(switch_xml_config_callback_t) config_callback_siptrace, NULL,
95 								"yes|no", "If enabled, print out sip messages on the console."),
96 	SWITCH_CONFIG_ITEM("integer", SWITCH_CONFIG_INT, CONFIG_RELOADABLE, &globals.integer, (void *) 100, &config_opt_integer,
97 					   NULL, NULL),
98 	SWITCH_CONFIG_ITEM_END()
99 };
100 
do_config(switch_bool_t reload)101 static switch_status_t do_config(switch_bool_t reload)
102 {
103 	memset(&globals, 0, sizeof(globals));
104 
105 	if (switch_xml_config_parse_module_settings("skel.conf", reload, instructions) != SWITCH_STATUS_SUCCESS) {
106 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Could not open skel.conf\n");
107 		return SWITCH_STATUS_FALSE;
108 	}
109 
110 	return SWITCH_STATUS_SUCCESS;
111 }
112 #include "switch_stun.h"
113 
114 #define _switch_stun_packet_next_attribute(attribute, end) (attribute && (attribute = (switch_stun_packet_attribute_t *) (attribute->value + ntohs(attribute->length))) && ((void *)attribute < end) && ntohs(attribute->length) && ((void *)(attribute + ntohs(attribute->length)) < end))
115 
116 #define _switch_stun_attribute_padded_length(attribute) ((uint16_t)(ntohs(attribute->length) + (sizeof(uint32_t)-1)) & ~sizeof(uint32_t))
117 
118 //#define _switch_stun_packet_next_attribute(attribute, end) (attribute && (attribute = (switch_stun_packet_attribute_t *) (attribute->value +  _switch_stun_attribute_padded_length(attribute))) && ((void *)attribute < end) && ((void *)(attribute +  _switch_stun_attribute_padded_length(attribute)) < end))
119 
120 #define MAX_PEERS 128
SWITCH_STANDARD_API(skel_function)121 SWITCH_STANDARD_API(skel_function)
122 {
123 	switch_dial_handle_t *dh;
124 	switch_dial_leg_list_t *ll;
125 	switch_dial_leg_t *leg = NULL;
126 	int timeout = 0;
127 	char *peer_names[MAX_PEERS] = { 0 };
128 	switch_event_t *peer_vars[MAX_PEERS] = { 0 };
129 	int i;
130 	switch_core_session_t *peer_session = NULL;
131 	switch_call_cause_t cause;
132 
133 	switch_dial_handle_create(&dh);
134 
135 
136 	switch_dial_handle_add_global_var(dh, "ignore_early_media", "true");
137 	switch_dial_handle_add_global_var_printf(dh, "coolness_count", "%d", 12);
138 
139 
140 	//// SET TO 1 FOR AND LIST example or to 0 for OR LIST example
141 #if 0
142 	switch_dial_handle_add_leg_list(dh, &ll);
143 
144 	switch_dial_leg_list_add_leg(ll, &leg, "sofia/internal/foo1@bar1.com");
145 	timeout += 10;
146 	switch_dial_handle_add_leg_var_printf(leg, "leg_timeout", "%d", timeout);
147 
148 	switch_dial_leg_list_add_leg(ll, &leg, "sofia/internal/foo2@bar2.com");
149 	timeout += 10;
150 	switch_dial_handle_add_leg_var_printf(leg, "leg_timeout", "%d", timeout);
151 
152 
153 	switch_dial_leg_list_add_leg(ll, &leg, "sofia/internal/foo3@bar3.com");
154 	timeout += 10;
155 	switch_dial_handle_add_leg_var_printf(leg, "leg_timeout", "%d", timeout);
156 
157 
158 	switch_dial_leg_list_add_leg(ll, &leg, "sofia/internal/3000@cantina.freeswitch.org");
159 
160 
161 #else
162 
163 	switch_dial_handle_add_leg_list(dh, &ll);
164 	switch_dial_leg_list_add_leg(ll, &leg, "sofia/internal/foo1@bar1.com");
165 	timeout += 10;
166 	switch_dial_handle_add_leg_var_printf(leg, "leg_timeout", "%d", timeout);
167 
168 	switch_dial_handle_add_leg_list(dh, &ll);
169 	switch_dial_leg_list_add_leg(ll, &leg, "sofia/internal/foo2@bar2.com");
170 	timeout += 10;
171 	switch_dial_handle_add_leg_var_printf(leg, "leg_timeout", "%d", timeout);
172 
173 	switch_dial_handle_add_leg_list(dh, &ll);
174 	switch_dial_leg_list_add_leg(ll, &leg, "sofia/internal/foo3@bar3.com");
175 	timeout += 10;
176 	switch_dial_handle_add_leg_var_printf(leg, "leg_timeout", "%d", timeout);
177 
178 
179 	switch_dial_handle_add_leg_list(dh, &ll);
180 	switch_dial_leg_list_add_leg(ll, &leg, "sofia/internal/3000@cantina.freeswitch.org");
181 #endif
182 
183 
184 
185 	/////// JUST DUMP SOME OF IT TO SEE FIRST
186 
187 	switch_dial_handle_get_peers(dh, 0, peer_names, MAX_PEERS);
188 	switch_dial_handle_get_vars(dh, 0, peer_vars, MAX_PEERS);
189 
190 
191 
192 
193 	for(i = 0; i < MAX_PEERS; i++) {
194 		if (peer_names[i]) {
195 			char *foo;
196 
197 			printf("peer: [%s]\n", peer_names[i]);
198 
199 			if (peer_vars[i]) {
200 				if (switch_event_serialize(peer_vars[i], &foo, SWITCH_FALSE) == SWITCH_STATUS_SUCCESS) {
201 					printf("%s\n", foo);
202 				}
203 			}
204 			printf("\n\n");
205 		}
206 	}
207 
208 
209 	switch_ivr_originate(NULL, &peer_session, &cause, NULL, 0, NULL, NULL, NULL, NULL, NULL, SOF_NONE, NULL, dh);
210 
211 	if (peer_session) {
212 		switch_ivr_session_transfer(peer_session, "3500", "XML", NULL);
213 		switch_core_session_rwunlock(peer_session);
214 	}
215 
216 
217 	switch_dial_handle_destroy(&dh);
218 
219 
220 
221 
222 	return SWITCH_STATUS_SUCCESS;
223 }
224 
mycb(switch_core_session_t * session,switch_channel_callstate_t callstate,switch_device_record_t * drec)225 static void mycb(switch_core_session_t *session, switch_channel_callstate_t callstate, switch_device_record_t *drec)
226 {
227 	switch_channel_t *channel = switch_core_session_get_channel(session);
228 
229 	switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(channel), SWITCH_LOG_CRIT,
230 					  "%s device: %s\nState: %s Dev State: %s/%s Total:%u Offhook:%u Active:%u Held:%u Hungup:%u Dur: %u %s\n",
231 					  switch_channel_get_name(channel),
232 					  drec->device_id,
233 					  switch_channel_callstate2str(callstate),
234 					  switch_channel_device_state2str(drec->last_state),
235 					  switch_channel_device_state2str(drec->state),
236 					  drec->stats.total,
237 					  drec->stats.offhook,
238 					  drec->stats.active,
239 					  drec->stats.held,
240 					  drec->stats.hup,
241 					  drec->active_stop ? (uint32_t)(drec->active_stop - drec->active_start) / 1000 : 0,
242 					  switch_channel_test_flag(channel, CF_FINAL_DEVICE_LEG) ? "FINAL LEG" : "");
243 
244 }
245 
246 
247 /* Macro expands to: switch_status_t mod_skel_load(switch_loadable_module_interface_t **module_interface, switch_memory_pool_t *pool) */
SWITCH_MODULE_LOAD_FUNCTION(mod_skel_load)248 SWITCH_MODULE_LOAD_FUNCTION(mod_skel_load)
249 {
250 	switch_api_interface_t *api_interface;
251 	/* connect my internal structure to the blank pointer passed to me */
252 	*module_interface = switch_loadable_module_create_module_interface(pool, modname);
253 
254 	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Hello World!\n");
255 
256 	do_config(SWITCH_FALSE);
257 
258 	SWITCH_ADD_API(api_interface, "skel", "Skel API", skel_function, "syntax");
259 
260 	switch_channel_bind_device_state_handler(mycb, NULL);
261 
262 	/* indicate that the module should continue to be loaded */
263 	return SWITCH_STATUS_SUCCESS;
264 }
265 
266 /*
267   Called when the system shuts down
268   Macro expands to: switch_status_t mod_skel_shutdown() */
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_skel_shutdown)269 SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_skel_shutdown)
270 {
271 	/* Cleanup dynamically allocated config settings */
272 	switch_channel_unbind_device_state_handler(mycb);
273 	switch_xml_config_cleanup(instructions);
274 	return SWITCH_STATUS_SUCCESS;
275 }
276 
277 
278 /*
279   If it exists, this is called in it's own thread when the module-load completes
280   If it returns anything but SWITCH_STATUS_TERM it will be called again automatically
281   Macro expands to: switch_status_t mod_skel_runtime()
282 SWITCH_MODULE_RUNTIME_FUNCTION(mod_skel_runtime)
283 {
284 	while(looping)
285 	{
286 		switch_cond_next();
287 	}
288 	return SWITCH_STATUS_TERM;
289 }
290 */
291 
292 /* For Emacs:
293  * Local Variables:
294  * mode:c
295  * indent-tabs-mode:t
296  * tab-width:4
297  * c-basic-offset:4
298  * End:
299  * For VIM:
300  * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet
301  */
302