blob: ea42013e68e3119bbbf125e59d0bbca4c85f45f9 [file] [log] [blame]
#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>
#include "gsw_log_interface.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
#define MBTK_CHANNEL_ID_1 1
#define MBTK_CHANNEL_ID_2 2
#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 enum {
GSW_ID_CONVERT_TYPE_LINKID = 0,
GSW_ID_CONVERT_TYPE_APNID
}gsw_id_convert_type_e;
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;
typedef struct{
uint8 cid; //cid number
bool connect_state; //true: connect, false: disconnect;
bool auto_urc; //urc generated by manual/auto dialing
uint8 pdp_result; //The cause of the dialing result
mbtk_ipv4_info_t ipv4; //ipv4 info
mbtk_ipv6_info_t ipv6; //ipv6 info
}mbtk_pdp_cb_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);
#define GSW_DATA "[HAL][GSW_DATA]"
//define
#define lib_mbtk_path "/lib/libmbtk_lib.so"
pthread_mutex_t data_mutex = PTHREAD_MUTEX_INITIALIZER;
static int data_init_flag = 0;
static void *dlHandle_mbtk = NULL;
static mbtk_info_handle_t* data_info_handle = NULL;
static gsw_data_call_evt_cb_t gsw_data_call_evt_cb = NULL;
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(GSW_DATA,"malloc Wan_State_ind_s fail\n");
return GSW_HAL_NORMAL_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(GSW_DATA,"(*linkState) is null\n");
return GSW_HAL_NORMAL_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_NORMAL_FAIL;
}
mbtk_info_handle_get = (mbtk_info_handle_t* (*)(void))dlsym(dlHandle_mbtk, "mbtk_info_handle_get");
if (mbtk_info_handle_get == NULL)
{
LOGE(GSW_DATA,"mbtk_info_handle_get dlsym fail\n");
return GSW_HAL_NORMAL_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(GSW_DATA,"mbtk_info_handle_free dlsym fail\n");
return GSW_HAL_NORMAL_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(GSW_DATA,"mbtk_pdp_state_change_cb_reg dlsym fail\n");
return GSW_HAL_NORMAL_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(GSW_DATA,"mbtk_qser_apn_set dlsym fail\n");
return GSW_HAL_NORMAL_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(GSW_DATA,"mbtk_data_call_start dlsym fail\n");
return GSW_HAL_NORMAL_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(GSW_DATA,"mbtk_data_call_stop dlsym fail\n");
return GSW_HAL_NORMAL_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(GSW_DATA,"mbtk_data_call_state_get dlsym fail\n");
return GSW_HAL_NORMAL_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(GSW_DATA,"mbtk_apn_set dlsym fail\n");
return GSW_HAL_NORMAL_FAIL;
}
ipv6_2_str = (int (*)(const void *ipv6, void *ipv6_str))dlsym(dlHandle_mbtk, "ipv6_2_str");
if (ipv6_2_str == NULL)
{
LOGE(GSW_DATA,"ipv6_2_str dlsym fail\n");
return GSW_HAL_NORMAL_FAIL;
}
return GSW_HAL_SUCCESS;
}
static int data_call_state_query(int call_id,Wan_State_ind_s *linkState, mbtk_ipv4_info_t *ipv4, mbtk_ipv6_info_t *ipv6)
{
//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
LOGD(GSW_DATA,"ipv4.valid = %d", ipv4->valid);
if(ipv4->valid)
{
if(linkState==NULL)
{
LOGE(GSW_DATA,"linkState is NULL");
return GSW_HAL_NORMAL_FAIL;
}
//parse ipv4_addr,gw,dns,sdns
if(inet_ntop(AF_INET, &(ipv4->IPAddr), linkState->v4_ip , GSW_PDP_ADDR_MAX_LEN) == NULL) {
LOGE(GSW_DATA,"IPv4 error");
} else {
LOGD(GSW_DATA,"IPv4 : %s", linkState->v4_ip);
}
if(inet_ntop(AF_INET, &(ipv4->GateWay), linkState->v4_gw , GSW_PDP_ADDR_MAX_LEN) == NULL) {
LOGE(GSW_DATA,"GateWay error");
} else {
LOGE(GSW_DATA,"GateWay : %s", linkState->v4_gw);
}
if(inet_ntop(AF_INET, &(ipv4->PrimaryDNS), linkState->v4_pdns , GSW_DNS_ADDR_MAX_LEN) == NULL) {
LOGE(GSW_DATA,"PrimaryDNS error");
} else {
LOGD(GSW_DATA,"PrimaryDNS : %s", linkState->v4_pdns);
}
if(inet_ntop(AF_INET, &(ipv4->SecondaryDNS), linkState->v4_sdns , GSW_DNS_ADDR_MAX_LEN) == NULL) {
LOGE(GSW_DATA,"SecondaryDNS error");
} else {
LOGD(GSW_DATA,"SecondaryDNS : %s", linkState->v4_sdns);
}
}
LOGD(GSW_DATA,"ipv6.valid = %d", ipv6->valid);
if(ipv6->valid)
{
//parse ipv6_addr,gw,dns,sdns
if(ipv6_2_str(&(ipv6->IPV6Addr), linkState->v6_ip)) {
LOGE(GSW_DATA,"IPv6 error");
} else {
LOGD(GSW_DATA,"IPv6 : %s", linkState->v6_ip);
}
if(ipv6_2_str(&(ipv6->PrimaryDNS), linkState->v6_pdns)) {
LOGE(GSW_DATA,"PrimaryDNS error");
} else {
LOGD(GSW_DATA,"PrimaryDNS : %s", linkState->v6_pdns);
}
if(ipv6_2_str(&(ipv6->SecondaryDNS), linkState->v6_sdns)) {
LOGE(GSW_DATA,"SecondaryDNS error");
} else {
LOGD(GSW_DATA,"SecondaryDNS : %s", linkState->v6_sdns);
}
}
return GSW_HAL_SUCCESS;
}
static void data_call_state_change_cb(const void* data, int data_len)
{
LOGE(GSW_DATA,"data_call_state_change_cb() start\n");
if(data == NULL || data_len != sizeof(mbtk_pdp_cb_info_s))
{
LOGE(GSW_DATA,"data = [NULL], data_len(%d) != sizeof(mbtk_pdp_cb_info_s)(%d)", data_len, sizeof(mbtk_pdp_cb_info_s));
return;
}
int ret = -1;
LOGD(GSW_DATA,"Wan_State_ind_malloc start\n");
ret = Wan_State_ind_malloc(&linkState_arr);
if (ret != 0)
{
LOGE(GSW_DATA,"Wan_State_ind_malloc[0] fail\n");
return ;
}
if(linkState_arr == NULL)
{
LOGE(GSW_DATA,"linkState_arr is NULL");
return ;
}
else
{
LOGE(GSW_DATA,"linkState_arr is not NULL");
}
mbtk_pdp_cb_info_s *pdp_cb_info = (mbtk_pdp_cb_info_s *)data;
linkState_arr->cid = pdp_cb_info->cid;
linkState_arr->handle = pdp_cb_info->cid;
if(pdp_cb_info->connect_state)
{
linkState_arr->state = 2;//connected
data_call_state_query(pdp_cb_info->cid, linkState_arr, &(pdp_cb_info->ipv4), &(pdp_cb_info->ipv6));
}
else
{
linkState_arr->state = 0;//disconnected
}
if(gsw_data_call_evt_cb)
{
gsw_data_call_evt_cb(linkState_arr);
}
ret = Wan_State_ind_free(&linkState_arr);
if (ret != 0)
{
LOGE(GSW_DATA,"Wan_State_ind_free fail\n");
}
}
static int gsw_id_convert_to_channel_id(int gsw_id, gsw_id_convert_type_e type, int *channel_id)
{
if(GSW_ID_CONVERT_TYPE_LINKID == type)
{
switch(gsw_id)
{
case 0:
{
*channel_id = MBTK_CHANNEL_ID_2;
break;
}
case 1:
{
*channel_id = MBTK_CHANNEL_ID_1;
break;
}
default :
{
LOGE(GSW_DATA,"[%s] gsw_id(linkid) unknown.[%d]", __func__, gsw_id);
return -1;
}
}
}
else
{
switch(gsw_id)
{
case 1:
case -2:
{
*channel_id = MBTK_CHANNEL_ID_1;
break;
}
case -1:
case 2:
{
*channel_id = MBTK_CHANNEL_ID_2;
break;
}
default :
{
LOGE(GSW_DATA,"[%s] gsw_id(apnid) unknown.[%d]", __func__, gsw_id);
return -1;
}
}
}
return 0;
}
/**
* @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_NORMAL_FAIL;
}
if(data_init_flag != 0 && data_info_handle != NULL)
{
return GSW_HAL_NORMAL_FAIL;
}
if (ret != GSW_HAL_SUCCESS)
{
LOGE(GSW_DATA,"mbtk_data_api_import fail\n");
return GSW_HAL_NORMAL_FAIL;
}
data_info_handle = (mbtk_info_handle_t*)mbtk_info_handle_get();
if (data_info_handle == NULL)
{
LOGE(GSW_DATA,"mbtk_info_handle_get fail\n");
return GSW_HAL_NORMAL_FAIL;
}
ret = mbtk_pdp_state_change_cb_reg((mbtk_info_handle_t*)data_info_handle, data_call_state_change_cb);
if (ret != 0)
{
LOGE(GSW_DATA,"mbtk_pdp_state_change_cb_reg fail\n");
return GSW_HAL_NORMAL_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_NORMAL_FAIL;
}
ret = mbtk_info_handle_free((mbtk_info_handle_t**)&data_info_handle);
if (ret != 0)
{
LOGE(GSW_DATA,"mbtk_info_handle_free fail\n");
return GSW_HAL_NORMAL_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_NORMAL_FAIL;
}
if(LinkInf == NULL)
{
LOGE(GSW_DATA,"apn info is null");
return GSW_HAL_NORMAL_FAIL;
}
int ret = 0;
int cid = -1;
int apnid = -1;
mbtk_ip_type_enum ip_type;
char auth_type[GSW_AUTH_TYPE_MAX_LEN + 1] = {0};
//pdp type
if(strcmp(LinkInf->normalProtocol,"IP") == 0)
{
ip_type = MBTK_IP_TYPE_IP;
LOGE(GSW_DATA,"pdp_type(protocol) is IP");
}
else if(strcmp(LinkInf->normalProtocol,"IPV6") == 0)
{
ip_type = MBTK_IP_TYPE_IPV6;
LOGE(GSW_DATA,"pdp_type(protocol) is IPV6");
}
else if(strcmp(LinkInf->normalProtocol,"IPV4V6") == 0)
{
ip_type = MBTK_IP_TYPE_IPV4V6;
LOGE(GSW_DATA,"pdp_type(protocol) is IPV4V6");
}
else
{
LOGE(GSW_DATA,"pdp_type(protocol) error");
return GSW_HAL_NORMAL_FAIL;
}
apnid = atoi(LinkInf->apnid);
ret = gsw_id_convert_to_channel_id(apnid, GSW_ID_CONVERT_TYPE_APNID, &cid);
if(0 != ret)
{
LOGE(GSW_DATA,"[%s] gsw_id_convert_to_channel_id() fail", __func__);
return GSW_HAL_NORMAL_FAIL;
}
//bypass solution for authparams issue
#if 0
memset(LinkInf->usr, 0x00, sizeof(LinkInf->usr));
memset(LinkInf->pwd, 0x00, sizeof(LinkInf->pwd));
memset(LinkInf->authType, 0x00, sizeof(LinkInf->authType));
#endif
//auth type
memset(auth_type, 0x00, GSW_AUTH_TYPE_MAX_LEN + 1);
if(0 == strlen(LinkInf->authType) || 0 == strcmp(LinkInf->authType, "0"))
{
memcpy(auth_type, "NONE", strlen("NONE"));
}
else if(0 == strcmp(LinkInf->authType, "1"))
{
memcpy(auth_type, "PAP", strlen("PAP"));
}
else if(0 == strcmp(LinkInf->authType, "2"))
{
memcpy(auth_type, "CHAP", strlen("CHAP"));
}
else if(0 == strcmp(LinkInf->authType, "3"))
{
/* //nonsupport
LOGE(GSW_DATA,"[%s] LinkInf->authType(3) nonsupport", __func__);
return GSW_HAL_NORMAL_FAIL;*/
memcpy(auth_type, "CHAP", strlen("CHAP"));/*in T106 if set chap and pap, chap and chap are used, all use chap here, hq modify for the timebing 25/07/24*/
}
else
{
LOGE(GSW_DATA,"[%s] LinkInf->authType(%s) unknown", __func__, LinkInf->authType);
return GSW_HAL_NORMAL_FAIL;
}
ret = mbtk_apn_set(data_info_handle, cid, ip_type, LinkInf -> apn, LinkInf -> usr, LinkInf -> pwd, (const void *)auth_type);
if(ret != 0)
{
LOGE(GSW_DATA,"[%s] mbtk_apn_set fail() fail.[%d]", __func__, ret);
return GSW_HAL_NORMAL_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;
int cid = -1;
if(data_init_flag == 0 || data_info_handle == NULL)
{
return GSW_HAL_NORMAL_FAIL;
}
if(LinkInf == NULL)
{
LOGE(GSW_DATA,"apn info is null");
return GSW_HAL_NORMAL_FAIL;
}
ret = gsw_data_call_set_apn(LinkInf);
if(ret != GSW_HAL_SUCCESS)
{
LOGE(GSW_DATA,"[%s] gsw_data_call_set_apn() fail", __func__);
return GSW_HAL_NORMAL_FAIL;
}
ret = gsw_id_convert_to_channel_id(linkid, GSW_ID_CONVERT_TYPE_LINKID, &cid);
if(0 != ret)
{
LOGE(GSW_DATA,"[%s] gsw_id_convert_to_channel_id() fail", __func__);
return GSW_HAL_NORMAL_FAIL;
}
ret = mbtk_data_call_start((mbtk_info_handle_t*)data_info_handle, cid, 0, FALSE, 0);
if(ret != 0)
{
LOGE(GSW_DATA,"[%s] mbtk_data_call_start fail.[%d]", __func__, ret);
return GSW_HAL_NORMAL_FAIL;
}
LinkInf->handle = cid;
memset(LinkInf->apnid, 0x00, sizeof(LinkInf->apnid));
sprintf(LinkInf->apnid, "%d", cid);
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;
int cid = -1;
if(data_init_flag == 0 || data_info_handle == NULL)
{
return GSW_HAL_NORMAL_FAIL;
}
if(LinkInf == NULL)
{
LOGE(GSW_DATA,"apn info is null");
return GSW_HAL_NORMAL_FAIL;
}
ret = gsw_id_convert_to_channel_id(linkid, GSW_ID_CONVERT_TYPE_LINKID, &cid);
if(0 != ret)
{
LOGE(GSW_DATA,"[%s] gsw_id_convert_to_channel_id() fail", __func__);
return GSW_HAL_NORMAL_FAIL;
}
ret = mbtk_data_call_stop((mbtk_info_handle_t*)data_info_handle, cid, 15);
if(ret != 0)
{
LOGE(GSW_DATA,"[%s] mbtk_data_call_stop() fail.[%d]",__func__, ret);
return GSW_HAL_NORMAL_FAIL;
}
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)
{
if(data_init_flag == 0 || data_info_handle == NULL)
{
return GSW_HAL_NORMAL_FAIL;
}
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(GSW_DATA,"popen fail\n");
return GSW_HAL_NORMAL_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(GSW_DATA,"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(GSW_DATA,"TX Bytes: %llu\n", tx_bytes);
LOGE(GSW_DATA,"TX Packets: %llu\n", tx_packets);
LOGE(GSW_DATA,"TX Dropped: %llu\n", tx_dropped);
LOGE(GSW_DATA,"RX Bytes: %llu\n", rx_bytes);
LOGE(GSW_DATA,"RX Packets: %llu\n", rx_packets);
LOGE(GSW_DATA,"RX Dropped: %llu\n", rx_dropped);
if(data_pkt == NULL)
{
LOGE(GSW_DATA,"data_pkt is null\n");
return GSW_HAL_NORMAL_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;
}