Esempio n. 1
0
        def RemoteCall(*args, **kwargs):
            """Dynamically calls a remote API and returns the result value."""
            func_msg = self.GetApi(api_name)
            if not func_msg:
                raise MirrorObjectError("api %s unknown", func_msg)

            logging.info("remote call %s%s", api_name, args)
            if args:
                for arg_msg, value_msg in zip(func_msg.arg, args):
                    logging.debug("arg msg %s", arg_msg)
                    logging.debug("value %s", value_msg)
                    if value_msg is not None:
                        converted_msg = py2pb.Convert(arg_msg, value_msg)
                        logging.debug("converted_message: %s", converted_msg)
                        arg_msg.CopyFrom(converted_msg)
            else:
                # TODO: use kwargs
                for arg in func_msg.arg:
                    # TODO: handle other
                    if (arg.type == CompSpecMsg.TYPE_SCALAR and
                            arg.scalar_type == "pointer"):
                        arg.scalar_value.pointer = 0
                logging.debug(func_msg)

            call_msg = CompSpecMsg.FunctionCallMessage()
            if self._if_spec_msg.component_class:
                call_msg.component_class = self._if_spec_msg.component_class
            call_msg.hal_driver_id = self._driver_id
            call_msg.api.CopyFrom(func_msg)
            logging.info("final msg %s", call_msg)
            results = self._client.CallApi(
                text_format.MessageToString(call_msg), self._caller_uid)
            if (isinstance(results, tuple) and len(results) == 2
                    and isinstance(results[1], dict)
                    and "coverage" in results[1]):
                self._last_raw_code_coverage_data = results[1]["coverage"]
                results = results[0]

            if isinstance(results, list):  # Non-HIDL HAL does not return list.
              # Translate TYPE_HIDL_INTERFACE to halMirror.
              for i, _ in enumerate(results):
                  result = results[i]
                  if (result and isinstance(result,
                                          CompSpecMsg.VariableSpecificationMessage)
                        and result.type == CompSpecMsg.TYPE_HIDL_INTERFACE):
                    if result.hidl_interface_id <= -1:
                      results[i] = None
                    driver_id = result.hidl_interface_id
                    nested_interface_name = result.predefined_type.split("::")[-1]
                    logging.debug("Nested interface name: %s",
                                  nested_interface_name)
                    nested_interface = self.GetHalMirrorForInterface(
                        nested_interface_name, driver_id)
                    results[i] = nested_interface
              if len(results) == 1: # singe return result, return the value direcly.
                  return results[0]
            return results
Esempio n. 2
0
    def Py2Pb(self, attribute_name, py_values):
        """Returns the ProtoBuf of a give Python values.

        Args:
            attribute_name: string, the name of a target attribute.
            py_values: Python values.

        Returns:
            Converted VariableSpecificationMessage if found, None otherwise
        """
        attribute_spec = self.GetAttribute(attribute_name)
        if attribute_spec:
            return py2pb.Convert(attribute_spec, py_values)
        return None
Esempio n. 3
0
    def Py2Pb(self, attribute_name, py_values):
        """Returns the ProtoBuf of a give Python values.

        Args:
            attribute_name: string, the name of a target attribute.
            py_values: Python values.

        Returns:
            Converted VariableSpecificationMessage if found, None otherwise
        """
        attribute_spec = self.GetAttribute(attribute_name)
        if attribute_spec:
            converted_attr = py2pb.Convert(attribute_spec, py_values)
            if converted_attr is None:
                raise MirrorObjectError("Failed to convert attribute %s",
                                        attribute_spec)
            return converted_attr
        logging.error("Can not find attribute: %s", attribute_name)
        return None
Esempio n. 4
0
    def testSimpleReadWriteStructType(self):
        """Test read/write on queue with predefined type in HAL service.

        This test operates on queue 4, and tests reader and writer can interact
        in a queue with predefined type ReadParameters defined in IStreamIn.hal.
        """
        write_data = [{
            "command": self._stream_in_types.ReadCommand.READ,
            "params": {
                "read": 100
            }
        }, {
            "command": self._stream_in_types.ReadCommand.READ,
            "params": {
                "read": 1000
            }
        }, {
            "command":
            self._stream_in_types.ReadCommand.GET_CAPTURE_POSITION,
            "params": {}
        }]

        # Convert each item into a VariableSpecificationMessage using Py2Pb library.
        read_param_type = self._stream_in_types.GetAttribute("ReadParameters")
        converted_write_data = map(
            lambda item: py2pb.Convert(read_param_type, item), write_data)
        asserts.assertTrue(
            self._queue4_writer.write(converted_write_data, 3),
            "Writer should write successfully.")

        # Reader reads the data back, result is a list of dict.
        read_data = []
        asserts.assertTrue(
            self._queue4_reader.read(read_data, 3),
            "Reader should read successfully.")
        for i in range(len(write_data)):
            asserts.assertTrue(
                self.VerifyDict(write_data[i], read_data[i]),
                "Dictionary item %d mismatch.", i)
Esempio n. 5
0
        def RemoteCall(*args, **kwargs):
            """Dynamically calls a remote API and returns the result value."""
            func_msg = self.GetApi(api_name)
            if not func_msg:
                raise MirrorObjectError("api %s unknown", func_msg)

            logging.debug("remote call %s%s", api_name, args)
            if args:
                for arg_msg, value_msg in zip(func_msg.arg, args):
                    logging.debug("arg msg %s", arg_msg)
                    logging.debug("value %s", value_msg)
                    if value_msg is not None:
                        converted_msg = py2pb.Convert(arg_msg, value_msg)
                        if converted_msg is None:
                            raise MirrorObjectError("Failed to convert arg %s",
                                                    value_msg)
                        logging.debug("converted_message: %s", converted_msg)
                        arg_msg.CopyFrom(converted_msg)
            else:
                # TODO: use kwargs
                for arg in func_msg.arg:
                    # TODO: handle other
                    if (arg.type == CompSpecMsg.TYPE_SCALAR
                            and arg.scalar_type == "pointer"):
                        arg.scalar_value.pointer = 0
                logging.debug(func_msg)

            call_msg = CompSpecMsg.FunctionCallMessage()
            if self._if_spec_msg.component_class:
                call_msg.component_class = self._if_spec_msg.component_class
            call_msg.hal_driver_id = self._driver_id
            call_msg.api.CopyFrom(func_msg)
            logging.debug("final msg %s", call_msg)
            results = self._client.CallApi(
                text_format.MessageToString(call_msg), self._caller_uid)
            if (isinstance(results, tuple) and len(results) == 2
                    and isinstance(results[1], dict)
                    and "coverage" in results[1]):
                self._last_raw_code_coverage_data = results[1]["coverage"]
                results = results[0]

            if isinstance(results, list):  # Non-HIDL HAL does not return list.
                # Translate TYPE_HIDL_INTERFACE to halMirror.
                for i, _ in enumerate(results):
                    result = results[i]
                    if (not result or not isinstance(
                            result, CompSpecMsg.VariableSpecificationMessage)):
                        # no need to process the return values.
                        continue

                    if result.type == CompSpecMsg.TYPE_HIDL_INTERFACE:
                        if result.hidl_interface_id <= -1:
                            results[i] = None
                        driver_id = result.hidl_interface_id
                        nested_interface_name = \
                            result.predefined_type.split("::")[-1]
                        logging.debug("Nested interface name: %s",
                                      nested_interface_name)
                        nested_interface = self.GetHalMirrorForInterface(
                            nested_interface_name, driver_id)
                        results[i] = nested_interface
                    elif (result.type == CompSpecMsg.TYPE_FMQ_SYNC
                          or result.type == CompSpecMsg.TYPE_FMQ_UNSYNC):
                        if (result.fmq_value[0].fmq_id == -1):
                            logging.error("Invalid new queue_id.")
                            results[i] = None
                        else:
                            # Retrieve type of data in this FMQ.
                            data_type = None
                            # For scalar, read scalar_type field.
                            if result.fmq_value[0].type == \
                                    CompSpecMsg.TYPE_SCALAR:
                                data_type = result.fmq_value[0].scalar_type
                            # For enum, struct, and union, read predefined_type
                            # field.
                            elif (result.fmq_value[0].type
                                  == CompSpecMsg.TYPE_ENUM
                                  or result.fmq_value[0].type
                                  == CompSpecMsg.TYPE_STRUCT
                                  or result.fmq_value[0].type
                                  == CompSpecMsg.TYPE_UNION):
                                data_type = result.fmq_value[0].predefined_type

                            # Encounter an unknown type in FMQ.
                            if data_type == None:
                                logging.error(
                                    "Unknown type %d in the new FMQ.",
                                    result.fmq_value[0].type)
                                results[i] = None
                                continue
                            sync = result.type == CompSpecMsg.TYPE_FMQ_SYNC
                            fmq_mirror = resource_mirror.ResourceFmqMirror(
                                data_type, sync, self._client,
                                result.fmq_value[0].fmq_id)
                            results[i] = fmq_mirror
                    elif result.type == CompSpecMsg.TYPE_HIDL_MEMORY:
                        if result.hidl_memory_value.mem_id == -1:
                            logging.error("Invalid new mem_id.")
                            results[i] = None
                        else:
                            mem_mirror = resource_mirror.ResourceHidlMemoryMirror(
                                self._client, result.hidl_memory_value.mem_id)
                            results[i] = mem_mirror
                    elif result.type == CompSpecMsg.TYPE_HANDLE:
                        if result.handle_value.handle_id == -1:
                            logging.error("Invalid new handle_id.")
                            results[i] = None
                        else:
                            handle_mirror = resource_mirror.ResourceHidlHandleMirror(
                                self._client, result.handle_value.handle_id)
                            results[i] = handle_mirror
                if len(results) == 1:
                    # single return result, return the value directly.
                    return results[0]
            return results