Soletta™ Framework
Framework for making IoT devices

Full online documentation | C API Index
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
/src/samples/coap/oic-server.c
/*
* This file is part of the Soletta (TM) Project
*
* Copyright (C) 2015 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <errno.h>
#include <fcntl.h>
#include <linux/kd.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include "sol-log.h"
#include "sol-mainloop.h"
#include "sol-str-slice.h"
#include "sol-util.h"
#include "sol-vector.h"
#include "sol-coap.h"
#include "sol-oic-server.h"
static int console_fd;
static bool led_state;
static bool
{
char value;
if (console_fd < 0)
return led_state;
if (ioctl(console_fd, KDGETLED, (char *)&value)) {
perror("Could not get led state");
return false;
}
return value & LED_SCR;
}
static bool
{
char old;
if (console_fd < 0) {
printf("setting LED to %s\n", on ? "true" : "false");
led_state = on;
return true;
}
if (ioctl(console_fd, KDGETLED, (char *)&old)) {
perror("Could not get led state");
return false;
}
if (ioctl(console_fd, KDSETLED, on ? (old | LED_SCR) : (old & ~LED_SCR))) {
perror("Could not set led state");
return false;
}
return true;
}
static int
user_handle_get(void *data, struct sol_oic_request *request)
{
int r;
struct sol_oic_repr_field field;
struct sol_oic_response *response = NULL;
struct sol_oic_map_writer *output;
response = sol_oic_server_response_new(request);
SOL_NULL_CHECK_GOTO(response, error);
r = sol_oic_map_append(output, &field);
SOL_INT_CHECK_GOTO(r, < 0, error);
field = SOL_OIC_REPR_INT("power", 13);
r = sol_oic_map_append(output, &field);
SOL_INT_CHECK_GOTO(r, < 0, error);
return sol_oic_server_send_response(request, response,
error:
if (response)
return sol_oic_server_send_response(request, NULL,
}
static int
user_handle_put(void *data, struct sol_oic_request *request)
{
struct sol_oic_repr_field field;
struct sol_oic_map_reader iter;
struct sol_oic_map_reader *input;
SOL_OIC_MAP_LOOP(input, &field, &iter, reason) {
if (!strcmp(field.key, "state") && field.type == SOL_OIC_REPR_TYPE_BOOL) {
if (set_scrolllock_led(field.v_boolean))
else
goto end;
}
}
end:
return sol_oic_server_send_response(request, NULL, code);
}
static struct sol_oic_server_resource *
int (*handle_get)(void *data, struct sol_oic_request *request),
int (*handle_put)(void *data, struct sol_oic_request *request),
const char *resource_type)
{
/* This function will be auto-generated from the RAML definitions. */
struct sol_oic_resource_type rt = {
.resource_type = sol_str_slice_from_str(resource_type),
.interface = SOL_STR_SLICE_LITERAL("oc.mi.def"),
.get = {
.handle = handle_get /* User-provided. */
},
.put = {
.handle = handle_put /* User-provided. */
}
};
}
int
main(int argc, char *argv[])
{
char old_led_state;
const char *resource_type = "core.light";
if (argc < 2) {
printf("No resource type specified, assuming core.light\n");
} else {
printf("Resource type specified: %s\n", argv[1]);
resource_type = argv[1];
}
resource_type);
if (!res) {
SOL_WRN("Could not register light resource type.");
return -1;
}
console_fd = open("/dev/console", O_RDWR);
if (console_fd < 0) {
SOL_WRN("Could not open '/dev/console', printing to stdout");
} else if (ioctl(console_fd, KDGETLED, (char *)&old_led_state)) {
SOL_ERR("Could not get the keyboard leds state");
return -1;
}
if (console_fd >= 0 && ioctl(console_fd, KDSETLED, old_led_state)) {
SOL_ERR("Could not return the leds to the old state");
return -1;
}
return 0;
}