Skip to content

Commit 8ec2b1b

Browse files
author
Ricky-Kwon
committed
Resolved #18
- Added a feature that AT+MQTTSET : (AT command) This command is used to initialize the necessary parameters of MQTT connection(set information automatically saved) - Added a feature that AT+MQTTCON : (AT command) Implement MQTT to connect the Broker - Added a feature that AT+MQTTPUB : (AT command) Publish the message to subscribed Client(set topic automatically saved) - Added a feature that AT+MQTTSUB : (AT command) Such topics will be pushed to the WIZ550S2E when received by the Broker(set topic automatically saved)
1 parent d5a8982 commit 8ec2b1b

File tree

7 files changed

+493
-23
lines changed

7 files changed

+493
-23
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@ Install JAVA Runtime Environment
137137

138138
## Revision History
139139

140+
### v1.3.0
141+
142+
- Added features
143+
- AT+MQTTSET : (AT command) This command is used to initialize the necessary parameters of MQTT connection(set information automatically saved)
144+
- AT+MQTTCON : (AT command) Implement MQTT to connect the Broker
145+
- AT+MQTTPUB : (AT command) Publish the message to subscribed Client(set topic automatically saved)
146+
- AT+MQTTSUB : (AT command) Such topics will be pushed to the WIZ550S2E when received by the Broker(set topic automatically saved)
147+
140148
### v1.2.2
141149

142150
- Fixed bugs

WIZ550S2E_App/src/ATcmd/atcmd.c

+205
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include "cmdrun.h"
1919
#include "ConfigData.h"
2020
#include "uartHandler.h"
21+
#include "mqtt_interface.h"
22+
#include "MQTTClient.h"
2123

2224
#define CMD_CLEAR() { \
2325
atci.tcmd.op[0] = atci.tcmd.sign = atci.tcmd.arg1[0] = atci.tcmd.arg2[0] = 0; \
@@ -76,6 +78,10 @@ static void hdl_mname(void);
7678
static void hdl_fdns(void);
7779
static void hdl_estat(void);
7880

81+
static void hdl_mqttset(void);
82+
static void hdl_mqttcon(void);
83+
static void hdl_mqttpub(void);
84+
static void hdl_mqttsub(void);
7985

8086
#define ATCMD_BUF_SIZE 100
8187
#define PREVBUF_MAX_SIZE 250
@@ -112,6 +118,11 @@ const struct at_command g_cmd_table[] =
112118

113119
{ "FDNS", hdl_fdns, NULL, NULL },
114120

121+
{ "MQTTSET", hdl_mqttset, NULL, NULL },
122+
{ "MQTTCON", hdl_mqttcon, NULL, NULL },
123+
{ "MQTTPUB", hdl_mqttpub, NULL, NULL },
124+
{ "MQTTSUB", hdl_mqttsub, NULL, NULL },
125+
115126
{ NULL, NULL, NULL, NULL } // Should be last item
116127
};
117128

@@ -139,6 +150,9 @@ void atc_init()
139150
atci.sendbuf = g_send_buf;
140151
atci.recvbuf = g_recv_buf;
141152

153+
atci.mqtt_con = 0;
154+
atci.mqtt_run = 0;
155+
142156
sockwatch_open(0, atc_async_cb); // Assign socket 1 to ATC module
143157

144158
//UART_write("\r\n\r\n\r\n[W,0]\r\n", 13);
@@ -1070,3 +1084,194 @@ static void hdl_estat(void)
10701084
{
10711085
RESP_CR(RET_NOT_ALLOWED);
10721086
}
1087+
1088+
static void hdl_mqttset(void)
1089+
{
1090+
char mqttUser[MQTT_ACCOUNT_LEN] = {'\0',};
1091+
char mqttPass[MQTT_ACCOUNT_LEN] = {'\0',};
1092+
char mqttId[NAME_LEN] = {'\0',};
1093+
1094+
uint8_t *mqttUserPtr = NULL;
1095+
uint8_t *mqttPassPtr = NULL;
1096+
uint8_t *mqttIdPtr = NULL;
1097+
1098+
uint8_t mqttUserLen, mqttPassLen, mqttIdLen;
1099+
1100+
uint8_t i;
1101+
1102+
if(atci.tcmd.sign == CMD_SIGN_NONE) atci.tcmd.sign = CMD_SIGN_QUEST;
1103+
1104+
if(atci.tcmd.sign == CMD_SIGN_QUEST) {
1105+
CMD_CLEAR();
1106+
act_mqttset_q();
1107+
}
1108+
else if(atci.tcmd.sign == CMD_SIGN_INDIV) RESP_CR(RET_WRONG_SIGN);
1109+
else if(atci.tcmd.sign == CMD_SIGN_EQUAL) {
1110+
1111+
mqttUserLen = strlen(atci.tcmd.arg1);
1112+
mqttPassLen = strlen(atci.tcmd.arg2);
1113+
mqttIdLen = strlen(atci.tcmd.arg3);
1114+
1115+
if(mqttUserLen > (MQTT_ACCOUNT_LEN - 1)) RESP_CDR(RET_RANGE_OUT, 1);
1116+
if(mqttPassLen > (MQTT_ACCOUNT_LEN - 1)) RESP_CDR(RET_RANGE_OUT, 1);
1117+
if(mqttIdLen > (NAME_LEN - 1)) RESP_CDR(RET_RANGE_OUT, 1);
1118+
1119+
for(i=0; i < mqttUserLen; i++) {
1120+
mqttUser[i] = atci.tcmd.arg1[i];
1121+
}
1122+
for(i=0; i < mqttPassLen; i++) {
1123+
mqttPass[i] = atci.tcmd.arg2[i];
1124+
}
1125+
for(i=0; i < mqttIdLen; i++) {
1126+
mqttId[i] = atci.tcmd.arg3[i];
1127+
}
1128+
1129+
mqttUserPtr = mqttUser;
1130+
mqttPassPtr = mqttPass;
1131+
mqttIdPtr = mqttId;
1132+
1133+
CMD_CLEAR();
1134+
1135+
act_mqttset_a(mqttUserPtr, mqttUserLen, mqttPassPtr, mqttPassLen, mqttIdPtr, mqttIdLen);
1136+
}
1137+
else CRITICAL_ERRA("wrong sign(%d)", atci.tcmd.sign);
1138+
}
1139+
1140+
static void hdl_mqttcon(void)
1141+
{
1142+
int8_t type=0;
1143+
uint8_t brokerIp[4] = {0,};
1144+
uint8_t *brokerIpPtr = NULL;
1145+
uint16_t brokertPort = 0;
1146+
uint16_t SrcPort;
1147+
int8_t num = 0;
1148+
1149+
if(atci.tcmd.sign == CMD_SIGN_NONE) atci.tcmd.sign = CMD_SIGN_QUEST;
1150+
if(atci.tcmd.sign == CMD_SIGN_QUEST)
1151+
{
1152+
CMD_CLEAR();
1153+
act_mqttcon_q();
1154+
}
1155+
else if(atci.tcmd.sign == CMD_SIGN_INDIV) RESP_CR(RET_WRONG_SIGN);
1156+
else if(atci.tcmd.sign == CMD_SIGN_EQUAL)
1157+
{
1158+
//choice connect = 1, disconnect = 0
1159+
if(str_check(isdigit, atci.tcmd.arg1) != RET_OK) RESP_CDR(RET_WRONG_ARG, 1);
1160+
if(CHK_DGT_RANGE(atci.tcmd.arg1, num, 0, 1)) RESP_CDR(RET_RANGE_OUT, 1);
1161+
else type = num;
1162+
1163+
if(type == 1) {
1164+
//setting mqtt broker ip and check
1165+
if(ip_check(atci.tcmd.arg2, brokerIp) != RET_OK) RESP_CDR(RET_WRONG_ARG, 2);
1166+
//setting mqtt broker port and check
1167+
if(port_check(atci.tcmd.arg3, &brokertPort) != RET_OK) RESP_CDR(RET_WRONG_ARG, 3);
1168+
brokerIpPtr = brokerIp;
1169+
SrcPort = client_port;
1170+
CMD_CLEAR();
1171+
act_mqttcon_a(SrcPort, brokerIpPtr, brokertPort);
1172+
} else if(type == 0) {
1173+
CMD_CLEAR();
1174+
act_nclose(num);
1175+
if(client_port == 65535)
1176+
client_port = 2000;
1177+
else
1178+
client_port++;
1179+
}
1180+
}
1181+
else CRITICAL_ERRA("wrong sign(%d)", atci.tcmd.sign);
1182+
}
1183+
1184+
static void hdl_mqttpub(void)
1185+
{
1186+
int8_t num = -1;
1187+
int32_t ret;
1188+
uint8_t *dip = NULL;
1189+
uint16_t *dport = NULL;
1190+
1191+
uint8_t mqttPublishTopicLen;
1192+
uint8_t i;
1193+
1194+
struct __options *option = (struct __options *)&(get_S2E_Packet_pointer()->options);
1195+
1196+
if(atci.tcmd.sign == CMD_SIGN_NONE) RESP_CR(RET_WRONG_SIGN);
1197+
if(atci.tcmd.sign == CMD_SIGN_QUEST) RESP_CR(RET_WRONG_SIGN);
1198+
else if(atci.tcmd.sign == CMD_SIGN_INDIV) RESP_CR(RET_WRONG_SIGN);
1199+
else if(atci.tcmd.sign == CMD_SIGN_EQUAL)
1200+
{
1201+
//choice socket id(num)
1202+
if(atci.tcmd.arg1[0] != 0) {
1203+
if(str_check(isdigit, atci.tcmd.arg1) != RET_OK) RESP_CDR(RET_WRONG_ARG, 1);
1204+
if(CHK_DGT_RANGE(atci.tcmd.arg1, num, ATC_SOCK_NUM_START, ATC_SOCK_NUM_END))
1205+
RESP_CDR(RET_RANGE_OUT, 1);
1206+
}
1207+
1208+
//setting mqtt publish topic
1209+
if(atci.tcmd.arg2[0] != 0) {
1210+
mqttPublishTopicLen = strlen(atci.tcmd.arg2);
1211+
if(mqttPublishTopicLen > (MQTT_TOPIC_LEN - 1)) RESP_CDR(RET_RANGE_OUT, 2);
1212+
memset(option->mqtt_publish_topic, '\0', MQTT_TOPIC_LEN);
1213+
for(i=0; i < mqttPublishTopicLen; i++) {
1214+
option->mqtt_publish_topic[i] = atci.tcmd.arg2[i];
1215+
save_S2E_Packet_to_eeprom();
1216+
}
1217+
}
1218+
else {
1219+
if(strlen(option->mqtt_publish_topic) == 0) RESP_CDR(RET_WRONG_ARG, 2);
1220+
}
1221+
1222+
//setting size
1223+
if(str_check(isdigit, atci.tcmd.arg3) != RET_OK ||
1224+
(atci.sendlen = atoi((char*)atci.tcmd.arg3)) < 1 ||
1225+
atci.sendlen > WORK_BUF_SIZE) RESP_CDR(RET_RANGE_OUT, 3);
1226+
1227+
CMD_CLEAR();
1228+
ret = act_nsend_chk(num, &atci.sendlen, dip, dport);
1229+
if(ret != RET_OK) return;
1230+
1231+
atci.sendsock = num; // 유효성 검사가 완료되면 SEND모드로 전환
1232+
atci.worklen = 0;
1233+
cmd_resp(RET_ASYNC, num);
1234+
}
1235+
else CRITICAL_ERRA("wrong sign(%d)", atci.tcmd.sign);
1236+
}
1237+
1238+
static void hdl_mqttsub(void)
1239+
{
1240+
int8_t type=0;
1241+
int8_t num = -1;
1242+
int32_t ret;
1243+
1244+
uint8_t mqttSubscribeTopicLen;
1245+
uint8_t i;
1246+
1247+
struct __options *option = (struct __options *)&(get_S2E_Packet_pointer()->options);
1248+
1249+
if(atci.tcmd.sign == CMD_SIGN_NONE) RESP_CR(RET_WRONG_SIGN);
1250+
if(atci.tcmd.sign == CMD_SIGN_QUEST) RESP_CR(RET_WRONG_SIGN);
1251+
else if(atci.tcmd.sign == CMD_SIGN_INDIV) RESP_CR(RET_WRONG_SIGN);
1252+
else if(atci.tcmd.sign == CMD_SIGN_EQUAL)
1253+
{
1254+
//choice subscribe = 1, unsubscribe = 0
1255+
if(str_check(isdigit, atci.tcmd.arg1) != RET_OK) RESP_CDR(RET_WRONG_ARG, 1);
1256+
if(CHK_DGT_RANGE(atci.tcmd.arg1, num, 0, 1)) RESP_CDR(RET_RANGE_OUT, 1);
1257+
else type = num;
1258+
1259+
//setting mqtt subscribe topic
1260+
if(atci.tcmd.arg2[0] != 0) {
1261+
mqttSubscribeTopicLen = strlen(atci.tcmd.arg2);
1262+
if(mqttSubscribeTopicLen > (MQTT_TOPIC_LEN - 1)) RESP_CDR(RET_RANGE_OUT, 2);
1263+
memset(option->mqtt_subscribe_topic, '\0', MQTT_TOPIC_LEN);
1264+
for(i=0; i < mqttSubscribeTopicLen; i++) {
1265+
option->mqtt_subscribe_topic[i] = atci.tcmd.arg2[i];
1266+
}
1267+
}
1268+
else {
1269+
if(strlen(option->mqtt_subscribe_topic) == 0) RESP_CDR(RET_WRONG_ARG, 2);
1270+
}
1271+
1272+
act_mqtt_sub(type);
1273+
1274+
CMD_CLEAR();
1275+
}
1276+
else CRITICAL_ERRA("wrong sign(%d)", atci.tcmd.sign);
1277+
}

0 commit comments

Comments
 (0)