AtGsmSimA76xx/Src/AtGsmA76xx.c

85 lines
2.9 KiB
C

//
// Created by cfif on 25.01.23.
//
#include "AtGsmA76xx.h"
#include "stdlib.h"
#include "memory.h"
// начало-------------------------- Служебные ----------------------------------------------------------------------
// начало-------------------------- Служебные ----------------------------------------------------------------------
// начало-------------------------- Служебные ----------------------------------------------------------------------
// IMEI
AtCommandResult AtGsm_Simcom_A76xx_GET_CGSN(tAtCmd *env, char *acpString, size_t *acpStringLen) {
AtCmdPrepare(env);
AtCmdTxClear(env);
AtCmdTxAddStatic(env, "AT+CGSN");
AtCmdTxSendLn(env);
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, "ERROR")) {
AtCmdRxClear(env);
return AT_ERROR;
} else if (AtCmdRxBeginWithStatic(env, "AT+CGSN")) {
while (AtCmdReceiveNextLine(env, 1000) == AT_OK) {
*acpStringLen = env->rxBuffer.len - 2;
memcpy(acpString, env->rxBuffer.data, *acpStringLen);
AtCmdRxClear(env);
return AtCmdOkErrAnswer(env, env->stdRxTimeout);
}
} else {
AtCmdProcessUnresolvedLine(env);
AtCmdRxClear(env);
continue;
}
}
return AT_TIMEOUT;
}
AtCommandResult AtGsm_Simcom_A76xx_CCID(tAtCmd *env, char *ssidStr, size_t *ssidStrLen) {
AtCmdPrepare(env);
AtCmdTxClear(env);
AtCmdSendStatic(env, "AT+CICCID\r\n");
uint32_t endMs = SystemGetMs() + 5000;
while (endMs > SystemGetMs()) {
if (AtCmdReceiveNextLine(env, 1000) == AT_OK) {
if (AtCmdRxBeginWithStatic(env, "+ICCID: ")) {
static const uint16_t prefixSize = sizeof("+ICCID: ") - 1;
*ssidStrLen = env->rxBuffer.len - (prefixSize + 2);
memcpy(ssidStr, env->rxBuffer.data + prefixSize, *ssidStrLen);
return AT_OK;
}
if (AtCmdRxBeginWithStatic(env, "ERROR")) {
return AT_ERROR;
}
if (AtCmdRxBeginWithStatic(env, "+CME ERROR: SIM busy")) {
AtCmdPrepare(env);
AtCmdRxClear(env);
continue;
}
AtCmdProcessUnresolvedLine(env);
}
}
return AT_TIMEOUT;
}
// конец-------------------------- Служебные ----------------------------------------------------------------------
// конец-------------------------- Служебные ----------------------------------------------------------------------
// конец-------------------------- Служебные ----------------------------------------------------------------------