aqhome: completed adapting to msgio2 interface.
This commit is contained in:
359
aqhome/libtest.c
359
aqhome/libtest.c
@@ -4,16 +4,9 @@
|
||||
#endif
|
||||
|
||||
#include "aqhome/msg/msg_ping.h"
|
||||
#include "aqhome/msg/endpointmgr.h"
|
||||
#include "aqhome/msg/endpoint_node.h"
|
||||
#include "aqhome/msg/endpoint_log.h"
|
||||
#include "aqhome/msg/endpoint_tty.h"
|
||||
#include "aqhome/ipc/endpoint_node_ipc_tcpd.h"
|
||||
#include "aqhome/ipc/endpoint_ipc_tcpc.h"
|
||||
#include "aqhome/ipc/msg_ipc_ping.h"
|
||||
#include "aqhome/ipc/msg_ipc_forward.h"
|
||||
#include "aqhome/ipc/msg_ipc_setaccmsggrps.h"
|
||||
#include "aqhome/mqtt/endpoint_mqttc.h"
|
||||
#include "aqhome/mqtt/endpoint2_mqttc.h"
|
||||
#include "aqhome/mqtt/msg_mqtt_connect.h"
|
||||
#include "aqhome/mqtt/msg_mqtt_connack.h"
|
||||
@@ -21,7 +14,6 @@
|
||||
#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"
|
||||
|
||||
@@ -31,9 +23,6 @@
|
||||
#include <gwenhywfar/text.h>
|
||||
#include <gwenhywfar/gwentime.h>
|
||||
#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>
|
||||
@@ -116,143 +105,6 @@ GWEN_MSG *createPingMsg(uint8_t destAddr, uint8_t srcAddr)
|
||||
|
||||
|
||||
|
||||
int testEndpoints()
|
||||
{
|
||||
int rv;
|
||||
GWEN_MSG_ENDPOINT_MGR *emgr;
|
||||
GWEN_MSG_ENDPOINT *epTty;
|
||||
GWEN_MSG_ENDPOINT *epLog;
|
||||
GWEN_MSG_ENDPOINT *epTcp;
|
||||
|
||||
rv=AQH_Init();
|
||||
if (rv<0) {
|
||||
}
|
||||
|
||||
emgr=AQH_MsgManager_new(0xc0);
|
||||
epTty=AQH_TtyNodeEndpoint_new("/dev/ttyUSB0", AQH_MSGMGR_ENDPOINTGROUP_NODE);
|
||||
if (epTty==NULL) {
|
||||
DBG_ERROR(NULL, "Error creating endpoint TTY");
|
||||
return 2;
|
||||
}
|
||||
GWEN_MsgEndpointMgr_AddEndpoint(emgr, epTty);
|
||||
|
||||
epLog=AQH_LogEndpoint_new("endpoints.log", AQH_MSGMGR_ENDPOINTGROUP_NODE);
|
||||
if (epLog==NULL) {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Error creating endpoint LOG");
|
||||
return 2;
|
||||
}
|
||||
GWEN_MsgEndpointMgr_AddEndpoint(emgr, epLog);
|
||||
|
||||
epTcp=AQH_TcpdIpcNodeEndpoint_new("127.0.0.1", 45454, NULL, AQH_MSGMGR_ENDPOINTGROUP_IPC);
|
||||
if (epTcp==NULL) {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Error creating endpoint TCP");
|
||||
return 2;
|
||||
}
|
||||
GWEN_MsgEndpointMgr_AddEndpoint(emgr, epTcp);
|
||||
|
||||
for (;;) {
|
||||
DBG_DEBUG(AQH_LOGDOMAIN, "Next loop");
|
||||
AQH_MsgManager_LoopOnce(emgr);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int testMsgMqttConnect()
|
||||
{
|
||||
GWEN_MSG *msg;
|
||||
GWEN_BUFFER *dbuf;
|
||||
|
||||
msg=GWEN_ConnectMqttMsg_new("MQTT", 4,
|
||||
AQH_MQTTMSG_CONNECT_FLAGS_USERNAME | AQH_MQTTMSG_CONNECT_FLAGS_PASSWD,
|
||||
512,
|
||||
"CLIENTID123",
|
||||
"USER123",
|
||||
"PASSWD123");
|
||||
if (msg==NULL) {
|
||||
DBG_ERROR(NULL, "Error creating message");
|
||||
return 2;
|
||||
}
|
||||
dbuf=GWEN_Buffer_new(0, 256, 0, 1);
|
||||
AQH_ConnectMqttMsg_DumpToBuffer(msg, dbuf, "created");
|
||||
fprintf(stdout, "%s\n", GWEN_Buffer_GetStart(dbuf));
|
||||
GWEN_Buffer_free(dbuf);
|
||||
|
||||
GWEN_Text_DumpString((const char*) GWEN_Msg_GetConstBuffer(msg), GWEN_Msg_GetBytesInBuffer(msg), 2);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int testMqttConnection()
|
||||
{
|
||||
int rv;
|
||||
GWEN_MSG_ENDPOINT_MGR *emgr;
|
||||
GWEN_MSG_ENDPOINT *epTcp;
|
||||
GWEN_MSG *msgOut;
|
||||
|
||||
rv=AQH_Init();
|
||||
if (rv<0) {
|
||||
}
|
||||
|
||||
emgr=GWEN_MsgEndpointMgr_new();
|
||||
|
||||
epTcp=AQH_MqttClientEndpoint_new("127.0.0.1", 1883, "MQTTClient", 0);
|
||||
if (epTcp==NULL) {
|
||||
DBG_ERROR(NULL, "Error creating endpoint TCPc");
|
||||
return 2;
|
||||
}
|
||||
GWEN_MsgEndpointMgr_AddEndpoint(emgr, epTcp);
|
||||
|
||||
rv=_mqttConnect(emgr, epTcp);
|
||||
if (rv!=0) {
|
||||
DBG_ERROR(NULL, "Error connecting");
|
||||
return 2;
|
||||
}
|
||||
|
||||
fprintf(stdout, "Sending PUBLISH\n");
|
||||
//msgOut=GWEN_PublishMqttMsg_new(AQH_MQTTMSG_FLAGS_QOS1, 1, "test/subject1", (const uint8_t*) "29.9", 4);
|
||||
msgOut=GWEN_PublishMqttMsg_new(0, 0, "test/subject1", (const uint8_t*) "29.9", 4);
|
||||
if (msgOut==NULL) {
|
||||
DBG_ERROR(NULL, "Error creating message");
|
||||
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_PUBACK) {
|
||||
GWEN_BUFFER *buf;
|
||||
|
||||
buf=GWEN_Buffer_new(0, 256, 0, 1);
|
||||
AQH_PubResponseMqttMsg_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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int testMqttConnection2()
|
||||
{
|
||||
GWEN_MSG_ENDPOINT2 *epClient;
|
||||
@@ -276,94 +128,6 @@ int testMqttConnection2()
|
||||
|
||||
|
||||
|
||||
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 testMqttSubscribe2(int argc, char **argv)
|
||||
{
|
||||
GWEN_MSG_ENDPOINT2 *epClient;
|
||||
@@ -387,7 +151,7 @@ int testMqttSubscribe2(int argc, char **argv)
|
||||
}
|
||||
|
||||
pckId=AQH_MqttClientEndpoint2_GetNextPacketId(epClient);
|
||||
//msgOut=GWEN_PublishMqttMsg_new(AQH_MQTTMSG_FLAGS_QOS1, 1, "test/subject1", (const uint8_t*) "29.9", 4);
|
||||
//msgOut=AQH_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);
|
||||
msgOut=GWEN_SubscribeMqttMsg_new(AQH_MQTTMSG_MSGTYPE_SUBSCRIBE, pckId, "#", 0);
|
||||
if (msgOut==NULL) {
|
||||
@@ -483,120 +247,6 @@ GWEN_MSG *_awaitPacket2(GWEN_MSG_ENDPOINT2 *epClient, uint8_t expectedPacketType
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
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;
|
||||
GWEN_MSG_ENDPOINT_MGR *emgr;
|
||||
GWEN_MSG_ENDPOINT *epTcp;
|
||||
GWEN_MSG *msgOut;
|
||||
|
||||
rv=AQH_Init();
|
||||
if (rv<0) {
|
||||
}
|
||||
|
||||
//emgr=AQH_MsgManager_new(0xc0);
|
||||
emgr=GWEN_MsgEndpointMgr_new();
|
||||
|
||||
epTcp=AQH_IpcTcpClientEndpoint_new("127.0.0.1", 45454, "IPCClient", 0);
|
||||
if (epTcp==NULL) {
|
||||
DBG_ERROR(NULL, "Error creating endpoint TCPc");
|
||||
return 2;
|
||||
}
|
||||
GWEN_MsgEndpointMgr_AddEndpoint(emgr, epTcp);
|
||||
|
||||
fprintf(stdout, "Sending SET_ACCEPTABLE_MSG_GROUPS\n");
|
||||
msgOut=AQH_SetAcceptedMsgGroupsIpcMsg_new(AQH_MSGTYPE_IPC_SETACCMSGGRPS, AQH_MSG_TYPEGROUP_ALL);
|
||||
if (msgOut==NULL) {
|
||||
DBG_ERROR(NULL, "Error creating message");
|
||||
return 2;
|
||||
}
|
||||
GWEN_MsgEndpoint_AddSendMessage(epTcp, msgOut);
|
||||
|
||||
|
||||
fprintf(stdout, "Sending PING\n");
|
||||
msgOut=AQH_PingIpcMsg_new(AQH_MSGTYPE_IPC_PING, 2);
|
||||
if (msgOut==NULL) {
|
||||
DBG_ERROR(NULL, "Error creating message");
|
||||
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_RunAllEndpoints(emgr);
|
||||
GWEN_MsgEndpointMgr_IoLoopOnce(emgr);
|
||||
msg=GWEN_MsgEndpoint_TakeFirstReceivedMessage(epTcp);
|
||||
if (msg) {
|
||||
DBG_ERROR(NULL, "Received this message:");
|
||||
GWEN_Text_DumpString((const char*) GWEN_Msg_GetConstBuffer(msg), GWEN_Msg_GetBytesInBuffer(msg), 2);
|
||||
if (GWEN_IpcMsg_GetCode(msg)==AQH_MSGTYPE_IPC_FORWARD) {
|
||||
GWEN_MSG *nodeMsg;
|
||||
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Received FORWARD msg");
|
||||
nodeMsg=AQH_ForwardIpcMsg_GetCopyOfNodeMsg(msg);
|
||||
if (nodeMsg) {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Received node msg (%d)", AQH_NodeMsg_GetMsgType(nodeMsg));
|
||||
if (AQH_NodeMsg_GetMsgType(nodeMsg)==AQH_MSG_TYPE_PONG) {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Received PONG");
|
||||
GWEN_Msg_free(msg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
GWEN_Msg_free(msg);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int testHexfile(int argc, char **argv)
|
||||
{
|
||||
const char *inFilename;
|
||||
@@ -700,15 +350,8 @@ int testFlashRecords(int argc, char **argv)
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
//return testEndpoints();
|
||||
//return testMsgMqttConnect();
|
||||
//return testMqttConnection();
|
||||
|
||||
//return testIpcConnection();
|
||||
//return testHexfile(argc, argv);
|
||||
//return testFlashRecords(argc, argv);
|
||||
//return testMqttSubscribe(argc, argv);
|
||||
|
||||
//return testMqttConnection2();
|
||||
return testMqttSubscribe2(argc, argv);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user