res_smdi.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2005-2008, Digium, Inc.
  5. *
  6. * Matthew A. Nicholson <mnicholson@digium.com>
  7. * Russell Bryant <russell@digium.com>
  8. *
  9. * See http://www.asterisk.org for more information about
  10. * the Asterisk project. Please do not directly contact
  11. * any of the maintainers of this project for assistance;
  12. * the project provides a web site, mailing lists and IRC
  13. * channels for your use.
  14. *
  15. * This program is free software, distributed under the terms of
  16. * the GNU General Public License Version 2. See the LICENSE file
  17. * at the top of the source tree.
  18. */
  19. /*!
  20. * \file
  21. * \brief SMDI support for Asterisk.
  22. * \author Matthew A. Nicholson <mnicholson@digium.com>
  23. * \author Russell Bryant <russell@digium.com>
  24. *
  25. * Here is a useful mailing list post that describes SMDI protocol details:
  26. * http://lists.digium.com/pipermail/asterisk-dev/2003-June/000884.html
  27. *
  28. * \todo This module currently has its own mailbox monitoring thread. This should
  29. * be converted to MWI subscriptions and just let the optional global voicemail
  30. * polling thread handle it.
  31. */
  32. /*! \li \ref res_smdi.c uses the configuration file \ref smdi.conf
  33. * \addtogroup configuration_file Configuration Files
  34. */
  35. /*!
  36. * \page smdi.conf smdi.conf
  37. * \verbinclude smdi.conf.sample
  38. */
  39. /*** MODULEINFO
  40. <support_level>core</support_level>
  41. ***/
  42. #include "asterisk.h"
  43. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  44. #include <termios.h>
  45. #include <sys/time.h>
  46. #include <time.h>
  47. #include <ctype.h>
  48. #include "asterisk/module.h"
  49. #include "asterisk/lock.h"
  50. #include "asterisk/utils.h"
  51. #define AST_API_MODULE
  52. #include "asterisk/smdi.h"
  53. #include "asterisk/config.h"
  54. #include "asterisk/io.h"
  55. #include "asterisk/stringfields.h"
  56. #include "asterisk/linkedlists.h"
  57. #include "asterisk/app.h"
  58. #include "asterisk/pbx.h"
  59. #include "asterisk/channel.h"
  60. /* Message expiry time in milliseconds */
  61. #define SMDI_MSG_EXPIRY_TIME 30000 /* 30 seconds */
  62. /*** DOCUMENTATION
  63. <function name="SMDI_MSG_RETRIEVE" language="en_US">
  64. <synopsis>
  65. Retrieve an SMDI message.
  66. </synopsis>
  67. <syntax>
  68. <parameter name="smdi port" required="true" />
  69. <parameter name="search key" required="true" />
  70. <parameter name="timeout" />
  71. <parameter name="options">
  72. <enumlist>
  73. <enum name="t">
  74. <para>Instead of searching on the forwarding station, search on the message desk terminal.</para>
  75. </enum>
  76. <enum name="n">
  77. <para>Instead of searching on the forwarding station, search on the message desk number.</para>
  78. </enum>
  79. </enumlist>
  80. </parameter>
  81. </syntax>
  82. <description>
  83. <para>This function is used to retrieve an incoming SMDI message. It returns
  84. an ID which can be used with the SMDI_MSG() function to access details of
  85. the message. Note that this is a destructive function in the sense that
  86. once an SMDI message is retrieved using this function, it is no longer in
  87. the global SMDI message queue, and can not be accessed by any other Asterisk
  88. channels. The timeout for this function is optional, and the default is
  89. 3 seconds. When providing a timeout, it should be in milliseconds.
  90. </para>
  91. <para>The default search is done on the forwarding station ID. However, if
  92. you set one of the search key options in the options field, you can change
  93. this behavior.
  94. </para>
  95. </description>
  96. <see-also>
  97. <ref type="function">SMDI_MSG</ref>
  98. </see-also>
  99. </function>
  100. <function name="SMDI_MSG" language="en_US">
  101. <synopsis>
  102. Retrieve details about an SMDI message.
  103. </synopsis>
  104. <syntax>
  105. <parameter name="message_id" required="true" />
  106. <parameter name="component" required="true">
  107. <para>Valid message components are:</para>
  108. <enumlist>
  109. <enum name="number">
  110. <para>The message desk number</para>
  111. </enum>
  112. <enum name="terminal">
  113. <para>The message desk terminal</para>
  114. </enum>
  115. <enum name="station">
  116. <para>The forwarding station</para>
  117. </enum>
  118. <enum name="callerid">
  119. <para>The callerID of the calling party that was forwarded</para>
  120. </enum>
  121. <enum name="type">
  122. <para>The call type. The value here is the exact character
  123. that came in on the SMDI link. Typically, example values
  124. are:</para>
  125. <para>Options:</para>
  126. <enumlist>
  127. <enum name="D">
  128. <para>Direct Calls</para>
  129. </enum>
  130. <enum name="A">
  131. <para>Forward All Calls</para>
  132. </enum>
  133. <enum name="B">
  134. <para>Forward Busy Calls</para>
  135. </enum>
  136. <enum name="N">
  137. <para>Forward No Answer Calls</para>
  138. </enum>
  139. </enumlist>
  140. </enum>
  141. </enumlist>
  142. </parameter>
  143. </syntax>
  144. <description>
  145. <para>This function is used to access details of an SMDI message that was
  146. pulled from the incoming SMDI message queue using the SMDI_MSG_RETRIEVE()
  147. function.</para>
  148. </description>
  149. <see-also>
  150. <ref type="function">SMDI_MSG_RETRIEVE</ref>
  151. </see-also>
  152. </function>
  153. ***/
  154. static const char config_file[] = "smdi.conf";
  155. static int smdi_loaded;
  156. struct ast_smdi_interface {
  157. char name[SMDI_MAX_FILENAME_LEN];
  158. struct ao2_container *md_q;
  159. ast_mutex_t md_q_lock;
  160. ast_cond_t md_q_cond;
  161. struct ao2_container *mwi_q;
  162. ast_mutex_t mwi_q_lock;
  163. ast_cond_t mwi_q_cond;
  164. FILE *file;
  165. int fd;
  166. pthread_t thread;
  167. struct termios mode;
  168. int msdstrip;
  169. long msg_expiry;
  170. };
  171. static AO2_GLOBAL_OBJ_STATIC(smdi_ifaces);
  172. /*! \brief A mapping between an SMDI mailbox ID and an Asterisk mailbox */
  173. struct mailbox_mapping {
  174. /*! This is the current state of the mailbox. It is simply on or
  175. * off to indicate if there are messages waiting or not. */
  176. unsigned int cur_state:1;
  177. /*! A Pointer to the appropriate SMDI interface */
  178. struct ast_smdi_interface *iface;
  179. AST_DECLARE_STRING_FIELDS(
  180. /*! The Name of the mailbox for the SMDI link. */
  181. AST_STRING_FIELD(smdi);
  182. /*! The name of the mailbox on the Asterisk side */
  183. AST_STRING_FIELD(mailbox);
  184. /*! The name of the voicemail context in use */
  185. AST_STRING_FIELD(context);
  186. );
  187. AST_LIST_ENTRY(mailbox_mapping) entry;
  188. };
  189. /*! 10 seconds */
  190. #define DEFAULT_POLLING_INTERVAL 10
  191. /*! \brief Data that gets used by the SMDI MWI monitoring thread */
  192. static struct {
  193. /*! The thread ID */
  194. pthread_t thread;
  195. ast_mutex_t lock;
  196. ast_cond_t cond;
  197. /*! A list of mailboxes that need to be monitored */
  198. AST_LIST_HEAD_NOLOCK(, mailbox_mapping) mailbox_mappings;
  199. /*! Polling Interval for checking mailbox status */
  200. unsigned int polling_interval;
  201. /*! Set to 1 to tell the polling thread to stop */
  202. unsigned int stop:1;
  203. /*! The time that the last poll began */
  204. struct timeval last_poll;
  205. } mwi_monitor = {
  206. .thread = AST_PTHREADT_NULL,
  207. };
  208. static void smdi_interface_destroy(void *obj)
  209. {
  210. struct ast_smdi_interface *iface = obj;
  211. int mod_unref_defer = 0;
  212. if (iface->thread != AST_PTHREADT_NULL && iface->thread != AST_PTHREADT_STOP) {
  213. pthread_cancel(iface->thread);
  214. pthread_join(iface->thread, NULL);
  215. mod_unref_defer = 1;
  216. }
  217. iface->thread = AST_PTHREADT_STOP;
  218. if (iface->file) {
  219. fclose(iface->file);
  220. }
  221. ao2_cleanup(iface->md_q);
  222. ast_mutex_destroy(&iface->md_q_lock);
  223. ast_cond_destroy(&iface->md_q_cond);
  224. ao2_cleanup(iface->mwi_q);
  225. ast_mutex_destroy(&iface->mwi_q_lock);
  226. ast_cond_destroy(&iface->mwi_q_cond);
  227. if (mod_unref_defer) {
  228. ast_module_unref(ast_module_info->self);
  229. }
  230. }
  231. /*!
  232. * \internal
  233. * \brief Push an SMDI message to the back of an interface's message queue.
  234. * \param iface a pointer to the interface to use.
  235. * \param md_msg a pointer to the message to use.
  236. */
  237. static void ast_smdi_md_message_push(struct ast_smdi_interface *iface, struct ast_smdi_md_message *md_msg)
  238. {
  239. ast_mutex_lock(&iface->md_q_lock);
  240. ao2_link(iface->md_q, md_msg);
  241. ast_cond_broadcast(&iface->md_q_cond);
  242. ast_mutex_unlock(&iface->md_q_lock);
  243. }
  244. /*!
  245. * \internal
  246. * \brief Push an SMDI message to the back of an interface's message queue.
  247. * \param iface a pointer to the interface to use.
  248. * \param mwi_msg a pointer to the message to use.
  249. */
  250. static void ast_smdi_mwi_message_push(struct ast_smdi_interface *iface, struct ast_smdi_mwi_message *mwi_msg)
  251. {
  252. ast_mutex_lock(&iface->mwi_q_lock);
  253. ao2_link(iface->mwi_q, mwi_msg);
  254. ast_cond_broadcast(&iface->mwi_q_cond);
  255. ast_mutex_unlock(&iface->mwi_q_lock);
  256. }
  257. static int smdi_toggle_mwi(struct ast_smdi_interface *iface, const char *mailbox, int on)
  258. {
  259. FILE *file;
  260. int i;
  261. if (!(file = fopen(iface->name, "w"))) {
  262. ast_log(LOG_ERROR, "Error opening SMDI interface %s (%s) for writing\n", iface->name, strerror(errno));
  263. return 1;
  264. }
  265. ao2_wrlock(iface);
  266. fprintf(file, "%s:MWI ", on ? "OP" : "RMV");
  267. for (i = 0; i < iface->msdstrip; i++)
  268. fprintf(file, "0");
  269. fprintf(file, "%s!\x04", mailbox);
  270. fclose(file);
  271. ao2_unlock(iface);
  272. ast_debug(1, "Sent MWI set message for %s on %s\n", mailbox, iface->name);
  273. return 0;
  274. }
  275. int AST_OPTIONAL_API_NAME(ast_smdi_mwi_set)(struct ast_smdi_interface *iface, const char *mailbox)
  276. {
  277. return smdi_toggle_mwi(iface, mailbox, 1);
  278. }
  279. int AST_OPTIONAL_API_NAME(ast_smdi_mwi_unset)(struct ast_smdi_interface *iface, const char *mailbox)
  280. {
  281. return smdi_toggle_mwi(iface, mailbox, 0);
  282. }
  283. enum smdi_message_type {
  284. SMDI_MWI,
  285. SMDI_MD,
  286. };
  287. static inline int lock_msg_q(struct ast_smdi_interface *iface, enum smdi_message_type type)
  288. {
  289. switch (type) {
  290. case SMDI_MWI:
  291. return ast_mutex_lock(&iface->mwi_q_lock);
  292. case SMDI_MD:
  293. return ast_mutex_lock(&iface->md_q_lock);
  294. }
  295. return -1;
  296. }
  297. static inline int unlock_msg_q(struct ast_smdi_interface *iface, enum smdi_message_type type)
  298. {
  299. switch (type) {
  300. case SMDI_MWI:
  301. return ast_mutex_unlock(&iface->mwi_q_lock);
  302. case SMDI_MD:
  303. return ast_mutex_unlock(&iface->md_q_lock);
  304. }
  305. return -1;
  306. }
  307. static inline void *unlink_from_msg_q(struct ast_smdi_interface *iface, enum smdi_message_type type)
  308. {
  309. switch (type) {
  310. case SMDI_MWI:
  311. return ao2_callback(iface->mwi_q, OBJ_UNLINK, NULL, NULL);
  312. case SMDI_MD:
  313. return ao2_callback(iface->md_q, OBJ_UNLINK, NULL, NULL);
  314. }
  315. return NULL;
  316. }
  317. static inline struct timeval msg_timestamp(void *msg, enum smdi_message_type type)
  318. {
  319. struct ast_smdi_md_message *md_msg = msg;
  320. struct ast_smdi_mwi_message *mwi_msg = msg;
  321. switch (type) {
  322. case SMDI_MWI:
  323. return mwi_msg->timestamp;
  324. case SMDI_MD:
  325. return md_msg->timestamp;
  326. }
  327. return ast_tv(0, 0);
  328. }
  329. static void purge_old_messages(struct ast_smdi_interface *iface, enum smdi_message_type type)
  330. {
  331. struct timeval now = ast_tvnow();
  332. long elapsed = 0;
  333. void *msg;
  334. lock_msg_q(iface, type);
  335. msg = unlink_from_msg_q(iface, type);
  336. unlock_msg_q(iface, type);
  337. /* purge old messages */
  338. while (msg) {
  339. elapsed = ast_tvdiff_ms(now, msg_timestamp(msg, type));
  340. if (elapsed > iface->msg_expiry) {
  341. /* found an expired message */
  342. ao2_ref(msg, -1);
  343. ast_log(LOG_NOTICE, "Purged expired message from %s SMDI %s message queue. "
  344. "Message was %ld milliseconds too old.\n",
  345. iface->name, (type == SMDI_MD) ? "MD" : "MWI",
  346. elapsed - iface->msg_expiry);
  347. lock_msg_q(iface, type);
  348. msg = unlink_from_msg_q(iface, type);
  349. unlock_msg_q(iface, type);
  350. } else {
  351. /* good message, put it back and return */
  352. switch (type) {
  353. case SMDI_MD:
  354. ast_smdi_md_message_push(iface, msg);
  355. break;
  356. case SMDI_MWI:
  357. ast_smdi_mwi_message_push(iface, msg);
  358. break;
  359. }
  360. ao2_ref(msg, -1);
  361. break;
  362. }
  363. }
  364. }
  365. static void *smdi_msg_pop(struct ast_smdi_interface *iface, enum smdi_message_type type)
  366. {
  367. void *msg;
  368. purge_old_messages(iface, type);
  369. lock_msg_q(iface, type);
  370. msg = unlink_from_msg_q(iface, type);
  371. unlock_msg_q(iface, type);
  372. return msg;
  373. }
  374. enum {
  375. OPT_SEARCH_TERMINAL = (1 << 0),
  376. OPT_SEARCH_NUMBER = (1 << 1),
  377. };
  378. static void *smdi_msg_find(struct ast_smdi_interface *iface,
  379. enum smdi_message_type type, const char *search_key, struct ast_flags options)
  380. {
  381. void *msg = NULL;
  382. purge_old_messages(iface, type);
  383. switch (type) {
  384. case SMDI_MD:
  385. if (ast_strlen_zero(search_key)) {
  386. /* No search key provided (the code from chan_dahdi does this).
  387. * Just pop the top message off of the queue. */
  388. msg = ao2_callback(iface->md_q, 0, NULL, NULL);
  389. } else if (ast_test_flag(&options, OPT_SEARCH_TERMINAL)) {
  390. /* Searching by the message desk terminal */
  391. struct ast_smdi_md_message md_msg = { .name = "" };
  392. strncpy(md_msg.mesg_desk_term, search_key, SMDI_MESG_DESK_TERM_LEN);
  393. msg = ao2_find(iface->md_q, &md_msg, OBJ_SEARCH_OBJECT);
  394. } else if (ast_test_flag(&options, OPT_SEARCH_NUMBER)) {
  395. /* Searching by the message desk number */
  396. struct ast_smdi_md_message md_msg = { .name = "" };
  397. strncpy(md_msg.mesg_desk_num, search_key, SMDI_MESG_DESK_NUM_LEN);
  398. msg = ao2_find(iface->md_q, &md_msg, OBJ_SEARCH_OBJECT);
  399. } else {
  400. /* Searching by the forwarding station */
  401. msg = ao2_find(iface->md_q, search_key, OBJ_SEARCH_KEY);
  402. }
  403. break;
  404. case SMDI_MWI:
  405. if (ast_strlen_zero(search_key)) {
  406. /* No search key provided (the code from chan_dahdi does this).
  407. * Just pop the top message off of the queue. */
  408. msg = ao2_callback(iface->mwi_q, 0, NULL, NULL);
  409. } else {
  410. msg = ao2_find(iface->mwi_q, search_key, OBJ_SEARCH_KEY);
  411. }
  412. break;
  413. }
  414. return msg;
  415. }
  416. static void *smdi_message_wait(struct ast_smdi_interface *iface, int timeout,
  417. enum smdi_message_type type, const char *search_key, struct ast_flags options)
  418. {
  419. struct timeval start;
  420. long diff = 0;
  421. void *msg;
  422. ast_cond_t *cond = NULL;
  423. ast_mutex_t *lock = NULL;
  424. switch (type) {
  425. case SMDI_MWI:
  426. cond = &iface->mwi_q_cond;
  427. lock = &iface->mwi_q_lock;
  428. break;
  429. case SMDI_MD:
  430. cond = &iface->md_q_cond;
  431. lock = &iface->md_q_lock;
  432. break;
  433. }
  434. start = ast_tvnow();
  435. while (diff < timeout) {
  436. struct timespec ts = { 0, };
  437. struct timeval wait;
  438. lock_msg_q(iface, type);
  439. if ((msg = smdi_msg_find(iface, type, search_key, options))) {
  440. unlock_msg_q(iface, type);
  441. return msg;
  442. }
  443. wait = ast_tvadd(start, ast_tv(0, timeout));
  444. ts.tv_sec = wait.tv_sec;
  445. ts.tv_nsec = wait.tv_usec * 1000;
  446. /* If there were no messages in the queue, then go to sleep until one
  447. * arrives. */
  448. ast_cond_timedwait(cond, lock, &ts);
  449. if ((msg = smdi_msg_find(iface, type, search_key, options))) {
  450. unlock_msg_q(iface, type);
  451. return msg;
  452. }
  453. unlock_msg_q(iface, type);
  454. /* check timeout */
  455. diff = ast_tvdiff_ms(ast_tvnow(), start);
  456. }
  457. return NULL;
  458. }
  459. struct ast_smdi_md_message * AST_OPTIONAL_API_NAME(ast_smdi_md_message_pop)(struct ast_smdi_interface *iface)
  460. {
  461. return smdi_msg_pop(iface, SMDI_MD);
  462. }
  463. struct ast_smdi_md_message * AST_OPTIONAL_API_NAME(ast_smdi_md_message_wait)(struct ast_smdi_interface *iface, int timeout)
  464. {
  465. struct ast_flags options = { 0 };
  466. return smdi_message_wait(iface, timeout, SMDI_MD, NULL, options);
  467. }
  468. struct ast_smdi_mwi_message * AST_OPTIONAL_API_NAME(ast_smdi_mwi_message_pop)(struct ast_smdi_interface *iface)
  469. {
  470. return smdi_msg_pop(iface, SMDI_MWI);
  471. }
  472. struct ast_smdi_mwi_message * AST_OPTIONAL_API_NAME(ast_smdi_mwi_message_wait)(struct ast_smdi_interface *iface, int timeout)
  473. {
  474. struct ast_flags options = { 0 };
  475. return smdi_message_wait(iface, timeout, SMDI_MWI, NULL, options);
  476. }
  477. struct ast_smdi_mwi_message * AST_OPTIONAL_API_NAME(ast_smdi_mwi_message_wait_station)(struct ast_smdi_interface *iface, int timeout,
  478. const char *station)
  479. {
  480. struct ast_flags options = { 0 };
  481. return smdi_message_wait(iface, timeout, SMDI_MWI, station, options);
  482. }
  483. struct ast_smdi_interface * AST_OPTIONAL_API_NAME(ast_smdi_interface_find)(const char *iface_name)
  484. {
  485. struct ao2_container *c;
  486. struct ast_smdi_interface *iface = NULL;
  487. c = ao2_global_obj_ref(smdi_ifaces);
  488. if (c) {
  489. iface = ao2_find(c, iface_name, OBJ_SEARCH_KEY);
  490. ao2_ref(c, -1);
  491. }
  492. return iface;
  493. }
  494. /*!
  495. * \internal
  496. * \brief Read an SMDI message.
  497. *
  498. * \param iface_p the SMDI interface to read from.
  499. *
  500. * This function loops and reads from and SMDI interface. It must be stopped
  501. * using pthread_cancel().
  502. */
  503. static void *smdi_read(void *iface_p)
  504. {
  505. struct ast_smdi_interface *iface = iface_p;
  506. struct ast_smdi_md_message *md_msg;
  507. struct ast_smdi_mwi_message *mwi_msg;
  508. char *cp = NULL;
  509. int i, c;
  510. int start = 0;
  511. /* read an smdi message */
  512. while ((c = fgetc(iface->file))) {
  513. /* check if this is the start of a message */
  514. if (!start) {
  515. if (c == 'M') {
  516. ast_debug(1, "Read an 'M' to start an SMDI message\n");
  517. start = 1;
  518. }
  519. continue;
  520. }
  521. if (c == 'D') { /* MD message */
  522. start = 0;
  523. ast_debug(1, "Read a 'D' ... it's an MD message.\n");
  524. md_msg = ao2_alloc(sizeof(*md_msg), NULL);
  525. if (!md_msg) {
  526. return NULL;
  527. }
  528. /* read the message desk number */
  529. for (i = 0; i < sizeof(md_msg->mesg_desk_num) - 1; i++) {
  530. c = fgetc(iface->file);
  531. if (c == EOF) {
  532. ast_log(LOG_ERROR, "Unexpected EOF while reading MD message\n");
  533. ao2_ref(md_msg, -1);
  534. return NULL;
  535. }
  536. md_msg->mesg_desk_num[i] = (char) c;
  537. ast_debug(1, "Read a '%c'\n", md_msg->mesg_desk_num[i]);
  538. }
  539. md_msg->mesg_desk_num[sizeof(md_msg->mesg_desk_num) - 1] = '\0';
  540. ast_debug(1, "The message desk number is '%s'\n", md_msg->mesg_desk_num);
  541. /* read the message desk terminal number */
  542. for (i = 0; i < sizeof(md_msg->mesg_desk_term) - 1; i++) {
  543. c = fgetc(iface->file);
  544. if (c == EOF) {
  545. ast_log(LOG_ERROR, "Unexpected EOF while reading SMDI message\n");
  546. ao2_ref(md_msg, -1);
  547. return NULL;
  548. }
  549. md_msg->mesg_desk_term[i] = (char) c;
  550. ast_debug(1, "Read a '%c'\n", md_msg->mesg_desk_term[i]);
  551. }
  552. md_msg->mesg_desk_term[sizeof(md_msg->mesg_desk_term) - 1] = '\0';
  553. ast_debug(1, "The message desk terminal is '%s'\n", md_msg->mesg_desk_term);
  554. /* read the message type */
  555. c = fgetc(iface->file);
  556. if (c == EOF) {
  557. ast_log(LOG_ERROR, "Unexpected EOF while reading SMDI message\n");
  558. ao2_ref(md_msg, -1);
  559. return NULL;
  560. }
  561. md_msg->type = (char) c;
  562. ast_debug(1, "Message type is '%c'\n", md_msg->type);
  563. /* read the forwarding station number (may be blank) */
  564. cp = &md_msg->fwd_st[0];
  565. for (i = 0; i < sizeof(md_msg->fwd_st) - 1; i++) {
  566. if ((c = fgetc(iface->file)) == ' ') {
  567. *cp = '\0';
  568. ast_debug(1, "Read a space, done looking for the forwarding station\n");
  569. break;
  570. }
  571. /* store c in md_msg->fwd_st */
  572. if (i >= iface->msdstrip) {
  573. ast_debug(1, "Read a '%c' and stored it in the forwarding station buffer\n", c);
  574. *cp++ = c;
  575. } else {
  576. ast_debug(1, "Read a '%c', but didn't store it in the fwd station buffer, because of the msdstrip setting (%d < %d)\n", c, i, iface->msdstrip);
  577. }
  578. }
  579. /* make sure the value is null terminated, even if this truncates it */
  580. md_msg->fwd_st[sizeof(md_msg->fwd_st) - 1] = '\0';
  581. cp = NULL;
  582. ast_debug(1, "The forwarding station is '%s'\n", md_msg->fwd_st);
  583. /* Put the fwd_st in the name field so that we can use ao2_find to look
  584. * up a message on this field */
  585. ast_copy_string(md_msg->name, md_msg->fwd_st, sizeof(md_msg->name));
  586. /* read the calling station number (may be blank) */
  587. cp = &md_msg->calling_st[0];
  588. for (i = 0; i < sizeof(md_msg->calling_st) - 1; i++) {
  589. if (!isdigit((c = fgetc(iface->file)))) {
  590. *cp = '\0';
  591. ast_debug(1, "Read a '%c', but didn't store it in the calling station buffer because it's not a digit\n", c);
  592. if (c == ' ') {
  593. /* Don't break on a space. We may read the space before the calling station
  594. * here if the forwarding station buffer filled up. */
  595. i--; /* We're still on the same character */
  596. continue;
  597. }
  598. break;
  599. }
  600. /* store c in md_msg->calling_st */
  601. if (i >= iface->msdstrip) {
  602. ast_debug(1, "Read a '%c' and stored it in the calling station buffer\n", c);
  603. *cp++ = c;
  604. } else {
  605. ast_debug(1, "Read a '%c', but didn't store it in the calling station buffer, because of the msdstrip setting (%d < %d)\n", c, i, iface->msdstrip);
  606. }
  607. }
  608. /* make sure the value is null terminated, even if this truncates it */
  609. md_msg->calling_st[sizeof(md_msg->calling_st) - 1] = '\0';
  610. cp = NULL;
  611. ast_debug(1, "The calling station is '%s'\n", md_msg->calling_st);
  612. /* add the message to the message queue */
  613. md_msg->timestamp = ast_tvnow();
  614. ast_smdi_md_message_push(iface, md_msg);
  615. ast_debug(1, "Received SMDI MD message on %s\n", iface->name);
  616. ao2_ref(md_msg, -1);
  617. } else if (c == 'W') { /* MWI message */
  618. start = 0;
  619. ast_debug(1, "Read a 'W', it's an MWI message. (No more debug coming for MWI messages)\n");
  620. mwi_msg = ao2_alloc(sizeof(*mwi_msg), NULL);
  621. if (!mwi_msg) {
  622. return NULL;
  623. }
  624. /* discard the 'I' (from 'MWI') */
  625. fgetc(iface->file);
  626. /* read the forwarding station number (may be blank) */
  627. cp = &mwi_msg->fwd_st[0];
  628. for (i = 0; i < sizeof(mwi_msg->fwd_st) - 1; i++) {
  629. if ((c = fgetc(iface->file)) == ' ') {
  630. *cp = '\0';
  631. break;
  632. }
  633. /* store c in md_msg->fwd_st */
  634. if (i >= iface->msdstrip)
  635. *cp++ = c;
  636. }
  637. /* make sure the station number is null terminated, even if this will truncate it */
  638. mwi_msg->fwd_st[sizeof(mwi_msg->fwd_st) - 1] = '\0';
  639. cp = NULL;
  640. /* Put the fwd_st in the name field so that we can use ao2_find to look
  641. * up a message on this field */
  642. ast_copy_string(mwi_msg->name, mwi_msg->fwd_st, sizeof(mwi_msg->name));
  643. /* read the mwi failure cause */
  644. for (i = 0; i < sizeof(mwi_msg->cause) - 1; i++) {
  645. c = fgetc(iface->file);
  646. if (c == EOF) {
  647. ast_log(LOG_ERROR, "Unexpected EOF while reading MWI message\n");
  648. ao2_ref(mwi_msg, -1);
  649. return NULL;
  650. }
  651. mwi_msg->cause[i] = (char) c;
  652. }
  653. mwi_msg->cause[sizeof(mwi_msg->cause) - 1] = '\0';
  654. /* add the message to the message queue */
  655. mwi_msg->timestamp = ast_tvnow();
  656. ast_smdi_mwi_message_push(iface, mwi_msg);
  657. ast_debug(1, "Received SMDI MWI message on %s\n", iface->name);
  658. ao2_ref(mwi_msg, -1);
  659. } else {
  660. ast_log(LOG_ERROR, "Unknown SMDI message type received on %s (M%c).\n", iface->name, c);
  661. start = 0;
  662. }
  663. }
  664. ast_log(LOG_ERROR, "Error reading from SMDI interface %s, stopping listener thread\n", iface->name);
  665. return NULL;
  666. }
  667. static void destroy_mailbox_mapping(struct mailbox_mapping *mm)
  668. {
  669. ast_string_field_free_memory(mm);
  670. ao2_ref(mm->iface, -1);
  671. free(mm);
  672. }
  673. static void destroy_all_mailbox_mappings(void)
  674. {
  675. struct mailbox_mapping *mm;
  676. ast_mutex_lock(&mwi_monitor.lock);
  677. while ((mm = AST_LIST_REMOVE_HEAD(&mwi_monitor.mailbox_mappings, entry)))
  678. destroy_mailbox_mapping(mm);
  679. ast_mutex_unlock(&mwi_monitor.lock);
  680. }
  681. static void append_mailbox_mapping(struct ast_variable *var, struct ast_smdi_interface *iface)
  682. {
  683. struct mailbox_mapping *mm;
  684. char *mailbox, *context;
  685. if (!(mm = ast_calloc_with_stringfields(1, struct mailbox_mapping, 32)))
  686. return;
  687. ast_string_field_set(mm, smdi, var->name);
  688. context = ast_strdupa(var->value);
  689. mailbox = strsep(&context, "@");
  690. if (ast_strlen_zero(context))
  691. context = "default";
  692. ast_string_field_set(mm, mailbox, mailbox);
  693. ast_string_field_set(mm, context, context);
  694. mm->iface = ao2_bump(iface);
  695. ast_mutex_lock(&mwi_monitor.lock);
  696. AST_LIST_INSERT_TAIL(&mwi_monitor.mailbox_mappings, mm, entry);
  697. ast_mutex_unlock(&mwi_monitor.lock);
  698. }
  699. /*!
  700. * \note Called with the mwi_monitor.lock locked
  701. */
  702. static void poll_mailbox(struct mailbox_mapping *mm)
  703. {
  704. char buf[1024];
  705. unsigned int state;
  706. snprintf(buf, sizeof(buf), "%s@%s", mm->mailbox, mm->context);
  707. state = !!ast_app_has_voicemail(mm->mailbox, NULL);
  708. if (state != mm->cur_state) {
  709. if (state)
  710. ast_smdi_mwi_set(mm->iface, mm->smdi);
  711. else
  712. ast_smdi_mwi_unset(mm->iface, mm->smdi);
  713. mm->cur_state = state;
  714. }
  715. }
  716. static void *mwi_monitor_handler(void *data)
  717. {
  718. while (!mwi_monitor.stop) {
  719. struct timespec ts = { 0, };
  720. struct timeval polltime;
  721. struct mailbox_mapping *mm;
  722. ast_mutex_lock(&mwi_monitor.lock);
  723. mwi_monitor.last_poll = ast_tvnow();
  724. AST_LIST_TRAVERSE(&mwi_monitor.mailbox_mappings, mm, entry)
  725. poll_mailbox(mm);
  726. /* Sleep up to the configured polling interval. Allow unload_module()
  727. * to signal us to wake up and exit. */
  728. polltime = ast_tvadd(mwi_monitor.last_poll, ast_tv(mwi_monitor.polling_interval, 0));
  729. ts.tv_sec = polltime.tv_sec;
  730. ts.tv_nsec = polltime.tv_usec * 1000;
  731. ast_cond_timedwait(&mwi_monitor.cond, &mwi_monitor.lock, &ts);
  732. ast_mutex_unlock(&mwi_monitor.lock);
  733. }
  734. return NULL;
  735. }
  736. static int smdi_mwi_q_cmp_fn(void *obj, void *data, int flags)
  737. {
  738. struct ast_smdi_mwi_message *msg = obj;
  739. char *str = data;
  740. return !strcmp(msg->name, str) ? CMP_MATCH | CMP_STOP : 0;
  741. }
  742. static int smdi_md_q_cmp_fn(void *obj, void *arg, int flags)
  743. {
  744. const struct ast_smdi_md_message *msg = obj;
  745. const struct ast_smdi_md_message *search_msg = arg;
  746. const char *search_key = arg;
  747. int cmp = 0;
  748. switch (flags & OBJ_SEARCH_MASK) {
  749. case OBJ_SEARCH_OBJECT:
  750. if (!ast_strlen_zero(search_msg->mesg_desk_num)) {
  751. cmp = strcmp(msg->mesg_desk_num, search_msg->mesg_desk_num);
  752. }
  753. if (!ast_strlen_zero(search_msg->mesg_desk_term)) {
  754. cmp |= strcmp(msg->mesg_desk_term, search_msg->mesg_desk_term);
  755. }
  756. break;
  757. case OBJ_SEARCH_KEY:
  758. cmp = strcmp(msg->name, search_key);
  759. break;
  760. }
  761. if (cmp) {
  762. return 0;
  763. }
  764. return CMP_MATCH;
  765. }
  766. static struct ast_smdi_interface *alloc_smdi_interface(void)
  767. {
  768. struct ast_smdi_interface *iface;
  769. if (!(iface = ao2_alloc(sizeof(*iface), smdi_interface_destroy))) {
  770. return NULL;
  771. }
  772. iface->md_q = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, NULL, smdi_md_q_cmp_fn);
  773. iface->mwi_q = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, NULL, smdi_mwi_q_cmp_fn);
  774. ast_mutex_init(&iface->md_q_lock);
  775. ast_cond_init(&iface->md_q_cond, NULL);
  776. ast_mutex_init(&iface->mwi_q_lock);
  777. ast_cond_init(&iface->mwi_q_cond, NULL);
  778. return iface;
  779. }
  780. static int smdi_ifaces_cmp_fn(void *obj, void *data, int flags)
  781. {
  782. struct ast_smdi_interface *iface = obj;
  783. char *str = data;
  784. return !strcmp(iface->name, str) ? CMP_MATCH | CMP_STOP : 0;
  785. }
  786. /*!
  787. * \internal
  788. * \brief Load and reload SMDI configuration.
  789. * \param reload this should be 1 if we are reloading and 0 if not.
  790. *
  791. * This function loads/reloads the SMDI configuration and starts and stops
  792. * interfaces accordingly.
  793. *
  794. * \return zero on success, -1 on failure, and 1 if no smdi interfaces were started.
  795. */
  796. static int smdi_load(int reload)
  797. {
  798. struct ast_config *conf;
  799. struct ast_variable *v;
  800. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  801. int res = 0;
  802. RAII_VAR(struct ao2_container *, new_ifaces, NULL, ao2_cleanup);
  803. RAII_VAR(struct ao2_container *, old_ifaces, ao2_global_obj_ref(smdi_ifaces), ao2_cleanup);
  804. struct ast_smdi_interface *mailbox_iface = NULL;
  805. /* Config options */
  806. speed_t baud_rate = B9600; /* 9600 baud rate */
  807. tcflag_t paritybit = PARENB; /* even parity checking */
  808. tcflag_t charsize = CS7; /* seven bit characters */
  809. int stopbits = 0; /* One stop bit */
  810. int msdstrip = 0; /* strip zero digits */
  811. long msg_expiry = SMDI_MSG_EXPIRY_TIME;
  812. if (!(conf = ast_config_load(config_file, config_flags)) || conf == CONFIG_STATUS_FILEINVALID) {
  813. if (reload)
  814. ast_log(LOG_NOTICE, "Unable to reload config %s: SMDI untouched\n", config_file);
  815. else
  816. ast_log(LOG_NOTICE, "Unable to load config %s: SMDI disabled\n", config_file);
  817. return 1;
  818. } else if (conf == CONFIG_STATUS_FILEUNCHANGED)
  819. return 0;
  820. new_ifaces = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0, NULL, smdi_ifaces_cmp_fn);
  821. if (!new_ifaces) {
  822. ast_config_destroy(conf);
  823. return -1;
  824. }
  825. for (v = ast_variable_browse(conf, "interfaces"); v; v = v->next) {
  826. RAII_VAR(struct ast_smdi_interface *, iface, NULL, ao2_cleanup);
  827. if (!strcasecmp(v->name, "baudrate")) {
  828. if (!strcasecmp(v->value, "9600"))
  829. baud_rate = B9600;
  830. else if (!strcasecmp(v->value, "4800"))
  831. baud_rate = B4800;
  832. else if (!strcasecmp(v->value, "2400"))
  833. baud_rate = B2400;
  834. else if (!strcasecmp(v->value, "1200"))
  835. baud_rate = B1200;
  836. else {
  837. ast_log(LOG_NOTICE, "Invalid baud rate '%s' specified in %s (line %d), using default\n", v->value, config_file, v->lineno);
  838. baud_rate = B9600;
  839. }
  840. } else if (!strcasecmp(v->name, "msdstrip")) {
  841. if (!sscanf(v->value, "%30d", &msdstrip)) {
  842. ast_log(LOG_NOTICE, "Invalid msdstrip value in %s (line %d), using default\n", config_file, v->lineno);
  843. msdstrip = 0;
  844. } else if (0 > msdstrip || msdstrip > 9) {
  845. ast_log(LOG_NOTICE, "Invalid msdstrip value in %s (line %d), using default\n", config_file, v->lineno);
  846. msdstrip = 0;
  847. }
  848. } else if (!strcasecmp(v->name, "msgexpirytime")) {
  849. if (!sscanf(v->value, "%30ld", &msg_expiry)) {
  850. ast_log(LOG_NOTICE, "Invalid msgexpirytime value in %s (line %d), using default\n", config_file, v->lineno);
  851. msg_expiry = SMDI_MSG_EXPIRY_TIME;
  852. }
  853. } else if (!strcasecmp(v->name, "paritybit")) {
  854. if (!strcasecmp(v->value, "even"))
  855. paritybit = PARENB;
  856. else if (!strcasecmp(v->value, "odd"))
  857. paritybit = PARENB | PARODD;
  858. else if (!strcasecmp(v->value, "none"))
  859. paritybit = ~PARENB;
  860. else {
  861. ast_log(LOG_NOTICE, "Invalid parity bit setting in %s (line %d), using default\n", config_file, v->lineno);
  862. paritybit = PARENB;
  863. }
  864. } else if (!strcasecmp(v->name, "charsize")) {
  865. if (!strcasecmp(v->value, "7"))
  866. charsize = CS7;
  867. else if (!strcasecmp(v->value, "8"))
  868. charsize = CS8;
  869. else {
  870. ast_log(LOG_NOTICE, "Invalid character size setting in %s (line %d), using default\n", config_file, v->lineno);
  871. charsize = CS7;
  872. }
  873. } else if (!strcasecmp(v->name, "twostopbits")) {
  874. stopbits = ast_true(v->name);
  875. } else if (!strcasecmp(v->name, "smdiport")) {
  876. if (reload) {
  877. /* we are reloading, check if we are already
  878. * monitoring this interface, if we are we do
  879. * not want to start it again. This also has
  880. * the side effect of not updating different
  881. * setting for the serial port, but it should
  882. * be trivial to rewrite this section so that
  883. * options on the port are changed without
  884. * restarting the interface. Or the interface
  885. * could be restarted with out emptying the
  886. * queue. */
  887. if ((iface = ao2_find(old_ifaces, v->value, OBJ_SEARCH_KEY))) {
  888. ast_log(LOG_NOTICE, "SMDI interface %s already running, not restarting\n", iface->name);
  889. ao2_link(new_ifaces, iface);
  890. continue;
  891. }
  892. }
  893. if (!(iface = alloc_smdi_interface()))
  894. continue;
  895. ast_copy_string(iface->name, v->value, sizeof(iface->name));
  896. iface->thread = AST_PTHREADT_NULL;
  897. if (!(iface->file = fopen(iface->name, "r"))) {
  898. ast_log(LOG_ERROR, "Error opening SMDI interface %s (%s)\n", iface->name, strerror(errno));
  899. continue;
  900. }
  901. iface->fd = fileno(iface->file);
  902. /* Set the proper attributes for our serial port. */
  903. /* get the current attributes from the port */
  904. if (tcgetattr(iface->fd, &iface->mode)) {
  905. ast_log(LOG_ERROR, "Error getting atributes of %s (%s)\n", iface->name, strerror(errno));
  906. continue;
  907. }
  908. /* set the desired speed */
  909. if (cfsetispeed(&iface->mode, baud_rate) || cfsetospeed(&iface->mode, baud_rate)) {
  910. ast_log(LOG_ERROR, "Error setting baud rate on %s (%s)\n", iface->name, strerror(errno));
  911. continue;
  912. }
  913. /* set the stop bits */
  914. if (stopbits)
  915. iface->mode.c_cflag = iface->mode.c_cflag | CSTOPB; /* set two stop bits */
  916. else
  917. iface->mode.c_cflag = iface->mode.c_cflag & ~CSTOPB; /* set one stop bit */
  918. /* set the parity */
  919. iface->mode.c_cflag = (iface->mode.c_cflag & ~PARENB & ~PARODD) | paritybit;
  920. /* set the character size */
  921. iface->mode.c_cflag = (iface->mode.c_cflag & ~CSIZE) | charsize;
  922. /* commit the desired attributes */
  923. if (tcsetattr(iface->fd, TCSAFLUSH, &iface->mode)) {
  924. ast_log(LOG_ERROR, "Error setting attributes on %s (%s)\n", iface->name, strerror(errno));
  925. continue;
  926. }
  927. /* set the msdstrip */
  928. iface->msdstrip = msdstrip;
  929. /* set the message expiry time */
  930. iface->msg_expiry = msg_expiry;
  931. /*
  932. * start the listener thread
  933. *
  934. * The listener thread does not actually hold a ref to iface. When all
  935. * external refs go away, the destructor will stop the listener thread
  936. * before actually destroying the iface object.
  937. */
  938. ast_verb(3, "Starting SMDI monitor thread for %s\n", iface->name);
  939. if (ast_pthread_create_background(&iface->thread, NULL, smdi_read, iface)) {
  940. ast_log(LOG_ERROR, "Error starting SMDI monitor thread for %s\n", iface->name);
  941. continue;
  942. }
  943. ao2_link(new_ifaces, iface);
  944. ast_module_ref(ast_module_info->self);
  945. } else {
  946. ast_log(LOG_NOTICE, "Ignoring unknown option %s in %s\n", v->name, config_file);
  947. }
  948. }
  949. destroy_all_mailbox_mappings();
  950. mwi_monitor.polling_interval = DEFAULT_POLLING_INTERVAL;
  951. for (v = ast_variable_browse(conf, "mailboxes"); v; v = v->next) {
  952. if (!strcasecmp(v->name, "smdiport")) {
  953. ao2_cleanup(mailbox_iface);
  954. if (!(mailbox_iface = ao2_find(new_ifaces, v->value, OBJ_SEARCH_KEY))) {
  955. ast_log(LOG_NOTICE, "SMDI interface %s not found\n", v->value);
  956. continue;
  957. }
  958. } else if (!strcasecmp(v->name, "pollinginterval")) {
  959. if (sscanf(v->value, "%30u", &mwi_monitor.polling_interval) != 1) {
  960. ast_log(LOG_ERROR, "Invalid value for pollinginterval: %s\n", v->value);
  961. mwi_monitor.polling_interval = DEFAULT_POLLING_INTERVAL;
  962. }
  963. } else {
  964. if (!mailbox_iface) {
  965. ast_log(LOG_ERROR, "Mailbox mapping ignored, no valid SMDI interface specified in mailboxes section\n");
  966. continue;
  967. }
  968. append_mailbox_mapping(v, mailbox_iface);
  969. }
  970. }
  971. ao2_cleanup(mailbox_iface);
  972. ast_config_destroy(conf);
  973. ao2_global_obj_replace_unref(smdi_ifaces, new_ifaces);
  974. if (!AST_LIST_EMPTY(&mwi_monitor.mailbox_mappings) && mwi_monitor.thread == AST_PTHREADT_NULL
  975. && ast_pthread_create_background(&mwi_monitor.thread, NULL, mwi_monitor_handler, NULL)) {
  976. ast_log(LOG_ERROR, "Failed to start MWI monitoring thread. This module will not operate.\n");
  977. return -1;
  978. }
  979. if (!ao2_container_count(new_ifaces)) {
  980. res = 1;
  981. }
  982. return res;
  983. }
  984. struct smdi_msg_datastore {
  985. unsigned int id;
  986. struct ast_smdi_interface *iface;
  987. struct ast_smdi_md_message *md_msg;
  988. };
  989. static void smdi_msg_datastore_destroy(void *data)
  990. {
  991. struct smdi_msg_datastore *smd = data;
  992. ao2_cleanup(smd->iface);
  993. ao2_cleanup(smd->md_msg);
  994. free(smd);
  995. }
  996. static const struct ast_datastore_info smdi_msg_datastore_info = {
  997. .type = "SMDIMSG",
  998. .destroy = smdi_msg_datastore_destroy,
  999. };
  1000. static int smdi_msg_id;
  1001. /*! In milliseconds */
  1002. #define SMDI_RETRIEVE_TIMEOUT_DEFAULT 3000
  1003. AST_APP_OPTIONS(smdi_msg_ret_options, BEGIN_OPTIONS
  1004. AST_APP_OPTION('t', OPT_SEARCH_TERMINAL),
  1005. AST_APP_OPTION('n', OPT_SEARCH_NUMBER),
  1006. END_OPTIONS );
  1007. static int smdi_msg_retrieve_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  1008. {
  1009. struct ast_module_user *u;
  1010. AST_DECLARE_APP_ARGS(args,
  1011. AST_APP_ARG(port);
  1012. AST_APP_ARG(search_key);
  1013. AST_APP_ARG(timeout);
  1014. AST_APP_ARG(options);
  1015. );
  1016. struct ast_flags options = { 0 };
  1017. unsigned int timeout = SMDI_RETRIEVE_TIMEOUT_DEFAULT;
  1018. int res = -1;
  1019. char *parse = NULL;
  1020. struct smdi_msg_datastore *smd = NULL;
  1021. struct ast_datastore *datastore = NULL;
  1022. struct ast_smdi_interface *iface = NULL;
  1023. struct ast_smdi_md_message *md_msg = NULL;
  1024. u = ast_module_user_add(chan);
  1025. if (ast_strlen_zero(data)) {
  1026. ast_log(LOG_ERROR, "SMDI_MSG_RETRIEVE requires an argument\n");
  1027. goto return_error;
  1028. }
  1029. if (!chan) {
  1030. ast_log(LOG_ERROR, "SMDI_MSG_RETRIEVE must be used with a channel\n");
  1031. goto return_error;
  1032. }
  1033. ast_autoservice_start(chan);
  1034. parse = ast_strdupa(data);
  1035. AST_STANDARD_APP_ARGS(args, parse);
  1036. if (ast_strlen_zero(args.port) || ast_strlen_zero(args.search_key)) {
  1037. ast_log(LOG_ERROR, "Invalid arguments provided to SMDI_MSG_RETRIEVE\n");
  1038. goto return_error;
  1039. }
  1040. if (!(iface = ast_smdi_interface_find(args.port))) {
  1041. ast_log(LOG_ERROR, "SMDI port '%s' not found\n", args.port);
  1042. goto return_error;
  1043. }
  1044. if (!ast_strlen_zero(args.options)) {
  1045. ast_app_parse_options(smdi_msg_ret_options, &options, NULL, args.options);
  1046. }
  1047. if (!ast_strlen_zero(args.timeout)) {
  1048. if (sscanf(args.timeout, "%30u", &timeout) != 1) {
  1049. ast_log(LOG_ERROR, "'%s' is not a valid timeout\n", args.timeout);
  1050. timeout = SMDI_RETRIEVE_TIMEOUT_DEFAULT;
  1051. }
  1052. }
  1053. if (!(md_msg = smdi_message_wait(iface, timeout, SMDI_MD, args.search_key, options))) {
  1054. ast_log(LOG_WARNING, "No SMDI message retrieved for search key '%s' after "
  1055. "waiting %u ms.\n", args.search_key, timeout);
  1056. goto return_error;
  1057. }
  1058. if (!(smd = ast_calloc(1, sizeof(*smd))))
  1059. goto return_error;
  1060. smd->iface = ao2_bump(iface);
  1061. smd->md_msg = ao2_bump(md_msg);
  1062. smd->id = ast_atomic_fetchadd_int((int *) &smdi_msg_id, 1);
  1063. snprintf(buf, len, "%u", smd->id);
  1064. if (!(datastore = ast_datastore_alloc(&smdi_msg_datastore_info, buf)))
  1065. goto return_error;
  1066. datastore->data = smd;
  1067. ast_channel_lock(chan);
  1068. ast_channel_datastore_add(chan, datastore);
  1069. ast_channel_unlock(chan);
  1070. res = 0;
  1071. return_error:
  1072. ao2_cleanup(iface);
  1073. ao2_cleanup(md_msg);
  1074. if (smd && !datastore)
  1075. smdi_msg_datastore_destroy(smd);
  1076. if (parse)
  1077. ast_autoservice_stop(chan);
  1078. ast_module_user_remove(u);
  1079. return res;
  1080. }
  1081. static int smdi_msg_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  1082. {
  1083. struct ast_module_user *u;
  1084. int res = -1;
  1085. AST_DECLARE_APP_ARGS(args,
  1086. AST_APP_ARG(id);
  1087. AST_APP_ARG(component);
  1088. );
  1089. char *parse;
  1090. struct ast_datastore *datastore = NULL;
  1091. struct smdi_msg_datastore *smd = NULL;
  1092. u = ast_module_user_add(chan);
  1093. if (!chan) {
  1094. ast_log(LOG_ERROR, "SMDI_MSG can not be called without a channel\n");
  1095. goto return_error;
  1096. }
  1097. if (ast_strlen_zero(data)) {
  1098. ast_log(LOG_WARNING, "SMDI_MSG requires an argument\n");
  1099. goto return_error;
  1100. }
  1101. parse = ast_strdupa(data);
  1102. AST_STANDARD_APP_ARGS(args, parse);
  1103. if (ast_strlen_zero(args.id)) {
  1104. ast_log(LOG_WARNING, "ID must be supplied to SMDI_MSG\n");
  1105. goto return_error;
  1106. }
  1107. if (ast_strlen_zero(args.component)) {
  1108. ast_log(LOG_WARNING, "ID must be supplied to SMDI_MSG\n");
  1109. goto return_error;
  1110. }
  1111. ast_channel_lock(chan);
  1112. datastore = ast_channel_datastore_find(chan, &smdi_msg_datastore_info, args.id);
  1113. ast_channel_unlock(chan);
  1114. if (!datastore) {
  1115. ast_log(LOG_WARNING, "No SMDI message found for message ID '%s'\n", args.id);
  1116. goto return_error;
  1117. }
  1118. smd = datastore->data;
  1119. if (!strcasecmp(args.component, "number")) {
  1120. ast_copy_string(buf, smd->md_msg->mesg_desk_num, len);
  1121. } else if (!strcasecmp(args.component, "terminal")) {
  1122. ast_copy_string(buf, smd->md_msg->mesg_desk_term, len);
  1123. } else if (!strcasecmp(args.component, "station")) {
  1124. ast_copy_string(buf, smd->md_msg->fwd_st, len);
  1125. } else if (!strcasecmp(args.component, "callerid")) {
  1126. ast_copy_string(buf, smd->md_msg->calling_st, len);
  1127. } else if (!strcasecmp(args.component, "type")) {
  1128. snprintf(buf, len, "%c", smd->md_msg->type);
  1129. } else {
  1130. ast_log(LOG_ERROR, "'%s' is not a valid message component for SMDI_MSG\n",
  1131. args.component);
  1132. goto return_error;
  1133. }
  1134. res = 0;
  1135. return_error:
  1136. ast_module_user_remove(u);
  1137. return res;
  1138. }
  1139. static struct ast_custom_function smdi_msg_retrieve_function = {
  1140. .name = "SMDI_MSG_RETRIEVE",
  1141. .read = smdi_msg_retrieve_read,
  1142. };
  1143. static struct ast_custom_function smdi_msg_function = {
  1144. .name = "SMDI_MSG",
  1145. .read = smdi_msg_read,
  1146. };
  1147. static int _unload_module(int fromload);
  1148. /*!
  1149. * \brief Load the module
  1150. *
  1151. * Module loading including tests for configuration or dependencies.
  1152. * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
  1153. * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
  1154. * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
  1155. * configuration file or other non-critical problem return
  1156. * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
  1157. */
  1158. static int load_module(void)
  1159. {
  1160. int res;
  1161. smdi_loaded = 1;
  1162. ast_mutex_init(&mwi_monitor.lock);
  1163. ast_cond_init(&mwi_monitor.cond, NULL);
  1164. /* load the config and start the listener threads*/
  1165. res = smdi_load(0);
  1166. if (res < 0) {
  1167. _unload_module(1);
  1168. return AST_MODULE_LOAD_DECLINE;
  1169. } else if (res == 1) {
  1170. _unload_module(1);
  1171. ast_log(LOG_NOTICE, "No SMDI interfaces are available to listen on, not starting SMDI listener.\n");
  1172. return AST_MODULE_LOAD_DECLINE;
  1173. }
  1174. ast_custom_function_register(&smdi_msg_retrieve_function);
  1175. ast_custom_function_register(&smdi_msg_function);
  1176. /* For Optional API. */
  1177. ast_module_shutdown_ref(ast_module_info->self);
  1178. return AST_MODULE_LOAD_SUCCESS;
  1179. }
  1180. static int _unload_module(int fromload)
  1181. {
  1182. if (!smdi_loaded) {
  1183. return 0;
  1184. }
  1185. ao2_global_obj_release(smdi_ifaces);
  1186. destroy_all_mailbox_mappings();
  1187. ast_mutex_lock(&mwi_monitor.lock);
  1188. mwi_monitor.stop = 1;
  1189. ast_cond_signal(&mwi_monitor.cond);
  1190. ast_mutex_unlock(&mwi_monitor.lock);
  1191. if (mwi_monitor.thread != AST_PTHREADT_NULL) {
  1192. pthread_join(mwi_monitor.thread, NULL);
  1193. }
  1194. if (!fromload) {
  1195. ast_custom_function_unregister(&smdi_msg_retrieve_function);
  1196. ast_custom_function_unregister(&smdi_msg_function);
  1197. }
  1198. smdi_loaded = 0;
  1199. return 0;
  1200. }
  1201. static int unload_module(void)
  1202. {
  1203. return _unload_module(0);
  1204. }
  1205. static int reload(void)
  1206. {
  1207. int res;
  1208. res = smdi_load(1);
  1209. if (res < 0) {
  1210. return res;
  1211. } else if (res == 1) {
  1212. ast_log(LOG_WARNING, "No SMDI interfaces were specified to listen on, not starting SDMI listener.\n");
  1213. return 0;
  1214. } else
  1215. return 0;
  1216. }
  1217. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Simplified Message Desk Interface (SMDI) Resource",
  1218. .support_level = AST_MODULE_SUPPORT_CORE,
  1219. .load = load_module,
  1220. .unload = unload_module,
  1221. .reload = reload,
  1222. .load_pri = AST_MODPRI_CHANNEL_DEPEND,
  1223. );