Skip to content

Commit 379c56f

Browse files
rslater-csCopilot
andauthored
Gid parse fix (#173)
* changed parsing method for gid1 to more robust method * added gid tests * Update src/services/hal/drivers/modem/mode/qmi.lua Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 2331362 commit 379c56f

2 files changed

Lines changed: 220 additions & 15 deletions

File tree

src/services/hal/drivers/modem/mode/qmi.lua

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,29 +52,22 @@ return function(modem)
5252

5353
modem.uim_get_gids = function()
5454
local gids = {}
55-
local key_map = {
56-
["Card result"] = "card-result",
57-
SW1 = 'sw1',
58-
SW2 = 'sw2',
59-
["Read result"] = "read-result"
60-
}
55+
6156
local gid1_ctx = context.with_timeout(modem.ctx, CMD_TIMEOUT)
6257
local gid1_cmd = qmicli.uim_read_transparent(gid1_ctx, modem.primary_port, '0x3F00,0x7FFF,0x6F3E')
6358
gid1_cmd:setpgid(true)
59+
6460
local gid1_out, gid1_cmd_err = gid1_cmd:combined_output()
6561
if gid1_cmd_err then return gids, wraperr.new(gid1_cmd_err) end
6662

67-
local gid1_out_parsed, gid1_parse_err = utils.parse_qmicli_output(gid1_out, key_map)
68-
if gid1_parse_err == nil and gid1_out_parsed["read-result"] then
69-
local gid1
70-
-- unfortnuately the parser incorrectly identifies the hex as a key value pair
71-
-- so f:o:o:b:a:r becomes {f = o:o:b:a:r}
72-
for k, v in pairs(gid1_out_parsed["read-result"]) do
73-
gid1 = k .. ":" .. v
74-
end
75-
gids.gid1 = gid1
63+
-- Anchor parsing to the "Read result:" section and make it nil-safe.
64+
-- Example expected format (simplified): "Read result: 12:34:56:78"
65+
local gid1_hex = gid1_out:match("Read result:%s*([0-9A-Fa-f:]+)")
66+
if not gid1_hex then
67+
return gids, wraperr.new("failed to parse GID1 from qmicli output")
7668
end
7769

70+
gids.gid1 = gid1_hex:gsub(":", "")
7871
return gids
7972
end
8073

tests/test_hal_qmi.lua

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
package.path = "../src/lua-fibers/?.lua;" -- fibers submodule src
2+
.. "../src/lua-trie/src/?.lua;" -- trie submodule src
3+
.. "../src/lua-bus/src/?.lua;" -- bus submodule src
4+
.. "../src/?.lua;"
5+
.. "./test_utils/?.lua;"
6+
.. package.path
7+
.. ";/usr/lib/lua/?.lua;/usr/lib/lua/?/init.lua"
8+
9+
local fiber = require "fibers.fiber"
10+
local context = require "fibers.context"
11+
12+
local function test_uim_get_gids_simple()
13+
-- Clear cached module
14+
package.loaded["services.hal.drivers.modem.mode.qmi"] = nil
15+
16+
-- Mock dependencies
17+
local mock_context = {
18+
with_timeout = function(ctx, timeout)
19+
return ctx
20+
end
21+
}
22+
23+
local mock_cmd = {
24+
setpgid_called = false,
25+
setpgid = function(self, val)
26+
self.setpgid_called = true
27+
end,
28+
combined_output = function(self)
29+
local output = [[
30+
[/dev/cdc-wdm1] Successfully read information from the UIM:
31+
Card result:
32+
SW1: '0x90'
33+
SW2: '0x00'
34+
Read result:
35+
FF]]
36+
return output, nil
37+
end
38+
}
39+
40+
local mock_qmicli = {
41+
uim_read_transparent = function(ctx, port, id)
42+
return mock_cmd
43+
end
44+
}
45+
46+
local mock_wraperr = {
47+
new = function(err) return err end
48+
}
49+
50+
-- Load the qmi module with mocked dependencies
51+
package.loaded["services.hal.drivers.modem.qmicli"] = mock_qmicli
52+
package.loaded["fibers.context"] = mock_context
53+
package.loaded["wraperr"] = mock_wraperr
54+
package.loaded["services.hal.utils"] = require("services.hal.utils")
55+
package.loaded["services.log"] = require("services.log")
56+
57+
local qmi_module = require("services.hal.drivers.modem.mode.qmi")
58+
59+
-- Create a mock modem object
60+
local modem = {
61+
ctx = context.background(),
62+
primary_port = "/dev/cdc-wdm1"
63+
}
64+
65+
-- Initialize the modem with qmi functions
66+
qmi_module(modem)
67+
68+
-- Test the function
69+
local gids, err = modem.uim_get_gids()
70+
71+
assert(err == nil, "expected err to be nil but got " .. tostring(err))
72+
assert(gids ~= nil, "expected gids to be a table but got nil")
73+
assert(gids.gid1 == "FF", "expected gid1 to be 'FF' but got '" .. tostring(gids.gid1) .. "'")
74+
assert(mock_cmd.setpgid_called, "expected setpgid to be called")
75+
end
76+
77+
local function test_uim_get_gids_complex()
78+
-- Clear cached module
79+
package.loaded["services.hal.drivers.modem.mode.qmi"] = nil
80+
81+
-- Mock dependencies
82+
local mock_context = {
83+
with_timeout = function(ctx, timeout)
84+
return ctx
85+
end
86+
}
87+
88+
local mock_cmd = {
89+
setpgid_called = false,
90+
setpgid = function(self, val)
91+
self.setpgid_called = true
92+
end,
93+
combined_output = function(self)
94+
local output = [[
95+
[/dev/cdc-wdm1] Successfully read information from the UIM:
96+
Card result:
97+
SW1: '0x90'
98+
SW2: '0x00'
99+
Read result:
100+
85:FF:FF:FF:FF:FF:47:45:4E:49:45:49:4E:20:20:20:20:20:20:20]]
101+
return output, nil
102+
end
103+
}
104+
105+
local mock_qmicli = {
106+
uim_read_transparent = function(ctx, port, id)
107+
return mock_cmd
108+
end
109+
}
110+
111+
local mock_wraperr = {
112+
new = function(err) return err end
113+
}
114+
115+
-- Load the qmi module with mocked dependencies
116+
package.loaded["services.hal.drivers.modem.qmicli"] = mock_qmicli
117+
package.loaded["fibers.context"] = mock_context
118+
package.loaded["wraperr"] = mock_wraperr
119+
package.loaded["services.hal.utils"] = require("services.hal.utils")
120+
package.loaded["services.log"] = require("services.log")
121+
122+
local qmi_module = require("services.hal.drivers.modem.mode.qmi")
123+
124+
-- Create a mock modem object
125+
local modem = {
126+
ctx = context.background(),
127+
primary_port = "/dev/cdc-wdm1"
128+
}
129+
130+
-- Initialize the modem with qmi functions
131+
qmi_module(modem)
132+
133+
-- Test the function
134+
local gids, err = modem.uim_get_gids()
135+
136+
assert(err == nil, "expected err to be nil but got " .. tostring(err))
137+
assert(gids ~= nil, "expected gids to be a table but got nil")
138+
139+
local expected = "85FFFFFFFFFF47454E4945494E20202020202020"
140+
assert(gids.gid1 == expected,
141+
"expected gid1 to be '" .. expected .. "' but got '" .. tostring(gids.gid1) .. "'")
142+
assert(mock_cmd.setpgid_called, "expected setpgid to be called")
143+
end
144+
145+
local function test_uim_get_gids_error()
146+
-- Clear cached module
147+
package.loaded["services.hal.drivers.modem.mode.qmi"] = nil
148+
149+
-- Mock dependencies
150+
local mock_context = {
151+
with_timeout = function(ctx, timeout)
152+
return ctx
153+
end
154+
}
155+
156+
local mock_cmd = {
157+
setpgid_called = false,
158+
setpgid = function(self, val)
159+
self.setpgid_called = true
160+
end,
161+
combined_output = function(self)
162+
return "", "command failed"
163+
end
164+
}
165+
166+
local mock_qmicli = {
167+
uim_read_transparent = function(ctx, port, id)
168+
return mock_cmd
169+
end
170+
}
171+
172+
local mock_wraperr = {
173+
new = function(err) return "wrapped: " .. err end
174+
}
175+
176+
-- Load the qmi module with mocked dependencies
177+
package.loaded["services.hal.drivers.modem.qmicli"] = mock_qmicli
178+
package.loaded["fibers.context"] = mock_context
179+
package.loaded["wraperr"] = mock_wraperr
180+
package.loaded["services.hal.utils"] = require("services.hal.utils")
181+
package.loaded["services.log"] = require("services.log")
182+
183+
local qmi_module = require("services.hal.drivers.modem.mode.qmi")
184+
185+
-- Create a mock modem object
186+
local modem = {
187+
ctx = context.background(),
188+
primary_port = "/dev/cdc-wdm1"
189+
}
190+
191+
-- Initialize the modem with qmi functions
192+
qmi_module(modem)
193+
194+
-- Test the function
195+
local gids, err = modem.uim_get_gids()
196+
197+
assert(err ~= nil, "expected err to be set but got nil")
198+
assert(err == "wrapped: command failed", "expected wrapped error but got " .. tostring(err))
199+
assert(gids ~= nil, "expected gids to be a table but got nil")
200+
assert(next(gids) == nil, "expected gids to be empty but it has values")
201+
end
202+
203+
fiber.spawn(function ()
204+
test_uim_get_gids_simple()
205+
test_uim_get_gids_complex()
206+
test_uim_get_gids_error()
207+
fiber.stop()
208+
end)
209+
210+
print("running hal qmi tests")
211+
fiber.main()
212+
print("passed")

0 commit comments

Comments
 (0)