Skip to content

Commit

Permalink
Trim trailing zero bytes in packet before encryption. Results in smal…
Browse files Browse the repository at this point in the history
…ler amount of data to send in some cases and therefore to fewer collisions and lower battery usage.
  • Loading branch information
breaker27 committed Oct 4, 2014
1 parent d8bd0a2 commit 2e6ecdc
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions firmware/src_common/util_hw.c
Expand Up @@ -326,15 +326,25 @@ void inc_packetcounter(void)

void rfm12_send_bufx(void)
{
uint8_t packet_len = __PACKETSIZEBYTES;

// Trim 0 bytes at the end.
while ((packet_len > 0) && (bufx[packet_len - 1] == 0))
{
packet_len--;
}

packet_len = ((packet_len - 1) / 16 + 1) * 16;

UART_PUTS("Before encryption: ");
print_bytearray(bufx, __PACKETSIZEBYTES);
print_bytearray(bufx, packet_len);

uint8_t aes_byte_count = aes256_encrypt_cbc(bufx, __PACKETSIZEBYTES);
packet_len = aes256_encrypt_cbc(bufx, packet_len);

UART_PUTS("After encryption: ");
print_bytearray(bufx, aes_byte_count);
print_bytearray(bufx, packet_len);

rfm12_tx(aes_byte_count, 0, (uint8_t *) bufx);
rfm12_tx(packet_len, 0, (uint8_t *) bufx);
}

// Go to sleep. Wakeup by RFM12 wakeup-interrupt or pin change (if configured).
Expand Down

0 comments on commit 2e6ecdc

Please sign in to comment.