#!/usr/bin/env ruby $LOAD_PATH.unshift(File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib')) $LOAD_PATH.unshift(File.join(File.expand_path(File.dirname(__FILE__)), '..', 'config')) require 'socket' require 'origen_link/server/sequencer' require 'origen_link/server/jtag' require 'version' # method to handle the processing of a jtag message def processjtagmessage(message) reply = 'Error' if message =~ /jtag_configure:/ # Allow the default io numbers to be overridden newios = message.gsub(/\s+/, '').split(':')[1].split(',') if newios.size = 4 $tdiio = newios[0] $tdoio = newios[1] $tmsio = newios[2] $tckio = newios[3] reply = "jtag_configure: tdi = #{$tdiio}, tdo = #{$tdoio}, tms = #{$tmsio}, tck = #{$tckio}" else reply = 'jtag_configure failed, need 4 io numbers, tdi, tdo, tms, tck - in that order' end elsif message =~ /jtag_reinit/ $jtag_interpreter.destroy unless $jtag_interpreter.nil? $jtag_interpreter = OrigenLink::Server::Jtag.new($tdiio, $tdoio, $tmsio, $tckio) reply = 'done' else $jtag_interpreter = OrigenLink::Server::Jtag.new($tdiio, $tdoio, $tmsio, $tckio) if $jtag_interpreter.nil? reply = $jtag_interpreter.processmessage(message) end reply end # General initialization server = TCPServer.open('', 12_777) remoteuser = '' remotehost = '' sessionactivity = Time.now sessionactive = false # Initialize the pin sequencer object pinsequencer = OrigenLink::Server::Sequencer.new pinsequencer.version = OrigenLink::VERSION puts "server version #{pinsequencer.version} started" # Set default values for the Jtag object $tdiio = 116 $tdoio = 124 $tmsio = 6 $tckio = 119 $jtag_interpreter = nil # Wait for connection requests in an infinite loop loop do client = server.accept thisuser = client.gets.chomp thisremotehost = client.peeraddr[3] # for now assume that any session lasting longer than 20 minutes has timed out (will happen if the origen side app is killed or stopped at a breakpoint with no activity) if (Time.now - sessionactivity) > 1200 sessionactive = false end change_in_user = false # if there is no active session running allow one to start unless sessionactive #flag any change in host machine or user for collision detection change_in_user = true unless (remoteuser.eql? thisuser) && (remotehost.eql? thisremotehost) remoteuser = thisuser remotehost = thisremotehost end # always return whether or not the user has been changed (for collision detection) if change_in_user response = "user_change:user_change\n" else response = "\n" end # Now we're ready to process the actual message # if this connection is from the active user\host machine, then process the information if (remoteuser.eql? thisuser) && (remotehost.eql? thisremotehost) while (message = client.gets) != "\n" # process the message if message =~ /session_end/ || message =~ /session_kill/ sessionactive = false response = response + "session_end:session_end\n" elsif message[0,5] == 'jtag_' # jtag messages get routed to the jtag message handler sessionactive = true response = response + processjtagmessage(message.chomp) + "\n" else # default is pin sequencer message handling sessionactive = true response = response + pinsequencer.processmessage(message.chomp) + "\n" end end sessionactivity = Time.now else # The connection didn't come from the active user. Only session_kill is allowed. checkmessage = client.gets.chomp if checkmessage =~ /session_kill/ sessionactive = false response = response + "Terminated: session from #{remoteuser} at IP address #{remotehost} inactive for #{Time.now - sessionactivity} seconds has been killed\n" change_in_user = true unless (remoteuser.eql? thisuser) && (remotehost.eql? thisremotehost) remoteuser = thisuser remotehost = thisremotehost else response = response + "Busy: server is in use by #{remoteuser} from IP address #{remotehost}\n" end end client.write(response) client.close end