Ejemplo n.º 1
0
int ToggleLED(UserLed user_led)
{
	int ret;
	LED_State state;

	ret = GetLEDState(user_led, &state);
	if (ret!=0)
		return -1;
	return SetLEDState(user_led, !state);
}
/*******************************************************************************
*	MQTT Callback Handler
*
*	Is called when the LEd status changes ()
********************************************************************************/
int MQTTcallbackHandler(MQTTCallbackParams params)
{
	IoT_Error_t rc = NONE_ERROR;

	int ret;
	bool des_state, rep_state;
	int payload_len = (int)params.MessageParams.PayloadLen;
	char* payload = (char*)params.MessageParams.pPayload;

	//Receive desired LED state in payload
	INFO("Subscribe callback");
	INFO("%.*s\t%.*s",
				(int)params.TopicNameLen, params.pTopicName,
				(int)params.MessageParams.PayloadLen, (char*)params.MessageParams.pPayload);

	//Read the desired state from message
	ret = MSG_GetDesiredState( payload_len, payload, &des_state);
	if(ret !=0)
		goto JSON_ERROR;

	//Update current LED- and GPIO- state to the desired state
	INFO("Updating state: %d\n",des_state);
	SetLEDState(UserLED_4, des_state);
	Write_GPIO(GPIO12, des_state);

	//Write response message
	char msg_payload[MAX_PAYLOAD];
	payload = msg_payload;
	payload_len = MAX_PAYLOAD;

	LED_State led_state;
	GetLEDState(UserLED_4, &led_state);
	ret = MSG_SetReportedState(  payload_len, payload, (int)led_state);
	if(ret !=0)
		goto JSON_ERROR;

	char topic[512];
        sprintf(topic, led_state_pub_topic, GetMachineID());
	printf("Sending payload: %s", payload);
	rc = MQTT_Send_Message(topic, payload, strlen(payload) );
	if (NONE_ERROR != rc)
		ERROR("Could not publish new LED state to topic: %s", topic );

	return 0;

JSON_ERROR:
	ERROR("JSON format Error! %s", (char*)params.MessageParams.pPayload);
	return -1;
}