-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplinter_cli_cmd_bind.c
More file actions
117 lines (101 loc) · 3.34 KB
/
splinter_cli_cmd_bind.c
File metadata and controls
117 lines (101 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
* @file splinter_cli_cmd_bind.c
* @brief Implements the CLI 'bind' command to map Bloom labels to signal groups.
* License: Apache 2
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <getopt.h>
#include "splinter_cli.h"
#include "splinter.h"
static const char *modname = "bind";
/**
* @brief Display help for the bind command.
*/
void help_cmd_bind(unsigned int level) {
(void) level;
printf("Usage: %s [label_name | mask] <group_id> [--bloom]\n", modname);
printf("Maps a semantic Bloom label to a signal group (0-63).\n");
printf("When a key's label matches the mask, the signal group is pulsed.\n");
puts("\nOptions:");
puts(" -b, --bloom Explicitly indicate this is a Bloom-to-Group binding.");
puts("");
}
static const struct option long_options[] = {
{ "help", no_argument, NULL, 'h' },
{ "bloom", no_argument, NULL, 'b' },
{ NULL, 0, NULL, 0 }
};
static const char *optstring = "hb";
/**
* @brief CLI entry point for the 'bind' command.
*/
int cmd_bind(int argc, char *argv[]) {
int opt = 0, group_id = -1;
uint64_t mask = 0;
bool found = false;
bool bloom_mode = true;
// Reset getopt state for REPL safety
optind = 0;
while ((opt = getopt_long(argc, argv, optstring, long_options, NULL)) != -1) {
switch (opt) {
case 'h':
help_cmd_bind(1);
return 0;
case 'b':
// right now this is the only mode
// that may change to allow external triggers
// still debating (so it's here for now)
bloom_mode = true;
break;
default:
help_cmd_bind(1);
return 1;
}
}
// We need at least the label/mask and the group_id
if (argc - optind < 2) {
help_cmd_bind(1);
return 1;
}
if (! bloom_mode) {
return 1;
}
const char *label_target = argv[optind++];
const char *group_target = argv[optind++];
// resolve the Label/Mask (same logic from cmd_label)
for (int i = 0; i < thisuser.label_count; i++) {
if (!strcasecmp(label_target, thisuser.labels[i].name)) {
mask = thisuser.labels[i].mask;
found = true;
break;
}
}
if (!found) {
char *endptr;
mask = strtoull(label_target, &endptr, 0);
if (*endptr != '\0' || mask == 0) {
fprintf(stderr, "%s: unknown label or invalid hex mask '%s'\n", modname, label_target);
return 1;
}
}
// Resolve Group ID
group_id = atoi(group_target);
if (group_id < 0 || group_id >= 64) {
fprintf(stderr, "%s: invalid signal group '%d' (must be 0-63)\n", modname, group_id);
return 1;
}
// Now we can register the watch
// THIS API MAY CHANGE - It may be better to return the bitmask itself? Debating.
if (splinter_watch_label_register(mask, (uint8_t)group_id) == 0) {
printf("Binding applied: Label '%s' (0x%lx) -> Signal Group %d\n",
label_target, (unsigned long)mask, group_id);
return 0;
} else {
fprintf(stderr, "%s: failed to register watch for mask 0x%lx (check store connection)\n",
modname, (unsigned long)mask);
return 1;
}
}