ParamterParsing.c 3.9 KB
#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
#include <unistd.h>   
#include <ctype.h>
#include <getopt.h>

#include "JZsdkLib.h"
#include "./ParamterParsing.h"

/*
struct option {
        const char  *name;     参数名称
        int has_arg;               指明是否带有参数         
        int *flag;                 flag=NULL时,返回value;不为空时,*flag=val,返回0 
        int val;                   用于指定函数找到选项的返回值或flag非空时指定*flag的值
};

no_argument	不带参数	如: --version,--help
required_argument	必须带参数	--参数 值 或者 --参数=值。如:--dir=/root
optional_argument	参数可选	只能为:--参数=值

char *optstring="ab:c::";

运行程序可以给最多三个参数,分别为-a,-b和-c,其中

a选项 :因为没有跟冒号,不能有参数,如:-a

b选项 :因为后面跟了1个冒号,必须有参数,如:-b 100

c选项 :因为跟了2个冒号,参数可有可无,如:-c 100 或 -c 都行

*/

typedef enum parameter{
	PARAMETER_OUT_TIMEFLAG = 0x1010,            //备用超时标志位
	PARAMETER_OUT_TIMEOUT  = 0x1011,            //备用超时时间
    PARAMETER_PLAY_PROMPT_TTS = 0x1012,         //播放提示音
}parameter;

static const struct option storage_opts[] = {
	{ "OutFlag", required_argument, 0, PARAMETER_OUT_TIMEFLAG },  //超时标志
	{ "OutTime", required_argument, 0, PARAMETER_OUT_TIMEOUT },  //超时时间

	{ "play_prompt_tts", required_argument, NULL, PARAMETER_PLAY_PROMPT_TTS }, //提示选项
	{ 0, 0, 0, 0 }
};
 

static int Timeout_flag = JZ_FLAGCODE_OFF; //超时标记位,默认关闭
static int Timeout_time = 60; //超时时间
unsigned char *g_JZsdkParam_PlayPromptTTS = NULL; //播放提示音

int JZsdk_ParamterParsing_GetTimeOutFlag()
{
    return Timeout_flag;
}

int JZsdk_ParamterParsing_GetTimeOutTime()
{
    return Timeout_time;
}

/*************************************
 * 
 *    *  程序参数解析
 *    *  输入:参数字符串
 *       输出:模式 
 *        目前 模式0 为不开启任何单独功能,即psdk或者串口模式
 *        模式1 为开起喊话器播放模式
 * 
 * ********************************/
T_JZsdkReturnCode JZsdk_ParamterParsing(int argc, char *argv[], unsigned int *mode)
{
    JZSDK_LOG_INFO("参数解析");

    int insCode = 0; //指令码

    //获取短选项
    static char *opt_storage = "t:f:"; 

    //解析参数
    while (1) 
    {
		int opt_index = 0;
		insCode = getopt_long(argc, argv, opt_storage, storage_opts, &opt_index);
		if (insCode == -1)
			break;
 
		switch (insCode) 
        {
    		case PARAMETER_OUT_TIMEFLAG:
                printf("备用:outtimeflag:%d \n",  atoi(optarg));
                Timeout_flag = atoi(optarg); 
    			break;
    		case PARAMETER_OUT_TIMEOUT:
    			printf("备用:time %d \n", atoi(optarg));
                Timeout_time = atoi(optarg); 
    			break;

            //提示音选项
    		case PARAMETER_PLAY_PROMPT_TTS:   
    			printf("prompt_option:%s \n", optarg);
                g_JZsdkParam_PlayPromptTTS = optarg;
                *mode = 1;
    		break;

            case 'f':  //超时标记位
                Timeout_flag = atoi(optarg); 
                break;  
            case 't':  //超时时间
                Timeout_time = atoi(optarg); 
                break;  

    		case '?':  
                // 如果getopt遇到一个它不认识的选项,它会打印一个消息到stderr  
                // 并返回'?'。然后我们可以选择退出或给出更多帮助。  
                if (isprint(optopt))  
                    JZSDK_LOG_ERROR("Unknown option `-%c'.", optopt);  
                else  
                    JZSDK_LOG_ERROR("Unknown option character `\\x%x'.", optopt);  
                    return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE; 
                break;
 
    		default:
    			break;
		}
	}


    JZSDK_LOG_INFO("参数解析成功");

    return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}