blob: bea51f77115a89b02e14b023b74a01fa6bc1f480 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/* Copyright Statement:
2 *
3 * This software/firmware and related documentation ("MediaTek Software") are
4 * protected under relevant copyright laws. The information contained herein is
5 * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
6 * the prior written permission of MediaTek inc. and/or its licensors, any
7 * reproduction, modification, use or disclosure of MediaTek Software, and
8 * information contained herein, in whole or in part, shall be strictly
9 * prohibited.
10 *
11 * MediaTek Inc. (C) 2016~2017. All rights reserved.
12 *
13 * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
14 * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
15 * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
16 * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
17 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
19 * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
20 * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
21 * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
22 * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
23 * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
24 * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
25 * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
26 * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
27 * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
28 * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
29 * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
30 * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
31 * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
32 *
33 * The following software/firmware and/or related documentation ("MediaTek
34 * Software") have been modified by MediaTek Inc. All revisions are subject to
35 * any receiver's applicable license agreements with MediaTek Inc.
36 */
37
38//- vim: set ts=4 sts=4 sw=4 et: --------------------------------------------
39#include <errno.h>
40#include "boots_country.h"
41
42#define LOG_TAG "boots_country"
43
44//---------------------------------------------------------------------------
45static pkt_list_s *mtk_boots_country_get_limit(char *cmd[], size_t len, int index);
46static pkt_list_s *mtk_boots_country_set_limit(char *cmd[], size_t len, int index);
47static pkt_list_s *mtk_boots_country_dump_limit(char *cmd[], size_t len, int index);
48static pkt_list_s *mtk_boots_country_create_limit_table(char *cmd[], size_t len, int index);
49static pkt_list_s *mtk_boots_country_get_srs(char *cmd[], size_t len, int index);
50static pkt_list_s *mtk_boots_country_set_srs(char *cmd[], size_t len, int index);
51static pkt_list_s *mtk_boots_country_dump_srs(char *cmd[], size_t len, int index);
52
53//---------------------------------------------------------------------------
54#define USAGE_COUNTRY_GET_LIMIT_DETAILS \
55 " [c] Country code: country code value\n" \
56 " [f] File path: full path and name of table file, default:/vendor/firmware/BtCountryLimit.dat"
57
58#define USAGE_COUNTRY_SET_LIMIT_DETAILS \
59 " [c] Country code: country code value\n" \
60 " [f] File path: full path and name of table file, default:/vendor/firmware/BtCountryLimit.dat" \
61 " [b] BT TX power level\n" \
62 " [e] BLE TX power level\n" \
63 " [m] Max TX power level"
64
65#define USAGE_COUNTRY_DUMP_LIMIT_DETAILS \
66 " [f] File path: full path and name of table file, default:/vendor/firmware/BtCountryLimit.dat"
67
68#define USAGE_COUNTRY_CREATE_LIMIT_DETAILS \
69 " [c] Country code: country code value\n" \
70 " [t] File path: full path and name of country code [t]able file, default:/vendor/firmware/BtCountryLimit.dat\n" \
71 " [f] File path: full path and name of table file, default:/vendor/firmware/BtTxPwrLimit.bin"
72
73#define USAGE_COUNTRY_GET_SRS_DETAILS \
74 " [c] Country code: country code value\n" \
75 " [f] File path: full path and name of table file, default:/vendor/firmware/BtSRS.dat"
76
77#define USAGE_COUNTRY_SET_SRS_DETAILS \
78 " [c] Country_code: country code value\n" \
79 " [f] File path: full path and name of table file, default:/vendor/firmware/BtSRS.dat\n" \
80 " [s] SRS: SRS value"
81
82#define USAGE_COUNTRY_DUMP_SRS_DETAILS \
83 " [f] File path: full path and name of table file, default:/vendor/firmware/BtSRS.dat"
84
85boots_country_cmds_s country_commands[] = {
86 { "get_limit", mtk_boots_country_get_limit,
87 "Get power limit with country code. [-c <country_code>] [-f <file_path>]",
88 USAGE_COUNTRY_GET_LIMIT_DETAILS, false },
89 { "set_limit", mtk_boots_country_set_limit,
90 "Set power limit with country code. [-c <country_code>] [-f <file_path>] [-b <0-9>] [-e <0-9>] [-m <0-9>]",
91 USAGE_COUNTRY_SET_LIMIT_DETAILS, false },
92 { "dump_limit", mtk_boots_country_dump_limit,
93 "Dump power limit with all country code. [-f <file_path>]",
94 USAGE_COUNTRY_DUMP_LIMIT_DETAILS, false },
95 { "create_table", mtk_boots_country_create_limit_table,
96 "Create power limit table. [-c <country_code>] [-t <file_path>] [-f <file_path>]",
97 USAGE_COUNTRY_CREATE_LIMIT_DETAILS, false },
98 { "get_srs", mtk_boots_country_get_srs,
99 "Get SRS with country code. [-c <country_code>] [-f <file_path>]",
100 USAGE_COUNTRY_GET_SRS_DETAILS, false },
101 { "set_srs", mtk_boots_country_set_srs,
102 "Set SRS with country code. [-c <country_code>] [-f <file_path>] [-s <SRS>]",
103 USAGE_COUNTRY_SET_SRS_DETAILS, false },
104 { "dump_srs", mtk_boots_country_dump_srs,
105 "Dump SRS with all country code. [-f <file_path>]",
106 USAGE_COUNTRY_DUMP_SRS_DETAILS, false },
107 { NULL, NULL, NULL, NULL, NULL }
108};
109
110/****************************************************
111 Table format:
112 Country code|BT TX power level|BLE TX power level|Max TX power level|Diff mode
113****************************************************/
114static pkt_list_s *mtk_boots_country_get_limit(char *cmd[], size_t len, int index)
115{
116 FILE *fp;
117 char *line = NULL;
118 size_t line_len = 0;
119 ssize_t read;
120 char path[64] = DEFAULT_COUNTRY_LIMIT;
121 size_t i = 1;
122 int country_code = 0;
123 int temp_code = 0;
124 int bt_pwr = 0;
125 int ble_pwr = 0;
126 int max_pwr = 0;
127 uint8_t find = 0;
128 char *delim = "|";
129 char *pch = NULL;
130
131 BPRINT_D("%s", __func__);
132 if (len == 2 && *(cmd[1] + 1) == 'h') {
133 BPRINT_I("%s\n%s", country_commands[index].comment, country_commands[index].details);
134 return NULL;
135 }
136
137 while (i + 1 < len) {
138 switch (*(cmd[i] + 1)) {
139 case 'f':
140 memset(path, 0x00, sizeof(path));
141 strncpy(path, cmd[i + 1], sizeof(path) - 1);
142 i += 2;
143 break;
144 case 'c':
145 country_code = atoi(cmd[i + 1]);
146 i += 2;
147 break;
148 default:
149 BPRINT_E("%s: Incorrect format: %s", __func__, cmd[i]);
150 return NULL;
151 }
152 }
153
154 fp = fopen(path, "r");
155 if (fp == NULL) {
156 BPRINT_E("open file %s failed, errno = %s(%d)\n", path, strerror(errno), errno);
157 } else {
158 while ((read = getline(&line, &line_len, fp)) != -1) {
159 i = 0;
160 pch = strtok(line, delim);
161 while (pch != NULL) {
162 if (i == 0) {
163 temp_code = atoi(pch);
164 if (temp_code != country_code)
165 break;
166 else
167 find = 1;
168 } else if (i == 1)
169 bt_pwr = atoi(pch);
170 else if (i == 2)
171 ble_pwr = atoi(pch);
172 else if (i == 3)
173 max_pwr = atoi(pch);
174 else
175 BPRINT_D("Out of table");
176 pch = strtok (NULL, delim);
177 i++;
178 }
179 if (find == 1)
180 break;
181 }
182 free(line);
183 fclose(fp);
184 if (find == 1) {
185 BPRINT_I("Country_code:%d BT_Power:%d BLE_Power:%d Max_Power:%d", country_code, bt_pwr, ble_pwr, max_pwr);
186 } else {
187 BPRINT_I("Country_code:%d Not Find.", country_code);
188 }
189 }
190 return NULL;
191}
192
193//--------------------------------------------------------------------------
194static pkt_list_s *mtk_boots_country_set_limit(char *cmd[], size_t len, int index)
195{
196 FILE *fp;
197 ssize_t read;
198 size_t line_len = 0;
199 char *wbuf = NULL;
200 char *line = NULL;
201 char *line_cp = NULL;
202 int fp_len = 0;
203 char path[64] = DEFAULT_COUNTRY_LIMIT;
204 char path_cp[64] = DEFAULT_COUNTRY_LIMIT;
205 size_t i = 1;
206 ssize_t w_size = 0;
207 int country_code = 0;
208 int temp_code = 0;
209 int bt_pwr = 0;
210 int ble_pwr = 0;
211 int max_pwr = 0;
212 char *delim = "|";
213 char *pch = NULL;
214 char str_buf[13];
215
216 BPRINT_D("%s", __func__);
217 if (len == 2 && *(cmd[1] + 1) == 'h') {
218 BPRINT_I("%s\n%s", country_commands[index].comment, country_commands[index].details);
219 return NULL;
220 }
221
222 while (i + 1 < len) {
223 switch (*(cmd[i] + 1)) {
224 case 'f':
225 memset(path, 0x00, sizeof(path));
226 strncpy(path, cmd[i + 1], sizeof(path) - 1);
227 strncpy(path_cp, cmd[i + 1], sizeof(path_cp) - 1);
228 i += 2;
229 break;
230 case 'c':
231 country_code = atoi(cmd[i + 1]);
232 i += 2;
233 break;
234 case 'b':
235 bt_pwr = atoi(cmd[i + 1]);
236 i += 2;
237 break;
238 case 'e':
239 ble_pwr = atoi(cmd[i + 1]);
240 i += 2;
241 break;
242 case 'm':
243 max_pwr = atoi(cmd[i + 1]);
244 i += 2;
245 break;
246 default:
247 BPRINT_E("%s: Incorrect format: %s", __func__, cmd[i]);
248 return NULL;
249 }
250 }
251
252 fp = fopen(path, "r+");
253 if (fp != NULL) {
254 fseek(fp, 0L, SEEK_END);
255 fp_len = ftell(fp);
256 wbuf = malloc(fp_len + 1 + 12);
257 if (!wbuf) {
258 BPRINT_E("%s: Alloc wbuf failed", __func__);
259 fclose(fp);
260 return NULL;
261 }
262 fseek(fp, 0L, SEEK_SET);
263
264 while ((read = getline(&line, &line_len, fp)) != -1) {
265 line_cp = malloc(read);
266 if (!line_cp) {
267 BPRINT_E("%s: Alloc line_cp failed", __func__);
268 free(line);
269 free(wbuf);
270 fclose(fp);
271 return NULL;
272 }
273 memcpy(line_cp, line, read);
274 i = 0;
275 pch = strtok(line, delim);
276 while (pch != NULL) {
277 if (i == 0) {
278 temp_code = atoi(pch);
279 if (temp_code != country_code) {
280 memcpy(&wbuf[w_size], line_cp, read);
281 w_size += read;
282 }
283 }
284 pch = strtok (NULL, delim);
285 i++;
286 }
287 free(line_cp);
288 }
289 free(line);
290 fclose(fp);
291 } else {
292 wbuf = malloc(13);
293 if (!wbuf) {
294 BPRINT_E("%s: Alloc wbuf failed", __func__);
295 return NULL;
296 }
297 }
298
299 i = sprintf(str_buf, "%03d|%01d|%01d|%01d|0\n", country_code, bt_pwr, ble_pwr, max_pwr);
300 str_buf[12] = '\0';
301 memcpy(&wbuf[w_size], str_buf, i);
302 w_size += i;
303 fp = fopen(path_cp, "w");
304 if (fp == NULL) {
305 BPRINT_E("open file %s failed, errno = %s(%d)\n", path_cp, strerror(errno), errno);
306 free(wbuf);
307 return NULL;
308 }
309 fwrite(wbuf, 1, w_size, fp);
310 fclose(fp);
311 free(wbuf);
312
313 BPRINT_I("Set Country_code:%d BT_Power:%d BLE_Power:%d Max_Power:%d", country_code, bt_pwr, ble_pwr, max_pwr);
314 return NULL;
315}
316
317//--------------------------------------------------------------------------
318static pkt_list_s *mtk_boots_country_dump_limit(char *cmd[], size_t len, int index)
319{
320 FILE *fp;
321 char *line = NULL;
322 size_t line_len = 0;
323 ssize_t read;
324 char path[64] = DEFAULT_COUNTRY_LIMIT;
325 size_t i = 1;
326 int country_code = 0;
327 int bt_pwr = 0;
328 int ble_pwr = 0;
329 int max_pwr = 0;
330 char *delim = "|";
331 char *pch = NULL;
332
333 BPRINT_D("%s", __func__);
334 if (len == 2 && *(cmd[1] + 1) == 'h') {
335 BPRINT_I("%s\n%s", country_commands[index].comment, country_commands[index].details);
336 return NULL;
337 }
338
339 while (i + 1 < len) {
340 switch (*(cmd[i] + 1)) {
341 case 'f':
342 memset(path, 0x00, sizeof(path));
343 strncpy(path, cmd[i + 1], sizeof(path) - 1);
344 i += 2;
345 break;
346 default:
347 BPRINT_E("%s: Incorrect format: %s", __func__, cmd[i]);
348 return NULL;
349 }
350 }
351
352 fp = fopen(path, "r");
353 if (fp == NULL) {
354 BPRINT_E("open file %s failed, errno = %s(%d)\n", path, strerror(errno), errno);
355 } else {
356 while ((read = getline(&line, &line_len, fp)) != -1) {
357 i = 0;
358 pch = strtok(line, delim);
359 while (pch != NULL) {
360 if (i == 0)
361 country_code = atoi(pch);
362 else if (i == 1)
363 bt_pwr = atoi(pch);
364 else if (i == 2)
365 ble_pwr = atoi(pch);
366 else if (i == 3)
367 max_pwr = atoi(pch);
368 else if (i == 4) {
369 BPRINT_I("Country_code:%d BT_Power:%d BLE_Power:%d Max_Power:%d", country_code, bt_pwr, ble_pwr, max_pwr);
370 } else
371 BPRINT_D("Out of table");
372 pch = strtok (NULL, delim);
373 i++;
374 }
375 }
376 free(line);
377 fclose(fp);
378 }
379 return NULL;
380}
381
382//--------------------------------------------------------------------------
383static pkt_list_s *mtk_boots_country_create_limit_table(char *cmd[], size_t len, int index)
384{
385 FILE *fp;
386 char *line = NULL;
387 size_t line_len = 0;
388 ssize_t read;
389 char path[64] = DEFAULT_COUNTRY_LIMIT;
390 char table_path[64] = DEFAULT_POWER_LIMIT;
391 size_t i = 1;
392 int country_code = 0;
393 int temp_code = 0;
394 uint8_t bt_pwr = 0;
395 uint8_t ble_pwr = 0;
396 uint8_t max_pwr = 0;
397 uint8_t diff = 0;
398 uint8_t find = 0;
399 char *delim = "|";
400 char *pch = NULL;
401
402 BPRINT_D("%s", __func__);
403 if (len == 2 && *(cmd[1] + 1) == 'h') {
404 BPRINT_I("%s\n%s", country_commands[index].comment, country_commands[index].details);
405 return NULL;
406 }
407
408 while (i + 1 < len) {
409 switch (*(cmd[i] + 1)) {
410 case 't':
411 memset(path, 0x00, sizeof(path));
412 strncpy(path, cmd[i + 1], sizeof(path) - 1);
413 i += 2;
414 break;
415 case 'f':
416 memset(table_path, 0x00, sizeof(table_path));
417 strncpy(table_path, cmd[i + 1], sizeof(table_path) - 1);
418 i += 2;
419 break;
420 case 'c':
421 country_code = atoi(cmd[i + 1]);
422 i += 2;
423 break;
424 default:
425 BPRINT_E("%s: Incorrect format: %s", __func__, cmd[i]);
426 return NULL;
427 }
428 }
429
430 fp = fopen(path, "r");
431 if (fp == NULL) {
432 BPRINT_E("open file %s failed, errno = %s(%d)\n", path, strerror(errno), errno);
433 return NULL;
434 } else {
435 while ((read = getline(&line, &line_len, fp)) != -1) {
436 i = 0;
437 pch = strtok(line, delim);
438 while (pch != NULL) {
439 if (i == 0) {
440 temp_code = atoi(pch);
441 if (temp_code != country_code)
442 break;
443 else
444 find = 1;
445 } else if (i == 1)
446 bt_pwr = atoi(pch);
447 else if (i == 2)
448 ble_pwr = atoi(pch);
449 else if (i == 3)
450 max_pwr = atoi(pch);
451 else if (i == 4)
452 diff = atoi(pch);
453 else
454 BPRINT_D("Out of table");
455 pch = strtok (NULL, delim);
456 i++;
457 }
458 if (find == 1)
459 break;
460 }
461 free(line);
462 fclose(fp);
463 if (find == 1) {
464 BPRINT_I("Country_code:%d BT_Power:%d BLE_Power:%d Max_Power:%d", country_code, bt_pwr, ble_pwr, max_pwr);
465 } else {
466 BPRINT_I("Country_code:%d Not Find.", country_code);
467 return NULL;
468 }
469 }
470 fp = fopen(table_path, "wb");
471 if (fp == NULL) {
472 BPRINT_E("open file %s failed, errno = %s(%d)\n", path, strerror(errno), errno);
473 return NULL;
474 }
475
476 BPRINT_I("Write to table: %s", table_path);
477 fwrite(&bt_pwr, 1, sizeof(uint8_t), fp);
478 fwrite(&ble_pwr, 1, sizeof(uint8_t), fp);
479 fwrite(&max_pwr, 1, sizeof(uint8_t), fp);
480 fwrite(&diff, 1, sizeof(uint8_t), fp);
481 fclose(fp);
482 return NULL;
483}
484
485//--------------------------------------------------------------------------
486/****************************************************
487 Table format:
488 Country code|SRS
489****************************************************/
490
491static pkt_list_s *mtk_boots_country_get_srs(char *cmd[], size_t len, int index)
492{
493 FILE *fp;
494 char *line = NULL;
495 size_t line_len = 0;
496 ssize_t read;
497 char path[64] = DEFAULT_SRS;
498 size_t i = 1;
499 int country_code = 0;
500 int temp_code = 0;
501 int srs = 0;
502 uint8_t find = 0;
503 char *delim = "|";
504 char *pch = NULL;
505
506 BPRINT_D("%s", __func__);
507 if (len == 2 && *(cmd[1] + 1) == 'h') {
508 BPRINT_I("%s\n%s", country_commands[index].comment, country_commands[index].details);
509 return NULL;
510 }
511
512 while (i + 1 < len) {
513 switch (*(cmd[i] + 1)) {
514 case 'f':
515 memset(path, 0x00, sizeof(path));
516 strncpy(path, cmd[i + 1], sizeof(path) - 1);
517 i += 2;
518 break;
519 case 'c':
520 country_code = atoi(cmd[i + 1]);
521 i += 2;
522 break;
523 default:
524 BPRINT_E("%s: Incorrect format: %s", __func__, cmd[i]);
525 return NULL;
526 }
527 }
528
529 fp = fopen(path, "r");
530 if (fp == NULL) {
531 BPRINT_E("open file %s failed, errno = %s(%d)\n", path, strerror(errno), errno);
532 } else {
533 while ((read = getline(&line, &line_len, fp)) != -1) {
534 i = 0;
535 pch = strtok(line, delim);
536 while (pch != NULL) {
537 if (i == 0) {
538 temp_code = atoi(pch);
539 if (temp_code != country_code)
540 break;
541 else
542 find = 1;
543 } else if (i == 1)
544 srs = atoi(pch);
545 else
546 BPRINT_D("Out of table");
547 pch = strtok (NULL, delim);
548 i++;
549 }
550 if (find == 1)
551 break;
552 }
553 free(line);
554 fclose(fp);
555 if (find == 1) {
556 BPRINT_I("Country_code:%d SRS:%d", country_code, srs);
557 } else {
558 BPRINT_I("Country_code:%d Not Find.", country_code);
559 }
560 }
561 return NULL;
562}
563
564//--------------------------------------------------------------------------
565static pkt_list_s *mtk_boots_country_set_srs(char *cmd[], size_t len, int index)
566{
567 FILE *fp;
568 ssize_t read;
569 size_t line_len = 0;
570 char *wbuf = NULL;
571 char *line = NULL;
572 char *line_cp = NULL;
573 int fp_len = 0;
574 char path[64] = DEFAULT_SRS;
575 char path_cp[64] = DEFAULT_SRS;
576 size_t i = 1;
577 ssize_t w_size = 0;
578 int country_code = 0;
579 int temp_code = 0;
580 int srs = 0;
581 char *delim = "|";
582 char *pch = NULL;
583 char str_buf[12];
584
585 BPRINT_D("%s", __func__);
586 if (len == 2 && *(cmd[1] + 1) == 'h') {
587 BPRINT_I("%s\n%s", country_commands[index].comment, country_commands[index].details);
588 return NULL;
589 }
590
591 while (i + 1 < len) {
592 switch (*(cmd[i] + 1)) {
593 case 'f':
594 memset(path, 0x00, sizeof(path));
595 strncpy(path, cmd[i + 1], sizeof(path) - 1);
596 strncpy(path_cp, cmd[i + 1], sizeof(path_cp) - 1);
597 i += 2;
598 break;
599 case 'c':
600 country_code = atoi(cmd[i + 1]);
601 i += 2;
602 break;
603 case 's':
604 srs = atoi(cmd[i + 1]);
605 i += 2;
606 break;
607 default:
608 BPRINT_E("%s: Incorrect format: %s", __func__, cmd[i]);
609 return NULL;
610 }
611 }
612
613 fp = fopen(path, "r+");
614 if (fp != NULL) {
615 fseek(fp, 0L, SEEK_END);
616 fp_len = ftell(fp);
617 wbuf = malloc(fp_len + 1 + 12);
618 if (!wbuf) {
619 BPRINT_E("%s: Alloc wbuf failed", __func__);
620 fclose(fp);
621 return NULL;
622 }
623 fseek(fp, 0L, SEEK_SET);
624
625 while ((read = getline(&line, &line_len, fp)) != -1) {
626 line_cp = malloc(read);
627 if (!line_cp) {
628 BPRINT_E("%s: Alloc line_cp failed", __func__);
629 free(line);
630 free(wbuf);
631 fclose(fp);
632 return NULL;
633 }
634 memcpy(line_cp, line, read);
635 i = 0;
636 pch = strtok(line, delim);
637 while (pch != NULL) {
638 if (i == 0) {
639 temp_code = atoi(pch);
640 if (temp_code != country_code) {
641 memcpy(&wbuf[w_size], line_cp, read);
642 w_size += read;
643 }
644 }
645 pch = strtok (NULL, delim);
646 i++;
647 }
648 free(line_cp);
649 }
650 free(line);
651 fclose(fp);
652 } else {
653 wbuf = malloc(13);
654 if (!wbuf) {
655 BPRINT_E("%s: Alloc wbuf failed", __func__);
656 return NULL;
657 }
658 }
659 i = sprintf(str_buf, "%03d|%d\n", country_code, srs);
660 memcpy(&wbuf[w_size], str_buf, i);
661 w_size += i;
662 fp = fopen(path_cp, "w");
663 if (fp == NULL) {
664 BPRINT_E("open file %s failed, errno = %s(%d)\n", path_cp, strerror(errno), errno);
665 free(wbuf);
666 return NULL;
667 }
668 fwrite(wbuf, 1, w_size, fp);
669 fclose(fp);
670 free(wbuf);
671
672 BPRINT_I("Set Country_code:%d SRS:%d", country_code, srs);
673 return NULL;
674}
675
676//--------------------------------------------------------------------------
677static pkt_list_s *mtk_boots_country_dump_srs(char *cmd[], size_t len, int index)
678{
679 FILE *fp;
680 char *line = NULL;
681 size_t line_len = 0;
682 ssize_t read;
683 char path[64] = DEFAULT_SRS;
684 size_t i = 1;
685 int country_code = 0;
686 int srs = 0;
687 char *delim = "|";
688 char *pch = NULL;
689
690 BPRINT_D("%s", __func__);
691 if (len == 2 && *(cmd[1] + 1) == 'h') {
692 BPRINT_I("%s\n%s", country_commands[index].comment, country_commands[index].details);
693 return NULL;
694 }
695
696 while (i + 1 < len) {
697 switch (*(cmd[i] + 1)) {
698 case 'f':
699 memset(path, 0x00, sizeof(path));
700 strncpy(path, cmd[i + 1], sizeof(path) - 1);
701 i += 2;
702 break;
703 default:
704 BPRINT_E("%s: Incorrect format: %s", __func__, cmd[i]);
705 return NULL;
706 }
707 }
708
709 fp = fopen(path, "r");
710 if (fp == NULL) {
711 BPRINT_E("open file %s failed, errno = %s(%d)\n", path, strerror(errno), errno);
712 } else {
713 while ((read = getline(&line, &line_len, fp)) != -1) {
714 i = 0;
715 pch = strtok(line, delim);
716 while (pch != NULL) {
717 if (i == 0)
718 country_code = atoi(pch);
719 else if (i == 1) {
720 srs = atoi(pch);
721 BPRINT_I("Country_code:%d SRS:%d", country_code, srs);
722 }
723 else
724 BPRINT_D("Out of table");
725 pch = strtok (NULL, delim);
726 i++;
727 }
728 }
729 free(line);
730 fclose(fp);
731 }
732 return NULL;
733}
734
735//---------------------------------------------------------------------------
736pkt_list_s *boots_country_set_handler(char *cmd[], size_t len)
737{
738 uint8_t i = 0;
739 pkt_list_s *list = NULL;
740 UNUSED(boots_btif);
741
742 if (!cmd || !len) return NULL;
743
744 for (i = 0; i < ARRAY_SIZE(country_commands); i++) {
745 if (country_commands[i].cmd && !memcmp(cmd[0], country_commands[i].cmd, strlen(cmd[0]))) {
746 list = country_commands[i].func(cmd, len, i);
747 return list;
748 }
749 }
750 BPRINT_I("%s: Command Not Found(%s)", __func__, cmd[0]);
751
752 return NULL;
753}
754
755//---------------------------------------------------------------------------