sim_card 0.0.3 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,6 +5,11 @@ end
5
5
  require 'date'
6
6
 
7
7
  root = File.join(File.dirname(__FILE__), 'sim_card')
8
+ require File.join(root, 'error.rb')
8
9
  require File.join(root, 'received_sms_message.rb')
9
10
  require File.join(root, 'sim.rb')
10
- require File.join(root, 'signal_quality.rb')
11
+ require File.join(root, 'signal_quality.rb')
12
+ require File.join(root, 'phonebook.rb')
13
+ require File.join(root, 'phonebook_entry.rb')
14
+ require File.join(root, 'at_interface.rb')
15
+ require File.join(root, 'real_at_interface.rb')
@@ -3,7 +3,8 @@ class SimCard
3
3
 
4
4
  # parse raw output from SIM card and return list of ReceivedSmsMessage instances.
5
5
  # see SimCardTest for examples of raw SIM output.
6
- def self.to_messages raw_sim_output
6
+ def self.load_messages at_command_sender
7
+ raw_sim_output = at_command_sender.send "AT+CMGL=\"ALL\""
7
8
  messages = []
8
9
  raw_input2 = raw_sim_output[14..-1] # remove initial AT+CMGL="ALL"\n
9
10
  raw_input3 = (raw_input2 || '').split('+CMGL: ')[1..-1]
@@ -2,8 +2,8 @@ class SimCard
2
2
  class SignalQuality
3
3
  attr_reader :signal_strength
4
4
 
5
- def initialize raw_sim_output
6
- @raw_sim_output = raw_sim_output
5
+ def initialize at_interface
6
+ @at_interface = at_interface
7
7
  @signal_strength = nil
8
8
  @bit_error_rate_min = nil
9
9
  @bit_error_rate_max = nil
@@ -12,7 +12,8 @@ class SimCard
12
12
 
13
13
  private
14
14
  def parse
15
- a = @raw_sim_output.split("\n")
15
+ response = @at_interface.send 'AT+CSQ'
16
+ a = response.split("\n")
16
17
  if a[1] && a[1].include?('+CSQ: ')
17
18
  b = a[1].match /(\d+),(\d+)/
18
19
  if b.size == 3
@@ -1,6 +1,6 @@
1
1
  class SimCard
2
2
 
3
- # code below is based on:
3
+ # the initial idea is borrowed from here:
4
4
  # http://www.dzone.com/snippets/send-and-receive-sms-text
5
5
 
6
6
  class Sim
@@ -8,19 +8,24 @@ class SimCard
8
8
  # connect to SIM card. user_options may contain:
9
9
  # * port : device where the gsm modem is connected, default is '/dev/ttyUSB0'
10
10
  # * speed : connection speed in bauds, default is 9600
11
- # * pin : pin to your sim card, default is '0000'
11
+ # * pin : pin code to your sim card. not required if your sim does not require authorization via pin
12
12
  # * sms_center_no : SMS center of you provider. required only if you want to send SMS messages
13
+ # * debug_mode: when set to true, will print all communication with SIM card to stdout
13
14
  def initialize(user_options = {})
14
- default_options = {:port => '/dev/ttyUSB0', :speed => 9600, :pin => '0000'}
15
+ default_options = {:port => '/dev/ttyUSB0', :speed => 9600}
15
16
  options = default_options.merge user_options
16
17
 
17
- @port = SerialPort.new(options[:port], options[:speed])
18
- send_raw_at_command("AT")
19
- send_raw_at_command("AT+CPIN=\"#{options[:pin]}\"")
18
+ @port = SerialPort.new options[:port], options[:speed]
19
+
20
+ debug_mode = (options[:debug_mode] == true)
21
+ @at_interface = RealAtInterface.new @port, debug_mode
22
+
23
+ initial_check
24
+ authorize options[:pin]
20
25
  # Set to text mode
21
- send_raw_at_command("AT+CMGF=1")
26
+ @at_interface.send "AT+CMGF=1"
22
27
  # Set SMSC number
23
- send_raw_at_command("AT+CSCA=\"#{options[:sms_center_no]}\"") if options[:sms_center_no]
28
+ @at_interface.send "AT+CSCA=\"#{options[:sms_center_no]}\"" if options[:sms_center_no]
24
29
  end
25
30
 
26
31
  # correctly disconnect from SIM card
@@ -28,48 +33,67 @@ class SimCard
28
33
  @port.close
29
34
  end
30
35
 
36
+ # send SMS message
31
37
  def send_sms number, message_text
32
- send_raw_at_command("AT+CMGS=\"#{number}\"")
33
- send_raw_at_command("#{message_text[0..140]}#{26.chr}\r\r")
38
+ @at_interface.send "AT+CMGS=\"#{number}\""
39
+ @at_interface.send "#{message_text[0..140]}#{26.chr}\r\r"
34
40
  sleep 3
35
- wait
36
- send_raw_at_command("AT")
41
+ @at_interface.send "AT"
37
42
  end
38
43
 
39
44
  # list SMS messages in SIM memory
40
45
  def sms_messages
41
- raw_sim_output = send_raw_at_command("AT+CMGL=\"ALL\"")
42
- ReceivedSmsMessage.to_messages raw_sim_output
46
+ ReceivedSmsMessage.load_messages @at_interface
47
+ end
48
+
49
+ # return instance of Phonebook
50
+ def phonebook
51
+ Phonebook.new @at_interface
43
52
  end
44
53
 
45
54
  # remove SMS message from SIM card memory
46
55
  # * sms_message: instance of SimCard::SmsMessage to be deleted
47
56
  def delete_sms_message sms_message
48
- send_raw_at_command("AT+CMGD=#{sms_message.message_id}")
57
+ @at_interface.send "AT+CMGD=#{sms_message.message_id}"
49
58
  end
50
59
 
51
- # in dBm. -60 is almost perfect signal, -112 is very poor (call dropping bad)
60
+ # signal strengh in dBm. -60 is almost perfect signal, -112 is very poor (call dropping bad)
52
61
  def signal_strength
53
- raw_sim_output = send_raw_at_command 'AT+CSQ'
54
- sq = SimCard::SignalQuality.new raw_sim_output
62
+ sq = SimCard::SignalQuality.new @at_interface
55
63
  return sq.signal_strength
56
64
  end
57
-
65
+
66
+ # for hackers
58
67
  def send_raw_at_command cmd
59
- # puts "SIM CMD IN:#{cmd}"
60
- @port.write(cmd + "\r")
61
- wait
68
+ @at_interface.send cmd
62
69
  end
63
70
 
64
71
  private
65
- def wait
66
- buffer = ''
67
- while IO.select([@port], [], [], 0.25)
68
- chr = @port.getc.chr;
69
- buffer += chr
72
+
73
+ def initial_check
74
+ response = @at_interface.send "AT"
75
+ if response.include?("OK")
76
+ return true
77
+ else
78
+ raise Error.new("SIM is not responsing properly to initial handshake. response: #{response.inspect}")
79
+ end
80
+ end
81
+
82
+ def authorize pin
83
+ auth_status = @at_interface.send "AT+CPIN?"
84
+ if auth_status.include?('READY')
85
+ # no pin required or already authorized
86
+ return true
87
+ elsif auth_status.include?('SIM PIN')
88
+ response = @at_interface.send "AT+CPIN=\"#{pin}\""
89
+ if response.include?('OK')
90
+ return true
91
+ else
92
+ raise Error.new("SIM authorization failed: #{response.inspect}")
93
+ end
94
+ else
95
+ raise Error.new("unknown SIM authorization status: #{auth_status.inspect}")
70
96
  end
71
- # puts "SIM OUT:#{buffer}"
72
- buffer
73
97
  end
74
98
  end
75
99
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sim_card
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-23 00:00:00.000000000 Z
12
+ date: 2013-09-07 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description:
15
15
  email: