-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotocol.c
47 lines (35 loc) · 1.24 KB
/
protocol.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "protocol.h"
#include <string.h>
#include <inttypes.h>
#include <unistd.h>
msg_t msg_buf;
int get_msg_type(const msg_t * const msg_buf) {
if (!strncmp(msg_buf->header.cmd, GOOD_DAY, MSG_LENGTH) ||
!strncmp(msg_buf->header.cmd, ADD, MSG_LENGTH) ||
!strncmp(msg_buf->header.cmd, CAN_ADD, MSG_LENGTH) ||
!strncmp(msg_buf->header.cmd, CONNECT_ME, MSG_LENGTH))
return CMPLX;
return SIMPL;
}
size_t calc_msg_len (const msg_t * const msg_buf, const size_t data_len) {
return (get_msg_type(msg_buf) == CMPLX)
?sizeof(msg_buf->header)+sizeof(msg_buf->param)+data_len
:sizeof(msg_buf->header)+data_len;
}
size_t calc_data_len (const msg_t * const msg_buf, const size_t msg_len) {
size_t pref_len = (get_msg_type(msg_buf) == CMPLX)
?sizeof(msg_buf->header)+sizeof(msg_buf->param)
:sizeof(msg_buf->header);
return (pref_len > msg_len)
?0
:(msg_len-pref_len);
}
int validate_msg (const msg_t * const msg_buf, const uint64_t expected_seq,
const size_t msg_len, const char * const expected_cmd) {
if (expected_cmd == NULL ||
strncmp(msg_buf->header.cmd, expected_cmd, MSG_LENGTH) != 0)
return 0;
else
return msg_len >= calc_msg_len(msg_buf, 0)
&& msg_buf->header.cmd_seq == expected_seq;
}