| #include "lynq/lynq_net_api.h" |
| |
| static mbtk_info_handle_t* info_handle = NULL; |
| |
| typedef struct |
| { |
| uint8 *operator_l; |
| uint8 *operator_s; |
| uint32 mcc_mnc; |
| } operator_mcc_mnc_t; |
| |
| static operator_mcc_mnc_t operator_mcc_mnc[] = |
| { |
| {"China Mobile","CMCC",46000}, |
| {"China Unicom","CU",46001}, |
| {"China Mobile","CMCC",46002}, |
| {"China Telecom","CT",46003}, |
| {"China Mobile","CMCC",46004}, |
| {"China Telecom","CT",46005}, |
| {"China Unicom","CU",46006}, |
| {"China Mobile","CMCC",46007}, |
| {"China Mobile","CMCC",46008}, |
| {"China Unicom","CU",46009}, |
| {"China Telecom","CT",46011} |
| }; |
| |
| int lynq_network_init(int uToken) |
| { |
| UNUSED(uToken); |
| if(info_handle == NULL) |
| { |
| info_handle = mbtk_info_handle_get(); |
| if(info_handle) |
| { |
| return 0; |
| } |
| } |
| |
| return -1; |
| } |
| |
| int lynq_network_deinit(void) |
| { |
| if(info_handle) |
| { |
| return mbtk_info_handle_free(&info_handle); |
| } |
| else |
| { |
| return -1; |
| } |
| } |
| |
| int lynq_get_version(char buf[]) |
| { |
| if(info_handle == NULL || buf == NULL) |
| { |
| return -1; |
| } |
| |
| int err = mbtk_version_get(info_handle, buf); |
| if(err) { |
| return -1; |
| } else { |
| return 0; |
| } |
| } |
| |
| int lynq_get_imei(char buf[]) |
| { |
| if(info_handle == NULL || buf == NULL) |
| { |
| return -1; |
| } |
| |
| int err = mbtk_imei_get(info_handle, buf); |
| if(err) { |
| return -1; |
| } else { |
| return 0; |
| } |
| } |
| |
| int lynq_get_sn(char buf[]) |
| { |
| if(info_handle == NULL || buf == NULL) |
| { |
| return -1; |
| } |
| |
| int err = mbtk_sn_get(info_handle, buf); |
| if(err) { |
| return -1; |
| } else { |
| return 0; |
| } |
| } |
| |
| int lynq_get_zone_tmp(ZONE_NUM num, int *temp) |
| { |
| if(info_handle == NULL || temp == NULL) |
| { |
| return -1; |
| } |
| |
| return mbtk_temp_get(info_handle, num, temp); |
| } |
| |
| int lynq_shutdown(char options[]) |
| { |
| if(options == NULL) |
| { |
| return -1; |
| } |
| |
| if(!strcmp(options, "reboot")) { |
| return mbtk_system_reboot(0); |
| } else if(!strcmp(options, "poweroff")) { |
| return mbtk_system_reboot(1); |
| } else if(!strcmp(options, "halt")) { |
| return mbtk_system_reboot(2); |
| } else { |
| return -1; |
| } |
| return 0; |
| } |
| |
| int lynq_time_set(mbtk_time_type_enum time_type, char* time_str) |
| { |
| if(info_handle == NULL) |
| { |
| return -1; |
| } |
| |
| return mbtk_time_set(info_handle, time_type, time_str); |
| } |
| |
| int lynq_get_sim_status(int *card_status) |
| { |
| if(info_handle == NULL || card_status == NULL) |
| { |
| return -1; |
| } |
| |
| mbtk_sim_state_enum sim; |
| int err = mbtk_sim_state_get(info_handle, &sim); |
| if(err) { |
| return -1; |
| } else { |
| *card_status = sim; |
| return 0; |
| } |
| } |
| |
| int lynq_get_imsi(char buf[]) |
| { |
| if(info_handle == NULL || buf == NULL) |
| { |
| return -1; |
| } |
| |
| int err = mbtk_imsi_get(info_handle, buf); |
| if(err) { |
| return -1; |
| } else { |
| return 0; |
| } |
| } |
| |
| int lynq_get_iccid(char buf[]) |
| { |
| if(info_handle == NULL || buf == NULL) |
| { |
| return -1; |
| } |
| |
| int err = mbtk_iccid_get(info_handle, buf); |
| if(err) { |
| return -1; |
| } else { |
| return 0; |
| } |
| } |
| |
| int lynq_query_phone_number(char buf[]) |
| { |
| if(info_handle == NULL || buf == NULL) |
| { |
| return -1; |
| } |
| |
| int err = mbtk_phone_number_get(info_handle, buf); |
| if(err) { |
| return -1; |
| } else { |
| return 0; |
| } |
| } |
| |
| int lynq_sim_power (int mode) |
| { |
| if(mode != 0 && mode != 1) |
| { |
| return -1; |
| } |
| |
| return mbtk_sim_power_set(mode); |
| } |
| |
| int lynq_query_operater(char *OperatorFN,char *OperatorSH,char *MccMnc) |
| { |
| if(info_handle == NULL || OperatorFN == NULL || OperatorSH == NULL || MccMnc == NULL) |
| { |
| return -1; |
| } |
| |
| mbtk_net_info_t net; |
| if(!mbtk_net_sel_mode_get(info_handle, &net) && net.plmn > 0) |
| { |
| // printf("Net : %d, %d, %d\n", net.net_sel_mode, net.net_type, net.plmn); |
| int i = 0; |
| while(i < ARRAY_SIZE(operator_mcc_mnc)) |
| { |
| if(operator_mcc_mnc[i].mcc_mnc == net.plmn) |
| break; |
| i++; |
| } |
| |
| if(i == ARRAY_SIZE(operator_mcc_mnc)) // No found mcc&mnc |
| { |
| strcpy(OperatorFN, "UNKNOWN"); |
| strcpy(OperatorSH, "UNKNOWN"); |
| sprintf(MccMnc, "%d", net.plmn); |
| } |
| else |
| { |
| strcpy(OperatorFN, operator_mcc_mnc[i].operator_l); |
| strcpy(OperatorSH, operator_mcc_mnc[i].operator_s); |
| sprintf(MccMnc, "%d", operator_mcc_mnc[i].mcc_mnc); |
| } |
| return 0; |
| } |
| |
| return -1; |
| } |
| |
| int lynq_query_network_selection_mode (int *netselMode) |
| { |
| if(info_handle == NULL || netselMode == NULL) |
| { |
| return -1; |
| } |
| |
| mbtk_net_info_t net; |
| if(!mbtk_net_sel_mode_get(info_handle, &net)) |
| { |
| // printf("Net : %d, %d, %d\n", net.net_sel_mode, net.net_type, net.plmn); |
| *netselMode = net.net_sel_mode; |
| return 0; |
| } |
| |
| return -1; |
| } |
| |
| int lynq_set_network_selection_mode(const char *mode, const char* mccmnc) |
| { |
| if(info_handle == NULL || str_empty(mode)) |
| { |
| return -1; |
| } |
| |
| mbtk_net_info_t net; |
| net.net_type = 0xFF; |
| if(!strcmp(mode, "Auto")) |
| { |
| net.net_sel_mode = 0; |
| net.plmn = 0; |
| } |
| else if(!strcmp(mode, "Manual") && !str_empty(mccmnc)) |
| { |
| net.net_sel_mode = 1; |
| net.plmn = (uint32)atoi(mccmnc); |
| } |
| else |
| { |
| return -1; |
| } |
| if(!mbtk_net_sel_mode_set(info_handle, &net)) |
| { |
| return 0; |
| } |
| |
| return -1; |
| } |
| |
| int lynq_query_available_network(list_node_t** net_list) |
| { |
| if(info_handle == NULL) |
| { |
| return -1; |
| } |
| |
| return mbtk_available_net_get(info_handle, net_list); |
| } |
| |
| int lynq_query_registration_state(const char *type, int* regState,int *imsRegState,char * LAC,char *CID,int *netType,int * radioTechFam,int *netRejected) |
| { |
| if(info_handle == NULL || str_empty(type) || regState == NULL || imsRegState == NULL |
| || LAC == NULL || CID == NULL || netType == NULL || radioTechFam == NULL || netRejected == NULL) |
| { |
| return -1; |
| } |
| mbtk_net_reg_info_t reg; |
| int err = mbtk_net_reg_get(info_handle, ®); |
| if(err) { |
| *netRejected = err; |
| return -1; |
| } else { |
| //printf("REG : %d, %d, %d, %04x, %08o\n", reg.state, reg.type, reg.ims_reg, reg.lac, reg.ci); |
| // Voice/Data/IMS |
| if(strcmp("Voice", type) == 0) { |
| *regState = reg.call_state; |
| } else if(strcmp("Data", type) == 0) { |
| *regState = reg.data_state; |
| } else if(strcmp("IMS", type) == 0) { |
| *imsRegState = reg.ims_state; |
| } else { |
| return -1; |
| } |
| |
| if(reg.call_state != MBTK_NET_REG_STATE_NON || reg.data_state != MBTK_NET_REG_STATE_NON || reg.ims_state != MBTK_NET_REG_STATE_NON) { |
| sprintf(LAC, "%04x", reg.lac); |
| sprintf(CID, "%08o", reg.ci); |
| *netType = reg.type; |
| *radioTechFam = RADIO_TECH_3GPP; |
| } |
| return 0; |
| } |
| } |
| |
| int lynq_query_prefferred_networktype (int *preNetType) |
| { |
| if(info_handle == NULL || preNetType == NULL) |
| { |
| return -1; |
| } |
| |
| mbtk_band_info_t band; |
| int err = mbtk_current_band_get(info_handle, &band); |
| if(err) { |
| return -1; |
| } else { |
| //printf("Band : %d, %d, %d, %d, %d\n", band.net_pref, band.gsm_band, band.umts_band, band.tdlte_band, band.fddlte_band); |
| *preNetType = band.net_pref; |
| return 0; |
| } |
| } |
| |
| int lynq_set_prefferred_networktype (const int preNetType) |
| { |
| |
| if(info_handle == NULL) |
| { |
| return -1; |
| } |
| if(preNetType < 0 || preNetType > 15) |
| { |
| return -2; |
| } |
| mbtk_band_info_t band; |
| memset(&band, 0, sizeof(mbtk_band_info_t)); |
| band.net_pref = preNetType; |
| int err = mbtk_current_band_set(info_handle, &band); |
| if(err) { |
| return -1; |
| } else { |
| return 0; |
| } |
| } |
| |
| int lynq_set_band_mode(int gsm_band, int umts_band, int tdlte_band, int fddlte_band) |
| { |
| if(info_handle == NULL) |
| { |
| return -1; |
| } |
| |
| mbtk_band_info_t band; |
| band.net_pref = 0xFF; // No change network pref. |
| band.gsm_band = (uint16)gsm_band; |
| band.umts_band = (uint16)umts_band; |
| band.tdlte_band = (uint32)tdlte_band; |
| band.fddlte_band = (uint32)fddlte_band; |
| int err = mbtk_current_band_set(info_handle, &band); |
| if(err) { |
| return -1; |
| } else { |
| return 0; |
| } |
| } |
| |
| int lynq_query_available_bandmode (int *gsm_band, int *umts_band, int *tdlte_band, int *fddlte_band) |
| { |
| if(info_handle == NULL || gsm_band == NULL || umts_band == NULL || tdlte_band == NULL || fddlte_band == NULL) |
| { |
| return -1; |
| } |
| |
| mbtk_band_info_t band; |
| int err = mbtk_support_band_get(info_handle, &band); |
| if(err) { |
| return -1; |
| } else { |
| //printf("Band : %d, %d, %d, %d, %d\n", band.net_pref, band.gsm_band, band.umts_band, band.tdlte_band, band.fddlte_band); |
| *gsm_band = band.gsm_band; |
| *umts_band = band.umts_band; |
| *tdlte_band = band.tdlte_band; |
| *fddlte_band = band.fddlte_band; |
| return 0; |
| } |
| } |
| |
| int lynq_radio_on (const int data) |
| { |
| if(info_handle == NULL || (data != 0 && data != 1)) |
| { |
| return -1; |
| } |
| |
| int err = mbtk_radio_state_set(info_handle, data); |
| if(err) { |
| return -1; |
| } else { |
| return 0; |
| } |
| } |
| |
| int lynq_query_radio_tech (int* radioTech) |
| { |
| if(info_handle == NULL || radioTech == NULL) |
| { |
| return -1; |
| } |
| |
| int err = mbtk_radio_state_get(info_handle, radioTech); |
| if(err) { |
| return -1; |
| } else { |
| return 0; |
| } |
| } |
| |
| int lynq_solicited_signal_strength (signalStrength_t *solSigStren) |
| { |
| if(info_handle == NULL || solSigStren == NULL) |
| { |
| return -1; |
| } |
| |
| mbtk_signal_info_t signal; |
| int err = mbtk_net_signal_get(info_handle, &signal); |
| if(err) { |
| return -1; |
| } else { |
| memset(solSigStren, 0, sizeof(signalStrength_t)); |
| switch(mbtk_net_type_get(signal.type)) |
| { |
| case MBTK_NET_TYPE_GSM: |
| solSigStren->gsm_sig_valid = 1; |
| break; |
| case MBTK_NET_TYPE_UMTS: |
| solSigStren->umts_sig_valid = 1; |
| break; |
| case MBTK_NET_TYPE_LTE: |
| solSigStren->lte_sig_valid = 1; |
| break; |
| default: |
| break; |
| } |
| solSigStren->rssi = signal.rssi; |
| solSigStren->ber = signal.ber; |
| solSigStren->rxlev = signal.rxlev; |
| solSigStren->rscp = signal.rscp; |
| solSigStren->ecno = signal.ecno; |
| solSigStren->rsrq = signal.rsrq; |
| solSigStren->rsrp = signal.rsrp; |
| return 0; |
| } |
| } |
| |
| int lynq_set_ims (const int ims_mode) |
| { |
| if(info_handle == NULL || (ims_mode != 0 && ims_mode != 1)) |
| { |
| return -1; |
| } |
| |
| int err = mbtk_volte_state_set(info_handle, ims_mode); |
| if(err) { |
| return -1; |
| } else { |
| return 0; |
| } |
| } |
| |
| int lynq_init_cell(void) |
| { |
| if(info_handle == NULL) |
| { |
| info_handle = mbtk_info_handle_get(); |
| if(info_handle) |
| { |
| printf("creat info_handle is success\n"); |
| } |
| else{ |
| printf("creat info_handle is fail\n"); |
| return -1; |
| } |
| } |
| |
| return 0; |
| } |
| |
| int lynq_deinit_cell(void) |
| { |
| if(info_handle) |
| { |
| return mbtk_info_handle_free(&info_handle); |
| } |
| else |
| { |
| return -1; |
| } |
| } |
| |
| /* |
| * Get current cell infomation. |
| */ |
| int lynq_query_cell_info(int *type, list_node_t **cell_list) |
| { |
| if(info_handle == NULL || type == NULL) |
| { |
| return -1; |
| } |
| |
| return mbtk_cell_get(info_handle, type, cell_list); |
| } |
| |
| /* |
| * set current cell infomation. |
| */ |
| void lynq_set_cell_info(char *mem) |
| { |
| if(info_handle == NULL || mem == NULL) |
| { |
| return -1; |
| } |
| |
| char resp[1024] = {0}; |
| mbtk_cell_set(info_handle, mem, resp); |
| |
| return ; |
| } |
| |
| /* |
| * Set specific APN informations. |
| * |
| * cid : 2-7 |
| */ |
| int lynq_apn_set(int cid, mbtk_ip_type_enum ip_type, const void* apn_name, |
| const void *user_name, const void *user_pass, const void *auth) |
| { |
| if(info_handle == NULL || str_empty(apn_name)) |
| { |
| return -1; |
| } |
| |
| return mbtk_apn_set(info_handle, cid, ip_type, apn_name, user_name, user_pass, auth); |
| } |
| |
| /* |
| * Get current all APN informations. |
| */ |
| int lynq_apn_get(int *apn_num, mbtk_apn_info_t apns[]) |
| { |
| if(info_handle == NULL || apn_num == NULL || apns == NULL) |
| { |
| return -1; |
| } |
| |
| return mbtk_apn_get(info_handle, apn_num, apns); |
| } |
| |
| /* |
| * Start data call. |
| */ |
| int lynq_data_call_start(int cid, int timeout) |
| { |
| if(info_handle == NULL) |
| { |
| return -1; |
| } |
| |
| return mbtk_data_call_start(info_handle, cid, 0, FALSE, timeout); |
| } |
| |
| /* |
| * Stop data call. |
| */ |
| int lynq_data_call_stop(int cid, int timeout) |
| { |
| if(info_handle == NULL) |
| { |
| return -1; |
| } |
| |
| return mbtk_data_call_stop(info_handle, cid, timeout); |
| } |
| |
| /* |
| * Query data call state. |
| */ |
| int lynq_data_call_query(int cid, mbtk_ipv4_info_t *ipv4, mbtk_ipv6_info_t *ipv6) |
| { |
| if(info_handle == NULL || ipv4 == NULL || ipv6 == NULL) |
| { |
| return -1; |
| } |
| |
| return mbtk_data_call_state_get(info_handle, cid, ipv4, ipv6); |
| } |
| |
| /* |
| * Get the native ip and free port. |
| */ |
| int lynq_get_ip_and_port(char *ipBuf_out,int *port,int iptype) |
| { |
| char psz_port_cmd[128]; |
| int i=0; |
| |
| *port = rand() % (60000 - 50000 + 1) + 50000; |
| sprintf(psz_port_cmd, "netstat -an | grep :%d > /dev/null", *port); |
| |
| char ipBuf[32] = ""; |
| FILE *fstream=NULL; |
| |
| char buff[1024]; |
| char iptype_str[8]; |
| memset(buff,0,sizeof(buff)); |
| /*eth0????eth1?docker0?em1?lo?*/ |
| if(iptype == 1) |
| { |
| |
| if(NULL==(fstream=popen("ifconfig ccinet0 | grep \"inet6 addr: 2\" | awk '{print $3}'","r"))) |
| { |
| snprintf(ipBuf, 39, "%s","0:0:0:0:0:0:0:0"); |
| } |
| if(NULL!=fgets(buff, sizeof(buff), fstream)) |
| { |
| snprintf(ipBuf, 39, "%s",buff); |
| } |
| else |
| { |
| snprintf(ipBuf, 39, "%s","0:0:0:0:0:0:0:0"); |
| pclose(fstream); |
| } |
| } |
| else if(iptype == 0) |
| { |
| if(NULL==(fstream=popen("ifconfig ccinet0 | grep \"inet addr:\" | awk \'{print $2}\' | cut -c 6-","r"))) |
| { |
| snprintf(ipBuf, 18, "%s","0.0.0.0"); |
| } |
| if(NULL!=fgets(buff, sizeof(buff), fstream)) |
| { |
| snprintf(ipBuf, 18, "%s",buff); |
| } |
| else |
| { |
| snprintf(ipBuf, 18, "%s","0.0.0.0"); |
| pclose(fstream); |
| } |
| } |
| else |
| { |
| return -1; |
| } |
| pclose(fstream); |
| |
| printf("ip:%s\n", ipBuf); |
| memcpy(ipBuf_out, ipBuf, 32); |
| return 0; |
| } |
| |
| int lynq_get_modem_fun(MBTK_DEV_MODEM_FUNCTION *fun) |
| { |
| if(info_handle == NULL) |
| { |
| return -1; |
| } |
| |
| int err = mbtk_get_modem_fun(info_handle, fun); |
| if(err) { |
| return -1; |
| } else { |
| return 0; |
| } |
| } |