Class: Command_editor

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

Instance Method Summary collapse

Constructor Details

#initialize(action, cmd_name, chip, bus_id, parent, bus_param = {}) ⇒ Command_editor

Returns a new instance of Command_editor.



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
56
57
58
59
# File 'lib/class/Command_editor.rb', line 17

def initialize(action, cmd_name, chip, bus_id, parent, bus_param={})
  super()
  @view = Ui_Command_editor.new
  centerWindow(self)
  @view.setupUi(self)
  @bus = Bus.find(bus_id)
  @chip_id = chip.id
  @cmd_name = cmd_name
  @bus_id = bus_id
  @parent = parent
  @cmd_table = Command_table.new(@view.tbl_bytes, @bus.name)

  inputRestrict(@view.lie_name, 2)
  inputRestrict(@view.lie_description, 2)
  @view.lbl_chip_val.setText(chip.reference)
  @cmd_table.resize_to_content
  @cmd_table.build_spi if @bus.name == 'SPI'
  @view.lie_text_2_bytes.hide unless @bus.name == 'SPI'

  case action
  when 0
    # Add
    @view.lbl_cmd_val.setText('-')
    Qt::Object.connect(@view.btn_validate, SIGNAL('clicked()'), self, SLOT('add_cmd()'))
  when 1
    # Template
    @view.lbl_cmd_val.setText(cmd_name)
    feed_editor_form
    @view.lie_name.setText('')
    Qt::Object.connect(@view.btn_validate, SIGNAL('clicked()'), self, SLOT('add_cmd()'))
  else
    # Edit
    @view.lbl_cmd_val.setText(cmd_name)
    feed_editor_form
    @view.btn_validate.setText('Edit')
    Qt::Object.connect(@view.btn_validate, SIGNAL('clicked()'), self, SLOT('edit_cmd()'))
  end
  unless bus_param.empty?
    feed_i2c_cmd_array(bus_param)
  end
rescue Exception => msg
  ErrorMsg.new.unknown(msg)
end

Instance Method Details

#add_bytesObject



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
# File 'lib/class/Command_editor.rb', line 104

def add_bytes
  @view.tbl_bytes.sortItems(0, Qt::AscendingOrder)
  @view.tbl_bytes.rowCount.times do |i|
    if @bus.name == 'SPI'
      iteration = @view.tbl_bytes.item(i, 2).text
      description = @view.tbl_bytes.item(i, 3).text
    else
      iteration = nil
      description = @view.tbl_bytes.item(i, 2).text
    end
    byte = Byte.create(
      index:       i.next,
      value:       @view.tbl_bytes.item(i, 1).text,
      iteration:   iteration,
      description: description,
      command_id:  @cmd.id
    )
    return false if check_for_errors(byte)
  end
  Qt::MessageBox.new(
    Qt::MessageBox::Information,
    'Command status',
    'Command saved'
  ).exec
rescue Exception => msg
  ErrorMsg.new.unknown(msg)
end

#add_cmdObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/class/Command_editor.rb', line 69

def add_cmd
  return false if @cmd_table.empty_data_exist? || is_cmd_size_valid? == false
  @cmd = Command.create(
    name:        @view.lie_name.text,
    description: @view.lie_description.text,
    bus_id:      @bus_id,
    chip_id:     @chip_id
  )
  unless check_for_errors(@cmd)
    add_bytes
    @parent.feed_cmd_array
    close
  end
rescue Exception => msg
  ErrorMsg.new.unknown(msg)
end

#add_rowObject



200
201
202
203
204
205
206
207
# File 'lib/class/Command_editor.rb', line 200

def add_row
  if @view.lie_text_2_bytes.text.empty?
    @cmd_table.add_row
  else
    @cmd_table.add_text_rows(@view.lie_text_2_bytes.text)
    @view.lie_text_2_bytes.setText('')
  end
end

#check_cell_content(item) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/class/Command_editor.rb', line 132

def check_cell_content(item)
  case @view.tbl_bytes.horizontalHeaderItem(item.column).text
  when 'Order', 'Repetition'
    if item.text.to_i < 0
      item.setData(0, Qt::Variant.new(0))
      ErrorMsg.new.positive_cell_value
    end
  when 'Byte (Hexa)'
    reg = Qt::RegExp.new("^[A-Fa-f0-9]{2}")
    reg_val = Qt::RegExpValidator.new(reg, self)
    if reg_val.validate(item.text, item.text.length) == 0
      item.setText('')
      ErrorMsg.new.hexa_cell_value
    end
  when 'Description'
    unless item.text.nil?
      reg = Qt::RegExp.new("^[a-zA-Z0-9_@-\s]+$")
      reg_val = Qt::RegExpValidator.new(reg, self)
      if reg_val.validate(item.text, item.text.length) == 0
        item.setText('')
        ErrorMsg.new.char_cell_value
      end
    end
  end
rescue Exception => msg
  ErrorMsg.new.unknown(msg)
end

#clone_rowObject



209
210
211
# File 'lib/class/Command_editor.rb', line 209

def clone_row
  @cmd_table.clone_rows
end

#edit_cmdObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/class/Command_editor.rb', line 86

def edit_cmd
  return false if @cmd_table.empty_data_exist? && !is_cmd_size_valid?
  @cmd = Command.find_by(name: @cmd_name, chip_id: @chip_id)
  @cmd.update(
    name: @view.lie_name.text,
    description: @view.lie_description.text
  )
  @view.tbl_bytes.sortItems(0, Qt::AscendingOrder)
  unless check_for_errors(@cmd)
    Byte.where(command_id: @cmd.id).destroy_all
    add_bytes
    @parent.feed_cmd_array
    close
  end
rescue Exception => msg
  ErrorMsg.new.unknown(msg)
end

#feed_editor_formObject



191
192
193
194
195
196
197
198
# File 'lib/class/Command_editor.rb', line 191

def feed_editor_form
  cmd = Command.find_by(name: @cmd_name)
  @view.lie_name.setText(@cmd_name)
  @view.lie_description.setText(cmd.description)
  @cmd_table.fill_byte_table(cmd.bytes)
rescue Exception => msg
  ErrorMsg.new.unknown(msg)
end

#feed_i2c_cmd_array(bus_param) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/class/Command_editor.rb', line 61

def feed_i2c_cmd_array(bus_param)
  if bus_param[:mode] == 'w'
    @cmd_table.i2c_write_cmd(bus_param[:addr], bus_param[:size].to_i)
  else
    @cmd_table.i2c_read_cmd(bus_param[:addr], bus_param[:size].to_i)
  end
end

#is_cmd_size_valid?Boolean

Returns:

  • (Boolean)


160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/class/Command_editor.rb', line 160

def is_cmd_size_valid?
  byte_table_size = @view.tbl_bytes.rowCount
  case @bus_name
  when 'SPI'
    cmd_size =  byte_table_size + @cmd_table.count_total_repetition
    return true if cmd_size < 4000
    return ErrorMsg.new.spi_cmd_too_long
  when 'I2C'
    count = i = 0
    while i <= byte_table_size - 1 do
      low_byte     = @view.tbl_bytes.item(i, 1)
      high_byte    = @view.tbl_bytes.item(i + 1, 1)
      command_byte = @view.tbl_bytes.item(i + 2, 1)
      return ErrorMsg.new.lowbyte_missing  if low_byte.nil?
      return ErrorMsg.new.highbyte_missing if high_byte.nil?
      return ErrorMsg.new.mode_missing     if command_byte.nil?
      current_count = HardsploitAPI.BytesToInt(
        lByte: low_byte.text.to_i(16),
        hByte: high_byte.text.to_i(16)
      )
      count += current_count
      (command_type.to_i(16) % 2).zero? ? i += current_count + 3 : i += 3
    end
    return ErrorMsg.new.size_neq_row_number unless i == byte_table_size
    return ErrorMsg.new.i2c_cmd_too_long    if count > 2000
    return true
  end
rescue Exception => msg
  ErrorMsg.new.unknown(msg)
end

#remove_rowObject



213
214
215
# File 'lib/class/Command_editor.rb', line 213

def remove_row
  @cmd_table.remove_rows
end