1*** Settings ***
2Documentation      Implemented keywords to execute DBUS related commands on BMC.
3
4Resource           resource.robot
5Library            OperatingSystem
6Library            Collections
7
8*** Variable ***
9
10${BUSCTL_TREE_COMMAND}                   busctl tree | less
11${BUSCTL_INTROSPECT_COMMAND}             busctl introspect
12@{dbus_uri_list}
13
14*** Keywords ***
15
16Get DBUS URI List From BMC
17    [Documentation]  Get the available DBUS URIs from device tree on BMC.
18    [Arguments]  ${service_name}  ${dbus_url}
19
20    # Return the dbus uris corresponding to the service name provided.
21    # Description of argument(s):
22    #    service_name     Any service uri of the dbus.
23    #                        Eg : xyz.openbmc_project.FruDevice
24    #    dbus_url         Any dbus url of the dbus.
25    #                        Eg : /xyz/openbmc_project/FruDevice
26
27    # Execute dbus tree command
28    ${bmc_response}=  BMC Execute Command  ${BUSCTL_TREE_COMMAND}
29    ${bmc_response}=  Convert To List  ${bmc_response}
30    ${bmc_response_output}=  Get From List  ${bmc_response}  0
31    ${bmc_response_output_list}=  Split String  ${bmc_response_output}  \n\n
32    # Identify the offset of the service name in the response.
33    ${service_name_index_value}=  get_subsequent_value_from_list  ${bmc_response_output_list}  ${service_name}
34    ${service_name_index_value}=  Get From List  ${service_name_index_value}  0
35
36    # Get the service name and its corresponding URI's.
37    ${service_name_with_uri_list}=  Get From List  ${bmc_response_output_list}  ${service_name_index_value}
38    ${service_name_with_uri_list}=  Split String  ${service_name_with_uri_list}  \n
39
40    # Find index of all the uris where the dbus URI matched.
41    ${uri_list_index}=  get_subsequent_value_from_list  ${service_name_with_uri_list}  ${dbus_url}
42    FOR  ${list_index}  IN  @{uri_list_index}
43        # For each index, get the URI and append to list
44        ${dbus_uri}=  Get From List  ${service_name_with_uri_list}  ${list_index}
45        Append To List  ${dbus_uri_list}  ${dbus_uri}
46    END
47
48    [Return]  ${dbus_uri_list[1:]}
49
50
51Fetch DBUS URI List Without Unicode
52    [Documentation]  Gets the list of DBUS URI for the service and returns only sub URIs.
53    [Arguments]  ${dbus_uri_list}
54
55    # Return the dbus uris list without the unicodes.
56    # Description of argument(s):
57    #    dbus_uri_list      List of all the uris for the corresponding service name.
58    #    Example:    Converts "  ├─/xyz/openbmc_project/FruDevice/device_0",
59    #                ...  to '/xyz/openbmc_project/FruDevice/device_0'
60
61    @{dbus_list}=  Create List
62    FOR  ${item}  IN  @{dbus_uri_list}
63        ${item}=  Set Variable  ${item.strip()}
64        ${item}=  return_decoded_string  ${item}
65        Append To List  ${dbus_list}  ${item}
66    END
67
68    [Return]  ${dbus_list}
69
70
71Execute DBUS Introspect Command
72    [Documentation]  Execute the DBUS introspect command and return response.
73    [Arguments]  ${dbus_command}
74
75    # Execute the busctl instrospect command for the service name and dbus uri.
76    # Description of argument(s):
77    #   dbus_command    Command with service name and dbus uri for the fru device.
78    #      Example :    xyz.openbmc_project.FruDevice xyz/openbmc_project/FruDevice/device_0
79
80    ${cmd}=  Catenate  ${BUSCTL_INTROSPECT_COMMAND} ${dbus_command}
81    ${resp}=  BMC Execute Command  ${cmd}
82
83    [Return]  ${resp[0]}
84