Class: Kanrisuru::Core::Dmi::Parser::Dmi

Inherits:
Object
  • Object
show all
Defined in:
lib/kanrisuru/core/dmi/parsers/dmi.rb

Class Method Summary collapse

Class Method Details

.dmi_append_field(struct, field, value) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/kanrisuru/core/dmi/parsers/dmi.rb', line 75

def dmi_append_field(struct, field, value)
  field = dmi_field_translate(struct, field)
  field = field.to_sym

  if struct.respond_to?(field)
    case struct.dmi_type
    when 'OEM Strings'
      if struct.strings
        struct[field] << value
      else
        struct.strings = [value]
      end
    else
      struct[field] = value
    end
  else
    Kanrisuru.logger.warn("Field does not exist for: #{struct.dmi_type}: #{field} => #{value}")
  end
end

.dmi_field_translate(struct, field) ⇒ Object



95
96
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
# File 'lib/kanrisuru/core/dmi/parsers/dmi.rb', line 95

def dmi_field_translate(struct, field)
  field = dmi_scrub_field(field)
  case struct.dmi_type
  when 'Memory Device'
    case field
    when 'size'
      field = 'mem_size'
    end
  when 'System Slots'
    case field
    when 'length'
      field = 'slot_length'
    end
  when 'OEM Strings'
    case field
    when /^string/
      field = 'strings'
    end
  when 'Boot Integrity Services'
    case field
    when '16_bit_entry_point_address'
      field = 'sixteen_bit_entry_point_address'
    when '32_bit_entry_point_address'
      field = 'thirty_two_bit_entry_point_address'
    end
  end

  field
end

.dmi_scrub_field(field) ⇒ Object



125
126
127
128
129
130
# File 'lib/kanrisuru/core/dmi/parsers/dmi.rb', line 125

def dmi_scrub_field(field)
  field = field.downcase
  field = field.gsub(/\s/, '_')
  field = field.gsub('-', '_')
  field.gsub(':', '')
end

.dmi_type_to_struct(type) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/kanrisuru/core/dmi/parsers/dmi.rb', line 132

def dmi_type_to_struct(type)
  type =
    case type
    when '32-bit Memory Error'
      'Memory Error 32 Bit'
    when '64-bit Memory Error'
      'Memory Error 64 Bit'
    else
      type
    end

  type_camelized = Kanrisuru::Util.camelize(type.gsub(/\s/, ''))
  struct_class = Kanrisuru::Core::Dmi.const_get(type_camelized)
  struct_class.new
end

.parse(command) ⇒ Object



9
10
11
12
13
14
15
16
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/kanrisuru/core/dmi/parsers/dmi.rb', line 9

def parse(command)
  lines = command.to_a

  rows = []
  current_struct = nil
  wrapped_field_line = nil

  lines.each do |line|
    next if Kanrisuru::Util.blank?(line)

    case line
    when /^Handle/
      if current_struct
        rows << current_struct

        current_struct = nil
        wrapped_field_line = nil
      end

      values = line.split(', ')
      handle = values[0].split('Handle ')[1]

      type = values[1].split(/(\d+)/)[1].to_i
      type = Kanrisuru::Util::DmiType[type]
      next if Kanrisuru::Util.blank?(type)

      bytes = values[2].split(/(\d+)/)[1]

      current_struct = dmi_type_to_struct(type)
      current_struct.dmi_handle = handle
      current_struct.dmi_type = type
      current_struct.dmi_size = bytes.to_i
    when /:/
      values = line.split(': ')

      field = values[0].strip
      value = values[1] ? values[1].strip : ''

      dmi_append_field(current_struct, field, value)

      case line
      when 'Characteristics:'
        current_struct.characteristics = []
        wrapped_field_line = :characteristics
      when 'Flags:'
        current_struct.flags = []
        wrapped_field_line = :flags
      when 'Supported SRAM Types:'
        current_struct.supported_sram_types = []
        wrapped_field_line = :supported_sram_types
      when 'Features:'
        current_struct.features = []
        wrapped_field_line = :features
      when 'Strings:'
        current_struct.strings = []
        wrapped_field_line = :strings
      end
    else
      current_struct[wrapped_field_line] << line.strip if wrapped_field_line
    end
  end

  rows << current_struct if current_struct
  rows
end