Beispiel #1
0
int
handle_op(client_t *c, proto_msg_t *msg)
{
    proto_msg_t r;
    int res;
    int a = msg->p_u.op.a;
    int b = msg->p_u.op.b;
    char op = msg->p_u.op.op;

    switch (op) {
        case '+':
            res = a + b;
            break;
        case '-':
            res = a - b;
            break;
        case '*':
            res = a * b;
            break;
        case '/':
            if (b == 0) {
                proto_msg_text(&r, "cannot divide by 0");
                return (proto_send_msg(c->c_sock, &r));
            }
            res = a / b;
            break;
        default:
            proto_msg_text(&r, "invalid operator '%c'", op);
            return (proto_send_msg(c->c_sock, &r));
    }

    proto_msg_num(&r, res);
    return (proto_send_msg(c->c_sock, &r));
}
Beispiel #2
0
int
handle_num(client_t *c, proto_msg_t *msg)
{
    proto_msg_t r;
    int n = msg->p_u.num.n; 

    proto_msg_text(&r, "%d is %s number", n, (n % 2 == 0 ? "even" : "odd"));
    return (proto_send_msg(c->c_sock, &r));
}
Beispiel #3
0
int
handle_hello(client_t *c, proto_msg_t *msg)
{
    proto_msg_t r;

    c->c_version = msg->p_u.hello.version;
    proto_msg_hello(&r, PROTO_VERSION);
    return (proto_send_msg(c->c_sock, &r));
}
Beispiel #4
0
/* sender */
void sender(char msg) {
    volatile ProtoIOMBox * mbox;
    if (status) {
        mbox = proto_srv_dat.mailboxes[PROTO_MBOX_ADR_COMMON];
        mbox->outbox->header = 'C'; /* Command */
        mbox->outbox->size = 0x01; /* 0 for ping request */
        mbox->outbox->message[0] = msg; /* short message */
        mbox->outbox_s = PROTO_IO_MBOX_READY; /* Box ready */
        proto_send_msg(PROTO_MBOX_ADR_COMMON);
        /* wait to send message */
        while (mbox->outbox_s <= PROTO_IO_MBOX_SEND);
    }
}
Beispiel #5
0
int
send_text_message(client_t *c, char *msg, ...)
{
    va_list valist;
    proto_msg_t m;

    va_start(valist, msg);
    proto_msg_text(&m, msg, valist);
    va_end(valist);

    TRACE("SEND: \n");
    TRACE_MSG(&m);
    return (proto_send_msg(c->c_sock, &m));
}
Beispiel #6
0
int
handle_text(client_t *c, proto_msg_t *msg)
{
    proto_msg_t r;
    char *s;
    char *t = strtok_r(msg->p_u.text.text, " ", &s);
    int cnt = 0;

    while (t != NULL) {
        cnt++;
        t = strtok_r(NULL, " ", &s);
    }

    proto_msg_text(&r, "That was %d words!", cnt);
    return (proto_send_msg(c->c_sock, &r));
}
Beispiel #7
0
/* main work function */
void work(void) {
    unsigned short i, j;
    unsigned char mailbox_num = 0;
    volatile ProtoIOMBox * mbox;
    /* setup status */
    status_setup();
    /* setup serial console */
    usart1_setup();
    /* setup proto */
    proto_setup();
    mbox = proto_srv_dat.mailboxes[mailbox_num];
    /* setup button */
    buttons_setup();
    /* lcd setup */
    LCD_Setup();
    LCD_Clear(BLACK);
    
    for (i = 0; i < 10; i++) {
        for (j = 0; j < 10; j++) {
            LCD_Pixel(j + 10, i + 10, test.data[i * 10 + j]);
        }
    }
    LCD_Window(0, 0, 9, 9);
    LCD_RS_LOW;
    LCD_SELECT;
    for (i = 0; i < 100; i++) {
        LCD_Send(test.data[i] >> 8);
        LCD_Send(test.data[i] & 0x00ff);
    }
    LCD_Cursor(0, 0);
    LCD_DESEL;
    
    /* check status */
    check_status();
    /* send ping */
    mbox->outbox->header = 'C'; /* Command */
    mbox->outbox->size = 0x00; /* 0 for ping request */
    mbox->outbox_s = PROTO_IO_MBOX_READY; /* Box ready */
    mbox->inbox->size = 64; /* buffer len for control */
    mbox->inbox_s = PROTO_IO_MBOX_READY; /* Box ready */
    /* wait connection estabilished */
    while (status == 0);
    /* send ping message */
    proto_send_msg(mailbox_num);
    /* wait to send message */
    while (mbox->outbox_s <= PROTO_IO_MBOX_SEND);
    if (mbox->outbox_s == PROTO_IO_MBOX_COMPLETE)
        LCD_String("Con", 36, 6, 1, WHITE, GLASSY);
    else
        LCD_String("Un", 36, 6, 1, RED, GLASSY);
    /* get ping message */
    /* FIXME wtf? this not work or work parity */
    //proto_get_msg(mailbox_num);
    /* wait to get message */
    while (mbox->inbox_s <= PROTO_IO_MBOX_SEND);
    if (mbox->inbox_s == PROTO_IO_MBOX_COMPLETE) {
        LCD_String("OK", 36 + 3 * 7, 6, 1, GREEN, GLASSY);
        for (i = 0; i < mbox->inbox->size; i++)
            LCD_Char(mbox->inbox->message[i], 70 + i * 6, 6, 1, WHITE, GLASSY);
    }
    else
        LCD_String("ERR", 36 + 3 * 7, 6, 1, RED, GLASSY);
    /* infinity loop */
    while (1) {
        if (button_state.state[B_LGHT] == B_CLICK) {
            sender('+');
            button_state.state[B_LGHT] = B_RELEASE;
        }
        if (button_state.state[B_MOD] == B_CLICK) {
            sender('m');
            button_state.state[B_MOD] = B_RELEASE;
        }
        if (button_state.state[B_SET] == B_CLICK) {
            sender('-');
            button_state.state[B_SET] = B_RELEASE;
        }
        if (button_state.state[B_UP] == B_CLICK) {
            sender('<');
            button_state.state[B_UP] = B_RELEASE;
        }
        if (button_state.state[B_SU] == B_CLICK) {
            sender('p');
            button_state.state[B_SU] = B_RELEASE;
        }
        if (button_state.state[B_DWN] == B_CLICK) {
            sender('>');
            button_state.state[B_DWN] = B_RELEASE;
        }
    }
}