blob: 81acb5cf4674ee6b556ca6ad14cbfa55cb3f3031 [file] [log] [blame]
liubin281ac462023-07-19 14:22:54 +08001#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <errno.h>
5#include <sys/socket.h>
6#include <sys/un.h>
7#include <netinet/in.h>
8#include <pthread.h>
9#include <sys/epoll.h>
10#include <string.h>
11#include <fcntl.h>
12#include <signal.h>
13
14#include "ql/ql_nw.h"
15#include "mbtk_log.h"
16
17static void help()
18{
19 printf("net_pref : Get net prefferred.\n");
20 printf("net_pref <net_pref> <roaming> : Set net prefferred.\n");
21 printf("net_time : Get network time.\n");
22 printf("operator : Get current operator information.\n");
23 printf("net_avail : Get available networks.\n");
24 printf("reg : Get current reg info.\n");
25 printf("sel_mode: Get network select mode.\n");
26 printf("sel_mode <sel_mode> <net_type> <plmn>: Set network select mode.\n");
27 printf("signal : Get current signal.\n");
28 printf("cell : Get cell info.\n");
29 printf("volte : Get VOLTE state.\n");
30 printf("csq_signal : Get current csq signal.\n");
31}
32
33static int proc_exit()
34{
35 QL_NW_ERROR_CODE err = ql_nw_release();
36 if(QL_NW_SUCCESS != err)
37 {
38 printf("ql_nw_release fail.");
39 return -1;
40 }
41 return 0;
42}
43
44static void sig_process(int sig)
45{
46 LOGI("I got signal %d\n", sig);
47 switch(sig)
48 {
49 case SIGINT: // Ctrl + C
50 {
51 LOGI("Exit by SIGINT.\n");
52 proc_exit();
53 exit(0);
54 }
55 case SIGQUIT: // Ctrl + \ (类似 SIGINT ,但要产生core文件)
56 {
57 LOGI("Exit by SIGQUIT.\n");
58 proc_exit();
59 exit(0);
60 }
61 case SIGTERM:// 默认kill (同 SIGKILL ,但 SIGKILL 不可捕获)
62 {
63 LOGI("Exit by SIGTERM.\n");
64 proc_exit();
65 exit(0);
66 }
67 case SIGTSTP:// Ctrl + Z (同 SIGSTOP ,但 SIGSTOP 不可捕获)
68 {
69 LOGI("Exit by SIGTSTP.\n");
70 exit(0);
71 }
72 case SIGSEGV: // 如空指针
73 {
74 LOGI("Exit by SIGSEGV.\n");
75 exit(0);
76 }
77 default:
78 {
79 LOGI("Unknown sig:%d\n",sig);
80 break;
81 }
82 }
83}
84
85int main(int argc, char *argv[])
86{
87 signal(SIGINT, sig_process);
88 signal(SIGQUIT, sig_process);
89 signal(SIGTERM, sig_process);
90 //signal(SIGTSTP, sig_process);
91 //signal(SIGSEGV, sig_process);
92
93 mbtk_log_init(NULL,"MBTK_QL_TEST");
94
95 //test2(0, "192.168.1.198");
96 //test2(1, "2409:8162:140:cd3c:1:2:1494:72ba");
97 //test2(1, "254.128.0.0.0.0.0.0.0.1.0.2.144.5.212.239");
98 //test2(1, "2400:3200::1");
99
100 QL_NW_ERROR_CODE err = ql_nw_init();
101 if(QL_NW_SUCCESS != err)
102 {
103 printf("ql_nw_init fail.");
104 return -1;
105 }
106
107 printf(">>>>>>>>>>>>>>>>>>>>>>>>Enter cmd:\n");
108 char cmd[100];
109 while(1)
110 {
111 memset(cmd, 0, 100);
112 if(fgets(cmd, 100, stdin))
113 {
114 char *ptr = cmd + strlen(cmd) - 1;
115 while(ptr >= cmd && (*ptr == '\r' || *ptr == '\n'))
116 {
117 *ptr-- = '\0';
118 }
119
120 if(!strncasecmp(cmd, "net_pref", 8)){
121 QL_NW_CONFIG_INFO_T net_pref;
122 if(!strcasecmp(cmd, "net_pref")) { // Get
123 err = ql_nw_get_config(&net_pref);
124 if(err) {
125 printf("Error : %d\n", err);
126 } else {
127 printf("net_pref : %d, roaming : %d\n", net_pref.preferred_nw_mode, net_pref.roaming_pref);
128 }
129 } else { // Set
130 char *ptr = strstr(cmd, " ");
131 if(ptr == NULL)
132 continue;
133 while(*ptr != '\0' && *ptr == ' ')
134 ptr++;
135 net_pref.preferred_nw_mode = atoi(ptr);
136
137 ptr = strstr(ptr, " ");
138 if(ptr == NULL)
139 continue;
140 while(*ptr != '\0' && *ptr == ' ')
141 ptr++;
142 net_pref.roaming_pref = atoi(ptr);
143
144 err = ql_nw_set_config(&net_pref);
145 if(err) {
146 printf("Error : %d\n", err);
147 } else {
148 printf("net_pref set success\n");
149 }
150 }
151 } else if(!strncasecmp(cmd, "net_time", 8)){
152 QL_NW_NITZ_TIME_INFO_T time;
153 err = ql_nw_get_nitz_time_info(&time);
154 if(err) {
155 printf("Error : %d\n", err);
156 } else {
157 printf("Time : %s, %ld, %d\n", time.nitz_time, time.abs_time, time.leap_sec);
158 }
159 } else if(!strncasecmp(cmd, "operator", 8)){
160 QL_NW_OPERATOR_INFO_T operator;
161 err = ql_nw_get_operator_name(&operator);
162 if(err) {
163 printf("Error : %d\n", err);
164 } else {
165 printf("Operator : %s, %s, %s, %s\n", operator.long_eons, operator.short_eons, operator.mcc, operator.mnc);
166 }
167 } else if(!strncasecmp(cmd, "net_avail", 9)){
168 QL_NW_SCAN_RESULT_LIST_INFO_T nets;
169 err = ql_nw_perform_scan(&nets);
170 if(err) {
171 printf("Error : %d\n", err);
172 } else {
173 if(nets.entry_len > 0) {
174 int i = 0;
175 for(; i < nets.entry_len; i++) {
176 printf("Net %d: %d, %d, %s, %s, %s, %s\n", i+1, nets.entry[i].status, nets.entry[i].act,
177 nets.entry[i].operator_name.long_eons, nets.entry[i].operator_name.short_eons,
178 nets.entry[i].operator_name.mcc, nets.entry[i].operator_name.mnc);
179 }
180 }
181 }
182 } else if(!strncasecmp(cmd, "reg", 3)){
183 QL_NW_REG_STATUS_INFO_T reg;
184 err = ql_nw_get_reg_status(&reg);
185 if(err) {
186 printf("Error : %d\n", err);
187 } else {
188 printf("Data Reg:%d, %d, %x, %x\n", reg.data_reg.state, reg.data_reg.rat, reg.data_reg.lac, reg.data_reg.cid);
189 printf("Voice Reg:%d, %d, %x, %x\n", reg.voice_reg.state, reg.voice_reg.rat, reg.voice_reg.lac, reg.voice_reg.cid);
190 }
191 } else if(!strncasecmp(cmd, "sel_mode", 8)){ // "sel_mode" or "sel_mode 460 00 7"
192 QL_NW_SELECTION_INFO_T net;
193 memset(&net, 0, sizeof(QL_NW_SELECTION_INFO_T));
194 if(!strcasecmp(cmd, "sel_mode")) { // Get
195 err = ql_nw_get_selection(&net);
196 if(err) {
197 printf("Error : %d\n", err);
198 } else {
199 printf("Net : %d, %s, %s, %d\n", net.nw_selection_mode , net.mcc , net.mnc , net.act);
200 }
201 } else { // Set
202 char *ptr = strstr(cmd, " ");
203 if(ptr == NULL)
204 continue;
205 while(*ptr != '\0' && *ptr == ' ')
206 ptr++;
207 net.nw_selection_mode = atoi(ptr);
208
209 ptr = strstr(ptr, " ");
210 if(ptr == NULL)
211 continue;
212 while(*ptr != '\0' && *ptr == ' ')
213 ptr++;
214 //net.mcc = (uint8)atoi(ptr);
215 memcpy(net.mcc, ptr, 4);
216 int i = 0;
217 while(i < 4) {
218 if(net.mcc[i] == ' ') {
219 net.mcc[i] = '\0';
220 break;
221 }
222 i++;
223 }
224
225 ptr = strstr(ptr, " ");
226 if(ptr == NULL)
227 continue;
228 while(*ptr != '\0' && *ptr == ' ')
229 ptr++;
230 //net.mnc = (uint32)atoi(ptr);
231 memcpy(net.mnc, ptr, 4);
232 i = 0;
233 while(i < 4) {
234 if(net.mnc[i] == ' ') {
235 net.mnc[i] = '\0';
236 break;
237 }
238 i++;
239 }
240
241 ptr = strstr(ptr, " ");
242 if(ptr == NULL)
243 continue;
244 while(*ptr != '\0' && *ptr == ' ')
245 ptr++;
246 net.act = (QL_NW_ACCESS_TECHNOLOGY)atoi(ptr);
247
248 err = ql_nw_set_selection(&net);
249 if(err) {
250 printf("Error : %d\n", err);
251 } else {
252 printf("Net select mode set success\n");
253 }
254 }
255 } else if(!strncasecmp(cmd, "signal", 6)){
256 QL_NW_SIGNAL_STRENGTH_INFO_T sig;
257 err = ql_nw_get_signal_strength(&sig);
258 if(err) {
259 printf("Error : %d\n", err);
260 } else {
261 printf("Signal GSM:%d, %d, %d, %d\n", sig.GW_SignalStrength.rscp, sig.GW_SignalStrength.bitErrorRate, sig.GW_SignalStrength.rscp, sig.GW_SignalStrength.ecio);
262 printf("Signal LTE:%d, %d, %d, %d, %d\n", sig.LTE_SignalStrength.rssi , sig.LTE_SignalStrength.rsrp, sig.LTE_SignalStrength.rsrq, sig.LTE_SignalStrength.rssnr, sig.LTE_SignalStrength.cqi);
263 }
264 } else if(!strncasecmp(cmd, "cell", 4)){
265 QL_NW_CELL_INFO_T cell;
266 err = ql_nw_get_cell_info(&cell);
267 if(err) {
268 printf("Error : %d\n", err);
269 } else {
270 int i = 0;
271 if(cell.gsm_info_valid) {
272 while(i < cell.gsm_info_num) {
273 printf("GSM cell %d: %d, %x, %d, %d, %x, %d, %d\n", i + 1, cell.gsm_info[i].flag, cell.gsm_info[i].cid, cell.gsm_info[i].mcc, cell.gsm_info[i].mnc, cell.gsm_info[i].lac, cell.gsm_info[i].arfcn, cell.gsm_info[i].bsic);
274 i++;
275 }
276 }
277
278 if(cell.umts_info_valid) {
279 i = 0;
280 while(i < cell.umts_info_num) {
281 printf("UMTS cell %d: %d, %x, %x, %d, %d, %x, %d, %d\n", i + 1, cell.umts_info[i].flag , cell.umts_info[i].cid, cell.umts_info[i].lcid, cell.umts_info[i].mcc, cell.umts_info[i].mnc, cell.umts_info[i].lac, cell.umts_info[i].uarfcn, cell.umts_info[i].psc);
282 i++;
283 }
284 }
285
286 if(cell.lte_info_valid) {
287 i = 0;
288 while(i < cell.lte_info_num) {
289 printf("LTE cell %d: %d, %x, %d, %d, %x, %d, %d\n", i + 1, cell.lte_info[i].flag, cell.lte_info[i].cid, cell.lte_info[i].mcc, cell.lte_info[i].mnc, cell.lte_info[i].tac, cell.lte_info[i].pci, cell.lte_info[i].earfcn);
290 i++;
291 }
292 }
293 }
294 } else if(!strncasecmp(cmd, "volte", 5)){
295 VOLTE_STATE state;
296 err = ql_nw_get_volte_state(&state);
297 if(err) {
298 printf("Error : %d\n", err);
299 } else {
300 printf("VOLTE state : %d\n", state.reg_state);
301 }
302 } else if(!strncasecmp(cmd, "csq_signal", 10)){
303 QL_NW_CSQ_SIGNAL_STRENGTH_INFO_T signal;
304 err = ql_nw_csq_get_signal_strength(&signal);
305 if(err) {
306 printf("Error : %d\n", err);
307 } else {
308 printf("+CSQ : %d, %d\n", signal.rssi, signal.bitErrorRate);
309 }
310 }
311 else if(!strcasecmp(cmd, "h") || !strcasecmp(cmd, "help")) {
312 help();
313 } else if(!strcasecmp(cmd, "q")) {
314 break;
315 } else {
316 printf("\n");
317 }
318 }
319 }
320
321 proc_exit();
322
323 LOGI("Client exec complete.");
324#if 1
325 while(1)
326 {
327 sleep(1000 * 365 * 24 * 60 * 60);
328 }
329#else
330 sleep(1);
331#endif
332 return 0;
333}
334