Asterisk - The Open Source Telephony Project GIT-master-27fb039
Loading...
Searching...
No Matches
Data Structures | Macros | Functions | Variables
message_json.c File Reference
#include "asterisk.h"
#include "asterisk/json.h"
#include "asterisk/res_aeap_message.h"
Include dependency graph for message_json.c:

Go to the source code of this file.

Data Structures

struct  message_json
 Asterisk external application JSON message type. More...
 

Macros

#define JSON_MSG(lvalue, rvalue)
 

Functions

static int message_json_construct1 (struct ast_aeap_message *self, const void *params)
 
static int message_json_construct2 (struct ast_aeap_message *self, const char *msg_type, const char *name, const char *id, const void *params)
 
static void * message_json_data (struct ast_aeap_message *self)
 
static int message_json_deserialize (struct ast_aeap_message *self, const void *buf, intmax_t size)
 
static void message_json_destruct (struct ast_aeap_message *self)
 
static const char * message_json_error_msg (const struct ast_aeap_message *self)
 
static int message_json_error_msg_set (struct ast_aeap_message *self, const char *error_msg)
 
static const char * message_json_id (const struct ast_aeap_message *self)
 
static int message_json_id_set (struct ast_aeap_message *self, const char *id)
 
static int message_json_is_request (const struct ast_aeap_message *self)
 
static int message_json_is_response (const struct ast_aeap_message *self)
 
static const char * message_json_name (const struct ast_aeap_message *self)
 
static int message_json_serialize (const struct ast_aeap_message *self, void **buf, intmax_t *size)
 

Variables

const struct ast_aeap_message_typeast_aeap_message_type_json = &message_type_json
 Asterisk external application JSON message type.
 
static const struct ast_aeap_message_type message_type_json
 

Macro Definition Documentation

◆ JSON_MSG

#define JSON_MSG (   lvalue,
  rvalue 
)
Value:
struct message_json *lvalue = \
((struct message_json *)rvalue)
Asterisk external application JSON message type.

Definition at line 25 of file message_json.c.

30 {
31 /*! The base message type (must be first) */
32 struct ast_aeap_message base;
33 /*! Underlying JSON data structure */
34 struct ast_json *json;
35};
36
37static int message_json_construct1(struct ast_aeap_message *self, const void *params)
38{
39 JSON_MSG(msg, self);
40
41 msg->json = ast_json_ref((struct ast_json *)params) ?: ast_json_object_create();
42
43 return msg->json ? 0 : -1;
44}
45
46static int message_json_construct2(struct ast_aeap_message *self, const char *msg_type,
47 const char *name, const char *id, const void *params)
48{
49 struct ast_json *msg_data;
50 int res;
51
52 msg_data = ast_json_pack("{s:s,s:s*}", msg_type, name, "id", id);
53
54 if (!msg_data) {
55 ast_log(LOG_ERROR, "AEAP message json: failed to create data for '%s: %s'", msg_type, name);
56 return -1;
57 }
58
59 if (params && ast_json_object_update(msg_data, (struct ast_json *)params)) {
60 ast_log(LOG_ERROR, "AEAP message json: failed to update data for '%s: %s'", msg_type, name);
62 return -1;
63 }
64
67 return res;
68}
69
70static void message_json_destruct(struct ast_aeap_message *self)
71{
72 JSON_MSG(msg, self);
73
74 ast_json_unref(msg->json);
75}
76
77static int message_json_deserialize(struct ast_aeap_message *self, const void *buf, intmax_t size)
78{
79 JSON_MSG(msg, self);
80
81 msg->json = ast_json_load_buf(buf, size, NULL);
82
83 return msg->json ? 0 : -1;
84}
85
86static int message_json_serialize(const struct ast_aeap_message *self, void **buf, intmax_t *size)
87{
88 const JSON_MSG(msg, self);
89
90 *buf = ast_json_dump_string(msg->json);
91 if (!*buf) {
92 *size = 0;
93 return -1;
94 }
95
96 *size = strlen(*buf);
97
98 return 0;
99}
100
101static const char *message_json_id(const struct ast_aeap_message *self)
102{
103 const JSON_MSG(msg, self);
104
105 return ast_json_object_string_get(msg->json, "id");
106}
107
108static int message_json_id_set(struct ast_aeap_message *self, const char *id)
109{
110 JSON_MSG(msg, self);
111
112 if (ast_json_object_set(msg->json, "id", ast_json_string_create(id))) {
113 return -1;
114 }
115
116 return 0;
117}
118
119static const char *message_json_name(const struct ast_aeap_message *self)
120{
121 const JSON_MSG(msg, self);
122 struct ast_json_iter *iter;
123
124 iter = ast_json_object_iter_at(msg->json, "response");
125 if (!iter) {
126 iter = ast_json_object_iter_at(msg->json, "request");
127 }
128
129 return iter ? ast_json_string_get(ast_json_object_iter_value(iter)) : "";
130}
131
132static void *message_json_data(struct ast_aeap_message *self)
133{
134 JSON_MSG(msg, self);
135
136 return msg->json;
137}
138
139static int message_json_is_request(const struct ast_aeap_message *self)
140{
141 const JSON_MSG(msg, self);
142
143 return ast_json_object_iter_at(msg->json, "request") != NULL;
144}
145
146static int message_json_is_response(const struct ast_aeap_message *self)
147{
148 const JSON_MSG(msg, self);
149
150 return ast_json_object_iter_at(msg->json, "response") != NULL;
151}
152
153static const char *message_json_error_msg(const struct ast_aeap_message *self)
154{
155 const JSON_MSG(msg, self);
156
157 return ast_json_object_string_get(msg->json, "error_msg");
158}
159
160static int message_json_error_msg_set(struct ast_aeap_message *self, const char *error_msg)
161{
162 JSON_MSG(msg, self);
163
164 if (ast_json_object_set(msg->json, "error_msg", ast_json_string_create(error_msg))) {
165 return -1;
166 }
167
168 return 0;
169}
170
171static const struct ast_aeap_message_type message_type_json = {
172 .type_size = sizeof(struct message_json),
173 .type_name = "json",
174 .serial_type = AST_AEAP_DATA_TYPE_STRING,
175 .construct1 = message_json_construct1,
176 .construct2 = message_json_construct2,
177 .destruct = message_json_destruct,
178 .deserialize = message_json_deserialize,
179 .serialize = message_json_serialize,
180 .id = message_json_id,
181 .id_set = message_json_id_set,
182 .name = message_json_name,
183 .data = message_json_data,
184 .is_request = message_json_is_request,
185 .is_response = message_json_is_response,
186 .error_msg = message_json_error_msg,
187 .error_msg_set = message_json_error_msg_set,
188};
189
#define ast_log
Definition astobj2.c:42
char buf[BUFSIZE]
Definition eagi_proxy.c:66
static const char name[]
Definition format_mp3.c:68
#define LOG_ERROR
#define ast_json_object_string_get(object, key)
Get a string field from a JSON object.
Definition json.h:600
struct ast_json * ast_json_object_iter_value(struct ast_json_iter *iter)
Get the value from an iterator.
Definition json.c:455
struct ast_json * ast_json_string_create(const char *value)
Construct a JSON string from value.
Definition json.c:278
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition json.c:73
struct ast_json * ast_json_object_create(void)
Create a new JSON object.
Definition json.c:399
#define ast_json_dump_string(root)
Encode a JSON value to a compact string.
Definition json.h:810
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition json.c:612
struct ast_json_iter * ast_json_object_iter_at(struct ast_json *object, const char *key)
Get an iterator pointing to a specified key in object.
Definition json.c:443
struct ast_json * ast_json_ref(struct ast_json *value)
Increase refcount on value.
Definition json.c:67
int ast_json_object_set(struct ast_json *object, const char *key, struct ast_json *value)
Set a field in a JSON object.
Definition json.c:414
struct ast_json * ast_json_load_buf(const char *buffer, size_t buflen, struct ast_json_error *error)
Parse buffer with known length into a JSON object or array.
Definition json.c:585
const char * ast_json_string_get(const struct ast_json *string)
Get the value of a JSON string.
Definition json.c:283
int ast_json_object_update(struct ast_json *object, struct ast_json *other)
Update object with all of the fields of other.
Definition json.c:426
static void message_json_destruct(struct ast_aeap_message *self)
static int message_json_is_request(const struct ast_aeap_message *self)
static int message_json_serialize(const struct ast_aeap_message *self, void **buf, intmax_t *size)
static int message_json_error_msg_set(struct ast_aeap_message *self, const char *error_msg)
static int message_json_construct1(struct ast_aeap_message *self, const void *params)
const struct ast_aeap_message_type * ast_aeap_message_type_json
Asterisk external application JSON message type.
#define JSON_MSG(lvalue, rvalue)
static const char * message_json_id(const struct ast_aeap_message *self)
static const char * message_json_error_msg(const struct ast_aeap_message *self)
static const char * message_json_name(const struct ast_aeap_message *self)
static int message_json_id_set(struct ast_aeap_message *self, const char *id)
static int message_json_construct2(struct ast_aeap_message *self, const char *msg_type, const char *name, const char *id, const void *params)
static int message_json_deserialize(struct ast_aeap_message *self, const void *buf, intmax_t size)
static const struct ast_aeap_message_type message_type_json
static void * message_json_data(struct ast_aeap_message *self)
static int message_json_is_response(const struct ast_aeap_message *self)
@ AST_AEAP_DATA_TYPE_STRING
Definition res_aeap.h:138
#define NULL
Definition resample.c:96
Message type virtual method table.
Asterisk external application base message.
Iterator for JSON object key/values.
Abstract JSON element (object, array, string, int, ...).

Function Documentation

◆ message_json_construct1()

static int message_json_construct1 ( struct ast_aeap_message self,
const void *  params 
)
static

Definition at line 38 of file message_json.c.

39{
40 JSON_MSG(msg, self);
41
42 msg->json = ast_json_ref((struct ast_json *)params) ?: ast_json_object_create();
43
44 return msg->json ? 0 : -1;
45}

References ast_json_object_create(), ast_json_ref(), and JSON_MSG.

Referenced by message_json_construct2().

◆ message_json_construct2()

static int message_json_construct2 ( struct ast_aeap_message self,
const char *  msg_type,
const char *  name,
const char *  id,
const void *  params 
)
static

Definition at line 47 of file message_json.c.

49{
50 struct ast_json *msg_data;
51 int res;
52
53 msg_data = ast_json_pack("{s:s,s:s*}", msg_type, name, "id", id);
54
55 if (!msg_data) {
56 ast_log(LOG_ERROR, "AEAP message json: failed to create data for '%s: %s'", msg_type, name);
57 return -1;
58 }
59
60 if (params && ast_json_object_update(msg_data, (struct ast_json *)params)) {
61 ast_log(LOG_ERROR, "AEAP message json: failed to update data for '%s: %s'", msg_type, name);
63 return -1;
64 }
65
68 return res;
69}

References ast_json_object_update(), ast_json_pack(), ast_json_unref(), ast_log, LOG_ERROR, message_json_construct1(), and name.

◆ message_json_data()

static void * message_json_data ( struct ast_aeap_message self)
static

Definition at line 133 of file message_json.c.

134{
135 JSON_MSG(msg, self);
136
137 return msg->json;
138}

References JSON_MSG.

◆ message_json_deserialize()

static int message_json_deserialize ( struct ast_aeap_message self,
const void *  buf,
intmax_t  size 
)
static

Definition at line 78 of file message_json.c.

79{
80 JSON_MSG(msg, self);
81
82 msg->json = ast_json_load_buf(buf, size, NULL);
83
84 return msg->json ? 0 : -1;
85}

References ast_json_load_buf(), buf, JSON_MSG, and NULL.

◆ message_json_destruct()

static void message_json_destruct ( struct ast_aeap_message self)
static

Definition at line 71 of file message_json.c.

72{
73 JSON_MSG(msg, self);
74
75 ast_json_unref(msg->json);
76}

References ast_json_unref(), and JSON_MSG.

◆ message_json_error_msg()

static const char * message_json_error_msg ( const struct ast_aeap_message self)
static

Definition at line 154 of file message_json.c.

155{
156 const JSON_MSG(msg, self);
157
158 return ast_json_object_string_get(msg->json, "error_msg");
159}

References ast_json_object_string_get, and JSON_MSG.

◆ message_json_error_msg_set()

static int message_json_error_msg_set ( struct ast_aeap_message self,
const char *  error_msg 
)
static

Definition at line 161 of file message_json.c.

162{
163 JSON_MSG(msg, self);
164
165 if (ast_json_object_set(msg->json, "error_msg", ast_json_string_create(error_msg))) {
166 return -1;
167 }
168
169 return 0;
170}

References ast_json_object_set(), ast_json_string_create(), and JSON_MSG.

◆ message_json_id()

static const char * message_json_id ( const struct ast_aeap_message self)
static

Definition at line 102 of file message_json.c.

103{
104 const JSON_MSG(msg, self);
105
106 return ast_json_object_string_get(msg->json, "id");
107}

References ast_json_object_string_get, and JSON_MSG.

◆ message_json_id_set()

static int message_json_id_set ( struct ast_aeap_message self,
const char *  id 
)
static

Definition at line 109 of file message_json.c.

110{
111 JSON_MSG(msg, self);
112
113 if (ast_json_object_set(msg->json, "id", ast_json_string_create(id))) {
114 return -1;
115 }
116
117 return 0;
118}

References ast_json_object_set(), ast_json_string_create(), and JSON_MSG.

◆ message_json_is_request()

static int message_json_is_request ( const struct ast_aeap_message self)
static

Definition at line 140 of file message_json.c.

141{
142 const JSON_MSG(msg, self);
143
144 return ast_json_object_iter_at(msg->json, "request") != NULL;
145}

References ast_json_object_iter_at(), JSON_MSG, and NULL.

◆ message_json_is_response()

static int message_json_is_response ( const struct ast_aeap_message self)
static

Definition at line 147 of file message_json.c.

148{
149 const JSON_MSG(msg, self);
150
151 return ast_json_object_iter_at(msg->json, "response") != NULL;
152}

References ast_json_object_iter_at(), JSON_MSG, and NULL.

◆ message_json_name()

static const char * message_json_name ( const struct ast_aeap_message self)
static

Definition at line 120 of file message_json.c.

121{
122 const JSON_MSG(msg, self);
123 struct ast_json_iter *iter;
124
125 iter = ast_json_object_iter_at(msg->json, "response");
126 if (!iter) {
127 iter = ast_json_object_iter_at(msg->json, "request");
128 }
129
130 return iter ? ast_json_string_get(ast_json_object_iter_value(iter)) : "";
131}

References ast_json_object_iter_at(), ast_json_object_iter_value(), ast_json_string_get(), and JSON_MSG.

◆ message_json_serialize()

static int message_json_serialize ( const struct ast_aeap_message self,
void **  buf,
intmax_t *  size 
)
static

Definition at line 87 of file message_json.c.

88{
89 const JSON_MSG(msg, self);
90
91 *buf = ast_json_dump_string(msg->json);
92 if (!*buf) {
93 *size = 0;
94 return -1;
95 }
96
97 *size = strlen(*buf);
98
99 return 0;
100}

References ast_json_dump_string, buf, and JSON_MSG.

Variable Documentation

◆ ast_aeap_message_type_json

const struct ast_aeap_message_type* ast_aeap_message_type_json = &message_type_json

Asterisk external application JSON message type.

Definition at line 191 of file message_json.c.

Referenced by AST_TEST_DEFINE(), AST_TEST_DEFINE(), AST_TEST_DEFINE(), handle_request_set(), load_module(), and speech_aeap_send_request().

◆ message_type_json

const struct ast_aeap_message_type message_type_json
static

Definition at line 172 of file message_json.c.

172 {
173 .type_size = sizeof(struct message_json),
174 .type_name = "json",
175 .serial_type = AST_AEAP_DATA_TYPE_STRING,
176 .construct1 = message_json_construct1,
177 .construct2 = message_json_construct2,
178 .destruct = message_json_destruct,
179 .deserialize = message_json_deserialize,
180 .serialize = message_json_serialize,
181 .id = message_json_id,
182 .id_set = message_json_id_set,
183 .name = message_json_name,
184 .data = message_json_data,
185 .is_request = message_json_is_request,
186 .is_response = message_json_is_response,
187 .error_msg = message_json_error_msg,
188 .error_msg_set = message_json_error_msg_set,
189};