yy1717
2024-02-28 27fc91fbe8f88b6885356e68828cfe1ce1db7601
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//
// Created by YY on 2024/2/27.
//
 
#include "car_box.h"
#include "../jni_log.h"
#include "../common/string_util.h"
#include "../common/serial_port.h"
#include "../rtk_module/parse_gps.h"
#include <thread>
 
#define DEBUG(fmt, args...)     LOGD("<car_box> <%s>: " fmt, __func__, ##args)
 
#define PARSE_BUFF_SIZE         4096
 
static SerialPort *pCls = nullptr;
 
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;
 
    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) {
            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 CarBox error");
                RxBufLen = 0;
            }
        }
    }
 
    delete pCls;
    pCls = nullptr;
}
 
void InitCarBox(void)
{
    // TODO
    static struct serial_config serialConfig;
 
    strcpy(serialConfig.name, "/dev/ttyHSL0");
    serialConfig.baud = 115200;
    serialConfig.data_bit = 8;
    serialConfig.verify_bit = 'N';
    serialConfig.stop_bit = 1;
    serialConfig.flow_ctrl = 0;
 
    std::thread([&] {
        while (true) {
            std::thread t(UartThread, &serialConfig);
            t.join();
            std::this_thread::sleep_for(std::chrono::seconds(3));
        }
    }).detach();
}
 
void SendRtcmToUart(const uint8_t *dat, int length) {
    if (pCls != nullptr) {
        pCls->WriteSerialPort(dat, length);
    }
}
 
void handleCarinfo(const struct nmea *s) {
//    DEBUG("handleCarinfo");
}
 
void handleKsxt(const struct nmea *s) {
//    DEBUG("handleKsxt");
    char buff[256] = {0};
 
    memcpy(buff, s->nmea_value[9].data, s->nmea_value[9].length);
 
    DEBUG("定位  %s", buff);
}