1 /*
2  * Copyright (C) 2015-2018 Alibaba Group Holding Limited
3  */
4 
5 #ifndef _IOTX_OTA_INTERNAL_H_
6 #define _IOTX_OTA_INTERNAL_H_
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 
12 #include "linkkit/infra/infra_httpc.h"
13 #include "linkkit/infra/infra_string.h"
14 #include "linkkit/infra/infra_md5.h"
15 #include "linkkit/infra/infra_sha256.h"
16 #include "linkkit/infra/infra_json_parser.h"
17 #include "iotx_ota.h"
18 #include "ota_api.h"
19 #include "iotx_ota_config.h"
20 #include "linkkit/wrappers/wrappers.h"
21 
22 #ifdef INFRA_MEM_STATS
23 #include "linkkit/infra/infra_mem_stats.h"
24 #define OTA_MALLOC(size)     LITE_malloc(size, MEM_MAGIC, "ota")
25 #define OTA_FREE(ptr)        LITE_free(ptr)
26 #define OTA_API_MALLOC(size) LITE_malloc(size, MEM_MAGIC, "ota.api")
27 #define OTA_API_FREE(ptr)    LITE_free(ptr)
28 #else
29 #define OTA_MALLOC(size) HAL_Malloc(size)
30 #define OTA_FREE(ptr)          \
31     {                          \
32         HAL_Free((void *)ptr); \
33         ptr = NULL;            \
34     }
35 #define OTA_API_MALLOC(size) HAL_Malloc(size)
36 #define OTA_API_FREE(ptr)      \
37     {                          \
38         HAL_Free((void *)ptr); \
39         ptr = NULL;            \
40     }
41 #endif
42 
43 #define OTA_SNPRINTF HAL_Snprintf
44 
45 #ifdef INFRA_LOG
46 #include "linkkit/infra/infra_log.h"
47 #define OTA_LOG_CRIT(...)  log_crit("ota", __VA_ARGS__)
48 #define OTA_LOG_ERROR(...) log_err("ota", __VA_ARGS__)
49 #define OTA_LOG_WRN(...)   log_warning("ota", __VA_ARGS__)
50 #define OTA_LOG_INFO(...)  log_info("ota", __VA_ARGS__)
51 #define OTA_LOG_DEBUG(...) log_debug("ota", __VA_ARGS__)
52 #else
53 #define OTA_LOG_CRIT(...)        \
54     do {                         \
55         HAL_Printf(__VA_ARGS__); \
56         HAL_Printf("\r\n");      \
57     } while (0)
58 #define OTA_LOG_ERROR(...)       \
59     do {                         \
60         HAL_Printf(__VA_ARGS__); \
61         HAL_Printf("\r\n");      \
62     } while (0)
63 #define OTA_LOG_WRN(...)         \
64     do {                         \
65         HAL_Printf(__VA_ARGS__); \
66         HAL_Printf("\r\n");      \
67     } while (0)
68 #define OTA_LOG_INFO(...)        \
69     do {                         \
70         HAL_Printf(__VA_ARGS__); \
71         HAL_Printf("\r\n");      \
72     } while (0)
73 #define OTA_LOG_DEBUG(...)       \
74     do {                         \
75         HAL_Printf(__VA_ARGS__); \
76         HAL_Printf("\r\n");      \
77     } while (0)
78 #endif
79 
80 typedef enum {
81     IOTX_OTA_TOPIC_TYPE_DEVICE_REQUEST = 1,
82     IOTX_OTA_TOPIC_TYPE_DEVICE_UPGRATE = 2,
83     IOTX_OTA_TOPIC_TYPE_CONFIG_GET = 3,
84     IOTX_OTA_TOPIC_TYPE_CONFIG_PUSH = 4,
85     IOTX_OTA_TOPIC_TYPE_MAX
86 } iotx_ota_topic_types_t;
87 
88 typedef int (*ota_cb_fpt)(void *pcontext, const char *msg, uint32_t msg_len,
89                           iotx_ota_topic_types_t type);
90 /* is_fetch = 0; start fetch */
91 /* is_fetch = 1; stop fetch */
92 
93 const char *otalib_JsonValueOf(const char *json, uint32_t json_len,
94                                const char *key, uint32_t *val_len);
95 void *otalib_MD5Init(void);
96 void otalib_MD5Update(void *md5, const char *buf, size_t buf_len);
97 void otalib_MD5Finalize(void *md5, char *output_str);
98 void otalib_MD5Deinit(void *md5);
99 void *otalib_Sha256Init(void);
100 void otalib_Sha256Update(void *sha256, const char *buf, size_t buf_len);
101 void otalib_Sha256Finalize(void *sha256, char *output_str);
102 void otalib_Sha256Deinit(void *sha256);
103 int otalib_GetFirmwareFixlenPara(const char *json_doc, size_t json_doc_len,
104                                  const char *key, char *dest, size_t dest_len);
105 int otalib_GetFirmwareVarlenPara(const char *json_doc, size_t json_doc_len,
106                                  const char *key, char **dest);
107 int otalib_GetParams(const char *json_doc, uint32_t json_len, char **url,
108                      char **version, char *md5, uint32_t *file_size);
109 int otalib_GetConfigParams(const char *json_doc, uint32_t json_len,
110                            char **configId, uint32_t *configSize, char **sign,
111                            char **signMethod, char **url, char **getType);
112 int otalib_GenInfoMsg(char *buf, size_t buf_len, uint32_t id,
113                       const char *version);
114 int otalib_GenReportMsg(char *buf, size_t buf_len, uint32_t id, int progress,
115                         const char *msg_detail);
116 
117 void *ofc_Init(char *url, int offset);
118 int32_t ofc_Fetch(void *handle, char *buf, uint32_t buf_len,
119                   uint32_t timeout_s);
120 int ofc_Deinit(void **handle);
121 
122 #endif /* _IOTX_OTA_INTERNAL_H_ */
123