//
|
// Created by YY on 2022/11/14.
|
//
|
|
#include "ahp.h"
|
#include "../common/string_util.h"
|
#include "../common/serial_port.h"
|
#include "mcu_if.h"
|
#include "dfu.h"
|
#include "../jni_log.h"
|
#include "../defs.h"
|
#include "../rtk_module/parse_gps.h"
|
|
#define DEBUG(fmt, args...) LOGD("<ahp> <%s>: " fmt, __func__, ##args)
|
|
static Dfu *dfu = nullptr;
|
static uint8_t RxBuf[4096];
|
static int RxBufLen = 0;
|
|
static SerialPort *pCls = nullptr;
|
|
static void AhpCommandEntry(uint16_t id, const uint8_t *data, int length) {
|
switch (id) {
|
case ID_AHP_INFO_RESP: {
|
aph_info_t info;
|
|
info.version = StringUtil::BuffertoString(data, 8);
|
info.sn = StringUtil::BcdBuffertoString(data + 8, 8);
|
break;
|
}
|
case ID_AHP_DFU_RESP: {
|
if (dfu != nullptr) {
|
dfu->RemoteAck(data, length);
|
}
|
break;
|
}
|
|
case ID_GNSS_DATA: {
|
// DEBUG("ID_GNSS_DATA %d", length);
|
memcpy(RxBuf + RxBufLen, data, length);
|
RxBufLen += length;
|
|
// std::string out = StringUtil::BuffertoString(RxBuf, RxBufLen);
|
// DEBUG("%s", out.c_str());
|
|
const uint8_t *ptr = parseGPS(RxBuf, RxBuf + RxBufLen);
|
if (ptr != RxBuf) {
|
memcpy(RxBuf, ptr, RxBufLen - (ptr - RxBuf));
|
RxBufLen -= ptr - RxBuf;
|
} else if (RxBufLen == sizeof(RxBuf)) { //填满了,且没有一个\r,都抛弃
|
DEBUG("Parse GPS error");
|
RxBufLen = 0;
|
}
|
|
break;
|
}
|
default:break;
|
}
|
}
|
|
#define PARSE_BUFF_SIZE 4096
|
|
static void UartThread(void *p) {
|
struct serial_config *cfg = (struct serial_config *) p;
|
|
pCls = new SerialPort(*cfg);
|
|
int res = pCls->InitSerialPort();
|
DEBUG("Serial %s open %d", cfg->name, res);
|
|
uint8_t RxBuf[PARSE_BUFF_SIZE];
|
int RxBufLen = 0;
|
|
ParseUart parse(AhpCommandEntry);
|
|
while (res == 0) {
|
int ul = pCls->ReadSerialPort((uint8_t *)RxBuf + RxBufLen, sizeof(RxBuf) - RxBufLen);
|
if (ul < 0) {
|
continue;
|
} else if (ul == 0) {
|
// usb串口断开
|
break;
|
}
|
RxBufLen += ul;
|
|
/*{
|
static char buffd[16384];
|
|
buffd[0] = 0;
|
int i = 0;
|
for (i = 0; i < ul; i++) {
|
if ((i % 32) == 0) {
|
sprintf(buffd + strlen(buffd), "\n");
|
}
|
sprintf(buffd + strlen(buffd), "%02X ", RxBuf[i]);
|
if (strlen(buffd) > 800) {
|
DEBUG("%s <- %s...", "UART", buffd);
|
buffd[0] = 0;
|
}
|
}
|
if (strlen(buffd) > 0)
|
DEBUG("%s <- %s", "UART", buffd);
|
}*/
|
|
if (RxBufLen > 0) {
|
// DEBUG("RECV LEN %d", RxBufLen);
|
parse.ParseMcu(RxBuf, RxBufLen);
|
RxBufLen = 0;
|
}
|
}
|
|
delete pCls;
|
pCls = nullptr;
|
}
|
|
static void AhpRun(void)
|
{
|
static struct serial_config serialConfig;
|
|
strcpy(serialConfig.name, "/dev/ttyCH341USB3");
|
serialConfig.baud = 115200;
|
serialConfig.data_bit = 8;
|
serialConfig.verify_bit = 'N';
|
serialConfig.stop_bit = 1;
|
serialConfig.flow_ctrl = 0;
|
|
while (true) {
|
std::thread t(UartThread, &serialConfig);
|
t.join();
|
std::this_thread::sleep_for(std::chrono::seconds(3));
|
}
|
}
|
|
void InitAhp(void)
|
{
|
// TODO
|
std::thread(AhpRun).detach();
|
}
|
|
static void SendDfuFile(int fileLen, int sentLen, int blockLen, const uint8_t *data) {
|
uint8_t buffer[1024];
|
int x = 0;
|
|
DEBUG("SendDfuFile fileLen %d sentLen %d blockLen %d", fileLen, sentLen, blockLen);
|
|
buffer[x++] = BREAK_UINT32(fileLen, 3);
|
buffer[x++] = BREAK_UINT32(fileLen, 2);
|
buffer[x++] = BREAK_UINT32(fileLen, 1);
|
buffer[x++] = BREAK_UINT32(fileLen, 0);
|
|
buffer[x++] = BREAK_UINT32(sentLen, 3);
|
buffer[x++] = BREAK_UINT32(sentLen, 2);
|
buffer[x++] = BREAK_UINT32(sentLen, 1);
|
buffer[x++] = BREAK_UINT32(sentLen, 0);
|
|
buffer[x++] = HI_UINT16(blockLen);
|
buffer[x++] = LO_UINT16(blockLen);
|
|
memcpy(buffer + x, data, blockLen);
|
x += blockLen;
|
|
SendMcuCommand(pCls, &SerialPort::WriteSerialPort, ID_AHP_DFU_UPLOAD, buffer, x);
|
}
|
|
void EnterAhpDfu(const uint8_t *file, int length)
|
{
|
if (dfu != nullptr) {
|
delete dfu;
|
}
|
dfu = new Dfu(SendDfuFile, file, length);
|
dfu->Run();
|
delete dfu;
|
dfu = nullptr;
|
}
|
|
void WriteRtkCommand(const char *data, int length)
|
{
|
DEBUG("发送RTK命令 %s", StringUtil::BuffertoString((uint8_t *)data, length).c_str());
|
SendMcuCommand(pCls, &SerialPort::WriteSerialPort, ID_CTRL_CMD, reinterpret_cast<const uint8_t *>(data), length);
|
}
|