1#!/usr/bin/env python3
2#
3#   Copyright 2016 - The Android Open Source Project
4#
5#   Licensed under the Apache License, Version 2.0 (the "License");
6#   you may not use this file except in compliance with the License.
7#   You may obtain a copy of the License at
8#
9#       http://www.apache.org/licenses/LICENSE-2.0
10#
11#   Unless required by applicable law or agreed to in writing, software
12#   distributed under the License is distributed on an "AS IS" BASIS,
13#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#   See the License for the specific language governing permissions and
15#   limitations under the License.
16from acts.controllers.monsoon_lib.api.hvpm.monsoon import Monsoon as HvpmMonsoon
17from acts.controllers.monsoon_lib.api.lvpm_stock.monsoon import \
18    Monsoon as LvpmStockMonsoon
19
20ACTS_CONTROLLER_CONFIG_NAME = 'Monsoon'
21ACTS_CONTROLLER_REFERENCE_NAME = 'monsoons'
22
23
24def create(configs):
25    """Takes a list of Monsoon configs and returns Monsoon Controllers.
26
27    Args:
28        configs: A list of serial numbers, or dicts in the form:
29            {
30                'type': anyof('LvpmStockMonsoon', 'HvpmMonsoon')
31                'serial': int
32            }
33
34    Returns:
35        a list of Monsoon configs
36
37    Raises:
38        ValueError if the configuration does not provide the required info.
39    """
40    objs = []
41    for config in configs:
42        monsoon_type = None
43        if isinstance(config, dict):
44            if isinstance(config.get('type', None), str):
45                if 'lvpm' in config['type'].lower():
46                    monsoon_type = LvpmStockMonsoon
47                elif 'hvpm' in config['type'].lower():
48                    monsoon_type = HvpmMonsoon
49                else:
50                    raise ValueError('Unknown monsoon type %s in Monsoon '
51                                     'config %s' % (config['type'], config))
52            if 'serial' not in config:
53                raise ValueError('Monsoon config must specify "serial".')
54            serial_number = int(config.get('serial'))
55        else:
56            serial_number = int(config)
57        if monsoon_type is None:
58            if serial_number < 20000:
59                # This code assumes the LVPM has firmware version 20. If
60                # someone has updated the firmware, or somehow found an older
61                # version, the power measurement will fail.
62                monsoon_type = LvpmStockMonsoon
63            else:
64                monsoon_type = HvpmMonsoon
65
66        objs.append(monsoon_type(serial=serial_number))
67    return objs
68
69
70def destroy(monsoons):
71    for monsoon in monsoons:
72        monsoon.release_monsoon_connection()
73