From 2e05e40350c3282f4a2f51f75a150ddf93f52c94 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Fri, 17 Apr 2015 22:01:50 +0100 Subject: [PATCH] [464458] mosquitto_sub: Add option to print the payload in hex. Bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=464458 --- ChangeLog.txt | 5 +++++ client/client_shared.c | 5 +++++ client/client_shared.h | 1 + client/sub_client.c | 20 +++++++++++++++++--- man/mosquitto_sub.1.xml | 8 ++++++++ 5 files changed, 36 insertions(+), 3 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index 9fd8b7eb..4d4a50af 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -1,8 +1,13 @@ 1.5 - 2015xxxx ============== +Broker: - Reduce calls to malloc through the use of UHPA. +Client: +- Add -x to mosquitto_sub for printing the payload in hexadecimal format. + + 1.4.1 - 2015xxxx ================ diff --git a/client/client_shared.c b/client/client_shared.c index df8ad1d3..0a31e169 100644 --- a/client/client_shared.c +++ b/client/client_shared.c @@ -629,6 +629,11 @@ int client_config_line_proc(struct mosq_config *cfg, int pub_or_sub, int argc, c goto unknown_option; } cfg->verbose = 1; + }else if(!strcmp(argv[i], "-x")){ + if(pub_or_sub == CLIENT_PUB){ + goto unknown_option; + } + cfg->hex_output = true; }else{ goto unknown_option; } diff --git a/client/client_shared.h b/client/client_shared.h index 0a974b1e..caa20b3e 100644 --- a/client/client_shared.h +++ b/client/client_shared.h @@ -79,6 +79,7 @@ struct mosq_config { int filter_out_count; /* sub */ bool verbose; /* sub */ bool eol; /* sub */ + bool hex_output; /* sub */ int msg_count; /* sub */ #ifdef WITH_SOCKS char *socks5_host; diff --git a/client/sub_client.c b/client/sub_client.c index 3c6c2256..770fb2da 100644 --- a/client/sub_client.c +++ b/client/sub_client.c @@ -33,6 +33,19 @@ Contributors: bool process_messages = true; int msg_count = 0; +static void write_payload(const unsigned char *payload, int payloadlen, bool hex) +{ + int i; + + if(!hex){ + fwrite(payload, 1, payloadlen, stdout); + }else{ + for(i=0; iverbose){ if(message->payloadlen){ printf("%s ", message->topic); - fwrite(message->payload, 1, message->payloadlen, stdout); + write_payload(message->payload, message->payloadlen, cfg->hex_output); if(cfg->eol){ printf("\n"); } @@ -67,7 +80,7 @@ void my_message_callback(struct mosquitto *mosq, void *obj, const struct mosquit fflush(stdout); }else{ if(message->payloadlen){ - fwrite(message->payload, 1, message->payloadlen, stdout); + write_payload(message->payload, message->payloadlen, cfg->hex_output); if(cfg->eol){ printf("\n"); } @@ -137,7 +150,7 @@ void print_usage(void) printf(" [-A bind_address]\n"); #endif printf(" [-i id] [-I id_prefix]\n"); - printf(" [-d] [-N] [--quiet] [-v]\n"); + printf(" [-d] [-N] [--quiet] [-v] [-x]\n"); printf(" [-u username [-P password]]\n"); printf(" [--will-topic [--will-payload payload] [--will-qos qos] [--will-retain]]\n"); #ifdef WITH_TLS @@ -175,6 +188,7 @@ void print_usage(void) printf(" -v : print published messages verbosely.\n"); printf(" -V : specify the version of the MQTT protocol to use when connecting.\n"); printf(" Can be mqttv31 or mqttv311. Defaults to mqttv31.\n"); + printf(" -x : print published message payloads as hexadecimal data.\n"); printf(" --help : display this message.\n"); printf(" --quiet : don't print error messages.\n"); printf(" --will-payload : payload for the client Will, which is sent by the broker in case of\n"); diff --git a/man/mosquitto_sub.1.xml b/man/mosquitto_sub.1.xml index 7e6f6428..6dcf3290 100644 --- a/man/mosquitto_sub.1.xml +++ b/man/mosquitto_sub.1.xml @@ -32,6 +32,7 @@ + username password @@ -455,6 +456,13 @@ the client disconnects unexpectedly. + + + + Print published message payloads as hexadecimal + data. + +