irtest: use global arrays instead of __attributes__((section)) organization

N/A

fix __attribute__((section)) syntax issues on linux and mac
Change-Id: Ie8b5f52e552280bf3435b5bac03ffd8aed4d9e02
Signed-off-by: dongjiuzhu <dongjiuzhu1@xiaomi.com>
This commit is contained in:
dongjiuzhu 2020-11-27 22:09:21 +08:00 committed by Xiang Xiao
parent 1fcf43b544
commit b506600c5b
6 changed files with 316 additions and 392 deletions

View File

@ -1,5 +1,5 @@
/****************************************************************************
* testing/irtest/cmdLirc.cpp
* testing/irtest/cmd.cpp
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
@ -25,8 +25,12 @@
#include <nuttx/lirc.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/time.h>
#include <pthread.h>
#include <stdio.h>
#include <fcntl.h>
#include "enum.hpp"
#include "cmd.hpp"
/****************************************************************************
@ -39,16 +43,155 @@
* Private Data
****************************************************************************/
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
static int g_irdevs[CONFIG_TESTING_IRTEST_MAX_NIRDEV];
/****************************************************************************
* Public Functions
* Private Functions
****************************************************************************/
void init_device()
static void wake(int)
{
for (int i = 0; i < CONFIG_TESTING_IRTEST_MAX_NIRDEV; i++)
g_irdevs[i] = -1;
pthread_mutex_lock(&mutex);
pthread_cond_broadcast(&cond);
pthread_mutex_unlock(&mutex);
}
static void print_cmd(const cmd *cmd)
{
printf("%s(", cmd->name);
for (int i = 0; cmd->args[i].name; i++)
{
printf(i ? ", %s" : "%s", cmd->args[i].name);
}
printf(")\n");
}
static int print_cmd(const char *name)
{
for (int i = 0; g_cmd_table[i]; i++)
{
if (strcmp(name, g_cmd_table[i]->name) == 0)
{
print_cmd(g_cmd_table[i]);
return 0;
}
}
return -ENOENT;
}
static void print_all_cmds()
{
for (int i = 0; g_cmd_table[i]; i++)
{
print_cmd(g_cmd_table[i]);
}
}
static void print_enum(const enum_type *e)
{
printf("%s\n", e->type);
for (int i = 0; e->value[i].name; i++)
{
printf(e->fmt, e->value[i].value);
printf(" %s\n", e->value[i].name);
}
}
static int print_enum(const char *type)
{
for (int i = 0; g_enum_table[i]; i++)
{
if (strcmp(type, g_enum_table[i]->type) == 0)
{
print_enum(g_enum_table[i]);
return 0;
}
}
return -ENOENT;
}
static void print_all_enums()
{
for (int i = 0; g_enum_table[i]; i++)
{
print_enum(g_enum_table[i]);
}
}
static void print_cmd_and_enum(const cmd *cmd)
{
print_cmd(cmd);
for (int i = 0; cmd->args[i].type; i++)
{
print_enum(cmd->args[i].type);
}
}
static int print_cmd_and_enum(const char *name)
{
for (int i = 0; g_cmd_table[i]; i++)
{
if (strcmp(name, g_cmd_table[i]->name) == 0)
{
print_cmd_and_enum(g_cmd_table[i]);
return 0;
}
}
return -ENOENT;
}
CMD0(quit)
{
exit(0);
return 0;
}
CMD1(sleep, float, seconds)
{
int ret;
pthread_mutex_lock(&mutex);
signal(SIGINT, wake);
if (seconds)
{
struct timeval now;
struct timespec ts;
gettimeofday(&now, NULL);
ts.tv_sec = now.tv_sec + seconds;
ts.tv_nsec = now.tv_usec * 1000;
ret = pthread_cond_timedwait(&cond, &mutex, &ts);
}
else
{
ret = pthread_cond_wait(&cond, &mutex);
}
pthread_mutex_unlock(&mutex);
signal(SIGINT, SIG_DFL);
return ret == ETIMEDOUT ? 0 : ret;
}
CMD1(help, const char *, name)
{
int r = 0;
if (name != 0 && *name != 0)
{
r = print_cmd_and_enum(name);
}
else
{
print_all_enums();
print_all_cmds();
}
return r;
}
CMD1(open_device, const char *, file_name)
@ -375,3 +518,57 @@ CMD2(set_rec_carrier_range, size_t, index, unsigned int, carrier)
return ioctl(g_irdevs[index], LIRC_SET_REC_CARRIER_RANGE, &carrier);
}
/****************************************************************************
* Public Functions
****************************************************************************/
void init_device()
{
for (int i = 0; i < CONFIG_TESTING_IRTEST_MAX_NIRDEV; i++)
g_irdevs[i] = -1;
}
/****************************************************************************
* Public Data
****************************************************************************/
const struct cmd *g_cmd_table[] =
{
/* CMD0 */
&g_quit_cmd,
/* CMD1 */
&g_sleep_cmd,
&g_help_cmd,
&g_open_device_cmd,
&g_close_device_cmd,
&g_write_data_cmd,
&g_get_features_cmd,
&g_get_send_mode_cmd,
&g_get_rec_mode_cmd,
&g_get_rec_resolution_cmd,
&g_get_min_timeout_cmd,
&g_get_max_timeout_cmd,
&g_get_length_cmd,
/* CMD2 */
&g_read_data_cmd,
&g_set_send_mode_cmd,
&g_set_rec_mode_cmd,
&g_set_send_carrier_cmd,
&g_set_rec_carrier_cmd,
&g_set_send_duty_cycle_cmd,
&g_set_transmitter_mask_cmd,
&g_set_rec_timeout_cmd,
&g_set_rec_timeout_reports_cmd,
&g_set_measure_carrier_mode_cmd,
&g_set_rec_carrier_range_cmd,
/* CMD3 */
NULL,
};

View File

@ -29,6 +29,80 @@
#include <stdlib.h>
#include <string.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define CMD0(func) \
static int func(); \
static const arg g_##func##_args[] = \
{ \
{0, 0} \
}; \
static struct cmd g_##func##_cmd = \
{ \
#func, g_##func##_args, func \
}; \
static int func()
#define CMD1(func, type1, arg1) \
static int func(type1 arg1); \
static int func##_exec() \
{ \
type1 arg1 = get_next_arg<type1>(); \
return func(arg1); \
} \
static const arg g_##func##_args[] = \
{ \
{#type1, #arg1}, \
{0, 0} \
}; \
static struct cmd g_##func##_cmd = \
{ \
#func, g_##func##_args, func##_exec \
}; \
static int func(type1 arg1)
#define CMD2(func, type1, arg1, type2, arg2) \
static int func(type1 arg1, type2 arg2); \
static int func##_exec() \
{ \
type1 arg1 = get_next_arg<type1>(); \
type2 arg2 = get_next_arg<type2>(); \
return func(arg1, arg2); \
} \
static const arg g_##func##_args[] = { \
{#type1, #arg1}, \
{#type2, #arg2}, \
{0, 0} \
}; \
static struct cmd g_##func##_cmd = \
{ \
#func, g_##func##_args, func##_exec \
}; \
static int func(type1 arg1, type2 arg2)
#define CMD3(func, type1, arg1, type2, arg2, type3, arg3) \
static int func(type1 arg1, type2 arg2, type3 arg3); \
static int func##_exec() \
{ \
type1 arg1 = get_next_arg<type1>(); \
type2 arg2 = get_next_arg<type2>(); \
type3 arg3 = get_next_arg<type3>(); \
return func(arg1, arg2, arg3); \
} \
static const arg g_##func##_args[] = { \
{#type1, #arg1}, \
{#type2, #arg2}, \
{#type3, #arg3}, \
{0, 0} \
}; \
static struct cmd g_##func##_cmd = \
{ \
#func, func##_args, func##_exec \
}; \
static int func(type1 arg1, type2 arg2, type3 arg3)
/****************************************************************************
* Public Types
****************************************************************************/
@ -50,12 +124,7 @@ struct cmd
* Public Data
****************************************************************************/
/* Note: All commands put into cmds section, these two symbols
* are automatically defined by linker for the section boundary
*/
extern const cmd __start_cmds;
extern const cmd __stop_cmds;
extern const struct cmd *g_cmd_table[];
/****************************************************************************
* Inline Functions
@ -102,80 +171,6 @@ inline float get_next_arg()
return atof(get_next_arg < const char * > ());
}
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define CMD0(func) \
static int func(); \
static const arg func##_args[] = \
{ \
{0, 0} \
}; \
struct cmd func##_cmd __attribute__((section("cmds"))) = \
{ \
#func, func##_args, func \
}; \
static int func()
#define CMD1(func, type1, arg1) \
static int func(type1 arg1); \
static int func##_exec() \
{ \
type1 arg1 = get_next_arg<type1>(); \
return func(arg1); \
} \
static const arg func##_args[] = \
{ \
{#type1, #arg1}, \
{0, 0} \
}; \
struct cmd func##_cmd __attribute__((section("cmds"))) = \
{ \
#func, func##_args, func##_exec \
}; \
static int func(type1 arg1)
#define CMD2(func, type1, arg1, type2, arg2) \
static int func(type1 arg1, type2 arg2); \
static int func##_exec() \
{ \
type1 arg1 = get_next_arg<type1>(); \
type2 arg2 = get_next_arg<type2>(); \
return func(arg1, arg2); \
} \
static const arg func##_args[] = { \
{#type1, #arg1}, \
{#type2, #arg2}, \
{0, 0} \
}; \
struct cmd func##_cmd __attribute__((section("cmds"))) = \
{ \
#func, func##_args, func##_exec \
}; \
static int func(type1 arg1, type2 arg2)
#define CMD3(func, type1, arg1, type2, arg2, type3, arg3) \
static int func(type1 arg1, type2 arg2, type3 arg3); \
static int func##_exec() \
{ \
type1 arg1 = get_next_arg<type1>(); \
type2 arg2 = get_next_arg<type2>(); \
type3 arg3 = get_next_arg<type3>(); \
return func(arg1, arg2, arg3); \
} \
static const arg func##_args[] = { \
{#type1, #arg1}, \
{#type2, #arg2}, \
{#type3, #arg3}, \
{0, 0} \
}; \
struct cmd func##_cmd __attribute__((section("cmds"))) = \
{ \
#func, func##_args, func##_exec \
}; \
static int func(type1 arg1, type2 arg2, type3 arg3)
/****************************************************************************
* Public Function Prototypes
****************************************************************************/

View File

@ -1,194 +0,0 @@
/****************************************************************************
* testing/irtest/cmdGeneral.cpp
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <sys/ioctl.h>
#include <sys/time.h>
#include <pthread.h>
#include <stdio.h>
#include "enum.hpp"
#include "cmd.hpp"
/****************************************************************************
* Private Data
****************************************************************************/
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
/****************************************************************************
* Private Functions
****************************************************************************/
static void wake(int)
{
pthread_mutex_lock(&mutex);
pthread_cond_broadcast(&cond);
pthread_mutex_unlock(&mutex);
}
static void print_cmd(const cmd *cmd)
{
printf("%s(", cmd->name);
for (int i = 0; cmd->args[i].name; i++)
{
printf(i ? ", %s" : "%s", cmd->args[i].name);
}
printf(")\n");
}
static int print_cmd(const char *name)
{
const cmd *cmd = &__start_cmds;
for (; cmd < &__stop_cmds; cmd++)
{
if (strcmp(name, cmd->name) == 0)
{
print_cmd(cmd);
return 0;
}
}
return -ENOENT;
}
static void print_all_cmds()
{
const cmd *cmd = &__stop_cmds - 1;
for (; cmd >= &__start_cmds; cmd--)
{
print_cmd(cmd);
}
}
static void print_enum(const enum_type *e)
{
printf("%s\n", e->type);
for (int i = 0; e->value[i].name; i++)
{
printf(e->fmt, e->value[i].value);
printf(" %s\n", e->value[i].name);
}
}
static int print_enum(const char *type)
{
const enum_type *e = &__start_enums;
for (; e < &__stop_enums; e++)
{
if (strcmp(type, e->type) == 0)
{
print_enum(e);
return 0;
}
}
return -ENOENT;
}
static void print_all_enums()
{
const enum_type *e = &__stop_enums - 1;
for (; e >= &__start_enums; e--)
{
print_enum(e);
}
}
static void print_cmd_and_enum(const cmd *cmd)
{
print_cmd(cmd);
for (int i = 0; cmd->args[i].type; i++)
{
print_enum(cmd->args[i].type);
}
}
static int print_cmd_and_enum(const char *name)
{
const cmd *cmd = &__start_cmds;
for (; cmd < &__stop_cmds; cmd++)
{
if (strcmp(name, cmd->name) == 0)
{
print_cmd_and_enum(cmd);
return 0;
}
}
return -ENOENT;
}
/****************************************************************************
* Public Functions
****************************************************************************/
CMD0(quit)
{
exit(0);
return 0;
}
CMD1(sleep, float, seconds)
{
int ret;
pthread_mutex_lock(&mutex);
signal(SIGINT, wake);
if (seconds)
{
struct timeval now;
struct timespec ts;
gettimeofday(&now, NULL);
ts.tv_sec = now.tv_sec + seconds;
ts.tv_nsec = now.tv_usec * 1000;
ret = pthread_cond_timedwait(&cond, &mutex, &ts);
}
else
{
ret = pthread_cond_wait(&cond, &mutex);
}
pthread_mutex_unlock(&mutex);
signal(SIGINT, SIG_DFL);
return ret == ETIMEDOUT ? 0 : ret;
}
CMD1(help, const char *, name)
{
int r = 0;
if (name != 0 && *name != 0)
{
r = print_cmd_and_enum(name);
}
else
{
print_all_enums();
print_all_cmds();
}
return r;
}

View File

@ -1,5 +1,5 @@
/****************************************************************************
* testing/irtest/enumDefine.cpp
* testing/irtest/enum.cpp
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
@ -24,9 +24,10 @@
#include "enum.hpp"
#include <nuttx/lirc.h>
#include <stdio.h>
/****************************************************************************
* Public Data
* Private Data
****************************************************************************/
ENUM_START(mode_t)
@ -63,6 +64,17 @@ ENUM_START(features_t)
ENUM_VALUE(LIRC_CAN_NOTIFY_DECODE)
ENUM_END(features_t, "0x%08x")
/****************************************************************************
* Public Data
****************************************************************************/
const struct enum_type *g_enum_table[] =
{
&g_mode_t_type,
&g_features_t_type,
NULL,
};
/****************************************************************************
* Public Functions
****************************************************************************/

View File

@ -25,6 +25,27 @@
* Included Files
****************************************************************************/
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* macro to define the enum object */
#define ENUM_START(type) \
static const enum_value g_##type##_value[] = \
{
#define ENUM_VALUE(value) \
{#value, value},
#define ENUM_END(type, fmt) \
{0, 0} \
}; \
static struct enum_type g_##type##_type = \
{ \
#type, fmt, g_##type##_value \
}; \
/****************************************************************************
* Public Types
****************************************************************************/
@ -46,32 +67,6 @@ struct enum_type
* Public Data
****************************************************************************/
/* Note: All enum type put into enums section, these two symbols
* are automatically defined by linker for the section boundary
*/
extern const enum_type __start_enums;
extern const enum_type __stop_enums;
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* macro to define the enum object */
#define ENUM_START(type) \
static const enum_value type##_value[] = \
{
#define ENUM_VALUE(value) \
{#value, value},
#define ENUM_END(type, fmt) \
{0, 0} \
}; \
struct enum_type type##_type __attribute__((section("enums"))) = \
{ \
#type, fmt, type##_value \
}; \
extern const struct enum_type *g_enum_table[];
#endif /* __APPS_TESTING_IRTEST_ENUM_H */

View File

@ -26,86 +26,6 @@
#include "system/readline.h"
#include "cmd.hpp"
#include "enum.hpp"
/****************************************************************************
* Public Data
****************************************************************************/
extern cmd *quit_cmd;
extern cmd *sleep_cmd;
extern cmd *help_cmd;
extern cmd *open_device_cmd;
extern cmd *close_device_cmd;
extern cmd *write_data_cmd;
extern cmd *get_features_cmd;
extern cmd *get_send_mode_cmd;
extern cmd *get_rec_mode_cmd;
extern cmd *get_rec_resolution_cmd;
extern cmd *get_min_timeout_cmd;
extern cmd *get_max_timeout_cmd;
extern cmd *get_length_cmd;
extern cmd *read_data_cmd;
extern cmd *set_send_mode_cmd;
extern cmd *set_rec_mode_cmd;
extern cmd *set_send_carrier_cmd;
extern cmd *set_rec_carrier_cmd;
extern cmd *set_send_duty_cycle_cmd;
extern cmd *set_transmitter_mask_cmd;
extern cmd *set_rec_timeout_cmd;
extern cmd *set_rec_timeout_reports_cmd;
extern cmd *set_measure_carrier_mode_cmd;
extern cmd *set_rec_carrier_range_cmd;
extern enum_type *mode_t_type;
extern enum_type *features_t_type;
/****************************************************************************
* Private Data
****************************************************************************/
static const struct enum_type *g_enum_table[] =
{
mode_t_type,
features_t_type,
};
static const struct cmd *g_cmd_table[] =
{
/* CMD0 */
quit_cmd,
/* CMD1 */
sleep_cmd,
help_cmd,
open_device_cmd,
close_device_cmd,
write_data_cmd,
get_features_cmd,
get_send_mode_cmd,
get_rec_mode_cmd,
get_rec_resolution_cmd,
get_min_timeout_cmd,
get_max_timeout_cmd,
get_length_cmd,
/* CMD2 */
read_data_cmd,
set_send_mode_cmd,
set_rec_mode_cmd,
set_send_carrier_cmd,
set_rec_carrier_cmd,
set_send_duty_cycle_cmd,
set_transmitter_mask_cmd,
set_rec_timeout_cmd,
set_rec_timeout_reports_cmd,
set_measure_carrier_mode_cmd,
set_rec_carrier_range_cmd,
/* CMD3 */
};
/****************************************************************************
* Private Functions
@ -136,12 +56,11 @@ int main(int argc, char *argv[])
if (name != 0 && *name != 0)
{
int res = -ENOSYS;
const cmd *cmd = &__start_cmds;
for (; cmd < &__stop_cmds; cmd++)
for (int i = 0; g_cmd_table[i]; i++)
{
if (strcmp(name, cmd->name) == 0)
if (strcmp(name, g_cmd_table[i]->name) == 0)
{
res = cmd->exec();
res = g_cmd_table[i]->exec();
break;
}
}