Compare commits
16 Commits
d82cf253bd
...
main
Author | SHA1 | Date | |
---|---|---|---|
8c819ba562 | |||
080cfc8230 | |||
b3d99fd94b | |||
ada95d1d9c | |||
b21ce7d6ac | |||
4042ebb93d | |||
8853b3306a | |||
0ac6418870 | |||
60cfcf0bc7 | |||
f05d2c985c | |||
7134969e2a | |||
3f73b85e9d | |||
ace99dfe93 | |||
6c04cdf97f | |||
56bb116ab9 | |||
0104e542e1 |
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
bin/
|
||||||
|
.vscode/
|
||||||
|
config.h
|
9
Makefile
9
Makefile
@ -1,9 +1,12 @@
|
|||||||
BIN=bin
|
BIN=bin
|
||||||
|
|
||||||
all: dirs command
|
all: dirs command readref
|
||||||
|
|
||||||
command: command.c
|
command:
|
||||||
gcc -o ${BIN}/command command.c
|
gcc -o ${BIN}/command network.c command.c
|
||||||
|
|
||||||
|
readref:
|
||||||
|
gcc -o ${BIN}/readref network.c mqtt.c -lmosquitto readref.c -lm
|
||||||
|
|
||||||
.PHONY: clean dirs
|
.PHONY: clean dirs
|
||||||
clean:
|
clean:
|
||||||
|
7
README.md
Normal file
7
README.md
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
## build
|
||||||
|
```bash
|
||||||
|
apt install libmosquitto-dev
|
||||||
|
cp config.h.example config.h
|
||||||
|
# edit config.h
|
||||||
|
make
|
||||||
|
```
|
97
command.c
97
command.c
@ -2,82 +2,33 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include <sys/socket.h>
|
#include "network.h"
|
||||||
#include <sys/types.h> // BSD compatible
|
|
||||||
#include <arpa/inet.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#include <errno.h>
|
int main(int argc, char *argv[])
|
||||||
#include <netdb.h>
|
{
|
||||||
|
int xPlaneSocket;
|
||||||
|
|
||||||
#define SERVER "::1"
|
// check argument
|
||||||
#define PORT 49000
|
if (argc != 2)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Usage: %s <command>\n", argv[0]);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
#define COMMAND_BUFFER 256
|
// network
|
||||||
|
xPlaneSocket = createSocket(SRC_PORT);
|
||||||
|
if (xPlaneSocket < 0)
|
||||||
|
{
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
char* printable(char *str) {
|
// send payload
|
||||||
char *ret = strdup(str);
|
if (sendToXPlane(xPlaneSocket, DEST_SERVER, DEST_PORT, "CMND", argv[1], strlen(argv[1])) < 0)
|
||||||
|
{
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = strlen(ret); i >= 0; i--) {
|
// bye bye (and no, I don't care about error anymore; I'll exit anyway?!)
|
||||||
if (ret[i] == '\r' || ret[i] == '\n') {
|
closeSocket(xPlaneSocket);
|
||||||
ret[i] = '\0';
|
exit(EXIT_SUCCESS);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
void sendCommand(int sock, char *cmd) {
|
|
||||||
char bytesToSend[COMMAND_BUFFER];
|
|
||||||
memset(bytesToSend, 0, COMMAND_BUFFER);
|
|
||||||
|
|
||||||
strcpy(&bytesToSend[0], "CMND");
|
|
||||||
strcpy(&bytesToSend[5], cmd);
|
|
||||||
|
|
||||||
int numBytesSend = send(sock, bytesToSend, 5 + strlen(cmd), 0);
|
|
||||||
if (numBytesSend >= 0) {
|
|
||||||
//printf("Send %d bytes to %s:%d: %s\n", numBytesSend, SERVER, PORT, printable(bytesToSend));
|
|
||||||
} else {
|
|
||||||
fprintf(stderr, "Failed to send message to %s:%d (%d): %s", SERVER, PORT, errno, strerror(errno));
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
|
||||||
int xplaneSocket;
|
|
||||||
struct sockaddr_in6 xplaneAddress;
|
|
||||||
|
|
||||||
int err;
|
|
||||||
|
|
||||||
// check argument
|
|
||||||
if (argc != 2) {
|
|
||||||
fprintf(stderr, "Usage: %s <command>\n", argv[0]);
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
// create socket
|
|
||||||
xplaneSocket = socket(AF_INET6, SOCK_DGRAM, 0);
|
|
||||||
if (xplaneSocket == -1) {
|
|
||||||
fprintf(stderr, "Error creating socket (%d): %s\n", errno, strerror(errno));
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
// prepare address struct
|
|
||||||
xplaneAddress.sin6_family = AF_INET6;
|
|
||||||
xplaneAddress.sin6_port = htons(PORT);
|
|
||||||
inet_pton(AF_INET6, SERVER, &xplaneAddress.sin6_addr.s6_addr);
|
|
||||||
|
|
||||||
// connect to xplane
|
|
||||||
err = connect(xplaneSocket, (struct sockaddr *)&xplaneAddress, sizeof(xplaneAddress));
|
|
||||||
if (err) {
|
|
||||||
fprintf(stderr, "Error connecting %s:%d (%d): %s\n", SERVER, PORT, err, gai_strerror(err));
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
// send payload
|
|
||||||
sendCommand(xplaneSocket, argv[1]);
|
|
||||||
|
|
||||||
// bye bye (and no, I don't care about error anymore; I'll exit anyway?!)
|
|
||||||
close(xplaneSocket);
|
|
||||||
exit(EXIT_SUCCESS);
|
|
||||||
}
|
}
|
||||||
|
2
config.h.example
Normal file
2
config.h.example
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
#define MQTT_USER1 "<user>"
|
||||||
|
#define MQTT_PASS "<pass>"
|
93
mqtt.c
Normal file
93
mqtt.c
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
// https://mosquitto.org/api/files/mosquitto-h.html
|
||||||
|
#include <mosquitto.h>
|
||||||
|
|
||||||
|
struct mosquitto *connectMqtt(char *user, char *pass, void (*messageCallback)(struct mosquitto *, void *, const struct mosquitto_message *))
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
struct mosquitto *mqtt;
|
||||||
|
|
||||||
|
err = mosquitto_lib_init();
|
||||||
|
if (err)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error initalizing mosquitto lib (%d): %s\n", err, mosquitto_strerror(err));
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
mqtt = mosquitto_new(user, true, NULL);
|
||||||
|
if (mqtt == NULL)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error creating mosquitto instance (%d): %s\n", errno, strerror(errno));
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
err = mosquitto_username_pw_set(mqtt, user, pass);
|
||||||
|
if (err)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error setting username & password (%d): %s\n", err, mosquitto_strerror(err));
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// FIXME: I am hard coded :(
|
||||||
|
err = mosquitto_connect(mqtt, "openhab.sugarland.lan", 1883, 10);
|
||||||
|
if (err)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error on connection of type ");
|
||||||
|
if (err == MOSQ_ERR_ERRNO)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "system (%d): %s\n", errno, strerror(errno));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fprintf(stderr, "mosquitto (%d): %s\n", err, mosquitto_strerror(err));
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
mosquitto_message_callback_set(mqtt, messageCallback);
|
||||||
|
|
||||||
|
return mqtt;
|
||||||
|
}
|
||||||
|
|
||||||
|
int sendMqtt(struct mosquitto *mqtt, char *topic, char *payload, int payloadLength)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
|
err = mosquitto_publish(mqtt, NULL, topic, payloadLength, payload, 0, false);
|
||||||
|
if (err)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error publishing message to %s (%d): %s\n", topic, err, mosquitto_strerror(err));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int recvMqtt(struct mosquitto *mqtt, char *topic)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
|
err = mosquitto_subscribe(mqtt, NULL, topic, 0);
|
||||||
|
if (err) {
|
||||||
|
fprintf(stderr, "Error subscribing to %s (%d): %s", topic, err, mosquitto_strerror(err));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void disconnectMqtt(struct mosquitto *mqtt)
|
||||||
|
{
|
||||||
|
// ignore errors
|
||||||
|
mosquitto_disconnect(mqtt);
|
||||||
|
mosquitto_destroy(mqtt);
|
||||||
|
mosquitto_lib_cleanup();
|
||||||
|
}
|
4
mqtt.h
Normal file
4
mqtt.h
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
struct mosquitto * connectMqtt(char *user, char *pass, void (*messageCallback)(struct mosquitto *, void *, const struct mosquitto_message *));
|
||||||
|
int sendMqtt(struct mosquitto *mqtt, char *topic, char *payload, int payloadLength);
|
||||||
|
int recvMqtt(struct mosquitto *mqtt, char *topic);
|
||||||
|
void disconnectMqtt(struct mosquitto *mqtt);
|
126
network.c
Normal file
126
network.c
Normal file
@ -0,0 +1,126 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/types.h> // BSD compatible
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
|
||||||
|
#include "network.h"
|
||||||
|
|
||||||
|
const int DGRAM_MSG_START = 0;
|
||||||
|
const int DGRAM_PAYLOAD_START = DGRAM_MSG_START + DGRAM_MSG_LENGTH + DGRAM_NULL_LENGTH;
|
||||||
|
|
||||||
|
int sendToXPlane(int sock, char *destAddr, int destPort, char *msg, void *payload, int lengthOfPayload)
|
||||||
|
{
|
||||||
|
char bytesToSend[SEND_BUFFER];
|
||||||
|
struct sockaddr_in6 xPlaneAddress;
|
||||||
|
memset(bytesToSend, 0, SEND_BUFFER);
|
||||||
|
|
||||||
|
// msg is always 4 bytes long
|
||||||
|
if (strlen(msg) != DGRAM_MSG_LENGTH)
|
||||||
|
{
|
||||||
|
return -10;
|
||||||
|
}
|
||||||
|
|
||||||
|
// payload shouldn't exceed buffer
|
||||||
|
if (lengthOfPayload > (SEND_BUFFER - DGRAM_MSG_LENGTH - DGRAM_NULL_LENGTH))
|
||||||
|
{
|
||||||
|
return -11;
|
||||||
|
}
|
||||||
|
|
||||||
|
strcpy(&bytesToSend[DGRAM_MSG_START], msg);
|
||||||
|
memcpy(&bytesToSend[DGRAM_PAYLOAD_START], payload, lengthOfPayload);
|
||||||
|
|
||||||
|
// prepare destination address struct
|
||||||
|
xPlaneAddress.sin6_family = AF_INET6;
|
||||||
|
xPlaneAddress.sin6_port = htons(destPort);
|
||||||
|
inet_pton(AF_INET6, destAddr, &xPlaneAddress.sin6_addr.s6_addr);
|
||||||
|
|
||||||
|
int numBytesSend = sendto(sock, bytesToSend, DGRAM_MSG_LENGTH + DGRAM_NULL_LENGTH + lengthOfPayload, 0, (struct sockaddr *)&xPlaneAddress, sizeof(xPlaneAddress));
|
||||||
|
if (numBytesSend >= 0)
|
||||||
|
{
|
||||||
|
return numBytesSend;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Failed to send message (%d): %s\n", errno, strerror(errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int recvFromXPlane(int sock, char msg[DGRAM_MSG_LENGTH], void *payload, int maxLength)
|
||||||
|
{
|
||||||
|
char buf[maxLength + DGRAM_MSG_LENGTH];
|
||||||
|
memset(buf, 0, sizeof(buf));
|
||||||
|
|
||||||
|
strcpy(msg, "");
|
||||||
|
|
||||||
|
int recvBytes = recv(sock, buf, maxLength + DGRAM_MSG_LENGTH, MSG_DONTWAIT);
|
||||||
|
if (recvBytes >= 0)
|
||||||
|
{
|
||||||
|
if (recvBytes >= DGRAM_MSG_LENGTH)
|
||||||
|
{
|
||||||
|
memcpy(msg, &buf[0], DGRAM_MSG_LENGTH);
|
||||||
|
memcpy(payload, &buf[DGRAM_MSG_LENGTH], maxLength);
|
||||||
|
return recvBytes - DGRAM_MSG_LENGTH;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Received data without message header?!\n");
|
||||||
|
return -11;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (errno != EAGAIN)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error receiving data (%d): %s\n", errno, strerror(errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int createSocket(int srcPort)
|
||||||
|
{
|
||||||
|
int xPlaneSocket;
|
||||||
|
struct sockaddr_in6 localAddress;
|
||||||
|
|
||||||
|
int err;
|
||||||
|
|
||||||
|
// create socket
|
||||||
|
xPlaneSocket = socket(AF_INET6, SOCK_DGRAM, 0);
|
||||||
|
if (xPlaneSocket == -1)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error creating socket (%d): %s\n", errno, strerror(errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// prepare source address struct
|
||||||
|
localAddress.sin6_family = AF_INET6;
|
||||||
|
localAddress.sin6_port = htons(srcPort);
|
||||||
|
localAddress.sin6_addr = in6addr_any;
|
||||||
|
|
||||||
|
// bind to local port
|
||||||
|
err = bind(xPlaneSocket, (struct sockaddr *)&localAddress, sizeof(localAddress));
|
||||||
|
if (err)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error binding [::]:%d (%d): %s\n", srcPort, errno, gai_strerror(errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return xPlaneSocket;
|
||||||
|
}
|
||||||
|
|
||||||
|
void closeSocket(int sock)
|
||||||
|
{
|
||||||
|
close(sock);
|
||||||
|
}
|
17
network.h
Normal file
17
network.h
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#define DEST_SERVER "::1"
|
||||||
|
#define DEST_PORT 49000
|
||||||
|
#define SRC_PORT 49666
|
||||||
|
|
||||||
|
#define SEND_BUFFER 512
|
||||||
|
#define RECV_BUFFER 256
|
||||||
|
|
||||||
|
#define DGRAM_MSG_LENGTH 4
|
||||||
|
#define DGRAM_NULL_LENGTH 1
|
||||||
|
|
||||||
|
extern const int DGRAM_MSG_START;
|
||||||
|
extern const int DGRAM_PAYLOAD_START;
|
||||||
|
|
||||||
|
int sendToXPlane(int sock, char *destAddr, int destPort, char *msg, void *payload, int lengthOfPayload);
|
||||||
|
int recvFromXPlane(int sock, char msg[4], void *payload, int maxLength);
|
||||||
|
int createSocket(int srcPort);
|
||||||
|
void closeSocket(int sock);
|
241
readref.c
Normal file
241
readref.c
Normal file
@ -0,0 +1,241 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <inttypes.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
#include <mosquitto.h>
|
||||||
|
#include "network.h"
|
||||||
|
#include "mqtt.h"
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
#define MQTT_RREF_REQUEST_LENGTH_ID 3
|
||||||
|
#define MQTT_RREF_REQUEST_LENGTH_FREQ 3
|
||||||
|
#define MQTT_RREF_REQUEST_LENGTH_SEPARATOR 1
|
||||||
|
#define MQTT_RREF_REQUEST_SEPARATOR ' '
|
||||||
|
|
||||||
|
const int MQTT_RREF_REQUEST_START_ID = 0;
|
||||||
|
const int MQTT_RREF_REQUEST_START_FREQ = MQTT_RREF_REQUEST_START_ID + MQTT_RREF_REQUEST_LENGTH_ID + MQTT_RREF_REQUEST_LENGTH_SEPARATOR;
|
||||||
|
const int MQTT_RREF_REQUEST_START_RREF = MQTT_RREF_REQUEST_START_FREQ + MQTT_RREF_REQUEST_LENGTH_FREQ + MQTT_RREF_REQUEST_LENGTH_SEPARATOR;
|
||||||
|
|
||||||
|
const int MQTT_RREF_REQUEST_MIN_LENGTH = MQTT_RREF_REQUEST_LENGTH_ID + MQTT_RREF_REQUEST_LENGTH_FREQ + (2 * MQTT_RREF_REQUEST_LENGTH_SEPARATOR) + 1; // at least one char for rref
|
||||||
|
|
||||||
|
int xPlaneSocket;
|
||||||
|
struct mosquitto *mqtt;
|
||||||
|
|
||||||
|
struct valueCacheElement
|
||||||
|
{
|
||||||
|
float value;
|
||||||
|
bool force;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct valueCacheElement *valueCache = NULL;
|
||||||
|
int valueCacheCount = 0;
|
||||||
|
|
||||||
|
#define XPLANE_RREF_MAX_LENGTH 400
|
||||||
|
struct dref_struct_in
|
||||||
|
{
|
||||||
|
int32_t dref_freq;
|
||||||
|
int32_t dref_sender_index; // the index the customer is using to define this dataref
|
||||||
|
char dref_string[XPLANE_RREF_MAX_LENGTH];
|
||||||
|
};
|
||||||
|
|
||||||
|
struct dref_struct_out
|
||||||
|
{
|
||||||
|
int32_t dref_sender_index;
|
||||||
|
float dref_flt_value;
|
||||||
|
};
|
||||||
|
|
||||||
|
int *strtoint(char *in, size_t start, size_t n)
|
||||||
|
{
|
||||||
|
int *ret = malloc(sizeof(int));
|
||||||
|
*ret = 0;
|
||||||
|
int offset = start + n - 1;
|
||||||
|
|
||||||
|
if (offset < 0)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
int position = offset - i;
|
||||||
|
if (in[position] >= '0' && in[position] <= '9')
|
||||||
|
{
|
||||||
|
*ret += (in[position] - '0') * pow(10, i);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
void requestRrefFromXPlane(const struct mosquitto_message *message)
|
||||||
|
{
|
||||||
|
int *index, *frequency;
|
||||||
|
|
||||||
|
if (message->payloadlen >= MQTT_RREF_REQUEST_MIN_LENGTH)
|
||||||
|
{
|
||||||
|
index = 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 (index == NULL || frequency == NULL)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (*index < valueCacheCount)
|
||||||
|
{
|
||||||
|
valueCache[*index].force = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// prepare struct to send, make sure padding is with NULL bytes
|
||||||
|
struct dref_struct_in drefToRead = {
|
||||||
|
.dref_freq = *frequency,
|
||||||
|
.dref_sender_index = *index};
|
||||||
|
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);
|
||||||
|
|
||||||
|
if (strcmp(message->topic, "/xplane/meta/rref") == 0)
|
||||||
|
{
|
||||||
|
requestRrefFromXPlane(message);
|
||||||
|
}
|
||||||
|
else if (strcmp(message->topic, "/xplane/meta/cmnd") == 0)
|
||||||
|
{
|
||||||
|
sendCommandToXPlane(message);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Unkown topic %s\n", message->topic);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// network
|
||||||
|
xPlaneSocket = createSocket(SRC_PORT);
|
||||||
|
if (xPlaneSocket < 0)
|
||||||
|
{
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// mqtt
|
||||||
|
mqtt = connectMqtt(MQTT_USER1, MQTT_PASS, onMessage);
|
||||||
|
if (mqtt == NULL)
|
||||||
|
{
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// subscribe meta channel
|
||||||
|
recvMqtt(mqtt, "/xplane/meta/#");
|
||||||
|
|
||||||
|
// read from network
|
||||||
|
char msg[DGRAM_MSG_LENGTH + 1];
|
||||||
|
char payload[RECV_BUFFER];
|
||||||
|
|
||||||
|
char mqttTopic[100], mqttPayload[512];
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
int payloadBytes = recvFromXPlane(xPlaneSocket, msg, payload, RECV_BUFFER);
|
||||||
|
if (payloadBytes >= 0)
|
||||||
|
{
|
||||||
|
if (strcmp(msg, "RREF") == 0)
|
||||||
|
{
|
||||||
|
printf("Received message type %s\n", msg);
|
||||||
|
printf("Received message data (%d) ", payloadBytes);
|
||||||
|
for (int i = 0; i < payloadBytes; i++)
|
||||||
|
{
|
||||||
|
printf("%02x", payload[i]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// continue if payloadBytes is valid (1 byte + 8 bytes each dref)
|
||||||
|
if ((payloadBytes - 1) % sizeof(struct dref_struct_out) == 0)
|
||||||
|
{
|
||||||
|
// loop throught all provided drefs
|
||||||
|
for (int i = 1; i < payloadBytes; i += sizeof(struct dref_struct_out))
|
||||||
|
{
|
||||||
|
memset(mqttTopic, 0, sizeof(mqttTopic));
|
||||||
|
memset(mqttPayload, 0, sizeof(mqttPayload));
|
||||||
|
|
||||||
|
struct dref_struct_out drefRead;
|
||||||
|
memcpy(&drefRead, &payload[i], sizeof(struct dref_struct_out));
|
||||||
|
|
||||||
|
// add new element to array
|
||||||
|
if (valueCache == NULL)
|
||||||
|
{
|
||||||
|
valueCacheCount = drefRead.dref_sender_index + 10;
|
||||||
|
valueCache = calloc(valueCacheCount, sizeof(struct valueCacheElement));
|
||||||
|
}
|
||||||
|
else if (drefRead.dref_sender_index >= valueCacheCount)
|
||||||
|
{
|
||||||
|
valueCacheCount = drefRead.dref_sender_index + 10;
|
||||||
|
valueCache = reallocarray(valueCache, valueCacheCount, sizeof(struct valueCacheElement));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// size of array is sufficient
|
||||||
|
}
|
||||||
|
|
||||||
|
if (valueCache[drefRead.dref_sender_index].force || valueCache[drefRead.dref_sender_index].value != drefRead.dref_flt_value)
|
||||||
|
{
|
||||||
|
valueCache[drefRead.dref_sender_index].value = drefRead.dref_flt_value;
|
||||||
|
printf("%d: %f\n", drefRead.dref_sender_index, drefRead.dref_flt_value);
|
||||||
|
|
||||||
|
sprintf(mqttTopic, "/xplane/rref/%d", drefRead.dref_sender_index);
|
||||||
|
sprintf(mqttPayload, "%f", drefRead.dref_flt_value);
|
||||||
|
|
||||||
|
sendMqtt(mqtt, mqttTopic, mqttPayload, strlen(mqttPayload));
|
||||||
|
valueCache[drefRead.dref_sender_index].force = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// neither changed nor forced
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// unknown to me
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
mosquitto_loop(mqtt, 0, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// bye bye (and no, I don't care about error anymore; I'll exit anyway?!)
|
||||||
|
disconnectMqtt(mqtt);
|
||||||
|
closeSocket(xPlaneSocket);
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
|
}
|
3
test/.gitignore
vendored
3
test/.gitignore
vendored
@ -1,2 +1,5 @@
|
|||||||
airspeed
|
airspeed
|
||||||
heading
|
heading
|
||||||
|
mqtt
|
||||||
|
mqtt.h
|
||||||
|
strtoint
|
128
test/airspeed.c
128
test/airspeed.c
@ -15,75 +15,91 @@
|
|||||||
|
|
||||||
#define COMMAND_BUFFER 256
|
#define COMMAND_BUFFER 256
|
||||||
|
|
||||||
char* printable(char *str) {
|
char *printable(char *str)
|
||||||
char *ret = strdup(str);
|
{
|
||||||
|
char *ret = strdup(str);
|
||||||
|
|
||||||
for (int i = strlen(ret); i >= 0; i--) {
|
for (int i = strlen(ret); i >= 0; i--)
|
||||||
if (ret[i] == '\r' || ret[i] == '\n') {
|
{
|
||||||
ret[i] = '\0';
|
if (ret[i] == '\r' || ret[i] == '\n')
|
||||||
}
|
{
|
||||||
}
|
ret[i] = '\0';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendCommand(int sock, char *cmd) {
|
void sendCommand(int sock, char *cmd)
|
||||||
char bytesToSend[COMMAND_BUFFER];
|
{
|
||||||
memset(bytesToSend, 0, COMMAND_BUFFER);
|
char bytesToSend[COMMAND_BUFFER];
|
||||||
|
memset(bytesToSend, 0, COMMAND_BUFFER);
|
||||||
|
|
||||||
strcpy(&bytesToSend[0], "CMND");
|
strcpy(&bytesToSend[0], "CMND");
|
||||||
strcpy(&bytesToSend[5], cmd);
|
strcpy(&bytesToSend[5], cmd);
|
||||||
|
|
||||||
int numBytesSend = send(sock, bytesToSend, 5 + strlen(cmd), 0);
|
int numBytesSend = send(sock, bytesToSend, 5 + strlen(cmd), 0);
|
||||||
if (numBytesSend >= 0) {
|
if (numBytesSend >= 0)
|
||||||
//printf("Send %d bytes to %s:%d: %s\n", numBytesSend, SERVER, PORT, printable(bytesToSend));
|
{
|
||||||
} else {
|
// printf("Send %d bytes to %s:%d: %s\n", numBytesSend, SERVER, PORT, printable(bytesToSend));
|
||||||
fprintf(stderr, "Failed to send message to %s:%d (%d): %s", SERVER, PORT, errno, strerror(errno));
|
}
|
||||||
exit(EXIT_FAILURE);
|
else
|
||||||
}
|
{
|
||||||
|
fprintf(stderr, "Failed to send message to %s:%d (%d): %s", SERVER, PORT, errno, strerror(errno));
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[])
|
||||||
int xplaneSocket;
|
{
|
||||||
struct sockaddr_in6 xplaneAddress;
|
int xplaneSocket;
|
||||||
|
struct sockaddr_in6 xplaneAddress;
|
||||||
|
|
||||||
int err;
|
int err;
|
||||||
|
|
||||||
// check argument
|
// check argument
|
||||||
if (argc != 2 || (strcmp(argv[1], "up") != 0 && strcmp(argv[1], "down") != 0)) {
|
if (argc != 2 || (strcmp(argv[1], "up") != 0 && strcmp(argv[1], "down") != 0))
|
||||||
fprintf(stderr, "Usage: %s <up|down>\n", argv[0]);
|
{
|
||||||
exit(EXIT_FAILURE);
|
fprintf(stderr, "Usage: %s <up|down>\n", argv[0]);
|
||||||
}
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
// create socket
|
// create socket
|
||||||
xplaneSocket = socket(AF_INET6, SOCK_DGRAM, 0);
|
xplaneSocket = socket(AF_INET6, SOCK_DGRAM, 0);
|
||||||
if (xplaneSocket == -1) {
|
if (xplaneSocket == -1)
|
||||||
fprintf(stderr, "Error creating socket (%d): %s\n", errno, strerror(errno));
|
{
|
||||||
exit(EXIT_FAILURE);
|
fprintf(stderr, "Error creating socket (%d): %s\n", errno, strerror(errno));
|
||||||
}
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
// prepare address struct
|
// prepare address struct
|
||||||
xplaneAddress.sin6_family = AF_INET6;
|
xplaneAddress.sin6_family = AF_INET6;
|
||||||
xplaneAddress.sin6_port = htons(PORT);
|
xplaneAddress.sin6_port = htons(PORT);
|
||||||
inet_pton(AF_INET6, SERVER, &xplaneAddress.sin6_addr.s6_addr);
|
inet_pton(AF_INET6, SERVER, &xplaneAddress.sin6_addr.s6_addr);
|
||||||
|
|
||||||
// connect to xplane
|
// connect to xplane
|
||||||
err = connect(xplaneSocket, (struct sockaddr *)&xplaneAddress, sizeof(xplaneAddress));
|
err = connect(xplaneSocket, (struct sockaddr *)&xplaneAddress, sizeof(xplaneAddress));
|
||||||
if (err) {
|
if (err)
|
||||||
fprintf(stderr, "Error connecting %s:%d (%d): %s\n", SERVER, PORT, err, gai_strerror(err));
|
{
|
||||||
exit(EXIT_FAILURE);
|
fprintf(stderr, "Error connecting %s:%d (%d): %s\n", SERVER, PORT, err, gai_strerror(err));
|
||||||
}
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
// send payload
|
// send payload
|
||||||
if (strcmp(argv[1], "up") == 0) {
|
if (strcmp(argv[1], "up") == 0)
|
||||||
sendCommand(xplaneSocket, "sim/autopilot/airspeed_up");
|
{
|
||||||
} else if (strcmp(argv[1], "down") == 0) {
|
sendCommand(xplaneSocket, "sim/autopilot/airspeed_up");
|
||||||
sendCommand(xplaneSocket, "sim/autopilot/airspeed_down");
|
}
|
||||||
} else {
|
else if (strcmp(argv[1], "down") == 0)
|
||||||
exit(EXIT_FAILURE);
|
{
|
||||||
}
|
sendCommand(xplaneSocket, "sim/autopilot/airspeed_down");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
// bye bye (and no, I don't care about error anymore; I'll exit anyway?!)
|
// bye bye (and no, I don't care about error anymore; I'll exit anyway?!)
|
||||||
close(xplaneSocket);
|
close(xplaneSocket);
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
128
test/heading.c
128
test/heading.c
@ -15,75 +15,91 @@
|
|||||||
|
|
||||||
#define COMMAND_BUFFER 256
|
#define COMMAND_BUFFER 256
|
||||||
|
|
||||||
char* printable(char *str) {
|
char *printable(char *str)
|
||||||
char *ret = strdup(str);
|
{
|
||||||
|
char *ret = strdup(str);
|
||||||
|
|
||||||
for (int i = strlen(ret); i >= 0; i--) {
|
for (int i = strlen(ret); i >= 0; i--)
|
||||||
if (ret[i] == '\r' || ret[i] == '\n') {
|
{
|
||||||
ret[i] = '\0';
|
if (ret[i] == '\r' || ret[i] == '\n')
|
||||||
}
|
{
|
||||||
}
|
ret[i] = '\0';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendCommand(int sock, char *cmd) {
|
void sendCommand(int sock, char *cmd)
|
||||||
char bytesToSend[COMMAND_BUFFER];
|
{
|
||||||
memset(bytesToSend, 0, COMMAND_BUFFER);
|
char bytesToSend[COMMAND_BUFFER];
|
||||||
|
memset(bytesToSend, 0, COMMAND_BUFFER);
|
||||||
|
|
||||||
strcpy(&bytesToSend[0], "CMND");
|
strcpy(&bytesToSend[0], "CMND");
|
||||||
strcpy(&bytesToSend[5], cmd);
|
strcpy(&bytesToSend[5], cmd);
|
||||||
|
|
||||||
int numBytesSend = send(sock, bytesToSend, 5 + strlen(cmd), 0);
|
int numBytesSend = send(sock, bytesToSend, 5 + strlen(cmd), 0);
|
||||||
if (numBytesSend >= 0) {
|
if (numBytesSend >= 0)
|
||||||
//printf("Send %d bytes to %s:%d: %s\n", numBytesSend, SERVER, PORT, printable(bytesToSend));
|
{
|
||||||
} else {
|
// printf("Send %d bytes to %s:%d: %s\n", numBytesSend, SERVER, PORT, printable(bytesToSend));
|
||||||
fprintf(stderr, "Failed to send message to %s:%d (%d): %s", SERVER, PORT, errno, strerror(errno));
|
}
|
||||||
exit(EXIT_FAILURE);
|
else
|
||||||
}
|
{
|
||||||
|
fprintf(stderr, "Failed to send message to %s:%d (%d): %s", SERVER, PORT, errno, strerror(errno));
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[])
|
||||||
int xplaneSocket;
|
{
|
||||||
struct sockaddr_in6 xplaneAddress;
|
int xplaneSocket;
|
||||||
|
struct sockaddr_in6 xplaneAddress;
|
||||||
|
|
||||||
int err;
|
int err;
|
||||||
|
|
||||||
// check argument
|
// check argument
|
||||||
if (argc != 2 || (strcmp(argv[1], "up") != 0 && strcmp(argv[1], "down") != 0)) {
|
if (argc != 2 || (strcmp(argv[1], "up") != 0 && strcmp(argv[1], "down") != 0))
|
||||||
fprintf(stderr, "Usage: %s <up|down>\n", argv[0]);
|
{
|
||||||
exit(EXIT_FAILURE);
|
fprintf(stderr, "Usage: %s <up|down>\n", argv[0]);
|
||||||
}
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
// create socket
|
// create socket
|
||||||
xplaneSocket = socket(AF_INET6, SOCK_DGRAM, 0);
|
xplaneSocket = socket(AF_INET6, SOCK_DGRAM, 0);
|
||||||
if (xplaneSocket == -1) {
|
if (xplaneSocket == -1)
|
||||||
fprintf(stderr, "Error creating socket (%d): %s\n", errno, strerror(errno));
|
{
|
||||||
exit(EXIT_FAILURE);
|
fprintf(stderr, "Error creating socket (%d): %s\n", errno, strerror(errno));
|
||||||
}
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
// prepare address struct
|
// prepare address struct
|
||||||
xplaneAddress.sin6_family = AF_INET6;
|
xplaneAddress.sin6_family = AF_INET6;
|
||||||
xplaneAddress.sin6_port = htons(PORT);
|
xplaneAddress.sin6_port = htons(PORT);
|
||||||
inet_pton(AF_INET6, SERVER, &xplaneAddress.sin6_addr.s6_addr);
|
inet_pton(AF_INET6, SERVER, &xplaneAddress.sin6_addr.s6_addr);
|
||||||
|
|
||||||
// connect to xplane
|
// connect to xplane
|
||||||
err = connect(xplaneSocket, (struct sockaddr *)&xplaneAddress, sizeof(xplaneAddress));
|
err = connect(xplaneSocket, (struct sockaddr *)&xplaneAddress, sizeof(xplaneAddress));
|
||||||
if (err) {
|
if (err)
|
||||||
fprintf(stderr, "Error connecting %s:%d (%d): %s\n", SERVER, PORT, err, gai_strerror(err));
|
{
|
||||||
exit(EXIT_FAILURE);
|
fprintf(stderr, "Error connecting %s:%d (%d): %s\n", SERVER, PORT, err, gai_strerror(err));
|
||||||
}
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
// send payload
|
// send payload
|
||||||
if (strcmp(argv[1], "up") == 0) {
|
if (strcmp(argv[1], "up") == 0)
|
||||||
sendCommand(xplaneSocket, "sim/autopilot/heading_up");
|
{
|
||||||
} else if (strcmp(argv[1], "down") == 0) {
|
sendCommand(xplaneSocket, "sim/autopilot/heading_up");
|
||||||
sendCommand(xplaneSocket, "sim/autopilot/heading_down");
|
}
|
||||||
} else {
|
else if (strcmp(argv[1], "down") == 0)
|
||||||
exit(EXIT_FAILURE);
|
{
|
||||||
}
|
sendCommand(xplaneSocket, "sim/autopilot/heading_down");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
// bye bye (and no, I don't care about error anymore; I'll exit anyway?!)
|
// bye bye (and no, I don't care about error anymore; I'll exit anyway?!)
|
||||||
close(xplaneSocket);
|
close(xplaneSocket);
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
65
test/mqtt.c
Normal file
65
test/mqtt.c
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
// https://mosquitto.org/api/files/mosquitto-h.html
|
||||||
|
#include <mosquitto.h>
|
||||||
|
|
||||||
|
#include "mqtt.h"
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
struct mosquitto *mqtt;
|
||||||
|
|
||||||
|
err = mosquitto_lib_init();
|
||||||
|
if (err)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error initalizing mosquitto lib (%d): %s\n", err, mosquitto_strerror(err));
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
mqtt = mosquitto_new(MQTT_USER, true, NULL);
|
||||||
|
if (mqtt == NULL)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error creating mosquitto instance (%d): %s\n", errno, strerror(errno));
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
err = mosquitto_username_pw_set(mqtt, MQTT_USER, MQTT_PASS);
|
||||||
|
if (err)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error setting username & password (%d): %s\n", err, mosquitto_strerror(err));
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
err = mosquitto_connect(mqtt, "openhab.sugarland.lan", 1883, 10);
|
||||||
|
if (err)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error on connection of type ");
|
||||||
|
if (err == MOSQ_ERR_ERRNO)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "system (%d): %s\n", errno, strerror(errno));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fprintf(stderr, "mosquitto (%d): %s\n", err, mosquitto_strerror(err));
|
||||||
|
}
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *foo = "blah";
|
||||||
|
err = mosquitto_publish(mqtt, NULL, "/xplane/foo", strlen(foo), foo, 0, false);
|
||||||
|
if (err)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error publishing message (%d): %s\n", err, mosquitto_strerror(err));
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
err = mosquitto_disconnect(mqtt);
|
||||||
|
// ignore error
|
||||||
|
|
||||||
|
mosquitto_destroy(mqtt);
|
||||||
|
}
|
44
test/strtoint.c
Normal file
44
test/strtoint.c
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
int *strtoint(char *in, size_t start, size_t n)
|
||||||
|
{
|
||||||
|
int *ret = malloc(sizeof(int));
|
||||||
|
*ret = 0;
|
||||||
|
int offset = start + n - 1;
|
||||||
|
|
||||||
|
if (offset < 0)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
int position = offset - i;
|
||||||
|
printf("foo: %c\n", in[position]);
|
||||||
|
if (in[position] >= '0' && in[position] <= '9')
|
||||||
|
{
|
||||||
|
*ret += (in[position] - '0') * pow(10, i);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
char *in = "abcd123";
|
||||||
|
int *ret = strtoint(in, 4, 3);
|
||||||
|
if (ret != NULL)
|
||||||
|
{
|
||||||
|
printf("%s | %d\n", in, *ret);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("ret is NULL\n");
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user