yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame] | 1 | #ifndef __DMGR_API_H
|
| 2 | #define __DMGR_API_H
|
| 3 |
|
| 4 | #include <stdarg.h>
|
| 5 |
|
| 6 | typedef int dmgr_t;
|
| 7 | struct notifier_block;
|
| 8 |
|
| 9 | enum NOTIFY {
|
| 10 | NOTIFY_USER_BEGIN = 0,
|
| 11 | NOTIFY_CHECKVERSION_PREPARE,
|
| 12 | NOTIFY_CHECKVERSION_ABORTED,
|
| 13 | NOTIFY_CHECKVERSION_FINISHED,
|
| 14 | NOTIFY_DOWNLOAD_PREPARE,
|
| 15 | NOTIFY_DOWNLOAD_ABORTED,
|
| 16 | NOTIFY_DOWNLOAD_TRANSLATING,
|
| 17 | NOTIFY_DOWNLOAD_FINISHED,
|
| 18 | NOTIFY_DOWNLOAD_PARTTIAL_FINISHED,
|
| 19 | NOTIFY_SOCKET_CONNECT_PREPARE,
|
| 20 | NOTIFY_SOCKET_CONNECT_ABORTED,
|
| 21 | NOTIFY_SOCKET_CONNECT_FINISHED,
|
| 22 | NOTIFY_SOCKET_CONNECT_CLOSED,
|
| 23 | NOTIFY_SOCKET_SEND_PREPARE,
|
| 24 | NOTIFY_SOCKET_SEND_TRANSLATING,
|
| 25 | NOTIFY_SOCKET_SEND_ABORTED,
|
| 26 | NOTIFY_SOCKET_SEND_FINISHED,
|
| 27 | NOTIFY_SOCKET_RECV_PREPARE,
|
| 28 | NOTIFY_SOCKET_RECV_TRANSLATING,
|
| 29 | NOTIFY_SOCKET_RECV_ABORTED,
|
| 30 | NOTIFY_SOCKET_RECV_FINISHED,
|
| 31 | NOTIFY_USER_END
|
| 32 | };
|
| 33 |
|
| 34 | enum NOTIFIER_ID {
|
| 35 | NOTIFY_ID_CHECK_VERSION = 0,
|
| 36 | NOTIFY_ID_DOWNLOAD,
|
| 37 | NOTIFY_ID_SOCKET,
|
| 38 | MAX_USER_NOTIFIERS
|
| 39 | };
|
| 40 |
|
| 41 | enum _DMGR_ERRNO {
|
| 42 | E_OK = 0,
|
| 43 | E_BEGIN = 300,
|
| 44 | E_NOMEM,
|
| 45 | E_INVAL_PARAM,
|
| 46 | E_INVAL_OPERATION,
|
| 47 | E_IO_FAILED,
|
| 48 | E_IO_TIMEDOUT,
|
| 49 | E_VERIFIED_FAILED,
|
| 50 | E_CANCELLED,
|
| 51 | E_NESTING_CALLED,
|
| 52 | E_RESERVE1 = 400,
|
| 53 | /* Error from server */
|
| 54 | E_INVAL_TOKEN,
|
| 55 | E_INVAL_PLATFORM,
|
| 56 | E_PARAM_MISSING,
|
| 57 | E_UNCONFIGURED_VERSION,
|
| 58 | E_INTERNAL_ERROR,
|
| 59 | E_RESERVE2 = 500,
|
| 60 | E_URL_MALFORMAT,
|
| 61 | E_COULDNT_RESOLVE_HOST,
|
| 62 | E_COULDNT_CONNECT,
|
| 63 | E_HTTP_RETURNED_ERROR,
|
| 64 | E_RANGE_ERROR,
|
| 65 | E_HTTP_POST_ERROR,
|
| 66 | E_BAD_DOWNLOAD_RESUME,
|
| 67 | E_ABORTED,
|
| 68 | E_AGAIN,
|
| 69 | E_TOO_MANY_REDIRECTS,
|
| 70 | E_GOT_NOTHING,
|
| 71 | E_SEND_ERROR,
|
| 72 | E_RECV_ERROR,
|
| 73 | E_NO_CONNECTION_AVAILABLE,
|
| 74 | E_END
|
| 75 | };
|
| 76 |
|
| 77 | #define DMGR_ERRNO(err) (int)(-(err))
|
| 78 | #define SOCKET_WAIT_FOREVER ((int)-1)
|
| 79 |
|
| 80 | #define VERINFO_FLAGS_FORCE_PACKAGE (int)(0x01 << 0)
|
| 81 | typedef struct version_info {
|
| 82 | char *version_name;
|
| 83 | int file_size;
|
| 84 | char *delta_id;
|
| 85 | char *md5sum;
|
| 86 | char *delta_url;
|
| 87 | char *release_note;
|
| 88 | int flags;
|
| 89 | char *upgrade_from_time;
|
| 90 | char *upgrade_to_time;
|
| 91 | char *upgrade_gap;
|
| 92 | char *meta_data;
|
| 93 | char *event_id;
|
| 94 | } version_info_t;
|
| 95 |
|
| 96 | typedef struct download_info {
|
| 97 | long total_bytes;
|
| 98 | long break_bytes;
|
| 99 | long saved_bytes;
|
| 100 | long now_bytes;
|
| 101 | } download_info_t;
|
| 102 |
|
| 103 | typedef struct error_value {
|
| 104 | int error;
|
| 105 | } error_value_t;
|
| 106 |
|
| 107 | typedef struct socket_sbuf {
|
| 108 | char *data;
|
| 109 | size_t size;
|
| 110 | } socket_sbuf_t;
|
| 111 |
|
| 112 | typedef struct notifier_data {
|
| 113 | union {
|
| 114 | struct error_value errvalue;
|
| 115 | struct download_info dlinfo;
|
| 116 | struct version_info verinfo;
|
| 117 | struct socket_sbufs {
|
| 118 | struct socket_sbuf *sock_sbufs;
|
| 119 | size_t count;
|
| 120 | } ssbufs;
|
| 121 | } u;
|
| 122 | } notifier_data_t;
|
| 123 |
|
| 124 | typedef int (*notifier_fn_t)(struct notifier_block *nb,
|
| 125 | unsigned long action,
|
| 126 | void *data);
|
| 127 |
|
| 128 | typedef struct socket_connect {
|
| 129 | int (*create)(struct socket_connect *conn,
|
| 130 | char *hostname,
|
| 131 | int port,
|
| 132 | int recv_timeout,
|
| 133 | int send_timeout,
|
| 134 | int connect_timeout,
|
| 135 | int flags);
|
| 136 | int (*destory)(struct socket_connect *conn);
|
| 137 | int (*connect)(struct socket_connect *conn);
|
| 138 | int (*send)(struct socket_connect *conn,
|
| 139 | char *buf,
|
| 140 | size_t size,
|
| 141 | size_t *sent_size);
|
| 142 | int (*send_sbufs)(struct socket_connect *conn,
|
| 143 | struct socket_sbuf *sbufs,
|
| 144 | size_t count,
|
| 145 | size_t *sent_size);
|
| 146 | int (*recv)(struct socket_connect *conn,
|
| 147 | char *buf,
|
| 148 | size_t size,
|
| 149 | size_t *recved_size);
|
| 150 | int (*close)(struct socket_connect *conn);
|
| 151 | } socket_connect_t;
|
| 152 |
|
| 153 | typedef struct notifier_block {
|
| 154 | notifier_fn_t notifier_call;
|
| 155 | struct notifier_block *next;
|
| 156 | int priority;
|
| 157 | int alloc_set;
|
| 158 | } notifier_block_t;
|
| 159 |
|
| 160 | #define init_notifier_block(nb, call, pri) \
|
| 161 | do { \
|
| 162 | (nb)->notifier_call = call; \
|
| 163 | (nb)->next = NULL; \
|
| 164 | (nb)->priority = pri; \
|
| 165 | (nb)->alloc_set= 0; \
|
| 166 | } while (0)
|
| 167 |
|
| 168 | #define SERVER_TRANSOPT_NONE 0
|
| 169 | #define SERVER_TRANSOPT_FOTA ((0x00 << 0) & 0x0f)
|
| 170 | #define SERVER_TRANSOPT_RELIANCE ((0x01 << 0) & 0x0f)
|
| 171 | #define SERVER_TRANSOPT_RELIANCE_ST ((0x02 << 0) & 0x0f)
|
| 172 | #define SERVER_TRANSOPT_VEHICLE ((0x03 << 0) & 0x0f)
|
| 173 | #define SERVER_TRANSOPT_IOT ((0x04 << 0) & 0x0f)
|
| 174 | #define SERVER_TRANSOPT_HTTPS ((0x01 << 4) & 0xf0)
|
| 175 | #define SERVER_TRANSOPT_HTTP ((0x02 << 4) & 0xf0)
|
| 176 | #define SERVER_TRANSOPT_COMPACT_HTTP ((0x03 << 4) & 0xf0)
|
| 177 |
|
| 178 | typedef struct policy_info {
|
| 179 | int so_transfer_timeout; /* The tcp/ip receive/send data timeout */
|
| 180 | int so_connect_timeout; /* The tcp/ip connection timeout */
|
| 181 | int so_recv_buffer_size; /* The max size of receive buffer */
|
| 182 | int download_retry; /* The retry count for download failed */
|
| 183 | int download_retry_time; /* The retry delay time(seconds) for download failed */
|
| 184 | int server_transfer_opt; /* The type or opt of sepcific server */
|
| 185 | char *sstate_cached_path; /* The path used for cache dmgr shared-state */
|
| 186 | char *root_certificates; /* Root certificates are self-signed and form
|
| 187 | the basis of an X.509-based public key
|
| 188 | infrastructure (PKI) */
|
| 189 | char *sub_certificates; /* The path used for cache dmgr shared-state */
|
| 190 | int freespace_for_file_frac; /* file bavail safe proportion */
|
| 191 | } policy_info_t;
|
| 192 |
|
| 193 | #define POLICY_INFO_INIT(name) \
|
| 194 | { .so_transfer_timeout = -1, \
|
| 195 | .so_connect_timeout = -1, \
|
| 196 | .so_recv_buffer_size = 8192, \
|
| 197 | .download_retry = 0, \
|
| 198 | .download_retry_time = 5, \
|
| 199 | .server_transfer_opt = SERVER_TRANSOPT_FOTA| \
|
| 200 | SERVER_TRANSOPT_COMPACT_HTTP, \
|
| 201 | .sstate_cached_path = "/var", \
|
| 202 | .root_certificates = NULL, \
|
| 203 | .sub_certificates = NULL, \
|
| 204 | .freespace_for_file_frac = 90 \
|
| 205 | }
|
| 206 |
|
| 207 | #define POLICY_INFO(name) \
|
| 208 | struct policy_info name = POLICY_INFO_INIT(name)
|
| 209 |
|
| 210 | enum LOGGER {
|
| 211 | LOGGER_STDIO = 0,
|
| 212 | LOGGER_BUFFER,
|
| 213 | LOGGER_FILE
|
| 214 | };
|
| 215 |
|
| 216 | enum LOG_LEVEL {
|
| 217 | LOG_VERBOSE = 0,
|
| 218 | LOG_DEBUG,
|
| 219 | LOG_INFO,
|
| 220 | LOG_WARNING,
|
| 221 | LOG_ERROR,
|
| 222 | LOG_FATAL,
|
| 223 | LOG_OFF
|
| 224 | };
|
| 225 |
|
| 226 | enum LOG_TYPE {
|
| 227 | LOG_TYPE_CHECK = 0,
|
| 228 | LOG_TYPE_DOWNLOAD,
|
| 229 | LOG_TYPE_HTTP,
|
| 230 | LOG_TYPE_LAST
|
| 231 | };
|
| 232 |
|
| 233 | dmgr_t dmgr_alloc(struct policy_info *policy,
|
| 234 | enum LOG_LEVEL default_log_level,
|
| 235 | enum LOGGER logger, ...);
|
| 236 | int dmgr_free(dmgr_t dm_id);
|
| 237 | int dmgr_keepalive_with_server(dmgr_t dm_id);
|
| 238 | int dmgr_register_device_to_server(dmgr_t dm_id);
|
| 239 | int dmgr_register_socket(dmgr_t dm_id, struct socket_connect *conn);
|
| 240 | int dmgr_alloc_notifier(dmgr_t dm_id,
|
| 241 | enum NOTIFIER_ID nh_id,
|
| 242 | notifier_fn_t notifier_call,
|
| 243 | int priority,
|
| 244 | int extra_bytes);
|
| 245 | int dmgr_register_notifier(dmgr_t dm_id,
|
| 246 | struct notifier_block *nb,
|
| 247 | enum NOTIFIER_ID nh_id);
|
| 248 | int dmgr_register_deviceinfo(dmgr_t dm_id,
|
| 249 | const char *name,
|
| 250 | const char *value);
|
| 251 | int dmgr_register_salesinfo(dmgr_t dm_id,
|
| 252 | const char *name,
|
| 253 | const char *value);
|
| 254 |
|
| 255 | #define SERI_KEEP_URL "KEEP_URL"
|
| 256 | #define SERI_REG_URL "REG_URL"
|
| 257 | #define SERI_CHK_URL "CHK_URL"
|
| 258 | #define SERI_DL_URL "DL_URL"
|
| 259 | #define SERI_REPORT_DLR_URL "REPORT_DLR_URL"
|
| 260 | #define SERI_REPORT_UPGR_URL "REPORT_UPGR_URL"
|
| 261 | #define SERI_REPORT_SALES_URL "REPORT_SALES_URL"
|
| 262 |
|
| 263 | int dmgr_register_serverinfo(dmgr_t dm_id,
|
| 264 | const char *name,
|
| 265 | const char *value);
|
| 266 |
|
| 267 | /* The public for checking version */
|
| 268 | int dmgr_check_version(dmgr_t dm_id);
|
| 269 | int dmgr_async_check_version(dmgr_t dm_id);
|
| 270 | int dmgr_async_cancel_checking(dmgr_t dm_id);
|
| 271 |
|
| 272 | /* The public interface for downloading */
|
| 273 | #define DOWNLOAD_FILE_SEEK_BEGIN ((long)0)
|
| 274 | #define DOWNLOAD_FILE_SEEK_END ((long)-1)
|
| 275 |
|
| 276 | int dmgr_filmap_version(dmgr_t dm_id,
|
| 277 | struct version_info *version,
|
| 278 | char *file_path,
|
| 279 | long file_seek,
|
| 280 | long map_addr_start,
|
| 281 | long map_size);
|
| 282 | int dmgr_async_filmap_version(dmgr_t dm_id,
|
| 283 | struct version_info *version,
|
| 284 | char *file_path,
|
| 285 | long file_seek,
|
| 286 | long map_addr_start,
|
| 287 | long map_size);
|
| 288 |
|
| 289 | int dmgr_download_version(dmgr_t dm_id,
|
| 290 | struct version_info *version,
|
| 291 | char *file_path,
|
| 292 | long file_seek,
|
| 293 | long start_from,
|
| 294 | long max_size);
|
| 295 | int dmgr_async_download_version(dmgr_t dm_id,
|
| 296 | struct version_info *version,
|
| 297 | char *file_path,
|
| 298 | long file_seek,
|
| 299 | long start_from,
|
| 300 | long max_size);
|
| 301 |
|
| 302 | int dmgr_async_cancel_downloading(dmgr_t dm_id);
|
| 303 |
|
| 304 | /* The public for reporting interface */
|
| 305 | int dmgr_report_upgraded_version(dmgr_t dm_id,
|
| 306 | struct version_info *version,
|
| 307 | const char *upgraded_desc);
|
| 308 |
|
| 309 | int dmgr_report_sales_tracker(dmgr_t dm_id);
|
| 310 | int dmgr_set_log_level(int dm_id,
|
| 311 | enum LOG_TYPE type,
|
| 312 | enum LOG_LEVEL level);
|
| 313 | int dmgr_version(char *version_buf, int size);
|
| 314 |
|
| 315 | #endif
|