Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 import rospy
00045 import applanix_msgs.msg
00046
00047
00048 from data import DataPort
00049 from control import ControlPort
00050 from monitor import Monitor
00051
00052
00053 import socket
00054 import struct
00055 from cStringIO import StringIO
00056 import time
00057
00058 import translator
00059 from handlers import NullHandler, GroupHandler, MessageHandler, AckHandler
00060
00061 PORTS_DATA = {
00062 "realtime": 5602,
00063 "logging": 5603
00064 }
00065 PORT_CONTROL = 5601
00066
00067 DEFAULT_IP = '192.168.53.100'
00068 PREFIX_DEFAULTS = {
00069 "raw": True,
00070 "dmi": True,
00071 "status": True,
00072 "events": True
00073 }
00074
00075 SOCKET_TIMEOUT=10.0
00076 socks = []
00077 ports = {}
00078 monitor = Monitor(ports)
00079
00080
00081 def main():
00082 rospy.init_node('applanix_bridge')
00083
00084
00085
00086 ip = rospy.get_param('ip', DEFAULT_IP)
00087
00088
00089
00090
00091 data_port = PORTS_DATA[rospy.get_param('data', "realtime")]
00092
00093
00094
00095
00096 control_enabled = rospy.get_param('control', True)
00097
00098
00099
00100 exclude_prefixes = []
00101 for prefix, default in PREFIX_DEFAULTS.items():
00102 if not rospy.get_param('include_%s' % prefix, default):
00103 exclude_prefixes.append(prefix)
00104
00105
00106
00107 pcap_file_name = rospy.get_param('pcap_file', False)
00108
00109 if not pcap_file_name:
00110 sock = create_sock('data', ip, data_port)
00111 else:
00112 sock = create_test_sock(pcap_file_name)
00113
00114 ports['data'] = DataPort(sock, exclude_prefixes=exclude_prefixes)
00115
00116 if control_enabled:
00117 ports['control'] = ControlPort(create_sock('control', ip, PORT_CONTROL))
00118
00119 for name, port in ports.items():
00120 port.start()
00121 rospy.loginfo("Port %s thread started." % name)
00122 monitor.start()
00123
00124 rospy.on_shutdown(shutdown)
00125 rospy.spin()
00126
00127
00128 def create_sock(name, ip, port):
00129 try:
00130 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
00131 ip_port = (ip, port)
00132 sock.connect(ip_port)
00133 rospy.loginfo("Successfully connected to %%s port at %s:%d" % ip_port % name)
00134 except socket.error as e:
00135 rospy.logfatal("Couldn't connect to %%s port at %s:%d: %%s" % ip_port % (name, str(e)))
00136 exit(1)
00137 socks.append(sock)
00138 return sock
00139
00140
00141 def create_test_sock(pcap_filename):
00142 rospy.sleep(0.1)
00143
00144 import pcapy
00145 from StringIO import StringIO
00146 from impacket import ImpactDecoder
00147
00148 body_list = []
00149 cap = pcapy.open_offline(pcap_filename)
00150 decoder = ImpactDecoder.EthDecoder()
00151
00152 while True:
00153 header, payload = cap.next()
00154 if not header: break
00155 udp = decoder.decode(payload).child().child()
00156 body_list.append(udp.child().get_packet())
00157
00158 data_io = StringIO(''.join(body_list))
00159
00160 class MockSocket(object):
00161 def recv(self, byte_count):
00162 rospy.sleep(0.0001)
00163 data = data_io.read(byte_count)
00164 if data == "":
00165 rospy.signal_shutdown("Test completed.")
00166 return data
00167 def settimeout(self, timeout):
00168 pass
00169
00170 return MockSocket()
00171
00172
00173 def shutdown():
00174 monitor.finish.set()
00175 monitor.join()
00176 rospy.loginfo("Thread monitor finished.")
00177 for name, port in ports.items():
00178 port.finish.set()
00179 port.join()
00180 rospy.loginfo("Port %s thread finished." % name)
00181 for sock in socks:
00182 sock.shutdown(socket.SHUT_RDWR)
00183 sock.close()
00184 rospy.loginfo("Sockets closed.")