Branch data Line data Source code
1 : : #include <stdint.h>
2 : : #include <string.h>
3 : :
4 : : #include "can.h"
5 : : #include "message_types.h"
6 : : #include "msg_common.h"
7 : : #include "msg_stream.h"
8 : :
9 : : // Fields are 24-bit big-endian.
10 : : #define STREAM_SIZE_MAX 0xFFFFFFu
11 : :
12 : 0 : bool build_stream_status_msg(can_msg_prio_t prio, uint16_t timestamp, uint32_t total_size,
13 : : uint32_t tx_size, can_msg_t *output) {
14 [ # # ]: 0 : if (!output) {
15 : : return false;
16 : : }
17 [ # # ]: 0 : if ((total_size > STREAM_SIZE_MAX) || (tx_size > STREAM_SIZE_MAX)) {
18 : : return false;
19 : : }
20 : :
21 : 0 : output->sid = build_sid(prio, MSG_STREAM_STATUS, 0);
22 : 0 : write_timestamp(timestamp, output);
23 : :
24 : 0 : output->data[2] = (uint8_t)((total_size >> 16) & 0xFF);
25 : 0 : output->data[3] = (uint8_t)((total_size >> 8) & 0xFF);
26 : 0 : output->data[4] = (uint8_t)(total_size & 0xFF);
27 : 0 : output->data[5] = (uint8_t)((tx_size >> 16) & 0xFF);
28 : 0 : output->data[6] = (uint8_t)((tx_size >> 8) & 0xFF);
29 : 0 : output->data[7] = (uint8_t)(tx_size & 0xFF);
30 : 0 : output->data_len = 8;
31 : :
32 : 0 : return true;
33 : : }
34 : :
35 : 0 : bool build_stream_data_msg(can_msg_prio_t prio, uint16_t timestamp, uint8_t seq_id,
36 : : const uint8_t *payload, uint8_t payload_len, can_msg_t *output) {
37 [ # # ]: 0 : if (!output) {
38 : : return false;
39 : : }
40 [ # # ]: 0 : if (payload_len > STREAM_DATA_MAX_PAYLOAD_LEN) {
41 : : return false;
42 : : }
43 : :
44 : 0 : output->sid = build_sid(prio, MSG_STREAM_DATA, seq_id);
45 : 0 : write_timestamp(timestamp, output);
46 : :
47 : 0 : memcpy(&output->data[2], payload, payload_len);
48 : :
49 : 0 : output->data_len = (uint8_t)(payload_len + 2);
50 : :
51 : 0 : return true;
52 : : }
53 : :
54 : 0 : bool build_stream_retry_msg(can_msg_prio_t prio, uint16_t timestamp, uint8_t seq_id,
55 : : can_msg_t *output) {
56 [ # # ]: 0 : if (!output) {
57 : : return false;
58 : : }
59 : :
60 : 0 : output->sid = build_sid(prio, MSG_STREAM_RETRY, seq_id);
61 : 0 : write_timestamp(timestamp, output);
62 : :
63 : 0 : output->data_len = 2;
64 : :
65 : 0 : return true;
66 : : }
67 : :
68 : 0 : bool get_stream_status(const can_msg_t *msg, uint32_t *total_size, uint32_t *tx_size) {
69 [ # # # # ]: 0 : if (!msg || !total_size || !tx_size) {
70 : : return false;
71 : : }
72 [ # # # # ]: 0 : if (get_message_type(msg) != MSG_STREAM_STATUS || msg->data_len != 8) {
73 : : return false;
74 : : }
75 : :
76 : 0 : *total_size = ((uint32_t)msg->data[2] << 16) | ((uint32_t)msg->data[3] << 8) | msg->data[4];
77 : 0 : *tx_size = ((uint32_t)msg->data[5] << 16) | ((uint32_t)msg->data[6] << 8) | msg->data[7];
78 : :
79 : 0 : return true;
80 : : }
81 : :
82 : 0 : bool get_stream_data(const can_msg_t *msg, uint8_t *seq_id, uint8_t *payload,
83 : : uint8_t *payload_len) {
84 [ # # # # ]: 0 : if (!msg || !seq_id || !payload || !payload_len) {
85 : : return false;
86 : : }
87 [ # # # # : 0 : if ((get_message_type(msg) != MSG_STREAM_DATA) || (msg->data_len < 4) || (msg->data_len > 8)) {
# # ]
88 : : return false;
89 : : }
90 : :
91 : 0 : uint8_t msg_payload_len = (uint8_t)(msg->data_len - 3);
92 : 0 : memcpy(payload, &msg->data[3], msg_payload_len);
93 : :
94 : 0 : *seq_id = msg->data[2];
95 : 0 : *payload_len = msg_payload_len;
96 : :
97 : 0 : return true;
98 : : }
99 : :
100 : 0 : bool get_stream_retry_seq_id(const can_msg_t *msg, uint8_t *seq_id) {
101 [ # # ]: 0 : if (!msg || !seq_id) {
102 : : return false;
103 : : }
104 [ # # # # ]: 0 : if (get_message_type(msg) != MSG_STREAM_RETRY || msg->data_len != 2) {
105 : : return false;
106 : : }
107 : :
108 : 0 : *seq_id = get_message_metadata(msg);
109 : :
110 : 0 : return true;
111 : : }
|