1 /*
2  * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3  * Copyright (C) 2010, Mathieu Parent <math.parent@gmail.com>
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  * Mathieu Parent <math.parent@gmail.com>
21  * Portions created by the Initial Developer are Copyright (C)
22  * the Initial Developer. All Rights Reserved.
23  *
24  * Contributor(s):
25  *
26  * Mathieu Parent <math.parent@gmail.com>
27  *
28  *
29  * skinny_server.c -- Skinny Call Control Protocol (SCCP) Endpoint Module
30  *
31  */
32 #include <switch.h>
33 #include "mod_skinny.h"
34 #include "skinny_protocol.h"
35 #include "skinny_tables.h"
36 #include "skinny_server.h"
37 
38 uint32_t soft_key_template_default_textids[] = {
39 	SKINNY_TEXTID_REDIAL,
40 	SKINNY_TEXTID_NEWCALL,
41 	SKINNY_TEXTID_HOLD,
42 	SKINNY_TEXTID_TRANSFER,
43 	SKINNY_TEXTID_CFWDALL,
44 	SKINNY_TEXTID_CFWDBUSY,
45 	SKINNY_TEXTID_CFWDNOANSWER,
46 	SKINNY_TEXTID_BACKSPACE,
47 	SKINNY_TEXTID_ENDCALL,
48 	SKINNY_TEXTID_RESUME,
49 	SKINNY_TEXTID_ANSWER,
50 	SKINNY_TEXTID_INFO,
51 	SKINNY_TEXTID_CONF,
52 	SKINNY_TEXTID_PARK,
53 	SKINNY_TEXTID_JOIN,
54 	SKINNY_TEXTID_MEETME,
55 	SKINNY_TEXTID_CALLPICKUP,
56 	SKINNY_TEXTID_GRPCALLPICKUP,
57 	SKINNY_TEXTID_DND,
58 	SKINNY_TEXTID_IDIVERT
59 };
60 
61 uint32_t soft_key_template_default_events[] = {
62 	SOFTKEY_REDIAL,
63 	SOFTKEY_NEWCALL,
64 	SOFTKEY_HOLD,
65 	SOFTKEY_TRANSFER,
66 	SOFTKEY_CFWDALL,
67 	SOFTKEY_CFWDBUSY,
68 	SOFTKEY_CFWDNOANSWER,
69 	SOFTKEY_BACKSPACE,
70 	SOFTKEY_ENDCALL,
71 	SOFTKEY_RESUME,
72 	SOFTKEY_ANSWER,
73 	SOFTKEY_INFO,
74 	SOFTKEY_CONF,
75 	SOFTKEY_PARK,
76 	SOFTKEY_JOIN,
77 	SOFTKEY_MEETME,
78 	SOFTKEY_CALLPICKUP,
79 	SOFTKEY_GRPCALLPICKUP,
80 	SOFTKEY_DND,
81 	SOFTKEY_IDIVERT
82 };
83 
84 /*****************************************************************************/
85 /* SESSION FUNCTIONS */
86 /*****************************************************************************/
skinny_create_incoming_session(listener_t * listener,uint32_t * line_instance_p,switch_core_session_t ** session)87 switch_status_t skinny_create_incoming_session(listener_t *listener, uint32_t *line_instance_p, switch_core_session_t **session)
88 {
89 	uint32_t line_instance;
90 	switch_core_session_t *nsession;
91 	switch_channel_t *channel;
92 	private_t *tech_pvt;
93 	char name[128];
94 	char *sql;
95 	struct line_stat_res_message *button = NULL;
96 
97 	line_instance = *line_instance_p;
98 	if((nsession = skinny_profile_find_session(listener->profile, listener, line_instance_p, 0))) {
99 		if(skinny_line_get_state(listener, *line_instance_p, 0) == SKINNY_OFF_HOOK) {
100 			/* Reuse existing session */
101 			*session = nsession;
102 			return SWITCH_STATUS_SUCCESS;
103 		}
104 		switch_core_session_rwunlock(nsession);
105 	}
106 	*line_instance_p = line_instance;
107 	if(*line_instance_p == 0) {
108 		*line_instance_p = 1;
109 	}
110 
111 	skinny_hold_active_calls(listener);
112 
113 	skinny_line_get(listener, *line_instance_p, &button);
114 
115 	skinny_log_l(listener, SWITCH_LOG_INFO, "Attempting to create incoming session on Line %d\n", *line_instance_p);
116 
117 	if (!button || !button->shortname[0]) {
118 		skinny_log_l(listener, SWITCH_LOG_CRIT, "Line %d not found on device\n", *line_instance_p);
119 		goto error;
120 	}
121 
122 	if (!(nsession = switch_core_session_request(skinny_get_endpoint_interface(),
123 					SWITCH_CALL_DIRECTION_INBOUND, SOF_NONE, NULL))) {
124 		skinny_log_l_msg(listener, SWITCH_LOG_CRIT, "Error Creating Session\n");
125 		goto error;
126 	}
127 
128 	if (!(tech_pvt = (struct private_object *) switch_core_session_alloc(nsession, sizeof(*tech_pvt)))) {
129 		skinny_log_ls_msg(listener, nsession, SWITCH_LOG_CRIT, "Error Creating Session private object\n");
130 		goto error;
131 	}
132 
133 	switch_core_session_add_stream(nsession, NULL);
134 
135 	tech_init(tech_pvt, listener->profile, nsession);
136 
137 	channel = switch_core_session_get_channel(nsession);
138 
139 	snprintf(name, sizeof(name), "SKINNY/%s/%s:%d/%d", listener->profile->name,
140 			listener->device_name, listener->device_instance, *line_instance_p);
141 	switch_channel_set_name(channel, name);
142 
143 	if (switch_core_session_thread_launch(nsession) != SWITCH_STATUS_SUCCESS) {
144 		skinny_log_ls_msg(listener, nsession, SWITCH_LOG_CRIT, "Error Creating Session thread\n");
145 		goto error;
146 	}
147 	if (switch_core_session_read_lock(nsession) != SWITCH_STATUS_SUCCESS) {
148 		skinny_log_ls_msg(listener, nsession, SWITCH_LOG_CRIT, "Error Locking Session\n");
149 		goto error;
150 	}
151 	/* First create the caller profile in the patterns Dialplan */
152 	if (!(tech_pvt->caller_profile = switch_caller_profile_new(switch_core_session_get_pool(nsession),
153 					NULL, listener->profile->patterns_dialplan,
154 					button->displayname, button->shortname,
155 					listener->remote_ip, NULL, NULL, NULL,
156 					"skinny" /* modname */,
157 					listener->profile->patterns_context,
158 					""))) {
159 		skinny_log_ls_msg(listener, nsession, SWITCH_LOG_CRIT, "Error Creating Session caller profile\n");
160 		goto error;
161 	}
162 
163 	switch_channel_set_caller_profile(channel, tech_pvt->caller_profile);
164 
165 	if ((sql = switch_mprintf(
166 					"INSERT INTO skinny_active_lines "
167 					"(device_name, device_instance, line_instance, channel_uuid, call_id, call_state) "
168 					"SELECT device_name, device_instance, line_instance, '%q', %d, %d "
169 					"FROM skinny_lines "
170 					"WHERE value='%q'",
171 					switch_core_session_get_uuid(nsession), tech_pvt->call_id, SKINNY_ON_HOOK, button->shortname
172 				 ))) {
173 		skinny_execute_sql(listener->profile, sql, listener->profile->sql_mutex);
174 		switch_safe_free(sql);
175 	}
176 	skinny_session_set_variables(nsession, listener, *line_instance_p);
177 
178 	send_set_ringer(listener, SKINNY_RING_OFF, SKINNY_RING_FOREVER, *line_instance_p, tech_pvt->call_id);
179 	send_set_speaker_mode(listener, SKINNY_SPEAKER_ON);
180 	send_set_lamp(listener, SKINNY_BUTTON_LINE, *line_instance_p, SKINNY_LAMP_ON);
181 	skinny_line_set_state(listener, *line_instance_p, tech_pvt->call_id, SKINNY_OFF_HOOK);
182 	send_select_soft_keys(listener, *line_instance_p, tech_pvt->call_id, SKINNY_KEY_SET_OFF_HOOK, 0xffff);
183 
184 	send_display_prompt_status_textid(listener, 0, SKINNY_TEXTID_ENTER_NUMBER, *line_instance_p, tech_pvt->call_id);
185 
186 	send_activate_call_plane(listener, *line_instance_p);
187 	if (switch_channel_get_state(channel) == CS_NEW) {
188 		switch_channel_set_state(channel, CS_HIBERNATE);
189 	} else {
190 		skinny_log_ls_msg(listener, nsession, SWITCH_LOG_CRIT,
191 			"Wow! this channel should be in CS_NEW state, but it is not!\n");
192 	}
193 
194 	goto done;
195 error:
196 	skinny_log_l(listener, SWITCH_LOG_CRIT, "Failed to create incoming session for line instance %d", *line_instance_p);
197 	if (nsession) {
198 		switch_core_session_destroy(&nsession);
199 	}
200 
201 	listener->profile->ib_failed_calls++;
202 	switch_safe_free(button);
203 	return SWITCH_STATUS_FALSE;
204 
205 done:
206 	*session = nsession;
207 	listener->profile->ib_calls++;
208 	switch_safe_free(button);
209 	return SWITCH_STATUS_SUCCESS;
210 }
211 
skinny_session_dest_match_pattern(switch_core_session_t * session,char ** data)212 skinny_action_t skinny_session_dest_match_pattern(switch_core_session_t *session, char **data)
213 {
214 	skinny_action_t action = SKINNY_ACTION_DROP;
215 	switch_channel_t *channel = NULL;
216 	private_t *tech_pvt = NULL;
217 
218 	switch_assert(session);
219 
220 	channel = switch_core_session_get_channel(session);
221 	tech_pvt = switch_core_session_get_private(session);
222 
223 	/* this part of the code is similar to switch_core_standard_on_routing() */
224 	if (!zstr(tech_pvt->profile->patterns_dialplan)) {
225 		switch_dialplan_interface_t *dialplan_interface = NULL;
226 		switch_caller_extension_t *extension = NULL;
227 		char *expanded = NULL;
228 		char *dpstr = NULL;
229 		char *dp[25];
230 		int argc, x;
231 
232 		if ((dpstr = switch_core_session_strdup(session, tech_pvt->profile->patterns_dialplan))) {
233 			expanded = switch_channel_expand_variables(channel, dpstr);
234 			argc = switch_separate_string(expanded, ',', dp, (sizeof(dp) / sizeof(dp[0])));
235 			for (x = 0; x < argc; x++) {
236 				char *dpname = dp[x];
237 				char *dparg = NULL;
238 
239 				if (dpname) {
240 					if ((dparg = strchr(dpname, ':'))) {
241 						*dparg++ = '\0';
242 					}
243 				} else {
244 					continue;
245 				}
246 				if (!(dialplan_interface = switch_loadable_module_get_dialplan_interface(dpname))) {
247 					continue;
248 				}
249 
250 				extension = dialplan_interface->hunt_function(session, dparg, NULL);
251 				UNPROTECT_INTERFACE(dialplan_interface);
252 
253 				if (extension) {
254 					goto found;
255 				}
256 			}
257 		}
258 found:
259 		while (extension && extension->current_application) {
260 			switch_caller_application_t *current_application = extension->current_application;
261 
262 			extension->current_application = extension->current_application->next;
263 
264 			if (!strcmp(current_application->application_name, "skinny-route") || !strcmp(current_application->application_name, "skinny-process")) {
265 				action = SKINNY_ACTION_PROCESS;
266 			} else if (!strcmp(current_application->application_name, "skinny-drop")) {
267 				action = SKINNY_ACTION_DROP;
268 			} else if (!strcmp(current_application->application_name, "skinny-wait")) {
269 				action = SKINNY_ACTION_WAIT;
270 				*data = switch_core_session_strdup(session, current_application->application_data);
271 			} else {
272 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING,
273 						"Unknown skinny dialplan application %s\n", current_application->application_name);
274 			}
275 		}
276 	}
277 	return action;
278 }
279 
280 
skinny_session_process_dest(switch_core_session_t * session,listener_t * listener,uint32_t line_instance,char * dest,char append_dest,uint32_t backspace)281 switch_status_t skinny_session_process_dest(switch_core_session_t *session, listener_t *listener, uint32_t line_instance, char *dest, char append_dest, uint32_t backspace)
282 {
283 	switch_channel_t *channel = NULL;
284 	private_t *tech_pvt = NULL;
285 
286 	switch_assert(session);
287 	switch_assert(listener);
288 	switch_assert(listener->profile);
289 
290 	channel = switch_core_session_get_channel(session);
291 	tech_pvt = switch_core_session_get_private(session);
292 
293 	// get listener profile setting for ringdown/autodial
294 	// if initial offhook - and we have a ringdown/autodial configured, just dial it in one shot
295 	if (!dest && append_dest == '\0' && listener->ext_autodial ) {
296 		switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING,
297 			"triggering auto dial to (%s)\n", listener->ext_autodial);
298 
299 		tech_pvt->caller_profile->destination_number = switch_core_strdup(tech_pvt->caller_profile->pool, listener->ext_autodial);
300 		switch_set_flag_locked(tech_pvt, TFLAG_FORCE_ROUTE);
301 	} else if (!dest) {
302 		if (strlen(tech_pvt->caller_profile->destination_number) == 0) {/* no digit yet */
303 			send_start_tone(listener, SKINNY_TONE_DIALTONE, 0, line_instance, tech_pvt->call_id);
304 		}
305 		if (backspace && strlen(tech_pvt->caller_profile->destination_number)) { /* backspace */
306 			tech_pvt->caller_profile->destination_number[strlen(tech_pvt->caller_profile->destination_number)-1] = '\0';
307 			if (strlen(tech_pvt->caller_profile->destination_number) == 0) {
308 				send_select_soft_keys(listener, line_instance, tech_pvt->call_id, SKINNY_KEY_SET_OFF_HOOK, 0xffff);
309 			}
310 			send_back_space_request(listener, line_instance, tech_pvt->call_id);
311 		}
312 		if (append_dest != '\0') {/* append digit */
313 			tech_pvt->caller_profile->destination_number = switch_core_sprintf(tech_pvt->caller_profile->pool,
314 					"%s%c", tech_pvt->caller_profile->destination_number, append_dest);
315 		}
316 		if (strlen(tech_pvt->caller_profile->destination_number) == 1) {/* first digit */
317 			if(!backspace) {
318 				send_stop_tone(listener, line_instance, tech_pvt->call_id);
319 			}
320 			send_select_soft_keys(listener, line_instance, tech_pvt->call_id,
321 					SKINNY_KEY_SET_DIGITS_AFTER_DIALING_FIRST_DIGIT, 0xffff);
322 		}
323 	} else {
324 		tech_pvt->caller_profile->destination_number = switch_core_strdup(tech_pvt->caller_profile->pool, dest);
325 		switch_set_flag_locked(tech_pvt, TFLAG_FORCE_ROUTE);
326 	}
327 
328 	switch_channel_set_state(channel, CS_ROUTING);
329 
330 	return SWITCH_STATUS_SUCCESS;
331 }
332 
skinny_session_send_call_info(switch_core_session_t * session,listener_t * listener,uint32_t line_instance)333 switch_status_t skinny_session_send_call_info(switch_core_session_t *session, listener_t *listener, uint32_t line_instance)
334 {
335 	private_t *tech_pvt;
336 	switch_channel_t *channel;
337 
338 	const char *caller_party_name;
339 	const char *caller_party_number;
340 	const char *called_party_name;
341 	const char *called_party_number;
342 	uint32_t call_type = 0;
343 
344 	channel = switch_core_session_get_channel(session);
345 	tech_pvt = switch_core_session_get_private(session);
346 
347 	switch_assert(tech_pvt->caller_profile != NULL);
348 
349 	/* Calling party */
350 	if (zstr((caller_party_name = switch_channel_get_variable(channel, "effective_caller_id_name"))) &&
351 			zstr((caller_party_name = switch_channel_get_variable(channel, "caller_id_name"))) &&
352 			zstr((caller_party_name = switch_channel_get_variable_partner(channel, "effective_caller_id_name"))) &&
353 			zstr((caller_party_name = switch_channel_get_variable_partner(channel, "caller_id_name")))) {
354 		caller_party_name = SWITCH_DEFAULT_CLID_NAME;
355 	}
356 	if (zstr((caller_party_number = switch_channel_get_variable(channel, "effective_caller_id_number"))) &&
357 			zstr((caller_party_number = switch_channel_get_variable(channel, "caller_id_number"))) &&
358 			zstr((caller_party_number = switch_channel_get_variable_partner(channel, "effective_caller_id_number"))) &&
359 			zstr((caller_party_number = switch_channel_get_variable_partner(channel, "caller_id_number")))) {
360 		caller_party_number = SWITCH_DEFAULT_CLID_NUMBER;
361 	}
362 	/* Called party */
363 	if (zstr((called_party_name = switch_channel_get_variable(channel, "effective_callee_id_name"))) &&
364 			zstr((called_party_name = switch_channel_get_variable(channel, "callee_id_name"))) &&
365 			zstr((called_party_name = switch_channel_get_variable_partner(channel, "effective_callee_id_name"))) &&
366 			zstr((called_party_name = switch_channel_get_variable_partner(channel, "callee_id_name")))) {
367 		called_party_name = SWITCH_DEFAULT_CLID_NAME;
368 	}
369 	if (zstr((called_party_number = switch_channel_get_variable(channel, "effective_callee_id_number"))) &&
370 			zstr((called_party_number = switch_channel_get_variable(channel, "callee_id_number"))) &&
371 			zstr((called_party_number = switch_channel_get_variable_partner(channel, "effective_callee_id_number"))) &&
372 			zstr((called_party_number = switch_channel_get_variable_partner(channel, "callee_id_number"))) &&
373 			zstr((called_party_number = switch_channel_get_variable(channel, "destination_number")))) {
374 		called_party_number = SWITCH_DEFAULT_CLID_NUMBER;
375 	}
376 	if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND) {
377 		call_type = SKINNY_INBOUND_CALL;
378 	} else {
379 		call_type = SKINNY_OUTBOUND_CALL;
380 	}
381 	send_call_info(listener,
382 			caller_party_name, /* char calling_party_name[40], */
383 			caller_party_number, /* char calling_party[24], */
384 			called_party_name, /* char called_party_name[40], */
385 			called_party_number, /* char called_party[24], */
386 			line_instance, /* uint32_t line_instance, */
387 			tech_pvt->call_id, /* uint32_t call_id, */
388 			call_type, /* uint32_t call_type, */
389 			"", /* TODO char original_called_party_name[40], */
390 			"", /* TODO char original_called_party[24], */
391 			"", /* TODO char last_redirecting_party_name[40], */
392 			"", /* TODO char last_redirecting_party[24], */
393 			0, /* TODO uint32_t original_called_party_redirect_reason, */
394 			0, /* TODO uint32_t last_redirecting_reason, */
395 			"", /* TODO char calling_party_voice_mailbox[24], */
396 			"", /* TODO char called_party_voice_mailbox[24], */
397 			"", /* TODO char original_called_party_voice_mailbox[24], */
398 			"", /* TODO char last_redirecting_voice_mailbox[24], */
399 			1, /* uint32_t call_instance, */
400 			1, /* uint32_t call_security_status, */
401 			0 /* uint32_t party_pi_restriction_bits */
402 				);
403 	return SWITCH_STATUS_SUCCESS;
404 }
405 
406 struct skinny_session_send_call_info_all_helper {
407 	private_t *tech_pvt;
408 };
409 
skinny_session_send_call_info_all_callback(void * pArg,int argc,char ** argv,char ** columnNames)410 int skinny_session_send_call_info_all_callback(void *pArg, int argc, char **argv, char **columnNames)
411 {
412 	char *device_name = argv[0];
413 	uint32_t device_instance = atoi(argv[1]);
414 	/* uint32_t position = atoi(argv[2]); */
415 	uint32_t line_instance = atoi(argv[3]);
416 	/* char *label = argv[4]; */
417 	/* char *value = argv[5]; */
418 	/* char *caller_name = argv[6]; */
419 	/* uint32_t ring_on_idle = atoi(argv[7]); */
420 	/* uint32_t ring_on_active = atoi(argv[8]); */
421 	/* uint32_t busy_trigger = atoi(argv[9]); */
422 	/* char *forward_all = argv[10]; */
423 	/* char *forward_busy = argv[11]; */
424 	/* char *forward_noanswer = argv[12]; */
425 	/* uint32_t noanswer_duration = atoi(argv[13]); */
426 	/* char *channel_uuid = argv[14]; */
427 	/* uint32_t call_id = atoi(argv[15]); */
428 	/* uint32_t call_state = atoi(argv[16]); */
429 
430 	struct skinny_session_send_call_info_all_helper *helper = pArg;
431 	listener_t *listener = NULL;
432 
433 	skinny_profile_find_listener_by_device_name_and_instance(helper->tech_pvt->profile,
434 			device_name, device_instance, &listener);
435 	if(listener) {
436 		skinny_session_send_call_info(helper->tech_pvt->session, listener, line_instance);
437 	}
438 	return 0;
439 }
440 
skinny_session_send_call_info_all(switch_core_session_t * session)441 switch_status_t skinny_session_send_call_info_all(switch_core_session_t *session)
442 {
443 	struct skinny_session_send_call_info_all_helper helper = {0};
444 	private_t *tech_pvt = switch_core_session_get_private(session);
445 
446 	helper.tech_pvt = tech_pvt;
447 	return skinny_session_walk_lines(tech_pvt->profile,
448 			switch_core_session_get_uuid(tech_pvt->session), skinny_session_send_call_info_all_callback, &helper);
449 }
450 
451 struct skinny_session_set_variables_helper {
452 	private_t *tech_pvt;
453 	switch_channel_t *channel;
454 	listener_t *listener;
455 	uint32_t count;
456 };
457 
skinny_session_set_variables_callback(void * pArg,int argc,char ** argv,char ** columnNames)458 int skinny_session_set_variables_callback(void *pArg, int argc, char **argv, char **columnNames)
459 {
460 	char *device_name = argv[0];
461 	uint32_t device_instance = atoi(argv[1]);
462 	uint32_t position = atoi(argv[2]);
463 	uint32_t line_instance = atoi(argv[3]);
464 	char *label = argv[4];
465 	char *value = argv[5];
466 	char *caller_name = argv[6];
467 	/* uint32_t ring_on_idle = atoi(argv[7]); */
468 	/* uint32_t ring_on_active = atoi(argv[8]); */
469 	/* uint32_t busy_trigger = atoi(argv[9]); */
470 	/* char *forward_all = argv[10]; */
471 	/* char *forward_busy = argv[11]; */
472 	/* char *forward_noanswer = argv[12]; */
473 	/* uint32_t noanswer_duration = atoi(argv[13]); */
474 	/* char *channel_uuid = argv[14]; */
475 	/* uint32_t call_id = atoi(argv[15]); */
476 	/* uint32_t call_state = atoi(argv[16]); */
477 
478 	struct skinny_session_set_variables_helper *helper = pArg;
479 	char *tmp;
480 	listener_t *listener;
481 
482 	switch_xml_t xroot, xdomain, xuser, xvariables, xvariable;
483 
484 	helper->count++;
485 	switch_channel_set_variable_name_printf(helper->channel, device_name, "skinny_device_name_%d", helper->count);
486 	if ((tmp = switch_mprintf("%d", device_instance))) {
487 		switch_channel_set_variable_name_printf(helper->channel, tmp, "skinny_device_instance_%d", helper->count);
488 		switch_safe_free(tmp);
489 	}
490 	if ((tmp = switch_mprintf("%d", position))) {
491 		switch_channel_set_variable_name_printf(helper->channel, tmp, "skinny_line_position_%d", helper->count);
492 		switch_safe_free(tmp);
493 	}
494 	if ((tmp = switch_mprintf("%d", line_instance))) {
495 		switch_channel_set_variable_name_printf(helper->channel, tmp, "skinny_line_instance_%d", helper->count);
496 		switch_safe_free(tmp);
497 	}
498 	switch_channel_set_variable_name_printf(helper->channel, label, "skinny_line_label_%d", helper->count);
499 	switch_channel_set_variable_name_printf(helper->channel, value, "skinny_line_value_%d", helper->count);
500 	switch_channel_set_variable_name_printf(helper->channel, caller_name, "skinny_line_caller_name_%d", helper->count);
501 
502 	listener = helper->listener;
503 
504 	if ( ! listener ) {
505 		switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(helper->tech_pvt->session), SWITCH_LOG_DEBUG,
506 			"no defined listener on channel var setup, will not attempt to set variables\n");
507 		return(0);
508 	}
509 
510 	/* Process through and extract any variables from the user and set in the channel */
511 	switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(helper->tech_pvt->session), SWITCH_LOG_DEBUG,
512 			"searching for user (id=%s) in profile %s in channel var setup\n",
513 			listener->device_name, listener->profile->domain);
514 
515 	if (switch_xml_locate_user("id", listener->device_name, listener->profile->domain, "",
516 		&xroot, &xdomain, &xuser, NULL, NULL) != SWITCH_STATUS_SUCCESS) {
517 
518 		switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(helper->tech_pvt->session), SWITCH_LOG_WARNING,
519 				"unable to find user (id=%s) in channel var setup\n", listener->device_name);
520 	}
521 
522 	if ( xuser ) {
523 		char *uid = (char *) switch_xml_attr_soft(xuser, "id");
524 
525 		switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(helper->tech_pvt->session), SWITCH_LOG_DEBUG,
526 				"found user (id=%s) in channel var setup\n", uid);
527 
528 		if ((xvariables = switch_xml_child(xuser, "variables"))) {
529 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(helper->tech_pvt->session), SWITCH_LOG_DEBUG,
530 				"found variables section in user xml");
531 
532 			for (xvariable = switch_xml_child(xvariables, "variable"); xvariable; xvariable = xvariable->next) {
533 				char *name = (char *) switch_xml_attr_soft(xvariable, "name");
534 				char *value = (char *) switch_xml_attr_soft(xvariable, "value");
535 
536 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(helper->tech_pvt->session), SWITCH_LOG_DEBUG,
537 					"found variable (%s=%s) for user (%s) in channel var setup\n", name, value, listener->device_name);
538 
539 				switch_channel_set_variable_name_printf(helper->channel, value, "%s", name);
540 			}
541 		}
542 	}
543 
544 	if ( xroot ) {
545 		switch_xml_free(xroot);
546 	}
547 
548 	return 0;
549 }
550 
skinny_session_set_variables(switch_core_session_t * session,listener_t * listener,uint32_t line_instance)551 switch_status_t skinny_session_set_variables(switch_core_session_t *session, listener_t *listener, uint32_t line_instance)
552 {
553 	switch_status_t status;
554 	struct skinny_session_set_variables_helper helper = {0};
555 
556 	helper.tech_pvt = switch_core_session_get_private(session);
557 	helper.channel = switch_core_session_get_channel(session);
558 	helper.listener = listener;
559 	helper.count = 0;
560 
561 	switch_channel_set_variable(helper.channel, "skinny_profile_name", helper.tech_pvt->profile->name);
562 	if (listener) {
563 		switch_channel_set_variable(helper.channel, "skinny_device_name", listener->device_name);
564 		switch_channel_set_variable_printf(helper.channel, "skinny_device_instance", "%d", listener->device_instance);
565 		switch_channel_set_variable_printf(helper.channel, "skinny_line_instance", "%d", line_instance);
566 	}
567 	status = skinny_session_walk_lines(helper.tech_pvt->profile,
568 			switch_core_session_get_uuid(helper.tech_pvt->session), skinny_session_set_variables_callback, &helper);
569 
570 	switch_channel_set_variable_printf(helper.channel, "skinny_lines_count", "%d", helper.count);
571 	return status;
572 }
573 
574 struct skinny_ring_lines_helper {
575 	private_t *tech_pvt;
576 	switch_core_session_t *remote_session;
577 	uint32_t lines_count;
578 };
579 
skinny_ring_lines_callback(void * pArg,int argc,char ** argv,char ** columnNames)580 int skinny_ring_lines_callback(void *pArg, int argc, char **argv, char **columnNames)
581 {
582 	struct skinny_ring_lines_helper *helper = pArg;
583 	char *tmp;
584 	char *label;
585 
586 	char *device_name = argv[0];
587 	uint32_t device_instance = atoi(argv[1]);
588 	/* uint32_t position = atoi(argv[2]); */
589 	uint32_t line_instance = atoi(argv[3]);
590 	/* char *label = argv[4]; */
591 	char *value = argv[5];
592 	char *caller_name = argv[6];
593 	uint32_t ring_on_idle = atoi(argv[7]);
594 	uint32_t ring_on_active = atoi(argv[8]);
595 	/* uint32_t busy_trigger = atoi(argv[9]); */
596 	/* char *forward_all = argv[10]; */
597 	/* char *forward_busy = argv[11]; */
598 	/* char *forward_noanswer = argv[12]; */
599 	/* uint32_t noanswer_duration = atoi(argv[13]); */
600 	/* char *channel_uuid = argv[14]; */
601 	/* uint32_t call_id = atoi(argv[15]); */
602 	/* uint32_t call_state = atoi(argv[16]); */
603 
604 	listener_t *listener = NULL;
605 	uint32_t active_calls = 0;
606 
607 	skinny_profile_find_listener_by_device_name_and_instance(helper->tech_pvt->profile,
608 			device_name, device_instance, &listener);
609 	if(listener && helper->tech_pvt->session && helper->remote_session) {
610 		switch_channel_t *channel = switch_core_session_get_channel(helper->tech_pvt->session);
611 		switch_channel_set_state(channel, CS_ROUTING);
612 		helper->lines_count++;
613 		switch_channel_set_variable(channel, "effective_callee_id_number", value);
614 		switch_channel_set_variable(channel, "effective_callee_id_name", caller_name);
615 
616 		active_calls = skinny_line_count_active(listener);
617 
618 		skinny_log_l(listener, SWITCH_LOG_DEBUG,
619 			"Ring Lines Callback with Callee Number (%s), Caller Name (%s), Dest Number (%s), Active Calls (%d)\n",
620 			value, caller_name, helper->tech_pvt->caller_profile->destination_number, active_calls);
621 
622 		if (helper->remote_session) {
623 			switch_core_session_message_t msg = { 0 };
624 			msg.message_id = SWITCH_MESSAGE_INDICATE_DISPLAY;
625 			msg.string_array_arg[0] = switch_core_session_strdup(helper->remote_session, caller_name);
626 			msg.string_array_arg[1] = switch_core_session_strdup(helper->remote_session, value);
627 			msg.from = __FILE__;
628 
629 			if (switch_core_session_receive_message(helper->remote_session, &msg) != SWITCH_STATUS_SUCCESS) {
630 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(helper->tech_pvt->session), SWITCH_LOG_WARNING,
631 						"Unable to send SWITCH_MESSAGE_INDICATE_DISPLAY message to channel %s\n",
632 						switch_core_session_get_uuid(helper->remote_session));
633 			}
634 		}
635 
636 		skinny_line_set_state(listener, line_instance, helper->tech_pvt->call_id, SKINNY_RING_IN);
637 		send_select_soft_keys(listener, line_instance, helper->tech_pvt->call_id, SKINNY_KEY_SET_RING_IN, 0xffff);
638 
639 		label = skinny_textid2raw(SKINNY_TEXTID_FROM);
640 		if ((tmp = switch_mprintf("%s%s", label, helper->tech_pvt->caller_profile->destination_number))) {
641 			send_display_prompt_status(listener, 0, tmp, line_instance, helper->tech_pvt->call_id);
642 			switch_safe_free(tmp);
643 		}
644 		switch_safe_free(label);
645 
646 		if ((tmp = switch_mprintf("\005\000\000\000%s", helper->tech_pvt->caller_profile->destination_number))) {
647 			send_display_pri_notify(listener, 10 /* message_timeout */, 5 /* priority */, tmp);
648 			switch_safe_free(tmp);
649 		}
650 		skinny_session_send_call_info(helper->tech_pvt->session, listener, line_instance);
651 		send_set_lamp(listener, SKINNY_BUTTON_LINE, line_instance, SKINNY_LAMP_BLINK);
652 
653 		if ( active_calls < 1 && ring_on_idle ) {
654 			send_set_ringer(listener, SKINNY_RING_INSIDE, SKINNY_RING_FOREVER, line_instance, helper->tech_pvt->call_id);
655 		} else if ( active_calls > 0 && ring_on_active ) {
656 			send_start_tone(listener, SKINNY_TONE_CALLWAITTONE, 0, line_instance, helper->tech_pvt->call_id);
657 			send_stop_tone(listener, line_instance, helper->tech_pvt->call_id);
658 		} else {
659 			send_set_ringer(listener, SKINNY_RING_FLASHONLY, SKINNY_RING_FOREVER, line_instance, helper->tech_pvt->call_id);
660 		}
661 		switch_channel_ring_ready(channel);
662 	}
663 	return 0;
664 }
665 
skinny_ring_lines(private_t * tech_pvt,switch_core_session_t * remote_session)666 switch_call_cause_t skinny_ring_lines(private_t *tech_pvt, switch_core_session_t *remote_session)
667 {
668 	switch_status_t status;
669 	struct skinny_ring_lines_helper helper = {0};
670 
671 	switch_assert(tech_pvt);
672 	switch_assert(tech_pvt->profile);
673 	switch_assert(tech_pvt->session);
674 
675 	helper.tech_pvt = tech_pvt;
676 	helper.remote_session = remote_session;
677 	helper.lines_count = 0;
678 
679 	status = skinny_session_walk_lines(tech_pvt->profile,
680 			switch_core_session_get_uuid(tech_pvt->session), skinny_ring_lines_callback, &helper);
681 	skinny_session_set_variables(tech_pvt->session, NULL, 0);
682 
683 	if (status != SWITCH_STATUS_SUCCESS) {
684 		return SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER;
685 	} else if (helper.lines_count == 0) {
686 		return SWITCH_CAUSE_UNALLOCATED_NUMBER;
687 	} else {
688 		return SWITCH_CAUSE_SUCCESS;
689 	}
690 }
691 
skinny_session_ring_out(switch_core_session_t * session,listener_t * listener,uint32_t line_instance)692 switch_status_t skinny_session_ring_out(switch_core_session_t *session, listener_t *listener, uint32_t line_instance)
693 {
694 	private_t *tech_pvt = NULL;
695 
696 	switch_assert(session);
697 	switch_assert(listener);
698 	switch_assert(listener->profile);
699 
700 	tech_pvt = switch_core_session_get_private(session);
701 
702 	send_start_tone(listener, SKINNY_TONE_ALERT, 0, line_instance, tech_pvt->call_id);
703 	skinny_line_set_state(listener, line_instance, tech_pvt->call_id, SKINNY_RING_OUT);
704 	send_select_soft_keys(listener, line_instance, tech_pvt->call_id, SKINNY_KEY_SET_RING_OUT, 0xffff);
705 
706 	send_display_prompt_status_textid(listener, 0, SKINNY_TEXTID_RING_OUT, line_instance, tech_pvt->call_id);
707 
708 	skinny_session_send_call_info(session, listener, line_instance);
709 
710 	return SWITCH_STATUS_SUCCESS;
711 }
712 
713 
714 struct skinny_session_answer_helper {
715 	private_t *tech_pvt;
716 	listener_t *listener;
717 	uint32_t line_instance;
718 };
719 
skinny_session_answer_callback(void * pArg,int argc,char ** argv,char ** columnNames)720 int skinny_session_answer_callback(void *pArg, int argc, char **argv, char **columnNames)
721 {
722 	struct skinny_session_answer_helper *helper = pArg;
723 	listener_t *listener = NULL;
724 
725 	char *device_name = argv[0];
726 	uint32_t device_instance = atoi(argv[1]);
727 	/* uint32_t position = atoi(argv[2]); */
728 	uint32_t line_instance = atoi(argv[3]);
729 	/* char *label = argv[4]; */
730 	/* char *value = argv[5]; */
731 	/* char *caller_name = argv[6]; */
732 	/* uint32_t ring_on_idle = atoi(argv[7]); */
733 	/* uint32_t ring_on_active = atoi(argv[8]); */
734 	/* uint32_t busy_trigger = atoi(argv[9]); */
735 	/* char *forward_all = argv[10]; */
736 	/* char *forward_busy = argv[11]; */
737 	/* char *forward_noanswer = argv[12]; */
738 	/* uint32_t noanswer_duration = atoi(argv[13]); */
739 	/* char *channel_uuid = argv[14]; */
740 	/* uint32_t call_id = atoi(argv[15]); */
741 	/* uint32_t call_state = atoi(argv[16]); */
742 
743 	skinny_profile_find_listener_by_device_name_and_instance(helper->tech_pvt->profile, device_name, device_instance, &listener);
744 	if(listener) {
745 		if(!strcmp(device_name, helper->listener->device_name)
746 				&& (device_instance == helper->listener->device_instance)
747 				&& (line_instance == helper->line_instance)) {/* the answering line */
748 			/* nothing */
749 			skinny_log_l_msg(listener, SWITCH_LOG_DEBUG, "Session Answer Callback - matched helper\n");
750 		} else {
751 			skinny_log_l_msg(listener, SWITCH_LOG_DEBUG, "Session Answer Callback\n");
752 
753 			send_define_current_time_date(listener);
754 			send_set_lamp(listener, SKINNY_BUTTON_LINE, line_instance, SKINNY_LAMP_ON);
755 			skinny_line_set_state(listener, line_instance, helper->tech_pvt->call_id, SKINNY_IN_USE_REMOTELY);
756 			send_select_soft_keys(listener, line_instance, helper->tech_pvt->call_id, 10, 0x0002);
757 
758 			send_display_prompt_status_textid(listener, 0, SKINNY_TEXTID_IN_USE_REMOTE, line_instance, helper->tech_pvt->call_id);
759 
760 			send_set_ringer(listener, SKINNY_RING_OFF, SKINNY_RING_FOREVER, line_instance, helper->tech_pvt->call_id);
761 		}
762 	}
763 	return 0;
764 }
765 
skinny_session_answer(switch_core_session_t * session,listener_t * listener,uint32_t line_instance)766 switch_status_t skinny_session_answer(switch_core_session_t *session, listener_t *listener, uint32_t line_instance)
767 {
768 	struct skinny_session_answer_helper helper = {0};
769 	switch_channel_t *channel = NULL;
770 	private_t *tech_pvt = NULL;
771 
772 	switch_assert(session);
773 	switch_assert(listener);
774 	switch_assert(listener->profile);
775 
776 	skinny_hold_active_calls(listener);
777 
778 	channel = switch_core_session_get_channel(session);
779 	tech_pvt = switch_core_session_get_private(session);
780 
781 	send_set_ringer(listener, SKINNY_RING_OFF, SKINNY_RING_FOREVER, line_instance, tech_pvt->call_id);
782 	send_set_speaker_mode(listener, SKINNY_SPEAKER_ON);
783 	send_set_lamp(listener, SKINNY_BUTTON_LINE, line_instance, SKINNY_LAMP_ON);
784 	skinny_line_set_state(listener, line_instance, tech_pvt->call_id, SKINNY_OFF_HOOK);
785 	send_activate_call_plane(listener, line_instance);
786 
787 	helper.tech_pvt = tech_pvt;
788 	helper.listener = listener;
789 	helper.line_instance = line_instance;
790 
791 	skinny_session_walk_lines(tech_pvt->profile, switch_core_session_get_uuid(session), skinny_session_answer_callback, &helper);
792 
793 	if (switch_channel_get_state(channel) == CS_INIT) {
794 		switch_channel_set_state(channel, CS_ROUTING);
795 	}
796 	skinny_session_start_media(session, listener, line_instance);
797 
798 	return SWITCH_STATUS_SUCCESS;
799 }
800 
skinny_session_start_media(switch_core_session_t * session,listener_t * listener,uint32_t line_instance)801 switch_status_t skinny_session_start_media(switch_core_session_t *session, listener_t *listener, uint32_t line_instance)
802 {
803 	switch_channel_t *channel = NULL;
804 	private_t *tech_pvt = NULL;
805 
806 	switch_assert(session);
807 	switch_assert(listener);
808 	switch_assert(listener->profile);
809 
810 	channel = switch_core_session_get_channel(session);
811 	tech_pvt = switch_core_session_get_private(session);
812 
813 	if (!switch_channel_test_flag(channel, CF_EARLY_MEDIA)) {
814 		send_stop_tone(listener, line_instance, tech_pvt->call_id);
815 		send_open_receive_channel(listener,
816 				tech_pvt->call_id, /* uint32_t conference_id, */
817 				tech_pvt->call_id, /* uint32_t pass_thru_party_id, */
818 				SKINNY_PTIME, /* uint32_t ms_per_packet, */
819 				SKINNY_CODEC_ULAW_64K, /* uint32_t payload_capacity, */
820 				0, /* uint32_t echo_cancel_type, */
821 				0, /* uint32_t g723_bitrate, */
822 				tech_pvt->call_id, /* uint32_t conference_id2, */
823 				0 /* uint32_t reserved[10] */
824 				);
825 	}
826 	if (!switch_test_flag(tech_pvt, TFLAG_EARLY_MEDIA)) {
827 		skinny_line_set_state(listener, line_instance, tech_pvt->call_id, SKINNY_CONNECTED);
828 		send_select_soft_keys(listener, line_instance, tech_pvt->call_id,
829 				SKINNY_KEY_SET_CONNECTED, 0xffff);
830 
831 		send_display_prompt_status_textid(listener, 0, SKINNY_TEXTID_CONNECTED, line_instance, tech_pvt->call_id);
832 	}
833 	skinny_session_send_call_info(session, listener, line_instance);
834 
835 	return SWITCH_STATUS_SUCCESS;
836 }
837 
skinny_session_hold_line(switch_core_session_t * session,listener_t * listener,uint32_t line_instance)838 switch_status_t skinny_session_hold_line(switch_core_session_t *session, listener_t *listener, uint32_t line_instance)
839 {
840 	private_t *tech_pvt = NULL;
841 
842 	switch_assert(session);
843 	switch_assert(listener);
844 	switch_assert(listener->profile);
845 
846 	tech_pvt = switch_core_session_get_private(session);
847 
848 	skinny_session_stop_media(session, listener, line_instance);
849 	switch_ivr_hold(session, NULL, 1);
850 
851 	send_define_current_time_date(listener);
852 	send_set_lamp(listener, SKINNY_BUTTON_LINE, line_instance, SKINNY_LAMP_WINK);
853 	skinny_line_set_state(listener, line_instance, tech_pvt->call_id, SKINNY_HOLD);
854 	send_select_soft_keys(listener, line_instance, tech_pvt->call_id, SKINNY_KEY_SET_ON_HOLD, 0xffff);
855 
856 	send_display_prompt_status_textid(listener, 0, SKINNY_TEXTID_HOLD, line_instance, tech_pvt->call_id);
857 
858 	skinny_session_send_call_info(tech_pvt->session, listener, line_instance);
859 	send_set_speaker_mode(listener, SKINNY_SPEAKER_OFF);
860 	send_set_ringer(listener, SKINNY_RING_OFF, SKINNY_RING_FOREVER, line_instance, tech_pvt->call_id);
861 
862 	return SWITCH_STATUS_SUCCESS;
863 }
864 
skinny_session_unhold_line(switch_core_session_t * session,listener_t * listener,uint32_t line_instance)865 switch_status_t skinny_session_unhold_line(switch_core_session_t *session, listener_t *listener, uint32_t line_instance)
866 {
867 	private_t *tech_pvt = NULL;
868 
869 	switch_assert(session);
870 	switch_assert(listener);
871 	switch_assert(listener->profile);
872 
873 	tech_pvt = switch_core_session_get_private(session);
874 
875 	skinny_hold_active_calls(listener);
876 	send_set_ringer(listener, SKINNY_RING_OFF, SKINNY_RING_FOREVER, line_instance, tech_pvt->call_id);
877 	send_set_speaker_mode(listener, SKINNY_SPEAKER_ON);
878 	send_select_soft_keys(listener, line_instance, tech_pvt->call_id, SKINNY_KEY_SET_RING_OUT, 0xffff);
879 
880 	send_stop_tone(listener, line_instance, tech_pvt->call_id);
881 	send_open_receive_channel(listener,
882 		tech_pvt->call_id, /* uint32_t conference_id, */
883 		tech_pvt->call_id, /* uint32_t pass_thru_party_id, */
884 		SKINNY_PTIME, /* uint32_t ms_per_packet, */
885 		SKINNY_CODEC_ULAW_64K, /* uint32_t payload_capacity, */
886 		0, /* uint32_t echo_cancel_type, */
887 		0, /* uint32_t g723_bitrate, */
888 		tech_pvt->call_id, /* uint32_t conference_id2, */
889 		0 /* uint32_t reserved[10] */
890 		);
891 
892 	skinny_line_set_state(listener, line_instance, tech_pvt->call_id, SKINNY_CONNECTED);
893 	send_select_soft_keys(listener, line_instance, tech_pvt->call_id, SKINNY_KEY_SET_CONNECTED, 0xffff);
894 
895 	send_display_prompt_status_textid(listener, 0, SKINNY_TEXTID_CONNECTED, line_instance, tech_pvt->call_id);
896 	skinny_session_send_call_info(session, listener, line_instance);
897 
898 	return SWITCH_STATUS_SUCCESS;
899 }
900 
skinny_session_transfer(switch_core_session_t * session,listener_t * listener,uint32_t line_instance)901 switch_status_t skinny_session_transfer(switch_core_session_t *session, listener_t *listener, uint32_t line_instance)
902 {
903 	switch_status_t status = SWITCH_STATUS_SUCCESS;
904 	private_t *tech_pvt = NULL;
905 	switch_channel_t *channel = NULL;
906 	switch_channel_t *channel2 = NULL;
907 	const char *local_uuid = NULL;
908 	const char *local_uuid2 = NULL;
909 	const char *remote_uuid = NULL;
910 	const char *remote_uuid2 = NULL;
911 	switch_core_session_t *session2 = NULL;
912 	switch_core_session_t *rsession = NULL;
913 	private_t *tech_pvt2 = NULL;
914 
915 	switch_assert(session);
916 	switch_assert(listener);
917 	switch_assert(listener->profile);
918 
919 	tech_pvt = switch_core_session_get_private(session);
920 	channel = switch_core_session_get_channel(session);
921 	local_uuid = switch_channel_get_uuid(channel);
922 	remote_uuid = switch_channel_get_partner_uuid(channel);
923 
924 	if ( switch_core_session_get_partner(session, &rsession) == SWITCH_STATUS_SUCCESS )
925 	{
926 		switch_channel_t *rchannel = NULL;
927 		rchannel = switch_core_session_get_channel(rsession);
928 
929 		skinny_log_l_msg(listener, SWITCH_LOG_INFO, "SST: setting uuid bridge continue flag on remote channel\n");
930 
931 		switch_channel_set_variable(rchannel, "uuid_bridge_continue_on_cancel", "true");
932 		switch_core_session_rwunlock(rsession);
933 	}
934 
935 	skinny_log_l(listener, SWITCH_LOG_INFO, "SST: local_uuid=%s remote_uuid=%s\n", local_uuid, remote_uuid);
936 
937 	if (tech_pvt->transfer_from_call_id) {
938 		skinny_log_l_msg(listener, SWITCH_LOG_INFO, "SST: transfer_from_call_id\n");
939 
940 		if((session2 = skinny_profile_find_session(listener->profile, listener, &line_instance, tech_pvt->transfer_from_call_id))) {
941 			channel2 = switch_core_session_get_channel(session2);
942 			local_uuid2 = switch_channel_get_uuid(channel2);
943 			remote_uuid2 = switch_channel_get_partner_uuid(channel2);
944 			skinny_log_ls(listener, session2, SWITCH_LOG_INFO, "SST: tx from session - local_uuid=%s remote_uuid=%s local_uuid2=%s remote_uuid2=%s\n",
945 				local_uuid, remote_uuid, local_uuid2, remote_uuid2);
946 
947 			skinny_log_ls(listener, session2, SWITCH_LOG_INFO, "SST: attempting ivr bridge from (%s) to (%s)\n", remote_uuid, remote_uuid2);
948 
949 			if (switch_ivr_uuid_bridge(remote_uuid2, remote_uuid) == SWITCH_STATUS_SUCCESS) {
950 				skinny_log_ls_msg(listener, session2, SWITCH_LOG_INFO, "SST: success on uuid bridge\n");
951 
952 				switch_channel_hangup(channel, SWITCH_CAUSE_NORMAL_CLEARING);
953 				switch_channel_hangup(channel2, SWITCH_CAUSE_NORMAL_CLEARING);
954 			} else {
955 				skinny_log_ls_msg(listener, session2, SWITCH_LOG_INFO, "SST: failure on uuid bridge\n");
956 				/* TODO: How to inform the user that the bridge is not possible? */
957 			}
958 			switch_core_session_rwunlock(session2);
959 		}
960 	} else {
961 		skinny_log_l_msg(listener, SWITCH_LOG_INFO, "SST: !transfer_from_call_id\n");
962 
963 		if(remote_uuid) {
964 			skinny_log_ls_msg(listener, session2, SWITCH_LOG_INFO, "SST: found remote_uuid\n");
965 
966 			/* TODO CallSelectStat */
967 			skinny_log_ls_msg(listener, session2, SWITCH_LOG_INFO, "SST: creating incoming session\n");
968 			status = skinny_create_incoming_session(listener, &line_instance, &session2);
969 			if ( ! session2 ) {
970 				skinny_log_l_msg(listener, SWITCH_LOG_CRIT, "SST: Unable to create incoming session for transfer.\n");
971 				return SWITCH_STATUS_FALSE;
972 			}
973 			tech_pvt2 = switch_core_session_get_private(session2);
974 			tech_pvt2->transfer_from_call_id = tech_pvt->call_id;
975 			tech_pvt->transfer_to_call_id = tech_pvt2->call_id;
976 			skinny_log_ls(listener, session2, SWITCH_LOG_INFO, "SST: transfer_to_call_id=%d transfer_from_call_id=%d\n", tech_pvt2->call_id,
977 				tech_pvt->call_id);
978 			skinny_log_ls_msg(listener, session2, SWITCH_LOG_INFO, "SST: triggering dial on incoming session\n");
979 			skinny_session_process_dest(session2, listener, line_instance, NULL, '\0', 0);
980 
981 			channel2 = switch_core_session_get_channel(session2);
982 			local_uuid2 = switch_channel_get_uuid(channel2);
983 			remote_uuid2 = switch_channel_get_partner_uuid(channel2);
984 			skinny_log_ls(listener, session2, SWITCH_LOG_INFO, "SST: new session - local_uuid2=%s remote_uuid2=%s\n", local_uuid2, remote_uuid2);
985 
986 			switch_core_session_rwunlock(session2);
987 		} else {
988 			skinny_log_ls_msg(listener, session2, SWITCH_LOG_INFO, "SST: could not find remote_uuid\n");
989 
990 			/* TODO: How to inform the user that the bridge is not possible? */
991 		}
992 	}
993 	return status;
994 }
995 
skinny_session_stop_media(switch_core_session_t * session,listener_t * listener,uint32_t line_instance)996 switch_status_t skinny_session_stop_media(switch_core_session_t *session, listener_t *listener, uint32_t line_instance)
997 {
998 	private_t *tech_pvt = NULL;
999 
1000 	switch_assert(session);
1001 	switch_assert(listener);
1002 	switch_assert(listener->profile);
1003 
1004 	tech_pvt = switch_core_session_get_private(session);
1005 
1006 	switch_clear_flag_locked(tech_pvt, TFLAG_IO);
1007 
1008 	send_close_receive_channel(listener,
1009 			tech_pvt->call_id, /* uint32_t conference_id, */
1010 			tech_pvt->party_id, /* uint32_t pass_thru_party_id, */
1011 			tech_pvt->call_id /* uint32_t conference_id2, */
1012 			);
1013 	send_stop_media_transmission(listener,
1014 			tech_pvt->call_id, /* uint32_t conference_id, */
1015 			tech_pvt->party_id, /* uint32_t pass_thru_party_id, */
1016 			tech_pvt->call_id /* uint32_t conference_id2, */
1017 			);
1018 
1019 	return SWITCH_STATUS_SUCCESS;
1020 }
1021 
1022 struct skinny_hold_active_calls_helper {
1023 	listener_t *listener;
1024 };
1025 
skinny_hold_active_calls_callback(void * pArg,int argc,char ** argv,char ** columnNames)1026 int skinny_hold_active_calls_callback(void *pArg, int argc, char **argv, char **columnNames)
1027 {
1028 	struct skinny_hold_active_calls_helper *helper = pArg;
1029 	switch_core_session_t *session;
1030 
1031 	/* char *device_name = argv[0]; */
1032 	/* uint32_t device_instance = atoi(argv[1]); */
1033 	/* uint32_t position = atoi(argv[2]); */
1034 	uint32_t line_instance = atoi(argv[3]);
1035 	/* char *label = argv[4]; */
1036 	/* char *value = argv[5]; */
1037 	/* char *caller_name = argv[6]; */
1038 	/* uint32_t ring_on_idle = atoi(argv[7]); */
1039 	/* uint32_t ring_on_active = atoi(argv[8]); */
1040 	/* uint32_t busy_trigger = atoi(argv[9]); */
1041 	/* char *forward_all = argv[10]; */
1042 	/* char *forward_busy = argv[11]; */
1043 	/* char *forward_noanswer = argv[12]; */
1044 	/* uint32_t noanswer_duration = atoi(argv[13]); */
1045 	/* char *channel_uuid = argv[14]; */
1046 	uint32_t call_id = atoi(argv[15]);
1047 	/* uint32_t call_state = atoi(argv[16]); */
1048 
1049 	session = skinny_profile_find_session(helper->listener->profile, helper->listener, &line_instance, call_id);
1050 
1051 	if(session) {
1052 		skinny_session_hold_line(session, helper->listener, line_instance);
1053 		switch_core_session_rwunlock(session);
1054 	}
1055 
1056 	return 0;
1057 }
1058 
skinny_hold_active_calls(listener_t * listener)1059 switch_status_t skinny_hold_active_calls(listener_t *listener)
1060 {
1061 	struct skinny_hold_active_calls_helper helper = {0};
1062 	char *sql;
1063 
1064 	helper.listener = listener;
1065 
1066 	if ((sql = switch_mprintf(
1067 					"SELECT skinny_lines.*, channel_uuid, call_id, call_state "
1068 					"FROM skinny_active_lines "
1069 					"INNER JOIN skinny_lines "
1070 					"ON skinny_active_lines.device_name = skinny_lines.device_name "
1071 					"AND skinny_active_lines.device_instance = skinny_lines.device_instance "
1072 					"AND skinny_active_lines.line_instance = skinny_lines.line_instance "
1073 					"WHERE skinny_lines.device_name='%q' AND skinny_lines.device_instance=%d AND (call_state=%d OR call_state=%d)",
1074 					listener->device_name, listener->device_instance, SKINNY_PROCEED, SKINNY_CONNECTED))) {
1075 		skinny_execute_sql_callback(listener->profile, listener->profile->sql_mutex, sql, skinny_hold_active_calls_callback, &helper);
1076 		switch_safe_free(sql);
1077 	}
1078 
1079 	return SWITCH_STATUS_SUCCESS;
1080 }
1081 
1082 struct skinny_hangup_active_calls_helper {
1083     listener_t *listener;
1084 };
1085 
skinny_hangup_active_calls_callback(void * pArg,int argc,char ** argv,char ** columnNames)1086 int skinny_hangup_active_calls_callback(void *pArg, int argc, char **argv, char **columnNames)
1087 {
1088 	struct skinny_hangup_active_calls_helper *helper = pArg;
1089 	switch_core_session_t *session;
1090 
1091 	/* char *device_name = argv[0]; */
1092 	/* uint32_t device_instance = atoi(argv[1]); */
1093 	/* uint32_t position = atoi(argv[2]); */
1094 	uint32_t line_instance = atoi(argv[3]);
1095 	/* char *label = argv[4]; */
1096 	/* char *value = argv[5]; */
1097 	/* char *caller_name = argv[6]; */
1098 	/* uint32_t ring_on_idle = atoi(argv[7]); */
1099 	/* uint32_t ring_on_active = atoi(argv[8]); */
1100 	/* uint32_t busy_trigger = atoi(argv[9]); */
1101 	/* char *forward_all = argv[10]; */
1102 	/* char *forward_busy = argv[11]; */
1103 	/* char *forward_noanswer = argv[12]; */
1104 	/* uint32_t noanswer_duration = atoi(argv[13]); */
1105 	/* char *channel_uuid = argv[14]; */
1106 	uint32_t call_id = atoi(argv[15]);
1107 	uint32_t call_state = atoi(argv[16]);
1108 
1109 	session = skinny_profile_find_session(helper->listener->profile, helper->listener, &line_instance, call_id);
1110 
1111 	if(session) {
1112 		switch_channel_t *channel = NULL;
1113 		private_t *tech_pvt = NULL;
1114 
1115 		channel = switch_core_session_get_channel(session);
1116 		tech_pvt = switch_core_session_get_private(session);
1117 
1118 		if (tech_pvt->transfer_from_call_id) { /* Perform a blind transfer, instead of hanging up */
1119 			skinny_session_transfer(session, helper->listener, line_instance);
1120 		} else {
1121 			/* Hangup on an active call that is not in one of the states listed */
1122 			if ((call_state != SKINNY_ON_HOOK)&&(call_state != SKINNY_HOLD)&&(call_state != SKINNY_CALL_WAITING)&&(call_state != SKINNY_CALL_PARK)&&(call_state != SKINNY_IN_USE_REMOTELY)&&(call_state != SKINNY_RING_IN)) {
1123 				skinny_log_l(helper->listener, SWITCH_LOG_DEBUG, "Hangup Line Instance (%d), Call ID (%d), Line State (%d)\n", line_instance, tech_pvt->call_id, skinny_line_get_state(helper->listener,line_instance, call_id));
1124 				switch_channel_hangup(channel, SWITCH_CAUSE_NORMAL_CLEARING);
1125 			}
1126 		}
1127 
1128 		switch_core_session_rwunlock(session);
1129 	}
1130 
1131 	return 0;
1132 }
1133 
1134 /*****************************************************************************/
1135 /* SKINNY MESSAGE HANDLERS */
1136 /*****************************************************************************/
skinny_handle_keep_alive_message(listener_t * listener,skinny_message_t * request)1137 switch_status_t skinny_handle_keep_alive_message(listener_t *listener, skinny_message_t *request)
1138 {
1139 	keepalive_listener(listener, NULL);
1140 
1141 	send_keep_alive_ack(listener);
1142 
1143 	return SWITCH_STATUS_SUCCESS;
1144 }
1145 
skinny_handle_register(listener_t * listener,skinny_message_t * request)1146 switch_status_t skinny_handle_register(listener_t *listener, skinny_message_t *request)
1147 {
1148 	switch_status_t status = SWITCH_STATUS_FALSE;
1149 	skinny_profile_t *profile;
1150 	switch_event_t *event = NULL;
1151 	switch_event_t *params = NULL;
1152 	switch_xml_t xroot, xdomain, xgroup, xuser, xskinny, xparams, xparam, xbuttons, xbutton;
1153 	listener_t *listener2 = NULL;
1154 	char *sql;
1155 	assert(listener->profile);
1156 	profile = listener->profile;
1157 
1158 	skinny_check_data_length(request, sizeof(request->data.reg));
1159 
1160 	if (!zstr(listener->device_name)) {
1161 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
1162 				"A device is already registered on this listener.\n");
1163 		send_register_reject(listener, "A device is already registered on this listener");
1164 		return SWITCH_STATUS_FALSE;
1165 	}
1166 
1167 	/* Check directory */
1168 	skinny_device_event(listener, &params, SWITCH_EVENT_REQUEST_PARAMS, SWITCH_EVENT_SUBCLASS_ANY);
1169 	switch_event_add_header_string(params, SWITCH_STACK_BOTTOM, "action", "skinny-auth");
1170 
1171 	/* clean up all traces before adding to database */
1172 	skinny_lock_device_name(listener, request->data.reg.device_name);
1173 	skinny_clean_device_from_db(listener, request->data.reg.device_name);
1174 
1175 	if (switch_xml_locate_user("id", request->data.reg.device_name, profile->domain, "", &xroot, &xdomain, &xuser, &xgroup, params) != SWITCH_STATUS_SUCCESS) {
1176 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Can't find device [%s@%s]\n"
1177 				"You must define a domain called '%s' in your directory and add a user with id=\"%s\".\n"
1178 				, request->data.reg.device_name, profile->domain, profile->domain, request->data.reg.device_name);
1179 
1180 		/* unlock before trying to send response in case socket blocks */
1181 		skinny_unlock_device_name(listener, request->data.reg.device_name);
1182 
1183 		send_register_reject(listener, "Device not found");
1184 		status =  SWITCH_STATUS_FALSE;
1185 		goto end;
1186 	}
1187 
1188 	/* we clean up device above, so this below block will never trigger. I don't
1189 		know the full details of why there would be multiple listeners with
1190 		the same device - maybe a VGC or similar? Not really high priority for
1191 		support at the moment, but may need to revisit this later */
1192 
1193 	skinny_profile_find_listener_by_device_name_and_instance(listener->profile,
1194 			request->data.reg.device_name, request->data.reg.instance, &listener2);
1195 	if (listener2) {
1196 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
1197 				"Device %s:%d is already registered on another listener.\n",
1198 				request->data.reg.device_name, request->data.reg.instance);
1199 
1200 		/* unlock before trying to send response in case socket blocks */
1201 		skinny_unlock_device_name(listener, request->data.reg.device_name);
1202 
1203 		send_register_reject(listener, "Device is already registered on another listener");
1204 		status =  SWITCH_STATUS_FALSE;
1205 		goto end;
1206 	}
1207 
1208 	if ((sql = switch_mprintf(
1209 					"INSERT INTO skinny_devices "
1210 					"(name, user_id, instance, ip, type, max_streams, codec_string) "
1211 					"VALUES ('%q','%d','%d', '%q', '%d', '%d', '%q')",
1212 					request->data.reg.device_name,
1213 					request->data.reg.user_id,
1214 					request->data.reg.instance,
1215 					inet_ntoa(request->data.reg.ip),
1216 					request->data.reg.device_type,
1217 					request->data.reg.max_streams,
1218 					"" /* codec_string */
1219 				 ))) {
1220 		skinny_execute_sql(profile, sql, profile->sql_mutex);
1221 		switch_safe_free(sql);
1222 	}
1223 
1224 	switch_copy_string(listener->device_name, request->data.reg.device_name, 16);
1225 	listener->device_instance = request->data.reg.instance;
1226 	listener->device_type = request->data.reg.device_type;
1227 
1228 	skinny_unlock_device_name(listener, request->data.reg.device_name);
1229 
1230 	xskinny = switch_xml_child(xuser, "skinny");
1231 	if (xskinny) {
1232 		if ((xparams = switch_xml_child(xskinny, "params"))) {
1233 			for (xparam = switch_xml_child(xparams, "param"); xparam; xparam = xparam->next) {
1234 				const char *name = switch_xml_attr_soft(xparam, "name");
1235 				const char *value = switch_xml_attr_soft(xparam, "value");
1236 				if (!strcasecmp(name, "skinny-firmware-version")) {
1237 					switch_copy_string(listener->firmware_version, value, 16);
1238 				} else if (!strcasecmp(name, "skinny-soft-key-set-set")) {
1239 					listener->soft_key_set_set = switch_core_strdup(profile->pool, value);
1240 				} else if (!strcasecmp(name, "ext-voicemail")) {
1241 					if (!listener->ext_voicemail || strcmp(value,listener->ext_voicemail)) {
1242 						listener->ext_voicemail = switch_core_strdup(listener->pool, value);
1243 					}
1244 				} else if (!strcasecmp(name, "ext-redial")) {
1245 					if (!listener->ext_redial || strcmp(value,listener->ext_redial)) {
1246 						listener->ext_redial = switch_core_strdup(listener->pool, value);
1247 					}
1248 				} else if (!strcasecmp(name, "ext-meetme")) {
1249 					if (!listener->ext_meetme || strcmp(value,listener->ext_meetme)) {
1250 						listener->ext_meetme = switch_core_strdup(listener->pool, value);
1251 					}
1252 				} else if (!strcasecmp(name, "ext-pickup")) {
1253 					if (!listener->ext_pickup || strcmp(value,listener->ext_pickup)) {
1254 						listener->ext_pickup = switch_core_strdup(listener->pool, value);
1255 					}
1256 				} else if (!strcasecmp(name, "ext-cfwdall")) {
1257 					if (!listener->ext_cfwdall || strcmp(value,listener->ext_cfwdall)) {
1258 						listener->ext_cfwdall = switch_core_strdup(listener->pool, value);
1259 					}
1260 				} else if (!strcasecmp(name, "ext-autodial")) {
1261 					if (!listener->ext_autodial || strcmp(value,listener->ext_autodial)) {
1262 						listener->ext_autodial = switch_core_strdup(listener->pool, value);
1263 					}
1264 				}
1265 			}
1266 		}
1267 		if ((xbuttons = switch_xml_child(xskinny, "buttons"))) {
1268 			uint32_t line_instance = 1;
1269 			char *network_ip = inet_ntoa(request->data.reg.ip);
1270 			int network_port = 0;
1271 			char network_port_c[6];
1272 			snprintf(network_port_c, sizeof(network_port_c), "%d", network_port);
1273 			for (xbutton = switch_xml_child(xbuttons, "button"); xbutton; xbutton = xbutton->next) {
1274 				uint32_t position = atoi(switch_xml_attr_soft(xbutton, "position"));
1275 				uint32_t type = skinny_str2button(switch_xml_attr_soft(xbutton, "type"));
1276 				const char *label = switch_xml_attr_soft(xbutton, "label");
1277 				const char *value = switch_xml_attr_soft(xbutton, "value");
1278 				if(type ==  SKINNY_BUTTON_LINE) {
1279 					const char *caller_name = switch_xml_attr_soft(xbutton, "caller-name");
1280 					const char *reg_metadata = switch_xml_attr_soft(xbutton, "registration-metadata");
1281 					uint32_t ring_on_idle = 1;
1282 					uint32_t ring_on_active = 1;
1283 					uint32_t busy_trigger = atoi(switch_xml_attr_soft(xbutton, "busy-trigger"));
1284 					const char *forward_all = switch_xml_attr_soft(xbutton, "forward-all");
1285 					const char *forward_busy = switch_xml_attr_soft(xbutton, "forward-busy");
1286 					const char *forward_noanswer = switch_xml_attr_soft(xbutton, "forward-noanswer");
1287 					uint32_t noanswer_duration = atoi(switch_xml_attr_soft(xbutton, "noanswer-duration"));
1288 					const char *tmp;
1289 
1290 					tmp = switch_xml_attr_soft(xbutton, "ring-on-active");
1291 					if ( !zstr(tmp) ) {
1292 						ring_on_active = atoi(tmp);
1293 					}
1294 
1295 					tmp = switch_xml_attr_soft(xbutton, "ring-on-idle");
1296 					if ( !zstr(tmp) ) {
1297 						ring_on_idle = atoi(tmp);
1298 					}
1299 
1300 					if ((sql = switch_mprintf(
1301 									"INSERT INTO skinny_lines "
1302 									"(device_name, device_instance, position, line_instance, "
1303 									"label, value, caller_name, "
1304 									"ring_on_idle, ring_on_active, busy_trigger, "
1305 									"forward_all, forward_busy, forward_noanswer, noanswer_duration) "
1306 									"VALUES('%q', %d, %d, %d, '%q', '%q', '%q', %d, %d, %d, '%q', '%q', '%q', %d)",
1307 									request->data.reg.device_name, request->data.reg.instance, position, line_instance,
1308 									label, value, caller_name,
1309 									ring_on_idle, ring_on_active, busy_trigger,
1310 									forward_all, forward_busy, forward_noanswer, noanswer_duration))) {
1311 						char *token, *url;
1312 						skinny_execute_sql(profile, sql, profile->sql_mutex);
1313 						switch_safe_free(sql);
1314 						token = switch_mprintf("skinny/%q/%q/%q:%d", profile->name, value, request->data.reg.device_name, request->data.reg.instance);
1315 						url = switch_mprintf("skinny/%q/%q", profile->name, value);
1316 						switch_core_add_registration(value, profile->domain, token, url, 0, network_ip, network_port_c, "tcp", reg_metadata);
1317 						switch_safe_free(token);
1318 						switch_safe_free(url);
1319 					}
1320 					if (line_instance == 1) {
1321 						switch_event_t *message_query_event = NULL;
1322 						if (switch_event_create(&message_query_event, SWITCH_EVENT_MESSAGE_QUERY) == SWITCH_STATUS_SUCCESS) {
1323 							switch_event_add_header(message_query_event, SWITCH_STACK_BOTTOM, "Message-Account", "skinny:%s@%s", value, profile->domain);
1324 							switch_event_add_header_string(message_query_event, SWITCH_STACK_BOTTOM, "VM-Skinny-Profile", profile->name);
1325 							switch_event_fire(&message_query_event);
1326 						}
1327 					}
1328 					line_instance++;
1329 				} else {
1330 					const char *settings = switch_xml_attr_soft(xbutton, "settings");
1331 					if ((sql = switch_mprintf(
1332 									"INSERT INTO skinny_buttons "
1333 									"(device_name, device_instance, position, type, label, value, settings) "
1334 									"VALUES('%q', %d, %d, %d, '%q', '%q', '%q')",
1335 									request->data.reg.device_name,
1336 									request->data.reg.instance,
1337 									position,
1338 									type,
1339 									label,
1340 									value,
1341 									settings))) {
1342 						skinny_execute_sql(profile, sql, profile->sql_mutex);
1343 						switch_safe_free(sql);
1344 					}
1345 				}
1346 			}
1347 		}
1348 	}
1349 
1350 	status = SWITCH_STATUS_SUCCESS;
1351 
1352 	/* Reply with RegisterAckMessage */
1353 	send_register_ack(listener, profile->keep_alive, profile->date_format, "", profile->keep_alive, "");
1354 
1355 	/* Send CapabilitiesReqMessage */
1356 	send_capabilities_req(listener);
1357 
1358 	/* skinny::register event */
1359 	skinny_device_event(listener, &event, SWITCH_EVENT_CUSTOM, SKINNY_EVENT_REGISTER);
1360 	switch_event_fire(&event);
1361 
1362 	keepalive_listener(listener, NULL);
1363 
1364 end:
1365 	if (xroot) {
1366 		switch_xml_free(xroot);
1367 	}
1368 
1369 	if(params) {
1370 		switch_event_destroy(&params);
1371 	}
1372 
1373 	return status;
1374 }
1375 
skinny_handle_port_message(listener_t * listener,skinny_message_t * request)1376 switch_status_t skinny_handle_port_message(listener_t *listener, skinny_message_t *request)
1377 {
1378 	char *sql;
1379 	skinny_profile_t *profile;
1380 
1381 	switch_assert(listener->profile);
1382 	switch_assert(listener->device_name);
1383 
1384 	profile = listener->profile;
1385 
1386 	skinny_check_data_length(request, sizeof(request->data.as_uint16));
1387 
1388 	if ((sql = switch_mprintf(
1389 					"UPDATE skinny_devices SET port=%d WHERE name='%q' and instance=%d",
1390 					request->data.port.port,
1391 					listener->device_name,
1392 					listener->device_instance
1393 				 ))) {
1394 		skinny_execute_sql(profile, sql, profile->sql_mutex);
1395 		switch_safe_free(sql);
1396 	}
1397 	return SWITCH_STATUS_SUCCESS;
1398 }
1399 
skinny_handle_keypad_button_message(listener_t * listener,skinny_message_t * request)1400 switch_status_t skinny_handle_keypad_button_message(listener_t *listener, skinny_message_t *request)
1401 {
1402 	uint32_t line_instance = 1;
1403 	uint32_t call_id = 0;
1404 	switch_core_session_t *session = NULL;
1405 
1406 	skinny_check_data_length(request, sizeof(request->data.keypad_button.button));
1407 
1408 	if(skinny_check_data_length_soft(request, sizeof(request->data.keypad_button))) {
1409 		if (request->data.keypad_button.line_instance > 0) {
1410 			line_instance = request->data.keypad_button.line_instance;
1411 		}
1412 		call_id = request->data.keypad_button.call_id;
1413 	}
1414 
1415 	session = skinny_profile_find_session(listener->profile, listener, &line_instance, call_id);
1416 	if ( !session )
1417 	{
1418 		line_instance = 0;
1419 		session = skinny_profile_find_session(listener->profile, listener, &line_instance, 0);
1420 	}
1421 
1422 	if(session) {
1423 		switch_channel_t *channel = NULL;
1424 		private_t *tech_pvt = NULL;
1425 		char digit = '\0';
1426 
1427 		channel = switch_core_session_get_channel(session);
1428 		tech_pvt = switch_core_session_get_private(session);
1429 
1430 		switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "SEND DTMF ON CALL %d [%d]\n", tech_pvt->call_id, request->data.keypad_button.button);
1431 
1432 		if (request->data.keypad_button.button == 14) {
1433 			digit = '*';
1434 		} else if (request->data.keypad_button.button == 15) {
1435 			digit = '#';
1436 		} else if (request->data.keypad_button.button <= 9) { /* unsigned, so guaranteed to be >= 0 */
1437 			digit = '0' + request->data.keypad_button.button;
1438 		} else {
1439 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "UNKNOW DTMF RECEIVED ON CALL %d [%d]\n", tech_pvt->call_id, request->data.keypad_button.button);
1440 		}
1441 
1442 		/* TODO check call_id and line */
1443 
1444 		if((skinny_line_get_state(listener, line_instance, tech_pvt->call_id) == SKINNY_OFF_HOOK)) {
1445 
1446 			skinny_session_process_dest(session, listener, line_instance, NULL, digit, 0);
1447 		} else {
1448 			if(digit != '\0') {
1449 				switch_dtmf_t dtmf = { 0, switch_core_default_dtmf_duration(0)};
1450 				dtmf.digit = digit;
1451 				switch_channel_queue_dtmf(channel, &dtmf);
1452 			}
1453 		}
1454 	}
1455 
1456 	if(session) {
1457 		switch_core_session_rwunlock(session);
1458 	}
1459 
1460 	return SWITCH_STATUS_SUCCESS;
1461 }
1462 
skinny_handle_enbloc_call_message(listener_t * listener,skinny_message_t * request)1463 switch_status_t skinny_handle_enbloc_call_message(listener_t *listener, skinny_message_t *request)
1464 {
1465 	uint32_t line_instance = 1;
1466 	switch_core_session_t *session = NULL;
1467 
1468 	skinny_check_data_length(request, sizeof(request->data.enbloc_call.called_party));
1469 
1470 	if(skinny_check_data_length_soft(request, sizeof(request->data.enbloc_call))) {
1471 		if (request->data.enbloc_call.line_instance > 0) {
1472 			line_instance = request->data.enbloc_call.line_instance;
1473 		}
1474 	}
1475 
1476 	session = skinny_profile_find_session(listener->profile, listener, &line_instance, 0);
1477 
1478 	if(session) {
1479 		skinny_session_process_dest(session, listener, line_instance, request->data.enbloc_call.called_party, '\0', 0);
1480 		switch_core_session_rwunlock(session);
1481 	}
1482 
1483 	return SWITCH_STATUS_SUCCESS;
1484 }
1485 
skinny_handle_stimulus_message(listener_t * listener,skinny_message_t * request)1486 switch_status_t skinny_handle_stimulus_message(listener_t *listener, skinny_message_t *request)
1487 {
1488 	switch_status_t status = SWITCH_STATUS_SUCCESS;
1489 	uint32_t line_instance = 0;
1490 	uint32_t call_id = 0;
1491 	switch_core_session_t *session = NULL;
1492 	struct speed_dial_stat_res_message *button_speed_dial = NULL;
1493 	struct line_stat_res_message *button_line = NULL;
1494 	uint32_t line_state;
1495 
1496 	switch_channel_t *channel = NULL;
1497 
1498 	skinny_check_data_length(request, sizeof(request->data.stimulus)-sizeof(request->data.stimulus.call_id));
1499 
1500 	if(skinny_check_data_length_soft(request, sizeof(request->data.stimulus))) {
1501 		call_id = request->data.stimulus.call_id;
1502 	}
1503 
1504 	skinny_log_l(listener, SWITCH_LOG_DEBUG, "Received stimulus message of type (%s)\n",
1505 		skinny_button2str(request->data.stimulus.instance_type));
1506 
1507 	switch(request->data.stimulus.instance_type) {
1508 		case SKINNY_BUTTON_LAST_NUMBER_REDIAL:
1509 			skinny_create_incoming_session(listener, &line_instance, &session);
1510 			if ( ! session ) {
1511 				skinny_log_l_msg(listener, SWITCH_LOG_CRIT, "Unable to handle last number redial stimulus message, couldn't create incoming session.\n");
1512 				return SWITCH_STATUS_FALSE;
1513 			}
1514 			skinny_session_process_dest(session, listener, line_instance,
1515 				empty_null2(listener->ext_redial,listener->profile->ext_redial), '\0', 0);
1516 			break;
1517 		case SKINNY_BUTTON_SPEED_DIAL:
1518 			skinny_speed_dial_get(listener, request->data.stimulus.instance, &button_speed_dial);
1519 
1520 			session = skinny_profile_find_session(listener->profile, listener, &line_instance, 0);
1521 			if(strlen(button_speed_dial->line) > 0) {
1522 				if ( !session ) {
1523 					skinny_create_incoming_session(listener, &line_instance, &session);
1524 				}
1525 				if ( !session ) {
1526 					skinny_log_l_msg(listener, SWITCH_LOG_CRIT, "Unable to handle speed dial stimulus message, couldn't create incoming session.\n");
1527 					switch_safe_free(button_speed_dial);
1528 					return SWITCH_STATUS_FALSE;
1529 				}
1530 				skinny_session_process_dest(session, listener, line_instance, button_speed_dial->line, '\0', 0);
1531 			}
1532 			switch_safe_free(button_speed_dial);
1533 			break;
1534 		case SKINNY_BUTTON_HOLD:
1535 			session = skinny_profile_find_session(listener->profile, listener, &line_instance, call_id);
1536 
1537 			if(session) {
1538 				status = skinny_session_hold_line(session, listener, line_instance);
1539 			}
1540 			break;
1541 		case SKINNY_BUTTON_TRANSFER:
1542 			session = skinny_profile_find_session(listener->profile, listener, &line_instance, call_id);
1543 
1544 			if(session) {
1545 				status = skinny_session_transfer(session, listener, line_instance);
1546 			}
1547 			break;
1548 		case SKINNY_BUTTON_VOICEMAIL:
1549 			skinny_create_incoming_session(listener, &line_instance, &session);
1550 			if ( ! session ) {
1551 				skinny_log_l_msg(listener, SWITCH_LOG_CRIT, "Unable to handle stimulus message, couldn't create incoming session.\n");
1552 				return SWITCH_STATUS_FALSE;
1553 			}
1554 			skinny_session_process_dest(session, listener, line_instance,
1555 				empty_null2(listener->ext_voicemail, listener->profile->ext_voicemail), '\0', 0);
1556 			break;
1557 
1558 		case SKINNY_BUTTON_LINE:
1559 			// Get the button data
1560 			skinny_line_get(listener, request->data.stimulus.instance, &button_line);
1561 
1562 			// Set the button and try to open the incoming session with this
1563 			line_instance = button_line->number;
1564 			session = skinny_profile_find_session(listener->profile, listener, &line_instance, call_id);
1565 
1566 			// If session and line match, answer the call
1567 			if ( session && line_instance == button_line->number ) {
1568 				line_state = skinny_line_get_state(listener, line_instance, call_id);
1569 
1570 				if(line_state == SKINNY_OFF_HOOK) {
1571 					channel = switch_core_session_get_channel(session);
1572 					if (switch_channel_test_flag(channel, CF_HOLD)) {
1573 						switch_ivr_unhold(session);
1574 					}
1575 
1576 					switch_channel_hangup(channel, SWITCH_CAUSE_NORMAL_CLEARING);
1577 				}
1578 				else {
1579 					status = skinny_session_answer(session, listener, line_instance);
1580 				}
1581 			}
1582 			else {
1583 				if(skinny_check_data_length_soft(request, sizeof(request->data.soft_key_event))) {
1584 					line_instance = request->data.soft_key_event.line_instance;
1585 				}
1586 
1587 				skinny_create_incoming_session(listener, &line_instance, &session);
1588 				if ( ! session ) {
1589 					skinny_log_l_msg(listener, SWITCH_LOG_CRIT, "Unable to handle stimulus message, couldn't create incoming session.\n");
1590 					switch_safe_free(button_line);
1591 					return SWITCH_STATUS_FALSE;
1592 				}
1593 				skinny_session_process_dest(session, listener, line_instance, NULL, '\0', 0);
1594 			}
1595 			switch_safe_free(button_line);
1596 			break;
1597 
1598 		default:
1599 			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Unknown Stimulus Type Received [%d]\n", request->data.stimulus.instance_type);
1600 	}
1601 
1602 	if(session) {
1603 		switch_core_session_rwunlock(session);
1604 	}
1605 
1606 	return status;
1607 }
1608 
skinny_handle_off_hook_message(listener_t * listener,skinny_message_t * request)1609 switch_status_t skinny_handle_off_hook_message(listener_t *listener, skinny_message_t *request)
1610 {
1611 	uint32_t line_instance = 1;
1612 	uint32_t call_id = 0;
1613 	switch_core_session_t *session = NULL;
1614 	private_t *tech_pvt = NULL;
1615 	uint32_t line_state;
1616 
1617 	if(skinny_check_data_length_soft(request, sizeof(request->data.off_hook))) {
1618 		if (request->data.off_hook.line_instance > 0) {
1619 			line_instance = request->data.off_hook.line_instance;
1620 		}
1621 		call_id = request->data.off_hook.call_id;
1622 	}
1623 	skinny_log_l(listener, SWITCH_LOG_INFO, "Attempting to handle off hook message for call_id %d and line_instance %d.\n", call_id, line_instance);
1624 
1625 	session = skinny_profile_find_session(listener->profile, listener, &line_instance, call_id);
1626 
1627 	line_state = skinny_line_get_state(listener, line_instance, call_id);
1628 
1629 	if(session && line_state == SKINNY_RING_IN ) { /*answering a call */
1630 		skinny_session_answer(session, listener, line_instance);
1631 	} else { /* start a new call */
1632 
1633 		skinny_create_incoming_session(listener, &line_instance, &session);
1634 		if ( ! session ) {
1635 			skinny_log_l_msg(listener, SWITCH_LOG_CRIT, "Unable to handle off hook message, could not create session.\n");
1636 			return SWITCH_STATUS_FALSE;
1637 		}
1638 		tech_pvt = switch_core_session_get_private(session);
1639 		assert(tech_pvt != NULL);
1640 
1641 		skinny_session_process_dest(session, listener, line_instance, NULL, '\0', 0);
1642 	}
1643 
1644 	if(session) {
1645 		switch_core_session_rwunlock(session);
1646 	}
1647 
1648 	return SWITCH_STATUS_SUCCESS;
1649 }
1650 
skinny_handle_on_hook_message(listener_t * listener,skinny_message_t * request)1651 switch_status_t skinny_handle_on_hook_message(listener_t *listener, skinny_message_t *request)
1652 {
1653 	switch_status_t status = SWITCH_STATUS_SUCCESS;
1654 	uint32_t line_instance = 0;
1655 	uint32_t call_id = 0;
1656 	struct skinny_hangup_active_calls_helper helper = {0};
1657 	char *sql;
1658 
1659 	if(skinny_check_data_length_soft(request, sizeof(request->data.on_hook))) {
1660 		line_instance = request->data.on_hook.line_instance;
1661 		call_id = request->data.on_hook.call_id;
1662 	}
1663 	skinny_log_l(listener, SWITCH_LOG_INFO, "Attempting to handle on hook message for Call ID (%d), Line Instance (%d).\n", call_id, line_instance);
1664 
1665 	/* Walk through all active calls for this device. The callback should hangup any active calls that should be terminated when the device goes on-hook */
1666 
1667 	helper.listener = listener;
1668 
1669 	if ((sql = switch_mprintf(
1670 		"SELECT skinny_lines.*, channel_uuid, call_id, call_state "
1671 		"FROM skinny_active_lines "
1672 		"INNER JOIN skinny_lines "
1673 		"ON skinny_active_lines.device_name = skinny_lines.device_name "
1674 		"AND skinny_active_lines.device_instance = skinny_lines.device_instance "
1675 		"AND skinny_active_lines.line_instance = skinny_lines.line_instance "
1676 		"WHERE skinny_lines.device_name='%q' AND skinny_lines.device_instance=%d",
1677 		listener->device_name, listener->device_instance)))
1678 	{
1679 		skinny_execute_sql_callback(listener->profile, listener->profile->sql_mutex, sql, skinny_hangup_active_calls_callback, &helper);
1680 		switch_safe_free(sql);
1681 	}
1682 
1683 	return status;
1684 }
1685 
skinny_handle_forward_stat_req_message(listener_t * listener,skinny_message_t * request)1686 switch_status_t skinny_handle_forward_stat_req_message(listener_t *listener, skinny_message_t *request)
1687 {
1688 	skinny_message_t *message;
1689 
1690 	skinny_check_data_length(request, sizeof(request->data.forward_stat_req));
1691 
1692 	skinny_create_message(message, FORWARD_STAT_MESSAGE, forward_stat);
1693 
1694 	message->data.forward_stat.line_instance = request->data.forward_stat_req.line_instance;
1695 
1696 	if ( listener->profile->debug >= 9 ) {
1697 		skinny_log_l(listener, SWITCH_LOG_DEBUG, "Handle Forward Stat Req Message with Line Instance (%d)\n",
1698 			request->data.forward_stat_req.line_instance);
1699 	}
1700 	skinny_send_reply_quiet(listener, message, SWITCH_TRUE);
1701 
1702 	return SWITCH_STATUS_SUCCESS;
1703 }
1704 
skinny_handle_speed_dial_stat_request(listener_t * listener,skinny_message_t * request)1705 switch_status_t skinny_handle_speed_dial_stat_request(listener_t *listener, skinny_message_t *request)
1706 {
1707 	struct speed_dial_stat_res_message *button = NULL;
1708 
1709 	skinny_check_data_length(request, sizeof(request->data.speed_dial_req));
1710 
1711 	if ( listener->profile->debug >= 9 ) {
1712 		skinny_log_l(listener, SWITCH_LOG_DEBUG, "Handle Speed Dial Stat Request for Number (%d)\n", request->data.speed_dial_req.number);
1713 	}
1714 
1715 	skinny_speed_dial_get(listener, request->data.speed_dial_req.number, &button);
1716 
1717 	send_speed_dial_stat_res(listener, request->data.speed_dial_req.number, button->line, button->label);
1718 
1719 	switch_safe_free(button);
1720 
1721 	return SWITCH_STATUS_SUCCESS;
1722 }
1723 
skinny_handle_line_stat_request(listener_t * listener,skinny_message_t * request)1724 switch_status_t skinny_handle_line_stat_request(listener_t *listener, skinny_message_t *request)
1725 {
1726 	skinny_message_t *message;
1727 	struct line_stat_res_message *button = NULL;
1728 
1729 	skinny_check_data_length(request, sizeof(request->data.line_req));
1730 
1731 	skinny_create_message(message, LINE_STAT_RES_MESSAGE, line_res);
1732 
1733 	skinny_line_get(listener, request->data.line_req.number, &button);
1734 
1735 	memcpy(&message->data.line_res, button, sizeof(struct line_stat_res_message));
1736 
1737 	switch_safe_free(button);
1738 
1739 	skinny_send_reply(listener, message, SWITCH_TRUE);
1740 
1741 	return SWITCH_STATUS_SUCCESS;
1742 }
1743 
skinny_config_stat_res_callback(void * pArg,int argc,char ** argv,char ** columnNames)1744 int skinny_config_stat_res_callback(void *pArg, int argc, char **argv, char **columnNames)
1745 {
1746 	skinny_message_t *message = pArg;
1747 	char *device_name = argv[0];
1748 	int user_id = atoi(argv[1]);
1749 	int instance = atoi(argv[2]);
1750 	char *user_name = argv[3];
1751 	char *server_name = argv[4];
1752 	int number_lines = atoi(argv[5]);
1753 	int number_speed_dials = atoi(argv[6]);
1754 
1755 	switch_copy_string(message->data.config_res.device_name, device_name, 16);
1756 	message->data.config_res.user_id = user_id;
1757 	message->data.config_res.instance = instance;
1758 	switch_copy_string(message->data.config_res.user_name, user_name, 40);
1759 	switch_copy_string(message->data.config_res.server_name, server_name, 40);
1760 	message->data.config_res.number_lines = number_lines;
1761 	message->data.config_res.number_speed_dials = number_speed_dials;
1762 
1763 	return 0;
1764 }
1765 
skinny_handle_config_stat_request(listener_t * listener,skinny_message_t * request)1766 switch_status_t skinny_handle_config_stat_request(listener_t *listener, skinny_message_t *request)
1767 {
1768 	char *sql;
1769 	skinny_message_t *message;
1770 	skinny_profile_t *profile;
1771 
1772 	switch_assert(listener->profile);
1773 	switch_assert(listener->device_name);
1774 
1775 	profile = listener->profile;
1776 
1777 	skinny_create_message(message, CONFIG_STAT_RES_MESSAGE, config_res);
1778 
1779 	if ((sql = switch_mprintf(
1780 					"SELECT name, user_id, instance, '' AS user_name, '' AS server_name, "
1781 					"(SELECT COUNT(*) FROM skinny_lines WHERE device_name='%q' AND device_instance=%d) AS number_lines, "
1782 					"(SELECT COUNT(*) FROM skinny_buttons WHERE device_name='%q' AND device_instance=%d AND type=%d) AS number_speed_dials "
1783 					"FROM skinny_devices WHERE name='%q' ",
1784 					listener->device_name,
1785 					listener->device_instance,
1786 					listener->device_name,
1787 					listener->device_instance,
1788 					SKINNY_BUTTON_SPEED_DIAL,
1789 					listener->device_name
1790 				 ))) {
1791 		skinny_execute_sql_callback(profile, profile->sql_mutex, sql, skinny_config_stat_res_callback, message);
1792 		switch_safe_free(sql);
1793 	}
1794 	skinny_send_reply(listener, message, SWITCH_TRUE);
1795 
1796 	return SWITCH_STATUS_SUCCESS;
1797 }
1798 
skinny_handle_time_date_request(listener_t * listener,skinny_message_t * request)1799 switch_status_t skinny_handle_time_date_request(listener_t *listener, skinny_message_t *request)
1800 {
1801 	return send_define_current_time_date(listener);
1802 }
1803 
1804 struct button_template_helper {
1805 	skinny_message_t *message;
1806 	int count[SKINNY_BUTTON_UNDEFINED+1];
1807 	int max_position;
1808 };
1809 
skinny_handle_button_template_request_callback(void * pArg,int argc,char ** argv,char ** columnNames)1810 int skinny_handle_button_template_request_callback(void *pArg, int argc, char **argv, char **columnNames)
1811 {
1812 	struct button_template_helper *helper = pArg;
1813 	skinny_message_t *message = helper->message;
1814 	/* char *device_name = argv[0]; */
1815 	/* uint32_t device_instance = argv[1]; */
1816 	int position = atoi(argv[2]);
1817 	uint32_t type = atoi(argv[3]);
1818 	/* int relative_position = atoi(argv[4]); */
1819 
1820 	message->data.button_template.btn[position-1].instance_number = ++helper->count[type];
1821 	message->data.button_template.btn[position-1].button_definition = type;
1822 
1823 	message->data.button_template.button_count++;
1824 	message->data.button_template.total_button_count++;
1825 	if(position > helper->max_position) {
1826 		helper->max_position = position;
1827 	}
1828 
1829 	return 0;
1830 }
1831 
skinny_handle_button_template_request(listener_t * listener,skinny_message_t * request)1832 switch_status_t skinny_handle_button_template_request(listener_t *listener, skinny_message_t *request)
1833 {
1834 	skinny_message_t *message;
1835 	struct button_template_helper helper = {0};
1836 	skinny_profile_t *profile;
1837 	char *sql;
1838 	int i;
1839 
1840 	switch_assert(listener->profile);
1841 	switch_assert(listener->device_name);
1842 
1843 	profile = listener->profile;
1844 
1845 	skinny_create_message(message, BUTTON_TEMPLATE_RES_MESSAGE, button_template);
1846 
1847 	message->data.button_template.button_offset = 0;
1848 	message->data.button_template.button_count = 0;
1849 	message->data.button_template.total_button_count = 0;
1850 
1851 	helper.message = message;
1852 
1853 	/* Add buttons */
1854 	if ((sql = switch_mprintf(
1855 					"SELECT device_name, device_instance, position, type "
1856 					"FROM skinny_buttons "
1857 					"WHERE device_name='%q' AND device_instance=%d "
1858 					"ORDER BY position",
1859 					listener->device_name, listener->device_instance
1860 				 ))) {
1861 		skinny_execute_sql_callback(profile, profile->sql_mutex, sql, skinny_handle_button_template_request_callback, &helper);
1862 		switch_safe_free(sql);
1863 	}
1864 
1865 	/* Add lines */
1866 	if ((sql = switch_mprintf(
1867 					"SELECT device_name, device_instance, position, %d AS type "
1868 					"FROM skinny_lines "
1869 					"WHERE device_name='%q' AND device_instance=%d "
1870 					"ORDER BY position",
1871 					SKINNY_BUTTON_LINE,
1872 					listener->device_name, listener->device_instance
1873 				 ))) {
1874 		skinny_execute_sql_callback(profile, profile->sql_mutex, sql, skinny_handle_button_template_request_callback, &helper);
1875 		switch_safe_free(sql);
1876 	}
1877 
1878 	/* Fill remaining buttons with Undefined */
1879 	for(i = 0; i+1 < helper.max_position; i++) {
1880 		if(message->data.button_template.btn[i].button_definition == SKINNY_BUTTON_UNKNOWN) {
1881 			message->data.button_template.btn[i].instance_number = ++helper.count[SKINNY_BUTTON_UNDEFINED];
1882 			message->data.button_template.btn[i].button_definition = SKINNY_BUTTON_UNDEFINED;
1883 			message->data.button_template.button_count++;
1884 			message->data.button_template.total_button_count++;
1885 		}
1886 	}
1887 
1888 	return skinny_send_reply(listener, message, SWITCH_TRUE);
1889 }
1890 
skinny_handle_version_request(listener_t * listener,skinny_message_t * request)1891 switch_status_t skinny_handle_version_request(listener_t *listener, skinny_message_t *request)
1892 {
1893 	int saw_entry = 0;
1894 
1895 	if (zstr(listener->firmware_version)) {
1896 		char *id_str;
1897 		skinny_device_type_params_t *params;
1898 		id_str = switch_mprintf("%d", listener->device_type);
1899 		params = (skinny_device_type_params_t *) switch_core_hash_find(listener->profile->device_type_params_hash, id_str);
1900 		if (params) {
1901 			saw_entry = 1;
1902 
1903 			if (!zstr(params->firmware_version)) {
1904 				switch_copy_string(listener->firmware_version, params->firmware_version, 16);
1905 			}
1906 		}
1907 	}
1908 
1909 	if (!zstr(listener->firmware_version)) {
1910 		return send_version(listener, listener->firmware_version);
1911 	} else if (saw_entry) {
1912 		/* found entry with an empty string */
1913 		return send_version(listener, "");
1914 	} else {
1915 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
1916 				"Device %s:%d is requesting for firmware version, but none is set.\n",
1917 				listener->device_name, listener->device_instance);
1918 
1919 		/* CCM sends back an answer, but with all nulls */
1920 		return send_version(listener, "");
1921 	}
1922 }
1923 
skinny_handle_capabilities_response(listener_t * listener,skinny_message_t * request)1924 switch_status_t skinny_handle_capabilities_response(listener_t *listener, skinny_message_t *request)
1925 {
1926 	char *sql;
1927 	skinny_profile_t *profile;
1928 
1929 	uint32_t i = 0;
1930 	uint32_t n = 0;
1931 	char *codec_order[SWITCH_MAX_CODECS] = {0};
1932 	char *codec_string;
1933 
1934 	size_t string_len, string_pos, pos;
1935 
1936 	switch_assert(listener->profile);
1937 	switch_assert(listener->device_name);
1938 
1939 	profile = listener->profile;
1940 
1941 	skinny_check_data_length(request, sizeof(request->data.cap_res.count));
1942 
1943 	n = request->data.cap_res.count;
1944 	if (n > SWITCH_MAX_CODECS) {
1945 		n = SWITCH_MAX_CODECS;
1946 	}
1947 	string_len = -1;
1948 
1949 	skinny_check_data_length(request, sizeof(request->data.cap_res.count) + n * sizeof(request->data.cap_res.caps[0]));
1950 
1951 	for (i = 0; i < n; i++) {
1952 		char *codec = skinny_codec2string(request->data.cap_res.caps[i].codec);
1953 		codec_order[i] = codec;
1954 		string_len += strlen(codec)+1;
1955 	}
1956 	i = 0;
1957 	pos = 0;
1958 
1959 	if ( string_len > SKINNY_MAX_STRING ) {
1960 		skinny_log_l_msg(listener, SWITCH_LOG_ERROR, "Codec string list too long.\n");
1961 		return SWITCH_STATUS_FALSE;
1962 	}
1963 
1964 	codec_string = calloc(string_len+1,1);
1965 	if ( !codec_string ) {
1966 		skinny_log_l_msg(listener, SWITCH_LOG_ERROR, "Unable to allocate memory for codec string.\n");
1967 		return SWITCH_STATUS_FALSE;
1968 	}
1969 	for (string_pos = 0; string_pos < string_len; string_pos++) {
1970 		char *codec = codec_order[i];
1971 		switch_assert(i < n);
1972 		if(pos == strlen(codec)) {
1973 			codec_string[string_pos] = ',';
1974 			i++;
1975 			pos = 0;
1976 		} else {
1977 			codec_string[string_pos] = codec[pos++];
1978 		}
1979 	}
1980 	codec_string[string_len] = '\0';
1981 	if ((sql = switch_mprintf(
1982 					"UPDATE skinny_devices SET codec_string='%q' WHERE name='%q'",
1983 					codec_string,
1984 					listener->device_name
1985 				 ))) {
1986 		skinny_execute_sql(profile, sql, profile->sql_mutex);
1987 		switch_safe_free(sql);
1988 	}
1989 	if ( listener->profile->debug >= 9 ) {
1990 		skinny_log_l(listener, SWITCH_LOG_DEBUG, "Codecs %s supported.\n", codec_string);
1991 	}
1992 	switch_safe_free(codec_string);
1993 	return SWITCH_STATUS_SUCCESS;
1994 }
1995 
skinny_handle_alarm(listener_t * listener,skinny_message_t * request)1996 switch_status_t skinny_handle_alarm(listener_t *listener, skinny_message_t *request)
1997 {
1998 	switch_event_t *event = NULL;
1999 
2000 	skinny_check_data_length(request, sizeof(request->data.alarm));
2001 
2002 	skinny_log_l(listener, SWITCH_LOG_DEBUG, "Received alarm: Severity=%d, DisplayMessage=%s, Param1=%d, Param2=%d.\n",
2003 		request->data.alarm.alarm_severity, request->data.alarm.display_message,
2004 		request->data.alarm.alarm_param1, request->data.alarm.alarm_param2);
2005 
2006 	/* skinny::alarm event */
2007 	skinny_device_event(listener, &event, SWITCH_EVENT_CUSTOM, SKINNY_EVENT_ALARM);
2008 	switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Skinny-Alarm-Severity", "%d", request->data.alarm.alarm_severity);
2009 	switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Skinny-Alarm-DisplayMessage", "%s", request->data.alarm.display_message);
2010 	switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Skinny-Alarm-Param1", "%d", request->data.alarm.alarm_param1);
2011 	switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Skinny-Alarm-Param2", "%d", request->data.alarm.alarm_param2);
2012 	switch_event_fire(&event);
2013 
2014 	return SWITCH_STATUS_SUCCESS;
2015 }
2016 
skinny_handle_open_receive_channel_ack_message(listener_t * listener,skinny_message_t * request)2017 switch_status_t skinny_handle_open_receive_channel_ack_message(listener_t *listener, skinny_message_t *request)
2018 {
2019 	switch_status_t status = SWITCH_STATUS_SUCCESS;
2020 	uint32_t line_instance = 0;
2021 	switch_core_session_t *session;
2022 
2023 	skinny_check_data_length(request, sizeof(request->data.open_receive_channel_ack));
2024 
2025 	session = skinny_profile_find_session(listener->profile, listener, &line_instance, request->data.open_receive_channel_ack.pass_thru_party_id);
2026 
2027 	if(session) {
2028 		const char *err = NULL;
2029 		private_t *tech_pvt = NULL;
2030 		switch_channel_t *channel = NULL;
2031 		struct in_addr addr;
2032 		switch_rtp_flag_t flags[SWITCH_RTP_FLAG_INVALID] = {0};
2033 
2034 		tech_pvt = switch_core_session_get_private(session);
2035 		channel = switch_core_session_get_channel(session);
2036 
2037 		/* Codec */
2038 		tech_pvt->iananame = "PCMU"; /* TODO */
2039 		tech_pvt->codec_ms = SKINNY_PTIME; /* TODO */
2040 		tech_pvt->rm_rate = 8000; /* TODO */
2041 		tech_pvt->rm_fmtp = NULL; /* TODO */
2042 		tech_pvt->agreed_pt = (switch_payload_t) 0; /* TODO */
2043 		tech_pvt->rm_encoding = switch_core_strdup(switch_core_session_get_pool(session), "");
2044 		skinny_tech_set_codec(tech_pvt, 0);
2045 		if ((status = skinny_tech_set_codec(tech_pvt, 0)) != SWITCH_STATUS_SUCCESS) {
2046 			goto end;
2047 		}
2048 
2049 		tech_pvt->local_sdp_audio_ip = listener->local_ip;
2050 		/* Request a local port from the core's allocator */
2051 		if (!(tech_pvt->local_sdp_audio_port = switch_rtp_request_port(tech_pvt->local_sdp_audio_ip))) {
2052 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_CRIT, "No RTP ports available!\n");
2053 			return SWITCH_STATUS_FALSE;
2054 		}
2055 
2056 		tech_pvt->remote_sdp_audio_ip = inet_ntoa(request->data.open_receive_channel_ack.ip);
2057 		tech_pvt->remote_sdp_audio_port = request->data.open_receive_channel_ack.port;
2058 
2059 		tech_pvt->rtp_session = switch_rtp_new(tech_pvt->local_sdp_audio_ip,
2060 				tech_pvt->local_sdp_audio_port,
2061 				tech_pvt->remote_sdp_audio_ip,
2062 				tech_pvt->remote_sdp_audio_port,
2063 				tech_pvt->agreed_pt,
2064 				tech_pvt->read_impl.samples_per_packet,
2065 				tech_pvt->codec_ms * 1000,
2066 				flags, "soft", &err,
2067 				switch_core_session_get_pool(session), 0, 0);
2068 		switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG,
2069 				"AUDIO RTP [%s] %s:%d->%s:%d codec: %u ms: %d [%s]\n",
2070 				switch_channel_get_name(channel),
2071 				tech_pvt->local_sdp_audio_ip,
2072 				tech_pvt->local_sdp_audio_port,
2073 				tech_pvt->remote_sdp_audio_ip,
2074 				tech_pvt->remote_sdp_audio_port,
2075 				tech_pvt->agreed_pt,
2076 				tech_pvt->read_impl.microseconds_per_packet / 1000,
2077 				switch_rtp_ready(tech_pvt->rtp_session) ? "SUCCESS" : err);
2078 #ifdef WIN32
2079 		addr.s_addr = inet_addr(tech_pvt->local_sdp_audio_ip);
2080 #else
2081 		inet_aton(tech_pvt->local_sdp_audio_ip, &addr);
2082 #endif
2083 		send_start_media_transmission(listener,
2084 				tech_pvt->call_id, /* uint32_t conference_id, */
2085 				tech_pvt->party_id, /* uint32_t pass_thru_party_id, */
2086 				addr.s_addr, /* uint32_t remote_ip, */
2087 				tech_pvt->local_sdp_audio_port, /* uint32_t remote_port, */
2088 				SKINNY_PTIME, /* uint32_t ms_per_packet, */
2089 				SKINNY_CODEC_ULAW_64K, /* uint32_t payload_capacity, */
2090 				184, /* uint32_t precedence, */
2091 				0, /* uint32_t silence_suppression, */
2092 				0, /* uint16_t max_frames_per_packet, */
2093 				0 /* uint32_t g723_bitrate */
2094 				);
2095 
2096 		switch_set_flag_locked(tech_pvt, TFLAG_IO);
2097 		if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND) {
2098 			switch_channel_mark_answered(channel);
2099 		}
2100 		if (switch_channel_test_flag(channel, CF_HOLD)) {
2101 			switch_ivr_unhold(session);
2102 			send_set_lamp(listener, SKINNY_BUTTON_LINE, line_instance, SKINNY_LAMP_ON);
2103 		}
2104 	} else {
2105 		skinny_log_l(listener, SWITCH_LOG_WARNING, "Unable to find session for call id=%d.\n",
2106 				request->data.open_receive_channel_ack.pass_thru_party_id);
2107 	}
2108 end:
2109 	if(session) {
2110 		switch_core_session_rwunlock(session);
2111 	}
2112 	return status;
2113 }
2114 
skinny_handle_soft_key_set_request(listener_t * listener,skinny_message_t * request)2115 switch_status_t skinny_handle_soft_key_set_request(listener_t *listener, skinny_message_t *request)
2116 {
2117 	skinny_message_t *message = NULL;
2118 
2119 	if (listener->soft_key_set_set) {
2120 		message = switch_core_hash_find(listener->profile->soft_key_set_sets_hash, listener->soft_key_set_set);
2121 		if ( listener->profile->debug >= 9 ) {
2122 			skinny_log_l(listener, SWITCH_LOG_DEBUG, "Handle Soft Key Set Request with Set (%s)\n", listener->soft_key_set_set);
2123 		}
2124 	}
2125 	if (!message) {
2126 		message = switch_core_hash_find(listener->profile->soft_key_set_sets_hash, "default");
2127 		if ( listener->profile->debug >= 9 ) {
2128 			skinny_log_l(listener, SWITCH_LOG_DEBUG, "Handle Soft Key Set Request with Set (%s)\n", "default");
2129 		}
2130 	}
2131 	if (message) {
2132 		skinny_send_reply_quiet(listener, message, SWITCH_FALSE);
2133 	} else {
2134 		skinny_log_l(listener, SWITCH_LOG_ERROR, "Profile %s doesn't have a default <soft-key-set-set>.\n",
2135 			listener->profile->name);
2136 	}
2137 
2138 	/* Init the states */
2139 	send_select_soft_keys(listener, 0, 0, SKINNY_KEY_SET_ON_HOOK, 0xffff);
2140 
2141 	return SWITCH_STATUS_SUCCESS;
2142 }
2143 
skinny_handle_soft_key_event_message(listener_t * listener,skinny_message_t * request)2144 switch_status_t skinny_handle_soft_key_event_message(listener_t *listener, skinny_message_t *request)
2145 {
2146 	switch_status_t status = SWITCH_STATUS_SUCCESS;
2147 	uint32_t line_instance = 0;
2148 	uint32_t call_id = 0;
2149 	switch_core_session_t *session = NULL;
2150 	switch_channel_t *channel = NULL;
2151 
2152 	skinny_check_data_length(request, sizeof(request->data.soft_key_event.event));
2153 
2154 	if(skinny_check_data_length_soft(request, sizeof(request->data.soft_key_event))) {
2155 		line_instance = request->data.soft_key_event.line_instance;
2156 		call_id = request->data.soft_key_event.call_id;
2157 	}
2158 
2159 	skinny_log_l(listener, SWITCH_LOG_DEBUG, "Soft Key Event (%s) with Line Instance (%d), Call ID (%d)\n",
2160 		skinny_soft_key_event2str(request->data.soft_key_event.event), line_instance, call_id);
2161 
2162 	switch(request->data.soft_key_event.event) {
2163 		case SOFTKEY_REDIAL:
2164 			status = skinny_create_incoming_session(listener, &line_instance, &session);
2165 			if ( ! session ) {
2166 				skinny_log_l_msg(listener, SWITCH_LOG_CRIT, "Unable to handle soft key event, could not create incoming session.\n");
2167 				return SWITCH_STATUS_FALSE;
2168 			}
2169 			skinny_session_process_dest(session, listener, line_instance,
2170 				empty_null2(listener->ext_redial,listener->profile->ext_redial), '\0', 0);
2171 			break;
2172 		case SOFTKEY_NEWCALL:
2173 			status = skinny_create_incoming_session(listener, &line_instance, &session);
2174 			if ( ! session ) {
2175 				skinny_log_l_msg(listener, SWITCH_LOG_CRIT, "Unable to handle soft key event, could not create incoming session.\n");
2176 				return SWITCH_STATUS_FALSE;
2177 			}
2178 			skinny_session_process_dest(session, listener, line_instance, NULL, '\0', 0);
2179 			break;
2180 		case SOFTKEY_HOLD:
2181 			session = skinny_profile_find_session(listener->profile, listener, &line_instance, call_id);
2182 			if(session) {
2183 				status = skinny_session_hold_line(session, listener, line_instance);
2184 			}
2185 			break;
2186 		case SOFTKEY_TRANSFER:
2187 			session = skinny_profile_find_session(listener->profile, listener, &line_instance, call_id);
2188 
2189 			if(session) {
2190 				status = skinny_session_transfer(session, listener, line_instance);
2191 			}
2192 			break;
2193 		case SOFTKEY_BACKSPACE:
2194 			session = skinny_profile_find_session(listener->profile, listener, &line_instance, call_id);
2195 			if(session) {
2196 				skinny_session_process_dest(session, listener, line_instance, NULL, '\0', 1);
2197 			}
2198 			break;
2199 		case SOFTKEY_ENDCALL:
2200 			session = skinny_profile_find_session(listener->profile, listener, &line_instance, call_id);
2201 			if(session) {
2202 				channel = switch_core_session_get_channel(session);
2203 				if (switch_channel_test_flag(channel, CF_HOLD)) {
2204 					switch_ivr_unhold(session);
2205 				}
2206 				switch_channel_hangup(channel, SWITCH_CAUSE_NORMAL_CLEARING);
2207 			}
2208 			break;
2209 		case SOFTKEY_RESUME:
2210 			session = skinny_profile_find_session(listener->profile, listener, &line_instance, call_id);
2211 			if(session) {
2212 				status = skinny_session_unhold_line(session, listener, line_instance);
2213 			}
2214 			break;
2215 		case SOFTKEY_ANSWER:
2216 			session = skinny_profile_find_session(listener->profile, listener, &line_instance, call_id);
2217 			if(session) {
2218 				status = skinny_session_answer(session, listener, line_instance);
2219 			}
2220 			break;
2221 		case SOFTKEY_IDIVERT:
2222 			session = skinny_profile_find_session(listener->profile, listener, &line_instance, call_id);
2223 			if(session) {
2224 				switch_channel_t *channel = NULL;
2225 				channel = switch_core_session_get_channel(session);
2226 
2227 				if (channel) {
2228 					switch_channel_hangup(channel, SWITCH_CAUSE_NO_ANSWER);
2229 				}
2230 			}
2231 			break;
2232 		case SOFTKEY_MEETME:
2233 			skinny_create_incoming_session(listener, &line_instance, &session);
2234 			if ( ! session ) {
2235 				skinny_log_l_msg(listener, SWITCH_LOG_CRIT, "Unable to handle soft key event, could not create incoming session.\n");
2236 				return SWITCH_STATUS_FALSE;
2237 			}
2238 			skinny_session_process_dest(session, listener, line_instance,
2239 				empty_null2(listener->ext_meetme, listener->profile->ext_meetme), '\0', 0);
2240 			break;
2241 		case SOFTKEY_CALLPICKUP:
2242 		case SOFTKEY_GRPCALLPICKUP:
2243 			skinny_create_incoming_session(listener, &line_instance, &session);
2244 			if ( ! session ) {
2245 				skinny_log_l_msg(listener, SWITCH_LOG_CRIT, "Unable to handle soft key event, could not create incoming session.\n");
2246 				return SWITCH_STATUS_FALSE;
2247 			}
2248 			skinny_session_process_dest(session, listener, line_instance,
2249 				empty_null2(listener->ext_pickup, listener->profile->ext_pickup), '\0', 0);
2250 			break;
2251 		case SOFTKEY_CFWDALL:
2252 			skinny_create_incoming_session(listener, &line_instance, &session);
2253 			if ( ! session ) {
2254 				skinny_log_l_msg(listener, SWITCH_LOG_CRIT, "Unable to handle soft key event, could not create incoming session.\n");
2255 				return SWITCH_STATUS_FALSE;
2256 			}
2257 			skinny_session_process_dest(session, listener, line_instance,
2258 				empty_null2(listener->ext_cfwdall, listener->profile->ext_cfwdall), '\0', 0);
2259 			break;
2260 		default:
2261 			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
2262 					"Unknown SoftKeyEvent type: %d.\n", request->data.soft_key_event.event);
2263 	}
2264 
2265 	if(session) {
2266 		switch_core_session_rwunlock(session);
2267 	}
2268 
2269 	return status;
2270 }
2271 
skinny_handle_unregister(listener_t * listener,skinny_message_t * request)2272 switch_status_t skinny_handle_unregister(listener_t *listener, skinny_message_t *request)
2273 {
2274 	switch_event_t *event = NULL;
2275 	skinny_message_t *message;
2276 
2277 	/* skinny::unregister event */
2278 	skinny_device_event(listener, &event, SWITCH_EVENT_CUSTOM, SKINNY_EVENT_UNREGISTER);
2279 	switch_event_fire(&event);
2280 
2281 	skinny_create_message(message, UNREGISTER_ACK_MESSAGE, unregister_ack);
2282 
2283 	message->data.unregister_ack.unregister_status = 0; /* OK */
2284 
2285 	skinny_log_l(listener, SWITCH_LOG_DEBUG, "Handle Unregister with Status (%d)\n", message->data.unregister_ack.unregister_status);
2286 
2287 	skinny_send_reply_quiet(listener, message, SWITCH_TRUE);
2288 
2289 	/* Close socket */
2290 	switch_clear_flag_locked(listener, LFLAG_RUNNING);
2291 
2292 	/* Clear this device from database and any active lines/etc. */
2293 	skinny_clean_listener_from_db(listener);
2294 
2295 	return SWITCH_STATUS_SUCCESS;
2296 }
2297 
skinny_handle_soft_key_template_request(listener_t * listener,skinny_message_t * request)2298 switch_status_t skinny_handle_soft_key_template_request(listener_t *listener, skinny_message_t *request)
2299 {
2300 	size_t i;
2301 	skinny_message_t *message;
2302 
2303 	switch_assert(listener->profile);
2304 	switch_assert(listener->device_name);
2305 
2306 	skinny_create_message(message, SOFT_KEY_TEMPLATE_RES_MESSAGE, soft_key_template);
2307 
2308 	message->data.soft_key_template.soft_key_offset = 0;
2309 	message->data.soft_key_template.soft_key_count = 21;
2310 	message->data.soft_key_template.total_soft_key_count = 21;
2311 
2312 	for (i=0; i < sizeof(soft_key_template_default_textids)/4; i++) {
2313 		char *label = skinny_textid2raw(soft_key_template_default_textids[i]);
2314 		switch_copy_string(message->data.soft_key_template.soft_key[i].soft_key_label, label, sizeof(message->data.soft_key_template.soft_key[i].soft_key_label));
2315 		switch_safe_free(label);
2316 
2317 		message->data.soft_key_template.soft_key[i].soft_key_event = soft_key_template_default_events[i];
2318 	}
2319 
2320 	if ( listener->profile->debug >= 9 ) {
2321 		skinny_log_l_msg(listener, SWITCH_LOG_DEBUG, "Handle Soft Key Template Request with Default Template\n");
2322 	}
2323 
2324 	skinny_send_reply_quiet(listener, message, SWITCH_TRUE);
2325 
2326 	return SWITCH_STATUS_SUCCESS;
2327 }
2328 
skinny_headset_status_message(listener_t * listener,skinny_message_t * request)2329 switch_status_t skinny_headset_status_message(listener_t *listener, skinny_message_t *request)
2330 {
2331 	char *sql;
2332 
2333 	skinny_check_data_length(request, sizeof(request->data.headset_status));
2334 
2335 	if ((sql = switch_mprintf(
2336 					"UPDATE skinny_devices SET headset=%d WHERE name='%q' and instance=%d",
2337 					(request->data.headset_status.mode==1) ? SKINNY_ACCESSORY_STATE_OFFHOOK : SKINNY_ACCESSORY_STATE_ONHOOK,
2338 					listener->device_name,
2339 					listener->device_instance
2340 				 ))) {
2341 		skinny_execute_sql(listener->profile, sql, listener->profile->sql_mutex);
2342 		switch_safe_free(sql);
2343 	}
2344 
2345 	if ( listener->profile->debug >= 9 ) {
2346 		skinny_log_l(listener, SWITCH_LOG_DEBUG, "Update headset accessory status (%s)\n",
2347 			skinny_accessory_state2str(request->data.headset_status.mode));
2348 	}
2349 
2350 	return SWITCH_STATUS_SUCCESS;
2351 }
2352 
skinny_handle_media_resource_message(listener_t * listener,skinny_message_t * request)2353 switch_status_t skinny_handle_media_resource_message(listener_t *listener, skinny_message_t *request)
2354 {
2355 	skinny_check_data_length(request, sizeof(request->data.media_resource));
2356 
2357 	skinny_log_l_msg(listener, SWITCH_LOG_DEBUG, "Handle Media Resource Notification\n");
2358 
2359 	/* Do nothing */
2360 	return SWITCH_STATUS_SUCCESS;
2361 }
2362 
skinny_handle_register_available_lines_message(listener_t * listener,skinny_message_t * request)2363 switch_status_t skinny_handle_register_available_lines_message(listener_t *listener, skinny_message_t *request)
2364 {
2365 	skinny_check_data_length(request, sizeof(request->data.reg_lines));
2366 
2367 	if ( listener->profile->debug >= 9 ) {
2368 		skinny_log_l_msg(listener, SWITCH_LOG_DEBUG, "Handle Register Available Lines\n");
2369 	}
2370 
2371 	/* Do nothing */
2372 	return SWITCH_STATUS_SUCCESS;
2373 }
2374 
skinny_handle_data_message(listener_t * listener,skinny_message_t * request)2375 switch_status_t skinny_handle_data_message(listener_t *listener, skinny_message_t *request)
2376 {
2377 	switch_event_t *event = NULL;
2378 	char *tmp = NULL;
2379 	skinny_check_data_length(request, sizeof(request->data.data));
2380 	skinny_check_data_length(request, sizeof(request->data.data) + request->data.data.data_length - 1);
2381 
2382 	/* skinny::device_to_user event */
2383 	skinny_device_event(listener, &event, SWITCH_EVENT_CUSTOM, SKINNY_EVENT_DEVICE_TO_USER);
2384 	switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Skinny-DeviceToUser-Message-Id", "%d", request->type);
2385 	switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Skinny-DeviceToUser-Message-Id-String", "%s", skinny_message_type2str(request->type));
2386 	switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Skinny-DeviceToUser-Application-Id", "%d", request->data.data.application_id);
2387 	switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Skinny-DeviceToUser-Line-Instance", "%d", request->data.data.line_instance);
2388 	switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Skinny-DeviceToUser-Call-Id", "%d", request->data.data.call_id);
2389 	switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Skinny-DeviceToUser-Transaction-Id", "%d", request->data.data.transaction_id);
2390 	switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Skinny-DeviceToUser-Data-Length", "%d", request->data.data.data_length);
2391 
2392 	tmp = malloc(request->data.data.data_length + 1);
2393 	memcpy(tmp, request->data.data.data, request->data.data.data_length);
2394 
2395 	/* Ensure that the body is null-terminated */
2396 	tmp[request->data.data.data_length] = '\0';
2397 	switch_event_add_body(event, "%s", tmp);
2398 
2399 	switch_safe_free(tmp);
2400 
2401 	switch_event_fire(&event);
2402 
2403 	return SWITCH_STATUS_SUCCESS;
2404 }
2405 
skinny_handle_service_url_stat_request(listener_t * listener,skinny_message_t * request)2406 switch_status_t skinny_handle_service_url_stat_request(listener_t *listener, skinny_message_t *request)
2407 {
2408 	skinny_message_t *message;
2409 	struct service_url_stat_res_message *button = NULL;
2410 
2411 	skinny_check_data_length(request, sizeof(request->data.service_url_req));
2412 
2413 	skinny_create_message(message, SERVICE_URL_STAT_RES_MESSAGE, service_url_res);
2414 
2415 	skinny_service_url_get(listener, request->data.service_url_req.service_url_index, &button);
2416 
2417 	memcpy(&message->data.service_url_res, button, sizeof(struct service_url_stat_res_message));
2418 
2419 	skinny_send_reply(listener, message, SWITCH_TRUE);
2420 
2421 	switch_safe_free(button);
2422 
2423 	return SWITCH_STATUS_SUCCESS;
2424 }
2425 
skinny_handle_feature_stat_request(listener_t * listener,skinny_message_t * request)2426 switch_status_t skinny_handle_feature_stat_request(listener_t *listener, skinny_message_t *request)
2427 {
2428 	skinny_message_t *message;
2429 	struct feature_stat_res_message *button = NULL;
2430 
2431 	skinny_check_data_length(request, sizeof(request->data.feature_req));
2432 
2433 	skinny_create_message(message, FEATURE_STAT_RES_MESSAGE, feature_res);
2434 
2435 	skinny_feature_get(listener, request->data.feature_req.feature_index, &button);
2436 
2437 	memcpy(&message->data.feature_res, button, sizeof(struct feature_stat_res_message));
2438 
2439 	skinny_send_reply(listener, message, SWITCH_TRUE);
2440 
2441 	switch_safe_free(button);
2442 
2443 	return SWITCH_STATUS_SUCCESS;
2444 }
2445 
skinny_handle_extended_data_message(listener_t * listener,skinny_message_t * request)2446 switch_status_t skinny_handle_extended_data_message(listener_t *listener, skinny_message_t *request)
2447 {
2448 	switch_event_t *event = NULL;
2449 	char *tmp = NULL;
2450 	skinny_check_data_length(request, sizeof(request->data.extended_data));
2451 	skinny_check_data_length(request, sizeof(request->data.extended_data)+request->data.extended_data.data_length-1);
2452 
2453 	/* skinny::device_to_user event */
2454 	skinny_device_event(listener, &event, SWITCH_EVENT_CUSTOM, SKINNY_EVENT_DEVICE_TO_USER);
2455 	switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Skinny-DeviceToUser-Message-Id", "%d", request->type);
2456 	switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Skinny-DeviceToUser-Message-Id-String", "%s", skinny_message_type2str(request->type));
2457 	switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Skinny-DeviceToUser-Application-Id", "%d", request->data.extended_data.application_id);
2458 	switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Skinny-DeviceToUser-Line-Instance", "%d", request->data.extended_data.line_instance);
2459 	switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Skinny-DeviceToUser-Call-Id", "%d", request->data.extended_data.call_id);
2460 	switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Skinny-DeviceToUser-Transaction-Id", "%d", request->data.extended_data.transaction_id);
2461 	switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Skinny-DeviceToUser-Data-Length", "%d", request->data.extended_data.data_length);
2462 	switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Skinny-DeviceToUser-Sequence-Flag", "%d", request->data.extended_data.sequence_flag);
2463 	switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Skinny-DeviceToUser-Display-Priority", "%d", request->data.extended_data.display_priority);
2464 	switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Skinny-DeviceToUser-Conference-Id", "%d", request->data.extended_data.conference_id);
2465 	switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Skinny-DeviceToUser-App-Instance-Id", "%d", request->data.extended_data.app_instance_id);
2466 	switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Skinny-DeviceToUser-Routing-Id", "%d", request->data.extended_data.routing_id);
2467 
2468 	tmp = malloc(request->data.data.data_length + 1);
2469 	memcpy(tmp, request->data.data.data, request->data.data.data_length);
2470 
2471 	/* Ensure that the body is null-terminated */
2472 	tmp[request->data.data.data_length] = '\0';
2473 	switch_event_add_body(event, "%s", tmp);
2474 
2475 	switch_safe_free(tmp);
2476 	switch_event_fire(&event);
2477 
2478 	return SWITCH_STATUS_SUCCESS;
2479 }
2480 
skinny_handle_dialed_phone_book_message(listener_t * listener,skinny_message_t * request)2481 switch_status_t skinny_handle_dialed_phone_book_message(listener_t *listener, skinny_message_t *request)
2482 {
2483 	skinny_message_t *message;
2484 
2485 	skinny_check_data_length(request, sizeof(request->data.dialed_phone_book));
2486 
2487 	skinny_create_message(message, DIALED_PHONE_BOOK_ACK_MESSAGE, dialed_phone_book_ack);
2488 
2489 	message->data.dialed_phone_book_ack.number_index = request->data.dialed_phone_book.number_index;
2490 	message->data.dialed_phone_book_ack.line_instance = request->data.dialed_phone_book.line_instance;
2491 	message->data.dialed_phone_book_ack.unknown = request->data.dialed_phone_book.unknown;
2492 	message->data.dialed_phone_book_ack.unknown2 = 0;
2493 
2494 #if 0
2495 	/* Not sure why this isn't being sent at this point, need to investigate */
2496 	skinny_log_l_ffl(listener, file, func, line, SWITCH_LOG_DEBUG,
2497 		"Sending Handle Dialed Phone Book Ack Message with Number Index (%d), Line Instance (%d)\n",
2498 		request->data.dialed_phone_book.number_index, request->data.dialed_phone_book.line_instance);
2499 
2500 	return skinny_send_reply_quiet(listener, message, SWITCH_TRUE);
2501 #else
2502 	switch_safe_free(message);
2503 	return SWITCH_STATUS_SUCCESS;
2504 #endif
2505 }
2506 
skinny_handle_accessory_status_message(listener_t * listener,skinny_message_t * request)2507 switch_status_t skinny_handle_accessory_status_message(listener_t *listener, skinny_message_t *request)
2508 {
2509 	char *sql;
2510 
2511 	skinny_check_data_length(request, sizeof(request->data.accessory_status));
2512 
2513 	switch(request->data.accessory_status.accessory_id) {
2514 		case SKINNY_ACCESSORY_HEADSET:
2515 			if ((sql = switch_mprintf(
2516 							"UPDATE skinny_devices SET headset=%d WHERE name='%q' and instance=%d",
2517 							request->data.accessory_status.accessory_status,
2518 							listener->device_name,
2519 							listener->device_instance
2520 						 ))) {
2521 				skinny_execute_sql(listener->profile, sql, listener->profile->sql_mutex);
2522 				switch_safe_free(sql);
2523 			}
2524 			break;
2525 		case SKINNY_ACCESSORY_HANDSET:
2526 			if ((sql = switch_mprintf(
2527 							"UPDATE skinny_devices SET handset=%d WHERE name='%q' and instance=%d",
2528 							request->data.accessory_status.accessory_status,
2529 							listener->device_name,
2530 							listener->device_instance
2531 						 ))) {
2532 				skinny_execute_sql(listener->profile, sql, listener->profile->sql_mutex);
2533 				switch_safe_free(sql);
2534 			}
2535 			break;
2536 		case SKINNY_ACCESSORY_SPEAKER:
2537 			if ((sql = switch_mprintf(
2538 							"UPDATE skinny_devices SET speaker=%d WHERE name='%q' and instance=%d",
2539 							request->data.accessory_status.accessory_status,
2540 							listener->device_name,
2541 							listener->device_instance
2542 						 ))) {
2543 				skinny_execute_sql(listener->profile, sql, listener->profile->sql_mutex);
2544 				switch_safe_free(sql);
2545 			}
2546 			break;
2547 	}
2548 
2549 	return SWITCH_STATUS_SUCCESS;
2550 }
2551 
skinny_handle_updatecapabilities(listener_t * listener,skinny_message_t * request)2552 switch_status_t skinny_handle_updatecapabilities(listener_t *listener, skinny_message_t *request)
2553 {
2554 	char *sql;
2555 	skinny_profile_t *profile;
2556 
2557 	uint32_t i = 0;
2558 	uint32_t n = 0;
2559 	char *codec_order[SKINNY_MAX_CAPABILITIES] = {0};
2560 	char *codec_string;
2561 
2562 	size_t string_len, string_pos, pos;
2563 
2564 	switch_assert(listener->profile);
2565 	switch_assert(listener->device_name);
2566 
2567 	profile = listener->profile;
2568 
2569 	skinny_check_data_length(request, sizeof(request->data.upd_cap.audio_cap_count));
2570 
2571 	n = request->data.upd_cap.audio_cap_count;
2572 	if (n > SKINNY_MAX_CAPABILITIES) {
2573 		n = SKINNY_MAX_CAPABILITIES;
2574 	}
2575 	string_len = -1;
2576 
2577 	skinny_check_data_length(request, sizeof(request->data.upd_cap.audio_cap_count) + n * sizeof(request->data.upd_cap.audioCaps[0]));
2578 
2579 	for (i = 0; i < n; i++) {
2580 		char *codec = skinny_codec2string(request->data.upd_cap.audioCaps[i].payload_capability);
2581 		codec_order[i] = codec;
2582 		string_len += strlen(codec)+1;
2583 	}
2584 	i = 0;
2585 	pos = 0;
2586 	codec_string = calloc(string_len+1, 1);
2587 	for (string_pos = 0; string_pos < string_len; string_pos++) {
2588 		char *codec = codec_order[i];
2589 		switch_assert(i < n);
2590 		if(pos == strlen(codec)) {
2591 			codec_string[string_pos] = ',';
2592 			i++;
2593 			pos = 0;
2594 		} else {
2595 			codec_string[string_pos] = codec[pos++];
2596 		}
2597 	}
2598 	codec_string[string_len] = '\0';
2599 	if ((sql = switch_mprintf(
2600 					"UPDATE skinny_devices SET codec_string='%q' WHERE name='%q'",
2601 					codec_string,
2602 					listener->device_name
2603 				 ))) {
2604 		skinny_execute_sql(profile, sql, profile->sql_mutex);
2605 		switch_safe_free(sql);
2606 	}
2607 	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG,
2608 			"Codecs %s supported.\n", codec_string);
2609 	switch_safe_free(codec_string);
2610 	return SWITCH_STATUS_SUCCESS;
2611 }
2612 
2613 
skinny_handle_server_req_message(listener_t * listener,skinny_message_t * request)2614 switch_status_t skinny_handle_server_req_message(listener_t *listener, skinny_message_t *request)
2615 {
2616 	skinny_profile_t *profile;
2617 
2618 	profile = listener->profile;
2619 
2620 	skinny_log_l(listener, SWITCH_LOG_INFO, "Received Server Request Message (length=%d).\n", request->length);
2621 
2622 	send_srvreq_response(listener, profile->ip, profile->port);
2623 	return SWITCH_STATUS_SUCCESS;
2624 }
2625 
skinny_handle_xml_alarm(listener_t * listener,skinny_message_t * request)2626 switch_status_t skinny_handle_xml_alarm(listener_t *listener, skinny_message_t *request)
2627 {
2628 	switch_event_t *event = NULL;
2629 	char *tmp = NULL;
2630 
2631 	skinny_log_l(listener, SWITCH_LOG_DEBUG, "Received XML alarm (length=%d).\n", request->length);
2632 	/* skinny::xml_alarm event */
2633 	skinny_device_event(listener, &event, SWITCH_EVENT_CUSTOM, SKINNY_EVENT_XML_ALARM);
2634 	/* Ensure that the body is null-terminated */
2635 	tmp = malloc(request->length - 4 + 1);
2636 	memcpy(tmp, request->data.as_char, request->length - 4);
2637 	tmp[request->length - 4] = '\0';
2638 	switch_event_add_body(event, "%s", tmp);
2639 	switch_safe_free(tmp);
2640 	switch_event_fire(&event);
2641 
2642 	return SWITCH_STATUS_SUCCESS;
2643 }
2644 
skinny_handle_request(listener_t * listener,skinny_message_t * request)2645 switch_status_t skinny_handle_request(listener_t *listener, skinny_message_t *request)
2646 {
2647 	if (listener->profile->debug >= 10 ||
2648 		(listener->profile->debug >= 9 && request->type != KEEP_ALIVE_MESSAGE)) {
2649 		skinny_log_l(listener, SWITCH_LOG_DEBUG, "Received %s (type=%x,length=%d).\n",
2650 			skinny_message_type2str(request->type), request->type, request->length);
2651 	}
2652 	if(zstr(listener->device_name) && request->type != REGISTER_MESSAGE && request->type != ALARM_MESSAGE && request->type != XML_ALARM_MESSAGE && request->type != KEEP_ALIVE_MESSAGE) {
2653 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
2654 				"Device should send a register message first. Received %s (type=%x,length=%d).\n", skinny_message_type2str(request->type), request->type, request->length);
2655 		return SWITCH_STATUS_FALSE;
2656 	}
2657 	switch(request->type) {
2658 		case KEEP_ALIVE_MESSAGE:
2659 			return skinny_handle_keep_alive_message(listener, request);
2660 		case REGISTER_MESSAGE:
2661 			return skinny_handle_register(listener, request);
2662 		case PORT_MESSAGE:
2663 			return skinny_handle_port_message(listener, request);
2664 		case KEYPAD_BUTTON_MESSAGE:
2665 			return skinny_handle_keypad_button_message(listener, request);
2666 		case ENBLOC_CALL_MESSAGE:
2667 			return skinny_handle_enbloc_call_message(listener, request);
2668 		case STIMULUS_MESSAGE:
2669 			return skinny_handle_stimulus_message(listener, request);
2670 		case OFF_HOOK_MESSAGE:
2671 			return skinny_handle_off_hook_message(listener, request);
2672 		case ON_HOOK_MESSAGE:
2673 			return skinny_handle_on_hook_message(listener, request);
2674 		case FORWARD_STAT_REQ_MESSAGE:
2675 			return skinny_handle_forward_stat_req_message(listener, request);
2676 		case SPEED_DIAL_STAT_REQ_MESSAGE:
2677 			return skinny_handle_speed_dial_stat_request(listener, request);
2678 		case LINE_STAT_REQ_MESSAGE:
2679 			return skinny_handle_line_stat_request(listener, request);
2680 		case CONFIG_STAT_REQ_MESSAGE:
2681 			return skinny_handle_config_stat_request(listener, request);
2682 		case TIME_DATE_REQ_MESSAGE:
2683 			return skinny_handle_time_date_request(listener, request);
2684 		case BUTTON_TEMPLATE_REQ_MESSAGE:
2685 			return skinny_handle_button_template_request(listener, request);
2686 		case VERSION_REQ_MESSAGE:
2687 			return skinny_handle_version_request(listener, request);
2688 		case CAPABILITIES_RES_MESSAGE:
2689 			return skinny_handle_capabilities_response(listener, request);
2690 		case ALARM_MESSAGE:
2691 			return skinny_handle_alarm(listener, request);
2692 		case OPEN_RECEIVE_CHANNEL_ACK_MESSAGE:
2693 			return skinny_handle_open_receive_channel_ack_message(listener, request);
2694 		case SOFT_KEY_SET_REQ_MESSAGE:
2695 			return skinny_handle_soft_key_set_request(listener, request);
2696 		case SOFT_KEY_EVENT_MESSAGE:
2697 			return skinny_handle_soft_key_event_message(listener, request);
2698 		case UNREGISTER_MESSAGE:
2699 			return skinny_handle_unregister(listener, request);
2700 		case SOFT_KEY_TEMPLATE_REQ_MESSAGE:
2701 			return skinny_handle_soft_key_template_request(listener, request);
2702 		case MEDIA_RESOURCE_MESSAGE:
2703 			return skinny_handle_media_resource_message(listener, request);
2704 		case HEADSET_STATUS_MESSAGE:
2705 			return skinny_headset_status_message(listener, request);
2706 		case REGISTER_AVAILABLE_LINES_MESSAGE:
2707 			return skinny_handle_register_available_lines_message(listener, request);
2708 		case DEVICE_TO_USER_DATA_MESSAGE:
2709 			return skinny_handle_data_message(listener, request);
2710 		case DEVICE_TO_USER_DATA_RESPONSE_MESSAGE:
2711 			return skinny_handle_data_message(listener, request);
2712 		case SERVICE_URL_STAT_REQ_MESSAGE:
2713 			return skinny_handle_service_url_stat_request(listener, request);
2714 		case FEATURE_STAT_REQ_MESSAGE:
2715 			return skinny_handle_feature_stat_request(listener, request);
2716 		case DEVICE_TO_USER_DATA_VERSION1_MESSAGE:
2717 			return skinny_handle_extended_data_message(listener, request);
2718 		case DEVICE_TO_USER_DATA_RESPONSE_VERSION1_MESSAGE:
2719 			return skinny_handle_extended_data_message(listener, request);
2720 		case DIALED_PHONE_BOOK_MESSAGE:
2721 			return skinny_handle_dialed_phone_book_message(listener, request);
2722 		case ACCESSORY_STATUS_MESSAGE:
2723 			return skinny_handle_accessory_status_message(listener, request);
2724 		case XML_ALARM_MESSAGE:
2725 			return skinny_handle_xml_alarm(listener, request);
2726 		case UPDATE_CAPABILITIES_MESSAGE:
2727 			return skinny_handle_updatecapabilities(listener, request);
2728 		case SERVER_REQ_MESSAGE:
2729 			return skinny_handle_server_req_message(listener, request);
2730 		default:
2731 			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
2732 					"Unhandled %s (type=%x,length=%d).\n", skinny_message_type2str(request->type), request->type, request->length);
2733 			return SWITCH_STATUS_SUCCESS;
2734 	}
2735 }
2736 
2737 /* For Emacs:
2738  * Local Variables:
2739  * mode:c
2740  * indent-tabs-mode:t
2741  * tab-width:4
2742  * c-basic-offset:4
2743  * End:
2744  * For VIM:
2745  * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
2746  */
2747 
2748