Class: Generic_commands

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

Instance Method Summary collapse

Constructor Details

#initialize(chip, bus_name) ⇒ Generic_commands

Returns a new instance of Generic_commands.



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

def initialize(chip, bus_name)
  super()
  @view = Ui_Generic_commands.new
  centerWindow(self)
  @view.setupUi(self)
  @view.lbl_chip.setText(chip.reference)
  @view.lbl_search.setPixmap(Qt::Pixmap.new('images/search.png'))
  inputRestrict(@view.lie_search, 2)
  @chip = chip
  @bus_name = bus_name
  @bus_id = Bus.find_by(name: bus_name).id
  select_chip_settings(bus_name)
  feed_cmd_array
end

Instance Method Details

#check_concatenation_size(bytes_cmd_1, bytes_cmd_2) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/class/Generic_commands.rb', line 229

def check_concatenation_size(bytes_cmd_1, bytes_cmd_2)
  check_size = []
  bytes_cmd_1.each do |b1|
    check_size.push(b1.value)
  end
  bytes_cmd_2.each do |b2|
    check_size.push(b2.value)
  end
  count = 0
  i = 0
  while i <= (check_size.size) - 1 do
    lowByte = check_size[i]
    highByte = check_size[i + 1]
    commandType = check_size[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

#concatenateObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/class/Generic_commands.rb', line 97

def concatenate
  return ErrorMsg.new.concat_disallow unless @bus_name == "I2C"
  return ErrorMsg.new.concat_nbr unless @view.tbl_cmd.selectedItems.count == 2
  cmd1 = Command.find_by(name: @view.tbl_cmd.selectedItems[0].text)
  cmd2 = Command.find_by(name: @view.tbl_cmd.selectedItems[1].text)
  return false unless check_concatenation_size(cmd1.bytes, cmd2.bytes)

  cmd_1 = @view.tbl_cmd.selectedItems[0].text
  cmd_2 = @view.tbl_cmd.selectedItems[1].text
  cmd = Command.create(
    name:        'New concatenation',
    description: "Concatenation of #{cmd1.name} and #{cmd2.name} commands",
    bus_id:      @bus_id,
    chip_id:     @chip.id
  )
  unless check_for_errors(cmd)
    cmd1.bytes.each do |b1|
      byte1 = Byte.create(
        index:        b1.index,
        value:        b1.value,
        description:  b1.description,
        iteration:    b1.iteration,
        command_id:   Command.last.id
      )
      check_for_errors(byte1)
    end
    cmd2.bytes.each do |b2|
      byte2 = Byte.create(
        index:        Byte.last.index + 1,
        value:        b2.value,
        description:  b2.description,
        iteration:    b2.iteration,
        command_id:   Command.last.id
      )
      check_for_errors(byte2)
    end
    feed_cmd_array
  end
end

#contextMenuEvent(event) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/class/Generic_commands.rb', line 35

def contextMenuEvent(event)
  if @view.tbl_cmd.currentItem.nil?
    return false
  else
    return false if @view.tbl_cmd.currentItem.column == 1
    menu = Qt::Menu.new(self)
    menu.addAction(@view.actionExecute)
    menu.addAction(@view.actionEdit)
    menu.addAction(@view.actionTemplate)
    menu.addAction(@view.actionDelete)
    menu.addAction(@view.actionConcatenate)
    menu.exec(event.globalPos())
  end
end

#createObject



62
63
64
65
66
67
68
69
# File 'lib/class/Generic_commands.rb', line 62

def create
  if @bus_name == 'I2C'
    cmdBase = I2c_command.new(@chip, @bus_id, self)
  else
    cmdBase = Command_editor.new(0, nil, @chip, @bus_id, self)
  end
  cmdBase.show
end

#deleteObject



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/class/Generic_commands.rb', line 83

def delete
  return ErrorMsg.new.no_cmd_selected if @view.tbl_cmd.currentItem.nil?
  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.commands.find_by(name: @view.tbl_cmd.currentItem.text).destroy
    feed_cmd_array
  end
end

#editObject



71
72
73
74
75
# File 'lib/class/Generic_commands.rb', line 71

def edit
  return ErrorMsg.new.no_cmd_selected if @view.tbl_cmd.currentItem.nil?
  cmdBase = Command_editor.new(2, @view.tbl_cmd.currentItem.text, @chip, @bus_id, self)
  cmdBase.show
end

#exec_cmd(bus, array_sent) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/class/Generic_commands.rb', line 199

def exec_cmd(bus, array_sent)
  Firmware.new(bus)
  case bus
  when 'SPI'
    spi = HardsploitAPI_SPI.new(
      speed: @speeds[@chip.spi_setting.frequency],
      mode:  @chip.spi_setting.mode
    )
    return spi.spi_Interact(payload: array_sent)
  when 'I2C'
    if [40, 100, 400, 1000].include?(@chip.i2c_setting.frequency)
      speed = 0 if @chip.i2c_setting.frequency == 100
      speed = 1 if @chip.i2c_setting.frequency == 400
      speed = 2 if @chip.i2c_setting.frequency == 1000
      speed = 3 if @chip.i2c_setting.frequency == 40
      i2c = HardsploitAPI_I2C.new(speed: speed)
    end
    return i2c.i2c_Interact(payload: array_sent)
  end
rescue HardsploitAPI::ERROR::HARDSPLOIT_NOT_FOUND
  ErrorMsg.new.hardsploit_not_found
  return false
rescue HardsploitAPI::ERROR::USB_ERROR
  ErrorMsg.new.usb_error
  return false
rescue Exception => msg
  ErrorMsg.new.unknown(msg)
  return false
end

#executeObject



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/class/Generic_commands.rb', line 50

def execute
  return ErrorMsg.new.hardsploit_not_found unless HardsploitAPI.getNumberOfBoardAvailable > 0
  return ErrorMsg.new.no_cmd_selected if @view.tbl_cmd.currentItem.nil?
  cmd_array = prepare_cmd
  result = exec_cmd(@bus_name, cmd_array)
  if @view.check_result.isChecked && result != false
    export_manager = Export_manager.new(@bus_name, result, cmd_array)
    export_manager.setWindowModality(Qt::ApplicationModal)
    export_manager.show
  end
end

#feed_cmd_arrayObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/class/Generic_commands.rb', line 137

def feed_cmd_array
  @view.tbl_cmd.clearContents
  cmd = @chip.commands.where(bus_id: @bus_id)
  unless @view.lie_search.text.empty?
    cmd = cmd.where('name LIKE ?', "%#{@view.lie_search.text}%")
  end
  @view.tbl_cmd.setRowCount(cmd.count);
  cmd.to_enum.with_index(0).each do |c, i|
    it1 = Qt::TableWidgetItem.new(c.name)
    it1.setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled)
    it2 =  Qt::TableWidgetItem.new(c.description)
    it2.setFlags(Qt::ItemIsEnabled)
    @view.tbl_cmd.setItem(i, 0, it1);
    @view.tbl_cmd.setItem(i, 1, it2);
  end
  @view.tbl_cmd.resizeColumnsToContents
  @view.tbl_cmd.resizeRowsToContents
  @view.tbl_cmd.horizontalHeader.stretchLastSection = true
rescue Exception => msg
  ErrorMsg.new.unknown(msg)
end

#prepare_cmdObject



184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/class/Generic_commands.rb', line 184

def prepare_cmd
  byte_list = Byte.where(command_id: Command.find_by(name: @view.tbl_cmd.currentItem.text))
  packet = []
  byte_list.each do |bl|
    unless bl.iteration == 0 && bl.iteration.nil?
      packet.push(bl.value.to_i(16))
    else
      for i in 1..bl.iteration.to_i do
        packet.push(bl.value.to_i(16))
      end
    end
  end
  return packet
end

#select_chip_settings(bus) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/class/Generic_commands.rb', line 159

def select_chip_settings(bus)
  case bus
  when 'SPI'
    @chip_settings = SpiSetting.find_by(chip_id: @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 = @chip.i2c_setting
  end
end

#templateObject



77
78
79
80
81
# File 'lib/class/Generic_commands.rb', line 77

def template
  return ErrorMsg.new.no_cmd_selected if @view.tbl_cmd.currentItem.nil?
  cmdBase = Command_editor.new(1, @view.tbl_cmd.currentItem.text, @chip, @bus_id, self)
  cmdBase.show
end