1import xml.etree.ElementTree as ET
2import tempfile
3
4DEVICE_PATH = '/vendor/etc/powerhint.xml'
5
6
7def restart_power_hal(target):
8  """Kill power HAL service so it can pick up new values in powerhint.xml."""
9  target.execute('pkill -f android\.hardware\.power')
10
11
12def set_touch_param(target, opcode, new_val):
13  """Set a new value for the touch hint parameter with the specified opcode."""
14  hinttype = '0x1A00'
15
16  # Get current powerhint.xml file
17  with tempfile.NamedTemporaryFile() as tmp:
18    target.pull(DEVICE_PATH, tmp.name)
19
20    # Replace current parameter value
21    tree = ET.parse(tmp.name)
22    xpath = './Hint[@type="{}"]/Resource[@opcode="{}"]'.format(hinttype, opcode)
23    tree.findall(xpath)[0].set('value', '{:#x}'.format(new_val))
24
25    # Write new powerhint.xml file to device
26    tree.write(tmp.name)
27    target.push(tmp.name, DEVICE_PATH)
28
29  # Restart power HAL to pick up new value
30  restart_power_hal(target)
31
32
33def set_touch_boost(target, boost=50):
34  """Change the top-app schedtune.boost value to use after touch events."""
35  opcode = '0x42C18000'
36  if boost < 0:
37    boost = 100-boost
38  set_touch_param(target, opcode, boost)
39
40
41def set_touch_min_freq(target, cluster, freq=1100):
42  """Change the CPU cluster min frequency (in Mhz) to use after touch events."""
43  opcode = '0x40800000' if cluster == 'big' else '0x40800100'
44  set_touch_param(target, opcode, freq)
45
46
47def set_touch_cpubw_hysteresis(target, enable=False):
48  """Set whether to leave CPUBW hysteresis enabled after touch events."""
49  opcode = '0x4180C000'
50  enable_num = 1 if enable else 0
51  set_touch_param(target, opcode, enable_num)
52
53
54def set_touch_cpubw_min_freq(target, freq=51):
55  """Set CPUBW min freq used after touch events. See mapping in msm8998.dtsi."""
56  opcode = '0x41800000'
57  set_touch_param(target, opcode, freq)
58
59
60def restore_defaults(target, powerhint_host_path):
61  """Restore default power hint settings using a powerhint.xml file from the host."""
62  target.push(powerhint_host_path, DEVICE_PATH)
63
64  restart_power_hal(target)
65
66
67def disable_launch_hint(target):
68  """Turn off all launch hint tweaks."""
69  hinttype = '0x1B00'
70
71  with tempfile.NamedTemporaryFile() as tmp:
72    target.pull(DEVICE_PATH, tmp.name)
73
74    tree = ET.parse(tmp.name)
75    xpath = './Hint[@type="{}"]'.format(hinttype)
76    launch_hints = tree.findall(xpath)[0]
77    for child in launch_hints.findall('./Resource'):
78      launch_hints.remove(child)
79
80    tree.write(tmp.name)
81    target.push(tmp.name, DEVICE_PATH)
82
83  restart_power_hal(target)
84