Skip to content

Commit

Permalink
Finishing up the fixes for encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Rafter committed Mar 17, 2009
1 parent c4a9b35 commit bd94d58
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
54 changes: 49 additions & 5 deletions lib/rubygsm/core.rb
Expand Up @@ -18,7 +18,6 @@ module Gsm
class Modem
include Timeout


attr_accessor :verbosity, :read_timeout
attr_reader :device, :port

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -96,8 +96,6 @@ def initialize(port=:auto, verbosity=:warn, baud=9600, cmd_delay=0.1)
end




private


Expand Down Expand Up @@ -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
Expand All @@ -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)
#
Expand Down Expand Up @@ -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=?")
Expand Down
6 changes: 3 additions & 3 deletions 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 = [
Expand Down

0 comments on commit bd94d58

Please sign in to comment.