aqhome/mqtt: added messages regarding subscription.
This commit is contained in:
170
aqhome/libtest.c
170
aqhome/libtest.c
@@ -18,6 +18,8 @@
|
||||
#include "aqhome/mqtt/msg_mqtt_connack.h"
|
||||
#include "aqhome/mqtt/msg_mqtt_publish.h"
|
||||
#include "aqhome/mqtt/msg_mqtt_pubresponse.h"
|
||||
#include "aqhome/mqtt/msg_mqtt_subscribe.h"
|
||||
#include "aqhome/mqtt/msg_mqtt_suback.h"
|
||||
#include "aqhome/msgmanager.h"
|
||||
#include "aqhome/hexfile/hexfile.h"
|
||||
#include "aqhome/hexfile/flashrecord.h"
|
||||
@@ -30,11 +32,16 @@
|
||||
#include <gwenhywfar/buffer.h>
|
||||
#include <gwenhywfar/endpoint_tcpd_ipc.h>
|
||||
#include <gwenhywfar/endpoint_tcpc.h>
|
||||
#include <gwenhywfar/endpoint_connectable.h>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
|
||||
|
||||
static int _mqttConnect(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp);
|
||||
static GWEN_MSG *_awaitPacket(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp, uint8_t expectedPacketType);
|
||||
|
||||
|
||||
|
||||
GWEN_MSG *createPingMsg(uint8_t destAddr, uint8_t srcAddr)
|
||||
{
|
||||
@@ -186,7 +193,6 @@ int testMqttConnection()
|
||||
if (rv<0) {
|
||||
}
|
||||
|
||||
//emgr=AQH_MsgManager_new(0xc0);
|
||||
emgr=GWEN_MsgEndpointMgr_new();
|
||||
|
||||
epTcp=AQH_MqttClientEndpoint_new("127.0.0.1", 1883, "MQTTClient", 0);
|
||||
@@ -196,39 +202,11 @@ int testMqttConnection()
|
||||
}
|
||||
GWEN_MsgEndpointMgr_AddEndpoint(emgr, epTcp);
|
||||
|
||||
fprintf(stdout, "Sending CONNECT\n");
|
||||
msgOut=GWEN_ConnectMqttMsg_new("MQTT", 4, 0, 10, "CLIENTID123", NULL, NULL);
|
||||
if (msgOut==NULL) {
|
||||
DBG_ERROR(NULL, "Error creating message");
|
||||
rv=_mqttConnect(emgr, epTcp);
|
||||
if (rv!=0) {
|
||||
DBG_ERROR(NULL, "Error connecting");
|
||||
return 2;
|
||||
}
|
||||
GWEN_MsgEndpoint_AddSendMessage(epTcp, msgOut);
|
||||
|
||||
fprintf(stdout, "Waiting for response\n");
|
||||
for (;;) {
|
||||
GWEN_MSG *msg;
|
||||
|
||||
DBG_DEBUG(AQH_LOGDOMAIN, "Next loop");
|
||||
//GWEN_MsgManager_LoopOnce(emgr);
|
||||
GWEN_MsgEndpointMgr_IoLoopOnce(emgr);
|
||||
msg=GWEN_MsgEndpoint_TakeFirstReceivedMessage(epTcp);
|
||||
if (msg) {
|
||||
if (AQH_MqttMsg_GetMsgTypeAndFlags(msg)==AQH_MQTTMSG_MSGTYPE_CONNACK) {
|
||||
GWEN_BUFFER *buf;
|
||||
|
||||
buf=GWEN_Buffer_new(0, 256, 0, 1);
|
||||
AQH_ConnAckMqttMsg_DumpToBuffer(msg, buf, "received");
|
||||
fprintf(stdout, "%s\n", GWEN_Buffer_GetStart(buf));
|
||||
GWEN_Buffer_free(buf);
|
||||
}
|
||||
else {
|
||||
DBG_ERROR(NULL, "Received this message:");
|
||||
GWEN_Text_DumpString((const char*) GWEN_Msg_GetConstBuffer(msg), GWEN_Msg_GetBytesInBuffer(msg), 2);
|
||||
}
|
||||
GWEN_Msg_free(msg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
fprintf(stdout, "Sending PUBLISH\n");
|
||||
//msgOut=GWEN_PublishMqttMsg_new(AQH_MQTTMSG_FLAGS_QOS1, 1, "test/subject1", (const uint8_t*) "29.9", 4);
|
||||
@@ -271,6 +249,131 @@ int testMqttConnection()
|
||||
|
||||
|
||||
|
||||
int testMqttSubscribe(int argc, char **argv)
|
||||
{
|
||||
int rv;
|
||||
GWEN_MSG_ENDPOINT_MGR *emgr;
|
||||
GWEN_MSG_ENDPOINT *epTcp;
|
||||
GWEN_MSG *msgOut;
|
||||
GWEN_MSG *msgIn;
|
||||
uint16_t pckId;
|
||||
const char *host="127.0.0.1";
|
||||
|
||||
if (argc>1)
|
||||
host=argv[1];
|
||||
|
||||
rv=AQH_Init();
|
||||
if (rv<0) {
|
||||
}
|
||||
|
||||
//emgr=AQH_MsgManager_new(0xc0);
|
||||
emgr=GWEN_MsgEndpointMgr_new();
|
||||
|
||||
epTcp=AQH_MqttClientEndpoint_new(host, 1883, "MQTTClient", 0);
|
||||
if (epTcp==NULL) {
|
||||
DBG_ERROR(NULL, "Error creating endpoint TCPc");
|
||||
return 2;
|
||||
}
|
||||
AQH_MqttClientEndpoint_SetClientId(epTcp, "CLIENTID123");
|
||||
GWEN_MsgEndpointMgr_AddEndpoint(emgr, epTcp);
|
||||
|
||||
rv=_mqttConnect(emgr, epTcp);
|
||||
if (rv!=0) {
|
||||
DBG_ERROR(NULL, "Error connecting");
|
||||
return 2;
|
||||
}
|
||||
|
||||
fprintf(stdout, "Sending SUBSCRIBE\n");
|
||||
pckId=AQH_MqttClientEndpoint_GetNextPacketId(epTcp);
|
||||
//msgOut=GWEN_PublishMqttMsg_new(AQH_MQTTMSG_FLAGS_QOS1, 1, "test/subject1", (const uint8_t*) "29.9", 4);
|
||||
msgOut=GWEN_SubscribeMqttMsg_new(AQH_MQTTMSG_MSGTYPE_SUBSCRIBE, pckId, "aqhome/#", 0);
|
||||
if (msgOut==NULL) {
|
||||
DBG_ERROR(NULL, "Error creating message");
|
||||
return 2;
|
||||
}
|
||||
DBG_ERROR(NULL, "Sending this message:");
|
||||
GWEN_Text_DumpString((const char*) GWEN_Msg_GetConstBuffer(msgOut), GWEN_Msg_GetBytesInBuffer(msgOut), 2);
|
||||
|
||||
GWEN_MsgEndpoint_AddSendMessage(epTcp, msgOut);
|
||||
|
||||
fprintf(stdout, "Waiting for response\n");
|
||||
msgIn=_awaitPacket(emgr, epTcp, AQH_MQTTMSG_MSGTYPE_SUBACK);
|
||||
if (msgIn) {
|
||||
GWEN_BUFFER *buf;
|
||||
|
||||
buf=GWEN_Buffer_new(0, 256, 0, 1);
|
||||
AQH_SubAckMqttMsg_DumpToBuffer(msgIn, buf, "received");
|
||||
fprintf(stdout, "%s\n", GWEN_Buffer_GetStart(buf));
|
||||
GWEN_Buffer_free(buf);
|
||||
GWEN_Msg_free(msgIn);
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
GWEN_MSG *msg;
|
||||
|
||||
//GWEN_MsgManager_LoopOnce(emgr);
|
||||
GWEN_MsgEndpointMgr_IoLoopOnce(emgr);
|
||||
msg=GWEN_MsgEndpoint_TakeFirstReceivedMessage(epTcp);
|
||||
if (msg) {
|
||||
if ((AQH_MqttMsg_GetMsgTypeAndFlags(msg) & 0xf0)==(AQH_MQTTMSG_MSGTYPE_PUBLISH & 0xf0)) {
|
||||
GWEN_BUFFER *buf;
|
||||
|
||||
buf=GWEN_Buffer_new(0, 256, 0, 1);
|
||||
AQH_PublishMqttMsg_DumpToBuffer(msg, buf, "received");
|
||||
fprintf(stdout, "%s\n", GWEN_Buffer_GetStart(buf));
|
||||
GWEN_Buffer_free(buf);
|
||||
}
|
||||
else {
|
||||
DBG_ERROR(NULL, "Received this message:");
|
||||
GWEN_Text_DumpString((const char*) GWEN_Msg_GetConstBuffer(msg), GWEN_Msg_GetBytesInBuffer(msg), 2);
|
||||
}
|
||||
GWEN_Msg_free(msg);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int _mqttConnect(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp)
|
||||
{
|
||||
while(GWEN_ConnectableMsgEndpoint_GetState(epTcp)<GWEN_ENDPOINT_MQTTC_STATE_ESTABLISHED) {
|
||||
DBG_DEBUG(AQH_LOGDOMAIN, "Next loop");
|
||||
GWEN_MsgEndpointMgr_IoLoopOnce(emgr);
|
||||
GWEN_MsgEndpointMgr_RunAllEndpoints(emgr);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
GWEN_MSG *_awaitPacket(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp, uint8_t expectedPacketType)
|
||||
{
|
||||
fprintf(stdout, "Waiting for response\n");
|
||||
for (;;) {
|
||||
GWEN_MSG *msg;
|
||||
|
||||
DBG_DEBUG(AQH_LOGDOMAIN, "Next loop");
|
||||
//GWEN_MsgManager_LoopOnce(emgr);
|
||||
GWEN_MsgEndpointMgr_IoLoopOnce(emgr);
|
||||
msg=GWEN_MsgEndpoint_TakeFirstReceivedMessage(epTcp);
|
||||
if (msg) {
|
||||
if ((AQH_MqttMsg_GetMsgTypeAndFlags(msg) & 0xf0)==(expectedPacketType & 0xf0)) {
|
||||
return msg;
|
||||
}
|
||||
else {
|
||||
DBG_ERROR(NULL, "Received this message:");
|
||||
GWEN_Text_DumpString((const char*) GWEN_Msg_GetConstBuffer(msg), GWEN_Msg_GetBytesInBuffer(msg), 2);
|
||||
}
|
||||
}
|
||||
} /* for */
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int testIpcConnection()
|
||||
{
|
||||
int rv;
|
||||
@@ -453,7 +556,8 @@ int main(int argc, char **argv)
|
||||
|
||||
//return testIpcConnection();
|
||||
//return testHexfile(argc, argv);
|
||||
return testFlashRecords(argc, argv);
|
||||
//return testFlashRecords(argc, argv);
|
||||
return testMqttSubscribe(argc, argv);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user