Ejemplo n.º 1
0
static int
get_string_by_port(const struct sol_flow_packet *packet,
    uint16_t port,
    UChar **string)
{
    UChar *new_str = NULL;
    const char *in_value;
    UErrorCode err;
    int r;

    r = sol_flow_packet_get_string(packet, &in_value);
    SOL_INT_CHECK(r, < 0, r);

    r = icu_str_from_utf8(in_value, &new_str, &err);
    SOL_INT_CHECK(r, < 0, r);

    if (string[port] && !u_strCompare(string[port],
        -1, new_str, -1, r)) {
        free(new_str);
        return 0;
    }

    free(string[port]);
    string[port] = new_str;
    return 0;
}
Ejemplo n.º 2
0
static int
thingspeak_channel_update_field_process(struct sol_flow_node *node,
    void *data, uint16_t port, uint16_t conn_id,
    const struct sol_flow_packet *packet)
{
    struct thingspeak_channel_update_data *mdata = data;
    int n_field = port - SOL_FLOW_NODE_TYPE_THINGSPEAK_CHANNEL_UPDATE__IN__FIELD;
    const char *field;

    if (n_field < 0 || n_field >= (int)ARRAY_SIZE(mdata->fields)) {
        SOL_WRN("Invalid field ID: %d, expecting 0 to 7", n_field);
        return -EINVAL;
    }

    if (sol_flow_packet_get_string(packet, &field) < 0) {
        SOL_WRN("Could not get field <%d> string", n_field);
        return -EINVAL;
    }

    free(mdata->fields[n_field]);
    mdata->fields[n_field] = strdup(field);
    if (!mdata->fields[n_field]) {
        SOL_WRN("Could not store field <%d> value", n_field);
        return -ENOMEM;
    }

    thingspeak_channel_update_queue(mdata);
    return 0;
}
Ejemplo n.º 3
0
static int
platform_service_name_process(struct sol_flow_node *node, void *data,
    uint16_t port, uint16_t conn_id, const struct sol_flow_packet *packet)
{
    struct platform_service_data *mdata = data;
    const char *name;
    int r;

    r = sol_flow_packet_get_string(packet, &name);
    SOL_INT_CHECK(r, < 0, r);
    if (mdata->service_name) {
        sol_platform_del_service_monitor(on_service_state_changed,
            mdata->service_name, mdata);
    }

    r = sol_util_replace_str_if_changed(&mdata->service_name, name);
    SOL_INT_CHECK(r, < 0, r);

    sol_platform_add_service_monitor(on_service_state_changed,
        mdata->service_name, mdata);
    mdata->state = sol_platform_get_service_state(mdata->service_name);

    return sol_flow_send_boolean_packet(node,
        SOL_FLOW_NODE_TYPE_PLATFORM_SERVICE__OUT__ACTIVE,
        (mdata->state == SOL_PLATFORM_SERVICE_STATE_ACTIVE));
}
Ejemplo n.º 4
0
static int
set_locale(enum sol_platform_locale_category category, const struct sol_flow_packet *packet)
{
    int r;
    const char *locale;

    r = sol_flow_packet_get_string(packet, &locale);
    SOL_INT_CHECK(r, < 0, r);

    r = sol_platform_set_locale(category, locale);
    SOL_INT_CHECK(r, < 0, r);
    return 0;
}
Ejemplo n.º 5
0
static int
timezone_process(struct sol_flow_node *node, void *data, uint16_t port,
    uint16_t conn_id, const struct sol_flow_packet *packet)
{
    int r;
    const char *tz;

    r = sol_flow_packet_get_string(packet, &tz);
    SOL_INT_CHECK(r, < 0, r);
    r = sol_platform_set_timezone(tz);
    SOL_INT_CHECK(r, < 0, r);
    return 0;
}
Ejemplo n.º 6
0
static int
hostname_process(struct sol_flow_node *node, void *data, uint16_t port,
    uint16_t conn_id, const struct sol_flow_packet *packet)
{
    int r;
    const char *name;

    r = sol_flow_packet_get_string(packet, &name);
    SOL_INT_CHECK(r, < 0, r);
    r = sol_platform_set_hostname(name);
    SOL_INT_CHECK(r, < 0, r);
    return 0;
}
Ejemplo n.º 7
0
int
gtk_label_in_process(struct sol_flow_node *node,
    void *data,
    uint16_t port,
    uint16_t conn_id,
    const struct sol_flow_packet *packet)
{
    struct gtk_common_data *mdata = data;

    if (sol_flow_packet_get_type(packet) == SOL_FLOW_PACKET_TYPE_EMPTY) {
        gtk_label_set_text(GTK_LABEL(mdata->widget), "[empty]");
    } else if (sol_flow_packet_get_type(packet) == SOL_FLOW_PACKET_TYPE_STRING) {
        const char *in_value;
        int r = sol_flow_packet_get_string(packet, &in_value);
        SOL_INT_CHECK(r, < 0, r);
        gtk_label_set_text(GTK_LABEL(mdata->widget), in_value);
    } else if (sol_flow_packet_get_type(packet) == SOL_FLOW_PACKET_TYPE_BOOLEAN) {
Ejemplo n.º 8
0
static int
file_reader_path_process(struct sol_flow_node *node, void *data, uint16_t port, uint16_t conn_id, const struct sol_flow_packet *packet)
{
    struct file_reader_data *mdata = data;
    const char *path;
    int r;

    r = sol_flow_packet_get_string(packet, &path);
    SOL_INT_CHECK(r, < 0, r);

    if (path && mdata->path && streq(path, mdata->path))
        return 0;

    file_reader_unload(mdata);

    mdata->path = path ? strdup(path) : NULL;
    return file_reader_load(mdata);
}
Ejemplo n.º 9
0
static int
string_writer_process(struct sol_flow_node *node, void *data, uint16_t port, uint16_t conn_id, const struct sol_flow_packet *packet)
{
    struct ipm_data *mdata = data;
    const char *in_value;
    struct sol_blob *blob;
    int r;

    r = sol_flow_packet_get_string(packet, &in_value);
    SOL_INT_CHECK(r, < 0, r);

    blob = sol_blob_new_dup_str(in_value);
    SOL_NULL_CHECK(in_value, -ENOMEM);

    SEND_BLOB(blob, mdata->id, node);

    return 0;
}
Ejemplo n.º 10
0
static int
platform_target_process(struct sol_flow_node *node, void *data, uint16_t port,
    uint16_t conn_id, const struct sol_flow_packet *packet)
{
    int r;
    const char *target;

    r = sol_flow_packet_get_string(packet, &target);
    SOL_INT_CHECK(r, < 0, r);

    r = sol_platform_set_target(target);

    if (r < 0) {
        sol_flow_send_error_packet(node, r,
            "Could not change the system target to: %s", target);
    }

    return 0;
}
Ejemplo n.º 11
0
static int
string_writer_process(struct sol_flow_node *node, void *data, uint16_t port, uint16_t conn_id, const struct sol_flow_packet *packet)
{
    struct unix_socket_data *mdata = data;
    const char *val;
    size_t len;
    int r;

    r = sol_flow_packet_get_string(packet, &val);
    SOL_INT_CHECK(r, < 0, r);

    len = strlen(val);
    if (unix_socket_write(mdata->un_socket, &len, sizeof(len)) < 0) {
        SOL_WRN("Failed to write the string length");
        return -1;
    }

    return unix_socket_write(mdata->un_socket, val, len);
}
Ejemplo n.º 12
0
static int
get_string_by_port(const struct sol_flow_packet *packet,
    uint16_t port,
    char **string)
{
    const char *in_value;
    int r;

    r = sol_flow_packet_get_string(packet, &in_value);
    SOL_INT_CHECK(r, < 0, r);

    if (string[port] && !strcmp(string[port], in_value))
        return 0;

    free(string[port]);

    string[port] = strdup(in_value);
    SOL_NULL_CHECK(string[port], -ENOMEM);

    return 0;
}
Ejemplo n.º 13
0
static int
thingspeak_channel_update_status_process(struct sol_flow_node *node,
    void *data, uint16_t port, uint16_t conn_id,
    const struct sol_flow_packet *packet)
{
    struct thingspeak_channel_update_data *mdata = data;
    const char *status;

    if (sol_flow_packet_get_string(packet, &status) < 0) {
        SOL_WRN("Could not get status string");
        return -EINVAL;
    }

    free(mdata->status);
    mdata->status = strdup(status);
    if (!mdata->status) {
        SOL_WRN("Could not store status string");
        return -ENOMEM;
    }

    thingspeak_channel_update_queue(mdata);
    return 0;
}
Ejemplo n.º 14
0
static int
convert_char(struct sol_flow_node *node, void *data, uint16_t port, uint16_t conn_id, const struct sol_flow_packet *packet)
{
    const char *in_value;
    int c_index, r;
    unsigned char byte = 0;

    r = sol_flow_packet_get_string(packet, &in_value);
    SOL_INT_CHECK(r, < 0, r);

    c_index = in_value[0];
    if (c_index != '\0' && c_index != ' ') {
        byte = conversion_table[c_index];
        if (!byte) {
            sol_flow_send_error_packet(node, EINVAL,
                "Char '%c' can't be represented with 7 segments.", c_index);
            return 0;
        }
    }

    return sol_flow_send_byte_packet(node,
        SOL_FLOW_NODE_TYPE_LED_7SEG_CHAR_TO_BYTE__OUT__OUT,
        byte);
}
Ejemplo n.º 15
0
static int
thingspeak_add_in_process(struct sol_flow_node *node, void *data,
    uint16_t port, uint16_t conn_id, const struct sol_flow_packet *packet)
{
    struct thingspeak_add_data *mdata = data;
    const char *cmd_str;
    struct sol_http_param params;
    struct sol_http_client_connection *connection;
    int error_code = 0;
    int r;

    r = sol_flow_packet_get_string(packet, &cmd_str);
    if (r < 0) {
        SOL_WRN("Could not get command string from packet");
        return -EINVAL;
    }

    sol_http_param_init(&params);

    if (!sol_http_param_add(&params,
        SOL_HTTP_REQUEST_PARAM_POST_FIELD("api_key", mdata->talkback.api_key))) {
        SOL_WRN("Could not add API key");
        error_code = -ENOMEM;
        goto out;
    }

    if (!sol_http_param_add(&params,
        SOL_HTTP_REQUEST_PARAM_POST_FIELD("command_string", cmd_str))) {
        SOL_WRN("Could not add command string");
        error_code = -ENOMEM;
        goto out;
    }

    if (mdata->position >= 0) {
        char position_str[3 * sizeof(int)];
        char *pos_str;

        r = snprintf(position_str, sizeof(position_str), "%d", mdata->position);
        if (r < 0 || r >= (int)sizeof(position_str)) {
            SOL_WRN("Could not convert position to string");
            error_code = -ENOMEM;
            goto out;
        }

        /* Use pos_str at SOL_HTTP_REQUEST_PARAM_POST_FIELD macro, otherwise
           the compiler will complain about an "always true" comparison.
         */
        pos_str = position_str;
        if (!sol_http_param_add(&params,
            SOL_HTTP_REQUEST_PARAM_POST_FIELD("position", pos_str))) {
            SOL_WRN("Could not add position");
            error_code = -ENOMEM;
            goto out;
        }
    }

    connection = sol_http_client_request(SOL_HTTP_METHOD_POST,
        mdata->talkback.endpoint,
        &params, thingspeak_add_request_finished, mdata);
    if (!connection) {
        SOL_WRN("Could not create HTTP request");
        error_code = -EINVAL;
        goto out;
    }

    r = sol_ptr_vector_append(&mdata->pending_conns, connection);
    if (r < 0) {
        SOL_WRN("Failed to keep pending connection.");
        sol_http_client_connection_cancel(connection);
        error_code = -ENOMEM;
    }

out:
    sol_http_param_free(&params);
    return error_code;
}
Ejemplo n.º 16
0
static void
inspector_show_packet_value(const struct sol_flow_packet *packet)
{
    const struct sol_flow_packet_type *type = sol_flow_packet_get_type(packet);

    if (type == SOL_FLOW_PACKET_TYPE_EMPTY) {
        fputs("<empty>", stdout);
        return;
    } else if (type == SOL_FLOW_PACKET_TYPE_ANY) {
        fputs("<any>", stdout);
        return;
    } else if (type == SOL_FLOW_PACKET_TYPE_ERROR) {
        int code;
        const char *msg;
        if (sol_flow_packet_get_error(packet, &code, &msg) == 0) {
            fprintf(stdout, "<error:%d \"%s\">", code, msg ? msg : "");
            return;
        }
    } else if (type == SOL_FLOW_PACKET_TYPE_BOOL) {
        bool v;
        if (sol_flow_packet_get_bool(packet, &v) == 0) {
            fprintf(stdout, "<%s>", v ? "true" : "false");
            return;
        }
    } else if (type == SOL_FLOW_PACKET_TYPE_BYTE) {
        unsigned char v;
        if (sol_flow_packet_get_byte(packet, &v) == 0) {
            fprintf(stdout, "<%#x>", v);
            return;
        }
    } else if (type == SOL_FLOW_PACKET_TYPE_IRANGE) {
        struct sol_irange v;
        if (sol_flow_packet_get_irange(packet, &v) == 0) {
            fprintf(stdout, "<val:%d|min:%d|max:%d|step:%d>",
                    v.val, v.min, v.max, v.step);
            return;
        }
    } else if (type == SOL_FLOW_PACKET_TYPE_DRANGE) {
        struct sol_drange v;
        if (sol_flow_packet_get_drange(packet, &v) == 0) {
            fprintf(stdout, "<val:%g|min:%g|max:%g|step:%g>",
                    v.val, v.min, v.max, v.step);
            return;
        }
    } else if (type == SOL_FLOW_PACKET_TYPE_STRING) {
        const char *v;
        if (sol_flow_packet_get_string(packet, &v) == 0) {
            fprintf(stdout, "<\"%s\">", v);
            return;
        }
    } else if (type == SOL_FLOW_PACKET_TYPE_BLOB) {
        struct sol_blob *v;
        if (sol_flow_packet_get_blob(packet, &v) == 0) {
            fprintf(stdout, "<mem=%p|size=%zd|refcnt=%hu|type=%p|parent=%p>",
                    v->mem, v->size, v->refcnt, v->type, v->parent);
            return;
        }
    } else if (type == SOL_FLOW_PACKET_TYPE_JSON_OBJECT) {
        struct sol_blob *v;
        if (sol_flow_packet_get_json_object(packet, &v) == 0) {
            fprintf(stdout, "<%.*s>",
                    SOL_STR_SLICE_PRINT(sol_str_slice_from_blob(v)));
            return;
        }
    } else if (type == SOL_FLOW_PACKET_TYPE_JSON_ARRAY) {
        struct sol_blob *v;
        if (sol_flow_packet_get_json_array(packet, &v) == 0) {
            fprintf(stdout, "<%.*s>",
                    SOL_STR_SLICE_PRINT(sol_str_slice_from_blob(v)));
            return;
        }
    } else if (type == SOL_FLOW_PACKET_TYPE_RGB) {
        struct sol_rgb v;
        if (sol_flow_packet_get_rgb(packet, &v) == 0) {
            fprintf(stdout,
                    "<red=%u|green=%u|blue=%u"
                    "|red_max=%u|green_max=%u|blue_max=%u>",
                    v.red, v.green, v.blue,
                    v.red_max, v.green_max, v.blue_max);
            return;
        }
    } else if (type == SOL_FLOW_PACKET_TYPE_DIRECTION_VECTOR) {
        struct sol_direction_vector v;
        if (sol_flow_packet_get_direction_vector(packet, &v) == 0) {
            fprintf(stdout,
                    "<x=%g|y=%g|z=%g|min=%g|max=%g>",
                    v.x, v.y, v.z, v.min, v.max);
            return;
        }
    } else if (type == SOL_FLOW_PACKET_TYPE_LOCATION) {
        struct sol_location v;
        if (sol_flow_packet_get_location(packet, &v) == 0) {
            fprintf(stdout, "<lat=%g|lon=%g|alt=%g>", v.lat, v.lon, v.alt);
            return;
        }
    } else if (type == SOL_FLOW_PACKET_TYPE_TIMESTAMP) {
        struct timespec v;
        if (sol_flow_packet_get_timestamp(packet, &v) == 0) {
            struct tm cur_time;
            char buf[32];
            tzset();
            if (gmtime_r(&v.tv_sec, &cur_time)) {
                if (strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%SZ",
                             &cur_time) > 0) {
                    fprintf(stdout, "<%s>", buf);
                    return;
                }
            }
        }
    } else if (type == SOL_FLOW_PACKET_TYPE_HTTP_RESPONSE) {
        int code;
        const char *url, *content_type;
        const struct sol_blob *content;
        struct sol_vector headers, cookies;
        if (sol_flow_packet_get_http_response(packet, &code, &url,
                                              &content_type, &content, &cookies, &headers) == 0) {
            fprintf(stdout, "<response_code:%d|content type:%s|url:%s|",
                    code, content_type, url);
            fprintf(stdout, "|cookies: {");
            inpector_print_key_value_array(&cookies);
            fprintf(stdout, "}|headers:{");
            inpector_print_key_value_array(&headers);
            fprintf(stdout,
                    "}|content:{mem=%p|size=%zd|refcnt=%hu|type=%p|parent=%p}>",
                    content->mem, content->size, content->refcnt,
                    content->type, content->parent);
        }
        return;
    }

    fputs("<?>", stdout);
}