1*** Settings ***
2Documentation   This module is for IPMI client for copying ipmitool to
3...             openbmc box and execute ipmitool IPMI standard
4...             command. IPMI raw command will use dbus-send command
5Resource        ../lib/resource.txt
6Resource        ../lib/connection_client.robot
7Library         String
8
9*** Variables ***
10${dbusHostIpmicmd1}=   dbus-send --system  ${OPENBMC_BASE_URI}HostIpmi/1
11${dbusHostIpmiCmdReceivedMsg}=   ${OPENBMC_BASE_DBUS}.HostIpmi.ReceivedMessage
12${netfnByte}=          ${EMPTY}
13${cmdByte}=            ${EMPTY}
14${arrayByte}=          array:byte:
15${IPMI_EXT_CMD}=       ipmitool -I lanplus -C 1 -P
16${HOST}=               -H
17${RAW}=                raw
18
19*** Keywords ***
20
21Run IPMI Command
22    [Arguments]    ${args}
23    ${resp}=     Run Keyword If   '${IPMI_COMMAND}'=='External'
24    ...    Run External IPMI RAW Command   ${args}
25    ...          ELSE IF          '${IPMI_COMMAND}'=='Dbus'
26    ...    Run Dbus IPMI RAW Command   ${args}
27    ...          ELSE             Fail
28    ...    msg=Invalid IPMI Command type provided : ${IPMI_COMMAND}
29    [Return]    ${resp}
30
31Run IPMI Standard Command
32    [Arguments]    ${args}
33    ${resp}=     Run Keyword If   '${IPMI_COMMAND}'=='External'
34    ...    Run External IPMI Standard Command   ${args}
35    ...          ELSE IF          '${IPMI_COMMAND}'=='Dbus'
36    ...    Run Dbus IPMI Standard Command   ${args}
37    ...          ELSE             Fail
38    ...    msg=Invalid IPMI Command type provided : ${IPMI_COMMAND}
39
40    [Return]    ${resp}
41
42Run Dbus IPMI RAW Command
43    [Arguments]    ${args}
44    ${valueinBytes}=   Byte Conversion  ${args}
45    ${cmd}=   Catenate   ${dbushostipmicmd1} ${dbusHostIpmiCmdReceivedMsg}
46    ${cmd}=   Catenate   ${cmd} ${valueinBytes}
47    ${output}   ${stderr}=  Execute Command  ${cmd}  return_stderr=True
48    Should Be Empty      ${stderr}
49    set test variable    ${OUTPUT}     "${output}"
50
51Run Dbus IPMI Standard Command
52    [Arguments]    ${args}
53    Copy ipmitool
54    ${stdout}    ${stderr}    ${output}=  Execute Command
55    ...    /tmp/ipmitool -I dbus ${args}    return_stdout=True
56    ...    return_stderr= True    return_rc=True
57    Should Be Equal    ${output}    ${0}    msg=${stderr}
58    [Return]    ${stdout}
59
60Run External IPMI RAW Command
61    [Arguments]    ${args}
62    ${ipmi_raw_cmd}=   Catenate  SEPARATOR=
63    ...    ${IPMI_EXT_CMD}${SPACE}${IPMI_PASSWORD}${SPACE}
64    ...    ${HOST}${SPACE}${OPENBMC_HOST}${SPACE}${RAW}${SPACE}${args}
65    ${rc}    ${output}=    Run and Return RC and Output    ${ipmi_raw_cmd}
66    Should Be Equal    ${rc}    ${0}    msg=${output}
67    [Return]    ${output}
68
69Run External IPMI Standard Command
70    [Arguments]    ${args}
71    ${ipmi_cmd}=   Catenate  SEPARATOR=
72    ...    ${IPMI_EXT_CMD}${SPACE}${IPMI_PASSWORD}${SPACE}
73    ...    ${HOST}${SPACE}${OPENBMC_HOST}${SPACE}${args}
74    ${rc}    ${output}=    Run and Return RC and Output    ${ipmi_cmd}
75    Should Be Equal    ${rc}    ${0}    msg=${output}
76    [Return]   ${output}
77
78
79Byte Conversion
80    [Documentation]   Byte Conversion method receives IPMI RAW commands as
81    ...               argument in string format.
82    ...               Sample argument is as follows
83    ...               "0x04 0x30 9 0x01 0x00 0x35 0x00 0x00 0x00 0x00 0x00
84    ...               0x00"
85    ...               IPMI RAW command format is as follows
86    ...               <netfn Byte> <cmd Byte> <Data Bytes..>
87    ...               This method converts IPMI command format into
88    ...               dbus command format  as follows
89    ...               <byte:seq-id> <byte:netfn> <byte:lun> <byte:cmd>
90    ...               <array:byte:data>
91    ...               Sample dbus  Host IPMI Received Message argument
92    ...               byte:0x00 byte:0x04 byte:0x00 byte:0x30
93    ...               array:byte:9,0x01,0x00,0x35,0x00,0x00,0x00,0x00,0x00,0x00
94    [Arguments]     ${args}
95    ${argLength}=   Get Length  ${args}
96    Set Global Variable  ${arrayByte}   array:byte:
97    @{listargs}=   Split String  ${args}
98    ${index}=   Set Variable   ${0}
99    :FOR   ${word}   in   @{listargs}
100    \    Run Keyword if   ${index} == 0   Set NetFn Byte  ${word}
101    \    Run Keyword if   ${index} == 1   Set Cmd Byte    ${word}
102    \    Run Keyword if   ${index} > 1    Set Array Byte  ${word}
103    \    ${index}=    Set Variable    ${index + 1}
104    ${length}=   Get Length  ${arrayByte}
105    ${length}=   Evaluate  ${length} - 1
106    ${arrayByteLocal}=  Get Substring  ${arrayByte}  0   ${length}
107    Set Global Variable  ${arrayByte}   ${arrayByteLocal}
108    ${valueinBytesWithArray}=   Catenate  byte:0x00   ${netfnByte}  byte:0x00
109    ${valueinBytesWithArray}=   Catenate  ${valueinBytesWithArray}  ${cmdByte}
110    ${valueinBytesWithArray}=   Catenate  ${valueinBytesWithArray} ${arrayByte}
111    ${valueinBytesWithoutArray}=   Catenate  byte:0x00 ${netfnByte}  byte:0x00
112    ${valueinBytesWithoutArray}=   Catenate  ${valueinBytesWithoutArray} ${cmdByte}
113#   To Check scenario for smaller IPMI raw commands with only 2 arguments
114#   instead of usual 12 arguments.
115#   Sample small IPMI raw command: Run IPMI command 0x06 0x36
116#   If IPMI raw argument length is only 9 then return value in bytes without
117#   array population.
118#   Equivalent dbus-send argument for smaller IPMI raw command:
119#   byte:0x00 byte:0x06 byte:0x00 byte:0x36
120    Run Keyword if   ${argLength} == 9     Return from Keyword    ${valueinBytesWithoutArray}
121    [Return]    ${valueinBytesWithArray}
122
123
124Set NetFn Byte
125   [Arguments]    ${word}
126   ${netfnByteLocal}=  Catenate   byte:${word}
127   Set Global Variable  ${netfnByte}  ${netfnByteLocal}
128
129Set Cmd Byte
130   [Arguments]    ${word}
131   ${cmdByteLocal}=  Catenate   byte:${word}
132   Set Global Variable  ${cmdByte}  ${cmdByteLocal}
133
134Set Array Byte
135   [Arguments]    ${word}
136   ${arrayByteLocal}=   Catenate   SEPARATOR=  ${arrayByte}  ${word}
137   ${arrayByteLocal}=   Catenate   SEPARATOR=  ${arrayByteLocal}   ,
138   Set Global Variable  ${arrayByte}   ${arrayByteLocal}
139
140Copy ipmitool
141    OperatingSystem.File Should Exist   tools/ipmitool      msg=The ipmitool program could not be found in the tools directory. It is not part of the automation code by default. You must manually copy or link the correct openbmc version of the tool in to the tools directory in order to run this test suite.
142
143    Import Library      SCPLibrary      WITH NAME       scp
144    scp.Open connection     ${OPENBMC_HOST}     username=${OPENBMC_USERNAME}      password=${OPENBMC_PASSWORD}
145    scp.Put File    tools/ipmitool   /tmp
146    SSHLibrary.Open Connection     ${OPENBMC_HOST}
147    Login   ${OPENBMC_USERNAME}    ${OPENBMC_PASSWORD}
148    Execute Command     chmod +x /tmp/ipmitool
149