Class: Generic_commands

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

Instance Method Summary collapse

Constructor Details

#initialize(api, chip, bus_name) ⇒ Generic_commands

Returns a new instance of Generic_commands.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/class/Generic_commands.rb', line 21

def initialize(api, chip, bus_name)
  super()
  @generic_command_gui = Ui_Generic_commands.new
  centerWindow(self)
  @generic_command_gui.setupUi(self)
  @generic_command_gui.lbl_chip.setText(chip.chip_reference)
  @generic_command_gui.lbl_search.setPixmap(Qt::Pixmap.new('images/search.png'))
  inputRestrict(@generic_command_gui.lie_search, 2)
  @generic_command_gui.check_result.setChecked(true)
  @api = api
  @chip = chip
  @bus_name = bus_name
  @bus_id = Bus.find_by(bus_name: bus_name).bus_id
  select_chip_settings(bus_name)
  feed_cmd_array
end

Instance Method Details

#check_concatenation_size(bytesCmd1, bytesCmd2) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/class/Generic_commands.rb', line 212

def check_concatenation_size(bytesCmd1, bytesCmd2)
  checkSize = []
  bytesCmd1.each do |b1|
    checkSize.push(b1.byte_value)
  end
  bytesCmd2.each do |b2|
    checkSize.push(b2.byte_value)
  end
  count = 0
  i = 0
  while i <= (checkSize.size) - 1 do
    lowByte = checkSize[i]
    highByte = checkSize[i + 1]
    commandType = checkSize[i + 2]
    count += (lowByte.to_i(16) + (highByte.to_i(16)<<8))
    if commandType.to_i(16) % 2 == 0 #WRITE
      i += ((lowByte.to_i(16) + (highByte.to_i(16)<<8)) + 3)
    else #READ
      i = (i + 3)
    end
  end
  if count > 2000
    Qt::MessageBox.new(Qt::MessageBox::Critical, 'Critical error', 'Command too big: unable to concatenate').exec
    return false
  end
  return true
end

#check_send_and_received_data(value) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/class/Generic_commands.rb', line 258

def check_send_and_received_data(value)
  case value
  when HardsploitAPI::USB_STATE::PACKET_IS_TOO_LARGE
    Qt::MessageBox.new(Qt::MessageBox::Critical, 'Critical error', "PACKET_IS_TOO_LARGE max: #{HardsploitAPI::USB::USB_TRAME_SIZE}").exec
  when HardsploitAPI::USB_STATE::ERROR_SEND
    Qt::MessageBox.new(Qt::MessageBox::Critical, 'Critical error', 'ERROR_SEND').exec
  when HardsploitAPI::USB_STATE::BUSY
    Qt::MessageBox.new(Qt::MessageBox::Warning, 'BUSY', 'Device busy').exec
  else
    return value
  end
end

#concatenate_cmdsObject



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/class/Generic_commands.rb', line 170

def concatenate_cmds
  if @generic_command_gui.tbl_cmd.selectedItems.count != 2
    Qt::MessageBox.new(Qt::MessageBox::Critical, 'Wrong selection', 'Select two commands in the table to concatenate them').exec
    return 0
  end
  bytesCmd1 = Byte.where(byte_cmd: Cmd.find_by(cmd_name: @generic_command_gui.tbl_cmd.selectedItems[0].text).cmd_id)
  bytesCmd2 = Byte.where(byte_cmd: Cmd.find_by(cmd_name: @generic_command_gui.tbl_cmd.selectedItems[1].text).cmd_id)
  if check_concatenation_size(bytesCmd1, bytesCmd2)
    # Save cmd
    cmd = Cmd.new
    cmd.cmd_name = 'New concatenation'
    cmd.cmd_desc = "Concatenation of #{@generic_command_gui.tbl_cmd.selectedItems[0].text} and #{@generic_command_gui.tbl_cmd.selectedItems[1].text} commands"
    cmd.cmd_bus = @bus_id
    cmd.cmd_chip = @chip.chip_id
    cmd.save
    # Save cmd bytes
    bytesCmd1.each do |b1|
      byte = Byte.new
      byte.byte_index = b1.byte_index
      byte.byte_value = b1.byte_value
      byte.byte_description = b1.byte_description
      byte.byte_iteration = b1.byte_iteration
      byte.byte_cmd = Cmd.ids.last
      byte.save
    end
    bytesCmd2.each do |b2|
      byte2 = Byte.new
      byte2.byte_index = Byte.last.byte_index + 1
      byte2.byte_value = b2.byte_value
      byte2.byte_description = b2.byte_description
      byte2.byte_iteration = b2.byte_iteration
      byte2.byte_cmd = Cmd.ids.last
      byte2.save
    end
    feed_cmd_array
  end
rescue Exception => msg
  logger = Logger.new($logFilePath)
  logger.error msg
  Qt::MessageBox.new(Qt::MessageBox::Critical, 'Critical error', 'Error occured when concatenating the command. Consult the log for more details').exec
end

#delete_cmdObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/class/Generic_commands.rb', line 153

def delete_cmd
  msg = Qt::MessageBox.new
  msg.setWindowTitle('Delete command')
  msg.setText('Confirm the delete command action ?')
  msg.setIcon(Qt::MessageBox::Question)
  msg.setStandardButtons(Qt::MessageBox::Cancel | Qt::MessageBox::Ok)
  msg.setDefaultButton(Qt::MessageBox::Cancel)
  if msg.exec == Qt::MessageBox::Ok
    @chip.cmd.find_by(cmd_name: @generic_command_gui.tbl_cmd.currentItem.text).destroy
    feed_cmd_array
  end
rescue Exception => msg
  logger = Logger.new($logFilePath)
  logger.error msg
  Qt::MessageBox.new(Qt::MessageBox::Critical, 'Critical error', 'Error occured when deleting the command. Consult the log for more details').exec
end

#exec_actionObject

Execute action



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/class/Generic_commands.rb', line 89

def exec_action
  if @generic_command_gui.tbl_cmd.currentItem.nil?
    Qt::MessageBox.new(Qt::MessageBox::Critical, 'Missing command', 'Select a command in the array first').exec
    return 0
  end
  case @generic_command_gui.cbx_action.currentText
  when 'Execute'
    cmd_array = prepare_cmd
    result = exec_cmd(@bus_name, cmd_array)
    if @generic_command_gui.check_result.isChecked
      export_manager = Export_manager.new(@bus_name, result, cmd_array)
      export_manager.setWindowModality(Qt::ApplicationModal)
      export_manager.show
    end
  when 'Template'
    open_cmd_form(:option_1 => 'temp')
  when 'Edit'
    open_cmd_form(:option_1 => 'edit')
  when 'Concatenate'
    concatenate_cmds
  when 'Delete'
    delete_cmd
  else
    Qt::MessageBox.new(Qt::MessageBox::Critical, 'Wrong option', 'Please choose a correct action').exec
  end
rescue Exception => msg
  Qt::MessageBox.new(Qt::MessageBox::Critical, 'Critical error', 'Error occured while executing the action. Consult the logs for more details').exec
  logger = Logger.new($logFilePath)
  logger.error msg
end

#exec_cmd(bus, array_sent) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/class/Generic_commands.rb', line 139

def exec_cmd(bus, array_sent)
    Firmware.new(@api, @bus_name)
    case bus
    when 'SPI'
      return check_send_and_received_data(@api.spi_Interact(@chip_settings.spi_mode, @speeds[@chip_settings.spi_frequency], array_sent))
    when 'I2C'
      return check_send_and_received_data(@api.i2c_Interact(@chip_settings.i2c_frequency, array_sent))
    end
rescue Exception => msg
  logger = Logger.new($logFilePath)
  logger.error msg
  Qt::MessageBox.new(Qt::MessageBox::Critical, 'Critical error', 'Error occured when executing the command. Consult the log for more details').exec
end

#feed_cmd_arrayObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/class/Generic_commands.rb', line 38

def feed_cmd_array
  @generic_command_gui.tbl_cmd.clearContents
  cmd = @chip.cmd.where(cmd_bus: @bus_id)
  unless @generic_command_gui.lie_search.text.empty?
    cmd = cmd.where('cmd_name LIKE ?', "%#{@generic_command_gui.lie_search.text}%")
  end
  @generic_command_gui.tbl_cmd.setRowCount(cmd.count);
  cmd.to_enum.with_index(0).each do |c, i|
    it1 = Qt::TableWidgetItem.new(c.cmd_name)
    it1.setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled)
    it2 =  Qt::TableWidgetItem.new(c.cmd_desc)
    it2.setFlags(Qt::ItemIsEnabled)
    @generic_command_gui.tbl_cmd.setItem(i, 0, it1);
    @generic_command_gui.tbl_cmd.setItem(i, 1, it2);
  end
  @generic_command_gui.tbl_cmd.resizeColumnsToContents
  @generic_command_gui.tbl_cmd.resizeRowsToContents
  @generic_command_gui.tbl_cmd.horizontalHeader.stretchLastSection = true
rescue Exception => msg
  logger = Logger.new($logFilePath)
  logger.error msg
  Qt::MessageBox.new(Qt::MessageBox::Critical, 'Critical error', 'Error while loading the command array. Consult the log for more details').exec
end

#open_cmd_form(options = {}) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/class/Generic_commands.rb', line 240

def open_cmd_form(options={})
    if options[:option_1].nil?
      if @bus_name == 'I2C'
        cmdBase = I2c_command.new(@api, @chip, @bus_id, self)
      else
        cmdBase = Command_editor.new(0, nil, @chip, @bus_id, self, @api)
      end
    else
      if options[:option_1] == 'temp'
        cmdBase = Command_editor.new(1, @generic_command_gui.tbl_cmd.currentItem.text, @chip, @bus_id, self, @api)
      else
        cmdBase = Command_editor.new(2, @generic_command_gui.tbl_cmd.currentItem.text, @chip, @bus_id, self, @api)
      end
    end
  cmdBase.setWindowModality(Qt::ApplicationModal)
  cmdBase.show
end

#prepare_cmdObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/class/Generic_commands.rb', line 120

def prepare_cmd
  byte_list = Byte.where(byte_cmd: Cmd.find_by(cmd_name: @generic_command_gui.tbl_cmd.currentItem.text))
  array_sent = Array.new
  byte_list.each do |bl|
    if bl.byte_iteration != 0 && !bl.byte_iteration.nil?
      for i in 1..bl.byte_iteration.to_i do
        array_sent.push(bl.byte_value.to_i(16))
      end
    else
      array_sent.push(bl.byte_value.to_i(16))
    end
  end
  return array_sent
rescue Exception => msg
  logger = Logger.new($logFilePath)
  logger.error msg
  Qt::MessageBox.new(Qt::MessageBox::Critical, 'Critical error', 'Error occured when preparing the command. Consult the log for more details').exec
end

#select_chip_settings(bus) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/class/Generic_commands.rb', line 62

def select_chip_settings(bus)
  case bus
  when 'SPI'
    @chip_settings = Spi.find_by(spi_chip: @chip.chip_id)
    @speeds = {
      '25.00' => 3,
      '18.75' => 4,
      '15.00' => 5,
      '12.50' => 6,
      '10.71' => 7,
      '9.38' => 8,
      '7.50' => 10,
      '5.00' => 15,
      '3.95' => 19,
      '3.00' => 25,
      '2.03' => 37,
      '1.00' => 75,
      '0.50' => 150,
      '0.29' => 255
    }
  when 'I2C'
    @chip_settings = I2C.find_by(i2c_chip: @chip.chip_id)
    @generic_command_gui.cbx_action.insertItem(3, 'Concatenate')
  end
end