Esempio n. 1
0
//! [uart write]
static bool
send_blob(struct sol_blob *blob)
{
    int err;
    bool r = true;

    //Actually write the blob into UART bus
    err = sol_uart_feed(producer, blob);
    if (err < 0) {
        //Oh no, there's no more space left.
        if (err == -ENOSPC) {
            pending_blob = blob;
            printf("No space left in the tx buffer - saving blob for later. Data: %.*s\n",
                SOL_STR_SLICE_PRINT(sol_str_slice_from_blob(pending_blob)));
        } else {
            fprintf(stderr, "Could not not perform an UART write - Reason: %s\n",
                sol_util_strerrora(-r));
            r = false;
            sol_blob_unref(blob);
        }
    } else {
        sol_blob_unref(blob);
        if (blob == pending_blob)
            pending_blob = NULL; //Flag that data production can start once again!
    }

    return r;
}
Esempio n. 2
0
static bool
producer_make_data(void *data)
{
    void *v;
    size_t size;
    struct sol_blob *blob;
    struct sol_buffer buf = SOL_BUFFER_INIT_EMPTY;
    static uint16_t packets_created = 0;
    bool keep_running = true;
    int r;

    //Stop the production until the pendind blob is sent
    if (pending_blob) {
        printf("Waiting for blob data: %.*s to be transfered.\n",
            SOL_STR_SLICE_PRINT(sol_str_slice_from_blob(pending_blob)));
        return true;
    }

    packets_created++;

    //Generate data
    if (packets_created != MAX_PACKETS)
        r = sol_util_uuid_gen(true, true, &buf);
    else {
        r = sol_buffer_append_slice(&buf, sol_str_slice_from_str("close"));
        keep_running = false;
    }

    if (r < 0) {
        fprintf(stderr, "Could not create the UUID - Reason: %s\n",
            sol_util_strerrora(-r));
        goto err_exit;
    }

    v = sol_buffer_steal(&buf, &size);

    blob = sol_blob_new(&SOL_BLOB_TYPE_DEFAULT, NULL,
        v, size + 1);

    if (!blob) {
        fprintf(stderr, "Could not alloc memory for the blob\n");
        goto err_exit;
    }

    //Send it
    if (!send_blob(blob))
        goto err_exit;

    if (!keep_running)
        goto exit;
    return true;

err_exit:
    sol_quit();
exit:
    producer_timeout = NULL;
    return false;
}
Esempio n. 3
0
static void
on_feed_done_cb(void *data, struct sol_http_progressive_response *sse, struct sol_blob *blob, int status)
{
    struct sol_str_slice slice = sol_str_slice_from_blob(blob);

    if (sol_str_slice_str_eq(slice, "data: ") || sol_str_slice_str_eq(slice, "\n\n"))
        return;
    if (isspace(slice.data[slice.len - 1]))
        slice.len--;
    printf("Blob data *%.*s* sent\n", SOL_STR_SLICE_PRINT(slice));
}
Esempio n. 4
0
static void
on_digest_ready_cb(void *data, struct sol_message_digest *md, struct sol_blob *output)
{
    struct update_get_hash_handle *handle = data;
    struct sol_buffer buffer = SOL_BUFFER_INIT_EMPTY;
    struct sol_str_slice slice = sol_str_slice_from_blob(output);
    int r = 0;

    r = sol_buffer_append_as_base16(&buffer, slice, false);
    SOL_INT_CHECK_GOTO(r, < 0, end);

end:
    handle->cb((void *)handle->user_data, r, (char *)buffer.data);
    sol_message_digest_del(md);
    sol_buffer_fini(&buffer);
    delete_handle(handle);
}
Esempio n. 5
0
//! [uart write completed]
static void
producer_data_written(void *data, struct sol_uart *uart, struct sol_blob *blob, int status)
{
    struct sol_str_slice slice;

    slice = sol_str_slice_from_blob(blob);

    if (status < 0) {
        fprintf(stderr, "Could not write the UUID %.*s - Reason: %s\n", SOL_STR_SLICE_PRINT(slice),
            sol_util_strerrora(-status));
        sol_quit();
    } else {
        printf("Producer: UUID %.*s written\n", SOL_STR_SLICE_PRINT(slice));
        if (pending_blob) { //If we have a pending blob now it's the time to try to send it!
            if (!send_blob(pending_blob)) {
                fprintf(stderr, "Could not send the pending blob!\n");
                sol_quit();
            }
        }
    }
}
Esempio n. 6
0
static void
on_digest_ready(void *data, struct sol_message_digest *handle, struct sol_blob *digest)
{
    struct feed_ctx *ctx = data;
    struct sol_buffer buf;
    int r;

    sol_buffer_init(&buf);
    r = sol_buffer_append_as_base16(&buf, sol_str_slice_from_blob(digest), false);
    if (r == 0) {
        printf("%s\t%s\n", (char *)buf.data, ctx->file);
    }

    sol_buffer_fini(&buf);

    print_time(ctx, ctx->done, "final");

    sol_message_digest_del(handle);
    free(ctx);

    pending--;
    if (pending == 0)
        sol_quit();
}
Esempio n. 7
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);
}