diff --git a/include/libwebsockets/lws-secure-streams-policy.h b/include/libwebsockets/lws-secure-streams-policy.h index 967ef8dd2..7cf3dce4c 100644 --- a/include/libwebsockets/lws-secure-streams-policy.h +++ b/include/libwebsockets/lws-secure-streams-policy.h @@ -211,7 +211,16 @@ typedef struct lws_ss_policy { struct { const char *topic; /* stream sends on this topic */ const char *subscribe; /* stream subscribes to this topic */ + + const char *will_topic; + const char *will_message; + + uint16_t keep_alive; uint8_t qos; + uint8_t clean_start; + uint8_t will_qos; + uint8_t will_retain; + } mqtt; /* details for non-http related protocols... */ diff --git a/lib/secure-streams/README.md b/lib/secure-streams/README.md index baf7c370c..b970d3aa0 100644 --- a/lib/secure-streams/README.md +++ b/lib/secure-streams/README.md @@ -292,6 +292,55 @@ Set the topic this streamtype subscribes to Set the QOS level for this streamtype +### `mqtt_keep_alive` + +16-bit number representing MQTT keep alive for the stream. + +This is applied at connection time... where different streams may bind to the +same underlying MQTT connection, all the streams should have an identical +setting for this. + +### `mqtt_clean_start` + +Set to true if the connection should use MQTT's "clean start" feature. + +This is applied at connection time... where different streams may bind to the +same underlying MQTT connection, all the streams should have an identical +setting for this. + +### `mqtt_will_topic` + +Set the topic of the connection's will message, if any (there is none by default). + +This is applied at connection time... where different streams may bind to the +same underlying MQTT connection, all the streams should have an identical +setting for this. + +### `mqtt_will_message` + +Set the content of the connect's will message, if any (there is none by default). + +This is applied at connection time... where different streams may bind to the +same underlying MQTT connection, all the streams should have an identical +setting for this. + +### `mqtt_will_qos` + +Set the QoS of the will message, if any (there is none by default). + +This is applied at connection time... where different streams may bind to the +same underlying MQTT connection, all the streams should have an identical +setting for this. + +### `mqtt_will_retain` + +Set to true if the connection should use MQTT's "will retain" feature, if there +is a will message (there is none by default). + +This is applied at connection time... where different streams may bind to the +same underlying MQTT connection, all the streams should have an identical +setting for this. + ## Loading and using updated remote policy If the default, hardcoded policy includes a streamtype `fetch_policy`, diff --git a/lib/secure-streams/policy.c b/lib/secure-streams/policy.c index 0ef8f1d54..ec59e7fce 100644 --- a/lib/secure-streams/policy.c +++ b/lib/secure-streams/policy.c @@ -85,6 +85,12 @@ static const char * const lejp_tokens_policy[] = { "s[].*.mqtt_topic", "s[].*.mqtt_subscribe", "s[].*.mqtt_qos", + "s[].*.mqtt_keep_alive", + "s[].*.mqtt_clean_start", + "s[].*.mqtt_will_topic", + "s[].*.mqtt_will_message", + "s[].*.mqtt_will_qos", + "s[].*.mqtt_will_retain", "s[].*", }; @@ -142,6 +148,12 @@ typedef enum { LSSPPT_MQTT_TOPIC, LSSPPT_MQTT_SUBSCRIBE, LSSPPT_MQTT_QOS, + LSSPPT_MQTT_KEEPALIVE, + LSSPPT_MQTT_CLEAN_START, + LSSPPT_MQTT_WILL_TOPIC, + LSSPPT_MQTT_WILL_MESSAGE, + LSSPPT_MQTT_WILL_QOS, + LSSPPT_MQTT_WILL_RETAIN, LSSPPT_STREAMTYPES } policy_token_t; @@ -657,6 +669,30 @@ lws_ss_policy_parser_cb(struct lejp_ctx *ctx, char reason) a->curr[LTY_POLICY].p->u.mqtt.qos = atoi(ctx->buf); break; + case LSSPPT_MQTT_KEEPALIVE: + a->curr[LTY_POLICY].p->u.mqtt.keep_alive = atoi(ctx->buf); + break; + + case LSSPPT_MQTT_CLEAN_START: + a->curr[LTY_POLICY].p->u.mqtt.clean_start = + reason == LEJPCB_VAL_TRUE; + break; + case LSSPPT_MQTT_WILL_TOPIC: + pp = (char **)&a->curr[LTY_POLICY].p->u.mqtt.will_topic; + goto string2; + + case LSSPPT_MQTT_WILL_MESSAGE: + pp = (char **)&a->curr[LTY_POLICY].p->u.mqtt.will_message; + goto string2; + + case LSSPPT_MQTT_WILL_QOS: + a->curr[LTY_POLICY].p->u.mqtt.will_qos = atoi(ctx->buf); + break; + case LSSPPT_MQTT_WILL_RETAIN: + a->curr[LTY_POLICY].p->u.mqtt.will_retain = + reason == LEJPCB_VAL_TRUE; + break; + case LSSPPT_PROTOCOL: a->curr[LTY_POLICY].p->protocol = 0xff; for (n = 0; n < (int)LWS_ARRAY_SIZE(protonames); n++) diff --git a/lib/secure-streams/protocols/ss-mqtt.c b/lib/secure-streams/protocols/ss-mqtt.c index 2d53c6620..99a36b615 100644 --- a/lib/secure-streams/protocols/ss-mqtt.c +++ b/lib/secure-streams/protocols/ss-mqtt.c @@ -204,12 +204,12 @@ secstream_connect_munge_mqtt(lws_ss_handle_t *h, char *buf, size_t len, memset(&ct->ccp, 0, sizeof(ct->ccp)); ct->ccp.client_id = "lwsMqttClient"; - ct->ccp.keep_alive = 60; - ct->ccp.clean_start = 1; - ct->ccp.will_param.topic = "good/bye"; - ct->ccp.will_param.message = "sign-off"; - ct->ccp.will_param.qos = 0; - ct->ccp.will_param.retain = 0; + ct->ccp.keep_alive = h->policy->u.mqtt.keep_alive; + ct->ccp.clean_start = h->policy->u.mqtt.clean_start; + ct->ccp.will_param.topic = h->policy->u.mqtt.will_topic; + ct->ccp.will_param.message = h->policy->u.mqtt.will_message; + ct->ccp.will_param.qos = h->policy->u.mqtt.will_qos; + ct->ccp.will_param.retain = h->policy->u.mqtt.will_retain; lwsl_notice("%s\n", __func__);