| 1 | /* |
| 2 | * lws OTA updates |
| 3 | * |
| 4 | * Copyright (C) 2019 - 2022 Andy Green <andy@warmcat.com> |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 22 | * IN THE SOFTWARE. |
| 23 | * |
| 24 | * This is the platform interface that lws_ota uses to flash new firmware. |
| 25 | * The platform implementation for these ops is set via lws_system and consists |
| 26 | * of user code. |
| 27 | * |
| 28 | * All the update-related calls have async interfaces with a callback and opaque |
| 29 | * callback context that is called on completion. This allows us to, eg, |
| 30 | * download the next buffer while flashing the previous one. |
| 31 | * |
| 32 | * If the platform implementation is actually synchronous, then just call the |
| 33 | * callback before returning. |
| 34 | * |
| 35 | * If it is async, because eg, erase is slow, in the platform ota op |
| 36 | * implementation spawn a thread to do the platform operation, return |
| 37 | * immediately with LWSOTARET_ONGOING, and call the callback from the spawned |
| 38 | * thread context with the real return before terminating the thread. |
| 39 | */ |
| 40 | |
| 41 | typedef void * lws_ota_process_t; |
| 42 | |
| 43 | typedef enum { |
| 44 | LWSOTARET_OK, |
| 45 | LWSOTARET_ONGOING, /* result not ready to read yet */ |
| 46 | LWSOTARET_REJECTED, |
| 47 | LWSOTARET_NOSLOT, |
| 48 | |
| 49 | LWSOTARET_UPDATE_AVAILABLE, |
| 50 | LWSOTARET_PROGRESS, |
| 51 | LWSOTARET_FAILED, |
| 52 | LWSOTARET_COMPLETED |
| 53 | } lws_ota_ret_t; |
| 54 | |
| 55 | typedef enum { |
| 56 | LWS_OTA_ASYNC_START = 1, |
| 57 | LWS_OTA_ASYNC_WRITE, |
| 58 | LWS_OTA_ASYNC_ABORT, |
| 59 | LWS_OTA_ASYNC_FINALIZE |
| 60 | } lws_ota_async_t; |
| 61 | |
| 62 | struct lws_ota; |
| 63 | |
| 64 | typedef void (*lws_ota_cb_t)(void *ctx, lws_ota_ret_t r); |
| 65 | |
| 66 | typedef struct { |
| 67 | |
| 68 | /* asynchronous (completions via lws_cancel_service) */ |
| 69 | |
| 70 | int (*ota_start)(struct lws_ota *g); |
| 71 | /**< Creates the ota task and queues LWS_OTA_ASYNC_START on it. */ |
| 72 | |
| 73 | void (*ota_queue)(struct lws_ota *g, lws_ota_async_t a); |
| 74 | /**< Queue next command to OTA task (args are in g) */ |
| 75 | |
| 76 | /* synchronous */ |
| 77 | |
| 78 | int (*ota_report_current)(struct lws_ota *g, int bad); |
| 79 | /**< Report information to the platform code about how we feel about the |
| 80 | * current boot... if we can check the OTA then we report it seems in |
| 81 | * good shape (bad = 0), if we can identify it's brain-damaged then |
| 82 | * (bad = 1). What action the platform takes about these reports is up |
| 83 | * to the platform code */ |
| 84 | |
| 85 | int (*ota_progress)(lws_ota_ret_t state, int percent); |
| 86 | /**< Gets called so the platform can represent OTA progress, give |
| 87 | * platform a chance to choose what to do about an available update */ |
| 88 | |
| 89 | int (*ota_get_last_fw_unixtime)(uint64_t *fw_unixtime); |
| 90 | /**< tries to recover the newest firmware unixtime that had been |
| 91 | * OTA'd into fw_unixtime, updates from same or earlier unixtime are |
| 92 | * ignored for update purposes. */ |
| 93 | |
| 94 | int ota_periodic_check_secs; |
| 95 | /**< Check after this many seconds for a new update */ |
| 96 | } lws_ota_ops_t; |
| 97 | |
| 98 | /** |
| 99 | * lws_ota_variant_name() - returns the build variant name |
| 100 | * |
| 101 | * Returns a string that uniquely identifies the kind of firmware build this |
| 102 | * device is running. |
| 103 | */ |
| 104 | |
| 105 | LWS_VISIBLE LWS_EXTERN const char * |
| 106 | lws_ota_variant_name(void); |
| 107 | |
| 108 | LWS_VISIBLE LWS_EXTERN int |
| 109 | lws_plat_ota_start(struct lws_ota *g); |
| 110 | |
| 111 | |
| 112 | #define LWSOTAFIN_OK 0 |
| 113 | #define LWSOTAFIN_BAD 1 |
| 114 | |
| 115 | LWS_VISIBLE LWS_EXTERN void |
| 116 | lws_plat_ota_queue(struct lws_ota *g, lws_ota_async_t a); |
| 117 | |
| 118 | LWS_VISIBLE LWS_EXTERN int |
| 119 | lws_plat_ota_report_current(struct lws_ota *g, int bad); |
| 120 | |
| 121 | LWS_VISIBLE LWS_EXTERN int |
| 122 | lws_plat_ota_get_last_fw_unixtime(uint64_t *fw_unixtime); |
| 123 | |