/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Contains implementation of a class EmulatedBaseCamera that encapsulates
 * functionality common to all emulated camera device versions ("fake",
 * "webcam", "video file", "cam2.0" etc.).  Instances of this class (for each
 * emulated camera) are created during the construction of the
 * EmulatedCameraFactory instance.  This class serves as an entry point for all
 * camera API calls that are common across all versions of the
 * camera_device_t/camera_module_t structures.
 */

#define LOG_NDEBUG 0
#define LOG_TAG "EmulatedCamera_BaseCamera"
#include <cutils/log.h>

#include "EmulatedBaseCamera.h"

namespace android {

EmulatedBaseCamera::EmulatedBaseCamera(int cameraId, uint32_t cameraVersion,
                                       struct hw_device_t* device,
                                       struct hw_module_t* module)
    : mCameraInfo(NULL),
      mCameraID(cameraId),
      mCameraDeviceVersion(cameraVersion) {
  /*
   * Initialize camera_device descriptor for this object.
   */

  /* Common header */
  device->tag = HARDWARE_DEVICE_TAG;
  device->version = cameraVersion;
  device->module = module;
  device->close = NULL;  // Must be filled in by child implementation
}

EmulatedBaseCamera::~EmulatedBaseCamera() {}

status_t EmulatedBaseCamera::getCameraInfo(struct camera_info* info) {
  ALOGV("%s", __FUNCTION__);

  info->device_version = mCameraDeviceVersion;
#if VSOC_PLATFORM_SDK_BEFORE(O)
  // static_camera_characteristics should be initialized if and only if two
  // conditions hold:
  //    CAMERA_MODULE_API_VERSION_2_0 or higher
  //    CAMERA_DEVICE_API_VERSION_2_0 or higher
  // See android/hardware/libhardware/include/hardware/camera_common.h
  //
  // The CAMERA_MODULE_API_VERSION is above 2 on all of the supported
  // branches.
  //
  // The CVD supports both CAMERA_DEVICE_API_VERSION_1_0 and
  // CAMERA_DEVICE_API_VERSION_3_0.
  //
  // By the spec, the framework should not look at this field on
  // CAMERA_DEVICE_API_VERSION_1_0. However, the framework
  // referenced them unconditionally in the M, N, and N-MR1 branches.
  // See b/67841929 for evidence.
  //
  // We have to support those branches, so make initialization uconditional.
  // However, keep the 0xcafef00d fake initiziation on O and later to ensure
  // that we'll catch future framework changes that violate the spec.
  info->static_camera_characteristics = mCameraInfo;
#else
  if (mCameraDeviceVersion >= HARDWARE_DEVICE_API_VERSION(2, 0)) {
    info->static_camera_characteristics = mCameraInfo;
  } else {
    info->static_camera_characteristics = (camera_metadata_t*)0xcafef00d;
  }
#endif

  return NO_ERROR;
}

status_t EmulatedBaseCamera::plugCamera() {
  ALOGE("%s: not supported", __FUNCTION__);
  return INVALID_OPERATION;
}

status_t EmulatedBaseCamera::unplugCamera() {
  ALOGE("%s: not supported", __FUNCTION__);
  return INVALID_OPERATION;
}

#if VSOC_PLATFORM_SDK_AFTER(J_MR2)
camera_device_status_t EmulatedBaseCamera::getHotplugStatus() {
  return CAMERA_DEVICE_STATUS_PRESENT;
}
#endif

status_t EmulatedBaseCamera::setTorchMode(bool /* enabled */) {
  ALOGE("%s: not supported", __FUNCTION__);
  return INVALID_OPERATION;
}

} /* namespace android */