Class: NetworkManager::Modem

Inherits:
Object
  • Object
show all
Defined in:
lib/network_manager/modem.rb

Defined Under Namespace

Classes: NotValid

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Modem

Returns a new instance of Modem.



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
# File 'lib/network_manager/modem.rb', line 10

def initialize(opts = {})
  opts.each do |k,v|
    instance_variable_set("@#{k}", v) unless v.nil?
  end

  unless defined?(@service)
    @bus        ||= DBus::SystemBus.instance
    @service    = @bus.service(NetworkManager::MM_DBUS_SERVICE) 
  end

  # Set DBUS proxy
  @proxy = @service.object(@bus_path)
  @proxy.introspect

  @properties = @proxy.dup
  @properties.default_iface = NetworkManager::DBUS_PROPERTIES
  @properties.introspect

  @s_modem = @proxy[NetworkManager::MM_DBUS_INTERFACE_MODEM_SIMPLE]
  @modem   = @proxy[NetworkManager::MM_DBUS_INTERFACE_MODEM]
  @network = @proxy[NetworkManager::MM_DBUS_INTERFACE_MODEM_GSM_NETWORK]
  @ussd    = @proxy[NetworkManager::MM_DBUS_INTERFACE_MODEM_GSM_USSD]

  @device_info = @properties.GetInfo[0] rescue nil
  raise NetworkManager::Modem::NotValid unless valid?
end

Instance Attribute Details

#bus_pathObject (readonly) Also known as: dbus_path

Returns the value of attribute bus_path.



6
7
8
# File 'lib/network_manager/modem.rb', line 6

def bus_path
  @bus_path
end

#serviceObject (readonly)

Returns the value of attribute service.



6
7
8
# File 'lib/network_manager/modem.rb', line 6

def service
  @service
end

Class Method Details

.fetch(paths_array, opts = {}) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/network_manager/modem.rb', line 141

def fetch(paths_array, opts = {})
  devices = []
  paths_array.compact.reject {|x| x && x.size <= 0 }.each do |path|
    begin
      devices << self.new(opts.merge({bus_path: path}))
    rescue NetworkManager::Modem::NotValid => e 
      puts "Bus #{path} contains empty object, skipping"
    end
  end
  devices
end

Instance Method Details

#deviceObject



105
106
107
108
109
110
111
112
113
# File 'lib/network_manager/modem.rb', line 105

def device
  {
    model: model,
    port: @properties[MM_DBUS_INTERFACE_MODEM]['Device'],
    unlock_required: @properties[MM_DBUS_INTERFACE_MODEM]['UnlockRequired'],
    master_device: @properties[MM_DBUS_INTERFACE_MODEM]['MasterDevice'],
    dbus_path: @bus_path
  }
end

#disable!Object



49
50
51
# File 'lib/network_manager/modem.rb', line 49

def disable!
  @modem.Enable(false) == [] if enabled?
end

#disabled?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/network_manager/modem.rb', line 45

def disabled?
  !enabled?
end

#enable!Object



41
42
43
# File 'lib/network_manager/modem.rb', line 41

def enable!
  @modem.Enable(true) == [] if disabled?
end

#imeiObject



78
79
80
# File 'lib/network_manager/modem.rb', line 78

def imei
  @properties.GetImei[0] rescue nil
end

#imsiObject



82
83
84
# File 'lib/network_manager/modem.rb', line 82

def imsi
  @properties.GetImsi[0] rescue nil
end

#inspectObject



119
120
121
122
123
124
125
# File 'lib/network_manager/modem.rb', line 119

def inspect
  if enabled?
    "#<NetworkManager::Modem##{object_id} IMEI: #{imei} Device: #{vendor} #{model} #{version} USSD_STATE: #{ussd_state}>"
  else
    "#<NetworkManager::Modem##{object_id} DISABLED Device: #{vendor} #{model} #{version}"
  end
end

#modelObject



53
54
55
# File 'lib/network_manager/modem.rb', line 53

def model
  @device_info[1] rescue nil
end

#operator_codeObject



62
63
64
# File 'lib/network_manager/modem.rb', line 62

def operator_code
  status["operator_code"] rescue nil
end

#scanObject



86
87
88
# File 'lib/network_manager/modem.rb', line 86

def scan
  @network.Scan[0] rescue nil
end

#send_ussd(message) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/network_manager/modem.rb', line 90

def send_ussd(message)
  rsp = case ussd_state
  when 'idle'
    @ussd.Initiate(message)
  when 'active'
    @ussd.Cancel rescue nil
    @ussd.Initiate(message)
  when 'user-response'
    @ussd.Respond(message)
  end

  # @ussd.Cancel rescue nil
  rsp[0] if rsp.is_a?(Array)
end

#signalObject



74
75
76
# File 'lib/network_manager/modem.rb', line 74

def signal
  @properties.GetSignalQuality[0] rescue 0
end

#statusObject Also known as: enabled?



57
58
59
# File 'lib/network_manager/modem.rb', line 57

def status
  @properties['org.freedesktop.ModemManager.Modem']['Enabled'] rescue false
end

#to_hObject



127
128
129
130
131
132
133
134
135
136
# File 'lib/network_manager/modem.rb', line 127

def to_h
  {
    imei: imei,
    imsi: imsi,
    signal: signal,
    operator_code: operator_code, 
    device: device,
    status: (enabled? ? :enabled : :disabled )
  }
end

#ussd_stateObject



115
116
117
# File 'lib/network_manager/modem.rb', line 115

def ussd_state
  @properties['org.freedesktop.ModemManager.Modem.Gsm.Ussd']['State']
end

#valid?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/network_manager/modem.rb', line 37

def valid?
  @properties.interfaces.size > 0
end

#vendorObject



66
67
68
# File 'lib/network_manager/modem.rb', line 66

def vendor
  @device_info[0] rescue nil
end

#versionObject



70
71
72
# File 'lib/network_manager/modem.rb', line 70

def version
  @device_info[2] rescue nil
end