app_alarmreceiver.c 21 KB

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