#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <pthread.h>
#include <fcntl.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <cust.h>
#include "si_linux.h"
#include "si_voice.h"
#include "proslic.h"
#include "si3217x_constants.h"
#include "autoconf.h"
#ifdef CONFIG_RALINK_GPIO
#include "ralink_gpio.h"
#define GPIO_DEV "/dev/gpio"
#else
#error no defined
#endif
#include <signal.h>
//by zhuhaiyan 20120218
#include "nvram.h"
#ifdef FEATURE_SUPPORT_SMS
#include "../../sms/sms.h"
#endif
#include "../../comop/comop.h"
#ifdef SI3226X
#error "Only Si3226X has been tested!"
#else
#define VOICE_DEVICE_TYPE SI3217X_TYPE
#endif
pid_t g_wan_pid = 0;
char g_pin_lock = 0;
char g_sysinfo = -1;
char g_pinstatus = -1;
char g_sim_type = SIM_UNKNOWN;
char g_service_type = NONE;
char g_init_modem = 0;
unsigned char g_reg_stat = 0;
unsigned char g_ring = 0;
unsigned char g_is_ringing = 0;
unsigned char g_call_ison = 0;
unsigned char g_get_done = 0;
unsigned char g_slic_init = 0;
unsigned char g_ring_hookon = 0;
unsigned char g_voice = 0;
unsigned char g_answer = 0;
//unsigned char count = 5;
#define PCM4
#define debug printf("\nFuncton:%s,run to %d\n",__FUNCTION__,__LINE__)
//printf("Functon:%s,run to %d\n",__FUNCTION__,__LINE__)
typedef struct
{
SiVoiceChanType_ptr VoiceObj;
int is_tone_on;
int is_ringing;
int tone_selected; /* -1 = Invalid */
int tone_needs_tmr;
int is_xconn;
} chanState;
si_hctrl_t g_hw_ctrl;
SiVoiceControlInterfaceType * g_sivoice_sys_if;
chanState g_ports[NUMBER_OF_CHAN];
#ifdef FEATURE_CALL_ID
char cid[40];
#endif
int modem_reboot()
{
int fd = 0;
int data = 0;
fd = open(GPIO_DEV, O_RDONLY);
if (fd < 0) {
perror(GPIO_DEV);
return -1;
}
if (ioctl(fd, RALINK_GPIO3924_SET_DIR_OUT, 1 << (35 -24)) < 0)
goto ioctl_err;
if (ioctl(fd, RALINK_GPIO3924_CLEAR, 1 << (35 -24)) < 0)
goto ioctl_err;
sleep(2);
if (ioctl(fd, RALINK_GPIO3924_SET, 1 << (35 -24)) < 0)
goto ioctl_err;
close(fd);
return 0;
printf("<< modem_reboot >>\n");
ioctl_err:
perror("ioctl");
close(fd);
return -1;
}
int send_at_cmd(const char * cmd, int tmo_level)
{
char receive[AT_RECEIVE_BUFF_SIZE] = {0};
int i = 0;
atoperation(cmd, receive, 0, tmo_level);
printf("\n\nsend_at_cmd: at:%s,receive:%s\n",cmd,receive);
if(0 != test_at_message_is_error(receive))
{
printf("fail to send at cmd:%s\n", cmd);
printf(receive);
return -1;
}
return 0;
}
int sendate(void)
{
char ret = -1;
char sl = 0;
do
{
ret = send_at_cmd(ATE, WAIT_TIME_LEVEL_1);
if (ret != 0)
{
printf("fail to init AT cmd on %s, try gain...\n", AT_PORT);
sleep(1);
sl++;
printf("have sleep %ds\n",sleep);
}
else
break;
} while (1);
#ifdef BMC_HR22_P300
do
{
ret = send_at_cmd(CFUN, WAIT_TIME_LEVEL_1);
if (ret != 0)
{
printf("fail to init AT cmd on %s, try gain...\n", AT_PORT);
sleep(1);
sl++;
printf("have sleep %ds\n",sleep);
}
else
break;
} while (1);
#endif
return 0;
}
void getcurnetmode()
{
char receive[AT_RECEIVE_BUFF_SIZE] = {0};
char bmmode = 0;
const char * pszHeader = "+BMMODPREF: ";
char * p = NULL;
atoperation(BMMODPREF, receive, 0, WAIT_TIME_LEVEL_2);
p = strstr(receive, pszHeader);
if (!p) return write_at_message(BMRAT_SAVE_FILE, "bmmode Error");
p += strlen(pszHeader);
sscanf(p, "%d", &bmmode);
if (0 != bmmode)
{
atoperation(BMMODPREF0, receive, 0, WAIT_TIME_LEVEL_2);
sleep(1);
system("ralink_init clear 2860");
sleep(2);
// system("killall -SIGUSR2 down");
system("reboot");
}
}
//get cpin status.
int getcpin(void)
{
char receive[AT_RECEIVE_BUFF_SIZE] = {0};
atoperation(CPIN, receive, 0, WAIT_TIME_LEVEL_5);
printf("\n\n\t\tgetcpin send: %s\n",CPIN);
printf("receive:%s\n",receive);
if (0 == test_at_message_is_error(receive)) {
printf("\n\ngetcpin ------>>:receive:%s\n",receive);
char *p = strstr(receive,"+CPIN:");
printf("get cpin status: %s\n", receive);
if(NULL != p)
{
p = p + strlen("+CPIN:");
if (NULL != strstr(p, "Ready")) {
system("echo 0 > /tmp/lock_pin");
system("echo 0 > /tmp/pinstatus");
g_pin_lock = 0;
return write_at_message(CPIN_SAVE_FILE, "Sim Ready");
}
else if(NULL != strstr(p, "READY")) {
system("echo 0 > /tmp/lock_pin");
system("echo 0 > /tmp/pinstatus");
g_pin_lock = 0;
return write_at_message(CPIN_SAVE_FILE, "Sim Ready");
}
else if(NULL != strstr(p, "PIN")) {
system("echo 1 > /tmp/lock_pin");
system("echo 1 > /tmp/pinstatus");
g_pin_lock = 1;
return write_at_message(CPIN_SAVE_FILE, "Sim PIN-lock");
}
else if(NULL != strstr(p, "PUK")) {
system("echo 2 > /tmp/lock_pin");
system("echo 1 > /tmp/pinstatus");
g_pin_lock = 2;
system("cat /tmp/lock_pin");
system("cat /tmp/pinstatus");
return write_at_message(CPIN_SAVE_FILE, "Sim PUK-lock");
}
else {
system("echo 5 > /tmp/lock_pin");
system("echo 1 > /tmp/pinstatus");
g_pin_lock = 5;
return write_at_message(CPIN_SAVE_FILE, "Sim Error");
}
}
}
else {
if(NULL != strstr(receive, "SIM busy"))
{
system("echo 3 > /tmp/lock_pin");
g_pin_lock = 3;
return write_at_message(CPIN_SAVE_FILE, "SIM busy");
}
else if (NULL !=strstr(receive,"SIM failure"))
{
system("echo 4 > /tmp/lock_pin");
g_pin_lock = 4;
return write_at_message(CPIN_SAVE_FILE, "SIM failure");
}
else if (NULL !=strstr(receive,"SIM not inserted"))
{
system("echo -1 > /tmp/lock_pin");
g_pin_lock = -1;
return write_at_message(CPIN_SAVE_FILE, "SIM not inserted");
}
else
{
system("echo 5 > /tmp/lock_pin");
g_pin_lock = 5;
printf("LINE %d,FUNCTION %s,%s", __LINE__,__FUNCTION__,receive);
return write_at_message(CPIN_SAVE_FILE, "SIM Eorr");
}
}
}
int getcpncnt(void)
{
char receive[AT_RECEIVE_BUFF_SIZE] = {0};
char message[MESSAGE_SAVE_FILE_SIZE] = {0};
int pincnt = 0, pukcnt = 0;
const char * pszHeader = "+BMCPNCNT: ";
char * p = NULL;
atoperation(BMCPNCNT, receive, 0, WAIT_TIME_LEVEL_6);
printf("\n\ngetcpncnt----------------->>>>>:send %s\n",BMCPNCNT);
printf("receive:%s\n",receive);
if (0 != test_at_message_is_error(receive)) {
return write_at_message(BMCPNCNT_SAVE_FILE, "PIN Error\nPUK Error");
}
p = strstr(receive, pszHeader);
if (!p) return write_at_message(BMCPNCNT_SAVE_FILE, "PIN Error\nPUK Error");
p += strlen(pszHeader);
sscanf(receive, "PIN1=%d; PUK1=%d;", &pincnt, &pukcnt);
sprintf(message,"PIN %d\nPUK %d", pincnt, pukcnt);
return write_at_message(BMCPNCNT_SAVE_FILE, message);
}
void getmeid();
//wait for pin ready, if locked, wait for unlocked, if puk ,wait for unlock puk...
void waitfor_pin_ready()
{
char buffer[48] = {0};
int cnt = 0;
FILE *fd = fopen("/tmp/pin_process_status","r+");
if(NULL != fd)
{
printf("<<<---------------------------------->>>\n");
//fscanf(fd,"%c",g_pinstatus);
char ch;
ch = fgetc(fd);
printf("pin_process_status -->ch:%c\n",ch);
g_pinstatus = ch - '0';
printf("<<<<*************************************>>\n");
printf("g_pinstatus:%d\n",g_pinstatus);
fclose(fd);
}
// getmeid();
int counter = 6;
do
{
getcpin();
printf("cpin status=%d\n", g_pin_lock);
//by zhuhaiyan 20120705, for pin_process_status.
sprintf(buffer, "echo %d > /tmp/pin_process_status", g_pin_lock);
system(buffer);
if(4 == g_pin_lock && counter > 0)
{
counter--;
sleep(1);
continue;
}
} while(g_pin_lock == 3); //if the status is busy status, go continue...
if(1 == g_pinstatus)
{
char pin[8]= {0};
char AT_CMD[40] = {0};
char receive[AT_RECEIVE