dey-examples/awsiot-sample/src/main.c

100 lines
2.8 KiB
C

/*
* Copyright (c) 2017 Digi International Inc.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*
* Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
* =======================================================================
*/
#include <aws_iot_log.h>
#include <signal.h>
#include <unistd.h>
/*------------------------------------------------------------------------------
F U N C T I O N D E C L A R A T I O N S
------------------------------------------------------------------------------*/
static int check_stop(void);
static void add_sigkill_signal(void);
static void graceful_shutdown(void);
static void sigint_handler(int signum);
/*------------------------------------------------------------------------------
G L O B A L V A R I A B L E S
------------------------------------------------------------------------------*/
static volatile int stop = 0;
/*------------------------------------------------------------------------------
F U N C T I O N D E F I N I T I O N S
------------------------------------------------------------------------------*/
int main(int argc, char **argv)
{
int result = EXIT_SUCCESS;
add_sigkill_signal();
do {
sleep(1);
} while (!check_stop());
return result;
}
/*
* check_stop() - Stop application
*
* Return: 1 if it is successfully stopped, 0 otherwise.
*/
static int check_stop(void)
{
return stop;
}
/*
* add_sigkill_signal() - Add the kill signal to the process
*/
static void add_sigkill_signal(void)
{
struct sigaction new_action;
struct sigaction old_action;
/* Setup signal hander. */
atexit(graceful_shutdown);
new_action.sa_handler = sigint_handler;
sigemptyset(&new_action.sa_mask);
new_action.sa_flags = 0;
sigaction(SIGINT, NULL, &old_action);
if (old_action.sa_handler != SIG_IGN)
sigaction(SIGINT, &new_action, NULL);
}
/*
* graceful_shutdown() - Stop MQTT connection and all threads
*/
void graceful_shutdown(void)
{
stop = 1;
check_stop();
}
/**
* sigint_handler() - Manage signal received.
*
* @signum: Received signal.
*/
static void sigint_handler(int signum)
{
IOT_DEBUG("Received signal %d to close Cloud connection.", signum);
exit(0);
}