This commit is contained in:
2021-02-16 11:44:51 +01:00
parent 5b7248a8a5
commit d29529bf8a
4 changed files with 58 additions and 54 deletions

View File

@ -5,18 +5,7 @@
#include <sinkStruct.h>
#include <config.h>
#define NETWORK_LAN 1
#define NETWORK_WiFi 2
#define NETWORK_GSM 3
#if NETWORK == NETWORK_LAN
#include <wizHelper.h>
#include <wizchip_conf.h>
#include <socket.h>
#endif
#include <networkAbstractionLayer_impl.h>
const uint16_t SINK_PORT = 20169;
@ -25,12 +14,6 @@ static t_seconds seconds = { .seconds = 0, .missedUpdates = 0, .valid = false };
static t_configBlock *config;
#if NETWORK == NETWORK_LAN
static uint64_t networkSntpQuery() {
return wizSntpQuery();
}
#endif
static void networkSecondsHandler(void *handle) {
static bool tryAgain = false;
@ -75,46 +58,13 @@ t_seconds* networkGetSeconds() {
return &seconds;
}
#if NETWORK == NETWORK_LAN
extern uint8_t SINK_SOCK;
static int8_t networkUdpSend(char *hostname, uint16_t port, uint8_t *buf, uint16_t bufLen) {
uint8_t sinkAddr[4];
if (! wizDnsQuery(hostname, sinkAddr)) {
coloredMsg(LOG_BLUE, "nus, failed to resolve sink server name");
return -1;
} else {
coloredMsg(LOG_BLUE, "nus, sink server at %d.%d.%d.%d", sinkAddr[0], sinkAddr[1], sinkAddr[2], sinkAddr[3]);
}
socket(SINK_SOCK, Sn_MR_UDP, SINK_PORT, 0);
uint8_t sockState = getSn_SR(SINK_SOCK);
if (sockState == SOCK_UDP) {
sendto(SINK_SOCK, buf, bufLen, sinkAddr, SINK_PORT);
coloredMsg(LOG_BLUE, "nus, sent");
} else {
coloredMsg(LOG_BLUE, "nus, socket in unexpected state: %d", sockState);
return -2;
}
close(SINK_SOCK);
return 1;
}
#endif
int8_t networkSendMinuteBuffer(t_minuteBuffer *minuteBuffer) {
return networkUdpSend(config->sinkServer, SINK_PORT, minuteBuffer->b, sizeof(minuteBuffer->b));
}
void networkInit() {
#if NETWORK == NETWORK_LAN
wizInit();
#endif
networkImplInit();
config = getConfig();
schAdd(networkSecondsHandler, NULL, 0, 1000);
}