app_alarmreceiver.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2004 - 2005 Steve Rodgers
  5. *
  6. * Steve Rodgers <hwstar@rodgers.sdcoxmail.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. * \brief Central Station Alarm receiver for Ademco Contact ID
  20. * \author Steve Rodgers <hwstar@rodgers.sdcoxmail.com>
  21. *
  22. * *** WARNING *** WARNING *** WARNING *** WARNING *** WARNING *** WARNING *** WARNING *** WARNING ***
  23. *
  24. * Use at your own risk. Please consult the GNU GPL license document included with Asterisk details. *
  25. *
  26. * *** WARNING *** WARNING *** WARNING *** WARNING *** WARNING *** WARNING *** WARNING *** WARNING ***
  27. *
  28. * \ingroup applications
  29. */
  30. #include <string.h>
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. #include <math.h>
  34. #include <sys/wait.h>
  35. #include <unistd.h>
  36. #include <sys/time.h>
  37. #include "asterisk.h"
  38. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  39. #include "asterisk/lock.h"
  40. #include "asterisk/file.h"
  41. #include "asterisk/logger.h"
  42. #include "asterisk/channel.h"
  43. #include "asterisk/pbx.h"
  44. #include "asterisk/module.h"
  45. #include "asterisk/translate.h"
  46. #include "asterisk/ulaw.h"
  47. #include "asterisk/options.h"
  48. #include "asterisk/app.h"
  49. #include "asterisk/dsp.h"
  50. #include "asterisk/config.h"
  51. #include "asterisk/localtime.h"
  52. #include "asterisk/callerid.h"
  53. #include "asterisk/astdb.h"
  54. #define ALMRCV_CONFIG "alarmreceiver.conf"
  55. #define ADEMCO_CONTACT_ID "ADEMCO_CONTACT_ID"
  56. struct event_node{
  57. char data[17];
  58. struct event_node *next;
  59. };
  60. typedef struct event_node event_node_t;
  61. static char *tdesc = "Alarm Receiver for Asterisk";
  62. static char *app = "AlarmReceiver";
  63. static char *synopsis = "Provide support for receving alarm reports from a burglar or fire alarm panel";
  64. static char *descrip =
  65. " AlarmReceiver(): Only 1 signalling format is supported at this time: Ademco\n"
  66. "Contact ID. This application should be called whenever there is an alarm\n"
  67. "panel calling in to dump its events. The application will handshake with the\n"
  68. "alarm panel, and receive events, validate them, handshake them, and store them\n"
  69. "until the panel hangs up. Once the panel hangs up, the application will run the\n"
  70. "system command specified by the eventcmd setting in alarmreceiver.conf and pipe\n"
  71. "the events to the standard input of the application. The configuration file also\n"
  72. "contains settings for DTMF timing, and for the loudness of the acknowledgement\n"
  73. "tones.\n";
  74. /* Config Variables */
  75. static int fdtimeout = 2000;
  76. static int sdtimeout = 200;
  77. static int toneloudness = 4096;
  78. static int log_individual_events = 0;
  79. static char event_spool_dir[128] = {'\0'};
  80. static char event_app[128] = {'\0'};
  81. static char db_family[128] = {'\0'};
  82. static char time_stamp_format[128] = {"%a %b %d, %Y @ %H:%M:%S %Z"};
  83. /* Misc variables */
  84. static char event_file[14] = "/event-XXXXXX";
  85. STANDARD_LOCAL_USER;
  86. LOCAL_USER_DECL;
  87. /*
  88. * Attempt to access a database variable and increment it,
  89. * provided that the user defined db-family in alarmreceiver.conf
  90. * The alarmreceiver app will write statistics to a few variables
  91. * in this family if it is defined. If the new key doesn't exist in the
  92. * family, then create it and set its value to 1.
  93. */
  94. static void database_increment( char *key )
  95. {
  96. int res = 0;
  97. unsigned v;
  98. char value[16];
  99. if (ast_strlen_zero(db_family))
  100. return; /* If not defined, don't do anything */
  101. res = ast_db_get(db_family, key, value, sizeof(value) - 1);
  102. if(res){
  103. if(option_verbose >= 4)
  104. ast_verbose(VERBOSE_PREFIX_4 "AlarmReceiver: Creating database entry %s and setting to 1\n", key);
  105. /* Guess we have to create it */
  106. res = ast_db_put(db_family, key, "1");
  107. return;
  108. }
  109. sscanf(value, "%u", &v);
  110. v++;
  111. if(option_verbose >= 4)
  112. ast_verbose(VERBOSE_PREFIX_4 "AlarmReceiver: New value for %s: %u\n", key, v);
  113. snprintf(value, sizeof(value), "%u", v);
  114. res = ast_db_put(db_family, key, value);
  115. if((res)&&(option_verbose >= 4))
  116. ast_verbose(VERBOSE_PREFIX_4 "AlarmReceiver: database_increment write error");
  117. return;
  118. }
  119. /*
  120. * Build a MuLaw data block for a single frequency tone
  121. */
  122. static void make_tone_burst(unsigned char *data, float freq, float loudness, int len, int *x)
  123. {
  124. int i;
  125. float val;
  126. for(i = 0; i < len; i++){
  127. val = loudness * sin((freq * 2.0 * M_PI * (*x)++)/8000.0);
  128. data[i] = AST_LIN2MU((int)val);
  129. }
  130. /* wrap back around from 8000 */
  131. if (*x >= 8000) *x = 0;
  132. return;
  133. }
  134. /*
  135. * Send a single tone burst for a specifed duration and frequency.
  136. * Returns 0 if successful
  137. */
  138. static int send_tone_burst(struct ast_channel *chan, float freq, int duration, int tldn)
  139. {
  140. int res = 0;
  141. int i = 0;
  142. int x = 0;
  143. struct ast_frame *f, wf;
  144. struct {
  145. unsigned char offset[AST_FRIENDLY_OFFSET];
  146. unsigned char buf[640];
  147. } tone_block;
  148. for(;;)
  149. {
  150. if (ast_waitfor(chan, -1) < 0){
  151. res = -1;
  152. break;
  153. }
  154. f = ast_read(chan);
  155. if (!f){
  156. res = -1;
  157. break;
  158. }
  159. if (f->frametype == AST_FRAME_VOICE) {
  160. wf.frametype = AST_FRAME_VOICE;
  161. wf.subclass = AST_FORMAT_ULAW;
  162. wf.offset = AST_FRIENDLY_OFFSET;
  163. wf.mallocd = 0;
  164. wf.data = tone_block.buf;
  165. wf.datalen = f->datalen;
  166. wf.samples = wf.datalen;
  167. make_tone_burst(tone_block.buf, freq, (float) tldn, wf.datalen, &x);
  168. i += wf.datalen / 8;
  169. if (i > duration) {
  170. break;
  171. }
  172. if (ast_write(chan, &wf)){
  173. if(option_verbose >= 4)
  174. ast_verbose(VERBOSE_PREFIX_4 "AlarmReceiver: Failed to write frame on %s\n", chan->name);
  175. ast_log(LOG_WARNING, "AlarmReceiver Failed to write frame on %s\n",chan->name);
  176. res = -1;
  177. break;
  178. }
  179. }
  180. ast_frfree(f);
  181. }
  182. return res;
  183. }
  184. /*
  185. * Receive a string of DTMF digits where the length of the digit string is known in advance. Do not give preferential
  186. * treatment to any digit value, and allow separate time out values to be specified for the first digit and all subsequent
  187. * digits.
  188. *
  189. * Returns 0 if all digits successfully received.
  190. * Returns 1 if a digit time out occurred
  191. * Returns -1 if the caller hung up or there was a channel error.
  192. *
  193. */
  194. static int receive_dtmf_digits(struct ast_channel *chan, char *digit_string, int length, int fdto, int sdto)
  195. {
  196. int res = 0;
  197. int i = 0;
  198. int r;
  199. struct ast_frame *f;
  200. struct timeval lastdigittime;
  201. lastdigittime = ast_tvnow();
  202. for(;;){
  203. /* if outa time, leave */
  204. if (ast_tvdiff_ms(ast_tvnow(), lastdigittime) >
  205. ((i > 0) ? sdto : fdto)){
  206. if(option_verbose >= 4)
  207. ast_verbose(VERBOSE_PREFIX_4 "AlarmReceiver: DTMF Digit Timeout on %s\n", chan->name);
  208. ast_log(LOG_DEBUG,"AlarmReceiver: DTMF timeout on chan %s\n",chan->name);
  209. res = 1;
  210. break;
  211. }
  212. if ((r = ast_waitfor(chan, -1) < 0)) {
  213. ast_log(LOG_DEBUG, "Waitfor returned %d\n", r);
  214. continue;
  215. }
  216. f = ast_read(chan);
  217. if (f == NULL){
  218. res = -1;
  219. break;
  220. }
  221. /* If they hung up, leave */
  222. if ((f->frametype == AST_FRAME_CONTROL) &&
  223. (f->subclass == AST_CONTROL_HANGUP)){
  224. ast_frfree(f);
  225. res = -1;
  226. break;
  227. }
  228. /* if not DTMF, just do it again */
  229. if (f->frametype != AST_FRAME_DTMF){
  230. ast_frfree(f);
  231. continue;
  232. }
  233. digit_string[i++] = f->subclass; /* save digit */
  234. ast_frfree(f);
  235. /* If we have all the digits we expect, leave */
  236. if(i >= length)
  237. break;
  238. lastdigittime = ast_tvnow();
  239. }
  240. digit_string[i] = '\0'; /* Nul terminate the end of the digit string */
  241. return res;
  242. }
  243. /*
  244. * Write the metadata to the log file
  245. */
  246. static int write_metadata( FILE *logfile, char *signalling_type, struct ast_channel *chan)
  247. {
  248. int res = 0;
  249. time_t t;
  250. struct tm now;
  251. char *cl,*cn;
  252. char workstring[80];
  253. char timestamp[80];
  254. /* Extract the caller ID location */
  255. if (chan->cid.cid_num)
  256. ast_copy_string(workstring, chan->cid.cid_num, sizeof(workstring));
  257. workstring[sizeof(workstring) - 1] = '\0';
  258. ast_callerid_parse(workstring, &cn, &cl);
  259. if (cl)
  260. ast_shrink_phone_number(cl);
  261. /* Get the current time */
  262. time(&t);
  263. ast_localtime(&t, &now, NULL);
  264. /* Format the time */
  265. strftime(timestamp, sizeof(timestamp), time_stamp_format, &now);
  266. res = fprintf(logfile, "\n\n[metadata]\n\n");
  267. if(res >= 0)
  268. res = fprintf(logfile, "PROTOCOL=%s\n", signalling_type);
  269. if(res >= 0)
  270. res = fprintf(logfile, "CALLINGFROM=%s\n", (!cl) ? "<unknown>" : cl);
  271. if(res >- 0)
  272. res = fprintf(logfile, "CALLERNAME=%s\n", (!cn) ? "<unknown>" : cn);
  273. if(res >= 0)
  274. res = fprintf(logfile, "TIMESTAMP=%s\n\n", timestamp);
  275. if(res >= 0)
  276. res = fprintf(logfile, "[events]\n\n");
  277. if(res < 0){
  278. ast_verbose(VERBOSE_PREFIX_4 "AlarmReceiver: can't write metadata\n");
  279. ast_log(LOG_DEBUG,"AlarmReceiver: can't write metadata\n");
  280. }
  281. else
  282. res = 0;
  283. return res;
  284. }
  285. /*
  286. * Write a single event to the log file
  287. */
  288. static int write_event( FILE *logfile, event_node_t *event)
  289. {
  290. int res = 0;
  291. if( fprintf(logfile, "%s\n", event->data) < 0)
  292. res = -1;
  293. return res;
  294. }
  295. /*
  296. * If we are configured to log events, do so here.
  297. *
  298. */
  299. static int log_events(struct ast_channel *chan, char *signalling_type, event_node_t *event)
  300. {
  301. int res = 0;
  302. char workstring[sizeof(event_spool_dir)+sizeof(event_file)] = "";
  303. int fd;
  304. FILE *logfile;
  305. event_node_t *elp = event;
  306. if (!ast_strlen_zero(event_spool_dir)) {
  307. /* Make a template */
  308. ast_copy_string(workstring, event_spool_dir, sizeof(workstring));
  309. strncat(workstring, event_file, sizeof(workstring) - strlen(workstring) - 1);
  310. /* Make the temporary file */
  311. fd = mkstemp(workstring);
  312. if(fd == -1){
  313. ast_verbose(VERBOSE_PREFIX_4 "AlarmReceiver: can't make temporary file\n");
  314. ast_log(LOG_DEBUG,"AlarmReceiver: can't make temporary file\n");
  315. res = -1;
  316. }
  317. if(!res){
  318. logfile = fdopen(fd, "w");
  319. if(logfile){
  320. /* Write the file */
  321. res = write_metadata(logfile, signalling_type, chan);
  322. if(!res)
  323. while((!res) && (elp != NULL)){
  324. res = write_event(logfile, elp);
  325. elp = elp->next;
  326. }
  327. if(!res){
  328. if(fflush(logfile) == EOF)
  329. res = -1;
  330. if(!res){
  331. if(fclose(logfile) == EOF)
  332. res = -1;
  333. }
  334. }
  335. }
  336. else
  337. res = -1;
  338. }
  339. }
  340. return res;
  341. }
  342. /*
  343. * This function implements the logic to receive the Ademco contact ID format.
  344. *
  345. * The function will return 0 when the caller hangs up, else a -1 if there was a problem.
  346. */
  347. static int receive_ademco_contact_id( struct ast_channel *chan, void *data, int fdto, int sdto, int tldn, event_node_t **ehead)
  348. {
  349. int i,j;
  350. int res = 0;
  351. int checksum;
  352. char event[17];
  353. event_node_t *enew, *elp;
  354. int got_some_digits = 0;
  355. int events_received = 0;
  356. int ack_retries = 0;
  357. static char digit_map[15] = "0123456789*#ABC";
  358. static unsigned char digit_weights[15] = {10,1,2,3,4,5,6,7,8,9,11,12,13,14,15};
  359. database_increment("calls-received");
  360. /* Wait for first event */
  361. if(option_verbose >= 4)
  362. ast_verbose(VERBOSE_PREFIX_4 "AlarmReceiver: Waiting for first event from panel\n");
  363. while(res >= 0){
  364. if(got_some_digits == 0){
  365. /* Send ACK tone sequence */
  366. if(option_verbose >= 4)
  367. ast_verbose(VERBOSE_PREFIX_4 "AlarmReceiver: Sending 1400Hz 100ms burst (ACK)\n");
  368. res = send_tone_burst(chan, 1400.0, 100, tldn);
  369. if(!res)
  370. res = ast_safe_sleep(chan, 100);
  371. if(!res){
  372. if(option_verbose >= 4)
  373. ast_verbose(VERBOSE_PREFIX_4 "AlarmReceiver: Sending 2300Hz 100ms burst (ACK)\n");
  374. res = send_tone_burst(chan, 2300.0, 100, tldn);
  375. }
  376. }
  377. if( res >= 0)
  378. res = receive_dtmf_digits(chan, event, sizeof(event) - 1, fdto, sdto);
  379. if (res < 0){
  380. if(events_received == 0)
  381. /* Hangup with no events received should be logged in the DB */
  382. database_increment("no-events-received");
  383. else{
  384. if(ack_retries){
  385. if(option_verbose >= 4)
  386. ast_verbose(VERBOSE_PREFIX_2 "AlarmReceiver: ACK retries during this call: %d\n", ack_retries);
  387. database_increment("ack-retries");
  388. }
  389. }
  390. if(option_verbose >= 4)
  391. ast_verbose(VERBOSE_PREFIX_4 "AlarmReceiver: App exiting...\n");
  392. res = -1;
  393. break;
  394. }
  395. if(res != 0){
  396. /* Didn't get all of the digits */
  397. if(option_verbose >= 2)
  398. ast_verbose(VERBOSE_PREFIX_2 "AlarmReceiver: Incomplete string: %s, trying again...\n", event);
  399. if(!got_some_digits){
  400. got_some_digits = (!ast_strlen_zero(event)) ? 1 : 0;
  401. ack_retries++;
  402. }
  403. continue;
  404. }
  405. got_some_digits = 1;
  406. if(option_verbose >= 2)
  407. ast_verbose(VERBOSE_PREFIX_2 "AlarmReceiver: Received Event %s\n", event);
  408. ast_log(LOG_DEBUG, "AlarmReceiver: Received event: %s\n", event);
  409. /* Calculate checksum */
  410. for(j = 0, checksum = 0; j < 16; j++){
  411. for(i = 0 ; i < sizeof(digit_map) ; i++){
  412. if(digit_map[i] == event[j])
  413. break;
  414. }
  415. if(i == 16)
  416. break;
  417. checksum += digit_weights[i];
  418. }
  419. if(i == 16){
  420. if(option_verbose >= 2)
  421. ast_verbose(VERBOSE_PREFIX_2 "AlarmReceiver: Bad DTMF character %c, trying again\n", event[j]);
  422. continue; /* Bad character */
  423. }
  424. /* Checksum is mod(15) of the total */
  425. checksum = checksum % 15;
  426. if(checksum){
  427. database_increment("checksum-errors");
  428. if(option_verbose >= 2){
  429. ast_verbose(VERBOSE_PREFIX_2 "AlarmReceiver: Nonzero checksum\n");
  430. ast_log(LOG_DEBUG, "AlarmReceiver: Nonzero checksum\n");
  431. continue;
  432. }
  433. }
  434. /* Check the message type for correctness */
  435. if(strncmp(event + 4, "18", 2)){
  436. if(strncmp(event + 4, "98", 2)){
  437. database_increment("format-errors");
  438. if(option_verbose >= 2)
  439. ast_verbose(VERBOSE_PREFIX_2 "AlarmReceiver: Wrong message type\n");
  440. ast_log(LOG_DEBUG, "AlarmReceiver: Wrong message type\n");
  441. continue;
  442. }
  443. }
  444. events_received++;
  445. /* Queue the Event */
  446. if((enew = malloc(sizeof(event_node_t))) == NULL){
  447. if(option_verbose >= 1)
  448. ast_verbose(VERBOSE_PREFIX_1 "AlarmReceiver: Failed to allocate memory\n");
  449. ast_log(LOG_WARNING, "AlarmReceiver Failed to allocate memory\n");
  450. res = -1;
  451. break;
  452. }
  453. memset(enew, 0, sizeof(event_node_t));
  454. enew->next = NULL;
  455. ast_copy_string(enew->data, event, sizeof(enew->data));
  456. /*
  457. * Insert event onto end of list
  458. */
  459. if(*ehead == NULL){
  460. *ehead = enew;
  461. }
  462. else{
  463. for(elp = *ehead; elp->next != NULL; elp = elp->next)
  464. ;
  465. elp->next = enew;
  466. }
  467. if(res > 0)
  468. res = 0;
  469. /* Let the user have the option of logging the single event before sending the kissoff tone */
  470. if((res == 0) && (log_individual_events))
  471. res = log_events(chan, ADEMCO_CONTACT_ID, enew);
  472. /* Wait 200 msec before sending kissoff */
  473. if(res == 0)
  474. res = ast_safe_sleep(chan, 200);
  475. /* Send the kissoff tone */
  476. if(res == 0)
  477. res = send_tone_burst(chan, 1400.0, 900, tldn);
  478. }
  479. return res;
  480. }
  481. /*
  482. * This is the main function called by Asterisk Core whenever the App is invoked in the extension logic.
  483. * This function will always return 0.
  484. */
  485. static int alarmreceiver_exec(struct ast_channel *chan, void *data)
  486. {
  487. int res = 0;
  488. struct localuser *u;
  489. event_node_t *elp, *efree;
  490. char signalling_type[64] = "";
  491. event_node_t *event_head = NULL;
  492. LOCAL_USER_ADD(u);
  493. /* Set write and read formats to ULAW */
  494. if(option_verbose >= 4)
  495. ast_verbose(VERBOSE_PREFIX_4 "AlarmReceiver: Setting read and write formats to ULAW\n");
  496. if (ast_set_write_format(chan,AST_FORMAT_ULAW)){
  497. ast_log(LOG_WARNING, "AlarmReceiver: Unable to set write format to Mu-law on %s\n",chan->name);
  498. LOCAL_USER_REMOVE(u);
  499. return -1;
  500. }
  501. if (ast_set_read_format(chan,AST_FORMAT_ULAW)){
  502. ast_log(LOG_WARNING, "AlarmReceiver: Unable to set read format to Mu-law on %s\n",chan->name);
  503. LOCAL_USER_REMOVE(u);
  504. return -1;
  505. }
  506. /* Set default values for this invokation of the application */
  507. ast_copy_string(signalling_type, ADEMCO_CONTACT_ID, sizeof(signalling_type));
  508. /* Answer the channel if it is not already */
  509. if(option_verbose >= 4)
  510. ast_verbose(VERBOSE_PREFIX_4 "AlarmReceiver: Answering channel\n");
  511. if (chan->_state != AST_STATE_UP) {
  512. res = ast_answer(chan);
  513. if (res) {
  514. LOCAL_USER_REMOVE(u);
  515. return -1;
  516. }
  517. }
  518. /* Wait for the connection to settle post-answer */
  519. if(option_verbose >= 4)
  520. ast_verbose(VERBOSE_PREFIX_4 "AlarmReceiver: Waiting for connection to stabilize\n");
  521. res = ast_safe_sleep(chan, 1250);
  522. /* Attempt to receive the events */
  523. if(!res){
  524. /* Determine the protocol to receive in advance */
  525. /* Note: Ademco contact is the only one supported at this time */
  526. /* Others may be added later */
  527. if(!strcmp(signalling_type, ADEMCO_CONTACT_ID))
  528. receive_ademco_contact_id(chan, data, fdtimeout, sdtimeout, toneloudness, &event_head);
  529. else
  530. res = -1;
  531. }
  532. /* Events queued by receiver, write them all out here if so configured */
  533. if((!res) && (log_individual_events == 0)){
  534. res = log_events(chan, signalling_type, event_head);
  535. }
  536. /*
  537. * Do we exec a command line at the end?
  538. */
  539. if((!res) && (!ast_strlen_zero(event_app)) && (event_head)){
  540. ast_log(LOG_DEBUG,"Alarmreceiver: executing: %s\n", event_app);
  541. ast_safe_system(event_app);
  542. }
  543. /*
  544. * Free up the data allocated in our linked list
  545. */
  546. for(elp = event_head; (elp != NULL);){
  547. efree = elp;
  548. elp = elp->next;
  549. free(efree);
  550. }
  551. LOCAL_USER_REMOVE(u);
  552. return 0;
  553. }
  554. /*
  555. * Load the configuration from the configuration file
  556. */
  557. static int load_config(void)
  558. {
  559. struct ast_config *cfg;
  560. char *p;
  561. /* Read in the config file */
  562. cfg = ast_config_load(ALMRCV_CONFIG);
  563. if(!cfg){
  564. if(option_verbose >= 4)
  565. ast_verbose(VERBOSE_PREFIX_4 "AlarmReceiver: No config file\n");
  566. }
  567. else{
  568. p = ast_variable_retrieve(cfg, "general", "eventcmd");
  569. if(p){
  570. ast_copy_string(event_app, p, sizeof(event_app));
  571. event_app[sizeof(event_app) - 1] = '\0';
  572. }
  573. p = ast_variable_retrieve(cfg, "general", "loudness");
  574. if(p){
  575. toneloudness = atoi(p);
  576. if(toneloudness < 100)
  577. toneloudness = 100;
  578. if(toneloudness > 8192)
  579. toneloudness = 8192;
  580. }
  581. p = ast_variable_retrieve(cfg, "general", "fdtimeout");
  582. if(p){
  583. fdtimeout = atoi(p);
  584. if(fdtimeout < 1000)
  585. fdtimeout = 1000;
  586. if(fdtimeout > 10000)
  587. fdtimeout = 10000;
  588. }
  589. p = ast_variable_retrieve(cfg, "general", "sdtimeout");
  590. if(p){
  591. sdtimeout = atoi(p);
  592. if(sdtimeout < 110)
  593. sdtimeout = 110;
  594. if(sdtimeout > 4000)
  595. sdtimeout = 4000;
  596. }
  597. p = ast_variable_retrieve(cfg, "general", "logindividualevents");
  598. if(p){
  599. log_individual_events = ast_true(p);
  600. }
  601. p = ast_variable_retrieve(cfg, "general", "eventspooldir");
  602. if(p){
  603. ast_copy_string(event_spool_dir, p, sizeof(event_spool_dir));
  604. event_spool_dir[sizeof(event_spool_dir) - 1] = '\0';
  605. }
  606. p = ast_variable_retrieve(cfg, "general", "timestampformat");
  607. if(p){
  608. ast_copy_string(time_stamp_format, p, sizeof(time_stamp_format));
  609. time_stamp_format[sizeof(time_stamp_format) - 1] = '\0';
  610. }
  611. p = ast_variable_retrieve(cfg, "general", "db-family");
  612. if(p){
  613. ast_copy_string(db_family, p, sizeof(db_family));
  614. db_family[sizeof(db_family) - 1] = '\0';
  615. }
  616. ast_config_destroy(cfg);
  617. }
  618. return 0;
  619. }
  620. /*
  621. * These functions are required to implement an Asterisk App.
  622. */
  623. int unload_module(void)
  624. {
  625. int res;
  626. res = ast_unregister_application(app);
  627. STANDARD_HANGUP_LOCALUSERS;
  628. return res;
  629. }
  630. int load_module(void)
  631. {
  632. load_config();
  633. return ast_register_application(app, alarmreceiver_exec, synopsis, descrip);
  634. }
  635. char *description(void)
  636. {
  637. return tdesc;
  638. }
  639. int usecount(void)
  640. {
  641. int res;
  642. STANDARD_USECOUNT(res);
  643. return res;
  644. }
  645. char *key()
  646. {
  647. return ASTERISK_GPL_KEY;
  648. }