Skip to content

Commit

Permalink
[FORMAT] Formatted all files with clang-format. Renumbered tests to 71
Browse files Browse the repository at this point in the history
Former-commit-id: 4715608
  • Loading branch information
AVSurfer123 committed Aug 22, 2020
1 parent f238847 commit 02130c0
Show file tree
Hide file tree
Showing 123 changed files with 5,037 additions and 5,185 deletions.
173 changes: 86 additions & 87 deletions dev_handler/dev_handler.c
100755 → 100644

Large diffs are not rendered by default.

81 changes: 41 additions & 40 deletions dev_handler/message.c
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// ******************************** Utility ********************************* //

void print_bytes(uint8_t *data, size_t len) {
void print_bytes(uint8_t* data, size_t len) {
printf("0x");
for (int i = 0; i < len; i++) {
printf("%02X ", data[i]);
Expand All @@ -23,7 +23,7 @@ void print_bytes(uint8_t *data, size_t len) {
*/
static size_t device_write_payload_size(uint8_t device_type, uint32_t param_bitmap) {
size_t result = BITMAP_SIZE;
device_t *dev = get_device(device_type);
device_t* dev = get_device(device_type);
// Loop through each of the device's parameters and add the size of the parameter
for (int i = 0; ((param_bitmap >> i) > 0) && (i < MAX_PARAMS); i++) {
// Include parameter i if the i-th bit is on
Expand Down Expand Up @@ -55,7 +55,7 @@ static size_t device_write_payload_size(uint8_t device_type, uint32_t param_bitm
* 0 if successful
* -1 if max_payload_length is too small
*/
static int append_payload(message_t *msg, uint8_t *data, size_t len) {
static int append_payload(message_t* msg, uint8_t* data, size_t len) {
memcpy(&(msg->payload[msg->payload_length]), data, len);
msg->payload_length += len;
return (msg->payload_length > msg->max_payload_length) ? -1 : 0;
Expand All @@ -69,7 +69,7 @@ static int append_payload(message_t *msg, uint8_t *data, size_t len) {
* Returns:
* The checksum of DATA
*/
static uint8_t checksum(uint8_t *data, size_t len) {
static uint8_t checksum(uint8_t* data, size_t len) {
uint8_t chk = data[0];
for (int i = 1; i < len; i++) {
chk ^= data[i];
Expand All @@ -80,13 +80,14 @@ static uint8_t checksum(uint8_t *data, size_t len) {
/**
* A macro to help with cobs_encode
*/
#define finish_block() {\
block[0] = (uint8_t) block_len; \
block = dst; \
dst++; \
dst_len++; \
block_len = 1; \
};
#define finish_block() \
{ \
block[0] = (uint8_t) block_len; \
block = dst; \
dst++; \
dst_len++; \
block_len = 1; \
};

/**
* Cobs encodes a byte array into a buffer
Expand All @@ -97,9 +98,9 @@ static uint8_t checksum(uint8_t *data, size_t len) {
* Returns:
* The size of the encoded data, DST
*/
static ssize_t cobs_encode(uint8_t *dst, const uint8_t *src, size_t src_len) {
const uint8_t *end = src + src_len;
uint8_t *block = dst; // Advancing pointer to start of each block
static ssize_t cobs_encode(uint8_t* dst, const uint8_t* src, size_t src_len) {
const uint8_t* end = src + src_len;
uint8_t* block = dst; // Advancing pointer to start of each block
dst++;
size_t block_len = 1;
ssize_t dst_len = 0;
Expand Down Expand Up @@ -142,9 +143,9 @@ static ssize_t cobs_encode(uint8_t *dst, const uint8_t *src, size_t src_len) {
* Returns:
* The size of the decoded data, DST
*/
static ssize_t cobs_decode(uint8_t *dst, const uint8_t *src, size_t src_len) {
static ssize_t cobs_decode(uint8_t* dst, const uint8_t* src, size_t src_len) {
// Pointer to end of source array
const uint8_t *end = src + src_len;
const uint8_t* end = src + src_len;
// Size counter of decoded array to return
ssize_t out_len = 0;

Expand All @@ -156,7 +157,7 @@ static ssize_t cobs_decode(uint8_t *dst, const uint8_t *src, size_t src_len) {
dst++;
src++;
out_len++;
if (src > end) { // Bad packet
if (src > end) { // Bad packet
return 0;
}
}
Expand All @@ -171,35 +172,35 @@ static ssize_t cobs_decode(uint8_t *dst, const uint8_t *src, size_t src_len) {

// ************************* MESSAGE CONSTRUCTORS *************************** //

message_t *make_empty(ssize_t payload_size) {
message_t *msg = malloc(sizeof(message_t));
message_t* make_empty(ssize_t payload_size) {
message_t* msg = malloc(sizeof(message_t));
msg->message_id = NOP;
msg->payload = malloc(payload_size);
msg->payload_length = 0;
msg->max_payload_length = payload_size;
return msg;
}

message_t *make_ping() {
message_t *ping = malloc(sizeof(message_t));
message_t* make_ping() {
message_t* ping = malloc(sizeof(message_t));
ping->message_id = PING;
ping->payload = NULL;
ping->payload_length = 0;
ping->max_payload_length = 0;
return ping;
}

message_t *make_subscription_request(uint8_t dev_type, uint32_t pmap, uint16_t interval) {
device_t *dev = get_device(dev_type);
message_t* make_subscription_request(uint8_t dev_type, uint32_t pmap, uint16_t interval) {
device_t* dev = get_device(dev_type);
// Don't read from non-existent params
pmap &= ((uint32_t) -1) >> (MAX_PARAMS - dev->num_params); // Set non-existent params to 0
pmap &= ((uint32_t) -1) >> (MAX_PARAMS - dev->num_params); // Set non-existent params to 0
// Set non-read-able params to 0
for (int i = 0; i < MAX_PARAMS; i++) {
if (dev->params[i].read == 0) {
pmap &= ~(1 << i); // Set bit i to 0
pmap &= ~(1 << i); // Set bit i to 0
}
}
message_t *sub_request = malloc(sizeof(message_t));
message_t* sub_request = malloc(sizeof(message_t));
sub_request->message_id = SUBSCRIPTION_REQUEST;
sub_request->payload = malloc(BITMAP_SIZE + INTERVAL_SIZE);
sub_request->payload_length = 0;
Expand All @@ -211,18 +212,18 @@ message_t *make_subscription_request(uint8_t dev_type, uint32_t pmap, uint16_t i
return (status == 0) ? sub_request : NULL;
}

message_t *make_device_write(uint8_t dev_type, uint32_t pmap, param_val_t param_values[]) {
device_t *dev = get_device(dev_type);
message_t* make_device_write(uint8_t dev_type, uint32_t pmap, param_val_t param_values[]) {
device_t* dev = get_device(dev_type);
// Don't write to non-existent params
pmap &= ((uint32_t) -1) >> (MAX_PARAMS - dev->num_params); // Set non-existent params to 0
pmap &= ((uint32_t) -1) >> (MAX_PARAMS - dev->num_params); // Set non-existent params to 0
// Set non-writeable params to 0
for (int i = 0; ((pmap >> i) > 0) && (i < MAX_PARAMS); i++) {
if (dev->params[i].write == 0) {
pmap &= ~(1 << i); // Set bit i to 0
pmap &= ~(1 << i); // Set bit i to 0
}
}
// Build the message
message_t *dev_write = malloc(sizeof(message_t));
message_t* dev_write = malloc(sizeof(message_t));
dev_write->message_id = DEVICE_WRITE;
dev_write->payload_length = 0;
dev_write->max_payload_length = device_write_payload_size(dev_type, pmap);
Expand Down Expand Up @@ -250,14 +251,14 @@ message_t *make_device_write(uint8_t dev_type, uint32_t pmap, param_val_t param_
return (status == 0) ? dev_write : NULL;
}

void destroy_message(message_t *message) {
void destroy_message(message_t* message) {
free(message->payload);
free(message);
}

// ********************* SERIALIZE AND PARSE MESSAGES *********************** //

size_t calc_max_cobs_msg_length(message_t *msg){
size_t calc_max_cobs_msg_length(message_t* msg) {
size_t required_packet_length = MESSAGE_ID_SIZE + PAYLOAD_LENGTH_SIZE + msg->payload_length + CHECKSUM_SIZE;
// Cobs encoding a length N message adds overhead of at most ceil(N/254)
size_t cobs_length = required_packet_length + (required_packet_length / 254) + 1;
Expand All @@ -267,13 +268,13 @@ size_t calc_max_cobs_msg_length(message_t *msg){
return DELIMITER_SIZE + COBS_LENGTH_SIZE + cobs_length;
}

ssize_t message_to_bytes(message_t *msg, uint8_t cobs_encoded[], size_t len) {
ssize_t message_to_bytes(message_t* msg, uint8_t cobs_encoded[], size_t len) {
size_t required_length = calc_max_cobs_msg_length(msg);
if (len < required_length) {
return -1;
}
// Build an intermediate byte array to hold the serialized message to be encoded
uint8_t *data = malloc(MESSAGE_ID_SIZE + PAYLOAD_LENGTH_SIZE + msg->payload_length + CHECKSUM_SIZE);
uint8_t* data = malloc(MESSAGE_ID_SIZE + PAYLOAD_LENGTH_SIZE + msg->payload_length + CHECKSUM_SIZE);
data[0] = msg->message_id;
data[1] = msg->payload_length;
for (int i = 0; i < msg->payload_length; i++) {
Expand All @@ -289,9 +290,9 @@ ssize_t message_to_bytes(message_t *msg, uint8_t cobs_encoded[], size_t len) {
return DELIMITER_SIZE + COBS_LENGTH_SIZE + cobs_len;
}

int parse_message(uint8_t data[], message_t *msg_to_fill) {
int parse_message(uint8_t data[], message_t* msg_to_fill) {
uint8_t cobs_len = data[1];
uint8_t *decoded = malloc(cobs_len); // Actual number of bytes populated will be a couple less due to overhead
uint8_t* decoded = malloc(cobs_len); // Actual number of bytes populated will be a couple less due to overhead
int ret = cobs_decode(decoded, &data[2], cobs_len);
if (ret < (MESSAGE_ID_SIZE + PAYLOAD_LENGTH_SIZE + CHECKSUM_SIZE)) {
// Smaller than valid message
Expand Down Expand Up @@ -319,13 +320,13 @@ int parse_message(uint8_t data[], message_t *msg_to_fill) {
return (expected_checksum != received_checksum) ? 1 : 0;
}

void parse_device_data(uint8_t dev_type, message_t *dev_data, param_val_t vals[]) {
device_t *dev = get_device(dev_type);
void parse_device_data(uint8_t dev_type, message_t* dev_data, param_val_t vals[]) {
device_t* dev = get_device(dev_type);
// Bitmap is stored in the first 32 bits of the payload
uint32_t bitmap = *((uint32_t*) dev_data->payload);
/* Iterate through device's parameters. If bit is off, continue
* If bit is on, determine how much to read from the payload then put it in VALS in the appropriate field */
uint8_t *payload_ptr = &(dev_data->payload[BITMAP_SIZE]); // Start the pointer at the beginning of the values (skip the bitmap)
uint8_t* payload_ptr = &(dev_data->payload[BITMAP_SIZE]); // Start the pointer at the beginning of the values (skip the bitmap)
for (int i = 0; ((bitmap >> i) > 0) && (i < MAX_PARAMS); i++) {
// If bit is on, parameter is included in the payload
if ((1 << i) & bitmap) {
Expand Down
46 changes: 23 additions & 23 deletions dev_handler/message.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#ifndef MESSAGE_H
#define MESSAGE_H

#include "../runtime_util/runtime_util.h"
#include "../logger/logger.h"
#include "../runtime_util/runtime_util.h"

/* The maximum number of milliseconds to wait between each PING from a device
* Waiting for this long will exit all threads for that device (doing cleanup as necessary) */
Expand All @@ -29,33 +29,33 @@
// The size in bytes of the section specifying the length of the payload in the message
#define PAYLOAD_LENGTH_SIZE 1
// The size in bytes of the param bitmap in SUBSCRIPTION_REQUEST, DEVICE_WRITE and DEVICE_DATA payloads
#define BITMAP_SIZE (MAX_PARAMS/8)
#define BITMAP_SIZE (MAX_PARAMS / 8)
// The size in bytes of the section specifying the interval for SUBSCRIPTION_REQUEST
#define INTERVAL_SIZE 2
// The size in bytes of the section specifying the device id for ACKNOWLEDGEMENT
#define DEVICE_ID_SIZE 10
// The size in bytes of the section specifying the checksum of the message id, the payload length, and the payload itself
#define CHECKSUM_SIZE 1
// The length of the largest payload in bytes, which may be reached for DEVICE_WRITE and DEVICE_DATA message types.
#define MAX_PAYLOAD_SIZE (BITMAP_SIZE+(MAX_PARAMS*sizeof(float))) // Bitmap + Each param (may be floats)
#define MAX_PAYLOAD_SIZE (BITMAP_SIZE + (MAX_PARAMS * sizeof(float))) // Bitmap + Each param (may be floats)

// The types of messages
typedef enum {
NOP = 0x00, // Dummy message
PING = 0x01, // To lowcar
ACKNOWLEDGEMENT = 0x02, // To dev handler
SUBSCRIPTION_REQUEST = 0x03, // To lowcar
DEVICE_WRITE = 0x04, // To lowcar
DEVICE_DATA = 0x05, // To dev handler
LOG = 0x06 // To dev handler
NOP = 0x00, // Dummy message
PING = 0x01, // To lowcar
ACKNOWLEDGEMENT = 0x02, // To dev handler
SUBSCRIPTION_REQUEST = 0x03, // To lowcar
DEVICE_WRITE = 0x04, // To lowcar
DEVICE_DATA = 0x05, // To dev handler
LOG = 0x06 // To dev handler
} message_id_t;

// A struct defining a message to be sent over serial
typedef struct {
message_id_t message_id;
uint8_t* payload; // Array of bytes
size_t payload_length; // The current number of bytes in payload
size_t max_payload_length; // The maximum length of the payload for the specific message_id
uint8_t* payload; // Array of bytes
size_t payload_length; // The current number of bytes in payload
size_t max_payload_length; // The maximum length of the payload for the specific message_id
} message_t;

// ******************************** Utility ********************************* //
Expand All @@ -66,7 +66,7 @@ typedef struct {
* data: Byte array to be printed
* len: The length of the data array
*/
void print_bytes(uint8_t *data, size_t len);
void print_bytes(uint8_t* data, size_t len);

// ************************* MESSAGE CONSTRUCTORS *************************** //
// Messages built from these constructors MUST be decalloated with destroy_message()
Expand All @@ -79,7 +79,7 @@ void print_bytes(uint8_t *data, size_t len);
* Returns:
* A message of type NOP, payload_length 0, and max_payload_length PAYLOAD_SIZE
*/
message_t *make_empty(ssize_t payload_size);
message_t* make_empty(ssize_t payload_size);

/**
* Builds a PING message
Expand All @@ -88,7 +88,7 @@ message_t *make_empty(ssize_t payload_size);
* payload_length 0
* max_payload_length 0
*/
message_t *make_ping();
message_t* make_ping();

/**
* Builds a SUBSCRIPTION_REQUEST
Expand All @@ -102,7 +102,7 @@ message_t *make_ping();
* payload_length: sizeof(pmap) + sizeof(interval)
* max_payload_length: same as above
*/
message_t *make_subscription_request(uint8_t dev_type, uint32_t pmap, uint16_t interval);
message_t* make_subscription_request(uint8_t dev_type, uint32_t pmap, uint16_t interval);

/**
* Builds a DEVICE_WRITE
Expand All @@ -117,14 +117,14 @@ message_t *make_subscription_request(uint8_t dev_type, uint32_t pmap, uint16_t i
* payload_length: sizeof(pmap) + sizeof(all the values in PARAM_VALUES)
* max_payload_length: same as above
*/
message_t *make_device_write(uint8_t dev_type, uint32_t pmap, param_val_t param_values[]);
message_t* make_device_write(uint8_t dev_type, uint32_t pmap, param_val_t param_values[]);

/**
* Frees the memory allocated for the message struct and its payload.
* Arguments:
* message: The message to have its memory deallocated.
*/
void destroy_message(message_t *message);
void destroy_message(message_t* message);

// ********************* SERIALIZE AND PARSE MESSAGES *********************** //

Expand All @@ -136,7 +136,7 @@ void destroy_message(message_t *message);
* Returns:
* The size of a byte array that should be allocated to serialize the message into
*/
size_t calc_max_cobs_msg_length(message_t *msg);
size_t calc_max_cobs_msg_length(message_t* msg);

/**
* Serializes then cobs encodes a message into a byte array
Expand All @@ -148,7 +148,7 @@ size_t calc_max_cobs_msg_length(message_t *msg);
* The size of COBS_ENCODED that was actually populated
* -1 if len is too small (less than calc_max_cobs_msg_length)
*/
ssize_t message_to_bytes(message_t *msg, uint8_t cobs_encoded[], size_t len);
ssize_t message_to_bytes(message_t* msg, uint8_t cobs_encoded[], size_t len);

/**
* Cobs decodes a byte array and populates the fields of input message
Expand All @@ -163,7 +163,7 @@ ssize_t message_to_bytes(message_t *msg, uint8_t cobs_encoded[], size_t len);
* 2 if max_payload_length is too small
* 3 if invalid message (decoded message has invalid length)
*/
int parse_message(uint8_t data[], message_t *empty_msg);
int parse_message(uint8_t data[], message_t* empty_msg);

/**
* Reads the parameter values from a DEVICE_DATA message into param_val_t[]
Expand All @@ -174,6 +174,6 @@ int parse_message(uint8_t data[], message_t *empty_msg);
* NOTE: The length of vals MUST be at LEAST the number of params sent in the DEVICE_DATA message
* Allocate MAX_PARAMS param_val_t structs to guarantee this
*/
void parse_device_data(uint8_t dev_type, message_t *dev_data, param_val_t vals[]);
void parse_device_data(uint8_t dev_type, message_t* dev_data, param_val_t vals[]);

#endif
Loading

0 comments on commit 02130c0

Please sign in to comment.