blob: 976f289de2ec05f827e8dd35a24650a0011728f4 [file] [log] [blame]
b.liu9e8584b2024-11-06 19:21:28 +08001#include <string.h>
b.liu0d56e092025-03-18 11:15:48 +08002#include <stdint.h>
3#include <cutils/properties.h>
4#include <unistd.h>
5
b.liuf678f992024-05-08 15:23:10 +08006#include "mbtk_type.h"
b.liu61ad9172025-01-09 14:33:55 +08007#include "mbtk_utils.h"
b.liu472cfaf2024-12-19 19:08:19 +08008
b.liubb5e7682024-02-28 20:13:04 +08009#include "mbtk_device.h"
b.liu472cfaf2024-12-19 19:08:19 +080010
b.liu0d56e092025-03-18 11:15:48 +080011/*
12* Exec shell command.
13*/
14static bool shell_cmd_exec(const char *cmd, char *buf, int buf_size){
15 FILE *fcmd;
16 bool result = FALSE;
17 fcmd = popen(cmd, "r");
18 memset(buf, 0, buf_size);
19 if(fcmd)
20 {
21 int pos = 0;
22 int len = 0;
23
24 while(!ferror(fcmd) && !feof(fcmd))
25 {
26 if(buf_size - pos == 0)
27 {
28 break;
29 }
30 len = fread(buf + pos,1,buf_size - pos,fcmd);
31 if(len > 0)
32 pos += len;
33 }
34
35 if(buf_size == pos)
36 buf[buf_size - 1] = '\0';
37
38 pclose(fcmd);
39 result = TRUE;
40 }
41
42// LOGV("%s [result:%d]: %s",cmd,result,buf);
43
44 return result;
45}
46
b.liubb5e7682024-02-28 20:13:04 +080047
48static char* band_2_str(mbtk_modem_band_area_enum band_area)
49{
50 switch(band_area)
51 {
52 case MBTK_MODEM_BAND_AREA_CN:
53 return "CN";
54 case MBTK_MODEM_BAND_AREA_EU:
55 return "EU";
b.liu288093c2024-05-09 17:02:57 +080056 case MBTK_MODEM_BAND_AREA_SA:
57 return "SA";
b.liubb5e7682024-02-28 20:13:04 +080058 default:
59 return "ALL";
60 }
61}
62
b.liu61ad9172025-01-09 14:33:55 +080063static char* net_support_str_get(uint32 net_support)
64{
65 static char net_str[100] = {0};
66
67 if(net_support & MBTK_NET_SUPPORT_2G) { // GSM
68 if(strlen(net_str) > 0) {
69 strcat(net_str, "/2G");
70 } else {
71 strcat(net_str, "2G");
72 }
73 }
74
75 if(net_support & MBTK_NET_SUPPORT_3G) { // WCDMA
76 if(strlen(net_str) > 0) {
77 strcat(net_str, "/3G");
78 } else {
79 strcat(net_str, "3G");
80 }
81 }
82
83 if(net_support & MBTK_NET_SUPPORT_4G) { // LTE
84 if(strlen(net_str) > 0) {
85 strcat(net_str, "/4G");
86 } else {
87 strcat(net_str, "4G");
88 }
89 }
90
91 if(net_support & MBTK_NET_SUPPORT_5G) { // NR
92 if(strlen(net_str) > 0) {
93 strcat(net_str, "/5G");
94 } else {
95 strcat(net_str, "5G");
96 }
97 }
98 return net_str;
99}
100
101static char* band_str_get(uint32 band, int index)
102{
103 int band_list[33] = {0};
104 int ret = mbtk_band_2_list(band, index, band_list);
105 if(ret > 0) {
106 static char buff[150] = {0};
107 memset(buff, 0, sizeof(buff));
108 int i = 0;
109 while(i < ret) {
110 if(strlen(buff) > 0) {
111 sprintf(buff + strlen(buff), "/b%d", band_list[i]);
112 } else {
113 sprintf(buff + strlen(buff), "b%d", band_list[i]);
114 }
115 i++;
116 }
117 return buff;
118 } else {
119 return "NON";
120 }
121}
122
b.liu0d56e092025-03-18 11:15:48 +0800123static void band_support_init(mbtk_device_info_modem_t *info_modem)
124{
125 memset(info_modem, 0, sizeof(mbtk_device_info_modem_t));
126 if(mbtk_dev_info_read(MBTK_DEVICE_INFO_ITEM_MODEM, info_modem, sizeof(mbtk_device_info_modem_t))) {
127 printf("mbtk_dev_info_read(MODEM) fail, use default band.\n");
b.liub7530d22025-06-16 19:49:05 +0800128 info_modem->version = DEV_INFO_VERSION_V2;
129 info_modem->modem.v2.band_area = MBTK_MODEM_BAND_AREA_ALL;
b.liu0d56e092025-03-18 11:15:48 +0800130#ifdef MBTK_5G_SUPPORT
b.liub7530d22025-06-16 19:49:05 +0800131 info_modem->modem.v2.net_pref = 19; // MBTK_NET_PREF_LTE_NR_NR_PREF
132 info_modem->modem.v2.net_support = MBTK_NET_SUPPORT_4G | MBTK_NET_SUPPORT_5G;
b.liu0d56e092025-03-18 11:15:48 +0800133
b.liub7530d22025-06-16 19:49:05 +0800134 info_modem->modem.v2.band_gsm = 0;
135 info_modem->modem.v2.band_wcdma = 0;
136 info_modem->modem.v2.band_tdlte = MBTK_BAND_ALL_TDLTE_DEFAULT;
137 info_modem->modem.v2.band_fddlte = MBTK_BAND_ALL_FDDLTE_DEFAULT;
138 info_modem->modem.v2.band_lte_ext = MBTK_BAND_ALL_EXT_LTE_DEFAULT;
b.liu0d56e092025-03-18 11:15:48 +0800139
b.liub7530d22025-06-16 19:49:05 +0800140 info_modem->modem.v2.band_nr_3 = MBTK_BAND_ALL_NR_3_DEFAULT;
141 info_modem->modem.v2.band_nr_2 = MBTK_BAND_ALL_NR_2_DEFAULT;
142 info_modem->modem.v2.band_nr_1 = MBTK_BAND_ALL_NR_1_DEFAULT;
143 info_modem->modem.v2.band_nr_0 = MBTK_BAND_ALL_NR_0_DEFAULT;
b.liu0d56e092025-03-18 11:15:48 +0800144#else
b.liub7530d22025-06-16 19:49:05 +0800145 info_modem->modem.v2.net_pref = 15; // MBTK_NET_PREF_GSM_UMTS_LTE_LTE_PREF
146 info_modem->modem.v2.net_support = MBTK_NET_SUPPORT_2G | MBTK_NET_SUPPORT_3G | MBTK_NET_SUPPORT_4G;
b.liu0d56e092025-03-18 11:15:48 +0800147
b.liub7530d22025-06-16 19:49:05 +0800148 info_modem->modem.v2.band_gsm = MBTK_BAND_ALL_GSM_DEFAULT;
149 info_modem->modem.v2.band_wcdma = MBTK_BAND_ALL_WCDMA_DEFAULT;
150 info_modem->modem.v2.band_tdlte = MBTK_BAND_ALL_TDLTE_DEFAULT;
151 info_modem->modem.v2.band_fddlte = MBTK_BAND_ALL_FDDLTE_DEFAULT;
152 info_modem->modem.v2.band_lte_ext = MBTK_BAND_ALL_EXT_LTE_DEFAULT;
b.liu0d56e092025-03-18 11:15:48 +0800153
b.liub7530d22025-06-16 19:49:05 +0800154 info_modem->modem.v2.band_nr_3 = 0;
155 info_modem->modem.v2.band_nr_2 = 0;
156 info_modem->modem.v2.band_nr_1 = 0;
157 info_modem->modem.v2.band_nr_0 = 0;
b.liu0d56e092025-03-18 11:15:48 +0800158#endif
159 }
160}
161
162static int band_set_by_at(mbtk_device_info_modem_t *band)
163{
164 char cmd[100] = {0};
165 char rsp[100] = {0};
166
167 // Only support 4G/5G
b.liub7530d22025-06-16 19:49:05 +0800168 if(band->modem.v2.net_support & MBTK_NET_SUPPORT_5G) {
b.liu0d56e092025-03-18 11:15:48 +0800169 // AT*band=19,0,0,482,134742231,0,24576,256,134217877,0
b.liub7530d22025-06-16 19:49:05 +0800170 if(band->modem.v2.band_lte_ext > 0) {
171 sprintf(cmd, "serial_atcmd AT*BAND=%d,%d,%d,%d,%d,%d,%d,%d,%d,0,,,,%d", band->modem.v2.net_pref, band->modem.v2.band_gsm,
172 band->modem.v2.band_wcdma, band->modem.v2.band_tdlte, band->modem.v2.band_fddlte,
173 band->modem.v2.band_nr_3, band->modem.v2.band_nr_2, band->modem.v2.band_nr_1,
174 band->modem.v2.band_nr_0, band->modem.v2.band_lte_ext);
b.liu0d56e092025-03-18 11:15:48 +0800175 } else {
b.liub7530d22025-06-16 19:49:05 +0800176 sprintf(cmd, "serial_atcmd AT*BAND=%d,%d,%d,%d,%d,%d,%d,%d,%d,0", band->modem.v2.net_pref, band->modem.v2.band_gsm,
177 band->modem.v2.band_wcdma, band->modem.v2.band_tdlte, band->modem.v2.band_fddlte,
178 band->modem.v2.band_nr_3, band->modem.v2.band_nr_2, band->modem.v2.band_nr_1, band->modem.v2.band_nr_0);
b.liu0d56e092025-03-18 11:15:48 +0800179 }
180 } else {
b.liub7530d22025-06-16 19:49:05 +0800181 if(band->modem.v2.band_lte_ext > 0) {
182 sprintf(cmd, "serial_atcmd AT*BAND=%d,%d,%d,%d,%d,,,,%d", band->modem.v2.net_pref, band->modem.v2.band_gsm, band->modem.v2.band_wcdma,
183 band->modem.v2.band_tdlte, band->modem.v2.band_fddlte, band->modem.v2.band_lte_ext);
b.liu0d56e092025-03-18 11:15:48 +0800184 } else {
b.liub7530d22025-06-16 19:49:05 +0800185 sprintf(cmd, "serial_atcmd AT*BAND=%d,%d,%d,%d,%d", band->modem.v2.net_pref, band->modem.v2.band_gsm, band->modem.v2.band_wcdma, band->modem.v2.band_tdlte, band->modem.v2.band_fddlte);
b.liu0d56e092025-03-18 11:15:48 +0800186 }
187 }
188
189 if(shell_cmd_exec(cmd, rsp, sizeof(rsp))) {
190 if(strstr(rsp, "\r\nOK\r\n")) {
191 return 0;
192 } else {
193 return -1;
194 }
195 } else {
196 return -1;
197 }
198}
199
200/*
201AT*BAND=15,78,147,482,134742231
202
203OK
204*/
205static int band_config(mbtk_device_info_modem_t *info_modem)
206{
207 if(band_set_by_at(info_modem))
208 {
209 return -1;
210 }
211 else // Set band success.
212 {
b.liub7530d22025-06-16 19:49:05 +0800213 if(info_modem->modem.v2.band_area == MBTK_MODEM_BAND_AREA_CN) {
b.liu0d56e092025-03-18 11:15:48 +0800214 property_set("persist.mbtk.band_config", "CN");
b.liub7530d22025-06-16 19:49:05 +0800215 } else if(info_modem->modem.v2.band_area == MBTK_MODEM_BAND_AREA_EU) {
b.liu0d56e092025-03-18 11:15:48 +0800216 property_set("persist.mbtk.band_config", "EU");
b.liub7530d22025-06-16 19:49:05 +0800217 } else if(info_modem->modem.v2.band_area == MBTK_MODEM_BAND_AREA_SA) {
b.liu0d56e092025-03-18 11:15:48 +0800218 property_set("persist.mbtk.band_config", "SA");
219 } else {
220 property_set("persist.mbtk.band_config", "ALL");
221 }
222// LOGD("Set band success.");
223 return 0;
224 }
225}
226
b.liubb5e7682024-02-28 20:13:04 +0800227int main(int argc, char *argv[])
228{
b.liu0d56e092025-03-18 11:15:48 +0800229 if(argc > 1 && strcmp(argv[1], "band_set") == 0) {
230 char buff[10];
231 memset(buff, 0, 10);
232 property_get("persist.mbtk.band_config", buff, "");
233 if(strlen(buff) == 0) {
234 bool band_set_success = FALSE;
235
236 mbtk_device_info_modem_t info_modem;
237 band_support_init(&info_modem);
238
239 while(!band_set_success) {
240 // Set band.
241 if(band_config(&info_modem)) {
242 printf("band_config() fail,will waitting 5s to retry...\n");
243 sleep(5);
244 } else {
245 break;
246 }
247 }
248 printf("Set band success.\n");
249 }
250 } else {
251 mbtk_device_info_basic_t info_basic;
252 memset(&info_basic, 0, sizeof(mbtk_device_info_basic_t));
253 int result = mbtk_dev_info_read(MBTK_DEVICE_INFO_ITEM_BASIC, &info_basic, sizeof(mbtk_device_info_basic_t));
254 if(result) {
255 printf("mbtk_dev_info_read(BASIC) fail.\n");
256 return -1;
257 }
258
259 /*
260 uint8 project[16]; // T108 / L508_X6
261 uint8 project_cust[16]; // T108_C1 / L508_X6_C1 (Refer to: Custom_Model in blf file.)
262 uint32 ab_support; // 1 for ab
263 uint8 revision_out[48]; // L508_X6v01.01b04.00
264 uint8 revision_in[64];
265 */
b.liub7530d22025-06-16 19:49:05 +0800266 printf("Version:%d\n", info_basic.version);
267 if(info_basic.version == DEV_INFO_VERSION_V1) {
268 printf("Project:%s\n", info_basic.basic.v1.project);
269 printf("Custom_Model:%s\n", info_basic.basic.v1.project_cust);
270 printf("Revision_Out:%s\n", info_basic.basic.v1.revision_out);
271 printf("Revision_In:%s\n", info_basic.basic.v1.revision_in);
272 printf("Build_Time:%s\n", info_basic.basic.v1.build_time);
273 printf("AB System:%s\n", info_basic.basic.v1.ab_support ? "Yes" : "No");
274 printf("Reboot flag:%d\n", info_basic.basic.v1.reboot_flag);
275 } else {
276 printf("Project:%s\n", info_basic.basic.v2.project);
277 printf("Custom_Model:%s\n", info_basic.basic.v2.project_cust);
278 printf("Revision_Out:%s\n", info_basic.basic.v2.revision_out);
279 printf("Revision_In:%s\n", info_basic.basic.v2.revision_in);
280 printf("Build_Time:%s\n", info_basic.basic.v2.build_time);
281 printf("AB System:%s\n", info_basic.basic.v2.ab_support ? "Yes" : "No");
282 printf("Reboot flag:%d\n", info_basic.basic.v2.reboot_flag);
283 printf("ASR Baseline:%s\n", info_basic.basic.v2.asr_baseline);
284 }
b.liu0d56e092025-03-18 11:15:48 +0800285
286 mbtk_device_info_modem_t info_modem;
287 memset(&info_modem, 0, sizeof(mbtk_device_info_modem_t));
288 result = mbtk_dev_info_read(MBTK_DEVICE_INFO_ITEM_MODEM, &info_modem, sizeof(mbtk_device_info_modem_t));
289 if(result) {
290 printf("mbtk_dev_info_read(MODEM) fail.\n");
291 return -1;
292 }
293
b.liub7530d22025-06-16 19:49:05 +0800294 if(info_modem.version == DEV_INFO_VERSION_V1) {
295 printf("Band:%s\n", band_2_str(info_modem.modem.v1.band_area));
296 printf("Band GSM:0x%08x(%s)\n", info_modem.modem.v1.band_gsm, band_str_get(info_modem.modem.v1.band_gsm, 0));
297 printf("Band WCDMA:0x%08x(%s)\n", info_modem.modem.v1.band_wcdma, band_str_get(info_modem.modem.v1.band_wcdma, 0));
298 printf("Band TDLTE:0x%08x(%s)\n", info_modem.modem.v1.band_tdlte, band_str_get(info_modem.modem.v1.band_tdlte, 1));
299 printf("Band FDDLTE:0x%08x(%s)\n", info_modem.modem.v1.band_fddlte, band_str_get(info_modem.modem.v1.band_fddlte, 0));
300 printf("Band EXT_LTE:0x%08x(%s)\n", info_modem.modem.v1.band_lte_ext, band_str_get(info_modem.modem.v1.band_lte_ext, 0));
301 } else {
302 printf("net_pref:%d\n", info_modem.modem.v2.net_pref);
303 printf("net_support:%s\n", net_support_str_get(info_modem.modem.v2.net_support));
304 printf("Band:%s\n", band_2_str(info_modem.modem.v2.band_area));
305 printf("Band GSM:0x%08x(%s)\n", info_modem.modem.v2.band_gsm, band_str_get(info_modem.modem.v2.band_gsm, 0));
306 printf("Band WCDMA:0x%08x(%s)\n", info_modem.modem.v2.band_wcdma, band_str_get(info_modem.modem.v2.band_wcdma, 0));
307 printf("Band TDLTE:0x%08x(%s)\n", info_modem.modem.v2.band_tdlte, band_str_get(info_modem.modem.v2.band_tdlte, 1));
308 printf("Band FDDLTE:0x%08x(%s)\n", info_modem.modem.v2.band_fddlte, band_str_get(info_modem.modem.v2.band_fddlte, 0));
309 printf("Band EXT_LTE:0x%08x(%s)\n", info_modem.modem.v2.band_lte_ext, band_str_get(info_modem.modem.v2.band_lte_ext, 0));
310 printf("Band NR_3:0x%08x(%s)\n", info_modem.modem.v2.band_nr_3, band_str_get(info_modem.modem.v2.band_nr_3, 3));
311 printf("Band NR_2:0x%08x(%s)\n", info_modem.modem.v2.band_nr_2, band_str_get(info_modem.modem.v2.band_nr_2, 2));
312 printf("Band NR_1:0x%08x(%s)\n", info_modem.modem.v2.band_nr_1, band_str_get(info_modem.modem.v2.band_nr_1, 1));
313 printf("Band NR_0:0x%08x(%s)\n", info_modem.modem.v2.band_nr_0, band_str_get(info_modem.modem.v2.band_nr_0, 0));
314 }
b.liubb5e7682024-02-28 20:13:04 +0800315 }
b.liubb5e7682024-02-28 20:13:04 +0800316 return 0;
317}
318
319