pb.lua 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. --- SIM电话簿管理
  2. -- @module cc
  3. -- @author 黄洵
  4. -- @license MIT
  5. -- @copyright yunpolice.cn
  6. -- @release 2018.03.10
  7. module(..., package.seeall)
  8. local base = _G
  9. local ril = require"ril"
  10. local sys = require"sys"
  11. local print,tonumber,req = base.print,base.tonumber,ril.request
  12. --local smatch = string.match
  13. local storagecb,readcb,writecb,deletecb
  14. --- 设置电话本存储区域
  15. -- @param str, string类型,存储区域字符串,仅支持"ME"和"SM"
  16. -- @param cb, 设置后的回调函数
  17. -- @return 无
  18. -- @usage pb.setsgorage(str,cb)
  19. function setstorage(str,cb)
  20. if str=="SM" or str=="ME" then
  21. storagecb = cb
  22. req("AT+CPBS=\"" .. str .. "\"" )
  23. end
  24. end
  25. --- 查找用户名是否存在
  26. -- @param name,string类型
  27. -- @return boole,true存在,false不存在
  28. -- @usage pb.find(name)
  29. function find(name)
  30. if name == "" or name == nil then
  31. return false
  32. end
  33. req("AT+CPBF=\"" .. name .. "\"" )
  34. return true
  35. end
  36. --- 读取一条电话本记录
  37. -- @param index, number类型,电话本在存储区的位置
  38. -- @param cb,读取后的回调函数
  39. -- @usage 无
  40. -- @usage pb.read(1,cb)
  41. function read(index,cb)
  42. if index == "" or index == nil then
  43. return false
  44. end
  45. readcb = cb
  46. req("AT+CPBR=" .. index)
  47. end
  48. --- 写一条电话本记录
  49. -- @param index, number类型,电话本在存储区的位置
  50. -- @param name, string类型 ,姓名
  51. -- @param num, number or string类型,号码
  52. -- @param cb, functionl类型,写入后的回调函数
  53. -- @return 无
  54. -- @usage pb.writeitem(1,zhangsan,13233334444,cb)
  55. function writeitem(index,name,num,cb)
  56. if num == nil or name == nil or index == nil then
  57. return false
  58. end
  59. writecb = cb
  60. req("AT+CPBW=" .. index .. ",\"" .. num .. "\"," .. "129" .. ",\"" .. name .. "\"" )
  61. return true
  62. end
  63. --- 删除一条电话本记录
  64. -- @param i, number类型,电话本在存储区的位置
  65. -- @param cb, function类型,删除后的回调函数
  66. -- @return 无
  67. -- @usage pb.deleteitem(1,cb)
  68. function deleteitem(i,cb)
  69. if i == "" or i == nil then
  70. return false
  71. end
  72. deletecb = cb
  73. req("AT+CPBW=" .. i)
  74. return true
  75. end
  76. local function pbrsp(cmd,success,response,intermediate)
  77. local prefix = string.match(cmd,"AT(%+%u+%?*)")
  78. intermediate = intermediate or ""
  79. if prefix == "+CPBF" then
  80. local name = string.match(cmd,"AT%+CPBF%s*=%s*\"(%w*)\"")
  81. if intermediate == "" then
  82. sys.subscribe("PB_FIND_CNF",success,"","",name)
  83. else
  84. for w in string.gmatch(intermediate, "(.-)\r\n") do
  85. local index,n = string.match(w,"+CPBF:%s*(%d+),\"([#%*%+%d]*)\",%d+,")
  86. index = index or ""
  87. n = n or ""
  88. sys.subscribe("PB_FIND_CNF",success,index,n,name)
  89. end
  90. end
  91. elseif prefix == "+CPBR" then
  92. local index = string.match(cmd,"AT%+CPBR%s*=%s*(%d+)")
  93. local num,name = string.match(intermediate,"+CPBR:%s*%d+,\"([#%*%+%d]*)\",%d+,\"(%w*)\"")
  94. num,name = num or "",name or ""
  95. sys.subscribe("PB_READ_CNF",success,index,num,name)
  96. local cb = readcb
  97. readcb = nil
  98. if cb then cb(success,name,num) return end
  99. elseif prefix == "+CPBW" then
  100. sys.subscribe("PB_WRITE_CNF",success)
  101. local cb = writecb
  102. writecb = nil
  103. if cb then cb(success) return end
  104. cb = deletecb
  105. deletecb = nil
  106. if cb then cb(success) return end
  107. elseif prefix == "+CPBS?" then
  108. local storage,used,total = string.match(intermediate,"+CPBS:%s*\"(%u+)\",(%d+),(%d+)")
  109. used,total = tonumber(used),tonumber(total)
  110. sys.subscribe("CPBS_READ_CNF",success,storage,used,total)
  111. elseif prefix == "+CPBS" then
  112. local cb = storagecb
  113. storagecb = nil
  114. if cb then cb(success) return end
  115. end
  116. end
  117. ril.regrsp("+CPBF",pbrsp)
  118. ril.regrsp("+CPBR",pbrsp)
  119. ril.regrsp("+CPBW",pbrsp)
  120. ril.regrsp("+CPBS",pbrsp)
  121. ril.regrsp("+CPBS?",pbrsp)