#include "gsw_nw_interface.h" | |
#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> | |
#include <pthread.h> | |
//mbtk include | |
#define MBTK_APN_NAME_SIZE 150+1 | |
#define MBTK_APN_USERNAME_SIZE 127+1 | |
#define MBTK_APN_PASSWORD_SIZE 127+1 | |
#define MBTK_APN_TYPE_SIZE 127+1 | |
#ifndef FALSE | |
#define FALSE (0) | |
#endif | |
#ifndef TRUE | |
#define TRUE (!FALSE) | |
#endif | |
typedef unsigned int uint32; | |
typedef unsigned char uint8; | |
typedef unsigned short uint16; | |
typedef void (*mbtk_info_callback_func)(const void* data, int data_len); | |
typedef unsigned long long uint64_t; | |
typedef struct | |
{ | |
int client_fd; | |
pthread_t read_thread_id; | |
int exit_fd[2]; | |
bool is_waitting; | |
pthread_cond_t cond; | |
pthread_mutex_t mutex; | |
pthread_mutex_t send_mutex; | |
// Temp response data. | |
uint16 info_err; | |
uint16 data_len; | |
void *data; | |
//mbtk wyq for server_ready_status add start | |
char server_ready_status; | |
//mbtk wyq for server_ready_status add end | |
mbtk_info_callback_func net_state_cb; | |
mbtk_info_callback_func call_state_cb; | |
mbtk_info_callback_func sms_state_cb; | |
mbtk_info_callback_func radio_state_cb; | |
mbtk_info_callback_func sim_state_cb; | |
mbtk_info_callback_func pdp_state_cb; | |
//add signal by xr | |
mbtk_info_callback_func signal_state_cb; | |
} mbtk_info_handle_t; | |
typedef enum { | |
MBTK_IP_TYPE_IP, | |
MBTK_IP_TYPE_IPV6, | |
MBTK_IP_TYPE_IPV4V6, | |
MBTK_IP_TYPE_PPP | |
} mbtk_ip_type_enum; | |
typedef enum { | |
MBTK_APN_REQ_TYPE_SET = 0, // set apn req | |
MBTK_APN_REQ_TYPE_ADD // add apn req | |
}mbtk_apn_req_type_enum; | |
typedef enum { | |
MBTK_APN_AUTH_PROTO_DEFAULT = 0, | |
MBTK_APN_AUTH_PROTO_NONE, | |
MBTK_APN_AUTH_PROTO_PAP, | |
MBTK_APN_AUTH_PROTO_CHAP, | |
#if 0 | |
MBTK_APN_AUTH_PROTO_PAP_CHAP, | |
//NOT SUPPORT | |
#endif | |
} mbtk_apn_auth_proto_enum; | |
typedef struct { | |
bool valid; | |
uint32 IPAddr; | |
uint32 PrimaryDNS; | |
uint32 SecondaryDNS; | |
uint32 GateWay; | |
uint32 NetMask; | |
} __attribute__((packed)) mbtk_ipv4_info_t; | |
typedef struct { | |
bool valid; | |
uint32 IPV6Addr[4]; | |
uint32 PrimaryDNS[4]; | |
uint32 SecondaryDNS[4]; | |
uint32 GateWay[4]; | |
uint32 NetMask[4]; | |
} __attribute__((packed)) mbtk_ipv6_info_t; | |
typedef struct { | |
int cid; /*!< UMTS/CDMA profile ID. range: 0 - 7*/ | |
mbtk_ip_type_enum ip_type; /*!< Packet Data Protocol (PDP) type specifies the type of data payload | |
exchanged over the airlink when the packet data session is | |
established with this profile. */ | |
mbtk_apn_req_type_enum req_type; /*!< apn req type*/ | |
mbtk_apn_auth_proto_enum auth_proto; /*!< Authentication Protocol. */ | |
uint8 apn_name[MBTK_APN_NAME_SIZE]; /*!< A string parameter that is a logical name used to select the GGSN | |
and external packet data network. */ | |
uint8 user_name[MBTK_APN_USERNAME_SIZE]; /*!< Username used during data network authentication. */ | |
uint8 user_pass[MBTK_APN_PASSWORD_SIZE]; /*!< Password to be used during data network authentication. */ | |
uint8 apn_type[MBTK_APN_TYPE_SIZE]; | |
} mbtk_qser_apn_info_s; | |
static mbtk_info_handle_t* (*mbtk_info_handle_get)(void); | |
static int (*mbtk_info_handle_free)(mbtk_info_handle_t** handle); | |
int (*mbtk_pdp_state_change_cb_reg)(mbtk_info_handle_t* handle, mbtk_info_callback_func cb); | |
int (*mbtk_qser_apn_set)(mbtk_info_handle_t* handle, mbtk_qser_apn_info_s *apninfo, unsigned char *cid); | |
int (*mbtk_data_call_start)(mbtk_info_handle_t* handle, int cid, int auto_conn_interval, bool boot_conn, int timeout); | |
int (*mbtk_data_call_stop)(mbtk_info_handle_t* handle, int cid, int timeout); | |
int (*mbtk_data_call_state_get)(mbtk_info_handle_t* handle, int cid, mbtk_ipv4_info_t *ipv4, mbtk_ipv6_info_t *ipv6); | |
int (*mbtk_apn_set)(mbtk_info_handle_t* handle, int cid, mbtk_ip_type_enum ip_type, const void* apn_name, const void *user_name, const void *user_pass, const void *auth); | |
int (*ipv6_2_str)(const void *ipv6, void *ipv6_str); | |
static void (*mbtk_log)(int level, const char *format, ...); | |
static void (*mbtk_log_init)(char *path, char *tag); | |
#ifndef LOG_ERR_LEVEL | |
#define LOG_ERR_LEVEL 3 /* error conditions */ | |
#endif | |
#ifndef LOG_WARN_LEVEL | |
#define LOG_WARN_LEVEL 4 /* warning conditions */ | |
#endif | |
#ifndef LOG_INFO_LEVEL | |
#define LOG_INFO_LEVEL 6 /* informational */ | |
#endif | |
#ifndef LOG_DEBUG_LEVEL | |
#define LOG_DEBUG_LEVEL 7 /* debug-level messages */ | |
#endif | |
#ifndef LOG_VERBOSE_LEVEL | |
#define LOG_VERBOSE_LEVEL 8 | |
#endif | |
#define LOGV(fmt, args ...) \ | |
do{ \ | |
char *file_ptr_1001 = __FILE__; \ | |
char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \ | |
char line_1001[10] = {0}; \ | |
sprintf(line_1001, "%d", __LINE__); \ | |
while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \ | |
if(*ptr_1001 == '/') \ | |
break; \ | |
ptr_1001--; \ | |
} \ | |
mbtk_log(LOG_VERBOSE_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \ | |
} while(0) | |
#define LOGI(fmt, args...) \ | |
do{ \ | |
char *file_ptr_1001 = __FILE__; \ | |
char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \ | |
char line_1001[10] = {0}; \ | |
sprintf(line_1001, "%d", __LINE__); \ | |
while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \ | |
if(*ptr_1001 == '/') \ | |
break; \ | |
ptr_1001--; \ | |
} \ | |
mbtk_log(LOG_INFO_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \ | |
} while(0) | |
#define LOGD(fmt, args...) \ | |
do{ \ | |
char *file_ptr_1001 = __FILE__; \ | |
char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \ | |
char line_1001[10] = {0}; \ | |
sprintf(line_1001, "%d", __LINE__); \ | |
while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \ | |
if(*ptr_1001 == '/') \ | |
break; \ | |
ptr_1001--; \ | |
} \ | |
mbtk_log(LOG_DEBUG_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \ | |
} while(0) | |
#define LOGW(fmt, args...) \ | |
do{ \ | |
char *file_ptr_1001 = __FILE__; \ | |
char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \ | |
char line_1001[10] = {0}; \ | |
sprintf(line_1001, "%d", __LINE__); \ | |
while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \ | |
if(*ptr_1001 == '/') \ | |
break; \ | |
ptr_1001--; \ | |
} \ | |
mbtk_log(LOG_WARN_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \ | |
} while(0) | |
#define LOGE(fmt, args...) \ | |
do{ \ | |
char *file_ptr_1001 = __FILE__; \ | |
char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \ | |
char line_1001[10] = {0}; \ | |
sprintf(line_1001, "%d", __LINE__); \ | |
while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \ | |
if(*ptr_1001 == '/') \ | |
break; \ | |
ptr_1001--; \ | |
} \ | |
mbtk_log(LOG_ERR_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \ | |
} while(0) | |
//define | |
#define GSW_HAL_FAIL GSW_HAL_NORMAL_FAIL | |
#define lib_mbtk_path "/lib/libmbtk_lib.so" | |
pthread_mutex_t data_mutex = PTHREAD_MUTEX_INITIALIZER; | |
static int data_call_lock = 0; | |
static int data_init_flag = 0; | |
static void *dlHandle_mbtk; | |
static mbtk_info_handle_t* data_info_handle = NULL; | |
static gsw_data_call_evt_cb_t gsw_data_call_evt_cb; | |
Wan_State_ind_s *linkState_arr; | |
static int Wan_State_ind_malloc(Wan_State_ind_s **linkState) | |
{ | |
(*linkState) = (Wan_State_ind_s *)malloc(sizeof(Wan_State_ind_s)); | |
if((*linkState) == NULL) | |
{ | |
LOGE("malloc Wan_State_ind_s fail\n"); | |
return GSW_HAL_FAIL; | |
} | |
memset((*linkState), 0x00, sizeof(Wan_State_ind_s)); | |
(*linkState)->handle = -1; | |
(*linkState)->cid = -1; | |
(*linkState)->state = 0; | |
(*linkState)->fail_cause = 0; | |
(*linkState)->type = (char *)malloc(GSW_PDP_TYPE_MAX_LEN); | |
memset((*linkState)->type, 0x00, GSW_PDP_TYPE_MAX_LEN); | |
(*linkState)->ifname = (char *)malloc(GSW_IFACE_NAME_MAX_LEN); | |
memset((*linkState)->ifname, 0x00, GSW_IFACE_NAME_MAX_LEN); | |
(*linkState)->v4_ip = (char *)malloc(GSW_PDP_ADDR_MAX_LEN); | |
memset((*linkState)->v4_ip, 0x00, GSW_PDP_ADDR_MAX_LEN); | |
(*linkState)->v4_pdns = (char *)malloc(GSW_DNS_ADDR_MAX_LEN); | |
memset((*linkState)->v4_pdns, 0x00, GSW_DNS_ADDR_MAX_LEN); | |
(*linkState)->v4_sdns = (char *)malloc(GSW_DNS_ADDR_MAX_LEN); | |
memset((*linkState)->v4_sdns, 0x00, GSW_DNS_ADDR_MAX_LEN); | |
(*linkState)->v4_gw = (char *)malloc(GSW_GETWAYS_ADDR_MAX_LEN); | |
memset((*linkState)->v4_gw, 0x00, GSW_GETWAYS_ADDR_MAX_LEN); | |
(*linkState)->v6_ip = (char *)malloc(GSW_PDP_ADDR_MAX_LEN); | |
memset((*linkState)->v6_ip, 0x00, GSW_PDP_ADDR_MAX_LEN); | |
(*linkState)->v6_pdns = (char *)malloc(GSW_DNS_ADDR_MAX_LEN); | |
memset((*linkState)->v6_pdns, 0x00, GSW_DNS_ADDR_MAX_LEN); | |
(*linkState)->v6_sdns = (char *)malloc(GSW_DNS_ADDR_MAX_LEN); | |
memset((*linkState)->v6_sdns, 0x00, GSW_DNS_ADDR_MAX_LEN); | |
return GSW_HAL_SUCCESS; | |
} | |
static int Wan_State_ind_free(Wan_State_ind_s **linkState) | |
{ | |
if ((*linkState) == NULL) | |
{ | |
LOGE("(*linkState) is null\n"); | |
return GSW_HAL_FAIL; | |
} | |
free((*linkState)->type); | |
(*linkState)->type = NULL; | |
free((*linkState)->ifname); | |
(*linkState)->ifname = NULL; | |
free((*linkState)->v4_ip); | |
(*linkState)->v4_ip = NULL; | |
free((*linkState)->v4_pdns); | |
(*linkState)->v4_pdns = NULL; | |
free((*linkState)->v4_sdns); | |
(*linkState)->v4_sdns = NULL; | |
free((*linkState)->v4_gw); | |
(*linkState)->v4_gw = NULL; | |
free((*linkState)->v6_ip); | |
(*linkState)->v6_ip = NULL; | |
free((*linkState)->v6_pdns); | |
(*linkState)->v6_pdns = NULL; | |
free((*linkState)->v6_sdns); | |
(*linkState)->v6_sdns = NULL; | |
free((*linkState)); | |
(*linkState) = NULL; | |
return GSW_HAL_SUCCESS; | |
} | |
static int mbtk_data_api_import() | |
{ | |
dlHandle_mbtk = dlopen(lib_mbtk_path, RTLD_NOW); | |
if (dlHandle_mbtk == NULL) | |
{ | |
return GSW_HAL_FAIL; | |
} | |
mbtk_log_init = (void (*)(char *path, char *tag))dlsym(dlHandle_mbtk, "mbtk_log_init"); | |
if (mbtk_log_init == NULL) | |
{ | |
return GSW_HAL_FAIL; | |
} | |
mbtk_log = (void (*)(int level, const char *format, ...))dlsym(dlHandle_mbtk, "mbtk_log"); | |
if (mbtk_log == NULL) | |
{ | |
return GSW_HAL_FAIL; | |
} | |
mbtk_info_handle_get = (mbtk_info_handle_t* (*)(void))dlsym(dlHandle_mbtk, "mbtk_info_handle_get"); | |
if (mbtk_info_handle_get == NULL) | |
{ | |
LOGE("mbtk_info_handle_get dlsym fail\n"); | |
return GSW_HAL_FAIL; | |
} | |
mbtk_info_handle_free = (int (*)(mbtk_info_handle_t** handle))dlsym(dlHandle_mbtk, "mbtk_info_handle_free"); | |
if (mbtk_info_handle_free == NULL) | |
{ | |
LOGE("mbtk_info_handle_free dlsym fail\n"); | |
return GSW_HAL_FAIL; | |
} | |
mbtk_pdp_state_change_cb_reg = (int (*)(mbtk_info_handle_t* handle, mbtk_info_callback_func cb))dlsym(dlHandle_mbtk, "mbtk_pdp_state_change_cb_reg"); | |
if (mbtk_pdp_state_change_cb_reg == NULL) | |
{ | |
LOGE("mbtk_pdp_state_change_cb_reg dlsym fail\n"); | |
return GSW_HAL_FAIL; | |
} | |
mbtk_qser_apn_set = (int (*)(mbtk_info_handle_t* handle, mbtk_qser_apn_info_s *apninfo, unsigned char *cid))dlsym(dlHandle_mbtk, "mbtk_qser_apn_set"); | |
if (mbtk_qser_apn_set == NULL) | |
{ | |
LOGE("mbtk_qser_apn_set dlsym fail\n"); | |
return GSW_HAL_FAIL; | |
} | |
mbtk_data_call_start = (int (*)(mbtk_info_handle_t* handle, int cid, int auto_conn_interval, bool boot_conn, int timeout))dlsym(dlHandle_mbtk, "mbtk_data_call_start"); | |
if (mbtk_data_call_start == NULL) | |
{ | |
LOGE("mbtk_data_call_start dlsym fail\n"); | |
return GSW_HAL_FAIL; | |
} | |
mbtk_data_call_stop = (int (*)(mbtk_info_handle_t* handle, int cid, int timeout))dlsym(dlHandle_mbtk, "mbtk_data_call_stop"); | |
if (mbtk_data_call_stop == NULL) | |
{ | |
LOGE("mbtk_data_call_stop dlsym fail\n"); | |
return GSW_HAL_FAIL; | |
} | |
mbtk_data_call_state_get = (int (*)(mbtk_info_handle_t* handle, int cid, mbtk_ipv4_info_t *ipv4, mbtk_ipv6_info_t *ipv6))dlsym(dlHandle_mbtk, "mbtk_data_call_state_get"); | |
if (mbtk_data_call_state_get == NULL) | |
{ | |
LOGE("mbtk_data_call_state_get dlsym fail\n"); | |
return GSW_HAL_FAIL; | |
} | |
mbtk_apn_set = (int (*)(mbtk_info_handle_t* handle, int cid, mbtk_ip_type_enum ip_type, const void* apn_name, const void *user_name, const void *user_pass, const void *auth))dlsym(dlHandle_mbtk, "mbtk_apn_set"); | |
if (mbtk_apn_set == NULL) | |
{ | |
LOGE("mbtk_apn_set dlsym fail\n"); | |
return GSW_HAL_FAIL; | |
} | |
ipv6_2_str = (int (*)(const void *ipv6, void *ipv6_str))dlsym(dlHandle_mbtk, "ipv6_2_str"); | |
if (ipv6_2_str == NULL) | |
{ | |
LOGE("ipv6_2_str dlsym fail\n"); | |
return GSW_HAL_FAIL; | |
} | |
return GSW_HAL_SUCCESS; | |
} | |
static int data_call_state_query(int call_id,Wan_State_ind_s *linkState) | |
{ | |
int ret = -1; | |
mbtk_ipv4_info_t ipv4; | |
mbtk_ipv6_info_t ipv6; | |
LOGE("mbtk_data_call_state_get start\n"); | |
ret = mbtk_data_call_state_get(data_info_handle, call_id, &ipv4, &ipv6); | |
LOGE("mbtk_data_call_state_get end\n"); | |
if (ret != 0) | |
{ | |
LOGE("mbtk_data_call_state_get fail\n"); | |
return ret; | |
} | |
//ifname | |
sprintf(linkState->ifname, "ccinet%d", call_id-1); | |
//type | |
if(ipv4.valid) | |
{ | |
if(ipv6.valid) | |
{ | |
strcpy(linkState->type, "IPV4V6"); | |
} | |
else { | |
strcpy(linkState->type, "IP"); | |
} | |
} | |
else if(ipv6.valid) | |
{ | |
strcpy(linkState->type, "IPV6"); | |
} | |
//ip | |
LOGE("ipv4.valid = %d\n", ipv4.valid); | |
if(ipv4.valid) | |
{ | |
if(linkState==NULL) | |
{ | |
LOGE("linkState is NULL\n"); | |
return GSW_HAL_FAIL; | |
} | |
//parse ipv4_addr,gw,dns,sdns | |
if(inet_ntop(AF_INET, &(ipv4.IPAddr), linkState->v4_ip , GSW_PDP_ADDR_MAX_LEN) == NULL) { | |
LOGE("IPv4 error.\n"); | |
} else { | |
LOGE("IPv4 : %s\n", linkState->v4_ip); | |
} | |
if(inet_ntop(AF_INET, &(ipv4.GateWay), linkState->v4_gw , GSW_PDP_ADDR_MAX_LEN) == NULL) { | |
LOGE("GateWay error.\n"); | |
} else { | |
LOGE("GateWay : %s\n", linkState->v4_gw); | |
} | |
if(inet_ntop(AF_INET, &(ipv4.PrimaryDNS), linkState->v4_pdns , GSW_DNS_ADDR_MAX_LEN) == NULL) { | |
LOGE("PrimaryDNS error.\n"); | |
} else { | |
LOGE("PrimaryDNS : %s\n", linkState->v4_pdns); | |
} | |
if(inet_ntop(AF_INET, &(ipv4.SecondaryDNS), linkState->v4_sdns , GSW_DNS_ADDR_MAX_LEN) == NULL) { | |
LOGE("SecondaryDNS error.\n"); | |
} else { | |
LOGE("SecondaryDNS : %s\n", linkState->v4_sdns); | |
} | |
} | |
LOGE("ipv6.valid = %d\n", ipv6.valid); | |
if(ipv6.valid) | |
{ | |
//parse ipv6_addr,gw,dns,sdns | |
if(ipv6_2_str(&(ipv6.IPV6Addr), linkState->v6_ip)) { | |
LOGE("IPv6 error.\n"); | |
} else { | |
LOGE("IPv6 : %s\n", linkState->v6_ip); | |
} | |
if(ipv6_2_str(&(ipv6.PrimaryDNS), linkState->v6_pdns)) { | |
LOGE("PrimaryDNS error.\n"); | |
} else { | |
LOGE("PrimaryDNS : %s\n", linkState->v6_pdns); | |
} | |
if(ipv6_2_str(&(ipv6.SecondaryDNS), linkState->v6_sdns)) { | |
LOGE("SecondaryDNS error.\n"); | |
} else { | |
LOGE("SecondaryDNS : %s\n", linkState->v6_sdns); | |
} | |
} | |
return GSW_HAL_SUCCESS; | |
} | |
static void data_call_state_change_cb(const void* data, int data_len) | |
{ | |
LOGE("data_call_state_change_cb() start\n"); | |
if(data == NULL || data_len == 0) | |
{ | |
return; | |
} | |
int ret = -1; | |
LOGD("Wan_State_ind_malloc start\n"); | |
ret = Wan_State_ind_malloc(&linkState_arr); | |
if (ret != 0) | |
{ | |
LOGE("Wan_State_ind_malloc[0] fail\n"); | |
return ; | |
} | |
if(linkState_arr == NULL) | |
{ | |
LOGE("linkState_arr is NULL"); | |
return ; | |
} | |
else | |
{ | |
LOGE("linkState_arr is not NULL"); | |
} | |
uint8 *net_data = NULL; | |
net_data = (uint8 *)data; | |
//disconnected | |
LOGE("net_data = %d\n", *net_data); | |
if(*net_data > 100 && *net_data < 200) | |
{ | |
int apn_id = *net_data - 100; | |
int handle_temp = apn_id-2; | |
LOGE("apn_id = %d\n", apn_id); | |
linkState_arr->state = 0;//disconnected | |
linkState_arr->cid = -1; | |
linkState_arr->handle = handle_temp; | |
//get data_call_state | |
if(!data_call_lock) | |
{ | |
data_call_state_query(apn_id, linkState_arr); | |
} | |
if(gsw_data_call_evt_cb) | |
{ | |
gsw_data_call_evt_cb(linkState_arr); | |
} | |
} | |
else if(*net_data > 200 && *net_data < 220) | |
{ | |
LOGE("cid[%d] is open.\n", *net_data - 200); | |
int apn_id = *net_data-200; | |
int handle_temp = apn_id-2; | |
linkState_arr->state = 2;//connected | |
linkState_arr->cid = apn_id; | |
linkState_arr->handle = handle_temp; | |
//get data_call_state | |
if(!data_call_lock) | |
{ | |
data_call_state_query(apn_id, linkState_arr); | |
} | |
if(gsw_data_call_evt_cb) | |
{ | |
gsw_data_call_evt_cb(linkState_arr); | |
} | |
} | |
else if(*net_data > 220) | |
{ | |
LOGE("cid [%d] is reopen.\n", *net_data - 220); | |
int apn_id = *net_data-220; | |
int handle_temp = apn_id-2; | |
linkState_arr->state = 2;//connected | |
linkState_arr->cid = apn_id; | |
linkState_arr->handle = handle_temp; | |
//get data_call_state | |
if(!data_call_lock) | |
{ | |
data_call_state_query(apn_id, linkState_arr); | |
} | |
if(gsw_data_call_evt_cb) | |
{ | |
gsw_data_call_evt_cb(linkState_arr); | |
} | |
} | |
else if(*net_data == 1) | |
{ | |
LOGE("pdp is open.\n"); | |
} | |
else | |
{ | |
LOGE("unknown param [%d].\n", *net_data); | |
} | |
ret = Wan_State_ind_free(&linkState_arr); | |
if (ret != 0) | |
{ | |
LOGE("Wan_State_ind_free fail\n"); | |
} | |
} | |
/** | |
* @brief datacall sdk init | |
* @param [in] evt_cb callback function for data connection state change event | |
* call back; | |
* @retval 0: success | |
* @retval other: fail | |
*/ | |
int gsw_data_call_init(gsw_data_call_evt_cb_t evt_cb) | |
{ | |
int ret; | |
ret = mbtk_data_api_import(); | |
if (ret != 0) | |
{ | |
return GSW_HAL_FAIL; | |
} | |
if(data_init_flag != 0 && data_info_handle != NULL) | |
{ | |
return GSW_HAL_FAIL; | |
} | |
if (ret != GSW_HAL_SUCCESS) | |
{ | |
LOGE("mbtk_data_api_import fail\n"); | |
return GSW_HAL_FAIL; | |
} | |
data_info_handle = (mbtk_info_handle_t*)mbtk_info_handle_get(); | |
if (data_info_handle == NULL) | |
{ | |
LOGE("mbtk_info_handle_get fail\n"); | |
return GSW_HAL_FAIL; | |
} | |
ret = mbtk_pdp_state_change_cb_reg((mbtk_info_handle_t*)data_info_handle, data_call_state_change_cb); | |
if (ret != 0) | |
{ | |
LOGE("mbtk_pdp_state_change_cb_reg fail\n"); | |
return GSW_HAL_FAIL; | |
} | |
gsw_data_call_evt_cb = evt_cb; | |
data_init_flag = 1; | |
return GSW_HAL_SUCCESS; | |
} | |
/** | |
* @brief data_call sdk deinit | |
* @param | |
* @retval 0: success | |
* @retval other: fail | |
*/ | |
int gsw_data_call_deinit(void) | |
{ | |
int ret; | |
if(data_init_flag == 0 && data_info_handle == NULL) | |
{ | |
return GSW_HAL_FAIL; | |
} | |
ret = mbtk_info_handle_free((mbtk_info_handle_t**)&data_info_handle); | |
if (ret != 0) | |
{ | |
LOGE("mbtk_info_handle_free fail\n"); | |
return GSW_HAL_FAIL; | |
} | |
data_init_flag = 0; | |
return GSW_HAL_SUCCESS; | |
} | |
/** | |
* @brief set apn parameters for data call | |
* @param [in/out] LinkInf apn info for pub or private datacall | |
* inlcude apn name mcc mnc, passwork apnid type cid etc | |
* @retval 0: success | |
* @retval other: fail | |
*/ | |
int gsw_data_call_set_apn(Link_Info_s *LinkInf) | |
{ | |
if(data_init_flag == 0 && data_info_handle == NULL) | |
{ | |
return GSW_HAL_FAIL; | |
} | |
if(LinkInf == NULL) | |
{ | |
LOGE("apn info is null\n"); | |
return GSW_HAL_FAIL; | |
} | |
int ret = 0; | |
mbtk_ip_type_enum ip_type; | |
int auth_type = -1; | |
//pdp type | |
if(strcmp(LinkInf->normalProtocol,"IPV4") == 0) | |
{ | |
ip_type = MBTK_IP_TYPE_IP; | |
LOGE("pdp_type(protocol) is IPV4\n"); | |
} | |
else if(strcmp(LinkInf->normalProtocol,"IPV6") == 0) | |
{ | |
ip_type = MBTK_IP_TYPE_IPV6; | |
LOGE("pdp_type(protocol) is IPV6\n"); | |
} | |
else if(strcmp(LinkInf->normalProtocol,"IPV4V6") == 0) | |
{ | |
ip_type = MBTK_IP_TYPE_IPV4V6; | |
LOGE("pdp_type(protocol) is IPV4V6\n"); | |
} | |
else | |
{ | |
LOGE("pdp_type(protocol) error\n"); | |
return GSW_HAL_FAIL; | |
} | |
//auth type | |
auth_type = atoi(LinkInf->authType); | |
ret = mbtk_apn_set(data_info_handle, LinkInf->handle+2, ip_type, LinkInf -> apn, LinkInf -> usr, LinkInf -> pwd, (const void *)auth_type); | |
if(ret != 0) | |
{ | |
LOGE("mbtk_apn_set fail."); | |
return GSW_HAL_FAIL; | |
} | |
return GSW_HAL_SUCCESS; | |
} | |
/** | |
* @brief set apn parameters for data call | |
* @param [in] linkid data connetion link number | |
* @param [in/out] LinkInf link info req: apn info for pub or private datacall | |
* inlcude apn name mcc mnc, passwork apnid etc | |
* resp: data call link state ipaddress type etc | |
* @retval 0: success | |
* @retval other: fail | |
*/ | |
int gsw_data_call_connect(int linkid, Link_Info_s *LinkInf) | |
{ | |
int ret = -1; | |
if(data_init_flag == 0 && data_info_handle == NULL) | |
{ | |
return GSW_HAL_FAIL; | |
} | |
if(LinkInf == NULL) | |
{ | |
LOGE("apn info is null\n"); | |
return GSW_HAL_FAIL; | |
} | |
data_call_lock = 1; | |
ret = mbtk_data_call_start((mbtk_info_handle_t*)data_info_handle, linkid + 2, 0, FALSE, 0); | |
data_call_lock = 0; | |
if(ret != 0) | |
{ | |
LOGE("mbtk_data_call_start fail,\n"); | |
return GSW_HAL_FAIL; | |
} | |
uint8_t cid_temp = (uint8_t)(linkid + 202); | |
data_call_state_change_cb(&cid_temp,1); | |
return GSW_HAL_SUCCESS; | |
} | |
/** | |
* @brief set apn parameters for data call | |
* @param [in] linkid | |
* @param [in/out] LinkInf link info req: apn info for pub or private datacall | |
* inlcude apn name mcc mnc, passwork apnid etc | |
* resp: data call link state ipaddress type etc | |
* @retval 0: success | |
* @retval other: fail | |
*/ | |
int gsw_data_call_disconnect(int linkid, Link_Info_s *LinkInf) | |
{ | |
int ret = -1; | |
if(data_init_flag == 0 && data_info_handle == NULL) | |
{ | |
return GSW_HAL_FAIL; | |
} | |
if(LinkInf == NULL) | |
{ | |
LOGE("apn info is null\n"); | |
return GSW_HAL_FAIL; | |
} | |
data_call_lock = 1; | |
ret = mbtk_data_call_stop((mbtk_info_handle_t*)data_info_handle, linkid + 2, 15); | |
data_call_lock = 0; | |
if(ret != 0) | |
{ | |
LOGE("mbtk_data_call_stop fail,ret = %d\n",ret); | |
return GSW_HAL_FAIL; | |
} | |
uint8_t cid_temp = (uint8_t)(linkid + 102); | |
data_call_state_change_cb(&cid_temp,1); | |
return GSW_HAL_SUCCESS; | |
} | |
/* | |
* @brief get mobile operator name | |
@param [in] linkid apn id | |
@param [out] data_pkt obtian actual data call network card data traffic | |
@retval 0: success | |
@retval 0: other: fail | |
*/ | |
int gsw_get_data_call_pkt_stats(int linkid, gsw_data_pkt_stats *data_pkt) | |
{ | |
char temp_linkid[12] = {0}; | |
char interface_name[32] = {0}; | |
sprintf(temp_linkid,"%d",linkid+1); | |
snprintf(interface_name, sizeof(interface_name),"ccinet%s", temp_linkid); | |
uint64_t tx_bytes = 0; | |
uint64_t tx_packets = 0; | |
uint64_t tx_dropped = 0; | |
uint64_t rx_bytes = 0; | |
uint64_t rx_packets = 0; | |
uint64_t rx_dropped = 0; | |
char command[128] = {0}; | |
char* temp; | |
char buffer[512] = {0}; | |
snprintf(command, sizeof(command), "ifconfig %s", interface_name); | |
FILE *fp = popen(command, "r"); | |
if(fp == NULL) | |
{ | |
LOGE("popen fail\n"); | |
return GSW_HAL_FAIL; | |
} | |
//read line by line | |
while (fgets(buffer, sizeof(buffer), fp) != NULL) { | |
// TX bytes | |
if (strstr(buffer, "TX bytes")) { | |
temp = strstr(buffer, "TX bytes"); | |
sscanf(temp, " TX bytes:%llu ", &tx_bytes); | |
} | |
// TX packets | |
if (strstr(buffer, "TX packets")) { | |
sscanf(buffer, " TX packets:%llu ", &tx_packets); | |
sscanf(buffer, " TX packets:%*u errors:%*u dropped:%llu", &tx_dropped); | |
} | |
// RX bytes | |
if (strstr(buffer, "RX bytes")) { | |
LOGE("RX bytes %s\n",strstr(buffer, "RX bytes")); | |
sscanf(buffer, " RX bytes:%llu ", &rx_bytes); | |
} | |
// RX packets | |
if (strstr(buffer, "RX packets")) { | |
sscanf(buffer, " RX packets:%llu ", &rx_packets); | |
sscanf(buffer, " RX packets:%*u errors:%*u dropped:%llu", &rx_dropped); | |
} | |
} | |
// 关闭管道 | |
pclose(fp); | |
// 输出结果 | |
LOGE("TX Bytes: %llu\n", tx_bytes); | |
LOGE("TX Packets: %llu\n", tx_packets); | |
LOGE("TX Dropped: %llu\n", tx_dropped); | |
LOGE("RX Bytes: %llu\n", rx_bytes); | |
LOGE("RX Packets: %llu\n", rx_packets); | |
LOGE("RX Dropped: %llu\n", rx_dropped); | |
if(data_pkt == NULL) | |
{ | |
LOGE("data_pkt is null\n"); | |
return GSW_HAL_FAIL; | |
} | |
else | |
{ | |
data_pkt->tx_bytes = tx_bytes; | |
data_pkt->tx_pkts = tx_packets; | |
data_pkt->tx_dropped_pkts = tx_dropped; | |
data_pkt->rx_bytes = rx_bytes; | |
data_pkt->rx_pkts = rx_packets; | |
data_pkt->rx_dropped_pkts = rx_dropped; | |
} | |
return GSW_HAL_SUCCESS; | |
} |