PageRenderTime 39ms CodeModel.GetById 9ms app.highlight 23ms RepoModel.GetById 2ms app.codeStats 0ms

/helpers.c

http://datacard.googlecode.com/
C | 196 lines | 157 code | 29 blank | 10 comment | 26 complexity | feb5e10b8030925897710c2a4a5c46a2 MD5 | raw file
  1/* 
  2   Copyright (C) 2009 - 2010
  3   
  4   Artem Makhutov <artem@makhutov.org>
  5   http://www.makhutov.org
  6   
  7   Dmitry Vagin <dmitry2004@yandex.ru>
  8
  9   bg <bg_one@mail.ru>
 10*/
 11#ifdef HAVE_CONFIG_H
 12#include <config.h>
 13#endif /* HAVE_CONFIG_H */
 14
 15#include <signal.h>				/* SIGURG */
 16
 17#include <asterisk.h>
 18#include <asterisk/callerid.h>			/*  AST_PRES_* */
 19
 20#include "helpers.h"
 21#include "chan_datacard.h"			/* devices */
 22#include "at_command.h"
 23#include "pdu.h"				/* pdu_digit2code() */
 24
 25static int is_valid_ussd_string(const char* number)
 26{
 27	for(; *number; number++)
 28		if(pdu_digit2code(*number) == 0)
 29			return 0;
 30
 31	return 1;
 32}
 33
 34#/* */
 35EXPORT_DEF int is_valid_phone_number(const char* number)
 36{
 37	if(number[0] == '+')
 38		number++;
 39	return is_valid_ussd_string(number);
 40}
 41
 42
 43#/* */
 44EXPORT_DEF int get_at_clir_value (struct pvt* pvt, int clir)
 45{
 46	int res = 0;
 47
 48	switch (clir)
 49	{
 50		case AST_PRES_ALLOWED_NETWORK_NUMBER:
 51		case AST_PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN:
 52		case AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED:
 53		case AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN:
 54		case AST_PRES_NUMBER_NOT_AVAILABLE:
 55			ast_debug (2, "[%s] callingpres: %s\n", PVT_ID(pvt), ast_describe_caller_presentation (clir));
 56			res = 2;
 57			break;
 58
 59		case AST_PRES_PROHIB_NETWORK_NUMBER:
 60		case AST_PRES_PROHIB_USER_NUMBER_FAILED_SCREEN:
 61		case AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED:
 62		case AST_PRES_PROHIB_USER_NUMBER_PASSED_SCREEN:
 63			ast_debug (2, "[%s] callingpres: %s\n", PVT_ID(pvt), ast_describe_caller_presentation (clir));
 64			res = 1;
 65			break;
 66
 67		default:
 68			ast_log (LOG_WARNING, "[%s] Unsupported callingpres: %d\n", PVT_ID(pvt), clir);
 69			if ((clir & AST_PRES_RESTRICTION) != AST_PRES_ALLOWED)
 70			{
 71				res = 0;
 72			}
 73			else
 74			{
 75				res = 2;
 76			}
 77			break;
 78	}
 79
 80	return res;
 81}
 82
 83typedef int (*at_cmd_f)(struct cpvt*, const char*, const char*, unsigned, int, void **);
 84
 85#/* */
 86static const char* send2(const char* dev_name, int * status, int online, const char* emsg, const char* okmsg, at_cmd_f func, const char* arg1, const char* arg2, unsigned arg3, int arg4, void ** arg5)
 87{
 88	struct pvt* pvt;
 89	const char* msg;
 90
 91	if(status)
 92		*status = 0;
 93	pvt = find_device_ext(dev_name, &msg);
 94	if(pvt)
 95	{
 96		if(pvt->connected && (!online || (pvt->initialized && pvt->gsm_registered)))
 97		{
 98			if((*func) (&pvt->sys_chan, arg1, arg2, arg3, arg4, arg5))
 99			{
100				msg = emsg;
101				ast_log (LOG_ERROR, "[%s] %s\n", PVT_ID(pvt), emsg);
102			}
103			else
104			{
105				msg = okmsg;
106				if(status)
107					*status = 1;
108			}
109		}
110		else
111			msg = "Device not connected / initialized / registered";
112		ast_mutex_unlock (&pvt->lock);
113	}
114	return msg;
115}
116
117#/* */
118EXPORT_DEF const char* send_ussd(const char* dev_name, const char* ussd, int * status, void ** id)
119{
120	if(is_valid_ussd_string(ussd))
121		return send2(dev_name, status, 1, "Error adding USSD command to queue", "USSD queued for send", (at_cmd_f)at_enque_ussd, ussd, 0, 0, 0, id);
122	if(status)
123		*status = 0;
124	return "Invalid USSD";
125}
126
127#/* */
128EXPORT_DEF const char * send_sms(const char * dev_name, const char * number, const char * message, const char * validity, const char * report, int * status, void ** id)
129{
130	if(is_valid_phone_number(number))
131	{
132		int val = 0;
133		int srr = 0;
134
135		if(validity)
136		{
137			val = strtol (validity, NULL, 10);
138			if(val <= 0)
139				val = 0;
140		}
141
142		if(report)
143			srr = ast_true (report);
144
145		return send2(dev_name, status, 1, "Error adding SMS commands to queue", "SMS queued for send", at_enque_sms, number, message, val, srr, id);
146	}
147	if(status)
148		*status = 0;
149	return "Invalid destination number";
150}
151
152#/* */
153EXPORT_DEF const char* send_reset(const char* dev_name, int * status)
154{
155	return send2(dev_name, status, 0, "Error adding reset command to queue", "Reset command queued for execute", (at_cmd_f)at_enque_reset, 0, 0, 0, 0, NULL);
156}
157
158#/* */
159EXPORT_DEF const char* send_ccwa_set(const char* dev_name, call_waiting_t enable, int * status)
160{
161	return send2(dev_name, status, 1, "Error adding CCWA commands to queue", "Call-Waiting commands queued for execute", (at_cmd_f)at_enque_set_ccwa, 0, 0, enable, 0, NULL);
162}
163
164#/* */
165EXPORT_DEF const char* send_at_command(const char* dev_name, const char* command)
166{
167	return send2(dev_name, NULL, 0, "Error adding command", "Command queued for execute", (at_cmd_f)at_enque_user_cmd, command, NULL, 0, 0, NULL);
168}
169
170EXPORT_DEF const char* schedule_restart_event(dev_state_t event, restate_time_t when, const char* dev_name, int * status)
171{
172	const char * msg;
173	struct pvt * pvt = find_device(dev_name);
174
175	if (pvt)
176	{
177		pvt->desired_state = event;
178		pvt->restart_time = when;
179
180		pvt_try_restate(pvt);
181		ast_mutex_unlock (&pvt->lock);
182
183		msg = dev_state2str_msg(event);
184
185		if(status)
186			*status = 1;
187	}
188	else
189	{
190		msg = "Device not found";
191		if(status)
192			*status = 0;
193	}
194
195	return msg;
196}