From b17458c8b7de319b2bd8c28deec704e295333049 Mon Sep 17 00:00:00 2001 From: Tatiana Leon Date: Thu, 7 Sep 2023 18:33:00 +0200 Subject: [PATCH] cccs: add ConnectCore Cloud Services upload binary file example https://onedigi.atlassian.net/browse/DEL-8628 Signed-off-by: Tatiana Leon --- cccs-examples.mk | 1 + cccs-upload-binary-file-example/Makefile | 40 +++++++++ cccs-upload-binary-file-example/README.md | 56 ++++++++++++ cccs-upload-binary-file-example/main.c | 105 ++++++++++++++++++++++ samples-manifest.xml | 23 +++++ 5 files changed, 225 insertions(+) create mode 100644 cccs-upload-binary-file-example/Makefile create mode 100644 cccs-upload-binary-file-example/README.md create mode 100644 cccs-upload-binary-file-example/main.c diff --git a/cccs-examples.mk b/cccs-examples.mk index f5930eb..6df66ab 100644 --- a/cccs-examples.mk +++ b/cccs-examples.mk @@ -16,6 +16,7 @@ SUBDIRS := \ cccs-data-request-example \ + cccs-upload-binary-file-example \ cccs-upload-data-points-example all: $(SUBDIRS) diff --git a/cccs-upload-binary-file-example/Makefile b/cccs-upload-binary-file-example/Makefile new file mode 100644 index 0000000..8ff59bc --- /dev/null +++ b/cccs-upload-binary-file-example/Makefile @@ -0,0 +1,40 @@ +# +# Copyright 2023, Digi International Inc. +# +# Permission to use, copy, modify, and/or distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# 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. +# + +BINARY := cccs-upload-binary-file-example + +CFLAGS += -Wall + +CFLAGS += $(shell pkg-config --cflags cccs) +LDLIBS += $(shell pkg-config --libs --static cccs) + +SRCS := $(wildcard *.c) +OBJS := $(SRCS:.c=.o) + +$(BINARY): $(OBJS) + $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@ + +.PHONY: install +install: $(BINARY) + install -d $(DESTDIR)/usr/bin + install -m 0755 $^ $(DESTDIR)/usr/bin/ + +.PHONY: all +all: $(BINARY) + +.PHONY: clean +clean: + -rm -f *.o $(BINARY) diff --git a/cccs-upload-binary-file-example/README.md b/cccs-upload-binary-file-example/README.md new file mode 100644 index 0000000..46192b5 --- /dev/null +++ b/cccs-upload-binary-file-example/README.md @@ -0,0 +1,56 @@ +Digi ConnectCore Cloud Services Upload Binary File Example Application +====================================================================== + +Example application to upload a binary file to Remote Manager using ConnectCore +Cloud Services. + +This application uploads the content of file `/etc/build` as a binary data point +to stream `binary_dp`. + +Running the application +----------------------- +This application requires `cccsd` (ConnectCore Cloud Services daemon) running +on the device. + +Once the binary is in the target, launch the application: + +``` +# ./cccs-upload-binary-file-example +cccs-upload-binary-file-example[35036]: [DEBUG] CCCSD: Connected to CCCSD (s=4) +cccs-upload-binary-file-example[35036]: [DEBUG] CCCS daemon ready +cccs-upload-binary-file-example[35036]: [INFO] Sending binary file '/etc/build' +cccs-upload-binary-file-example[35036]: [INFO] DP: Sending data points to CCCSD +cccs-upload-binary-file-example[35036]: [DEBUG] CCCSD: Connected to CCCSD (s=4) +cccs-upload-binary-file-example[35036]: [DEBUG] CCCSD: Success from CCCSD +cccs-upload-binary-file-example[35036]: [DEBUG] DP: Binary data point uploaded to 'binary_dp' +# +``` + +Compiling the application +------------------------- +This example can be compiled using a Digi Embedded Yocto based toolchain. Make +sure to source the corresponding toolchain of the platform you are using, +for example, for ConnectCore 6UL: + +``` +~$ . /environment-setup-cortexa7t2hf-neon-dey-linux-gnueabi +~$ make +``` + +For more information, see the [Digi Embedded Yocto online documentation](https://github.com/digi-embedded/meta-digi). + +License +------- +Copyright 2023, Digi International Inc. + +Permission to use, copy, modify, and/or distribute this software for any purpose +with or without fee is hereby granted, provided that the above copyright notice +and this permission notice appear in all copies. + +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/cccs-upload-binary-file-example/main.c b/cccs-upload-binary-file-example/main.c new file mode 100644 index 0000000..eb2d018 --- /dev/null +++ b/cccs-upload-binary-file-example/main.c @@ -0,0 +1,105 @@ +/* + * Copyright 2023, Digi International Inc. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * 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. + */ + +#include +#include +#include +#include +#include + +#define STREAM_NAME "binary_dp" +#define UPLOAD_FILE "/etc/build" + +/* + * sigaction_handler() - Handler to execute after receiving a signal + * + * @signum: Received signal. + */ +static void sigaction_handler(int signum) +{ + log_debug("%s: received signal %d", __func__, signum); + + /* 'atexit' executes the cleanup function */ + exit(0); +} + +/* + * cleanup() - Frees all the allocated memory before exiting + */ +static void cleanup(void) +{ + deinit_logger(); +} + +/* + * register_signals() - Registers program signals + */ +static void register_signals(void) +{ + struct sigaction new_action; + struct sigaction old_action; + + atexit(cleanup); + + new_action.sa_handler = sigaction_handler; + new_action.sa_flags = 0; + sigemptyset(&new_action.sa_mask); + + sigaction(SIGINT, NULL, &old_action); + if (old_action.sa_handler != SIG_IGN) + sigaction(SIGINT, &new_action, NULL); + + sigaction(SIGHUP, &old_action, NULL); + if (old_action.sa_handler != SIG_IGN) + sigaction(SIGHUP, &new_action, NULL); + + sigaction(SIGTERM, &old_action, NULL); + if (old_action.sa_handler != SIG_IGN) + sigaction(SIGTERM, &new_action, NULL); +} + +int main(int argc, char *argv[]) +{ + char *name = basename(argv[0]); + cccs_comm_error_t ret; + cccs_resp_t resp; + + init_logger(LOG_DEBUG, LOG_CONS | LOG_NDELAY | LOG_PID | LOG_PERROR, name); + + register_signals(); + + if (!cccs_is_daemon_ready(CCCSD_NO_WAIT)) { + log_error("%s: CCCS daemon not ready... exiting", __func__); + + return EXIT_FAILURE; + } + + log_info("Sending binary file '%s'", UPLOAD_FILE); + + ret = cccs_send_dp_binary_file(UPLOAD_FILE, STREAM_NAME, 5, &resp); + if (ret != CCCS_SEND_ERROR_NONE) { + log_error("%s: error sending binary file: CCCSD error %d", __func__, ret); + } else if (resp.code != 0) { + if (resp.hint) + log_error("%s: error sending binary file: CCCSD error %s (%d)", __func__, resp.hint, resp.code); + else + log_error("%s: error sending binary file: CCCSD error %d", __func__, resp.code); + } + + free(resp.hint); + + return EXIT_SUCCESS; +} diff --git a/samples-manifest.xml b/samples-manifest.xml index ce97fb3..59f0ccf 100644 --- a/samples-manifest.xml +++ b/samples-manifest.xml @@ -270,4 +270,27 @@ The uploads takes place every 10 new samples, that is every 50 seconds. ccimx93-dvk + + Digi CCCS upload binary file Example + +Example application to upload a binary file to Remote Manager using ConnectCore +Cloud Services. +This application uploads the content of file `/etc/build` as a binary data point +to stream `binary_dp`. + + cccs-upload-binary-file-example + + ccimx6sbc + ccimx6qpsbc + ccimx6ulstarter + ccimx6ulsbc + ccimx8x-sbc-express + ccimx8x-sbc-pro + ccimx8mn-dvk + ccimx8mm-dvk + ccmp15-dvk + ccmp13-dvk + ccimx93-dvk + +