b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 1 | /** |
| 2 | * @file |
| 3 | * @author Jinhua Huang <jinhuahuang@asrmicro.com> |
| 4 | * @version 1.0 |
| 5 | * |
| 6 | * @section LICENSE |
| 7 | * Copyright (C) 2020, ASR microelectronics, All rights reserved. |
| 8 | * |
| 9 | * @section DESCRIPTION |
| 10 | * |
| 11 | * The time class represents a moment of time. |
| 12 | */ |
| 13 | #ifndef ABOOT_TINY_H |
| 14 | #define ABOOT_TINY_H |
| 15 | |
| 16 | #include <stddef.h> |
| 17 | #include <stdio.h> |
| 18 | #include <stdint.h> |
| 19 | #include <stdbool.h> |
| 20 | #ifdef __linux__ |
| 21 | #include <sys/select.h> |
| 22 | #endif |
| 23 | |
| 24 | #ifdef __cplusplus |
| 25 | extern "C" { |
| 26 | #endif |
| 27 | |
| 28 | #if defined _WIN32 || defined __CYGWIN__ || defined __MINGW32__ |
| 29 | #if defined(BUILD_DLL) |
| 30 | #define ABOOT_API __declspec(dllexport) |
| 31 | #elif defined(USE_DLL) |
| 32 | #define ABOOT_API __declspec(dllimport) |
| 33 | #else |
| 34 | #define ABOOT_API |
| 35 | #endif |
| 36 | #else |
| 37 | #define ABOOT_API __attribute__ ((visibility("default"))) |
| 38 | #endif |
| 39 | |
| 40 | /** |
| 41 | * Callback events to upper layer |
| 42 | */ |
| 43 | typedef enum { |
| 44 | ABOOT_TINY_EVENT_START, |
| 45 | ABOOT_TINY_EVENT_DOWNLOAD, |
| 46 | ABOOT_TINY_EVENT_STOP, |
| 47 | ABOOT_TINY_EVENT_PROGRESS, |
| 48 | ABOOT_TINY_EVENT_STATUS, |
| 49 | } aboot_tiny_event_t; |
| 50 | |
| 51 | /** |
| 52 | * Aboot device running status |
| 53 | */ |
| 54 | #define ABOOT_TINY_STATUS_CONNECTING "CONNECTING" |
| 55 | #define ABOOT_TINY_STATUS_RUNNING "RUNNING" |
| 56 | #define ABOOT_TINY_STATUS_FAILED "FAILED" |
| 57 | #define ABOOT_TINY_STATUS_SUCCEEDED "SUCCEEDED" |
| 58 | |
| 59 | /** |
| 60 | * Aboot command/response size definition |
| 61 | */ |
| 62 | #define ABOOT_COMMAND_SZ 128 |
| 63 | #define ABOOT_RESPONSE_SZ 128 |
| 64 | |
| 65 | /** |
| 66 | * Aboot error code definition |
| 67 | */ |
| 68 | typedef enum { |
| 69 | ABOOT_TINY_ERROR_SUCCESS, |
| 70 | ABOOT_TINY_ERROR_PREAMBLE_TIMEOUT, |
| 71 | ABOOT_TINY_ERROR_TRANSPORT_INIT_FAILED, |
| 72 | ABOOT_TINY_ERROR_LINK_LOST, |
| 73 | ABOOT_TINY_ERROR_NO_RESPONSE, |
| 74 | ABOOT_TINY_ERROR_HANDLE_GETVAR, |
| 75 | ABOOT_TINY_ERROR_HANDLE_DOWNLOAD, |
| 76 | ABOOT_TINY_ERROR_READ_DATA, |
| 77 | ABOOT_TINY_ERROR_UNEXPECT_RESPONSE, |
| 78 | ABOOT_TINY_ERROR_FORCE_STOP, |
| 79 | } aboot_tiny_error_t; |
| 80 | |
| 81 | /** |
| 82 | * Callback message to upper layer |
| 83 | */ |
| 84 | typedef struct { |
| 85 | aboot_tiny_event_t event; |
| 86 | aboot_tiny_error_t error; |
| 87 | union { |
| 88 | const char *message; |
| 89 | int progress; |
| 90 | const char *status; |
| 91 | } u; |
| 92 | } aboot_tiny_message_t; |
| 93 | |
| 94 | /** |
| 95 | * Callback function pointer type |
| 96 | * |
| 97 | * @param msg callback info |
| 98 | * |
| 99 | */ |
| 100 | typedef void (*aboot_tiny_callback_t)(const aboot_tiny_message_t *msg); |
| 101 | |
| 102 | /** |
| 103 | * Function poiner type for customization layer interface |
| 104 | */ |
| 105 | typedef struct firmware_handle firmware_handle_t; |
| 106 | typedef int (*aboot_tiny_uart_send_t)(const uint8_t *data, size_t size); |
| 107 | typedef int (*aboot_tiny_firmware_read_line_t)(firmware_handle_t *handle, |
| 108 | char *data); |
| 109 | typedef int (*aboot_tiny_firmware_read_data_t)(firmware_handle_t *handle, |
| 110 | uint8_t *data, size_t size); |
| 111 | typedef unsigned (*aboot_tiny_clock_get_t)(void); |
| 112 | typedef size_t (*aboot_tiny_uart_rx_callback_t)(const uint8_t *data, |
| 113 | size_t size); |
| 114 | typedef void *(*aboot_tiny_mem_alloc_t)(size_t size); |
| 115 | typedef void (*aboot_tiny_mem_free_t)(void *ptr); |
| 116 | typedef int (*aboot_tiny_log_printf_t)(const char *format, ...); |
| 117 | typedef int (*aboot_tiny_usleep_t)(unsigned long usec); |
| 118 | |
| 119 | /** |
| 120 | * Global function pointer should be assign by porting implementation |
| 121 | */ |
| 122 | extern aboot_tiny_uart_send_t aboot_tiny_uart_send; |
| 123 | extern aboot_tiny_firmware_read_line_t aboot_tiny_firmware_read_line; |
| 124 | extern aboot_tiny_firmware_read_data_t aboot_tiny_firmware_read_data; |
| 125 | extern aboot_tiny_clock_get_t aboot_tiny_clock_get; |
| 126 | extern aboot_tiny_mem_alloc_t aboot_tiny_mem_alloc; |
| 127 | extern aboot_tiny_mem_free_t aboot_tiny_mem_free; |
| 128 | extern aboot_tiny_log_printf_t aboot_tiny_log_printf; |
| 129 | extern aboot_tiny_usleep_t aboot_tiny_usleep; |
| 130 | |
| 131 | #ifndef WITH_CONTIKI |
| 132 | |
| 133 | /** |
| 134 | * System main loop needed for non-protothread environment |
| 135 | * |
| 136 | * @return 0 on success, otherwise failed |
| 137 | */ |
| 138 | int ABOOT_API aboot_tiny_main_loop(void); |
| 139 | |
| 140 | #ifdef __linux__ |
| 141 | struct select_callback { |
| 142 | int (*set_fd)(fd_set *fdr, fd_set *fdw); |
| 143 | void (*handle_fd)(fd_set *fdr, fd_set *fdw); |
| 144 | }; |
| 145 | int aboot_tiny_select_set_callback(int fd, const struct select_callback *callback); |
| 146 | #endif |
| 147 | |
| 148 | #endif /* WITH_CONTIKI */ |
| 149 | |
| 150 | /** |
| 151 | * Initialize base platform environment |
| 152 | * |
| 153 | * @return 0 on success, otherwise failed |
| 154 | */ |
| 155 | int ABOOT_API aboot_tiny_platform_init(void); |
| 156 | |
| 157 | /** |
| 158 | * Callback for uart rx data ready. |
| 159 | * |
| 160 | * @param data uart rx data pointer |
| 161 | * @param size uart rx data size |
| 162 | * |
| 163 | * @return data size processed |
| 164 | * |
| 165 | */ |
| 166 | size_t ABOOT_API aboot_tiny_uart_rx_callback(const uint8_t *data, size_t size); |
| 167 | |
| 168 | /** |
| 169 | * Get aboot max download size. |
| 170 | * |
| 171 | * @return max download size |
| 172 | * |
| 173 | */ |
| 174 | size_t ABOOT_API aboot_tiny_get_max_download_size(void); |
| 175 | |
| 176 | /** |
| 177 | * Set aboot link lost check timeout. |
| 178 | * |
| 179 | * @param second link lost check timeout in seconds |
| 180 | * |
| 181 | */ |
| 182 | void ABOOT_API aboot_tiny_set_link_lost_timeout(int second); |
| 183 | |
| 184 | /** |
| 185 | * Set aboot command response check timeout. |
| 186 | * |
| 187 | * @param second command response check timeout in seconds |
| 188 | * |
| 189 | */ |
| 190 | void ABOOT_API aboot_tiny_set_cmd_response_timeout(int second); |
| 191 | |
| 192 | /** |
| 193 | * Initialize aboot tiny engine. |
| 194 | * |
| 195 | * @param cb callback function pointer |
| 196 | * |
| 197 | * @return 0 on success, otherwise failed |
| 198 | */ |
| 199 | int ABOOT_API aboot_tiny_init(aboot_tiny_callback_t cb); |
| 200 | |
| 201 | /** |
| 202 | * Start aboot download engine. |
| 203 | * |
| 204 | * @param progress_fixup downloading extra image progress |
| 205 | * @param reboot if reboot after completed |
| 206 | * |
| 207 | * @return 0 on success, otherwise failed |
| 208 | */ |
| 209 | int ABOOT_API aboot_tiny_start(firmware_handle_t *firmware, |
| 210 | size_t progress_fixup, bool reboot); |
| 211 | |
| 212 | /** |
| 213 | * Begin aboot download process |
| 214 | * |
| 215 | * @return 0 on success, otherwise failed |
| 216 | */ |
| 217 | int ABOOT_API aboot_tiny_download(void); |
| 218 | |
| 219 | /** |
| 220 | * Stop aboot download engine. |
| 221 | * |
| 222 | * @return 0 on success, otherwise failed |
| 223 | */ |
| 224 | int ABOOT_API aboot_tiny_stop(void); |
| 225 | |
| 226 | /** |
| 227 | * Cleanup aboot tiny engine. |
| 228 | */ |
| 229 | int ABOOT_API aboot_tiny_exit(void); |
| 230 | |
| 231 | /** |
| 232 | * Check if aboot tiny engine is running |
| 233 | * |
| 234 | * @return true for running, otherwise false |
| 235 | */ |
| 236 | bool aboot_tiny_is_running(void); |
| 237 | |
| 238 | #ifdef __cplusplus |
| 239 | } |
| 240 | #endif |
| 241 | |
| 242 | #endif /* ABOOT_TINY_H */ |