blob: b4c88736521d8a38f23f7c7042c83dca6c07ef86 [file] [log] [blame]
#include <stdbool.h>
#include <unistd.h>
#include <dlfcn.h>
#include <time.h>
#include "gsw_nw_interface.h"
#define MBTK_BUFF_TEMP_SIZE_32 32
#define MBTK_ERR_OK 0
#define MBTK_ERR_FAIL -1
#define GSW_SIM_ICCID_LENGTH 15+1
#define GSW_SIM_IMSI_LENGTH 15+1
#define GSW_SIM_MSISDN_LENGTH 15+1
//mbtk include
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 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_SIM_ABSENT = 0,
MBTK_SIM_NOT_READY = 1,
MBTK_SIM_READY = 2,
MBTK_SIM_PIN = 3,
MBTK_SIM_PUK = 4,
MBTK_SIM_NETWORK_PERSONALIZATION = 5
} mbtk_sim_state_enum;
typedef enum
{
MBTK_DEV_MODEM_MIN_FUN, //Modem 最小功能
MBTK_DEV_MODEM_FULL_FUN, //Modem 全功能
MBTK_DEV_MODEM_DISABLE_RECEIVE_RF_CIRCUITS = 3, //Modem 禁用射频接收电路
MBTK_DEV_MODEM_DISABLE_TRANSMIT_AND_RECEIVE_RF_CIRCUITS, //Modem禁用射频发射和接收电路
MBTK_DEV_MODEM_DISABLE_SIM, //Modem 禁用(U)SIM 卡
MBTK_DEV_MODEM_TURN_OFF_FULL_SECONDARY_RECEIVE, //Modem 完全禁用辅助接收
}MBTK_DEV_MODEM_FUNCTION;
typedef struct
{
MBTK_DEV_MODEM_FUNCTION fun;
int rst;
} mbtk_modem_info_t;
//api
static mbtk_info_handle_t* (*mbtk_info_handle_get)(void);
#define lib_mbtk_path "/lib/libmbtk_lib.so"
static int sim_init_flag = 0;
mbtk_info_handle_t* sim_info_handle = NULL;
static int (*mbtk_info_handle_free)(mbtk_info_handle_t** handle);
int (*mbtk_sim_state_get)(mbtk_info_handle_t* handle, mbtk_sim_state_enum *sim_state);
int (*mbtk_imsi_get)(mbtk_info_handle_t* handle, void *imsi);
int (*mbtk_iccid_get)(mbtk_info_handle_t* handle, void *iccid);
int (*mbtk_phone_number_get)(mbtk_info_handle_t* handle, void *phone_number);
int (*mbtk_set_modem_fun)(mbtk_info_handle_t* handle, mbtk_modem_info_t *info);
int (*mbtk_sim_power_set)(int power);
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 GSW_SIM "[HAL][GSW_SIM]"
#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: "GSW_SIM"" 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: "GSW_SIM"" 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: "GSW_SIM"" 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: "GSW_SIM"" 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: "GSW_SIM"" fmt, ptr_1001 + 1, line_1001, ##args); \
} while(0)
static void *dlHandle_mbtk;
static int gsw_sim_api_import()
{
dlHandle_mbtk = dlopen(lib_mbtk_path, RTLD_NOW);
if (dlHandle_mbtk == NULL)
{
return GSW_HAL_NORMAL_FAIL;
}
mbtk_log_init = (void (*)(char *path, char *tag))dlsym(dlHandle_mbtk, "mbtk_log_init");
if (mbtk_log_init == NULL)
{
return GSW_HAL_NORMAL_FAIL;
}
mbtk_log = (void (*)(int level, const char *format, ...))dlsym(dlHandle_mbtk, "mbtk_log");
if (mbtk_log == 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("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("mbtk_info_handle_free dlsym fail\n");
return GSW_HAL_NORMAL_FAIL;
}
mbtk_imsi_get = (int (*)(mbtk_info_handle_t* handle, void *imsi))dlsym(dlHandle_mbtk, "mbtk_imsi_get");
if (mbtk_imsi_get == NULL)
{
LOGE("mbtk_imsi_get dlsym fail\n");
return GSW_HAL_NORMAL_FAIL;
}
mbtk_iccid_get = (int (*)(mbtk_info_handle_t* handle, void *iccid))dlsym(dlHandle_mbtk, "mbtk_iccid_get");
if (mbtk_iccid_get == NULL)
{
LOGE("mbtk_iccid_get dlsym fail\n");
return GSW_HAL_NORMAL_FAIL;
}
mbtk_phone_number_get = (int (*)(mbtk_info_handle_t* handle, void *phone_number))dlsym(dlHandle_mbtk, "mbtk_phone_number_get");
if (mbtk_phone_number_get == NULL)
{
LOGE("mbtk_phone_number_get dlsym fail\n");
return GSW_HAL_NORMAL_FAIL;
}
mbtk_sim_state_get = (int (*)(mbtk_info_handle_t* handle, mbtk_sim_state_enum *sim_state))dlsym(dlHandle_mbtk, "mbtk_sim_state_get");
if (mbtk_sim_state_get == NULL)
{
LOGE("mbtk_sim_state_get dlsym fail\n");
return GSW_HAL_NORMAL_FAIL;
}
mbtk_set_modem_fun = (int (*)(mbtk_info_handle_t* handle, mbtk_modem_info_t *info))dlsym(dlHandle_mbtk, "mbtk_set_modem_fun");
if (mbtk_set_modem_fun == NULL)
{
LOGE("mbtk_set_modem_fun dlsym fail\n");
return GSW_HAL_NORMAL_FAIL;
}
mbtk_sim_power_set = (int (*)(int power))dlsym(dlHandle_mbtk, "mbtk_sim_power_set");
if (mbtk_sim_power_set == NULL)
{
LOGE("mbtk_sim_power_set dlsym fail\n");
return GSW_HAL_NORMAL_FAIL;
}
LOGD("gsw_sim_api_import end\n");
return GSW_HAL_SUCCESS;
}
/**
* @brief sim sdk init
* @param [in] token usr id define by who use
* @retval 0: success
* @retval other: fail
*/
int gsw_sim_sdk_init(int32_t token)
{
int ret = GSW_HAL_NORMAL_FAIL;
if(sim_init_flag == 1 && sim_info_handle != NULL)
{
return GSW_HAL_SUCCESS;
}
ret = gsw_sim_api_import();
if(ret != GSW_HAL_SUCCESS)
{
if(mbtk_log != NULL)
{
LOGE("gsw_sim_import fail\n");
}
return GSW_HAL_NORMAL_FAIL;
}
mbtk_log_init("syslog", "MBTK_RIL");
sim_info_handle = mbtk_info_handle_get();
if(sim_info_handle == NULL)
{
LOGE("mbtk_info_handle_get fail\n");
return GSW_HAL_NORMAL_FAIL;
}
sim_init_flag = 1;
return GSW_HAL_SUCCESS;
}
/**
* @brief sim sdk deinit
* @param
* @retval 0: success
* @retval other: fail
*/
int gsw_sim_sdk_deinit(void)
{
int ret = -1;
if(sim_init_flag == 0 || sim_info_handle == NULL)
{
printf("sim sdk has been deinit\n");
return GSW_HAL_NORMAL_FAIL;
}
ret = mbtk_info_handle_free(&sim_info_handle);
if(ret != GSW_HAL_SUCCESS)
{
LOGE("mbtk_info_handle_free fail\n");
return GSW_HAL_NORMAL_FAIL;
}
sim_init_flag = 0;
sim_info_handle = NULL;
dlclose(dlHandle_mbtk);
return GSW_HAL_SUCCESS;
}
/**
* @brief get sim state
* @param [out] sim_state sim status as sim_status_e_type
* @retval 0: success
* @retval other: fail
*/
int gsw_get_sim_status(int *sim_state)
{
printf("gsw_get_sim_status enter\n");
int ret = -1;
if(sim_init_flag == 0 || sim_info_handle == NULL)
{
printf("sim sdk has been deinit\n");
return GSW_HAL_NORMAL_FAIL;
}
mbtk_sim_state_enum sim = MBTK_SIM_ABSENT;
LOGD("mbtk_get_sim_status start\n");
ret = mbtk_sim_state_get(sim_info_handle, &sim);
if(ret)
{
LOGE("[gsw_get_sim_status] mbtk_sim_state_get fail [err = %d].", ret);
return GSW_HAL_NORMAL_FAIL;
}
else
{
LOGD("[gsw_get_sim_status] sim = %d\n", sim);
switch (sim)
{
case MBTK_SIM_ABSENT:
{
*sim_state = SIM_STATUS_ABSENT;
break;
}
case MBTK_SIM_NOT_READY:
{
*sim_state = SIM_STATUS_ERROR;// not SIM_STATUS_PRESENT, changed for gsw requirement, hq at 250606
break;
}
case MBTK_SIM_READY:
{
*sim_state = SIM_STATUS_PRESENT;// not SIM_STATUS_READY, changed for gsw requirement, hq at 250606
break;
}
case MBTK_SIM_PIN:
{
*sim_state = SIM_STATUS_PIN;
break;
}
case MBTK_SIM_PUK:
{
break;
}
case MBTK_SIM_NETWORK_PERSONALIZATION:
{
break;
}
}
}
return GSW_HAL_SUCCESS;
}
/**
* @brief get iccid function
* @param [in] len iccid length,max is 20
* @param [out] iccid return iccid from this func
* @retval 0: success
* @retval other: fail
*/
int gsw_get_sim_iccid(int len, char *iccid)
{
int ret = -1;
if(sim_init_flag == 0 || sim_info_handle == NULL)
{
printf("sim sdk has been deinit\n");
return GSW_HAL_NORMAL_FAIL;
}
if(iccid == NULL)
{
LOGE("iccid is NULL.");
return GSW_HAL_ARG_INVALID;
}
if(len < GSW_SIM_ICCID_LENGTH)
{
LOGE("iccid len is too short,len = %d\n", len);
return GSW_HAL_NORMAL_FAIL;
}
ret = mbtk_iccid_get(sim_info_handle, (void *)iccid);
if(ret != MBTK_ERR_OK)
{
LOGE("[gsw_sim] mbtk_iccid_get fail [err = %d].", ret);
return GSW_HAL_NORMAL_FAIL;
}
return GSW_HAL_SUCCESS;
}
/**
* @brief get imsi function
* @param [in] len imsi length,max is 20
* @param [out] iccid return imsi from this func
* @retval 0: success
* @retval other: fail
*/
int gsw_get_sim_imsi(int len, char *imsi)
{
int ret = -1;
if(sim_init_flag == 0 || sim_info_handle == NULL)
{
printf("sim sdk has been deinit\n");
return GSW_HAL_NORMAL_FAIL;
}
if(imsi == NULL)
{
LOGE("imsi is NULL.");
return GSW_HAL_ARG_INVALID;
}
if(len < GSW_SIM_IMSI_LENGTH)
{
LOGE("imsi len is too short,len = %d\n", len);
return GSW_HAL_NORMAL_FAIL;
}
ret = mbtk_imsi_get(sim_info_handle, (void *)imsi);
if(ret != MBTK_ERR_OK)
{
LOGE("[gsw_sim] mbtk_imsi_get fail [err = %d].", ret);
return GSW_HAL_NORMAL_FAIL;
}
return GSW_HAL_SUCCESS;
}
/**
* @brief get sim msisdn function
* @param [in] len msisdn length,max is 20
* @param [out] msisdn msisdn length,max is 20
* @retval 0: success
* @retval other: fail
*/
int gsw_get_sim_msisdn(int len, char *msisdn)
{
int ret = -1;
if(sim_init_flag == 0 || sim_info_handle == NULL)
{
printf("sim sdk has been deinit\n");
return GSW_HAL_NORMAL_FAIL;
}
if(msisdn == NULL)
{
printf("msisdn is NULL.");
return GSW_HAL_ARG_INVALID;
}
if(len < GSW_SIM_MSISDN_LENGTH)
{
printf("msisdn len is too short,len = %d\n", len);
return GSW_HAL_NORMAL_FAIL;
}
ret = mbtk_phone_number_get(sim_info_handle, (void *)msisdn);
if(ret != MBTK_ERR_OK)
{
LOGE("[gsw_sim] mbtk_phone_number_get fail [err = %d].", ret);
return GSW_HAL_NORMAL_FAIL;
}
return GSW_HAL_SUCCESS;
}
/**
* @brief set sim power down
* @param
* @retval 0: success
* @retval other: fail
*/
int gsw_set_sim_power_down(void)
{
int ret = -1;
if(sim_init_flag == 0 || sim_info_handle == NULL)
{
printf("sim sdk has been deinit\n");
return GSW_HAL_NORMAL_FAIL;
}
mbtk_modem_info_t info;
info.fun = MBTK_DEV_MODEM_DISABLE_SIM;
info.rst = 0;
ret = mbtk_set_modem_fun(sim_info_handle, &info);
if(ret)
{
LOGE("mbtk_set_modem_fun() fail [err = %d].", ret);
return GSW_HAL_NORMAL_FAIL;
}
return GSW_HAL_SUCCESS;
}
/**
* @brief set sim power up
* @param
* @retval 0: success
* @retval other: fail
*/
int gsw_set_sim_power_up(void)
{
int ret = -1;
if(sim_init_flag == 0 || sim_info_handle == NULL)
{
printf("sim sdk has been deinit\n");
return GSW_HAL_NORMAL_FAIL;
}
mbtk_modem_info_t info;
info.fun = MBTK_DEV_MODEM_FULL_FUN;
info.rst = 0;
ret = mbtk_set_modem_fun(sim_info_handle, &info);
if(ret)
{
LOGE("mbtk_set_modem_fun() fail [err = %d].", ret);
return GSW_HAL_NORMAL_FAIL;
}
return GSW_HAL_SUCCESS;
}