forked from apache/nuttx-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonkey_event.c
193 lines (159 loc) · 6 KB
/
monkey_event.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/****************************************************************************
* apps/testing/monkey/monkey_event.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 <string.h>
#include <unistd.h>
#include "monkey_dev.h"
#include "monkey_event.h"
#include "monkey_log.h"
#include "monkey_utils.h"
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: monkey_exec_darg
****************************************************************************/
static bool monkey_exec_darg(FAR struct monkey_dev_s *dev,
FAR struct monkey_dev_state_s *state,
FAR const struct monkey_event_param_s *param)
{
int t = 0;
uint32_t start;
state->data.touch.is_pressed = true;
start = monkey_tick_get();
while (t < param->duration)
{
t = monkey_tick_elaps(monkey_tick_get(), start);
state->data.touch.x = monkey_map(t, 0, param->duration,
param->x1, param->x2);
state->data.touch.y = monkey_map(t, 0, param->duration,
param->y1, param->y2);
monkey_dev_set_state(dev, state);
if (usleep(1000) < 0)
{
return false;
}
}
return true;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: monkey_event_gen
****************************************************************************/
void monkey_event_gen(FAR struct monkey_s *monkey,
FAR struct monkey_event_param_s *param)
{
int weight = 0;
int total = 0;
int i;
int rnd;
int duration_min;
int duration_max;
memset(param, 0, sizeof(struct monkey_event_param_s));
/* Calculate total weight */
for (i = 0; i < MONKEY_EVENT_LAST; i++)
{
total += monkey->config.event[i].weight;
}
rnd = monkey_random(0, total);
/* Select random events based on weight */
for (i = 0; i < MONKEY_EVENT_LAST; i++)
{
weight += monkey->config.event[i].weight;
if (rnd < weight)
{
param->event = i;
break;
}
}
duration_min = monkey->config.event[param->event].duration_min;
duration_max = monkey->config.event[param->event].duration_max;
param->duration = monkey_random(duration_min, duration_max);
param->x1 = monkey_random(0, monkey->config.screen.hor_res - 1);
param->y1 = monkey_random(0, monkey->config.screen.ver_res - 1);
param->x2 = monkey_random(0, monkey->config.screen.hor_res - 1);
param->y2 = monkey_random(0, monkey->config.screen.ver_res - 1);
MONKEY_LOG_INFO("event=%d(%s) duration=%d x1=%d y1=%d x2=%d y2=%d",
param->event,
monkey_event_type2name(param->event),
param->duration,
param->x1, param->y1,
param->x2, param->y2);
}
/****************************************************************************
* Name: monkey_event_exec
****************************************************************************/
bool monkey_event_exec(FAR struct monkey_s *monkey,
FAR struct monkey_dev_s *dev,
FAR const struct monkey_event_param_s *param)
{
bool retval = false;
struct monkey_dev_state_s state;
memset(&state, 0, sizeof(struct monkey_dev_state_s));
state.type = monkey_dev_get_type(dev);
MONKEY_LOG_INFO("dev=0x%x event=%d(%s) duration=%d"
" x1=%d y1=%d x2=%d y2=%d",
state.type,
param->event,
monkey_event_type2name(param->event),
param->duration,
param->x1, param->y1,
param->x2, param->y2);
if (state.type & MONKEY_DEV_TYPE_TOUCH)
{
state.data.touch.x = param->x1;
state.data.touch.y = param->y1;
state.data.touch.is_pressed = true;
monkey_dev_set_state(dev, &state);
if (param->event == MONKEY_EVENT_DRAG)
{
retval = monkey_exec_darg(dev, &state, param);
}
else
{
retval = usleep(param->duration * 1000) == 0;
}
if (!retval)
{
MONKEY_LOG_NOTICE("detect monkey killed");
}
state.data.touch.is_pressed = false;
monkey_dev_set_state(dev, &state);
}
else if (state.type & MONKEY_DEV_TYPE_BUTTON)
{
/* press button */
state.data.button.value = 1 << monkey->config.btn_bit;
monkey_dev_set_state(dev, &state);
retval = usleep(param->duration * 1000) == 0;
/* release button */
state.data.button.value = 0;
monkey_dev_set_state(dev, &state);
}
else
{
MONKEY_LOG_WARN("unsupport device type: %d", state.type);
}
return retval;
}