From 7281b8f82d24d5ed0cb8bea5b765eef0f9863196 Mon Sep 17 00:00:00 2001 From: Guillaume DOTT Date: Wed, 11 Sep 2013 16:38:19 +0200 Subject: [PATCH] Add support for +CNUM to get SIM phone numbers --- lib/biju/hayes.rb | 12 ++++++++++++ lib/biju/parser.rb | 24 ++++++++++++++++-------- spec/biju/parser_spec.rb | 33 +++++++++++++++++++++++++++++++-- 3 files changed, 59 insertions(+), 10 deletions(-) diff --git a/lib/biju/hayes.rb b/lib/biju/hayes.rb index aa5ea37..cafd8de 100644 --- a/lib/biju/hayes.rb +++ b/lib/biju/hayes.rb @@ -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 diff --git a/lib/biju/parser.rb b/lib/biju/parser.rb index 41dc547..1ee4b61 100644 --- a/lib/biju/parser.rb +++ b/lib/biju/parser.rb @@ -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 diff --git a/spec/biju/parser_spec.rb b/spec/biju/parser_spec.rb index 2223f7b..e20705a 100644 --- a/spec/biju/parser_spec.rb +++ b/spec/biju/parser_spec.rb @@ -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"