Debian Codegraph @

main/aoc.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2010, Digium, Inc.
5 *
6 * David Vossel <[email protected]>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19 /*!
20 * \file
21 * \brief generic AOC payload generation encoding and decoding
22 *
23 * \author David Vossel <[email protected]>
24 */
25
26 /*** MODULEINFO
27 <support_level>core</support_level>
28 ***/
29
30 #include "asterisk.h"
31
32 #include "asterisk/aoc.h"
33 #include "asterisk/utils.h"
34 #include "asterisk/strings.h"
35 #include "asterisk/_private.h"
36 #include "asterisk/cli.h"
37 #include "asterisk/manager.h"
38 #include "asterisk/stasis_channels.h"
39 #include "asterisk/stasis_message_router.h"
40
41 /*** DOCUMENTATION
42 <managerEvent language="en_US" name="AOC-S">
43 <managerEventInstance class="EVENT_FLAG_AOC">
44 <since>
45 <version>12.0.0</version>
46 </since>
47 <synopsis>Raised when an Advice of Charge message is sent at the beginning of a call.</synopsis>
48 <syntax>
49 <channel_snapshot/>
50 <parameter name="Chargeable" />
51 <parameter name="RateType">
52 <enumlist>
53 <enum name="NotAvailable" />
54 <enum name="Free" />
55 <enum name="FreeFromBeginning" />
56 <enum name="Duration" />
57 <enum name="Flag" />
58 <enum name="Volume" />
59 <enum name="SpecialCode" />
60 </enumlist>
61 </parameter>
62 <parameter name="Currency" />
63 <parameter name="Name" />
64 <parameter name="Cost" />
65 <parameter name="Multiplier">
66 <enumlist>
67 <enum name="1/1000" />
68 <enum name="1/100" />
69 <enum name="1/10" />
70 <enum name="1" />
71 <enum name="10" />
72 <enum name="100" />
73 <enum name="1000" />
74 </enumlist>
75 </parameter>
76 <parameter name="ChargingType" />
77 <parameter name="StepFunction" />
78 <parameter name="Granularity" />
79 <parameter name="Length" />
80 <parameter name="Scale" />
81 <parameter name="Unit">
82 <enumlist>
83 <enum name="Octet" />
84 <enum name="Segment" />
85 <enum name="Message" />
86 </enumlist>
87 </parameter>
88 <parameter name="SpecialCode" />
89 </syntax>
90 <see-also>
91 <ref type="managerEvent">AOC-D</ref>
92 <ref type="managerEvent">AOC-E</ref>
93 </see-also>
94 </managerEventInstance>
95 </managerEvent>
96 <managerEvent language="en_US" name="AOC-D">
97 <managerEventInstance class="EVENT_FLAG_AOC">
98 <since>
99 <version>12.0.0</version>
100 </since>
101 <synopsis>Raised when an Advice of Charge message is sent during a call.</synopsis>
102 <syntax>
103 <channel_snapshot/>
104 <parameter name="Charge" />
105 <parameter name="Type">
106 <enumlist>
107 <enum name="NotAvailable" />
108 <enum name="Free" />
109 <enum name="Currency" />
110 <enum name="Units" />
111 </enumlist>
112 </parameter>
113 <parameter name="BillingID">
114 <enumlist>
115 <enum name="Normal" />
116 <enum name="Reverse" />
117 <enum name="CreditCard" />
118 <enum name="CallForwardingUnconditional" />
119 <enum name="CallForwardingBusy" />
120 <enum name="CallForwardingNoReply" />
121 <enum name="CallDeflection" />
122 <enum name="CallTransfer" />
123 <enum name="NotAvailable" />
124 </enumlist>
125 </parameter>
126 <parameter name="TotalType">
127 <enumlist>
128 <enum name="SubTotal" />
129 <enum name="Total" />
130 </enumlist>
131 </parameter>
132 <parameter name="Currency" />
133 <parameter name="Name" />
134 <parameter name="Cost" />
135 <parameter name="Multiplier">
136 <enumlist>
137 <enum name="1/1000" />
138 <enum name="1/100" />
139 <enum name="1/10" />
140 <enum name="1" />
141 <enum name="10" />
142 <enum name="100" />
143 <enum name="1000" />
144 </enumlist>
145 </parameter>
146 <parameter name="Units" />
147 <parameter name="NumberOf" />
148 <parameter name="TypeOf" />
149 </syntax>
150 <see-also>
151 <ref type="manager">AOCMessage</ref>
152 <ref type="managerEvent">AOC-S</ref>
153 <ref type="managerEvent">AOC-E</ref>
154 </see-also>
155 </managerEventInstance>
156 </managerEvent>
157 <managerEvent language="en_US" name="AOC-E">
158 <managerEventInstance class="EVENT_FLAG_AOC">
159 <since>
160 <version>12.0.0</version>
161 </since>
162 <synopsis>Raised when an Advice of Charge message is sent at the end of a call.</synopsis>
163 <syntax>
164 <channel_snapshot/>
165 <parameter name="ChargingAssociation" />
166 <parameter name="Number" />
167 <parameter name="Plan" />
168 <parameter name="ID" />
169 <xi:include xpointer="xpointer(/docs/managerEvent[@name='AOC-D']/managerEventInstance/syntax/parameter)" />
170 </syntax>
171 <see-also>
172 <ref type="manager">AOCMessage</ref>
173 <ref type="managerEvent">AOC-S</ref>
174 <ref type="managerEvent">AOC-D</ref>
175 </see-also>
176 </managerEventInstance>
177 </managerEvent>
178 ***/
179
180 /* Encoded Payload Flags */
181 #define AST_AOC_ENCODED_TYPE_REQUEST (0 << 0)
182 #define AST_AOC_ENCODED_TYPE_D (1 << 0)
183 #define AST_AOC_ENCODED_TYPE_E (2 << 0)
184 #define AST_AOC_ENCODED_TYPE_S (3 << 0)
185
186 #define AST_AOC_ENCODED_REQUEST_S (1 << 2)
187 #define AST_AOC_ENCODED_REQUEST_D (1 << 3)
188 #define AST_AOC_ENCODED_REQUEST_E (1 << 4)
189
190 #define AST_AOC_ENCODED_CHARGE_NA (0 << 5)
191 #define AST_AOC_ENCODED_CHARGE_FREE (1 << 5)
192 #define AST_AOC_ENCODED_CHARGE_CURRENCY (2 << 5)
193 #define AST_AOC_ENCODED_CHARGE_UNIT (3 << 5)
194
195 #define AST_AOC_ENCODED_CHARGE_SUBTOTAL (1 << 7)
196 #define AST_AOC_ENCODED_CHARGE_TOTAL (0 << 7)
197
198 #define AST_AOC_ENCODE_VERSION 1
199
200
201 static char aoc_debug_enabled = 0;
202 static void aoc_display_decoded_debug(const struct ast_aoc_decoded *decoded, int decoding, struct ast_channel *chan);
203 static int aoc_s_add_entry(struct ast_aoc_decoded *decoded, struct ast_aoc_s_entry *entry);
204
205 /* AOC Payload Header. Holds all the encoded AOC data to pass on the wire */
206 struct ast_aoc_encoded {
207 uint8_t version;
208 uint8_t flags;
209 uint16_t datalen;
210 unsigned char data[0];
211 };
212
213 /* Decoded AOC data */
214 struct ast_aoc_decoded {
215 enum ast_aoc_type msg_type;
216 enum ast_aoc_charge_type charge_type;
217 enum ast_aoc_request request_flag;
218 enum ast_aoc_total_type total_type;
219
220 /* currency information */
221 enum ast_aoc_currency_multiplier multiplier;
222 unsigned int currency_amount;
223 char currency_name[AOC_CURRENCY_NAME_SIZE];
224
225 /* unit information */
226 int unit_count;
227 struct ast_aoc_unit_entry unit_list[32];
228
229 /* Billing Id */
230 enum ast_aoc_billing_id billing_id;
231
232 /* Charging Association information */
233 struct ast_aoc_charging_association charging_association;
234
235 /* AOC-S charge information */
236 int aoc_s_count;
237 struct ast_aoc_s_entry aoc_s_entries[10];
238
239 /* Is this an AOC Termination Request */
240 char termination_request;
241 };
242
243 /*! \brief AOC Payload Information Elements */
244 enum AOC_IE {
245 AOC_IE_CURRENCY = 1,
246 AOC_IE_UNIT = 2,
247 AOC_IE_BILLING = 3,
248 AOC_IE_CHARGING_ASSOCIATION = 4,
249 AOC_IE_RATE = 5,
250 AOC_IE_TERMINATION_REQUEST = 6,
251 };
252
253 /*! \brief AOC IE payload header */
254 struct aoc_pl_ie_hdr {
255 uint8_t ie_id;
256 uint8_t datalen;
257 char data[0];
258 } __attribute__((packed));
259
260 struct aoc_ie_currency {
261 uint32_t amount;
262 uint8_t multiplier;
263 char name[AOC_CURRENCY_NAME_SIZE];
264 } __attribute__((packed));
265
266 struct aoc_ie_unit {
267 uint32_t amount;
268 uint8_t valid_type;
269 uint8_t valid_amount;
270 uint8_t type;
271 } __attribute__((packed));
272
273 struct aoc_ie_billing {
274 uint8_t id;
275 } __attribute__((packed));
276
277 struct aoc_ie_charging_association {
278 struct ast_aoc_charging_association ca;
279 } __attribute__((packed));
280
281 struct aoc_ie_charging_rate {
282 struct ast_aoc_s_entry entry;
283 } __attribute__((packed));
284
285 struct ast_aoc_decoded *ast_aoc_create(const enum ast_aoc_type msg_type,
286 const enum ast_aoc_charge_type charge_type,
287 const enum ast_aoc_request requests)
288 {
289 struct ast_aoc_decoded *decoded = NULL;
290
291 /* verify input */
292 if (((unsigned int) charge_type > AST_AOC_CHARGE_UNIT) ||
293 ((unsigned int) msg_type > AST_AOC_E) ||
294 ((msg_type == AST_AOC_REQUEST) && !requests)) {
295
296 ast_log(LOG_WARNING, "Failed to create ast_aoc_decoded object, invalid input\n");
297 return NULL;
298 }
299
300 if (!(decoded = ast_calloc(1, sizeof(struct ast_aoc_decoded)))) {
301 ast_log(LOG_WARNING, "Failed to create ast_aoc_decoded object \n");
302 return NULL;
303 }
304
305 decoded->msg_type = msg_type;
306
307 if (msg_type == AST_AOC_REQUEST) {
308 decoded->request_flag = requests;
309 } else if ((msg_type == AST_AOC_D) || (msg_type == AST_AOC_E)) {
310 decoded->charge_type = charge_type;
311 }
312
313 return decoded;
314 }
315
316 void *ast_aoc_destroy_decoded(struct ast_aoc_decoded *decoded)
317 {
318 ast_free(decoded);
319 return NULL;
320 }
321
322 void *ast_aoc_destroy_encoded(struct ast_aoc_encoded *encoded)
323 {
324 ast_free(encoded);
325 return NULL;
326 }
327
328 static void aoc_parse_ie_charging_rate(struct ast_aoc_decoded *decoded, const struct aoc_ie_charging_rate *ie)
329 {
330 struct ast_aoc_s_entry entry = { 0, };
331
332 entry.charged_item = ntohs(ie->entry.charged_item);
333 entry.rate_type = ntohs(ie->entry.rate_type);
334
335 switch (entry.rate_type) {
336 case AST_AOC_RATE_TYPE_DURATION:
337 entry.rate.duration.multiplier = ntohs(ie->entry.rate.duration.multiplier);
338 entry.rate.duration.amount = ntohl(ie->entry.rate.duration.amount);
339 entry.rate.duration.time = ntohl(ie->entry.rate.duration.time);
340 entry.rate.duration.time_scale = ntohs(ie->entry.rate.duration.time_scale);
341 entry.rate.duration.granularity_time = ntohl(ie->entry.rate.duration.granularity_time);
342 entry.rate.duration.granularity_time_scale = ntohs(ie->entry.rate.duration.granularity_time_scale);
343 entry.rate.duration.charging_type = ie->entry.rate.duration.charging_type; /* only one byte */
344
345 if (!ast_strlen_zero(ie->entry.rate.duration.currency_name)) {
346 ast_copy_string(entry.rate.duration.currency_name,
347 ie->entry.rate.duration.currency_name,
348 sizeof(entry.rate.duration.currency_name));
349 }
350 break;
351 case AST_AOC_RATE_TYPE_FLAT:
352 entry.rate.flat.multiplier = ntohs(ie->entry.rate.flat.multiplier);
353 entry.rate.flat.amount = ntohl(ie->entry.rate.flat.amount);
354 if (!ast_strlen_zero(ie->entry.rate.flat.currency_name)) {
355 ast_copy_string(entry.rate.flat.currency_name,
356 ie->entry.rate.flat.currency_name,
357 sizeof(entry.rate.flat.currency_name));
358 }
359 break;
360 case AST_AOC_RATE_TYPE_VOLUME:
361 entry.rate.volume.multiplier = ntohs(ie->entry.rate.volume.multiplier);
362 entry.rate.volume.amount = ntohl(ie->entry.rate.volume.amount);
363 entry.rate.volume.volume_unit = ntohs(ie->entry.rate.volume.volume_unit);
364 if (!ast_strlen_zero(ie->entry.rate.volume.currency_name)) {
365 ast_copy_string(entry.rate.volume.currency_name,
366 ie->entry.rate.volume.currency_name,
367 sizeof(entry.rate.volume.currency_name));
368 }
369 break;
370 case AST_AOC_RATE_TYPE_SPECIAL_CODE:
371 entry.rate.special_code = ntohs(ie->entry.rate.special_code);
372 break;
373 }
374
375 aoc_s_add_entry(decoded, &entry);
376 }
377
378 static int aoc_parse_ie(struct ast_aoc_decoded *decoded, unsigned char *data, unsigned int datalen)
379 {
380 enum AOC_IE ie_id;
381 unsigned int len;
382
383 while (datalen >= 2) {
384 ie_id = data[0];
385 len = data[1];
386 if (len > datalen -2) {
387 ast_log(LOG_ERROR, "AOC information element length exceeds the total message size\n");
388 return -1;
389 }
390
391 switch(ie_id) {
392 case AOC_IE_CURRENCY:
393 if (len == sizeof(struct aoc_ie_currency)) {
394 struct aoc_ie_currency ie;
395 memcpy(&ie, data + 2, len);
396 decoded->currency_amount = ntohl(ie.amount);
397 decoded->multiplier = ie.multiplier; /* only one byte */
398 memcpy(decoded->currency_name, ie.name, sizeof(decoded->currency_name));
399 } else {
400 ast_log(LOG_WARNING, "Received invalid currency ie\n");
401 }
402 break;
403 case AOC_IE_UNIT:
404 if (len == sizeof(struct aoc_ie_unit)) {
405 struct aoc_ie_unit ie;
406 memcpy(&ie, data + 2, len);
407 ast_aoc_add_unit_entry(decoded, ie.valid_amount, ntohl(ie.amount), ie.valid_type, ie.type);
408 } else {
409 ast_log(LOG_WARNING, "Received invalid unit ie\n");
410 }
411 break;
412 case AOC_IE_BILLING:
413 if (len == sizeof(struct aoc_ie_billing)) {
414 struct aoc_ie_billing ie;
415 memcpy(&ie, data + 2, len);
416 decoded->billing_id = ie.id; /* only one byte */
417 } else {
418 ast_log(LOG_WARNING, "Received invalid billing ie\n");
419 }
420 break;
421 case AOC_IE_CHARGING_ASSOCIATION:
422 if (len == sizeof(struct aoc_ie_charging_association)) {
423 memcpy(&decoded->charging_association, data + 2, sizeof(decoded->charging_association));
424 /* everything in the charging_association struct is a single byte except for the id */
425 if (decoded->charging_association.charging_type == AST_AOC_CHARGING_ASSOCIATION_ID) {
426 decoded->charging_association.charge.id = ntohl(decoded->charging_association.charge.id);
427 }
428 } else {
429 ast_log(LOG_WARNING, "Received invalid charging association ie\n");
430 }
431 break;
432 case AOC_IE_RATE:
433 if (len == sizeof(struct aoc_ie_charging_rate)) {
434 struct aoc_ie_charging_rate ie;
435 memcpy(&ie, data + 2, len);
436 aoc_parse_ie_charging_rate(decoded, &ie);
437 } else {
438 ast_log(LOG_WARNING, "Received invalid charging rate ie\n");
439 }
440 break;
441 case AOC_IE_TERMINATION_REQUEST:
442 if (len == 0) {
443 decoded->termination_request = 1;
444 } else {
445 ast_log(LOG_WARNING, "Received invalid termination request ie\n");
446 }
447 break;
448 default:
449 ast_log(LOG_WARNING, "Unknown AOC Information Element, ignoring.\n");
450 }
451
452 datalen -= (len + 2);
453 data += (len + 2);
454 }
455 return 0;
456 }
457
458 struct ast_aoc_decoded *ast_aoc_decode(struct ast_aoc_encoded *encoded, size_t size, struct ast_channel *chan)
459 {
460 struct ast_aoc_decoded *decoded;
461
462 /* verify our encoded payload is actually large enough to hold all the ies */
463 if ((size - (sizeof(struct ast_aoc_encoded)) != ntohs(encoded->datalen))) {
464 ast_log(LOG_WARNING, "Corrupted aoc encoded object, can not decode\n");
465 return NULL;
466 }
467
468 if (!(decoded = ast_calloc(1, sizeof(struct ast_aoc_decoded)))) {
469 ast_log(LOG_WARNING, "Failed to create ast_aoc_decoded object \n");
470 return NULL;
471 }
472
473 /* decode flags */
474
475 if ((encoded->flags & AST_AOC_ENCODED_TYPE_S) == AST_AOC_ENCODED_TYPE_S) {
476 decoded->msg_type = AST_AOC_S;
477 } else if (encoded->flags & AST_AOC_ENCODED_TYPE_E) {
478 decoded->msg_type = AST_AOC_E;
479 } else if (encoded->flags & AST_AOC_ENCODED_TYPE_D) {
480 decoded->msg_type = AST_AOC_D;
481 } else {
482 decoded->msg_type = AST_AOC_REQUEST;
483 }
484
485 if (decoded->msg_type == AST_AOC_REQUEST) {
486 if (encoded->flags & AST_AOC_ENCODED_REQUEST_S) {
487 decoded->request_flag |= AST_AOC_REQUEST_S;
488 }
489 if (encoded->flags & AST_AOC_ENCODED_REQUEST_D) {
490 decoded->request_flag |= AST_AOC_REQUEST_D;
491 }
492 if (encoded->flags & AST_AOC_ENCODED_REQUEST_E) {
493 decoded->request_flag |= AST_AOC_REQUEST_E;
494 }
495 } else if ((decoded->msg_type == AST_AOC_D) || (decoded->msg_type == AST_AOC_E)) {
496 if ((encoded->flags & AST_AOC_ENCODED_CHARGE_UNIT) == AST_AOC_ENCODED_CHARGE_UNIT) {
497 decoded->charge_type = AST_AOC_CHARGE_UNIT;
498 } else if ((encoded->flags & AST_AOC_ENCODED_CHARGE_CURRENCY) == AST_AOC_ENCODED_CHARGE_CURRENCY) {
499 decoded->charge_type = AST_AOC_CHARGE_CURRENCY;
500 } else if ((encoded->flags & AST_AOC_ENCODED_CHARGE_FREE) == AST_AOC_ENCODED_CHARGE_FREE) {
501 decoded->charge_type = AST_AOC_CHARGE_FREE;
502 } else {
503 decoded->charge_type = AST_AOC_CHARGE_NA;
504 }
505
506 if (encoded->flags & AST_AOC_ENCODED_CHARGE_SUBTOTAL) {
507 decoded->total_type = AST_AOC_SUBTOTAL;
508 }
509 }
510
511 /* decode information elements */
512 aoc_parse_ie(decoded, encoded->data, ntohs(encoded->datalen));
513
514 if (aoc_debug_enabled) {
515 aoc_display_decoded_debug(decoded, 1, chan);
516 }
517
518 return decoded;
519 }
520
521 struct aoc_ie_data {
522 unsigned char buf[1024];
523 int pos;
524 };
525
526 /*!
527 * \internal
528 * \brief append an AOC information element
529 * \note data is expected to already be in network byte order at this point
530 */
531 static int aoc_append_ie(struct aoc_ie_data *ied, unsigned short ie_id, const void *data, unsigned short datalen)
532 {
533 if (datalen > ((int)sizeof(ied->buf) - ied->pos)) {
534 ast_log(LOG_WARNING, "Failure to append AOC information element, out of space \n");
535 return -1;
536 }
537 ied->buf[ied->pos++] = ie_id;
538 ied->buf[ied->pos++] = datalen;
539 if (datalen) {
540 memcpy(ied->buf + ied->pos, data, datalen);
541 ied->pos += datalen;
542 }
543 return 0;
544 }
545
546 static void aoc_create_ie_data_charging_rate(const struct ast_aoc_s_entry *entry, struct aoc_ie_charging_rate *ie)
547 {
548 ie->entry.charged_item = htons(entry->charged_item);
549 ie->entry.rate_type = htons(entry->rate_type);
550
551 switch (entry->rate_type) {
552 case AST_AOC_RATE_TYPE_DURATION:
553 ie->entry.rate.duration.multiplier = htons(entry->rate.duration.multiplier);
554 ie->entry.rate.duration.amount = htonl(entry->rate.duration.amount);
555 ie->entry.rate.duration.time = htonl(entry->rate.duration.time);
556 ie->entry.rate.duration.time_scale = htons(entry->rate.duration.time_scale);
557 ie->entry.rate.duration.granularity_time = htonl(entry->rate.duration.granularity_time);
558 ie->entry.rate.duration.granularity_time_scale = htons(entry->rate.duration.granularity_time_scale);
559 ie->entry.rate.duration.charging_type = entry->rate.duration.charging_type; /* only one byte */
560
561 if (!ast_strlen_zero(entry->rate.duration.currency_name)) {
562 ast_copy_string(ie->entry.rate.duration.currency_name,
563 entry->rate.duration.currency_name,
564 sizeof(ie->entry.rate.duration.currency_name));
565 }
566 break;
567 case AST_AOC_RATE_TYPE_FLAT:
568 ie->entry.rate.flat.multiplier = htons(entry->rate.flat.multiplier);
569 ie->entry.rate.flat.amount = htonl(entry->rate.flat.amount);
570 if (!ast_strlen_zero(entry->rate.flat.currency_name)) {
571 ast_copy_string(ie->entry.rate.flat.currency_name,
572 entry->rate.flat.currency_name,
573 sizeof(ie->entry.rate.flat.currency_name));
574 }
575 break;
576 case AST_AOC_RATE_TYPE_VOLUME:
577 ie->entry.rate.volume.multiplier = htons(entry->rate.volume.multiplier);
578 ie->entry.rate.volume.amount = htonl(entry->rate.volume.amount);
579 ie->entry.rate.volume.volume_unit = htons(entry->rate.volume.volume_unit);
580 if (!ast_strlen_zero(entry->rate.volume.currency_name)) {
581 ast_copy_string(ie->entry.rate.volume.currency_name,
582 entry->rate.volume.currency_name,
583 sizeof(ie->entry.rate.volume.currency_name));
584 }
585 break;
586 case AST_AOC_RATE_TYPE_SPECIAL_CODE:
587 ie->entry.rate.special_code = htons(entry->rate.special_code);
588 break;
589 }
590
591 }
592 static void aoc_create_ie_data(struct ast_aoc_decoded *decoded, struct aoc_ie_data *ied)
593 {
594 ied->pos = 0;
595
596 if (decoded->currency_amount) {
597 struct aoc_ie_currency ie = {
598 .amount = htonl(decoded->currency_amount),
599 .multiplier = decoded->multiplier, /* only one byte */
600 .name = { 0, },
601 };
602
603 if (!ast_strlen_zero(decoded->currency_name)) {
604 ast_copy_string(ie.name, decoded->currency_name, sizeof(ie.name));
605 }
606
607 aoc_append_ie(ied, AOC_IE_CURRENCY, (const void *) &ie, sizeof(ie));
608 }
609
610 if (decoded->unit_count) {
611 struct aoc_ie_unit ie = { 0 };
612 int i;
613
614 for (i = 0; i < decoded->unit_count; i++) {
615 ie.valid_amount = decoded->unit_list[i].valid_amount; /* only one byte */
616 ie.amount = htonl(decoded->unit_list[i].amount);
617 ie.valid_type = decoded->unit_list[i].valid_type; /* only one byte */
618 ie.type = decoded->unit_list[i].type; /* only one byte */
619 aoc_append_ie(ied, AOC_IE_UNIT, (const void *) &ie, sizeof(ie));
620 }
621 }
622
623 if (decoded->billing_id) {
624 struct aoc_ie_billing ie;
625 ie.id = decoded->billing_id; /* only one byte */
626 aoc_append_ie(ied, AOC_IE_BILLING, (const void *) &ie, sizeof(ie));
627 }
628
629 if (decoded->charging_association.charging_type != AST_AOC_CHARGING_ASSOCIATION_NA) {
630 struct aoc_ie_charging_association ie;
631 memset(&ie, 0, sizeof(ie));
632 ie.ca.charging_type = decoded->charging_association.charging_type; /* only one byte */
633 if (decoded->charging_association.charging_type == AST_AOC_CHARGING_ASSOCIATION_NUMBER) {
634 ie.ca.charge.number.plan = decoded->charging_association.charge.number.plan; /* only one byte */
635 ast_copy_string(ie.ca.charge.number.number,
636 decoded->charging_association.charge.number.number,
637 sizeof(ie.ca.charge.number.number));
638 } else if (decoded->charging_association.charging_type == AST_AOC_CHARGING_ASSOCIATION_ID) {
639 ie.ca.charge.id = htonl(decoded->charging_association.charge.id);
640 }
641 aoc_append_ie(ied, AOC_IE_CHARGING_ASSOCIATION, (const void *) &ie, sizeof(ie));
642 }
643
644 if (decoded->aoc_s_count) {
645 struct aoc_ie_charging_rate ie;
646 int i;
647 for (i = 0; i < decoded->aoc_s_count; i++) {
648 memset(&ie, 0, sizeof(ie));
649 aoc_create_ie_data_charging_rate(&decoded->aoc_s_entries[i], &ie);
650 aoc_append_ie(ied, AOC_IE_RATE, (const void *) &ie, sizeof(ie));
651 }
652 }
653
654 if (decoded->termination_request) {
655 aoc_append_ie(ied, AOC_IE_TERMINATION_REQUEST, NULL, 0);
656 }
657 }
658
659 struct ast_aoc_encoded *ast_aoc_encode(struct ast_aoc_decoded *decoded, size_t *out_size, struct ast_channel *chan)
660 {
661 struct aoc_ie_data ied;
662 struct ast_aoc_encoded *encoded = NULL;
663 size_t size = 0;
664
665 if (!decoded || !out_size) {
666 return NULL;
667 }
668
669 *out_size = 0;
670
671 /* create information element buffer before allocating the payload,
672 * by doing this the exact size of the payload + the id data can be
673 * allocated all at once. */
674 aoc_create_ie_data(decoded, &ied);
675
676 size = sizeof(struct ast_aoc_encoded) + ied.pos;
677
678 if (!(encoded = ast_calloc(1, size))) {
679 ast_log(LOG_WARNING, "Failed to create ast_aoc_encoded object during decode routine. \n");
680 return NULL;
681 }
682
683 /* -- Set ie data buffer */
684 if (ied.pos) {
685 /* this is safe because encoded was allocated to fit this perfectly */
686 memcpy(encoded->data, ied.buf, ied.pos);
687 encoded->datalen = htons(ied.pos);
688 }
689
690 /* --- Set Flags --- */
691 switch (decoded->msg_type) {
692 case AST_AOC_S:
693 encoded->flags = AST_AOC_ENCODED_TYPE_S;
694 break;
695 case AST_AOC_D:
696 encoded->flags = AST_AOC_ENCODED_TYPE_D;
697 break;
698 case AST_AOC_E:
699 encoded->flags = AST_AOC_ENCODED_TYPE_E;
700 break;
701 case AST_AOC_REQUEST:
702 encoded->flags = AST_AOC_ENCODED_TYPE_REQUEST;
703 default:
704 break;
705 }
706
707 /* if it is type request, set the types requested, else set charge type */
708 if (decoded->msg_type == AST_AOC_REQUEST) {
709 if (decoded->request_flag & AST_AOC_REQUEST_S) {
710 encoded->flags |= AST_AOC_ENCODED_REQUEST_S;
711 }
712 if (decoded->request_flag & AST_AOC_REQUEST_D) {
713 encoded->flags |= AST_AOC_ENCODED_REQUEST_D;
714 }
715 if (decoded->request_flag & AST_AOC_REQUEST_E) {
716 encoded->flags |= AST_AOC_ENCODED_REQUEST_E;
717 }
718 } else if ((decoded->msg_type == AST_AOC_D) || (decoded->msg_type == AST_AOC_E)) {
719 switch (decoded->charge_type) {
720 case AST_AOC_CHARGE_UNIT:
721 encoded->flags |= AST_AOC_ENCODED_CHARGE_UNIT;
722 break;
723 case AST_AOC_CHARGE_CURRENCY:
724 encoded->flags |= AST_AOC_ENCODED_CHARGE_CURRENCY;
725 break;
726 case AST_AOC_CHARGE_FREE:
727 encoded->flags |= AST_AOC_ENCODED_CHARGE_FREE;
728 case AST_AOC_CHARGE_NA:
729 default:
730 encoded->flags |= AST_AOC_ENCODED_CHARGE_NA;
731 break;
732 }
733
734 if (decoded->total_type == AST_AOC_SUBTOTAL) {
735 encoded->flags |= AST_AOC_ENCODED_CHARGE_SUBTOTAL;
736 }
737 }
738
739 /* --- Set Version Number --- */
740 encoded->version = AST_AOC_ENCODE_VERSION;
741
742 /* set the output size */
743 *out_size = size;
744
745 if (aoc_debug_enabled) {
746 aoc_display_decoded_debug(decoded, 0, chan);
747 }
748
749 return encoded;
750 }
751
752 static int aoc_s_add_entry(struct ast_aoc_decoded *decoded, struct ast_aoc_s_entry *entry)
753 {
754 if (decoded->aoc_s_count >= ARRAY_LEN(decoded->aoc_s_entries)) {
755 return -1;
756 }
757
758 decoded->aoc_s_entries[decoded->aoc_s_count] = *entry;
759 decoded->aoc_s_count++;
760
761 return 0;
762 }
763
764
765 unsigned int ast_aoc_s_get_count(struct ast_aoc_decoded *decoded)
766 {
767 return decoded->aoc_s_count;
768 }
769
770 const struct ast_aoc_s_entry *ast_aoc_s_get_rate_info(struct ast_aoc_decoded *decoded, unsigned int entry_number)
771 {
772 if (entry_number >= decoded->aoc_s_count) {
773 return NULL;
774 }
775
776 return (const struct ast_aoc_s_entry *) &decoded->aoc_s_entries[entry_number];
777 }
778
779 int ast_aoc_s_add_rate_duration(struct ast_aoc_decoded *decoded,
780 enum ast_aoc_s_charged_item charged_item,
781 unsigned int amount,
782 enum ast_aoc_currency_multiplier multiplier,
783 const char *currency_name,
784 unsigned long time,
785 enum ast_aoc_time_scale time_scale,
786 unsigned long granularity_time,
787 enum ast_aoc_time_scale granularity_time_scale,
788 int step_function)
789 {
790
791 struct ast_aoc_s_entry entry = { 0, };
792
793 entry.charged_item = charged_item;
794 entry.rate_type = AST_AOC_RATE_TYPE_DURATION;
795 entry.rate.duration.amount = amount;
796 entry.rate.duration.multiplier = multiplier;
797 entry.rate.duration.time = time;
798 entry.rate.duration.time_scale = time_scale;
799 entry.rate.duration.granularity_time = granularity_time;
800 entry.rate.duration.granularity_time_scale = granularity_time_scale;
801 entry.rate.duration.charging_type = step_function ? 1 : 0;
802
803 if (!ast_strlen_zero(currency_name)) {
804 ast_copy_string(entry.rate.duration.currency_name, currency_name, sizeof(entry.rate.duration.currency_name));
805 }
806
807 return aoc_s_add_entry(decoded, &entry);
808 }
809
810 int ast_aoc_s_add_rate_flat(struct ast_aoc_decoded *decoded,
811 enum ast_aoc_s_charged_item charged_item,
812 unsigned int amount,
813 enum ast_aoc_currency_multiplier multiplier,
814 const char *currency_name)
815 {
816 struct ast_aoc_s_entry entry = { 0, };
817
818 entry.charged_item = charged_item;
819 entry.rate_type = AST_AOC_RATE_TYPE_FLAT;
820 entry.rate.flat.amount = amount;
821 entry.rate.flat.multiplier = multiplier;
822
823 if (!ast_strlen_zero(currency_name)) {
824 ast_copy_string(entry.rate.flat.currency_name, currency_name, sizeof(entry.rate.flat.currency_name));
825 }
826
827 return aoc_s_add_entry(decoded, &entry);
828 }
829
830
831 int ast_aoc_s_add_rate_volume(struct ast_aoc_decoded *decoded,
832 enum ast_aoc_s_charged_item charged_item,
833 enum ast_aoc_volume_unit volume_unit,
834 unsigned int amount,
835 enum ast_aoc_currency_multiplier multiplier,
836 const char *currency_name)
837 {
838 struct ast_aoc_s_entry entry = { 0, };
839
840 entry.charged_item = charged_item;
841 entry.rate_type = AST_AOC_RATE_TYPE_VOLUME;
842 entry.rate.volume.multiplier = multiplier;
843 entry.rate.volume.amount = amount;
844 entry.rate.volume.volume_unit = volume_unit;
845
846 if (!ast_strlen_zero(currency_name)) {
847 ast_copy_string(entry.rate.volume.currency_name, currency_name, sizeof(entry.rate.volume.currency_name));
848 }
849
850 return aoc_s_add_entry(decoded, &entry);
851 }
852
853 int ast_aoc_s_add_rate_special_charge_code(struct ast_aoc_decoded *decoded,
854 enum ast_aoc_s_charged_item charged_item,
855 unsigned int code)
856 {
857 struct ast_aoc_s_entry entry = { 0, };
858
859 entry.charged_item = charged_item;
860 entry.rate_type = AST_AOC_RATE_TYPE_SPECIAL_CODE;
861 entry.rate.special_code = code;
862
863 return aoc_s_add_entry(decoded, &entry);
864 }
865
866 int ast_aoc_s_add_rate_free(struct ast_aoc_decoded *decoded,
867 enum ast_aoc_s_charged_item charged_item,
868 int from_beginning)
869 {
870 struct ast_aoc_s_entry entry = { 0, };
871
872 entry.charged_item = charged_item;
873 entry.rate_type = from_beginning ? AST_AOC_RATE_TYPE_FREE_FROM_BEGINNING : AST_AOC_RATE_TYPE_FREE;
874
875 return aoc_s_add_entry(decoded, &entry);
876 }
877
878 int ast_aoc_s_add_rate_na(struct ast_aoc_decoded *decoded,
879 enum ast_aoc_s_charged_item charged_item)
880 {
881 struct ast_aoc_s_entry entry = { 0, };
882
883 entry.charged_item = charged_item;
884 entry.rate_type = AST_AOC_RATE_TYPE_NA;
885
886 return aoc_s_add_entry(decoded, &entry);
887 }
888
889 int ast_aoc_s_add_special_arrangement(struct ast_aoc_decoded *decoded,
890 unsigned int code)
891 {
892 struct ast_aoc_s_entry entry = { 0, };
893
894 entry.charged_item = AST_AOC_CHARGED_ITEM_SPECIAL_ARRANGEMENT;
895 entry.rate_type = AST_AOC_RATE_TYPE_SPECIAL_CODE;
896 entry.rate.special_code = code;
897
898 return aoc_s_add_entry(decoded, &entry);
899 }
900
901 enum ast_aoc_type ast_aoc_get_msg_type(struct ast_aoc_decoded *decoded)
902 {
903 return decoded->msg_type;
904 }
905
906 enum ast_aoc_charge_type ast_aoc_get_charge_type(struct ast_aoc_decoded *decoded)
907 {
908 return decoded->charge_type;
909 }
910
911 enum ast_aoc_request ast_aoc_get_request(struct ast_aoc_decoded *decoded)
912 {
913 return decoded->request_flag;
914 }
915
916 int ast_aoc_set_total_type(struct ast_aoc_decoded *decoded,
917 const enum ast_aoc_total_type type)
918 {
919 decoded->total_type = type;
920 return 0;
921 }
922
923 enum ast_aoc_total_type ast_aoc_get_total_type(struct ast_aoc_decoded *decoded)
924 {
925 return decoded->total_type;
926 }
927
928 int ast_aoc_set_currency_info(struct ast_aoc_decoded *decoded,
929 const unsigned int amount,
930 const enum ast_aoc_currency_multiplier multiplier,
931 const char *name)
932 {
933
934 if (!ast_strlen_zero(name)) {
935 ast_copy_string(decoded->currency_name, name, sizeof(decoded->currency_name));
936 }
937
938 decoded->currency_amount = amount;
939
940 if (multiplier && (multiplier < AST_AOC_MULT_NUM_ENTRIES)) {
941 decoded->multiplier = multiplier;
942 } else {
943 decoded->multiplier = AST_AOC_MULT_ONE;
944 }
945
946 return 0;
947 }
948
949 unsigned int ast_aoc_get_currency_amount(struct ast_aoc_decoded *decoded)
950 {
951 return decoded->currency_amount;
952 }
953
954 enum ast_aoc_currency_multiplier ast_aoc_get_currency_multiplier(struct ast_aoc_decoded *decoded)
955 {
956 return decoded->multiplier;
957 }
958
959 const char *ast_aoc_get_currency_multiplier_decimal(struct ast_aoc_decoded *decoded)
960 {
961 switch (decoded->multiplier) {
962 case AST_AOC_MULT_ONETHOUSANDTH:
963 return "0.001";
964 case AST_AOC_MULT_ONEHUNDREDTH:
965 return "0.01";
966 case AST_AOC_MULT_ONETENTH:
967 return "0.1";
968 case AST_AOC_MULT_ONE:
969 return "1.0";
970 case AST_AOC_MULT_TEN:
971 return "10.0";
972 case AST_AOC_MULT_HUNDRED:
973 return "100.0";
974 case AST_AOC_MULT_THOUSAND:
975 return "1000.0";
976 default:
977 return "1.0";
978 }
979 }
980
981 const char *ast_aoc_get_currency_name(struct ast_aoc_decoded *decoded)
982 {
983 return decoded->currency_name;
984 }
985
986 int ast_aoc_add_unit_entry(struct ast_aoc_decoded *decoded,
987 const unsigned int amount_is_present,
988 const unsigned int amount,
989 const unsigned int type_is_present,
990 const unsigned int type)
991 {
992 if ((decoded->msg_type == AST_AOC_REQUEST) ||
993 (decoded->unit_count >= ARRAY_LEN(decoded->unit_list))) {
994 return -1;
995 }
996
997 if (!amount_is_present && !type_is_present) {
998 return -1;
999 }
1000
1001 decoded->unit_list[decoded->unit_count].valid_amount = amount_is_present;
1002 if (amount_is_present) {
1003 decoded->unit_list[decoded->unit_count].amount = amount;
1004 } else {
1005 decoded->unit_list[decoded->unit_count].amount = 0;
1006 }
1007
1008 decoded->unit_list[decoded->unit_count].valid_type = type_is_present;
1009 if (type_is_present) {
1010 decoded->unit_list[decoded->unit_count].type = type;
1011 } else {
1012 decoded->unit_list[decoded->unit_count].type = 0;
1013 }
1014 decoded->unit_count++;
1015
1016 return 0;
1017 }
1018
1019 const struct ast_aoc_unit_entry *ast_aoc_get_unit_info(struct ast_aoc_decoded *decoded, unsigned int entry_number)
1020 {
1021 if (entry_number >= decoded->unit_count) {
1022 return NULL;
1023 }
1024
1025 return (const struct ast_aoc_unit_entry *) &decoded->unit_list[entry_number];
1026 }
1027
1028 unsigned int ast_aoc_get_unit_count(struct ast_aoc_decoded *decoded)
1029 {
1030 return decoded->unit_count;
1031 }
1032
1033 int ast_aoc_set_billing_id(struct ast_aoc_decoded *decoded, const enum ast_aoc_billing_id id)
1034 {
1035 if ((id >= AST_AOC_BILLING_NUM_ENTRIES) || (id < AST_AOC_BILLING_NA)) {
1036 return -1;
1037 }
1038
1039 decoded->billing_id = id;
1040
1041 return 0;
1042 }
1043
1044 enum ast_aoc_billing_id ast_aoc_get_billing_id(struct ast_aoc_decoded *decoded)
1045 {
1046 return decoded->billing_id;
1047 }
1048
1049 int ast_aoc_set_association_id(struct ast_aoc_decoded *decoded, const int id)
1050 {
1051 if (decoded->msg_type != AST_AOC_E) {
1052 return -1;
1053 }
1054 memset(&decoded->charging_association, 0, sizeof(decoded->charging_association));
1055 decoded->charging_association.charging_type = AST_AOC_CHARGING_ASSOCIATION_ID;
1056 decoded->charging_association.charge.id = id;
1057 return 0;
1058 }
1059
1060 const struct ast_aoc_charging_association *ast_aoc_get_association_info(struct ast_aoc_decoded *decoded)
1061 {
1062 return &decoded->charging_association;
1063 }
1064
1065 int ast_aoc_set_association_number(struct ast_aoc_decoded *decoded, const char *num, uint8_t plan)
1066 {
1067 if ((decoded->msg_type != AST_AOC_E) || ast_strlen_zero(num)) {
1068 return -1;
1069 }
1070 memset(&decoded->charging_association, 0, sizeof(decoded->charging_association));
1071 decoded->charging_association.charging_type = AST_AOC_CHARGING_ASSOCIATION_NUMBER;
1072 decoded->charging_association.charge.number.plan = plan;
1073 ast_copy_string(decoded->charging_association.charge.number.number, num, sizeof(decoded->charging_association.charge.number.number));
1074
1075 return 0;
1076 }
1077
1078 int ast_aoc_set_termination_request(struct ast_aoc_decoded *decoded)
1079 {
1080 if (decoded->msg_type != AST_AOC_REQUEST) {
1081 return -1;
1082 }
1083 decoded->termination_request = 1;
1084
1085 return 0;
1086 }
1087
1088 int ast_aoc_get_termination_request(struct ast_aoc_decoded *decoded)
1089 {
1090 return decoded->termination_request;
1091 }
1092
1093 /*!
1094 * \internal
1095 * \brief Convert AST_AOC_VOLUME_UNIT to string.
1096 * \since 1.8
1097 *
1098 * \param value Value to convert to string.
1099 *
1100 * \return String equivalent.
1101 */
1102 static const char *aoc_volume_unit_str(enum ast_aoc_volume_unit value)
1103 {
1104 const char *str;
1105
1106 switch (value) {
1107 default:
1108 case AST_AOC_VOLUME_UNIT_OCTET:
1109 str = "Octet";
1110 break;
1111 case AST_AOC_VOLUME_UNIT_SEGMENT:
1112 str = "Segment";
1113 break;
1114 case AST_AOC_VOLUME_UNIT_MESSAGE:
1115 str = "Message";
1116 break;
1117 }
1118 return str;
1119 }
1120
1121 /*!
1122 * \internal
1123 * \brief Convert ast_aoc_charged_item to string.
1124 * \since 1.8
1125 *
1126 * \param value Value to convert to string.
1127 *
1128 * \return String equivalent.
1129 */
1130 static const char *aoc_charged_item_str(enum ast_aoc_s_charged_item value)
1131 {
1132 const char *str;
1133
1134 switch (value) {
1135 default:
1136 case AST_AOC_CHARGED_ITEM_NA:
1137 str = "NotAvailable";
1138 break;
1139 case AST_AOC_CHARGED_ITEM_SPECIAL_ARRANGEMENT:
1140 str = "SpecialArrangement";
1141 break;
1142 case AST_AOC_CHARGED_ITEM_BASIC_COMMUNICATION:
1143 str = "BasicCommunication";
1144 break;
1145 case AST_AOC_CHARGED_ITEM_CALL_ATTEMPT:
1146 str = "CallAttempt";
1147 break;
1148 case AST_AOC_CHARGED_ITEM_CALL_SETUP:
1149 str = "CallSetup";
1150 break;
1151 case AST_AOC_CHARGED_ITEM_USER_USER_INFO:
1152 str = "UserUserInfo";
1153 break;
1154 case AST_AOC_CHARGED_ITEM_SUPPLEMENTARY_SERVICE:
1155 str = "SupplementaryService";
1156 break;
1157 }
1158 return str;
1159 }
1160
1161 /*!
1162 * \internal
1163 * \brief Convert ast_aoc_total_type to string.
1164 * \since 1.8
1165 *
1166 * \param value Value to convert to string.
1167 *
1168 * \return String equivalent.
1169 */
1170 static const char *aoc_type_of_totaling_str(enum ast_aoc_total_type value)
1171 {
1172 const char *str;
1173
1174 switch (value) {
1175 default:
1176 case AST_AOC_SUBTOTAL:
1177 str = "SubTotal";
1178 break;
1179 case AST_AOC_TOTAL:
1180 str = "Total";
1181 break;
1182 }
1183 return str;
1184 }
1185
1186 /*!
1187 * \internal
1188 * \brief Convert ast_aoc_rate_type to string.
1189 * \since 1.8
1190 *
1191 * \param value Value to convert to string.
1192 *
1193 * \return String equivalent.
1194 */
1195 static const char *aoc_rate_type_str(enum ast_aoc_s_rate_type value)
1196 {
1197 const char *str;
1198
1199 switch (value) {
1200 default:
1201 case AST_AOC_RATE_TYPE_NA:
1202 str = "NotAvailable";
1203 break;
1204 case AST_AOC_RATE_TYPE_FREE:
1205 str = "Free";
1206 break;
1207 case AST_AOC_RATE_TYPE_FREE_FROM_BEGINNING:
1208 str = "FreeFromBeginning";
1209 break;
1210 case AST_AOC_RATE_TYPE_DURATION:
1211 str = "Duration";
1212 break;
1213 case AST_AOC_RATE_TYPE_FLAT:
1214 str = "Flat";
1215 break;
1216 case AST_AOC_RATE_TYPE_VOLUME:
1217 str = "Volume";
1218 break;
1219 case AST_AOC_RATE_TYPE_SPECIAL_CODE:
1220 str = "SpecialCode";
1221 break;
1222 }
1223 return str;
1224 }
1225
1226 /*!
1227 * \internal
1228 * \brief Convert AST_AOC_TIME_SCALE to string.
1229 * \since 1.8
1230 *
1231 * \param value Value to convert to string.
1232 *
1233 * \return String equivalent.
1234 */
1235 static const char *aoc_scale_str(enum ast_aoc_time_scale value)
1236 {
1237 const char *str;
1238
1239 switch (value) {
1240 default:
1241 case AST_AOC_TIME_SCALE_HUNDREDTH_SECOND:
1242 str = "OneHundredthSecond";
1243 break;
1244 case AST_AOC_TIME_SCALE_TENTH_SECOND:
1245 str = "OneTenthSecond";
1246 break;
1247 case AST_AOC_TIME_SCALE_SECOND:
1248 str = "Second";
1249 break;
1250 case AST_AOC_TIME_SCALE_TEN_SECOND:
1251 str = "TenSeconds";
1252 break;
1253 case AST_AOC_TIME_SCALE_MINUTE:
1254 str = "Minute";
1255 break;
1256 case AST_AOC_TIME_SCALE_HOUR:
1257 str = "Hour";
1258 break;
1259 case AST_AOC_TIME_SCALE_DAY:
1260 str = "Day";
1261 break;
1262 }
1263 return str;
1264 }
1265
1266 static const char *aoc_charge_type_str(enum ast_aoc_charge_type value)
1267 {
1268 const char *str;
1269
1270 switch (value) {
1271 default:
1272 case AST_AOC_CHARGE_NA:
1273 str = "NotAvailable";
1274 break;
1275 case AST_AOC_CHARGE_FREE:
1276 str = "Free";
1277 break;
1278 case AST_AOC_CHARGE_CURRENCY:
1279 str = "Currency";
1280 break;
1281 case AST_AOC_CHARGE_UNIT:
1282 str = "Units";
1283 break;
1284 }
1285
1286 return str;
1287 }
1288
1289 static const char *aoc_multiplier_str(enum ast_aoc_currency_multiplier mult)
1290 {
1291 switch (mult) {
1292 case AST_AOC_MULT_ONETHOUSANDTH:
1293 return "1/1000";
1294 case AST_AOC_MULT_ONEHUNDREDTH:
1295 return "1/100";
1296 case AST_AOC_MULT_ONETENTH:
1297 return "1/10";
1298 case AST_AOC_MULT_ONE:
1299 return "1";
1300 case AST_AOC_MULT_TEN:
1301 return "10";
1302 case AST_AOC_MULT_HUNDRED:
1303 return "100";
1304 case AST_AOC_MULT_THOUSAND:
1305 return "1000";
1306 case AST_AOC_MULT_NUM_ENTRIES:
1307 break;
1308 }
1309 return "1";
1310 }
1311
1312 static const char *aoc_billingid_str(enum ast_aoc_billing_id billing_id)
1313 {
1314 switch (billing_id) {
1315 case AST_AOC_BILLING_NORMAL:
1316 return "Normal";
1317 case AST_AOC_BILLING_REVERSE_CHARGE:
1318 return "Reverse";
1319 case AST_AOC_BILLING_CREDIT_CARD:
1320 return "CreditCard";
1321 case AST_AOC_BILLING_CALL_FWD_UNCONDITIONAL:
1322 return "CallForwardingUnconditional";
1323 case AST_AOC_BILLING_CALL_FWD_BUSY:
1324 return "CallForwardingBusy";
1325 case AST_AOC_BILLING_CALL_FWD_NO_REPLY:
1326 return "CallForwardingNoReply";
1327 case AST_AOC_BILLING_CALL_DEFLECTION:
1328 return "CallDeflection";
1329 case AST_AOC_BILLING_CALL_TRANSFER:
1330 return "CallTransfer";
1331 case AST_AOC_BILLING_NA:
1332 return "NotAvailable";
1333 case AST_AOC_BILLING_NUM_ENTRIES:
1334 break;
1335 }
1336 return "NotAvailable";
1337 }
1338
1339 int ast_aoc_test_encode_decode_match(struct ast_aoc_decoded *decoded)
1340 {
1341 struct ast_aoc_decoded *new_decoded = NULL;
1342 struct ast_aoc_encoded *encoded = NULL;
1343 size_t size;
1344 int res = 0;
1345
1346 if (!(encoded = ast_aoc_encode(decoded, &size, NULL))) {
1347 return -1;
1348 }
1349
1350 if (!(new_decoded = ast_aoc_decode(encoded, size, NULL))) {
1351 ast_free(encoded);
1352 return -1;
1353 }
1354
1355 if (memcmp(new_decoded, decoded, sizeof(struct ast_aoc_decoded))) {
1356 res = -1;
1357 }
1358
1359 ast_aoc_destroy_decoded(new_decoded);
1360 ast_aoc_destroy_encoded(encoded);
1361 return res;
1362 }
1363
1364 static char *aoc_cli_debug_enable(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1365 {
1366 switch (cmd) {
1367 case CLI_INIT:
1368 e->command = "aoc set debug";
1369 e->usage =
1370 "Usage: 'aoc set debug on' to enable aoc debug, 'aoc set debug off' to disable debug.\n";
1371 return NULL;
1372 case CLI_GENERATE:
1373 return NULL;
1374 case CLI_HANDLER:
1375 if (a->argc != 4) {
1376 return CLI_SHOWUSAGE;
1377 } else if(ast_true(a->argv[3])) {
1378 ast_cli(a->fd, "aoc debug enabled\n");
1379 aoc_debug_enabled = 1;
1380 } else if (ast_false(a->argv[3])) {
1381 ast_cli(a->fd, "aoc debug disabled\n");
1382 aoc_debug_enabled = 0;
1383 } else {
1384 return CLI_SHOWUSAGE;
1385 }
1386 }
1387
1388 return CLI_SUCCESS;
1389 }
1390
1391 /*!
1392 * \internal
1393 * \brief Append the time structure to the event message string.
1394 * \since 1.8
1395 *
1396 * \param msg Event message string being built.
1397 * \param prefix Prefix to add to the amount lines.
1398 * \param name Name of the time structure to convert.
1399 * \param time Data to convert.
1400 * \param scale Data to convert.
1401 */
1402 static void aoc_time_str(struct ast_str **msg, const char *prefix, const char *name, unsigned long time, enum ast_aoc_time_scale scale)
1403 {
1404 ast_str_append(msg, 0, "%s/%s/Length: %lu\r\n", prefix, name, time);
1405 ast_str_append(msg, 0, "%s/%s/Scale: %s\r\n", prefix, name,
1406 aoc_scale_str(scale));
1407 }
1408
1409 /*!
1410 * \internal
1411 * \brief Append the amount structure to the event message string.
1412 * \since 1.8
1413 *
1414 * \param msg Event message string being built.
1415 * \param prefix Prefix to add to the amount lines.
1416 * \param amount Data to convert.
1417 * \param mult to convert
1418 */
1419 static void aoc_amount_str(struct ast_str **msg, const char *prefix, unsigned int amount, enum ast_aoc_currency_multiplier mult)
1420 {
1421 static const char name[] = "Amount";
1422
1423 ast_str_append(msg, 0, "%s/%s/Cost: %u\r\n", prefix, name, amount);
1424 ast_str_append(msg, 0, "%s/%s/Multiplier: %s\r\n", prefix, name,
1425 aoc_multiplier_str(mult));
1426 }
1427
1428 static void aoc_request_event(const struct ast_aoc_decoded *decoded, struct ast_str **msg)
1429 {
1430 if (decoded->request_flag) {
1431 ast_str_append(msg, 0, "AOCRequest:");
1432 if (decoded->request_flag & AST_AOC_REQUEST_S) {
1433 ast_str_append(msg, 0, "S");
1434 }
1435 if (decoded->request_flag & AST_AOC_REQUEST_D) {
1436 ast_str_append(msg, 0, "D");
1437 }
1438 if (decoded->request_flag & AST_AOC_REQUEST_E) {
1439 ast_str_append(msg, 0, "E");
1440 }
1441 ast_str_append(msg, 0, "\r\n");
1442
1443 } else {
1444 ast_str_append(msg, 0, "AOCRequest: NONE\r\n");
1445 }
1446 }
1447
1448 static void aoc_s_event(const struct ast_aoc_decoded *decoded, struct ast_str **msg)
1449 {
1450 const char *rate_str;
1451 char prefix[32];
1452 int idx;
1453
1454 ast_str_append(msg, 0, "NumberRates: %d\r\n", decoded->aoc_s_count);
1455 for (idx = 0; idx < decoded->aoc_s_count; ++idx) {
1456 snprintf(prefix, sizeof(prefix), "Rate(%d)", idx);
1457
1458 ast_str_append(msg, 0, "%s/Chargeable: %s\r\n", prefix,
1459 aoc_charged_item_str(decoded->aoc_s_entries[idx].charged_item));
1460 if (decoded->aoc_s_entries[idx].charged_item == AST_AOC_CHARGED_ITEM_NA) {
1461 continue;
1462 }
1463 rate_str = aoc_rate_type_str(decoded->aoc_s_entries[idx].rate_type);
1464 ast_str_append(msg, 0, "%s/Type: %s\r\n", prefix, rate_str);
1465 switch (decoded->aoc_s_entries[idx].rate_type) {
1466 case AST_AOC_RATE_TYPE_DURATION:
1467 strcat(prefix, "/");
1468 strcat(prefix, rate_str);
1469 ast_str_append(msg, 0, "%s/Currency: %s\r\n", prefix,
1470 decoded->aoc_s_entries[idx].rate.duration.currency_name);
1471 aoc_amount_str(msg, prefix,
1472 decoded->aoc_s_entries[idx].rate.duration.amount,
1473 decoded->aoc_s_entries[idx].rate.duration.multiplier);
1474 ast_str_append(msg, 0, "%s/ChargingType: %s\r\n", prefix,
1475 decoded->aoc_s_entries[idx].rate.duration.charging_type ?
1476 "StepFunction" : "ContinuousCharging");
1477 aoc_time_str(msg, prefix, "Time",
1478 decoded->aoc_s_entries[idx].rate.duration.time,
1479 decoded->aoc_s_entries[idx].rate.duration.time_scale);
1480 if (decoded->aoc_s_entries[idx].rate.duration.granularity_time) {
1481 aoc_time_str(msg, prefix, "Granularity",
1482 decoded->aoc_s_entries[idx].rate.duration.granularity_time,
1483 decoded->aoc_s_entries[idx].rate.duration.granularity_time_scale);
1484 }
1485 break;
1486 case AST_AOC_RATE_TYPE_FLAT:
1487 strcat(prefix, "/");
1488 strcat(prefix, rate_str);
1489 ast_str_append(msg, 0, "%s/Currency: %s\r\n", prefix,
1490 decoded->aoc_s_entries[idx].rate.flat.currency_name);
1491 aoc_amount_str(msg, prefix,
1492 decoded->aoc_s_entries[idx].rate.flat.amount,
1493 decoded->aoc_s_entries[idx].rate.flat.multiplier);
1494 break;
1495 case AST_AOC_RATE_TYPE_VOLUME:
1496 strcat(prefix, "/");
1497 strcat(prefix, rate_str);
1498 ast_str_append(msg, 0, "%s/Currency: %s\r\n", prefix,
1499 decoded->aoc_s_entries[idx].rate.volume.currency_name);
1500 aoc_amount_str(msg, prefix,
1501 decoded->aoc_s_entries[idx].rate.volume.amount,
1502 decoded->aoc_s_entries[idx].rate.volume.multiplier);
1503 ast_str_append(msg, 0, "%s/Unit: %s\r\n", prefix,
1504 aoc_volume_unit_str(decoded->aoc_s_entries[idx].rate.volume.volume_unit));
1505 break;
1506 case AST_AOC_RATE_TYPE_SPECIAL_CODE:
1507 ast_str_append(msg, 0, "%s/%s: %d\r\n", prefix, rate_str,
1508 decoded->aoc_s_entries[idx].rate.special_code);
1509 break;
1510 default:
1511 break;
1512 }
1513 }
1514 }
1515
1516 static void aoc_d_event(const struct ast_aoc_decoded *decoded, struct ast_str **msg)
1517 {
1518 const char *charge_str;
1519 int idx;
1520 char prefix[32];
1521
1522 charge_str = aoc_charge_type_str(decoded->charge_type);
1523 ast_str_append(msg, 0, "Type: %s\r\n", charge_str);
1524
1525 switch (decoded->charge_type) {
1526 case AST_AOC_CHARGE_CURRENCY:
1527 case AST_AOC_CHARGE_UNIT:
1528 ast_str_append(msg, 0, "BillingID: %s\r\n",
1529 aoc_billingid_str(decoded->billing_id));
1530 ast_str_append(msg, 0, "TypeOfCharging: %s\r\n",
1531 aoc_type_of_totaling_str(decoded->total_type));
1532 break;
1533 default:
1534 break;
1535 }
1536
1537 switch (decoded->charge_type) {
1538 case AST_AOC_CHARGE_CURRENCY:
1539 ast_str_append(msg, 0, "%s: %s\r\n", charge_str,
1540 decoded->currency_name);
1541 aoc_amount_str(msg, charge_str,
1542 decoded->currency_amount,
1543 decoded->multiplier);
1544 break;
1545 case AST_AOC_CHARGE_UNIT:
1546 ast_str_append(msg, 0, "%s/NumberItems: %d\r\n", charge_str,
1547 decoded->unit_count);
1548 for (idx = 0; idx < decoded->unit_count; ++idx) {
1549 snprintf(prefix, sizeof(prefix), "%s/Item(%d)", charge_str, idx);
1550 if (decoded->unit_list[idx].valid_amount) {
1551 ast_str_append(msg, 0, "%s/NumberOf: %u\r\n", prefix,
1552 decoded->unit_list[idx].amount);
1553 }
1554 if (decoded->unit_list[idx].valid_type) {
1555 ast_str_append(msg, 0, "%s/TypeOf: %u\r\n", prefix,
1556 decoded->unit_list[idx].type);
1557 }
1558 }
1559 break;
1560 default:
1561 break;
1562 }
1563 }
1564
1565 static void aoc_e_event(const struct ast_aoc_decoded *decoded, struct ast_str **msg)
1566 {
1567 const char *charge_str;
1568 int idx;
1569 char prefix[32];
1570
1571 charge_str = "ChargingAssociation";
1572
1573 switch (decoded->charging_association.charging_type) {
1574 case AST_AOC_CHARGING_ASSOCIATION_NUMBER:
1575 snprintf(prefix, sizeof(prefix), "%s/Number", charge_str);
1576 ast_str_append(msg, 0, "%s: %s\r\n", prefix,
1577 decoded->charging_association.charge.number.number);
1578 ast_str_append(msg, 0, "%s/Plan: %d\r\n", prefix,
1579 decoded->charging_association.charge.number.plan);
1580 break;
1581 case AST_AOC_CHARGING_ASSOCIATION_ID:
1582 ast_str_append(msg, 0, "%s/ID: %d\r\n", charge_str, decoded->charging_association.charge.id);
1583 break;
1584 case AST_AOC_CHARGING_ASSOCIATION_NA:
1585 default:
1586 break;
1587 }
1588
1589 charge_str = aoc_charge_type_str(decoded->charge_type);
1590 ast_str_append(msg, 0, "Type: %s\r\n", charge_str);
1591 switch (decoded->charge_type) {
1592 case AST_AOC_CHARGE_CURRENCY:
1593 case AST_AOC_CHARGE_UNIT:
1594 ast_str_append(msg, 0, "BillingID: %s\r\n",
1595 aoc_billingid_str(decoded->billing_id));
1596 break;
1597 default:
1598 break;
1599 }
1600 switch (decoded->charge_type) {
1601 case AST_AOC_CHARGE_CURRENCY:
1602 ast_str_append(msg, 0, "%s: %s\r\n", charge_str,
1603 decoded->currency_name);
1604 aoc_amount_str(msg, charge_str,
1605 decoded->currency_amount,
1606 decoded->multiplier);
1607 break;
1608 case AST_AOC_CHARGE_UNIT:
1609 ast_str_append(msg, 0, "%s/NumberItems: %d\r\n", charge_str,
1610 decoded->unit_count);
1611 for (idx = 0; idx < decoded->unit_count; ++idx) {
1612 snprintf(prefix, sizeof(prefix), "%s/Item(%d)", charge_str, idx);
1613 if (decoded->unit_list[idx].valid_amount) {
1614 ast_str_append(msg, 0, "%s/NumberOf: %u\r\n", prefix,
1615 decoded->unit_list[idx].amount);
1616 }
1617 if (decoded->unit_list[idx].valid_type) {
1618 ast_str_append(msg, 0, "%s/TypeOf: %u\r\n", prefix,
1619 decoded->unit_list[idx].type);
1620 }
1621 }
1622 break;
1623 default:
1624 break;
1625 }
1626 }
1627
1628 static struct ast_json *units_to_json(const struct ast_aoc_decoded *decoded)
1629 {
1630 int i;
1631 struct ast_json *units = ast_json_array_create();
1632
1633 if (!units) {
1634 return ast_json_null();
1635 }
1636
1637 for (i = 0; i < decoded->unit_count; ++i) {
1638 struct ast_json *unit = ast_json_object_create();
1639
1640 if (decoded->unit_list[i].valid_amount) {
1641 ast_json_object_set(
1642 unit, "NumberOf", ast_json_stringf(
1643 "%u", decoded->unit_list[i].amount));
1644 }
1645
1646 if (decoded->unit_list[i].valid_type) {
1647 ast_json_object_set(
1648 unit, "TypeOf", ast_json_stringf(
1649 "%u", decoded->unit_list[i].type));
1650 }
1651
1652 if (ast_json_array_append(units, unit)) {
1653 break;
1654 }
1655 }
1656
1657 return units;
1658 }
1659
1660 static struct ast_json *currency_to_json(const char *name, int cost,
1661 enum ast_aoc_currency_multiplier mult)
1662 {
1663 return ast_json_pack("{s:s, s:i, s:s}",
1664 "Name", AST_JSON_UTF8_VALIDATE(name),
1665 "Cost", cost,
1666 "Multiplier", aoc_multiplier_str(mult));
1667 }
1668
1669 static struct ast_json *charge_to_json(const struct ast_aoc_decoded *decoded)
1670 {
1671 struct ast_json *obj;
1672 const char *obj_type;
1673
1674 if (decoded->charge_type != AST_AOC_CHARGE_CURRENCY &&
1675 decoded->charge_type != AST_AOC_CHARGE_UNIT) {
1676 return ast_json_pack("{s:s}",
1677 "Type", aoc_charge_type_str(decoded->charge_type));
1678 }
1679
1680 if (decoded->charge_type == AST_AOC_CHARGE_CURRENCY) {
1681 obj_type = "Currency";
1682 obj = currency_to_json(decoded->currency_name, decoded->currency_amount,
1683 decoded->multiplier);
1684 } else { /* decoded->charge_type == AST_AOC_CHARGE_UNIT */
1685 obj_type = "Units";
1686 obj = units_to_json(decoded);
1687 }
1688
1689 return ast_json_pack("{s:s, s:s, s:s, s:o}",
1690 "Type", aoc_charge_type_str(decoded->charge_type),
1691 "BillingID", aoc_billingid_str(decoded->billing_id),
1692 "TotalType", aoc_type_of_totaling_str(decoded->total_type),
1693 obj_type, obj);
1694 }
1695
1696 static struct ast_json *association_to_json(const struct ast_aoc_decoded *decoded)
1697 {
1698 switch (decoded->charging_association.charging_type) {
1699 case AST_AOC_CHARGING_ASSOCIATION_NUMBER:
1700 return ast_json_pack("{s:s, s:i}",
1701 "Number", AST_JSON_UTF8_VALIDATE(decoded->charging_association.charge.number.number),
1702 "Plan", decoded->charging_association.charge.number.plan);
1703 case AST_AOC_CHARGING_ASSOCIATION_ID:
1704 return ast_json_pack("{s:i}", "ID", decoded->charging_association.charge.id);
1705 case AST_AOC_CHARGING_ASSOCIATION_NA:
1706 default:
1707 return ast_json_null();
1708 }
1709 }
1710
1711 static struct ast_json *s_to_json(const struct ast_aoc_decoded *decoded)
1712 {
1713 int i;
1714 struct ast_json *rates = ast_json_array_create();
1715
1716 if (!rates) {
1717 return ast_json_null();
1718 }
1719
1720 for (i = 0; i < decoded->aoc_s_count; ++i) {
1721 struct ast_json *rate;
1722 struct ast_json *type = NULL;
1723 struct ast_json *currency;
1724 const char *charge_item = aoc_charged_item_str(
1725 decoded->aoc_s_entries[i].charged_item);
1726
1727 if (decoded->aoc_s_entries[i].charged_item == AST_AOC_CHARGED_ITEM_NA) {
1728 rate = ast_json_pack("{s:s}", "Chargeable", charge_item);
1729 if (ast_json_array_append(rates, rate)) {
1730 break;
1731 }
1732 continue;
1733 }
1734
1735 switch (decoded->aoc_s_entries[i].rate_type) {
1736 case AST_AOC_RATE_TYPE_DURATION:
1737 {
1738 struct ast_json *time;
1739 struct ast_json *granularity = NULL;
1740
1741 currency = currency_to_json(
1742 decoded->aoc_s_entries[i].rate.duration.currency_name,
1743 decoded->aoc_s_entries[i].rate.duration.amount,
1744 decoded->aoc_s_entries[i].rate.duration.multiplier);
1745
1746 time = ast_json_pack("{s:I, s:i}",
1747 "Length", (ast_json_int_t)decoded->aoc_s_entries[i].rate.duration.time,
1748 "Scale", decoded->aoc_s_entries[i].rate.duration.time_scale);
1749
1750 if (decoded->aoc_s_entries[i].rate.duration.granularity_time) {
1751 granularity = ast_json_pack("{s:I, s:i}",
1752 "Length", (ast_json_int_t)decoded->aoc_s_entries[i].rate.duration.granularity_time,
1753 "Scale", decoded->aoc_s_entries[i].rate.duration.granularity_time_scale);
1754 }
1755
1756 type = ast_json_pack("{s:o, s:s, s:o, s:o}",
1757 "Currency", currency,
1758 "ChargingType", decoded->aoc_s_entries[i].rate.duration.charging_type
1759 ? "StepFunction" : "ContinuousCharging",
1760 "Time", time,
1761 "Granularity", granularity ?: ast_json_null());
1762
1763 break;
1764 }
1765 case AST_AOC_RATE_TYPE_FLAT:
1766 currency = currency_to_json(
1767 decoded->aoc_s_entries[i].rate.flat.currency_name,
1768 decoded->aoc_s_entries[i].rate.flat.amount,
1769 decoded->aoc_s_entries[i].rate.flat.multiplier);
1770
1771 type = ast_json_pack("{s:o}", "Currency", currency);
1772 break;
1773 case AST_AOC_RATE_TYPE_VOLUME:
1774 currency = currency_to_json(
1775 decoded->aoc_s_entries[i].rate.volume.currency_name,
1776 decoded->aoc_s_entries[i].rate.volume.amount,
1777 decoded->aoc_s_entries[i].rate.volume.multiplier);
1778
1779 type = ast_json_pack("{s:s, s:o}",
1780 "Unit", aoc_volume_unit_str(
1781 decoded->aoc_s_entries[i].rate.volume.volume_unit),
1782 "Currency", currency);
1783 break;
1784 case AST_AOC_RATE_TYPE_SPECIAL_CODE:
1785 type = ast_json_pack("{s:i}",
1786 "SpecialCode", decoded->aoc_s_entries[i].rate.special_code);
1787 break;
1788 default:
1789 break;
1790 }
1791
1792 rate = ast_json_pack("{s:s, s:o}",
1793 "Chargeable", charge_item,
1794 aoc_rate_type_str(decoded->aoc_s_entries[i].rate_type), type);
1795 if (ast_json_array_append(rates, rate)) {
1796 break;
1797 }
1798 }
1799 return rates;
1800 }
1801
1802 static struct ast_json *d_to_json(const struct ast_aoc_decoded *decoded)
1803 {
1804 return ast_json_pack("{s:o}", "Charge", charge_to_json(decoded));
1805 }
1806
1807 static struct ast_json *e_to_json(const struct ast_aoc_decoded *decoded)
1808 {
1809 return ast_json_pack("{s:o, s:o}",
1810 "ChargingAssociation", association_to_json(decoded),
1811 "Charge", charge_to_json(decoded));
1812 }
1813
1814 struct aoc_event_blob {
1815 /*! Channel AOC event is associated with (NULL for unassociated) */
1816 struct ast_channel_snapshot *snapshot;
1817 /*! AOC JSON blob of data */
1818 struct ast_json *blob;
1819 };
1820
1821 static void aoc_event_blob_dtor(void *obj)
1822 {
1823 struct aoc_event_blob *aoc_event = obj;
1824
1825 ao2_cleanup(aoc_event->snapshot);
1826 ast_json_unref(aoc_event->blob);
1827 }
1828
1829 /*!
1830 * \internal
1831 * \brief Publish an AOC event.
1832 * \since 13.3.0
1833 *
1834 * \param chan Channel associated with the AOC event. (May be NULL if no channel)
1835 * \param msg_type What kind of AOC event.
1836 * \param blob AOC data blob to publish.
1837 */
1838 static void aoc_publish_blob(struct ast_channel *chan, struct stasis_message_type *msg_type, struct ast_json *blob)
1839 {
1840 struct stasis_message *msg;
1841 struct aoc_event_blob *aoc_event;
1842
1843 if (!blob || ast_json_is_null(blob)) {
1844 /* No AOC blob information? Nothing to send an event about. */
1845 return;
1846 }
1847
1848 aoc_event = ao2_alloc_options(sizeof(*aoc_event), aoc_event_blob_dtor,
1849 AO2_ALLOC_OPT_LOCK_NOLOCK);
1850 if (!aoc_event) {
1851 return;
1852 }
1853
1854 if (chan) {
1855 ast_channel_lock(chan);
1856 aoc_event->snapshot = ao2_bump(ast_channel_snapshot(chan));
1857 ast_channel_unlock(chan);
1858 if (!aoc_event->snapshot) {
1859 ao2_ref(aoc_event, -1);
1860 return;
1861 }
1862 }
1863 aoc_event->blob = ast_json_ref(blob);
1864
1865 msg = stasis_message_create(msg_type, aoc_event);
1866 ao2_ref(aoc_event, -1);
1867
1868 if (msg) {
1869 stasis_publish(ast_manager_get_topic(), msg);
1870 ao2_ref(msg, -1);
1871 }
1872 }
1873
1874 static struct ast_manager_event_blob *aoc_to_ami(struct stasis_message *message,
1875 const char *event_name)
1876 {
1877 struct aoc_event_blob *aoc_event = stasis_message_data(message);
1878 struct ast_str *channel = NULL;
1879 struct ast_str *aoc;
1880 struct ast_manager_event_blob *ev = NULL;
1881
1882 if (aoc_event->snapshot) {
1883 channel = ast_manager_build_channel_state_string(aoc_event->snapshot);
1884 if (!channel) {
1885 return NULL;
1886 }
1887 }
1888
1889 aoc = ast_manager_str_from_json_object(aoc_event->blob, NULL);
1890 if (aoc && !ast_strlen_zero(ast_str_buffer(aoc))) {
1891 ev = ast_manager_event_blob_create(EVENT_FLAG_AOC, event_name, "%s%s",
1892 AS_OR(channel, ""), ast_str_buffer(aoc));
1893 }
1894
1895 ast_free(aoc);
1896 ast_free(channel);
1897 return ev;
1898 }
1899
1900 static struct ast_manager_event_blob *aoc_s_to_ami(struct stasis_message *message)
1901 {
1902 return aoc_to_ami(message, "AOC-S");
1903 }
1904
1905 static struct ast_manager_event_blob *aoc_d_to_ami(struct stasis_message *message)
1906 {
1907 return aoc_to_ami(message, "AOC-D");
1908 }
1909
1910 static struct ast_manager_event_blob *aoc_e_to_ami(struct stasis_message *message)
1911 {
1912 return aoc_to_ami(message, "AOC-E");
1913 }
1914
1915 struct stasis_message_type *aoc_s_type(void);
1916 struct stasis_message_type *aoc_d_type(void);
1917 struct stasis_message_type *aoc_e_type(void);
1918
1919 STASIS_MESSAGE_TYPE_DEFN(
1920 aoc_s_type,
1921 .to_ami = aoc_s_to_ami);
1922
1923 STASIS_MESSAGE_TYPE_DEFN(
1924 aoc_d_type,
1925 .to_ami = aoc_d_to_ami);
1926
1927 STASIS_MESSAGE_TYPE_DEFN(
1928 aoc_e_type,
1929 .to_ami = aoc_e_to_ami);
1930
1931 int ast_aoc_manager_event(const struct ast_aoc_decoded *decoded, struct ast_channel *chan)
1932 {
1933 struct ast_json *blob;
1934 struct stasis_message_type *msg_type;
1935
1936 if (!decoded) {
1937 return -1;
1938 }
1939
1940 switch (decoded->msg_type) {
1941 case AST_AOC_S:
1942 blob = s_to_json(decoded);
1943 msg_type = aoc_s_type();
1944 break;
1945 case AST_AOC_D:
1946 blob = d_to_json(decoded);
1947 msg_type = aoc_d_type();
1948 break;
1949 case AST_AOC_E:
1950 blob = e_to_json(decoded);
1951 msg_type = aoc_e_type();
1952 break;
1953 default:
1954 /* events for AST_AOC_REQUEST are not generated here */
1955 return 0;
1956 }
1957
1958 aoc_publish_blob(chan, msg_type, blob);
1959 ast_json_unref(blob);
1960 return 0;
1961 }
1962
1963 int ast_aoc_decoded2str(const struct ast_aoc_decoded *decoded, struct ast_str **msg)
1964 {
1965 if (!decoded || !msg) {
1966 return -1;
1967 }
1968
1969 switch (decoded->msg_type) {
1970 case AST_AOC_S:
1971 ast_str_append(msg, 0, "AOC-S\r\n");
1972 aoc_s_event(decoded, msg);
1973 break;
1974 case AST_AOC_D:
1975 ast_str_append(msg, 0, "AOC-D\r\n");
1976 aoc_d_event(decoded, msg);
1977 break;
1978 case AST_AOC_E:
1979 ast_str_append(msg, 0, "AOC-E\r\n");
1980 aoc_e_event(decoded, msg);
1981 break;
1982 case AST_AOC_REQUEST:
1983 ast_str_append(msg, 0, "AOC-Request\r\n");
1984 aoc_request_event(decoded, msg);
1985 break;
1986 }
1987
1988 return 0;
1989 }
1990
1991 static void aoc_display_decoded_debug(const struct ast_aoc_decoded *decoded, int decoding, struct ast_channel *chan)
1992 {
1993 struct ast_str *msg;
1994
1995 if (!decoded || !(msg = ast_str_create(1024))) {
1996 return;
1997 }
1998
1999 if (decoding) {
2000 ast_str_append(&msg, 0, "---- DECODED AOC MSG ----\r\n");
2001 } else {
2002 ast_str_append(&msg, 0, "---- ENCODED AOC MSG ----\r\n");
2003 }
2004 if (chan) {
2005 ast_str_append(&msg, 0, "CHANNEL: %s\r\n", ast_channel_name(chan));
2006 }
2007
2008 if (ast_aoc_decoded2str(decoded, &msg)) {
2009 ast_free(msg);
2010 return;
2011 }
2012
2013 ast_verb(1, "%s\r\n", ast_str_buffer(msg));
2014 ast_free(msg);
2015 }
2016
2017 static struct ast_cli_entry aoc_cli[] = {
2018 AST_CLI_DEFINE(aoc_cli_debug_enable, "enable cli debugging of AOC messages"),
2019 };
2020
2021 static void aoc_shutdown(void)
2022 {
2023 STASIS_MESSAGE_TYPE_CLEANUP(aoc_s_type);
2024 STASIS_MESSAGE_TYPE_CLEANUP(aoc_d_type);
2025 STASIS_MESSAGE_TYPE_CLEANUP(aoc_e_type);
2026
2027 ast_cli_unregister_multiple(aoc_cli, ARRAY_LEN(aoc_cli));
2028 }
2029 int ast_aoc_cli_init(void)
2030 {
2031 STASIS_MESSAGE_TYPE_INIT(aoc_s_type);
2032 STASIS_MESSAGE_TYPE_INIT(aoc_d_type);
2033 STASIS_MESSAGE_TYPE_INIT(aoc_e_type);
2034
2035 ast_register_cleanup(aoc_shutdown);
2036 return ast_cli_register_multiple(aoc_cli, ARRAY_LEN(aoc_cli));
2037 }