[asterisk-commits] kharwell: branch kharwell/stasis_aoc_event r392212 - /team/kharwell/stasis_ao...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Jun 18 18:23:49 CDT 2013


Author: kharwell
Date: Tue Jun 18 18:23:48 2013
New Revision: 392212

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=392212
Log:
some refactor work done, broken won't build

Modified:
    team/kharwell/stasis_aoc_event/main/aoc.c

Modified: team/kharwell/stasis_aoc_event/main/aoc.c
URL: http://svnview.digium.com/svn/asterisk/team/kharwell/stasis_aoc_event/main/aoc.c?view=diff&rev=392212&r1=392211&r2=392212
==============================================================================
--- team/kharwell/stasis_aoc_event/main/aoc.c (original)
+++ team/kharwell/stasis_aoc_event/main/aoc.c Tue Jun 18 18:23:48 2013
@@ -36,6 +36,8 @@
 #include "asterisk/_private.h"
 #include "asterisk/cli.h"
 #include "asterisk/manager.h"
+#include "asterisk/stasis_channels.h"
+#include "asterisk/stasis_message_router.h"
 
 /* Encoded Payload Flags */
 #define AST_AOC_ENCODED_TYPE_REQUEST    (0 << 0)
@@ -1261,12 +1263,12 @@
  *
  * \return Nothing
  */
-static void aoc_time_str(struct ast_str **msg, const char *prefix, const char *name, unsigned long time, enum ast_aoc_time_scale scale)
-{
-	ast_str_append(msg, 0, "%s/%s/Length: %lu\r\n", prefix, name, time);
-	ast_str_append(msg, 0, "%s/%s/Scale: %s\r\n", prefix, name,
-		aoc_scale_str(scale));
-}
+/* static void aoc_time_str(struct ast_str **msg, const char *prefix, const char *name, unsigned long time, enum ast_aoc_time_scale scale) */
+/* { */
+/* 	ast_str_append(msg, 0, "%s/%s/Length: %lu\r\n", prefix, name, time); */
+/* 	ast_str_append(msg, 0, "%s/%s/Scale: %s\r\n", prefix, name, */
+/* 		aoc_scale_str(scale)); */
+/* } */
 
 /*!
  * \internal
@@ -1280,14 +1282,14 @@
  *
  * \return Nothing
  */
-static void aoc_amount_str(struct ast_str **msg, const char *prefix, unsigned int amount, enum ast_aoc_currency_multiplier mult)
-{
-	static const char name[] = "Amount";
-
-	ast_str_append(msg, 0, "%s/%s/Cost: %u\r\n", prefix, name, amount);
-	ast_str_append(msg, 0, "%s/%s/Multiplier: %s\r\n", prefix, name,
-		aoc_multiplier_str(mult));
-}
+/* static void aoc_amount_str(struct ast_str **msg, const char *prefix, unsigned int amount, enum ast_aoc_currency_multiplier mult) */
+/* { */
+/* 	static const char name[] = "Amount"; */
+
+/* 	ast_str_append(msg, 0, "%s/%s/Cost: %u\r\n", prefix, name, amount); */
+/* 	ast_str_append(msg, 0, "%s/%s/Multiplier: %s\r\n", prefix, name, */
+/* 		aoc_multiplier_str(mult)); */
+/* } */
 
 static void aoc_request_event(const struct ast_aoc_decoded *decoded, struct ast_channel *chan, struct ast_str **msg)
 {
@@ -1314,72 +1316,378 @@
 	}
 }
 
-static void aoc_s_event(const struct ast_aoc_decoded *decoded, struct ast_channel *owner, struct ast_str **msg)
-{
-	const char *rate_str;
+/* static void aoc_s_event(const struct ast_aoc_decoded *decoded, struct ast_channel *owner, struct ast_str **msg) */
+/* { */
+/* 	const char *rate_str; */
+/* 	char prefix[32]; */
+/* 	int idx; */
+
+/* 	if (owner) { */
+/* 		ast_str_append(msg, 0, "Channel: %s\r\n", ast_channel_name(owner)); */
+/* 		ast_str_append(msg, 0, "UniqueID: %s\r\n", ast_channel_uniqueid(owner)); */
+/* 	} */
+
+/* 	ast_str_append(msg, 0, "NumberRates: %d\r\n", decoded->aoc_s_count); */
+/* 	for (idx = 0; idx < decoded->aoc_s_count; ++idx) { */
+/* 		snprintf(prefix, sizeof(prefix), "Rate(%d)", idx); */
+
+/* 		ast_str_append(msg, 0, "%s/Chargeable: %s\r\n", prefix, */
+/* 			aoc_charged_item_str(decoded->aoc_s_entries[idx].charged_item)); */
+/* 		if (decoded->aoc_s_entries[idx].charged_item == AST_AOC_CHARGED_ITEM_NA) { */
+/* 			continue; */
+/* 		} */
+/* 		rate_str = aoc_rate_type_str(decoded->aoc_s_entries[idx].rate_type); */
+/* 		ast_str_append(msg, 0, "%s/Type: %s\r\n", prefix, rate_str); */
+/* 		switch (decoded->aoc_s_entries[idx].rate_type) { */
+/* 		case AST_AOC_RATE_TYPE_DURATION: */
+/* 			strcat(prefix, "/"); */
+/* 			strcat(prefix, rate_str); */
+/* 			ast_str_append(msg, 0, "%s/Currency: %s\r\n", prefix, */
+/* 				decoded->aoc_s_entries[idx].rate.duration.currency_name); */
+/* 			aoc_amount_str(msg, prefix, */
+/* 				decoded->aoc_s_entries[idx].rate.duration.amount, */
+/* 				decoded->aoc_s_entries[idx].rate.duration.multiplier); */
+/* 			ast_str_append(msg, 0, "%s/ChargingType: %s\r\n", prefix, */
+/* 				decoded->aoc_s_entries[idx].rate.duration.charging_type ? */
+/* 				"StepFunction" : "ContinuousCharging"); */
+/* 			aoc_time_str(msg, prefix, "Time", */
+/* 				decoded->aoc_s_entries[idx].rate.duration.time, */
+/* 				decoded->aoc_s_entries[idx].rate.duration.time_scale); */
+/* 			if (decoded->aoc_s_entries[idx].rate.duration.granularity_time) { */
+/* 				aoc_time_str(msg, prefix, "Granularity", */
+/* 					decoded->aoc_s_entries[idx].rate.duration.granularity_time, */
+/* 					decoded->aoc_s_entries[idx].rate.duration.granularity_time_scale); */
+/* 			} */
+/* 			break; */
+/* 		case AST_AOC_RATE_TYPE_FLAT: */
+/* 			strcat(prefix, "/"); */
+/* 			strcat(prefix, rate_str); */
+/* 			ast_str_append(msg, 0, "%s/Currency: %s\r\n", prefix, */
+/* 				decoded->aoc_s_entries[idx].rate.flat.currency_name); */
+/* 			aoc_amount_str(msg, prefix, */
+/* 				decoded->aoc_s_entries[idx].rate.flat.amount, */
+/* 				decoded->aoc_s_entries[idx].rate.flat.multiplier); */
+/* 			break; */
+/* 		case AST_AOC_RATE_TYPE_VOLUME: */
+/* 			strcat(prefix, "/"); */
+/* 			strcat(prefix, rate_str); */
+/* 			ast_str_append(msg, 0, "%s/Currency: %s\r\n", prefix, */
+/* 				decoded->aoc_s_entries[idx].rate.volume.currency_name); */
+/* 			aoc_amount_str(msg, prefix, */
+/* 				decoded->aoc_s_entries[idx].rate.volume.amount, */
+/* 				decoded->aoc_s_entries[idx].rate.volume.multiplier); */
+/* 			ast_str_append(msg, 0, "%s/Unit: %s\r\n", prefix, */
+/* 				aoc_volume_unit_str(decoded->aoc_s_entries[idx].rate.volume.volume_unit)); */
+/* 			break; */
+/* 		case AST_AOC_RATE_TYPE_SPECIAL_CODE: */
+/* 			ast_str_append(msg, 0, "%s/%s: %d\r\n", prefix, rate_str, */
+/* 				decoded->aoc_s_entries[idx].rate.special_code); */
+/* 			break; */
+/* 		default: */
+/* 			break; */
+/* 		} */
+/* 	} */
+/* } */
+
+/* static void aoc_d_event(const struct ast_aoc_decoded *decoded, struct ast_channel *chan, struct ast_str **msg) */
+/* { */
+/* 	const char *charge_str; */
+/* 	int idx; */
+/* 	char prefix[32]; */
+
+/* 	if (chan) { */
+/* 		ast_str_append(msg, 0, "Channel: %s\r\n", ast_channel_name(chan)); */
+/* 		ast_str_append(msg, 0, "UniqueID: %s\r\n", ast_channel_uniqueid(chan)); */
+/* 	} */
+
+/* 	charge_str = aoc_charge_type_str(decoded->charge_type); */
+/* 	ast_str_append(msg, 0, "Type: %s\r\n", charge_str); */
+
+/* 	switch (decoded->charge_type) { */
+/* 	case AST_AOC_CHARGE_CURRENCY: */
+/* 	case AST_AOC_CHARGE_UNIT: */
+/* 		ast_str_append(msg, 0, "BillingID: %s\r\n", */
+/* 			aoc_billingid_str(decoded->billing_id)); */
+/* 		ast_str_append(msg, 0, "TypeOfCharging: %s\r\n", */
+/* 			aoc_type_of_totaling_str(decoded->total_type)); */
+/* 		break; */
+/* 	default: */
+/* 		break; */
+/* 	} */
+
+/* 	switch (decoded->charge_type) { */
+/* 	case AST_AOC_CHARGE_CURRENCY: */
+/* 		ast_str_append(msg, 0, "%s: %s\r\n", charge_str, */
+/* 			decoded->currency_name); */
+/* 		aoc_amount_str(msg, charge_str, */
+/* 			decoded->currency_amount, */
+/* 			decoded->multiplier); */
+/* 		break; */
+/* 	case AST_AOC_CHARGE_UNIT: */
+/* 		ast_str_append(msg, 0, "%s/NumberItems: %d\r\n", charge_str, */
+/* 			decoded->unit_count); */
+/* 		for (idx = 0; idx < decoded->unit_count; ++idx) { */
+/* 			snprintf(prefix, sizeof(prefix), "%s/Item(%d)", charge_str, idx); */
+/* 			if (decoded->unit_list[idx].valid_amount) { */
+/* 				ast_str_append(msg, 0, "%s/NumberOf: %u\r\n", prefix, */
+/* 					decoded->unit_list[idx].amount); */
+/* 			} */
+/* 			if (decoded->unit_list[idx].valid_type) { */
+/* 				ast_str_append(msg, 0, "%s/TypeOf: %d\r\n", prefix, */
+/* 					decoded->unit_list[idx].type); */
+/* 			} */
+/* 		} */
+/* 		break; */
+/* 	default: */
+/* 		break; */
+/* 	} */
+/* } */
+
+/* static void aoc_e_event(const struct ast_aoc_decoded *decoded, struct ast_channel *chan, struct ast_str **msg) */
+/* { */
+/* 	const char *charge_str; */
+/* 	int idx; */
+/* 	char prefix[32]; */
+
+/* 	if (chan) { */
+/* 		ast_str_append(msg, 0, "Channel: %s\r\n", ast_channel_name(chan)); */
+/* 		ast_str_append(msg, 0, "UniqueID: %s\r\n", ast_channel_uniqueid(chan)); */
+/* 	} */
+
+/* 	charge_str = "ChargingAssociation"; */
+
+/* 	switch (decoded->charging_association.charging_type) { */
+/* 	case AST_AOC_CHARGING_ASSOCIATION_NUMBER: */
+/* 		snprintf(prefix, sizeof(prefix), "%s/Number", charge_str); */
+/* 		ast_str_append(msg, 0, "%s: %s\r\n", prefix, */
+/* 			decoded->charging_association.charge.number.number); */
+/* 		ast_str_append(msg, 0, "%s/Plan: %d\r\n", prefix, */
+/* 			decoded->charging_association.charge.number.plan); */
+/* 		break; */
+/* 	case AST_AOC_CHARGING_ASSOCIATION_ID: */
+/* 		ast_str_append(msg, 0, "%s/ID: %d\r\n", charge_str, decoded->charging_association.charge.id); */
+/* 		break; */
+/* 	case AST_AOC_CHARGING_ASSOCIATION_NA: */
+/* 	default: */
+/* 		break; */
+/* 	} */
+
+/* 	charge_str = aoc_charge_type_str(decoded->charge_type); */
+/* 	ast_str_append(msg, 0, "Type: %s\r\n", charge_str); */
+/* 	switch (decoded->charge_type) { */
+/* 	case AST_AOC_CHARGE_CURRENCY: */
+/* 	case AST_AOC_CHARGE_UNIT: */
+/* 		ast_str_append(msg, 0, "BillingID: %s\r\n", */
+/* 			aoc_billingid_str(decoded->billing_id)); */
+/* 		break; */
+/* 	default: */
+/* 		break; */
+/* 	} */
+/* 	switch (decoded->charge_type) { */
+/* 	case AST_AOC_CHARGE_CURRENCY: */
+/* 		ast_str_append(msg, 0, "%s: %s\r\n", charge_str, */
+/* 			decoded->currency_name); */
+/* 		aoc_amount_str(msg, charge_str, */
+/* 			decoded->currency_amount, */
+/* 			decoded->multiplier); */
+/* 		break; */
+/* 	case AST_AOC_CHARGE_UNIT: */
+/* 		ast_str_append(msg, 0, "%s/NumberItems: %d\r\n", charge_str, */
+/* 			decoded->unit_count); */
+/* 		for (idx = 0; idx < decoded->unit_count; ++idx) { */
+/* 			snprintf(prefix, sizeof(prefix), "%s/Item(%d)", charge_str, idx); */
+/* 			if (decoded->unit_list[idx].valid_amount) { */
+/* 				ast_str_append(msg, 0, "%s/NumberOf: %u\r\n", prefix, */
+/* 					decoded->unit_list[idx].amount); */
+/* 			} */
+/* 			if (decoded->unit_list[idx].valid_type) { */
+/* 				ast_str_append(msg, 0, "%s/TypeOf: %d\r\n", prefix, */
+/* 					decoded->unit_list[idx].type); */
+/* 			} */
+/* 		} */
+/* 		break; */
+/* 	default: */
+/* 		break; */
+/* 	} */
+/* } */
+
+/* int ast_aoc_manager_event(const struct ast_aoc_decoded *decoded, struct ast_channel *chan) */
+/* { */
+/* 	struct ast_str *msg; */
+
+/* 	if (!decoded || !(msg = ast_str_create(1024))) { */
+/* 		return -1; */
+/* 	} */
+
+/* 	stasis_message_create */
+/* 	switch (decoded->msg_type) { */
+/* 	case AST_AOC_S: */
+/* 		if (chan) { */
+/* 			aoc_s_event(decoded, chan, &msg); */
+/* 			ast_manager_event(chan, EVENT_FLAG_AOC, "AOC-S", "%s", ast_str_buffer(msg)); */
+/* 		} */
+/* 		break; */
+/* 	case AST_AOC_D: */
+/* 		if (chan) { */
+/* 			aoc_d_event(decoded, chan, &msg); */
+/* 			ast_manager_event(chan, EVENT_FLAG_AOC, "AOC-D", "%s", ast_str_buffer(msg)); */
+/* 		} */
+/* 		break; */
+/* 	case AST_AOC_E: */
+/* 		{ */
+/* 			struct ast_channel *chans[1]; */
+/* 			aoc_e_event(decoded, chan, &msg); */
+/* 			chans[0] = chan; */
+/* 			ast_manager_event_multichan(EVENT_FLAG_AOC, "AOC-E", chan ? 1 : 0, chans, "%s", ast_str_buffer(msg)); */
+/* 		} */
+/* 		break; */
+/* 	default: */
+/* 		/\* events for AST_AOC_REQUEST are not generated here *\/ */
+/* 		break; */
+/* 	} */
+
+/* 	ast_free(msg); */
+/* 	return 0; */
+/* } */
+
+static void blob_set(struct ast_json *blob, const char *s, ...)
+{
+	RAII_VAR(struct ast_json *, obj, NULL, ast_json_unref);
+
+	va_list args;
+	va_start(args, s);
+	if ((obj = ast_json_vstringf(s, args))) {
+		ast_json_object_update(blob, obj);
+	}
+	va_end(args);
+}
+
+static void set_time(struct ast_json *blob, const char *prefix, const char *name,
+		     unsigned long time, enum ast_aoc_time_scale scale)
+{
+	blob_set("%s/%s/Length: %lu", prefix, name, time);
+	blob_set("%s/%s/Scale: %s", prefix, name, aoc_scale_str(scale));
+}
+
+static void set_amount(struct ast_json *blob, const char *prefix,
+		       unsigned int amount, enum ast_aoc_currency_multiplier mult)
+{
+	static const char name[] = "Amount";
+
+	blob_set("%s/%s/Cost: %u", prefix, name, amount);
+	blob_set("%s/%s/Multiplier: %s", prefix, name,
+		 aoc_multiplier_str(mult));
+}
+
+static void set_currency(struct ast_json *blob, const char *charge,
+			 const struct ast_aoc_decoded *decoded)
+{
+	ast_json_object_set(blob, charge, ast_json_string_create(
+				    decoded->currency_name));
+
+	set_amount(blob, charge, decoded->currency_amount,
+		   decoded->multiplier);
+}
+
+static void set_unit(struct ast_json *blob, const char *charge,
+		     const struct ast_aoc_decoded *decoded)
+{
+	int i;
 	char prefix[32];
-	int idx;
-
-	if (owner) {
-		ast_str_append(msg, 0, "Channel: %s\r\n", ast_channel_name(owner));
-		ast_str_append(msg, 0, "UniqueID: %s\r\n", ast_channel_uniqueid(owner));
-	}
-
-	ast_str_append(msg, 0, "NumberRates: %d\r\n", decoded->aoc_s_count);
-	for (idx = 0; idx < decoded->aoc_s_count; ++idx) {
-		snprintf(prefix, sizeof(prefix), "Rate(%d)", idx);
-
-		ast_str_append(msg, 0, "%s/Chargeable: %s\r\n", prefix,
-			aoc_charged_item_str(decoded->aoc_s_entries[idx].charged_item));
-		if (decoded->aoc_s_entries[idx].charged_item == AST_AOC_CHARGED_ITEM_NA) {
+
+	blob_set("%s/NumberItems: %d", charge, decoded->unit_count);
+
+	for (i = 0; i < decoded->unit_count; ++i) {
+		snprintf(prefix, sizeof(prefix), "%s/Item(%d)", charge, i);
+		if (decoded->unit_list[i].valid_amount) {
+			blob_set("%s/NumberOf: %u", prefix,
+				 decoded->unit_list[i].amount);
+		}
+
+		if (decoded->unit_list[i].valid_type) {
+			blob_set("%s/TypeOf: %d", prefix,
+				 decoded->unit_list[i].type);
+		}
+	}
+}
+
+static void set_currency_or_unit(struct ast_json *blob, const char *charge,
+				 const struct ast_aoc_decoded *decoded)
+{
+	if (decoded->charge_type == AST_AOC_CHARGE_CURRENCY) {
+		set_currency(blob, charge, decoded);
+		return;
+	}
+
+	/* decoded->charge_type == AST_AOC_CHARGE_UNIT */
+	set_unit(blob, charge, decoded);
+}
+
+static void set_s_blob(struct ast_json *blob, const struct ast_aoc_decoded *decoded)
+{
+	const char *rate;
+	char prefix[32];
+	int i;
+
+	blob_set("NumberRates: %d", decoded->aoc_s_count);
+	for (i = 0; i < decoded->aoc_s_count; ++i) {
+		snprintf(prefix, sizeof(prefix), "Rate(%d)", i);
+
+		blob_set("%s/Chargeable: %s", prefix,
+			 aoc_charged_item_str(decoded->aoc_s_entries[i].charged_item));
+
+		if (decoded->aoc_s_entries[i].charged_item == AST_AOC_CHARGED_ITEM_NA) {
 			continue;
 		}
-		rate_str = aoc_rate_type_str(decoded->aoc_s_entries[idx].rate_type);
-		ast_str_append(msg, 0, "%s/Type: %s\r\n", prefix, rate_str);
-		switch (decoded->aoc_s_entries[idx].rate_type) {
+
+		rate = aoc_rate_type_str(decoded->aoc_s_entries[i].rate_type);
+		blob_set("%s/Type: %s", prefix, rate);
+
+		strcat(prefix, "/");
+		strcat(prefix, rate);
+
+		switch (decoded->aoc_s_entries[i].rate_type) {
 		case AST_AOC_RATE_TYPE_DURATION:
-			strcat(prefix, "/");
-			strcat(prefix, rate_str);
-			ast_str_append(msg, 0, "%s/Currency: %s\r\n", prefix,
-				decoded->aoc_s_entries[idx].rate.duration.currency_name);
-			aoc_amount_str(msg, prefix,
-				decoded->aoc_s_entries[idx].rate.duration.amount,
-				decoded->aoc_s_entries[idx].rate.duration.multiplier);
-			ast_str_append(msg, 0, "%s/ChargingType: %s\r\n", prefix,
-				decoded->aoc_s_entries[idx].rate.duration.charging_type ?
-				"StepFunction" : "ContinuousCharging");
-			aoc_time_str(msg, prefix, "Time",
-				decoded->aoc_s_entries[idx].rate.duration.time,
-				decoded->aoc_s_entries[idx].rate.duration.time_scale);
-			if (decoded->aoc_s_entries[idx].rate.duration.granularity_time) {
-				aoc_time_str(msg, prefix, "Granularity",
-					decoded->aoc_s_entries[idx].rate.duration.granularity_time,
-					decoded->aoc_s_entries[idx].rate.duration.granularity_time_scale);
+			blob_set("%s/Currency: %s", prefix,
+				 decoded->aoc_s_entries[i].rate.duration.currency_name);
+
+			set_amount(blob, prefix,
+				   decoded->aoc_s_entries[i].rate.duration.amount,
+				   decoded->aoc_s_entries[i].rate.duration.multiplier);
+
+			blob_set("%s/ChargingType: %s", prefix,
+				 decoded->aoc_s_entries[i].rate.duration.charging_type ?
+				 "StepFunction" : "ContinuousCharging");
+
+			set_time(blob, prefix, "Time",
+				 decoded->aoc_s_entries[i].rate.duration.time,
+				 decoded->aoc_s_entries[i].rate.duration.time_scale);
+
+			if (decoded->aoc_s_entries[i].rate.duration.granularity_time) {
+				set_time(blob, prefix, "Granularity",
+					 decoded->aoc_s_entries[i].rate.duration.granularity_time,
+					 decoded->aoc_s_entries[i].rate.duration.granularity_time_scale);
 			}
 			break;
 		case AST_AOC_RATE_TYPE_FLAT:
-			strcat(prefix, "/");
-			strcat(prefix, rate_str);
-			ast_str_append(msg, 0, "%s/Currency: %s\r\n", prefix,
-				decoded->aoc_s_entries[idx].rate.flat.currency_name);
-			aoc_amount_str(msg, prefix,
-				decoded->aoc_s_entries[idx].rate.flat.amount,
-				decoded->aoc_s_entries[idx].rate.flat.multiplier);
+			blob_set("%s/Currency: %s", prefix,
+				 decoded->aoc_s_entries[i].rate.flat.currency_name);
+			set_amount(blob, prefix,
+				   decoded->aoc_s_entries[i].rate.flat.amount,
+				   decoded->aoc_s_entries[i].rate.flat.multiplier);
 			break;
 		case AST_AOC_RATE_TYPE_VOLUME:
-			strcat(prefix, "/");
-			strcat(prefix, rate_str);
-			ast_str_append(msg, 0, "%s/Currency: %s\r\n", prefix,
-				decoded->aoc_s_entries[idx].rate.volume.currency_name);
-			aoc_amount_str(msg, prefix,
-				decoded->aoc_s_entries[idx].rate.volume.amount,
-				decoded->aoc_s_entries[idx].rate.volume.multiplier);
-			ast_str_append(msg, 0, "%s/Unit: %s\r\n", prefix,
-				aoc_volume_unit_str(decoded->aoc_s_entries[idx].rate.volume.volume_unit));
+			blob_set("%s/Currency: %s", prefix,
+				 decoded->aoc_s_entries[i].rate.volume.currency_name);
+			set_amount(blob, prefix,
+				   decoded->aoc_s_entries[i].rate.volume.amount,
+				   decoded->aoc_s_entries[i].rate.volume.multiplier);
+			blob_set("%s/Unit: %s", prefix,
+				 aoc_volume_unit_str(decoded->aoc_s_entries[i].rate.volume.volume_unit));
 			break;
 		case AST_AOC_RATE_TYPE_SPECIAL_CODE:
-			ast_str_append(msg, 0, "%s/%s: %d\r\n", prefix, rate_str,
-				decoded->aoc_s_entries[idx].rate.special_code);
+			blob_set("%s: %d", prefix,
+				 decoded->aoc_s_entries[i].rate.special_code);
 			break;
 		default:
 			break;
@@ -1387,190 +1695,125 @@
 	}
 }
 
-static void aoc_d_event(const struct ast_aoc_decoded *decoded, struct ast_channel *chan, struct ast_str **msg)
-{
-	const char *charge_str;
-	int idx;
+static void set_d_blob(struct ast_json *blob, const struct ast_aoc_decoded *decoded)
+{
+	const char *charge = aoc_charge_type_str(decoded->charge_type);
+
+	ast_json_object_set(blob, "Type", charge);
+
+	if (decoded->charge_type != AST_AOC_CHARGE_CURRENCY ||
+	    decoded->charge_type != AST_AOC_CHARGE_UNIT) {
+		return;
+	}
+
+	ast_json_object_set(blob, "BillingID", ast_json_string_create(
+				    aoc_billingid_str(decoded->billing_id)));
+	ast_json_object_set(blob, "TypeOfCharging", ast_json_string_create(
+				    aoc_type_of_totaling_str(decoded->total_type)));
+
+	set_currency_or_unit(blob, charge, decoded);
+}
+
+static void set_e_blob(struct ast_json *blob, const struct ast_aoc_decoded *decoded)
+{
 	char prefix[32];
-
-	if (chan) {
-		ast_str_append(msg, 0, "Channel: %s\r\n", ast_channel_name(chan));
-		ast_str_append(msg, 0, "UniqueID: %s\r\n", ast_channel_uniqueid(chan));
-	}
-
-	charge_str = aoc_charge_type_str(decoded->charge_type);
-	ast_str_append(msg, 0, "Type: %s\r\n", charge_str);
-
-	switch (decoded->charge_type) {
-	case AST_AOC_CHARGE_CURRENCY:
-	case AST_AOC_CHARGE_UNIT:
-		ast_str_append(msg, 0, "BillingID: %s\r\n",
-			aoc_billingid_str(decoded->billing_id));
-		ast_str_append(msg, 0, "TypeOfCharging: %s\r\n",
-			aoc_type_of_totaling_str(decoded->total_type));
-		break;
-	default:
-		break;
-	}
-
-	switch (decoded->charge_type) {
-	case AST_AOC_CHARGE_CURRENCY:
-		ast_str_append(msg, 0, "%s: %s\r\n", charge_str,
-			decoded->currency_name);
-		aoc_amount_str(msg, charge_str,
-			decoded->currency_amount,
-			decoded->multiplier);
-		break;
-	case AST_AOC_CHARGE_UNIT:
-		ast_str_append(msg, 0, "%s/NumberItems: %d\r\n", charge_str,
-			decoded->unit_count);
-		for (idx = 0; idx < decoded->unit_count; ++idx) {
-			snprintf(prefix, sizeof(prefix), "%s/Item(%d)", charge_str, idx);
-			if (decoded->unit_list[idx].valid_amount) {
-				ast_str_append(msg, 0, "%s/NumberOf: %u\r\n", prefix,
-					decoded->unit_list[idx].amount);
-			}
-			if (decoded->unit_list[idx].valid_type) {
-				ast_str_append(msg, 0, "%s/TypeOf: %d\r\n", prefix,
-					decoded->unit_list[idx].type);
-			}
-		}
-		break;
-	default:
-		break;
-	}
-}
-
-static void aoc_e_event(const struct ast_aoc_decoded *decoded, struct ast_channel *chan, struct ast_str **msg)
-{
-	const char *charge_str;
-	int idx;
-	char prefix[32];
-
-	if (chan) {
-		ast_str_append(msg, 0, "Channel: %s\r\n", ast_channel_name(chan));
-		ast_str_append(msg, 0, "UniqueID: %s\r\n", ast_channel_uniqueid(chan));
-	}
-
-	charge_str = "ChargingAssociation";
+	const char *charge = "ChargingAssociation";
 
 	switch (decoded->charging_association.charging_type) {
 	case AST_AOC_CHARGING_ASSOCIATION_NUMBER:
-		snprintf(prefix, sizeof(prefix), "%s/Number", charge_str);
-		ast_str_append(msg, 0, "%s: %s\r\n", prefix,
-			decoded->charging_association.charge.number.number);
-		ast_str_append(msg, 0, "%s/Plan: %d\r\n", prefix,
-			decoded->charging_association.charge.number.plan);
+		snprintf(prefix, sizeof(prefix), "%s/Number", charge);
+
+		blob_set("%s: %s", prefix,
+			 decoded->charging_association.charge.number.number);
+		blob_set("%s/Plan: %d", prefix,
+			 decoded->charging_association.charge.number.plan);
 		break;
 	case AST_AOC_CHARGING_ASSOCIATION_ID:
-		ast_str_append(msg, 0, "%s/ID: %d\r\n", charge_str, decoded->charging_association.charge.id);
+		blob_set("%s/ID: %d", charge,
+			 decoded->charging_association.charge.id);
 		break;
 	case AST_AOC_CHARGING_ASSOCIATION_NA:
 	default:
 		break;
 	}
 
-	charge_str = aoc_charge_type_str(decoded->charge_type);
-	ast_str_append(msg, 0, "Type: %s\r\n", charge_str);
-	switch (decoded->charge_type) {
-	case AST_AOC_CHARGE_CURRENCY:
-	case AST_AOC_CHARGE_UNIT:
-		ast_str_append(msg, 0, "BillingID: %s\r\n",
-			aoc_billingid_str(decoded->billing_id));
-		break;
-	default:
-		break;
-	}
-	switch (decoded->charge_type) {
-	case AST_AOC_CHARGE_CURRENCY:
-		ast_str_append(msg, 0, "%s: %s\r\n", charge_str,
-			decoded->currency_name);
-		aoc_amount_str(msg, charge_str,
-			decoded->currency_amount,
-			decoded->multiplier);
-		break;
-	case AST_AOC_CHARGE_UNIT:
-		ast_str_append(msg, 0, "%s/NumberItems: %d\r\n", charge_str,
-			decoded->unit_count);
-		for (idx = 0; idx < decoded->unit_count; ++idx) {
-			snprintf(prefix, sizeof(prefix), "%s/Item(%d)", charge_str, idx);
-			if (decoded->unit_list[idx].valid_amount) {
-				ast_str_append(msg, 0, "%s/NumberOf: %u\r\n", prefix,
-					decoded->unit_list[idx].amount);
-			}
-			if (decoded->unit_list[idx].valid_type) {
-				ast_str_append(msg, 0, "%s/TypeOf: %d\r\n", prefix,
-					decoded->unit_list[idx].type);
-			}
-		}
-		break;
-	default:
-		break;
-	}
+	charge = aoc_charge_type_str(decoded->charge_type);
+	ast_json_object_set(blob, "Type", charge);
+
+	if (decoded->charge_type != AST_AOC_CHARGE_CURRENCY ||
+	    decoded->charge_type != AST_AOC_CHARGE_UNIT) {
+		return;
+	}
+
+	ast_json_object_set(blob, "BillingID", ast_json_string_create(
+				    aoc_billingid_str(decoded->billing_id)));
+
+	set_currency_or_unit(blob, charge, decoded);
 }
 
 int ast_aoc_manager_event(const struct ast_aoc_decoded *decoded, struct ast_channel *chan)
 {
-	struct ast_str *msg;
-
-	if (!decoded || !(msg = ast_str_create(1024))) {
+	RAII_VAR(struct ast_json *, blob, ast_json_object_create(), ast_json_unref);
+	struct stasis_message_type *msg_type;
+
+	if (!decoded || !blob) {
 		return -1;
 	}
 
 	switch (decoded->msg_type) {
 	case AST_AOC_S:
-		if (chan) {
-			aoc_s_event(decoded, chan, &msg);
-			ast_manager_event(chan, EVENT_FLAG_AOC, "AOC-S", "%s", ast_str_buffer(msg));
-		}
+		set_s_blob(blob, decoded);
+		msg_type = aoc_s_type();
 		break;
 	case AST_AOC_D:
-		if (chan) {
-			aoc_d_event(decoded, chan, &msg);
-			ast_manager_event(chan, EVENT_FLAG_AOC, "AOC-D", "%s", ast_str_buffer(msg));
-		}
+		set_d_blob(blob, decoded);
+		msg_type = aoc_d_type();
 		break;
 	case AST_AOC_E:
-		{
-			struct ast_channel *chans[1];
-			aoc_e_event(decoded, chan, &msg);
-			chans[0] = chan;
-			ast_manager_event_multichan(EVENT_FLAG_AOC, "AOC-E", chan ? 1 : 0, chans, "%s", ast_str_buffer(msg));
-		}
+		set_e_blob(blob, decoded);
+		msg_type = aoc_e_type();
 		break;
 	default:
 		/* events for AST_AOC_REQUEST are not generated here */
 		break;
 	}
 
-	ast_free(msg);
+	ast_channel_publish_blob(chan, msg_type, blob);
+
 	return 0;
 }
 
 int ast_aoc_decoded2str(const struct ast_aoc_decoded *decoded, struct ast_str **msg)
 {
-	if (!decoded || !msg) {
+	RAII_VAR(struct ast_json *, blob, ast_json_object_create(), ast_json_unref);
+
+	if (!decoded || !msg || !blob) {
 		return -1;
 	}
 
 	switch (decoded->msg_type) {
 	case AST_AOC_S:
-		ast_str_append(msg, 0, "AOC-S\r\n");
-		aoc_s_event(decoded, NULL, msg);
+		set_blob(blob, "AOC-S");
+		set_s_blob(blob, decoded);
+		msg_type = aoc_s_type();
 		break;
 	case AST_AOC_D:
-		ast_str_append(msg, 0, "AOC-D\r\n");
-		aoc_d_event(decoded, NULL, msg);
+		set_blob(blob, "AOC-D");
+		build_d_blob(blob, decoded);
+		msg_type = aoc_d_type();
 		break;
 	case AST_AOC_E:
-		ast_str_append(msg, 0, "AOC-E\r\n");
-		aoc_e_event(decoded, NULL, msg);
+		set_blob(blob, "AOC-E");
+		build_e_blob(blob, decoded);
+		msg_type = aoc_e_type();
 		break;
 	case AST_AOC_REQUEST:
-		ast_str_append(msg, 0, "AOC-Request\r\n");
+		set_blob(blob, "AOC-Request");
 		aoc_request_event(decoded, NULL, msg);
 		break;
 	}
+
+	ast_json_dump_string_format(blob, msg, AST_JSON_PRETTY);
 
 	return 0;
 }
@@ -1605,12 +1848,83 @@
 	AST_CLI_DEFINE(aoc_cli_debug_enable, "enable cli debugging of AOC messages"),
 };
 
+/* static void aoc_d_event(void *data, struct stasis_subscription *sub, */
+/* 			struct stasis_topic *topic, struct stasis_message *message) */
+/* { */
+/* } */
+
+/* static void aoc_e_event(void *data, struct stasis_subscription *sub, */
+/* 			struct stasis_topic *topic, struct stasis_message *message) */
+/* { */
+/* } */
+
+/* static void aoc_s_event(void *data, struct stasis_subscription *sub, */
+/* 			struct stasis_topic *topic, struct stasis_message *message) */
+/* { */
+/* } */
+
+/* static void aoc_request_event(void *data, struct stasis_subscription *sub, */
+/* 			      struct stasis_topic *topic, struct stasis_message *message) */
+/* { */
+/* } */
+
+/* struct stasis_message_type *aoc_s_type(void); */
+/* struct stasis_message_type *aoc_d_type(void); */
+/* struct stasis_message_type *aoc_e_type(void); */
+/* struct stasis_message_type *aoc_request_type(void); */
+
+/* STASIS_MESSAGE_TYPE_DEFN(aoc_s_type); */
+/* STASIS_MESSAGE_TYPE_DEFN(aoc_d_type); */
+/* STASIS_MESSAGE_TYPE_DEFN(aoc_e_type); */
+/* STASIS_MESSAGE_TYPE_DEFN(aoc_request_type); */
+
+int ast_aoc_cli_init(void)
+{
+	/* struct stasis_message_router *message_router = */
+	/* 	ast_manager_get_message_router(); */
+
+	/* if (!message_router) { */
+	/* 	return AST_MODULE_LOAD_DECLINE; */
+	/* } */
+
+	/* STASIS_MESSAGE_TYPE_INIT(aoc_s_type); */
+	/* STASIS_MESSAGE_TYPE_INIT(aoc_d_type); */
+	/* STASIS_MESSAGE_TYPE_INIT(aoc_e_type); */
+	/* STASIS_MESSAGE_TYPE_INIT(aoc_request_type); */
+
+	/* stasis_message_router_add(message_router, aoc_s_type(), */
+	/* 			  aoc_s_event, "AOC-S"); */
+
+	/* stasis_message_router_add(message_router, aoc_d_type(), */
+	/* 			  aoc_d_event, "AOC-D"); */
+
+	/* stasis_message_router_add(message_router, aoc_e_type(), */
+	/* 			  aoc_e_event, "AOC-E"); */
+
+	/* stasis_message_router_add(message_router, aoc_s_type(), */
+	/* 			  aoc_request_event, "AOC-Request"); */
+
+	ast_register_atexit(aoc_shutdown);
+
+	return ast_cli_register_multiple(aoc_cli, ARRAY_LEN(aoc_cli));
+}
+
 static void aoc_shutdown(void)
 {
+	/* struct stasis_message_router *message_route = */
+	/* 	= ast_manager_get_message_router(); */
+
+	/* if (message_router) { */
+	/* 	stasis_message_router_remove(message_router, aoc_s_type()); */
+	/* 	stasis_message_router_remove(message_router, aoc_d_type(); */
+	/* 	stasis_message_router_remove(message_router, aoc_e_type()); */
+	/* 	stasis_message_router_remove(message_router, aoc_request_type()); */
+	/* } */
+
+	/* STASIS_MESSAGE_TYPE_CLEANUP(aoc_s_type); */
+	/* STASIS_MESSAGE_TYPE_CLEANUP(aoc_d_type); */
+	/* STASIS_MESSAGE_TYPE_CLEANUP(aoc_e_type); */
+	/* STASIS_MESSAGE_TYPE_CLEANUP(aoc_request_type); */
+
 	ast_cli_unregister_multiple(aoc_cli, ARRAY_LEN(aoc_cli));
 }
-int ast_aoc_cli_init(void)
-{
-	ast_register_atexit(aoc_shutdown);
-	return ast_cli_register_multiple(aoc_cli, ARRAY_LEN(aoc_cli));
-}




More information about the asterisk-commits mailing list