This repository has been archived by the owner on Oct 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialize.h
147 lines (132 loc) · 5.04 KB
/
serialize.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#ifndef SERIALIZE_H_
#define SERIALIZE_H_
#include <stdbool.h>
#include <stdint.h>
#include "error.h"
#include "message_types.h"
/*
* This macro defines how long (in bytes) a string must be in order to
* hold a serialized system_state. It includes the null terminator, so
* a serialized system state should have SERIALIZED_OUTPUT_LEN - 1 ascii
* characters in it
*/
#define SERIALIZED_OUTPUT_LEN 9
/*
* Length of a state command. A state command is a block of characters
* that can be sent over the radio. It's mostly the serialized state output
* with a couple of additional bytes for STATE_COMMAND header and for a
* CRC or hamming code
*/
#define STATE_COMMAND_LEN (SERIALIZED_OUTPUT_LEN + 2)
/*
* This character indicates the beginning of a state command.
*/
#define STATE_COMMAND_HEADER '{'
/*
* This character means "hey radio board, send your current state"
*/
#define STATE_REQUEST_HEADER '}'
/*
* This character indicates the beginning of a error message
*/
#define ERROR_COMMAND_HEADER '!'
/*
* This type contains all of the information that needs to be shared between
* the operator on the ground and the CAN system in the rocket.
*/
typedef struct {
uint16_t tank_pressure;
uint8_t num_boards_connected;
enum VALVE_STATE injector_valve_state;
enum VALVE_STATE vent_valve_state;
bool bus_is_powered;
bool any_errors_detected;
uint16_t bus_battery_voltage_mv;
uint16_t vent_battery_voltage_mv;
} system_state;
/*
* This function converts a binary value from 0 to 63 inclusive into a
* printable charcter using a modified version of Base64. The + character is
* not used, and is replaced by the & character, due to the XBEE interpreting
* the + character as a special character.
*/
char binary_to_base64(uint8_t binary);
/*
* This function converts a value from a modified version of Base64 into a raw
* binary value. See the description of the function binary_to_base64() for a
* description of the modified Base64 encoding and the reason for its use.
*/
uint8_t base64_to_binary(char base64);
/*
* This function takes a system_state and serializes it into ASCII text that
* can be sent over the radio. It will return true if it was able to
* successfully serialize the state, false otherwise. Examples of how it can
* return false is passing it a NULL pointer as either argument
*/
bool serialize_state(const system_state *state, char *str);
/*
* This function takes a string which was generated by the serialize_state
* function and converts it back into a system_state. Returns true if it
* was successfully able to deserialize a state, false otherwise
*/
bool deserialize_state(system_state *state, const char *str);
/*
* This function serializes an error message for transmission over radio.
* Returns true if it successfully serialized a state and false otherwise.
*/
bool serialize_error(const error_t *err, char *str);
/*
*
*/
bool deserialize_error(error_t *err, const char *str);
#define GPS_MSG_LEN 11
#define GPS_MSG_HEADER '$'
/*
* Packs the latitude and longitude into str. str must be a buffer
* at least GPS_MSG_LEN bytes long. Returns true on success.
*
* Example of what it might put in str: "$IBIyah4vgY"
* Note that this function does not null terminate str
*/
bool create_gps_message(uint8_t latitude_deg,
uint8_t latitude_min,
uint8_t latitude_dmin,
uint8_t latitude_dir,
uint8_t longitude_deg,
uint8_t longitude_min,
uint8_t longitude_dmin,
uint8_t longitude_dir,
char *str);
/*
* Unpacks str into latitude and longitude values. str must be a buffer
* at least GPS_MSG_LEN bytes long. Returns true on success.
*/
bool expand_gps_message(uint8_t *latitude_deg,
uint8_t *latitude_min,
uint8_t *latitude_dmin,
uint8_t *latitude_dir,
uint8_t *longitude_deg,
uint8_t *longitude_min,
uint8_t *longitude_dmin,
uint8_t *longitude_dir,
char *str);
/*
* Returns true if the two system states passed to it are equal (returns
* false if either of them are NULL). Note that in C you're not just allowed
* to do (*s == *p), you have to individually compare each element in the
* struct, due to data representation reasons
*/
bool compare_system_states(const system_state *s, const system_state *p);
/*
* This function creates a state command that can be sent over the radio
* byte by byte. Returns false if it couldn't do so, for some reason
*/
bool create_state_command(char *cmd, const system_state *state);
/*
* This function computes the checksum of a NULL-terminated message using a
* modified version of the Luhn algorithm. The checksum is equal to the sum of
* the odd-placed digits plus three times the sum of the even-placed digits,
* modulo 64. The function returns the Base-64 encoding of the checksum.
*/
char checksum(char *cmd);
#endif