| #include <stdint.h> |
| #include <string.h> |
| #include <stdbool.h> |
| #include <stdio.h> |
| #include <dlfcn.h> |
| #include <stdlib.h> |
| #include <sys/socket.h> |
| #include <netinet/in.h> |
| #include <arpa/inet.h> |
| |
| //gsw include |
| |
| |
| #define GSW_HAL_SUCCESS 0 |
| #define GSW_HAL_FAIL -1 |
| #define GSW_HAL_MEM_INVAILD -2 |
| #define GSW_NW_OPERATION_NAME_LEN 128 |
| #define GSW_NW_PLMN_LEN 6 |
| #define GSW_NW_MCC_LEN 3 |
| #define GSW_NW_MNC_LEN 3 |
| #define GSW_NW_PARA_MAX_LEN 128 |
| #define GSW_MCC_MAX_LEN 16 |
| #define GSW_MNC_MAX_LEN 16 |
| #define GSW_APN_CHANNEL_MAX 10 |
| #define GSW_POXY_ADDR_MAX_LEN 64 |
| #define GSW_PDP_TYPE_MAX_LEN 16 |
| #define GSW_APN_MAX_LEN 32 |
| #define GSW_APN_ID_MAX_LEN 32 |
| #define GSW_APN_TYPE_MAX_LEN 50 |
| #define GSW_USR_MAX_LEN 16 |
| #define GSW_PWD_MAX_LEN 16 |
| #define GSW_AUTH_TYPE_MAX_LEN 50 |
| #define GSW_PROTOCO_MAX_LEN 64 |
| #define GSW_CARRIER_ID_MAX_LEN 64 |
| #define GSW_IFACE_NAME_MAX_LEN 50 |
| #define GSW_PDP_ADDR_MAX_LEN 64 |
| #define GSW_DNS_ADDR_MAX_LEN 64 |
| #define GSW_GETWAYS_ADDR_MAX_LEN 64 |
| #define GSW_RSP_MAX_LEN 256 |
| |
| typedef struct { |
| int handle; /**< data connection handle*/ |
| int cid; /**< data connection cid*/ |
| int state; /**< data connection state*/ |
| int fail_cause; /**< data connection fail cause*/ |
| char *type; /**< data connection type*/ |
| char *ifname; /**< ifname*/ |
| |
| char *v4_ip; /**< ipv4 address*/ |
| char *v4_pdns; /**< ipv4 pdns address*/ |
| char *v4_sdns; /**< ipv4 sdns address*/ |
| char *v4_gw; /**< ipv4 gateway address*/ |
| |
| char *v6_ip; /**< ipv6 address*/ |
| char *v6_pdns; /**< ipv6 gateway address*/ |
| char *v6_sdns; /**< ipv6 gateway address*/ |
| }Wan_State_ind_s; |
| |
| typedef struct { |
| char ip[GSW_PDP_ADDR_MAX_LEN]; /**< ip address */ |
| char pdns[GSW_DNS_ADDR_MAX_LEN]; /**< pdns address*/ |
| char sdns[GSW_DNS_ADDR_MAX_LEN]; /**< sdns address*/ |
| char gw[GSW_GETWAYS_ADDR_MAX_LEN]; /**< gate way address*/ |
| }V4_Addr_s; |
| |
| typedef struct { |
| char ip[GSW_PDP_ADDR_MAX_LEN]; /**< ip address*/ |
| char pdns[GSW_DNS_ADDR_MAX_LEN]; /**< pdns address*/ |
| char sdns[GSW_DNS_ADDR_MAX_LEN]; /**< sdns address*/ |
| }V6_Addr_s; |
| |
| typedef struct { |
| int handle; /**< data connection handle*/ |
| |
| /* req info */ |
| char mcc[GSW_MCC_MAX_LEN]; /**< apn mcc value*/ |
| char mnc[GSW_MNC_MAX_LEN]; /**< apn mnc value*/ |
| char apnid[GSW_APN_ID_MAX_LEN]; /**< apn id*/ |
| char apn[GSW_APN_MAX_LEN]; /**< apn name*/ |
| char apnType[GSW_APN_TYPE_MAX_LEN]; /**< apn type ipv4 ipv4v6 ipv6*/ |
| char usr[GSW_USR_MAX_LEN]; /**< apn usr name*/ |
| char pwd[GSW_PWD_MAX_LEN]; /**< apn password*/ |
| char authType[GSW_AUTH_TYPE_MAX_LEN]; /**< apn auth type*/ |
| char normalProtocol[GSW_PROTOCO_MAX_LEN]; /**< apn auth protoco*/ |
| char roamingProtocol[GSW_PROTOCO_MAX_LEN]; /**< apn auth protoco roaming*/ |
| char carrier[GSW_CARRIER_ID_MAX_LEN]; /**< carrier id*/ |
| |
| /* rsp */ |
| int cid; /**< data connection cid for mdm sdk*/ |
| int active; /**< pdn context active state: 2,actived; other,not active */ |
| char ifname[GSW_IFACE_NAME_MAX_LEN]; /**< if name of current datalink */ |
| int type; /**< data connection type*/ |
| V4_Addr_s v4; /**< ipv4 addr*/ |
| V6_Addr_s v6; /**< ipv6 addr*/ |
| int mtu; /**< mtu value*/ |
| |
| char out[GSW_RSP_MAX_LEN]; /**< sdk return value*/ |
| }Link_Info_s; |
| |
| typedef struct |
| { |
| uint64_t tx_pkts; /* the number of packet sent*/ |
| uint64_t tx_bytes; /* the number of packet sent bytes */ |
| uint64_t tx_dropped_pkts; /* the number of packet dropped by sent*/ |
| uint64_t rx_pkts; /* the number of packet received*/ |
| uint64_t rx_bytes; /* the number of packet receivedbytes */ |
| uint64_t rx_dropped_pkts; /* the number of packet dropped by received*/ |
| }gsw_data_pkt_stats; |
| |
| |
| typedef void (*gsw_data_call_evt_cb_t)(Wan_State_ind_s *linkState); |
| |
| int (*gsw_data_call_init)(gsw_data_call_evt_cb_t evt_cb); |
| int (*gsw_data_call_deinit)(void); |
| int (*gsw_data_call_set_apn)(Link_Info_s *LinkInf); |
| int (*gsw_data_call_connect)(int linkid, Link_Info_s *LinkInf); |
| int (*gsw_data_call_disconnect)(int linkid, Link_Info_s *LinkInf); |
| int (*gsw_get_data_call_pkt_stats)(int linkid, gsw_data_pkt_stats *data_pkt); |
| |
| #define lib_gsw_data_path "/lib/libgsw_lib.so" |
| static void *dlHandle_data = NULL; |
| |
| |
| static int gsw_data_api_import() |
| { |
| dlHandle_data = dlopen(lib_gsw_data_path, RTLD_NOW); |
| if (dlHandle_data == NULL) { |
| printf("error: %s\n", dlerror()); |
| return -1; |
| } |
| |
| gsw_data_call_init = (int (*) (gsw_data_call_evt_cb_t evt_cb))dlsym(dlHandle_data, "gsw_data_call_init"); |
| if (gsw_data_call_init == NULL) { |
| printf("gsw_data_call_init dlsym failed\n"); |
| return -1; |
| } |
| |
| gsw_data_call_deinit = (int (*) (void))dlsym(dlHandle_data, "gsw_data_call_deinit"); |
| if (gsw_data_call_deinit == NULL) { |
| printf("gsw_data_call_deinit dlsym failed\n"); |
| return -1; |
| } |
| |
| gsw_data_call_set_apn = (int (*)(Link_Info_s *LinkInf))dlsym(dlHandle_data, "gsw_data_call_set_apn"); |
| if (gsw_data_call_set_apn == NULL) { |
| printf("gsw_data_call_set_apn dlsym failed\n"); |
| return -1; |
| } |
| |
| gsw_data_call_connect = (int (*)(int linkid, Link_Info_s *LinkInf))dlsym(dlHandle_data, "gsw_data_call_connect"); |
| if (gsw_data_call_connect == NULL) { |
| printf("gsw_data_call_connect dlsym failed\n"); |
| return -1; |
| } |
| |
| gsw_data_call_disconnect = (int (*)(int linkid, Link_Info_s *LinkInf))dlsym(dlHandle_data, "gsw_data_call_disconnect"); |
| if (gsw_data_call_disconnect == NULL) { |
| printf("gsw_data_call_disconnect dlsym failed\n"); |
| return -1; |
| } |
| |
| gsw_get_data_call_pkt_stats = (int (*)(int linkid, gsw_data_pkt_stats *data_pkt))dlsym(dlHandle_data, "gsw_get_data_call_pkt_stats"); |
| if (gsw_get_data_call_pkt_stats == NULL) { |
| printf("gsw_get_data_call_pkt_stats dlsym failed\n"); |
| return -1; |
| } |
| |
| return 0; |
| } |
| |
| void gsw_data_call_back(Wan_State_ind_s *linkState) |
| { |
| printf("gsw_data_call_back, handle = %d\n", linkState->handle); |
| printf("gsw_data_call_back, cid = %d\n", linkState->cid); |
| printf("gsw_data_call_back, state = %d\n", linkState->state); |
| printf("gsw_data_call_back, fail_cause = %d\n", linkState->fail_cause); |
| printf("gsw_data_call_back, type = %s\n", linkState->type); |
| printf("gsw_data_call_back, ifname = %s\n", linkState->ifname); |
| printf("gsw_data_call_back, v4_ip = %s\n", linkState->v4_ip); |
| printf("gsw_data_call_back, v4_pdns = %s\n", linkState->v4_pdns); |
| printf("gsw_data_call_back, v4_sdns = %s\n", linkState->v4_sdns); |
| printf("gsw_data_call_back, v4_gw = %s\n", linkState->v4_gw); |
| printf("gsw_data_call_back, v6_ip = %s\n", linkState->v6_ip); |
| printf("gsw_data_call_back, v6_pdns = %s\n", linkState->v6_pdns); |
| printf("gsw_data_call_back, v6_sdns = %s\n", linkState->v6_sdns); |
| } |
| |
| |
| void dump_items() |
| { |
| |
| printf("\n"); |
| printf("The current module is: data \n"); |
| |
| printf("-------------------------------------------------------------------------\n"); |
| printf("| index | function name |\n"); |
| printf("| 0 exit\n"); |
| printf(" 1 gsw_data_call_init\n"); |
| printf(" 2 gsw_data_call_deinit\n"); |
| printf(" 3 gsw_data_call_set_apn\n"); |
| printf(" 4 gsw_data_call_connect\n"); |
| printf(" 5 gsw_data_call_disconnect\n"); |
| printf(" 6 gsw_get_data_call_pkt_stats\n"); |
| printf("-------------------------------------------------------------------------\n"); |
| } |
| |
| int main() |
| { |
| int ret; |
| char operator[10]; |
| int opt; |
| |
| Link_Info_s* pub_LinkInf = (Link_Info_s*)malloc(sizeof(Link_Info_s)); |
| memset(pub_LinkInf, 0x0, sizeof(Link_Info_s)); |
| |
| Link_Info_s* pri_LinkInf = (Link_Info_s*)malloc(sizeof(Link_Info_s)); |
| memset(pri_LinkInf, 0x0, sizeof(Link_Info_s)); |
| |
| pub_LinkInf->handle = 0; |
| strcpy(pub_LinkInf->mcc,"460"); |
| strcpy(pub_LinkInf->mnc,"11"); |
| strcpy(pub_LinkInf->apn,"pub_apn"); |
| strcpy(pub_LinkInf->apnid,"pub_apnid"); |
| strcpy(pub_LinkInf->apnType,"default"); |
| strcpy(pub_LinkInf->usr,""); |
| strcpy(pub_LinkInf->pwd,""); |
| strcpy(pub_LinkInf->authType,"0"); |
| strcpy(pub_LinkInf->normalProtocol,"IPV4V6"); |
| strcpy(pub_LinkInf->roamingProtocol,"IPV4V6"); |
| strcpy(pub_LinkInf->carrier,"CHINA TELECOM"); |
| |
| pri_LinkInf->handle = 1; |
| strcpy(pri_LinkInf->mcc,"460"); |
| strcpy(pri_LinkInf->mnc,"11"); |
| strcpy(pri_LinkInf->apn,"pri_apn"); |
| strcpy(pri_LinkInf->apnid,"pri_apnid"); |
| strcpy(pri_LinkInf->apnType,"mms"); |
| strcpy(pri_LinkInf->usr,""); |
| strcpy(pri_LinkInf->pwd,""); |
| strcpy(pri_LinkInf->authType,"0"); |
| strcpy(pri_LinkInf->normalProtocol,"IPV4V6"); |
| strcpy(pri_LinkInf->roamingProtocol,"IPV4V6"); |
| strcpy(pri_LinkInf->carrier,"CHINA TELECOM"); |
| |
| |
| ret = gsw_data_api_import(); |
| if (ret != 0) { |
| printf("gsw_data_api_import failed \n"); |
| free(pub_LinkInf); |
| free(pri_LinkInf); |
| return -1; |
| } |
| |
| |
| while(1) |
| { |
| dump_items(); |
| printf("input the index of module: "); |
| opt = -1; |
| printf("%s/n",fgets(operator, sizeof(operator), stdin)); |
| fflush(stdin); |
| |
| opt = atoi(operator); |
| switch (opt) { |
| case 0: |
| { |
| printf("exit\n"); |
| free(pub_LinkInf); |
| free(pri_LinkInf); |
| return 0; |
| } |
| |
| case 1: |
| { |
| printf("gsw_data_call_init start\n"); |
| ret = gsw_data_call_init(gsw_data_call_back); |
| if (ret != 0) { |
| printf("gsw_data_call_init failed,ret = %d\n",ret); |
| continue; |
| } |
| printf("gsw_data_call_init success\n"); |
| break; |
| } |
| |
| case 2: |
| { |
| printf("gsw_data_call_deinit start\n"); |
| ret = gsw_data_call_deinit(); |
| if (ret != 0) { |
| printf("gsw_data_call_deinit failed,ret = %d\n",ret); |
| continue; |
| } |
| printf("gsw_data_call_deinit success\n"); |
| break; |
| } |
| case 3: |
| { |
| printf("gsw_data_call_set_apn start\n"); |
| ret = gsw_data_call_set_apn(pub_LinkInf); |
| if (ret != 0) { |
| printf("gsw_data_call_set_apn failed,ret = %d\n",ret); |
| continue; |
| } |
| printf("gsw_data_call_set_apn success\n"); |
| break; |
| } |
| |
| case 4: |
| { |
| printf("gsw_data_call_connect start\n"); |
| ret = gsw_data_call_connect(pub_LinkInf->handle, pub_LinkInf); |
| if (ret != 0) { |
| printf("gsw_data_call_connect failed, ret = %d\n",ret); |
| continue; |
| } |
| printf("gsw_data_call_connect success\n"); |
| break; |
| } |
| |
| case 5: |
| { |
| printf("gsw_data_call_disconnect start\n"); |
| ret = gsw_data_call_disconnect(pub_LinkInf->handle, pub_LinkInf); |
| if (ret != 0) { |
| printf("gsw_data_call_disconnect failed,ret = %d\n",ret); |
| continue; |
| } |
| printf("gsw_data_call_disconnect success\n"); |
| break; |
| } |
| |
| case 6: |
| { |
| printf("gsw_get_data_call_pkt_stats start\n"); |
| gsw_data_pkt_stats* data_pkt = (gsw_data_pkt_stats*)malloc(sizeof(gsw_data_pkt_stats)); |
| memset(data_pkt, 0x0, sizeof(gsw_data_pkt_stats)); |
| ret = gsw_get_data_call_pkt_stats(pub_LinkInf->handle, data_pkt); |
| if (ret != 0) { |
| printf("gsw_get_data_call_pkt_stats failed,ret = %d\n",ret); |
| continue; |
| } |
| else |
| { |
| printf("gsw_get_data_call_pkt_stats success \n"); |
| printf("txPkts = %llu\n", data_pkt->tx_pkts); |
| printf("txbytes = %llu\n", data_pkt->tx_bytes); |
| printf("tx_dropped_pkts = %llu\n", data_pkt->tx_dropped_pkts); |
| printf("rxPkts = %llu\n", data_pkt->rx_pkts); |
| printf("rxbytes = %llu\n", data_pkt->rx_bytes); |
| printf("rx_dropped_pkts = %llu\n", data_pkt->rx_dropped_pkts); |
| free(data_pkt); |
| } |
| printf("gsw_get_data_call_pkt_stats success\n"); |
| break; |
| } |
| |
| default: |
| { |
| continue; |
| } |
| } |
| } |
| } |
| |
| |