pespin has submitted this change. ( https://gerrit.osmocom.org/c/osmo-uecups/+/42455?usp=email )
Change subject: cups_client: SPlit Tx of json str into its own function ......................................................................
cups_client: SPlit Tx of json str into its own function
This helper can be used in the future to submit a str multiple times (eg. to multiple cups clients) without having to re-allocate the content.
Change-Id: I505bf2eeea998a749e55f8f3b05c5013b5761297 --- M daemon/cups_client.c 1 file changed, 22 insertions(+), 16 deletions(-)
Approvals: pespin: Looks good to me, approved Jenkins Builder: Verified laforge: Looks good to me, but someone else must approve fixeria: Looks good to me, but someone else must approve
diff --git a/daemon/cups_client.c b/daemon/cups_client.c index acadcd5..e283f23 100644 --- a/daemon/cups_client.c +++ b/daemon/cups_client.c @@ -85,13 +85,30 @@ talloc_free(sproc); }
+static int cups_client_tx_json_str(struct cups_client *cc, const char *json_str, unsigned int json_strlen) +{ + struct msgb *msg = msgb_alloc(CUPS_MSGB_SIZE, "Tx JSON"); + char *out; + LOGCC(cc, LOGL_DEBUG, "JSON Tx '%s'\n", json_str); + + if (json_strlen > msgb_tailroom(msg)) { + LOGCC(cc, LOGL_ERROR, "Not enough room for JSON in msgb\n"); + return 0; + } + + out = (char *)msgb_put(msg, json_strlen); + memcpy(out, json_str, json_strlen); + osmo_stream_srv_send(cc->srv, msg); + + return 0; +} + /* Send JSON to a given client/connection */ int cups_client_tx_json(struct cups_client *cc, json_t *jtx) { - struct msgb *msg = msgb_alloc(CUPS_MSGB_SIZE, "Tx JSON"); char *json_str = json_dumps(jtx, JSON_SORT_KEYS); - char *out; - int json_strlen; + unsigned int json_strlen; + int rc;
json_decref(jtx); if (!json_str) { @@ -100,20 +117,9 @@ } json_strlen = strlen(json_str);
- LOGCC(cc, LOGL_DEBUG, "JSON Tx '%s'\n", json_str); - - if (json_strlen > msgb_tailroom(msg)) { - LOGCC(cc, LOGL_ERROR, "Not enough room for JSON in msgb\n"); - free(json_str); - return 0; - } - - out = (char *)msgb_put(msg, json_strlen); - memcpy(out, json_str, json_strlen); + rc = cups_client_tx_json_str(cc, json_str, json_strlen); free(json_str); - osmo_stream_srv_send(cc->srv, msg); - - return 0; + return rc; }
json_t *gen_uecups_result(const char *name, const char *res)