Add support for device grouping

Introduce new '-g/--group' option to specify device group.

The changes allow organizing devices into groups by adding a new group
option that gets included in the registration message.

Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
This commit is contained in:
Jianhui Zhao
2025-07-04 11:38:21 +08:00
parent 136e03aa33
commit 935a7589c2
5 changed files with 21 additions and 6 deletions

View File

@@ -64,6 +64,7 @@ static void signal_cb(struct ev_loop *loop, ev_signal *w, int revents)
}
static struct option long_options[] = {
{"group", required_argument, NULL, 'g'},
{"id", required_argument, NULL, 'I'},
{"host", required_argument, NULL, 'h'},
{"port", required_argument, NULL, 'p'},
@@ -84,6 +85,8 @@ static struct option long_options[] = {
static void usage(const char *prog)
{
fprintf(stderr, "Usage: %s [option]\n"
" -g, --group=string Set a group for the device(Any printable character except\n"
" space is allowed, with a maximum of 16 characters)\n"
" -I, --id=string Set an ID for the device(Any printable character except\n"
" space is allowed, with a maximum of 32 characters)\n"
" -h, --host=string Server's host or ipaddr(Default is localhost)\n"
@@ -118,7 +121,7 @@ int main(int argc, char **argv)
#define SSL_SHORTOPTS ""
#endif
const char *shortopts = "I:i:h:p:d:aDt:f:RS:vV"SSL_SHORTOPTS;
const char *shortopts = "g:I:i:h:p:d:aDt:f:RS:vV"SSL_SHORTOPTS;
struct ev_loop *loop = EV_DEFAULT;
struct ev_signal signal_watcher;
bool background = false;
@@ -150,8 +153,15 @@ int main(int argc, char **argv)
break;
switch (c) {
case 'g':
if (!valid_id(optarg, 16)) {
log_err("invalid group\n");
return -1;
}
rtty.group = optarg;
break;
case 'I':
if (!valid_id(optarg)) {
if (!valid_id(optarg, 32)) {
log_err("invalid device id\n");
return -1;
}

View File

@@ -369,6 +369,9 @@ static void rtty_register(struct rtty *rtty)
len += rtty_put_attr_u8(wb, MSG_REG_ATTR_HEARTBEAT, rtty->heartbeat);
len += rtty_put_attr_str(wb, MSG_REG_ATTR_DEVID, rtty->devid);
if (rtty->group)
len += rtty_put_attr_str(wb, MSG_REG_ATTR_GROUP, rtty->group);
if (rtty->description)
len += rtty_put_attr_str(wb, MSG_REG_ATTR_DESCRIPTION, rtty->description);

View File

@@ -61,7 +61,8 @@ enum {
MSG_REG_ATTR_HEARTBEAT,
MSG_REG_ATTR_DEVID,
MSG_REG_ATTR_DESCRIPTION,
MSG_REG_ATTR_TOKEN
MSG_REG_ATTR_TOKEN,
MSG_REG_ATTR_GROUP,
};
enum {
@@ -96,6 +97,7 @@ struct rtty {
const char *host;
int port;
int sock;
const char *group;
const char *devid;
const char *token; /* authorization token */
const char *description;

View File

@@ -48,9 +48,9 @@ int find_login(char *buf, int len)
return -1;
}
bool valid_id(const char *id)
bool valid_id(const char *id, size_t limit)
{
if (strlen(id) > 32)
if (strlen(id) > limit)
return false;
while (*id) {

View File

@@ -30,7 +30,7 @@
int find_login(char *buf, int len);
bool valid_id(const char *id);
bool valid_id(const char *id, size_t limit);
int b64_encode(const void *src, size_t srclen, void *dest, size_t destsize);