blob: c3cb0a603c701975514970cd0ab0473ce207a68b [file] [log] [blame]
b.liu8583dce2024-04-03 13:30:08 +08001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <sys/socket.h>
5#include <netinet/in.h>
6#include <arpa/inet.h>
7#include <dlfcn.h>
8#include <errno.h>
9#include <sys/un.h>
10#include <ctype.h>
11#include <unistd.h>
12#include <lynq/lynq-qser-data.h>
13
14#define MAX_COMMAND_LEN 10
15
16
17void evt_cb(qser_data_call_state_s *state);
18int qser_init_data();
19int qser_deinit_data();
20int data_demo_auto_test(char *apn_name1, char *apn_name2);
21
22qser_data_call_state_s state = {0};
23
24void delete_enter(char data[])
25{
26 char *find = strchr(data, '\n');
27 if(find)
28 *find = '\0';
29 return ;
30}
31void printf_help(void)
32{
33 printf("*******************************User Guide*************************************************\n");
34 printf("usage: lynq-qser-data-demo <apn_name> <apn_type>\n");
35 printf("Example: lynq-qser-data-demo cmwap lynq_apn1\n");
36 printf("*******************************************************************************************\n");
37 return;
38}
39
40/**
41 * @brief qser data call demo entry function.
42 *
43 * @detail This example will take a complete data process
44 * and show you how to call the API in liblynq-qser-data.
45 */
46int main(int argc, char *argv[])
47{
48 int i, ret;
49
50 printf("[%s] [%d] entry !\n", __FUNCTION__, __LINE__);
51 printf("[DATA_DEMO]lynq-qser-data-demo entry !\n");
52
53 if(argc != 3)
54 {
55 printf_help();
56 exit(EXIT_FAILURE);
57 }
58
59 //do initialization
60 ret = qser_data_call_init(evt_cb);
61 if(0 != ret)
62 {
63 printf("initial failed !\n");
64 return -1;
65 }
66
67 data_demo_auto_test(argv[1], argv[2]);
68 qser_data_call_destroy();
69
70 return 0;
71}
72
73//demo示例拨两路apn,一路使用默认apn配置拨号访问公网,另一路建议为私网配置
74int data_demo_auto_test(char *apn_name1, char *apn_type)
75{
76 int ret = 0;
77 char command[MAX_COMMAND_LEN] = {'\0'};
78 bool apn_test1_need_insert = true;
79 unsigned char profile_idx_char1;
80 qser_apn_add_s apn_test1 = {QSER_APN_PDP_TYPE_IPV4V6, QSER_APN_AUTH_PROTO_DEFAULT, NULL, NULL, NULL, NULL};
81 qser_data_call_s apn_normal_datacall;
82 qser_data_call_s apn_test1_datacall;
83 qser_data_call_error_e err = QSER_DATA_CALL_ERROR_NONE;
84
85 memcpy(apn_test1.apn_type,apn_type,QSER_APN_NAME_SIZE);
86 memcpy(apn_test1.apn_name,apn_name1,QSER_APN_NAME_SIZE);
87
88 //1、检查apn是否存在
89 qser_apn_info_list_s apn_list = {0};
90 ret = qser_apn_get_list(&apn_list);
91 if(ret != 0)
92 {
93 printf("\n get apn list error\n");
94 return -1;
95 }
96 for(int i = 0; i < apn_list.cnt; i++)
97 {
98 printf("data_demo_auto_test: pdp_type=%d, auth_proto=%d, apn_name=%s, username=%s, password=%s, apn_type=%s\n"
99 ,apn_list.apn[i].pdp_type, apn_list.apn[i].auth_proto, apn_list.apn[i].apn_name, apn_list.apn[i].username, apn_list.apn[i].password, apn_list.apn[i].apn_type);
100
101 if( apn_list.apn[i].pdp_type == apn_test1.pdp_type
102 && apn_list.apn[i].auth_proto == apn_test1.auth_proto
103 && (strcmp(apn_list.apn[i].apn_name, apn_test1.apn_name) == 0)
104 && (strcmp(apn_list.apn[i].username, apn_test1.username) == 0)
105 && (strcmp(apn_list.apn[i].password, apn_test1.password) == 0)
106 && (strcmp(apn_list.apn[i].apn_type, apn_test1.apn_type) == 0)
107 )
108 {
109 profile_idx_char1 = apn_list.apn[i].profile_idx;
110 apn_test1_need_insert = false;
111 }
112 }
113 //2、若不存在,插入apn
114 if(apn_test1_need_insert)
115 {
116 ret = qser_apn_add(&apn_test1, &profile_idx_char1);
117 if(ret != 0)
118 {
119 printf("\n add apn error\n");
120 return -1;
121 }
122 }
123 //3、拨号
124 apn_normal_datacall.profile_idx = 0;//默认apn profile_idx
125 apn_test1_datacall.profile_idx = profile_idx_char1;
126 //拨号时实际使用的ip_name,userdata,password等配置以apn信息中的为准,不使用datacall中的配置
127 ret = qser_data_call_start(&apn_normal_datacall, &err);//默认apn拨号
128 if(ret != 0)
129 {
130 printf("\nERROR: setup data call fail!!!\n");
131 return -1;
132 }
133
134 ret = qser_data_call_start(&apn_test1_datacall, &err);
135 if(ret != 0)
136 {
137 printf("\nERROR: setup data call fail!!!\n");
138 return -1;
139 }
140
141 printf("\n[DATA_DEMO]Enter --exit to exit data demo\n");
142 while(1)
143 {
144 fgets(command , MAX_COMMAND_LEN, stdin);
145 delete_enter(command);
146 if(strcmp(command,"--exit") == 0)
147 {
148 break;
149 }
150 else
151 {
152 printf("\n[DATA_DEMO]Enter --exit to deactive data call and exit data demo\n");
153 }
154 }
155
156 //4、去激活
157 //去激活时有效入参仅为profile_idx
158 ret = qser_data_call_stop(apn_normal_datacall.profile_idx, apn_normal_datacall.ip_family, &err);
159 if(ret < 0)
160 {
161 printf("\nERROR: deactive data call fail!!!\n");
162 return -1;
163 }
164 ret = qser_data_call_stop(apn_test1_datacall.profile_idx, apn_test1_datacall.ip_family, &err);
165 if(ret < 0)
166 {
167 printf("\nERROR: deactive data call fail!!!\n");
168 return -1;
169 }
170 return 0;
171}
172void evt_cb(qser_data_call_state_s *state)
173{
174 char buf_ip[64] = {0};
175 char buf_gateway[64] = {0};
176 char buf_pri_dns[64] = {0};
177 char buf_sec_dns[64] = {0};
178 printf("DATA_DEMO_CALL_BACK: profile_idx=%d, name=%s, ip_family=%d, state=%d, error=%d\n"
179 , state->profile_idx, state->name, state->ip_family, state->state, state->err);
wangyouqianga24887d2024-04-08 18:31:23 +0800180 #if 1
b.liu8583dce2024-04-03 13:30:08 +0800181 printf("DATA_DEMO_CALL_BACK: v4.ip=%s\n"
182 , inet_ntoa(state->v4.ip));
183 printf("DATA_DEMO_CALL_BACK: v4.gateway=%s\n"
184 , inet_ntoa(state->v4.gateway));
wangyouqianga24887d2024-04-08 18:31:23 +0800185 printf("DATA_DEMO_CALL_BACK: v4.pri_dns=%s\n"
b.liu8583dce2024-04-03 13:30:08 +0800186 , inet_ntoa(state->v4.pri_dns));
187 printf("DATA_DEMO_CALL_BACK: v4.sec_dns=%s\n"
188 , inet_ntoa(state->v4.sec_dns));
189 inet_ntop(AF_INET6, &(state->v6.ip), buf_ip, sizeof(buf_ip));
190 inet_ntop(AF_INET6, &(state->v6.gateway), buf_gateway, sizeof(buf_gateway));
191 inet_ntop(AF_INET6, &(state->v6.pri_dns), buf_pri_dns, sizeof(buf_pri_dns));
192 inet_ntop(AF_INET6, &(state->v6.sec_dns), buf_sec_dns, sizeof(buf_sec_dns));
wangyouqianga24887d2024-04-08 18:31:23 +0800193 printf("DATA_DEMO_CALL_BACK: v6.ip=%s\n", buf_ip);
194 printf("DATA_DEMO_CALL_BACK: v6.gateway=%s\n", buf_gateway);
195 printf("DATA_DEMO_CALL_BACK: v6.pri_dns=%s\n", buf_pri_dns);
196 printf("DATA_DEMO_CALL_BACK: v6.sec_dns=%s\n", buf_sec_dns);
197 #endif
b.liu8583dce2024-04-03 13:30:08 +0800198 //后续对回调函数中返回值的处理建议放在其它线程中进行,避免阻塞在回调函数中影响后续消息上报
199}