send udp
This commit is contained in:
@ -3,6 +3,8 @@
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <sinkStruct.h>
|
||||
|
||||
|
||||
typedef struct {
|
||||
uint64_t seconds;
|
||||
@ -12,5 +14,7 @@ typedef struct {
|
||||
|
||||
void networkInit();
|
||||
t_seconds* networkGetSeconds();
|
||||
int8_t networkSendMinuteBuffer(t_minuteBuffer *minuteBuffer);
|
||||
|
||||
|
||||
#endif /* _NETWORK_ABSTRACTION_LAYER_H_ */
|
||||
|
@ -7,7 +7,6 @@
|
||||
#include <show.h>
|
||||
#include <logger.h>
|
||||
#include <PontCoopScheduler.h>
|
||||
#include <wizHelper.h>
|
||||
#include <config.h>
|
||||
#include <socket.h>
|
||||
#include <sinkStruct.h>
|
||||
@ -38,33 +37,6 @@ uint8_t activeMinuteBuffer;
|
||||
|
||||
static t_configBlock *config;
|
||||
|
||||
extern uint8_t SINK_SOCK;
|
||||
const uint16_t SINK_PORT = 20169;
|
||||
|
||||
int8_t counterSendMinuteBuffer(t_minuteBuffer *minuteBuffer) {
|
||||
uint8_t sinkAddr[4];
|
||||
if (! wizDnsQuery(config->sinkServer, sinkAddr)) {
|
||||
coloredMsg(LOG_BLUE, "csmb, failed to resolve sink server name");
|
||||
return -1;
|
||||
} else {
|
||||
coloredMsg(LOG_BLUE, "csmb, 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, minuteBuffer->b, sizeof(minuteBuffer->b), sinkAddr, SINK_PORT);
|
||||
coloredMsg(LOG_BLUE, "csmb, sent");
|
||||
} else {
|
||||
coloredMsg(LOG_BLUE, "csmb, socket in unexpected state: %d", sockState);
|
||||
return -2;
|
||||
}
|
||||
|
||||
close(SINK_SOCK);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void counterMinuteTick(void *handle) {
|
||||
for (uint8_t minuteBufferIdx = 0; minuteBufferIdx < NUM_OF_MINUTE_BUFFERS; minuteBufferIdx++) {
|
||||
@ -122,7 +94,7 @@ void counterMinuteTick(void *handle) {
|
||||
minuteBuffer->s.hash[31]
|
||||
);
|
||||
|
||||
int8_t res = counterSendMinuteBuffer(minuteBuffer);
|
||||
int8_t res = networkSendMinuteBuffer(minuteBuffer);
|
||||
if (res == 1) {
|
||||
coloredMsg(LOG_BLUE, "cmt, successfully sent");
|
||||
minuteBufferReady[minuteBufferIdx] = false;
|
||||
|
@ -2,11 +2,15 @@
|
||||
#include <PontCoopScheduler.h>
|
||||
#include <iwdg.h>
|
||||
#include <logger.h>
|
||||
#include <sinkStruct.h>
|
||||
#include <config.h>
|
||||
|
||||
#include <wizHelper.h>
|
||||
#include <wizchip_conf.h>
|
||||
|
||||
|
||||
static t_seconds seconds = { .seconds = 0, .missedUpdates = 0, .valid = false };
|
||||
static t_configBlock *config;
|
||||
|
||||
|
||||
static void networkSecondsHandler(void *handle) {
|
||||
@ -54,7 +58,42 @@ t_seconds* networkGetSeconds() {
|
||||
}
|
||||
|
||||
|
||||
extern uint8_t SINK_SOCK;
|
||||
const uint16_t SINK_PORT = 20169;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
int8_t networkSendMinuteBuffer(t_minuteBuffer *minuteBuffer) {
|
||||
return networkUdpSend(config->sinkServer, SINK_PORT, minuteBuffer->b, sizeof(minuteBuffer->b));
|
||||
}
|
||||
|
||||
void networkInit() {
|
||||
static t_configBlock *config;
|
||||
|
||||
wizInit();
|
||||
|
||||
schAdd(networkSecondsHandler, NULL, 0, 1000);
|
||||
}
|
||||
|
@ -6,6 +6,5 @@
|
||||
|
||||
const uint8_t DHCP_SOCK = 0;
|
||||
const uint8_t SNTP_SOCK = 1;
|
||||
const uint8_t CMD_SOCK = 2;
|
||||
const uint8_t DNS_SOCK = 3;
|
||||
const uint8_t SINK_SOCK = 4;
|
||||
const uint8_t DNS_SOCK = 2;
|
||||
const uint8_t SINK_SOCK = 3;
|
||||
|
Reference in New Issue
Block a user