[svn-commits] dvossel: branch group/aoc r251408 - in /team/group/aoc: include/asterisk/ mai...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Tue Mar 9 11:23:46 CST 2010


Author: dvossel
Date: Tue Mar  9 11:23:42 2010
New Revision: 251408

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=251408
Log:
aoc decode routine and aoc unit test update

Modified:
    team/group/aoc/include/asterisk/aoc.h
    team/group/aoc/main/aoc.c
    team/group/aoc/tests/test_aoc.c

Modified: team/group/aoc/include/asterisk/aoc.h
URL: http://svnview.digium.com/svn/asterisk/team/group/aoc/include/asterisk/aoc.h?view=diff&rev=251408&r1=251407&r2=251408
==============================================================================
--- team/group/aoc/include/asterisk/aoc.h (original)
+++ team/group/aoc/include/asterisk/aoc.h Tue Mar  9 11:23:42 2010
@@ -224,4 +224,10 @@
 
 /* \brief get the charging association number for AOC-E messages*/
 const char *ast_aoc_get_association_number(struct ast_aoc_decoded *decoded);
+
+/* \brief test aoc encode decode routines.
+ * \note  This function verifies that a decoded message matches itself after
+ *        the encode decode routine.
+ */
+int ast_aoc_test_encode_decode_match(struct ast_aoc_decoded *decoded);
 #endif

Modified: team/group/aoc/main/aoc.c
URL: http://svnview.digium.com/svn/asterisk/team/group/aoc/main/aoc.c?view=diff&rev=251408&r1=251407&r2=251408
==============================================================================
--- team/group/aoc/main/aoc.c (original)
+++ team/group/aoc/main/aoc.c Tue Mar  9 11:23:42 2010
@@ -37,7 +37,7 @@
 
 
 /* Encoded Payload Flags */
-#define AOC_ENCODED_TYPE_REQUEST    (0 << 0)
+#define AOC_ENCODED_TYPE_REQUEST    ((0 << 0) | (0 << 1))
 #define AOC_ENCODED_TYPE_D          (1 << 0)
 #define AOC_ENCODED_TYPE_E          (2 << 0)
 /* #define AOC_PAYLOAD_TYPE_S (3 << 0)  This is not yet supported, but reserve this for future use */
@@ -145,6 +145,48 @@
 {
 	ast_free(decoded);
 	return NULL;
+}
+
+static int aoc_parse_ie(struct ast_aoc_decoded *decoded, unsigned char *data, unsigned int datalen)
+{
+/* TODO XXX parse the ie */
+	int ie;
+	int len;
+
+	while (datalen >= 2) {
+		ie = data[0];
+		len = data[1];
+		if (len > datalen -2) {
+			ast_log(LOG_ERROR, "AOC information element length exceeds the total message size\n");
+			return -1;
+		}
+
+		switch(ie) {
+		case AOC_IE_CURRENCY:
+			if (len == sizeof(struct aoc_ie_currency)) {
+				struct aoc_ie_currency ie;
+
+				memcpy(&ie, data + 2, len);
+				decoded->currency_amount = ntohl(ie.amount);
+				decoded->multiplier = ie.multiplier; /* only one byte */
+			} else {
+				ast_log(LOG_WARNING, "Recieved invalid currency ie\n");
+			}
+		break;
+		case AOC_IE_UNIT:
+
+		case AOC_IE_BILLING:
+
+		case AOC_IE_CHARGING_ASSOCIATION:
+
+		default:
+			ast_log(LOG_WARNING, "Unknown AOC Information Element, ignoring.\n");
+		}
+
+		datalen -= (len + 2);
+		data += (len + 2);
+	}
+	return 0;
 }
 
 struct ast_aoc_decoded *ast_aoc_decode(struct ast_aoc_encoded *encoded)
@@ -167,21 +209,21 @@
 
 	if (decoded->msg_type == AOC_REQUEST) {
 		if (encoded->flags & AOC_ENCODED_REQUEST_S) {
-			decoded->request_flag |= AOC_ENCODED_REQUEST_S;
+			decoded->request_flag |= AOC_REQUEST_S;
 		}
 		if (encoded->flags & AOC_ENCODED_REQUEST_D) {
-			decoded->request_flag |= AOC_ENCODED_REQUEST_D;
+			decoded->request_flag |= AOC_REQUEST_D;
 		}
 		if (encoded->flags & AOC_ENCODED_REQUEST_E) {
-			decoded->request_flag |= AOC_ENCODED_REQUEST_E;
+			decoded->request_flag |= AOC_REQUEST_E;
 		}
 	} else {
-		if (encoded->flags & AOC_ENCODED_CHARGE_UNIT) {
-			decoded->charge_type = AOC_CHARGE_UNIT;
-		} else if (encoded->flags & AOC_ENCODED_CHARGE_CURRENCY) {
+		if (encoded->flags & AOC_ENCODED_CHARGE_CURRENCY) {
 			decoded->charge_type = AOC_CHARGE_CURRENCY;
 		} else if (encoded->flags & AOC_ENCODED_CHARGE_FREE) {
 			decoded->charge_type = AOC_CHARGE_FREE;
+		} else if (encoded->flags & AOC_ENCODED_CHARGE_UNIT) {
+			decoded->charge_type = AOC_CHARGE_UNIT;
 		} else {
 			decoded->charge_type = AOC_CHARGE_NA;
 		}
@@ -192,7 +234,8 @@
 	}
 
 	/* decode information elements */
-	/* TODO XXX */
+	/* TODO, make the lengths actually match up */
+	aoc_parse_ie(decoded, encoded->data, ntohs(encoded->datalen));
 	return decoded;
 }
 
@@ -204,7 +247,7 @@
 /* \brief append an AOC information element
  * \note data is expecte to already be in network byte order at this point
  */
-static int append_ie(struct aoc_ie_data *ied, unsigned short ie_id, const void *data, unsigned short datalen)
+static int aoc_append_ie(struct aoc_ie_data *ied, unsigned short ie_id, const void *data, unsigned short datalen)
 {
 	if (datalen > ((int)sizeof(ied->buf) - ied->pos)) {
 		ast_log(LOG_WARNING, "Failure to append AOC information element, out of space \n");
@@ -231,7 +274,7 @@
 			ast_copy_string(ie.name, decoded->currency_name, sizeof(ie.name));
 		}
 
-		append_ie(ied, AOC_IE_CURRENCY, (const void *) &ie, sizeof(ie));
+		aoc_append_ie(ied, AOC_IE_CURRENCY, (const void *) &ie, sizeof(ie));
 	}
 
 	if (decoded->unit_count) {
@@ -240,14 +283,14 @@
 		for (i = 0; i < decoded->unit_count; i++) {
 			ie.amount = htonl(decoded->unit_list[i].amount);
 			ie.type = decoded->unit_list[i].type; /* only one byte */
-			append_ie(ied, AOC_IE_UNIT, (const void *) &ie, sizeof(ie));
+			aoc_append_ie(ied, AOC_IE_UNIT, (const void *) &ie, sizeof(ie));
 		}
 	}
 
 	if (decoded->billing_id) {
 		struct aoc_ie_billing ie;
 		ie.id = htons(decoded->billing_id);
-		append_ie(ied, AOC_IE_BILLING, (const void *) &ie, sizeof(ie));
+		aoc_append_ie(ied, AOC_IE_BILLING, (const void *) &ie, sizeof(ie));
 	}
 
 	if (decoded->charging_association_id ||
@@ -257,7 +300,7 @@
 		if (!ast_strlen_zero(decoded->charging_association_number)) {
 			ast_copy_string(ie.charge_number, decoded->charging_association_number, sizeof(ie.charge_number));
 		}
-		append_ie(ied, AOC_IE_CHARGING_ASSOCIATION, (const void *) &ie, sizeof(ie));
+		aoc_append_ie(ied, AOC_IE_CHARGING_ASSOCIATION, (const void *) &ie, sizeof(ie));
 	}
 }
 
@@ -278,27 +321,27 @@
 
 	/* -- Set ie data buffer */
 	if (ied.pos) {
-		memcpy(ied.buf, encoded->data, ied.pos);
+		/* this is safe because encoded was allocated to fit this perfectly */
+		memcpy(encoded->data, ied.buf, ied.pos);
 		encoded->datalen = htons(ied.pos);
 	}
-
-	/* --- Set Version Number --- */
-	encoded->version = AOC_ENCODE_VERSION;
 
 	/* --- Set Flags --- */
 	switch (decoded->msg_type) {
 	case AOC_D:
-		encoded->flags |= AOC_ENCODED_TYPE_D;
+		encoded->flags = AOC_ENCODED_TYPE_D;
 		break;
 	case AOC_E:
-		encoded->flags |= AOC_ENCODED_TYPE_E;
+		encoded->flags = AOC_ENCODED_TYPE_E;
 		break;
 	case AOC_REQUEST:
+		encoded->flags = AOC_ENCODED_TYPE_REQUEST; /* request turns off this bit */
 	default:
 		break;
 	}
 
-	if (encoded->flags & AOC_ENCODED_TYPE_REQUEST) {
+	/* if it is type request, set the types requested, else set charge type */
+	if (decoded->msg_type == AOC_REQUEST) {
 		if (decoded->request_flag & AOC_REQUEST_S) {
 			encoded->flags |= AOC_ENCODED_REQUEST_S;
 		}
@@ -328,6 +371,8 @@
 		}
 	}
 
+	/* --- Set Version Number --- */
+	encoded->version = AOC_ENCODE_VERSION;
 
 
 	return encoded;
@@ -462,5 +507,29 @@
 const char *ast_aoc_get_association_number(struct ast_aoc_decoded *decoded)
 {
 	return decoded->charging_association_number;
-
-}
+}
+
+int ast_aoc_test_encode_decode_match(struct ast_aoc_decoded *decoded)
+{
+	struct ast_aoc_decoded *new = NULL;
+	struct ast_aoc_encoded *encoded = NULL;
+	int res = 0;
+	if (!(encoded = ast_aoc_encode(decoded))) {
+		return -1;
+	}
+
+	if (!(new = ast_aoc_decode(encoded))) {
+		ast_free(encoded);
+		return -1;
+	}
+
+	if (memcmp(new, decoded, sizeof(new))) {
+		res = -1;
+	}
+
+	ast_free(new);
+	ast_free(encoded);
+	return res;
+}
+
+

Modified: team/group/aoc/tests/test_aoc.c
URL: http://svnview.digium.com/svn/asterisk/team/group/aoc/tests/test_aoc.c?view=diff&rev=251408&r1=251407&r2=251408
==============================================================================
--- team/group/aoc/tests/test_aoc.c (original)
+++ team/group/aoc/tests/test_aoc.c Tue Mar  9 11:23:42 2010
@@ -91,8 +91,12 @@
 		goto cleanup_aoc_test;
 	}
 
-	//TODO add encode and decode test here
-
+	/* Encode the message */
+	if (ast_aoc_test_encode_decode_match(decoded)) {
+		ast_test_status_update(test, "Test1: encode decode routine did not match expected results \n");
+		res = AST_TEST_FAIL;
+		goto cleanup_aoc_test;
+	}
 	/* cleanup decoded msg */
 	decoded = ast_aoc_destroy_decoded(decoded);
 




More information about the svn-commits mailing list