[svn-commits] dvossel: branch group/aoc r251403 - /team/group/aoc/main/aoc.c

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


Author: dvossel
Date: Tue Mar  9 09:46:07 2010
New Revision: 251403

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=251403
Log:
fixes some logical errors and begins the decode routine

Modified:
    team/group/aoc/main/aoc.c

Modified: team/group/aoc/main/aoc.c
URL: http://svnview.digium.com/svn/asterisk/team/group/aoc/main/aoc.c?view=diff&rev=251403&r1=251402&r2=251403
==============================================================================
--- team/group/aoc/main/aoc.c (original)
+++ team/group/aoc/main/aoc.c Tue Mar  9 09:46:07 2010
@@ -46,9 +46,9 @@
 #define AOC_ENCODED_REQUEST_D       (1 << 3)
 #define AOC_ENCODED_REQUEST_E       (1 << 4)
 
-#define AOC_ENCODED_CHARGE_NA       (0 << 5) | (0 << 6)
-#define AOC_ENCODED_CHARGE_FREE     (1 << 5) | (0 << 6)
-#define AOC_ENCODED_CHARGE_CURRENCY (2 << 5) | (0 << 5)
+#define AOC_ENCODED_CHARGE_NA       ((0 << 5) | (0 << 6))
+#define AOC_ENCODED_CHARGE_FREE     ((1 << 5) | (0 << 6))
+#define AOC_ENCODED_CHARGE_CURRENCY ((2 << 5) | (0 << 5))
 #define AOC_ENCODED_CHARGE_UNIT     (3 << 5)
 
 #define AOC_ENCODED_CHARGE_SUBTOTAL (1 << 7)
@@ -113,7 +113,7 @@
 	uint8_t id;
 };
 
-#define AOC_IE_CHARGING_ASSOCIATION 5
+#define AOC_IE_CHARGING_ASSOCIATION 4
 struct aoc_ie_charging_association {
 	int32_t charge_id;
 	char charge_number[32];
@@ -156,8 +156,43 @@
 		return NULL;
 	}
 
-//todohere actually decode the encoded msg 
-
+	/* decode flags */
+	if (encoded->flags & AOC_ENCODED_TYPE_E) {
+		decoded->msg_type = AOC_E;
+	} else if (encoded->flags & AOC_ENCODED_TYPE_D) {
+		decoded->msg_type = AOC_D;
+	} else {
+		decoded->msg_type = AOC_REQUEST;
+	}
+
+	if (decoded->msg_type == AOC_REQUEST) {
+		if (encoded->flags & AOC_ENCODED_REQUEST_S) {
+			decoded->request_flag |= AOC_ENCODED_REQUEST_S;
+		}
+		if (encoded->flags & AOC_ENCODED_REQUEST_D) {
+			decoded->request_flag |= AOC_ENCODED_REQUEST_D;
+		}
+		if (encoded->flags & AOC_ENCODED_REQUEST_E) {
+			decoded->request_flag |= AOC_ENCODED_REQUEST_E;
+		}
+	} else {
+		if (encoded->flags & AOC_ENCODED_CHARGE_UNIT) {
+			decoded->charge_type = AOC_CHARGE_UNIT;
+		} else 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 {
+			decoded->charge_type = AOC_CHARGE_NA;
+		}
+
+		if (encoded->flags & AOC_ENCODED_CHARGE_SUBTOTAL) {
+			decoded->total_type = AOC_SUBTOTAL;
+		}
+	}
+
+	/* decode information elements */
+	/* TODO XXX */
 	return decoded;
 }
 
@@ -175,27 +210,82 @@
 		ast_log(LOG_WARNING, "Failure to append AOC information element, out of space \n");
 		return -1;
 	}
-	ied->buf[ied->pos++] = htons(ie_id);
-	ied->buf[ied->pos++] = htons(datalen);
+	ied->buf[ied->pos++] = ie_id;
+	ied->buf[ied->pos++] = datalen;
 	memcpy(ied->buf + ied->pos, data, datalen);
 	ied->pos += datalen;
 	return 0;
 }
 
+static void aoc_create_ie_data(struct ast_aoc_decoded *decoded, struct aoc_ie_data *ied)
+{
+
+	ied->pos = 0;
+
+	if (decoded->currency_amount) {
+		struct aoc_ie_currency ie = {
+			.amount = htonl(decoded->currency_amount),
+			.multiplier = decoded->multiplier, /* only one byte */
+		};
+		if (!ast_strlen_zero(decoded->currency_name)) {
+			ast_copy_string(ie.name, decoded->currency_name, sizeof(ie.name));
+		}
+
+		append_ie(ied, AOC_IE_CURRENCY, (const void *) &ie, sizeof(ie));
+	}
+
+	if (decoded->unit_count) {
+		struct aoc_ie_unit ie = { 0 };
+		int i;
+		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));
+		}
+	}
+
+	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));
+	}
+
+	if (decoded->charging_association_id ||
+		!ast_strlen_zero(decoded->charging_association_number)) {
+		struct aoc_ie_charging_association ie;
+		ie.charge_id = htonl(decoded->charging_association_id);
+		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));
+	}
+}
+
 struct ast_aoc_encoded *ast_aoc_encode(struct ast_aoc_decoded *decoded)
 {
-	struct ast_aoc_encoded *encoded = ast_calloc(1, sizeof(struct ast_aoc_encoded));
-	struct aoc_ie_data ied = { .pos = 0 };
-
-	if (!encoded) {
+	struct ast_aoc_encoded *encoded = NULL;
+	struct aoc_ie_data ied;
+
+	/* create information element buffer before allocating the payload,
+	 * by doing this the exact size of the payload + the id data can be
+	 * allocated all at once. */
+	aoc_create_ie_data(decoded, &ied);
+
+	if (!(encoded = ast_calloc(1, (sizeof(struct ast_aoc_encoded) + ied.pos)))) {
 		ast_log(LOG_WARNING, "Failed to create ast_aoc_encoded object during decode routine. \n");
 		return NULL;
 	}
 
-	/* encode the message */
+	/* -- Set ie data buffer */
+	if (ied.pos) {
+		memcpy(ied.buf, encoded->data, ied.pos);
+		encoded->datalen = htons(ied.pos);
+	}
+
+	/* --- Set Version Number --- */
 	encoded->version = AOC_ENCODE_VERSION;
 
-	/* set msg type */
+	/* --- Set Flags --- */
 	switch (decoded->msg_type) {
 	case AOC_D:
 		encoded->flags |= AOC_ENCODED_TYPE_D;
@@ -208,21 +298,17 @@
 		break;
 	}
 
-	/* set request type */
-	if (encoded->flags | AOC_ENCODED_TYPE_REQUEST) {
-		if (decoded->request_flag | AOC_REQUEST_S) {
+	if (encoded->flags & AOC_ENCODED_TYPE_REQUEST) {
+		if (decoded->request_flag & AOC_REQUEST_S) {
 			encoded->flags |= AOC_ENCODED_REQUEST_S;
 		}
-		if (decoded->request_flag | AOC_REQUEST_D) {
+		if (decoded->request_flag & AOC_REQUEST_D) {
 			encoded->flags |= AOC_ENCODED_REQUEST_D;
 		}
-		if (decoded->request_flag | AOC_REQUEST_E) {
+		if (decoded->request_flag & AOC_REQUEST_E) {
 			encoded->flags |= AOC_ENCODED_REQUEST_E;
 		}
-	}
-
-	/* set charge type */
-	if (!(encoded->flags & AOC_ENCODED_TYPE_REQUEST)) {
+	} else {
 		switch (decoded->charge_type) {
 		case AOC_CHARGE_UNIT:
 			encoded->flags |= AOC_ENCODED_CHARGE_UNIT;
@@ -237,53 +323,12 @@
 			break;
 		}
 
-	}
-
-	/* set total type */
-	if (decoded->total_type == AOC_SUBTOTAL) {
-		encoded->flags |= AOC_ENCODED_CHARGE_SUBTOTAL;
-	}
-
-	/* Append Information Elements
-	 * NOTE, everything must be in NETWORK BYTE ORDER */
-
-	if (decoded->currency_amount) {
-		struct aoc_ie_currency ie = { 0 };
-
-		ie.amount = htonl(decoded->currency_amount);
-		ie.multiplier = htons(decoded->multiplier);
-		if (!ast_strlen_zero(decoded->currency_name)) {
-			ast_copy_string(ie.name, decoded->currency_name, sizeof(ie.name));
-		}
-
-		append_ie(&ied, AOC_IE_CURRENCY, (const void *) &ie, sizeof(ie));
-	}
-
-	if (decoded->unit_count) {
-		struct aoc_ie_unit ie = { 0 };
-		int i;
-		for (i = 0; i < decoded->unit_count; i++) {
-			ie.amount = htonl(decoded->unit_list[i].amount);
-			ie.type = htons(decoded->unit_list[i].type);
-			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));
-	}
-
-	if (decoded->charging_association_id ||
-		!ast_strlen_zero(decoded->charging_association_number)) {
-		struct aoc_ie_charging_association ie;
-		ie.charge_id = htonl(decoded->charging_association_id);
-		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));
-	}
+		if (decoded->total_type == AOC_SUBTOTAL) {
+			encoded->flags |= AOC_ENCODED_CHARGE_SUBTOTAL;
+		}
+	}
+
+
 
 	return encoded;
 }




More information about the svn-commits mailing list