//
|
// Created by YY on 2019/12/21.
|
//
|
|
#include <cstring>
|
#include <thread>
|
#include <mutex>
|
#include <cstdlib>
|
#include "mcu_if.h"
|
#include "ahp.h"
|
#include "../common/apptimer.h"
|
#include "../utils/crc16.h"
|
#include "../defs.h"
|
#include "../jni_log.h"
|
#include "../common/serial_port.h"
|
#include "../rtk_platform/platform.h"
|
#include "../rtk_module/parse_gps.h"
|
#include "../native-lib.h"
|
#include "../rtk_module/virtual_rtk.h"
|
#include "../common/string_util.h"
|
|
#define DEBUG(fmt, args...) LOGD("<mcu_if> <%s>: " fmt, __func__, ##args)
|
|
#define MCU_UART UART_1
|
|
#define ID_CM_AHP_INQ 0x0001
|
#define ID_MC_AHP_RESP 0x8001
|
#define ID_CM_AHP_DFU_DATA_UPLOAD 0x0002
|
#define ID_MC_AHP_DFU_RESP 0x8002
|
#define ID_MC_RTK_DATA 0x8008
|
#define ID_CM_RTK_DATA 0x0008
|
#define ID_CM_READ_RFCARD 0x0009
|
|
static int (*WriteMcu)(int id, const void *buf, int len);
|
|
static int WriteBluetooth(int id, const void *buf, int len);
|
|
static void ReadCardTimeout(apptimer_var_t val);
|
|
ParseUart::ParseUart(funptr fun) {
|
McuPkt.buffer = new uint8_t [4096 + 4];
|
this->fun = fun;
|
parse_status = SYNC_HEAD_ONE;
|
}
|
|
ParseUart::~ParseUart() {
|
delete []McuPkt.buffer;
|
}
|
|
static int WriteBluetooth(int id, const void *buf, int len)
|
{
|
SendToBluetooth((uint8_t *)buf, len);
|
return len;
|
}
|
|
void ParseUart::ParseMcu(const uint8_t *data, int length)
|
{
|
int x = 0;
|
uint32_t now = AppTimer_GetTickCount();
|
|
if (now < sync_time) {
|
sync_time = now;
|
}
|
|
if (now - sync_time >= D_SEC(5)) {
|
parse_status = SYNC_HEAD_ONE;
|
}
|
|
while (x < length) {
|
uint8_t c = data[x];
|
switch (parse_status) {
|
case SYNC_HEAD_ONE:
|
if (c == 0x55) {
|
parse_status = SYNC_HEAD_TWO;
|
sync_time = now;
|
}
|
x++;
|
break;
|
case SYNC_HEAD_TWO:
|
if (c == 0xAA) {
|
parse_status = GET_ID_HI;
|
} else if (c != 0x55) {
|
parse_status = SYNC_HEAD_ONE;
|
}
|
x++;
|
break;
|
case GET_ID_HI:
|
McuPkt.id = c;
|
parse_status = GET_ID_LO;
|
x++;
|
break;
|
case GET_ID_LO:
|
McuPkt.id <<= 8;
|
McuPkt.id += c;
|
parse_status = GET_LENGTH_HI;
|
x++;
|
break;
|
case GET_LENGTH_HI:
|
McuPkt.rx_len = 0;
|
McuPkt.length = c;
|
parse_status = GET_LENGTH_LO;
|
x++;
|
break;
|
case GET_LENGTH_LO:
|
McuPkt.length <<= 8;
|
McuPkt.length += c;
|
parse_status = GET_PAYLOAD;
|
|
if (McuPkt.length >= 1500) {
|
DEBUG("Pkt Too large!");
|
parse_status = SYNC_HEAD_ONE;
|
}
|
|
McuPkt.buffer[0] = HI_UINT16(McuPkt.id);
|
McuPkt.buffer[1] = LO_UINT16(McuPkt.id);
|
McuPkt.buffer[2] = HI_UINT16(McuPkt.length);
|
McuPkt.buffer[3] = LO_UINT16(McuPkt.length);
|
x++;
|
break;
|
case GET_PAYLOAD:
|
if (length - x >= McuPkt.length - McuPkt.rx_len) {
|
memcpy(McuPkt.buffer + 4 + McuPkt.rx_len, data + x, McuPkt.length - McuPkt.rx_len);
|
x += McuPkt.length - McuPkt.rx_len;
|
McuPkt.rx_len = McuPkt.length;
|
parse_status = GET_CRC16_HI;
|
} else {
|
memcpy(McuPkt.buffer + 4 + McuPkt.rx_len, data + x, length - x);
|
McuPkt.rx_len += length - x;
|
x = length;
|
}
|
break;
|
case GET_CRC16_HI:
|
McuPkt.crc16 = c;
|
parse_status = GET_CRC16_LO;
|
x++;
|
break;
|
case GET_CRC16_LO: {
|
McuPkt.crc16 <<= 8;
|
McuPkt.crc16 += c;
|
|
uint16_t crc16 = CRCCCITT(McuPkt.buffer, McuPkt.length + 4, 0, 0);
|
|
// DEBUG("mcuif 0x%02X: crc16 but 0x%04X exp 0x%04X", McuPkt.id, McuPkt.crc16, crc16);
|
|
if (McuPkt.crc16 == crc16 && fun != nullptr) {
|
fun(McuPkt.id, McuPkt.buffer + 4, McuPkt.length);
|
//McuCommandEntry(McuPkt.id, McuPkt.buffer + 4, McuPkt.length);
|
}
|
|
parse_status = SYNC_HEAD_ONE;
|
x++;
|
break;
|
}
|
default:
|
break;
|
}
|
}
|
}
|
|
void SendMcuCommand(SerialPort *pClass, pSerialPortClassFun fun, uint16_t id, const uint8_t *data, int length)
|
{
|
if (pClass == nullptr)
|
return;
|
|
uint8_t buffer[2048];
|
int x = 0;
|
|
buffer[x++] = 0x55;
|
buffer[x++] = 0xAA;
|
buffer[x++] = HI_UINT16(id);
|
buffer[x++] = LO_UINT16(id);
|
buffer[x++] = HI_UINT16(length);
|
buffer[x++] = LO_UINT16(length);
|
|
if (data != NULL && length > 0) {
|
memcpy(buffer + x, data, length);
|
x += length;
|
}
|
|
uint16_t crc16 = CRCCCITT(buffer + 2, length + 4, 0, 0);
|
buffer[x++] = HI_UINT16(crc16);
|
buffer[x++] = LO_UINT16(crc16);
|
|
(pClass->*fun)(buffer, x);
|
}
|
|
void SendRtkToMcu(const uint8_t *data, int length)
|
{
|
//SendMcuCommand(ID_CM_RTK_DATA, data, length);
|
}
|
|
static void sendrtk(apptimer_var_t val) {
|
uint8_t data[486];
|
|
memset(data, 0x86, sizeof(data));
|
|
//SendMcuCommand(ID_CM_RTK_DATA, data, sizeof(data));
|
|
//SendMcuCommand(ID_CM_READ_RFCARD, NULL, 0);
|
|
AppTimer_add(sendrtk, D_SEC(1));
|
}
|
|
static int readCartCnt = 0;
|
|
static void ReadCardTimeout(apptimer_var_t val) {
|
readCartCnt++;
|
|
if (readCartCnt < 2) {
|
AppTimer_add(ReadCardTimeout, D_SEC(3));
|
//SendMcuCommand(ID_CM_READ_RFCARD, NULL, 0);
|
} else {
|
uint8_t data[8] = {0};
|
PlatformStatusChanged(CARD_UPDATE_EVT, data, sizeof(data));
|
}
|
}
|
|
void ReadCard(void)
|
{
|
readCartCnt = 0;
|
|
AppTimer_delete(ReadCardTimeout);
|
AppTimer_add(ReadCardTimeout, D_SEC(3));
|
//SendMcuCommand(ID_CM_READ_RFCARD, NULL, 0);
|
}
|