b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame^] | 1 | #include <stdint.h> |
| 2 | #include <string.h> |
| 3 | #include <stdbool.h> |
| 4 | #include <stdio.h> |
| 5 | #include <dlfcn.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <sys/socket.h> |
| 8 | #include <netinet/in.h> |
| 9 | #include <arpa/inet.h> |
| 10 | |
| 11 | //gsw include |
| 12 | |
| 13 | |
| 14 | #define GSW_HAL_SUCCESS 0 |
| 15 | #define GSW_HAL_FAIL -1 |
| 16 | #define GSW_HAL_MEM_INVAILD -2 |
| 17 | #define GSW_NW_OPERATION_NAME_LEN 128 |
| 18 | #define GSW_NW_PLMN_LEN 6 |
| 19 | #define GSW_NW_MCC_LEN 3 |
| 20 | #define GSW_NW_MNC_LEN 3 |
| 21 | #define GSW_NW_PARA_MAX_LEN 128 |
| 22 | #define GSW_MCC_MAX_LEN 16 |
| 23 | #define GSW_MNC_MAX_LEN 16 |
| 24 | #define GSW_APN_CHANNEL_MAX 10 |
| 25 | #define GSW_POXY_ADDR_MAX_LEN 64 |
| 26 | #define GSW_PDP_TYPE_MAX_LEN 16 |
| 27 | #define GSW_APN_MAX_LEN 32 |
| 28 | #define GSW_APN_ID_MAX_LEN 32 |
| 29 | #define GSW_APN_TYPE_MAX_LEN 50 |
| 30 | #define GSW_USR_MAX_LEN 16 |
| 31 | #define GSW_PWD_MAX_LEN 16 |
| 32 | #define GSW_AUTH_TYPE_MAX_LEN 50 |
| 33 | #define GSW_PROTOCO_MAX_LEN 64 |
| 34 | #define GSW_CARRIER_ID_MAX_LEN 64 |
| 35 | #define GSW_IFACE_NAME_MAX_LEN 50 |
| 36 | #define GSW_PDP_ADDR_MAX_LEN 64 |
| 37 | #define GSW_DNS_ADDR_MAX_LEN 64 |
| 38 | #define GSW_GETWAYS_ADDR_MAX_LEN 64 |
| 39 | #define GSW_RSP_MAX_LEN 256 |
| 40 | |
| 41 | typedef struct { |
| 42 | int handle; /**< data connection handle*/ |
| 43 | int cid; /**< data connection cid*/ |
| 44 | int state; /**< data connection state*/ |
| 45 | int fail_cause; /**< data connection fail cause*/ |
| 46 | char *type; /**< data connection type*/ |
| 47 | char *ifname; /**< ifname*/ |
| 48 | |
| 49 | char *v4_ip; /**< ipv4 address*/ |
| 50 | char *v4_pdns; /**< ipv4 pdns address*/ |
| 51 | char *v4_sdns; /**< ipv4 sdns address*/ |
| 52 | char *v4_gw; /**< ipv4 gateway address*/ |
| 53 | |
| 54 | char *v6_ip; /**< ipv6 address*/ |
| 55 | char *v6_pdns; /**< ipv6 gateway address*/ |
| 56 | char *v6_sdns; /**< ipv6 gateway address*/ |
| 57 | }Wan_State_ind_s; |
| 58 | |
| 59 | typedef struct { |
| 60 | char ip[GSW_PDP_ADDR_MAX_LEN]; /**< ip address */ |
| 61 | char pdns[GSW_DNS_ADDR_MAX_LEN]; /**< pdns address*/ |
| 62 | char sdns[GSW_DNS_ADDR_MAX_LEN]; /**< sdns address*/ |
| 63 | char gw[GSW_GETWAYS_ADDR_MAX_LEN]; /**< gate way address*/ |
| 64 | }V4_Addr_s; |
| 65 | |
| 66 | typedef struct { |
| 67 | char ip[GSW_PDP_ADDR_MAX_LEN]; /**< ip address*/ |
| 68 | char pdns[GSW_DNS_ADDR_MAX_LEN]; /**< pdns address*/ |
| 69 | char sdns[GSW_DNS_ADDR_MAX_LEN]; /**< sdns address*/ |
| 70 | }V6_Addr_s; |
| 71 | |
| 72 | typedef struct { |
| 73 | int handle; /**< data connection handle*/ |
| 74 | |
| 75 | /* req info */ |
| 76 | char mcc[GSW_MCC_MAX_LEN]; /**< apn mcc value*/ |
| 77 | char mnc[GSW_MNC_MAX_LEN]; /**< apn mnc value*/ |
| 78 | char apnid[GSW_APN_ID_MAX_LEN]; /**< apn id*/ |
| 79 | char apn[GSW_APN_MAX_LEN]; /**< apn name*/ |
| 80 | char apnType[GSW_APN_TYPE_MAX_LEN]; /**< apn type ipv4 ipv4v6 ipv6*/ |
| 81 | char usr[GSW_USR_MAX_LEN]; /**< apn usr name*/ |
| 82 | char pwd[GSW_PWD_MAX_LEN]; /**< apn password*/ |
| 83 | char authType[GSW_AUTH_TYPE_MAX_LEN]; /**< apn auth type*/ |
| 84 | char normalProtocol[GSW_PROTOCO_MAX_LEN]; /**< apn auth protoco*/ |
| 85 | char roamingProtocol[GSW_PROTOCO_MAX_LEN]; /**< apn auth protoco roaming*/ |
| 86 | char carrier[GSW_CARRIER_ID_MAX_LEN]; /**< carrier id*/ |
| 87 | |
| 88 | /* rsp */ |
| 89 | int cid; /**< data connection cid for mdm sdk*/ |
| 90 | int active; /**< pdn context active state: 2,actived; other,not active */ |
| 91 | char ifname[GSW_IFACE_NAME_MAX_LEN]; /**< if name of current datalink */ |
| 92 | int type; /**< data connection type*/ |
| 93 | V4_Addr_s v4; /**< ipv4 addr*/ |
| 94 | V6_Addr_s v6; /**< ipv6 addr*/ |
| 95 | int mtu; /**< mtu value*/ |
| 96 | |
| 97 | char out[GSW_RSP_MAX_LEN]; /**< sdk return value*/ |
| 98 | }Link_Info_s; |
| 99 | |
| 100 | typedef struct |
| 101 | { |
| 102 | uint64_t tx_pkts; /* the number of packet sent*/ |
| 103 | uint64_t tx_bytes; /* the number of packet sent bytes */ |
| 104 | uint64_t tx_dropped_pkts; /* the number of packet dropped by sent*/ |
| 105 | uint64_t rx_pkts; /* the number of packet received*/ |
| 106 | uint64_t rx_bytes; /* the number of packet receivedbytes */ |
| 107 | uint64_t rx_dropped_pkts; /* the number of packet dropped by received*/ |
| 108 | }gsw_data_pkt_stats; |
| 109 | |
| 110 | |
| 111 | typedef void (*gsw_data_call_evt_cb_t)(Wan_State_ind_s *linkState); |
| 112 | |
| 113 | int (*gsw_data_call_init)(gsw_data_call_evt_cb_t evt_cb); |
| 114 | int (*gsw_data_call_deinit)(void); |
| 115 | int (*gsw_data_call_set_apn)(Link_Info_s *LinkInf); |
| 116 | int (*gsw_data_call_connect)(int linkid, Link_Info_s *LinkInf); |
| 117 | int (*gsw_data_call_disconnect)(int linkid, Link_Info_s *LinkInf); |
| 118 | int (*gsw_get_data_call_pkt_stats)(int linkid, gsw_data_pkt_stats *data_pkt); |
| 119 | |
| 120 | #define lib_gsw_data_path "/lib/libgsw_lib.so" |
| 121 | static void *dlHandle_data = NULL; |
| 122 | |
| 123 | |
| 124 | static int gsw_data_api_import() |
| 125 | { |
| 126 | dlHandle_data = dlopen(lib_gsw_data_path, RTLD_NOW); |
| 127 | if (dlHandle_data == NULL) { |
| 128 | printf("error: %s\n", dlerror()); |
| 129 | return -1; |
| 130 | } |
| 131 | |
| 132 | gsw_data_call_init = (int (*) (gsw_data_call_evt_cb_t evt_cb))dlsym(dlHandle_data, "gsw_data_call_init"); |
| 133 | if (gsw_data_call_init == NULL) { |
| 134 | printf("gsw_data_call_init dlsym failed\n"); |
| 135 | return -1; |
| 136 | } |
| 137 | |
| 138 | gsw_data_call_deinit = (int (*) (void))dlsym(dlHandle_data, "gsw_data_call_deinit"); |
| 139 | if (gsw_data_call_deinit == NULL) { |
| 140 | printf("gsw_data_call_deinit dlsym failed\n"); |
| 141 | return -1; |
| 142 | } |
| 143 | |
| 144 | gsw_data_call_set_apn = (int (*)(Link_Info_s *LinkInf))dlsym(dlHandle_data, "gsw_data_call_set_apn"); |
| 145 | if (gsw_data_call_set_apn == NULL) { |
| 146 | printf("gsw_data_call_set_apn dlsym failed\n"); |
| 147 | return -1; |
| 148 | } |
| 149 | |
| 150 | gsw_data_call_connect = (int (*)(int linkid, Link_Info_s *LinkInf))dlsym(dlHandle_data, "gsw_data_call_connect"); |
| 151 | if (gsw_data_call_connect == NULL) { |
| 152 | printf("gsw_data_call_connect dlsym failed\n"); |
| 153 | return -1; |
| 154 | } |
| 155 | |
| 156 | gsw_data_call_disconnect = (int (*)(int linkid, Link_Info_s *LinkInf))dlsym(dlHandle_data, "gsw_data_call_disconnect"); |
| 157 | if (gsw_data_call_disconnect == NULL) { |
| 158 | printf("gsw_data_call_disconnect dlsym failed\n"); |
| 159 | return -1; |
| 160 | } |
| 161 | |
| 162 | gsw_get_data_call_pkt_stats = (int (*)(int linkid, gsw_data_pkt_stats *data_pkt))dlsym(dlHandle_data, "gsw_get_data_call_pkt_stats"); |
| 163 | if (gsw_get_data_call_pkt_stats == NULL) { |
| 164 | printf("gsw_get_data_call_pkt_stats dlsym failed\n"); |
| 165 | return -1; |
| 166 | } |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | void gsw_data_call_back(Wan_State_ind_s *linkState) |
| 172 | { |
| 173 | printf("gsw_data_call_back, handle = %d\n", linkState->handle); |
| 174 | printf("gsw_data_call_back, cid = %d\n", linkState->cid); |
| 175 | printf("gsw_data_call_back, state = %d\n", linkState->state); |
| 176 | printf("gsw_data_call_back, fail_cause = %d\n", linkState->fail_cause); |
| 177 | printf("gsw_data_call_back, type = %s\n", linkState->type); |
| 178 | printf("gsw_data_call_back, ifname = %s\n", linkState->ifname); |
| 179 | printf("gsw_data_call_back, v4_ip = %s\n", linkState->v4_ip); |
| 180 | printf("gsw_data_call_back, v4_pdns = %s\n", linkState->v4_pdns); |
| 181 | printf("gsw_data_call_back, v4_sdns = %s\n", linkState->v4_sdns); |
| 182 | printf("gsw_data_call_back, v4_gw = %s\n", linkState->v4_gw); |
| 183 | printf("gsw_data_call_back, v6_ip = %s\n", linkState->v6_ip); |
| 184 | printf("gsw_data_call_back, v6_pdns = %s\n", linkState->v6_pdns); |
| 185 | printf("gsw_data_call_back, v6_sdns = %s\n", linkState->v6_sdns); |
| 186 | } |
| 187 | |
| 188 | |
| 189 | void dump_items() |
| 190 | { |
| 191 | |
| 192 | printf("\n"); |
| 193 | printf("The current module is: data \n"); |
| 194 | |
| 195 | printf("-------------------------------------------------------------------------\n"); |
| 196 | printf("| index | function name |\n"); |
| 197 | printf("| 0 exit\n"); |
| 198 | printf(" 1 gsw_data_call_init\n"); |
| 199 | printf(" 2 gsw_data_call_deinit\n"); |
| 200 | printf(" 3 gsw_data_call_set_apn\n"); |
| 201 | printf(" 4 gsw_data_call_connect\n"); |
| 202 | printf(" 5 gsw_data_call_disconnect\n"); |
| 203 | printf(" 6 gsw_get_data_call_pkt_stats\n"); |
| 204 | printf("-------------------------------------------------------------------------\n"); |
| 205 | } |
| 206 | |
| 207 | int main() |
| 208 | { |
| 209 | int ret; |
| 210 | char operator[10]; |
| 211 | int opt; |
| 212 | |
| 213 | Link_Info_s* pub_LinkInf = (Link_Info_s*)malloc(sizeof(Link_Info_s)); |
| 214 | memset(pub_LinkInf, 0x0, sizeof(Link_Info_s)); |
| 215 | |
| 216 | Link_Info_s* pri_LinkInf = (Link_Info_s*)malloc(sizeof(Link_Info_s)); |
| 217 | memset(pri_LinkInf, 0x0, sizeof(Link_Info_s)); |
| 218 | |
| 219 | pub_LinkInf->handle = 0; |
| 220 | strcpy(pub_LinkInf->mcc,"460"); |
| 221 | strcpy(pub_LinkInf->mnc,"11"); |
| 222 | strcpy(pub_LinkInf->apn,"pub_apn"); |
| 223 | strcpy(pub_LinkInf->apnid,"pub_apnid"); |
| 224 | strcpy(pub_LinkInf->apnType,"default"); |
| 225 | strcpy(pub_LinkInf->usr,""); |
| 226 | strcpy(pub_LinkInf->pwd,""); |
| 227 | strcpy(pub_LinkInf->authType,"0"); |
| 228 | strcpy(pub_LinkInf->normalProtocol,"IPV4V6"); |
| 229 | strcpy(pub_LinkInf->roamingProtocol,"IPV4V6"); |
| 230 | strcpy(pub_LinkInf->carrier,"CHINA TELECOM"); |
| 231 | |
| 232 | pri_LinkInf->handle = 1; |
| 233 | strcpy(pri_LinkInf->mcc,"460"); |
| 234 | strcpy(pri_LinkInf->mnc,"11"); |
| 235 | strcpy(pri_LinkInf->apn,"pri_apn"); |
| 236 | strcpy(pri_LinkInf->apnid,"pri_apnid"); |
| 237 | strcpy(pri_LinkInf->apnType,"mms"); |
| 238 | strcpy(pri_LinkInf->usr,""); |
| 239 | strcpy(pri_LinkInf->pwd,""); |
| 240 | strcpy(pri_LinkInf->authType,"0"); |
| 241 | strcpy(pri_LinkInf->normalProtocol,"IPV4V6"); |
| 242 | strcpy(pri_LinkInf->roamingProtocol,"IPV4V6"); |
| 243 | strcpy(pri_LinkInf->carrier,"CHINA TELECOM"); |
| 244 | |
| 245 | |
| 246 | ret = gsw_data_api_import(); |
| 247 | if (ret != 0) { |
| 248 | printf("gsw_data_api_import failed \n"); |
| 249 | free(pub_LinkInf); |
| 250 | free(pri_LinkInf); |
| 251 | return -1; |
| 252 | } |
| 253 | |
| 254 | |
| 255 | while(1) |
| 256 | { |
| 257 | dump_items(); |
| 258 | printf("input the index of module: "); |
| 259 | opt = -1; |
| 260 | printf("%s/n",fgets(operator, sizeof(operator), stdin)); |
| 261 | fflush(stdin); |
| 262 | |
| 263 | opt = atoi(operator); |
| 264 | switch (opt) { |
| 265 | case 0: |
| 266 | { |
| 267 | printf("exit\n"); |
| 268 | free(pub_LinkInf); |
| 269 | free(pri_LinkInf); |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | case 1: |
| 274 | { |
| 275 | printf("gsw_data_call_init start\n"); |
| 276 | ret = gsw_data_call_init(gsw_data_call_back); |
| 277 | if (ret != 0) { |
| 278 | printf("gsw_data_call_init failed,ret = %d\n",ret); |
| 279 | continue; |
| 280 | } |
| 281 | printf("gsw_data_call_init success\n"); |
| 282 | break; |
| 283 | } |
| 284 | |
| 285 | case 2: |
| 286 | { |
| 287 | printf("gsw_data_call_deinit start\n"); |
| 288 | ret = gsw_data_call_deinit(); |
| 289 | if (ret != 0) { |
| 290 | printf("gsw_data_call_deinit failed,ret = %d\n",ret); |
| 291 | continue; |
| 292 | } |
| 293 | printf("gsw_data_call_deinit success\n"); |
| 294 | break; |
| 295 | } |
| 296 | case 3: |
| 297 | { |
| 298 | printf("gsw_data_call_set_apn start\n"); |
| 299 | ret = gsw_data_call_set_apn(pub_LinkInf); |
| 300 | if (ret != 0) { |
| 301 | printf("gsw_data_call_set_apn failed,ret = %d\n",ret); |
| 302 | continue; |
| 303 | } |
| 304 | printf("gsw_data_call_set_apn success\n"); |
| 305 | break; |
| 306 | } |
| 307 | |
| 308 | case 4: |
| 309 | { |
| 310 | printf("gsw_data_call_connect start\n"); |
| 311 | ret = gsw_data_call_connect(pub_LinkInf->handle, pub_LinkInf); |
| 312 | if (ret != 0) { |
| 313 | printf("gsw_data_call_connect failed, ret = %d\n",ret); |
| 314 | continue; |
| 315 | } |
| 316 | printf("gsw_data_call_connect success\n"); |
| 317 | break; |
| 318 | } |
| 319 | |
| 320 | case 5: |
| 321 | { |
| 322 | printf("gsw_data_call_disconnect start\n"); |
| 323 | ret = gsw_data_call_disconnect(pub_LinkInf->handle, pub_LinkInf); |
| 324 | if (ret != 0) { |
| 325 | printf("gsw_data_call_disconnect failed,ret = %d\n",ret); |
| 326 | continue; |
| 327 | } |
| 328 | printf("gsw_data_call_disconnect success\n"); |
| 329 | break; |
| 330 | } |
| 331 | |
| 332 | case 6: |
| 333 | { |
| 334 | printf("gsw_get_data_call_pkt_stats start\n"); |
| 335 | gsw_data_pkt_stats* data_pkt = (gsw_data_pkt_stats*)malloc(sizeof(gsw_data_pkt_stats)); |
| 336 | memset(data_pkt, 0x0, sizeof(gsw_data_pkt_stats)); |
| 337 | ret = gsw_get_data_call_pkt_stats(pub_LinkInf->handle, data_pkt); |
| 338 | if (ret != 0) { |
| 339 | printf("gsw_get_data_call_pkt_stats failed,ret = %d\n",ret); |
| 340 | continue; |
| 341 | } |
| 342 | else |
| 343 | { |
| 344 | printf("gsw_get_data_call_pkt_stats success \n"); |
| 345 | printf("txPkts = %llu\n", data_pkt->tx_pkts); |
| 346 | printf("txbytes = %llu\n", data_pkt->tx_bytes); |
| 347 | printf("tx_dropped_pkts = %llu\n", data_pkt->tx_dropped_pkts); |
| 348 | printf("rxPkts = %llu\n", data_pkt->rx_pkts); |
| 349 | printf("rxbytes = %llu\n", data_pkt->rx_bytes); |
| 350 | printf("rx_dropped_pkts = %llu\n", data_pkt->rx_dropped_pkts); |
| 351 | free(data_pkt); |
| 352 | } |
| 353 | printf("gsw_get_data_call_pkt_stats success\n"); |
| 354 | break; |
| 355 | } |
| 356 | |
| 357 | default: |
| 358 | { |
| 359 | continue; |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | |