chromium/services/device/public/mojom/sensor_provider.mojom

// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

module device.mojom;

import "mojo/public/mojom/base/shared_memory.mojom";
import "services/device/public/mojom/sensor.mojom";

struct SensorInitParams {
  // The Sensor interface.
  pending_remote<Sensor> sensor;

  // The SensorClient interface used to report sensor events.
  pending_receiver<SensorClient> client_receiver;

  // The shared memory handle used to fetch the sensor reading.
  mojo_base.mojom.ReadOnlySharedMemoryRegion memory;

  // The offset at which shared buffer must be mapped.
  uint64 buffer_offset;

  // The ReportingMode supported by the sensor.
  ReportingMode mode;

  // Default sensor configuration.
  SensorConfiguration default_configuration;

  // Maximum sampling frequency for the sensor: it considers both
  // |SensorConfiguration::kMaxAllowedFrequency| and actual sensor
  // capabilities.
  double maximum_frequency;

  // Minimum sampling frequency for the sensor.
  double minimum_frequency;

  // Each sensor's read buffer contains 5 tightly packed 64-bit floating
  // point fields (please see sensor_reading.h) and a seqlock, so its size is
  // 6 * 8 = 48 bytes.
  const uint64 kReadBufferSizeForTests = 48;
};

enum SensorCreationResult {
  SUCCESS,
  ERROR_NOT_AVAILABLE, // Sensor is not available on the platform.
  ERROR_NOT_ALLOWED,  // The required permissions are not granted.
};

enum CreateVirtualSensorResult {
  kSuccess,
  kSensorTypeAlreadyOverridden,
};

enum UpdateVirtualSensorResult {
  kSuccess,
  kSensorTypeNotOverridden,
};

enum GetVirtualSensorInformationError {
  kSensorTypeNotOverridden,
};

union GetVirtualSensorInformationResult {
  VirtualSensorInformation info;
  GetVirtualSensorInformationError error;
};

struct VirtualSensorMetadata {
  bool available = true;

  double? maximum_frequency;
  double? minimum_frequency;

  // Only used by unit tests.
  ReportingMode? reporting_mode;
};

struct VirtualSensorInformation {
  double sampling_frequency;
};

// Manages Sensor interfaces. This interface is responsible for creating or
// retrieving a Sensor via GetSensor() and for managing virtual sensors.
interface SensorProvider {
  // Gets the sensor interface by the given type.
  //
  // |type| type of the sensor.
  //
  // If a virtual sensor of given |type| has been created, it will be used
  // instead of a real, hardware-based one.
  //
  // |result| on success contains SUCCESS, otherwise contains the
  //          SensorCreationResult describing the failure reason.
  // |init_params| on success will contain the SensorInitParams describing
  //               the sensor, contains null on failure.
  GetSensor(SensorType type) => (SensorCreationResult result,
                                 SensorInitParams? init_params);

  // Creates a new virtual sensor of a given |type|, or fails if one already
  // exists.
  CreateVirtualSensor(SensorType type, VirtualSensorMetadata metadata) =>
      (CreateVirtualSensorResult result);

  // Updates readings for a virtual sensor of given |type|, or fails if one has
  // not been created.
  UpdateVirtualSensor(SensorType type, SensorReadingRaw reading) =>
      (UpdateVirtualSensorResult result);

  // Removes a virtual sensor of given |type|, or does nothing if one has not
  // been created.
  RemoveVirtualSensor(SensorType type) => ();

  // Retrieves information about a virtual sensor of given |type|. It fails if
  // one has not been created. If the sensor is not active, all information
  // will be null or zeroed out.
  GetVirtualSensorInformation(SensorType type) =>
      (GetVirtualSensorInformationResult result);
};