1# Copyright 2014 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Package entry-point."""
6
7import argparse
8
9import netifaces
10from tornado import ioloop
11
12import echo_gadget
13import hid_echo_gadget
14import keyboard_gadget
15import linux_gadgetfs
16import mouse_gadget
17import server
18
19
20def ParseArgs():
21  """Parse application arguments."""
22  parser = argparse.ArgumentParser(description='USB gadget server.')
23  parser.add_argument(
24      '-i', '--interface', default='lo',
25      help='Listen for HTTP connections on this interface.')
26  parser.add_argument(
27      '-p', '--port', default=8080,
28      help='Listen for HTTP connections on this port.')
29  parser.add_argument(
30      '--hardware', default='beaglebone-black',
31      help='Hardware configuration.')
32  parser.add_argument(
33      '--start-claimed',
34      help='Start with the device claimed by this client.')
35  return parser.parse_args()
36
37
38def main():
39  args = ParseArgs()
40
41  server.interface = args.interface
42  server.port = args.port
43  server.hardware = args.hardware
44  server.claimed_by = args.start_claimed
45
46  addrs = netifaces.ifaddresses(server.interface)
47  ip_address = addrs[netifaces.AF_INET][0]['addr']
48  server.address = '{}:{}'.format(ip_address, server.port)
49
50  server.chip = linux_gadgetfs.LinuxGadgetfs(server.hardware)
51  server.SwitchGadget(server.default)
52
53  echo_gadget.RegisterHandlers()
54  hid_echo_gadget.RegisterHandlers()
55  keyboard_gadget.RegisterHandlers()
56  mouse_gadget.RegisterHandlers()
57
58  server.http_server.listen(server.port)
59
60  ioloop.IOLoop.instance().start()
61  print 'Exiting...'
62
63
64if __name__ == '__main__':
65  main()
66