send command to xplane received by mqtt
This commit is contained in:
parent
4042ebb93d
commit
b21ce7d6ac
60
readref.c
60
readref.c
@ -64,35 +64,51 @@ int *strtoint(char *in, size_t start, size_t n)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void onMessage(struct mosquitto *mqtt, void *obj, const struct mosquitto_message *message)
|
void requestRrefFromXPlane(const struct mosquitto_message *message)
|
||||||
{
|
{
|
||||||
int *id, *frequency;
|
int *id, *frequency;
|
||||||
char rref[XPLANE_RREF_MAX_LENGTH];
|
|
||||||
|
|
||||||
|
if (message->payloadlen >= MQTT_RREF_REQUEST_MIN_LENGTH)
|
||||||
|
{
|
||||||
|
id = strtoint(message->payload, MQTT_RREF_REQUEST_START_ID, MQTT_RREF_REQUEST_LENGTH_ID);
|
||||||
|
frequency = strtoint(message->payload, MQTT_RREF_REQUEST_START_FREQ, MQTT_RREF_REQUEST_LENGTH_FREQ);
|
||||||
|
|
||||||
|
if (id == NULL || frequency == NULL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// prepare struct to send, make sure padding is with NULL bytes
|
||||||
|
struct dref_struct_in drefToRead = {
|
||||||
|
.dref_freq = *frequency,
|
||||||
|
.dref_sender_index = *id
|
||||||
|
};
|
||||||
|
memset(drefToRead.dref_string, 0, sizeof(drefToRead.dref_string));
|
||||||
|
strncpy(drefToRead.dref_string, &((char *)message->payload)[MQTT_RREF_REQUEST_START_RREF], message->payloadlen - MQTT_RREF_REQUEST_START_RREF);
|
||||||
|
|
||||||
|
sendToXPlane(xPlaneSocket, DEST_SERVER, DEST_PORT, "RREF", (char *)&drefToRead, sizeof(drefToRead));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void sendCommandToXPlane(const struct mosquitto_message *message)
|
||||||
|
{
|
||||||
|
char *command;
|
||||||
|
|
||||||
|
if (message->payloadlen > 0) {
|
||||||
|
sendToXPlane(xPlaneSocket, DEST_SERVER, DEST_PORT, "CMND", (char *)message->payload, message->payloadlen);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void onMessage(struct mosquitto *mqtt, void *obj, const struct mosquitto_message *message)
|
||||||
|
{
|
||||||
printf("Received message %.*s on topic %s\n", message->payloadlen, (char *)message->payload, message->topic);
|
printf("Received message %.*s on topic %s\n", message->payloadlen, (char *)message->payload, message->topic);
|
||||||
|
|
||||||
if (strcmp(message->topic, "/xplane/meta/rref") == 0)
|
if (strcmp(message->topic, "/xplane/meta/rref") == 0)
|
||||||
{
|
{
|
||||||
printf("found rref request\n");
|
requestRrefFromXPlane(message);
|
||||||
if (message->payloadlen >= MQTT_RREF_REQUEST_MIN_LENGTH)
|
}
|
||||||
{
|
else if (strcmp(message->topic, "/xplane/meta/cmnd") == 0)
|
||||||
id = strtoint(message->payload, MQTT_RREF_REQUEST_START_ID, MQTT_RREF_REQUEST_LENGTH_ID);
|
{
|
||||||
frequency = strtoint(message->payload, MQTT_RREF_REQUEST_START_FREQ, MQTT_RREF_REQUEST_LENGTH_FREQ);
|
sendCommandToXPlane(message);
|
||||||
|
|
||||||
if (id == NULL || frequency == NULL) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// prepare struct to send, make sure padding is with NULL bytes
|
|
||||||
struct dref_struct_in drefToRead = {
|
|
||||||
.dref_freq = *frequency,
|
|
||||||
.dref_sender_index = *id
|
|
||||||
};
|
|
||||||
memset(drefToRead.dref_string, 0, sizeof(drefToRead.dref_string));
|
|
||||||
strncpy(drefToRead.dref_string, &((char *)message->payload)[MQTT_RREF_REQUEST_START_RREF], message->payloadlen - MQTT_RREF_REQUEST_START_RREF);
|
|
||||||
|
|
||||||
sendToXPlane(xPlaneSocket, DEST_SERVER, DEST_PORT, "RREF", (char *)&drefToRead, sizeof(drefToRead));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user