diff --git a/awsiot-sample/Makefile b/awsiot-sample/Makefile new file mode 100644 index 0000000..4cc0578 --- /dev/null +++ b/awsiot-sample/Makefile @@ -0,0 +1,49 @@ +# *************************************************************************** +# 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 +# +# *************************************************************************** +EXECUTABLE = awsiot-sample + +SRC = src + +CFLAGS += -Wall +CFLAGS += $(LOG_FLAGS) +CFLAGS += -DGIT_REVISION=\"$(if $(GIT_REVISION),-g$(GIT_REVISION))\" + +CFLAGS += $(shell pkg-config --cflags awsiotsdk) +LDLIBS += $(shell pkg-config --libs --static awsiotsdk) + +SRCS = $(wildcard $(SRC)/*.c) +OBJS = $(SRCS:.c=.o) + +.PHONY: all +all: $(EXECUTABLE) + +$(EXECUTABLE): $(OBJS) + $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@ + +.PHONY: install +install: $(EXECUTABLE) + install -d $(DESTDIR)/usr/bin + install -m 0755 $(EXECUTABLE) $(DESTDIR)/usr/bin/ + install -d $(DESTDIR)/etc + install -m 0644 cfg_files/*.conf $(DESTDIR)/etc/ + +.PHONY: clean +clean: + -rm -f $(EXECUTABLE) $(OBJS) + diff --git a/awsiot-sample/README.md b/awsiot-sample/README.md new file mode 100644 index 0000000..861baa5 --- /dev/null +++ b/awsiot-sample/README.md @@ -0,0 +1,20 @@ +AWS IoT device SDK Demo Application +=================================== +Demo application to connect devices to AWS IoT. + +License +------- +Copyright 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. + diff --git a/awsiot-sample/src/main.c b/awsiot-sample/src/main.c new file mode 100644 index 0000000..b21729b --- /dev/null +++ b/awsiot-sample/src/main.c @@ -0,0 +1,99 @@ +/* + * 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 +#include +#include + +/*------------------------------------------------------------------------------ + 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); +}