Skip to content

Commit

Permalink
Implement WLAN reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
wegendt-bosch committed Jul 30, 2019
1 parent 3f65d6a commit 619d5e0
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 3 deletions.
Expand Up @@ -175,6 +175,7 @@ class SystemResourceHandlingGenerator {
«internalGenerator?.generateAdditionalImplementation()»
''')
.addHeader('MitaExceptions.h', false)
.addHeader(obj.fileBasename + ".h", false)
.toImplementation(context))
return cgn;
}
Expand Down
Expand Up @@ -42,7 +42,7 @@ class MqttGenerator extends AbstractSystemResourceGenerator {
protected extension StatementGenerator statementGenerator

@Inject
protected GeneratorUtils generatorUtils
protected extension GeneratorUtils generatorUtils

override generateAdditionalFiles(IFileSystemAccess2 fsa) {
val brokerUri = new URI(configuration.getString("url"));
Expand Down Expand Up @@ -194,6 +194,7 @@ class MqttGenerator extends AbstractSystemResourceGenerator {
.addHeader("stdint.h", true, IncludePath.HIGH_PRIORITY)
.addHeader("XdkCommonInfo.h", true)
.addHeader("BCDS_WlanNetworkConfig.h", true)
.addHeader(setup.getConfigurationItemValue("transport").baseName + ".h", false)
if(isSecure) {
result.addHeader("HTTPRestClientSecurity.h", true)
Expand Down Expand Up @@ -383,6 +384,9 @@ class MqttGenerator extends AbstractSystemResourceGenerator {
}
override generateAdditionalImplementation() {
val brokerUri = new URI(configuration.getString("url"));
val isSecure = brokerUri.scheme == "mqtts";
codeFragmentProvider.create('''
/**
* @brief Callback function used by the stack to communicate events to the application.
Expand Down Expand Up @@ -416,6 +420,8 @@ class MqttGenerator extends AbstractSystemResourceGenerator {
case MQTT_CONNECT_SEND_FAILED:
case MQTT_CONNECT_TIMEOUT:
mqttIsConnected = false;
«loggingGenerator.generateLogStatement(LogLevel.Warning, "MQTT_Event : Connection timeout -> disconnected. Will try to reconnect on next send."
Mqtt_disconnect(&mqttSession);
if (pdTRUE != xSemaphoreGive(mqttConnectHandle))
{
exception = RETCODE(RETCODE_SEVERITY_ERROR, RETCODE_SEMAPHORE_ERROR);
Expand Down Expand Up @@ -469,6 +475,8 @@ class MqttGenerator extends AbstractSystemResourceGenerator {

if (RETCODE_OK != exception)
{
mqttIsConnected = false;
Mqtt_disconnect(&mqttSession);
Retcode_RaiseError(exception);
}

Expand All @@ -479,12 +487,35 @@ class MqttGenerator extends AbstractSystemResourceGenerator {
* Connects to a configured backend.
*/
Retcode_T connectToBackend(void) {
Retcode_T exception = NO_EXCEPTION, tempException = NO_EXCEPTION;
bool exceptionHappened = false;
«IF isSecure»
tempException = SNTP_Disable();
if(!exceptionHappened && tempException != NO_EXCEPTION) {
exception = tempException;
exceptionHappened = true;
}
«ENDIF»
exception = CheckWlanConnectivityAndReconnect();
if(!exceptionHappened && tempException != NO_EXCEPTION) {
exception = tempException;
exceptionHappened = true;
}
«IF isSecure»
exception = SNTP_Enable();
if(!exceptionHappened && tempException != NO_EXCEPTION) {
exception = tempException;
exceptionHappened = true;
}
«ENDIF»
«generatorUtils.generateExceptionHandler(null, "exception"

/* This is a dummy take. In case of any callback received
* after the previous timeout will be cleared here. */
(void) xSemaphoreTake(mqttConnectHandle, 0UL);
retcode_t rc = Mqtt_connect(&mqttSession);
if(RC_OK != rc) {
«loggingGenerator.generateLogStatement(LogLevel.Error, "MQTT_Connect : Failed to connect MQTT: 0x%d", codeFragmentProvider.create('''rc'''))»
«loggingGenerator.generateLogStatement(LogLevel.Error, "MQTT_Connect : Failed to connect MQTT: 0x%x", codeFragmentProvider.create('''rc'''))»
return RETCODE(RETCODE_SEVERITY_ERROR, RETCODE_MQTT_CONNECT_FAILED);
}
if (pdTRUE != xSemaphoreTake(mqttConnectHandle, pdMS_TO_TICKS(30000)))
Expand All @@ -495,6 +526,7 @@ class MqttGenerator extends AbstractSystemResourceGenerator {
if (!mqttIsConnected)
{
«loggingGenerator.generateLogStatement(LogLevel.Error, "MQTT_Connect : Failed to connect"
Mqtt_disconnect(&mqttSession);
return RETCODE(RETCODE_SEVERITY_ERROR, RETCODE_MQTT_CONNECT_STATUS_ERROR);
}
return RETCODE_OK;
Expand Down Expand Up @@ -530,6 +562,7 @@ class MqttGenerator extends AbstractSystemResourceGenerator {
}
else {
«loggingGenerator.generateLogStatement(LogLevel.Error, "MQTT_Write : Connection failed!"
return RETCODE(RETCODE_SEVERITY_ERROR, RETCODE_MQTT_PUBLISH_FAILED);
}
}
«generatorUtils.generateExceptionHandler(signalInstance, "exception"
Expand Down
Expand Up @@ -110,6 +110,7 @@ class RestClientGenerator extends AbstractSystemResourceGenerator {
.addHeader('stdio.h', true)
.addHeader('FreeRTOS.h', true, IncludePath.HIGH_PRIORITY)
.addHeader('semphr.h', true)
.addHeader(setup.getConfigurationItemValue("transport").baseName + ".h", false)
}

override generateSignalInstanceSetter(SignalInstance signalInstance, String variableName) {
Expand All @@ -121,15 +122,19 @@ class RestClientGenerator extends AbstractSystemResourceGenerator {
val port = if(baseUrl.port < 0) 80 else baseUrl.port;

codeFragmentProvider.create('''
Retcode_T exception = RETCODE_OK;
size_t messageLength = strlen((const char*) *«variableName») + 1;
exception = CheckWlanConnectivityAndReconnect();
«generateExceptionHandler(signalInstance, "exception")»
if(messageLength > sizeof(httpBodyBuffer))
{
return EXCEPTION_INDEXOUTOFBOUNDSEXCEPTION;
}
memcpy(httpBodyBuffer, *«variableName», messageLength);
Retcode_T exception = RETCODE_OK;
Ip_Address_T destAddr;
exception = NetworkConfig_GetIpAddress((uint8_t*) «setup.baseName.toUpperCase»_HOST, &destAddr);
if (exception != RETCODE_OK)
Expand Down
Expand Up @@ -236,6 +236,59 @@ class WlanGenerator extends AbstractSystemResourceGenerator {
}
return exception;
}
Retcode_T CheckWlanConnectivityAndReconnect(void)
{
Retcode_T exception = RETCODE_OK;
WlanNetworkConnect_ScanInterval_T scanInterval = 5;
WlanNetworkConnect_ScanList_T scanList;
WlanNetworkConnect_IpStatus_T nwStatus;
bool networkStatusFlag = false;
nwStatus = WlanNetworkConnect_GetIpStatus();
if (WLANNWCT_IPSTATUS_CT_AQRD != nwStatus)
{
printf("Checking for network availability and trying to connect again\n");
exception = WlanNetworkConnect_ScanNetworks(scanInterval, &scanList);
if (RETCODE_OK == exception)
{
for (int i = 0U; i < WLANNWCT_MAX_SCAN_INFO_BUF; i++)
{
if (0U == strcmp((char *) NETWORK_SSID, (char *) scanList.ScanData[i].Ssid))
{
networkStatusFlag = true;
printf("Network with SSID %s is available\n", NETWORK_SSID);
exception = ConnectivityWLANWifi_Enable();
if (RETCODE_OK != exception)
{
printf("Not able to connect to the network\n");
}
break;
}
else
{
networkStatusFlag = false;
}
}
if (false == networkStatusFlag)
{
exception = RETCODE(RETCODE_SEVERITY_ERROR, RETCODE_WLAN_NETWORK_NOT_AVAILABLE);
printf("Network with SSID %s is not available\n", NETWORK_SSID);
}
}
else if ((uint32_t) RETCODE_NO_NW_AVAILABLE == Retcode_GetCode(exception))
{
printf("Network not available\n");
}
}
else
{
printf("Network Connection is active\n");
}
return exception;
}
''')
.addHeader('XdkCommonInfo.h', true, IncludePath.HIGH_PRIORITY)
.addHeader('BCDS_Basics.h', true, IncludePath.VERY_HIGH_PRIORITY)
Expand All @@ -257,6 +310,13 @@ class WlanGenerator extends AbstractSystemResourceGenerator {
}
return result
}
override generateAdditionalHeaderContent() {
return codeFragmentProvider.create('''
Retcode_T CheckWlanConnectivityAndReconnect(void);
''');
}
private def CodeFragment buildStatusCallbacks(SystemResourceSetup component) {
val baseName = component.baseName
Expand Down

0 comments on commit 619d5e0

Please sign in to comment.