1# Copyright (c) 2024 Meta Platforms
2# SPDX-License-Identifier: Apache-2.0
3
4import logging
5
6from twister_harness import Shell
7
8logger = logging.getLogger(__name__)
9
10
11def test_sensor_shell_info(shell: Shell):
12    logger.info('send "sensor info" command')
13
14    shell.wait_for_prompt()
15    lines = shell.exec_command('sensor info')
16    assert any(['device name: sensor@0' in line for line in lines]), 'expected response not found'
17    assert any(['device name: sensor@1' in line for line in lines]), 'expected response not found'
18
19    logger.info('response is valid')
20
21
22def test_sensor_shell_get(shell: Shell):
23    logger.info('get "sensor get" command count')
24
25    lines = shell.exec_command('sensor get sensor@0')
26    channel_count = int(lines[-2].split("=")[1].split("(")[0]) + 1
27    logger.info(f'channel count: [{channel_count}]')
28
29    logger.info('send "sensor get" command')
30    for channel in range(channel_count):
31        logger.info(f'channel {channel}')
32        shell.wait_for_prompt()
33        lines = shell.exec_command(f'sensor get sensor@0 {channel}')
34        assert any([f'channel type={channel}' in line for line in lines]), 'expected response not found'
35
36    logger.info('response is valid')
37
38
39def test_sensor_shell_attr_get(shell: Shell):
40    logger.info('send "sensor attr_get" command')
41
42    shell.wait_for_prompt()
43    lines = shell.exec_command('sensor attr_get sensor@0 co2 sampling_frequency')
44    assert any(['sensor@0(channel=co2, attr=sampling_frequency)' in line for line in lines]), 'expected response not found'
45
46    shell.wait_for_prompt()
47    lines = shell.exec_command('sensor attr_get sensor@1 gauge_state_of_health 3')
48    assert any(['sensor@1(channel=gauge_state_of_health, attr=slope_th)' in line for line in lines]), 'expected response not found'
49
50    logger.info('response is valid')
51
52
53def test_sensor_shell_attr_set(shell: Shell):
54    logger.info('send "sensor attr_set" command')
55
56    shell.wait_for_prompt()
57    lines = shell.exec_command('sensor attr_set sensor@0 co2 sampling_frequency 1')
58    expected_line = 'sensor@0 channel=co2, attr=sampling_frequency set to value=1'
59    assert any([expected_line in line for line in lines]), 'expected response not found'
60
61    shell.wait_for_prompt()
62    lines = shell.exec_command('sensor attr_set sensor@1 gauge_state_of_health 3 1')
63    expected_line = 'sensor@1 channel=gauge_state_of_health, attr=slope_th set to value=1'
64    assert any([expected_line in line for line in lines]), 'expected response not found'
65
66    logger.info('response is valid')
67
68
69def test_sensor_shell_trig(shell: Shell):
70    logger.info('send "sensor trig" command')
71
72    shell.wait_for_prompt()
73    lines = shell.exec_command('sensor trig sensor@0 on data_ready')
74    expected_line = 'Enabled trigger idx=1 data_ready on device sensor@0'
75    assert any([expected_line in line for line in lines]), 'expected response not found'
76
77    shell.wait_for_prompt()
78    lines = shell.exec_command('sensor trig sensor@0 off data_ready')
79    expected_line = 'Disabled trigger idx=1 data_ready on device sensor@0'
80    assert any([expected_line in line for line in lines]), 'expected response not found'
81
82    logger.info('response is valid')
83