1#!/usr/bin/env python3
2#
3# Copyright (C) 2016 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may not
6# use this file except in compliance with the License. You may obtain a copy of
7# 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, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations under
15# the License.
16"""
17Script for initializing a cmd line tool for PTS and other purposes.
18Required custom config parameters:
19'target_mac_address': '00:00:00:00:00:00'
20
21Optional config parameters:
22'sim_conf_file' : '/path_to_config/'
23"""
24from acts_contrib.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest
25from cmd_input import CmdInput
26
27import os
28
29from acts_contrib.test_utils.tel.tel_test_utils import setup_droid_properties
30
31
32class BtCmdLineTest(BluetoothBaseTest):
33    target_mac_address = ""
34
35    def setup_class(self):
36        super().setup_class()
37        if not "target_mac_address" in self.user_params.keys():
38            self.log.warning("Missing user config \"target_mac_address\"!")
39            self.target_mac_address = ""
40        else:
41            self.target_mac_address = self.user_params[
42                "target_mac_address"].upper()
43
44        self.android_devices[0].droid.bluetoothSetLocalName("CMD LINE Test")
45        if len(self.android_devices) > 1:
46            #Setup for more complex testcases
47            if not "sim_conf_file" in self.user_params.keys():
48                self.log.error(
49                    "Missing mandatory user config \"sim_conf_file\"!")
50                return False
51            sim_conf_file = self.user_params["sim_conf_file"][0]
52            # If the sim_conf_file is not a full path, attempt to find it
53            # relative to the config file.
54            if not os.path.isfile(sim_conf_file):
55                sim_conf_file = os.path.join(
56                    self.user_params[Config.key_config_path.value],
57                    sim_conf_file)
58                if not os.path.isfile(sim_conf_file):
59                    log.error("Unable to load user config " + sim_conf_file +
60                              " from test config file.")
61                    return False
62            for ad in self.android_devices:
63                setup_droid_properties(self.log, ad, sim_conf_file)
64            music_path_str = "music_path"
65            android_music_path = "/sdcard/Music/"
66            if music_path_str not in self.user_params:
67                self.log.error("Need music for A2DP testcases")
68                return False
69            music_path = self.user_params[music_path_str]
70            self._add_music_to_primary_android_device(music_path,
71                                                      android_music_path)
72        return True
73
74    def _add_music_to_primary_android_device(self, music_path,
75                                             android_music_path):
76        music_list = []
77        for dirname, dirnames, filenames in os.walk(music_path):
78            for filename in filenames:
79                file = os.path.join(dirname, filename)
80                #TODO: Handle file paths with spaces
81                self.android_devices[0].adb.push("{} {}".format(
82                    file, android_music_path))
83
84    def test_pts_cmd_line_helper(self):
85        cmd_line = CmdInput()
86        cmd_line.setup_vars(self.android_devices, self.target_mac_address,
87                            self.log)
88        cmd_line.cmdloop()
89        return True
90