91 lines
3.1 KiB
C
91 lines
3.1 KiB
C
//
|
|
// Created by cfif on 05.07.23.
|
|
//
|
|
#include <SystemDelayInterface.h>
|
|
#include "GsmWithGnss.h"
|
|
#include "AtGsmSimComA7600.h"
|
|
#include "Gsm_SockInt.h"
|
|
|
|
#define LOG_SIGN "GSM SOCK"
|
|
#define LOGGER &env->slog->logger
|
|
|
|
tGsmSocketSlot *Gsm_getFreeSlot(tGsmWithGnss *env) {
|
|
for (uint8_t i = 0; i < GSM_SLOTS_COUNT; ++i) {
|
|
if (GsmSocketSlot_IsClear(env->slots + i)) {
|
|
return env->slots + i;
|
|
}
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
static bool Gsm_StayConnected_socket(
|
|
tGsmWithGnss *env, uint8_t idx, eSocketType type, eAtGsmSimComA7600_SSL_Type clientType, char *addr, size_t addrSize, uint16_t port
|
|
) {
|
|
bool connected = false;
|
|
AtGsmSimComA7600_HasConnection(env->gsmAt, idx, &connected);
|
|
if (connected) {
|
|
return true;
|
|
} else {
|
|
if (AtGsmSimComA7600_HasIp(env->gsmAt) != AT_OK) {
|
|
AtGsmSimComA7600_StartSocketService(env->gsmAt);
|
|
}
|
|
return AtGsmSimComA7600_EstablishConnection(
|
|
env->gsmAt,
|
|
idx,
|
|
type == eSocketType_TCP ? eAtGsmSimComA7600_IpType_TCP : eAtGsmSimComA7600_IpType_UDP,
|
|
addr,
|
|
addrSize,
|
|
port,
|
|
type == eSocketType_TCP ? 0 : port
|
|
// "193.232.47.4", 30197
|
|
) == AT_OK;
|
|
}
|
|
}
|
|
|
|
uint8_t Gsm_Socket_open(tGsmWithGnss *env, eSocketType type, eAtGsmSimComA7600_SSL_Type clientType, char *addr, size_t addrSize, uint16_t port, uint32_t timeout) {
|
|
uint8_t res = SOCKET_WRONG_CONTEXT;
|
|
|
|
if (osMutexAcquire(env->gsmAt->access, timeout) == osOK) {
|
|
tGsmSocketSlot *slot = Gsm_getFreeSlot(env);
|
|
if (slot) {
|
|
if (Gsm_StayConnected_socket(env, slot->contextId, type, clientType, addr, addrSize, port)) {
|
|
GsmSocketSlot_Set(slot, type, addr, addrSize, port);
|
|
res = slot->contextId;
|
|
} else {
|
|
res = SOCKET_WRONG_CONTEXT;
|
|
}
|
|
}
|
|
osMutexRelease(env->gsmAt->access);
|
|
} else {
|
|
LoggerInfoStatic(LOGGER, LOG_SIGN, "Ошибка доступа (Gsm_Socket_open)")
|
|
}
|
|
return res;
|
|
}
|
|
|
|
void Gsm_Socket_close(tGsmWithGnss *env, uint8_t contextId) {
|
|
if (osMutexAcquire(env->gsmAt->access, defaultSocketTimeout) == osOK) {
|
|
if (contextId != SOCKET_WRONG_CONTEXT) {
|
|
AtGsmSimComA7600_CloseConnection(env->gsmAt, contextId);
|
|
GsmSocketSlot_Clear(env->slots + contextId);
|
|
} else {
|
|
asm("nop");
|
|
}
|
|
osMutexRelease(env->gsmAt->access);
|
|
} else {
|
|
LoggerInfoStatic(LOGGER, LOG_SIGN, "Ошибка доступа (Gsm_Socket_close)")
|
|
}
|
|
}
|
|
|
|
|
|
eSocketStatus Gsm_Socket_status(tGsmWithGnss *env, uint8_t contextId) {
|
|
bool connected = false;
|
|
if (osMutexAcquire(env->gsmAt->access, defaultSocketTimeout) == osOK) {
|
|
AtGsmSimComA7600_HasConnection(env->gsmAt, contextId, &connected);
|
|
osMutexRelease(env->gsmAt->access);
|
|
} else {
|
|
LoggerInfoStatic(LOGGER, LOG_SIGN, "Ошибка доступа (Gsm_Socket_status)")
|
|
}
|
|
|
|
return connected ? eSocketStatus_Connected : eSocketStatus_Disconnected;
|
|
}
|