666 lines
14 KiB
C
666 lines
14 KiB
C
#include "configm.h"
|
||
#include "rpc.h"
|
||
#include "parsefile.h"
|
||
#include "LTE_config.h"
|
||
#include "static_routing.h"
|
||
|
||
int g_action = 0;
|
||
int g_status = STOPPED;
|
||
pthread_t lte;
|
||
|
||
/*******************************************
|
||
*函数功能:解析json,如果操作是set,需要继续
|
||
*解析“action”,如果是getall就直接返回
|
||
*
|
||
* {
|
||
* "operate":"set",
|
||
* "action":"start"
|
||
* }
|
||
*
|
||
* {
|
||
* "operate":"getall"
|
||
* }
|
||
*
|
||
*输入:
|
||
*输出:conf_type,action
|
||
*返回值:成功RET_OK,失败其他错误码
|
||
*********************************************/
|
||
ret_code LTE_json_parse(pointer input, uint *conf_type, int action)
|
||
{
|
||
ret_code ret = RET_OK;
|
||
if(NULL == input)
|
||
{
|
||
return RET_NULL_INPUT_ERR;
|
||
}
|
||
|
||
cJSON *json_obj = cJSON_Parse(input);
|
||
|
||
if(NULL == json_obj)
|
||
{
|
||
rpc_log_info("json obj is NULL \n");
|
||
return RET_EMPTY_STRING;
|
||
}
|
||
|
||
rpc_json_print(json_obj);
|
||
|
||
cJSON *operate = cJSON_GetObjectItem(json_obj,"operate");
|
||
if((NULL == operate) || (NULL == (operate->valuestring)))
|
||
{
|
||
cJSON_Delete(json_obj);
|
||
return RET_EMPTY_STRING;
|
||
}
|
||
|
||
rpc_log_info("operate is %s\n",operate->valuestring);
|
||
|
||
if(strcmp(operate->valuestring,"set") == 0)
|
||
{
|
||
*conf_type = CM_CONFIG_SET;
|
||
}
|
||
else if(strcmp(operate->valuestring,"getall") == 0)
|
||
{
|
||
*conf_type = CM_CONFIG_GET_ALL;
|
||
cJSON_Delete(json_obj);
|
||
return RET_OK;
|
||
}
|
||
else
|
||
{
|
||
cJSON_Delete(json_obj);
|
||
return RET_INPUTERR;
|
||
}
|
||
|
||
rpc_log_info("config type is %d\n",*conf_type);
|
||
cJSON *action_type;
|
||
action_type = cJSON_GetObjectItem(json_obj,"action");
|
||
if((NULL == action_type) || (NULL == (action_type->valuestring)))
|
||
{
|
||
cJSON_Delete(json_obj);
|
||
return RET_NULL_INPUT_ERR;
|
||
}
|
||
else
|
||
{
|
||
rpc_log_info("action is %s\n",action_type->valuestring);
|
||
if(strcmp(action_type->valuestring,"start") == 0)
|
||
{
|
||
g_action = START;
|
||
}
|
||
else if(strcmp(action_type->valuestring,"stop") == 0)
|
||
{
|
||
g_action = STOP;
|
||
}
|
||
else
|
||
{
|
||
cJSON_Delete(json_obj);
|
||
return RET_INPUTERR;
|
||
}
|
||
}
|
||
|
||
rpc_log_info("action type is %d\n",g_action);
|
||
cJSON_Delete(json_obj);
|
||
return ret;
|
||
}
|
||
|
||
/************************************
|
||
*函数功能:杀死进程
|
||
*输入:
|
||
*输出:
|
||
*返回值:成功1,失败0
|
||
*************************************/
|
||
int killpid()
|
||
{
|
||
char pid[10] =
|
||
{0};
|
||
char cmd[20] = {0};
|
||
int pid_int;
|
||
FILE *fp = NULL;
|
||
fp = popen("pgrep pppd","r");
|
||
if(NULL == fp)
|
||
{
|
||
rpc_log_info("can not get pid\n");
|
||
return 0;
|
||
}
|
||
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;
|
||
}
|
||
|
||
/************************************
|
||
*函数功能:判断4G是否开启成功
|
||
*输入:
|
||
*输出:
|
||
*返回值:成功1,失败0
|
||
*************************************/
|
||
|
||
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;
|
||
}
|
||
}
|
||
|
||
/************************************
|
||
*函数功能:恢复和ppp0口相关的静态路由
|
||
*输入:
|
||
*输出:
|
||
*返回值:
|
||
*************************************/
|
||
int recover_static_route()
|
||
{
|
||
FILE *f;
|
||
char buff[LINE_MAX_LENGTH] = {0};
|
||
char buff_total[LINE_MAX_LENGTH*ROUTING_TABLE_MAX_NUMBER] = {0};
|
||
int status;
|
||
|
||
f= fopen(STATIC_ROUTING_PATH,"r");
|
||
|
||
if(NULL == f)
|
||
{
|
||
rpc_log_error("open config file fail\n");
|
||
return RET_OPEN_FILE_ERR;
|
||
}
|
||
|
||
fseek(f,0,SEEK_SET);
|
||
|
||
while (fgets(buff,sizeof(buff),f) != NULL)
|
||
{
|
||
if(strlen(buff) < 3)
|
||
{
|
||
goto next_while;
|
||
}
|
||
|
||
if(strstr(buff,"ppp0") == NULL)
|
||
{
|
||
strcat(buff_total,buff);
|
||
goto next_while;
|
||
}
|
||
|
||
else
|
||
{
|
||
status = system(buff);
|
||
|
||
if (-1 == status)
|
||
{
|
||
rpc_log_error("system shell error!\n");
|
||
goto next_while;
|
||
}
|
||
else {
|
||
if (WIFEXITED(status)){
|
||
if (0 == WEXITSTATUS(status)){
|
||
rpc_log_info("run shell successfully!\n");
|
||
strcat(buff_total,buff);
|
||
goto next_while;
|
||
}
|
||
else{
|
||
rpc_log_error("run shell script fail,script exit code:%d\n",WEXITSTATUS(status));
|
||
goto next_while;
|
||
}
|
||
}
|
||
else {
|
||
rpc_log_error("exit status = [%d]\n",WEXITSTATUS(status));
|
||
goto next_while;
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
next_while:
|
||
if(fgetc(f) == EOF)
|
||
{
|
||
break;
|
||
}
|
||
|
||
fseek(f,-1,SEEK_CUR);
|
||
memset(buff,0,sizeof(buff));
|
||
}
|
||
fclose(f);
|
||
|
||
return conf_file_write(STATIC_ROUTING_PATH,buff_total);
|
||
}
|
||
|
||
/*******************************************************
|
||
*函数功能:新创建的线程,追踪4G状态。
|
||
*最大拨号时间设置为60s,拨号失败则初始化模块,重新拨号
|
||
*输入:
|
||
*输出:g_status
|
||
*返回值:
|
||
********************************************************/
|
||
void _start_lte(void)
|
||
{
|
||
char result[RESULT_LENGTH] = {0};
|
||
int wait_time_phase1 = 0;
|
||
int wait_time_phase2 = 0;
|
||
g_status = STARTING;
|
||
while(wait_time_phase1 < 60)
|
||
{
|
||
if(start_success())
|
||
{
|
||
g_status = CONNECTED;
|
||
rpc_log_info("connected\n");
|
||
sleep(1);
|
||
//执行各模块与ppp0有关的脚本
|
||
if(recover_static_route())
|
||
{
|
||
rpc_log_info("recover ppp0 route success");
|
||
}
|
||
return;
|
||
}
|
||
else
|
||
{
|
||
sleep(1);
|
||
wait_time_phase1 ++;
|
||
//rpc_log_info("time %d\n",wait_time_phase1);
|
||
}
|
||
}
|
||
|
||
if(killpid())
|
||
{
|
||
rpc_log_info("kill success\n");
|
||
}
|
||
|
||
rpc_log_info("start init\n");
|
||
system("/etc/ppp/ppp-4g init");
|
||
|
||
FILE *f = NULL;
|
||
f = popen("/etc/ppp/ppp-4g start","r");
|
||
|
||
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;
|
||
sleep(1);
|
||
//执行各模块与ppp0有关的脚本
|
||
if(recover_static_route())
|
||
{
|
||
rpc_log_info("recover ppp0 route success");
|
||
}
|
||
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;
|
||
}
|
||
|
||
/****************************************
|
||
*函数功能:判断开启4G的脚本是否执行成功
|
||
*输入:
|
||
*输出:
|
||
*返回值:成功RET_OK,失败错误码
|
||
******************************************/
|
||
ret_code start_lte()
|
||
{
|
||
if(g_status == STARTING)
|
||
{
|
||
return RET_ANOTHER_IN_PROCESS;
|
||
}
|
||
char result[RESULT_LENGTH] = {0};
|
||
int i = 0;
|
||
|
||
FILE *f = NULL;
|
||
|
||
f = popen("/etc/ppp/ppp-4g start","r");
|
||
|
||
if (NULL == f)
|
||
{
|
||
rpc_log_info("execuate cmd fail\n");
|
||
return RET_EXEC_SHELL_ERR;
|
||
}
|
||
|
||
fseek(f,0,SEEK_SET);
|
||
|
||
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 )
|
||
{
|
||
pclose(f);
|
||
return RET_NO_LTE_MODULE_ERR;
|
||
}
|
||
|
||
else if(strstr(result,"pppd apparently already active")
|
||
!= NULL)
|
||
{
|
||
pclose(f);
|
||
return RET_ALREADY_START_ERR;
|
||
}
|
||
else if(strstr(result,"start pppd ppp0") != NULL)
|
||
{
|
||
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;
|
||
}
|
||
else
|
||
{
|
||
pclose(f);
|
||
return RET_EXEC_SHELL_ERR;
|
||
}
|
||
}
|
||
|
||
|
||
/****************************************
|
||
*函数功能:判断关闭4G的脚本是否执行成功
|
||
*输入:
|
||
*输出:
|
||
*返回值:成功RET_OK,失败错误码
|
||
******************************************/
|
||
ret_code stop_lte()
|
||
{
|
||
if(g_status == STARTING)
|
||
{
|
||
return RET_ANOTHER_IN_PROCESS;
|
||
}
|
||
|
||
char result[RESULT_LENGTH] = {0};
|
||
|
||
FILE *f = NULL;
|
||
f = popen("/etc/ppp/ppp-4g stop","r");
|
||
|
||
if (NULL == f)
|
||
{
|
||
rpc_log_info("execuate cmd fail\n");
|
||
return RET_EXEC_SHELL_ERR;
|
||
}
|
||
|
||
fseek(f,0,SEEK_SET);
|
||
|
||
fgets(result,sizeof(result),f);
|
||
|
||
rpc_log_info("%s\n",result);
|
||
|
||
if(strstr(result,"pppd may be sotped already") != NULL)
|
||
{
|
||
g_status = STOPPED;
|
||
pclose(f);
|
||
return RET_ALREADY_STOP_ERR;
|
||
}
|
||
|
||
else if((strstr(result,"stop pppd ppp0") != NULL))
|
||
{
|
||
g_status = STOPPED;
|
||
rpc_log_info("%d\n",g_status);
|
||
pclose(f);
|
||
return RET_OK;
|
||
}
|
||
else
|
||
{
|
||
pclose(f);
|
||
return RET_EXEC_SHELL_ERR;
|
||
}
|
||
|
||
}
|
||
|
||
|
||
|
||
/****************************************
|
||
*函数功能:把查询的状态封装成json
|
||
*输入:status
|
||
*输出:output、output_len
|
||
*返回值:成功RET_OK,失败错误码
|
||
******************************************/
|
||
ret_code lte_to_json(char *status, pointer output, int *output_len)
|
||
{
|
||
if(NULL == status || NULL == output || NULL == output_len)
|
||
{
|
||
return RET_NULL_INPUT_ERR;
|
||
}
|
||
|
||
ret_code ret = RET_OK;
|
||
cJSON *ret_root = cJSON_CreateObject();
|
||
if(ret_root == NULL)
|
||
{
|
||
cJSON_Delete(ret_root);
|
||
return RET_EMPTY_STRING;
|
||
}
|
||
|
||
cJSON_AddStringToObject(ret_root,"status", status);
|
||
|
||
rpc_json_print(ret_root);
|
||
|
||
char *status_info = cJSON_PrintUnformatted(ret_root);
|
||
*output_len = strlen(status_info) + 1;
|
||
memcpy(output, status_info, *output_len);
|
||
|
||
free(status_info);
|
||
cJSON_Delete(ret_root);
|
||
|
||
return ret;
|
||
}
|
||
|
||
|
||
/*********************************************
|
||
*函数功能:判断查询4G状态的脚本是否执行成功
|
||
*输入:
|
||
*输出:
|
||
*返回值:成功RET_OK,失败错误码
|
||
**********************************************/
|
||
|
||
ret_code status_lte(pointer output, int *output_len)
|
||
{
|
||
if((NULL == output) || (NULL == output_len))
|
||
{
|
||
return RET_INPUTERR;
|
||
}
|
||
|
||
char result[RESULT_LENGTH] = {0};
|
||
char status[STATUS_LENGTH] = {0};
|
||
ret_code ret = RET_OK;
|
||
|
||
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,"off") != NULL)
|
||
{
|
||
rpc_log_info("status is %d\n",g_status);
|
||
if(g_status == STARTING)
|
||
{
|
||
strcpy(status,"starting...");
|
||
}
|
||
else if(g_status == STOPPED)
|
||
{
|
||
strcpy(status,"stopped");
|
||
}
|
||
else if(g_status == START_FAIL)
|
||
{
|
||
strcpy(status,"start fail");
|
||
}
|
||
}
|
||
else if(strstr(result,"on") != NULL)
|
||
{
|
||
strcpy(status,"started");
|
||
}
|
||
|
||
else
|
||
{
|
||
pclose(f);
|
||
return RET_ERR;
|
||
}
|
||
//封装json
|
||
ret = lte_to_json(status,output,output_len);
|
||
pclose(f);
|
||
return ret;
|
||
}
|
||
|
||
|
||
/****************************************
|
||
*函数功能:4G模块输入校验
|
||
*输入:
|
||
*输出:
|
||
*返回值:
|
||
******************************************/
|
||
ret_code LTE_config_chk(uint source, uint *config_type,
|
||
pointer input, int *input_len,
|
||
pointer output, int *output_len)
|
||
{
|
||
if((NULL == config_type) || (NULL == input) || (NULL == input_len) || (NULL == output_len))
|
||
{
|
||
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;
|
||
|
||
ret = LTE_json_parse(input,config_type,g_action);
|
||
|
||
return ret;
|
||
}
|
||
|
||
|
||
|
||
/*****************************************************
|
||
*函数功能:4G模块配置入口,根据config_type调用不同函数
|
||
*输入:
|
||
*输出:
|
||
*返回值:成功RET_OK,失败错误码
|
||
******************************************************/
|
||
ret_code LTE_config_proc(uint source, uint config_type,
|
||
pointer input, int input_len,
|
||
pointer output, int *output_len)
|
||
{
|
||
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 STOP:
|
||
ret = stop_lte();
|
||
break;
|
||
default:
|
||
ret = RET_INPUTERR;
|
||
break;
|
||
}
|
||
}
|
||
|
||
else
|
||
{
|
||
ret = RET_INPUTERR;
|
||
}
|
||
|
||
return ret;
|
||
}
|
||
|
||
|
||
/****************************************
|
||
*函数功能:4G模块get_all入口
|
||
*输入:
|
||
*输出:
|
||
*返回值:成功RET_OK,失败错误码
|
||
******************************************/
|
||
ret_code LTE_config_get_all(uint source,
|
||
pointer output, int *output_len)
|
||
{
|
||
if((NULL == output) || (NULL == output_len))
|
||
{
|
||
return RET_INPUTERR;
|
||
}
|
||
|
||
if (source == CONFIG_FROM_RECOVER1 || source == CONFIG_FROM_RECOVER2)
|
||
{
|
||
return RET_OK;
|
||
}
|
||
|
||
ret_code ret = RET_OK;
|
||
|
||
*output_len = 0;
|
||
ret = status_lte(output, output_len);
|
||
|
||
rpc_log_info("status is %s\n",(char *)output);
|
||
return ret;
|
||
|
||
}
|
||
|
||
|