• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2#
3#   Copyright 2018 - 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.
16
17import collections
18import json
19import logging
20import math
21import os
22import random
23import re
24import requests
25import socket
26import time
27
28from urllib.parse import urlparse
29
30from acts import utils
31from acts.libs.proc import job
32
33
34class DeviceOffline(Exception):
35    """Exception if the device is no longer reachable via the network."""
36
37
38class BaseLib():
39    def __init__(self, addr, tc, client_id):
40        self.address = addr
41        self.test_counter = tc
42        self.client_id = client_id
43
44    def build_id(self, test_id):
45        """Concatenates client_id and test_id to form a command_id.
46
47        Args:
48            test_id: string, unique identifier of test command.
49        """
50        return self.client_id + "." + str(test_id)
51
52    def send_command(self, test_id, test_cmd, test_args, response_timeout=30):
53        """Builds and sends a JSON command to SL4F server.
54
55        Args:
56            test_id: string, unique identifier of test command.
57            test_cmd: string, sl4f method name of command.
58            test_args: dictionary, arguments required to execute test_cmd.
59            response_timeout: int, seconds to wait for a response before
60                throwing an exception. Defaults to no timeout.
61
62        Returns:
63            Dictionary, Result of sl4f command executed.
64        """
65        if not utils.can_ping(job, urlparse(self.address).hostname):
66            raise DeviceOffline("FuchsiaDevice %s is not reachable via the "
67                                "network." % urlparse(self.address).hostname)
68        test_data = json.dumps({
69            "jsonrpc": "2.0",
70            "id": test_id,
71            "method": test_cmd,
72            "params": test_args
73        })
74        try:
75            return requests.get(url=self.address,
76                                data=test_data,
77                                timeout=response_timeout).json()
78        except requests.exceptions.Timeout as e:
79            if not utils.can_ping(job, urlparse(self.address).hostname):
80                raise DeviceOffline(
81                    "FuchsiaDevice %s is not reachable via the "
82                    "network." % urlparse(self.address).hostname)
83            else:
84                logging.debug(
85                    'FuchsiaDevice %s is online but SL4f call timed out.' %
86                    urlparse(self.address).hostname)
87                raise e
88