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