diff --git a/cube/User/Inc/sinkStruct.h b/cube/User/Inc/sinkStruct.h index 85153d7..881ba6f 100644 --- a/cube/User/Inc/sinkStruct.h +++ b/cube/User/Inc/sinkStruct.h @@ -19,4 +19,9 @@ typedef struct __attribute__((__packed__)) { t_event events[SECONDS_PER_MINUTE]; } t_minuteStruct; +typedef union { + t_minuteStruct s; + uint8_t b[sizeof(t_minuteStruct)]; +} t_minuteBuffer; + #endif // _SINKSTRUCT_H_ \ No newline at end of file diff --git a/cube/User/Src/counter.c b/cube/User/Src/counter.c index 4199dd6..66ba3b5 100644 --- a/cube/User/Src/counter.c +++ b/cube/User/Src/counter.c @@ -27,10 +27,6 @@ volatile static uint32_t mainsCntCnt = 0; static t_seconds *seconds; -typedef union { - t_minuteStruct s; - uint8_t b[sizeof(t_minuteStruct)]; -} t_minuteBuffer; #define NUM_OF_MINUTE_BUFFERS 2 t_minuteBuffer minuteBuffers[NUM_OF_MINUTE_BUFFERS]; diff --git a/sink/sink20169.c b/sink/sink20169.c index df20bd4..2af320e 100644 --- a/sink/sink20169.c +++ b/sink/sink20169.c @@ -4,6 +4,20 @@ #include #include +#include + + +typedef struct { + char deviceId[sizeof(((t_configBlock*)0)->deviceId)]; + char sharedSecret[sizeof(((t_configBlock*)0)->sharedSecret)]; +} t_device; + +const t_device devices[] = { + { .deviceId = "MainsCnt01", .sharedSecret = "sharedSecretGanzGeheim" }, + { .deviceId = NULL, .sharedSecret = NULL } +}; + + int main() { int sockfd; @@ -19,13 +33,52 @@ int main() { bind(sockfd, (const struct sockaddr *) &servaddr, sizeof(servaddr)); - uint8_t buf[800]; + t_minuteBuffer buf; while (1) { - int n = recvfrom(sockfd, buf, sizeof(buf), MSG_TRUNC, + int n = recvfrom(sockfd, buf.b, sizeof(buf.b), MSG_TRUNC, (struct sockaddr *) &cliaddr, &cliaddrlen); printf("received %d octets from %04x\n", n, cliaddr.sin_addr.s_addr); + + if (n != sizeof(buf.b)) { + printf("Illegal packet size: %d\n", n); + continue; + } + + uint8_t i = 0; + while (1) { + if (strncmp(devices[i].deviceId, buf.s.deviceId, sizeof(((t_configBlock*)0)->deviceId)) == 0) { + printf("Device found: %s\n", devices[i].deviceId); + + uint8_t receivedHash[SHA256_BLOCK_SIZE]; + memcpy(receivedHash, buf.s.hash, SHA256_BLOCK_SIZE); + memcpy(buf.s.hash, devices[i].sharedSecret, SHA256_BLOCK_SIZE); + + SHA256_CTX ctx; + uint8_t calculatedHash[SHA256_BLOCK_SIZE]; + sha256_init(&ctx); + sha256_update(&ctx, buf.b, sizeof(buf.b)); + sha256_final(&ctx, calculatedHash); + + if (memcmp(receivedHash, calculatedHash, SHA256_BLOCK_SIZE) != 0) { + printf("Invalid hash\n"); + } + + printf("DeviceId: %s\n", buf.s.deviceId); + printf("Location: %s\n", buf.s.location); + for (uint8_t j = 0; j < SECONDS_PER_MINUTE; j++) { + printf("Time: %llu, Frequency: %lu\n", buf.s.events[j].timestamp, buf.s.events[j].frequency); + } + printf("\n"); + + + break; + } else { + printf("Unknown device\n"); + } + } + } }