Sha256: 33ee797acb48cbb77f0417ab300aa8f94d35093a4f79300d65f739dea4b4eac1

Contents?: true

Size: 1.47 KB

Versions: 2

Compression:

Stored size: 1.47 KB

Contents

module Wifly
  class Connection
    attr_accessor :address, :port, :version
  
    ##
    # address => the hostname or IP address of the wifly device
    # port =>    the port for communicating with the wifly
    # version => the firmware version of the device
    def initialize(address, port, version)
      self.address = address
      self.port    = port
      self.version = version
    end

    ##
    # str =>  the command to send to the wifly, without any carriage return
    # [return_len] => the expected length of the return string; defaults to 0
    #
    # The wifly will echo back the command (with carriage return)
    # along with another CRLF and the command prompt string.
    # Something like "lites\r\r\n<2.32> "
    # Since the string has a predictable length, we can do a blocking read.
    #
    def send_command(str, return_len=0)
      str += "\r"
      socket.write(str)
      expected_return_length = str.length + "\r\n#{prompt}".length + return_len
      socket.read(expected_return_length).gsub(prompt,'')
    rescue Errno::EPIPE # connection closed on the client end
      initialize_socket
      retry
    end

    def close
      socket.close
    end

    def socket
      @socket ||= initialize_socket
    end
    
    private
    def prompt
      "<#{version}> "
    end

    def initialize_socket
      sock = Socket.tcp(address, port)
      sock.write(COMMAND_MODE) # enter command mode 
      sock.read(HELLO.length)  # read off the response
      sock
    end 
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
wifly-0.0.5 lib/wifly/connection.rb
wifly-0.0.4 lib/wifly/connection.rb