1 /*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "sync_clock.h"
18
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <linux/usbdevice_fs.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <sys/ioctl.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27
28
main(int argc,char ** argv)29 int main(int argc, char ** argv) {
30 if(argc < 2) {
31 printf("Usage %s <device_path>\n"
32 "Try `lsusb | grep eensy` and use /dev/bus/usb/<Bus>/<Device>\n",
33 argv[0]);
34 return 1;
35 }
36
37 printf("Opening %s\n", argv[1]);
38 int fd = open(argv[1], O_RDWR);
39 printf("open() fd=%d, errno=%d, %s\n", fd, errno, strerror(errno));
40
41 // The interface and endpoint numbers are defined by the TeensyUSB. They may
42 // be different depending on the mode (Serial vs HID) the Teensy code is
43 // compiled in. A real app would employ some discovery logic here. To list
44 // the interfaces and endpoints use `lsusb --verbose` or an app like USB
45 // Host Viewer on Android. Look for a "CDC Data" interface (class 0x0a).
46 int interface = 1;
47 int ep_out = 0x03;
48 int ep_in = 0x84;
49
50 int ret = ioctl(fd, USBDEVFS_CLAIMINTERFACE, &interface);
51 printf("Interface claimed retval=%d, errno=%d, %s\n", ret, errno, strerror(errno));
52 if (errno == EBUSY) {
53 printf("You may need to run 'sudo rmmod cdc_acm' to release the "
54 "interface claimed by the kernel serial driver.");
55 return 1;
56 }
57
58 struct clock_connection clk;
59 clk.fd = fd;
60 clk.endpoint_in = ep_in;
61 clk.endpoint_out = ep_out;
62
63 sync_clocks(&clk);
64
65 printf("===========================\n"
66 "sync_clocks base_t=%lld, minE=%d, maxE=%d\n",
67 (long long int)clk.t_base, clk.minE, clk.maxE);
68
69 // Check for clock drift. Try sleeping here to let it actually drift away.
70 update_bounds(&clk);
71
72 printf("*** UPDATE ****************\n"
73 "Update_bounds base_t=%lld, minE=%d, maxE=%d\n",
74 (long long int)(clk.t_base), clk.minE, clk.maxE
75 );
76
77
78 close(fd);
79 return 0;
80 }