yy1717
2020-11-24 6e0f29b08a040d14576d7053c1206a8439936570
lib/src/main/cpp/mcu/mcu_if.cpp
@@ -12,6 +12,8 @@
#include "../jni_log.h"
#include "../common/serial_port.h"
#include "../rtk_platform/platform.h"
#include "../rtk_module/parse_gps.h"
#include "../native-lib.h"
#define DEBUG(fmt, args...)     LOGD("<mcu_if> <%s>: " fmt, __func__, ##args)
@@ -46,6 +48,8 @@
#define ID_CM_READ_RFCARD         0x0009
#define ID_MC_RFCARD_RSP        0x8009
#define ID_CM_SHUTDOWN          0x0020
#define ID_MC_GNSS_DATA         0x800A
#define ID_CM_SINAN_CMD_DATA    0x000B
static parse_status_t parse_status;
@@ -65,15 +69,27 @@
const int DFU_MAX_TRY = 3;
const int DFU_FILE_BLOCK_SIZE = 896;
static int (*WriteMcu)(int id, const void *buf, int len);
static int WriteBluetooth(int id, const void *buf, int len);
static void *UartThread1(void *p);
static void ParseMcuTimeout(union sigval sig);
static void McuCommandEntry(uint16_t id, const uint8_t *data, int lenth);
static void McuCommandEntry(uint16_t id, const uint8_t *data, int length);
static void SendDfuFile(int fileLen, int sentLen, int blockLen, const uint8_t *data);
static void GoNextDfuLater(union sigval sig);
static void GoNextDfu(void);
static void ReadCardTimeout(union sigval sig);
void McuCommModeSel(int mode)
{
    if (mode == 0) {
        WriteMcu = WriteSerialPort;
    } else {
        WriteMcu = WriteBluetooth;
    }
}
void ParseMcuInit(void)
{
@@ -83,8 +99,15 @@
    parse_status = SYNC_HEAD_ONE;
    AppTimer_delete(ParseMcuTimeout);
    SendMcuCommand(ID_CM_APP_BOOT, NULL, 0);
//    SendMcuCommand(ID_CM_APP_BOOT, NULL, 0);
}
static int WriteBluetooth(int id, const void *buf, int len)
{
    SendToBluetooth((uint8_t *)buf, len);
    return len;
}
#define PARSE_BUFF_SIZE         4096
@@ -251,13 +274,15 @@
    buffer[x++] = HI_UINT16(crc16);
    buffer[x++] = LO_UINT16(crc16);
    WriteSerialPort(MCU_UART, buffer, x);
    WriteMcu(MCU_UART, buffer, x);
}
void ConfigMCU(void)
{
    McuCommModeSel(1);
    // TODO
    static struct serial_config serialConfig;
  /*  static struct serial_config serialConfig;
    strcpy(serialConfig.name, "/dev/ttyHSL1");
    serialConfig.baud = 115200;
@@ -270,7 +295,7 @@
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);//detached
    pthread_create(&pid, &attr, UartThread1, &serialConfig);
    pthread_create(&pid, &attr, UartThread1, &serialConfig);*/
}
void SendRtkToMcu(const uint8_t *data, int length)
@@ -296,20 +321,23 @@
    AppTimer_add(sendrtk, D_SEC(1));
}
static void McuCommandEntry(uint16_t id, const uint8_t *data, int lenth)
static uint8_t GnssBuf[PARSE_BUFF_SIZE];
static int GnssBufLen = 0;
static void McuCommandEntry(uint16_t id, const uint8_t *data, int length)
{
    static int ii = 0;
    switch (id) {
        case ID_MC_MCU_BOOT:
            DEBUG("MCU BOOT");
            if (lenth == 8) {
                PlatformStatusChanged(CAN_UPDATE_EVT, data, lenth);
            if (length == 8) {
                PlatformStatusChanged(CAN_UPDATE_EVT, data, length);
            }
            break;
        case ID_MC_DFU_RSP:
            DEBUG("ID_MC_DFU_RSP %d len %d", data[0], lenth);
            DEBUG("ID_MC_DFU_RSP %d len %d", data[0], length);
            if (data[0] == 0) {
                // 第一包传输成功
@@ -329,12 +357,12 @@
                int total = dfuFileLength / DFU_FILE_BLOCK_SIZE + ((dfuFileLength % DFU_FILE_BLOCK_SIZE)?1:0);
                int a = 0, b = 0;
                for (int i = 1; i < lenth; ++i) {
                for (int i = 1; i < length; ++i) {
                    for (int j = 0; j < 8; ++j) {
                        if ((data[i] & BV(j))) b++;
                        a++;
                        if (a == total) {
                            i = lenth;
                            i = length;
                            break;
                        }
                    }
@@ -343,7 +371,7 @@
                DEBUG("BITMAP total %d succ %d", total, b);
                //memset(dfuFileBitmap, 0, sizeof(dfuFileBitmap));
                memcpy(dfuFileBitmap, data + 1, lenth - 1);
                memcpy(dfuFileBitmap, data + 1, length - 1);
                if (total % 8) {
                    dfuFileBitmap[total/8] &= ~BV((total%8) - 1);
@@ -362,16 +390,16 @@
        case ID_MC_MCU_DFU_RSP:
            break;
        case ID_MC_CAR_INFO2:
//            DEBUG("ID_MC_CAR_INFO2 %d", lenth);
            if (lenth > 0) {
                PlatformStatusChanged(CAR_SENSOR_UPDATE_EVT, data, lenth);
//            DEBUG("ID_MC_CAR_INFO2 %d", length);
            if (length > 0) {
                PlatformStatusChanged(CAR_SENSOR_UPDATE_EVT, data, length);
            }
            break;
        case ID_MC_CAR_INFO: {
//            DEBUG("ID_MC_CAR_INFO %d", lenth);
//            DEBUG("ID_MC_CAR_INFO %d", length);
            if (lenth > 0)
                PlatformStatusChanged(MCU_UPDATE_EVT, data, lenth);
            if (length > 0)
                PlatformStatusChanged(MCU_UPDATE_EVT, data, length);
            break;
        }
        case ID_MC_RTK_DATA:
@@ -381,9 +409,26 @@
            DEBUG("ID_MC_RFCARD_RSP");
            AppTimer_delete(ReadCardTimeout);
            if (lenth > 0)
                PlatformStatusChanged(CARD_UPDATE_EVT, data, lenth);
            if (length > 0)
                PlatformStatusChanged(CARD_UPDATE_EVT, data, length);
            break;
        case ID_MC_GNSS_DATA: {
            length = (length > PARSE_BUFF_SIZE - GnssBufLen) ? (PARSE_BUFF_SIZE - GnssBufLen) : length;
            memcpy(GnssBuf + GnssBufLen, data, length);
            GnssBufLen += length;
            if (GnssBufLen > 0) {
                const uint8_t *ptr = parseGPS(GnssBuf, GnssBuf + GnssBufLen);
                if (ptr != GnssBuf) {
                    memcpy(GnssBuf, ptr, GnssBufLen - (ptr - GnssBuf));
                    GnssBufLen -= ptr - GnssBuf;
                } else if (GnssBufLen == PARSE_BUFF_SIZE) {        //填满了,且没有一个\r,都抛弃
                    DEBUG("Parse GPS error");
                    GnssBufLen = 0;
                }
            }
            break;
        }
        default:
            break;
    }