예제 #1
0
void run_test_ok_data(void)
{
    MCSPacket *pkt_wr, *pkt_rd;
    unsigned char data[] = "Hello World!";
    bool error = false;

    start_test();

    abs_test_printf("Testing command OK_DATA\n");

    pkt_wr = mcs_ok_packet_data(data, strlen((char *)data));

    if(pkt_wr != NULL) {
        if(mcs_write_command_and_free(pkt_wr, pipe_fd[1]) < 0) {
            abs_test_printf("mcs_write_command returned a wrong value\n");
            error = true;
        } else {
            pkt_rd = mcs_read_command(pipe_fd[0], pipe_fd[1]);
            error = !mcs_cmp(pkt_wr, pkt_rd);
            mcs_free(pkt_rd);
        }
    } else {
        error = true;
    }

    end_test();

    if(error) {
        abs_test_add_result(FAIL, "TYPE_OK_DATA");
    } else {
        abs_test_add_result(PASS, "TYPE_OK_DATA");
    }
}
예제 #2
0
void run_test_ok(void)
{
    MCSPacket *pkt_wr, *pkt_rd;
    bool error = false;

    start_test();

    abs_test_printf("Testing command OK\n");

    pkt_wr = mcs_ok_packet();

    if(pkt_wr != NULL) {
        if(mcs_write_command_and_free(pkt_wr, pipe_fd[1]) < 0) {
            abs_test_printf("mcs_write_command returned a wrong value\n");
            error = true;
        } else {
            pkt_rd = mcs_read_command(pipe_fd[0], pipe_fd[1]);
            error = !mcs_cmp(pkt_wr, pkt_rd);
            mcs_free(pkt_rd);
        }
    } else {
        error = EUNDEF;
    }

    end_test();

    if(error) {
        abs_test_add_result(FAIL, "TYPE_OK");
    } else {
        abs_test_add_result(PASS, "TYPE_OK");
    }
}
예제 #3
0
파일: mcs.c 프로젝트: mark-mb/abs-software
/*
 * Read a command from a given file descriptor and translate it into a
 * MCSPacket
 */
MCSPacket *mcs_read_command(int rfd, int wfd)
{
    MCSPacket *pkt;

    if(rfd < 0) {
        printf_dbg("FD not valid\n");
        return NULL;
    }

    pkt = abs_malloc0(sizeof(*pkt));

    if(abs_read(rfd, (char *)&pkt->id, sizeof(pkt->id), MCS_READ_TIMEOUT_US)
                                                    < (int)sizeof(pkt->id)) {
        printf_dbg("Could not read packet ID\n");
        goto error_no_notify;
    }

    if(abs_read(rfd, (char *)&pkt->type, sizeof(char), MCS_READ_TIMEOUT_US)
                                                    < (int)sizeof(char)) {
        printf_dbg("Could not read packet type\n");
        goto error;
    }

    switch(pkt->type) {
        case MCS_TYPE_MESSAGE:
        case MCS_TYPE_STATE:
        case MCS_TYPE_PAYLOAD:
            if(abs_read(rfd, (char *)&pkt->cmd, sizeof(pkt->cmd),
                                MCS_READ_TIMEOUT_US) < (int)sizeof(pkt->cmd)) {
                printf_dbg("Could not read command\n");
                goto error;
            }

            if(read_check_config(rfd, pkt) < 0) {
                goto error;
            }
            break;

        case MCS_TYPE_ERR:
            pkt->data_size = sizeof(int);
            pkt->data = malloc(pkt->data_size);
            if(abs_read(rfd, (char *)pkt->data, pkt->data_size,
                                MCS_READ_TIMEOUT_US) < pkt->data_size) {
                printf_dbg("Could not read error code\n");
                goto error;
            }
            break;

        case MCS_TYPE_OK_DATA:
            if(abs_read(rfd, (char *)&pkt->data_size, sizeof(pkt->data_size),
                        MCS_READ_TIMEOUT_US) < (int)sizeof(pkt->data_size)) {
                printf_dbg("Could not read raw data size\n");
                goto error;
            }

            pkt->data = malloc(pkt->data_size);
            if(abs_read(rfd, (char *)pkt->data, pkt->data_size,
                                MCS_READ_TIMEOUT_US) < pkt->data_size) {
                printf_dbg("Could not read raw data\n");
                goto error;
            }
            break;

        case MCS_TYPE_OK:
            break;

        default:
            printf_dbg("Non valid command\n");
            goto error;
    }

    return pkt;

error:
    mcs_write_command_and_free(mcs_err_packet(pkt, EBADFORMAT), wfd);
    mcs_free(pkt);
error_no_notify:
    return NULL;
}