diff --git a/lib/linker.version b/lib/linker.version index c047e87c..9502cc15 100644 --- a/lib/linker.version +++ b/lib/linker.version @@ -94,7 +94,8 @@ MOSQ_1.5 { MOSQ_1.6 { global: - mosquitto_subscribe_multiple; mosquitto_property_free_all; mosquitto_property_command_check; + mosquitto_string_to_command; + mosquitto_subscribe_multiple; } MOSQ_1.5; diff --git a/lib/mosquitto.c b/lib/mosquitto.c index e5bd3292..ebecbdc9 100644 --- a/lib/mosquitto.c +++ b/lib/mosquitto.c @@ -493,6 +493,40 @@ const char *mosquitto_reason_string(int reason_code) } } + +int mosquitto_string_to_command(const char *str, int *cmd) +{ + if(!strcasecmp(str, "connect")){ + *cmd = CMD_CONNECT; + }else if(!strcasecmp(str, "connack")){ + *cmd = CMD_CONNACK; + }else if(!strcasecmp(str, "publish")){ + *cmd = CMD_PUBLISH; + }else if(!strcasecmp(str, "puback")){ + *cmd = CMD_PUBACK; + }else if(!strcasecmp(str, "pubrec")){ + *cmd = CMD_PUBREC; + }else if(!strcasecmp(str, "pubrel")){ + *cmd = CMD_PUBREL; + }else if(!strcasecmp(str, "pubcomp")){ + *cmd = CMD_PUBCOMP; + }else if(!strcasecmp(str, "subscribe")){ + *cmd = CMD_SUBSCRIBE; + }else if(!strcasecmp(str, "unsubscribe")){ + *cmd = CMD_UNSUBSCRIBE; + }else if(!strcasecmp(str, "disconnect")){ + *cmd = CMD_DISCONNECT; + }else if(!strcasecmp(str, "auth")){ + *cmd = CMD_AUTH; + }else if(!strcasecmp(str, "will")){ + *cmd = CMD_WILL; + }else{ + return MOSQ_ERR_INVAL; + } + return MOSQ_ERR_SUCCESS; +} + + int mosquitto_sub_topic_tokenise(const char *subtopic, char ***topics, int *count) { int len; diff --git a/lib/mosquitto.h b/lib/mosquitto.h index 129560c0..9ff9f787 100644 --- a/lib/mosquitto.h +++ b/lib/mosquitto.h @@ -1568,6 +1568,25 @@ libmosq_EXPORT const char *mosquitto_strerror(int mosq_errno); */ libmosq_EXPORT const char *mosquitto_connack_string(int connack_code); +/* Function: mosquitto_string_to_command + * + * Take a string input representing an MQTT command and convert it to the + * libmosquitto integer representation. + * + * Parameters: + * str - the string to parse. + * cmd - pointer to an int, for the result. + * + * Returns: + * MOSQ_ERR_SUCCESS - on success + * MOSQ_ERR_INVAL - on an invalid input. + * + * Example: + * mosquitto_string_to_command("CONNECT", &cmd); + * // cmd == CMD_CONNECT + */ +libmosq_EXPORT int mosquitto_string_to_command(const char *str, int *cmd); + /* * Function: mosquitto_sub_topic_tokenise *