diff --git a/lib/biju/hayes.rb b/lib/biju/hayes.rb index cafd8de..3b52111 100644 --- a/lib/biju/hayes.rb +++ b/lib/biju/hayes.rb @@ -79,8 +79,12 @@ module Biju nil end + def pin_status + at_command('+CPIN?')[:result] + end + def unlock_pin(pin) - at_command('+CPIN', pin)[:status] + at_command('+CPIN', pin)[:status] if pin_status == 'SIM PIN' end def messages(which = :all) diff --git a/lib/biju/parser.rb b/lib/biju/parser.rb index 1ee4b61..7c9b57e 100644 --- a/lib/biju/parser.rb +++ b/lib/biju/parser.rb @@ -17,7 +17,7 @@ module Biju # RESPONSE rule(:response) { ((command.maybe >> status) | merror) >> crlf | prompt } rule(:prompt) { str('> ').as(:prompt) } - rule(:command) { mgl | num | pms | mgf | mgs } + rule(:command) { mgl | num | pin | pms | mgf | mgs } rule(:merror) do (str('+CME ERROR') | str('+CMS ERROR')).as(:cmd) >> str(': ') >> @@ -39,6 +39,9 @@ module Biju rule(:mgf) do str('+CMGF').as(:cmd) >> str(': ') >> boolean.as(:result) >> crlf >> crlf end + rule(:pin) do + str('+CPIN').as(:cmd) >> str(': ') >> eol.as(:result) >> crlf >> crlf + end rule(:mgs) do str('+CMGS').as(:cmd) >> str(': ') >> int.as(:result) >> crlf >> crlf end @@ -66,6 +69,7 @@ module Biju rule(:string) { quote >> match('[^\"]').repeat.as(:string) >> quote } rule(:int) { match('[0-9]').repeat(1).as(:int) } rule(:boolean) { match('[01]').as(:boolean) } + rule(:eol) { (crlf.absent? >> any).repeat.as(:string) } rule(:datetime) { quote >> (date >> str(',') >> time).as(:datetime) >> quote } rule(:date) do diff --git a/spec/biju/parser_spec.rb b/spec/biju/parser_spec.rb index e20705a..4f7b4ea 100644 --- a/spec/biju/parser_spec.rb +++ b/spec/biju/parser_spec.rb @@ -93,6 +93,16 @@ describe Biju::ATParser do expect(result[:array]).to eq([23, 23, 7, 100, 7, 100]) end + it "gets pin status" do + pin = "AT+CPIN?\r\r\n+CPIN: READY\r\n\r\nOK\r\n" + + result = Biju::ATTransform.new.apply( + Biju::ATParser.new.parse(pin)) + + expect(result).to include(status: true) + expect(result[:result]).to eq("READY") + end + it "parses +CMGF? response" do mgf = "AT+CMGF?\r\r\n+CMGF: 0\r\n\r\nOK\r\n"