-
Notifications
You must be signed in to change notification settings - Fork 0
/
Commands.h
84 lines (73 loc) · 2.04 KB
/
Commands.h
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
#include <stdio.h>
#include <string.h>
void handleHelp(char *);
void sendPing(char*);
void retrievePacket(char*);
void retrievePacketSize(char*);
void evalCmd(char *, char *);
void handleCommands(char *);
char deviceName[32] = {};
uint32_t pingCounter = 0;
int cmdCount = 0;
struct myCommand {
void (*ptr)(char *); // Function pointer
char name[12];
char help[48];
};
myCommand cmds[] = {
{handleHelp, "help", "Shows this help."},
{sendPing, "ping", "Sends a preformatted PING."},
{retrievePacketSize, "any", "Retrieves the pending packet's size, if any."},
{retrievePacket, "msg", "Retrieves the pending packet."},
};
void evalCmd(char *str, char *fullString) {
char strq[12];
for (int i = 0; i < cmdCount; i++) {
sprintf(strq, "%s?", cmds[i].name);
if (strcmp(str, cmds[i].name) == 0 || strcmp(strq, str) == 0) {
cmds[i].ptr(fullString);
return;
}
}
}
void handleCommands(char *str1) {
char kwd[32];
int i = sscanf(str1, "/%s", kwd);
if (i > 0) evalCmd(kwd, str1);
else handleHelp("");
}
void retrievePacket(char *param) {
// Do we have a packet available?
msgLen = packetSize;
memcpy(msg, currentPacket, msgLen);
deletePacket = true;
return;
}
void retrievePacketSize(char *param) {
// Do we have a packet available?
Serial.println("Requested packet size");
msgLen = 3;
msg[0] = packetSize;
msg[1] = (uint8_t)(LoRa.packetSnr() + 100);
msg[2] = (uint8_t)(LoRa.packetRssi() * -1);
deletePacket = false;
return;
}
void handleHelp(char *param) {
sprintf(msg, "Available commands: %d\n", cmdCount);
Serial.print(msg);
for (int i = 0; i < cmdCount; i++) {
sprintf(msg, " . /%s: %s", cmds[i].name, cmds[i].help);
Serial.println(msg);
}
}
void sendPing(char* param) {
char UUID[4];
if (randomIndex > 252) stockUpRandom();
sprintf(
msg,
"{\"from\":\"%s\",\"UUID\":\"%02x%02x%02x%02x\", \"cmd\":\"ping\", \"count\":%d}",
deviceName, UUID[randomIndex], UUID[randomIndex + 1], UUID[randomIndex + 2], UUID[randomIndex + 3], pingCounter++);
randomIndex += 4;
sendPacket(msg);
}