forked from apache/nuttx-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonkey_utils.c
256 lines (217 loc) · 7.14 KB
/
monkey_utils.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/****************************************************************************
* 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_map
****************************************************************************/
int monkey_map(int x, int min_in, int max_in, int min_out, int max_out)
{
if (max_in >= min_in && x >= max_in)
{
return max_out;
}
if (max_in >= min_in && x <= min_in)
{
return min_out;
}
if (max_in <= min_in && x <= max_in)
{
return max_out;
}
if (max_in <= min_in && x >= min_in)
{
return min_out;
}
/* The equation should be:
* ((x - min_in) * delta_out) / delta in) + min_out
* To avoid rounding error reorder the operations:
* (x - min_in) * (delta_out / delta_min) + min_out
*/
int delta_in = max_in - min_in;
int delta_out = max_out - min_out;
return ((x - min_in) * delta_out) / delta_in + min_out;
}
/****************************************************************************
* 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;
}
/****************************************************************************
* Name: monkey_event_type2name
****************************************************************************/
FAR const char *monkey_event_type2name(enum monkey_event_e event)
{
switch (event)
{
case MONKEY_EVENT_CLICK:
return "click";
case MONKEY_EVENT_LONG_PRESS:
return "long-press";
case MONKEY_EVENT_DRAG:
return "drag";
default:
break;
}
return "unknow";
}