You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
311 lines
9.8 KiB
311 lines
9.8 KiB
/*
|
|
* Copyright (c) Hisilicon Technologies Co., Ltd. 2020-2022. All rights reserved.
|
|
* Description: android ir options interface
|
|
* Author: App-Dev Group
|
|
* Create: 2020-07-02
|
|
*/
|
|
|
|
#include <string>
|
|
#include <unistd.h>
|
|
#include "tinyxml2.h"
|
|
#include "Options.h"
|
|
|
|
using namespace std;
|
|
using namespace android;
|
|
|
|
void Options::Usage(const char& base) const
|
|
{
|
|
printf("Usage: %s [-m 0|1] [-u 0|1] [-U num] [-r 0|1] [-R num] [-T readtimeout] [-E 0|1]"
|
|
" [-g protocolname] [-l] [-s protocolname -e 0|1] [-h]\n"
|
|
"\t-m : key fetch mode, 0 to read key parsed by driver, 1 to read symbols received.\n"
|
|
"\t-u : set keyup event, 0 to disable keyup event, 1 to enable keyup event.\n"
|
|
"\t-U : set keyup timeout, e.g. -U 300, driver will send a keyup event of last key parsed.\n"
|
|
"\t after 300ms if no follow key recevied. Not supported yet.\n"
|
|
"\t-r : set repeat key event report or not. 0 to disable repeat event, 1 to enable.\n"
|
|
"\t-R : set repeat key event interval time. Default 200ms.\n"
|
|
"\t e.g. -R 100 was set, then driver will report a key repeat event each 100ms.\n"
|
|
"\t-T : set read timeout value. e.g. -T 1000 was set, then read syscall will return after 1000 ms if no key "
|
|
"recevied.\n"
|
|
"\t-E : enable or disable ir module. 0 to disable, 1 to enable. If 1 is set, ir module will do nothing.\n"
|
|
"\t-g : get the enable/disable status of protocol which is specified by protocolname and then exit.\n"
|
|
"\t-l : list all default protocol name and exit. Only constains IR module, no customer's included!\n"
|
|
"\t-s : set the enable/disable status of protocol which is specified by protocolname, 0 to enable, 1 to "
|
|
"disable.\n"
|
|
"\t while set disable state, the respect protocol will not be considerd as a protocol parser,\n"
|
|
"\t and if some corresponding infrared symbol received, they are not parsed and may be discarded.\n"
|
|
"\t-e : 0 to enable, 1 to disable, must be used with -s\n"
|
|
"\t-d : simulate an automated infrared test.\n"
|
|
"\t-h : display this message and exit.\n"
|
|
"\tNOTES: Only [-s -e] option can be set more than once!\n",
|
|
&base);
|
|
}
|
|
|
|
int Options::CheckParamsSet() const
|
|
{
|
|
if (gMode == 0) {
|
|
if (gKeyupTimeoutSet != 0) {
|
|
printf("This version cannot support dynamicly change keyup timeout!\n");
|
|
}
|
|
if (gKeyupEventSet != 0 && androidIrInterface->IR_EnableKeyUp(gKeyupEvent > 0 ? TD_TRUE : TD_FALSE) != 0) {
|
|
perror("Fail to set keyup event!");
|
|
return TD_FAILURE;
|
|
} else if (gRepeatEventSet != 0 &&
|
|
androidIrInterface->IR_EnableRepKey(gRepeatEvent > 0 ? TD_TRUE : TD_FALSE) != 0) {
|
|
perror("Fail to set repeat key event!");
|
|
return TD_FAILURE;
|
|
} else if (gRepeatIntervalSet != 0 && androidIrInterface->IR_SetRepKeyTimeoutAttr(gRepeatInterval) != 0) {
|
|
perror("Fail to set repeat interval!");
|
|
return TD_FAILURE;
|
|
}
|
|
}
|
|
|
|
if (androidIrInterface->IR_SetFetchMode(gMode) != 0) {
|
|
perror("Fail to set key fetch gMode!");
|
|
return TD_FAILURE;
|
|
} else if (gIrEnableSet != 0 && androidIrInterface->IR_Enable(gIrEnable > 0 ? TD_TRUE : TD_FALSE) != 0) {
|
|
perror("Fail to set enable state of ir module!");
|
|
return TD_FAILURE;
|
|
}
|
|
return TD_SUCCESS;
|
|
}
|
|
|
|
int Options::CheckParams() const
|
|
{
|
|
if (gModeSet > 1 || (gMode != 0 && gMode != 1)) {
|
|
printf("fetch gMode error!\n");
|
|
return -1;
|
|
}
|
|
|
|
if (gKeyupEventSet > 1 || (gKeyupEvent != 0 && gKeyupEvent != 1)) {
|
|
printf("keyup event error!\n");
|
|
return -1;
|
|
}
|
|
if (gRepeatEventSet > 1 || gRepeatIntervalSet > 1) {
|
|
printf("Only -g [-s -e] option can be set more than once!\n");
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int Options::IrGetProtStatus(string prot) const
|
|
{
|
|
td_bool enabled = TD_FALSE;
|
|
|
|
if (prot.empty()) {
|
|
perror("prot is null!\n");
|
|
return -1;
|
|
}
|
|
|
|
int ret = androidIrInterface->IR_Init();
|
|
if (ret != 0) {
|
|
perror("Fail to open ir device!\n");
|
|
return -1;
|
|
}
|
|
|
|
ret = androidIrInterface->IR_GetProtocolEnabled(prot, enabled);
|
|
if (ret != 0) {
|
|
androidIrInterface->IR_DeInit();
|
|
printf("Fail to get protocol status!\n");
|
|
return -1;
|
|
}
|
|
|
|
androidIrInterface->IR_DeInit();
|
|
return enabled;
|
|
}
|
|
|
|
int Options::IrSampleSetProtEnable() const
|
|
{
|
|
if (gProtName.empty()) {
|
|
perror("gProtName is null!\n");
|
|
return -1;
|
|
}
|
|
|
|
int ret = androidIrInterface->IR_Init();
|
|
if (ret != 0) {
|
|
perror("Fail to open ir device!\n");
|
|
return -1;
|
|
}
|
|
|
|
printf("Try set enable status %d to protocol %s.\n", gProtEnable, gProtName.c_str());
|
|
if (gProtEnable != 0) {
|
|
ret = androidIrInterface->IR_EnableProtocol(gProtName);
|
|
} else {
|
|
ret = androidIrInterface->IR_DisableProtocol(gProtName);
|
|
}
|
|
if (ret != 0) {
|
|
printf("Fail to set enable status of %s\n", gProtName.c_str());
|
|
androidIrInterface->IR_DeInit();
|
|
return -1;
|
|
}
|
|
|
|
printf("Check it out if we do have set succussfully!\n");
|
|
if (gProtEnable == IrGetProtStatus(gProtName)) {
|
|
printf("Check pass!\n");
|
|
} else {
|
|
printf("Check fail!\n");
|
|
}
|
|
|
|
androidIrInterface->IR_DeInit();
|
|
return 0;
|
|
}
|
|
|
|
void Options::SetIrEnable()
|
|
{
|
|
gIrEnable = std::stoi(optarg);
|
|
if (gIrEnable != 0 && gIrEnable != 1) {
|
|
printf("-E parameter not correct, only 0 or 1 allowed!\n");
|
|
return;
|
|
}
|
|
gIrEnableSet = 1;
|
|
printf("Set ir mode to %s state!\n", gIrEnable == 0 ? "disable" : "enable");
|
|
}
|
|
|
|
int Options::GetProtStatus()
|
|
{
|
|
printf("try to get the enable status of %s\n", optarg);
|
|
gProtStatus = IrGetProtStatus(optarg);
|
|
if (gProtStatus >= 0) {
|
|
printf("The status of %s is :%s\n", optarg, gProtStatus == 0 ? "disabled" : "enabled");
|
|
} else {
|
|
printf("Fail to get protocol status!\n");
|
|
}
|
|
return TD_SUCCESS;
|
|
}
|
|
|
|
int Options::GetProtList() const
|
|
{
|
|
printf("Available default protocols list:\n");
|
|
for (int i = 0; i < static_cast<signed int>(sizeof(gProtNames) / sizeof(gProtNames[0])); i++) {
|
|
printf("%s\n", gProtNames[i].c_str());
|
|
}
|
|
return TD_SUCCESS;
|
|
}
|
|
|
|
int Options::SetProtEnable()
|
|
{
|
|
gProtName = optarg;
|
|
gProtNameSet = 1;
|
|
|
|
printf("gProtName :%s.\n", gProtName.c_str());
|
|
|
|
if (gProtEnableSet != 0 && gProtNameSet != 0) {
|
|
gProtEnableSet = 0;
|
|
gProtNameSet = 0;
|
|
int ret = IrSampleSetProtEnable();
|
|
if (ret != 0) {
|
|
perror("Fail to set gProtEnable!\n");
|
|
return TD_SUCCESS;
|
|
}
|
|
}
|
|
return TD_FAILURE;
|
|
}
|
|
|
|
int Options::SetProtE()
|
|
{
|
|
gProtEnable = std::stoi(optarg);
|
|
gProtEnableSet = 1;
|
|
|
|
printf("protocol enable status will set to :%d\n", gProtEnable);
|
|
|
|
if (gProtEnableSet != 0 && gProtNameSet != 0) {
|
|
gProtEnableSet = 0;
|
|
gProtNameSet = 0;
|
|
int ret = IrSampleSetProtEnable();
|
|
if (ret != 0) {
|
|
perror("Fail to set gProtEnable!\n");
|
|
return TD_SUCCESS;
|
|
}
|
|
}
|
|
return TD_FAILURE;
|
|
}
|
|
|
|
void Options::SetMode()
|
|
{
|
|
gMode = std::stoi(optarg);
|
|
gModeSet++;
|
|
printf("key fetch gMode set to :%d\n", gMode);
|
|
}
|
|
|
|
void Options::SetTimeOut()
|
|
{
|
|
gKeyupTimeout = strtoul(optarg, nullptr, 10); // 10, an integral number of the specified base
|
|
gKeyupTimeoutSet++;
|
|
printf("keyup timeout set to %u\n", gKeyupTimeout);
|
|
}
|
|
|
|
void Options::SetRepeatEvent()
|
|
{
|
|
gRepeatEvent = std::stoi(optarg);
|
|
gRepeatEventSet++;
|
|
printf("repeat event set to %d\n", gRepeatEvent);
|
|
}
|
|
|
|
void Options::SetRepeatInterval()
|
|
{
|
|
gRepeatInterval = strtoul(optarg, nullptr, 10); // 10, an integral number of the specified base
|
|
gRepeatIntervalSet++;
|
|
printf("repeat interval set to %u\n", gRepeatInterval);
|
|
}
|
|
|
|
int Options::ProcessOptionsDefault(int op, char *argv[])
|
|
{
|
|
switch (op) {
|
|
case 'g':
|
|
return GetProtStatus();
|
|
case 'l':
|
|
return GetProtList();
|
|
case 's':
|
|
return SetProtEnable();
|
|
case 'e':
|
|
return SetProtE();
|
|
case 'h':
|
|
default:
|
|
Usage(*argv[0]);
|
|
return TD_SUCCESS;
|
|
}
|
|
}
|
|
|
|
int Options::ProcessOptions(int argc, char *argv[])
|
|
{
|
|
int op;
|
|
opterr = 0;
|
|
while ((op = getopt(argc, argv, "m:u:U:r:R:T:E:g:ls:e:hD:d")) != -1) {
|
|
switch (op) {
|
|
case 'm':
|
|
SetMode();
|
|
break;
|
|
case 'u':
|
|
gKeyupEvent = std::stoi(optarg);
|
|
gKeyupEventSet++;
|
|
printf("keyup event set to %d\n", gKeyupEvent);
|
|
break;
|
|
case 'U':
|
|
SetTimeOut();
|
|
break;
|
|
case 'r':
|
|
SetRepeatEvent();
|
|
break;
|
|
case 'R':
|
|
SetRepeatInterval();
|
|
break;
|
|
case 'T':
|
|
gReadTimeout = strtoul(optarg, nullptr, 10); // 10, an integral number of the specified base
|
|
printf("read syscall timeout set to %u\n", gReadTimeout);
|
|
break;
|
|
case 'E':
|
|
SetIrEnable();
|
|
break;
|
|
case 'D':
|
|
gDebugEnable = 1;
|
|
break;
|
|
case 'd':
|
|
debugIrEnable = !debugIrEnable;
|
|
printf("ir automated test enable %d\n", debugIrEnable);
|
|
break;
|
|
default:
|
|
return ProcessOptionsDefault(op, argv);
|
|
}
|
|
}
|
|
return TD_FAILURE;
|
|
}
|