bridge.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 # Software License Agreement (BSD)
5 #
6 # file @bridge.py
7 # authors Mike Purvis <mpurvis@clearpathrobotics.com>
8 # NovAtel <novatel.com/support>
9 # copyright Copyright (c) 2012, Clearpath Robotics, Inc., All rights reserved.
10 # Copyright (c) 2014, NovAtel Inc., All rights reserved.
11 #
12 # Redistribution and use in source and binary forms, with or without modification, are permitted provided that
13 # the following conditions are met:
14 # * Redistributions of source code must retain the above copyright notice, this list of conditions and the
15 # following disclaimer.
16 # * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
17 # following disclaimer in the documentation and/or other materials provided with the distribution.
18 # * Neither the name of Clearpath Robotics nor the names of its contributors may be used to endorse or promote
19 # products derived from this software without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WAR-
22 # RANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, IN-
24 # DIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
25 # OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 # ROS
30 import rospy
31 from novatel_msgs.msg import *
32 from std_msgs.msg import String
33 
34 # Package modules
35 from novatel_span_driver.data import DataPort
36 from novatel_span_driver.monitor import Monitor
37 
38 # Standard
39 import socket
40 import struct
41 from cStringIO import StringIO
42 import time
43 
44 from novatel_span_driver import translator
45 
46 DEFAULT_IP = '198.161.73.9'
47 DEFAULT_PORT = 3001
48 
49 SOCKET_TIMEOUT = 100.0
50 socks = []
51 ports = {}
52 monitor = Monitor(ports)
53 
54 
55 def init():
56  ip = rospy.get_param('~ip', DEFAULT_IP)
57  data_port = rospy.get_param('~port', DEFAULT_PORT)
58 
59  # Pass this parameter to use pcap data rather than a socket to a device.
60  # For testing the node itself--to exercise downstream algorithms, use a bag.
61  pcap_file_name = rospy.get_param('~pcap_file', False)
62 
63  if not pcap_file_name:
64  sock = create_sock('data', ip, data_port)
65  else:
66  sock = create_test_sock(pcap_file_name)
67 
68  ports['data'] = DataPort(sock)
69 
70  configure_receiver(sock)
71 
72  for name, port in ports.items():
73  port.start()
74  rospy.loginfo("Port %s thread started." % name)
75  monitor.start()
76 
77  rospy.on_shutdown(shutdown)
78 
79 
80 def create_sock(name, ip, port):
81  try:
82  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
83  ip_port = (ip, port)
84  sock.connect(ip_port)
85  rospy.loginfo("Successfully connected to %%s port at %s:%d" % ip_port % name)
86  except socket.error as e:
87  rospy.logfatal("Couldn't connect to %%s port at %s:%d: %%s" % ip_port % (name, str(e)))
88  exit(1)
89  sock.settimeout(SOCKET_TIMEOUT)
90  socks.append(sock)
91  return sock
92 
93 
94 def create_test_sock(pcap_filename):
95  rospy.sleep(0.1)
96 
97  try:
98  import pcapy
99  except ImportError:
100  import pure_pcapy as pcapy
101 
102  from StringIO import StringIO
103  from impacket import ImpactDecoder
104 
105  body_list = []
106  if pcap_filename.endswith("gz"):
107  # From: http://projects.honeynet.org/honeysnap/changeset/35/trunk/honeysnap/__init__.py
108  import tempfile
109  import gzip
110  tmph, tmpf = tempfile.mkstemp()
111  tmph = open(tmpf, 'wb')
112  gfile = gzip.open(pcap_filename)
113  tmph.write(gfile.read())
114  gfile.close()
115  tmph.close()
116  pcap_filename = tmpf
117 
118  cap = pcapy.open_offline(pcap_filename)
119  decoder = ImpactDecoder.EthDecoder()
120 
121  while True:
122  header, payload = cap.next()
123  if not header:
124  break
125  try:
126  tcp = decoder.decode(payload).child().child()
127  body_list.append(tcp.child().get_packet())
128  except AttributeError:
129  print(decoder.decode(payload))
130  raise
131 
132  data_io = StringIO(''.join(body_list))
133 
134  class MockSocket(object):
135  def recv(self, byte_count):
136  rospy.sleep(0.002)
137  data = data_io.read(byte_count)
138  if data == "":
139  rospy.signal_shutdown("Test completed.")
140  return data
141 
142  def settimeout(self, timeout):
143  pass
144 
145  return MockSocket()
146 
147 
149  receiver_config = rospy.get_param('~configuration', None)
150 
151  if receiver_config is not None:
152  imu_connect = receiver_config.get('imu_connect', None)
153  if imu_connect is not None:
154  rospy.loginfo("Sending IMU connection string to SPAN system.")
155  port.send('connectimu ' + imu_connect['port'] + ' ' + imu_connect['type'] + '\r\n')
156 
157  logger = receiver_config.get('log_request', [])
158  rospy.loginfo("Enabling %i log outputs from SPAN system." % len(logger))
159  for log in logger:
160  port.send('log ' + log + ' ontime ' + str(logger[log]) + '\r\n')
161 
162  commands = receiver_config.get('command', [])
163  rospy.loginfo("Sending %i user-specified initialization commands to SPAN system." % len(commands))
164  for cmd in commands:
165  port.send(cmd + ' ' + str(commands[cmd]) + '\r\n')
166 
167 
168 def shutdown():
169  monitor.finish.set()
170  monitor.join()
171  rospy.loginfo("Thread monitor finished.")
172  for name, port in ports.items():
173  port.finish.set()
174  port.join()
175  rospy.loginfo("Port %s thread finished." % name)
176  for sock in socks:
177  sock.shutdown(socket.SHUT_RDWR)
178  sock.close()
179  rospy.loginfo("Sockets closed.")
def create_test_sock(pcap_filename)
Definition: bridge.py:94
def configure_receiver(port)
Definition: bridge.py:148
def create_sock(name, ip, port)
Definition: bridge.py:80


novatel_span_driver
Author(s): NovAtel Support
autogenerated on Wed Apr 3 2019 02:52:53