Project

General

Profile

Statistics
| Revision:

root / proj / libs / uart / src / hltp.c @ 344

History | View | Annotate | Download (5.71 KB)

1 289 up20180642
#include <lcom/lcf.h>
2
3
#include "hltp.h"
4
5
#include "uart.h"
6
7
static void* hltp_interpret_string(const uint8_t *p, const size_t sz){
8
    void *ret = malloc((sz+1)*sizeof(char));
9 324 up20180642
    for(size_t i = 0; i < sz; ++i) ((char*)ret)[i] = (char)p[i];
10 289 up20180642
    ((char*)ret)[sz] = '\0';
11
    return ret;
12
}
13
int hltp_send_string(const char *p){
14
    uint8_t type = hltp_type_string;
15 324 up20180642
    const uint8_t* ptr[2]; ptr[0] = &type; ptr[1] = (const uint8_t*)p;
16
    size_t         sz[2]; sz [0] =     1; sz [1] = strlen(p);
17 289 up20180642
    return nctp_send(2, ptr, sz);
18
}
19
20 342 up20180655
static host_info_t* hltp_interpret_host_info(const uint8_t *p, const size_t sz) {
21 344 up20180642
    printf("%s\n", __func__);
22
    printf("sizeof(host_info_t)=%d, sz=%d\n", sizeof(host_info_t), sz);
23 342 up20180655
    if (sz != sizeof(host_info_t)) { printf("%d should equal %d\n", sz, sizeof(host_info_t)); return NULL;}
24 311 up20180655
    host_info_t *ret = (host_info_t*)malloc(sizeof(host_info_t));
25 342 up20180655
    //size_t pos = 0;
26 311 up20180655
    // players information
27 342 up20180655
    memcpy(ret, p, sz);
28
    /*
29 329 up20180655
    pos += sizeof(int16_t)*10;
30 311 up20180655
    // size of arrays
31 327 up20180655
    memcpy(&(ret->no_bullets), p + pos, sizeof(uint8_t));
32
    pos += sizeof(uint8_t);
33 311 up20180655
    size_t sz = ret->no_bullets;
34
    // array containing the X positions of the bullets
35 329 up20180655
    (ret->bullets_x) = (int16_t*)malloc(sizeof(int16_t)*sz);
36
    memcpy((ret->bullets_x), p + pos, sizeof(int16_t)*sz);
37
    pos += sizeof(int16_t)*sz;
38 311 up20180655
    // array containing the Y positions of the bullets
39 329 up20180655
    (ret->bullets_y) = (int16_t*)malloc(sizeof(int16_t)*(ret->no_bullets));
40
    memcpy((ret->bullets_y), p + pos, sizeof(int16_t)*sz);
41
    pos += sizeof(int16_t)*sz;
42 311 up20180655
    // array containing the X velocity of the bullets
43 329 up20180655
    (ret->bullets_vx) = (int16_t*)malloc(sizeof(int16_t)*(ret->no_bullets));
44
    memcpy((ret->bullets_vx), p + pos, sizeof(int16_t)*sz);
45
    pos += sizeof(int16_t)*sz;
46 311 up20180655
    // array containing the Y velocity of the bullets
47 329 up20180655
    (ret->bullets_vy) = (int16_t*)malloc(sizeof(int16_t)*(ret->no_bullets));
48
    memcpy((ret->bullets_vy), p + pos, sizeof(int16_t)*sz);
49
    pos += sizeof(int16_t)*sz;
50 311 up20180655
    // array containing the shooter id of the bullets
51
    (ret->bullets_shooter) = (bool*)malloc(sizeof(bool)*(ret->no_bullets));
52
    memcpy((ret->bullets_shooter), p + pos, sizeof(bool)*sz);
53 342 up20180655
    */
54 311 up20180655
    return ret;
55
}
56 344 up20180642
int hltp_send_host_info(const host_info_t *p) { printf("%s, %d\n",__func__, rand());
57 311 up20180655
58 344 up20180642
    printf("    sending angle: 0x%04X\n", p->host_angle);
59
60 311 up20180655
    uint8_t type = hltp_type_host;
61 342 up20180655
    const uint8_t* ptr[11]; size_t sz[11];
62 320 up20180655
    ptr[0]  = (uint8_t*)&   type                  ;     sz[0]   = 1;
63 329 up20180655
    ptr[1]  = (uint8_t*)&p->host_x                ;     sz[1]   = sizeof(int16_t);
64
    ptr[2]  = (uint8_t*)&p->host_y                ;     sz[2]   = sizeof(int16_t);
65
    ptr[3]  = (uint8_t*)&p->host_angle            ;     sz[3]   = sizeof(int16_t);
66
    ptr[4]  = (uint8_t*)&p->host_health           ;     sz[4]   = sizeof(int16_t);
67
    ptr[5]  = (uint8_t*)&p->host_current_health   ;     sz[5]   = sizeof(int16_t);
68
    ptr[6]  = (uint8_t*)&p->remote_x              ;     sz[6]   = sizeof(int16_t);
69
    ptr[7]  = (uint8_t*)&p->remote_y              ;     sz[7]   = sizeof(int16_t);
70
    ptr[8]  = (uint8_t*)&p->remote_angle          ;     sz[8]   = sizeof(int16_t);
71
    ptr[9]  = (uint8_t*)&p->remote_health         ;     sz[9]   = sizeof(int16_t);
72
    ptr[10] = (uint8_t*)&p->remote_current_health ;     sz[10]  = sizeof(int16_t);
73 342 up20180655
    /*
74 327 up20180655
    ptr[11] = (uint8_t*)&p->no_bullets            ;     sz[11]  = sizeof(uint8_t);
75 329 up20180655
    ptr[12] = (uint8_t*) p->bullets_x             ;     sz[12]  = sizeof(int16_t) * p->no_bullets;
76
    ptr[13] = (uint8_t*) p->bullets_y             ;     sz[13]  = sizeof(int16_t) * p->no_bullets;
77
    ptr[14] = (uint8_t*) p->bullets_vx            ;     sz[14]  = sizeof(int16_t) * p->no_bullets;
78
    ptr[15] = (uint8_t*) p->bullets_vy            ;     sz[15]  = sizeof(int16_t) * p->no_bullets;
79
    ptr[16] = (uint8_t*) p->bullets_shooter       ;     sz[16]  = sizeof(bool) * p->no_bullets;
80 342 up20180655
    */
81
    return nctp_send(11, ptr, sz);
82 311 up20180655
}
83
84
static remote_info_t* hltp_interpret_remote_info(const uint8_t *p) {
85
    remote_info_t *ret = (remote_info_t*)malloc(sizeof(remote_info_t));
86
    size_t pos = 0;
87
    // keys pressed
88
    memcpy(&(ret->remote_keys_pressed), p + pos, sizeof(keys_t));
89
    pos += sizeof(keys_t);
90
    // mouse positions
91 320 up20180655
    memcpy(&(ret->remote_angle), p + pos, sizeof(double));
92 311 up20180655
93
    return ret;
94
}
95
int hltp_send_remote_info(const remote_info_t *p) {
96
97
    uint8_t type = hltp_type_remote;
98 325 up20180642
    const uint8_t* ptr[3]; size_t sz[3];
99 315 up20180655
    ptr[0]  = (uint8_t*)&   type                  ;     sz[0] = 1;
100 320 up20180655
    ptr[1]  = (uint8_t*)&p->remote_keys_pressed   ;     sz[1] = sizeof(keys_t);
101
    ptr[2]  = (uint8_t*)&p->remote_angle          ;     sz[2] = sizeof(double);
102
    return nctp_send(3, ptr, sz);
103
}
104
105
static bullet_info_t* hltp_interpret_bullet_info(const uint8_t *p) {
106
    bullet_info_t *ret = (bullet_info_t*)malloc(sizeof(bullet_info_t));
107
    memcpy(&(ret->new_bullet), p, sizeof(bool));
108
109
    return ret;
110
}
111
112
int hltp_send_bullet_info(const bullet_info_t *p) {
113
    uint8_t type = hltp_type_bullet;
114 325 up20180642
    const uint8_t* ptr[2]; size_t sz[2];
115 320 up20180655
    ptr[0]  = (uint8_t*)&   type            ;     sz[0] = 1;
116
    ptr[1]  = (uint8_t*)&p->new_bullet      ;     sz[1] = sizeof(bool);
117 311 up20180655
    return nctp_send(2, ptr, sz);
118
}
119
120 344 up20180642
hltp_type hltp_interpret(const uint8_t *p, const size_t sz, void **dest){ printf("%s %d\n",__func__, rand());
121 289 up20180642
    uint8_t ret = p[0];
122 344 up20180642
    printf("    ret=%d\n", ret);
123 289 up20180642
    switch(ret){
124 320 up20180655
        case hltp_type_string: *dest = hltp_interpret_string     (p+1, sz-1);   break;
125 342 up20180655
        case hltp_type_host  : *dest = hltp_interpret_host_info  (p+1, sz-1);   break;
126
        case hltp_type_remote: *dest = hltp_interpret_remote_info(p+1);   break;
127
        case hltp_type_bullet: *dest = hltp_interpret_bullet_info(p+1);   break;
128 289 up20180642
        default: *dest = NULL; break;
129
    }
130
    return ret;
131
}