This repository has been archived by the owner on Nov 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathu_cell_fota.c
421 lines (375 loc) · 17.1 KB
/
u_cell_fota.c
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*
* Copyright 2019-2024 u-blox
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* Only #includes of u_* and the C standard library are allowed here,
* no platform stuff and no OS stuff. Anything required from
* the platform/OS must be brought in through u_port* to maintain
* portability.
*/
/** @file
* @brief Implementation of the FOTA API for cellular.
*/
#ifdef U_CFG_OVERRIDE
# include "u_cfg_override.h" // For a customer's configuration override
#endif
#include "stdlib.h" // atoi()
#include "stddef.h" // NULL, size_t etc.
#include "stdint.h" // int32_t etc.
#include "stdbool.h"
#include "u_cfg_sw.h"
#include "u_error_common.h"
#include "u_port.h"
#include "u_port_os.h"
#include "u_port_heap.h"
#include "u_timeout.h"
#include "u_at_client.h"
#include "u_cell_module_type.h"
#include "u_cell_file.h"
#include "u_cell_net.h" // Order is important here
#include "u_cell_private.h" // don't change it
#include "u_cell_fota.h"
/* ----------------------------------------------------------------
* COMPILE-TIME MACROS
* -------------------------------------------------------------- */
/* ----------------------------------------------------------------
* TYPES
* -------------------------------------------------------------- */
/** Structure to help translate the fail cases in the detailed
* download status from the AT interface into our download status.
*/
typedef struct {
int32_t atInterfaceStatus;
uCellFotaStatusDownload_t status;
} uCellFotaStatusDownloadFailConvert_t;
/** Structure to help translate the codes returned by tje +UUFWINSTALL
* URC into our install status.
*/
typedef struct {
int32_t uufwInstallStatus;
uCellFotaStatusInstall_t status;
} uCellFotaStatusInstallConvert_t;
/** All the parameters for the FOTA status callback.
*/
typedef struct {
uDeviceHandle_t cellHandle;
uCellFotaStatus_t status;
uCellFotaStatusCallback_t *pCallback;
void *pCallbackParam;
} uCellFotaStatusCallbackParameters_t;
/** Structure defining the FOTA context.
*/
typedef struct {
uCellFotaStatusCallback_t *pCallback;
void *pCallbackParameter;
} uCellPrivateFotaContext_t;
/* ----------------------------------------------------------------
* VARIABLES
* -------------------------------------------------------------- */
/** Array to convert the fail cases of the detailed download status
* from the AT interface into our download status enum.
*/
static uCellFotaStatusDownloadFailConvert_t gAtDownloadFailureStatus[] = {
{100, U_CELL_FOTA_STATUS_DOWNLOAD_USER_CANCEL},
{101, U_CELL_FOTA_STATUS_DOWNLOAD_MEMORY_ERROR},
{102, U_CELL_FOTA_STATUS_DOWNLOAD_NETWORK_ERROR},
{103, U_CELL_FOTA_STATUS_DOWNLOAD_UNKNOWN_ERROR},
{104, U_CELL_FOTA_STATUS_DOWNLOAD_BAD_URL},
{105, U_CELL_FOTA_STATUS_DOWNLOAD_CONNECTIVITY_LOSS}
};
/** Array to convert the some of the +UUFWINSTALL URCs into
* out install status enum. Any not mentioned here are a one-to-one
* mapping
*/
static uCellFotaStatusInstallConvert_t gInstallStatus[] = {
{128, U_CELL_FOTA_STATUS_INSTALL_SUCCESS},
{141, U_CELL_FOTA_STATUS_INSTALL_RAM_ERROR}
};
/* ----------------------------------------------------------------
* STATIC FUNCTIONS
* -------------------------------------------------------------- */
// Convert a download status fail case number into our enum.
static int32_t convertDownloadFailureStatus(int32_t atDownloadFailureStatus)
{
int32_t downloadFailureStatus = -1;
for (int32_t x = 0;
(x < sizeof(gAtDownloadFailureStatus) / sizeof(gAtDownloadFailureStatus[0])) &&
(downloadFailureStatus < 0); x++) {
if (atDownloadFailureStatus == gAtDownloadFailureStatus[x].atInterfaceStatus) {
downloadFailureStatus = (int32_t) gAtDownloadFailureStatus[x].status;
}
}
return downloadFailureStatus;
}
// Convert a +UUFWINSTALL status value (i.e. ones from 128 upwards) into
// one of our install status enums
static int32_t convertUufwinstallStatus(int32_t uufwinstallStatus)
{
int32_t installStatus = -1;
for (int32_t x = 0;
(x < sizeof(gInstallStatus) / sizeof(gInstallStatus[0])) &&
(installStatus < 0); x++) {
if (uufwinstallStatus == gInstallStatus[x].uufwInstallStatus) {
installStatus = (int32_t) gInstallStatus[x].status;
}
}
if (installStatus < 0) {
// If there's no translation, just pass out what came in
installStatus = uufwinstallStatus;
}
return installStatus;
}
// Callback via which the user's FOTA status callback is called.
// This must be called through the uAtClientCallback() mechanism in
// order to prevent customer code blocking the AT client.
static void fotaStatusCallback(uAtClientHandle_t atHandle, void *pParameter)
{
uCellFotaStatusCallbackParameters_t *pCallback = (uCellFotaStatusCallbackParameters_t *) pParameter;
(void) atHandle;
if (pCallback != NULL) {
if (pCallback->pCallback != NULL) {
pCallback->pCallback(pCallback->cellHandle,
&(pCallback->status),
pCallback->pCallbackParam);
}
uPortFree(pCallback);
}
}
// Call fotaStatusCallback() via the AT client callback queue.
static void queueFotaStatus(uCellPrivateInstance_t *pInstance,
uCellFotaStatus_t *pStatus)
{
uCellPrivateFotaContext_t *pContext = (uCellPrivateFotaContext_t *) pInstance->pFotaContext;
uCellFotaStatusCallbackParameters_t *pCallback;
// Put all the data in a struct and pass a pointer to it to our
// local callback via the AT client's callback mechanism to decouple
// it from whatever might have called us.
// Note: fotaStatusCallback will free the allocated memory.
pCallback = (uCellFotaStatusCallbackParameters_t *) pUPortMalloc(sizeof(*pCallback));
if (pCallback != NULL) {
pCallback->cellHandle = pInstance->cellHandle;
pCallback->status = *pStatus;
pCallback->pCallback = pContext->pCallback;
pCallback->pCallbackParam = pContext->pCallbackParameter;
uAtClientCallback(pInstance->atHandle, fotaStatusCallback, pCallback);
}
}
// The UFOSTAT URC callback.
static void UFOTASTAT_urc(uAtClientHandle_t atHandle,
void *pParameter)
{
uCellFotaStatus_t status;
int32_t event;
int32_t param1;
int32_t param2;
bool urcIsGood = false;
uCellPrivateInstance_t *pInstance = (uCellPrivateInstance_t *) pParameter;
// Populate the status from the URC
event = uAtClientReadInt(atHandle);
// All events have two parameters
param1 = uAtClientReadInt(atHandle);
param2 = uAtClientReadInt(atHandle);
if ((param1 >= 0) && (param2 >= 0)) {
switch (event) {
case 0: // download progress (percentage in param2)
if ((param1 == 1) && (param2 >= 0)) {
status.type = U_CELL_FOTA_STATUS_TYPE_PERCENTAGE_DOWNLOAD;
status.value.percentage = param2;
urcIsGood = true;
}
break;
case 1: // download start
if ((param1 == 0) && (param2 == 0)) {
status.type = U_CELL_FOTA_STATUS_TYPE_DOWNLOAD;
// params tell us nothing of any use, just indicate
// a status of "start"
status.value.download = U_CELL_FOTA_STATUS_DOWNLOAD_START;
urcIsGood = true;
}
break;
case 2: // download complete
status.type = U_CELL_FOTA_STATUS_TYPE_DOWNLOAD;
if ((param1 == 2) && (param2 == 100)) { // success
status.value.download = U_CELL_FOTA_STATUS_DOWNLOAD_SUCCESS;
urcIsGood = true;
} else if (param1 == 3) { // NOT success!
status.value.download = convertDownloadFailureStatus(param2);
if (status.value.download >= 0) {
urcIsGood = true;
}
}
break;
case 3: // FOTA update (or install) status
status.type = U_CELL_FOTA_STATUS_TYPE_INSTALL;
// param1 is our enum exactly, param2 tells us nothing of interest
// Note that we deliberately don't range check the upper bound of
// param1, better to let it through for forwards-compatibility
status.value.install = (uCellFotaStatusInstall_t) param1;
urcIsGood = true;
break;
}
if (urcIsGood) {
queueFotaStatus(pInstance, &status);
}
}
}
// The URC callback for both the UFWPREVAL and UUFWINSTALL URCs.
static void UFWPREVAL_UUFWINSTALL_urc(uAtClientHandle_t atHandle,
void *pParameter,
uCellFotaStatusType_t type)
{
uCellFotaStatus_t status;
int32_t percentageOrStatusCode;
uCellPrivateInstance_t *pInstance = (uCellPrivateInstance_t *) pParameter;
percentageOrStatusCode = uAtClientReadInt(atHandle);
if (percentageOrStatusCode >= 0) {
if (percentageOrStatusCode < U_CELL_FOTA_STATUS_INSTALL_MIN_NUM_UUFWINSTALL) {
status.type = type;
status.value.percentage = percentageOrStatusCode;
} else {
// The +UUFWINSTALL URC sneaks error code in at the end
// Some of these have the same meaning as those emitted
// by the +UFOSTAT URC so some conversion is required
percentageOrStatusCode = convertUufwinstallStatus(percentageOrStatusCode);
status.type = U_CELL_FOTA_STATUS_TYPE_INSTALL;
status.value.install = (uCellFotaStatusInstall_t) percentageOrStatusCode;
}
queueFotaStatus(pInstance, &status);
}
}
// The UUFWPREVAL URC callback.
static void UFWPREVAL_urc(uAtClientHandle_t atHandle,
void *pParameter)
{
UFWPREVAL_UUFWINSTALL_urc(atHandle, pParameter,
U_CELL_FOTA_STATUS_TYPE_PERCENTAGE_CHECK);
}
// The UUFWINSTALL URC callback.
static void UUFWINSTALL_urc(uAtClientHandle_t atHandle,
void *pParameter)
{
UFWPREVAL_UUFWINSTALL_urc(atHandle, pParameter,
U_CELL_FOTA_STATUS_TYPE_PERCENTAGE_INSTALL);
}
/* ----------------------------------------------------------------
* PUBLIC FUNCTIONS
* -------------------------------------------------------------- */
// Enable or disable the FOTA status callback.
int32_t uCellFotaSetStatusCallback(uDeviceHandle_t cellHandle,
int32_t modulePortNumber,
uCellFotaStatusCallback_t *pCallback,
void *pCallbackParameter)
{
int32_t errorCode = (int32_t) U_ERROR_COMMON_NOT_INITIALISED;
uCellPrivateInstance_t *pInstance;
uAtClientHandle_t atHandle;
uCellPrivateFotaContext_t *pContext;
int32_t state = 0;
if (gUCellPrivateMutex != NULL) {
U_PORT_MUTEX_LOCK(gUCellPrivateMutex);
pInstance = pUCellPrivateGetInstance(cellHandle);
errorCode = (int32_t) U_ERROR_COMMON_INVALID_PARAMETER;
if (pInstance != NULL) {
errorCode = (int32_t) U_ERROR_COMMON_NOT_SUPPORTED;
if (U_CELL_PRIVATE_HAS(pInstance->pModule,
U_CELL_PRIVATE_FEATURE_FOTA)) {
errorCode = (int32_t) U_ERROR_COMMON_NO_MEMORY;
pContext = (uCellPrivateFotaContext_t *) pInstance->pFotaContext;
if (pContext == NULL) {
// Note: we don't deallocate this until
// cellular is closed down in order to
// ensure thread-safety of the callback
pContext = (uCellPrivateFotaContext_t *) pUPortMalloc(sizeof(uCellPrivateFotaContext_t));
}
if (pContext != NULL) {
pInstance->pFotaContext = pContext;
errorCode = (int32_t) U_ERROR_COMMON_SUCCESS;
atHandle = pInstance->atHandle;
// Remove any existing URC handlers
uAtClientRemoveUrcHandler(atHandle, "+UFOTASTAT:");
uAtClientRemoveUrcHandler(atHandle, "+UFWPREVAL:");
uAtClientRemoveUrcHandler(atHandle, "+UUFWINSTALL:");
// Remember the new one
pContext->pCallback = pCallback;
pContext->pCallbackParameter = pCallbackParameter;
if (pCallback != NULL) {
state = 1;
if (pInstance->pModule->moduleType != U_CELL_MODULE_TYPE_LEXI_R10) {
errorCode = uAtClientSetUrcHandler(atHandle, "+UFOTASTAT:",
UFOTASTAT_urc, pInstance);
}
}
if (errorCode == 0) {
if (pInstance->pModule->moduleType != U_CELL_MODULE_TYPE_LEXI_R10) {
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UFOTASTAT=");
uAtClientWriteInt(atHandle, state);
uAtClientCommandStopReadResponse(atHandle);
errorCode = uAtClientUnlock(atHandle);
}
if (errorCode == 0) {
if ((state == 1) &&
(uAtClientSetUrcHandler(atHandle, "+UUFWINSTALL:",
UUFWINSTALL_urc, pInstance) == 0)) {
if (pInstance->pModule->moduleType == U_CELL_MODULE_TYPE_LEXI_R10) {
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UFWINSTALL");
uAtClientCommandStopReadResponse(atHandle);
if (uAtClientUnlock(atHandle) != 0) {
// Clean up on error
uAtClientRemoveUrcHandler(atHandle, "+UUFWINSTALL:");
}
} else {
if (uAtClientSetUrcHandler(atHandle, "+UFWPREVAL:",
UFWPREVAL_urc, pInstance) == 0) {
// Not all modules support the AT+UFWINSTALL
// command which is required to get the validation
// and installation progress (and it can only be
// switched on, not off); don't fail on this
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UFWINSTALL=");
// Specify the port number if given
if (modulePortNumber >= 0) {
uAtClientWriteInt(atHandle, modulePortNumber);
} else {
uAtClientWriteString(atHandle, "", false);
}
// Skip the second and third parameters
uAtClientWriteString(atHandle, "", false);
uAtClientWriteString(atHandle, "", false);
uAtClientWriteInt(atHandle, state);
uAtClientCommandStopReadResponse(atHandle);
if (uAtClientUnlock(atHandle) != 0) {
// Clean up on error
uAtClientRemoveUrcHandler(atHandle, "+UUFWINSTALL:");
uAtClientRemoveUrcHandler(atHandle, "+UFWPREVAL:");
}
}
}
}
} else {
// Clean up on error
pContext->pCallback = NULL;
pContext->pCallbackParameter = NULL;
uAtClientRemoveUrcHandler(atHandle, "+UFOTASTAT:");
}
}
}
}
}
U_PORT_MUTEX_UNLOCK(gUCellPrivateMutex);
}
return errorCode;
}
// End of file