340 lines
8.4 KiB
C
Executable File
340 lines
8.4 KiB
C
Executable File
//
|
|
// Created by xemon on 10.05.23.
|
|
//
|
|
#include <memory.h>
|
|
#include "AtGsmTelitLe910_EstablishConnection.h"
|
|
#include "AtCmdCommonProtected.h"
|
|
#include "SystemDelayInterface.h"
|
|
#include "string.h"
|
|
#include "stdlib.h"
|
|
|
|
|
|
AtCommandResult AtGsmTelitLe910_EstablishConnection(
|
|
tAtCmd *env,
|
|
uint8_t linkNum,
|
|
eAtGsmSimComA7600_IpType type,
|
|
char *srvIp,
|
|
uint8_t srvIpLen,
|
|
uint16_t srvPort,
|
|
uint16_t localPort
|
|
) {
|
|
AtCmdPrepare(env);
|
|
|
|
if (linkNum > 9) {
|
|
return AT_ERROR;
|
|
}
|
|
|
|
AtCmdTxClear(env);
|
|
AtCmdTxAddStatic(env, "AT#SD=");
|
|
AtCmdTxAddDecimalIntWithLimit(env, linkNum, 1);
|
|
AtCmdTxAddChar(env, ',');
|
|
|
|
switch (type) {
|
|
|
|
case eAtGsmSimComA7600_IpType_TCP:
|
|
AtCmdTxAddStatic(env, "0");
|
|
break;
|
|
|
|
case eAtGsmSimComA7600_IpType_UDP:
|
|
AtCmdTxAddStatic(env, "1");
|
|
break;
|
|
default:
|
|
return AT_ERROR;
|
|
|
|
}
|
|
AtCmdTxAddChar(env, ',');
|
|
|
|
if (srvPort) {
|
|
AtCmdTxAddDecimalIntWithLimit(env, srvPort, 5);
|
|
}
|
|
AtCmdTxAddChar(env, ',');
|
|
|
|
if (srvIpLen) {
|
|
AtCmdTxAddChar(env, '"');
|
|
AtCmdTxAdd(env, srvIp, srvIpLen);
|
|
AtCmdTxAddChar(env, '"');
|
|
}
|
|
AtCmdTxAddChar(env, ',');
|
|
|
|
AtCmdTxAddStatic(env, "0");
|
|
|
|
AtCmdTxAddChar(env, ',');
|
|
|
|
// if (localPort) {
|
|
AtCmdTxAddDecimalIntWithLimit(env, localPort, 5);
|
|
// }
|
|
|
|
AtCmdTxAddChar(env, ',');
|
|
|
|
AtCmdTxAddStatic(env, "1");
|
|
|
|
AtCmdTxSendLn(env);
|
|
|
|
uint32_t timeout = env->stdRxTimeout * 5;
|
|
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, "CONNECT")) {
|
|
AtCmdRxClear(env);
|
|
} else if (AtCmdRxBeginWithStatic(env, "ERROR")) {
|
|
AtCmdRxClear(env);
|
|
return AT_ERROR;
|
|
|
|
} else if (AtCmdRxBeginWithStatic(env, "+CME ERROR:")) {
|
|
AtCmdRxClear(env);
|
|
return AT_ERROR;
|
|
} else {
|
|
AtCmdProcessUnresolvedLine(env);
|
|
AtCmdRxClear(env);
|
|
continue;
|
|
}
|
|
}
|
|
|
|
return AT_TIMEOUT;
|
|
}
|
|
|
|
/*
|
|
AtCommandResult AtGsmSimComA7600_EstablishConnection(
|
|
tAtCmd *env,
|
|
uint8_t linkNum,
|
|
eAtGsmSimComA7600_IpType type,
|
|
char *srvIp,
|
|
uint8_t srvIpLen,
|
|
uint16_t srvPort,
|
|
uint16_t localPort
|
|
) {
|
|
AtCmdPrepare(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);
|
|
}
|
|
AtCmdTxAddChar(env, ',');
|
|
|
|
if (localPort) {
|
|
AtCmdTxAddDecimalIntWithLimit(env, localPort, 5);
|
|
}
|
|
|
|
AtCmdTxSendLn(env);
|
|
|
|
uint32_t timeout = env->stdRxTimeout * 5;
|
|
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 AtGsmTelitLe910_HasConnection(tAtCmd *env, uint8_t linkNum, bool *status) {
|
|
|
|
char pattern[9];
|
|
memset(pattern, 0, sizeof(pattern));
|
|
strcat(pattern, "#SS: ");
|
|
utoa(linkNum, &pattern[strlen(pattern)], 10);
|
|
strcat(pattern, ",");
|
|
|
|
*status = false;
|
|
|
|
AtCmdPrepare(env);
|
|
AtCmdSendStatic(env, "AT#SS\r\n");
|
|
|
|
uint32_t timeout = env->stdRxTimeout * 5;
|
|
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 (AtCmdRxBeginWith(env, pattern, strlen(pattern))) {
|
|
|
|
if ((env->rxBuffer.data[strlen(pattern)] == '0') || (env->rxBuffer.data[strlen(pattern)] == '3')) {
|
|
*status = false;
|
|
} else {
|
|
*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_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 * 5;
|
|
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 AtGsmTelitLe910_CloseConnection(
|
|
tAtCmd *env,
|
|
uint8_t linkNum
|
|
) {
|
|
// AtCmdPrepare(env);
|
|
|
|
if (linkNum > 9) {
|
|
return AT_ERROR;
|
|
}
|
|
|
|
// AtCmdPrepare(env);
|
|
AtCmdTxClear(env);
|
|
AtCmdTxAddStatic(env, "AT#SH=");
|
|
AtCmdTxAddDecimalIntWithLimit(env, linkNum, 2);
|
|
AtCmdTxSendLn(env);
|
|
|
|
//debug_printf(env->io, "AT#SH=%u\r\n", idSocket);
|
|
return AtCmdOkErrAnswer(env, 5 * env->stdRxTimeout);
|
|
|
|
}
|
|
|
|
/*
|
|
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 = env->stdRxTimeout * 5;
|
|
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;
|
|
}
|
|
*/ |