167 lines
4.4 KiB
C
167 lines
4.4 KiB
C
//
|
|
// Created by cfif on 24.05.2024.
|
|
//
|
|
#include "AtGsmSimComA7600_SSL_EstablishConnection.h"
|
|
#include "AtCmdCommonProtected.h"
|
|
#include "SystemDelayInterface.h"
|
|
#include <string.h>
|
|
#include "stdlib.h"
|
|
|
|
AtCommandResult AtGsmSimComA7600_SSL_EstablishConnection(
|
|
tAtCmd *env,
|
|
uint8_t linkNum,
|
|
char *srvIp,
|
|
uint8_t srvIpLen,
|
|
uint16_t srvPort,
|
|
eAtGsmSimComA7600_SSL_Type sslType,
|
|
uint8_t *codeResultOpen
|
|
) {
|
|
AtCmdPrepare(env);
|
|
AtCmdRxClear(env);
|
|
|
|
if (linkNum > 1) {
|
|
return AT_ERROR;
|
|
}
|
|
|
|
AtCmdTxClear(env);
|
|
AtCmdTxAddStatic(env, "AT+CCHOPEN=");
|
|
AtCmdTxAddDecimalIntWithLimit(env, linkNum, 1);
|
|
AtCmdTxAddChar(env, ',');
|
|
|
|
if (srvIpLen) {
|
|
AtCmdTxAddChar(env, '"');
|
|
AtCmdTxAdd(env, srvIp, srvIpLen);
|
|
AtCmdTxAddChar(env, '"');
|
|
}
|
|
AtCmdTxAddChar(env, ',');
|
|
|
|
if (srvPort) {
|
|
AtCmdTxAddDecimalIntWithLimit(env, srvPort, 5);
|
|
}
|
|
if (sslType) {
|
|
AtCmdTxAddChar(env, ',');
|
|
AtCmdTxAddDecimalIntWithLimit(env, sslType, 1);
|
|
}
|
|
|
|
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, "+CCHOPEN: ")) {
|
|
|
|
*codeResultOpen = atoi(&env->rxBuffer.data[sizeof("+CIPOPEN: X,") - 1]);
|
|
|
|
AtCommandResult res = (env->rxBuffer.data[sizeof("+CIPOPEN: X,") - 1] == '0') ? AT_OK : AT_ERROR;
|
|
AtCmdRxClear(env);
|
|
return res;
|
|
} else if (AtCmdRxBeginWithStatic(env, "ERROR")) {
|
|
*codeResultOpen = 20;
|
|
AtCmdRxClear(env);
|
|
return AT_ERROR;
|
|
} else {
|
|
AtCmdProcessUnresolvedLine(env);
|
|
AtCmdRxClear(env);
|
|
continue;
|
|
}
|
|
}
|
|
|
|
return AT_TIMEOUT;
|
|
}
|
|
|
|
|
|
AtCommandResult AtGsmSimComA7600_SSL_HasConnection(tAtCmd *env, uint8_t linkNum, bool *status) {
|
|
|
|
char pattern[13];
|
|
|
|
memcpy(pattern, "+CCHOPEN: X", 11);
|
|
|
|
pattern[10] = vAsciiStringDecimalIntToChar(linkNum);
|
|
pattern[11] = ',';
|
|
pattern[12] = '"';
|
|
|
|
uint8_t pattern_len = sizeof(pattern);
|
|
*status = false;
|
|
|
|
AtCmdPrepare(env);
|
|
AtCmdSendStatic(env, "AT+CCHOPEN?\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, "+CCHOPEN")) {
|
|
if (AtCmdRxBeginWith(env, pattern, pattern_len)) {
|
|
if (env->rxBuffer.data[pattern_len] == '"')
|
|
*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_SSL_CloseConnection(
|
|
tAtCmd *env,
|
|
uint8_t linkNum
|
|
) {
|
|
AtCmdPrepare(env);
|
|
|
|
if (linkNum > 9) {
|
|
return AT_ERROR;
|
|
}
|
|
|
|
AtCmdTxClear(env);
|
|
AtCmdTxAddStatic(env, "AT+CCHCLOSE=");
|
|
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;
|
|
} |