Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XDK - MQTT: Automatically ping every (keepAliveInterval/2) #353

Merged
merged 2 commits into from
Jul 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ class MqttGenerator extends AbstractSystemResourceGenerator {
«generatorUtils.generateExceptionHandler(setup, "exception")»

mqttSession.MQTTVersion = 4;
mqttSession.keepAliveInterval = 60;
mqttSession.keepAliveInterval = «configuration.getExpression("keepAliveInterval")?.code»;
mqttSession.cleanSession = false;
«IF lastWill instanceof SumTypeRepr»
«IF lastWill.hasLastWill»
Expand Down Expand Up @@ -338,6 +338,16 @@ class MqttGenerator extends AbstractSystemResourceGenerator {
exception = RETCODE(RETCODE_SEVERITY_ERROR, RETCODE_MQTT_PARSING_ERROR);
}

if(exception == RETCODE_OK) {
TimerHandle_t pingTimerHandle = xTimerCreate("mqttPing", UINT32_C(500*«configuration.getExpression("keepAliveInterval").code»), pdTRUE, NULL, mqttPing);
if(pingTimerHandle != NULL) {
xTimerStart(pingTimerHandle, 0);
}
else {
«loggingGenerator.generateLogStatement(LogLevel.Error, "MQTT_Enable : Failed to create ping task")»
}
}

return exception;
''')
.addHeader("BCDS_BSP_Board.h", true)
Expand All @@ -346,6 +356,8 @@ class MqttGenerator extends AbstractSystemResourceGenerator {
.addHeader("HTTPRestClientSecurity.h", true)
.addHeader("time.h", true)
.addHeader("XDK_TimeStamp.h", true)
.addHeader("Serval_StringDescr.h", true)
.addHeader("timers.h", true)
result.setPreamble('''
«IF auth instanceof SumTypeRepr»
«IF auth.isLogin()»
Expand All @@ -367,7 +379,7 @@ class MqttGenerator extends AbstractSystemResourceGenerator {
const char* lastWillMessageBuf = «lastWill.properties.get("message").code»;
«ENDIF»
«ENDIF»
''').addHeader("Serval_StringDescr.h", true);
''')


return result;
Expand Down Expand Up @@ -462,6 +474,8 @@ class MqttGenerator extends AbstractSystemResourceGenerator {
exception = RETCODE(RETCODE_SEVERITY_ERROR, RETCODE_SEMAPHORE_ERROR);
}
break;
case MQTT_PING_RESPONSE_RECEIVED:
break;
default:
«loggingGenerator.generateLogStatement(LogLevel.Info, "MqttEventHandler : Unhandled MQTT Event: %x", codeFragmentProvider.create('''event'''))»
break;
Expand All @@ -475,10 +489,21 @@ class MqttGenerator extends AbstractSystemResourceGenerator {
return RC_OK;
}

static void mqttPing(void* userParameter1, uint32_t userParameter2) {
if(!mqttIsConnected) {
«loggingGenerator.generateLogStatement(LogLevel.Warning, "MQTT: Ping failed: not connected")»
return;
}
retcode_t rc = Mqtt_ping(&mqttSession);
if(RC_OK != rc) {
«loggingGenerator.generateLogStatement(LogLevel.Error, "MQTT: Ping failed: %x", codeFragmentProvider.create('''rc'''))»
}
}

/**
* Connects to a configured backend.
*/
Retcode_T connectToBackend(void) {
static Retcode_T connectToBackend(void) {
/* This is a dummy take. In case of any callback received
* after the previous timeout will be cleared here. */
(void) xSemaphoreTake(mqttConnectHandle, 0UL);
Expand All @@ -503,6 +528,7 @@ class MqttGenerator extends AbstractSystemResourceGenerator {
.setPreamble('''
static retcode_t MqttEventHandler(MqttSession_T* session, MqttEvent_t event, const MqttEventData_t* eventData);
static Retcode_T connectToBackend(void);
static void mqttPing(void* userParameter1, uint32_t userParameter2);
''')
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ class MqttValidator implements IResourceValidator {
}
}

def validateKeepAliveInterval(SystemResourceSetup setup, ValidationMessageAcceptor acceptor) {
val keepAliveValueObj = setup.configurationItemValues.findFirst[ it.item.name == "keepAliveInterval"];
val keepAliveValue = StaticValueInferrer.infer(keepAliveValueObj.value, []);
if(keepAliveValue instanceof Long) {
if(keepAliveValue <= 0) {
acceptor.acceptError("keepAliveInterval must be greater than 0", keepAliveValueObj, ProgramPackage.Literals.CONFIGURATION_ITEM_VALUE__VALUE, 0, null);
}
}
}

def validateUrl(SystemResourceSetup setup, ValidationMessageAcceptor acceptor) {
val urlConfigValue = setup.configurationItemValues.findFirst[ it.item.name == "url"];
val url = setup.getConfigurationItemValue("url");
Expand Down
2 changes: 1 addition & 1 deletion website/site/content/platforms/xdk110.md
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ every 100 milliseconds {
| `authentication: MqttAuthentication` | Username/Password authentication or unauthenticated, for example `authentication = Login("user", "pass")`. Default: `None()`.
| `lastWill: MqttWill` | The last will that will be published on a forceful disconnect. Either `NoWill()` or `LastWill(topic="topic", message="message", qos=1)`. Default: `NoWill()`.
| `cleanSession: bool` | The clean session flag indicates to the broker whether the client wants to establish a clean session or a persistent session where all subscriptions and messages (QoS 1 & 2) are stored for the client. Default: `false`.
| `keepAliveInterval: uint32` | The keep alive interval (in seconds) is the time the client commits to for when sending regular pings to the broker. The broker responds to the pings enabling both sides to determine if the other one is still alive and reachable. Default: `60`.
| `keepAliveInterval: uint32` | The keep alive interval (in seconds) is the time the client commits to for when sending regular pings to the broker. The broker responds to the pings enabling both sides to determine if the other one is still alive and reachable. Creates an automatic ping task that pings twice in this interval. Default: `60`.
| `certificatePath: string` | The path to the expected server certificate used for MQTT with TLS. Can be an absolute path like `C:\certificates\mosquitto.crt` or `/etc/ssl/certs/mosquitto.crt` or a relative path like `certificates/server.crt`. In the latter case this will look for the certificate relative to the project root. The certificate needs to be in PEM format (`-----BEGIN CERTIFICATE-----`/`-----END CERTIFICATE-----`). **This configuration item is required when using MQTT over TLS.**
| `sntpServer: string` | To verify the server's certificate the XDK needs to get the current time over SNTP. Per default it connects to `pool.ntp.org:123`, however if for some reason like being in a closed network you cannot access this NTP server you can specify your own.

Expand Down