pb.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. --- 模块功能:电话簿管理
  2. -- @module pb
  3. -- @author openLuat
  4. -- @license MIT
  5. -- @copyright openLuat
  6. -- @release 2018.03.10
  7. module(..., package.seeall)
  8. require"ril"
  9. local req = ril.request
  10. local storagecb,readcb,writecb,deletecb
  11. local curPb = "SM"
  12. --- 设置电话本存储区域
  13. -- @string storage 存储区域字符串,仅支持"SM"
  14. -- @param cb 设置后的回调函数
  15. --
  16. -- 回调方式为cb(result),result为true表示成功,false或者nil表示失败
  17. -- @return 无
  18. -- @usage pb.setStorage(storage,cb)
  19. function setStorage(storage,cb)
  20. if storage=="SM" or storage=="FD" then
  21. storagecb = cb
  22. req("AT+CPBS=\"" .. storage .. "\"" )
  23. end
  24. end
  25. --- 读取一条电话本记录
  26. -- @number index 电话本在存储区的位置
  27. -- @function cb function类型,读取后的回调函数
  28. --
  29. -- 回调方式为cb(result,name,number):result为true表示成功,false或者nil表示失败;name为姓名;number为号码
  30. -- @usage pb.read(1,cb)
  31. function read(index,cb)
  32. if index == "" or index == nil then
  33. return false
  34. end
  35. readcb = cb
  36. req("AT+CPBR=" .. index)
  37. end
  38. --- 写入一条电话本记录
  39. -- @number index 电话本在存储区的位置
  40. -- @string name 姓名
  41. -- @string num 号码
  42. -- @function cb functionl类型,写入后的回调函数
  43. --
  44. -- 回调方式为cb(result):result为true表示成功,false或者nil表示失败
  45. -- @return 无
  46. -- @usage pb.write(1,"zhangsan","13233334444",cb)
  47. function write(index,name,num,cb)
  48. if num == nil or name == nil or index == nil then
  49. return false
  50. end
  51. writecb = cb
  52. req("AT+CPBW=" .. index .. ",\"" .. num .. "\"," .. "129" .. ",\"" .. name .. "\"" )
  53. return true
  54. end
  55. --- 删除一条电话本记录
  56. -- @number index 电话本在存储区的位置
  57. -- @function cb function类型,删除后的回调函数
  58. --
  59. -- 回调方式为cb(result):result为true表示成功,false或者nil表示失败
  60. -- @return 无
  61. -- @usage pb.delete(1,cb)
  62. function delete(index,cb)
  63. if index == "" or index == nil then
  64. return false
  65. end
  66. deletecb = cb
  67. req("AT+CPBW=" .. index)
  68. return true
  69. end
  70. local function pbrsp(cmd,success,response,intermediate)
  71. local prefix = string.match(cmd,"AT(%+%u+%?*)")
  72. intermediate = intermediate or ""
  73. if prefix == "+CPBR" then
  74. local index = string.match(cmd,"AT%+CPBR%s*=%s*(%d+)")
  75. local num,name = string.match(intermediate,"+CPBR:%s*%d+,\"([#%*%+%d]*)\",%d+,\"(%w*)\"")
  76. num,name = num or "",name or ""
  77. sys.publish("PB_READ_CNF",success,index,num,name)
  78. local cb = readcb
  79. readcb = nil
  80. if cb then cb(success,name,num) return end
  81. elseif prefix == "+CPBW" then
  82. sys.publish("PB_WRITE_CNF",success)
  83. local cb = writecb
  84. writecb = nil
  85. if cb then cb(success) return end
  86. cb = deletecb
  87. deletecb = nil
  88. if cb then cb(success) return end
  89. elseif prefix == "+CPBS?" then
  90. local storage,used,total = string.match(intermediate,"+CPBS:%s*\"(%u+)\",(%d+),(%d+)")
  91. used,total = tonumber(used),tonumber(total)
  92. sys.publish("CPBS_READ_CNF",success,storage,used,total)
  93. elseif prefix == "+CPBS" then
  94. local cb = storagecb
  95. storagecb = nil
  96. if cb then cb(success) return end
  97. end
  98. end
  99. ril.regRsp("+CPBR",pbrsp)
  100. ril.regRsp("+CPBW",pbrsp)
  101. ril.regRsp("+CPBS",pbrsp)
  102. ril.regRsp("+CPBS?",pbrsp)
  103. req("AT+CPBS=\"SM\"")