fc7649f3c7e9829110bfb1834730f8be3d7eb4a5
[apps/agl-service-can-low-level.git] / test / afb-test / tests / low-can_J1939Test.lua
1
2 --[[
3     Copyright (C) 2018 "IoT.bzh"
4     Author Arthur Guyader <arthur.guyader@iot.bzh>
5
6     Licensed under the Apache License, Version 2.0 (the "License");
7     you may not use this file except in compliance with the License.
8     You may obtain a copy of the License at
9
10       http://www.apache.org/licenses/LICENSE-2.0
11
12     Unless required by applicable law or agreed to in writing, software
13     distributed under the License is distributed on an "AS IS" BASIS,
14     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15     See the License for the specific language governing permissions and
16     limitations under the License.
17
18
19     NOTE: strict mode: every global variables should be prefixed by '_'
20 --]]
21
22
23 _AFT.setBeforeAll(function()
24     local can = io.open("/sys/class/net/can2")
25     if can == nil then
26         print("# You do not have 'can2' device set. Please run the following command:\n### sudo modprobe vcan; sudo ip link add can2 type vcan; sudo ip link set can2 up ")
27         return -1
28     end
29     return 0
30 end)
31
32 _AFT.setBeforeEach(function() print("~~~~~ Begin Test ~~~~~") end)
33
34 _AFT.setAfterEach(function()
35     os.execute("pkill canplayer")
36     os.execute("pkill linuxcan-canpla")
37     print("~~~~~ End Test ~~~~~")
38 end)
39
40 _AFT.setAfterAll( function()
41     os.execute("pkill canplayer")
42     os.execute("pkill linuxcan-canpla")
43     return 0
44 end)
45
46 --[[
47 ###########################################################
48 ####################### J1939 TESTS #######################
49 ###########################################################
50 ]] 
51
52 print("\n##########################################")
53 print("#########    BEGIN J1939 TESTS   #########")
54 print("##########################################\n")
55
56 --[[
57 +++++++++++++++++++++++++++
58 Tests subscribe/unsubscribe
59 +++++++++++++++++++++++++++
60 ]]
61
62
63 --print("\n++++++++++++++++++++++++++++++++++++++++++")
64 --print("++++  TESTS SUBSCRIBE / UNSUBSCRIBE   ++++")
65 --print("++++++++++++++++++++++++++++++++++++++++++\n")
66
67 _AFT.describe("Test subscribe/unsubscribe", function()
68 _AFT.testVerbStatusSuccess("low-can_subscribe_j1939_event", "low-can", "subscribe", {event= "Eng.Momentary.Overspeed.Enable"})
69 _AFT.testVerbStatusSuccess("low-can_subscribe_j1939_events", "low-can", "subscribe", {event= "Eng.*"})
70 _AFT.testVerbStatusSuccess("low-can_subscribe_j1939_pgn", "low-can", "subscribe", {pgn= 61442})
71 _AFT.testVerbStatusSuccess("low-can_subscribe_j1939_all_pgn", "low-can", "subscribe", {pgn= "*"})
72 _AFT.testVerbStatusSuccess("low-can_subscribe_j1939_id", "low-can", "subscribe", {id= 61442})
73 _AFT.testVerbStatusSuccess("low-can_subscribe_j1939_all_id", "low-can", "subscribe", {id= "*"})
74 _AFT.testVerbStatusSuccess("low-can_subscribe_j1939_event", "low-can", "unsubscribe", {event= "Eng.Momentary.Overspeed.Enable"})
75 _AFT.testVerbStatusSuccess("low-can_subscribe_j1939_event", "low-can", "unsubscribe", {event= "*"})
76 _AFT.testVerbStatusSuccess("low-can_subscribe_j1939_id", "low-can", "subscribe", {id= 61442})
77 _AFT.testVerbStatusError("low-can_subscribe_j1939_no_event", "low-can", "subscribe", {event= ""})
78 _AFT.testVerbStatusError("low-can_subscribe_j1939_no_event", "low-can", "subscribe", {id= ""})
79 _AFT.testVerbStatusError("low-can_subscribe_j1939_no_event", "low-can", "subscribe", {pgn= ""})
80 end)
81
82
83 --[[
84 +++++++++++
85 Tests write
86 +++++++++++
87 ]] 
88
89 --print("\n++++++++++++++++++++++++++")
90 --print("++++   TESTS WRITE    ++++")
91 --print("++++++++++++++++++++++++++\n")
92
93 _AFT.describe("Test write", function()
94 _AFT.testVerbStatusError("low-can_write_wo_auth", "low-can", "write", { signal_name = "Eng.Momentary.Overspeed.Enable", signal_value = 1})
95 _AFT.testVerbStatusSuccess("low-can_auth", "low-can", "auth", {})
96 _AFT.testVerbStatusSuccess("low-can_write_signal", "low-can", "write", { signal_name = "Eng.Momentary.Overspeed.Enable", signal_value = 1})
97 _AFT.testVerbStatusSuccess("low-can_write_frame", "low-can", "write", { bus_name= "j1939", frame= { pgn= 62420, length=8, data= {1,2,3,4,5,6,7,8}}})
98 _AFT.testVerbStatusSuccess("low-can_write_frame_other_pgn", "low-can", "write", { bus_name= "j1939", frame= { pgn= 126208, length=8, data= {9,10,11,12,13,14,15,16}}})
99 _AFT.testVerbStatusError("low-can_write_frame_invalid_pgn", "low-can", "write", { bus_name= "j1939", frame= { pgn= 1234, length=8, data= {9,10,11,12,13,14,15,16}}})
100 _AFT.testVerbStatusSuccess("low-can_write_multi_frame", "low-can", "write", { bus_name= "j1939", frame= { pgn= 126208, length=9, data= {9,10,11,12,13,14,15,16,17}}})
101 end)
102 --[[
103 ++++++++++
104 Tests read
105 ++++++++++
106 ]] 
107
108 local api = "low-can"
109 local evt = "messages.Eng.Momentary.Overspeed.Enable"
110 local evt2 = "messages.Actl.Eng.Prcnt.Trque.High.Resolution"
111
112 _AFT.describe("Test subscribe read frame", function()
113     _AFT.addEventToMonitor(api .. "/" ..evt, function(eventname,data)
114         _AFT.assertEquals(eventname, api.."/"..evt)
115     end)
116
117     _AFT.assertVerbStatusSuccess(api ,"subscribe", { event = evt})
118     local ret = os.execute("bash ".._AFT.bindingRootDir.."/var/replay_launcher.sh ".._AFT.bindingRootDir.."/var/testj1939.canreplay");
119     _AFT.assertIsTrue(ret)
120
121     _AFT.assertEvtReceived(api .. "/" ..evt, 1000000)
122 end,
123 nil,
124 function()
125     _AFT.callVerb(api, "unsubscribe", { event = evt})
126 end)
127
128 ------------------------------------------------------------------------------
129
130 _AFT.describe("Test subscribe not read all frame", function()
131     _AFT.addEventToMonitor(api .. "/" ..evt2, function(eventname,data)
132         _AFT.assertEquals(eventname, api.."/"..evt2)
133     end)
134
135     _AFT.assertVerbStatusSuccess(api ,"subscribe", { event = evt2})
136     local ret = os.execute("bash ".._AFT.bindingRootDir.."/var/replay_launcher.sh ".._AFT.bindingRootDir.."/var/testj1939.canreplay");
137     _AFT.assertIsTrue(ret)
138
139     _AFT.assertEvtNotReceived(api .. "/" ..evt2, 1000000)
140 end,
141 nil,
142 function()
143     _AFT.callVerb(api, "unsubscribe", { event = evt2})
144 end)
145
146
147 ---------------
148
149 _AFT.describe("Test subscribe read frame low time", function()
150     _AFT.addEventToMonitor(api .. "/" ..evt, function(eventname,data)
151         _AFT.assertEquals(eventname, api.."/"..evt)
152     end)
153
154     _AFT.assertVerbStatusSuccess(api ,"subscribe", { event = evt})
155     local ret = os.execute("bash ".._AFT.bindingRootDir.."/var/replay_launcher.sh ".._AFT.bindingRootDir.."/var/testj1939.canreplay");
156     _AFT.assertIsTrue(ret)
157 end,
158 nil,
159 function()
160     _AFT.callVerb(api, "unsubscribe", { event = evt})
161 end)
162
163 _AFT.exitAtEnd()