Class: Meshtastic::Admin::Firmware::BLE::Stream

Inherits:
Object
  • Object
show all
Defined in:
lib/meshtastic/admin/firmware/ble.rb

Instance Method Summary collapse

Constructor Details

#initialize(backend) ⇒ Stream

Returns a new instance of Stream.



152
153
154
155
# File 'lib/meshtastic/admin/firmware/ble.rb', line 152

def initialize(backend)
  @backend = backend
  @buffer = +''.b
end

Instance Method Details

#command(bytes) ⇒ Object



157
158
159
160
161
162
163
# File 'lib/meshtastic/admin/firmware/ble.rb', line 157

def command(bytes)
  offset = 0
  while offset < bytes.bytesize
    @backend.write(uuid: OTA_UUID, bytes: bytes.byteslice(offset, 20), response: true)
    offset += 20
  end
end

#lineObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/meshtastic/admin/firmware/ble.rb', line 165

def line
  loop do
    if (index = @buffer.index("\n"))
      raise IOError, 'Oversized OTA response' if index > 511

      return @buffer.slice!(0, index + 1).chomp
    end
    raise IOError, 'Oversized OTA response' if @buffer.bytesize > 512

    event = @backend.notification(timeout: 120)
    raise IOError, 'OTA disconnected before confirmation' unless event
    raise IOError, 'Unexpected OTA notification UUID' unless event[:uuid].to_s.casecmp?(TX_UUID)

    raise IOError, 'Invalid OTA notification bytes' unless event[:bytes].is_a?(String) && !event[:bytes].empty?

    @buffer << event.fetch(:bytes)
  end
end