Asterisk - The Open Source Telephony Project  21.4.1
Data Structures | Macros | Typedefs | Functions | Variables
app_alarmreceiver.c File Reference

Central Station Alarm receiver for Ademco Contact ID. More...

#include "asterisk.h"
#include <math.h>
#include <sys/wait.h>
#include <sys/time.h>
#include "asterisk/lock.h"
#include "asterisk/file.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
#include "asterisk/translate.h"
#include "asterisk/app.h"
#include "asterisk/dsp.h"
#include "asterisk/config.h"
#include "asterisk/localtime.h"
#include "asterisk/callerid.h"
#include "asterisk/astdb.h"
#include "asterisk/utils.h"
#include "asterisk/indications.h"
#include "asterisk/format_cache.h"

Go to the source code of this file.

Data Structures

struct  event_node
 

Macros

#define ADEMCO_AUDIO_CALL_NEXT   "606"
 
#define ADEMCO_CONTACT_ID   "ADEMCO_CONTACT_ID"
 
#define ADEMCO_EXPRESS_4_1   "ADEMCO_EXPRESS_4_1"
 
#define ADEMCO_EXPRESS_4_2   "ADEMCO_EXPRESS_4_2"
 
#define ADEMCO_HIGH_SPEED   "ADEMCO_HIGH_SPEED"
 
#define ADEMCO_MSG_TYPE_1   "18"
 
#define ADEMCO_MSG_TYPE_2   "98"
 
#define ADEMCO_MSG_TYPE_3   "17"
 
#define ADEMCO_MSG_TYPE_4   "27"
 
#define ADEMCO_MSG_TYPE_5   "55"
 
#define ADEMCO_MSG_TYPE_6   "56"
 
#define ADEMCO_SUPER_FAST   "ADEMCO_SUPER_FAST"
 
#define ALMRCV_CONFIG   "alarmreceiver.conf"
 
#define UNKNOWN_FORMAT   "UNKNOWN_FORMAT"
 

Typedefs

typedef struct event_node event_node_t
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
static int ademco_check_valid (char *signalling_type, char *event)
 Check if the message is in known and valid Ademco format. More...
 
static int ademco_detect_format (char *signalling_type, char *event, int *no_checksum)
 Detect the message format of an event. More...
 
static int ademco_verify_checksum (char *event, int expected)
 Verify Ademco checksum. More...
 
static int alarmreceiver_exec (struct ast_channel *chan, const char *data)
 This is the main function called by Asterisk Core whenever the App is invoked in the extension logic. More...
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static void database_increment (char *key)
 Attempt to access a database variable and increment it. More...
 
static int load_config (int reload)
 Load the configuration from the configuration file. More...
 
static int load_module (void)
 Load the module. More...
 
static int log_events (struct ast_channel *chan, char *signalling_type, event_node_t *event, int no_checksum)
 Log events if configuration key logindividualevents is enabled or on exit. More...
 
static int receive_ademco_event (struct ast_channel *chan, event_node_t **ehead, char *signalling_type, int *no_checksum)
 Receive Ademco ContactID or other format Data String. More...
 
static int receive_dtmf_digits (struct ast_channel *chan, char *digit_string, int buf_size, int expected, int *received)
 Receive a fixed length DTMF string. More...
 
static int reload (void)
 
static int send_tone_burst (struct ast_channel *chan, const char *tone_freq, int tone_duration, int delay)
 Send a single tone burst for a specified duration and frequency. More...
 
static int unload_module (void)
 Unregister Alarm Receiver App. More...
 
static int write_event (FILE *logfile, event_node_t *event)
 Log a single event. More...
 
static int write_metadata (FILE *logfile, char *signalling_type, struct ast_channel *chan, int no_checksum)
 Write metadata to log file. More...
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Alarm Receiver for Asterisk" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "da6642af068ee5e6490c5b1d2cc1d238" , .support_level = AST_MODULE_SUPPORT_EXTENDED, .load = load_module, .unload = unload_module, .reload = reload, }
 
static int answait = 1250
 
static const char app [] = "AlarmReceiver"
 
static const struct ast_module_infoast_module_info = &__mod_info
 
struct timeval call_start_time
 
static char db_family [128] = {'\0'}
 
struct {
   char   digit
 
   char   weight
 
digits_mapping []
 
static char event_app [128] = {'\0'}
 
static char event_file [14] = "/event-XXXXXX"
 
static char event_spool_dir [128] = {'\0'}
 
static int fdtimeout = 2000
 
static int log_individual_events = 0
 
static int no_group_meta = 0
 
static int sdtimeout = 200
 
static char time_stamp_format [128] = {"%a %b %d, %Y @ %H:%M:%S %Z"}
 
static int toneloudness = 4096
 

Detailed Description

Central Station Alarm receiver for Ademco Contact ID.

Author
Steve Rodgers hwsta.nosp@m.r@ro.nosp@m.dgers.nosp@m..sdc.nosp@m.oxmai.nosp@m.l.co.nosp@m.m

*** WARNING *** WARNING *** WARNING *** WARNING *** WARNING *** WARNING *** WARNING *** WARNING ***

Use at your own risk. Please consult the GNU GPL license document included with Asterisk. *

*** WARNING *** WARNING *** WARNING *** WARNING *** WARNING *** WARNING *** WARNING *** WARNING ***

Definition in file app_alarmreceiver.c.

Function Documentation

static int ademco_check_valid ( char *  signalling_type,
char *  event 
)
static

Check if the message is in known and valid Ademco format.

Parameters
signalling_typeExpected signalling type for the message
eventevent received
Return values
0The event is valid
-1The event is not valid

Definition at line 522 of file app_alarmreceiver.c.

Referenced by receive_ademco_event().

523 {
524  if (!strcmp(signalling_type, UNKNOWN_FORMAT)) {
525  return 1;
526  }
527 
528  if (!strcmp(signalling_type, ADEMCO_CONTACT_ID)
529  && strncmp(event + 4, ADEMCO_MSG_TYPE_1, 2)
530  && strncmp(event + 4, ADEMCO_MSG_TYPE_2, 2)) {
531  return -1;
532  }
533 
534  if (!strcmp(signalling_type, ADEMCO_EXPRESS_4_1) && strncmp(event + 4, ADEMCO_MSG_TYPE_3, 2)) {
535  return -1;
536  }
537 
538  if (!strcmp(signalling_type, ADEMCO_EXPRESS_4_2) && strncmp(event + 4, ADEMCO_MSG_TYPE_4, 2)) {
539  return -1;
540  }
541 
542  if (!strcmp(signalling_type, ADEMCO_HIGH_SPEED) && strncmp(event + 4, ADEMCO_MSG_TYPE_5, 2)) {
543  return -1;
544  }
545 
546  if (!strcmp(signalling_type, ADEMCO_SUPER_FAST) && strncmp(event + 4, ADEMCO_MSG_TYPE_6, 2)) {
547  return -1;
548  }
549 
550  return 0;
551 }
Definition: astman.c:222
static int ademco_detect_format ( char *  signalling_type,
char *  event,
int *  no_checksum 
)
static

Detect the message format of an event.

Parameters
signalling_typeExpected signalling type for the message
eventevent received
no_checksumShould we calculate checksum for the message
Returns
The expected digits for the detected event type

Definition at line 562 of file app_alarmreceiver.c.

References ast_debug.

Referenced by receive_ademco_event().

563 {
564  int res = 16;
565 
566  if (!strncmp(event + 4, ADEMCO_MSG_TYPE_1, 2)
567  || !strncmp(event + 4, ADEMCO_MSG_TYPE_2, 2)) {
568  sprintf(signalling_type, "%s", ADEMCO_CONTACT_ID);
569  }
570 
571  if (!strncmp(event + 4, ADEMCO_MSG_TYPE_3, 2)) {
572  sprintf(signalling_type, "%s", ADEMCO_EXPRESS_4_1);
573  res = 8;
574  }
575 
576  if (!strncmp(event + 4, ADEMCO_MSG_TYPE_4, 2)) {
577  sprintf(signalling_type, "%s", ADEMCO_EXPRESS_4_2);
578  res = 9;
579  }
580 
581  if (!strncmp(event + 4, ADEMCO_MSG_TYPE_5, 2)) {
582  sprintf(signalling_type, "%s", ADEMCO_HIGH_SPEED);
583  }
584 
585  if (!strncmp(event + 4, ADEMCO_MSG_TYPE_6, 2)) {
586  sprintf(signalling_type, "%s", ADEMCO_SUPER_FAST);
587  *no_checksum = 1;
588  res = 15;
589  }
590 
591  if (strcmp(signalling_type, UNKNOWN_FORMAT)) {
592  ast_verb(4, "AlarmMonitoring: Detected format %s.\n", signalling_type);
593  ast_debug(1, "AlarmMonitoring: Autodetected format %s.\n", signalling_type);
594  }
595 
596  return res;
597 }
Definition: astman.c:222
#define ast_debug(level,...)
Log a DEBUG message.
static int ademco_verify_checksum ( char *  event,
int  expected 
)
static

Verify Ademco checksum.

Since
11.0
Parameters
eventReceived DTMF String
expectedNumber of Digits expected
Return values
0success
-1failure

Definition at line 455 of file app_alarmreceiver.c.

Referenced by receive_ademco_event().

456 {
457  int checksum = 0;
458  int i, j;
459 
460  for (j = 0; j < expected; j++) {
461  for (i = 0; i < ARRAY_LEN(digits_mapping); i++) {
462  if (digits_mapping[i].digit == event[j]) {
463  break;
464  }
465  }
466 
467  if (i >= ARRAY_LEN(digits_mapping)) {
468  ast_verb(2, "AlarmReceiver: Bad DTMF character %c, trying again\n", event[j]);
469  return -1;
470  }
471 
472  checksum += digits_mapping[i].weight;
473  }
474 
475  /* Checksum is mod(15) of the total */
476  if (!(checksum % 15)) {
477  return 0;
478  }
479 
480  return -1;
481 }
Definition: astman.c:222
static int alarmreceiver_exec ( struct ast_channel chan,
const char *  data 
)
static

This is the main function called by Asterisk Core whenever the App is invoked in the extension logic.

Parameters
chanAsterisk Channel
dataApplication data
Return values
0success
-1failure

Definition at line 791 of file app_alarmreceiver.c.

References ast_answer(), ast_copy_string(), ast_debug, ast_format_alaw, ast_format_cmp(), AST_FORMAT_CMP_NOT_EQUAL, ast_format_ulaw, ast_safe_sleep(), ast_safe_system(), ast_set_read_format(), ast_set_write_format(), AST_STATE_UP, ast_tvnow(), log_events(), and receive_ademco_event().

Referenced by load_module().

792 {
793  int res = 0;
794  int no_checksum = 0;
795  event_node_t *elp, *efree;
796  char signalling_type[64] = "";
797  event_node_t *event_head = NULL;
798 
799  if ((ast_format_cmp(ast_channel_writeformat(chan), ast_format_ulaw) == AST_FORMAT_CMP_NOT_EQUAL) &&
800  (ast_format_cmp(ast_channel_writeformat(chan), ast_format_alaw) == AST_FORMAT_CMP_NOT_EQUAL)) {
801  ast_verb(4, "AlarmReceiver: Setting write format to Mu-law\n");
803  ast_log(LOG_WARNING, "AlarmReceiver: Unable to set write format to Mu-law on %s\n",ast_channel_name(chan));
804  return -1;
805  }
806  }
807 
808  if ((ast_format_cmp(ast_channel_readformat(chan), ast_format_ulaw) == AST_FORMAT_CMP_NOT_EQUAL) &&
809  (ast_format_cmp(ast_channel_readformat(chan), ast_format_alaw) == AST_FORMAT_CMP_NOT_EQUAL)) {
810  ast_verb(4, "AlarmReceiver: Setting read format to Mu-law\n");
812  ast_log(LOG_WARNING, "AlarmReceiver: Unable to set read format to Mu-law on %s\n",ast_channel_name(chan));
813  return -1;
814  }
815  }
816 
817  /* Set default values for this invocation of the application */
818  ast_copy_string(signalling_type, UNKNOWN_FORMAT, sizeof(signalling_type));
819  call_start_time = ast_tvnow();
820 
821  /* Answer the channel if it is not already */
822  if (ast_channel_state(chan) != AST_STATE_UP) {
823  ast_verb(4, "AlarmReceiver: Answering channel\n");
824  if (ast_answer(chan)) {
825  return -1;
826  }
827  }
828 
829  /* Wait for the connection to settle post-answer */
830  ast_verb(4, "AlarmReceiver: Waiting for connection to stabilize\n");
831  if (ast_safe_sleep(chan, answait)) {
832  return -1;
833  }
834 
835  /* Attempt to receive the events */
836  receive_ademco_event(chan, &event_head, signalling_type, &no_checksum);
837 
838  /* Events queued by receiver, write them all out here if so configured */
839  if (!log_individual_events) {
840  res = log_events(chan, signalling_type, event_head, no_checksum);
841  }
842 
843  /* Do we exec a command line at the end? */
844  if ((!res) && (!ast_strlen_zero(event_app)) && (event_head)) {
845  ast_debug(1,"Alarmreceiver: executing: %s\n", event_app);
846  ast_safe_system(event_app);
847  }
848 
849  /* Free up the data allocated in our linked list */
850  for (elp = event_head; (elp != NULL);) {
851  efree = elp;
852  elp = elp->next;
853  ast_free(efree);
854  }
855 
856  return 0;
857 }
int ast_safe_sleep(struct ast_channel *chan, int ms)
Wait for a specified amount of time, looking for hangups.
Definition: channel.c:1574
struct ast_format * ast_format_ulaw
Built-in cached ulaw format.
Definition: format_cache.c:86
ast_channel_state
ast_channel states
Definition: channelstate.h:35
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:159
enum ast_format_cmp_res ast_format_cmp(const struct ast_format *format1, const struct ast_format *format2)
Compare two formats.
Definition: format.c:201
int ast_set_read_format(struct ast_channel *chan, struct ast_format *format)
Sets read format on channel chan.
Definition: channel.c:5762
#define ast_debug(level,...)
Log a DEBUG message.
int ast_set_write_format(struct ast_channel *chan, struct ast_format *format)
Sets write format on channel chan.
Definition: channel.c:5803
int ast_safe_system(const char *s)
Safely spawn an OS shell command while closing file descriptors.
Definition: extconf.c:829
static int log_events(struct ast_channel *chan, char *signalling_type, event_node_t *event, int no_checksum)
Log events if configuration key logindividualevents is enabled or on exit.
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
struct ast_format * ast_format_alaw
Built-in cached alaw format.
Definition: format_cache.c:91
int ast_answer(struct ast_channel *chan)
Answer a channel.
Definition: channel.c:2805
static int receive_ademco_event(struct ast_channel *chan, event_node_t **ehead, char *signalling_type, int *no_checksum)
Receive Ademco ContactID or other format Data String.
static void database_increment ( char *  key)
static

Attempt to access a database variable and increment it.

Note
Only if the user defined db-family in alarmreceiver.conf

The alarmreceiver app will write statistics to a few variables in this family if it is defined. If the new key doesn't exist in the family, then create it and set its value to 1.

Parameters
keyA database key to increment

Definition at line 207 of file app_alarmreceiver.c.

References ast_db_get(), and ast_db_put().

Referenced by receive_ademco_event().

208 {
209  unsigned v;
210  char value[16];
211 
212  if (ast_strlen_zero(db_family)) {
213  return; /* If not defined, don't do anything */
214  }
215 
216  if (ast_db_get(db_family, key, value, sizeof(value) - 1)) {
217  ast_verb(4, "AlarmReceiver: Creating database entry %s and setting to 1\n", key);
218  /* Guess we have to create it */
219  ast_db_put(db_family, key, "1");
220  return;
221  }
222 
223  sscanf(value, "%30u", &v);
224  v++;
225 
226  ast_verb(4, "AlarmReceiver: New value for %s: %u\n", key, v);
227  snprintf(value, sizeof(value), "%u", v);
228 
229  if (ast_db_put(db_family, key, value)) {
230  ast_verb(4, "AlarmReceiver: database_increment write error\n");
231  }
232 
233  return;
234 }
int ast_db_get(const char *family, const char *key, char *value, int valuelen)
Get key value specified by family/key.
Definition: main/db.c:427
int ast_db_put(const char *family, const char *key, const char *value)
Store value addressed by family/key.
Definition: main/db.c:342
static int load_config ( int  reload)
static

Load the configuration from the configuration file.

Parameters
reloadTrue on reload
Return values
1success
0failure

Definition at line 867 of file app_alarmreceiver.c.

References ast_config_destroy(), ast_config_load, ast_copy_string(), ast_true(), and CONFIG_FLAG_FILEUNCHANGED.

Referenced by load_module().

868 {
869  struct ast_config *cfg;
870  const char *value;
871  struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
872 
873  /* Read in the config file */
874  cfg = ast_config_load(ALMRCV_CONFIG, config_flags);
875 
876  if (!cfg) {
877  ast_verb(4, "AlarmReceiver: No config file\n");
878  return 0;
879  } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
880  return 1;
881  } else if (cfg == CONFIG_STATUS_FILEINVALID) {
882  ast_log(LOG_ERROR, "Config file %s is in an invalid format. Aborting.\n",
883  ALMRCV_CONFIG);
884  return 0;
885  }
886 
887  if ((value = ast_variable_retrieve(cfg, "general", "eventcmd")) != NULL) {
888  ast_copy_string(event_app, value, sizeof(event_app));
889  }
890 
891  if ((value = ast_variable_retrieve(cfg, "general", "loudness")) != NULL) {
892  toneloudness = atoi(value);
893  if (toneloudness < 100) {
894  toneloudness = 100;
895  } else if (toneloudness > 8192) {
896  toneloudness = 8192;
897  }
898  }
899 
900  if ((value = ast_variable_retrieve(cfg, "general", "fdtimeout")) != NULL) {
901  fdtimeout = atoi(value);
902  if (fdtimeout < 1000) {
903  fdtimeout = 1000;
904  } else if (fdtimeout > 10000) {
905  fdtimeout = 10000;
906  }
907  }
908 
909  if ((value = ast_variable_retrieve(cfg, "general", "sdtimeout")) != NULL) {
910  sdtimeout = atoi(value);
911  if (sdtimeout < 110) {
912  sdtimeout = 110;
913  } else if (sdtimeout > 4000) {
914  sdtimeout = 4000;
915  }
916  }
917 
918  if ((value = ast_variable_retrieve(cfg, "general", "answait")) != NULL) {
919  answait = atoi(value);
920  if (answait < 500) {
921  answait = 500;
922  } else if (answait > 10000) {
923  answait = 10000;
924  }
925  }
926 
927  if ((value = ast_variable_retrieve(cfg, "general", "no_group_meta")) != NULL) {
928  no_group_meta = ast_true(value);
929  }
930 
931  if ((value = ast_variable_retrieve(cfg, "general", "logindividualevents")) != NULL) {
932  log_individual_events = ast_true(value);
933  }
934 
935  if ((value = ast_variable_retrieve(cfg, "general", "eventspooldir")) != NULL) {
936  ast_copy_string(event_spool_dir, value, sizeof(event_spool_dir));
937  }
938 
939  if ((value = ast_variable_retrieve(cfg, "general", "timestampformat")) != NULL) {
940  ast_copy_string(time_stamp_format, value, sizeof(time_stamp_format));
941  }
942 
943  if ((value = ast_variable_retrieve(cfg, "general", "db-family")) != NULL) {
944  ast_copy_string(db_family, value, sizeof(db_family));
945  }
946 
947  ast_config_destroy(cfg);
948 
949  return 1;
950 }
#define ast_config_load(filename, flags)
Load a config file.
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true". This function checks to see whether a string passed to it is an indication of an "true" value. It checks to see if the string is "yes", "true", "y", "t", "on" or "1".
Definition: utils.c:2199
Structure used to handle boolean flags.
Definition: utils.h:199
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
void ast_config_destroy(struct ast_config *cfg)
Destroys a config.
Definition: extconf.c:1289
static int load_module ( void  )
static

Load the module.

Module loading including tests for configuration or dependencies. This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE, or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails tests return AST_MODULE_LOAD_FAILURE. If the module can not load the configuration file or other non-critical problem return AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.

Definition at line 973 of file app_alarmreceiver.c.

References alarmreceiver_exec(), AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, ast_register_application_xml, and load_config().

974 {
975  if (load_config(0)) {
978  }
980  }
981 
983 }
static int load_config(int reload)
Load the configuration from the configuration file.
static int alarmreceiver_exec(struct ast_channel *chan, const char *data)
This is the main function called by Asterisk Core whenever the App is invoked in the extension logic...
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:640
static int log_events ( struct ast_channel chan,
char *  signalling_type,
event_node_t event,
int  no_checksum 
)
static

Log events if configuration key logindividualevents is enabled or on exit.

Parameters
chanAsterisk Channel
signalling_typeSignaling Type
eventEvent Structure
no_checksumExpecting messages without checksum
Return values
0success
-1failure

Definition at line 401 of file app_alarmreceiver.c.

References ast_copy_string(), ast_debug, write_event(), and write_metadata().

Referenced by alarmreceiver_exec(), and receive_ademco_event().

402 {
403  char workstring[sizeof(event_spool_dir) + sizeof(event_file)] = "";
404  int fd;
405  FILE *logfile;
406  event_node_t *elp = event;
407 
408  if (!ast_strlen_zero(event_spool_dir)) {
409 
410  /* Make a template */
411  ast_copy_string(workstring, event_spool_dir, sizeof(workstring));
412  strncat(workstring, event_file, sizeof(workstring) - strlen(workstring) - 1);
413 
414  /* Make the temporary file */
415  fd = mkstemp(workstring);
416 
417  if (fd == -1) {
418  ast_verb(3, "AlarmReceiver: can't make temporary file\n");
419  ast_debug(1, "AlarmReceiver: can't make temporary file\n");
420  return -1;
421  }
422 
423  if ((logfile = fdopen(fd, "w")) == NULL) {
424  return -1;
425  }
426 
427  /* Write the file */
428  if (write_metadata(logfile, signalling_type, chan, no_checksum)) {
429  fflush(logfile);
430  fclose(logfile);
431  return -1;
432  }
433 
434  while ((elp != NULL) && (write_event(logfile, elp) == 0)) {
435  elp = elp->next;
436  }
437 
438  fflush(logfile);
439  fclose(logfile);
440  }
441 
442  return 0;
443 }
static int write_metadata(FILE *logfile, char *signalling_type, struct ast_channel *chan, int no_checksum)
Write metadata to log file.
#define ast_debug(level,...)
Log a DEBUG message.
static int write_event(FILE *logfile, event_node_t *event)
Log a single event.
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
static int receive_ademco_event ( struct ast_channel chan,
event_node_t **  ehead,
char *  signalling_type,
int *  no_checksum 
)
static

Receive Ademco ContactID or other format Data String.

Parameters
chanAsterisk Channel
eheadPointer to events list
signalling_typeExpected signalling type for the message
no_checksumShould we calculate checksum for the message
Return values
0success
-1failure

Definition at line 610 of file app_alarmreceiver.c.

References ademco_check_valid(), ademco_detect_format(), ademco_verify_checksum(), ast_calloc, ast_copy_string(), ast_debug, ast_tvdiff_ms(), ast_tvnow(), database_increment(), log_events(), pbx_builtin_getvar_helper(), receive_dtmf_digits(), and send_tone_burst().

Referenced by alarmreceiver_exec().

611 {
612  int res = 0;
613  const char *limit;
614  char event[17];
615  event_node_t *enew, *elp;
616  int got_some_digits = 0;
617  int events_received = 0;
618  int ack_retries = 0;
619  int limit_retries = 0;
620  int expected_length = sizeof(event) - 1;
621 
622  database_increment("calls-received");
623 
624  /* Wait for first event */
625  ast_verb(4, "AlarmReceiver: Waiting for first event from panel...\n");
626 
627  while (res >= 0) {
628  int digits_received = 0;
629 
630  res = 0;
631 
632  if (log_individual_events) {
633  sprintf(signalling_type, "%s", UNKNOWN_FORMAT);
634  expected_length = 16;
635  *no_checksum = 0;
636  }
637 
638  if (got_some_digits == 0) {
639  /* Send ACK tone sequence */
640  ast_verb(4, "AlarmReceiver: Sending 1400Hz 100ms burst (ACK)\n");
641  res = send_tone_burst(chan, "1400", 100, 0);
642  if (!res) {
643  ast_verb(4, "AlarmReceiver: Sending 2300Hz 100ms burst (ACK)\n");
644  res = send_tone_burst(chan, "2300", 100, 100);
645  }
646  }
647  if (res) {
648  return -1;
649  }
650 
651  res = receive_dtmf_digits(chan, event, sizeof(event), expected_length, &digits_received);
652  if (res < 0) {
653  if (events_received == 0) {
654  /* Hangup with no events received should be logged in the DB */
655  database_increment("no-events-received");
656  ast_verb(4, "AlarmReceiver: No events received!\n");
657  } else {
658  if (ack_retries) {
659  database_increment("ack-retries");
660  ast_verb(4, "AlarmReceiver: ACK retries during this call: %d\n", ack_retries);
661  }
662  }
663  ast_verb(4, "AlarmReceiver: App exiting...\n");
664  break;
665  }
666 
667  if (!strcmp(signalling_type, UNKNOWN_FORMAT) && digits_received > 5) {
668  expected_length = ademco_detect_format(signalling_type, event, no_checksum);
669 
670  if (res > 0) {
671  if (digits_received == expected_length) {
672  res = limit_retries = 0;
673  } else if (digits_received == expected_length - 1
674  && (!strcmp(signalling_type, ADEMCO_EXPRESS_4_2)
675  || !strcmp(signalling_type, ADEMCO_EXPRESS_4_1))) {
676  /* ADEMCO EXPRESS without checksum */
677  res = limit_retries = 0;
678  expected_length--;
679  *no_checksum = 1;
680  ast_verb(4, "AlarmMonitoring: Skipping checksum for format %s.\n", signalling_type);
681  ast_debug(1, "AlarmMonitoring: Skipping checksum for format %s.\n", signalling_type);
682  }
683  }
684  }
685 
686  ast_channel_lock(chan);
687  limit = pbx_builtin_getvar_helper(chan, "ALARMRECEIVER_CALL_LIMIT");
688  if (!ast_strlen_zero(limit)) {
689  if (ast_tvdiff_ms(ast_tvnow(), call_start_time) > atoi(limit)) {
690  ast_channel_unlock(chan);
691  return -1;
692  }
693  }
694  limit = pbx_builtin_getvar_helper(chan, "ALARMRECEIVER_RETRIES_LIMIT");
695  ast_channel_unlock(chan);
696  if (!ast_strlen_zero(limit)) {
697  if (limit_retries + 1 >= atoi(limit)) {
698  return -1;
699  }
700  }
701 
702  if (res) {
703  /* Didn't get all of the digits */
704  ast_verb(2, "AlarmReceiver: Incomplete string: %s, trying again...\n", event);
705  limit_retries++;
706 
707  if (!events_received && strcmp(signalling_type, UNKNOWN_FORMAT))
708  {
709  sprintf(signalling_type, "%s", UNKNOWN_FORMAT);
710  expected_length = sizeof(event) - 1;
711  }
712 
713  if (!got_some_digits) {
714  got_some_digits = (!ast_strlen_zero(event)) ? 1 : 0;
715  ack_retries++;
716  }
717  continue;
718  }
719 
720  got_some_digits = 1;
721 
722  ast_verb(2, "AlarmReceiver: Received Event %s\n", event);
723  ast_debug(1, "AlarmReceiver: Received event: %s\n", event);
724 
725  /* Calculate checksum */
726  if (!(*no_checksum) && ademco_verify_checksum(event, expected_length)) {
727  database_increment("checksum-errors");
728  ast_verb(2, "AlarmReceiver: Nonzero checksum\n");
729  ast_debug(1, "AlarmReceiver: Nonzero checksum\n");
730  continue;
731  }
732 
733  /* Check the message type for correctness */
734  if (ademco_check_valid(signalling_type, event)) {
735  database_increment("format-errors");
736  ast_verb(2, "AlarmReceiver: Wrong message type\n");
737  ast_debug(1, "AlarmReceiver: Wrong message type\n");
738  continue;
739  }
740 
741  events_received++;
742 
743  /* Queue the Event */
744  if (!(enew = ast_calloc(1, sizeof(*enew)))) {
745  return -1;
746  }
747 
748  enew->next = NULL;
749  ast_copy_string(enew->data, event, sizeof(enew->data));
750 
751  /* Insert event onto end of list */
752  if (*ehead == NULL) {
753  *ehead = enew;
754  } else {
755  for (elp = *ehead; elp->next != NULL; elp = elp->next) {
756  ;
757  }
758  elp->next = enew;
759  }
760 
761  /* Let the user have the option of logging the single event before sending the kissoff tone */
762  if (log_individual_events && log_events(chan, signalling_type, enew, *no_checksum)) {
763  return -1;
764  }
765 
766  /* Send the kissoff tone (1400 Hz, 900 ms, after 200ms delay) */
767  if (send_tone_burst(chan, "1400", 900, 200)) {
768  return -1;
769  }
770 
771  /* If audio call follows, exit alarm receiver app */
772  if (!strcmp(signalling_type, ADEMCO_CONTACT_ID)
773  && !strncmp(event + 7, ADEMCO_AUDIO_CALL_NEXT, 3)) {
774  ast_verb(4, "AlarmReceiver: App exiting... Audio call next!\n");
775  return 0;
776  }
777  }
778 
779  return res;
780 }
static int receive_dtmf_digits(struct ast_channel *chan, char *digit_string, int buf_size, int expected, int *received)
Receive a fixed length DTMF string.
Definition: astman.c:222
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:159
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:107
static void database_increment(char *key)
Attempt to access a database variable and increment it.
const char * pbx_builtin_getvar_helper(struct ast_channel *chan, const char *name)
Return a pointer to the value of the corresponding channel variable.
#define ast_debug(level,...)
Log a DEBUG message.
static int ademco_verify_checksum(char *event, int expected)
Verify Ademco checksum.
static int send_tone_burst(struct ast_channel *chan, const char *tone_freq, int tone_duration, int delay)
Send a single tone burst for a specified duration and frequency.
static int log_events(struct ast_channel *chan, char *signalling_type, event_node_t *event, int no_checksum)
Log events if configuration key logindividualevents is enabled or on exit.
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
static int ademco_check_valid(char *signalling_type, char *event)
Check if the message is in known and valid Ademco format.
static int ademco_detect_format(char *signalling_type, char *event, int *no_checksum)
Detect the message format of an event.
static int receive_dtmf_digits ( struct ast_channel chan,
char *  digit_string,
int  buf_size,
int  expected,
int *  received 
)
static

Receive a fixed length DTMF string.

Note
Doesn't give preferential treatment to any digit,
allow different timeout values for the first and all subsequent digits
Parameters
chanAsterisk Channel
digit_stringDigits String
buf_sizeThe size of the Digits String buffer
expectedDigits expected for this message type
receivedPointer to number of digits received so far
Return values
0if all digits were successfully received
1if a timeout occurred
-1if the caller hung up or on channel errors

Definition at line 252 of file app_alarmreceiver.c.

References AST_CONTROL_HANGUP, ast_debug, AST_FRAME_CONTROL, ast_read(), ast_tvdiff_ms(), ast_tvnow(), ast_waitfor(), ast_frame::data, ast_frame::frametype, ast_frame_subclass::integer, and ast_frame::subclass.

Referenced by receive_ademco_event().

253 {
254  int rtn = 0;
255  int r;
256  struct ast_frame *f;
257  struct timeval lastdigittime;
258 
259  lastdigittime = ast_tvnow();
260  while (*received < expected && *received < buf_size - 1) {
261  /* If timed out, leave */
262  if (ast_tvdiff_ms(ast_tvnow(), lastdigittime) > ((*received > 0) ? sdtimeout : fdtimeout)) {
263  ast_verb(4, "AlarmReceiver: DTMF Digit Timeout on %s\n", ast_channel_name(chan));
264  ast_debug(1, "AlarmReceiver: DTMF timeout on chan %s\n", ast_channel_name(chan));
265  rtn = 1;
266  break;
267  }
268 
269  if ((r = ast_waitfor(chan, -1)) < 0) {
270  ast_debug(1, "Waitfor returned %d\n", r);
271  continue;
272  }
273 
274  if ((f = ast_read(chan)) == NULL) {
275  rtn = -1;
276  break;
277  }
278 
279  /* If they hung up, leave */
280  if ((f->frametype == AST_FRAME_CONTROL)
281  && (f->subclass.integer == AST_CONTROL_HANGUP)) {
282  if (f->data.uint32) {
283  ast_channel_hangupcause_set(chan, f->data.uint32);
284  }
285  ast_frfree(f);
286  rtn = -1;
287  break;
288  }
289 
290  /* If not DTMF, just do it again */
291  if (f->frametype != AST_FRAME_DTMF) {
292  ast_frfree(f);
293  continue;
294  }
295 
296  /* Save digit */
297  digit_string[(*received)++] = f->subclass.integer;
298  ast_frfree(f);
299 
300  lastdigittime = ast_tvnow();
301  }
302 
303  /* Null terminate the end of the digit_string */
304  digit_string[*received] = '\0';
305 
306  return rtn;
307 }
struct ast_frame * ast_read(struct ast_channel *chan)
Reads a frame.
Definition: channel.c:4257
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:159
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:107
struct ast_frame_subclass subclass
#define ast_debug(level,...)
Log a DEBUG message.
union ast_frame::@224 data
int ast_waitfor(struct ast_channel *chan, int ms)
Wait for input on a channel.
Definition: channel.c:3162
Data structure associated with a single frame of data.
enum ast_frame_type frametype
static int send_tone_burst ( struct ast_channel chan,
const char *  tone_freq,
int  tone_duration,
int  delay 
)
static

Send a single tone burst for a specified duration and frequency.

Since
11.0
Parameters
chanAsterisk Channel
tone_freqFrequency of the tone to send
tone_durationTone duration in ms
delayDelay before sending the tone
Return values
0success
-1failure

Definition at line 495 of file app_alarmreceiver.c.

References ast_playtones_start(), ast_playtones_stop(), and ast_safe_sleep().

Referenced by receive_ademco_event().

496 {
497  if (delay && ast_safe_sleep(chan, delay)) {
498  return -1;
499  }
500 
501  if (ast_playtones_start(chan, toneloudness, tone_freq, 0)) {
502  return -1;
503  }
504 
505  if (ast_safe_sleep(chan, tone_duration)) {
506  return -1;
507  }
508 
509  ast_playtones_stop(chan);
510  return 0;
511 }
int ast_safe_sleep(struct ast_channel *chan, int ms)
Wait for a specified amount of time, looking for hangups.
Definition: channel.c:1574
void ast_playtones_stop(struct ast_channel *chan)
Stop playing tones on a channel.
Definition: indications.c:393
int ast_playtones_start(struct ast_channel *chan, int vol, const char *tonelist, int interruptible)
Start playing a list of tones on a channel.
Definition: indications.c:302
static int unload_module ( void  )
static

Unregister Alarm Receiver App.

Return values
0success
-1failure

Definition at line 958 of file app_alarmreceiver.c.

References ast_unregister_application().

959 {
960  return ast_unregister_application(app);
961 }
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392
static int write_event ( FILE *  logfile,
event_node_t event 
)
static

Log a single event.

Parameters
logfileLog File Pointer
eventEvent Structure
Return values
0success
-1failure

Definition at line 381 of file app_alarmreceiver.c.

Referenced by log_events().

382 {
383  if (fprintf(logfile, "%s%s\n", no_group_meta ? "event=" : "", event->data) < 0) {
384  return -1;
385  }
386 
387  return 0;
388 }
static int write_metadata ( FILE *  logfile,
char *  signalling_type,
struct ast_channel chan,
int  no_checksum 
)
static

Write metadata to log file.

Parameters
logfileLog File Pointer
signalling_typeSignaling Type
chanAsterisk Channel
no_checksumExpecting messages without checksum
Return values
0success
-1failure

Definition at line 320 of file app_alarmreceiver.c.

References ast_copy_string(), ast_debug, ast_localtime(), ast_shrink_phone_number(), ast_strftime(), ast_tvnow(), and S_COR.

Referenced by log_events().

321 {
322  struct timeval t;
323  struct ast_tm now;
324  char *cl;
325  char *cn;
326  char workstring[80];
327  char timestamp[80];
328 
329  /* Extract the caller ID location */
330  ast_copy_string(workstring,
331  S_COR(ast_channel_caller(chan)->id.number.valid,
332  ast_channel_caller(chan)->id.number.str, ""), sizeof(workstring));
333  ast_shrink_phone_number(workstring);
334  if (ast_strlen_zero(workstring)) {
335  cl = "<unknown>";
336  } else {
337  cl = workstring;
338  }
339  cn = S_COR(ast_channel_caller(chan)->id.name.valid,
340  ast_channel_caller(chan)->id.name.str, "<unknown>");
341 
342  /* Get the current time */
343  t = ast_tvnow();
344  ast_localtime(&t, &now, NULL);
345 
346  /* Format the time */
347  ast_strftime(timestamp, sizeof(timestamp), time_stamp_format, &now);
348 
349  if (no_group_meta && fprintf(logfile, "PROTOCOL=%s\n"
350  "CHECKSUM=%s\n"
351  "CALLINGFROM=%s\n"
352  "CALLERNAME=%s\n"
353  "TIMESTAMP=%s\n\n",
354  signalling_type, (!no_checksum) ? "yes" : "no", cl, cn, timestamp) > -1) {
355  return 0;
356  } else if (fprintf(logfile, "\n\n[metadata]\n\n"
357  "PROTOCOL=%s\n"
358  "CHECKSUM=%s\n"
359  "CALLINGFROM=%s\n"
360  "CALLERNAME=%s\n"
361  "TIMESTAMP=%s\n\n"
362  "[events]\n\n",
363  signalling_type, (!no_checksum) ? "yes" : "no", cl, cn, timestamp) > -1) {
364  return 0;
365  }
366 
367  ast_verb(3, "AlarmReceiver: can't write metadata\n");
368  ast_debug(1, "AlarmReceiver: can't write metadata\n");
369  return -1;
370 }
struct ast_tm * ast_localtime(const struct timeval *timep, struct ast_tm *p_tm, const char *zone)
Timezone-independent version of localtime_r(3).
Definition: localtime.c:1739
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:159
Number structure.
Definition: app_followme.c:154
#define S_COR(a, b, c)
returns the equivalent of logic or for strings, with an additional boolean check: second one if not e...
Definition: strings.h:87
#define ast_debug(level,...)
Log a DEBUG message.
int ast_strftime(char *buf, size_t len, const char *format, const struct ast_tm *tm)
Special version of strftime(3) that handles fractions of a second. Takes the same arguments as strfti...
Definition: localtime.c:2524
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
void ast_shrink_phone_number(char *n)
Shrink a phone number in place to just digits (more accurately it just removes ()'s, .'s, and -'s...
Definition: callerid.c:1101

Variable Documentation

struct { ... } digits_mapping[]
Initial value:
= { {'0', 10}, {'1', 1} , {'2', 2}, {'3', 3}, {'4', 4}, {'5', 5},
{'6', 6}, {'7', 7}, {'8', 8}, {'9', 9}, {'*', 11}, {'#', 12},
{'A', 13}, {'B', 14}, {'C', 15} }