Files
domo-iot/src/main.c
2019-12-26 16:34:26 +01:00

189 lines
5.0 KiB
C

/*!
* main.c
*
* Copyright (c) 2015-2019, NADAL Jean-Baptiste. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
* @Author: NADAL Jean-Baptiste
* @Date: 08/11/2019
*
*/
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
/*-------------------------------- INCLUDES ---------------------------------*/
#include <stdlib.h>
#include <stdio.h>
#include <libgen.h>
#include <unistd.h>
#include <string.h>
#include <qlibc/qlibc.h>
#include <event2/event.h>
#include "rest/rest_handler.h"
#include "broker/nats_broker.h"
#include "devices/devices_manager.h"
#define k_max_path_len 200 /* make this larger if you need to. */
#define k_devices_file_path "share/domo/devices.json"
#define k_sequences_file_path "share/domo/sequences.json"
/*--------------------------------------------------------------------------*/
char *get_config_path(const char *file_path)
{
int length;
char full_path[k_max_path_len];
char *working_path;
char *last_slash;
length = readlink("/proc/self/exe", full_path, sizeof(full_path));
/* Catch some errors: */
if (length < 0)
{
fprintf(stderr, "Error resolving symlink /proc/self/exe.\n");
return NULL;
}
if (length >= k_max_path_len)
{
fprintf(stderr, "Path too long. Truncated.\n");
return NULL;
}
/* I don't know why, but the string this readlink() function
* returns is appended with a '@'.
*/
full_path[length] = '\0'; /* Strip '@' off the end. */
working_path = dirname(full_path);
last_slash = working_path != NULL ? strrchr(working_path, '/') : NULL;
if (last_slash != NULL)
{
full_path[(last_slash - working_path + 1)] = '\0';
strcat(full_path, file_path);
if (access(full_path, F_OK) != -1)
{
char *to_str = (char *) malloc(strlen(full_path) + 1);
to_str = strdup(full_path);
return to_str;
}
}
return NULL;
}
/*--------------------------------------------------------------------------*/
void usage(void)
{
printf("Usage : domo-iot [web server path] \n");
printf("web server path: \t root path of the Web server.\n");
}
/*--------------------------------------------------------------------------*/
int main(int argc, char **argv)
{
struct event_base *ev_base;
restd_server_t *rest_server;
nats_broker_t *nats_broker;
devices_manager_t *device_manager;
int ret;
if (argc < 2)
{
usage();
return -1;
}
restd_log_level(RESTD_LOG_DEBUG);
// data.
device_manager = devices_manager_new();
if (device_manager == NULL)
{
fprintf(stderr, "Failed to create the device manager.\n");
return -1;
}
if (devices_manager_load(device_manager, get_config_path(k_devices_file_path)) != 0)
{
fprintf(stderr, "Failed to Load the devices.\n");
return -1;
}
devices_manager_save(device_manager);
#if 0
/* Setup the Sequences Manager. */
SequencesManager the_sequences_manager(the_config_path + "/sequences.json", &the_devices_manager);
if (the_sequences_manager.load() != 0)
{
fprintf(stderr, "Failed to load timers.\n");
return -2;
}
the_sequences_manager.save();
#endif
/* event loop */
ev_base = event_base_new();
if (!ev_base)
{
fprintf(stderr, "Failed to create a new event base.\n");
return -1;
}
/* Create the rest Server. */
rest_server = restd_server_new();
if (!rest_server)
{
fprintf(stderr, "Failed to create a rest server.\n");
return -1;
}
restd_server_attach_event_loop(rest_server, ev_base);
if (setup_rest_server(rest_server, "8888", argv[1]) != 0)
{
fprintf(stderr, "Failed to setup rest Server\n.");
restd_server_free(rest_server);
return -1;
}
/* create and setup the nats broker. */
nats_broker = nats_broker_new();
if (!nats_broker)
{
fprintf(stderr, "Failed to create a nats broker.\n");
return -1;
}
nats_broker_setup(nats_broker, ev_base);
nats_broker_connect(nats_broker, "foo");
/* start the rest server */
ret = restd_server_start(rest_server);
/* Clean data. */
return ret;
}