Add +CMGF? support and text_mode? method

develop
Guillaume DOTT 2013-09-06 17:31:05 +02:00
parent c0a3fe8ef0
commit b4f49569bb
3 changed files with 40 additions and 5 deletions

View File

@ -40,6 +40,11 @@ module Biju
at_command('+CMGF', enabled)[:status]
end
def text_mode?(force = false)
@text_mode = at_command('+CMGF?')[:result] if @text_mode.nil? || force
@text_mode
end
def extended_error(enabled = true)
at_command('+CMEE', enabled)[:status]
end

View File

@ -13,7 +13,7 @@ module Biju
# RESPONSE
rule(:response) { cr >> crlf >> (status | command) >> crlf }
rule(:command) { mgl | pms | mserror }
rule(:command) { mgl | pms | mgf | mserror }
rule(:mserror) { str('+CMS ERROR').as(:cmd) >> str(': ') >> message }
rule(:mgl) do
@ -24,6 +24,9 @@ module Biju
str('+CPMS').as(:cmd) >> str(': ') >> str('(').maybe >> array >> str(')').maybe >>
crlf >> crlf >> status
end
rule(:mgf) do
str('+CMGF').as(:cmd) >> str(': ') >> boolean.as(:result) >> crlf >> crlf >> status
end
rule(:array) do
(data >> (comma >> data).repeat).as(:array)
@ -47,6 +50,7 @@ module Biju
rule(:empty_string) { str('').as(:empty_string) }
rule(:string) { quote >> match('[^\"]').repeat.as(:string) >> quote }
rule(:int) { match('[0-9]').repeat(1).as(:int) }
rule(:boolean) { match('[01]').as(:boolean) }
rule(:datetime) { quote >> (date >> str(',') >> time).as(:datetime) >> quote }
rule(:date) do
@ -65,9 +69,13 @@ module Biju
rule(cmd: simple(:cmd), array: subtree(:array)) do
{cmd: cmd.to_s, array: array}
end
rule(cmd: simple(:cmd), result: simple(:result)) do
{cmd: cmd.to_s, result: result}
end
rule(empty_string: simple(:empty_string)) { '' }
rule(int: simple(:int)) { int.to_i }
rule(boolean: simple(:boolean)) { boolean.to_i > 0 }
rule(string: simple(:string)) { string.to_s }
rule(datetime: simple(:datetime)) { DateTime.strptime(datetime.to_s, "%y/%m/%d,%T%Z") }
rule(array: subtree(:array)) { array }

View File

@ -4,19 +4,20 @@ require 'biju/parser'
describe Biju::ATParser do
context "status" do
it "returns ok status" do
result = Biju::ATTransform.new.apply(Biju::ATParser.new.parse("OK\r\n"))
result = Biju::ATTransform.new.apply(Biju::ATParser.new.parse("AT\r\r\nOK\r\n"))
expect(result).to include(status: true)
end
it "returns error status" do
result = Biju::ATTransform.new.apply(Biju::ATParser.new.parse("ERROR\r\n"))
result = Biju::ATTransform.new.apply(Biju::ATParser.new.parse("AT\r\r\nERROR\r\n"))
expect(result).to include(status: false)
end
end
context "response" do
it "parses messages list" do
messages = "+CMGL: 0,1,,23\r\n" <<
messages = "AT+CMGL=1\r\r\n" <<
"+CMGL: 0,1,,23\r\n" <<
"07913396050066F3040B91336789\r\n" <<
"+CMGL: 3,1,,74\r\n" <<
"BD60B917ACC68AC17431982E066BC5642205F3C95400\r\n" <<
@ -33,7 +34,7 @@ describe Biju::ATParser do
end
it "gets messages storage" do
pms = "+CPMS: ((\"SM\",\"BM\",\"SR\"),(\"SM\"))\r\n"
pms = "AT+CPMS=?\r\r\n+CPMS: ((\"SM\",\"BM\",\"SR\"),(\"SM\"))\r\n\r\nOK\r\n"
result = Biju::ATTransform.new.apply(
Biju::ATParser.new.parse(pms))
@ -42,6 +43,27 @@ describe Biju::ATParser do
expect(result[:array]).to have(2).storage
expect(result[:array][0]).to have(3).storage
end
it "gets specified message storage infos" do
pms = "AT+CPMS=\"MT\"\r\r\n+CPMS: 23,23,7,100,7,100\r\n\r\nOK\r\n"
result = Biju::ATTransform.new.apply(
Biju::ATParser.new.parse(pms))
expect(result).to include(status: true)
expect(result[:array]).to have(6).storage
expect(result[:array]).to eq([23, 23, 7, 100, 7, 100])
end
it "parses +CMGF? response" do
mgf = "AT+CMGF?\r\r\n+CMGF: 0\r\n\r\nOK\r\n"
result = Biju::ATTransform.new.apply(
Biju::ATParser.new.parse(mgf))
expect(result).to include(status: true)
expect(result[:result]).to be_false
end
end
it "raises ParseFailed exception" do