From 45173979db1833a86d32a95e92b2bed4450f2fe2 Mon Sep 17 00:00:00 2001 From: Pedro Kiefer Date: Mon, 13 Aug 2012 14:08:08 -0300 Subject: [PATCH 5/7] Remove checksum function out of the receive ContactId loop --- apps/app_alarmreceiver.c | 63 ++++++++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 27 deletions(-) diff --git a/apps/app_alarmreceiver.c b/apps/app_alarmreceiver.c index 9d86f22..0912276 100644 --- a/apps/app_alarmreceiver.c +++ b/apps/app_alarmreceiver.c @@ -389,23 +389,53 @@ static int log_events(struct ast_channel *chan, char *signalling_type, event_nod } /* + * Ademco checksum + */ +static int ademco_verify_checksum(char *event) +{ + static char digit_map[15] = "0123456789*#ABC"; + static unsigned char digit_weights[15] = { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15 }; + + int checksum = 0; + int i, j; + + for (j = 0, checksum = 0; j < 16; j++) { + for (i = 0; i < sizeof(digit_map); i++) { + if (digit_map[i] == event[j]) + break; + } + + if (i == 16) + break; + checksum += digit_weights[i]; + } + + if (i == 16) { + ast_verb(2, "AlarmReceiver: Bad DTMF character %c, trying again\n", event[j]); + return -1; + } + + /* Checksum is mod(15) of the total */ + if ((checksum % 15) == 0) { + return 0; + } + + return -1; +} + +/* * This function implements the logic to receive the Ademco contact ID format. * * The function will return 0 when the caller hangs up, else a -1 if there was a problem. */ static int receive_ademco_contact_id(struct ast_channel *chan, const void *data, int fdto, int sdto, int tldn, event_node_t **ehead) { - int i, j; int res = 0; - int checksum; char event[17]; event_node_t *enew, *elp; int got_some_digits = 0; int events_received = 0; int ack_retries = 0; - - static char digit_map[15] = "0123456789*#ABC"; - static unsigned char digit_weights[15] = {10,1,2,3,4,5,6,7,8,9,11,12,13,14,15}; database_increment("calls-received"); @@ -458,28 +488,7 @@ static int receive_ademco_contact_id(struct ast_channel *chan, const void *data, ast_debug(1, "AlarmReceiver: Received event: %s\n", event); /* Calculate checksum */ - - for (j = 0, checksum = 0; j < 16; j++) { - for (i = 0; i < sizeof(digit_map); i++) { - if (digit_map[i] == event[j]) - break; - } - - if (i == 16) - break; - - checksum += digit_weights[i]; - } - if (i == 16) { - ast_verb(2, "AlarmReceiver: Bad DTMF character %c, trying again\n", event[j]); - continue; /* Bad character */ - } - - /* Checksum is mod(15) of the total */ - - checksum = checksum % 15; - - if (checksum) { + if (ademco_verify_checksum(event)) { database_increment("checksum-errors"); ast_verb(2, "AlarmReceiver: Nonzero checksum\n"); ast_debug(1, "AlarmReceiver: Nonzero checksum\n"); -- 1.7.9.6 (Apple Git-31.1)