1 /*
2 * Copyright (C) 2015-2018 Alibaba Group Holding Limited
3 */
4 #include "iotx_dm_internal.h"
5
6 #if defined(OTA_ENABLED) && !defined(BUILD_AOS)
7
8 static dm_ota_ctx_t g_dm_ota_ctx;
9
_dm_ota_get_ctx(void)10 static dm_ota_ctx_t *_dm_ota_get_ctx(void)
11 {
12 return &g_dm_ota_ctx;
13 }
14
_dm_ota_clean_state(void * context)15 static int _dm_ota_clean_state(void *context)
16 {
17 dm_cota_ctx_t *cota = dm_cota_get_ctx();
18 dm_fota_ctx_t *fota = dm_fota_get_ctx();
19 if (cota != NULL) {
20 cota->is_report_new_config = 0;
21 }
22
23 if (fota != NULL) {
24 fota->is_report_new_config = 0;
25 }
26 return 0;
27 }
28
dm_ota_init(void)29 int dm_ota_init(void)
30 {
31 dm_ota_ctx_t *ctx = _dm_ota_get_ctx();
32 memset(ctx, 0, sizeof(dm_ota_ctx_t));
33
34 HAL_GetProductKey(ctx->product_key);
35 HAL_GetDeviceName(ctx->device_name);
36
37 return SUCCESS_RETURN;
38 }
39
dm_ota_sub(void)40 int dm_ota_sub(void)
41 {
42 dm_ota_ctx_t *ctx = _dm_ota_get_ctx();
43 void *handle = NULL;
44
45 /* Init OTA Handle */
46 handle = IOT_OTA_Init(ctx->product_key, ctx->device_name, NULL);
47 if (handle == NULL) {
48 return FAIL_RETURN;
49 }
50 IOT_OTA_SetOnPushedCallback(handle, _dm_ota_clean_state);
51 ctx->ota_handle = handle;
52
53 return SUCCESS_RETURN;
54 }
55
dm_ota_deinit(void)56 int dm_ota_deinit(void)
57 {
58 dm_ota_ctx_t *ctx = _dm_ota_get_ctx();
59
60 if (ctx->ota_handle) {
61 IOT_OTA_Deinit(ctx->ota_handle);
62 ctx->ota_handle = NULL;
63 }
64
65 return SUCCESS_RETURN;
66 }
67 #ifdef DEVICE_MODEL_GATEWAY
68 #ifdef DEVICE_MODEL_SUBDEV_OTA
dm_ota_switch_device(int devid)69 int dm_ota_switch_device(int devid)
70 {
71 char pk[IOTX_PRODUCT_KEY_LEN + 1] = { 0 };
72 char dn[IOTX_DEVICE_NAME_LEN + 1] = { 0 };
73 char ds[IOTX_DEVICE_SECRET_LEN + 1] = { 0 };
74 int ret = dm_mgr_search_device_by_devid(devid, pk, dn, ds);
75 void *ota_handle = NULL;
76 int res = -1;
77 dm_ota_ctx_t *ctx = NULL;
78
79 if (SUCCESS_RETURN != ret) {
80 dm_log_err("could not find device by id, ret is %d", ret);
81 return FAIL_RETURN;
82 }
83 dm_log_info("do subdevice ota, pk, dn is %s, %s", pk, dn);
84
85 ota_handle = NULL;
86 res = dm_ota_get_ota_handle(&ota_handle);
87
88 if (res != SUCCESS_RETURN) {
89 return FAIL_RETURN;
90 }
91
92 /* if currently a device is doing OTA, do not interrupt */
93 if (IOT_OTA_IsFetching(ota_handle)) {
94 dm_log_info("OTA is processing, can not switch to another device");
95 return FAIL_RETURN;
96 }
97
98 dm_ota_deinit();
99 ctx = _dm_ota_get_ctx();
100 memset(ctx, 0, sizeof(dm_ota_ctx_t));
101
102 memcpy(ctx->product_key, pk, strlen(pk) + 1);
103 memcpy(ctx->device_name, dn, strlen(dn) + 1);
104 ret = dm_ota_sub();
105 if (ret < 0) {
106 dm_log_err("dm_ota_sub ret is %d, %s, %s\n", ret, pk, dn);
107 }
108 return ret;
109 }
110 #endif
111 #endif
112
dm_ota_get_ota_handle(void ** handle)113 int dm_ota_get_ota_handle(void **handle)
114 {
115 dm_ota_ctx_t *ctx = _dm_ota_get_ctx();
116
117 if (handle == NULL || *handle != NULL) {
118 return FAIL_RETURN;
119 }
120
121 if (ctx->ota_handle == NULL) {
122 return FAIL_RETURN;
123 }
124
125 *handle = ctx->ota_handle;
126
127 return SUCCESS_RETURN;
128 }
129 #endif
130