forked from hotdll/irtu-rda8955
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpb.lua
137 lines (125 loc) · 3.81 KB
/
pb.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
--- 模块功能:电话簿管理
-- @module pb
-- @author openLuat
-- @license MIT
-- @copyright openLuat
-- @release 2018.03.10
module(..., package.seeall)
require"ril"
local req = ril.request
local storagecb,readcb,writecb,deletecb
--- 设置电话本存储区域
-- @string storage, 存储区域字符串,仅支持"ME"和"SM"
-- @param cb, 设置后的回调函数
--
-- 回调方式为cb(result),result为true表示成功,false或者nil表示失败
-- @return 无
-- @usage pb.setStorage(storage,cb)
function setStorage(storage,cb)
if storage=="SM" or storage=="ME" then
storagecb = cb
req("AT+CPBS=\"" .. storage .. "\"" )
end
end
-- 查找用户名是否存在
-- @string name,姓名
-- @return boole,true存在,false不存在
-- @usage pb.find(name)
function find(name)
if name == "" or name == nil then
return false
end
req("AT+CPBF=\"" .. name .. "\"" )
return true
end
--- 读取一条电话本记录
-- @number index,电话本在存储区的位置
-- @function cb,function类型,读取后的回调函数
--
-- 回调方式为cb(result,name,number):result为true表示成功,false或者nil表示失败;name为姓名;number为号码
-- @usage pb.read(1,cb)
function read(index,cb)
if index == "" or index == nil then
return false
end
readcb = cb
req("AT+CPBR=" .. index)
end
--- 写入一条电话本记录
-- @number index,电话本在存储区的位置
-- @string name,姓名
-- @string num,号码
-- @function cb, functionl类型,写入后的回调函数
--
-- 回调方式为cb(result):result为true表示成功,false或者nil表示失败
-- @return 无
-- @usage pb.write(1,"zhangsan","13233334444",cb)
function write(index,name,num,cb)
if num == nil or name == nil or index == nil then
return false
end
writecb = cb
req("AT+CPBW=" .. index .. ",\"" .. num .. "\"," .. "129" .. ",\"" .. name .. "\"" )
return true
end
--- 删除一条电话本记录
-- @number index, 电话本在存储区的位置
-- @function cb, function类型,删除后的回调函数
--
-- 回调方式为cb(result):result为true表示成功,false或者nil表示失败
-- @return 无
-- @usage pb.delete(1,cb)
function delete(index,cb)
if index == "" or index == nil then
return false
end
deletecb = cb
req("AT+CPBW=" .. index)
return true
end
local function pbrsp(cmd,success,response,intermediate)
local prefix = string.match(cmd,"AT(%+%u+%?*)")
intermediate = intermediate or ""
if prefix == "+CPBF" then
local name = string.match(cmd,"AT%+CPBF%s*=%s*\"(%w*)\"")
if intermediate == "" then
sys.publish("PB_FIND_CNF",success,"","",name)
else
for w in string.gmatch(intermediate, "(.-)\r\n") do
local index,n = string.match(w,"+CPBF:%s*(%d+),\"([#%*%+%d]*)\",%d+,")
index = index or ""
n = n or ""
sys.publish("PB_FIND_CNF",success,index,n,name)
end
end
elseif prefix == "+CPBR" then
local index = string.match(cmd,"AT%+CPBR%s*=%s*(%d+)")
local num,name = string.match(intermediate,"+CPBR:%s*%d+,\"([#%*%+%d]*)\",%d+,\"(%w*)\"")
num,name = num or "",name or ""
sys.publish("PB_READ_CNF",success,index,num,name)
local cb = readcb
readcb = nil
if cb then cb(success,name,num) return end
elseif prefix == "+CPBW" then
sys.publish("PB_WRITE_CNF",success)
local cb = writecb
writecb = nil
if cb then cb(success) return end
cb = deletecb
deletecb = nil
if cb then cb(success) return end
elseif prefix == "+CPBS?" then
local storage,used,total = string.match(intermediate,"+CPBS:%s*\"(%u+)\",(%d+),(%d+)")
used,total = tonumber(used),tonumber(total)
sys.publish("CPBS_READ_CNF",success,storage,used,total)
elseif prefix == "+CPBS" then
local cb = storagecb
storagecb = nil
if cb then cb(success) return end
end
end
ril.regRsp("+CPBF",pbrsp)
ril.regRsp("+CPBR",pbrsp)
ril.regRsp("+CPBW",pbrsp)
ril.regRsp("+CPBS",pbrsp)
ril.regRsp("+CPBS?",pbrsp)