AtGsmSimCom7600E/Src/AtGsmSimComA7600_EstablishC...

179 lines
4.6 KiB
C

//
// Created by cfif on 10.05.23.
//
#include <memory.h>
#include "AtGsmSimComA7600_EstablishConnection.h"
#include "AtCmdCommonProtected.h"
#include "SystemDelayInterface.h"
AtCommandResult AtGsmSimComA7600_EstablishConnection(
tAtCmd *env,
uint8_t linkNum,
eAtGsmSimComA7600_IpType type,
char *srvIp,
uint8_t srvIpLen,
uint16_t srvPort,
uint16_t localPort
) {
AtCmdPrepare(env);
AtCmdRxClear(env);
if (linkNum > 9) {
return AT_ERROR;
}
AtCmdTxClear(env);
AtCmdTxAddStatic(env, "AT+CIPOPEN=");
AtCmdTxAddDecimalIntWithLimit(env, linkNum, 1);
AtCmdTxAddChar(env, ',');
AtCmdTxAddChar(env, '"');
switch (type) {
case eAtGsmSimComA7600_IpType_TCP:
AtCmdTxAddStatic(env, "TCP");
break;
case eAtGsmSimComA7600_IpType_UDP:
AtCmdTxAddStatic(env, "UDP");
break;
default:
return AT_ERROR;
}
AtCmdTxAddChar(env, '"');
AtCmdTxAddChar(env, ',');
if (srvIpLen) {
AtCmdTxAddChar(env, '"');
AtCmdTxAdd(env, srvIp, srvIpLen);
AtCmdTxAddChar(env, '"');
}
AtCmdTxAddChar(env, ',');
if (srvPort) {
AtCmdTxAddDecimalIntWithLimit(env, srvPort, 5);
}
if (localPort) {
AtCmdTxAddChar(env, ',');
AtCmdTxAddDecimalIntWithLimit(env, localPort, 5);
}
AtCmdTxSendLn(env);
uint32_t timeout = 20000;
uint32_t endMs = SystemGetMs() + timeout;
uint32_t leftMs = timeout;
while ((AtCmdReceiveNextLine(env, leftMs) == AT_OK) && (SystemGetMs() < endMs)) {
leftMs = endMs - SystemGetMs();
if (AtCmdRxBeginWithStatic(env, "OK")) {
AtCmdRxClear(env);
} else if (AtCmdRxBeginWithStatic(env, "+CIPOPEN: ")) {
AtCommandResult res = (env->rxBuffer.data[sizeof("+CIPOPEN: X,") - 1] == '0') ? AT_OK : AT_ERROR;
AtCmdRxClear(env);
return res;
} else if (AtCmdRxBeginWithStatic(env, "ERROR")) {
AtCmdRxClear(env);
return AT_ERROR;
} else {
AtCmdProcessUnresolvedLine(env);
AtCmdRxClear(env);
continue;
}
}
return AT_TIMEOUT;
}
AtCommandResult AtGsmSimComA7600_HasConnection(tAtCmd *env, uint8_t linkNum, bool *status) {
char pattern[11];
memcpy(pattern, "+CIPOPEN: X", 11);
pattern[sizeof(pattern) - 1] = vAsciiStringDecimalIntToChar(linkNum);
uint8_t pattern_len = sizeof(pattern);
*status = false;
AtCmdPrepare(env);
AtCmdSendStatic(env, "AT+CIPOPEN?\r\n");
uint32_t timeout = env->stdRxTimeout;
uint32_t endMs = SystemGetMs() + timeout;
uint32_t leftMs = timeout;
while ((AtCmdReceiveNextLine(env, leftMs) == AT_OK) && (SystemGetMs() < endMs)) {
leftMs = endMs - SystemGetMs();
if (AtCmdRxBeginWithStatic(env, "OK")) {
AtCmdRxClear(env);
return AT_OK;
} else if (AtCmdRxBeginWithStatic(env, "+CIPOPEN")) {
if (AtCmdRxBeginWith(env, pattern, pattern_len)) {
if (env->rxBuffer.data[pattern_len] == '\r')
*status = false;
else if (env->rxBuffer.data[pattern_len] == ',') {
*status = true;
}
}
AtCmdRxClear(env);
} else if (AtCmdRxBeginWithStatic(env, "ERROR")) {
AtCmdRxClear(env);
return AT_ERROR;
} else {
AtCmdProcessUnresolvedLine(env);
AtCmdRxClear(env);
continue;
}
}
return AT_TIMEOUT;
}
AtCommandResult AtGsmSimComA7600_CloseConnection(
tAtCmd *env,
uint8_t linkNum
) {
AtCmdPrepare(env);
if (linkNum > 9) {
return AT_ERROR;
}
AtCmdTxClear(env);
AtCmdTxAddStatic(env, "AT+CIPClOSE=");
AtCmdTxAddDecimalIntWithLimit(env, linkNum, 1);
AtCmdTxSendLn(env);
uint32_t timeout = 1000;//env->stdRxTimeout;
uint32_t endMs = SystemGetMs() + timeout;
uint32_t leftMs = timeout;
while ((AtCmdReceiveNextLine(env, leftMs) == AT_OK) && (SystemGetMs() < endMs)) {
leftMs = endMs - SystemGetMs();
if (AtCmdRxBeginWithStatic(env, "OK")) {
AtCmdRxClear(env);
} else if (AtCmdRxBeginWithStatic(env, "CLOSED")) {
AtCmdRxClear(env);
return AT_OK;
} else if (AtCmdRxBeginWithStatic(env, "ERROR")) {
AtCmdRxClear(env);
return AT_ERROR;
} else {
AtCmdProcessUnresolvedLine(env);
AtCmdRxClear(env);
continue;
}
}
return AT_TIMEOUT;
}