Modify parser for PDU mode

develop
Guillaume DOTT 2013-09-06 10:41:17 +02:00
parent 1446be3c14
commit 84fbedc46b
2 changed files with 19 additions and 20 deletions

View File

@ -8,7 +8,7 @@ module Biju
rule(:at_string) { request | response } rule(:at_string) { request | response }
# REQUEST # REQUEST
rule(:request) { str('+++') | str('A/') | (prefix >> match('[^\r]').repeat) } rule(:request) { str('+++') | str('A/') | (prefix >> (crlf.absent? >> any).repeat) }
rule(:prefix) { str('AT') | str('at') } rule(:prefix) { str('AT') | str('at') }
# RESPONSE # RESPONSE
@ -17,7 +17,7 @@ module Biju
rule(:mserror) { str('+CMS ERROR').as(:cmd) >> str(': ') >> message } rule(:mserror) { str('+CMS ERROR').as(:cmd) >> str(': ') >> message }
rule(:mgl) do rule(:mgl) do
(str('+CMGL').as(:cmd) >> str(': ') >> infos >> crlf >> message).repeat.as(:sms) >> (str('+CMGL').as(:cmd) >> str(': ') >> infos >> crlf >> message >> crlf).repeat.as(:sms) >>
crlf >> status crlf >> status
end end
rule(:pms) do rule(:pms) do
@ -30,14 +30,16 @@ module Biju
rule(:data) { (str('(') >> array >> str(')')) | info } rule(:data) { (str('(') >> array >> str(')')) | info }
rule(:infos) { (info >> (comma >> info).repeat).as(:infos) } rule(:infos) { (info >> (comma >> info).repeat).as(:infos) }
rule(:info) { datetime | string | int | empty_string } rule(:info) { datetime | string | int | empty_string }
rule(:message) { match('[^\r]').repeat.as(:message) >> crlf } rule(:message) { match('[0-9A-Fa-f]').repeat.as(:message) }
# MISC # MISC
rule(:status) { (ok | error).as(:status) } rule(:status) { (ok | error).as(:status) }
rule(:ok) { str('OK').as(:ok) } rule(:ok) { str('OK').as(:ok) }
rule(:error) { str('ERROR').as(:error) } rule(:error) { str('ERROR').as(:error) }
rule(:crlf) { str("\r") } rule(:cr) { str("\r") }
rule(:lf) { str("\n") }
rule(:crlf) { cr >> lf }
rule(:comma) { str(',') } rule(:comma) { str(',') }
rule(:quote) { str('"') } rule(:quote) { str('"') }
@ -51,7 +53,7 @@ module Biju
end end
rule(:time) do rule(:time) do
(match('[0-9]').repeat(2) >> str(':')).repeat(2) >> match('[0-9]').repeat(2) >> (match('[0-9]').repeat(2) >> str(':')).repeat(2) >> match('[0-9]').repeat(2) >>
str('+') >> match('[0-9]').repeat(2) match('[-+]') >> match('[0-9]').repeat(2)
end end
end end

View File

@ -4,39 +4,36 @@ require 'biju/parser'
describe Biju::ATParser do describe Biju::ATParser do
context "status" do context "status" do
it "returns ok status" do it "returns ok status" do
result = Biju::ATTransform.new.apply(Biju::ATParser.new.parse("OK\r")) result = Biju::ATTransform.new.apply(Biju::ATParser.new.parse("OK\r\n"))
expect(result).to include(status: true) expect(result).to include(status: true)
end end
it "returns error status" do it "returns error status" do
result = Biju::ATTransform.new.apply(Biju::ATParser.new.parse("ERROR\r")) result = Biju::ATTransform.new.apply(Biju::ATParser.new.parse("ERROR\r\n"))
expect(result).to include(status: false) expect(result).to include(status: false)
end end
end end
context "response" do context "response" do
it "parses messages list" do it "parses messages list" do
messages = '+CMGL: 1,"REC READ","+85291234567",,"07/02/18,01:12:12+32"' << messages = "+CMGL: 0,1,,23\r\n" <<
"\r" << "07913396050066F3040B91336789\r\n" <<
"Reading text messages is easy.\r" << "+CMGL: 3,1,,74\r\n" <<
'+CMGL: 2,"REC READ","+85291234567",,"07/02/18,00:07:22+32"' << "BD60B917ACC68AC17431982E066BC5642205F3C95400\r\n" <<
"\r" << "+CMGL: 4,1,,20\r\n" <<
"A simple demo of SMS text messaging.\r" << "07913396050066F3040B913364446864\r\n" <<
'+CMGL: 3,"REC READ","+85291234567",,"07/02/18,00:12:05+32"' << "\r\n" <<
"\r" << "OK\r\n"
"Hello, welcome to our SMS tutorial.\r" <<
"\r" <<
"OK\r"
result = Biju::ATTransform.new.apply( result = Biju::ATTransform.new.apply(
Biju::ATParser.new.parse(messages)) Biju::ATParser.new.parse(messages))
expect(result).to include(status: true) expect(result).to include(status: true)
expect(result[:sms]).to have(3).messages expect(result[:sms]).to have(3).messages
expect(result[:sms][0][:message]).to eq('Reading text messages is easy.') expect(result[:sms][0][:message]).to eq('07913396050066F3040B91336789')
end end
it "gets messages storage" do it "gets messages storage" do
pms = "+CPMS: ((\"SM\",\"BM\",\"SR\"),(\"SM\"))\r" pms = "+CPMS: ((\"SM\",\"BM\",\"SR\"),(\"SM\"))\r\n"
result = Biju::ATTransform.new.apply( result = Biju::ATTransform.new.apply(
Biju::ATParser.new.parse(pms)) Biju::ATParser.new.parse(pms))