Asterisk - The Open Source Telephony Project GIT-master-7e7a603
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. More...
 
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: message_json.c:31

Definition at line 25 of file message_json.c.

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}
struct ast_json * ast_json_object_create(void)
Create a new JSON object.
Definition: json.c:399
struct ast_json * ast_json_ref(struct ast_json *value)
Increase refcount on value.
Definition: json.c:67
#define JSON_MSG(lvalue, rvalue)
Definition: message_json.c:25
Abstract JSON element (object, array, string, int, ...).

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}
#define ast_log
Definition: astobj2.c:42
static const char name[]
Definition: format_mp3.c:68
#define LOG_ERROR
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_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:612
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 int message_json_construct1(struct ast_aeap_message *self, const void *params)
Definition: message_json.c:38

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}
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
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
#define NULL
Definition: resample.c:96

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}
#define ast_json_object_string_get(object, key)
Get a string field from a JSON object.
Definition: json.h:600

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}
struct ast_json * ast_json_string_create(const char *value)
Construct a JSON string from value.
Definition: json.c:278
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

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}
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

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}
struct ast_json * ast_json_object_iter_value(struct ast_json_iter *iter)
Get the value from an iterator.
Definition: json.c:455
const char * ast_json_string_get(const struct ast_json *string)
Get the value of a JSON string.
Definition: json.c:283
Iterator for JSON object key/values.

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}
#define ast_json_dump_string(root)
Encode a JSON value to a compact string.
Definition: json.h:810

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(), 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.