-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoto_test.c
executable file
·326 lines (286 loc) · 7.82 KB
/
moto_test.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include <stdio.h>
#include <ctype.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <error.h>
#include "moto_drv.h"
#if defined(MOTO_DBG)
#define MOTO_LOG printf
#else
#define MOTO_LOG
#endif
#define DO_SELF_TEST_CHAR "selftest"
#define DO_HCCW_CHAR "hccw"
#define DO_HCW_CHAR "hcw"
#define DO_VCCW_CHAR "vccw"
#define DO_VCW_CHAR "vcw"
#define DO_HCCWB_CHAR "hccwb"
#define DO_HCWB_CHAR "hcwb"
#define DO_VCCWB_CHAR "vccwb"
#define DO_VCWB_CHAR "vcwb"
#define DO_HCRUISING_CHAR "hcruising"
#define DO_VCRUISING_CHAR "vcruising"
#define DO_HVCRUISING_CHAR "hvcruising"
#define DO_STOP_CHAR "stop"
#define DO_GETPOS_CHAR "getpos"
#define DO_TOPOS_CHAR "topos"
#define MAX_CHAR_LEN (16)
typedef void (*do_func)(int fd, int argc, char *argv[]);
struct func_arr {
char *cmd;
void *func;
};
void do_self_test(int fd, int argc , char* argv[]);
void do_hccw(int fd, int argc , char* argv[]);
void do_hcw(int fd, int argc , char* argv[]);
void do_vccw(int fd, int argc , char* argv[]);
void do_vcw(int fd, int argc , char* argv[]);
void do_hccwb(int fd, int argc , char* argv[]);
void do_hcwb(int fd, int argc , char* argv[]);
void do_vccwb(int fd, int argc , char* argv[]);
void do_vcwb(int fd, int argc , char* argv[]);
void do_hcruising(int fd, int argc , char* argv[]);
void do_vcruising(int fd, int argc , char* argv[]);
void do_hvcruising(int fd, int argc , char* argv[]);
void do_stop(int fd, int argc , char* argv[]);
void do_getpos(int fd, int argc , char* argv[]);
void do_topos(int fd, int argc , char* argv[]);
struct func_arr func_tab[] = {
{DO_SELF_TEST_CHAR,do_self_test}
,{DO_HCCW_CHAR,do_hccw}
,{DO_HCW_CHAR,do_hcw}
,{DO_VCCW_CHAR,do_vccw}
,{DO_VCW_CHAR,do_vcw}
,{DO_HCCWB_CHAR,do_hccwb}
,{DO_HCWB_CHAR,do_hcwb}
,{DO_VCCWB_CHAR,do_vccwb}
,{DO_VCWB_CHAR,do_vcwb}
,{DO_HCRUISING_CHAR,do_hcruising}
,{DO_VCRUISING_CHAR,do_vcruising}
,{DO_HVCRUISING_CHAR,do_hvcruising}
,{DO_STOP_CHAR,do_stop}
,{DO_GETPOS_CHAR,do_getpos}
,{DO_TOPOS_CHAR,do_topos}
,{NULL,NULL}
};
int main_dbg(int argc , char* argv[])
{
return(0);
}
void do_self_test(int fd, int argc , char* argv[])
{
int ret =0;
ret = ioctl(fd, MTDRV_SELF_TEST);
/* wait here for completing */
}
void do_hccw(int fd, int argc , char* argv[])
{
int ret =0;
unsigned int nsteps=0;
if(argc < 3) return;
nsteps = atol(argv[2]);
MOTO_LOG("%s nsteps=%d\n",__func__,nsteps);
ret = ioctl(fd, MTDRV_HCCW, &nsteps);
MOTO_LOG("%s ret=%d, %s\n",__func__,ret, strerror(errno));
/* wait here for completing */
}
void do_hcw(int fd, int argc , char* argv[])
{
int ret =0;
unsigned int nsteps=0;
if(argc < 3) return;
nsteps = atol(argv[2]);
MOTO_LOG("%s nsteps=%d\n",__func__,nsteps);
ret = ioctl(fd, MTDRV_HCW, &nsteps);
MOTO_LOG("%s ret=%d, %s\n",__func__,ret, strerror(errno));
/* wait here for completing */
}
void do_vccw(int fd, int argc , char* argv[])
{
int ret =0;
unsigned int nsteps=0;
if(argc < 3) return;
nsteps = atol(argv[2]);
MOTO_LOG("%s nsteps=%d\n",__func__,nsteps);
ret = ioctl(fd, MTDRV_VCCW, &nsteps);
MOTO_LOG("%s ret=%d, %s\n",__func__,ret, strerror(errno));
/* wait here for completing */
}
void do_vcw(int fd, int argc , char* argv[])
{
int ret =0;
unsigned int nsteps=0;
if(argc < 3) return;
nsteps = atol(argv[2]);
MOTO_LOG("%s nsteps=%d\n",__func__,nsteps);
ret = ioctl(fd, MTDRV_VCW, &nsteps);
MOTO_LOG("%s ret=%d, %s\n",__func__,ret, strerror(errno));
/* wait here for completing */
}
void do_hccwb(int fd, int argc , char* argv[])
{
int ret =0;
unsigned int state=0xFFFFFFFF;
ret = ioctl(fd, MTDRV_GET_STATE,&state);
while(MOTO_STATE_IDLE != state)
{
ret = ioctl(fd, MTDRV_STOP);
usleep(100);
ret = ioctl(fd, MTDRV_GET_STATE,&state);
}
ret = ioctl(fd, MTDRV_HCCWB);
/* wait here for completing */
}
void do_hcwb(int fd, int argc , char* argv[])
{
int ret =0;
unsigned int state=0xFFFFFFFF;
ret = ioctl(fd, MTDRV_GET_STATE,&state);
while(MOTO_STATE_IDLE != state)
{
ret = ioctl(fd, MTDRV_STOP);
usleep(100);
ret = ioctl(fd, MTDRV_GET_STATE,&state);
}
ret = ioctl(fd, MTDRV_HCWB);
/* wait here for completing */
}
void do_vccwb(int fd, int argc , char* argv[])
{
int ret =0;
unsigned int state=0xFFFFFFFF;
ret = ioctl(fd, MTDRV_GET_STATE,&state);
while(MOTO_STATE_IDLE != state)
{
ret = ioctl(fd, MTDRV_STOP);
usleep(100);
ret = ioctl(fd, MTDRV_GET_STATE,&state);
}
ret = ioctl(fd, MTDRV_VCCWB);
/* wait here for completing */
}
void do_vcwb(int fd, int argc , char* argv[])
{
int ret =0;
unsigned int state=0xFFFFFFFF;
ret = ioctl(fd, MTDRV_GET_STATE,&state);
while(MOTO_STATE_IDLE != state)
{
ret = ioctl(fd, MTDRV_STOP);
usleep(100);
ret = ioctl(fd, MTDRV_GET_STATE,&state);
}
ret = ioctl(fd, MTDRV_VCWB);
/* wait here for completing */
}
void do_hcruising(int fd, int argc , char* argv[])
{
int ret =0;
unsigned int state=0xFFFFFFFF;
ret = ioctl(fd, MTDRV_GET_STATE,&state);
while(MOTO_STATE_IDLE != state)
{
ret = ioctl(fd, MTDRV_STOP);
usleep(100);
ret = ioctl(fd, MTDRV_GET_STATE,&state);
}
ret = ioctl(fd, MTDRV_HCRUISING);
/* wait here for completing */
}
void do_vcruising(int fd, int argc , char* argv[])
{
int ret =0;
unsigned int state=0xFFFFFFFF;
ret = ioctl(fd, MTDRV_GET_STATE,&state);
while(MOTO_STATE_IDLE != state)
{
ret = ioctl(fd, MTDRV_STOP);
usleep(100);
ret = ioctl(fd, MTDRV_GET_STATE,&state);
}
ret = ioctl(fd, MTDRV_VCRUISING);
/* wait here for completing */
}
void do_hvcruising(int fd, int argc , char* argv[])
{
int ret =0;
unsigned int state=0xFFFFFFFF;
ret = ioctl(fd, MTDRV_GET_STATE,&state);
while(MOTO_STATE_IDLE != state)
{
ret = ioctl(fd, MTDRV_STOP);
usleep(100);
ret = ioctl(fd, MTDRV_GET_STATE,&state);
}
ret = ioctl(fd, MTDRV_HVCRUISING);
/* wait here for completing */
}
void do_getpos(int fd, int argc , char* argv[])
{
int ret =0;
struct moto_pos pos={0};
ret = ioctl(fd, MTDRV_GET_POS,&pos);
/* wait here for completing */
}
void do_topos(int fd, int argc , char* argv[])
{
int ret =0;
struct moto_pos pos={0};
if(argc < 4) return;
pos.hpos = atol(argv[2]);
pos.vpos = atol(argv[3]);
ret = ioctl(fd, MTDRV_TO_POS, &pos);
MOTO_LOG("%s ret=%d, %s\n",__func__,ret, strerror(errno));
/* wait here for completing */
}
void do_stop(int fd, int argc , char* argv[])
{
int ret =0;
ret = ioctl(fd, MTDRV_STOP);
/* wait here for completing */
}
void *get_func(char *cmd)
{
struct func_arr *fa = &func_tab[0];
while(fa->cmd)
{
if(strcmp(cmd,fa->cmd) == 0)
{
MOTO_LOG("%s OK!!!\n",__func__);
break;
}
fa++;
}
return(fa->func);
}
int main_body(int argc , char* argv[])
{
int fd = -1;
do_func func = NULL;
if(argc < 2) return -1;
fd = open("/dev/" MOTO_DEVICE_NAME, 0);
if(fd<0)
{
printf("Open "MOTO_DEVICE_NAME" error!\n");
return -1;
}
MOTO_LOG("%s open ok\n",__func__);
func = (do_func)get_func(argv[1]);
MOTO_LOG("%s get func ok\n",__func__);
if(!func) return -1;
MOTO_LOG("%s exec func ok\n",__func__);
func(fd, argc, argv);
close(fd);
return 0;
}
int main(int argc , char* argv[])
{
#if 0//defined(MOTO_DBG)
return main_dbg(argc, argv);
#else
return main_body(argc, argv);
#endif
}