Example #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;
}
Example #2
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;
}
Example #3
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;
}
Example #4
0
    return false;
}

static int
request_cb(void *data, struct sol_http_request *request)
{
    int r;
    struct sol_http_response response = {
        SOL_SET_API_VERSION(.api_version = SOL_HTTP_RESPONSE_API_VERSION, )
        .param = SOL_HTTP_REQUEST_PARAMS_INIT,
        .response_code = SOL_HTTP_STATUS_OK
    };

    response.content = value;

    r = sol_http_param_add(&response.param,
        SOL_HTTP_REQUEST_PARAM_HEADER("Content-Type", "text/plain"));
    if (r < 0)
        fprintf(stderr, "ERROR: Could not set the 'Content-Type' header\n");

    r = sol_http_server_send_response(request, &response);
    sol_http_params_clear(&response.param);

    return r;
}

static void
startup_server(void)
{
    char **argv = sol_argv();
    int port = 8080, c, opt_idx,  argc = sol_argc();
    static const struct option opts[] = {