// +build ignore

#include "./include/MvCameraControl.h"
#include "../swapi/include_swapiLib.h"

const char *MODULE_NAME = "HKCamReset";

int main(int argc,char *argv[])
{
  HANDLE hCam = NULL; MV_CC_DEVICE_INFO_LIST devList = { 0 };
  MV_CC_DEVICE_INFO *pDevInfo; int ret;

  // 1, 查找相机
  ret = MV_CC_Initialize();
  if(MV_OK != ret)
  {
    sw_log_error("[%s] 相机SDK初始化失败, errCode=0x%x!!", MODULE_NAME, ret);
    goto end_p;
  }

  ret = MV_CC_EnumDevices(MV_USB_DEVICE, &devList); // 目前只枚举USB连接的相机
  if(MV_OK != ret)
  {
    sw_log_error("[%s] USB口枚举相机失败, errCode=0x%x!!", MODULE_NAME, ret);
    goto end_p;
  }
  if(devList.nDeviceNum != 1)
  {
    ret = -1;
    sw_log_error("[%s] USB口相机数量错误, deviceNum=%u!!", MODULE_NAME, devList.nDeviceNum);
    goto end_p;
  }

  pDevInfo = devList.pDeviceInfo[0];
  if(!pDevInfo)
  {
    ret = -2;
    sw_log_error("[%s] unexpected internal error, unable to obtain detailed information about the industrial camera!!", MODULE_NAME);
    goto end_p;
  }

  // 2, 打开相机
  ret = MV_CC_CreateHandleWithoutLog(&hCam, pDevInfo);
  if(MV_OK == ret) ret = MV_CC_OpenDevice(hCam, MV_ACCESS_Exclusive, 0);
  if(MV_OK != ret)
  {
    sw_log_error("[%s] USB口相机打开失败, errCode=0x%x!!", MODULE_NAME, ret);
    goto end_p;
  }

  // 3, 重置相机
  ret = MV_CC_SetCommandValue(hCam, "DeviceReset");
  if(MV_OK != ret)
  {
    sw_log_error("[%s] USB口相机重置失败, errCode=0x%x!!", MODULE_NAME, ret);
    goto end_p;
  }

  // 4, 关闭相机
end_p:
  if(hCam)
  {
    MV_CC_CloseDevice(hCam);
    MV_CC_DestroyHandle(hCam);
  }
  MV_CC_Finalize();

  return ret;
}
