Class: CMUX::MUX

Inherits:
Object
  • Object
show all
Defined in:
lib/cmux/mux.rb

Instance Method Summary collapse

Constructor Details

#initialize(device) ⇒ MUX

Returns a new instance of MUX.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cmux/mux.rb', line 3

def initialize(device)
  begin
    @connection = CMUX::Connection.new
    @connection.open device

    io = CMUX::IO.open_tty device
    begin
      # Magic!

      # Modem will lose synchronization if previous session was interrupted
      # in the middle of frame, and it will resync only at start of second
      # frame.
      2.times do
        # Send 'AT' to ensure proper autobaud after DTR rise in the AT mode.
        io.write "AT\r"

        # Terminate GSM 07.10 simple multiplexer.
        # Will be ignored by modem in the AT mode.
        io.write "\xf9\x03\xef\x05\xc3\x01\xf2\xf9"
        io.flush

        # Read any crap from the buffer
        until ::IO.select([ io ], [], [], 0.2).nil?
          io.read_nonblock 2048
        end
      end

      chatter = CMUX::ModemChatter.new io

      all_done = false

      chatter.command("&D2+IPR=115200;+CMUX=0", 5) do |resp|
        if resp.failure?
          raise "CMUX failed: #{resp.error}"
        else
          all_done = true
        end
      end

      until all_done
        CMUX::ModemChatter.poll [ chatter ]
      end
    ensure
      io.close
    end

    @connection.activate
  rescue Exception => e
    @connection.close

    raise e
  end
end

Instance Method Details

#allocate(channel) ⇒ Object



61
62
63
# File 'lib/cmux/mux.rb', line 61

def allocate(channel)
  Channel.new @connection.open_port(channel), channel, self
end

#closeObject



57
58
59
# File 'lib/cmux/mux.rb', line 57

def close
  @connection.close
end

#close_port(channel) ⇒ Object



65
66
67
# File 'lib/cmux/mux.rb', line 65

def close_port(channel)
  @connection.close_port channel
end