Asterisk - The Open Source Telephony Project GIT-master-80b953f
Loading...
Searching...
No Matches
cdrel.h
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2026, Sangoma Technologies Corporation
5 *
6 * George Joseph <gjoseph@sangoma.com>
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 * \author George Joseph <gjoseph@sangoma.com>
22 *
23 * \brief Private header for res_cdrel_custom.
24 *
25 */
26
27#ifndef _CDREL_H
28#define _CDREL_H
29
30#include <sqlite3.h>
31
32#include "asterisk.h"
33#include "asterisk/cdr.h"
34#include "asterisk/cel.h"
35#include "asterisk/event.h"
36#include "asterisk/lock.h"
37#include "asterisk/strings.h"
38#include "asterisk/vector.h"
40
41extern const char *cdrel_record_type_map[];
42#define RECORD_TYPE_STR(_rt) (cdrel_record_type_map[_rt])
43
44extern const char *cdrel_module_type_map[];
45#define MODULE_TYPE_STR(_mt) (cdrel_module_type_map[_mt])
46
53
59
67
68/*
69 * ORDER IS IMPORTANT!
70 * The string output data types need to be first.
71 */
90
91extern const char *cdrel_data_type_map[];
92#define DATA_TYPE_STR(_dt) (_dt < cdrel_data_type_end ? cdrel_data_type_map[_dt] : NULL)
94
95#define CDREL_FIELD_FLAG_QUOTE (0)
96#define CDREL_FIELD_FLAG_NOQUOTE (1)
97#define CDREL_FIELD_FLAG_TYPE_FORCED (2)
98#define CDREL_FIELD_FLAG_USERVAR (3)
99#define CDREL_FIELD_FLAG_LITERAL (4)
100#define CDREL_FIELD_FLAG_FORMAT_SPEC (5)
101#define CDREL_FIELD_FLAG_LAST (6)
102
112
113/*
114 * CEL has a few synthetic fields that aren't defined
115 * in event.h so we'll define them ourselves after the
116 * last official id.
117 */
118#define AST_EVENT_IE_CEL_LITERAL (AST_EVENT_IE_TOTAL + 1)
119#define AST_EVENT_IE_CEL_EVENT_ENUM (AST_EVENT_IE_TOTAL + 2)
120#define LAST_CEL_ID AST_EVENT_IE_CEL_EVENT_ENUM
121
122/*!
123 * While CEL event fields are published as a generic AST_EVENT with
124 * a field id assigned to each field, CDRs are published as a fixed
125 * ast_cdr structure. To make it easier to share lower level code,
126 * we assign pseudo-field-ids to each CDR field that are really the offset
127 * of the field in the structure. This allows us to generically get
128 * any field using its id just like we do for CEL.
129 *
130 * To avoid conflicts with the existing CEL field ids, we'll start these
131 * after the last one.
132 */
133#define CDR_OFFSET_SHIFT (LAST_CEL_ID + 1)
134#define CDR_OFFSETOF(_field) (offsetof(struct ast_cdr, _field) + CDR_OFFSET_SHIFT)
135#define CDR_FIELD(__cdr, __offset) (((void *)__cdr) + __offset - CDR_OFFSET_SHIFT)
136
165
166
167struct cdrel_field;
168
169/*!
170 * \internal
171 * \brief A generic value wrapper structure.
172 */
177 union {
178 char *string;
179 int32_t int32;
180 uint32_t uint32;
181 int64_t int64;
182 uint64_t uint64;
183 struct timeval tv;
184 double doubler;
186};
187
188/*!
189 * \internal
190 * \brief A vector of cdrel_values.
191 */
193
194/*!
195 * \internal
196 * \brief Getter callbacks.
197 *
198 * \param record An ast_cdr or ast_event structure.
199 * \param config Config object.
200 * \param field Field object.
201 * \param value A pointer to a cdrel_value structure to populate.
202 * \retval 0 on success.
203 * \retval -1 on failure.
204 */
205typedef int (*cdrel_field_getter)(void *record, struct cdrel_config *config,
206 struct cdrel_field *field, struct cdrel_value *value);
207/*!
208 * \internal
209 * \brief The table of getter callbacks. Populated by getters_cdr.c and getters_cel.c.
210 *
211 * Defined in res_cdrel_custom.c
212 */
214
215/*!
216 * \internal
217 * \brief Data type formatters.
218 *
219 * \param config Config object.
220 * \param field Field object.
221 * \param input_value A pointer to a cdrel_value structure with the data to format.
222 * \param output_value A pointer to a cdrel_value structure to receive the formatted data.
223 * \retval 0 on success.
224 * \retval -1 on failure.
225 */
227 struct cdrel_field *field, struct cdrel_value *input_value, struct cdrel_value *output_value);
228/*!
229 * \internal
230 * \brief The table of formatter callbacks. Populated by formatters.c.
231 *
232 * Defined in res_cdrel_custom.c
233 */
235
236/*!
237 * \internal
238 * \brief Backend writers.
239 *
240 * \param config Config object.
241 * \param values A vector of cdrel_values to write.
242 * \retval 0 on success.
243 * \retval -1 on failure.
244 */
245typedef int (*cdrel_backend_writer)(struct cdrel_config *config, struct cdrel_values *values);
246/*!
247 * \internal
248 * \brief The table of writer callbacks. Populated by writers.c.
249 *
250 * Defined in res_cdrel_custom.c
251 */
253
254/*!
255 * \internal
256 * \brief Dummy channel allocators.
257 *
258 * Legacy configurations use dialplan functions so they need a dummy channel
259 * to operate on. CDR and CEL each have their own allocator.
260 *
261 * \param config Config object.
262 * \param record An ast_cdr or ast_event structure.
263 * \return an ast_channel or NULL on failure.
264 */
265typedef struct ast_channel * (*cdrel_dummy_channel_alloc)(struct cdrel_config *config, void *record);
267
268
269/*! Represents a field definition */
271 enum cdrel_record_type record_type; /*!< CDR or CEL */
272 int field_id; /*!< May be an AST_EVENT_IE_CEL or a cdr_field_id */
273 char *name; /*!< The name of the field */
274 enum cdrel_data_type input_data_type; /*!< The data type of the field in the source record */
276 struct ast_flags flags; /*!< Flags used during config parsing */
277 char data[1]; /*!< Could be literal data, a user variable name, etc */
278};
279
280/*! Represents an output definition from a config file */
282 enum cdrel_record_type record_type; /*!< CDR or CEL */
284 AST_STRING_FIELD(config_filename); /*!< Input configuration filename */
285 AST_STRING_FIELD(output_filename); /*!< Output text file or database */
286 AST_STRING_FIELD(template); /*!< Input template */
287 AST_STRING_FIELD(db_columns); /*!< List of columns for database backends */
288 AST_STRING_FIELD(db_table); /*!< Table name for database backends */
289 );
290 sqlite3 *db; /*!< sqlite3 database handle */
291 sqlite3_stmt *insert; /*!< sqlite3 prepared statement for insert */
292 int busy_timeout; /*!< sqlite3 query timeout value */
293 cdrel_dummy_channel_alloc dummy_channel_alloc; /*!< Legacy config types need a dummy channel */
294 enum cdrel_backend_type backend_type; /*!< Text file or database */
295 enum cdrel_config_type config_type; /*!< Legacy or advanced */
296 enum cdrel_text_format_type format_type; /*!< For text files, CSV or JSON */
297 enum cdrel_quoting_method quoting_method; /*!< When to quote */
298 char separator[2]; /*!< For text files, the field separator */
299 char quote[2]; /*!< For text files, the quote character */
300 char quote_escape[2]; /*!< For text files, character to use to escape embedded quotes */
301 AST_VECTOR(, struct cdrel_field *) fields; /*!< Vector of fields for this config */
302 ast_mutex_t lock; /*!< Lock that serializes filesystem writes */
303};
304
305/*!
306 * \internal
307 * \brief Get a cdrel_field structure by record type and field name.
308 *
309 * \param record_type The cdrel_record_type to search.
310 * \param name The field name to search for.
311 * \returns A pointer to a constant cdrel_field structure or NULL if not found.
312 * This pointer must never be freed.
313 */
315 const char *name);
316
317/*!
318 * \internal
319 * \brief Write a record to a text file
320 *
321 * \param config The configuration object.
322 * \param record The data to write.
323 * \retval 0 on success
324 * \retval -1 on failure
325 */
326int write_record_to_file(struct cdrel_config *config, struct ast_str *record);
327
328/*!
329 * \internal
330 * \brief Write a record to a database
331 *
332 * \param config The configuration object.
333 * \param values The values to write.
334 * \retval 0 on success
335 * \retval -1 on failure
336 */
337int write_record_to_database(struct cdrel_config *config, struct cdrel_values *values);
338
339/*!
340 * \internal
341 * \brief Return the basename of a path
342 *
343 * \param path
344 * \returns Pointer to last '/' or path if no '/' was found.
345 */
346const char *cdrel_basename(const char *path);
347
348/*!
349 * \internal
350 * \brief Get a string representing a field's flags
351 *
352 * \param flags An ast_flags structure
353 * \param str Pointer to ast_str* buffer
354 * \returns A string of field flag names
355 */
356const char *cdrel_get_field_flags(struct ast_flags *flags, struct ast_str **str);
357
358
359int load_cdr(void);
360int load_cel(void);
361int load_formatters(void);
362int load_writers(void);
363
364#endif /* _CDREL_H */
const char * str
Definition app_jack.c:150
Asterisk main include file. File version handling, generic pbx functions.
Call Detail Record API.
int load_cel(void)
const char * cdrel_basename(const char *path)
int(* cdrel_field_formatter)(struct cdrel_config *config, struct cdrel_field *field, struct cdrel_value *input_value, struct cdrel_value *output_value)
Definition cdrel.h:226
#define CDREL_FIELD_FLAG_USERVAR
Definition cdrel.h:98
int write_record_to_database(struct cdrel_config *config, struct cdrel_values *values)
Definition writers.c:169
cdr_field_id
Definition cdrel.h:137
@ cdr_field_peeraccount
Definition cdrel.h:155
@ cdr_field_amaflags
Definition cdrel.h:153
@ cdr_field_dcontext
Definition cdrel.h:142
@ cdr_field_flags
Definition cdrel.h:156
@ cdr_field_end
Definition cdrel.h:149
@ cdr_field_duration
Definition cdrel.h:150
@ cdr_field_billsec
Definition cdrel.h:151
@ cdr_field_dstchannel
Definition cdrel.h:144
@ cdr_field_literal
Definition cdrel.h:138
@ cdr_field_sequence
Definition cdrel.h:162
@ cdr_field_lastdata
Definition cdrel.h:146
@ cdr_field_uniqueid
Definition cdrel.h:157
@ cdr_field_answer
Definition cdrel.h:148
@ cdr_field_tenantid
Definition cdrel.h:159
@ cdr_field_linkedid
Definition cdrel.h:158
@ cdr_field_channel
Definition cdrel.h:143
@ cdr_field_peertenantid
Definition cdrel.h:160
@ cdr_field_clid
Definition cdrel.h:139
@ cdr_field_dst
Definition cdrel.h:141
@ cdr_field_varshead
Definition cdrel.h:163
@ cdr_field_start
Definition cdrel.h:147
@ cdr_field_disposition
Definition cdrel.h:152
@ cdr_field_lastapp
Definition cdrel.h:145
@ cdr_field_userfield
Definition cdrel.h:161
@ cdr_field_accountcode
Definition cdrel.h:154
@ cdr_field_src
Definition cdrel.h:140
#define CDREL_FIELD_FLAG_LITERAL
Definition cdrel.h:99
int write_record_to_file(struct cdrel_config *config, struct ast_str *record)
Definition writers.c:50
#define CDREL_FIELD_FLAG_LAST
Definition cdrel.h:101
const struct cdrel_field * get_registered_field_by_name(enum cdrel_record_type record_type, const char *name)
Definition registry.c:113
enum cdrel_data_type cdrel_data_type_from_str(const char *str)
int load_cdr(void)
struct ast_channel *(* cdrel_dummy_channel_alloc)(struct cdrel_config *config, void *record)
Definition cdrel.h:265
#define CDREL_FIELD_FLAG_TYPE_FORCED
Definition cdrel.h:97
cdrel_data_type
Definition cdrel.h:72
@ cdrel_type_event_type
Definition cdrel.h:79
@ cdrel_data_type_strings_end
Definition cdrel.h:82
@ cdrel_type_cel_timefmt
Definition cdrel.h:81
@ cdrel_type_amaflags
Definition cdrel.h:76
@ cdrel_type_event_enum
Definition cdrel.h:80
@ cdrel_type_disposition
Definition cdrel.h:77
@ cdrel_type_uint64
Definition cdrel.h:86
@ cdrel_type_uservar
Definition cdrel.h:78
@ cdrel_type_double
Definition cdrel.h:87
@ cdrel_type_int64
Definition cdrel.h:85
@ cdrel_type_literal
Definition cdrel.h:75
@ cdrel_type_int32
Definition cdrel.h:83
@ cdrel_type_string
Definition cdrel.h:73
@ cdrel_data_type_end
Definition cdrel.h:88
@ cdrel_type_timeval
Definition cdrel.h:74
@ cdrel_type_uint32
Definition cdrel.h:84
cdrel_quoting_method
Definition cdrel.h:60
@ cdrel_quoting_method_all
Definition cdrel.h:62
@ cdrel_quoting_method_none
Definition cdrel.h:61
@ cdrel_quoting_method_non_numeric
Definition cdrel.h:64
@ cdrel_quoting_method_end
Definition cdrel.h:65
@ cdrel_quoting_method_minimal
Definition cdrel.h:63
cdrel_field_formatter cdrel_field_formatters[cdrel_data_type_end]
#define CDREL_FIELD_FLAG_FORMAT_SPEC
Definition cdrel.h:100
cdrel_text_format_type
Definition cdrel.h:47
@ cdrel_format_json
Definition cdrel.h:49
@ cdrel_format_type_end
Definition cdrel.h:51
@ cdrel_format_dsv
Definition cdrel.h:48
@ cdrel_format_sql
Definition cdrel.h:50
cdrel_field_flags
Definition cdrel.h:103
@ cdrel_flag_literal
Definition cdrel.h:108
@ cdrel_flag_quote
Definition cdrel.h:104
@ cdrel_flag_format_spec
Definition cdrel.h:109
@ cdrel_flag_noquote
Definition cdrel.h:105
@ cdrel_flag_uservar
Definition cdrel.h:107
@ cdrel_field_flags_end
Definition cdrel.h:110
@ cdrel_flag_type_forced
Definition cdrel.h:106
#define CDREL_FIELD_FLAG_QUOTE
Definition cdrel.h:95
cdrel_config_type
Definition cdrel.h:54
@ cdrel_config_type_end
Definition cdrel.h:57
@ cdrel_config_legacy
Definition cdrel.h:55
@ cdrel_config_advanced
Definition cdrel.h:56
const char * cdrel_module_type_map[]
cdrel_dummy_channel_alloc cdrel_dummy_channel_allocators[cdrel_format_type_end]
cdrel_backend_writer cdrel_backend_writers[cdrel_format_type_end]
int load_formatters(void)
Definition formatters.c:189
int(* cdrel_field_getter)(void *record, struct cdrel_config *config, struct cdrel_field *field, struct cdrel_value *value)
Definition cdrel.h:205
int(* cdrel_backend_writer)(struct cdrel_config *config, struct cdrel_values *values)
Definition cdrel.h:245
#define CDR_OFFSETOF(_field)
Definition cdrel.h:134
cdrel_field_getter cdrel_field_getters[cdrel_record_type_end][cdrel_data_type_end]
int load_writers(void)
Definition writers.c:221
const char * cdrel_data_type_map[]
const char * cdrel_record_type_map[]
#define CDREL_FIELD_FLAG_NOQUOTE
Definition cdrel.h:96
const char * cdrel_get_field_flags(struct ast_flags *flags, struct ast_str **str)
Call Event Logging API.
static int amaflags
Definition chan_iax2.c:500
static char accountcode[AST_MAX_ACCOUNT_CODE]
Definition chan_iax2.c:497
static const char config[]
static int answer(void *data)
Definition chan_pjsip.c:687
char * end
Definition eagi_proxy.c:73
static const char name[]
Definition format_mp3.c:68
Asterisk locking-related definitions:
Protected header for the CDR and CEL Custom Backends.
cdrel_backend_type
cdrel_record_type
@ cdrel_record_type_end
#define AST_DECLARE_STRING_FIELDS(field_list)
Declare the fields needed in a structure.
#define AST_STRING_FIELD(name)
Declare a string field.
String manipulation functions.
Main Channel structure associated with a channel.
Structure used to handle boolean flags.
Definition utils.h:220
Structure for mutex and tracking information.
Definition lock.h:142
Support for dynamic strings.
Definition strings.h:623
char quote_escape[2]
Definition cdrel.h:300
enum cdrel_config_type config_type
Definition cdrel.h:295
enum cdrel_quoting_method quoting_method
Definition cdrel.h:297
cdrel_dummy_channel_alloc dummy_channel_alloc
Definition cdrel.h:293
const ast_string_field db_columns
Definition cdrel.h:289
const ast_string_field config_filename
Definition cdrel.h:289
char quote[2]
Definition cdrel.h:299
const ast_string_field db_table
Definition cdrel.h:289
struct cdrel_config::@455 fields
char separator[2]
Definition cdrel.h:298
const ast_string_field output_filename
Definition cdrel.h:289
enum cdrel_text_format_type format_type
Definition cdrel.h:296
int busy_timeout
Definition cdrel.h:292
enum cdrel_record_type record_type
Definition cdrel.h:282
sqlite3 * db
Definition cdrel.h:290
ast_mutex_t lock
Definition cdrel.h:302
enum cdrel_backend_type backend_type
Definition cdrel.h:294
sqlite3_stmt * insert
Definition cdrel.h:291
int field_id
Definition cdrel.h:272
char data[1]
Definition cdrel.h:277
enum cdrel_data_type input_data_type
Definition cdrel.h:274
char * name
Definition cdrel.h:273
enum cdrel_data_type output_data_type
Definition cdrel.h:275
enum cdrel_record_type record_type
Definition cdrel.h:271
struct ast_flags flags
Definition cdrel.h:276
union cdrel_value::@454 values
int64_t int64
Definition cdrel.h:181
int32_t int32
Definition cdrel.h:179
uint32_t uint32
Definition cdrel.h:180
uint64_t uint64
Definition cdrel.h:182
struct timeval tv
Definition cdrel.h:183
enum cdrel_data_type data_type
Definition cdrel.h:175
char * field_name
Definition cdrel.h:174
double doubler
Definition cdrel.h:184
int mallocd
Definition cdrel.h:176
char * string
Definition cdrel.h:178
int value
Definition syslog.c:37
Vector container support.
#define AST_VECTOR(name, type)
Define a vector structure.
Definition vector.h:44