<commit_before>/*
 * Copyright 2014 The Imaging Source Europe GmbH
 *
 * 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.
 */

#include "CaptureDeviceImpl.h"

#include "DeviceIndex.h"
#include "DeviceInterface.h"
#include "PipelineManager.h"
#include "SoftwareProperties.h"
#include "logging.h"
#include "utils.h"

#include <exception>

using namespace tcam;


struct bad_device : std::exception
{
    const char* what() const noexcept
    {
        return "Device did not comply with commands.";
    }
};


CaptureDeviceImpl::CaptureDeviceImpl() : pipeline(nullptr), device(nullptr), index_() {}


CaptureDeviceImpl::CaptureDeviceImpl(const DeviceInfo& _device)
    : pipeline(nullptr), device(nullptr), index_()
{
    if (!open_device(_device))
    {
        SPDLOG_ERROR("Unable to open device");
        throw bad_device();
    }
    const auto serial = open_device_info.get_serial();
    index_.register_device_lost(deviceindex_lost_cb, this, serial);
}


CaptureDeviceImpl::~CaptureDeviceImpl()
{
    close_device();
}


bool CaptureDeviceImpl::open_device(const DeviceInfo& device_desc)
{
    if (is_device_open())
    {
        bool ret = close_device();
        if (ret == false)
        {
            SPDLOG_ERROR("Unable to close previous device.");
            // setError(Error("A device is already open", EPERM));
            return false;
        }
    }

    open_device_info = device_desc;

    device = openDeviceInterface(open_device_info);

    if (device == nullptr)
    {
        return false;
    }

    pipeline = std::make_shared<PipelineManager>();
    pipeline->setSource(device);

    auto props = device->get_properties();

    return true;
}


bool CaptureDeviceImpl::is_device_open() const
{
    if (device != nullptr)
    {
        return true;
    }
<commit_msg>srcs: Ensure indexer always reports device lost<commit_after>/*
 * Copyright 2014 The Imaging Source Europe GmbH
 *
 * 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.
 */

#include "CaptureDeviceImpl.h"

#include "DeviceIndex.h"
#include "DeviceInterface.h"
#include "PipelineManager.h"
#include "SoftwareProperties.h"
#include "logging.h"
#include "utils.h"

#include <exception>

using namespace tcam;


struct bad_device : std::exception
{
    const char* what() const noexcept
    {
        return "Device did not comply with commands.";
    }
};


CaptureDeviceImpl::CaptureDeviceImpl() : pipeline(nullptr), device(nullptr), index_() {}


CaptureDeviceImpl::CaptureDeviceImpl(const DeviceInfo& _device)
    : pipeline(nullptr), device(nullptr), index_()
{
    if (!open_device(_device))
    {
        SPDLOG_ERROR("Unable to open device");
        throw bad_device();
    }
}


CaptureDeviceImpl::~CaptureDeviceImpl()
{
    close_device();
}


bool CaptureDeviceImpl::open_device(const DeviceInfo& device_desc)
{
    if (is_device_open())
    {
        bool ret = close_device();
        if (ret == false)
        {
            SPDLOG_ERROR("Unable to close previous device.");
            // setError(Error("A device is already open", EPERM));
            return false;
        }
    }

    open_device_info = device_desc;

    device = openDeviceInterface(open_device_info);

    if (device == nullptr)
    {
        return false;
    }

    pipeline = std::make_shared<PipelineManager>();
    pipeline->setSource(device);

    auto props = device->get_properties();

    const auto serial = open_device_info.get_serial();
    index_.register_device_lost(deviceindex_lost_cb, this, serial);

    return true;
}


bool CaptureDeviceImpl::is_device_open() const
{
    if (device != nullptr)
    {
        return true;
    }

{'fraction_non_alphanumeric': 0.10534285096257841, 'fraction_numerical': 0.0034609560891196195, 'mean_word_length': 3.4206500956022943, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2852120', 'n_tokens_mistral': 1333, 'n_tokens_neox': 1304, 'n_words': 400}