res_smdi.c 41 KB

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