yy1717
2022-12-08 f7a18ec4494b9c5c9ef3fd440bbf68ffc6425e18
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
//
// Created by Administrator on 2022/11/28.
//
 
#include <cstring>
#include "dfu.h"
#include "../defs.h"
#include "../jni_log.h"
 
#define DEBUG(fmt, args...)     LOGD("<dfu> <%s>: " fmt, __func__, ##args)
 
Dfu::Dfu(SendDfuPtr fun, const uint8_t *data, int length) {
    dfuTryCnt = 0;
    dfuFileBitmap = new uint8_t [128];
    memset(dfuFileBitmap, 0, sizeof(dfuFileBitmap));
 
    if (length > 0) {
        dfuFile = new uint8_t[length];
        dfuFileLength = length;
        memcpy(dfuFile, data, length);
    }
    SendDfuFile = fun;
}
 
Dfu::~Dfu() {
    delete []dfuFileBitmap;
    if (dfuFile != nullptr) {
        delete []dfuFile;
    }
}
 
int Dfu::GoNextDfu(void)
{
    int dfuFileSent = 0, currDfuBlockLength = 0;
 
    if (dfuFileLength == 0 || dfuFile == NULL || SendDfuFile == nullptr || dfuCancel)
        return DFU_END;
 
    int row = 0, col = 0;
 
    dfuFileSent = dfuFileLength;
 
    for (row = 0; row < sizeof(dfuFileBitmap); ++row) {
        for (col = 0; col < 8; ++col) {
            if ((dfuFileBitmap[row] & BV(col)) == 0) {
                DEBUG("ROW = %d COL = %d", row, col);
                dfuFileSent = row * DFU_FILE_BLOCK_SIZE * 8 + col * DFU_FILE_BLOCK_SIZE;
                goto GET_FILE_START;
            }
        }
    }
 
    GET_FILE_START:
    currDfuBlockLength = (dfuFileLength - dfuFileSent > DFU_FILE_BLOCK_SIZE) ? DFU_FILE_BLOCK_SIZE : (dfuFileLength - dfuFileSent);
 
    if (dfuFileSent >= dfuFileLength || currDfuBlockLength == 0) {
        return DFU_END;
    }
 
    SendDfuFile(dfuFileLength, dfuFileSent, currDfuBlockLength, dfuFile + dfuFileSent);
 
    if (dfuFileSent > 0 && dfuFileSent + currDfuBlockLength < dfuFileLength) {
        dfuFileBitmap[row] |= BV(col);
    }
 
    if (dfuFileSent == 0 || dfuFileSent + currDfuBlockLength == dfuFileLength) {
        dfuTryCnt++;
        if (dfuTryCnt > DFU_MAX_TRY) {
            return DFU_END;
        }
        DEBUG("GoNextDfuLater 3 sec...");
        return DFU_LONG_WAIT;
    }
    return DFU_SHORT_WAIT;
}
 
void Dfu::RemoteAck(const uint8_t *data, int length)
{
    DEBUG("ID_MC_AHP_DFU_RESP %d len %d", data[0], length);
 
    if (data[0] == 0) {
        // 第一包传输成功
        dfuFileBitmap[0] |= 0x01;
    } else if (data[0] == 10) {
        // 升级完成
        memset(dfuFileBitmap, 0xFF, sizeof(dfuFileBitmap));
    } else if (data[0] == 11) {
        // 放弃传输
        dfuCancel = true;
    } else if (data[0] == 12) {
        // 全部重传
        memset(dfuFileBitmap, 0, sizeof(dfuFileBitmap));
    } else if (data[0] == 13) {
        // 部分重传,有后续字段
        DEBUG("BITMAP %02X %02X %02X %02X %02X", data[1], data[2], data[3], data[4], data[5]);
 
        int total = dfuFileLength / DFU_FILE_BLOCK_SIZE + ((dfuFileLength % DFU_FILE_BLOCK_SIZE)?1:0);
        int a = 0, b = 0;
        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 = length;
                    break;
                }
            }
        }
 
        DEBUG("BITMAP total %d succ %d", total, b);
 
        //memset(dfuFileBitmap, 0, sizeof(dfuFileBitmap));
        memcpy(dfuFileBitmap, data + 1, length - 1);
 
        if (total % 8) {
            dfuFileBitmap[total/8] &= ~BV((total%8) - 1);
        } else {
            dfuFileBitmap[(total+7)/8 - 1] &= ~BV(7);
        }
//                dfuFileBitmap[total/8] &= ~BV(total%8);
    }
 
    std::unique_lock<std::mutex> lk(cv_mtx);
    expired = true;
    cv.notify_one();
}
 
void Dfu::Run(void)
{
    while(true) {
        std::unique_lock<std::mutex> lk(cv_mtx);
        expired = false;
        int status = GoNextDfu();
 
        if (status == DFU_END) {
            break;
        }
 
        if (cv.wait_for(lk, std::chrono::milliseconds(status == DFU_LONG_WAIT? 3000: 10), [this] { return expired == true; }) ==
            false) {
            // timeout
        } else {
            // cancel
            dfuTryCnt = 0;
        }
    }
}