adammck-rubygsm 0.2 → 0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +2 -2
- data/bin/gsm-modem-band +49 -0
- data/lib/rubygsm.rb +4 -3
- data/lib/rubygsm/core.rb +95 -15
- data/lib/rubygsm/errors.rb +12 -1
- data/rubygsm.gemspec +8 -3
- metadata +5 -4
data/README.rdoc
CHANGED
@@ -3,7 +3,6 @@ to provide a nifty interface to send and receive SMS messages via a GSM modem.
|
|
3
3
|
|
4
4
|
|
5
5
|
=== Sample Usage
|
6
|
-
# included in bin/gsm-app-reverse
|
7
6
|
|
8
7
|
class ReverseApp
|
9
8
|
def initialize(gsm)
|
@@ -16,7 +15,7 @@ to provide a nifty interface to send and receive SMS messages via a GSM modem.
|
|
16
15
|
end
|
17
16
|
end
|
18
17
|
|
19
|
-
gsm = GsmModem.new
|
18
|
+
gsm = GsmModem.new
|
20
19
|
ReverseApp.new(gsm)
|
21
20
|
|
22
21
|
|
@@ -32,6 +31,7 @@ dependancy, so to install both:
|
|
32
31
|
|
33
32
|
$ sudo gem install adammck-rubygsm
|
34
33
|
|
34
|
+
|
35
35
|
==== For Debian/Ubuntu users...
|
36
36
|
The ruby-serialport library is available in the APT repository as a binary package, which
|
37
37
|
is substatially less hassle to install than the Gem, for those who don't have a compiler
|
data/bin/gsm-modem-band
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# vim: noet
|
3
|
+
|
4
|
+
#require "rubygems"
|
5
|
+
#require "rubygsm"
|
6
|
+
dir = File.dirname(__FILE__)
|
7
|
+
require "#{dir}/../lib/rubygsm.rb"
|
8
|
+
|
9
|
+
|
10
|
+
begin
|
11
|
+
modem = GsmModem.new
|
12
|
+
|
13
|
+
rescue GsmModem::Error => err
|
14
|
+
puts "Initialization Error:"
|
15
|
+
puts " " + err.desc
|
16
|
+
exit
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
puts "Found modem on port:"
|
21
|
+
puts " " + modem.port
|
22
|
+
puts
|
23
|
+
|
24
|
+
|
25
|
+
puts "GSM bands supported:"
|
26
|
+
puts " " + modem.bands_available.join(", ")
|
27
|
+
puts
|
28
|
+
|
29
|
+
|
30
|
+
puts "Currently selected:"
|
31
|
+
puts " " + modem.band
|
32
|
+
puts
|
33
|
+
|
34
|
+
|
35
|
+
if ARGV[0]
|
36
|
+
new_band = ARGV[0].downcase
|
37
|
+
|
38
|
+
begin
|
39
|
+
modem.band = new_band
|
40
|
+
puts "Switched to: "
|
41
|
+
puts " " + modem.band
|
42
|
+
|
43
|
+
rescue StandardError => err
|
44
|
+
puts "Error switching band: "
|
45
|
+
puts " " + err
|
46
|
+
end
|
47
|
+
|
48
|
+
puts
|
49
|
+
end
|
data/lib/rubygsm.rb
CHANGED
data/lib/rubygsm/core.rb
CHANGED
@@ -20,7 +20,7 @@ class GsmModem
|
|
20
20
|
|
21
21
|
|
22
22
|
attr_accessor :verbosity, :read_timeout
|
23
|
-
attr_reader :device
|
23
|
+
attr_reader :device, :port
|
24
24
|
|
25
25
|
# call-seq:
|
26
26
|
# GsmModem.new(port, verbosity=:warn)
|
@@ -28,10 +28,39 @@ class GsmModem
|
|
28
28
|
# Create a new instance, to initialize and communicate exclusively with a
|
29
29
|
# single modem device via the _port_ (which is usually either /dev/ttyS0
|
30
30
|
# or /dev/ttyUSB0), and start logging to *rubygsm.log* in the chdir.
|
31
|
-
def initialize(port, verbosity=:warn, baud=9600, cmd_delay=0.1)
|
31
|
+
def initialize(port=:auto, verbosity=:warn, baud=9600, cmd_delay=0.1)
|
32
|
+
|
33
|
+
# if no port was specified, we'll attempt to iterate
|
34
|
+
# all of the serial ports that i've ever seen gsm
|
35
|
+
# modems mounted on. this is kind of shaky, and
|
36
|
+
# only works well with a single modem. for now,
|
37
|
+
# we'll try: ttyS0, ttyUSB0, ttyACM0, ttyS1...
|
38
|
+
if port == :auto
|
39
|
+
@device, @port = catch(:found) do
|
40
|
+
0.upto(8).each do |n|
|
41
|
+
["ttyS", "ttyUSB", "ttyACM"].each do |prefix|
|
42
|
+
try_port = "/dev/#{prefix}#{n}"
|
43
|
+
|
44
|
+
begin
|
45
|
+
# serialport args: port, baud, data bits, stop bits, parity
|
46
|
+
device = SerialPort.new(try_port, baud, 8, 1, SerialPort::NONE)
|
47
|
+
throw :found, [device, try_port]
|
48
|
+
|
49
|
+
rescue ArgumentError, Errno::ENOENT
|
50
|
+
# do nothing, just continue to
|
51
|
+
# try the next port in order
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
32
55
|
|
33
|
-
|
34
|
-
|
56
|
+
# tried all ports, nothing worked
|
57
|
+
raise AutoDetectError
|
58
|
+
end
|
59
|
+
|
60
|
+
else
|
61
|
+
@device = SerialPort.new(port, baud, 8, 1, SerialPort::NONE)
|
62
|
+
@port = port
|
63
|
+
end
|
35
64
|
|
36
65
|
@cmd_delay = cmd_delay
|
37
66
|
@verbosity = verbosity
|
@@ -419,21 +448,21 @@ class GsmModem
|
|
419
448
|
# command, mapped to frequency bands, in MHz. Copied
|
420
449
|
# directly from the MultiTech AT command-set reference
|
421
450
|
Bands = {
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
451
|
+
0 => "850",
|
452
|
+
1 => "900",
|
453
|
+
2 => "1800",
|
454
|
+
3 => "1900",
|
455
|
+
4 => "850/1900",
|
456
|
+
5 => "900E/1800",
|
457
|
+
6 => "900E/1900"
|
429
458
|
}
|
430
459
|
|
431
460
|
# call-seq:
|
432
|
-
#
|
461
|
+
# bands_available => array
|
433
462
|
#
|
434
463
|
# Returns an array containing the bands supported by
|
435
464
|
# the modem.
|
436
|
-
def
|
465
|
+
def bands_available
|
437
466
|
data = query("AT+WMBS=?")
|
438
467
|
|
439
468
|
# wmbs data is returned as something like:
|
@@ -444,7 +473,7 @@ class GsmModem
|
|
444
473
|
# readable description
|
445
474
|
if m = data.match(/^\+WMBS: \(([\d,]+)\),/)
|
446
475
|
return m.captures[0].split(",").collect do |index|
|
447
|
-
Bands[index]
|
476
|
+
Bands[index.to_i]
|
448
477
|
end
|
449
478
|
|
450
479
|
else
|
@@ -462,7 +491,7 @@ class GsmModem
|
|
462
491
|
def band
|
463
492
|
data = query("AT+WMBS?")
|
464
493
|
if m = data.match(/^\+WMBS: (\d+),/)
|
465
|
-
return Bands[m.captures[0]]
|
494
|
+
return Bands[m.captures[0].to_i]
|
466
495
|
|
467
496
|
else
|
468
497
|
# Todo: Recover from this exception
|
@@ -471,6 +500,57 @@ class GsmModem
|
|
471
500
|
end
|
472
501
|
end
|
473
502
|
|
503
|
+
BandAreas = {
|
504
|
+
:usa => 4,
|
505
|
+
:africa => 5,
|
506
|
+
:europe => 5,
|
507
|
+
:asia => 5,
|
508
|
+
:mideast => 5
|
509
|
+
}
|
510
|
+
|
511
|
+
# call-seq:
|
512
|
+
# band=(_numeric_band_) => string
|
513
|
+
#
|
514
|
+
# Sets the band currently selected for use
|
515
|
+
# by the modem, using either a literal band
|
516
|
+
# number (passed directly to the modem, see
|
517
|
+
# GsmModem.Bands) or a named area from
|
518
|
+
# GsmModem.BandAreas:
|
519
|
+
#
|
520
|
+
# m = GsmModem.new
|
521
|
+
# m.band = :usa => "850/1900"
|
522
|
+
# m.band = :africa => "900E/1800"
|
523
|
+
# m.band = :monkey => ArgumentError
|
524
|
+
#
|
525
|
+
# (Note that as usual, the United States of
|
526
|
+
# America is wearing its ass backwards.)
|
527
|
+
#
|
528
|
+
# Raises ArgumentError if an unrecognized band was
|
529
|
+
# given, or raises GsmModem::Error if the modem does
|
530
|
+
# not support the given band.
|
531
|
+
def band=(new_band)
|
532
|
+
|
533
|
+
# resolve named bands into numeric
|
534
|
+
# (mhz values first, then band areas)
|
535
|
+
unless new_band.is_a?(Numeric)
|
536
|
+
|
537
|
+
if Bands.has_value?(new_band.to_s)
|
538
|
+
new_band = Bands.index(new_band.to_s)
|
539
|
+
|
540
|
+
elsif BandAreas.has_key?(new_band.to_sym)
|
541
|
+
new_band = BandAreas[new_band.to_sym]
|
542
|
+
|
543
|
+
else
|
544
|
+
err = "Invalid band: #{new_band}"
|
545
|
+
raise ArgumentError.new(err)
|
546
|
+
end
|
547
|
+
end
|
548
|
+
|
549
|
+
# set the band right now (second wmbs
|
550
|
+
# argument is: 0=NEXT-BOOT, 1=NOW). if it
|
551
|
+
# fails, allowGsmModem::Error to propagate
|
552
|
+
command("AT+WMBS=#{new_band},1")
|
553
|
+
end
|
474
554
|
|
475
555
|
# call-seq:
|
476
556
|
# pin_required? => true or false
|
data/lib/rubygsm/errors.rb
CHANGED
@@ -93,11 +93,16 @@ class GsmModem
|
|
93
93
|
return "Unknown error (unrecognized command?) " +\
|
94
94
|
"[type=#{@type}] [code=#{code}]"
|
95
95
|
end
|
96
|
+
|
97
|
+
alias :to_s :desc
|
96
98
|
end
|
97
99
|
|
100
|
+
# TODO: what the hell is going on with
|
101
|
+
# all these "desc" methods? refactor this
|
102
|
+
|
98
103
|
class TimeoutError < Error #:nodoc:
|
99
104
|
def desc
|
100
|
-
|
105
|
+
"The command timed out"
|
101
106
|
end
|
102
107
|
end
|
103
108
|
|
@@ -114,4 +119,10 @@ class GsmModem
|
|
114
119
|
"may have crashed or been unplugged"
|
115
120
|
end
|
116
121
|
end
|
122
|
+
|
123
|
+
class AutoDetectError < Error #:nodoc:
|
124
|
+
def desc
|
125
|
+
"No modem could be auto-detected."
|
126
|
+
end
|
127
|
+
end
|
117
128
|
end
|
data/rubygsm.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "rubygsm"
|
3
|
-
s.version = "0.
|
4
|
-
s.date = "2008-12-
|
3
|
+
s.version = "0.3"
|
4
|
+
s.date = "2008-12-31"
|
5
5
|
s.summary = "Send and receive SMS with a GSM modem"
|
6
6
|
s.email = "adam.mckaig@gmail.com"
|
7
7
|
s.homepage = "http://github.com/adammck/rubygsm"
|
@@ -14,7 +14,12 @@ Gem::Specification.new do |s|
|
|
14
14
|
"lib/rubygsm.rb",
|
15
15
|
"lib/rubygsm/core.rb",
|
16
16
|
"lib/rubygsm/errors.rb",
|
17
|
-
"lib/rubygsm/log.rb"
|
17
|
+
"lib/rubygsm/log.rb",
|
18
|
+
"bin/gsm-modem-band"
|
19
|
+
]
|
20
|
+
|
21
|
+
s.executables = [
|
22
|
+
"gsm-modem-band"
|
18
23
|
]
|
19
24
|
|
20
25
|
s.add_dependency("toholio-serialport", ["> 0.7.1"])
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: adammck-rubygsm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: "0.
|
4
|
+
version: "0.3"
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Mckaig
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-12-
|
12
|
+
date: 2008-12-31 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -23,8 +23,8 @@ dependencies:
|
|
23
23
|
version:
|
24
24
|
description:
|
25
25
|
email: adam.mckaig@gmail.com
|
26
|
-
executables:
|
27
|
-
|
26
|
+
executables:
|
27
|
+
- gsm-modem-band
|
28
28
|
extensions: []
|
29
29
|
|
30
30
|
extra_rdoc_files: []
|
@@ -36,6 +36,7 @@ files:
|
|
36
36
|
- lib/rubygsm/core.rb
|
37
37
|
- lib/rubygsm/errors.rb
|
38
38
|
- lib/rubygsm/log.rb
|
39
|
+
- bin/gsm-modem-band
|
39
40
|
has_rdoc: true
|
40
41
|
homepage: http://github.com/adammck/rubygsm
|
41
42
|
post_install_message:
|