aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin Redon <kevredon@mail.tsaitgaist.info>2011-05-02 20:03:41 +0200
committerKevin Redon <kevredon@mail.tsaitgaist.info>2011-05-02 20:03:41 +0200
commit560c14687b6e7eeb8a93230438fe5ec03eb735d4 (patch)
treeab46ec25955726d3fbfb0e8aa5112ca4a9b2e106
parent93aa6bc90414700faa2b0bfeedd4a6d1d8307941 (diff)
better verbosity
-rwxr-xr-xsrc/demo_client.rb25
-rwxr-xr-xsrc/demo_server.rb13
-rw-r--r--src/lib/apdu.rb3
-rw-r--r--src/lib/client.rb9
-rw-r--r--src/lib/common.rb17
-rw-r--r--src/lib/server.rb3
6 files changed, 31 insertions, 39 deletions
diff --git a/src/demo_client.rb b/src/demo_client.rb
index 7ece6ea..a739ec9 100755
--- a/src/demo_client.rb
+++ b/src/demo_client.rb
@@ -17,18 +17,19 @@ along with SAP. If not, see <http://www.gnu.org/licenses/>.
Copyright (C) 2011 Kevin "tsaitgaist" Redon kevredon@mail.tsaitgaist.info
=end
-# This programm will create a client which can be used to test servers
+# this programm will create a client which can be used to test servers
require 'lib/client'
require 'lib/apdu'
+# wich IO to use (:tcp,:unix,:bt)
+client_io = :tcp
+# the verbosity (from common)
+$verbosity = 1
+
#=================
#== client type ==
#=================
-# wich IO to use
-client_io = :tcp
-# the IO itself
-io = nil
# create IO
case client_io
when :tcp
@@ -41,11 +42,6 @@ when :bt
#sudo gem install serialport (http://rubygems.org/gems/serialport)
require 'rubygems'
require 'serialport'
-=begin
-to monitor bluetooth traffic
-sudo aptitude install bluez-hcidump
-sudo hcidump -x -i hci0 rfcomm
-=end
bt = BluetoothSAPSerial.new
# using SerialPort because reading the File does not work (have to find right stty options)
io = SerialPort.new(bt.connect)
@@ -57,11 +53,7 @@ end
#== constants ==
#===============
-# to debug the program
-# shows APDU IO
-DEBUG = true
-# the verbosity from common
-VERBOSE = 0
+
#=============
#== methods ==
@@ -78,13 +70,12 @@ end
#== main ==
#==========
-@client = Client.new(io,VERBOSE)
+@client = Client.new(io)
@client.start
@client.connect
atr = @client.atr
puts atr ? "ATR : #{atr.to_hex_disp}" : "could not get ATR"
# select MF
-transmit(GET_RESPONSE+[0x1a])
select(MF)
@client.disconnect
diff --git a/src/demo_server.rb b/src/demo_server.rb
index 414c5d3..e19df68 100755
--- a/src/demo_server.rb
+++ b/src/demo_server.rb
@@ -18,22 +18,23 @@ along with SAP. If not, see <http://www.gnu.org/licenses/>.
Copyright (C) 2011 Kevin "tsaitgaist" Redon kevredon@mail.tsaitgaist.info
=end
# this program is there to start a server
-require 'socket'
-require 'pcsc_server'
-require 'sim_server'
-# the io the server should use (:tcp, :unix)
+# the io the server should use (:tcp,:unix)
io_type = :tcp
-# the server to use (:pcsc, :sim)
+# the server to use (:pcsc,:sim)
server_type = :pcsc
+# the verbosity (from common)
+$verbosity = 1
# create the IO
case io_type
when :tcp
+ require 'socket'
TCP_HOST = "localhost"
TCP_PORT = "1337"
socket = TCPServer.new(TCP_HOST,TCP_PORT)
when :unix
+ require 'socket'
UNIX = "/tmp/sap_server.socket"
socket = UNIXServer.new(APDU_SOCKET)
else
@@ -45,8 +46,10 @@ io = socket.accept
case server_type
when :pcsc
+ require 'pcsc_server'
server = PCSCServer.new(io)
when :sim
+ require 'sim_server'
server = SIMServer.new(io)
else
raise "unkown server type"
diff --git a/src/lib/apdu.rb b/src/lib/apdu.rb
index 33303a9..15dea0e 100644
--- a/src/lib/apdu.rb
+++ b/src/lib/apdu.rb
@@ -117,10 +117,7 @@ module APDU
def transmit(apdu)
# send APDU
- puts "< "+apdu.to_hex_disp if DEBUG
resp = transmit_apdu(apdu)
- puts "> "+resp.to_hex_disp if DEBUG
-
# parse response
response = resp[0..-3]
sw1 = resp[-2]
diff --git a/src/lib/client.rb b/src/lib/client.rb
index 3696948..1cbd142 100644
--- a/src/lib/client.rb
+++ b/src/lib/client.rb
@@ -30,8 +30,8 @@ class Client < SAP
# make the class abstract
private :initialize
- def initialize(io,verbosity=SAP::VERBOSE)
- super(io,verbosity)
+ def initialize(io)
+ super(io)
# state of the state machine
@state = nil
@@ -181,7 +181,7 @@ class Client < SAP
end
end
- # return ATR (byts array)
+ # return ATR (byte array)
def atr
if @state==:idle then
connect = create_message("TRANSFER_ATR_REQ")
@@ -191,6 +191,7 @@ class Client < SAP
until @state==:idle
sleep @wait_time
end
+ log("ATR","#{hex(@atr)}",1)
return @atr
else
raise "can not ask ATR. must be in state idle, current state : #{@state}"
@@ -201,6 +202,7 @@ class Client < SAP
# return the response of the apdu request
def apdu(request)
raise "APDU request empty" unless request and request.size>=5
+ log("APDU","< #{hex(request)}",1)
if @state==:idle then
# ["CommandAPDU",[apdu]]
connect = create_message("TRANSFER_APDU_REQ",[[0x04,request]])
@@ -210,6 +212,7 @@ class Client < SAP
until @state==:idle
sleep @wait_time
end
+ log("APDU","> #{hex(@apdu)}",1)
return @apdu
else
raise "can not sen APDU request. must be in state idle, current state : #{@state}"
diff --git a/src/lib/common.rb b/src/lib/common.rb
index 2a66837..ff2b8cf 100644
--- a/src/lib/common.rb
+++ b/src/lib/common.rb
@@ -16,8 +16,6 @@ along with SAP. If not, see <http://www.gnu.org/licenses/>.
Copyright (C) 2011 Kevin "tsaitgaist" Redon kevredon@mail.tsaitgaist.info
=end
-require 'stringio'
-
# the common part of SAP server and client
# it includes :
# - constants
@@ -155,12 +153,8 @@ class SAP
# create a new SAP client/server
# - io : the Input/Output to monitor
- def initialize(io,verbosity=VERBOSE)
+ def initialize(io)
- # the verbose output
- #@verbose = StringIO.new # no output
- @verbose = $> # std output
- @verbosity = verbosity
# this has to be defined in child class
# @socket can be any IO
@io = io
@@ -203,6 +197,7 @@ class SAP
end
end
+ # set the new state of the state machine
def set_state (new_state)
if @state then
log("state","state changed from #{@state} to #{new_state}",2)
@@ -242,7 +237,7 @@ class SAP
end
# client : ask for the ATR from SAP server (must be connected)
- # server : get ATR from SIM card
+ # server : get ATR from SIM card (or SAP result code)
# return : ATR
def atr
raise NotImplementedError
@@ -450,13 +445,13 @@ class SAP
# - 3 : inner task (green)
# - 4 : messages (red)
# - 5 : byte traffic
- VERBOSE = 5
+ $verbosity = 5 unless $verbosity
# for the logs
def log (group,message,level)
- if @verbosity and @verbosity>=level then
+ if $verbosity and $verbosity>=level then
color = 95-level
- @verbose.puts "\e[1m\e[#{color}m[#{group}]\e[0m #{message}"
+ puts "\e[1m\e[#{color}m[#{group}]\e[0m #{message}"
end
end
diff --git a/src/lib/server.rb b/src/lib/server.rb
index ea5a952..ec80f85 100644
--- a/src/lib/server.rb
+++ b/src/lib/server.rb
@@ -108,6 +108,7 @@ class Server < SAP
atr_result = atr
payload = []
if atr_result.kind_of?(Array) then
+ log("ATR","#{hex(atr_result)}",1)
# ["ResultCode",["OK, request processed correctly"]]
payload << [0x02,[0x00]]
# ["ATR",atr]
@@ -127,7 +128,9 @@ class Server < SAP
set_state :processing_apdu_request
# apdu should return APDU response byte array, or error result code
raise "no APDU request in message" unless message[:payload].size==1
+ log("APDU","> #{hex(message[:payload][0][:value])}",1)
apdu_result = apdu(message[:payload][0][:value])
+ log("APDU","< #{hex(apdu_result)}",1)
payload = []
if apdu_result.kind_of?(Array) then
# ["ResultCode",["OK, request processed correctly"]]