parent
96a82a6f12
commit
7593b9f5c2
|
@ -9,63 +9,6 @@ int g_status = STOPPED;
|
|||
pthread_t lte;
|
||||
|
||||
|
||||
ret_code compare(char *result)
|
||||
{
|
||||
if(NULL == result)
|
||||
{
|
||||
return RET_NULL_INPUT_ERR;
|
||||
}
|
||||
|
||||
if(strstr(result,"lte no match") != NULL)
|
||||
{
|
||||
return RET_LTE_NO_MATCH_ERR;
|
||||
}
|
||||
|
||||
else if(strstr(result,"no found lte module") != NULL)
|
||||
{
|
||||
return RET_NO_LTE_MODULE_ERR;
|
||||
}
|
||||
|
||||
else if(strstr(result,"4g init done") != NULL)
|
||||
{
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
return RET_UNKNOWN;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
ret_code LTE_config_init()
|
||||
{
|
||||
ret_code ret = RET_OK;
|
||||
char result[RESULT_LENGTH] = {0};
|
||||
FILE *f = NULL;
|
||||
f = popen("/etc/ppp/ppp-4g init","r");
|
||||
|
||||
if (NULL == f)
|
||||
{
|
||||
rpc_log_info("execuate cmd fail\n");
|
||||
return RET_EXEC_SHELL_ERR;
|
||||
}
|
||||
|
||||
fseek(f,0,SEEK_SET);
|
||||
|
||||
while(fgets(result,sizeof(result),f) != NULL)
|
||||
{
|
||||
rpc_log_info("%s\n",result);
|
||||
ret = compare(result);
|
||||
memset(result,sizeof(result),0);
|
||||
fseek(f,0,SEEK_CUR);
|
||||
}
|
||||
|
||||
pclose(f);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
{
|
||||
"operate":"set",
|
||||
|
@ -135,18 +78,10 @@ ret_code LTE_json_parse(pointer input, uint *conf_type, int action)
|
|||
{
|
||||
g_action = START;
|
||||
}
|
||||
/*else if(strcmp(action_type->valuestring,"restart") == 0)
|
||||
{
|
||||
g_action = RESTART;
|
||||
}*/
|
||||
else if(strcmp(action_type->valuestring,"stop") == 0)
|
||||
{
|
||||
g_action = STOP;
|
||||
}
|
||||
/*else if(strcmp(action_type->valuestring,"init") == 0)
|
||||
{
|
||||
g_action = INIT;
|
||||
}*/
|
||||
else
|
||||
{
|
||||
cJSON_Delete(json_obj);
|
||||
|
@ -159,49 +94,137 @@ ret_code LTE_json_parse(pointer input, uint *conf_type, int action)
|
|||
return ret;
|
||||
}
|
||||
|
||||
void killpid()
|
||||
int killpid()
|
||||
{
|
||||
char pid[10] =
{0};
|
||||
char cmd[20] = {0};
|
||||
int pid_int;
|
||||
FILE *fp = NULL;
|
||||
fp = popen(GETPID,"r");
|
||||
fp = popen("pgrep pppd","r");
|
||||
if(NULL == fp)
|
||||
{
|
||||
rpc_log_info("can not get pid\n");
|
||||
return;
|
||||
return 0;
|
||||
}
|
||||
fgets(pid,sizeof(pid),fp);
|
||||
while(fgets(pid,sizeof(pid),fp) != NULL)
|
||||
{
|
||||
sscanf(pid,"%d",&pid_int);
|
||||
rpc_log_info("pid is %d\n",pid_int);
|
||||
sprintf(cmd,"kill %d",pid_int);
|
||||
system(cmd);
|
||||
memset(pid,0,sizeof(pid));
|
||||
}
|
||||
pclose(fp);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int start_success()
|
||||
{
|
||||
char result[RESULT_LENGTH] = {0};
|
||||
FILE *f = NULL;
|
||||
f = popen("/etc/ppp/ppp-4g status","r");
|
||||
if(NULL == f)
|
||||
{
|
||||
rpc_log_info("execuate cmd fail\n");
|
||||
return RET_EXEC_SHELL_ERR;
|
||||
}
|
||||
|
||||
fgets(result,sizeof(result),f);
|
||||
//rpc_log_info("%s\n",result);
|
||||
if(strstr(result,"on") != NULL)
|
||||
{
|
||||
pclose(f);
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
pclose(f);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void _start_lte(void)
|
||||
{
|
||||
int wait_time = 0;
|
||||
char result[RESULT_LENGTH] = {0};
|
||||
int wait_time_phase1 = 0;
|
||||
int wait_time_phase2 = 0;
|
||||
int recover = 0;
|
||||
g_status = STARTING;
|
||||
while(wait_time < 60)
|
||||
while(wait_time_phase1 < 60)
|
||||
{
|
||||
if(access(file_path,0) != -1)
|
||||
if(start_success())
|
||||
{
|
||||
sleep(1000);
|
||||
wait_time ++;
|
||||
g_status = CONNECTED;
|
||||
rpc_log_info("connected\n");
|
||||
if(!recover)
|
||||
{
|
||||
//执行各模块与ppp0有关的脚本
|
||||
recover = 1;
|
||||
}
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
g_status = CONNECTED;
|
||||
return;
|
||||
sleep(1);
|
||||
wait_time_phase1 ++;
|
||||
//rpc_log_info("time %d\n",wait_time_phase1);
|
||||
}
|
||||
}
|
||||
|
||||
rpc_log_info("start init\n");
|
||||
system("/etc/ppp/ppp-4g init");
|
||||
|
||||
//restart_lte();
|
||||
FILE *f = NULL;
|
||||
f = popen("/etc/ppp/ppp-4g start","r");
|
||||
|
||||
pthread_join(lte,NULL);
|
||||
if (NULL == f)
|
||||
{
|
||||
rpc_log_info("execuate cmd fail\n");
|
||||
g_status = START_FAIL;
|
||||
return;
|
||||
}
|
||||
|
||||
fseek(f,0,SEEK_SET);
|
||||
fgets(result,sizeof(result),f);
|
||||
rpc_log_info("%s\n",result);
|
||||
|
||||
if(strstr(result,"start pppd ppp0") != NULL)
|
||||
{
|
||||
while(wait_time_phase2 < 60)
|
||||
{
|
||||
if(start_success())
|
||||
{
|
||||
g_status = CONNECTED;
|
||||
if(!recover)
|
||||
{
|
||||
//执行各模块与ppp0有关的脚本
|
||||
recover = 1;
|
||||
}
|
||||
pclose(f);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
sleep(1);
|
||||
wait_time_phase2
++;
|
||||
//rpc_log_info("time %d\n",wait_time_phase2);
|
||||
}
|
||||
}
|
||||
pclose(f);
|
||||
g_status = START_FAIL;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
g_status = START_FAIL;
|
||||
pclose(f);
|
||||
}
|
||||
|
||||
if(killpid())
|
||||
{
|
||||
rpc_log_info("kill success\n");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -220,7 +243,6 @@ ret_code start_lte()
|
|||
|
||||
if (NULL == f)
|
||||
{
|
||||
g_status = START_FAIL;
|
||||
rpc_log_info("execuate cmd fail\n");
|
||||
return RET_EXEC_SHELL_ERR;
|
||||
}
|
||||
|
@ -230,24 +252,32 @@ ret_code start_lte()
|
|||
fgets(result,sizeof(result),f);
|
||||
|
||||
rpc_log_info("%s\n",result);
|
||||
rpc_log_info("status %d\n",g_status);
|
||||
|
||||
if(strstr(result,"not install lte") != NULL )
|
||||
{
|
||||
g_status = START_FAIL;
|
||||
pclose(f);
|
||||
return RET_NO_LTE_MODULE_ERR;
|
||||
}
|
||||
|
||||
else if(strstr(result,"pppd apparently already active")
!= NULL)
|
||||
{
|
||||
g_status = START_FAIL;
|
||||
pclose(f);
|
||||
return RET_ALREADY_START_ERR;
|
||||
}
|
||||
else if(strstr(result,"start pppd ppp0") != NULL)
|
||||
{
|
||||
pthread_create(<e,NULL,(void *)&_start_lte,NULL);
|
||||
g_status = STARTING;
|
||||
if (pthread_create(<e,NULL,(void *)&_start_lte,NULL) == 0)
|
||||
{
|
||||
rpc_log_info("create new pthread success\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
rpc_log_info("create new pthread fail\n");
|
||||
killpid();
|
||||
pclose(f);
|
||||
return RET_ERR;
|
||||
}
|
||||
pclose(f);
|
||||
return RET_OK;
|
||||
}
|
||||
|
@ -260,6 +290,11 @@ ret_code start_lte()
|
|||
|
||||
ret_code stop_lte()
|
||||
{
|
||||
if(g_status == STARTING)
|
||||
{
|
||||
return RET_ANOTHER_IN_PROCESS;
|
||||
}
|
||||
|
||||
char result[RESULT_LENGTH] = {0};
|
||||
|
||||
FILE *f = NULL;
|
||||
|
@ -284,9 +319,10 @@ ret_code stop_lte()
|
|||
return RET_ALREADY_STOP_ERR;
|
||||
}
|
||||
|
||||
else if((strstr(result,"stop pppd ppp0") != NULL) && access(file_path,0) != -1)
|
||||
else if((strstr(result,"stop pppd ppp0") != NULL))
|
||||
{
|
||||
g_status = STOPPED;
|
||||
rpc_log_info("%d\n",g_status);
|
||||
pclose(f);
|
||||
return RET_OK;
|
||||
}
|
||||
|
@ -298,7 +334,7 @@ ret_code stop_lte()
|
|||
|
||||
}
|
||||
|
||||
void restart_lte()
|
||||
/*void restart_lte()
|
||||
{
|
||||
char result[RESULT_LENGTH] = {0};
|
||||
int wait_time = 0;
|
||||
|
@ -321,38 +357,23 @@ void restart_lte()
|
|||
|
||||
rpc_log_info("%s\n",result);
|
||||
|
||||
if(strstr(result,"not install lte! ") != NULL)
|
||||
{
|
||||
g_status = START_FAIL;
|
||||
pclose(f);
|
||||
return;
|
||||
}
|
||||
|
||||
else if(strstr(result,"pppd apparently already active") != NULL)
|
||||
{
|
||||
g_status = START_FAIL;
|
||||
pclose(f);
|
||||
return;
|
||||
}
|
||||
else if(strstr(result,"start pppd ppp0") != NULL)
|
||||
if(strstr(result,"start pppd ppp0") != NULL)
|
||||
{
|
||||
while(wait_time < 60)
|
||||
{
|
||||
if(access(file_path,0) != -1)
|
||||
{
|
||||
sleep(1000);
|
||||
wait_time
++;
|
||||
}
|
||||
else
|
||||
if(start_success() == 1)
|
||||
{
|
||||
g_status = CONNECTED;
|
||||
pclose(f);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
sleep(1);
|
||||
wait_time
++;
|
||||
}
|
||||
}
|
||||
pclose(f);
|
||||
//ɱËÀ½ø³Ì
|
||||
killpid();
|
||||
g_status = START_FAIL;
|
||||
return;
|
||||
}
|
||||
|
@ -364,9 +385,10 @@ void restart_lte()
|
|||
return;
|
||||
}
|
||||
|
||||
}
|
||||
}*/
|
||||
|
||||
ret_code lte_to_json(char *status, int connect_time, pointer output, int *output_len)
|
||||
|
||||
ret_code lte_to_json(char *status, pointer output, int *output_len)
|
||||
{
|
||||
if(NULL == status || NULL == output || NULL == output_len)
|
||||
{
|
||||
|
@ -383,11 +405,6 @@ ret_code lte_to_json(char *status, int connect_time, pointer output, int *output
|
|||
|
||||
cJSON_AddStringToObject(ret_root,"status", status);
|
||||
|
||||
if(connect_time != 0)
|
||||
{
|
||||
cJSON_AddNumberToObject(ret_root,"connect_time",connect_time);
|
||||
}
|
||||
|
||||
rpc_json_print(ret_root);
|
||||
|
||||
char *status_info = cJSON_PrintUnformatted(ret_root);
|
||||
|
@ -409,7 +426,6 @@ ret_code status_lte(pointer output, int *output_len)
|
|||
|
||||
char result[RESULT_LENGTH] = {0};
|
||||
char status[STATUS_LENGTH] = {0};
|
||||
int connect_time = 0;
|
||||
ret_code ret = RET_OK;
|
||||
|
||||
FILE *f = NULL;
|
||||
|
@ -424,6 +440,7 @@ ret_code status_lte(pointer output, int *output_len)
|
|||
rpc_log_info("%s\n",result);
|
||||
if(strstr(result,"off") != NULL)
|
||||
{
|
||||
rpc_log_info("status is %d\n",g_status);
|
||||
if(g_status == STARTING)
|
||||
{
|
||||
strcpy(status,"starting...");
|
||||
|
@ -436,31 +453,21 @@ ret_code status_lte(pointer output, int *output_len)
|
|||
{
|
||||
strcpy(status,"start fail");
|
||||
}
|
||||
else
|
||||
{
|
||||
return RET_ERR;
|
||||
}
|
||||
//·â×°json
|
||||
ret = lte_to_json(status,connect_time,output,output_len);
|
||||
pclose(f);
|
||||
return ret;
|
||||
}
|
||||
else if(strstr(result,"on") != NULL)
|
||||
{
|
||||
strcpy(status,"started");
|
||||
memset(result,0,sizeof(result));
|
||||
fgets(result,sizeof(result),f);
|
||||
rpc_log_info("%s\n",result);
|
||||
sscanf(result,"ppp_keep_time=%d",&connect_time);
|
||||
ret = lte_to_json(status,connect_time,output,output_len);
|
||||
pclose(f);
|
||||
return ret;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
pclose(f);
|
||||
return RET_UNKNOWN;
|
||||
return RET_ERR;
|
||||
}
|
||||
//封装json
|
||||
ret = lte_to_json(status,output,output_len);
|
||||
pclose(f);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret_code LTE_config_chk(uint source, uint *config_type,
|
||||
|
@ -472,6 +479,17 @@ ret_code LTE_config_chk(uint source, uint *config_type,
|
|||
return RET_NULL_INPUT_ERR;
|
||||
}
|
||||
|
||||
|
||||
if(source == CONFIG_FROM_RECOVER1)
|
||||
{
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
if(source == CONFIG_FROM_RECOVER2)
|
||||
{
|
||||
return RET_CHKERR;
|
||||
}
|
||||
|
||||
ret_code ret = RET_OK;
|
||||
//int action = 0;
|
||||
|
||||
|
@ -486,21 +504,21 @@ ret_code LTE_config_proc(uint source, uint config_type,
|
|||
{
|
||||
ret_code ret = RET_OK;
|
||||
|
||||
if(source == CONFIG_FROM_RECOVER1 || source == CONFIG_FROM_RECOVER2)
|
||||
{
|
||||
ret = start_lte();
|
||||
return ret;
|
||||
}
|
||||
|
||||
if(config_type == CM_CONFIG_SET)
|
||||
{
|
||||
switch(g_action){
|
||||
case START:
|
||||
ret = start_lte();
|
||||
break;
|
||||
/* case RESTART:
|
||||
ret = restart_lte();
|
||||
break;*/
|
||||
case STOP:
|
||||
ret = stop_lte();
|
||||
break;
|
||||
/* case INIT:
|
||||
ret = LTE_config_init();
|
||||
break;*/
|
||||
default:
|
||||
ret = RET_INPUTERR;
|
||||
break;
|
||||
|
@ -524,6 +542,11 @@ ret_code LTE_config_get_all(uint source,
|
|||
return RET_INPUTERR;
|
||||
}
|
||||
|
||||
if (source == CONFIG_FROM_RECOVER1 || source == CONFIG_FROM_RECOVER2)
|
||||
{
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
ret_code ret = RET_OK;
|
||||
|
||||
*output_len = 0;
|
||||
|
|
|
@ -20,19 +20,18 @@
|
|||
#define RESTART_CMD "/etc/ppp/ppp-4G restart"
|
||||
#define STATUS_CMD "/etc/ppp/ppp-4G status"
|
||||
#define file_path "/var/run/ppp0.pid"
|
||||
#define GETPID "pgrep ppp-4g"
|
||||
#define GETPID "pgrep pppd"
|
||||
#define START 1
|
||||
#define STOP 2
|
||||
#define STATUS 3
|
||||
#define RESULT_LENGTH 60
|
||||
#define ACTION_LENGTH 10
|
||||
#define STATUS_LENGTH 10
|
||||
#define STATUS_LENGTH 15
|
||||
#define STARTING 1
|
||||
#define STOPPED 2
|
||||
#define CONNECTED 3
|
||||
#define START_FAIL 4
|
||||
|
||||
//ret_code LTE_config_init(void);
|
||||
|
||||
ret_code LTE_config_chk(uint source, uint *config_type,
|
||||
pointer input, int *input_len,
|
||||
|
|
|
@ -306,7 +306,7 @@
|
|||
{\
|
||||
LTE_CONFIG, \
|
||||
CONFIG_FROM_WEB|CONFIG_FROM_NETOPEER, \
|
||||
FALSE, \
|
||||
TRUE, \
|
||||
LTE_config_chk, \
|
||||
LTE_config_proc, \
|
||||
NULL, \
|
||||
|
|
Loading…
Reference in New Issue