1#!/usr/bin/env python
2
3r"""
4PLDM functions.
5"""
6
7import re
8import var_funcs as vf
9import func_args as fa
10import bmc_ssh_utils as bsu
11
12
13def pldmtool(option_string, parse_results=1, **bsu_options):
14    r"""
15    Run pldmtool on the BMC with the caller's option string and return the result.
16
17    Example:
18
19    ${pldm_results}=  Pldmtool  base GetPLDMTypes
20    Rprint Vars  pldm_results
21
22    pldm_results:
23      [request_message]:                              08 01 80 00 04
24      [success_in_creating_the_socket]:               RC = 3
25      [success_in_connecting_to_socket]:              RC = 0
26      [success_in_sending_message_type_as_pldm_to_mctp]:RC = 0
27      [write_to_socket_successful]:                   RC = 5
28      [total_length]:                                 14
29      [loopback_response_message]:                    08 01 80 00 04
30      [on_first_recv(),response_==_request]:          RC = 0
31      [shutdown_socket_successful]:                   RC = 0
32      [response_message]:                             08 01 00 00 04 00 0d 00 00 00 00 00 00 00
33      [supported_types]:
34        [raw]:
35          [0]:                                        0
36          [1]:                                        2
37          [2]:                                        3
38        [text]:
39          [0]:                                        base
40          [1]:                                        platform
41          [2]:                                        bios
42
43    Description of argument(s):
44    option_string                   A string of options which are to be processed by the pldmtool command.
45    parse_results                   Parse the pldmtool results and return a dictionary rather than the raw
46                                    pldmtool output.
47    bsu_options                     Options to be passed directly to bmc_execute_command.  See its prolog for
48                                    details.
49    """
50
51    # This allows callers to specify arguments in python style (e.g. print_out=1 vs. print_out=${1}).
52    bsu_options = fa.args_to_objects(bsu_options)
53
54    stdout, stderr, rc = bsu.bmc_execute_command('pldmtool ' + option_string, **bsu_options)
55
56    if parse_results:
57        # Remove linefeeds following colons.
58        stdout = re.sub(":\n", ":", stdout)
59        # Remove first line (e.g. "Encode request successfully").
60        stdout = re.sub("^.*\\n", "", stdout)
61        result = vf.key_value_outbuf_to_dict(stdout)
62        if 'supported_types' in result:
63            # 'supported types' begins like this:
64            # 0(base) 2(platform) 3(bios)
65            # Parsing it to look like it does in the example above.
66            supported_types = {'raw': [], 'text': []}
67            for entry in result['supported_types'].split(" "):
68                record = entry.split("(")
69                supported_types['raw'].append(record[0])
70                supported_types['text'].append(record[1].rstrip(")"))
71            result['supported_types'] = supported_types
72        return result
73
74    return stdout
75