summaryrefslogtreecommitdiff
path: root/main/stasis_system.c
blob: 15451ed1bf4566186caaf1d9fb29769c64cc924c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2013, Digium, Inc.
 *
 * Jason Parker <jparker@digium.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*! \file
 *
 * \brief Stasis Messages and Data Types for System events
 *
 * \author Jason Parker <jparker@digium.com>
 */

/*** MODULEINFO
	<support_level>core</support_level>
 ***/

#include "asterisk.h"

ASTERISK_FILE_VERSION(__FILE__, "$Revision$")

#include "asterisk/astobj2.h"
#include "asterisk/stasis.h"
#include "asterisk/stasis_system.h"

/*** DOCUMENTATION
	<managerEvent language="en_US" name="Registry">
		<managerEventInstance class="EVENT_FLAG_SYSTEM">
			<synopsis>Raised when an outbound registration completes.</synopsis>
			<syntax>
				<parameter name="ChannelType">
					<para>The type of channel that was registered (or not).</para>
				</parameter>
				<parameter name="Username">
					<para>The username portion of the registration.</para>
				</parameter>
				<parameter name="Domain">
					<para>The address portion of the registration.</para>
				</parameter>
				<parameter name="Status">
					<para>The status of the registration request.</para>
					<enumlist>
						<enum name="Registered"/>
						<enum name="Unregistered"/>
						<enum name="Rejected"/>
						<enum name="Failed"/>
					</enumlist>
				</parameter>
				<parameter name="Cause">
					<para>What caused the rejection of the request, if available.</para>
				</parameter>
			</syntax>
		</managerEventInstance>
	</managerEvent>
 ***/

/*! \brief The \ref stasis topic for system level changes */
static struct stasis_topic *system_topic;

static struct ast_manager_event_blob *system_registry_to_ami(struct stasis_message *message);

STASIS_MESSAGE_TYPE_DEFN(ast_network_change_type);
STASIS_MESSAGE_TYPE_DEFN(ast_system_registry_type,
	.to_ami = system_registry_to_ami,
	);

void ast_system_publish_registry(const char *channeltype, const char *username, const char *domain, const char *status, const char *cause)
{
	RAII_VAR(struct ast_json *, registry, NULL, ast_json_unref);
	RAII_VAR(struct ast_json_payload *, payload, NULL, ao2_cleanup);
	RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);

	registry = ast_json_pack("{s: s, s: s, s: s, s: s, s: s, s: s}",
		"type", "registry",
		"channeltype", channeltype,
		"username", username,
		"domain", domain,
		"status", status,
		"cause", S_OR(cause, ""));

	if (!(payload = ast_json_payload_create(registry))) {
		return;
	}

	if (!(message = stasis_message_create(ast_system_registry_type(), payload))) {
		return;
	}

	stasis_publish(ast_system_topic(), message);
}

static struct ast_manager_event_blob *system_registry_to_ami(struct stasis_message *message)
{
	struct ast_json_payload *payload = stasis_message_data(message);
	const char *channeltype;
	const char *username;
	const char *domain;
	const char *status;
	const char *cause;
	RAII_VAR(struct ast_str *, cause_string, ast_str_create(32), ast_free);

	if (!cause_string) {
		return NULL;
	}

	channeltype = ast_json_string_get(ast_json_object_get(payload->json, "channeltype"));
	username = ast_json_string_get(ast_json_object_get(payload->json, "username"));
	domain = ast_json_string_get(ast_json_object_get(payload->json, "domain"));
	status = ast_json_string_get(ast_json_object_get(payload->json, "status"));
	cause = ast_json_string_get(ast_json_object_get(payload->json, "cause"));

	if (!ast_strlen_zero(cause)) {
		ast_str_set(&cause_string, 0, "Cause: %s\r\n", cause);
	}

	return ast_manager_event_blob_create(EVENT_FLAG_SYSTEM, "Registry",
		"ChannelType: %s\r\n"
		"Username: %s\r\n"
		"Domain: %s\r\n"
		"Status: %s\r\n"
		"%s",
		channeltype, username, domain, status, ast_str_buffer(cause_string));
}

struct stasis_topic *ast_system_topic(void)
{
	return system_topic;
}

/*! \brief Cleanup the \ref stasis system level items */
static void stasis_system_cleanup(void)
{
	ao2_cleanup(system_topic);
	system_topic = NULL;
	STASIS_MESSAGE_TYPE_CLEANUP(ast_network_change_type);
	STASIS_MESSAGE_TYPE_CLEANUP(ast_system_registry_type);
}

/*! \brief Initialize the system level items for \ref stasis */
int ast_stasis_system_init(void)
{
	ast_register_cleanup(stasis_system_cleanup);

	system_topic = stasis_topic_create("ast_system");
	if (!system_topic) {
		return 1;
	}

	if (STASIS_MESSAGE_TYPE_INIT(ast_network_change_type) != 0) {
		return -1;
	}

	if (STASIS_MESSAGE_TYPE_INIT(ast_system_registry_type) != 0) {
		return -1;
	}

	return 0;
}