#include <errno.h>
#include <fcntl.h>
#include <linux/kd.h>
#include <netinet/in.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <unistd.h>
#define DEFAULT_UDP_PORT 5683
#define OC_CORE_JSON_SEPARATOR ","
#define OC_CORE_ELEM_JSON_START "{\"oc\":[{\"href\":\"%s\",\"rep\":{"
#define OC_CORE_PROP_JSON_NUMBER "\"%s\":%d"
#define OC_CORE_PROP_JSON_STRING "\"%s\":\"%s\""
#define OC_CORE_PROP_JSON_BOOLEAN "\"%s\":%s"
#define OC_CORE_ELEM_JSON_END "}}]}"
};
static bool
{
if (ioctl(
console_fd, KDGETLED, (
char *)&value)) {
perror("Could not get led state");
return false;
}
return value & LED_SCR;
}
static void
{
char old;
perror("Could not get led state");
return;
}
if (ioctl(
console_fd, KDSETLED, on ? (old | LED_SCR) : (old & ~LED_SCR))) {
perror("Could not set led state");
return;
}
}
static int
{
int r;
return 0;
}
static int
{
char *sub = NULL;
size_t offset;
int r;
if (buf)
if (!sub) {
goto done;
}
value = !memcmp(sub + strlen("state\":"), "true", sizeof("true") - 1);
SOL_INF(
"Changing light state to %s", value ?
"on" :
"off");
done:
if (!resp) {
SOL_WRN(
"Could not build response packet");
return -1;
}
err:
return r;
}
static bool
{
int r;
err:
return false;
}
static int
{
int r;
if (!resp) {
SOL_WRN(
"Could not build response packet");
return -1;
}
err:
return r;
}
.path = {
}
};
int
main(
int argc,
char *argv[])
{
char old_led_state;
if (!server) {
fprintf(stderr,
"Could not create a coap server using port %d.\n",
DEFAULT_UDP_PORT);
return -1;
}
perror("Could not register light resource");
goto err;
}
perror("Could not open '/dev/console'");
goto err;
}
if (ioctl(
console_fd, KDGETLED, (
char *)&old_led_state)) {
perror("Could not get the keyboard leds state");
goto err_fd;
}
perror("Could not return the leds to the old state");
goto err_fd;
}
return 0;
err_fd:
err:
return -1;
}