Пример #1
0
static bool
thingspeak_channel_update_send(void *data)
{
    struct thingspeak_channel_update_data *mdata = data;
    struct sol_http_param params;
    struct sol_http_client_connection *connection;
    char field_name[] = "fieldX";
    size_t i;
    int r;

    sol_http_param_init(&params);

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

    if (mdata->status) {
        if (!sol_http_param_add(&params,
            SOL_HTTP_REQUEST_PARAM_POST_FIELD("status", mdata->status))) {
            SOL_WRN("Could not add status field to POST parameters");
            goto out;
        }
    }

    for (i = 0; i < ARRAY_SIZE(mdata->fields); i++) {
        if (!mdata->fields[i])
            continue;

        field_name[sizeof("field") - 1] = i + '1';
        if (!sol_http_param_add(&params,
            SOL_HTTP_REQUEST_PARAM_POST_FIELD(strdupa(field_name), mdata->fields[i]))) {
            SOL_WRN("Could not add status field to POST %s parameters",
                field_name);
            goto out;
        }
    }

    connection = sol_http_client_request(SOL_HTTP_METHOD_POST,
        mdata->endpoint, &params, thingspeak_channel_update_finished, mdata);
    if (!connection) {
        SOL_WRN("Could not create HTTP request");
        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);
    }

out:
    sol_http_param_free(&params);

    mdata->timeout = NULL;
    return false;
}
Пример #2
0
static void
thingspeak_add_close(struct sol_flow_node *node, void *data)
{
    struct thingspeak_add_data *mdata = data;
    struct sol_http_client_connection *connection;
    uint16_t i;

    free_talkback(&mdata->talkback);

    SOL_PTR_VECTOR_FOREACH_IDX (&mdata->pending_conns, connection, i)
        sol_http_client_connection_cancel(connection);
    sol_ptr_vector_clear(&mdata->pending_conns);
}
Пример #3
0
static void
http_composed_client_close(struct sol_flow_node *node, void *data)
{
    struct sol_http_client_connection *connection;
    struct http_composed_client_data *cdata = data;
    uint16_t i;

    for (i = 0; i < cdata->inputs_len; i++)
        if (cdata->inputs[i])
            sol_flow_packet_del(cdata->inputs[i]);

    SOL_PTR_VECTOR_FOREACH_IDX (&cdata->pending_conns, connection, i)
        sol_http_client_connection_cancel(connection);
    sol_ptr_vector_clear(&cdata->pending_conns);

    free(cdata->url);
    free(cdata->inputs);
}
Пример #4
0
static void
thingspeak_channel_update_close(struct sol_flow_node *node, void *data)
{
    struct thingspeak_channel_update_data *mdata = data;
    struct sol_http_client_connection *connection;
    uint16_t i;

    for (i = 0; i < ARRAY_SIZE(mdata->fields); i++)
        free(mdata->fields[i]);
    free(mdata->status);

    free(mdata->api_key);
    free(mdata->endpoint);

    sol_timeout_del(mdata->timeout);

    SOL_PTR_VECTOR_FOREACH_IDX (&mdata->pending_conns, connection, i)
        sol_http_client_connection_cancel(connection);
    sol_ptr_vector_clear(&mdata->pending_conns);
}
Пример #5
0
static bool
thingspeak_execute_poll(void *data)
{
    struct thingspeak_execute_data *mdata = data;
    struct sol_http_param params;
    struct sol_http_client_connection *connection;
    int r;

    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 set API key");
        mdata->timeout = NULL;
        sol_http_param_free(&params);
        return false;
    }

    connection = sol_http_client_request(SOL_HTTP_METHOD_POST,
        mdata->talkback.endpoint, &params,
        thingspeak_execute_poll_finished, mdata);

    sol_http_param_free(&params);

    if (!connection) {
        SOL_WRN("Could not create HTTP request");
        mdata->timeout = NULL;
        return false;
    }

    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);
        return false;
    }

    return true;
}
Пример #6
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;
}