Skip to content

Commit

Permalink
adding new function
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertLRead committed Feb 13, 2021
1 parent 9e4b597 commit 979505e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
13 changes: 13 additions & 0 deletions pirds_library/PIRDS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,16 @@ Message get_message_from_JSON(char* buff,uint16_t blim) {
assign_value_message(&m,k,v);
return m;
}

char get_event_designation_char_from_json(char* buff,uint16_t blim) {
char *k = strtok(buff , "{,:}");
char *v = strtok(NULL, "{,:}");
char *stripped_key = trimwhitespace(k);
char *stripped_value = trimwhitespace(v);
if (0 == strcmp(stripped_key,"\"event\"")) {
return stripped_value[1];
} else {
return '\0'; // same as zero, an error condition
}

}
7 changes: 7 additions & 0 deletions pirds_library/PIRDS.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ typedef struct Message
#define FLOW_TOO_LOW "FLOW OUT OF RANGE LOW"
#define SAVE_LOG_TO_FILE "SAVE_LOG_TO_FILE:"


// This function returns the character in the "event" tag of the JSON object,
// or null/0 if there is none. This function can be used to determine if
// a JSON string is a measurement ('M') or a message ('E'). This then
// allows the proper interpretation function below to be called.
char get_event_designation_char_from_json(char* buff,uint16_t blim);

/* Fill the byte buffer with a PIRDS-standard bytes from the
Measurement Object */
uint16_t fill_byte_buffer_measurement(Measurement* m,uint8_t* buff,uint16_t blim);
Expand Down
26 changes: 26 additions & 0 deletions pirds_library/test_PIRDS.c.ext
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,38 @@ static char *test_can_create_Message_and_read_back_as_JSON() {
}


static char *test_get_event_designation() {
Message me = {
'E','M',4000,18,"Buckminster Fuller"
};
Measurement ms = {
'M','T',101,'B',3,-10115
};

const uint16_t BUFF_SIZE = 256+7;
char buff[BUFF_SIZE];
int err;
err = fill_JSON_buffer_message(&me,buff,BUFF_SIZE);
mu_assert("buffer problem", err > 0);
mu_assert("failed to get Message as E", 'E' == get_event_designation_char_from_json(buff,BUFF_SIZE));

err = fill_JSON_buffer_measurement(&ms,buff,BUFF_SIZE);
mu_assert("failed to get Measurement as M", 'M' == get_event_designation_char_from_json(buff,BUFF_SIZE));

// NOTE: Intentionally misspelled!
sprintf(buff,"{ exent: X}");
mu_assert("failed to return null when no event",0 == get_event_designation_char_from_json(buff,BUFF_SIZE));

return 0;
}

static char * all_tests() {
mu_run_test(test_can_create_Measurement_and_read_back_as_byte_buffer);
mu_run_test(test_can_create_Measurement_and_read_back_as_JSON);

mu_run_test(test_can_create_Message_and_read_back_as_byte_buffer);
mu_run_test(test_can_create_Message_and_read_back_as_JSON);
mu_run_test(test_get_event_designation);

return 0;
}
Expand Down

0 comments on commit 979505e

Please sign in to comment.