|
| 1 | +#include "packet_handler.h" |
| 2 | +#include "serial_linux.h" |
| 3 | +#include <assert.h> |
| 4 | +#include <stdint.h> |
| 5 | +#include <stdio.h> |
| 6 | +#include <stdlib.h> |
| 7 | +#include <string.h> |
| 8 | +#include <unistd.h> |
| 9 | + |
| 10 | +#define STR_LENGTH 16 |
| 11 | +char inputString[STR_LENGTH]; |
| 12 | + |
| 13 | +PacketHandler packet_handler; |
| 14 | + |
| 15 | +/* -------------------- TimerConfigPacket -------------------- */ |
| 16 | + |
| 17 | +#pragma pack(push, 1) |
| 18 | +typedef struct TimerConfigPacket{ |
| 19 | + PacketHeader header; |
| 20 | + uint32_t duration; |
| 21 | +} TimerConfigPacket; |
| 22 | +#pragma pack(pop) |
| 23 | + |
| 24 | +#define TIMER_CONFIG_PACKET_TYPE 0 |
| 25 | +#define TIMER_CONFIG_PACKET_SIZE (sizeof(TimerConfigPacket)) |
| 26 | + |
| 27 | +TimerConfigPacket timer_config_packet_buffer; |
| 28 | + |
| 29 | +PacketHeader* TimerConfigPacket_initializeBuffer(PacketType type, PacketSize size, void* args __attribute__((unused))) { |
| 30 | + if (type != TIMER_CONFIG_PACKET_TYPE || size != TIMER_CONFIG_PACKET_SIZE) |
| 31 | + return 0; |
| 32 | + return (PacketHeader*) &timer_config_packet_buffer; |
| 33 | +} |
| 34 | + |
| 35 | +PacketOperations TimerConfigPacket_ops = { |
| 36 | + TIMER_CONFIG_PACKET_TYPE, |
| 37 | + sizeof(TimerConfigPacket), |
| 38 | + TimerConfigPacket_initializeBuffer, |
| 39 | + 0, |
| 40 | + 0, |
| 41 | + 0 |
| 42 | +}; |
| 43 | + |
| 44 | +/* -------------------- LightConfigPacket -------------------- */ |
| 45 | + |
| 46 | +#pragma pack(push, 1) |
| 47 | +typedef struct LightConfigPacket{ |
| 48 | + PacketHeader header; |
| 49 | + uint16_t minLight; |
| 50 | + uint16_t maxLight; |
| 51 | +} LightConfigPacket; |
| 52 | +#pragma pack(pop) |
| 53 | + |
| 54 | +#define LIGHT_CONFIG_PACKET_TYPE 1 |
| 55 | +#define LIGHT_CONFIG_PACKET_SIZE (sizeof(LightConfigPacket)) |
| 56 | + |
| 57 | +LightConfigPacket light_config_packet_buffer; |
| 58 | + |
| 59 | +PacketHeader* LightConfigPacket_initializeBuffer(PacketType type, PacketSize size, void* args __attribute__((unused))) { |
| 60 | + if (type != LIGHT_CONFIG_PACKET_TYPE || size != LIGHT_CONFIG_PACKET_SIZE) |
| 61 | + return 0; |
| 62 | + return (PacketHeader*) &light_config_packet_buffer; |
| 63 | +} |
| 64 | + |
| 65 | +PacketOperations LightConfigPacket_ops = { |
| 66 | + LIGHT_CONFIG_PACKET_TYPE, |
| 67 | + sizeof(LightConfigPacket), |
| 68 | + LightConfigPacket_initializeBuffer, |
| 69 | + 0, |
| 70 | + 0, |
| 71 | + 0 |
| 72 | +}; |
| 73 | + |
| 74 | +/* -------------------- TemperatureConfigPacket -------------------- */ |
| 75 | + |
| 76 | +#pragma pack(push, 1) |
| 77 | +typedef struct TemperatureConfigPacket{ |
| 78 | + PacketHeader header; |
| 79 | + uint16_t minTemp; |
| 80 | + uint16_t maxTemp; |
| 81 | + float r1; |
| 82 | + float c1; |
| 83 | + float c2; |
| 84 | + float c3; |
| 85 | +} TemperatureConfigPacket; |
| 86 | +#pragma pack(pop) |
| 87 | + |
| 88 | +#define TEMPERATURE_CONFIG_PACKET_TYPE 2 |
| 89 | +#define TEMPERATURE_CONFIG_PACKET_SIZE (sizeof(TemperatureConfigPacket)) |
| 90 | + |
| 91 | +TemperatureConfigPacket temperature_config_packet_buffer; |
| 92 | + |
| 93 | +PacketHeader* TemperatureConfigPacket_initializeBuffer(PacketType type, PacketSize size, void* args __attribute__((unused))) { |
| 94 | + if (type != TEMPERATURE_CONFIG_PACKET_TYPE || size != TEMPERATURE_CONFIG_PACKET_SIZE) |
| 95 | + return 0; |
| 96 | + return (PacketHeader*) &temperature_config_packet_buffer; |
| 97 | +} |
| 98 | + |
| 99 | +PacketOperations TemperatureConfigPacket_ops = { |
| 100 | + TEMPERATURE_CONFIG_PACKET_TYPE, |
| 101 | + sizeof(TemperatureConfigPacket), |
| 102 | + TemperatureConfigPacket_initializeBuffer, |
| 103 | + 0, |
| 104 | + 0, |
| 105 | + 0 |
| 106 | +}; |
| 107 | + |
| 108 | +/* -------------------- PollutionConfigPacket -------------------- */ |
| 109 | + |
| 110 | +#pragma pack(push, 1) |
| 111 | +typedef struct PollutionConfigPacket{ |
| 112 | + PacketHeader header; |
| 113 | + uint16_t maxPoll; |
| 114 | +} PollutionConfigPacket; |
| 115 | +#pragma pack(pop) |
| 116 | + |
| 117 | +#define POLLUTION_CONFIG_PACKET_TYPE 3 |
| 118 | +#define POLLUTION_CONFIG_PACKET_SIZE (sizeof(PollutionConfigPacket)) |
| 119 | + |
| 120 | +PollutionConfigPacket pollution_config_packet_buffer; |
| 121 | + |
| 122 | +PacketHeader* PollutionConfigPacket_initializeBuffer(PacketType type, PacketSize size, void* args __attribute__((unused))) { |
| 123 | + if (type != POLLUTION_CONFIG_PACKET_TYPE || size != POLLUTION_CONFIG_PACKET_SIZE) |
| 124 | + return 0; |
| 125 | + return (PacketHeader*) &pollution_config_packet_buffer; |
| 126 | +} |
| 127 | + |
| 128 | +PacketOperations PollutionConfigPacket_ops = { |
| 129 | + POLLUTION_CONFIG_PACKET_TYPE, |
| 130 | + sizeof(PollutionConfigPacket), |
| 131 | + PollutionConfigPacket_initializeBuffer, |
| 132 | + 0, |
| 133 | + 0, |
| 134 | + 0 |
| 135 | +}; |
| 136 | + |
| 137 | +void read_from_stdin(void){ |
| 138 | + fgets(inputString, STR_LENGTH, stdin); |
| 139 | + if((strlen(inputString) > 0) && (inputString[strlen(inputString) - 1] == '\n')) |
| 140 | + inputString[strlen(inputString) - 1] = '\0'; |
| 141 | +} |
| 142 | + |
| 143 | +struct UART* uart; |
| 144 | + |
| 145 | +void flushOutputBuffer(int fd){ |
| 146 | + while (packet_handler.tx_size){ |
| 147 | + uint8_t c = PacketHandler_txByte(&packet_handler); |
| 148 | + write(fd, &c, 1); |
| 149 | + usleep(1000); |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +int main(int argc, char** argv){ |
| 154 | + assert(argc > 1); |
| 155 | + int fd = serial_open(argv[1]); |
| 156 | + if (fd < 0) |
| 157 | + return 0; |
| 158 | + if (serial_set_interface_attribs(fd, 115200, 0) < 0) |
| 159 | + return 0; |
| 160 | + serial_set_blocking(fd, 1); |
| 161 | + if (! fd) |
| 162 | + return 0; |
| 163 | + |
| 164 | + PacketHandler_initialize(&packet_handler); |
| 165 | + PacketHandler_installPacket(&packet_handler, &TimerConfigPacket_ops); |
| 166 | + PacketHandler_installPacket(&packet_handler, &LightConfigPacket_ops); |
| 167 | + PacketHandler_installPacket(&packet_handler, &TemperatureConfigPacket_ops); |
| 168 | + PacketHandler_installPacket(&packet_handler, &PollutionConfigPacket_ops); |
| 169 | + |
| 170 | + /* -------------------- TimerConfigPacket -------------------- */ |
| 171 | + |
| 172 | + printf("Insert timer duration: "); |
| 173 | + read_from_stdin(); |
| 174 | + uint32_t duration = atoi(inputString); |
| 175 | + |
| 176 | + TimerConfigPacket p0 = { {TIMER_CONFIG_PACKET_TYPE, TIMER_CONFIG_PACKET_SIZE, 0}, duration }; |
| 177 | + PacketHandler_sendPacket(&packet_handler, (PacketHeader*) &p0); |
| 178 | + flushOutputBuffer(fd); |
| 179 | + |
| 180 | + /* -------------------- LightConfigPacket -------------------- */ |
| 181 | + |
| 182 | + printf("Insert min light: "); |
| 183 | + read_from_stdin(); |
| 184 | + uint16_t minLight = atoi(inputString); |
| 185 | + |
| 186 | + printf("Insert max light: "); |
| 187 | + read_from_stdin(); |
| 188 | + uint16_t maxLight = atoi(inputString); |
| 189 | + |
| 190 | + LightConfigPacket p1 = { {LIGHT_CONFIG_PACKET_TYPE, LIGHT_CONFIG_PACKET_SIZE, 0}, minLight, maxLight }; |
| 191 | + PacketHandler_sendPacket(&packet_handler, (PacketHeader*) &p1); |
| 192 | + flushOutputBuffer(fd); |
| 193 | + |
| 194 | + /* -------------------- TemperatureConfigPacket -------------------- */ |
| 195 | + |
| 196 | + printf("Insert min temperature: "); |
| 197 | + read_from_stdin(); |
| 198 | + uint16_t minTemp = atoi(inputString); |
| 199 | + |
| 200 | + printf("Insert max temperature: "); |
| 201 | + read_from_stdin(); |
| 202 | + uint16_t maxTemp = atoi(inputString); |
| 203 | + |
| 204 | + printf("Insert thermistor resistance: "); |
| 205 | + read_from_stdin(); |
| 206 | + float r1 = atof(inputString); |
| 207 | + |
| 208 | + float c1 = 1.009249522e-03; |
| 209 | + float c2 = 2.378405444e-04; |
| 210 | + float c3 = 2.019202697e-07; |
| 211 | + |
| 212 | + TemperatureConfigPacket p2 = { {TEMPERATURE_CONFIG_PACKET_TYPE, TEMPERATURE_CONFIG_PACKET_SIZE, 0}, minTemp, maxTemp, r1, c1, c2, c3 }; |
| 213 | + PacketHandler_sendPacket(&packet_handler, (PacketHeader*) &p2); |
| 214 | + flushOutputBuffer(fd); |
| 215 | + |
| 216 | + /* -------------------- PollutionConfigPacket -------------------- */ |
| 217 | + |
| 218 | + printf("Insert max pollution: "); |
| 219 | + read_from_stdin(); |
| 220 | + uint16_t maxPoll = atoi(inputString); |
| 221 | + |
| 222 | + PollutionConfigPacket p3 = { {POLLUTION_CONFIG_PACKET_TYPE, POLLUTION_CONFIG_PACKET_SIZE, 0}, maxPoll }; |
| 223 | + PacketHandler_sendPacket(&packet_handler, (PacketHeader*) &p3); |
| 224 | + flushOutputBuffer(fd); |
| 225 | + |
| 226 | + return 0; |
| 227 | +} |
0 commit comments