you.chen | 79d8f93 | 2023-12-26 17:03:44 +0800 | [diff] [blame] | 1 | #include <stdio.h>
|
| 2 | #include <string.h>
|
| 3 | #include <stdlib.h>
|
| 4 | #include "include/libat/lynq_at_fota.h"
|
| 5 | #ifdef __cplusplus
|
| 6 | extern "C" {
|
| 7 | #endif
|
| 8 | #include <log/log.h>
|
| 9 | #include "mtk_device_wrap.h"
|
| 10 | #include "include/iot_rock.h"
|
| 11 |
|
| 12 | #define UPDATA_ADDR "/tmp/fota.delta"
|
| 13 | #define READ_BLOCK_SIZE 0x40000
|
| 14 | #define FLASH_DEV_DELTA "/dev/mtd41"
|
| 15 | #define LOG_TAG "AT-FOTA"
|
| 16 |
|
| 17 | lynq_atsvc_outcb g_handle_output;
|
| 18 | char str[128];
|
| 19 | char g_buffer[128] = {0};
|
| 20 | int Response = 0;
|
| 21 | int g_lynq_fota_md5_flag = -1;
|
| 22 | char g_put_addr[64];
|
| 23 | char g_put_path[35];
|
| 24 | char g_cmd[128];
|
| 25 |
|
| 26 | void *deal_download()
|
| 27 | {
|
| 28 | int ret;
|
| 29 | RLOGD("start deal_download !!!\n");
|
| 30 | ret = system(g_cmd);
|
| 31 | RLOGD("ret:%d \n", ret);
|
| 32 | if(ret != 0)
|
| 33 | {
|
| 34 | strcpy(str, "+FOTA: fota download fail\n");
|
| 35 | g_handle_output(str, strlen(str), Response);
|
| 36 | return NULL;
|
| 37 | }
|
| 38 | else
|
| 39 | {
|
| 40 | strcpy(str, "+FOTA: fota download success\n");
|
| 41 | g_handle_output(str, strlen(str), Response);
|
| 42 | }
|
| 43 | return NULL;
|
| 44 | }
|
| 45 |
|
| 46 |
|
| 47 | int lynq_download_package(char (*string)[35])
|
| 48 | {
|
| 49 | int ret = 0;
|
| 50 | int str_num = -1;
|
| 51 | pthread_t tid;
|
| 52 |
|
| 53 | RLOGD("function %s start\n", __FUNCTION__);
|
| 54 | for(int i = 0; i < 9; i++)
|
| 55 | {
|
| 56 | if(string[i] == NULL)
|
| 57 | {
|
| 58 | strcpy(str, "+FOTA: download data null\n");
|
| 59 | g_handle_output(str, strlen(str), Response);
|
| 60 | return 1;
|
| 61 | }
|
| 62 | }
|
| 63 |
|
| 64 | if(strlen(string[9]) == 0)
|
| 65 | {
|
| 66 | ret = chdir("/tmp/");
|
| 67 | if( 0 != ret)
|
| 68 | {
|
| 69 | RLOGD("/tmp/ is error\n");
|
| 70 | strcpy(str, "+FOTA: the tmp is error\n");
|
| 71 | g_handle_output(str, strlen(str), Response);
|
| 72 | return ret;
|
| 73 | }
|
| 74 | strcpy(g_put_path, "/tmp/");
|
| 75 | }
|
| 76 | else
|
| 77 | {
|
| 78 | ret = chdir(string[9]);
|
| 79 | if( 0 != ret)
|
| 80 | {
|
| 81 | RLOGD("%s is error directory \n", string[9]);
|
| 82 | strcpy(str, "+FOTA: NO such directory\n");
|
| 83 | g_handle_output(str, strlen(str), Response);
|
| 84 | return ret;
|
| 85 | }
|
| 86 | strcpy(g_put_path, string[9]);
|
| 87 | }
|
| 88 |
|
| 89 | sprintf(g_cmd,"wget ftp://%s:%s@%s:%s%s%s",string[2],string[3],string[0],string[1],string[7],string[8]);
|
| 90 | RLOGD("g_cmd:%s\n", g_cmd);
|
| 91 | str_num = strlen(g_put_path);
|
| 92 |
|
| 93 | if(g_put_path[str_num - 1] == '/')
|
| 94 | {
|
| 95 | sprintf(g_put_addr,"%s%s", g_put_path, string[8]);
|
| 96 | }
|
| 97 | else
|
| 98 | {
|
| 99 | sprintf(g_put_addr,"%s/%s", g_put_path, string[8]);
|
| 100 | }
|
| 101 | RLOGD("g_put_addr: %s\n", g_put_addr);
|
| 102 | if(lynq_fota_set_addr_value(g_put_addr,strlen(g_put_addr)))
|
| 103 | {
|
| 104 | strcpy(str, "+FOTA: set fota addr fail\n");
|
| 105 | g_handle_output(str, strlen(str), Response);
|
| 106 | }
|
| 107 |
|
| 108 | pthread_create(&tid,NULL,deal_download,NULL);
|
| 109 | pthread_detach(tid);
|
| 110 |
|
| 111 | return ret;
|
| 112 | }
|
| 113 |
|
| 114 | /*
|
| 115 | * @brief Differential subcontracting MD5 check
|
| 116 | *
|
| 117 | * @param string:MD5 value,Subcontracting address,Refer to the fota documentation
|
| 118 | */
|
| 119 |
|
| 120 | int lynq_md5_package(char (*string)[35])
|
| 121 | {
|
| 122 | char input_md5_data[64] = {0};
|
| 123 | RLOGD("function %s start\n", __FUNCTION__);
|
| 124 | for(int i = 0; i < 2; i++)
|
| 125 | {
|
| 126 | if(string[i] == NULL)
|
| 127 | {
|
| 128 | strcpy(str, "+FOTA: md5 value null\n");
|
| 129 | g_handle_output(str, strlen(str), Response);
|
| 130 | return 1;
|
| 131 | }
|
| 132 | }
|
| 133 |
|
| 134 | sprintf(input_md5_data,"%s",string[0]);
|
| 135 | RLOGD("input_md5_data: %s\n", string[0]);
|
| 136 | g_lynq_fota_md5_flag = lynq_md5_file_verfy(g_put_addr, input_md5_data);
|
| 137 | if(g_lynq_fota_md5_flag != 0)
|
| 138 | {
|
| 139 | strcpy(str, "+FOTA: md5 fail\n");
|
| 140 | g_handle_output(str, strlen(str), Response);
|
| 141 | return 1;
|
| 142 | }
|
| 143 | return 0;
|
| 144 | }
|
| 145 |
|
| 146 | /**
|
| 147 | * @brief fota
|
| 148 | *
|
| 149 | * @param void
|
| 150 | */
|
| 151 | int lynq_fota_try(void)
|
| 152 | {
|
| 153 | RLOGD("function %s start\n", __FUNCTION__);
|
| 154 | if(g_lynq_fota_md5_flag == 0) //upgrade success ,device reboot ,lynq_fota_md5_flag no need to reset
|
| 155 | {
|
| 156 | if(0 != lynq_rock_main(1))
|
| 157 | {
|
| 158 | g_lynq_fota_md5_flag = -1;
|
| 159 | strcpy(str, "+FOTA: update fail\n");
|
| 160 | g_handle_output(str, strlen(str), Response);
|
| 161 | return 1;
|
| 162 | }
|
| 163 | }
|
| 164 | else
|
| 165 | {
|
| 166 | strcpy(str, "+FOTA: md5 verify fail\n");
|
| 167 | g_handle_output(str, strlen(str), Response);
|
| 168 | return 1;
|
| 169 | }
|
| 170 |
|
| 171 | return 0;
|
| 172 | }
|
| 173 |
|
| 174 | void lynq_at_fota(char (*argv)[35])
|
| 175 | {
|
| 176 | int result;
|
| 177 | RLOGD("function %s start\n", __FUNCTION__);
|
| 178 | RLOGD("buffer: %s",g_buffer);
|
| 179 | if(!(strcmp(argv[0], "AT+FOTA?")))
|
| 180 | {
|
| 181 | if(strlen(g_buffer) == 0)
|
| 182 | {
|
| 183 | strcpy(str, "+FOTA: OK\n");
|
| 184 | }
|
| 185 | else
|
| 186 | {
|
| 187 | sprintf(str, "+FOTA: %s\n",g_buffer);
|
| 188 |
|
| 189 | }
|
| 190 | g_handle_output(str, strlen(str), Response);
|
| 191 | return ;
|
| 192 | }
|
| 193 | else if(!(strcmp(argv[0], "AT+FOTA=?")))
|
| 194 | {
|
| 195 | strcpy(str, "+FOTA:(0-2),""\n");
|
| 196 | g_handle_output(str, strlen(str), Response);
|
| 197 | return ;
|
| 198 |
|
| 199 | }
|
| 200 | if(!(strcmp(argv[0], "AT+FOTA=0")) || !(strcmp(argv[0], "AT+LYNQFOTA=download")))
|
| 201 | {
|
| 202 | RLOGD("argv[0]: %s\n", argv[0]);
|
| 203 | if(!(strcmp(argv[1], "ftp")))
|
| 204 | {
|
| 205 | result = lynq_download_package(&argv[2]);
|
| 206 | if(result != 0)
|
| 207 | {
|
| 208 | strcpy(str, "+FOTA: DOWNLOAD FAIL\n");
|
| 209 | }
|
| 210 | else
|
| 211 | {
|
| 212 | strcpy(str, "+FOTA: DOWNLOAD START\n");
|
| 213 | }
|
| 214 | }
|
| 215 | else
|
| 216 | {
|
| 217 | strcpy(str, "+FOTA: NO FTP DOWNLOAD\n");
|
| 218 | }
|
| 219 | }
|
| 220 |
|
| 221 | else if(!(strcmp(argv[0], "AT+FOTA=1")) || !(strcmp(argv[0], "AT+LYNQFOTA=md5")))
|
| 222 | {
|
| 223 | RLOGD("argv[0]: %s\n", argv[0]);
|
| 224 | result = lynq_md5_package(&argv[1]);
|
| 225 | if(result != 0)
|
| 226 | {
|
| 227 | strcpy(str, "+FOTA: MD5 FAIL\n");
|
| 228 | }
|
| 229 | else
|
| 230 | {
|
| 231 | strcpy(str, "+FOTA: MD5 SUCCESS\n");
|
| 232 | }
|
| 233 | }
|
| 234 | else if(!(strcmp(argv[0], "AT+FOTA=2")) || !(strcmp(argv[0], "AT+LYNQFOTA=upgrade")))
|
| 235 | {
|
| 236 | RLOGD("argv[0]: %s\n", argv[0]);
|
| 237 | result = lynq_fota_try();
|
| 238 | if(result == 0)
|
| 239 | {
|
| 240 | strcpy(str, "+FOTA: UPDATE SUCCESS\n");
|
| 241 | }
|
| 242 | else
|
| 243 | {
|
| 244 | strcpy(str, "+FOTA: UPDATE FAIL\n");
|
| 245 | }
|
| 246 | }
|
| 247 | else
|
| 248 | {
|
| 249 | strcpy(str, "+CME: ERROR: 100\n");
|
| 250 |
|
| 251 | }
|
| 252 | g_handle_output(str, strlen(str), Response);
|
| 253 |
|
| 254 | }
|
| 255 |
|
| 256 | void lynq_at_fota_cb(char *input, int input_max_size)
|
| 257 | {
|
| 258 | char arry[20][35]={};
|
| 259 | char *p = NULL;
|
| 260 | int i = 0;
|
| 261 |
|
| 262 | if(input[8] == '0')
|
| 263 | {
|
| 264 | strcpy(g_buffer, input);
|
| 265 | }
|
| 266 | if(g_handle_output != NULL)
|
| 267 | {
|
| 268 | RLOGD("function %s start,input: %s \n", __FUNCTION__, input);
|
| 269 | if(input != NULL)
|
| 270 | {
|
| 271 | while(( p = strsep(&input,",")) != NULL)
|
| 272 | {
|
| 273 | strcpy(arry[i],p);
|
| 274 | i++;
|
| 275 | }
|
| 276 | lynq_at_fota(arry);
|
| 277 | }
|
| 278 | }
|
| 279 |
|
| 280 | }
|
| 281 |
|
| 282 |
|
| 283 | lynq_atsvc_incb lynq_register_fota(lynq_atsvc_outcb out_cb)
|
| 284 | {
|
| 285 | if(out_cb != NULL)
|
| 286 | {
|
| 287 | g_handle_output = out_cb;
|
| 288 | RLOGD("function %s start\n", __FUNCTION__);
|
| 289 | return lynq_at_fota_cb;
|
| 290 | }
|
| 291 | }
|
| 292 |
|
| 293 |
|
| 294 | #ifdef __cplusplus
|
| 295 | }
|
| 296 | #endif
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|