Init
This commit is contained in:
commit
64e8e336ce
|
|
@ -0,0 +1,84 @@
|
||||||
|
//
|
||||||
|
// Created by cfif on 04.10.2022.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef TEXTERNAL_H
|
||||||
|
#define TEXTERNAL_H
|
||||||
|
|
||||||
|
#include <SerialPortIO.h>
|
||||||
|
#include <DeviceStorageIni.h>
|
||||||
|
#include <Gnss.h>
|
||||||
|
#include "ModemMain.h"
|
||||||
|
#include "auth.h"
|
||||||
|
#include "file_logger.h"
|
||||||
|
|
||||||
|
#define lenHexCRC 4
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
PROTOCOL_EXTERNAL = 0,
|
||||||
|
PROTOKOL_TELEMETRY = 1,
|
||||||
|
PROTOKOL_AMS_RADAR = 2,
|
||||||
|
PROTOKOL_LORA_WAN = 3
|
||||||
|
} tProtokolExternal;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
tSerialPortIO *io;
|
||||||
|
tDeviceStorageIni *store;
|
||||||
|
tFs *fs;
|
||||||
|
tModemMain *modemMain;
|
||||||
|
tRtcIO *rtcIo;
|
||||||
|
|
||||||
|
struct {
|
||||||
|
uint8_t modemRx[1024];
|
||||||
|
uint8_t modemTx[1024];
|
||||||
|
} mem;
|
||||||
|
|
||||||
|
tAtCmd externalAt;
|
||||||
|
uint32_t count;
|
||||||
|
uint32_t countPack;
|
||||||
|
uint32_t stepTimeSecond;
|
||||||
|
|
||||||
|
uint16_t sensorA;
|
||||||
|
uint16_t sensorB;
|
||||||
|
uint8_t sensorExt[16];
|
||||||
|
|
||||||
|
tLoggerInterface *logger;
|
||||||
|
|
||||||
|
struct {
|
||||||
|
osThreadId_t id;
|
||||||
|
uint32_t stack[2048];
|
||||||
|
StaticTask_t controlBlock;
|
||||||
|
osThreadAttr_t attr;
|
||||||
|
} thread;
|
||||||
|
|
||||||
|
uint32_t left;
|
||||||
|
char *bufAnswer;
|
||||||
|
char fileNameContent[128];
|
||||||
|
bool isContentFile;
|
||||||
|
|
||||||
|
|
||||||
|
struct {
|
||||||
|
int paramcount;
|
||||||
|
char *params_names[MAX_POST_GET_PARAMETERS];
|
||||||
|
char *params_vals[MAX_POST_GET_PARAMETERS];
|
||||||
|
} params_post_uri;
|
||||||
|
|
||||||
|
osMutexId_t sensorAccess;
|
||||||
|
|
||||||
|
} tExternal;
|
||||||
|
|
||||||
|
void getSensorAbExt(tExternal *env, uint16_t *sensorA, uint16_t *sensorB, uint8_t *sensorExt);
|
||||||
|
|
||||||
|
void ReInitExternalProtocol(tExternal *env);
|
||||||
|
|
||||||
|
void External_Init(tExternal *env,
|
||||||
|
tSerialPortIO *io,
|
||||||
|
tDeviceStorageIni *store,
|
||||||
|
tFs *fs,
|
||||||
|
tRtcIO *rtcIo,
|
||||||
|
tModemMain *modemMain);
|
||||||
|
|
||||||
|
void External_StartThread(tExternal *env);
|
||||||
|
|
||||||
|
|
||||||
|
#endif //TEXTERNAL_H
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
//
|
||||||
|
// Created by cfif on 27.06.23.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef GONEC_FULL_CRC16_TRACER_H
|
||||||
|
#define GONEC_FULL_CRC16_TRACER_H
|
||||||
|
|
||||||
|
unsigned short CRC16soft(unsigned char * pcBlock, unsigned short len);
|
||||||
|
|
||||||
|
#endif //GONEC_FULL_CRC16_TRACER_H
|
||||||
|
|
@ -0,0 +1,503 @@
|
||||||
|
//
|
||||||
|
// Created by cfif on 04.10.2022.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <SystemDelayInterface.h>
|
||||||
|
#include <External.h>
|
||||||
|
#include <CmsisRtosThreadUtils.h>
|
||||||
|
#include "SerialPort.h"
|
||||||
|
#include "string.h"
|
||||||
|
#include "fs_base_func.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "httpd_post.h"
|
||||||
|
#include "httpd_get.h"
|
||||||
|
#include "httpd_base_func.h"
|
||||||
|
#include "json_func.h"
|
||||||
|
#include "crc16_tracer.h"
|
||||||
|
|
||||||
|
#define LOGGER env->logger
|
||||||
|
#define LOG_SIGN "GONEC"
|
||||||
|
|
||||||
|
extern char bufAnswerGetGlobal[LEN_BUF_SMALL_ANSWER_HTTP];
|
||||||
|
|
||||||
|
void External_Init(tExternal *env,
|
||||||
|
tSerialPortIO *io,
|
||||||
|
tDeviceStorageIni *store,
|
||||||
|
tFs *fs,
|
||||||
|
tRtcIO *rtcIo,
|
||||||
|
tModemMain *modemMain
|
||||||
|
) {
|
||||||
|
env->io = io;
|
||||||
|
env->store = store;
|
||||||
|
env->fs = fs;
|
||||||
|
env->rtcIo = rtcIo;
|
||||||
|
env->modemMain = modemMain;
|
||||||
|
|
||||||
|
env->count = 0;
|
||||||
|
env->countPack = 0;
|
||||||
|
env->stepTimeSecond = 0;
|
||||||
|
|
||||||
|
memset(env->sensorExt, 0, sizeof(env->sensorExt));
|
||||||
|
env->sensorA = 0;
|
||||||
|
env->sensorB = 0;
|
||||||
|
|
||||||
|
env->sensorAccess = osMutexNew(NULL);
|
||||||
|
|
||||||
|
env->bufAnswer = bufAnswerGetGlobal;
|
||||||
|
|
||||||
|
AtCmdInit(
|
||||||
|
&env->externalAt, io,
|
||||||
|
env->mem.modemTx, sizeof(env->mem.modemTx),
|
||||||
|
env->mem.modemRx, sizeof(env->mem.modemRx),
|
||||||
|
5000, 5000
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
//Инициализируем поток
|
||||||
|
InitThreadAtrStatic(&env->thread.attr, "External", env->thread.controlBlock, env->thread.stack, osPriorityNormal);
|
||||||
|
env->thread.id = 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void ReInitExternalProtocol(tExternal *env) {
|
||||||
|
env->count = 0;
|
||||||
|
env->countPack = 0;
|
||||||
|
env->stepTimeSecond = 0;
|
||||||
|
memset(env->sensorExt, 0, sizeof(env->sensorExt));
|
||||||
|
env->sensorA = 0;
|
||||||
|
env->sensorB = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SendFileToUART(tFs *fs, char *fileName, tAtCmd *At) {
|
||||||
|
|
||||||
|
FIL fsrc; // File objects
|
||||||
|
BYTE buffer[512]; // File copy buffer
|
||||||
|
FRESULT fr; // FatFs function common result code
|
||||||
|
UINT br; // File read/write count
|
||||||
|
|
||||||
|
fr = f_open_i(fs, &fsrc, fileName, FA_READ);
|
||||||
|
if (fr) return false;
|
||||||
|
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
f_read_i(fs, &fsrc, buffer, sizeof(buffer), &br);
|
||||||
|
if (br == 0) break;
|
||||||
|
|
||||||
|
AtCmdTxClear(At);
|
||||||
|
AtCmdSend(At, buffer, br);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
f_close_i(fs, &fsrc);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ExternalProtocol(tExternal *env) {
|
||||||
|
|
||||||
|
char *params = NULL;
|
||||||
|
char uri[100];
|
||||||
|
char uri2[100];
|
||||||
|
|
||||||
|
while (AtCmdReceiveNextLine(&env->externalAt, 1000) == AT_OK) {
|
||||||
|
|
||||||
|
if (osMutexAcquire(httpSettings.accessHTTP, TIME_MUTEX_HTTP_ACCESS) == osOK) {
|
||||||
|
|
||||||
|
if (env->externalAt.rxBuffer.len > sizeof(uri)) {
|
||||||
|
uint32_t err = Post_PARAM_ERR;
|
||||||
|
env->left = json_generate_err_answer(env->bufAnswer, LEN_BUF_SMALL_ANSWER_HTTP, err);
|
||||||
|
AtCmdTxClear(&env->externalAt);
|
||||||
|
AtCmdSend(&env->externalAt, env->bufAnswer, env->left);
|
||||||
|
|
||||||
|
osMutexRelease(httpSettings.accessHTTP);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
uri[0] = '/';
|
||||||
|
memcpy(&uri[1], &env->externalAt.rxBuffer.data[2], env->externalAt.rxBuffer.len - 2);
|
||||||
|
|
||||||
|
if (uri[env->externalAt.rxBuffer.len - 2] == '\r')
|
||||||
|
uri[env->externalAt.rxBuffer.len - 2] = '\0';
|
||||||
|
|
||||||
|
if (uri[env->externalAt.rxBuffer.len - 2] == '\n')
|
||||||
|
uri[env->externalAt.rxBuffer.len - 2] = '\0';
|
||||||
|
|
||||||
|
if (uri[env->externalAt.rxBuffer.len - 3] == '\r')
|
||||||
|
uri[env->externalAt.rxBuffer.len - 3] = '\0';
|
||||||
|
|
||||||
|
if (uri[env->externalAt.rxBuffer.len - 3] == '\n')
|
||||||
|
uri[env->externalAt.rxBuffer.len - 3] = '\0';
|
||||||
|
|
||||||
|
|
||||||
|
int pos = -1;
|
||||||
|
int len = -1;
|
||||||
|
char filename[MAX_LEN_PATH_FS];
|
||||||
|
filename[0] = '\0';
|
||||||
|
|
||||||
|
|
||||||
|
memcpy(uri2, uri, sizeof(uri));
|
||||||
|
|
||||||
|
char *params2 = NULL;
|
||||||
|
|
||||||
|
params2 = (char *) strchr(uri2, '?');
|
||||||
|
if (params2 != NULL) {
|
||||||
|
*params2 = '\0';
|
||||||
|
params2++;
|
||||||
|
}
|
||||||
|
|
||||||
|
env->params_post_uri.paramcount = extract_uri_ex_parameters(params2, env->params_post_uri.params_names,
|
||||||
|
env->params_post_uri.params_vals,
|
||||||
|
MAX_POST_GET_PARAMETERS);
|
||||||
|
|
||||||
|
if (strcmp(env->params_post_uri.params_names[0], "wrfile") == 0) {
|
||||||
|
|
||||||
|
for (int i = 0; i < env->params_post_uri.paramcount; ++i) {
|
||||||
|
|
||||||
|
if (strcmp(env->params_post_uri.params_names[i], "pos") == 0)
|
||||||
|
pos = atoi(env->params_post_uri.params_vals[i]);
|
||||||
|
|
||||||
|
if (strcmp(env->params_post_uri.params_names[i], "len") == 0)
|
||||||
|
len = atoi(env->params_post_uri.params_vals[i]);
|
||||||
|
|
||||||
|
if (strcmp(env->params_post_uri.params_names[i], "wrfile") == 0)
|
||||||
|
extract_utf8_parameters(env->params_post_uri.params_vals[i], filename,
|
||||||
|
sizeof(filename));
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((pos == -1) || (len == -1) || (strlen(filename) == 0) || (len > 1024)) {
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
AtCmdPrepare(&env->externalAt);
|
||||||
|
AtCmdTxClear(&env->externalAt);
|
||||||
|
AtCmdTxAddStatic(&env->externalAt, ">");
|
||||||
|
AtCmdTxSendLn(&env->externalAt);
|
||||||
|
AtCmdRxClear(&env->externalAt);
|
||||||
|
|
||||||
|
uint16_t size = AtCmdRxReadRAW(&env->externalAt, (uint8_t *) env->bufAnswer, len, 5000);
|
||||||
|
|
||||||
|
if (len != size) {
|
||||||
|
uint32_t err = Post_PARAM_ERR;
|
||||||
|
env->left = json_generate_err_answer(env->bufAnswer, LEN_BUF_SMALL_ANSWER_HTTP, err);
|
||||||
|
AtCmdTxClear(&env->externalAt);
|
||||||
|
AtCmdSend(&env->externalAt, env->bufAnswer, env->left);
|
||||||
|
|
||||||
|
osMutexRelease(httpSettings.accessHTTP);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
params = (char *) strchr(uri, '?');
|
||||||
|
if (params != NULL) {
|
||||||
|
*params = '\0';
|
||||||
|
params++;
|
||||||
|
}
|
||||||
|
|
||||||
|
idPostResult_t resultGet = httpd_get_begin(&httpSettings, httpSettings.auth, uri, params,
|
||||||
|
&env->isContentFile,
|
||||||
|
env->fileNameContent,
|
||||||
|
env->bufAnswer,
|
||||||
|
&env->left);
|
||||||
|
|
||||||
|
if (env->isContentFile) {
|
||||||
|
|
||||||
|
SendFileToUART(env->fs, env->fileNameContent, &env->externalAt);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
if (env->left > 0) {
|
||||||
|
AtCmdTxClear(&env->externalAt);
|
||||||
|
AtCmdSend(&env->externalAt, env->bufAnswer, env->left);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
osMutexRelease(httpSettings.accessHTTP);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
AtCommandResult getDataAmsRadar(tExternal *env, int count) {
|
||||||
|
|
||||||
|
AtCmdPrepare(&env->externalAt);
|
||||||
|
AtCmdTxClear(&env->externalAt);
|
||||||
|
AtCmdTxAddStatic(&env->externalAt, "DATA=?");
|
||||||
|
AtCmdTxSendLn(&env->externalAt);
|
||||||
|
|
||||||
|
#ifdef DEBUG_EXT_MODEM
|
||||||
|
if (count == 0) {
|
||||||
|
LoggerInfoStatic(LOGGER, LOG_SIGN, "Отправка команды на АМС: DATA=?");
|
||||||
|
} else {
|
||||||
|
char bufText[128];
|
||||||
|
bufText[0] = '\0';
|
||||||
|
strcat(bufText, "Отправка команды на АМС (попытка №");
|
||||||
|
|
||||||
|
char buf[12];
|
||||||
|
utoa(count, buf, 10);
|
||||||
|
strcat(bufText, buf);
|
||||||
|
|
||||||
|
strcat(bufText, "): DATA=?");
|
||||||
|
|
||||||
|
LoggerInfo(LOGGER, LOG_SIGN, bufText, strlen(bufText));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
uint32_t timeout = 5000;
|
||||||
|
uint32_t endMs = SystemGetMs() + timeout;
|
||||||
|
uint32_t leftMs = timeout;
|
||||||
|
|
||||||
|
while ((AtCmdReceiveNextLine(&env->externalAt, leftMs) == AT_OK) && (SystemGetMs() < endMs)) {
|
||||||
|
leftMs = endMs - SystemGetMs();
|
||||||
|
|
||||||
|
if (env->externalAt.rxBuffer.len > 240) {
|
||||||
|
LoggerInfoStatic(LOGGER, LOG_SIGN,
|
||||||
|
"Превышен размер данных. Игнорирование полученных данных данных от АМС.");
|
||||||
|
//LoggerInfo(LOGGER, LOG_SIGN, env->externalAt.rxBuffer.data, env->externalAt.rxBuffer.len);
|
||||||
|
|
||||||
|
return AT_ERROR;
|
||||||
|
} else {
|
||||||
|
|
||||||
|
bool isStart = false;
|
||||||
|
bool isHead = false;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < env->externalAt.rxBuffer.len; ++i) {
|
||||||
|
|
||||||
|
if (env->externalAt.rxBuffer.data[i] == '$') {
|
||||||
|
isStart = true;
|
||||||
|
isHead = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isStart) {
|
||||||
|
|
||||||
|
if (isHead) {
|
||||||
|
isHead = false;
|
||||||
|
|
||||||
|
time_t timestamp;
|
||||||
|
env->rtcIo->get(&env->rtcIo, ×tamp);
|
||||||
|
|
||||||
|
char buf[12];
|
||||||
|
utoa((uint32_t) timestamp, buf, 10);
|
||||||
|
|
||||||
|
for (size_t j = 0; j < strlen(buf); ++j) {
|
||||||
|
env->bufAnswer[env->count] = buf[j];
|
||||||
|
++env->count;
|
||||||
|
}
|
||||||
|
|
||||||
|
env->bufAnswer[env->count] = ':';
|
||||||
|
++env->count;
|
||||||
|
}
|
||||||
|
|
||||||
|
env->bufAnswer[env->count] = env->externalAt.rxBuffer.data[i];
|
||||||
|
++env->count;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (env->externalAt.rxBuffer.data[i] == '\n') {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isStart) {
|
||||||
|
#ifdef DEBUG_EXT_MODEM
|
||||||
|
LoggerInfoStatic(LOGGER, LOG_SIGN, "Получены данные от АМС.");
|
||||||
|
//LoggerInfo(LOGGER, LOG_SIGN, env->externalAt.rxBuffer.data, env->externalAt.rxBuffer.len);
|
||||||
|
#endif
|
||||||
|
++env->countPack;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((env->countPack >= env->store->nvm.Settings_AmsRadar.AmsRadarNumPack) || (env->countPack >= 5)) {
|
||||||
|
//sendAmsRadar(env->modemMain, env->bufAnswer, env->count);
|
||||||
|
sendExtProtokolPack(env->modemMain, TYPE_FILE_AMS_RADAR, env->bufAnswer, env->count);
|
||||||
|
env->countPack = 0;
|
||||||
|
env->count = 0;
|
||||||
|
|
||||||
|
return AT_OK;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return AT_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
void AmsRadarProtocol(tExternal *env) {
|
||||||
|
|
||||||
|
SystemDelayMs(1000);
|
||||||
|
|
||||||
|
++env->stepTimeSecond;
|
||||||
|
if (env->stepTimeSecond >= env->store->nvm.Settings_AmsRadar.AmsRadarReqTime * 60) {
|
||||||
|
env->stepTimeSecond = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i <= 3; ++i) {
|
||||||
|
if (getDataAmsRadar(env, i) == AT_OK) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
AtCommandResult getDataLoraWan(tExternal *env) {
|
||||||
|
AtCmdPrepare(&env->externalAt);
|
||||||
|
|
||||||
|
uint32_t timeout = 1000;
|
||||||
|
uint32_t endMs = SystemGetMs() + timeout;
|
||||||
|
uint32_t leftMs = timeout;
|
||||||
|
|
||||||
|
while ((AtCmdReceiveNextLine(&env->externalAt, leftMs) == AT_OK) && (SystemGetMs() < endMs)) {
|
||||||
|
leftMs = endMs - SystemGetMs();
|
||||||
|
|
||||||
|
if (AtCmdRxBeginWithStatic(&env->externalAt, "JSON up: ")) {
|
||||||
|
|
||||||
|
env->count = env->externalAt.rxBuffer.len;
|
||||||
|
memcpy(env->bufAnswer, env->externalAt.rxBuffer.data, env->externalAt.rxBuffer.len);
|
||||||
|
|
||||||
|
sendExtProtokolPack(env->modemMain, TYPE_FILE_LORA_WAN, env->bufAnswer, env->count);
|
||||||
|
|
||||||
|
AtCmdRxClear(&env->externalAt);
|
||||||
|
return AT_OK;
|
||||||
|
} else {
|
||||||
|
AtCmdProcessUnresolvedLine(&env->externalAt);
|
||||||
|
AtCmdRxClear(&env->externalAt);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return AT_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
void LoraWanProtocol(tExternal *env) {
|
||||||
|
// SystemDelayMs(1000);
|
||||||
|
getDataLoraWan(env);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t getCrcTracer(char *hexCrc) {
|
||||||
|
uint8_t dataCrc[4];
|
||||||
|
iAsciiStringParseHexBytes(dataCrc, hexCrc, 4);
|
||||||
|
return dataCrc[1] | (dataCrc[0] << 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
void getSensorAbExt(tExternal *env, uint16_t *sensorA, uint16_t *sensorB, uint8_t *sensorExt) {
|
||||||
|
|
||||||
|
if (osMutexAcquire(env->sensorAccess, 1000) == osOK) {
|
||||||
|
|
||||||
|
*sensorA = env->sensorA;
|
||||||
|
*sensorB = env->sensorB;
|
||||||
|
|
||||||
|
memcpy(sensorExt, env->sensorExt, 16);
|
||||||
|
|
||||||
|
osMutexRelease(env->sensorAccess);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void TracerProtocol(tExternal *env) {
|
||||||
|
|
||||||
|
AtCmdPrepare(&env->externalAt);
|
||||||
|
AtCmdRxClear(&env->externalAt);
|
||||||
|
uint8_t dataAFX[32];
|
||||||
|
uint8_t dataAF[32];
|
||||||
|
|
||||||
|
while (AtCmdReceiveNextLine(&env->externalAt, 1000) == AT_OK) {
|
||||||
|
|
||||||
|
|
||||||
|
if (AtCmdRxBeginWithStatic(&env->externalAt, "AFX")) {
|
||||||
|
|
||||||
|
--env->externalAt.rxBuffer.len;
|
||||||
|
|
||||||
|
volatile uint16_t crc16 = getCrcTracer(
|
||||||
|
&env->externalAt.rxBuffer.data[env->externalAt.rxBuffer.len - lenHexCRC]);
|
||||||
|
volatile uint16_t crc16calc = CRC16soft((unsigned char *) env->externalAt.rxBuffer.data,
|
||||||
|
env->externalAt.rxBuffer.len - lenHexCRC);
|
||||||
|
|
||||||
|
memset(dataAFX, 0, sizeof(dataAFX));
|
||||||
|
|
||||||
|
if (crc16 == crc16calc) {
|
||||||
|
iAsciiStringParseHexBytes(dataAFX, &env->externalAt.rxBuffer.data[3],
|
||||||
|
env->externalAt.rxBuffer.len - lenHexCRC - 3);
|
||||||
|
|
||||||
|
if (osMutexAcquire(env->sensorAccess, 1000) == osOK) {
|
||||||
|
for (int i = 0; i < 16; ++i) {
|
||||||
|
env->sensorExt[i] = dataAFX[15 - i];
|
||||||
|
}
|
||||||
|
osMutexRelease(env->sensorAccess);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
AtCmdRxClear(&env->externalAt);
|
||||||
|
} else if (AtCmdRxBeginWithStatic(&env->externalAt, "AF")) {
|
||||||
|
|
||||||
|
--env->externalAt.rxBuffer.len;
|
||||||
|
|
||||||
|
volatile uint16_t crc16 = getCrcTracer(
|
||||||
|
&env->externalAt.rxBuffer.data[env->externalAt.rxBuffer.len - lenHexCRC]);
|
||||||
|
volatile uint16_t crc16calc = CRC16soft((unsigned char *) env->externalAt.rxBuffer.data,
|
||||||
|
env->externalAt.rxBuffer.len - lenHexCRC);
|
||||||
|
|
||||||
|
memset(dataAF, 0, sizeof(dataAF));
|
||||||
|
|
||||||
|
if (crc16 == crc16calc) {
|
||||||
|
iAsciiStringParseHexBytes(dataAF, &env->externalAt.rxBuffer.data[2],
|
||||||
|
env->externalAt.rxBuffer.len - lenHexCRC - 2);
|
||||||
|
|
||||||
|
if (osMutexAcquire(env->sensorAccess, 1000) == osOK) {
|
||||||
|
env->sensorA = dataAF[2] | (8 << dataAF[1]);
|
||||||
|
env->sensorB = dataAF[5] | (8 << dataAF[4]);
|
||||||
|
osMutexRelease(env->sensorAccess);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
AtCmdRxClear(&env->externalAt);
|
||||||
|
}
|
||||||
|
|
||||||
|
AtCmdRxClear(&env->externalAt);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static _Noreturn void External_Thread(tExternal *env) {
|
||||||
|
AtCmdPrepare(&env->externalAt);
|
||||||
|
AtCmdRxClear(&env->externalAt);
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
|
||||||
|
if (env->store->runtime.Settings_RS485_Bluetooth.PortAssign == PROTOCOL_EXTERNAL) {
|
||||||
|
ExternalProtocol(env);
|
||||||
|
} else if (env->store->runtime.Settings_RS485_Bluetooth.PortAssign == PROTOKOL_AMS_RADAR) {
|
||||||
|
|
||||||
|
if (env->modemMain->location.isOneDateTime) {
|
||||||
|
SystemDelayMs(1000);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
AmsRadarProtocol(env);
|
||||||
|
} else if (env->store->runtime.Settings_RS485_Bluetooth.PortAssign == PROTOKOL_LORA_WAN) {
|
||||||
|
|
||||||
|
if (env->modemMain->location.isOneDateTime) {
|
||||||
|
SystemDelayMs(1000);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
LoraWanProtocol(env);
|
||||||
|
} else if (env->store->runtime.Settings_RS485_Bluetooth.PortAssign == PROTOKOL_TELEMETRY) {
|
||||||
|
TracerProtocol(env);
|
||||||
|
} else {
|
||||||
|
SystemDelayMs(1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void External_StartThread(tExternal *env) {
|
||||||
|
if (!env->thread.id) {
|
||||||
|
env->thread.id = osThreadNew((osThreadFunc_t) (External_Thread), (void *) (env), &env->thread.attr);
|
||||||
|
} else {
|
||||||
|
osThreadResume(env->thread.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,46 @@
|
||||||
|
//
|
||||||
|
// Created by cfif on 27.06.23.
|
||||||
|
//
|
||||||
|
|
||||||
|
// Polynomial 1021, Initial 0xFFFF, XORout 0xFFFF
|
||||||
|
unsigned short CRC16soft(unsigned char * pcBlock, unsigned short len)
|
||||||
|
{
|
||||||
|
const unsigned short Crc16Table[256] = {
|
||||||
|
0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
|
||||||
|
0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
|
||||||
|
0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
|
||||||
|
0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
|
||||||
|
0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
|
||||||
|
0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
|
||||||
|
0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
|
||||||
|
0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
|
||||||
|
0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
|
||||||
|
0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
|
||||||
|
0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
|
||||||
|
0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
|
||||||
|
0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
|
||||||
|
0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
|
||||||
|
0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
|
||||||
|
0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
|
||||||
|
0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
|
||||||
|
0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
|
||||||
|
0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
|
||||||
|
0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
|
||||||
|
0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
|
||||||
|
0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
|
||||||
|
0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
|
||||||
|
0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
|
||||||
|
0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
|
||||||
|
0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
|
||||||
|
0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
|
||||||
|
0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
|
||||||
|
0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
|
||||||
|
0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
|
||||||
|
0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
|
||||||
|
0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
|
||||||
|
};
|
||||||
|
unsigned short crc = 0xFFFF;
|
||||||
|
while (len--)
|
||||||
|
crc = (crc >> 8) ^ Crc16Table[(crc ^ *pcBlock++)&0xFF];
|
||||||
|
return crc^0xFFFF;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
{
|
||||||
|
"dep": [
|
||||||
|
{
|
||||||
|
"type": "git",
|
||||||
|
"provider": "GONEC_NEW",
|
||||||
|
"repo": "SystemDelayInterface"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
|
||||||
|
"cmake": {
|
||||||
|
"inc_dirs": [
|
||||||
|
"Inc"
|
||||||
|
],
|
||||||
|
"srcs": [
|
||||||
|
"Src/**.c"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue