| rjw | 2e8229f | 2022-02-15 21:08:12 +0800 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * Copyright Statement: |
| 3 | * -------------------- |
| 4 | * This software is protected by Copyright and the information contained |
| 5 | * herein is confidential. The software may not be copied and the information |
| 6 | * contained herein may not be used or disclosed except with the written |
| 7 | * permission of MediaTek Inc. (C) 2020 |
| 8 | * |
| 9 | * BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES |
| 10 | * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE") |
| 11 | * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON |
| 12 | * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES, |
| 13 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF |
| 14 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT. |
| 15 | * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE |
| 16 | * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR |
| 17 | * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH |
| 18 | * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO |
| 19 | * NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S |
| 20 | * SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM. |
| 21 | * |
| 22 | * BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE |
| 23 | * LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE, |
| 24 | * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE, |
| 25 | * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO |
| 26 | * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE. |
| 27 | * |
| 28 | * THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE |
| 29 | * WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF |
| 30 | * LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND |
| 31 | * RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER |
| 32 | * THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC). |
| 33 | * |
| 34 | *******************************************************************************/ |
| 35 | |
| 36 | /****************************************************************************** |
| 37 | * Filename: |
| 38 | * --------- |
| 39 | * mcf_cmd.c |
| 40 | * |
| 41 | * Project: |
| 42 | * -------- |
| 43 | * Colgin |
| 44 | * |
| 45 | * Description: |
| 46 | * ------------ |
| 47 | * MD Configuration Framework, opreation command, send by sAP/openWrt |
| 48 | * |
| 49 | *******************************************************************************/ |
| 50 | |
| 51 | /****************************************************************************** |
| 52 | * Includes |
| 53 | ******************************************************************************/ |
| 54 | #include <stdbool.h> |
| 55 | #include <unistd.h> |
| 56 | #include <pthread.h> |
| 57 | #include <sys/time.h> |
| 58 | #include "mipc_msg_host.h" |
| 59 | |
| 60 | /****************************************************************************** |
| 61 | * Macro |
| 62 | ******************************************************************************/ |
| 63 | #define SPECIFIC_MIPC_PORT "/dev/ttyCMIPC9" |
| 64 | |
| 65 | /****************************************************************************** |
| 66 | * Typedefs |
| 67 | ******************************************************************************/ |
| 68 | typedef enum { |
| 69 | MCF_CMD_BIN_TYPE_DEFAULT_BIN = 0, |
| 70 | MCF_CMD_BIN_TYPE_CARRIER_BIN = 1, |
| 71 | MCF_CMD_BIN_TYPE_GENERAL_CARRIER_BIN = 2, |
| 72 | MCF_CMD_BIN_TYPE_MAX |
| 73 | } mcf_cmd_bin_type_enum; |
| 74 | |
| 75 | typedef enum { |
| 76 | MCF_CMD_PATH_TYPE_OTA = 0, |
| 77 | MCF_CMD_PATH_TYPE_RUNTIME = 1, |
| 78 | MCF_CMD_PATH_TYPE_MAX |
| 79 | } mcf_cmd_path_type_enum; |
| 80 | |
| 81 | typedef enum { |
| 82 | MCF_CMD_VARIABLE_ACT_READ_OTA = 0, |
| 83 | MCF_CMD_VARIABLE_ACT_READ_OPOTA = 1, |
| 84 | MCF_CMD_VARIABLE_ACT_WRITE_OTA = 2, |
| 85 | MCF_CMD_VARIABLE_ACT_WRITE_OPOTA = 3, |
| 86 | MCF_CMD_VARIABLE_ACT_MAX |
| 87 | } mcf_cmd_variable_act_enum; |
| 88 | |
| 89 | typedef enum { |
| 90 | MCF_CMD_QUERY_VARIABLE_FORM_PATH = 0, |
| 91 | MCF_CMD_QUERY_VARIABLE_FORM_GID = 1, |
| 92 | }mcf_cmd_query_variable_form_enum; |
| 93 | |
| 94 | typedef enum { |
| 95 | MCF_CMD_IS_TRIGGER_FALSE = 0, |
| 96 | MCF_CMD_IS_TRIGGER_TRUE = 1, |
| 97 | |
| 98 | MCF_CMD_IS_APPEND_OTA = 0, |
| 99 | MCF_CMD_IS_RESET_LID = 1, |
| 100 | |
| 101 | MCF_CMD_IS_UPDATE_WITH_NO_READ_INI = 0, |
| 102 | MCF_CMD_IS_UPDATE_WITH_READ_INI = 1, |
| 103 | |
| 104 | MCF_PARA_IS_MAX = 2, |
| 105 | } mcf_para_is_enum; |
| 106 | |
| 107 | typedef struct { |
| 108 | uint8_t num; |
| 109 | char str[7]; |
| 110 | } mapping_table_t; |
| 111 | |
| 112 | typedef struct { |
| 113 | /* required parameter */ |
| 114 | uint8_t opnum; |
| 115 | uint8_t bin_type; |
| 116 | uint8_t path_folder_idx; |
| 117 | uint8_t appendOTA_resetLID; |
| 118 | /* optional parameter */ |
| 119 | uint8_t is_trigger; |
| 120 | uint8_t is_with; |
| 121 | uint8_t record_id; |
| 122 | uint8_t data_length; |
| 123 | /* internal use variable */ |
| 124 | uint8_t write_flag; |
| 125 | uint8_t real_write_length; |
| 126 | /* required parameter */ |
| 127 | uint32_t gid; |
| 128 | char* file_name; |
| 129 | char* write_value; |
| 130 | /* optional parameter */ |
| 131 | char* lid_str; |
| 132 | char* array_index; |
| 133 | /* internal use variable */ |
| 134 | uint16_t run_cmd_ret; |
| 135 | uint16_t sim_ps_id; |
| 136 | } mcf_cmd_parameter_list_t; |
| 137 | |
| 138 | /****************************************************************************** |
| 139 | * Global variables |
| 140 | ******************************************************************************/ |
| 141 | #define PARA_INT8_ERROE_MAX 0xFF |
| 142 | #define RESULT_SUCCESS 0 |
| 143 | #define WRITE_FLAG 90 |
| 144 | |
| 145 | extern char *optarg; |
| 146 | extern int optind, optopt, opterr; |
| 147 | static mipc_msg_t* msg_req_ptr = NULL; |
| 148 | static mipc_msg_t* msg_cnf_ptr = NULL; |
| 149 | static mcf_cmd_parameter_list_t mc; |
| 150 | |
| 151 | /* NOTE: read and write are same opreation code 10 */ |
| 152 | static mapping_table_t cmd_map[9] = { |
| 153 | {MIPC_SYS_MCF_OP_GET_APPLIED_FILE_PATH, "get"}, |
| 154 | {MIPC_SYS_MCF_OP_DUMP_LID_DATA, "dump"}, |
| 155 | {MIPC_SYS_MCF_OP_SET_FILE_PATH_AND_AUTO_SELECT_BIN, "set"}, |
| 156 | {MIPC_SYS_MCF_OP_UPDATE_OPOTA_FILE, "update"}, |
| 157 | {MIPC_SYS_MCF_OP_QUERY_VARIABLE_VALUE, "read"}, |
| 158 | {MIPC_SYS_MCF_OP_QUERY_VARIABLE_VALUE + WRITE_FLAG, "write"}, |
| 159 | {MIPC_SYS_MCF_OP_ASSIGN_COMBINED_PATH, "merge"}, |
| 160 | {99,"reboot"}, |
| 161 | }; |
| 162 | |
| 163 | static mapping_table_t path_map[2] = { |
| 164 | {MCF_CMD_PATH_TYPE_OTA, "mdota2"}, |
| 165 | {MCF_CMD_PATH_TYPE_RUNTIME, "mdota"} |
| 166 | }; |
| 167 | |
| 168 | #define CMD_MAP_LEN (sizeof(cmd_map)/sizeof(cmd_map[0])) |
| 169 | #define PATH_MAP_LEN (sizeof(path_map)/sizeof(path_map[0])) |
| 170 | |
| 171 | //#define USE_DEBUG_LOG |
| 172 | #ifdef USE_DEBUG_LOG |
| 173 | #define debug_log(format, ...) printf(format, ##__VA_ARGS__) |
| 174 | #else |
| 175 | #define debug_log(format, ...) |
| 176 | #endif |
| 177 | |
| 178 | /****************************************************************************** |
| 179 | * Feature: wait ind callback by linux signal |
| 180 | ******************************************************************************/ |
| 181 | #define USE_MCF_THREAD_WAIT_IND_CB |
| 182 | |
| 183 | #ifdef USE_MCF_THREAD_WAIT_IND_CB |
| 184 | #define WAIT_IND_CB_TIMEOUT_SEC 5 |
| 185 | static pthread_cond_t g_cond; |
| 186 | static pthread_mutex_t g_mutex; |
| 187 | static bool g_ind_cb_done = false; |
| 188 | |
| 189 | static void mcf_thread_wait_ind_cb(void) |
| 190 | { |
| 191 | struct timeval now_time; |
| 192 | struct timespec out_time; |
| 193 | |
| 194 | if (g_ind_cb_done == false) { |
| 195 | pthread_cond_init(&g_cond, NULL); |
| 196 | pthread_mutex_init(&g_mutex, NULL); |
| 197 | |
| 198 | gettimeofday(&now_time, NULL); |
| 199 | out_time.tv_sec = now_time.tv_sec + WAIT_IND_CB_TIMEOUT_SEC; |
| 200 | out_time.tv_nsec = now_time.tv_usec * 1000; |
| 201 | |
| 202 | debug_log("wait mcf ind cb\n"); |
| 203 | pthread_mutex_lock(&g_mutex); |
| 204 | pthread_cond_timedwait(&g_cond, &g_mutex, &out_time); |
| 205 | pthread_mutex_unlock(&g_mutex); |
| 206 | debug_log("wait done\n"); |
| 207 | |
| 208 | pthread_cond_destroy(&g_cond); |
| 209 | pthread_mutex_destroy(&g_mutex); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | static void mcf_thread_send_signal(void) |
| 214 | { |
| 215 | if (g_ind_cb_done == false) { |
| 216 | pthread_mutex_lock(&g_mutex); |
| 217 | pthread_cond_signal(&g_cond); |
| 218 | pthread_mutex_unlock(&g_mutex); |
| 219 | g_ind_cb_done = true; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | #else |
| 224 | #define mcf_thread_wait_ind_cb() |
| 225 | #define mcf_thread_send_signal() |
| 226 | #endif /* THREAD_WAIT_MCF_IND_CB */ |
| 227 | |
| 228 | /****************************************************************************** |
| 229 | * Functions |
| 230 | ******************************************************************************/ |
| 231 | static void printf_help(void) |
| 232 | { |
| 233 | char help_text[] = {"Usage:\n" |
| 234 | " mcf_cmd <-o get> <-b bin_type>\n" |
| 235 | " mcf_cmd <-o dump> [-d lid1,lid2,...,lid32]\n" |
| 236 | " mcf_cmd <-o set> <-b bin_type> <-p path_folder> <-f file_name> <-r appendOTA_resetLID> [-t trigger_dsbp] [-s sim_id]\n" |
| 237 | " mcf_cmd <-o update> [-w with_ini]\n" |
| 238 | " mcf_cmd <-o read> <-b bin_type> <-g GID> [-i record_id] [-y array_index] [-l length]\n" |
| 239 | " mcf_cmd <-o write> <-b bin_type> <-g GID> [-i record_id] [-y array_index] [-l length] <-v write_value>\n" |
| 240 | " mcf_cmd <-o merge> <-b bin_type> <-p path_folder> <-f file_name>\n" |
| 241 | " <> is required parameter; [] is optional parameter, notice its default value if not set\n" |
| 242 | " -o, operation (str): get/dump/set/update/read/write/merge\n" |
| 243 | " -b, bin type: 0:OTA, 1:OTA by OP, 2:general OTA by OP\n" |
| 244 | " -d, LID list (str): use \",\" to split, max num is 32; default dump all\n" |
| 245 | " -p, path folder (str): \"mdota\"/\"mdota2\"\n" |
| 246 | " -f, OTA file name (str)\n" |
| 247 | " -r, appendOTA_resetLID: Valid after running merge cmd, 0:append OTA file, 1:reset LIDs\n" |
| 248 | " -t, trigger dsbp: 0:not trigger, 1:trigger; default 0\n" |
| 249 | " -w, update with ini file: 0:no read ini, 1:read ini; default 0\n" |
| 250 | " -i, NVRAM item record id: default 1\n" |
| 251 | " -g, GID number\n" |
| 252 | " -y, array index (str): use \",\" to split; default \"\"\n" |
| 253 | " -l, data length\n" |
| 254 | " -v, write value (str): hex string, use \"3412\" for 0x1234\n" |
| 255 | " -s, send cmd by sim id, support all cmd: 0 for sim1, 1 for sim2, optional, defatul 0\n" |
| 256 | " note: if parameter is string, its \"\" can be omitted\n" |
| 257 | }; |
| 258 | printf("%s", help_text); |
| 259 | } |
| 260 | |
| 261 | static void mcf_ind_cb(mipc_msg_t *msg_ptr, void *priv_ptr) |
| 262 | { |
| 263 | uint32_t ret; |
| 264 | |
| 265 | printf("MCF cmd CB type=%u result=%u\n", mipc_msg_get_val_uint8(msg_ptr, MIPC_SYS_MCF_IND_T_TYPE, 0xff), |
| 266 | ret = mipc_msg_get_val_uint8(msg_ptr, MIPC_SYS_MCF_IND_T_RESULT, 0xff)); |
| 267 | |
| 268 | if ((mc.opnum == MIPC_SYS_MCF_OP_DUMP_LID_DATA) && (ret == RESULT_SUCCESS)) { |
| 269 | mc.run_cmd_ret = RESULT_SUCCESS; |
| 270 | } |
| 271 | |
| 272 | mcf_thread_send_signal(); |
| 273 | } |
| 274 | |
| 275 | static void mcf_mipc_init(void) |
| 276 | { |
| 277 | #ifdef SPECIFIC_MIPC_PORT |
| 278 | SETCOM(SPECIFIC_MIPC_PORT); |
| 279 | printf("MCF cmd SETCOM %s\n", SPECIFIC_MIPC_PORT); |
| 280 | #endif |
| 281 | |
| 282 | mipc_init("mcf_cmd"); |
| 283 | mipc_msg_register_ind(mc.sim_ps_id, MIPC_SYS_MCF_IND, mcf_ind_cb, NULL); |
| 284 | } |
| 285 | |
| 286 | static void mcf_mipc_deinit(void) |
| 287 | { |
| 288 | mipc_msg_deinit(msg_req_ptr); |
| 289 | mipc_msg_deinit(msg_cnf_ptr); |
| 290 | mipc_deinit(); |
| 291 | } |
| 292 | |
| 293 | static char* mcf_get_map_str(uint8_t num, mapping_table_t *map, uint32_t map_len) |
| 294 | { |
| 295 | static char* null_str = ""; |
| 296 | uint32_t i = 0; |
| 297 | |
| 298 | for (i=0;i<map_len;i++) { |
| 299 | if (num == (map+i)->num) { |
| 300 | return (map+i)->str; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | return null_str; |
| 305 | } |
| 306 | |
| 307 | static uint8_t mcf_get_map_num(char* str, mapping_table_t *map, uint32_t map_len) |
| 308 | { |
| 309 | uint32_t i = 0; |
| 310 | |
| 311 | for (i=0;i<map_len;i++) { |
| 312 | if (strcmp(str, (map+i)->str) == 0) { |
| 313 | if (strcmp(str, "write") == 0) { |
| 314 | mc.write_flag = WRITE_FLAG; |
| 315 | return (map+i)->num - mc.write_flag; |
| 316 | } else { |
| 317 | return (map+i)->num; |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | return PARA_INT8_ERROE_MAX; |
| 323 | } |
| 324 | |
| 325 | static void log_op_error_parameter(uint8_t num) |
| 326 | { |
| 327 | printf("MCF op=\"%s\" error parameter, CHECK!\nRun mcf_cmd to see the format!", mcf_get_map_str(num, cmd_map, CMD_MAP_LEN)); |
| 328 | } |
| 329 | |
| 330 | static void log_mipc_result_fail(uint32_t result) |
| 331 | { |
| 332 | printf("MCF cmd FAIL, mipc result=%u\n", result); |
| 333 | } |
| 334 | |
| 335 | static void mcf_cmd_parameters_init(void) |
| 336 | { |
| 337 | /* required parameter, will check them */ |
| 338 | mc.opnum = PARA_INT8_ERROE_MAX; |
| 339 | mc.bin_type = PARA_INT8_ERROE_MAX; |
| 340 | mc.path_folder_idx = PARA_INT8_ERROE_MAX; |
| 341 | mc.appendOTA_resetLID = PARA_INT8_ERROE_MAX; |
| 342 | mc.gid = 0; |
| 343 | mc.file_name = NULL; |
| 344 | mc.write_value = NULL; |
| 345 | /* optional parameter, set default value */ |
| 346 | mc.is_trigger = 0; |
| 347 | mc.is_with = 0; |
| 348 | mc.record_id = 1; |
| 349 | mc.data_length = 0; |
| 350 | mc.lid_str = ""; |
| 351 | mc.array_index = ""; |
| 352 | /* internal use variable */ |
| 353 | mc.write_flag = 0; |
| 354 | mc.real_write_length = 0; |
| 355 | mc.run_cmd_ret = PARA_INT8_ERROE_MAX; // linux shell only get 0~255 |
| 356 | mc.sim_ps_id = MIPC_MSG_PS0; // set default as MIPC_MSG_PS0 |
| 357 | } |
| 358 | |
| 359 | static void mcf_cmd_hex_string_to_bytes(char *input_string) |
| 360 | { |
| 361 | uint32_t length = strlen(input_string); |
| 362 | uint8_t temp[2]; |
| 363 | uint32_t i,j; |
| 364 | |
| 365 | if (length%2 != 0) { |
| 366 | printf("write_value str len need be even\n"); |
| 367 | return; |
| 368 | } |
| 369 | |
| 370 | char* pdata = malloc(length/2); |
| 371 | if (pdata == NULL) return; |
| 372 | |
| 373 | for (i=0;i<length/2;i++) { |
| 374 | temp[0] = input_string[i*2]; |
| 375 | temp[1] = input_string[i*2+1]; |
| 376 | |
| 377 | for (j=0; j<2; j++) { |
| 378 | if (temp[j] >= '0' && temp[j] <= '9') { |
| 379 | temp[j] = temp[j] - '0'; |
| 380 | } else if (temp[j] >= 'a' && temp[j] <= 'f') { |
| 381 | temp[j] = temp[j] - 'a' + 10; |
| 382 | } else if (temp[j] >= 'A' && temp[j] <= 'F') { |
| 383 | temp[j] = temp[j] - 'A' + 10; |
| 384 | } else { |
| 385 | free(pdata); |
| 386 | return; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | pdata[i] = (temp[0]<<4) | temp[1]; |
| 391 | } |
| 392 | |
| 393 | if (mc.write_value != NULL) free(mc.write_value); |
| 394 | mc.write_value = pdata; |
| 395 | mc.real_write_length = length/2; |
| 396 | } |
| 397 | |
| 398 | static void mcf_cmd_parameters_parser(int argc, char *argv[]) |
| 399 | { |
| 400 | uint32_t ch; |
| 401 | uint16_t sim_id; |
| 402 | char* opstr = ""; |
| 403 | |
| 404 | if (argc == 1) { |
| 405 | printf_help(); |
| 406 | mc.run_cmd_ret = RESULT_SUCCESS; |
| 407 | return; |
| 408 | } |
| 409 | |
| 410 | while ((ch = getopt(argc, argv, "o:b:d:p:f:t:r:w:g:i:y:l:v:s:")) != -1) { |
| 411 | switch(ch) { |
| 412 | case 'o': |
| 413 | mc.opnum = mcf_get_map_num(optarg, cmd_map, CMD_MAP_LEN); |
| 414 | opstr = optarg; |
| 415 | break; |
| 416 | case 'b': |
| 417 | mc.bin_type = atoi(optarg); |
| 418 | break; |
| 419 | case 'd': |
| 420 | mc.lid_str = optarg; |
| 421 | break; |
| 422 | case 'p': |
| 423 | mc.path_folder_idx = mcf_get_map_num(optarg, path_map, PATH_MAP_LEN); |
| 424 | break; |
| 425 | case 'f': |
| 426 | mc.file_name = optarg; |
| 427 | break; |
| 428 | case 't': |
| 429 | mc.is_trigger = atoi(optarg); |
| 430 | break; |
| 431 | case 'r': |
| 432 | mc.appendOTA_resetLID = atoi(optarg); |
| 433 | break; |
| 434 | case 'w': |
| 435 | mc.is_with = atoi(optarg); |
| 436 | break; |
| 437 | case 'g': |
| 438 | mc.gid = atoi(optarg); |
| 439 | break; |
| 440 | case 'i': |
| 441 | mc.record_id = atoi(optarg); |
| 442 | break; |
| 443 | case 'y': |
| 444 | mc.array_index = optarg; |
| 445 | break; |
| 446 | case 'l': |
| 447 | mc.data_length = atoi(optarg); |
| 448 | break; |
| 449 | case 'v': |
| 450 | mcf_cmd_hex_string_to_bytes(optarg); |
| 451 | break; |
| 452 | case 's': |
| 453 | sim_id = atoi(optarg); |
| 454 | if (sim_id == 0xFF) { |
| 455 | mc.sim_ps_id = MIPC_MSG_ALL; |
| 456 | } else if (sim_id <= 7) { |
| 457 | mc.sim_ps_id = (mipc_msg_sim_ps_id_enum)(1 << sim_id); |
| 458 | } |
| 459 | break; |
| 460 | default: |
| 461 | break; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | if (mc.opnum == PARA_INT8_ERROE_MAX) { |
| 466 | printf("MCF unsupport: -o \"%s\"\n", opstr); |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | static void mcf_cmd_parameters_free(void) |
| 471 | { |
| 472 | if(mc.write_value != NULL) |
| 473 | free(mc.write_value); |
| 474 | } |
| 475 | |
| 476 | /******************************************************************************* |
| 477 | * origin cmd: AT+EMCFC=4,<config_type> |
| 478 | * response: +EMCFC:4,<config_type>,<path_type>,<config1 str> |
| 479 | * ind cb: none |
| 480 | * mcf cmd: mcf_cmd <-o get> <-b bin_type> |
| 481 | *******************************************************************************/ |
| 482 | static void mcf_cmd_op_get_applied_file_path(int argc, char *argv[]) |
| 483 | { |
| 484 | uint32_t mipc_ret = 0; |
| 485 | |
| 486 | if (mc.bin_type >= MCF_CMD_BIN_TYPE_MAX) { |
| 487 | log_op_error_parameter(MIPC_SYS_MCF_OP_GET_APPLIED_FILE_PATH); |
| 488 | return; |
| 489 | } |
| 490 | |
| 491 | msg_req_ptr = mipc_msg_init(MIPC_SYS_MCF_REQ, mc.sim_ps_id); |
| 492 | mipc_msg_add_tlv_uint8(msg_req_ptr, MIPC_SYS_MCF_REQ_T_OP, MIPC_SYS_MCF_OP_GET_APPLIED_FILE_PATH); |
| 493 | mipc_msg_add_tlv_uint8(msg_req_ptr, MIPC_SYS_MCF_REQ_T_CONFIG_TYPE, mc.bin_type); |
| 494 | msg_cnf_ptr = mipc_msg_sync(msg_req_ptr); |
| 495 | |
| 496 | mipc_ret = mipc_msg_get_val_uint32(msg_cnf_ptr, MIPC_T_RESULT, MIPC_RESULT_FAILURE); |
| 497 | |
| 498 | if (mipc_ret == MIPC_RESULT_SUCCESS) { |
| 499 | printf("MCF cmd SUCCESS op=\"%s\" bin_type=%u path_folder=\"%s\" config_file=\"%s\"\n", |
| 500 | mcf_get_map_str(mipc_msg_get_val_uint8(msg_cnf_ptr, MIPC_SYS_MCF_CNF_T_OP, 0xff), cmd_map, CMD_MAP_LEN), |
| 501 | mipc_msg_get_val_uint8(msg_cnf_ptr, MIPC_SYS_MCF_CNF_T_CONFIG_TYPE, 0xff), |
| 502 | mcf_get_map_str(mipc_msg_get_val_uint8(msg_cnf_ptr, MIPC_SYS_MCF_CNF_T_PATH_TYPE, 0xff), path_map, PATH_MAP_LEN), |
| 503 | (char *)mipc_msg_get_val_ptr(msg_cnf_ptr, MIPC_SYS_MCF_CNF_T_CONFIG1, NULL)); |
| 504 | mc.run_cmd_ret = RESULT_SUCCESS; |
| 505 | } else { |
| 506 | log_mipc_result_fail(mipc_ret); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | /******************************************************************************* |
| 511 | * origin cmd: AT+EMCFC=5 [lid1,lid2,...,lid32] |
| 512 | * response: +EMCFC:5,<MCF result> |
| 513 | * ind cb: +EMCFRPT:<type>,<MCF result> |
| 514 | * mcf cmd: mcf_cmd <-o dump> [-d lid1,lid2,...,lid32] |
| 515 | *******************************************************************************/ |
| 516 | static void mcf_cmd_op_dump_lid_data(int argc, char *argv[]) |
| 517 | { |
| 518 | uint32_t mipc_ret = 0, mcf_ret = 0xffffffff; |
| 519 | |
| 520 | msg_req_ptr = mipc_msg_init(MIPC_SYS_MCF_REQ, mc.sim_ps_id); |
| 521 | mipc_msg_add_tlv_uint8(msg_req_ptr, MIPC_SYS_MCF_REQ_T_OP, MIPC_SYS_MCF_OP_DUMP_LID_DATA); |
| 522 | if (strcmp(mc.lid_str, "") != 0) { |
| 523 | mipc_msg_add_tlv(msg_req_ptr, MIPC_SYS_MCF_REQ_T_DUMP_LIDS, strlen(mc.lid_str) + 1, mc.lid_str); |
| 524 | } |
| 525 | msg_cnf_ptr = mipc_msg_sync(msg_req_ptr); |
| 526 | |
| 527 | mipc_ret = mipc_msg_get_val_uint32(msg_cnf_ptr, MIPC_T_RESULT, MIPC_RESULT_FAILURE); |
| 528 | if (mipc_ret == MIPC_RESULT_SUCCESS) { |
| 529 | printf("MCF cmd SUCCESS op=\"%s\" mcf_result=%u\n", |
| 530 | mcf_get_map_str(mipc_msg_get_val_uint8(msg_cnf_ptr, MIPC_SYS_MCF_CNF_T_OP, 0xff), cmd_map, CMD_MAP_LEN), |
| 531 | mcf_ret = mipc_msg_get_val_uint32(msg_cnf_ptr, MIPC_SYS_MCF_CNF_T_MCF_RESULT, 0xffffffff)); |
| 532 | } else { |
| 533 | log_mipc_result_fail(mipc_ret); |
| 534 | } |
| 535 | |
| 536 | if(mcf_ret == RESULT_SUCCESS) { |
| 537 | mcf_thread_wait_ind_cb(); |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | /******************************************************************************* |
| 542 | * origin cmd: AT+EMCFC=6,<config_type>,<path_type>,<config1 str>,<trigger_dsbp>,<is_reset> |
| 543 | * response: +EMCFC:6,<MCF result>,<DSBP result> |
| 544 | * ind cb: (if trigger_dsbp == 1) +EMCFRPT:<type>,<DSBP result> |
| 545 | * mcf cmd: mcf_cmd <-o set> <-b bin_type> <-p path_folder> <-f file_name> <-r append_resetLID> [-t trigger_dsbp] |
| 546 | *******************************************************************************/ |
| 547 | static void mcf_cmd_op_set_file_path_and_auto_select_bin(int argc, char *argv[]) |
| 548 | { |
| 549 | uint32_t mipc_ret = 0, mcf_ret = 0xffffffff, dsbp_ret = 0xffffffff; |
| 550 | |
| 551 | if ((mc.bin_type >= MCF_CMD_BIN_TYPE_MAX) || (mc.path_folder_idx >= MCF_CMD_PATH_TYPE_MAX) || (mc.file_name == NULL ) || |
| 552 | (mc.is_trigger >= MCF_PARA_IS_MAX )|| (mc.appendOTA_resetLID >= MCF_PARA_IS_MAX)) { |
| 553 | log_op_error_parameter(MIPC_SYS_MCF_OP_SET_FILE_PATH_AND_AUTO_SELECT_BIN); |
| 554 | return; |
| 555 | } |
| 556 | |
| 557 | msg_req_ptr = mipc_msg_init(MIPC_SYS_MCF_REQ, mc.sim_ps_id); |
| 558 | mipc_msg_add_tlv_uint8(msg_req_ptr, MIPC_SYS_MCF_REQ_T_OP, MIPC_SYS_MCF_OP_SET_FILE_PATH_AND_AUTO_SELECT_BIN); |
| 559 | mipc_msg_add_tlv_uint8(msg_req_ptr, MIPC_SYS_MCF_REQ_T_CONFIG_TYPE, mc.bin_type); |
| 560 | mipc_msg_add_tlv_uint8(msg_req_ptr, MIPC_SYS_MCF_REQ_T_PATH_TYPE, mc.path_folder_idx); |
| 561 | mipc_msg_add_tlv(msg_req_ptr, MIPC_SYS_MCF_REQ_T_CONFIG1, strlen(mc.file_name)+1, mc.file_name); |
| 562 | mipc_msg_add_tlv_uint8(msg_req_ptr, MIPC_SYS_MCF_REQ_T_TRIGGER_DSBP, mc.is_trigger); |
| 563 | mipc_msg_add_tlv_uint8(msg_req_ptr, MIPC_SYS_MCF_REQ_T_IS_RESET, mc.appendOTA_resetLID); |
| 564 | msg_cnf_ptr = mipc_msg_sync(msg_req_ptr); |
| 565 | |
| 566 | mipc_ret = mipc_msg_get_val_uint32(msg_cnf_ptr, MIPC_T_RESULT, MIPC_RESULT_FAILURE); |
| 567 | if (mipc_ret == MIPC_RESULT_SUCCESS) { |
| 568 | printf("MCF cmd SUCCESS op=\"%s\" mcf_result=%u dsbp_result=%u\n", |
| 569 | mcf_get_map_str(mipc_msg_get_val_uint8(msg_cnf_ptr, MIPC_SYS_MCF_CNF_T_OP, 0xff), cmd_map, CMD_MAP_LEN), |
| 570 | mcf_ret = mipc_msg_get_val_uint32(msg_cnf_ptr, MIPC_SYS_MCF_CNF_T_MCF_RESULT, 0xffffffff), |
| 571 | dsbp_ret = mipc_msg_get_val_uint32(msg_cnf_ptr, MIPC_SYS_MCF_CNF_T_DSBP_RESULT, 0xffffffff)); |
| 572 | } else { |
| 573 | log_mipc_result_fail(mipc_ret); |
| 574 | } |
| 575 | |
| 576 | if (mcf_ret == RESULT_SUCCESS && dsbp_ret == RESULT_SUCCESS) { |
| 577 | mc.run_cmd_ret = RESULT_SUCCESS; |
| 578 | if(mc.is_trigger == MCF_CMD_IS_TRIGGER_TRUE) { |
| 579 | mcf_thread_wait_ind_cb(); |
| 580 | } |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | /******************************************************************************* |
| 585 | * cmd: AT+EMCFC=7,<action> |
| 586 | * response: +EMCFC:7,<MCF result> |
| 587 | * ind cb: +EMCFRPT:<type>,<result> |
| 588 | * mcf cmd: mcf_cmd <-o update> [-w with_ini] |
| 589 | *******************************************************************************/ |
| 590 | static void mcf_cmd_op_update_opota_file(int argc, char *argv[]) |
| 591 | { |
| 592 | uint32_t mipc_ret = 0, mcf_ret = 0xffffffff; |
| 593 | |
| 594 | if (mc.is_with >= MCF_PARA_IS_MAX) { |
| 595 | log_op_error_parameter(MIPC_SYS_MCF_OP_UPDATE_OPOTA_FILE); |
| 596 | return; |
| 597 | } |
| 598 | |
| 599 | msg_req_ptr = mipc_msg_init(MIPC_SYS_MCF_REQ, mc.sim_ps_id); |
| 600 | mipc_msg_add_tlv_uint8(msg_req_ptr, MIPC_SYS_MCF_REQ_T_OP, MIPC_SYS_MCF_OP_UPDATE_OPOTA_FILE); |
| 601 | mipc_msg_add_tlv_uint8(msg_req_ptr, MIPC_SYS_MCF_REQ_T_ACTION, mc.is_with); |
| 602 | msg_cnf_ptr = mipc_msg_sync(msg_req_ptr); |
| 603 | |
| 604 | mipc_ret = mipc_msg_get_val_uint32(msg_cnf_ptr, MIPC_T_RESULT, MIPC_RESULT_FAILURE); |
| 605 | if (mipc_ret == MIPC_RESULT_SUCCESS) { |
| 606 | printf("MCF cmd SUCCESS op=\"%s\" mcf_result=%u\n", |
| 607 | mcf_get_map_str(mipc_msg_get_val_uint8(msg_cnf_ptr, MIPC_SYS_MCF_CNF_T_OP, 0xff), cmd_map, CMD_MAP_LEN), |
| 608 | mcf_ret = mipc_msg_get_val_uint32(msg_cnf_ptr, MIPC_SYS_MCF_CNF_T_MCF_RESULT, 0xffffffff)); |
| 609 | } else { |
| 610 | log_mipc_result_fail(mipc_ret); |
| 611 | } |
| 612 | |
| 613 | if (mcf_ret == RESULT_SUCCESS) { |
| 614 | mc.run_cmd_ret = RESULT_SUCCESS; |
| 615 | mcf_thread_wait_ind_cb(); |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | /******************************************************************************* |
| 620 | * cmd: AT+EMCFC=10,<format>,<action>,<record ID>,<number>,<config str>[,<length>],[,<value>]] |
| 621 | * response: +EMCFC:10,<format>,<action>,<mcf result>,<length>,<value> |
| 622 | * ind cb: none |
| 623 | * mcf_cmd <-o read> <-b bin_type> <-g GID> [-i record_id] [-y array_index] [-l length] |
| 624 | * mcf_cmd <-o write> <-b bin_type> <-g GID> [-i record_id] [-y array_index] [-l length] <-v write_value> |
| 625 | *******************************************************************************/ |
| 626 | static void mcf_cmd_op_query_variable_value(int argc, char *argv[]) |
| 627 | { |
| 628 | uint32_t mipc_ret = 0, mcf_ret = 0xffffffff; |
| 629 | uint8_t action, ret_len, i; |
| 630 | char *ret_str; |
| 631 | |
| 632 | if ((mc.bin_type >= MCF_CMD_BIN_TYPE_MAX) || (mc.gid == 0) || (mc.write_flag == WRITE_FLAG && mc.write_value == NULL)) { |
| 633 | log_op_error_parameter(MIPC_SYS_MCF_OP_QUERY_VARIABLE_VALUE); |
| 634 | return; |
| 635 | } |
| 636 | |
| 637 | if (mc.write_flag != WRITE_FLAG) { |
| 638 | action = (mc.bin_type == MCF_CMD_BIN_TYPE_DEFAULT_BIN) ? MCF_CMD_VARIABLE_ACT_READ_OTA : MCF_CMD_VARIABLE_ACT_READ_OPOTA; |
| 639 | } else { |
| 640 | action = (mc.bin_type == MCF_CMD_BIN_TYPE_DEFAULT_BIN) ? MCF_CMD_VARIABLE_ACT_WRITE_OTA : MCF_CMD_VARIABLE_ACT_WRITE_OPOTA; |
| 641 | } |
| 642 | |
| 643 | msg_req_ptr = mipc_msg_init(MIPC_SYS_MCF_REQ, mc.sim_ps_id); |
| 644 | mipc_msg_add_tlv_uint8(msg_req_ptr, MIPC_SYS_MCF_REQ_T_OP, MIPC_SYS_MCF_OP_QUERY_VARIABLE_VALUE); |
| 645 | mipc_msg_add_tlv_uint8(msg_req_ptr, MIPC_SYS_MCF_REQ_T_FORMAT, MCF_CMD_QUERY_VARIABLE_FORM_GID); |
| 646 | mipc_msg_add_tlv_uint8(msg_req_ptr, MIPC_SYS_MCF_REQ_T_ACTION, action); |
| 647 | mipc_msg_add_tlv_uint32(msg_req_ptr, MIPC_SYS_MCF_REQ_T_NUM, mc.gid); |
| 648 | mipc_msg_add_tlv_uint16(msg_req_ptr, MIPC_SYS_MCF_REQ_T_REC_ID, mc.record_id); |
| 649 | mipc_msg_add_tlv(msg_req_ptr, MIPC_SYS_MCF_REQ_T_CONFIG, strlen(mc.array_index)+1, mc.array_index); |
| 650 | mipc_msg_add_tlv_uint8(msg_req_ptr, MIPC_SYS_MCF_REQ_T_LEN, mc.data_length); |
| 651 | if (mc.write_flag == WRITE_FLAG) { |
| 652 | mipc_msg_add_tlv(msg_req_ptr, MIPC_SYS_MCF_REQ_T_VALUE, mc.real_write_length, mc.write_value); |
| 653 | } |
| 654 | |
| 655 | msg_cnf_ptr = mipc_msg_sync(msg_req_ptr); |
| 656 | mipc_ret = mipc_msg_get_val_uint32(msg_cnf_ptr, MIPC_T_RESULT, MIPC_RESULT_FAILURE); |
| 657 | if (mipc_ret == MIPC_RESULT_SUCCESS) { |
| 658 | printf("MCF cmd SUCCESS op=\"%s\" mcf_result=%u bin_type=%u", |
| 659 | mcf_get_map_str(mipc_msg_get_val_uint8(msg_cnf_ptr, MIPC_SYS_MCF_CNF_T_OP, 0xff)+mc.write_flag, cmd_map, CMD_MAP_LEN), |
| 660 | mcf_ret = mipc_msg_get_val_uint32(msg_cnf_ptr, MIPC_SYS_MCF_CNF_T_MCF_RESULT, 0xffffffff), |
| 661 | (mipc_msg_get_val_uint8(msg_cnf_ptr, MIPC_SYS_MCF_CNF_T_ACTION, 0xff)%2 == 0) ? MCF_CMD_BIN_TYPE_DEFAULT_BIN : MCF_CMD_BIN_TYPE_CARRIER_BIN); |
| 662 | if (mcf_ret == RESULT_SUCCESS) { |
| 663 | mc.run_cmd_ret = RESULT_SUCCESS; |
| 664 | ret_len = mipc_msg_get_val_uint8(msg_cnf_ptr, MIPC_SYS_MCF_CNF_T_LEN, 0xff); |
| 665 | printf(" length=%u", ret_len); |
| 666 | if (mc.write_flag != WRITE_FLAG) { |
| 667 | ret_str = (char*)mipc_msg_get_val_ptr(msg_cnf_ptr, MIPC_SYS_MCF_CNF_T_VALUE, NULL); |
| 668 | printf(" value=\""); |
| 669 | for (i=0;i<ret_len;i++) { |
| 670 | printf("%02X", *(ret_str + i)); |
| 671 | } |
| 672 | printf("\""); |
| 673 | } |
| 674 | } |
| 675 | printf("\n"); |
| 676 | } else { |
| 677 | log_mipc_result_fail(mipc_ret); |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | /******************************************************************************* |
| 682 | * cmd: AT+EMCFC=11,<config_type>,<path_type>,<config1 str> |
| 683 | * response: +EMCFC:11,<MCF result> |
| 684 | * ind cb: none |
| 685 | * mcf cmd: mcf_cmd <-o merge> <-b bin_type> <-p path_folder> <-f file_name> |
| 686 | *******************************************************************************/ |
| 687 | static void mcf_cmd_op_assign_combined_path(int argc, char *argv[]) |
| 688 | { |
| 689 | uint32_t mipc_ret = 0, mcf_ret = 0xffffffff; |
| 690 | |
| 691 | if ((mc.bin_type >= MCF_CMD_BIN_TYPE_MAX) || (mc.path_folder_idx >= MCF_CMD_PATH_TYPE_MAX) || (mc.file_name == NULL)) { |
| 692 | log_op_error_parameter(MIPC_SYS_MCF_OP_ASSIGN_COMBINED_PATH); |
| 693 | return; |
| 694 | } |
| 695 | |
| 696 | msg_req_ptr = mipc_msg_init(MIPC_SYS_MCF_REQ, mc.sim_ps_id); |
| 697 | mipc_msg_add_tlv_uint8(msg_req_ptr, MIPC_SYS_MCF_REQ_T_OP, MIPC_SYS_MCF_OP_ASSIGN_COMBINED_PATH); |
| 698 | mipc_msg_add_tlv_uint8(msg_req_ptr, MIPC_SYS_MCF_REQ_T_CONFIG_TYPE, mc.bin_type); |
| 699 | mipc_msg_add_tlv_uint8(msg_req_ptr, MIPC_SYS_MCF_REQ_T_PATH_TYPE, mc.path_folder_idx); |
| 700 | mipc_msg_add_tlv(msg_req_ptr, MIPC_SYS_MCF_REQ_T_CONFIG1, strlen(mc.file_name)+1, mc.file_name); |
| 701 | msg_cnf_ptr = mipc_msg_sync(msg_req_ptr); |
| 702 | |
| 703 | mipc_ret = mipc_msg_get_val_uint32(msg_cnf_ptr, MIPC_T_RESULT, MIPC_RESULT_FAILURE); |
| 704 | if (mipc_ret == MIPC_RESULT_SUCCESS) { |
| 705 | printf("MCF cmd SUCCESS op=\"%s\" mcf_result=%u\n", |
| 706 | mcf_get_map_str(mipc_msg_get_val_uint8(msg_cnf_ptr, MIPC_SYS_MCF_CNF_T_OP, 0xff), cmd_map, CMD_MAP_LEN), |
| 707 | mcf_ret = mipc_msg_get_val_uint32(msg_cnf_ptr, MIPC_SYS_MCF_CNF_T_MCF_RESULT, 0xffffffff)); |
| 708 | if (mcf_ret == RESULT_SUCCESS) { |
| 709 | mc.run_cmd_ret = RESULT_SUCCESS; |
| 710 | } |
| 711 | } else { |
| 712 | log_mipc_result_fail(mipc_ret); |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | static void mcf_system_reboot(void) |
| 717 | { |
| 718 | printf("MCF system reboot\n"); |
| 719 | system("reboot"); |
| 720 | } |
| 721 | |
| 722 | int main(int argc, char *argv[]) |
| 723 | { |
| 724 | mcf_cmd_parameters_init(); |
| 725 | mcf_cmd_parameters_parser(argc, argv); |
| 726 | if (mc.opnum != PARA_INT8_ERROE_MAX) |
| 727 | { |
| 728 | mcf_mipc_init(); |
| 729 | switch (mc.opnum) { |
| 730 | case MIPC_SYS_MCF_OP_GET_APPLIED_FILE_PATH: |
| 731 | mcf_cmd_op_get_applied_file_path(argc, argv); |
| 732 | break; |
| 733 | case MIPC_SYS_MCF_OP_DUMP_LID_DATA: |
| 734 | mcf_cmd_op_dump_lid_data(argc, argv); |
| 735 | break; |
| 736 | case MIPC_SYS_MCF_OP_SET_FILE_PATH_AND_AUTO_SELECT_BIN: |
| 737 | mcf_cmd_op_set_file_path_and_auto_select_bin(argc, argv); |
| 738 | break; |
| 739 | case MIPC_SYS_MCF_OP_UPDATE_OPOTA_FILE: |
| 740 | mcf_cmd_op_update_opota_file(argc, argv); |
| 741 | break; |
| 742 | case MIPC_SYS_MCF_OP_QUERY_VARIABLE_VALUE: |
| 743 | mcf_cmd_op_query_variable_value(argc, argv); |
| 744 | break; |
| 745 | case MIPC_SYS_MCF_OP_ASSIGN_COMBINED_PATH: |
| 746 | mcf_cmd_op_assign_combined_path(argc, argv); |
| 747 | break; |
| 748 | case 99: |
| 749 | mcf_system_reboot(); |
| 750 | default: |
| 751 | break; |
| 752 | } |
| 753 | mcf_mipc_deinit(); |
| 754 | } |
| 755 | mcf_cmd_parameters_free(); |
| 756 | return mc.run_cmd_ret; |
| 757 | } |
| 758 | |