lib/xmodem.rb in xmodem-0.1.0 vs lib/xmodem.rb in xmodem-0.1.1
- old
+ new
@@ -1,40 +1,42 @@
# Author:: Jonno Downes (jonno@jamtronix.com)
# Contrib:: Sten Feldman (exile@chamber.ee)
#
# License:: Mozilla Public License 1.1
#
-# Doesn't seem to work for me using Ruby 2.0 - BORKEN unless fixed
-#
-# Public interface changes:
-# - ModemProtocols renamed to XMODEM
-# - xmodem_tx renamed to send
-# - xmodem_rx renamed to receive
-# - Logger outputs reflect the actors sender/receiver
require 'log4r'
include Log4r
module XMODEM
- XMODEM_BLOCK_SIZE = 128 #how many bytes (ecluding header & checksum) in each block?
XMODEM_MAX_TIMEOUTS = 5 #how many timeouts in a row before the sender gives up?
XMODEM_MAX_ERRORS = 10 #how many errors on a single block before the receiver gives up?
-
XMODEM_CRC_ATTEMPTS = 3 #how many times should receiver attempt to use CRC?
LOG_NAME='XModem'
- @timeout_seconds = 5.0 #default timeout period
+ @timeout_seconds = 5.0 #default timeout period
+ @block_size = 128 #how many bytes (excluding header & checksum) in each block?
+
#how long does the protocol wait before giving up?
def XMODEM::timeout_seconds
@timeout_seconds
end
def XMODEM::timeout_seconds=(val)
- @timeout_seconds=val
+ @timeout_seconds = val
end
+ #how long does the protocol wait before giving up?
+ def XMODEM::block_size
+ @block_size
+ end
+
+ def XMODEM::block_size=(val)
+ @block_size = val
+ end
+
# receive a file using XMODEM protocol (block size = 128 bytes)
# remote:: must be an IO object connected to an XMODEM sender
# local:: must be an IO object - the inbound file (trimmed of any final padding) will be written to this
# options:: hash of options. options are: values :crc (use 16bit CRC instead of 8 bit checksum)
# - :mode=> :crc or :checksum (default is 8 bit checksum)
@@ -101,11 +103,11 @@
if (block==expected_block) && (validity==:valid) then
local<<last_block
last_block=""
end
- XMODEM_BLOCK_SIZE.times do
+ block_size.times do
b=(receive_getbyte(remote))
data<<b
Thread.pass
end
@@ -159,11 +161,11 @@
def XMODEM::send(remote,local)
block_number=1
current_block=""
sent_eot=false
- XMODEM_BLOCK_SIZE.times do
+ block_size.times do
b=(local.eof? ? FILLER : local.getc)
current_block<<b.chr
Thread.pass
end
checksum = XMODEM::checksum(current_block)
@@ -178,11 +180,11 @@
if remote.eof? then
logger.warn "sender: unexpected eof on input"
break
end
tx_cmd=remote.getc.ord
- logger.debug "sendder: got 0x#{"%x" % tx_cmd}"
+ logger.debug "sender: got 0x#{"%x" % tx_cmd}"
if tx_cmd==ACK then
if sent_eot then
logger.debug "sender: got ACK of EOT"
break
end
@@ -193,11 +195,11 @@
sent_eot=true
next
end
block_number=((block_number+1)%0x100)
current_block=""
- XMODEM_BLOCK_SIZE.times do
+ block_size.times do
b=(local.eof? ? FILLER : local.getc)
current_block<<b
Thread.pass
end
@@ -229,21 +231,21 @@
end
#calculate an 8-bit XMODEM checksum
#this is just the sum of all bytes modulo 0x100
def XMODEM::checksum(block)
- raise RXChecksumError.new("checksum requested of invalid block {size should be #{XMODEM_BLOCK_SIZE}, was #{block.length}") unless block.length==XMODEM_BLOCK_SIZE
+ raise RXChecksumError.new("checksum requested of invalid block {size should be #{block_size}, was #{block.length}") unless block.length==block_size
checksum=0
block.each_byte do |b|
checksum = (checksum+b) % 0x100
end
checksum
end
#calculate a 16-bit CRC
def XMODEM::ccitt16_crc(block)
# cribbed from http://www.hadermann.be/blog/32/ruby-crc16-implementation/
- raise RXChecksumError.new("checksum requested of invalid block {size should be #{XMODEM_BLOCK_SIZE}, was #{block.length}") unless block.length==XMODEM_BLOCK_SIZE
+ raise RXChecksumError.new("checksum requested of invalid block {size should be #{block_size}, was #{block.length}") unless block.length==block_size
crc=0
block.each_byte{|x| crc = ((crc<<8) ^ CCITT_16[(crc>>8) ^ x])&0xffff}
crc
end