Add support for +CNUM to get SIM phone numbers

develop
Guillaume DOTT 2013-09-11 16:38:19 +02:00
parent 2f4f1f81f3
commit 7281b8f82d
3 changed files with 59 additions and 10 deletions

View File

@ -48,6 +48,18 @@ module Biju
at_command('Z')[:status] at_command('Z')[:status]
end end
def phone_numbers
result = at_command('+CNUM')
return [] unless result.has_key?(:phone_numbers)
result[:phone_numbers].map do |number|
{
number: number[:array][1].gsub(/[^0-9]/, ''),
type_of_address: PDU::TypeOfAddress.new(number[:array][2]).to_sym
}
end
end
def text_mode(enabled = true) def text_mode(enabled = true)
at_command('+CMGF', enabled)[:status] at_command('+CMGF', enabled)[:status]
end end

View File

@ -15,24 +15,32 @@ module Biju
rule(:prefix) { str('AT') | str('at') } rule(:prefix) { str('AT') | str('at') }
# RESPONSE # RESPONSE
rule(:response) { ((status | command) >> crlf) | prompt } rule(:response) { ((command.maybe >> status) | merror) >> crlf | prompt }
rule(:prompt) { str('> ').as(:prompt) } rule(:prompt) { str('> ').as(:prompt) }
rule(:command) { mgl | pms | mgf | mgs | merror } rule(:command) { mgl | num | pms | mgf | mgs }
rule(:merror) do
(str('+CME ERROR') | str('+CMS ERROR')).as(:cmd) >> str(': ') >>
int.as(:result)
end
rule(:merror) { (str('+CME ERROR') | str('+CMS ERROR')).as(:cmd) >> str(': ') >> int.as(:result) }
rule(:mgl) do rule(:mgl) do
(str('+CMGL').as(:cmd) >> str(': ') >> infos >> crlf >> message >> crlf).repeat.as(:sms) >> (str('+CMGL').as(:cmd) >> str(': ') >> infos >> crlf >> message >> crlf)
crlf >> status .repeat.as(:sms) >> crlf
end
rule(:num) do
(str('+CNUM').as(:cmd) >> str(': ') >> array >> crlf)
.repeat.as(:phone_numbers) >> crlf >> crlf
end end
rule(:pms) do rule(:pms) do
str('+CPMS').as(:cmd) >> str(': ') >> str('(').maybe >> array >> str(')').maybe >> str('+CPMS').as(:cmd) >> str(': ') >> str('(').maybe >> array >> str(')').maybe >>
crlf >> crlf >> status crlf >> crlf
end end
rule(:mgf) do rule(:mgf) do
str('+CMGF').as(:cmd) >> str(': ') >> boolean.as(:result) >> crlf >> crlf >> status str('+CMGF').as(:cmd) >> str(': ') >> boolean.as(:result) >> crlf >> crlf
end end
rule(:mgs) do rule(:mgs) do
str('+CMGS').as(:cmd) >> str(': ') >> int.as(:result) >> crlf >> crlf >> status str('+CMGS').as(:cmd) >> str(': ') >> int.as(:result) >> crlf >> crlf
end end
rule(:array) do rule(:array) do

View File

@ -4,16 +4,34 @@ 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("AT\r\r\nOK\r\n")) result = Biju::ATTransform.new.apply(
Biju::ATParser.new.parse("AT\r\r\nOK\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("AT\r\r\nERROR\r\n")) result = Biju::ATTransform.new.apply(
Biju::ATParser.new.parse("AT\r\r\nERROR\r\n"))
expect(result).to include(status: false) expect(result).to include(status: false)
end end
end end
context "errors" do
it "parses CMS ERROR" do
result = Biju::ATTransform.new.apply(
Biju::ATParser.new.parse("AT\r\r\n+CMS ERROR: 500\r\n"))
expect(result[:cmd]).to eq('+CMS ERROR')
expect(result[:result]).to eq(500)
end
it "parses CME ERROR" do
result = Biju::ATTransform.new.apply(
Biju::ATParser.new.parse("AT\r\r\n+CME ERROR: 100\r\n"))
expect(result[:cmd]).to eq('+CME ERROR')
expect(result[:result]).to eq(100)
end
end
context "response" do context "response" do
it "parses cmgs prompt" do it "parses cmgs prompt" do
mgs = "AT+CMGS=18\r\r\n> " mgs = "AT+CMGS=18\r\r\n> "
@ -42,6 +60,17 @@ describe Biju::ATParser do
expect(result[:sms][0][:message]).to eq('07913396050066F3040B91336789') expect(result[:sms][0][:message]).to eq('07913396050066F3040B91336789')
end end
it "gets phone numbers" do
pms = "AT+CNUM\r\r\n+CNUM: \"M\",\"+33666666666\",145\r\n\r\n\r\nOK\r\n"
result = Biju::ATTransform.new.apply(
Biju::ATParser.new.parse(pms))
expect(result[:phone_numbers][0][:cmd]).to eq('+CNUM')
expect(result[:phone_numbers]).to have(1).phone_number
expect(result[:phone_numbers][0][:array][1]).to eq('+33666666666')
end
it "gets messages storage" do it "gets messages storage" do
pms = "AT+CPMS=?\r\r\n+CPMS: ((\"SM\",\"BM\",\"SR\"),(\"SM\"))\r\n\r\nOK\r\n" pms = "AT+CPMS=?\r\r\n+CPMS: ((\"SM\",\"BM\",\"SR\"),(\"SM\"))\r\n\r\nOK\r\n"