diff --git a/lib/rubygsm/core.rb b/lib/rubygsm/core.rb index 14c9b3e..f511a4c 100644 --- a/lib/rubygsm/core.rb +++ b/lib/rubygsm/core.rb @@ -18,7 +18,6 @@ module Gsm class Modem include Timeout - attr_accessor :verbosity, :read_timeout attr_reader :device, :port @@ -66,6 +65,7 @@ def initialize(port=:auto, verbosity=:warn, baud=9600, cmd_delay=0.1) @verbosity = verbosity @read_timeout = 10 @locked_to = false + @encoding = nil # keep track of the depth which each # thread is indented in the log @@ -96,8 +96,6 @@ def initialize(port=:auto, verbosity=:warn, baud=9600, cmd_delay=0.1) end - - private @@ -699,6 +697,9 @@ def send_sms(*args) # the text prompt or an error message command "AT+CMGS=\"#{to}\"", ["\r\n", "> "] + # encode the message? + msg = encode(msg) + begin # send the sms, and wait until # it is accepted or rejected @@ -724,8 +725,27 @@ def send_sms(*args) # then the message was sent return true end - - + + # Encodes the message using the set encoding or, if no encoding is specified + # returns the msg unchange + def encode(msg) + if (@encoding == :ascii) + # TODO, use lucky sneaks here + msg + elsif (@encoding == :utf8) + # Unpacking and repacking supposedly cleans out bad (non-UTF-8) stuff + utf8 = msg.unpack("U*"); + packed = utf8.pack("U*"); + packed + elsif (@encoding == :ucs2) + ucs2 = Iconv.iconv("UCS-2", "UTF-8", msg).first + ucs2 = ucs2.unpack("H*").join + ucs2 + else + msg + end + end + # call-seq: # receive(callback_method, interval=5, join_thread=false) # @@ -801,6 +821,30 @@ def receive(callback, interval=5, join_thread=false) @thr.join if join_thread end + def encodings? + command "AT+CSCS=?" + end + + def encoding + @encoding + end + + def encoding=(enc) + @encoding = enc + case enc + when :ascii + command "AT+CSCS=\"ASCII\"" + when :utf8 + command "AT+CSCS=\"UTF8\"" + when :ucs2 + command "AT+CSCS=\"UCS2\"" + when :gsm + command "AT+CSCS=\"GSM\"" + when :iso88591 + command "AT+CSCS=\"8859-1\"" + end + end + def select_default_mailbox # Eventually we will select the first mailbox as the default result = command("AT+CPMS=?") diff --git a/rubygsm.gemspec b/rubygsm.gemspec index ed7a83f..78ef921 100644 --- a/rubygsm.gemspec +++ b/rubygsm.gemspec @@ -1,11 +1,11 @@ Gem::Specification.new do |s| s.name = "rubygsm" - s.version = "0.3.2" - s.date = "2009-01-09" + s.version = "0.3.3" + s.date = "2009-03-14" s.summary = "Send and receive SMS with a GSM modem" s.email = "adam.mckaig@gmail.com" s.homepage = "http://github.com/adammck/rubygsm" - s.authors = ["Adam Mckaig"] + s.authors = ["Adam Mckaig", "Jeff Rafter"] s.has_rdoc = true s.files = [