diff --git a/a.rb b/a.rb new file mode 100755 index 0000000..55fc6aa --- /dev/null +++ b/a.rb @@ -0,0 +1,73 @@ +#!/usr/bin/env ruby +#encoding: utf-8 + +$: << 'lib' + +require 'biju' +require 'pp' + +str = 'www.ruby-lang.org and bonjour www.rubygarden.org coucou' +#re = / +# ( # capture the hostname in $1 +# (?: # these parens for grouping only +# (?! [-_] ) # lookahead for neither underscore nor dash +# [\w-] + # hostname component +# \. # and the domain dot +# ) + # now repeat that whole thing a bunch of times +# [A-Za-z] # next must be a letter +# [\w-] + # now trailing domain part +# ) # end of $1 capture +# /x # /x for nice formatting +re = / + ( # capture the hostname in $1 + (?: # these parens for grouping only + (?! [-_] ) # lookahead for neither underscore nor dash + [\w-] + # hostname component + \. # and the domain dot + ) + # now repeat that whole thing a bunch of times + [A-Za-z] # next must be a letter + [\w-] + # now trailing domain part + ) # end of $1 capture + /x # /x for nice formatting + +str.gsub! re do # pass a block to execute replacement + pp $1 +end + +#exit + +puts "here" +hayes = Biju::HayesSms.new +pp hayes.attention +pp hayes.answer = 'OK' +pp hayes.ok? +pp hayes.init_modem +pp hayes.answer = 'OK' +pp hayes.ok? +pp hayes.text_mode +pp (hayes.answer = 'OK') +pp hayes.ok? +pp hayes.prefered_storage? +pp hayes.answer = '+CPMS: ("ME","MT","SM","SR"),("ME","MT","SM","SR"),("ME","MT","SM","SR")' +# ((),(),()) +# (),(),() +# 1,2 + +pp hayes.ok? + +exit + +@modem = Biju::Modem.new(:port => "/dev/ttyUSB0", :pin => '2382') + +# method to list all messages +@modem.messages.each do |sms| + puts sms +end + +# method to send sms +sms = Biju::Sms.new(:phone_number => "0668486469", :message => 'hello world3') +puts @modem.send(sms) + +@modem.close + + diff --git a/biju.gemspec b/biju.gemspec index 98fe990..a4aa2a4 100644 --- a/biju.gemspec +++ b/biju.gemspec @@ -17,5 +17,5 @@ Gem::Specification.new do |gem| gem.add_development_dependency "minitest", "3.0.0" - gem.add_dependency "serialport", "1.0.4" + gem.add_dependency "serialport", ">=1.0.4" end diff --git a/lib/biju.rb b/lib/biju.rb index a7a08b6..656b999 100644 --- a/lib/biju.rb +++ b/lib/biju.rb @@ -1,3 +1,4 @@ require 'biju/version' require "biju/modem" require "biju/sms" +require "biju/hayes" diff --git a/lib/biju/hayes.rb b/lib/biju/hayes.rb new file mode 100644 index 0000000..6f68bb1 --- /dev/null +++ b/lib/biju/hayes.rb @@ -0,0 +1,72 @@ +module Biju + class Hayes + attr_accessor :command, :attributes, :ok + attr_reader :answer + + #def method_missing(m, *args, &block) + #end + + def attention + basic_command { |response| response =~ /OK/ } + #basic_command { |response| true } + end + + def init_modem + basic_command('Z') { |response| response =~ /OK/ } + end + + def text_mode(enabled = true) + extended_command('CMGF', enabled) { |response| response =~ /OK/ } + end + + def prefered_storage? + extended_command('CPMS') { |response| response =~ /OK/ } + end + + def answer=(ret) + @answer = ret + ok? + end + + def ok? + ok.nil? ? true : !ok.call(answer).nil? + end + + private + + def basic_command(cmd = nil, options = {}, *args, &block) + option_prefix = options[:prefix] || nil + cmd_root = ['AT', cmd].compact.join(option_prefix) + cmd_args = args.compact.map { |arg| to_hayes_string(arg) } unless args.empty? + self.command = [cmd_root, cmd_args].compact.join('=') + self.ok = block if block_given? + command + end + + def extended_command(cmd = nil, *args, &block) + basic_command(cmd, {:prefix => '+'}, *args, &block) + end + + def hayes_to_obj(str) + end + + def to_hayes_string(arg) + case arg + when String + "\"#{arg}\"" + when Array + arg.join(',') + when TrueClass, FalseClass + !!arg ? 1 : 0 + else + "?" + end + end + end + + class HayesSms < Hayes + def unlock_pin(pin) + send_command("AT+CPIN=#{to_hayes_string(pin)}") { |response| response =~ /OK/ } + end + end +end diff --git a/lib/biju/modem.rb b/lib/biju/modem.rb index b9bf222..b55f2b8 100644 --- a/lib/biju/modem.rb +++ b/lib/biju/modem.rb @@ -3,6 +3,7 @@ require_relative 'sms' module Biju class Modem + attr_reader :connection # @param [Hash] Options to serial connection. # @option options [String] :port The modem port to connect @@ -11,23 +12,30 @@ module Biju # def initialize(options={}, &block) raise Exception.new("Port is required") unless options[:port] - pin = options.delete(:pin) - @connection = connection(options) + pin = options[:pin] || '0000' + @connection = connect(options) cmd("AT") # initialize modem cmd("ATZ") # unlock pin code cmd("AT+CPIN=\"#{pin}\"") if pin + + cmd("AT+CPMS=?") + # set SMS text mode cmd("AT+CMGF=1") # set extended error reports cmd('AT+CMEE=1') #instance_eval &block if block_given? + if block_given? + yield connection + close + end end # Close the serial connection. def close - @connection.close + connection.close end # Return an Array of Sms if there is messages nad return nil if not. @@ -60,7 +68,7 @@ module Biju end private - def connection(options) + def connect(options) port = options.delete(:port) SerialPort.new(port, default_options.merge!(options)) end @@ -70,17 +78,19 @@ module Biju end def cmd(cmd) - @connection.write(cmd + "\r") +puts "SENDING : #{cmd}" + connection.write(cmd + "\r") wait_str = wait #p "#{cmd} --> #{wait_str}" end def wait buffer = '' - while IO.select([@connection], [], [], 0.25) - chr = @connection.getc.chr; + while IO.select([connection], [], [], 0.25) + chr = connection.getc.chr; buffer += chr end +puts "RECEIVING : #{buffer}" buffer end end