src/mqtt_json_parse.c
void mqtt_json_parse_message(char* message, unsigned int length)
{
	cJSON *jsonMessage;
	cJSON *jsonBoxId;
	cJSON *jsonMsgId;
	cJSON *jsonMsgType;
	mqtt_json_result_t result = MQTT_PARSE_SUCCESS;
	printf("Parse message with length: %d\r\n", length);

	char* responseMessage;
	if ((message == NULL) || (length == 0))
		return;
	char* procBuffer = (char*)malloc(length + 1);
	memcpy(procBuffer, message, length);
	procBuffer[length] = 0;
	jsonMessage = cJSON_Parse(procBuffer);
	if (jsonMessage == NULL)
	{
		result = MQTT_PARSE_MESSAGE_ERROR;
		goto END_PARSE;
	}
	jsonBoxId = cJSON_GetObjectItem(jsonMessage, "id");
	if(cJSON_IsString(jsonBoxId) && (jsonBoxId->valuestring != NULL))
	{
		printf("BOX ID: %s\r\n", jsonBoxId->valuestring);
	}
	else
	{
		result = MQTT_PARSE_BOXID_ERROR;
		goto END_PARSE;
	}
	jsonMsgId = cJSON_GetObjectItem(jsonMessage, "message_id");
	if(cJSON_IsNumber(jsonMsgId))
	{
		printf("Message ID: %d\r\n", jsonMsgId->valueint);
	}
	else
	{
		result = MQTT_PARSE_MSGID_ERROR;
		goto END_PARSE;
	}
	jsonMsgType = cJSON_GetObjectItem(jsonMessage, "type");
	if (cJSON_IsString(jsonMsgType) && (jsonMsgType->valuestring != NULL))
	{
		printf("Message Type: %s\r\n", jsonMsgType->valuestring);
	}
	else
	{
		result = MQTT_PARSE_TYPE_ERROR;
		goto END_PARSE;
	}
	if (!strcmp(jsonMsgType->valuestring, "config"))
	{
		printf("Parse configuration message\r\n");
		result = mqtt_json_parse_configure_message(jsonMessage);
	}
	else if (!strcmp(jsonMsgType->valuestring, "control"))
	{
		printf("Parse control message \r\n");
		result = mqtt_json_parse_control_message(jsonMessage);
	}
	else if (!strcmp(jsonMsgType->valuestring, "reset"))
	{
		printf("Resetting ...\r\n");
	}
	else
		result = MQTT_PARSE_TYPE_ERROR;

	END_PARSE:
	if ((result != MQTT_PARSE_MESSAGE_ERROR) && (result != MQTT_PARSE_BOXID_ERROR) && (result != MQTT_PARSE_MSGID_ERROR))
	{
		if (result == MQTT_PARSE_SUCCESS)
			responseMessage = mqtt_json_make_response(jsonBoxId->valuestring, jsonMsgId->valueint, result);
		else
			responseMessage = mqtt_json_make_response(jsonBoxId->valuestring, jsonMsgId->valueint, result);
		if (responseMessage != NULL)
		{
			printf ("response:\n%s\r\n", responseMessage);
			free(responseMessage);
		}
	}
	cJSON_Delete(jsonMessage);
	free(procBuffer);
}
/* Parse message receive from MQTT input topic */
Parse message receive from MQTT input topic
Parse message receive from MQTT input topic
void mqtt_json_parse_message(char* message, unsigned int length)
{
	cJSON *jsonMessage;
	cJSON *jsonBoxId;
	cJSON *jsonMsgId;
	cJSON *jsonMsgType;
	mqtt_json_result_t result = MQTT_PARSE_SUCCESS;
	printf("Parse message with length: %d\r\n", length);
	char* responseMessage;
	if ((message == NULL) || (length == 0))
		return;
	char* procBuffer = (char*)malloc(length + 1);
	memcpy(procBuffer, message, length);
	procBuffer[length] = 0;
	jsonMessage = cJSON_Parse(procBuffer);
	if (jsonMessage == NULL)
	{
		result = MQTT_PARSE_MESSAGE_ERROR;
		goto END_PARSE;
	}
	jsonBoxId = cJSON_GetObjectItem(jsonMessage, "id");
	if(cJSON_IsString(jsonBoxId) && (jsonBoxId->valuestring != NULL))
	{
		printf("BOX ID: %s\r\n", jsonBoxId->valuestring);
	}
	else
	{
		result = MQTT_PARSE_BOXID_ERROR;
		goto END_PARSE;
	}
	jsonMsgId = cJSON_GetObjectItem(jsonMessage, "message_id");
	if(cJSON_IsNumber(jsonMsgId))
	{
		printf("Message ID: %d\r\n", jsonMsgId->valueint);
	}
	else
	{
		result = MQTT_PARSE_MSGID_ERROR;
		goto END_PARSE;
	}
	jsonMsgType = cJSON_GetObjectItem(jsonMessage, "type");
	if (cJSON_IsString(jsonMsgType) && (jsonMsgType->valuestring != NULL))
	{
		printf("Message Type: %s\r\n", jsonMsgType->valuestring);
	}
	else
	{
		result = MQTT_PARSE_TYPE_ERROR;
		goto END_PARSE;
	}
	if (!strcmp(jsonMsgType->valuestring, "config"))
	{
		printf("Parse configuration message\r\n");
		result = mqtt_json_parse_configure_message(jsonMessage);
	}
	else if (!strcmp(jsonMsgType->valuestring, "control"))
	{
		printf("Parse control message \r\n");
		result = mqtt_json_parse_control_message(jsonMessage);
	}
	else if (!strcmp(jsonMsgType->valuestring, "reset"))
	{
		printf("Resetting ...\r\n");
	}
	else
		result = MQTT_PARSE_TYPE_ERROR;
	END_PARSE:
	if ((result != MQTT_PARSE_MESSAGE_ERROR) && (result != MQTT_PARSE_BOXID_ERROR) && (result != MQTT_PARSE_MSGID_ERROR))
	{
		if (result == MQTT_PARSE_SUCCESS)
			responseMessage = mqtt_json_make_response(jsonBoxId->valuestring, jsonMsgId->valueint, result);
		else
			responseMessage = mqtt_json_make_response(jsonBoxId->valuestring, jsonMsgId->valueint, result);
		if (responseMessage != NULL)
		{
			printf ("response:\n%s\r\n", responseMessage);
			free(responseMessage);
		}
	}
	cJSON_Delete(jsonMessage);
	free(procBuffer);
}
void mqtt_json_parse_message ( char * message , unsigned int length ) { cJSON * jsonMessage ; cJSON * jsonBoxId ; cJSON * jsonMsgId ; cJSON * jsonMsgType ; mqtt_json_result_t result = MQTT_PARSE_SUCCESS ; printf ( " \r \n " , length ) ; char * responseMessage ; if ( ( message == NULL ) || ( length == 0 ) ) return ; char * procBuffer = ( char * ) malloc ( length + 1 ) ; memcpy ( procBuffer , message , length ) ; procBuffer [ length ] = 0 ; jsonMessage = cJSON_Parse ( procBuffer ) ; if ( jsonMessage == NULL ) { result = MQTT_PARSE_MESSAGE_ERROR ; goto END_PARSE ; } jsonBoxId = cJSON_GetObjectItem ( jsonMessage , " " ) ; if ( cJSON_IsString ( jsonBoxId ) && ( jsonBoxId -> valuestring != NULL ) ) { printf ( " \r \n " , jsonBoxId -> valuestring ) ; } else { result = MQTT_PARSE_BOXID_ERROR ; goto END_PARSE ; } jsonMsgId = cJSON_GetObjectItem ( jsonMessage , " " ) ; if ( cJSON_IsNumber ( jsonMsgId ) ) { printf ( " \r \n " , jsonMsgId -> valueint ) ; } else { result = MQTT_PARSE_MSGID_ERROR ; goto END_PARSE ; } jsonMsgType = cJSON_GetObjectItem ( jsonMessage , " " ) ; if ( cJSON_IsString ( jsonMsgType ) && ( jsonMsgType -> valuestring != NULL ) ) { printf ( " \r \n " , jsonMsgType -> valuestring ) ; } else { result = MQTT_PARSE_TYPE_ERROR ; goto END_PARSE ; } if ( ! strcmp ( jsonMsgType -> valuestring , " " ) ) { printf ( " \r \n " ) ; result = mqtt_json_parse_configure_message ( jsonMessage ) ; } else if ( ! strcmp ( jsonMsgType -> valuestring , " " ) ) { printf ( " \r \n " ) ; result = mqtt_json_parse_control_message ( jsonMessage ) ; } else if ( ! strcmp ( jsonMsgType -> valuestring , " " ) ) { printf ( " \r \n " ) ; } else result = MQTT_PARSE_TYPE_ERROR ; END_PARSE : if ( ( result != MQTT_PARSE_MESSAGE_ERROR ) && ( result != MQTT_PARSE_BOXID_ERROR ) && ( result != MQTT_PARSE_MSGID_ERROR ) ) { if ( result == MQTT_PARSE_SUCCESS ) responseMessage = mqtt_json_make_response ( jsonBoxId -> valuestring , jsonMsgId -> valueint , result ) ; else responseMessage = mqtt_json_make_response ( jsonBoxId -> valuestring , jsonMsgId -> valueint , result ) ; if ( responseMessage != NULL ) { printf ( " \n \r \n " , responseMessage ) ; free ( responseMessage ) ; } } cJSON_Delete ( jsonMessage ) ; free ( procBuffer ) ; }
Parse message receive from MQTT input topic
Parse message receive from MQTT input topic