1#!/usr/bin/env python
2#
3# Copyright (C) 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.
16#
17
18import logging
19import threading
20import time
21
22from vts.runners.host import asserts
23from vts.runners.host import test_runner
24from vts.testcases.template.hal_hidl_host_test import hal_hidl_host_test
25
26
27class ContextHubCallback:
28    def __init__(self, hub_id):
29        self.hub_id = hub_id
30        self.event = threading.Event()
31
32    def wait_on_callback(timeout=None):
33        """Wait on the next callback in this object to be invoked.
34
35        Args:
36            timeout: (fractional) seconds to wait before timing out, or None
37
38        Returns:
39            True when a callback was received, False if a timeout occurred
40        """
41        return self.event.wait(timeout)
42
43    def prepare():
44        # TODO: cleaner method of doing this, so that we clear --> call HAL
45        # method --> wait for CB, all wrapped into one
46        self.event.clear()
47
48    def ignore_client_msg(self, msg):
49        logging.debug("Ignoring client message from hubId %s: %s", self.hub_id, msg)
50        self.event.set()
51
52    def ignore_txn_result(self, txnId, result):
53        logging.debug("Ignoring transaction result from hubId %s: %s", self.hub_id, msg)
54        self.event.set()
55
56    def ignore_hub_event(self, evt):
57        logging.debug("Ignoring hub event from hubId %s: %s", self.hub_id, evt)
58        self.event.set()
59
60    def ignore_apps_info(self, appInfo):
61        logging.debug("Ignoring app info data from hubId %s: %s", self.hub_id, appInfo)
62        self.event.set()
63
64
65class ContexthubHidlTest(hal_hidl_host_test.HalHidlHostTest):
66    """A set of test cases for the context hub HIDL HAL"""
67    TEST_HAL_SERVICES = {
68        "android.hardware.contexthub@1.0::IContexthub",
69    }
70
71    def setUpClass(self):
72        """Creates a mirror and turns on the framework-layer CONTEXTHUB service."""
73        super(ContexthubHidlTest, self).setUpClass()
74        self.dut.hal.InitHidlHal(
75            target_type="contexthub",
76            target_basepaths=self.dut.libPaths,
77            target_version=1.0,
78            target_package="android.hardware.contexthub",
79            target_component_name="IContexthub",
80            bits=int(self.abi_bitness))
81
82        self.types = self.dut.hal.contexthub.GetHidlTypeInterface("types")
83        logging.info("types: %s", self.types)
84
85    def testContexthubBasic(self):
86        logging.info("About to call gethubs!!!")
87        hubs = self.dut.hal.contexthub.getHubs()
88        logging.info("Got result: %s", hubs)
89        #
90        #hub_id = 0 # TODO: should get this from hub list
91        #
92        #cb = ContextHubCallback(hub_id)
93        #client_callback = self.dut.hal.contexthub.GetHidlCallbackInterface(
94        #    "IContexthubCallback",
95        #    handleClientMsg=cb.ignore_client_msg,
96        #    handleTxnResult=cb.ignore_txn_result,
97        #    handleHubEvent=cb.ignore_hub_event,
98        #    handleAppsInfo=cb.ignore_apps_info)
99        #
100        #logging.info("About to call registerCallback")
101        #result = self.dut.hal.contexthub.registerCallback(hub_id,
102        #                                                  client_callback)
103        #logging.info("registerCallback returned result: %s", result)
104        #
105        #logging.info("About to call queryApps")
106        #result = self.dut.hal.contexthub.queryApps(hub_id)
107        #logging.info("queryApps returned result: %s", result)
108        #
109        #got_callback = cb.wait_on_callback(5)
110        #logging.info("Wait on callback returned %s", got_callback)
111
112if __name__ == "__main__":
113    test_runner.main()
114