Skip to content

Commit 26387a4

Browse files
Add strict compiler flags and fix all resulting warnings
2 parents 35a8aeb + 1ea9031 commit 26387a4

4 files changed

Lines changed: 55 additions & 52 deletions

File tree

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
CC = gcc
2-
CFLAGS = -Wall -Wextra -Werror -std=c99 -pedantic -I./include
2+
CFLAGS ?= -O2 -Iinclude -Wall -Wextra -Wpedantic -Wconversion -Wshadow \
3+
-Wcast-align -Wcast-qual -Wpointer-arith -Wformat=2 \
4+
-Wmissing-prototypes -Wstrict-prototypes -Wredundant-decls -Wundef \
5+
-std=c11
36
LDFLAGS =
47

58
SRC_DIR = src

examples/tm_example.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ int main(void) {
2121

2222
result = sdlp_tm_create_frame(&frame, spacecraft_id, virtual_channel,
2323
(const uint8_t *)telemetry_data,
24-
strlen(telemetry_data));
24+
(uint16_t)strlen(telemetry_data));
2525

2626
if (result != SDLP_SUCCESS) {
2727
printf("Error creating frame: %d\n", result);

src/sdlp_tc.c

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ int sdlp_tc_create_frame(sdlp_tc_frame_t *frame, uint16_t spacecraft_id,
1414
frame->header.bypass_flag = 0;
1515
frame->header.control_command_flag = 0;
1616
frame->header.reserved = 0;
17-
frame->header.spacecraft_id = spacecraft_id & 0x3FF;
18-
frame->header.virtual_channel_id = virtual_channel_id & 0x3F;
19-
frame->header.frame_length = data_length - 1;
17+
frame->header.spacecraft_id = (uint16_t)(spacecraft_id & 0x3ffu);
18+
frame->header.virtual_channel_id = (uint8_t)(virtual_channel_id & 0x3fu);
19+
frame->header.frame_length = (uint16_t)(data_length - 1u);
2020
frame->header.frame_sequence_number = frame_seq_num;
2121

2222
memcpy(frame->data, data, data_length);
@@ -46,29 +46,29 @@ int sdlp_tc_encode_frame(const sdlp_tc_frame_t *frame, uint8_t *buffer,
4646

4747
size_t offset = 0;
4848

49-
buffer[offset++] = (frame->header.transfer_frame_version << 6) |
50-
((frame->header.bypass_flag & 0x01) << 5) |
51-
((frame->header.control_command_flag & 0x01) << 4) |
52-
((frame->header.reserved & 0x03) << 2) |
53-
((frame->header.spacecraft_id >> 8) & 0x03);
54-
buffer[offset++] = frame->header.spacecraft_id & 0xFF;
55-
buffer[offset++] = ((frame->header.virtual_channel_id & 0x3F) << 2) | 0x00;
56-
buffer[offset++] = (frame->header.frame_length >> 8) & 0xFF;
57-
buffer[offset++] = frame->header.frame_length & 0xFF;
49+
buffer[offset++] = (uint8_t)((frame->header.transfer_frame_version << 6) |
50+
((frame->header.bypass_flag & 0x01u) << 5) |
51+
((frame->header.control_command_flag & 0x01u) << 4) |
52+
((frame->header.reserved & 0x03u) << 2) |
53+
((frame->header.spacecraft_id >> 8) & 0x03u));
54+
buffer[offset++] = (uint8_t)(frame->header.spacecraft_id & 0xffu);
55+
buffer[offset++] = (uint8_t)(((frame->header.virtual_channel_id & 0x3fu) << 2) | 0x00u);
56+
buffer[offset++] = (uint8_t)((frame->header.frame_length >> 8) & 0xffu);
57+
buffer[offset++] = (uint8_t)(frame->header.frame_length & 0xffu);
5858

5959
#ifdef TC_SEGMENT_HEADER_ENABLED
6060
if (!frame->header.control_command_flag) {
61-
buffer[offset++] = ((frame->segment_header.sequence_flags & 0x03) << 6) |
62-
(frame->segment_header.map_id & 0x3F);
61+
buffer[offset++] = (uint8_t)(((frame->segment_header.sequence_flags & 0x03u) << 6) |
62+
(frame->segment_header.map_id & 0x3fu));
6363
}
6464
#endif
6565

6666
memcpy(&buffer[offset], frame->data, frame->data_length);
6767
offset += frame->data_length;
6868

6969
uint16_t crc = sdlp_crc16(buffer, offset);
70-
buffer[offset++] = (crc >> 8) & 0xFF;
71-
buffer[offset++] = crc & 0xFF;
70+
buffer[offset++] = (uint8_t)((crc >> 8) & 0xffu);
71+
buffer[offset++] = (uint8_t)(crc & 0xffu);
7272

7373
*encoded_size = offset;
7474

@@ -85,34 +85,34 @@ int sdlp_tc_decode_frame(const uint8_t *buffer, size_t buffer_size,
8585

8686
size_t offset = 0;
8787

88-
frame->header.transfer_frame_version = (buffer[offset] >> 6) & 0x03;
89-
frame->header.bypass_flag = (buffer[offset] >> 5) & 0x01;
90-
frame->header.control_command_flag = (buffer[offset] >> 4) & 0x01;
91-
frame->header.reserved = (buffer[offset] >> 2) & 0x03;
92-
frame->header.spacecraft_id = ((buffer[offset] & 0x03) << 8) | buffer[offset + 1];
88+
frame->header.transfer_frame_version = (uint8_t)((buffer[offset] >> 6) & 0x03u);
89+
frame->header.bypass_flag = (uint8_t)((buffer[offset] >> 5) & 0x01u);
90+
frame->header.control_command_flag = (uint8_t)((buffer[offset] >> 4) & 0x01u);
91+
frame->header.reserved = (uint8_t)((buffer[offset] >> 2) & 0x03u);
92+
frame->header.spacecraft_id = (uint16_t)(((uint16_t)(buffer[offset] & 0x03u) << 8) | buffer[offset + 1]);
9393
offset += 2;
9494

95-
frame->header.virtual_channel_id = (buffer[offset] >> 2) & 0x3F;
95+
frame->header.virtual_channel_id = (uint8_t)((buffer[offset] >> 2) & 0x3fu);
9696
offset++;
9797

98-
frame->header.frame_length = (buffer[offset] << 8) | buffer[offset + 1];
98+
frame->header.frame_length = (uint16_t)(((uint16_t)buffer[offset] << 8) | buffer[offset + 1]);
9999
offset += 2;
100100

101101
#ifdef TC_SEGMENT_HEADER_ENABLED
102102
if (!frame->header.control_command_flag) {
103103
if (buffer_size < TC_PRIMARY_HEADER_SIZE + TC_SEGMENT_HEADER_SIZE + TC_FRAME_ERROR_CONTROL_SIZE) {
104104
return SDLP_ERROR_INVALID_FRAME;
105105
}
106-
frame->segment_header.sequence_flags = (buffer[offset] >> 6) & 0x03;
107-
frame->segment_header.map_id = buffer[offset] & 0x3F;
106+
frame->segment_header.sequence_flags = (uint8_t)((buffer[offset] >> 6) & 0x03u);
107+
frame->segment_header.map_id = (uint8_t)(buffer[offset] & 0x3fu);
108108
offset++;
109-
frame->data_length = buffer_size - TC_PRIMARY_HEADER_SIZE - TC_SEGMENT_HEADER_SIZE -
110-
TC_FRAME_ERROR_CONTROL_SIZE;
109+
frame->data_length = (uint16_t)(buffer_size - TC_PRIMARY_HEADER_SIZE - TC_SEGMENT_HEADER_SIZE -
110+
TC_FRAME_ERROR_CONTROL_SIZE);
111111
} else {
112-
frame->data_length = buffer_size - TC_PRIMARY_HEADER_SIZE - TC_FRAME_ERROR_CONTROL_SIZE;
112+
frame->data_length = (uint16_t)(buffer_size - TC_PRIMARY_HEADER_SIZE - TC_FRAME_ERROR_CONTROL_SIZE);
113113
}
114114
#else
115-
frame->data_length = buffer_size - TC_PRIMARY_HEADER_SIZE - TC_FRAME_ERROR_CONTROL_SIZE;
115+
frame->data_length = (uint16_t)(buffer_size - TC_PRIMARY_HEADER_SIZE - TC_FRAME_ERROR_CONTROL_SIZE);
116116
#endif
117117

118118
if (frame->data_length > TC_MAX_DATA_SIZE) {
@@ -122,7 +122,7 @@ int sdlp_tc_decode_frame(const uint8_t *buffer, size_t buffer_size,
122122
memcpy(frame->data, &buffer[offset], frame->data_length);
123123
offset += frame->data_length;
124124

125-
frame->fecf = (buffer[offset] << 8) | buffer[offset + 1];
125+
frame->fecf = (uint16_t)(((uint16_t)buffer[offset] << 8) | buffer[offset + 1]);
126126

127127
uint16_t calculated_crc = sdlp_crc16(buffer, buffer_size - TC_FRAME_ERROR_CONTROL_SIZE);
128128

@@ -138,8 +138,8 @@ int sdlp_tc_set_segment_header(sdlp_tc_frame_t *frame, sdlp_tc_seq_flag_t sequen
138138
if (!frame) {
139139
return SDLP_ERROR_INVALID_PARAM;
140140
}
141-
frame->segment_header.sequence_flags = sequence_flags & 0x03;
142-
frame->segment_header.map_id = map_id & 0x3F;
141+
frame->segment_header.sequence_flags = (uint8_t)(sequence_flags & 0x03u);
142+
frame->segment_header.map_id = (uint8_t)(map_id & 0x3fu);
143143
return SDLP_SUCCESS;
144144
}
145145
#endif

src/sdlp_tm.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ int sdlp_tm_create_frame(sdlp_tm_frame_t *frame, uint16_t spacecraft_id,
1414
memset(frame, 0, sizeof(sdlp_tm_frame_t));
1515

1616
frame->header.transfer_frame_version = SDLP_VERSION;
17-
frame->header.spacecraft_id = spacecraft_id & 0x3FF;
18-
frame->header.virtual_channel_id = virtual_channel_id & 0x07;
17+
frame->header.spacecraft_id = (uint16_t)(spacecraft_id & 0x3ffu);
18+
frame->header.virtual_channel_id = (uint8_t)(virtual_channel_id & 0x07u);
1919
frame->header.ocf_flag = 0;
2020
frame->header.master_channel_frame_count = tm_frame_counter++;
2121
frame->header.virtual_channel_frame_count = 0;
@@ -42,24 +42,24 @@ int sdlp_tm_encode_frame(const sdlp_tm_frame_t *frame, uint8_t *buffer,
4242

4343
size_t offset = 0;
4444

45-
buffer[offset++] = (frame->header.transfer_frame_version << 6) |
46-
((frame->header.spacecraft_id >> 4) & 0x3F);
47-
buffer[offset++] = ((frame->header.spacecraft_id & 0x0F) << 4) |
48-
((frame->header.virtual_channel_id & 0x07) << 1) |
49-
(frame->header.ocf_flag & 0x01);
45+
buffer[offset++] = (uint8_t)((frame->header.transfer_frame_version << 6) |
46+
((frame->header.spacecraft_id >> 4) & 0x3fu));
47+
buffer[offset++] = (uint8_t)(((frame->header.spacecraft_id & 0x0fu) << 4) |
48+
((frame->header.virtual_channel_id & 0x07u) << 1) |
49+
(frame->header.ocf_flag & 0x01u));
5050
buffer[offset++] = frame->header.master_channel_frame_count;
5151
buffer[offset++] = frame->header.virtual_channel_frame_count;
5252

5353
uint16_t data_field_status = frame->header.transfer_frame_data_field_status;
54-
buffer[offset++] = (data_field_status >> 8) & 0xFF;
55-
buffer[offset++] = data_field_status & 0xFF;
54+
buffer[offset++] = (uint8_t)((data_field_status >> 8) & 0xffu);
55+
buffer[offset++] = (uint8_t)(data_field_status & 0xffu);
5656

5757
memcpy(&buffer[offset], frame->data, frame->data_length);
5858
offset += frame->data_length;
5959

6060
uint16_t crc = sdlp_crc16(buffer, offset);
61-
buffer[offset++] = (crc >> 8) & 0xFF;
62-
buffer[offset++] = crc & 0xFF;
61+
buffer[offset++] = (uint8_t)((crc >> 8) & 0xffu);
62+
buffer[offset++] = (uint8_t)(crc & 0xffu);
6363

6464
*encoded_size = offset;
6565

@@ -76,22 +76,22 @@ int sdlp_tm_decode_frame(const uint8_t *buffer, size_t buffer_size,
7676

7777
size_t offset = 0;
7878

79-
frame->header.transfer_frame_version = (buffer[offset] >> 6) & 0x03;
80-
frame->header.spacecraft_id = ((buffer[offset] & 0x3F) << 4) | ((buffer[offset + 1] >> 4) & 0x0F);
79+
frame->header.transfer_frame_version = (uint8_t)((buffer[offset] >> 6) & 0x03u);
80+
frame->header.spacecraft_id = (uint16_t)(((buffer[offset] & 0x3fu) << 4) | ((buffer[offset + 1] >> 4) & 0x0fu));
8181
offset++;
8282

83-
frame->header.virtual_channel_id = (buffer[offset] >> 1) & 0x07;
84-
frame->header.ocf_flag = buffer[offset] & 0x01;
83+
frame->header.virtual_channel_id = (uint8_t)((buffer[offset] >> 1) & 0x07u);
84+
frame->header.ocf_flag = (uint8_t)(buffer[offset] & 0x01u);
8585
offset++;
8686

8787
frame->header.master_channel_frame_count = buffer[offset++];
8888
frame->header.virtual_channel_frame_count = buffer[offset++];
8989

90-
uint16_t data_field_status = (buffer[offset] << 8) | buffer[offset + 1];
90+
uint16_t data_field_status = (uint16_t)(((uint16_t)buffer[offset] << 8) | buffer[offset + 1]);
9191
frame->header.transfer_frame_data_field_status = data_field_status;
9292
offset += 2;
9393

94-
frame->data_length = buffer_size - TM_PRIMARY_HEADER_SIZE - TM_FRAME_ERROR_CONTROL_SIZE;
94+
frame->data_length = (uint16_t)(buffer_size - TM_PRIMARY_HEADER_SIZE - TM_FRAME_ERROR_CONTROL_SIZE);
9595

9696
if (frame->data_length > TM_MAX_DATA_SIZE) {
9797
return SDLP_ERROR_INVALID_FRAME;
@@ -100,7 +100,7 @@ int sdlp_tm_decode_frame(const uint8_t *buffer, size_t buffer_size,
100100
memcpy(frame->data, &buffer[offset], frame->data_length);
101101
offset += frame->data_length;
102102

103-
frame->fecf = (buffer[offset] << 8) | buffer[offset + 1];
103+
frame->fecf = (uint16_t)(((uint16_t)buffer[offset] << 8) | buffer[offset + 1]);
104104

105105
uint16_t calculated_crc = sdlp_crc16(buffer, buffer_size - TM_FRAME_ERROR_CONTROL_SIZE);
106106

0 commit comments

Comments
 (0)