PageRenderTime 32ms CodeModel.GetById 16ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/chan_datacard.h

http://datacard.googlecode.com/
C++ Header | 253 lines | 180 code | 54 blank | 19 comment | 1 complexity | 79e521713d16b929752846a1f8d37afe 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#ifndef CHAN_DATACARD_H_INCLUDED
 10#define CHAN_DATACARD_H_INCLUDED
 11
 12#include <asterisk.h>
 13#include <asterisk/lock.h>
 14#include <asterisk/linkedlists.h>
 15
 16#include "mixbuffer.h"				/* struct mixbuffer */
 17//#include "ringbuffer.h"				/* struct ringbuffer */
 18#include "cpvt.h"				/* struct cpvt */
 19#include "export.h"				/* EXPORT_DECL EXPORT_DEF */
 20#include "dc_config.h"				/* pvt_config_t */
 21
 22#define MODULE_DESCRIPTION	"Datacard Channel Driver"
 23#define MAXDATACARDDEVICES	256
 24
 25INLINE_DECL const char * dev_state2str(dev_state_t state)
 26{
 27	return enum2str(state, dev_state_strs, ITEMS_OF(dev_state_strs));
 28}
 29
 30INLINE_DECL const char * dev_state2str_msg(dev_state_t state)
 31{
 32	static const char * const states[] = { "Stop scheduled", "Restart scheduled", "Removal scheduled", "Start scheduled" };
 33	return enum2str(state, states, ITEMS_OF(states));
 34}
 35
 36typedef enum {
 37	RESTATE_TIME_NOW	= 0,
 38	RESTATE_TIME_GRACEFULLY,
 39	RESTATE_TIME_CONVENIENT,
 40} restate_time_t;
 41
 42/* state */
 43typedef struct pvt_state
 44{
 45	char			audio_tty[DEVPATHLEN];		/*!< tty for audio connection */
 46	char			data_tty[DEVPATHLEN];		/*!< tty for AT commands */
 47	uint32_t		at_tasks;			/*!< number of active tasks in at_queue */
 48	uint32_t		at_cmds;			/*!< number of active commands in at_queue */
 49	uint32_t		chansno;			/*!< number of channels in channels list */
 50	uint8_t			chan_count[CALL_STATES_NUMBER];	/*!< channel number grouped by state */
 51} pvt_state_t;
 52
 53#define PVT_STATE_T(state, name)			((state)->name)
 54
 55/* statictics */
 56typedef struct pvt_stat
 57{
 58	uint32_t		at_tasks;			/*!< number of tasks added to queue */
 59	uint32_t		at_cmds;			/*!< number of commands added to queue */
 60	uint32_t		at_responces;			/*!< number of responses handled */
 61
 62	uint32_t		d_read_bytes;			/*!< number of bytes of commands actually readed from device */
 63	uint32_t		d_write_bytes;			/*!< number of bytes of commands actually written to device */
 64
 65	uint64_t		a_read_bytes;			/*!< number of bytes of audio readed from device */
 66	uint64_t		a_write_bytes;			/*!< number of bytes of audio written to device */
 67
 68	uint32_t		read_frames;			/*!< number of frames readed from device */
 69	uint32_t		read_sframes;			/*!< number of truncated frames readed from device */
 70
 71	uint32_t		write_frames;			/*!< number of tries to frame write */
 72	uint32_t		write_tframes;			/*!< number of truncated frames to write */
 73	uint32_t		write_sframes;			/*!< number of silence frames to write */
 74
 75	uint64_t		write_rb_overflow_bytes;	/*!< number of overflow bytes */
 76	uint32_t		write_rb_overflow;		/*!< number of times when a_write_rb overflowed */
 77
 78	uint32_t		in_calls;			/*!< number of incoming calls not including waiting */
 79	uint32_t		cw_calls;			/*!< number of waiting calls */
 80	uint32_t		out_calls;			/*!< number of all outgoing calls attempts */
 81	uint32_t		in_calls_handled;		/*!< number of ncoming/waiting calls passed to dialplan */
 82	uint32_t		in_pbx_fails;			/*!< number of start_pbx fails */
 83
 84	uint32_t		calls_answered[2];		/*!< number of outgoing and incoming/waiting calls answered */
 85	uint32_t		calls_duration[2];		/*!< seconds of outgoing and incoming/waiting calls */
 86} pvt_stat_t;
 87
 88#define PVT_STAT_T(stat, name)			((stat)->name)
 89
 90struct at_queue_task;
 91
 92typedef struct pvt
 93{
 94	AST_LIST_ENTRY (pvt)	entry;				/*!< linked list pointers */
 95
 96	ast_mutex_t		lock;				/*!< pvt lock */
 97	AST_LIST_HEAD_NOLOCK (, at_queue_task) at_queue;	/*!< queue for commands to modem */
 98
 99	AST_LIST_HEAD_NOLOCK (, cpvt)		chans;		/*!< list of channels */
100	struct cpvt		sys_chan;			/*!< system channel */
101	struct cpvt		*last_dialed_cpvt;		/*!< channel what last call successfully set ATDnum; leave until ^ORIG received; need because real call idx of dialing call unknown until ^ORIG */
102
103	pthread_t		monitor_thread;			/*!< monitor (at commands reader) thread handle */
104
105	int			audio_fd;			/*!< audio descriptor */
106	int			data_fd;			/*!< data descriptor */
107	char			* alock;			/*!< name of lockfile for audio */
108	char			* dlock;			/*!< name of lockfile for data */
109
110	struct ast_dsp*		dsp;				/*!< silence/DTMF detector */
111	struct ast_timer*	a_timer;			/*!< audio write timer */
112
113	char			a_write_buf[FRAME_SIZE * 5];	/*!< audio write buffer */
114	struct mixbuffer	a_write_mixb;			/*!< audio mix buffer */
115//	struct ringbuffer	a_write_rb;			/*!< audio ring buffer */
116
117//	char			a_read_buf[FRAME_SIZE + AST_FRIENDLY_OFFSET];	/*!< audio read buffer */
118//	struct ast_frame	a_read_frame;			/*!< readed frame buffer */
119
120	
121	char			dtmf_digit;			/*!< last DTMF digit */
122	struct timeval		dtmf_begin_time;		/*!< time of begin of last DTMF digit */
123	struct timeval		dtmf_end_time;			/*!< time of end of last DTMF digit */
124
125	int			timeout;			/*!< used to set the timeout for data */
126#define DATA_READ_TIMEOUT	10000				/* 10 seconds */
127
128	unsigned long		channel_instanse;		/*!< number of channels created on this device */
129	unsigned int		rings;				/*!< ring/ccwa  number distributed to at_response_clcc() */
130
131	/* device caps */
132	unsigned int		use_ucs2_encoding:1;
133	unsigned int		cusd_use_7bit_encoding:1;
134	unsigned int		cusd_use_ucs2_decoding:1;
135
136	/* device state */
137	int			gsm_reg_status;
138	int			rssi;
139	int			linkmode;
140	int			linksubmode;
141	char			provider_name[32];
142	char			manufacturer[32];
143	char			model[32];
144	char			firmware[32];
145	char			imei[17];
146	char			imsi[17];
147	char			subscriber_number[128];
148	char			location_area_code[8];
149	char			cell_id[8];
150	char			sms_scenter[20];
151
152	unsigned int		connected:1;			/*!< do we have an connection to a device */
153	unsigned int		initialized:1;			/*!< whether a service level connection exists or not */
154	unsigned int		gsm_registered:1;		/*!< do we have an registration to a GSM */
155	unsigned int		dialing;			/*!< HW state; true from ATD response OK until CEND or CONN for this call idx */
156	unsigned int		ring:1;				/*!< HW state; true if has incoming call from first RING until CEND or CONN */
157	unsigned int		cwaiting:1;			/*!< HW state; true if has incoming call waiting from first CCWA until CEND or CONN for */
158	unsigned int		outgoing_sms:1;			/*!< outgoing sms */
159	unsigned int		incoming_sms:1;			/*!< incoming sms */
160	unsigned int		volume_sync_step:2;		/*!< volume synchronized stage */
161#define VOLUME_SYNC_BEGIN	0
162#define VOLUME_SYNC_DONE	3
163
164	unsigned int		use_pdu:1;			/*!< PDU SMS mode in force */
165	unsigned int		has_sms:1;			/*!< device has SMS support */
166	unsigned int		has_voice:1;			/*!< device has voice call support */
167	unsigned int		has_call_waiting:1;		/*!< call waiting enabled on device */
168
169	unsigned int		group_last_used:1;		/*!< mark the last used device */
170	unsigned int		prov_last_used:1;		/*!< mark the last used device */
171	unsigned int		sim_last_used:1;		/*!< mark the last used device */
172
173	unsigned int		terminate_monitor:1;		/*!< non-zero if we want terminate monitor thread i.e. restart, stop, remove */
174//	unsigned int		off:1;				/*!< device not used */
175//	unsigned int		prevent_new:1;			/*!< prevent new usage */
176
177	unsigned int		has_subscriber_number:1;	/*!< subscriber_number field is valid */
178//	unsigned int		monitor_running:1;		/*!< true if monitor thread is running */
179	unsigned int		must_remove:1;			/*!< mean not listed in config file when reload */
180
181	dev_state_t		desired_state;			/*!< desired state */
182	restate_time_t		restart_time;			/*!< time when change state */
183	dev_state_t		current_state;			/*!< current state */
184
185	pvt_config_t		settings;			/*!< all device settings from config file */
186	pvt_state_t		state;				/*!< state */
187	pvt_stat_t		stat;				/*!< various statistics */
188} pvt_t;
189
190#define CONF_GLOBAL(name)		(gpublic->global_settings.name)
191#define SCONF_GLOBAL(state, name)	((state)->global_settings.name)
192
193#define CONF_SHARED(pvt, name)		SCONFIG(&((pvt)->settings), name)
194#define CONF_UNIQ(pvt, name)		UCONFIG(&((pvt)->settings), name)
195#define PVT_ID(pvt)			UCONFIG(&((pvt)->settings), id)
196
197#define PVT_STATE(pvt, name)		PVT_STATE_T(&(pvt)->state, name)
198#define PVT_STAT(pvt, name)		PVT_STAT_T(&(pvt)->stat, name)
199
200typedef struct public_state
201{
202	AST_RWLIST_HEAD(devices, pvt)	devices;
203	ast_mutex_t			discovery_lock;
204	pthread_t			discovery_thread;		/* The discovery thread handler */
205	int				unloading_flag;			/* no need mutex or other locking for protect this variable because no concurent r/w and set non-0 atomically */
206	ast_mutex_t			round_robin_mtx;
207	struct pvt			* round_robin[MAXDATACARDDEVICES];
208	struct dc_gconfig		global_settings;
209} public_state_t;
210
211EXPORT_DECL public_state_t * gpublic;
212
213EXPORT_DECL void clean_read_data(const char * devname, int fd);
214EXPORT_DECL int pvt_get_pseudo_call_idx(const struct pvt * pvt);
215EXPORT_DECL int ready4voice_call(const struct pvt* pvt, const struct cpvt * current_cpvt, int opts);
216EXPORT_DECL int is_dial_possible(const struct pvt * pvt, int opts);
217
218EXPORT_DECL const char * pvt_str_state(const struct pvt* pvt);
219EXPORT_DECL struct ast_str * pvt_str_state_ex(const struct pvt* pvt);
220EXPORT_DECL const char * GSM_regstate2str(int gsm_reg_status);
221EXPORT_DECL const char * sys_mode2str(int sys_mode);
222EXPORT_DECL const char * sys_submode2str(int sys_submode);
223EXPORT_DECL char* rssi2dBm(int rssi, char* buf, unsigned len);
224
225EXPORT_DECL void pvt_on_create_1st_channel(struct pvt* pvt);
226EXPORT_DECL void pvt_on_remove_last_channel(struct pvt* pvt);
227EXPORT_DECL void pvt_reload(restate_time_t when);
228EXPORT_DECL int pvt_enabled(const struct pvt * pvt);
229EXPORT_DECL void pvt_try_restate(struct pvt * pvt);
230
231EXPORT_DECL int opentty (const char* dev, char ** lockfile);
232EXPORT_DECL void closetty(int fd, char ** lockfname);
233EXPORT_DECL int lock_try(const char * devname, char ** lockname);
234EXPORT_DECL struct pvt * find_device_ex(struct public_state * state, const char * name);
235
236INLINE_DECL struct pvt * find_device (const char* name)
237{
238	return find_device_ex(gpublic, name);
239}
240
241EXPORT_DECL struct pvt * find_device_ext(const char* name, const char ** reason);
242EXPORT_DECL struct pvt * find_device_by_resource_ex(struct public_state * state, const char * resource, int opts, const struct ast_channel * requestor, int * exists);
243
244INLINE_DECL struct pvt * find_device_by_resource(const char * resource, int opts, const struct ast_channel * requestor, int * exists)
245{
246	return find_device_by_resource_ex(gpublic, resource, opts, requestor, exists);
247}
248
249EXPORT_DECL struct ast_module * self_module();
250
251#define PVT_NO_CHANS(pvt)		(PVT_STATE(pvt, chansno) == 0)
252
253#endif /* CHAN_DATACARD_H_INCLUDED */