test_upgrade.c
14.1 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/**
********************************************************************
* @file test_upgrade.c
* @brief
*
* @copyright (c) 2021 DJI. All rights reserved.
*
* All information contained herein is, and remains, the property of DJI.
* The intellectual and technical concepts contained herein are proprietary
* to DJI and may be covered by U.S. and foreign patents, patents in process,
* and protected by trade secret or copyright law. Dissemination of this
* information, including but not limited to data and other proprietary
* material(s) incorporated within the information, in any form, is strictly
* prohibited without the express written consent of DJI.
*
* If you receive this source code without DJI’s authorization, you may not
* further disseminate the information, and you must immediately remove the
* source code and notify DJI of its removal. DJI reserves the right to pursue
* legal actions against you for any loss(es) or damage(s) caused by your
* failure to do so.
*
*********************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include <dji_logger.h>
#include <dji_platform.h>
#include <utils/util_misc.h>
#include "test_upgrade_common_file_transfer.h"
#include "test_upgrade_platform_opt.h"
#include "test_upgrade.h"
/* Private constants ---------------------------------------------------------*/
#define UPGRADE_TASK_STACK_SIZE (2048)
#define DJI_TEST_UPGRADE_TASK_FREQ (50)
#define DJI_TEST_ENTER_UPGRADE_WAIT_TIME (10) //wait 10s for enter upgrade process
#define DJI_TEST_UPGRADE_REBOOT_TIMEOUT (30) //reboot timeout 30s
/* Private types -------------------------------------------------------------*/
/* Private values -------------------------------------------------------------*/
static T_DjiUpgradeState s_upgradeState = {0};
static T_DjiMutexHandle s_upgradeStateMutex = {0};
static T_DjiTaskHandle s_upgradeProcessThread;
static T_DjiTaskHandle s_enterUpgradeModeProcessThread;
static bool s_isNeedEnterUpgradeModeProcess = false;
static bool s_isNeedReplaceProgramBeforeReboot = false;
/* Private functions declaration ---------------------------------------------*/
static T_DjiReturnCode DjiTest_EnterUpgradeMode(uint16_t *waitTime);
static T_DjiReturnCode DjiTest_CheckFirmware(void);
static T_DjiReturnCode DjiTest_StartUpgrade(void);
static T_DjiReturnCode DjiTest_FinishUpgrade(void);
static void *DjiTest_UpgradeProcessTask(void *arg);
static void *DjiTest_EnterUpgradeModeProcessTask(void *arg);
/* Exported functions definition ---------------------------------------------*/
T_DjiReturnCode
DjiTest_UpgradeStartService(const T_DjiTestUpgradePlatformOpt *upgradePlatformOpt,
T_DjiTestUpgradeConfig testUpgradeConfig)
{
T_DjiOsalHandler *osalHandler = DjiPlatform_GetOsalHandler();
T_DjiReturnCode returnCode;
bool isUpgradeReboot = false;
T_DjiUpgradeEndInfo upgradeEndInfo = {0};
T_DjiUpgradeConfig upgradeConfig = {
.currentFirmwareVersion = testUpgradeConfig.firmwareVersion,
.firmwareTransferInfo = {
.transferType = testUpgradeConfig.transferType,
.ftpTransferInfo.port = 21,
.dcftpFileTransferOpt = {
.start = DjiTestCommonFileTransfer_Start,
.transfer = DjiTestCommonFileTransfer_Transfer,
.finish = DjiTestCommonFileTransfer_Finish,
}
}
};
s_isNeedReplaceProgramBeforeReboot = testUpgradeConfig.needReplaceProgramBeforeReboot;
T_DjiUpgradeHandler s_upgradeHandler = {
.EnterUpgradeMode = DjiTest_EnterUpgradeMode,
.CheckFirmware = DjiTest_CheckFirmware,
.StartUpgrade = DjiTest_StartUpgrade,
.FinishUpgrade = DjiTest_FinishUpgrade
};
returnCode = DjiTest_RegUpgradePlatformOpt(upgradePlatformOpt);
if (returnCode != DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {
USER_LOG_ERROR("Reg upgrade platform opt error, return code = 0x%08llX", returnCode);
return returnCode;
}
returnCode = osalHandler->MutexCreate(&s_upgradeStateMutex);
if (returnCode != DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {
USER_LOG_ERROR("Create mutex error");
return returnCode;
}
returnCode = DjiTest_GetUpgradeRebootState(&isUpgradeReboot, &upgradeEndInfo);
if (returnCode != DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {
USER_LOG_ERROR("Get upgrade reboot state error");
isUpgradeReboot = false;
}
returnCode = DjiTest_CleanUpgradeRebootState();
if (returnCode != DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {
USER_LOG_ERROR("Clean upgrade reboot state error");
}
osalHandler->MutexLock(s_upgradeStateMutex);
if (isUpgradeReboot == true) {
s_upgradeState.upgradeStage = DJI_UPGRADE_STAGE_END;
s_upgradeState.upgradeEndInfo = upgradeEndInfo;
} else {
s_upgradeState.upgradeStage = DJI_UPGRADE_STAGE_IDLE;
}
osalHandler->MutexUnlock(s_upgradeStateMutex);
returnCode = DjiUpgrade_Init(&upgradeConfig);
if (returnCode != DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {
USER_LOG_ERROR("DjiUpgrade_Init error, return code = %d", returnCode);
return returnCode;
}
returnCode = DjiUpgrade_RegHandler(&s_upgradeHandler);
if (returnCode != DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {
USER_LOG_ERROR("DjiUpgrade_RegHandler error, return code = %d", returnCode);
return returnCode;
}
returnCode = DjiUpgrade_EnableLocalUpgrade();
if (returnCode != DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {
USER_LOG_ERROR("DjiUpgrade_EnableLocalUpgrade error, return code = %d", returnCode);
return returnCode;
}
if (osalHandler->TaskCreate("upgrade_task", DjiTest_UpgradeProcessTask, UPGRADE_TASK_STACK_SIZE, NULL,
&s_upgradeProcessThread) != DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {
USER_LOG_ERROR("Dji upgrade test task create error.");
return DJI_ERROR_SYSTEM_MODULE_CODE_UNKNOWN;
}
if (osalHandler->TaskCreate("enter_upgrade_mode_task", DjiTest_EnterUpgradeModeProcessTask, UPGRADE_TASK_STACK_SIZE,
NULL, &s_enterUpgradeModeProcessThread) !=
DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {
USER_LOG_ERROR("Dji upgrade test task create error.");
return DJI_ERROR_SYSTEM_MODULE_CODE_UNKNOWN;
}
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
/* Private functions definition-----------------------------------------------*/
static T_DjiReturnCode DjiTest_EnterUpgradeMode(uint16_t *waitTime)
{
// need 10s for upgrade preprocess work.
*waitTime = DJI_TEST_ENTER_UPGRADE_WAIT_TIME;
// enable is need enter upgrade mode process, the process is in DjiTest_EnterUpgradeModeProcessTask
s_isNeedEnterUpgradeModeProcess = true;
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
static T_DjiReturnCode DjiTest_CheckFirmware(void)
{
// you can do decrypt and check firmware in this stage
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
static T_DjiReturnCode DjiTest_StartUpgrade(void)
{
T_DjiOsalHandler *osalHandler = DjiPlatform_GetOsalHandler();
osalHandler->MutexLock(s_upgradeStateMutex);
s_upgradeState.upgradeOngoingInfo.upgradeProgress = 0;
s_upgradeState.upgradeStage = DJI_UPGRADE_STAGE_ONGOING;
osalHandler->MutexUnlock(s_upgradeStateMutex);
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
static T_DjiReturnCode DjiTest_FinishUpgrade(void)
{
T_DjiOsalHandler *osalHandler = DjiPlatform_GetOsalHandler();
osalHandler->MutexLock(s_upgradeStateMutex);
s_upgradeState.upgradeStage = DJI_UPGRADE_STAGE_IDLE;
osalHandler->MutexUnlock(s_upgradeStateMutex);
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
#ifndef __CC_ARM
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmissing-noreturn"
#pragma GCC diagnostic ignored "-Wreturn-type"
#endif
static void *DjiTest_EnterUpgradeModeProcessTask(void *arg)
{
T_DjiOsalHandler *osalHandler = DjiPlatform_GetOsalHandler();
T_DjiReturnCode returnCode;
USER_UTIL_UNUSED(arg);
while (1) {
if (s_isNeedEnterUpgradeModeProcess) {
// prepare enter upgrade mode
// you can do some thing before enter upgrade mode.
// clear upgrade program file store area
returnCode = DjiTest_CleanUpgradeProgramFileStoreArea();
if (returnCode != DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {
USER_LOG_ERROR("Clean upgrade file dir error, please check dir permission");
}
s_isNeedEnterUpgradeModeProcess = false;
USER_LOG_INFO("Clean upgrade store area");
}
osalHandler->TaskSleepMs(1000 / DJI_TEST_UPGRADE_TASK_FREQ);
}
}
#ifndef __CC_ARM
#pragma GCC diagnostic pop
#endif
#ifndef __CC_ARM
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmissing-noreturn"
#pragma GCC diagnostic ignored "-Wreturn-type"
#endif
static void *DjiTest_UpgradeProcessTask(void *arg)
{
T_DjiOsalHandler *osalHandler = DjiPlatform_GetOsalHandler();
T_DjiUpgradeState tempUpgradeState;
T_DjiUpgradeEndInfo upgradeEndInfo;
T_DjiReturnCode returnCode;
USER_UTIL_UNUSED(arg);
while (1) {
osalHandler->MutexLock(s_upgradeStateMutex);
tempUpgradeState = s_upgradeState;
osalHandler->MutexUnlock(s_upgradeStateMutex);
if (tempUpgradeState.upgradeStage == DJI_UPGRADE_STAGE_ONGOING) {
if (s_isNeedReplaceProgramBeforeReboot) {
// Step 1 : Replace old program
returnCode = DjiTest_ReplaceOldProgram();
if (returnCode != DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {
USER_LOG_ERROR("Replace firmware error, return code = 0x%08llX", returnCode);
osalHandler->MutexLock(s_upgradeStateMutex);
s_upgradeState.upgradeStage = DJI_UPGRADE_STAGE_END;
s_upgradeState.upgradeEndInfo.upgradeEndState = DJI_UPGRADE_END_STATE_UNKNOWN_ERROR;
osalHandler->MutexUnlock(s_upgradeStateMutex);
continue;
}
osalHandler->TaskSleepMs(1000);
osalHandler->MutexLock(s_upgradeStateMutex);
s_upgradeState.upgradeStage = DJI_UPGRADE_STAGE_ONGOING;
s_upgradeState.upgradeOngoingInfo.upgradeProgress = 20;
DjiUpgrade_PushUpgradeState(&s_upgradeState);
osalHandler->MutexUnlock(s_upgradeStateMutex);
// Step 2 : Clean upgrade program file store area
returnCode = DjiTest_CleanUpgradeProgramFileStoreArea();
if (returnCode != DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {
USER_LOG_ERROR("Clean upgrade file dir error, please check dir permission");
osalHandler->MutexLock(s_upgradeStateMutex);
s_upgradeState.upgradeStage = DJI_UPGRADE_STAGE_END;
s_upgradeState.upgradeEndInfo.upgradeEndState = DJI_UPGRADE_END_STATE_UNKNOWN_ERROR;
osalHandler->MutexUnlock(s_upgradeStateMutex);
continue;
}
osalHandler->TaskSleepMs(1000);
osalHandler->MutexLock(s_upgradeStateMutex);
s_upgradeState.upgradeStage = DJI_UPGRADE_STAGE_ONGOING;
s_upgradeState.upgradeOngoingInfo.upgradeProgress = 30;
DjiUpgrade_PushUpgradeState(&s_upgradeState);
osalHandler->MutexUnlock(s_upgradeStateMutex);
}
//attention emulation upgrade progress, user don't need this process
do {
osalHandler->TaskSleepMs(1000);
osalHandler->MutexLock(s_upgradeStateMutex);
s_upgradeState.upgradeStage = DJI_UPGRADE_STAGE_ONGOING;
s_upgradeState.upgradeOngoingInfo.upgradeProgress += 10;
tempUpgradeState = s_upgradeState;
DjiUpgrade_PushUpgradeState(&s_upgradeState);
osalHandler->MutexUnlock(s_upgradeStateMutex);
} while (tempUpgradeState.upgradeOngoingInfo.upgradeProgress < 100);
// Step 3 : Reboot device
osalHandler->MutexLock(s_upgradeStateMutex);
s_upgradeState.upgradeStage = DJI_UPGRADE_STAGE_DEVICE_REBOOT;
s_upgradeState.upgradeRebootInfo.rebootTimeout = DJI_TEST_UPGRADE_REBOOT_TIMEOUT;
DjiUpgrade_PushUpgradeState(&s_upgradeState);
osalHandler->MutexUnlock(s_upgradeStateMutex);
osalHandler->TaskSleepMs(1000); // sleep 1000ms to ensure push send terminal.
upgradeEndInfo.upgradeEndState = DJI_UPGRADE_END_STATE_SUCCESS;
returnCode = DjiTest_SetUpgradeRebootState(&upgradeEndInfo);
if (returnCode != DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {
USER_LOG_ERROR("Set Upgrade reboot state error");
osalHandler->MutexLock(s_upgradeStateMutex);
s_upgradeState.upgradeStage = DJI_UPGRADE_STAGE_END;
s_upgradeState.upgradeEndInfo.upgradeEndState = DJI_UPGRADE_END_STATE_UNKNOWN_ERROR;
osalHandler->MutexUnlock(s_upgradeStateMutex);
continue;
}
returnCode = DjiTest_RebootSystem();
if (returnCode != DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {
USER_LOG_ERROR("Reboot system error");
osalHandler->MutexLock(s_upgradeStateMutex);
s_upgradeState.upgradeStage = DJI_UPGRADE_STAGE_END;
s_upgradeState.upgradeEndInfo.upgradeEndState = DJI_UPGRADE_END_STATE_UNKNOWN_ERROR;
osalHandler->MutexUnlock(s_upgradeStateMutex);
continue;
}
while (1) {
osalHandler->TaskSleepMs(500);
}
} else if (s_upgradeState.upgradeStage == DJI_UPGRADE_STAGE_END) {
osalHandler->MutexLock(s_upgradeStateMutex);
DjiUpgrade_PushUpgradeState(&s_upgradeState);
osalHandler->MutexUnlock(s_upgradeStateMutex);
}
osalHandler->TaskSleepMs(500);
}
}
#ifndef __CC_ARM
#pragma GCC diagnostic pop
#endif
/****************** (C) COPYRIGHT DJI Innovations *****END OF FILE****/