1#!/usr/bin/env python3
2#
3# Copyright (c) 2022 - 2023 Intel Corporation
4#
5# SPDX-License-Identifier: Apache-2.0
6
7"""
8Utilities for MIPI Sys-T Collateral XML data
9"""
10
11from xml.sax.saxutils import escape
12
13
14XML_HEADER = """
15<?xml version="1.0" encoding="utf-8"?>
16<syst:Collateral xmlns:syst="http://www.mipi.org/1.0/sys-t"
17    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18    xsi:schemaLocation="http://www.mipi.org/1.0/sys-t
19                        https://www.mipi.org/schema/sys-t/sys-t_1-0.xsd">
20
21  <syst:Client Name="Zephyr">
22"""
23
24XML_FOOTER = """
25  </syst:Client>
26
27</syst:Collateral>
28"""
29
30XML_MODULE_HEADER = """
31    <syst:Modules>"""
32
33XML_MODULE_EACH = """
34      <syst:Module ID="{0}"><![CDATA[{1}]]></syst:Module>"""
35
36XML_MODULE_FOOTER = """
37    </syst:Modules>
38"""
39
40XML_CATALOG32_HEADER = """
41    <syst:Catalog32>"""
42
43XML_CATALOG32_EACH = """
44      <syst:Format ID="0x{0:08x}"><![CDATA[{1}]]></syst:Format>"""
45
46XML_CATALOG32_FOOTER = """
47    </syst:Catalog32>
48"""
49
50XML_CATALOG64_HEADER = """
51    <syst:Catalog64>"""
52
53XML_CATALOG64_EACH = """
54      <syst:Format ID="0x{0:016x}"><![CDATA[{1}]]></syst:Format>"""
55
56XML_CATALOG64_FOOTER = """
57    </syst:Catalog64>
58"""
59
60XML_GUIDS = """
61    <syst:Guids>
62      <syst:Guid   ID="{00000000-0000-0000-0000-000000000000}"
63                 Mask="{00000000-0000-0000-FF00-000000000000}"><![CDATA[Zephyr]]></syst:Guid>
64    </syst:Guids>
65"""
66
67
68def __gen_syst_modules(database):
69    """
70    Generate syst:Modules, which corresponds to log source ID and name
71    """
72    if 'log_subsys' not in database.database:
73        return ""
74
75    if 'log_instances' not in database.database['log_subsys']:
76        return ""
77
78    instances = database.database['log_subsys']['log_instances']
79
80    if not instances:
81        # Empty dictionary: no instances
82        return ""
83
84    xml = XML_MODULE_HEADER
85
86    for _, one_inst in instances.items():
87        xml += XML_MODULE_EACH.format(one_inst['source_id'], escape(one_inst['name']))
88
89    xml += XML_MODULE_FOOTER
90
91    return xml
92
93
94def __gen_syst_catalog(database):
95    """
96    Generate syst:Catalog, which corresponds to log strings
97    """
98    if not database.has_string_mappings():
99        return ""
100
101    if database.is_tgt_64bit():
102        xml = XML_CATALOG64_HEADER
103        fmt = XML_CATALOG64_EACH
104    else:
105        xml = XML_CATALOG32_HEADER
106        fmt = XML_CATALOG32_EACH
107
108    for addr, one_str in database.get_string_mappings().items():
109        xml += fmt.format(addr, one_str)
110
111    if database.is_tgt_64bit():
112        xml += XML_CATALOG64_FOOTER
113    else:
114        xml += XML_CATALOG32_FOOTER
115
116    return xml
117
118
119def gen_syst_xml_file(database):
120    """
121    Generate MIPI Sys-T Collateral XML data
122    """
123    xml = XML_HEADER
124
125    xml += XML_GUIDS
126
127    xml += __gen_syst_modules(database)
128
129    xml += __gen_syst_catalog(database)
130
131    xml += XML_FOOTER
132
133    return xml
134