Add support for +CNUM to get SIM phone numbers
parent
2f4f1f81f3
commit
7281b8f82d
|
@ -48,6 +48,18 @@ module Biju
|
|||
at_command('Z')[:status]
|
||||
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)
|
||||
at_command('+CMGF', enabled)[:status]
|
||||
end
|
||||
|
|
|
@ -15,24 +15,32 @@ module Biju
|
|||
rule(:prefix) { str('AT') | str('at') }
|
||||
|
||||
# RESPONSE
|
||||
rule(:response) { ((status | command) >> crlf) | prompt }
|
||||
rule(:response) { ((command.maybe >> status) | merror) >> crlf | 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
|
||||
(str('+CMGL').as(:cmd) >> str(': ') >> infos >> crlf >> message >> crlf).repeat.as(:sms) >>
|
||||
crlf >> status
|
||||
(str('+CMGL').as(:cmd) >> str(': ') >> infos >> crlf >> message >> crlf)
|
||||
.repeat.as(:sms) >> crlf
|
||||
end
|
||||
rule(:num) do
|
||||
(str('+CNUM').as(:cmd) >> str(': ') >> array >> crlf)
|
||||
.repeat.as(:phone_numbers) >> crlf >> crlf
|
||||
end
|
||||
rule(:pms) do
|
||||
str('+CPMS').as(:cmd) >> str(': ') >> str('(').maybe >> array >> str(')').maybe >>
|
||||
crlf >> crlf >> status
|
||||
crlf >> crlf
|
||||
end
|
||||
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
|
||||
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
|
||||
|
||||
rule(:array) do
|
||||
|
|
|
@ -4,16 +4,34 @@ require 'biju/parser'
|
|||
describe Biju::ATParser do
|
||||
context "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)
|
||||
end
|
||||
|
||||
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)
|
||||
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
|
||||
it "parses cmgs prompt" do
|
||||
mgs = "AT+CMGS=18\r\r\n> "
|
||||
|
@ -42,6 +60,17 @@ describe Biju::ATParser do
|
|||
expect(result[:sms][0][:message]).to eq('07913396050066F3040B91336789')
|
||||
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
|
||||
pms = "AT+CPMS=?\r\r\n+CPMS: ((\"SM\",\"BM\",\"SR\"),(\"SM\"))\r\n\r\nOK\r\n"
|
||||
|
||||
|
|
Loading…
Reference in New Issue