MQTTClient.c
/*
* MQTTClient.c
*
* Created on:
* Author:
*/
#include "../inc/MQTTClient.h"
#include "../Inc/app.h"
uint8_t MQTT_ClientConnect(const void* clientId, const void* username, const void* password, uint16_t keepalive, bool cleansession)
{
int len = 0;
int buflen = 256;
uint8_t buf[256];
uint8_t sessionPresent, connack_rc;
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
data.clientID.cstring = (char *) clientId;
data.keepAliveInterval = keepalive;
data.cleansession = cleansession;
data.username.cstring = (char *) username;
data.password.cstring = (char *) password;
data.MQTTVersion = 4;
if ((len = MQTTSerialize_connect((unsigned char *) buf, buflen, &data)) <= 0)
return 1;
WIFI_RX_STA = 0;
if (transport_sendPacketBuffer(buf, len) != len)
return 2;
delay_ms(1000);
if (MQTTPacket_read(buf, buflen, transport_getdata) != CONNACK)
return 3;
if (MQTTDeserialize_connack(&sessionPresent, &connack_rc, buf, buflen) != 1 || connack_rc != 0)
return 4;
return 0;
}
uint8_t MQTT_ClientPublish(const void* topic, const void* data)
{
uint8_t buf[256];
MQTTString topicString = MQTTString_initializer;
int len = 0;
topicString.cstring = (char *) topic;
if ((len = MQTTSerialize_publish((uint8_t *) buf, sizeof(buf), 0, 0, 0, 0,
topicString, (uint8_t*) data, strlen((const char*) data))) <= 0)
return 1;
if (transport_sendPacketBuffer(buf, len) != len)
return 2;
return 0;
}
uint8_t MQTT_ClientSubscribe(const void* topic, int qos)
{
uint8_t buf[200] =
{ 0 };
uint32_t buflen = sizeof(buf);
uint32_t len = 0;
uint16_t packetid;
int maxcount = 1;
int count;
int grantedQoSs;
MQTTString topicString = MQTTString_initializer;
topicString.cstring = (char *) topic;
if ((len = MQTTSerialize_subscribe(buf, buflen, 0, 1, 1, &topicString, &qos)) <= 0)
return 1;
WIFI_RX_STA = 0;
if (transport_sendPacketBuffer(buf, len) != len)
return 2;
delay_ms(1000);
if (MQTTPacket_read(buf, buflen, transport_getdata) != SUBACK)
return 3;
if (MQTTDeserialize_suback(&packetid, maxcount, &count, &grantedQoSs, buf, buflen) == 0)
return 4;
return 0;
}
extern bool G_isConnected;
void MQTT_RecvParser(void)
{
size_t topiclen = 0;
size_t datalen = 0;
static uint8_t topic[0xFF];
static uint8_t message[0xFF];
static const uint8_t* buf = WIFI_RX_BUF;
if(G_isConnected == true)
{
if (WIFI_RX_STA & 0x8000)
{
WIFI_RX_BUF[WIFI_RX_STA & 0x3FFF] = '\0';
if(strstr((char *)WIFI_RX_BUF, "CLOSED") != 0)
{
G_isConnected = false;
WIFI_RX_STA = 0;
return;
}
topiclen = buf[3];
datalen = buf[1] - buf[3] - 2;
memcpy((char *) topic, (const char *) (buf + 4), topiclen);
memcpy((char *) message, (const char *) (buf + 4 + topiclen), datalen);
topic[topiclen] = '\0';
message[datalen] = '\0';
if(strncmp(TOPIC_LED, (const char *)topic, strlen(TOPIC_LED)) == 0)
{
if(strncmp(MSG_LED_ON, (const char *)message, strlen(MSG_LED_ON)) == 0)
{
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET);
}else if(strncmp(MSG_LED_OFF, (const char *)message, strlen(MSG_LED_OFF)) == 0){
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET);
}
}
printf("\r\n");
printf("topic:%s\r\ndara:%s\r\n", (char *) topic, (char *) message);
WIFI_RX_STA = 0;
}
}
}
|