res_smdi.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442
  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. if (iface->thread != AST_PTHREADT_NULL && iface->thread != AST_PTHREADT_STOP) {
  212. pthread_cancel(iface->thread);
  213. pthread_join(iface->thread, NULL);
  214. }
  215. iface->thread = AST_PTHREADT_STOP;
  216. if (iface->file) {
  217. fclose(iface->file);
  218. }
  219. ao2_cleanup(iface->md_q);
  220. ast_mutex_destroy(&iface->md_q_lock);
  221. ast_cond_destroy(&iface->md_q_cond);
  222. ao2_cleanup(iface->mwi_q);
  223. ast_mutex_destroy(&iface->mwi_q_lock);
  224. ast_cond_destroy(&iface->mwi_q_cond);
  225. free(iface);
  226. ast_module_unref(ast_module_info->self);
  227. }
  228. /*!
  229. * \internal
  230. * \brief Push an SMDI message to the back of an interface's message queue.
  231. * \param iface a pointer to the interface to use.
  232. * \param md_msg a pointer to the message to use.
  233. */
  234. static void ast_smdi_md_message_push(struct ast_smdi_interface *iface, struct ast_smdi_md_message *md_msg)
  235. {
  236. ast_mutex_lock(&iface->md_q_lock);
  237. ao2_link(iface->md_q, md_msg);
  238. ast_cond_broadcast(&iface->md_q_cond);
  239. ast_mutex_unlock(&iface->md_q_lock);
  240. }
  241. /*!
  242. * \internal
  243. * \brief Push an SMDI message to the back of an interface's message queue.
  244. * \param iface a pointer to the interface to use.
  245. * \param mwi_msg a pointer to the message to use.
  246. */
  247. static void ast_smdi_mwi_message_push(struct ast_smdi_interface *iface, struct ast_smdi_mwi_message *mwi_msg)
  248. {
  249. ast_mutex_lock(&iface->mwi_q_lock);
  250. ao2_link(iface->mwi_q, mwi_msg);
  251. ast_cond_broadcast(&iface->mwi_q_cond);
  252. ast_mutex_unlock(&iface->mwi_q_lock);
  253. }
  254. static int smdi_toggle_mwi(struct ast_smdi_interface *iface, const char *mailbox, int on)
  255. {
  256. FILE *file;
  257. int i;
  258. if (!(file = fopen(iface->name, "w"))) {
  259. ast_log(LOG_ERROR, "Error opening SMDI interface %s (%s) for writing\n", iface->name, strerror(errno));
  260. return 1;
  261. }
  262. ao2_wrlock(iface);
  263. fprintf(file, "%s:MWI ", on ? "OP" : "RMV");
  264. for (i = 0; i < iface->msdstrip; i++)
  265. fprintf(file, "0");
  266. fprintf(file, "%s!\x04", mailbox);
  267. fclose(file);
  268. ao2_unlock(iface);
  269. ast_debug(1, "Sent MWI set message for %s on %s\n", mailbox, iface->name);
  270. return 0;
  271. }
  272. int AST_OPTIONAL_API_NAME(ast_smdi_mwi_set)(struct ast_smdi_interface *iface, const char *mailbox)
  273. {
  274. return smdi_toggle_mwi(iface, mailbox, 1);
  275. }
  276. int AST_OPTIONAL_API_NAME(ast_smdi_mwi_unset)(struct ast_smdi_interface *iface, const char *mailbox)
  277. {
  278. return smdi_toggle_mwi(iface, mailbox, 0);
  279. }
  280. enum smdi_message_type {
  281. SMDI_MWI,
  282. SMDI_MD,
  283. };
  284. static inline int lock_msg_q(struct ast_smdi_interface *iface, enum smdi_message_type type)
  285. {
  286. switch (type) {
  287. case SMDI_MWI:
  288. return ast_mutex_lock(&iface->mwi_q_lock);
  289. case SMDI_MD:
  290. return ast_mutex_lock(&iface->md_q_lock);
  291. }
  292. return -1;
  293. }
  294. static inline int unlock_msg_q(struct ast_smdi_interface *iface, enum smdi_message_type type)
  295. {
  296. switch (type) {
  297. case SMDI_MWI:
  298. return ast_mutex_unlock(&iface->mwi_q_lock);
  299. case SMDI_MD:
  300. return ast_mutex_unlock(&iface->md_q_lock);
  301. }
  302. return -1;
  303. }
  304. static inline void *unlink_from_msg_q(struct ast_smdi_interface *iface, enum smdi_message_type type)
  305. {
  306. switch (type) {
  307. case SMDI_MWI:
  308. return ao2_callback(iface->mwi_q, OBJ_UNLINK, NULL, NULL);
  309. case SMDI_MD:
  310. return ao2_callback(iface->md_q, OBJ_UNLINK, NULL, NULL);
  311. }
  312. return NULL;
  313. }
  314. static inline struct timeval msg_timestamp(void *msg, enum smdi_message_type type)
  315. {
  316. struct ast_smdi_md_message *md_msg = msg;
  317. struct ast_smdi_mwi_message *mwi_msg = msg;
  318. switch (type) {
  319. case SMDI_MWI:
  320. return mwi_msg->timestamp;
  321. case SMDI_MD:
  322. return md_msg->timestamp;
  323. }
  324. return ast_tv(0, 0);
  325. }
  326. static void purge_old_messages(struct ast_smdi_interface *iface, enum smdi_message_type type)
  327. {
  328. struct timeval now = ast_tvnow();
  329. long elapsed = 0;
  330. void *msg;
  331. lock_msg_q(iface, type);
  332. msg = unlink_from_msg_q(iface, type);
  333. unlock_msg_q(iface, type);
  334. /* purge old messages */
  335. while (msg) {
  336. elapsed = ast_tvdiff_ms(now, msg_timestamp(msg, type));
  337. if (elapsed > iface->msg_expiry) {
  338. /* found an expired message */
  339. ao2_ref(msg, -1);
  340. ast_log(LOG_NOTICE, "Purged expired message from %s SMDI %s message queue. "
  341. "Message was %ld milliseconds too old.\n",
  342. iface->name, (type == SMDI_MD) ? "MD" : "MWI",
  343. elapsed - iface->msg_expiry);
  344. lock_msg_q(iface, type);
  345. msg = unlink_from_msg_q(iface, type);
  346. unlock_msg_q(iface, type);
  347. } else {
  348. /* good message, put it back and return */
  349. switch (type) {
  350. case SMDI_MD:
  351. ast_smdi_md_message_push(iface, msg);
  352. break;
  353. case SMDI_MWI:
  354. ast_smdi_mwi_message_push(iface, msg);
  355. break;
  356. }
  357. ao2_ref(msg, -1);
  358. break;
  359. }
  360. }
  361. }
  362. static void *smdi_msg_pop(struct ast_smdi_interface *iface, enum smdi_message_type type)
  363. {
  364. void *msg;
  365. purge_old_messages(iface, type);
  366. lock_msg_q(iface, type);
  367. msg = unlink_from_msg_q(iface, type);
  368. unlock_msg_q(iface, type);
  369. return msg;
  370. }
  371. enum {
  372. OPT_SEARCH_TERMINAL = (1 << 0),
  373. OPT_SEARCH_NUMBER = (1 << 1),
  374. };
  375. static void *smdi_msg_find(struct ast_smdi_interface *iface,
  376. enum smdi_message_type type, const char *search_key, struct ast_flags options)
  377. {
  378. void *msg = NULL;
  379. purge_old_messages(iface, type);
  380. switch (type) {
  381. case SMDI_MD:
  382. if (ast_strlen_zero(search_key)) {
  383. /* No search key provided (the code from chan_dahdi does this).
  384. * Just pop the top message off of the queue. */
  385. msg = ao2_callback(iface->md_q, 0, NULL, NULL);
  386. } else if (ast_test_flag(&options, OPT_SEARCH_TERMINAL)) {
  387. /* Searching by the message desk terminal */
  388. struct ast_smdi_md_message md_msg = { .name = "" };
  389. strncpy(md_msg.mesg_desk_term, search_key, SMDI_MESG_DESK_TERM_LEN);
  390. msg = ao2_find(iface->md_q, &md_msg, OBJ_SEARCH_OBJECT);
  391. } else if (ast_test_flag(&options, OPT_SEARCH_NUMBER)) {
  392. /* Searching by the message desk number */
  393. struct ast_smdi_md_message md_msg = { .name = "" };
  394. strncpy(md_msg.mesg_desk_num, search_key, SMDI_MESG_DESK_NUM_LEN);
  395. msg = ao2_find(iface->md_q, &md_msg, OBJ_SEARCH_OBJECT);
  396. } else {
  397. /* Searching by the forwarding station */
  398. msg = ao2_find(iface->md_q, search_key, OBJ_SEARCH_KEY);
  399. }
  400. break;
  401. case SMDI_MWI:
  402. if (ast_strlen_zero(search_key)) {
  403. /* No search key provided (the code from chan_dahdi does this).
  404. * Just pop the top message off of the queue. */
  405. msg = ao2_callback(iface->mwi_q, 0, NULL, NULL);
  406. } else {
  407. msg = ao2_find(iface->mwi_q, search_key, OBJ_SEARCH_KEY);
  408. }
  409. break;
  410. }
  411. return msg;
  412. }
  413. static void *smdi_message_wait(struct ast_smdi_interface *iface, int timeout,
  414. enum smdi_message_type type, const char *search_key, struct ast_flags options)
  415. {
  416. struct timeval start;
  417. long diff = 0;
  418. void *msg;
  419. ast_cond_t *cond = NULL;
  420. ast_mutex_t *lock = NULL;
  421. switch (type) {
  422. case SMDI_MWI:
  423. cond = &iface->mwi_q_cond;
  424. lock = &iface->mwi_q_lock;
  425. break;
  426. case SMDI_MD:
  427. cond = &iface->md_q_cond;
  428. lock = &iface->md_q_lock;
  429. break;
  430. }
  431. start = ast_tvnow();
  432. while (diff < timeout) {
  433. struct timespec ts = { 0, };
  434. struct timeval wait;
  435. lock_msg_q(iface, type);
  436. if ((msg = smdi_msg_find(iface, type, search_key, options))) {
  437. unlock_msg_q(iface, type);
  438. return msg;
  439. }
  440. wait = ast_tvadd(start, ast_tv(0, timeout));
  441. ts.tv_sec = wait.tv_sec;
  442. ts.tv_nsec = wait.tv_usec * 1000;
  443. /* If there were no messages in the queue, then go to sleep until one
  444. * arrives. */
  445. ast_cond_timedwait(cond, lock, &ts);
  446. if ((msg = smdi_msg_find(iface, type, search_key, options))) {
  447. unlock_msg_q(iface, type);
  448. return msg;
  449. }
  450. unlock_msg_q(iface, type);
  451. /* check timeout */
  452. diff = ast_tvdiff_ms(ast_tvnow(), start);
  453. }
  454. return NULL;
  455. }
  456. struct ast_smdi_md_message * AST_OPTIONAL_API_NAME(ast_smdi_md_message_pop)(struct ast_smdi_interface *iface)
  457. {
  458. return smdi_msg_pop(iface, SMDI_MD);
  459. }
  460. struct ast_smdi_md_message * AST_OPTIONAL_API_NAME(ast_smdi_md_message_wait)(struct ast_smdi_interface *iface, int timeout)
  461. {
  462. struct ast_flags options = { 0 };
  463. return smdi_message_wait(iface, timeout, SMDI_MD, NULL, options);
  464. }
  465. struct ast_smdi_mwi_message * AST_OPTIONAL_API_NAME(ast_smdi_mwi_message_pop)(struct ast_smdi_interface *iface)
  466. {
  467. return smdi_msg_pop(iface, SMDI_MWI);
  468. }
  469. struct ast_smdi_mwi_message * AST_OPTIONAL_API_NAME(ast_smdi_mwi_message_wait)(struct ast_smdi_interface *iface, int timeout)
  470. {
  471. struct ast_flags options = { 0 };
  472. return smdi_message_wait(iface, timeout, SMDI_MWI, NULL, options);
  473. }
  474. struct ast_smdi_mwi_message * AST_OPTIONAL_API_NAME(ast_smdi_mwi_message_wait_station)(struct ast_smdi_interface *iface, int timeout,
  475. const char *station)
  476. {
  477. struct ast_flags options = { 0 };
  478. return smdi_message_wait(iface, timeout, SMDI_MWI, station, options);
  479. }
  480. struct ast_smdi_interface * AST_OPTIONAL_API_NAME(ast_smdi_interface_find)(const char *iface_name)
  481. {
  482. struct ao2_container *c;
  483. struct ast_smdi_interface *iface = NULL;
  484. c = ao2_global_obj_ref(smdi_ifaces);
  485. if (c) {
  486. iface = ao2_find(c, iface_name, OBJ_SEARCH_KEY);
  487. ao2_ref(c, -1);
  488. }
  489. return iface;
  490. }
  491. /*!
  492. * \internal
  493. * \brief Read an SMDI message.
  494. *
  495. * \param iface_p the SMDI interface to read from.
  496. *
  497. * This function loops and reads from and SMDI interface. It must be stopped
  498. * using pthread_cancel().
  499. */
  500. static void *smdi_read(void *iface_p)
  501. {
  502. struct ast_smdi_interface *iface = iface_p;
  503. struct ast_smdi_md_message *md_msg;
  504. struct ast_smdi_mwi_message *mwi_msg;
  505. char c = '\0';
  506. char *cp = NULL;
  507. int i;
  508. int start = 0;
  509. /* read an smdi message */
  510. while ((c = fgetc(iface->file))) {
  511. /* check if this is the start of a message */
  512. if (!start) {
  513. if (c == 'M') {
  514. ast_debug(1, "Read an 'M' to start an SMDI message\n");
  515. start = 1;
  516. }
  517. continue;
  518. }
  519. if (c == 'D') { /* MD message */
  520. start = 0;
  521. ast_debug(1, "Read a 'D' ... it's an MD message.\n");
  522. if (!(md_msg = ast_calloc(1, sizeof(*md_msg)))) {
  523. ao2_ref(iface, -1);
  524. return NULL;
  525. }
  526. md_msg = ao2_alloc(sizeof(*md_msg), NULL);
  527. /* read the message desk number */
  528. for (i = 0; i < sizeof(md_msg->mesg_desk_num) - 1; i++) {
  529. md_msg->mesg_desk_num[i] = fgetc(iface->file);
  530. ast_debug(1, "Read a '%c'\n", md_msg->mesg_desk_num[i]);
  531. }
  532. md_msg->mesg_desk_num[sizeof(md_msg->mesg_desk_num) - 1] = '\0';
  533. ast_debug(1, "The message desk number is '%s'\n", md_msg->mesg_desk_num);
  534. /* read the message desk terminal number */
  535. for (i = 0; i < sizeof(md_msg->mesg_desk_term) - 1; i++) {
  536. md_msg->mesg_desk_term[i] = fgetc(iface->file);
  537. ast_debug(1, "Read a '%c'\n", md_msg->mesg_desk_term[i]);
  538. }
  539. md_msg->mesg_desk_term[sizeof(md_msg->mesg_desk_term) - 1] = '\0';
  540. ast_debug(1, "The message desk terminal is '%s'\n", md_msg->mesg_desk_term);
  541. /* read the message type */
  542. md_msg->type = fgetc(iface->file);
  543. ast_debug(1, "Message type is '%c'\n", md_msg->type);
  544. /* read the forwarding station number (may be blank) */
  545. cp = &md_msg->fwd_st[0];
  546. for (i = 0; i < sizeof(md_msg->fwd_st) - 1; i++) {
  547. if ((c = fgetc(iface->file)) == ' ') {
  548. *cp = '\0';
  549. ast_debug(1, "Read a space, done looking for the forwarding station\n");
  550. break;
  551. }
  552. /* store c in md_msg->fwd_st */
  553. if (i >= iface->msdstrip) {
  554. ast_debug(1, "Read a '%c' and stored it in the forwarding station buffer\n", c);
  555. *cp++ = c;
  556. } else {
  557. 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);
  558. }
  559. }
  560. /* make sure the value is null terminated, even if this truncates it */
  561. md_msg->fwd_st[sizeof(md_msg->fwd_st) - 1] = '\0';
  562. cp = NULL;
  563. ast_debug(1, "The forwarding station is '%s'\n", md_msg->fwd_st);
  564. /* Put the fwd_st in the name field so that we can use ao2_find to look
  565. * up a message on this field */
  566. ast_copy_string(md_msg->name, md_msg->fwd_st, sizeof(md_msg->name));
  567. /* read the calling station number (may be blank) */
  568. cp = &md_msg->calling_st[0];
  569. for (i = 0; i < sizeof(md_msg->calling_st) - 1; i++) {
  570. if (!isdigit((c = fgetc(iface->file)))) {
  571. *cp = '\0';
  572. ast_debug(1, "Read a '%c', but didn't store it in the calling station buffer because it's not a digit\n", c);
  573. if (c == ' ') {
  574. /* Don't break on a space. We may read the space before the calling station
  575. * here if the forwarding station buffer filled up. */
  576. i--; /* We're still on the same character */
  577. continue;
  578. }
  579. break;
  580. }
  581. /* store c in md_msg->calling_st */
  582. if (i >= iface->msdstrip) {
  583. ast_debug(1, "Read a '%c' and stored it in the calling station buffer\n", c);
  584. *cp++ = c;
  585. } else {
  586. 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);
  587. }
  588. }
  589. /* make sure the value is null terminated, even if this truncates it */
  590. md_msg->calling_st[sizeof(md_msg->calling_st) - 1] = '\0';
  591. cp = NULL;
  592. ast_debug(1, "The calling station is '%s'\n", md_msg->calling_st);
  593. /* add the message to the message queue */
  594. md_msg->timestamp = ast_tvnow();
  595. ast_smdi_md_message_push(iface, md_msg);
  596. ast_debug(1, "Received SMDI MD message on %s\n", iface->name);
  597. ao2_ref(md_msg, -1);
  598. } else if (c == 'W') { /* MWI message */
  599. start = 0;
  600. ast_debug(1, "Read a 'W', it's an MWI message. (No more debug coming for MWI messages)\n");
  601. if (!(mwi_msg = ast_calloc(1, sizeof(*mwi_msg)))) {
  602. ao2_ref(iface, -1);
  603. return NULL;
  604. }
  605. mwi_msg = ao2_alloc(sizeof(*mwi_msg), NULL);
  606. /* discard the 'I' (from 'MWI') */
  607. fgetc(iface->file);
  608. /* read the forwarding station number (may be blank) */
  609. cp = &mwi_msg->fwd_st[0];
  610. for (i = 0; i < sizeof(mwi_msg->fwd_st) - 1; i++) {
  611. if ((c = fgetc(iface->file)) == ' ') {
  612. *cp = '\0';
  613. break;
  614. }
  615. /* store c in md_msg->fwd_st */
  616. if (i >= iface->msdstrip)
  617. *cp++ = c;
  618. }
  619. /* make sure the station number is null terminated, even if this will truncate it */
  620. mwi_msg->fwd_st[sizeof(mwi_msg->fwd_st) - 1] = '\0';
  621. cp = NULL;
  622. /* Put the fwd_st in the name field so that we can use ao2_find to look
  623. * up a message on this field */
  624. ast_copy_string(mwi_msg->name, mwi_msg->fwd_st, sizeof(mwi_msg->name));
  625. /* read the mwi failure cause */
  626. for (i = 0; i < sizeof(mwi_msg->cause) - 1; i++)
  627. mwi_msg->cause[i] = fgetc(iface->file);
  628. mwi_msg->cause[sizeof(mwi_msg->cause) - 1] = '\0';
  629. /* add the message to the message queue */
  630. mwi_msg->timestamp = ast_tvnow();
  631. ast_smdi_mwi_message_push(iface, mwi_msg);
  632. ast_debug(1, "Received SMDI MWI message on %s\n", iface->name);
  633. ao2_ref(mwi_msg, -1);
  634. } else {
  635. ast_log(LOG_ERROR, "Unknown SMDI message type received on %s (M%c).\n", iface->name, c);
  636. start = 0;
  637. }
  638. }
  639. ast_log(LOG_ERROR, "Error reading from SMDI interface %s, stopping listener thread\n", iface->name);
  640. ao2_ref(iface, -1);
  641. return NULL;
  642. }
  643. static void destroy_mailbox_mapping(struct mailbox_mapping *mm)
  644. {
  645. ast_string_field_free_memory(mm);
  646. ao2_ref(mm->iface, -1);
  647. free(mm);
  648. }
  649. static void destroy_all_mailbox_mappings(void)
  650. {
  651. struct mailbox_mapping *mm;
  652. ast_mutex_lock(&mwi_monitor.lock);
  653. while ((mm = AST_LIST_REMOVE_HEAD(&mwi_monitor.mailbox_mappings, entry)))
  654. destroy_mailbox_mapping(mm);
  655. ast_mutex_unlock(&mwi_monitor.lock);
  656. }
  657. static void append_mailbox_mapping(struct ast_variable *var, struct ast_smdi_interface *iface)
  658. {
  659. struct mailbox_mapping *mm;
  660. char *mailbox, *context;
  661. if (!(mm = ast_calloc_with_stringfields(1, struct mailbox_mapping, 32)))
  662. return;
  663. ast_string_field_set(mm, smdi, var->name);
  664. context = ast_strdupa(var->value);
  665. mailbox = strsep(&context, "@");
  666. if (ast_strlen_zero(context))
  667. context = "default";
  668. ast_string_field_set(mm, mailbox, mailbox);
  669. ast_string_field_set(mm, context, context);
  670. mm->iface = ao2_bump(iface);
  671. ast_mutex_lock(&mwi_monitor.lock);
  672. AST_LIST_INSERT_TAIL(&mwi_monitor.mailbox_mappings, mm, entry);
  673. ast_mutex_unlock(&mwi_monitor.lock);
  674. }
  675. /*!
  676. * \note Called with the mwi_monitor.lock locked
  677. */
  678. static void poll_mailbox(struct mailbox_mapping *mm)
  679. {
  680. char buf[1024];
  681. unsigned int state;
  682. snprintf(buf, sizeof(buf), "%s@%s", mm->mailbox, mm->context);
  683. state = !!ast_app_has_voicemail(mm->mailbox, NULL);
  684. if (state != mm->cur_state) {
  685. if (state)
  686. ast_smdi_mwi_set(mm->iface, mm->smdi);
  687. else
  688. ast_smdi_mwi_unset(mm->iface, mm->smdi);
  689. mm->cur_state = state;
  690. }
  691. }
  692. static void *mwi_monitor_handler(void *data)
  693. {
  694. while (!mwi_monitor.stop) {
  695. struct timespec ts = { 0, };
  696. struct timeval polltime;
  697. struct mailbox_mapping *mm;
  698. ast_mutex_lock(&mwi_monitor.lock);
  699. mwi_monitor.last_poll = ast_tvnow();
  700. AST_LIST_TRAVERSE(&mwi_monitor.mailbox_mappings, mm, entry)
  701. poll_mailbox(mm);
  702. /* Sleep up to the configured polling interval. Allow unload_module()
  703. * to signal us to wake up and exit. */
  704. polltime = ast_tvadd(mwi_monitor.last_poll, ast_tv(mwi_monitor.polling_interval, 0));
  705. ts.tv_sec = polltime.tv_sec;
  706. ts.tv_nsec = polltime.tv_usec * 1000;
  707. ast_cond_timedwait(&mwi_monitor.cond, &mwi_monitor.lock, &ts);
  708. ast_mutex_unlock(&mwi_monitor.lock);
  709. }
  710. return NULL;
  711. }
  712. static int smdi_mwi_q_cmp_fn(void *obj, void *data, int flags)
  713. {
  714. struct ast_smdi_mwi_message *msg = obj;
  715. char *str = data;
  716. return !strcmp(msg->name, str) ? CMP_MATCH | CMP_STOP : 0;
  717. }
  718. static int smdi_md_q_cmp_fn(void *obj, void *arg, int flags)
  719. {
  720. const struct ast_smdi_md_message *msg = obj;
  721. const struct ast_smdi_md_message *search_msg = arg;
  722. const char *search_key = arg;
  723. int cmp = 0;
  724. switch (flags & OBJ_SEARCH_MASK) {
  725. case OBJ_SEARCH_OBJECT:
  726. if (search_msg->mesg_desk_num) {
  727. cmp = strcmp(msg->mesg_desk_num, search_msg->mesg_desk_num);
  728. }
  729. if (search_msg->mesg_desk_term) {
  730. cmp |= strcmp(msg->mesg_desk_term, search_msg->mesg_desk_term);
  731. }
  732. break;
  733. case OBJ_SEARCH_KEY:
  734. cmp = strcmp(msg->name, search_key);
  735. break;
  736. }
  737. if (cmp) {
  738. return 0;
  739. }
  740. return CMP_MATCH;
  741. }
  742. static struct ast_smdi_interface *alloc_smdi_interface(void)
  743. {
  744. struct ast_smdi_interface *iface;
  745. if (!(iface = ao2_alloc(sizeof(*iface), smdi_interface_destroy))) {
  746. return NULL;
  747. }
  748. iface->md_q = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, NULL, smdi_md_q_cmp_fn);
  749. iface->mwi_q = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, NULL, smdi_mwi_q_cmp_fn);
  750. ast_mutex_init(&iface->md_q_lock);
  751. ast_cond_init(&iface->md_q_cond, NULL);
  752. ast_mutex_init(&iface->mwi_q_lock);
  753. ast_cond_init(&iface->mwi_q_cond, NULL);
  754. return iface;
  755. }
  756. static int smdi_ifaces_cmp_fn(void *obj, void *data, int flags)
  757. {
  758. struct ast_smdi_interface *iface = obj;
  759. char *str = data;
  760. return !strcmp(iface->name, str) ? CMP_MATCH | CMP_STOP : 0;
  761. }
  762. /*!
  763. * \internal
  764. * \brief Load and reload SMDI configuration.
  765. * \param reload this should be 1 if we are reloading and 0 if not.
  766. *
  767. * This function loads/reloads the SMDI configuration and starts and stops
  768. * interfaces accordingly.
  769. *
  770. * \return zero on success, -1 on failure, and 1 if no smdi interfaces were started.
  771. */
  772. static int smdi_load(int reload)
  773. {
  774. struct ast_config *conf;
  775. struct ast_variable *v;
  776. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  777. int res = 0;
  778. RAII_VAR(struct ao2_container *, new_ifaces, NULL, ao2_cleanup);
  779. RAII_VAR(struct ao2_container *, old_ifaces, ao2_global_obj_ref(smdi_ifaces), ao2_cleanup);
  780. struct ast_smdi_interface *mailbox_iface = NULL;
  781. /* Config options */
  782. speed_t baud_rate = B9600; /* 9600 baud rate */
  783. tcflag_t paritybit = PARENB; /* even parity checking */
  784. tcflag_t charsize = CS7; /* seven bit characters */
  785. int stopbits = 0; /* One stop bit */
  786. int msdstrip = 0; /* strip zero digits */
  787. long msg_expiry = SMDI_MSG_EXPIRY_TIME;
  788. if (!(conf = ast_config_load(config_file, config_flags)) || conf == CONFIG_STATUS_FILEINVALID) {
  789. if (reload)
  790. ast_log(LOG_NOTICE, "Unable to reload config %s: SMDI untouched\n", config_file);
  791. else
  792. ast_log(LOG_NOTICE, "Unable to load config %s: SMDI disabled\n", config_file);
  793. return 1;
  794. } else if (conf == CONFIG_STATUS_FILEUNCHANGED)
  795. return 0;
  796. new_ifaces = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0, NULL, smdi_ifaces_cmp_fn);
  797. for (v = ast_variable_browse(conf, "interfaces"); v; v = v->next) {
  798. RAII_VAR(struct ast_smdi_interface *, iface, NULL, ao2_cleanup);
  799. if (!strcasecmp(v->name, "baudrate")) {
  800. if (!strcasecmp(v->value, "9600"))
  801. baud_rate = B9600;
  802. else if (!strcasecmp(v->value, "4800"))
  803. baud_rate = B4800;
  804. else if (!strcasecmp(v->value, "2400"))
  805. baud_rate = B2400;
  806. else if (!strcasecmp(v->value, "1200"))
  807. baud_rate = B1200;
  808. else {
  809. ast_log(LOG_NOTICE, "Invalid baud rate '%s' specified in %s (line %d), using default\n", v->value, config_file, v->lineno);
  810. baud_rate = B9600;
  811. }
  812. } else if (!strcasecmp(v->name, "msdstrip")) {
  813. if (!sscanf(v->value, "%30d", &msdstrip)) {
  814. ast_log(LOG_NOTICE, "Invalid msdstrip value in %s (line %d), using default\n", config_file, v->lineno);
  815. msdstrip = 0;
  816. } else if (0 > msdstrip || msdstrip > 9) {
  817. ast_log(LOG_NOTICE, "Invalid msdstrip value in %s (line %d), using default\n", config_file, v->lineno);
  818. msdstrip = 0;
  819. }
  820. } else if (!strcasecmp(v->name, "msgexpirytime")) {
  821. if (!sscanf(v->value, "%30ld", &msg_expiry)) {
  822. ast_log(LOG_NOTICE, "Invalid msgexpirytime value in %s (line %d), using default\n", config_file, v->lineno);
  823. msg_expiry = SMDI_MSG_EXPIRY_TIME;
  824. }
  825. } else if (!strcasecmp(v->name, "paritybit")) {
  826. if (!strcasecmp(v->value, "even"))
  827. paritybit = PARENB;
  828. else if (!strcasecmp(v->value, "odd"))
  829. paritybit = PARENB | PARODD;
  830. else if (!strcasecmp(v->value, "none"))
  831. paritybit = ~PARENB;
  832. else {
  833. ast_log(LOG_NOTICE, "Invalid parity bit setting in %s (line %d), using default\n", config_file, v->lineno);
  834. paritybit = PARENB;
  835. }
  836. } else if (!strcasecmp(v->name, "charsize")) {
  837. if (!strcasecmp(v->value, "7"))
  838. charsize = CS7;
  839. else if (!strcasecmp(v->value, "8"))
  840. charsize = CS8;
  841. else {
  842. ast_log(LOG_NOTICE, "Invalid character size setting in %s (line %d), using default\n", config_file, v->lineno);
  843. charsize = CS7;
  844. }
  845. } else if (!strcasecmp(v->name, "twostopbits")) {
  846. stopbits = ast_true(v->name);
  847. } else if (!strcasecmp(v->name, "smdiport")) {
  848. if (reload) {
  849. /* we are reloading, check if we are already
  850. * monitoring this interface, if we are we do
  851. * not want to start it again. This also has
  852. * the side effect of not updating different
  853. * setting for the serial port, but it should
  854. * be trivial to rewrite this section so that
  855. * options on the port are changed without
  856. * restarting the interface. Or the interface
  857. * could be restarted with out emptying the
  858. * queue. */
  859. if ((iface = ao2_find(old_ifaces, v->value, OBJ_SEARCH_KEY))) {
  860. ast_log(LOG_NOTICE, "SMDI interface %s already running, not restarting\n", iface->name);
  861. ao2_link(new_ifaces, iface);
  862. continue;
  863. }
  864. }
  865. if (!(iface = alloc_smdi_interface()))
  866. continue;
  867. ast_copy_string(iface->name, v->value, sizeof(iface->name));
  868. iface->thread = AST_PTHREADT_NULL;
  869. if (!(iface->file = fopen(iface->name, "r"))) {
  870. ast_log(LOG_ERROR, "Error opening SMDI interface %s (%s)\n", iface->name, strerror(errno));
  871. continue;
  872. }
  873. iface->fd = fileno(iface->file);
  874. /* Set the proper attributes for our serial port. */
  875. /* get the current attributes from the port */
  876. if (tcgetattr(iface->fd, &iface->mode)) {
  877. ast_log(LOG_ERROR, "Error getting atributes of %s (%s)\n", iface->name, strerror(errno));
  878. continue;
  879. }
  880. /* set the desired speed */
  881. if (cfsetispeed(&iface->mode, baud_rate) || cfsetospeed(&iface->mode, baud_rate)) {
  882. ast_log(LOG_ERROR, "Error setting baud rate on %s (%s)\n", iface->name, strerror(errno));
  883. continue;
  884. }
  885. /* set the stop bits */
  886. if (stopbits)
  887. iface->mode.c_cflag = iface->mode.c_cflag | CSTOPB; /* set two stop bits */
  888. else
  889. iface->mode.c_cflag = iface->mode.c_cflag & ~CSTOPB; /* set one stop bit */
  890. /* set the parity */
  891. iface->mode.c_cflag = (iface->mode.c_cflag & ~PARENB & ~PARODD) | paritybit;
  892. /* set the character size */
  893. iface->mode.c_cflag = (iface->mode.c_cflag & ~CSIZE) | charsize;
  894. /* commit the desired attributes */
  895. if (tcsetattr(iface->fd, TCSAFLUSH, &iface->mode)) {
  896. ast_log(LOG_ERROR, "Error setting attributes on %s (%s)\n", iface->name, strerror(errno));
  897. continue;
  898. }
  899. /* set the msdstrip */
  900. iface->msdstrip = msdstrip;
  901. /* set the message expiry time */
  902. iface->msg_expiry = msg_expiry;
  903. /* start the listener thread */
  904. ast_verb(3, "Starting SMDI monitor thread for %s\n", iface->name);
  905. if (ast_pthread_create_background(&iface->thread, NULL, smdi_read, iface)) {
  906. ast_log(LOG_ERROR, "Error starting SMDI monitor thread for %s\n", iface->name);
  907. continue;
  908. }
  909. ao2_link(new_ifaces, iface);
  910. ast_module_ref(ast_module_info->self);
  911. } else {
  912. ast_log(LOG_NOTICE, "Ignoring unknown option %s in %s\n", v->name, config_file);
  913. }
  914. }
  915. destroy_all_mailbox_mappings();
  916. mwi_monitor.polling_interval = DEFAULT_POLLING_INTERVAL;
  917. for (v = ast_variable_browse(conf, "mailboxes"); v; v = v->next) {
  918. if (!strcasecmp(v->name, "smdiport")) {
  919. ao2_cleanup(mailbox_iface);
  920. if (!(mailbox_iface = ao2_find(new_ifaces, v->value, OBJ_SEARCH_KEY))) {
  921. ast_log(LOG_NOTICE, "SMDI interface %s not found\n", v->value);
  922. continue;
  923. }
  924. } else if (!strcasecmp(v->name, "pollinginterval")) {
  925. if (sscanf(v->value, "%30u", &mwi_monitor.polling_interval) != 1) {
  926. ast_log(LOG_ERROR, "Invalid value for pollinginterval: %s\n", v->value);
  927. mwi_monitor.polling_interval = DEFAULT_POLLING_INTERVAL;
  928. }
  929. } else {
  930. if (!mailbox_iface) {
  931. ast_log(LOG_ERROR, "Mailbox mapping ignored, no valid SMDI interface specified in mailboxes section\n");
  932. continue;
  933. }
  934. append_mailbox_mapping(v, mailbox_iface);
  935. }
  936. }
  937. ao2_cleanup(mailbox_iface);
  938. ast_config_destroy(conf);
  939. ao2_global_obj_replace_unref(smdi_ifaces, new_ifaces);
  940. if (!AST_LIST_EMPTY(&mwi_monitor.mailbox_mappings) && mwi_monitor.thread == AST_PTHREADT_NULL
  941. && ast_pthread_create_background(&mwi_monitor.thread, NULL, mwi_monitor_handler, NULL)) {
  942. ast_log(LOG_ERROR, "Failed to start MWI monitoring thread. This module will not operate.\n");
  943. return AST_MODULE_LOAD_FAILURE;
  944. }
  945. if (ao2_container_count(new_ifaces)) {
  946. res = 1;
  947. }
  948. return res;
  949. }
  950. struct smdi_msg_datastore {
  951. unsigned int id;
  952. struct ast_smdi_interface *iface;
  953. struct ast_smdi_md_message *md_msg;
  954. };
  955. static void smdi_msg_datastore_destroy(void *data)
  956. {
  957. struct smdi_msg_datastore *smd = data;
  958. ao2_cleanup(smd->iface);
  959. ao2_cleanup(smd->md_msg);
  960. free(smd);
  961. }
  962. static const struct ast_datastore_info smdi_msg_datastore_info = {
  963. .type = "SMDIMSG",
  964. .destroy = smdi_msg_datastore_destroy,
  965. };
  966. static int smdi_msg_id;
  967. /*! In milliseconds */
  968. #define SMDI_RETRIEVE_TIMEOUT_DEFAULT 3000
  969. AST_APP_OPTIONS(smdi_msg_ret_options, BEGIN_OPTIONS
  970. AST_APP_OPTION('t', OPT_SEARCH_TERMINAL),
  971. AST_APP_OPTION('n', OPT_SEARCH_NUMBER),
  972. END_OPTIONS );
  973. static int smdi_msg_retrieve_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  974. {
  975. struct ast_module_user *u;
  976. AST_DECLARE_APP_ARGS(args,
  977. AST_APP_ARG(port);
  978. AST_APP_ARG(search_key);
  979. AST_APP_ARG(timeout);
  980. AST_APP_ARG(options);
  981. );
  982. struct ast_flags options = { 0 };
  983. unsigned int timeout = SMDI_RETRIEVE_TIMEOUT_DEFAULT;
  984. int res = -1;
  985. char *parse = NULL;
  986. struct smdi_msg_datastore *smd = NULL;
  987. struct ast_datastore *datastore = NULL;
  988. struct ast_smdi_interface *iface = NULL;
  989. struct ast_smdi_md_message *md_msg = NULL;
  990. u = ast_module_user_add(chan);
  991. if (ast_strlen_zero(data)) {
  992. ast_log(LOG_ERROR, "SMDI_MSG_RETRIEVE requires an argument\n");
  993. goto return_error;
  994. }
  995. if (!chan) {
  996. ast_log(LOG_ERROR, "SMDI_MSG_RETRIEVE must be used with a channel\n");
  997. goto return_error;
  998. }
  999. ast_autoservice_start(chan);
  1000. parse = ast_strdupa(data);
  1001. AST_STANDARD_APP_ARGS(args, parse);
  1002. if (ast_strlen_zero(args.port) || ast_strlen_zero(args.search_key)) {
  1003. ast_log(LOG_ERROR, "Invalid arguments provided to SMDI_MSG_RETRIEVE\n");
  1004. goto return_error;
  1005. }
  1006. if (!(iface = ast_smdi_interface_find(args.port))) {
  1007. ast_log(LOG_ERROR, "SMDI port '%s' not found\n", args.port);
  1008. goto return_error;
  1009. }
  1010. if (!ast_strlen_zero(args.options)) {
  1011. ast_app_parse_options(smdi_msg_ret_options, &options, NULL, args.options);
  1012. }
  1013. if (!ast_strlen_zero(args.timeout)) {
  1014. if (sscanf(args.timeout, "%30u", &timeout) != 1) {
  1015. ast_log(LOG_ERROR, "'%s' is not a valid timeout\n", args.timeout);
  1016. timeout = SMDI_RETRIEVE_TIMEOUT_DEFAULT;
  1017. }
  1018. }
  1019. if (!(md_msg = smdi_message_wait(iface, timeout, SMDI_MD, args.search_key, options))) {
  1020. ast_log(LOG_WARNING, "No SMDI message retrieved for search key '%s' after "
  1021. "waiting %u ms.\n", args.search_key, timeout);
  1022. goto return_error;
  1023. }
  1024. if (!(smd = ast_calloc(1, sizeof(*smd))))
  1025. goto return_error;
  1026. smd->iface = ao2_bump(iface);
  1027. smd->md_msg = ao2_bump(md_msg);
  1028. smd->id = ast_atomic_fetchadd_int((int *) &smdi_msg_id, 1);
  1029. snprintf(buf, len, "%u", smd->id);
  1030. if (!(datastore = ast_datastore_alloc(&smdi_msg_datastore_info, buf)))
  1031. goto return_error;
  1032. datastore->data = smd;
  1033. ast_channel_lock(chan);
  1034. ast_channel_datastore_add(chan, datastore);
  1035. ast_channel_unlock(chan);
  1036. res = 0;
  1037. return_error:
  1038. ao2_cleanup(iface);
  1039. ao2_cleanup(md_msg);
  1040. if (smd && !datastore)
  1041. smdi_msg_datastore_destroy(smd);
  1042. if (parse)
  1043. ast_autoservice_stop(chan);
  1044. ast_module_user_remove(u);
  1045. return res;
  1046. }
  1047. static int smdi_msg_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  1048. {
  1049. struct ast_module_user *u;
  1050. int res = -1;
  1051. AST_DECLARE_APP_ARGS(args,
  1052. AST_APP_ARG(id);
  1053. AST_APP_ARG(component);
  1054. );
  1055. char *parse;
  1056. struct ast_datastore *datastore = NULL;
  1057. struct smdi_msg_datastore *smd = NULL;
  1058. u = ast_module_user_add(chan);
  1059. if (!chan) {
  1060. ast_log(LOG_ERROR, "SMDI_MSG can not be called without a channel\n");
  1061. goto return_error;
  1062. }
  1063. if (ast_strlen_zero(data)) {
  1064. ast_log(LOG_WARNING, "SMDI_MSG requires an argument\n");
  1065. goto return_error;
  1066. }
  1067. parse = ast_strdupa(data);
  1068. AST_STANDARD_APP_ARGS(args, parse);
  1069. if (ast_strlen_zero(args.id)) {
  1070. ast_log(LOG_WARNING, "ID must be supplied to SMDI_MSG\n");
  1071. goto return_error;
  1072. }
  1073. if (ast_strlen_zero(args.component)) {
  1074. ast_log(LOG_WARNING, "ID must be supplied to SMDI_MSG\n");
  1075. goto return_error;
  1076. }
  1077. ast_channel_lock(chan);
  1078. datastore = ast_channel_datastore_find(chan, &smdi_msg_datastore_info, args.id);
  1079. ast_channel_unlock(chan);
  1080. if (!datastore) {
  1081. ast_log(LOG_WARNING, "No SMDI message found for message ID '%s'\n", args.id);
  1082. goto return_error;
  1083. }
  1084. smd = datastore->data;
  1085. if (!strcasecmp(args.component, "number")) {
  1086. ast_copy_string(buf, smd->md_msg->mesg_desk_num, len);
  1087. } else if (!strcasecmp(args.component, "terminal")) {
  1088. ast_copy_string(buf, smd->md_msg->mesg_desk_term, len);
  1089. } else if (!strcasecmp(args.component, "station")) {
  1090. ast_copy_string(buf, smd->md_msg->fwd_st, len);
  1091. } else if (!strcasecmp(args.component, "callerid")) {
  1092. ast_copy_string(buf, smd->md_msg->calling_st, len);
  1093. } else if (!strcasecmp(args.component, "type")) {
  1094. snprintf(buf, len, "%c", smd->md_msg->type);
  1095. } else {
  1096. ast_log(LOG_ERROR, "'%s' is not a valid message component for SMDI_MSG\n",
  1097. args.component);
  1098. goto return_error;
  1099. }
  1100. res = 0;
  1101. return_error:
  1102. ast_module_user_remove(u);
  1103. return res;
  1104. }
  1105. static struct ast_custom_function smdi_msg_retrieve_function = {
  1106. .name = "SMDI_MSG_RETRIEVE",
  1107. .read = smdi_msg_retrieve_read,
  1108. };
  1109. static struct ast_custom_function smdi_msg_function = {
  1110. .name = "SMDI_MSG",
  1111. .read = smdi_msg_read,
  1112. };
  1113. static int _unload_module(int fromload);
  1114. /*!
  1115. * \brief Load the module
  1116. *
  1117. * Module loading including tests for configuration or dependencies.
  1118. * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
  1119. * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
  1120. * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
  1121. * configuration file or other non-critical problem return
  1122. * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
  1123. */
  1124. static int load_module(void)
  1125. {
  1126. int res;
  1127. smdi_loaded = 1;
  1128. ast_mutex_init(&mwi_monitor.lock);
  1129. ast_cond_init(&mwi_monitor.cond, NULL);
  1130. /* load the config and start the listener threads*/
  1131. res = smdi_load(0);
  1132. if (res < 0) {
  1133. _unload_module(1);
  1134. return res;
  1135. } else if (res == 1) {
  1136. _unload_module(1);
  1137. ast_log(LOG_NOTICE, "No SMDI interfaces are available to listen on, not starting SMDI listener.\n");
  1138. return AST_MODULE_LOAD_DECLINE;
  1139. }
  1140. ast_custom_function_register(&smdi_msg_retrieve_function);
  1141. ast_custom_function_register(&smdi_msg_function);
  1142. return AST_MODULE_LOAD_SUCCESS;
  1143. }
  1144. static int _unload_module(int fromload)
  1145. {
  1146. if (!smdi_loaded) {
  1147. return 0;
  1148. }
  1149. ao2_global_obj_release(smdi_ifaces);
  1150. destroy_all_mailbox_mappings();
  1151. ast_mutex_lock(&mwi_monitor.lock);
  1152. mwi_monitor.stop = 1;
  1153. ast_cond_signal(&mwi_monitor.cond);
  1154. ast_mutex_unlock(&mwi_monitor.lock);
  1155. if (mwi_monitor.thread != AST_PTHREADT_NULL) {
  1156. pthread_join(mwi_monitor.thread, NULL);
  1157. }
  1158. if (!fromload) {
  1159. ast_custom_function_unregister(&smdi_msg_retrieve_function);
  1160. ast_custom_function_unregister(&smdi_msg_function);
  1161. }
  1162. smdi_loaded = 0;
  1163. return 0;
  1164. }
  1165. static int unload_module(void)
  1166. {
  1167. return _unload_module(0);
  1168. }
  1169. static int reload(void)
  1170. {
  1171. int res;
  1172. res = smdi_load(1);
  1173. if (res < 0) {
  1174. return res;
  1175. } else if (res == 1) {
  1176. ast_log(LOG_WARNING, "No SMDI interfaces were specified to listen on, not starting SDMI listener.\n");
  1177. return 0;
  1178. } else
  1179. return 0;
  1180. }
  1181. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Simplified Message Desk Interface (SMDI) Resource",
  1182. .support_level = AST_MODULE_SUPPORT_CORE,
  1183. .load = load_module,
  1184. .unload = unload_module,
  1185. .reload = reload,
  1186. .load_pri = AST_MODPRI_CHANNEL_DEPEND,
  1187. );