source/MQTTRegistration.c
/**
 * @brief callback function for subriptions
 *        toggles LEDS or sets read data flag
 *
 * @param[in] md - received message from the MQTT Broker
 *
 * @return NONE
 */
@brief callback function for subriptions
toggles LEDS or sets read data flag

@param[in] md - received message from the MQTT Broker

@return NONE
@brief callback function for subriptions toggles LEDS or sets read data flag @param [ in ] md - received message from the MQTT Broker @return NONE
static void MQTTRegistration_ClientReceive(MQTT_SubscribeCBParam_TZ param) {
	memset(appIncomingMsgPayloadBuffer, 0, sizeof(appIncomingMsgPayloadBuffer));
	memset(appIncomingMsgTopicBuffer, 0, sizeof(appIncomingMsgTopicBuffer));
	strncpy(appIncomingMsgTopicBuffer, (const char *) param.Topic,
			fmin(param.TopicLength, (sizeof(appIncomingMsgTopicBuffer) - 1U)));
	strncpy(appIncomingMsgPayloadBuffer, (const char *) param.Payload,
			fmin(param.PayloadLength,
					(sizeof(appIncomingMsgPayloadBuffer) - 1U)));
	LOG_AT_DEBUG(
			("%s: Received new message on topic: %s from upstream: [%s]\n\r", MQTTREGISTRATION_LOGPREFIX, appIncomingMsgTopicBuffer, appIncomingMsgPayloadBuffer));
	if ((strncmp(param.Topic, TOPIC_CREDENTIAL, param.TopicLength) == 0)) {
		AppController_SetAppStatus(APP_STATUS_REGISTERED);
		char username[SIZE_SMALL_BUF] = { 0 };
		char tenant[SIZE_XSMALL_BUF] = { 0 };
		char password[SIZE_XSMALL_BUF] = { 0 };
		int token_pos = 0; 
		int command_pos = 0; 
		char *token = strtok(appIncomingMsgPayloadBuffer, ",");
		while (token != NULL) {
			LOG_AT_TRACE(("%s: Parsed token: [%s], command_pos: %i token_pos: %i \n\r", MQTTREGISTRATION_LOGPREFIX,
							token, command_pos, token_pos));
			if (token_pos == 0
					&& strcmp(token, TEMPLATE_STD_CREDENTIALS) == 0) {
				LOG_AT_TRACE(("%s: Correct message type \n\r", MQTTREGISTRATION_LOGPREFIX));
				command_pos = 1; 
			} else if (command_pos == 1 && token_pos == 1) {
				snprintf(tenant, SIZE_XSMALL_BUF, token);
				LOG_AT_TRACE(("%s: Found tenant: [%s]\n\r", MQTTREGISTRATION_LOGPREFIX, token));
			} else if (command_pos == 1 && token_pos == 2) {
				snprintf(username, SIZE_XSMALL_BUF, "%s/%s", tenant, token);
				LOG_AT_TRACE(("%s: Found username: [%s], [%s]\n\r", MQTTREGISTRATION_LOGPREFIX, token, username));
			} else if (command_pos == 1 && token_pos == 3) {
				snprintf(password, SIZE_XSMALL_BUF, token);
				LOG_AT_TRACE(("%s: Found password: [%s]\n\r", MQTTREGISTRATION_LOGPREFIX, token));
			} else {
				LOG_AT_TRACE(("%s: Wrong message format\n\r", MQTTREGISTRATION_LOGPREFIX));
				assert(0);
			}
			token = strtok(NULL, ", ");
			token_pos++;
		}
		char credentials[SIZE_SMALL_BUF] = { 0 };
		snprintf(credentials, SIZE_SMALL_BUF, "\n%s=%s\n%s=%s\n",
				ATT_KEY_NAME[5], username, ATT_KEY_NAME[6], password);
		MQTTStorage_SD_AppendCredentials(credentials);
		MQTTCfgParser_SetMqttUser(username);
		MQTTCfgParser_SetMqttPassword(password);
		MQTTCfgParser_FLWriteConfig();
		MQTTRegistration_StartRestartTimer(2000);
	}
}
static void MQTTRegistration_ClientReceive ( MQTT_SubscribeCBParam_TZ param ) { memset ( appIncomingMsgPayloadBuffer , 0 , sizeof ( appIncomingMsgPayloadBuffer ) ) ; memset ( appIncomingMsgTopicBuffer , 0 , sizeof ( appIncomingMsgTopicBuffer ) ) ; strncpy ( appIncomingMsgTopicBuffer , ( const char * ) param . Topic , fmin ( param . TopicLength , ( sizeof ( appIncomingMsgTopicBuffer ) - 1U ) ) ) ; strncpy ( appIncomingMsgPayloadBuffer , ( const char * ) param . Payload , fmin ( param . PayloadLength , ( sizeof ( appIncomingMsgPayloadBuffer ) - 1U ) ) ) ; LOG_AT_DEBUG ( ( " \n \r " , MQTTREGISTRATION_LOGPREFIX , appIncomingMsgTopicBuffer , appIncomingMsgPayloadBuffer ) ) ; if ( ( strncmp ( param . Topic , TOPIC_CREDENTIAL , param . TopicLength ) == 0 ) ) { AppController_SetAppStatus ( APP_STATUS_REGISTERED ) ; char username [ SIZE_SMALL_BUF ] = { 0 } ; char tenant [ SIZE_XSMALL_BUF ] = { 0 } ; char password [ SIZE_XSMALL_BUF ] = { 0 } ; int token_pos = 0 ; int command_pos = 0 ; char * token = strtok ( appIncomingMsgPayloadBuffer , " " ) ; while ( token != NULL ) { LOG_AT_TRACE ( ( " \n \r " , MQTTREGISTRATION_LOGPREFIX , token , command_pos , token_pos ) ) ; if ( token_pos == 0 && strcmp ( token , TEMPLATE_STD_CREDENTIALS ) == 0 ) { LOG_AT_TRACE ( ( " \n \r " , MQTTREGISTRATION_LOGPREFIX ) ) ; command_pos = 1 ; } else if ( command_pos == 1 && token_pos == 1 ) { snprintf ( tenant , SIZE_XSMALL_BUF , token ) ; LOG_AT_TRACE ( ( " \n \r " , MQTTREGISTRATION_LOGPREFIX , token ) ) ; } else if ( command_pos == 1 && token_pos == 2 ) { snprintf ( username , SIZE_XSMALL_BUF , " " , tenant , token ) ; LOG_AT_TRACE ( ( " \n \r " , MQTTREGISTRATION_LOGPREFIX , token , username ) ) ; } else if ( command_pos == 1 && token_pos == 3 ) { snprintf ( password , SIZE_XSMALL_BUF , token ) ; LOG_AT_TRACE ( ( " \n \r " , MQTTREGISTRATION_LOGPREFIX , token ) ) ; } else { LOG_AT_TRACE ( ( " \n \r " , MQTTREGISTRATION_LOGPREFIX ) ) ; assert ( 0 ) ; } token = strtok ( NULL , " " ) ; token_pos ++ ; } char credentials [ SIZE_SMALL_BUF ] = { 0 } ; snprintf ( credentials , SIZE_SMALL_BUF , " \n \n \n " , ATT_KEY_NAME [ 5 ] , username , ATT_KEY_NAME [ 6 ] , password ) ; MQTTStorage_SD_AppendCredentials ( credentials ) ; MQTTCfgParser_SetMqttUser ( username ) ; MQTTCfgParser_SetMqttPassword ( password ) ; MQTTCfgParser_FLWriteConfig ( ) ; MQTTRegistration_StartRestartTimer ( 2000 ) ; } }
@brief callback function for subriptions
        toggles LEDS or sets read data flag
@brief callback function for subriptions toggles LEDS or sets read data flag
/* Initialize Variables */ /* sample msg: 70,iotconnected,device_XDK_boot_123,l5vHRc%lFt */ //extract password and  username //mark position of token //mark positon for command which is parsed // mark that we are in a credential notification message // found tenant // found username // append credentials at the end of config.txt