Class: Uart_console

Inherits:
Qt::Widget
  • Object
show all
Defined in:
lib/class/uart/Uart_console.rb

Instance Method Summary collapse

Constructor Details

#initialize(chip) ⇒ Uart_console

Returns a new instance of Uart_console.



19
20
21
22
23
24
25
26
27
28
# File 'lib/class/uart/Uart_console.rb', line 19

def initialize(chip)
  super()
  Firmware.new('UART')
  @view = Ui_Uart_console.new
  centerWindow(self)
  @view.setupUi(self)
  @view.lbl_chip.setText(chip.reference)
  @chip = chip
  @data = ''
end

Instance Method Details

#clear_consoleObject



99
100
101
102
# File 'lib/class/uart/Uart_console.rb', line 99

def clear_console
  @view.console.clear
  @data = ''
end

#closeEvent(event) ⇒ Object



30
31
32
# File 'lib/class/uart/Uart_console.rb', line 30

def closeEvent(event)
  @timer.killTimer(@timer.timerId) unless @timer.nil?
end

#connectObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/class/uart/Uart_console.rb', line 38

def connect
  @uart = HardsploitAPI_UART.new(
    baud_rate:        @chip.uart_setting.baud_rate,
    word_width:       @chip.uart_setting.word_size,
    use_parity_bit:   @chip.uart_setting.parity_bit,
    parity_type:      @chip.uart_setting.parity_type,
    nb_stop_bits:     @chip.uart_setting.stop_bits_nbr,
    idle_line_level:  @chip.uart_setting.idle_line
  )
  @timer = Qt::Timer.new
  Qt::Object.connect(@timer, SIGNAL('timeout()'), self, SLOT('update()'))
  @timer.start(100)
  @data << 'Listening and ready to go !' + 0x0d.chr
  @view.console.setPlainText(@data)
  scroll_down
  sender.setEnabled(false)
  @view.btn_disconnect.setEnabled(true)
end

#disconnectObject



57
58
59
60
61
62
63
64
# File 'lib/class/uart/Uart_console.rb', line 57

def disconnect
  @timer.killTimer(@timer.timerId)
  @data << 'Disconnected' + 0x0d.chr
  @view.console.setPlainText(@data)
  scroll_down
  sender.setEnabled(false)
  @view.btn_connect.setEnabled(true)
end

#keyPressEvent(event) ⇒ Object



34
35
36
# File 'lib/class/uart/Uart_console.rb', line 34

def keyPressEvent(event)
  send if event.key() == Qt::Key_Enter or event.key() == Qt::Key_Return
end

#open_settingsObject



104
105
106
107
108
# File 'lib/class/uart/Uart_console.rb', line 104

def open_settings
  settingsUART = Uart_settings.new(@chip)
  settingsUART.setWindowModality(Qt::ApplicationModal)
  settingsUART.show
end

#scroll_downObject



110
111
112
113
114
# File 'lib/class/uart/Uart_console.rb', line 110

def scroll_down
  sb = Qt::ScrollBar.new
  sb = @view.console.verticalScrollBar
  sb.setValue(sb.maximum())
end

#sendObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/class/uart/Uart_console.rb', line 66

def send
  return false if @uart.nil? or @timer.nil?
  return ErrorMsg.new.ascii_only unless @view.lie_cmd.text.ascii_only?
  packet = []
  @view.lie_cmd.text.each_byte do |x|
    packet.push x
  end
  packet.push 13 if @chip.uart_setting.return_type == 0
  packet.push 10 if @chip.uart_setting.return_type == 1
  if @chip.uart_setting.return_type == 2
    packet.push 13
    packet.push 10
  end
  @view.lie_cmd.clear
@uart.write(payload: packet)
#Rescue
end

#updateObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/class/uart/Uart_console.rb', line 84

def update
  received = @uart.sendAndReceived.collect{|i| i.chr}
  unless received.empty?
    @data << "#{received.join('')}"
    @view.console.setPlainText(@data)
  end
  scroll_down
rescue HardsploitAPI::ERROR::HARDSPLOIT_NOT_FOUND
ErrorMsg.new.hardsploit_not_found
rescue HardsploitAPI::ERROR::USB_ERROR
 ErrorMsg.new.usb_error
rescue Exception => msg
  ErrorMsg.new.unknown(msg)
end