From b63c43b1d0cfbe29b7b241e9af2d1efa5c1fdb3a Mon Sep 17 00:00:00 2001
From: Pedro Kiefer
Date: Fri, 20 Jul 2012 13:13:59 -0300
Subject: [PATCH 5/6] Remove checksum check out of the receive Contact Id main
loop
---
apps/app_alarmreceiver.c | 71 ++++++++++++++++++++++++++--------------------
1 file changed, 40 insertions(+), 31 deletions(-)
diff --git a/apps/app_alarmreceiver.c b/apps/app_alarmreceiver.c
index 8c6e329..fe0ef0a 100644
--- a/apps/app_alarmreceiver.c
+++ b/apps/app_alarmreceiver.c
@@ -385,23 +385,57 @@ static int log_events(struct ast_channel *chan, char *signalling_type, event_no
}
/*
+ * 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 */
+ checksum = checksum % 15;
+ if (checksum) {
+ database_increment("checksum-errors");
+ ast_verb(2, "AlarmReceiver: Nonzero checksum\n");
+ ast_debug(1, "AlarmReceiver: Nonzero checksum\n");
+ return -1;
+ }
+
+ return 0;
+}
+
+/*
* 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");
@@ -454,33 +488,8 @@ 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) {
- database_increment("checksum-errors");
- ast_verb(2, "AlarmReceiver: Nonzero checksum\n");
- ast_debug(1, "AlarmReceiver: Nonzero checksum\n");
+ if (ademco_verify_checksum(event))
continue;
- }
/* Check the message type for correctness */
if (strncmp(event + 4, ADEMCO_MSG_TYPE_1, 2)) {
--
1.7.9.5