[asterisk-commits] dvossel: branch group/aoc r251398 - in /team/group/aoc: include/asterisk/ main/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Mar 8 18:15:54 CST 2010


Author: dvossel
Date: Mon Mar  8 18:15:50 2010
New Revision: 251398

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=251398
Log:
addition of generic aoc encode routine

Modified:
    team/group/aoc/include/asterisk/aoc.h
    team/group/aoc/main/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=251398&r1=251397&r2=251398
==============================================================================
--- team/group/aoc/include/asterisk/aoc.h (original)
+++ team/group/aoc/include/asterisk/aoc.h Mon Mar  8 18:15:50 2010
@@ -49,10 +49,10 @@
 };
 
 enum ast_aoc_type {
-	/* AOC_S */  /* Reserved for AOC-S support */
+	AOC_REQUEST = 0,
 	AOC_D,
 	AOC_E,
-	AOC_REQUEST,
+	/* AOC_S */  /* Reserved for AOC-S support */
 };
 
 enum ast_aoc_charge_type {

Modified: team/group/aoc/main/aoc.c
URL: http://svnview.digium.com/svn/asterisk/team/group/aoc/main/aoc.c?view=diff&rev=251398&r1=251397&r2=251398
==============================================================================
--- team/group/aoc/main/aoc.c (original)
+++ team/group/aoc/main/aoc.c Mon Mar  8 18:15:50 2010
@@ -24,11 +24,14 @@
  */
 
 #include "asterisk.h"
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$");
+
 #include "asterisk/aoc.h"
 #include "asterisk/utils.h"
 #include "asterisk/strings.h"
-
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$");
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
 
 #include "asterisk/_private.h"
 
@@ -50,6 +53,8 @@
 
 #define AOC_ENCODED_CHARGE_SUBTOTAL (1 << 7)
 #define AOC_ENCODED_CHARGE_TOTAL    (0 << 7)
+
+#define AOC_ENCODE_VERSION 1
 
 /* AOC Payload Header. Holds all the encoded AOC data to pass on the wire */
 struct ast_aoc_encoded {
@@ -80,13 +85,13 @@
 
 	/* Charging Association information */
 	int charging_association_id;
-	char charging_association_number[64];
+	char charging_association_number[32];
 };
 
 struct aoc_pl_ie_hdr {
 	uint8_t ie_id;
 	uint8_t datalen;
-	unsigned char data[0];
+	char data[0];
 };
 
 /* AOC Payload Information Elements */
@@ -103,22 +108,15 @@
 	uint8_t  type;
 };
 
-#define AOCE_IE_BILLING 3
-struct aoce_ie_billing {
-	uint8_t aoce_billing_id;
-};
-
-#define AOCD_IE_BILLING 4
-struct aocd_ie_billing {
-	uint8_t aocd_billing_id;
+#define AOC_IE_BILLING 3
+struct aoc_ie_billing {
+	uint8_t id;
 };
 
 #define AOC_IE_CHARGING_ASSOCIATION 5
 struct aoc_ie_charging_association {
-	int16_t charge_id;
-	uint8_t datalen;
-	unsigned char data[0];
-
+	int32_t charge_id;
+	char charge_number[32];
 };
 
 struct ast_aoc_decoded *ast_aoc_create(const enum ast_aoc_type msg_type,
@@ -129,7 +127,10 @@
 
 	if (!(decoded = ast_calloc(1, sizeof(struct ast_aoc_decoded)))) {
 		ast_log(LOG_WARNING, "Failed to create ast_aoc_decoded object \n");
-	}
+		return NULL;
+	}
+
+	decoded->msg_type = msg_type;
 
 	if (msg_type == AOC_REQUEST) {
 		decoded->request_flag = requests;
@@ -160,16 +161,129 @@
 	return decoded;
 }
 
+struct aoc_ie_data {
+	unsigned char buf[1024];
+	int pos;
+};
+
+/* \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)
+{
+	if (datalen > ((int)sizeof(ied->buf) - ied->pos)) {
+		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);
+	memcpy(ied->buf + ied->pos, data, datalen);
+	ied->pos += datalen;
+	return 0;
+}
+
 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) {
 		ast_log(LOG_WARNING, "Failed to create ast_aoc_encoded object during decode routine. \n");
 		return NULL;
 	}
 
-//todohere actually encode a msg 
+	/* encode the message */
+	encoded->version = AOC_ENCODE_VERSION;
+
+	/* set msg type */
+	switch (decoded->msg_type) {
+	case AOC_D:
+		encoded->flags |= AOC_ENCODED_TYPE_D;
+		break;
+	case AOC_E:
+		encoded->flags |= AOC_ENCODED_TYPE_E;
+		break;
+	case AOC_REQUEST:
+	default:
+		break;
+	}
+
+	/* set request type */
+	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) {
+			encoded->flags |= AOC_ENCODED_REQUEST_D;
+		}
+		if (decoded->request_flag | AOC_REQUEST_E) {
+			encoded->flags |= AOC_ENCODED_REQUEST_E;
+		}
+	}
+
+	/* set charge type */
+	if (!(encoded->flags & AOC_ENCODED_TYPE_REQUEST)) {
+		switch (decoded->charge_type) {
+		case AOC_CHARGE_UNIT:
+			encoded->flags |= AOC_ENCODED_CHARGE_UNIT;
+			break;
+		case AOC_CHARGE_CURRENCY:
+			encoded->flags |= AOC_ENCODED_CHARGE_CURRENCY;
+			break;
+		case AOC_CHARGE_FREE:
+			encoded->flags |= AOC_ENCODED_CHARGE_FREE;
+		case AOC_CHARGE_NA:
+		default:
+			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));
+	}
 
 	return encoded;
 }




More information about the asterisk-commits mailing list