testing: add monkey test

This commit is contained in:
pengyiqiang 2022-08-16 00:46:53 +08:00 committed by Xiang Xiao
parent e1a382d62a
commit 2ed3a91426
17 changed files with 2722 additions and 0 deletions

84
testing/monkey/Kconfig Normal file
View File

@ -0,0 +1,84 @@
#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#
menuconfig TESTING_MONKEY
tristate "Monkey test"
default n
if TESTING_MONKEY
config TESTING_MONKEY_PRIORITY
int "Task priority"
default 110
config TESTING_MONKEY_STACKSIZE
int "Stack size"
default 16384
config TESTING_MONKEY_LOG_ENABLE
bool "Enable log output"
default y
config TESTING_MONKEY_LOG_LEVEL_DEFAULT
int "Log level default"
range 0 3
default 1
depends on TESTING_MONKEY_LOG_ENABLE
---help---
0 - INFO
1 - NOTICE
2 - WARN
3 - ERROR
config TESTING_MONKEY_REC_DIR_PATH
string "Recorder directory path"
default "/data/monkey"
config TESTING_MONKEY_PERIOD_MIN_DEFAULT
int "Default test period[ms] min"
default 10
config TESTING_MONKEY_PERIOD_MAX_DEFAULT
int "Default test period[ms] max"
default 100
config TESTING_MONKEY_SCREEN_HOR_RES
int "Screen horizontal resolution"
default 480
config TESTING_MONKEY_SCREEN_VER_RES
int "Screen vertical resolution"
default 480
config TESTING_MONKEY_SCREEN_IS_ROUND
bool "Round screen"
default n
config TESTING_MONKEY_BUTTON_NUM
int "Button test number"
range 0 32
default 1
config TESTING_MONKEY_BUTTON_CLICK_TIME
int "Button click hold time[ms]"
default 100
config TESTING_MONKEY_DEV_PATH_TOUCH
string "input device path - touch"
default "/dev/input0"
config TESTING_MONKEY_DEV_PATH_BUTTON
string "input device path - button"
default "/dev/buttons"
config TESTING_MONKEY_DEV_PATH_UTOUCH
string "uinput device path - utouch"
default "/dev/utouch"
config TESTING_MONKEY_DEV_PATH_UBUTTON
string "uinput device path - ubutton"
default "/dev/ubutton"
endif

23
testing/monkey/Make.defs Normal file
View File

@ -0,0 +1,23 @@
############################################################################
# apps/testing/monkey/Make.defs
#
# 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.
#
############################################################################
ifneq ($(CONFIG_TESTING_MONKEY),)
CONFIGURED_APPS += $(APPDIR)/testing/monkey
endif

34
testing/monkey/Makefile Normal file
View File

@ -0,0 +1,34 @@
############################################################################
# apps/testing/monkey/Makefile
#
# 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.
#
############################################################################
include $(APPDIR)/Make.defs
# Monkey test example
PROGNAME = monkey
PRIORITY = $(CONFIG_TESTING_MONKEY_PRIORITY)
STACKSIZE = $(CONFIG_TESTING_MONKEY_STACKSIZE)
MODULE = $(CONFIG_TESTING_MONKEY)
MAINSRC = monkey_main.c
CSRCS += $(filter-out monkey_main.c, $(wildcard *.c))
include $(APPDIR)/Application.mk

196
testing/monkey/monkey.c Normal file
View File

@ -0,0 +1,196 @@
/****************************************************************************
* apps/testing/monkey/monkey.c
*
* 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 <nuttx/config.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include "monkey.h"
#include "monkey_recorder.h"
#include "monkey_assert.h"
#include "monkey_log.h"
#include "monkey_dev.h"
#include "monkey_utils.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define MONKEY_DEV_CREATE_MATCH(monkey, type_mask, type) \
do { \
if ((type_mask & MONKEY_DEV_TYPE_##type) == MONKEY_DEV_TYPE_##type) \
{ \
FAR struct monkey_dev_s *dev; \
dev = monkey_dev_create(CONFIG_TESTING_MONKEY_DEV_PATH_##type, \
MONKEY_DEV_TYPE_##type); \
if (!dev) \
{ \
goto failed; \
} \
monkey->devs[monkey->dev_num] = dev; \
monkey->dev_num++; \
} \
} while (0)
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: monkey_create
****************************************************************************/
FAR struct monkey_s *monkey_create(int dev_type_mask)
{
FAR struct monkey_s *monkey = malloc(sizeof(struct monkey_s));
MONKEY_ASSERT_NULL(monkey);
memset(monkey, 0, sizeof(struct monkey_s));
if (MONKEY_IS_UINPUT_TYPE(dev_type_mask))
{
MONKEY_DEV_CREATE_MATCH(monkey, dev_type_mask, UTOUCH);
MONKEY_DEV_CREATE_MATCH(monkey, dev_type_mask, UBUTTON);
}
else
{
MONKEY_DEV_CREATE_MATCH(monkey, dev_type_mask, TOUCH);
MONKEY_DEV_CREATE_MATCH(monkey, dev_type_mask, BUTTON);
}
if (monkey->dev_num == 0)
{
MONKEY_LOG_ERROR("NO enabled device detected");
goto failed;
}
MONKEY_ASSERT(monkey->dev_num <= MONKEY_DEV_MAX_NUM);
MONKEY_LOG_NOTICE("OK");
return monkey;
failed:
monkey_delete(monkey);
return NULL;
}
/****************************************************************************
* Name: monkey_delete
****************************************************************************/
void monkey_delete(FAR struct monkey_s *monkey)
{
int i;
MONKEY_ASSERT_NULL(monkey);
for (i = 0; i < monkey->dev_num; i++)
{
monkey_dev_delete(monkey->devs[i]);
}
if (monkey->recorder)
{
monkey_recorder_delete(monkey->recorder);
monkey->recorder = NULL;
}
free(monkey);
MONKEY_LOG_NOTICE("OK");
}
/****************************************************************************
* Name: monkey_config_default_init
****************************************************************************/
void monkey_config_default_init(FAR struct monkey_config_s *config)
{
MONKEY_ASSERT_NULL(config);
memset(config, 0, sizeof(struct monkey_config_s));
config->screen.type = MONKEY_SCREEN_TYPE_RECT;
config->screen.hor_res = CONFIG_TESTING_MONKEY_SCREEN_HOR_RES;
config->screen.ver_res = CONFIG_TESTING_MONKEY_SCREEN_VER_RES;
config->period.min = CONFIG_TESTING_MONKEY_PERIOD_MIN_DEFAULT;
config->period.max = CONFIG_TESTING_MONKEY_PERIOD_MAX_DEFAULT;
}
/****************************************************************************
* Name: monkey_set_config
****************************************************************************/
void monkey_set_config(FAR struct monkey_s *monkey,
FAR const struct monkey_config_s *config)
{
MONKEY_ASSERT_NULL(monkey);
MONKEY_ASSERT_NULL(config);
monkey->config = *config;
}
/****************************************************************************
* Name: monkey_set_mode
****************************************************************************/
void monkey_set_mode(FAR struct monkey_s *monkey,
enum monkey_mode_e mode)
{
MONKEY_ASSERT_NULL(monkey);
monkey->mode = mode;
}
/****************************************************************************
* Name: monkey_set_period
****************************************************************************/
void monkey_set_period(FAR struct monkey_s *monkey, uint32_t period)
{
MONKEY_ASSERT_NULL(monkey);
monkey->config.period.min = period;
monkey->config.period.max = period;
}
/****************************************************************************
* Name: monkey_set_recorder_path
****************************************************************************/
bool monkey_set_recorder_path(FAR struct monkey_s *monkey,
FAR const char *path)
{
MONKEY_ASSERT_NULL(monkey);
if (monkey->mode == MONKEY_MODE_RECORD)
{
monkey->recorder = monkey_recorder_create(path,
MONKEY_RECORDER_MODE_RECORD);
}
else if (monkey->mode == MONKEY_MODE_PLAYBACK)
{
monkey->recorder = monkey_recorder_create(path,
MONKEY_RECORDER_MODE_PLAYBACK);
}
else
{
MONKEY_LOG_WARN("mismatched mode: %d", monkey->mode);
}
return (monkey->recorder != NULL);
}

98
testing/monkey/monkey.h Normal file
View File

@ -0,0 +1,98 @@
/****************************************************************************
* apps/testing/monkey/monkey.h
*
* 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.
*
****************************************************************************/
#ifndef __MONKEY_H__
#define __MONKEY_H__
/****************************************************************************
* Included Files
****************************************************************************/
#include <stdint.h>
#include "monkey_type.h"
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
#ifdef __cplusplus
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
/****************************************************************************
* Name: monkey_create
****************************************************************************/
FAR struct monkey_s *monkey_create(int dev_type_mask);
/****************************************************************************
* Name: monkey_update
****************************************************************************/
int monkey_update(FAR struct monkey_s *monkey);
/****************************************************************************
* Name: monkey_delete
****************************************************************************/
void monkey_delete(FAR struct monkey_s *monkey);
/****************************************************************************
* Name: monkey_config_default_init
****************************************************************************/
void monkey_config_default_init(FAR struct monkey_config_s *config);
/****************************************************************************
* Name: monkey_set_config
****************************************************************************/
void monkey_set_config(FAR struct monkey_s *monkey,
FAR const struct monkey_config_s *config);
/****************************************************************************
* Name: monkey_set_mode
****************************************************************************/
void monkey_set_mode(FAR struct monkey_s *monkey, enum monkey_mode_e mode);
/****************************************************************************
* Name: monkey_set_period
****************************************************************************/
void monkey_set_period(FAR struct monkey_s *monkey, uint32_t period);
/****************************************************************************
* Name: monkey_set_recorder_path
****************************************************************************/
bool monkey_set_recorder_path(FAR struct monkey_s *monkey,
FAR const char *path);
#undef EXTERN
#ifdef __cplusplus
}
#endif
#endif /* __MONKEY_H__ */

View File

@ -0,0 +1,37 @@
/****************************************************************************
* apps/testing/monkey/monkey_assert.h
*
* 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.
*
****************************************************************************/
#ifndef __MONKEY_ASSERT_H__
#define __MONKEY_ASSERT_H__
/****************************************************************************
* Included Files
****************************************************************************/
#include <assert.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define MONKEY_ASSERT(expr) DEBUGASSERT(expr)
#define MONKEY_ASSERT_NULL(ptr) MONKEY_ASSERT(ptr != NULL)
#endif /* __MONKEY_ASSERT_H__ */

327
testing/monkey/monkey_dev.c Normal file
View File

@ -0,0 +1,327 @@
/****************************************************************************
* apps/testing/monkey/monkey_dev.c
*
* 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 <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <nuttx/input/buttons.h>
#include <nuttx/input/touchscreen.h>
#include <poll.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "monkey_assert.h"
#include "monkey_log.h"
#include "monkey_dev.h"
#include "monkey_utils.h"
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: utouch_write
****************************************************************************/
static void utouch_write(int fd, int x, int y, int touch_down)
{
struct touch_sample_s sample;
if (touch_down)
{
sample.point[0].x = x;
sample.point[0].y = y;
sample.point[0].pressure = 42;
sample.point[0].flags = TOUCH_DOWN | TOUCH_ID_VALID |
TOUCH_POS_VALID | TOUCH_PRESSURE_VALID;
}
else
{
sample.point[0].flags = TOUCH_UP | TOUCH_ID_VALID;
}
sample.npoints = 1;
sample.point[0].h = 1;
sample.point[0].w = 1;
write(fd, &sample, sizeof(struct touch_sample_s));
MONKEY_LOG_INFO("%s at x = %d, y = %d",
touch_down ? "PRESS " : "RELEASE", x, y);
}
/****************************************************************************
* Name: ubutton_write
****************************************************************************/
static void ubutton_write(int fd, uint32_t btn_bits)
{
btn_buttonset_t buttonset = btn_bits;
write(fd, &buttonset, sizeof(buttonset));
MONKEY_LOG_INFO("btn = 0x%08X", btn_bits);
}
/****************************************************************************
* Name: touch_read
****************************************************************************/
static bool touch_read(int fd, FAR int *x, FAR int *y, FAR int *touch_down)
{
bool retval = false;
struct touch_sample_s sample;
int nbytes = read(fd, &sample, sizeof(sample));
if (nbytes == sizeof(sample))
{
retval = true;
uint8_t touch_flags = sample.point[0].flags;
if (touch_flags & TOUCH_DOWN || touch_flags & TOUCH_MOVE)
{
*x = sample.point[0].x;
*y = sample.point[0].y;
*touch_down = true;
}
else if (touch_flags & TOUCH_UP)
{
*x = 0;
*y = 0;
*touch_down = false;
}
}
return retval;
}
/****************************************************************************
* Name: button_read
****************************************************************************/
static bool button_read(int fd, FAR uint32_t *value)
{
btn_buttonset_t buttonset;
int ret = read(fd, &buttonset, sizeof(buttonset));
if (ret < 0)
{
return false;
}
*value = buttonset;
return true;
}
/****************************************************************************
* Name: monkey_dev_create
****************************************************************************/
FAR struct monkey_dev_s *monkey_dev_create(FAR const char *dev_path,
enum monkey_dev_type_e type)
{
FAR struct monkey_dev_s *dev;
int oflag;
int fd;
MONKEY_ASSERT_NULL(dev_path);
dev = malloc(sizeof(struct monkey_dev_s));
MONKEY_ASSERT_NULL(dev);
memset(dev, 0, sizeof(struct monkey_dev_s));
if (MONKEY_IS_UINPUT_TYPE(type))
{
oflag = O_RDWR | O_NONBLOCK;
}
else
{
oflag = O_RDONLY | O_NONBLOCK;
}
fd = open(dev_path, oflag);
if (fd < 0)
{
MONKEY_LOG_ERROR("open %s failed: %d", dev_path, errno);
goto failed;
}
dev->type = type;
dev->fd = fd;
MONKEY_LOG_NOTICE("open %s success, fd = %d, type: %s",
dev_path, fd, monkey_dev_type2name(type));
return dev;
failed:
if (fd > 0)
{
close(fd);
}
if (dev)
{
free(dev);
}
return NULL;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: monkey_dev_delete
****************************************************************************/
void monkey_dev_delete(FAR struct monkey_dev_s *dev)
{
MONKEY_ASSERT_NULL(dev);
if (dev->fd > 0)
{
/* Reset input state */
struct monkey_dev_state_s state;
memset(&state, 0, sizeof(state));
state.type = dev->type;
monkey_dev_set_state(dev, &state);
MONKEY_LOG_NOTICE("close fd: %d", dev->fd);
close(dev->fd);
dev->fd = -1;
}
free(dev);
}
/****************************************************************************
* Name: monkey_dev_set_state
****************************************************************************/
void monkey_dev_set_state(FAR struct monkey_dev_s *dev,
FAR const struct monkey_dev_state_s *state)
{
MONKEY_ASSERT_NULL(dev);
switch (MONKEY_GET_DEV_TYPE(dev->type))
{
case MONKEY_DEV_TYPE_TOUCH:
utouch_write(dev->fd,
state->data.touch.x,
state->data.touch.y,
state->data.touch.is_pressed);
break;
case MONKEY_DEV_TYPE_BUTTON:
ubutton_write(dev->fd, state->data.button.value);
break;
default:
break;
}
}
/****************************************************************************
* Name: monkey_dev_get_state
****************************************************************************/
bool monkey_dev_get_state(FAR struct monkey_dev_s *dev,
FAR struct monkey_dev_state_s *state)
{
bool retval;
MONKEY_ASSERT_NULL(dev);
retval = false;
state->type = dev->type;
switch (dev->type)
{
case MONKEY_DEV_TYPE_TOUCH:
retval = touch_read(dev->fd,
&state->data.touch.x,
&state->data.touch.y,
&state->data.touch.is_pressed);
break;
case MONKEY_DEV_TYPE_BUTTON:
retval = button_read(dev->fd, &state->data.button.value);
break;
default:
break;
}
return retval;
}
/****************************************************************************
* Name: monkey_dev_get_state
****************************************************************************/
enum monkey_dev_type_e monkey_dev_get_type(
FAR struct monkey_dev_s *dev)
{
MONKEY_ASSERT_NULL(dev);
return dev->type;
}
/****************************************************************************
* Name: monkey_dev_get_available
****************************************************************************/
int monkey_dev_get_available(FAR struct monkey_dev_s *devs[], int dev_num)
{
int i;
int available = 0;
struct pollfd fds[MONKEY_DEV_MAX_NUM];
for (i = 0; i < dev_num; i++)
{
devs[i]->is_available = false;
fds[i].fd = devs[i]->fd;
fds[i].events = POLLIN | POLLPRI;
fds[i].revents = 0;
}
if (poll(fds, dev_num, -1) < 0)
{
MONKEY_LOG_WARN("stop: %d", errno);
return -1;
}
for (i = 0; i < dev_num; i++)
{
if (fds[i].revents & (POLLIN | POLLPRI))
{
devs[i]->is_available = true;
available++;
}
}
return available;
}

View File

@ -0,0 +1,97 @@
/****************************************************************************
* apps/testing/monkey/monkey_dev.h
*
* 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.
*
****************************************************************************/
#ifndef __MONKEY_DEV_H__
#define __MONKEY_DEV_H__
/****************************************************************************
* Included Files
****************************************************************************/
#include "monkey_type.h"
/****************************************************************************
* Public Types
****************************************************************************/
struct monkey_dev_s
{
int fd;
enum monkey_dev_type_e type;
bool is_available;
};
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
#ifdef __cplusplus
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
/****************************************************************************
* Name: monkey_dev_create
****************************************************************************/
FAR struct monkey_dev_s *monkey_dev_create(FAR const char *dev_path,
enum monkey_dev_type_e type);
/****************************************************************************
* Name: monkey_dev_delete
****************************************************************************/
void monkey_dev_delete(FAR struct monkey_dev_s *dev);
/****************************************************************************
* Name: monkey_dev_set_state
****************************************************************************/
void monkey_dev_set_state(FAR struct monkey_dev_s *dev,
FAR const struct monkey_dev_state_s *state);
/****************************************************************************
* Name: monkey_dev_get_state
****************************************************************************/
bool monkey_dev_get_state(FAR struct monkey_dev_s *dev,
FAR struct monkey_dev_state_s *state);
/****************************************************************************
* Name: monkey_dev_get_type
****************************************************************************/
enum monkey_dev_type_e monkey_dev_get_type(FAR struct monkey_dev_s *dev);
/****************************************************************************
* Name: monkey_dev_get_available
****************************************************************************/
int monkey_dev_get_available(FAR struct monkey_dev_s *devs[], int dev_num);
#undef EXTERN
#ifdef __cplusplus
}
#endif
#endif /* __MONKEY_DEV_H__ */

View File

@ -0,0 +1,99 @@
/****************************************************************************
* apps/testing/monkey/monkey_log.c
*
* 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 <stdarg.h>
#include <stdio.h>
#include <syslog.h>
#include "monkey_log.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#ifdef CONFIG_TESTING_MONKEY_LOG_ENABLE
/****************************************************************************
* Private Data
****************************************************************************/
static enum monkey_log_level_type_e g_log_level =
CONFIG_TESTING_MONKEY_LOG_LEVEL_DEFAULT;
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: monkey_log_printf
****************************************************************************/
void monkey_log_printf(enum monkey_log_level_type_e level,
FAR const char *func,
FAR const char *fmt,
...)
{
struct va_format vaf;
va_list ap;
static const int priority[_MONKEY_LOG_LEVEL_LAST] = {
LOG_INFO, LOG_NOTICE, LOG_WARNING, LOG_ERR
};
if (level < g_log_level)
{
return;
}
va_start(ap, fmt);
vaf.fmt = fmt;
vaf.va = &ap;
syslog(priority[level], "[monkey] %s: %pV\n", func, &vaf);
va_end(ap);
}
/****************************************************************************
* Name: monkey_log_set_level
****************************************************************************/
void monkey_log_set_level(enum monkey_log_level_type_e level)
{
if (level >= _MONKEY_LOG_LEVEL_LAST)
{
MONKEY_LOG_WARN("error level: %d", level);
return;
}
g_log_level = level;
}
/****************************************************************************
* Name: monkey_log_get_level
****************************************************************************/
enum monkey_log_level_type_e monkey_log_get_level(void)
{
return g_log_level;
}
#endif /* CONFIG_TESTING_MONKEY_LOG_ENABLE */

117
testing/monkey/monkey_log.h Normal file
View File

@ -0,0 +1,117 @@
/****************************************************************************
* apps/testing/monkey/monkey_log.h
*
* 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.
*
****************************************************************************/
#ifndef __MONKEY_LOG_H__
#define __MONKEY_LOG_H__
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#ifdef CONFIG_TESTING_MONKEY_LOG_ENABLE
# define MONKEY_LOG_INFO(format, ...) \
monkey_log_printf(MONKEY_LOG_LEVEL_INFO, \
__func__, \
format, \
##__VA_ARGS__)
# define MONKEY_LOG_NOTICE(format, ...) \
monkey_log_printf(MONKEY_LOG_LEVEL_NOTICE, \
__func__, \
format, \
##__VA_ARGS__)
# define MONKEY_LOG_WARN(format, ...) \
monkey_log_printf(MONKEY_LOG_LEVEL_WARN, \
__func__, \
format, \
##__VA_ARGS__)
# define MONKEY_LOG_ERROR(format, ...) \
monkey_log_printf(MONKEY_LOG_LEVEL_ERROR, \
__func__, \
format, \
##__VA_ARGS__)
#else
# define MONKEY_LOG_INFO(...)
# define MONKEY_LOG_NOTICE(...)
# define MONKEY_LOG_WARN(...)
# define MONKEY_LOG_ERROR(...)
#endif /* CONFIG_TESTING_MONKEY_LOG_ENABLE */
#ifdef __cplusplus
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
#ifdef CONFIG_TESTING_MONKEY_LOG_ENABLE
/****************************************************************************
* Public Types
****************************************************************************/
enum monkey_log_level_type_e
{
MONKEY_LOG_LEVEL_INFO,
MONKEY_LOG_LEVEL_NOTICE,
MONKEY_LOG_LEVEL_WARN,
MONKEY_LOG_LEVEL_ERROR,
_MONKEY_LOG_LEVEL_LAST
};
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: monkey_log_printf
****************************************************************************/
void monkey_log_printf(enum monkey_log_level_type_e level,
FAR const char *func,
FAR const char *fmt,
...);
/****************************************************************************
* Name: monkey_log_set_level
****************************************************************************/
void monkey_log_set_level(enum monkey_log_level_type_e level);
/****************************************************************************
* Name: monkey_log_get_level
****************************************************************************/
enum monkey_log_level_type_e monkey_log_get_level(void);
#endif /* CONFIG_TESTING_MONKEY_LOG_ENABLE */
#undef EXTERN
#ifdef __cplusplus
}
#endif
#endif /* __MONKEY_LOG_H__ */

View File

@ -0,0 +1,381 @@
/****************************************************************************
* apps/testing/monkey/monkey_main.c
*
* 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 <nuttx/config.h>
#include <errno.h>
#include <inttypes.h>
#include <signal.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "monkey.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define MONKEY_PREFIX "monkey"
#define OPTARG_TO_VALUE(value, type, base) \
do \
{ \
FAR char *ptr; \
value = (type)strtoul(optarg, &ptr, base); \
if (*ptr != '\0') \
{ \
printf(MONKEY_PREFIX "Parameter error: -%c %s\n", ch, optarg); \
show_usage(argv[0], EXIT_FAILURE); \
} \
} while (0)
/****************************************************************************
* Private Type Declarations
****************************************************************************/
struct monkey_param_s
{
int dev_type_mask;
FAR const char *file_path;
int hor_res;
int ver_res;
int period_min;
int period_max;
};
enum monkey_wait_res_e
{
MONKEY_WAIT_RES_AGAIN,
MONKEY_WAIT_RES_PAUSE,
MONKEY_WAIT_RES_STOP,
MONKEY_WAIT_RES_ERROR,
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: show_usage
****************************************************************************/
static void show_usage(FAR const char *progname, int exitcode)
{
printf("\nUsage: %s"
" -t <hex-value> -f <string> -p <string> -s <string>\n",
progname);
printf("\nWhere:\n");
printf(" -t <hex-value> Device type mask: uinput = 0x%02X;"
" touch = 0x%02X; button = 0x%02X.\n",
MONKEY_UINPUT_TYPE_MASK,
MONKEY_DEV_TYPE_TOUCH,
MONKEY_DEV_TYPE_BUTTON);
printf(" -f <string> Recorder playback file path.\n");
printf(" -p <string> Period(ms) range: "
"<decimal-value min>-<decimal-value max>\n");
printf(" -s <string> Screen resolution: "
"<decimal-value hor_res>x<decimal-value ver_res>\n");
exit(exitcode);
}
/****************************************************************************
* Name: monkey_init
****************************************************************************/
static FAR struct monkey_s *monkey_init(
FAR const struct monkey_param_s *param)
{
FAR struct monkey_s *monkey;
struct monkey_config_s config;
if (!param->dev_type_mask)
{
show_usage(MONKEY_PREFIX, EXIT_FAILURE);
}
monkey = monkey_create(param->dev_type_mask);
if (!monkey)
{
goto failed;
}
monkey_config_default_init(&config);
#ifdef CONFIG_TESTING_MONKEY_SCREEN_IS_ROUND
config.screen.type = MONKEY_SCREEN_TYPE_ROUND;
#endif
config.screen.hor_res = param->hor_res;
config.screen.ver_res = param->ver_res;
config.period.min = param->period_min;
config.period.max = param->period_max;
monkey_set_config(monkey, &config);
printf(MONKEY_PREFIX ": Screen: %dx%d %s\n",
config.screen.hor_res,
config.screen.ver_res,
(config.screen.type == MONKEY_SCREEN_TYPE_ROUND)
? "ROUND" : "RECT");
printf(MONKEY_PREFIX ": Period: %" PRIu32 " ~ %" PRIu32 "ms\n",
config.period.min,
config.period.max);
if (MONKEY_IS_UINPUT_TYPE(param->dev_type_mask))
{
if (param->file_path)
{
monkey_set_mode(monkey, MONKEY_MODE_PLAYBACK);
if (!monkey_set_recorder_path(monkey, param->file_path))
{
goto failed;
}
}
else
{
monkey_set_mode(monkey, MONKEY_MODE_RANDOM);
}
}
else
{
monkey_set_mode(monkey, MONKEY_MODE_RECORD);
if (!monkey_set_recorder_path(monkey,
CONFIG_TESTING_MONKEY_REC_DIR_PATH))
{
goto failed;
}
monkey_set_period(monkey, config.period.min);
}
return monkey;
failed:
if (monkey)
{
monkey_delete(monkey);
}
return NULL;
}
/****************************************************************************
* Name: parse_commandline
****************************************************************************/
static void parse_commandline(int argc, FAR char **argv,
FAR struct monkey_param_s *param)
{
int ch;
int hor_res = -1;
int ver_res = -1;
int period_min = -1;
int period_max = -1;
int converted;
memset(param, 0, sizeof(struct monkey_param_s));
param->hor_res = CONFIG_TESTING_MONKEY_SCREEN_HOR_RES;
param->ver_res = CONFIG_TESTING_MONKEY_SCREEN_VER_RES;
param->period_min = CONFIG_TESTING_MONKEY_PERIOD_MIN_DEFAULT;
param->period_max = CONFIG_TESTING_MONKEY_PERIOD_MAX_DEFAULT;
while ((ch = getopt(argc, argv, "t:f:p:s:")) != ERROR)
{
switch (ch)
{
case 't':
OPTARG_TO_VALUE(param->dev_type_mask, int, 16);
break;
case 'f':
param->file_path = optarg;
break;
case 'p':
converted = sscanf(optarg, "%d-%d", &period_min, &period_max);
if (converted == 2 && period_min >= 0 && period_max >= 0)
{
param->period_min = period_min;
param->period_max = period_max;
}
else
{
printf(MONKEY_PREFIX ": Error period range: %s\n", optarg);
show_usage(argv[0], EXIT_FAILURE);
}
break;
case 's':
converted = sscanf(optarg, "%dx%d", &hor_res, &ver_res);
if (converted == 2 && hor_res > 0 && ver_res > 0)
{
param->hor_res = hor_res;
param->ver_res = ver_res;
}
else
{
printf(MONKEY_PREFIX ": Error screen resolution: %s\n",
optarg);
show_usage(argv[0], EXIT_FAILURE);
}
break;
case '?':
printf(MONKEY_PREFIX ": Unknown option: %c\n", optopt);
show_usage(argv[0], EXIT_FAILURE);
break;
}
}
}
/****************************************************************************
* Name: monkey_pause
****************************************************************************/
static int monkey_pause(void)
{
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGCONT);
return sigwaitinfo(&set, NULL);
}
/****************************************************************************
* Name: monkey_wait
****************************************************************************/
static enum monkey_wait_res_e monkey_wait(uint32_t ms)
{
sigset_t set;
struct timespec timeout;
int ret;
enum monkey_wait_res_e res = MONKEY_WAIT_RES_ERROR;
if (ms == 0)
{
return MONKEY_WAIT_RES_AGAIN;
}
timeout.tv_sec = ms / 1000;
timeout.tv_nsec = (ms % 1000) * 1000000;
sigemptyset(&set);
sigaddset(&set, SIGTSTP);
ret = sigtimedwait(&set, NULL, &timeout);
if (ret < 0)
{
int errcode = errno;
if (errcode == EINTR)
{
res = MONKEY_WAIT_RES_STOP;
}
else if(errcode == EAGAIN)
{
res = MONKEY_WAIT_RES_AGAIN;
}
else
{
printf(MONKEY_PREFIX ": Unknow error: %d\n", errcode);
}
}
else if (ret == SIGTSTP)
{
res = MONKEY_WAIT_RES_PAUSE;
}
return res;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: main or monkey_main
*
* Description:
*
* Input Parameters:
* Standard argc and argv
*
* Returned Value:
* Zero on success; a positive, non-zero value on failure.
*
****************************************************************************/
int main(int argc, FAR char *argv[])
{
struct monkey_param_s param;
FAR struct monkey_s *monkey;
parse_commandline(argc, argv, &param);
monkey = monkey_init(&param);
if (!monkey)
{
return ERROR;
}
while (1)
{
enum monkey_wait_res_e res;
int sleep_ms;
sleep_ms = monkey_update(monkey);
if (sleep_ms < 0)
{
break;
}
res = monkey_wait(sleep_ms);
if (res == MONKEY_WAIT_RES_AGAIN)
{
continue;
}
else if (res == MONKEY_WAIT_RES_PAUSE)
{
printf(MONKEY_PREFIX ": pause\n");
if (monkey_pause() < 0)
{
break;
}
printf(MONKEY_PREFIX ": continue...\n");
}
else
{
/* STOP or ERROR */
break;
}
}
monkey_delete(monkey);
return OK;
}

View File

@ -0,0 +1,366 @@
/****************************************************************************
* apps/testing/monkey/monkey_proc.c
*
* 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 <nuttx/config.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "monkey.h"
#include "monkey_assert.h"
#include "monkey_log.h"
#include "monkey_dev.h"
#include "monkey_recorder.h"
#include "monkey_utils.h"
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: monkey_get_random_press
****************************************************************************/
static int monkey_get_random_press(int probability)
{
return monkey_random(0, 100) < probability ? 0 : 1;
}
/****************************************************************************
* Name: monkey_update_uinput_ramdom
****************************************************************************/
static bool monkey_update_uinput_ramdom(FAR struct monkey_s *monkey)
{
int i;
for (i = 0; i < monkey->dev_num; i++)
{
struct monkey_dev_state_s state;
FAR struct monkey_dev_s *dev = monkey->devs[i];
state.type = monkey_dev_get_type(dev);
if (state.type & MONKEY_DEV_TYPE_TOUCH)
{
int x_max = monkey->config.screen.hor_res - 1;
int y_max = monkey->config.screen.ver_res - 1;
state.data.touch.x = monkey_random(0, x_max);
state.data.touch.y = monkey_random(0, y_max);
state.data.touch.is_pressed = monkey_get_random_press(50);
monkey_dev_set_state(dev, &state);
}
else if (state.type & MONKEY_DEV_TYPE_BUTTON)
{
const int btn_num = CONFIG_TESTING_MONKEY_BUTTON_NUM;
int usleep_ret;
int btn_bits;
if (!btn_num)
{
MONKEY_LOG_ERROR("Button test number is 0");
return false;
}
/* select a button to click */
btn_bits = monkey_random(0, btn_num - 1);
/* press button */
state.data.button.value = 1 << btn_bits;
monkey_dev_set_state(dev, &state);
usleep_ret = usleep(CONFIG_TESTING_MONKEY_BUTTON_CLICK_TIME
* 1000);
/* release button */
state.data.button.value = 0;
monkey_dev_set_state(dev, &state);
/* detect monkey killed */
if (usleep_ret < 0)
{
MONKEY_LOG_NOTICE("detect monkey killed");
return false;
}
}
else
{
MONKEY_LOG_WARN("unsupport device type: %d", state.type);
}
}
return true;
}
/****************************************************************************
* Name: monkey_search_dev
****************************************************************************/
static FAR struct monkey_dev_s *monkey_search_dev(
FAR struct monkey_s *monkey,
enum monkey_dev_type_e type)
{
int i;
for (i = 0; i < monkey->dev_num; i++)
{
FAR struct monkey_dev_s *dev = monkey->devs[i];
if (type == dev->type)
{
return dev;
}
}
return NULL;
}
/****************************************************************************
* Name: monkey_recorder_get_next
****************************************************************************/
static int monkey_recorder_get_next(FAR struct monkey_s *monkey,
FAR uint32_t *cur_time_stamp_p,
FAR struct monkey_dev_state_s *cur_state_p,
FAR uint32_t *next_time_stamp_p,
FAR struct monkey_dev_state_s *next_state_p)
{
enum monkey_recorder_res_e res;
if (monkey->playback_ctx.time_stamp == 0)
{
res = monkey_recorder_read(monkey->recorder,
cur_state_p,
cur_time_stamp_p);
if (res != MONKEY_RECORDER_RES_OK)
{
MONKEY_LOG_ERROR("read first line failed: %d", res);
return 0;
}
}
else
{
*cur_time_stamp_p = monkey->playback_ctx.time_stamp;
*cur_state_p = monkey->playback_ctx.state;
}
res = monkey_recorder_read(monkey->recorder,
next_state_p,
next_time_stamp_p);
if (res != MONKEY_RECORDER_RES_OK)
{
if (res == MONKEY_RECORDER_RES_END_OF_FILE)
{
MONKEY_LOG_WARN("end of file");
return 1;
}
MONKEY_LOG_ERROR("read error: %d", res);
return 0;
}
monkey->playback_ctx.time_stamp = *next_time_stamp_p;
monkey->playback_ctx.state = *next_state_p;
return 2;
}
/****************************************************************************
* Name: monkey_update_uinput_playback
****************************************************************************/
static bool monkey_update_uinput_playback(FAR struct monkey_s *monkey)
{
FAR uint32_t cur_time_stamp;
struct monkey_dev_state_s cur_state;
FAR uint32_t next_time_stamp;
struct monkey_dev_state_s next_state;
uint32_t tick_elaps;
FAR struct monkey_dev_s *dev;
int num_of_get;
MONKEY_ASSERT_NULL(monkey->recorder);
num_of_get = monkey_recorder_get_next(monkey,
&cur_time_stamp,
&cur_state,
&next_time_stamp,
&next_state);
switch (num_of_get)
{
case 1:
next_time_stamp = cur_time_stamp;
memset(&monkey->playback_ctx, 0, sizeof(monkey->playback_ctx));
monkey_recorder_reset(monkey->recorder);
case 2:
dev = monkey_search_dev(monkey,
MONKEY_UINPUT_TYPE_MASK | cur_state.type);
if (dev)
{
monkey_dev_set_state(dev, &cur_state);
}
else
{
MONKEY_LOG_WARN("unsupport device type: %d", cur_state.type);
}
tick_elaps = monkey_tick_elaps(next_time_stamp, cur_time_stamp);
monkey_set_period(monkey, tick_elaps);
break;
default:
MONKEY_LOG_ERROR("error num_of_get = %d", num_of_get);
return false;
}
return true;
}
/****************************************************************************
* Name: monkey_update_uinput
****************************************************************************/
static bool monkey_update_uinput(FAR struct monkey_s *monkey)
{
bool retval = false;
MONKEY_ASSERT_NULL(monkey);
if (monkey->mode == MONKEY_MODE_RANDOM)
{
retval = monkey_update_uinput_ramdom(monkey);
}
else if(monkey->mode == MONKEY_MODE_PLAYBACK)
{
retval = monkey_update_uinput_playback(monkey);
}
else
{
MONKEY_LOG_ERROR("error mode: %d", monkey->mode);
}
return retval;
}
/****************************************************************************
* Name: monkey_update_input
****************************************************************************/
static bool monkey_update_input(FAR struct monkey_s *monkey)
{
struct monkey_dev_state_s state;
int available;
int i;
MONKEY_ASSERT_NULL(monkey);
available = monkey_dev_get_available(monkey->devs, monkey->dev_num);
if (available <= 0)
{
MONKEY_LOG_WARN("no available device");
return false;
}
for (i = 0; i < monkey->dev_num; i++)
{
FAR struct monkey_dev_s *dev = monkey->devs[i];
if (dev->is_available)
{
/* try to get device state */
if (!monkey_dev_get_state(dev, &state))
{
MONKEY_LOG_ERROR("can't get state");
return false;
}
if (dev->type == MONKEY_DEV_TYPE_TOUCH)
{
MONKEY_LOG_INFO("touch %s at x = %d, y = %d",
state.data.touch.is_pressed
? "PRESS " : "RELEASE",
state.data.touch.x, state.data.touch.y);
}
else if (dev->type == MONKEY_DEV_TYPE_BUTTON)
{
MONKEY_LOG_INFO("btn = 0x%08X", state.data.button.value);
}
/* record state */
if (monkey->recorder)
{
monkey_recorder_write(monkey->recorder, &state);
}
}
}
return true;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: monkey_update
****************************************************************************/
int monkey_update(FAR struct monkey_s *monkey)
{
int wait_time;
MONKEY_ASSERT_NULL(monkey);
srand(monkey_tick_get());
if (monkey->mode == MONKEY_MODE_RECORD)
{
if (monkey_update_input(monkey))
{
/* no need for sleep */
return 0;
}
else
{
return -1;
}
}
else
{
if (!monkey_update_uinput(monkey))
{
return -1;
}
}
wait_time = monkey_random(monkey->config.period.min,
monkey->config.period.max);
return wait_time;
}

View File

@ -0,0 +1,344 @@
/****************************************************************************
* apps/testing/monkey/monkey_recorder.c
*
* 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 <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "monkey_assert.h"
#include "monkey_log.h"
#include "monkey_dev.h"
#include "monkey_recorder.h"
#include "monkey_utils.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define MONKEY_REC_HEAD_FMT "T%" PRIu32 ",D%X,"
#define MONKEY_REC_TOUCH_FMT MONKEY_REC_HEAD_FMT "X%d,Y%d,PR%d\n"
#define MONKEY_REC_BTN_FMT MONKEY_REC_HEAD_FMT "V%" PRIu32 "\n"
#define MONKEY_REC_FILE_EXT ".csv"
#define MONKEY_REC_FILE_HEAD "rec_"
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: monkey_recorder_gen_file_path
****************************************************************************/
static void monkey_recorder_gen_file_path(FAR char *path_buf,
size_t buf_size,
FAR const char *dir_path)
{
char localtime_str_buf[64];
monkey_get_localtime_str(localtime_str_buf, sizeof(localtime_str_buf));
snprintf(path_buf, buf_size,
"%s/" MONKEY_REC_FILE_HEAD "%s" MONKEY_REC_FILE_EXT,
dir_path,
localtime_str_buf);
}
/****************************************************************************
* Name: monkey_readline
****************************************************************************/
static int monkey_readline(int fd, FAR char *ptr, int maxlen)
{
int n;
int rc;
char c;
for (n = 1; n < maxlen; n++)
{
if ((rc = read(fd, &c, 1)) == 1)
{
*ptr++ = c;
if (c == '\n')
{
break;
}
}
else if (rc == 0)
{
if (n == 1)
{
/* EOF no data read */
return 0;
}
else
{
/* EOF, some data read */
break;
}
}
else
{
/* error */
return -1;
}
}
*ptr = 0;
return n;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: monkey_recorder_create
****************************************************************************/
FAR struct monkey_recorder_s *monkey_recorder_create(FAR const char *path,
enum monkey_recorder_mode_e mode)
{
char file_path[PATH_MAX];
FAR const char *path_ptr;
int oflag;
int fd;
FAR struct monkey_recorder_s *recorder;
MONKEY_ASSERT_NULL(path);
recorder = malloc(sizeof(struct monkey_recorder_s));
MONKEY_ASSERT_NULL(recorder);
memset(recorder, 0, sizeof(struct monkey_recorder_s));
if (mode == MONKEY_RECORDER_MODE_RECORD)
{
if (!monkey_dir_check(path))
{
goto failed;
}
monkey_recorder_gen_file_path(file_path,
sizeof(file_path),
path);
path_ptr = file_path;
oflag = O_WRONLY | O_CREAT;
}
else
{
path_ptr = path;
oflag = O_RDONLY;
}
fd = open(path_ptr, oflag);
if (fd < 0)
{
MONKEY_LOG_ERROR("open %s failed: %d", path_ptr, errno);
goto failed;
}
recorder->fd = fd;
recorder->mode = mode;
MONKEY_LOG_NOTICE("open %s success, fd = %d, mode = %d",
path_ptr, fd, mode);
return recorder;
failed:
if (fd > 0)
{
close(fd);
}
if (recorder)
{
free(recorder);
}
return NULL;
}
/****************************************************************************
* Name: monkey_recorder_delete
****************************************************************************/
void monkey_recorder_delete(FAR struct monkey_recorder_s *recorder)
{
MONKEY_ASSERT_NULL(recorder);
if (recorder->fd > 0)
{
MONKEY_LOG_NOTICE("close fd: %d", recorder->fd);
close(recorder->fd);
recorder->fd = -1;
}
free(recorder);
}
/****************************************************************************
* Name: monkey_recorder_write
****************************************************************************/
enum monkey_recorder_res_e monkey_recorder_write(
FAR struct monkey_recorder_s *recorder,
FAR const struct monkey_dev_state_s *state)
{
char buffer[128];
int len = -1;
int ret;
uint32_t cur_tick;
MONKEY_ASSERT_NULL(recorder);
MONKEY_ASSERT(recorder->mode == MONKEY_RECORDER_MODE_RECORD);
cur_tick = monkey_tick_get();
switch (MONKEY_GET_DEV_TYPE(state->type))
{
case MONKEY_DEV_TYPE_TOUCH:
len = snprintf(buffer, sizeof(buffer), MONKEY_REC_TOUCH_FMT,
cur_tick,
state->type,
state->data.touch.x,
state->data.touch.y,
state->data.touch.is_pressed);
break;
case MONKEY_DEV_TYPE_BUTTON:
len = snprintf(buffer, sizeof(buffer), MONKEY_REC_BTN_FMT,
cur_tick,
state->type,
state->data.button.value);
break;
default:
return MONKEY_RECORDER_RES_DEV_TYPE_ERROR;
}
if (len <= 0)
{
return MONKEY_RECORDER_RES_PARSER_ERROR;
}
ret = write(recorder->fd, buffer, len);
if (ret < 0)
{
return MONKEY_RECORDER_RES_WRITE_ERROR;
}
return MONKEY_RECORDER_RES_OK;
}
/****************************************************************************
* Name: monkey_recorder_read
****************************************************************************/
enum monkey_recorder_res_e monkey_recorder_read(
FAR struct monkey_recorder_s *recorder,
FAR struct monkey_dev_state_s *state,
FAR uint32_t *time_stamp)
{
char buffer[128];
int dev_type;
int converted;
int ret;
MONKEY_ASSERT_NULL(recorder);
MONKEY_ASSERT(recorder->mode == MONKEY_RECORDER_MODE_PLAYBACK);
ret = monkey_readline(recorder->fd, buffer, sizeof(buffer));
if (ret < 0)
{
return MONKEY_RECORDER_RES_READ_ERROR;
}
if (ret == 0)
{
return MONKEY_RECORDER_RES_END_OF_FILE;
}
/* Read head to get device type */
converted = sscanf(buffer, MONKEY_REC_HEAD_FMT, time_stamp, &dev_type);
if (converted != 2)
{
return MONKEY_RECORDER_RES_PARSER_ERROR;
}
state->type = dev_type;
/* Get data */
switch (MONKEY_GET_DEV_TYPE(state->type))
{
case MONKEY_DEV_TYPE_TOUCH:
converted = sscanf(buffer,
MONKEY_REC_TOUCH_FMT,
time_stamp,
&dev_type,
&state->data.touch.x,
&state->data.touch.y,
&state->data.touch.is_pressed);
if (converted != 5)
{
return MONKEY_RECORDER_RES_PARSER_ERROR;
}
break;
case MONKEY_DEV_TYPE_BUTTON:
converted = sscanf(buffer,
MONKEY_REC_BTN_FMT,
time_stamp,
&dev_type,
&state->data.button.value);
if (converted != 3)
{
return MONKEY_RECORDER_RES_PARSER_ERROR;
}
break;
default:
return MONKEY_RECORDER_RES_DEV_TYPE_ERROR;
}
return MONKEY_RECORDER_RES_OK;
}
/****************************************************************************
* Name: monkey_recorder_reset
****************************************************************************/
enum monkey_recorder_res_e monkey_recorder_reset(
FAR struct monkey_recorder_s *recorder)
{
MONKEY_ASSERT_NULL(recorder);
lseek(recorder->fd, 0, SEEK_SET);
return MONKEY_RECORDER_RES_OK;
}

View File

@ -0,0 +1,110 @@
/****************************************************************************
* apps/testing/monkey/monkey_recorder.h
*
* 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.
*
****************************************************************************/
#ifndef __MONKEY_RECORDER_H__
#define __MONKEY_RECORDER_H__
/****************************************************************************
* Included Files
****************************************************************************/
#include "monkey_type.h"
/****************************************************************************
* Public Types
****************************************************************************/
enum monkey_recorder_mode_e
{
MONKEY_RECORDER_MODE_RECORD,
MONKEY_RECORDER_MODE_PLAYBACK
};
struct monkey_recorder_s
{
int fd;
enum monkey_recorder_mode_e mode;
};
enum monkey_recorder_res_e
{
MONKEY_RECORDER_RES_OK,
MONKEY_RECORDER_RES_END_OF_FILE,
MONKEY_RECORDER_RES_DEV_TYPE_ERROR,
MONKEY_RECORDER_RES_WRITE_ERROR,
MONKEY_RECORDER_RES_READ_ERROR,
MONKEY_RECORDER_RES_PARSER_ERROR
};
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
#ifdef __cplusplus
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
/****************************************************************************
* Name: monkey_recorder_create
****************************************************************************/
FAR struct monkey_recorder_s *monkey_recorder_create(FAR const char *path,
enum monkey_recorder_mode_e mode);
/****************************************************************************
* Name: monkey_recorder_delete
****************************************************************************/
void monkey_recorder_delete(FAR struct monkey_recorder_s *recorder);
/****************************************************************************
* Name: monkey_recorder_write
****************************************************************************/
enum monkey_recorder_res_e monkey_recorder_write(
FAR struct monkey_recorder_s *recorder,
FAR const struct monkey_dev_state_s *state);
/****************************************************************************
* Name: monkey_recorder_read
****************************************************************************/
enum monkey_recorder_res_e monkey_recorder_read(
FAR struct monkey_recorder_s *recorder,
FAR struct monkey_dev_state_s *state,
FAR uint32_t *time_stamp);
/****************************************************************************
* Name: monkey_recorder_reset
****************************************************************************/
enum monkey_recorder_res_e monkey_recorder_reset(
FAR struct monkey_recorder_s *recorder);
#undef EXTERN
#ifdef __cplusplus
}
#endif
#endif /* __MONKEY_RECORDER_H__ */

View File

@ -0,0 +1,118 @@
/****************************************************************************
* apps/testing/monkey/monkey_type.h
*
* 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.
*
****************************************************************************/
#ifndef __MONKEY_TYPE_H__
#define __MONKEY_TYPE_H__
/****************************************************************************
* Included Files
****************************************************************************/
#include <stdint.h>
#include <stdbool.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define MONKEY_UINPUT_TYPE_MASK (0x10)
#define MONKEY_IS_UINPUT_TYPE(type) (!!((type) & MONKEY_UINPUT_TYPE_MASK))
#define MONKEY_GET_DEV_TYPE(type) (type & ~MONKEY_UINPUT_TYPE_MASK)
#define MONKEY_DEV_MAX_NUM 2
/****************************************************************************
* Public Types
****************************************************************************/
struct monkey_dev_s;
struct monkey_recorder_s;
enum monkey_screen_type_e
{
MONKEY_SCREEN_TYPE_RECT,
MONKEY_SCREEN_TYPE_ROUND
};
enum monkey_mode_e
{
MONKEY_MODE_RANDOM,
MONKEY_MODE_RECORD,
MONKEY_MODE_PLAYBACK,
};
enum monkey_dev_type_e
{
MONKEY_DEV_TYPE_UNKNOW = 0x00,
MONKEY_DEV_TYPE_TOUCH = 0x01,
MONKEY_DEV_TYPE_BUTTON = 0x02,
MONKEY_DEV_TYPE_UTOUCH = MONKEY_UINPUT_TYPE_MASK | MONKEY_DEV_TYPE_TOUCH,
MONKEY_DEV_TYPE_UBUTTON = MONKEY_UINPUT_TYPE_MASK | MONKEY_DEV_TYPE_BUTTON,
};
struct monkey_dev_state_s
{
enum monkey_dev_type_e type;
union
{
struct
{
int x;
int y;
int is_pressed;
} touch;
struct
{
uint32_t value;
} button;
} data;
};
struct monkey_config_s
{
struct
{
enum monkey_screen_type_e type;
int hor_res;
int ver_res;
} screen;
struct
{
uint32_t min;
uint32_t max;
} period;
};
struct monkey_s
{
struct monkey_config_s config;
enum monkey_mode_e mode;
FAR struct monkey_dev_s *devs[MONKEY_DEV_MAX_NUM];
int dev_num;
FAR struct monkey_recorder_s *recorder;
struct
{
struct monkey_dev_state_s state;
uint32_t time_stamp;
} playback_ctx;
};
#endif /* __MONKEY_TYPE_H__ */

View File

@ -0,0 +1,197 @@
/****************************************************************************
* apps/testing/monkey/monkey_utils.c
*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include "monkey_log.h"
#include "monkey_utils.h"
/****************************************************************************
* Private Type Declarations
****************************************************************************/
struct monkey_port_dev_type_name_s
{
enum monkey_dev_type_e type;
FAR const char *name;
};
/****************************************************************************
* Private Data
****************************************************************************/
static const struct monkey_port_dev_type_name_s g_type_name_grp[] =
{
{ MONKEY_DEV_TYPE_TOUCH, "touch" },
{ MONKEY_DEV_TYPE_BUTTON, "button" },
{ MONKEY_DEV_TYPE_UTOUCH, "utouch" },
{ MONKEY_DEV_TYPE_UBUTTON, "ubutton" }
};
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: monkey_random
****************************************************************************/
int monkey_random(int min, int max)
{
if (min >= max)
{
return min;
}
long diff = max - min + 1;
return rand() % diff + min;
}
/****************************************************************************
* Name: monkey_tick_get
****************************************************************************/
uint32_t monkey_tick_get(void)
{
struct timespec ts;
uint32_t ms;
clock_gettime(CLOCK_MONOTONIC, &ts);
ms = ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
return ms;
}
/****************************************************************************
* Name: monkey_tick_elaps
****************************************************************************/
uint32_t monkey_tick_elaps(uint32_t act_time, uint32_t prev_tick)
{
/* If there is no overflow in sys_time simple subtract */
if (act_time >= prev_tick)
{
prev_tick = act_time - prev_tick;
}
else
{
prev_tick = UINT32_MAX - prev_tick + 1;
prev_tick += act_time;
}
return prev_tick;
}
/****************************************************************************
* Name: monkey_get_localtime_str
****************************************************************************/
void monkey_get_localtime_str(FAR char *str_buf, size_t buf_size)
{
time_t rawtime;
FAR struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
snprintf(str_buf, buf_size, "%04d%02d%02d_%02d%02d%02d",
1900 + timeinfo->tm_year,
timeinfo->tm_mon, timeinfo->tm_mday, timeinfo->tm_hour,
timeinfo->tm_min, timeinfo->tm_sec);
}
/****************************************************************************
* Name: monkey_dir_check
****************************************************************************/
bool monkey_dir_check(FAR const char *dir_path)
{
bool retval = false;
if (access(dir_path, F_OK) == 0)
{
MONKEY_LOG_NOTICE("directory: %s already exists", dir_path);
retval = true;
}
else
{
MONKEY_LOG_WARN("can't access directory: %s, creating...", dir_path);
if (mkdir(dir_path, 0777) == 0)
{
MONKEY_LOG_NOTICE("OK");
retval = true;
}
else
{
MONKEY_LOG_ERROR("create directory: %s failed", dir_path);
}
}
return retval;
}
/****************************************************************************
* Name: monkey_dev_type2name
****************************************************************************/
FAR const char *monkey_dev_type2name(enum monkey_dev_type_e type)
{
int i;
const int grp_len = sizeof(g_type_name_grp)
/ sizeof(struct monkey_port_dev_type_name_s);
for (i = 0; i < grp_len; i++)
{
if (type == g_type_name_grp[i].type)
{
return g_type_name_grp[i].name;
}
}
return "unknow";
}
/****************************************************************************
* Name: monkey_dev_name2type
****************************************************************************/
enum monkey_dev_type_e monkey_dev_name2type(FAR const char *name)
{
int i;
const int grp_len = sizeof(g_type_name_grp)
/ sizeof(struct monkey_port_dev_type_name_s);
if (name)
{
for (i = 0; i < grp_len; i++)
{
if (strcmp(name, g_type_name_grp[i].name) == 0)
{
return g_type_name_grp[i].type;
}
}
}
return MONKEY_DEV_TYPE_UNKNOW;
}

View File

@ -0,0 +1,94 @@
/****************************************************************************
* apps/testing/monkey/monkey_utils.h
*
* 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.
*
****************************************************************************/
#ifndef __MONKEY_UTILS_H__
#define __MONKEY_UTILS_H__
/****************************************************************************
* Included Files
****************************************************************************/
#include <stddef.h>
#include "monkey_type.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#ifdef __cplusplus
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: monkey_random
****************************************************************************/
int monkey_random(int min, int max);
/****************************************************************************
* Name: monkey_tick_get
****************************************************************************/
uint32_t monkey_tick_get(void);
/****************************************************************************
* Name: monkey_tick_elaps
****************************************************************************/
uint32_t monkey_tick_elaps(uint32_t act_time, uint32_t prev_tick);
/****************************************************************************
* Name: monkey_get_localtime_str
****************************************************************************/
void monkey_get_localtime_str(FAR char *str_buf, size_t buf_size);
/****************************************************************************
* Name: monkey_dir_check
****************************************************************************/
bool monkey_dir_check(FAR const char *dir_path);
/****************************************************************************
* Name: monkey_dev_type2name
****************************************************************************/
FAR const char *monkey_dev_type2name(enum monkey_dev_type_e type);
/****************************************************************************
* Name: monkey_dev_name2type
****************************************************************************/
enum monkey_dev_type_e monkey_dev_name2type(FAR const char *name);
#undef EXTERN
#ifdef __cplusplus
}
#endif
#endif /* __MONKEY_UTILS_H__ */