Skip to content

Commit

Permalink
udp: update protocol format
Browse files Browse the repository at this point in the history
Signed-off-by: David Bauer <[email protected]>
  • Loading branch information
blocktrron committed Jul 23, 2024
1 parent 3abc7c1 commit 1874ece
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@ start_service() {
local port="$(uci -q get ffda-oob-state-reporter.core.port)"
local interval="$(uci -q get ffda-oob-state-reporter.core.interval)"
local log_level="$(uci -q get ffda-oob-state-reporter.core.log_level)"
local reporter_id="$(uci -q get ffda-oob-state-reporter.core.reporter_id)"

[ -z "$interval" ] && interval=600
[ -z "$port" ] && port=1234
[ -z "$host" ] && return

[ -z "$log_level" ] && log_level=3
[ -z "$reporter_id" ] && return

[ "$enabled" -gt 0 ] || return

procd_open_instance
procd_set_param command /sbin/ffda-oob-state-reporter -s -i "$interval" -h "$host" -p "$port" -l "$log_level"
procd_set_param command /sbin/ffda-oob-state-reporter -s -i "$interval" -h "$host" -p "$port" -l "$log_level" -r "$reporter_id"
procd_set_param respawn 60 5 5
procd_close_instance
}
Expand Down
18 changes: 17 additions & 1 deletion src/reporter.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@ static void fosr_work(struct uloop_timeout *timeout)

static int fosr_parse_config(struct fosr *fosr, int argc, char *argv[])
{
int reporter_id = 0;
int c;

while ((c = getopt (argc, argv, "h:i:l:p:s")) != -1) {
while ((c = getopt (argc, argv, "h:i:l:p:r:s")) != -1) {
switch (c){
case 'h':
fosr->config.host = optarg;
Expand All @@ -97,6 +98,9 @@ static int fosr_parse_config(struct fosr *fosr, int argc, char *argv[])
case 'p':
fosr->config.port = atoi(optarg);
break;
case 'r':
reporter_id = atoi(optarg);
break;
case 's':
fosr_log_set_syslog(1);
break;
Expand Down Expand Up @@ -127,6 +131,18 @@ static int fosr_parse_config(struct fosr *fosr, int argc, char *argv[])
return 1;
}

if (!reporter_id) {
MSG(ERROR, "Missing reporter id\n");
return 1;
}

if (reporter_id < 0 || reporter_id > 65535) {
MSG(ERROR, "Invalid reporter id. Minimum: 0, Maximum: 65535\n");
return 1;
}

fosr->config.reporter_id = reporter_id;

return 0;
}

Expand Down
2 changes: 2 additions & 0 deletions src/reporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ struct fosr_config {
const char *host;
uint16_t port;
int32_t interval;

uint16_t reporter_id;
};

enum fosr_submission_reason {
Expand Down
17 changes: 13 additions & 4 deletions src/udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ static int fosr_udp_submit_getaddr(int family, const char *host, uint16_t port,

int fosr_udp_submit(struct fosr *fosr, struct fosr_metrics *metrics)
{
uint8_t submission_bufffer[3];
uint8_t submission_bufffer[6];
struct addrinfo *res = NULL;
uint16_t reporter_id;
int ret = 0;
int fd = -1;

Expand All @@ -46,9 +47,17 @@ int fosr_udp_submit(struct fosr *fosr, struct fosr_metrics *metrics)
goto out_free;
}

submission_bufffer[0] = (uint8_t)metrics->soc;
submission_bufffer[1] = metrics->charging ? 0xff : 0x00;
submission_bufffer[2] = (uint8_t)metrics->temperature;
/* Protocol version */
submission_bufffer[0] = 1;

/* Reporter ID */
reporter_id = htons(fosr->config.reporter_id);
memcpy(&submission_bufffer[1], &reporter_id, sizeof(fosr->config.reporter_id));

/* Metrics */
submission_bufffer[3] = (uint8_t)metrics->soc;
submission_bufffer[4] = metrics->charging ? 0xff : 0x00;
submission_bufffer[5] = (uint8_t)metrics->temperature;

if (sendto(fd, submission_bufffer, sizeof(submission_bufffer), 0, res->ai_addr, res->ai_addrlen) < 0) {
/* Don't be spammy, we have retry */
Expand Down

0 comments on commit 1874ece

Please sign in to comment.