parent
86675204cb
commit
478c3f654c
|
@ -3,7 +3,10 @@
|
||||||
require 'common'
|
require 'common'
|
||||||
|
|
||||||
# this is an abstract class
|
# this is an abstract class
|
||||||
# TODO : verify state before sending, respect max size
|
# TODO :
|
||||||
|
# - verify state before sending
|
||||||
|
# - respect max size (and require min size)
|
||||||
|
# - ERROR_RESP handling
|
||||||
class Client < SAP
|
class Client < SAP
|
||||||
|
|
||||||
# make the class abstract
|
# make the class abstract
|
||||||
|
@ -45,7 +48,7 @@ class Client < SAP
|
||||||
# verify response
|
# verify response
|
||||||
if connection_status==0x00 and message[:payload].size==2 then
|
if connection_status==0x00 and message[:payload].size==2 then
|
||||||
# OK, Server can fulfill requirements
|
# OK, Server can fulfill requirements
|
||||||
log("client","connection to server succeded",3)
|
log("client","connected",3)
|
||||||
set_state :idle
|
set_state :idle
|
||||||
elsif connection_status==0x02 and message[:payload].size==2 then
|
elsif connection_status==0x02 and message[:payload].size==2 then
|
||||||
# Error, Server does not support maximum message size
|
# Error, Server does not support maximum message size
|
||||||
|
@ -56,12 +59,15 @@ class Client < SAP
|
||||||
set_state :not_connected
|
set_state :not_connected
|
||||||
raise "connection error"
|
raise "connection error"
|
||||||
end
|
end
|
||||||
|
when "DISCONNECT_RESP"
|
||||||
|
log("client","disconnected",3)
|
||||||
|
set_state :not_connected
|
||||||
|
@end=true
|
||||||
else
|
else
|
||||||
raise "not implemented or unknown message type : #{message[:name]}"
|
raise "not implemented or unknown message type : #{message[:name]}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# send CONNECT_REQ
|
|
||||||
def connect
|
def connect
|
||||||
log("client","connecting",3)
|
log("client","connecting",3)
|
||||||
until @state==:idle do
|
until @state==:idle do
|
||||||
|
@ -81,15 +87,18 @@ class Client < SAP
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
# send TRANSFER_ATR_REQ
|
def disconnect
|
||||||
def get_atr
|
log("client","disconnecting",3)
|
||||||
if @state == :idle then
|
if @state==:not_connected or @state==:connection_under_negociation then
|
||||||
connect = create_message("TRANSFER_ATR_REQ",[])
|
raise "can not disconnect. must be connected, current state : #{@state}"
|
||||||
|
return false
|
||||||
|
else # send DISCONNECT_REQ
|
||||||
|
connect = create_message("DISCONNECT_REQ")
|
||||||
send(connect)
|
send(connect)
|
||||||
@state = :processing_atr_request
|
until @state==:not_connected
|
||||||
else
|
sleep 0.1
|
||||||
@sap.close
|
end
|
||||||
raise "can request ATR. required state : idle, current state : #{@sate}"
|
return true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -156,9 +156,9 @@ class SAP
|
||||||
# start listening the connection
|
# start listening the connection
|
||||||
def start
|
def start
|
||||||
until @end do
|
until @end do
|
||||||
log("task","select",3)
|
log("IO","select",3)
|
||||||
activity = IO.select([@io])
|
activity = IO.select([@io])
|
||||||
log("task","activity",3)
|
log("IO","activity",3)
|
||||||
begin
|
begin
|
||||||
input = activity[0][0].readpartial(4096)
|
input = activity[0][0].readpartial(4096)
|
||||||
rescue EOFError
|
rescue EOFError
|
||||||
|
@ -237,7 +237,7 @@ class SAP
|
||||||
# create a message
|
# create a message
|
||||||
# - type : message id or name
|
# - type : message id or name
|
||||||
# - payload : array [parameter id or name, content]
|
# - payload : array [parameter id or name, content]
|
||||||
def create_message(type,payload)
|
def create_message(type,payload=nil)
|
||||||
|
|
||||||
# the type
|
# the type
|
||||||
msg_type = nil
|
msg_type = nil
|
||||||
|
@ -253,6 +253,7 @@ class SAP
|
||||||
|
|
||||||
# the parameters
|
# the parameters
|
||||||
message[:payload] = []
|
message[:payload] = []
|
||||||
|
payload = [] unless payload
|
||||||
payload.each do |parameter|
|
payload.each do |parameter|
|
||||||
param_type = nil
|
param_type = nil
|
||||||
if parameter[0].kind_of?(Fixnum) then
|
if parameter[0].kind_of?(Fixnum) then
|
||||||
|
|
|
@ -12,4 +12,5 @@ end
|
||||||
io = TCPSocket.open("localhost",1234)
|
io = TCPSocket.open("localhost",1234)
|
||||||
client = DemoClient.new(io)
|
client = DemoClient.new(io)
|
||||||
client.start
|
client.start
|
||||||
client.connect
|
client.connect
|
||||||
|
client.disconnect
|
|
@ -4,7 +4,13 @@
|
||||||
require 'common'
|
require 'common'
|
||||||
|
|
||||||
# this is an bastract class
|
# this is an bastract class
|
||||||
# TODO : respect max message size
|
# TODO (not implemented) :
|
||||||
|
# - respect max message size (and check minimum)
|
||||||
|
# - refuse connection if card is missing
|
||||||
|
# - server initiated disconnect (when programm want to exit or card is lost)
|
||||||
|
# - transport protocol change
|
||||||
|
# - power sim on/off or reset
|
||||||
|
# - ERROR_RESP sending (instead of exception)
|
||||||
class Server < SAP
|
class Server < SAP
|
||||||
|
|
||||||
# make the class abstract
|
# make the class abstract
|
||||||
|
@ -25,7 +31,7 @@ class Server < SAP
|
||||||
|
|
||||||
# initiate the state machine (connect_req)
|
# initiate the state machine (connect_req)
|
||||||
set_state :not_connected
|
set_state :not_connected
|
||||||
@max_msg_size = 0x0fff
|
@max_msg_size = 0xffff
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -68,8 +74,11 @@ class Server < SAP
|
||||||
set_state :idle
|
set_state :idle
|
||||||
log("server","connection established",3)
|
log("server","connection established",3)
|
||||||
end
|
end
|
||||||
|
when "DISCONNECT_REQ"
|
||||||
when "STATUS_IND"
|
log("server","client disconneting",3)
|
||||||
|
response = create_message("DISCONNECT_RESP")
|
||||||
|
send(response)
|
||||||
|
set_state :not_connected
|
||||||
else
|
else
|
||||||
raise "not implemented or unknown message type : #{message[:name]}"
|
raise "not implemented or unknown message type : #{message[:name]}"
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue