From 453d8afffd406850f399a493f1f542dbc6da2b30 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Sat, 8 Jun 2013 22:55:51 +0200 Subject: [PATCH] allow commands to return individual json elements instead of just json objects --- commands-dms.c | 5 ++++- commands-nas.c | 10 +++++++++- commands-wds.c | 4 ++-- commands-wms.c | 26 ++++++++++++++------------ commands.c | 29 ++++++++++++++++++++++------- commands.h | 3 ++- main.c | 6 +++--- 7 files changed, 56 insertions(+), 27 deletions(-) diff --git a/commands-dms.c b/commands-dms.c index f26b066..a77bc6b 100644 --- a/commands-dms.c +++ b/commands-dms.c @@ -23,8 +23,10 @@ static const char *get_pin_status(QmiDmsUimPinStatus status) static void cmd_dms_get_pin_status_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg) { struct qmi_dms_uim_get_pin_status_response res; + void *c; qmi_parse_dms_uim_get_pin_status_response(msg, &res); + c = blobmsg_open_table(&status, NULL); if (res.set.pin1_status) { blobmsg_add_string(&status, "pin1_status", get_pin_status(res.data.pin1_status.current_status)); blobmsg_add_u32(&status, "pin1_verify_tries", (int32_t) res.data.pin1_status.verify_retries_left); @@ -35,6 +37,7 @@ static void cmd_dms_get_pin_status_cb(struct qmi_dev *qmi, struct qmi_request *r blobmsg_add_u32(&status, "pin2_verify_tries", (int32_t) res.data.pin2_status.verify_retries_left); blobmsg_add_u32(&status, "pin2_unblock_tries", (int32_t) res.data.pin2_status.unblock_retries_left); } + blobmsg_close_table(&status, c); } static enum qmi_cmd_result @@ -78,7 +81,7 @@ static void cmd_dms_get_imsi_cb(struct qmi_dev *qmi, struct qmi_request *req, st qmi_parse_dms_uim_get_imsi_response(msg, &res); if (res.data.imsi) - blobmsg_add_string(&status, "number", res.data.imsi); + blobmsg_add_string(&status, NULL, res.data.imsi); } static enum qmi_cmd_result diff --git a/commands-nas.c b/commands-nas.c index b30da1f..f502035 100644 --- a/commands-nas.c +++ b/commands-nas.c @@ -35,7 +35,7 @@ cmd_nas_set_network_modes_prepare(struct qmi_dev *qmi, struct qmi_request *req, } if (!found) { - blobmsg_add_string(&status, "error", "Invalid network mode"); + uqmi_add_error("Invalid network mode"); return QMI_CMD_EXIT; } } @@ -74,9 +74,11 @@ static void cmd_nas_get_signal_info_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg) { struct qmi_nas_get_signal_info_response res; + void *c; qmi_parse_nas_get_signal_info_response(msg, &res); + c = blobmsg_open_table(&status, NULL); if (res.set.cdma_signal_strength) { blobmsg_add_string(&status, "type", "cdma"); blobmsg_add_u32(&status, "rssi", (int32_t) res.data.cdma_signal_strength.rssi); @@ -108,6 +110,8 @@ cmd_nas_get_signal_info_cb(struct qmi_dev *qmi, struct qmi_request *req, struct blobmsg_add_u32(&status, "rsrp", (int32_t) res.data.lte_signal_strength.rsrp); blobmsg_add_u32(&status, "snr", (int32_t) res.data.lte_signal_strength.snr); } + + blobmsg_close_table(&status, c); } static enum qmi_cmd_result @@ -128,9 +132,11 @@ cmd_nas_get_serving_system_cb(struct qmi_dev *qmi, struct qmi_request *req, stru [QMI_NAS_REGISTRATION_STATE_REGISTRATION_DENIED] = "registering_denied", [QMI_NAS_REGISTRATION_STATE_UNKNOWN] = "unknown", }; + void *c; qmi_parse_nas_get_serving_system_response(msg, &res); + c = blobmsg_open_table(&status, NULL); if (res.set.serving_system) { int state = res.data.serving_system.registration_state; @@ -148,6 +154,8 @@ cmd_nas_get_serving_system_cb(struct qmi_dev *qmi, struct qmi_request *req, stru if (res.set.roaming_indicator) blobmsg_add_u8(&status, "roaming", !res.data.roaming_indicator); + + blobmsg_close_table(&status, c); } static enum qmi_cmd_result diff --git a/commands-wds.c b/commands-wds.c index 3b27bfd..2128bbf 100644 --- a/commands-wds.c +++ b/commands-wds.c @@ -28,7 +28,7 @@ cmd_wds_set_auth_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qm return QMI_CMD_DONE; } - blobmsg_add_string(&status, "error", "Invalid auth mode (valid: pap, chap, both, none)"); + uqmi_add_error("Invalid auth mode (valid: pap, chap, both, none)"); return QMI_CMD_EXIT; } @@ -55,7 +55,7 @@ cmd_wds_start_network_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qm qmi_parse_wds_start_network_response(msg, &res); if (res.set.packet_data_handle) - blobmsg_add_u32(&status, "handle", res.data.packet_data_handle); + blobmsg_add_u32(&status, NULL, res.data.packet_data_handle); } static enum qmi_cmd_result diff --git a/commands-wms.c b/commands-wms.c index 78554c5..6fc34df 100644 --- a/commands-wms.c +++ b/commands-wms.c @@ -3,16 +3,15 @@ static void cmd_wms_list_messages_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg) { struct qmi_wms_list_messages_response res; - int i, len = 0; + void *c; + int i; qmi_parse_wms_list_messages_response(msg, &res); - blobmsg_alloc_string_buffer(&status, "messages", 1); - for (i = 0; i < res.data.message_list_n; i++) { - len += sprintf(blobmsg_realloc_string_buffer(&status, len + 12) + len, - " %d" + (len ? 0 : 1), - res.data.message_list[i].memory_index); - } - blobmsg_add_string_buffer(&status); + c = blobmsg_open_array(&status, NULL); + for (i = 0; i < res.data.message_list_n; i++) + blobmsg_add_u32(&status, NULL, res.data.message_list[i].memory_index); + + blobmsg_close_array(&status, c); } static enum qmi_cmd_result @@ -262,8 +261,10 @@ static void cmd_wms_get_message_cb(struct qmi_dev *qmi, struct qmi_request *req, int cur_len; bool sent; unsigned char first, dcs; + void *c; qmi_parse_wms_raw_read_response(msg, &res); + c = blobmsg_open_table(&status, NULL); data = (unsigned char *) res.data.raw_message_data.raw_data; end = data + res.data.raw_message_data.raw_data_n; @@ -349,6 +350,7 @@ static void cmd_wms_get_message_cb(struct qmi_dev *qmi, struct qmi_request *req, cur_len = *(data++); decode_7bit_field("text", data, end - data, !!(first & 0x40)); + blobmsg_close_table(&status, c); } static enum qmi_cmd_result @@ -365,7 +367,7 @@ cmd_wms_get_message_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct id = strtoul(arg, &err, 10); if (err && *err) { - blobmsg_add_string(&status, "error", "Invalid message ID"); + uqmi_add_error("Invalid message ID"); return QMI_CMD_EXIT; } @@ -385,7 +387,7 @@ static void cmd_wms_get_raw_message_cb(struct qmi_dev *qmi, struct qmi_request * qmi_parse_wms_raw_read_response(msg, &res); data = (unsigned char *) res.data.raw_message_data.raw_data; - str = blobmsg_alloc_string_buffer(&status, "data", res.data.raw_message_data.raw_data_n * 3); + str = blobmsg_alloc_string_buffer(&status, NULL, res.data.raw_message_data.raw_data_n * 3); for (i = 0; i < res.data.raw_message_data.raw_data_n; i++) { str += sprintf(str, " %02x" + (i ? 0 : 1), data[i]); } @@ -545,12 +547,12 @@ cmd_wms_send_message_prepare(struct qmi_dev *qmi, struct qmi_request *req, struc unsigned char dcs = 0x00; if (!send.smsc || !*send.smsc || !send.target || !*send.target) { - blobmsg_add_string(&status, "error", "Missing argument"); + uqmi_add_error("Missing argument"); return QMI_CMD_EXIT; } if (strlen(send.smsc) > 16 || strlen(send.target) > 16 || strlen(arg) > 160) { - blobmsg_add_string(&status, "error", "Argument too long"); + uqmi_add_error("Argument too long"); return QMI_CMD_EXIT; } diff --git a/commands.c b/commands.c index 680b75d..54fcbb0 100644 --- a/commands.c +++ b/commands.c @@ -128,7 +128,10 @@ static void uqmi_print_result(struct blob_attr *data) { char *str; - str = blobmsg_format_json_indent(data, true, 0); + if (!blob_len(data)) + return; + + str = blobmsg_format_json_indent(blob_data(data), false, 0); if (!str) return; @@ -153,7 +156,7 @@ static bool __uqmi_run_commands(struct qmi_dev *qmi, bool option) blob_buf_init(&status, 0); if (cmds[i].handler->type > QMI_SERVICE_CTL && qmi_service_connect(qmi, cmds[i].handler->type, -1)) { - blobmsg_printf(&status, "error", "failed to connect to service"); + uqmi_add_error("Failed to connect to service"); res = QMI_CMD_EXIT; } else { res = cmds[i].handler->prepare(qmi, &req, (void *) buf, cmds[i].arg); @@ -162,8 +165,10 @@ static bool __uqmi_run_commands(struct qmi_dev *qmi, bool option) if (res == QMI_CMD_REQUEST) { qmi_request_start(qmi, &req, (void *) buf, cmds[i].handler->cb); req.no_error_cb = true; - if (qmi_request_wait(qmi, &req)) - blobmsg_add_string(&status, "error", qmi_get_error_str(req.ret)); + if (qmi_request_wait(qmi, &req)) { + uqmi_add_error(qmi_get_error_str(req.ret)); + do_break = true; + } } else if (res == QMI_CMD_EXIT) { do_break = true; } @@ -175,11 +180,21 @@ static bool __uqmi_run_commands(struct qmi_dev *qmi, bool option) return true; } -void uqmi_run_commands(struct qmi_dev *qmi) +void uqmi_add_error(const char *msg) +{ + blobmsg_add_string(&status, NULL, msg); +} + +bool uqmi_run_commands(struct qmi_dev *qmi) { - if (__uqmi_run_commands(qmi, true)) - __uqmi_run_commands(qmi, false); + bool ret; + + ret = __uqmi_run_commands(qmi, true) && + __uqmi_run_commands(qmi, false); + free(cmds); cmds = NULL; n_cmds = 0; + + return ret; } diff --git a/commands.h b/commands.h index 62d63f8..44971c5 100644 --- a/commands.h +++ b/commands.h @@ -48,6 +48,7 @@ enum uqmi_command { extern const struct uqmi_cmd_handler uqmi_cmd_handler[]; void uqmi_add_command(char *arg, int longidx); -void uqmi_run_commands(struct qmi_dev *qmi); +bool uqmi_run_commands(struct qmi_dev *qmi); +void uqmi_add_error(const char *msg); #endif diff --git a/main.c b/main.c index 3a86c84..f2f2e18 100644 --- a/main.c +++ b/main.c @@ -78,7 +78,7 @@ static void handle_exit_signal(int signal) int main(int argc, char **argv) { static struct qmi_dev dev; - int ch; + int ch, ret; uloop_init(); signal(SIGINT, handle_exit_signal); @@ -117,9 +117,9 @@ int main(int argc, char **argv) return 2; } - uqmi_run_commands(&dev); + ret = uqmi_run_commands(&dev) ? 0 : -1; qmi_device_close(&dev); - return 0; + return ret; } -- 2.25.1