struct ast_json *app_to_json(const struct stasis_app *app) { RAII_VAR(struct ast_json *, json, NULL, ast_json_unref); struct ast_json *channels; struct ast_json *bridges; struct ast_json *endpoints; struct ao2_iterator i; void *obj; json = ast_json_pack("{s: s, s: [], s: [], s: []}", "name", app->name, "channel_ids", "bridge_ids", "endpoint_ids"); channels = ast_json_object_get(json, "channel_ids"); bridges = ast_json_object_get(json, "bridge_ids"); endpoints = ast_json_object_get(json, "endpoint_ids"); i = ao2_iterator_init(app->forwards, 0); while ((obj = ao2_iterator_next(&i))) { RAII_VAR(struct app_forwards *, forwards, obj, ao2_cleanup); RAII_VAR(struct ast_json *, id, NULL, ast_json_unref); int append_res = -1; id = ast_json_string_create(forwards->id); switch (forwards->forward_type) { case FORWARD_CHANNEL: append_res = ast_json_array_append(channels, ast_json_ref(id)); break; case FORWARD_BRIDGE: append_res = ast_json_array_append(bridges, ast_json_ref(id)); break; case FORWARD_ENDPOINT: append_res = ast_json_array_append(endpoints, ast_json_ref(id)); break; } if (append_res != 0) { ast_log(LOG_ERROR, "Error building response\n"); ao2_iterator_destroy(&i); return NULL; } } ao2_iterator_destroy(&i); return ast_json_ref(json); }
struct ast_json *stasis_app_mailboxes_to_json() { struct ast_json *array = ast_json_array_create(); struct ao2_container *mailboxes; struct ao2_iterator iter; const struct ast_mwi_mailbox_object *mailbox; if (!array) { return NULL; } mailboxes = ast_mwi_mailbox_get_all(); if (!mailboxes) { ast_json_unref(array); return NULL; } iter = ao2_iterator_init(mailboxes, 0); for (; (mailbox = ao2_iterator_next(&iter)); ast_mwi_mailbox_unref(mailbox)) { struct ast_json *appending = mailbox_to_json(mailbox); if (!appending || ast_json_array_append(array, appending)) { /* Failed to append individual mailbox to the array. Abort. */ ast_json_unref(array); array = NULL; break; } } ao2_iterator_destroy(&iter); return array; }
/** * Get all plan info. * @return */ struct ast_json* get_plans_all(void) { char* sql; struct ast_json* j_res; struct ast_json* j_tmp; db_res_t* db_res; ast_asprintf(&sql, "select * from plan where in_use=%d;", E_DL_USE_OK); db_res = db_query(sql); ast_free(sql); if(db_res == NULL) { ast_log(LOG_ERROR, "Could not get plan all info.\n"); return NULL; } j_res = ast_json_array_create(); while(1) { j_tmp = db_get_record(db_res); if(j_tmp == NULL) { break; } ast_json_array_append(j_res, j_tmp); } db_free(db_res); return j_res; }
static void test_handler(void *data, const char *app_name, struct ast_json *message) { struct app_data *actual = data; int res; ++(actual->invocations); res = ast_json_array_append(actual->messages, ast_json_copy(message)); ast_assert(res == 0); }
void ast_ari_channels_list(struct ast_variable *headers, struct ast_ari_channels_list_args *args, struct ast_ari_response *response) { RAII_VAR(struct stasis_cache *, cache, NULL, ao2_cleanup); RAII_VAR(struct ao2_container *, snapshots, NULL, ao2_cleanup); RAII_VAR(struct ast_json *, json, NULL, ast_json_unref); struct ao2_iterator i; void *obj; struct stasis_message_sanitizer *sanitize = stasis_app_get_sanitizer(); cache = ast_channel_cache(); if (!cache) { ast_ari_response_error( response, 500, "Internal Server Error", "Message bus not initialized"); return; } ao2_ref(cache, +1); snapshots = stasis_cache_dump(cache, ast_channel_snapshot_type()); if (!snapshots) { ast_ari_response_alloc_failed(response); return; } json = ast_json_array_create(); if (!json) { ast_ari_response_alloc_failed(response); return; } for (i = ao2_iterator_init(snapshots, 0); (obj = ao2_iterator_next(&i)); ao2_cleanup(obj)) { RAII_VAR(struct stasis_message *, msg, obj, ao2_cleanup); struct ast_channel_snapshot *snapshot = stasis_message_data(msg); int r; if (sanitize && sanitize->channel_snapshot && sanitize->channel_snapshot(snapshot)) { continue; } r = ast_json_array_append( json, ast_channel_snapshot_to_json(snapshot, NULL)); if (r != 0) { ast_ari_response_alloc_failed(response); ao2_cleanup(obj); ao2_iterator_destroy(&i); return; } } ao2_iterator_destroy(&i); ast_ari_response_ok(response, ast_json_ref(json)); }
struct ast_json *app_to_json(const struct stasis_app *app) { struct ast_json *json; struct ast_json *channels; struct ast_json *bridges; struct ast_json *endpoints; struct ao2_iterator i; struct app_forwards *forwards; json = ast_json_pack("{s: s, s: [], s: [], s: []}", "name", app->name, "channel_ids", "bridge_ids", "endpoint_ids"); if (!json) { return NULL; } channels = ast_json_object_get(json, "channel_ids"); bridges = ast_json_object_get(json, "bridge_ids"); endpoints = ast_json_object_get(json, "endpoint_ids"); i = ao2_iterator_init(app->forwards, 0); while ((forwards = ao2_iterator_next(&i))) { struct ast_json *array = NULL; int append_res; switch (forwards->forward_type) { case FORWARD_CHANNEL: array = channels; break; case FORWARD_BRIDGE: array = bridges; break; case FORWARD_ENDPOINT: array = endpoints; break; } /* If forward_type value is unexpected this will safely return an error. */ append_res = ast_json_array_append(array, ast_json_string_create(forwards->id)); ao2_ref(forwards, -1); if (append_res != 0) { ast_log(LOG_ERROR, "Error building response\n"); ao2_iterator_destroy(&i); ast_json_unref(json); return NULL; } } ao2_iterator_destroy(&i); return json; }
/*! \brief Generate a Sound structure and append it to the output blob */ static int append_sound_cb(void *obj, void *arg, void *data, int flags) { struct ast_json *sounds_array = arg; char *filename = obj; struct ast_ari_sounds_list_args *args = data; struct ast_json *sound_blob = create_sound_blob(filename, args); if (!sound_blob) { return 0; } ast_json_array_append(sounds_array, sound_blob); return 0; }
/*! * \brief Process logger information and append to a json array * \param channel Resource logger channel name path * \param type Resource log type * \param status Resource log status * \param configuration Resource logger levels * \param log_data_list Resource array * * \retval -1 if no resource exists * \retval 0 if resource exists */ static int process_log_list(const char *channel, const char *type, const char *status, const char *configuration, void *log_data_list) { struct ast_json *logger_info; logger_info = ast_json_pack("{s: s, s: s, s: s, s: s}", "channel", channel, "type", type, "status", status, "configuration", configuration); if (!logger_info) { return AST_LOGGER_FAILURE; } ast_json_array_append(log_data_list, logger_info); return AST_LOGGER_SUCCESS; }
void ast_ari_bridges_list(struct ast_variable *headers, struct ast_ari_bridges_list_args *args, struct ast_ari_response *response) { RAII_VAR(struct stasis_cache *, cache, NULL, ao2_cleanup); RAII_VAR(struct ao2_container *, snapshots, NULL, ao2_cleanup); RAII_VAR(struct ast_json *, json, NULL, ast_json_unref); struct ao2_iterator i; void *obj; cache = ast_bridge_cache(); if (!cache) { ast_ari_response_error( response, 500, "Internal Server Error", "Message bus not initialized"); return; } ao2_ref(cache, +1); snapshots = stasis_cache_dump(cache, ast_bridge_snapshot_type()); if (!snapshots) { ast_ari_response_alloc_failed(response); return; } json = ast_json_array_create(); if (!json) { ast_ari_response_alloc_failed(response); return; } i = ao2_iterator_init(snapshots, 0); while ((obj = ao2_iterator_next(&i))) { RAII_VAR(struct stasis_message *, msg, obj, ao2_cleanup); struct ast_bridge_snapshot *snapshot = stasis_message_data(msg); struct ast_json *json_bridge = ast_bridge_snapshot_to_json(snapshot, stasis_app_get_sanitizer()); if (!json_bridge || ast_json_array_append(json, json_bridge)) { ao2_iterator_destroy(&i); ast_ari_response_alloc_failed(response); return; } } ao2_iterator_destroy(&i); ast_ari_response_ok(response, ast_json_ref(json)); }
static void return_sorcery_object(struct ast_sorcery *sorcery, void *sorcery_obj, struct ast_ari_response *response) { RAII_VAR(struct ast_json *, return_set, NULL, ast_json_unref); struct ast_variable *change_set; struct ast_variable *it_change_set; return_set = ast_json_array_create(); if (!return_set) { ast_ari_response_alloc_failed(response); return; } /* Note that we can't use the sorcery JSON change set directly, * as it will hand us back an Object (with fields), and we need * a more generic representation of whatever the API call asked * for, i.e., a list of tuples. */ change_set = ast_sorcery_objectset_create(sorcery, sorcery_obj); if (!change_set) { ast_ari_response_alloc_failed(response); return; } for (it_change_set = change_set; it_change_set; it_change_set = it_change_set->next) { struct ast_json *tuple; tuple = ast_json_pack("{s: s, s: s}", "attribute", it_change_set->name, "value", it_change_set->value); if (!tuple) { ast_variables_destroy(change_set); ast_ari_response_alloc_failed(response); return; } if (ast_json_array_append(return_set, tuple)) { ast_json_unref(tuple); ast_variables_destroy(change_set); ast_ari_response_alloc_failed(response); return; } } ast_variables_destroy(change_set); ast_ari_response_ok(response, ast_json_ref(return_set)); }
struct ast_json *ast_endpoint_snapshot_to_json( const struct ast_endpoint_snapshot *snapshot, const struct stasis_message_sanitizer *sanitize) { RAII_VAR(struct ast_json *, json, NULL, ast_json_unref); struct ast_json *channel_array; int i; json = ast_json_pack("{s: s, s: s, s: s, s: []}", "technology", snapshot->tech, "resource", snapshot->resource, "state", ast_endpoint_state_to_string(snapshot->state), "channel_ids"); if (json == NULL) { return NULL; } if (snapshot->max_channels != -1) { int res = ast_json_object_set(json, "max_channels", ast_json_integer_create(snapshot->max_channels)); if (res != 0) { return NULL; } } channel_array = ast_json_object_get(json, "channel_ids"); ast_assert(channel_array != NULL); for (i = 0; i < snapshot->num_channels; ++i) { int res; if (sanitize && sanitize->channel_id && sanitize->channel_id(snapshot->channel_ids[i])) { continue; } res = ast_json_array_append(channel_array, ast_json_string_create(snapshot->channel_ids[i])); if (res != 0) { return NULL; } } return ast_json_ref(json); }
/*! * \brief Process module information and append to a json array * \param module Resource name * \param description Resource description * \param usecnt Resource use count * \param status Resource running status * \param like * \param support_level Resource support level * \param module_data_list Resource array * * \retval 0 if no resource exists * \retval 1 if resource exists */ static int process_module_list(const char *module, const char *description, int usecnt, const char *status, const char *like, enum ast_module_support_level support_level, void *module_data_list) { struct ast_json *module_info; module_info = ast_json_pack("{s: s, s: s, s: i, s: s, s: s}", "name", module, "description", description, "use_count", usecnt, "status", status, "support_level", ast_module_support_level_to_string(support_level)); if (!module_info) { return 0; } ast_json_array_append(module_data_list, module_info); return 1; }
/*! \brief Add format/lang pairs to the array embedded in the sound object */ static int add_format_information_cb(void *obj, void *arg, int flags) { char *language = obj; struct lang_format_info *args = arg; int idx; RAII_VAR(struct ast_format_cap *, cap, NULL, ao2_cleanup); RAII_VAR(struct ast_media_index *, sounds_index, ast_sounds_get_index(), ao2_cleanup); if (!sounds_index) { return CMP_STOP; } cap = ast_media_get_format_cap(sounds_index, args->filename, language); if (!cap) { return CMP_STOP; } for (idx = 0; idx < ast_format_cap_count(cap); idx++) { struct ast_format *format = ast_format_cap_get_format(cap, idx); struct ast_json *lang_format_pair; if (!ast_strlen_zero(args->format_filter) && strcmp(args->format_filter, ast_format_get_name(format))) { ao2_ref(format, -1); continue; } lang_format_pair = ast_json_pack("{s: s, s: s}", "language", language, "format", ast_format_get_name(format)); if (!lang_format_pair) { ao2_ref(format, -1); return CMP_STOP; } ast_json_array_append(args->format_list, lang_format_pair); ao2_ref(format, -1); } return 0; }
/*! \brief Add format/lang pairs to the array embedded in the sound object */ static int add_format_information_cb(void *obj, void *arg, int flags) { char *language = obj; struct lang_format_info *args = arg; struct ast_format format; RAII_VAR(struct ast_format_cap *, cap, NULL, ast_format_cap_destroy); RAII_VAR(struct ast_media_index *, sounds_index, ast_sounds_get_index(), ao2_cleanup); if (!sounds_index) { return CMP_STOP; } cap = ast_media_get_format_cap(sounds_index, args->filename, language); if (!cap) { return CMP_STOP; } ast_format_cap_iter_start(cap); while (!ast_format_cap_iter_next(cap, &format)) { struct ast_json *lang_format_pair; const char *format_name = ast_getformatname(&format); if (!ast_strlen_zero(args->format_filter) && strcmp(args->format_filter, format_name)) { continue; } lang_format_pair = ast_json_pack("{s: s, s: s}", "language", language, "format", format_name); if (!lang_format_pair) { ast_format_cap_iter_end(cap); return CMP_STOP; } ast_json_array_append(args->format_list, lang_format_pair); } ast_format_cap_iter_end(cap); return 0; }
void ast_ari_recordings_list_stored(struct ast_variable *headers, struct ast_ari_recordings_list_stored_args *args, struct ast_ari_response *response) { RAII_VAR(struct ao2_container *, recordings, NULL, ao2_cleanup); RAII_VAR(struct ast_json *, json, NULL, ast_json_unref); struct ao2_iterator i; void *obj; recordings = stasis_app_stored_recording_find_all(); if (!recordings) { ast_ari_response_alloc_failed(response); return; } json = ast_json_array_create(); if (!json) { ast_ari_response_alloc_failed(response); return; } i = ao2_iterator_init(recordings, 0); while ((obj = ao2_iterator_next(&i))) { RAII_VAR(struct stasis_app_stored_recording *, recording, obj, ao2_cleanup); int r = ast_json_array_append( json, stasis_app_stored_recording_to_json(recording)); if (r != 0) { ast_ari_response_alloc_failed(response); ao2_iterator_destroy(&i); return; } } ao2_iterator_destroy(&i); ast_ari_response_ok(response, ast_json_ref(json)); }
void ast_ari_endpoints_list_by_tech(struct ast_variable *headers, struct ast_ari_endpoints_list_by_tech_args *args, struct ast_ari_response *response) { RAII_VAR(struct stasis_cache *, cache, NULL, ao2_cleanup); RAII_VAR(struct ao2_container *, snapshots, NULL, ao2_cleanup); RAII_VAR(struct ast_json *, json, NULL, ast_json_unref); struct ao2_iterator i; void *obj; if (!ast_get_channel_tech(args->tech)) { ast_ari_response_error(response, 404, "Not Found", "No Endpoints found - invalid tech %s", args->tech); return; } cache = ast_endpoint_cache(); if (!cache) { ast_ari_response_error( response, 500, "Internal Server Error", "Message bus not initialized"); return; } ao2_ref(cache, +1); snapshots = stasis_cache_dump(cache, ast_endpoint_snapshot_type()); if (!snapshots) { ast_ari_response_alloc_failed(response); return; } json = ast_json_array_create(); if (!json) { ast_ari_response_alloc_failed(response); return; } i = ao2_iterator_init(snapshots, 0); while ((obj = ao2_iterator_next(&i))) { RAII_VAR(struct stasis_message *, msg, obj, ao2_cleanup); struct ast_endpoint_snapshot *snapshot = stasis_message_data(msg); struct ast_json *json_endpoint; int r; if (strcasecmp(args->tech, snapshot->tech) != 0) { continue; } json_endpoint = ast_endpoint_snapshot_to_json(snapshot, stasis_app_get_sanitizer()); if (!json_endpoint) { continue; } r = ast_json_array_append( json, json_endpoint); if (r != 0) { ast_ari_response_alloc_failed(response); return; } } ao2_iterator_destroy(&i); ast_ari_response_ok(response, ast_json_ref(json)); }