yy1717
2023-03-31 4bd08f0355b6b2cf3c027202d5ad301b4e182953
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//
// Created by Administrator on 2022/11/15.
//
 
#include <thread>
#include "ada.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"
 
#define DEBUG(fmt, args...)     LOGD("<ada> <%s>: " fmt, __func__, ##args)
 
static Dfu *dfu = nullptr;
static SerialPort *pCls = nullptr;
 
static void InquiryAdaInfo(void)
{
    SendMcuCommand(pCls, &SerialPort::WriteSerialPort, ID_ADA_INFO_REQ, nullptr, 0);
}
 
static void AdaCommandEntry(uint16_t id, const uint8_t *data, int length)
{
    switch (id) {
        case ID_ADA_INFO_RESP: {
            DEBUG("ID_ADA_INFO_RESP");
            ada_info_t info;
 
            info.version = StringUtil::BuffertoString(data, 8);
            info.sn = StringUtil::BcdBuffertoString(data + 8, 8);
            break;
        }
        case ID_ADA_DFU_RESP: {
            if (dfu != nullptr) {
                dfu->RemoteAck(data, length);
            }
            break;
        }
        case ID_ADA_OBD: {
//            DEBUG("ID_ADA_OBD %d %d", length, sizeof(car_sensor_t));
            if (length == sizeof(car_sensor_t)) {
                car_sensor_t *ptr = (car_sensor_t *) data;
            }
            break;
        }
        default:break;
    }
}
 
#define PARSE_BUFF_SIZE         4096
 
static void UartThread1(void *p) {
    struct serial_config *cfg = (struct serial_config *) p;
    SerialPort serialPort(*cfg);
 
    int res = serialPort.InitSerialPort();
    DEBUG("Serial %s open %d", cfg->name, res);
    uint8_t RxBuf[PARSE_BUFF_SIZE];
    int RxBufLen = 0;
 
    ParseUart parse(AdaCommandEntry);
 
    while (res == 0) {
        int ul = serialPort.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) {
            parse.ParseMcu(RxBuf, RxBufLen);
            RxBufLen = 0;
        }
    }
}
 
static void AdaRun(void)
{
    static struct serial_config serialConfig;
 
    strcpy(serialConfig.name, "/dev/ttyCH341USB4");
    serialConfig.baud = 115200;
    serialConfig.data_bit = 8;
    serialConfig.verify_bit = 'N';
    serialConfig.stop_bit = 1;
    serialConfig.flow_ctrl = 0;
 
    while (true) {
        std::thread t(UartThread1, &serialConfig);
        t.join();
        std::this_thread::sleep_for(std::chrono::seconds(3));
    }
}
 
void InitAda(void)
{
    // TODO
    static struct serial_config serialConfig;
    strcpy(serialConfig.name, "/dev/ttyCH341USB4");
    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(UartThread1, &serialConfig);
            t.join();
            std::this_thread::sleep_for(std::chrono::seconds(3));
        }
    }).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_ADA_DFU_UPLOAD, buffer, x);
}
 
void EnterAdaDfu(const uint8_t *file, int length)
{
    if (dfu != nullptr) {
        delete dfu;
    }
    dfu = new Dfu(SendDfuFile, file, length);
    dfu->Run();
    delete dfu;
    dfu = nullptr;
}