<commit_before>/*
 * This file is part of the Soletta (TM) Project
 *
 * Copyright (C) 2015 Intel Corporation. All rights reserved.
 *
 * 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 "sol-flow/iio.h"
#include "sol-flow-internal.h"

#include <sol-types.h>
#include <sol-util-internal.h>
#include <errno.h>

#include <sol-iio.h>

struct iio_device_config {
    struct sol_iio_config config;
    struct sol_drange_spec out_range;
    struct sol_iio_device *device;
    bool buffer_enabled : 1;
    bool use_device_default_scale : 1;
    bool use_device_default_offset : 1;
};

/* Make sure the iio_device_config is the first element */
struct iio_double_data {
    struct iio_device_config iio_base;
    double scale;
    double offset;
    struct sol_iio_channel *channel_val;
};

struct iio_direction_vector_data {
    struct iio_device_config iio_base;
    struct sol_direction_vector scale;
    struct sol_direction_vector offset;
    struct sol_iio_channel *channel_x;
    struct sol_iio_channel *channel_y;
    struct sol_iio_channel *channel_z;
};

struct iio_color_data {
    struct iio_device_config iio_base;
    double scale_red;
    double offset_red;
    double scale_green;
    double offset_green;
    double scale_blue;
    double offset_blue;
    struct sol_iio_channel *channel_red;
    struct sol_iio_channel *channel_green;
    struct sol_iio_channel *channel_blue;
};

struct iio_node_type {
    struct sol_flow_node_type base;
    uint16_t out_port;
    void (*reader_cb)(void *data, struct sol_iio_device *device);
};

static inline struct sol_iio_channel *
iio_add_channel(double scale, double offset, const char *name, struct iio_device_config *base)
{
    struct sol_iio_channel_config channel_config = SOL_IIO_CHANNEL_CONFIG_INIT;

    if (base->use_device_default_scale)
        channel_config.scale = scale;

    if (base->use_device_default_offset)
        channel_config.offset = offset;

    return sol_iio_add_channel(base->device, name, &channel_config);
}

static void
iio_common_close(struct sol_flow_node *node, void *data)
{
    struct iio_device_config *mdata = data;

    free((char *)mdata->config.trigger_name);
    if (mdata->device)
        sol_iio_close(mdata->device);
}

static int
iio_common_tick(struct sol_flow_node *node, void *data, uint16_t port, uint16_t conn_id, const struct sol_flow_packet *packet)
{
    static const char *errmsg = "Could not read channel values";
    struct iio_device_config *mdata = data;
    struct iio_node_type *type;<commit_msg>iio: fix the logic error caused by code refactor<commit_after>/*
 * This file is part of the Soletta (TM) Project
 *
 * Copyright (C) 2015 Intel Corporation. All rights reserved.
 *
 * 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 "sol-flow/iio.h"
#include "sol-flow-internal.h"

#include <sol-types.h>
#include <sol-util-internal.h>
#include <errno.h>

#include <sol-iio.h>

struct iio_device_config {
    struct sol_iio_config config;
    struct sol_drange_spec out_range;
    struct sol_iio_device *device;
    bool buffer_enabled : 1;
    bool use_device_default_scale : 1;
    bool use_device_default_offset : 1;
};

/* Make sure the iio_device_config is the first element */
struct iio_double_data {
    struct iio_device_config iio_base;
    double scale;
    double offset;
    struct sol_iio_channel *channel_val;
};

struct iio_direction_vector_data {
    struct iio_device_config iio_base;
    struct sol_direction_vector scale;
    struct sol_direction_vector offset;
    struct sol_iio_channel *channel_x;
    struct sol_iio_channel *channel_y;
    struct sol_iio_channel *channel_z;
};

struct iio_color_data {
    struct iio_device_config iio_base;
    double scale_red;
    double offset_red;
    double scale_green;
    double offset_green;
    double scale_blue;
    double offset_blue;
    struct sol_iio_channel *channel_red;
    struct sol_iio_channel *channel_green;
    struct sol_iio_channel *channel_blue;
};

struct iio_node_type {
    struct sol_flow_node_type base;
    uint16_t out_port;
    void (*reader_cb)(void *data, struct sol_iio_device *device);
};

static inline struct sol_iio_channel *
iio_add_channel(double scale, double offset, const char *name, struct iio_device_config *base)
{
    struct sol_iio_channel_config channel_config = SOL_IIO_CHANNEL_CONFIG_INIT;

    if (!base->use_device_default_scale)
        channel_config.scale = scale;

    if (!base->use_device_default_offset)
        channel_config.offset = offset;

    return sol_iio_add_channel(base->device, name, &channel_config);
}

static void
iio_common_close(struct sol_flow_node *node, void *data)
{
    struct iio_device_config *mdata = data;

    free((char *)mdata->config.trigger_name);
    if (mdata->device)
        sol_iio_close(mdata->device);
}

static int
iio_common_tick(struct sol_flow_node *node, void *data, uint16_t port, uint16_t conn_id, const struct sol_flow_packet *packet)
{
    static const char *errmsg = "Could not read channel values";
    struct iio_device_config *mdata = data;
    struct iio_node_type *type;
{'fraction_non_alphanumeric': 0.0769735762350238, 'fraction_numerical': 0.005580173970129657, 'mean_word_length': 4.195225916453538, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 23, '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': '1436629', 'n_tokens_mistral': 1974, 'n_tokens_neox': 1940, 'n_words': 575}