diff --git a/meta-digi-dey/recipes-digi/dey-examples/dey-examples-cloudconnector.bb b/meta-digi-dey/recipes-digi/dey-examples/dey-examples-cloudconnector.bb new file mode 100644 index 000000000..7a1643281 --- /dev/null +++ b/meta-digi-dey/recipes-digi/dey-examples/dey-examples-cloudconnector.bb @@ -0,0 +1,18 @@ +SUMMARY = "DEY examples: Device cloud test applications" +SECTION = "examples" +LICENSE = "MPL-2.0" +LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MPL-2.0;md5=815ca599c9df247a0c7f619bab123dad" + +DEPENDS = "cloudconnector" + +SRC_URI = "file://cloudconnector_test" + +S = "${WORKDIR}/cloudconnector_test" + +do_install() { + oe_runmake DESTDIR=${D} install +} + +RDEPENDS_${PN} = "cloudconnector-cert" + +COMPATIBLE_MACHINE = "(ccimx6$|ccimx6ul)" diff --git a/meta-digi-dey/recipes-digi/dey-examples/files/cloudconnector_test/Makefile b/meta-digi-dey/recipes-digi/dey-examples/files/cloudconnector_test/Makefile new file mode 100644 index 000000000..989acb8a6 --- /dev/null +++ b/meta-digi-dey/recipes-digi/dey-examples/files/cloudconnector_test/Makefile @@ -0,0 +1,27 @@ +# +# 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. +# + +SUBDIRS := device_request_listener upload_data_points upload_file + +all: $(SUBDIRS) + +.PHONY: $(SUBDIRS) +$(SUBDIRS): + $(MAKE) -C $@ + +.PHONY: clean install +clean install: + for a in $(SUBDIRS); do $(MAKE) -C $$a $@; done diff --git a/meta-digi-dey/recipes-digi/dey-examples/files/cloudconnector_test/device_request_listener/Makefile b/meta-digi-dey/recipes-digi/dey-examples/files/cloudconnector_test/device_request_listener/Makefile new file mode 100644 index 000000000..4ebffe73e --- /dev/null +++ b/meta-digi-dey/recipes-digi/dey-examples/files/cloudconnector_test/device_request_listener/Makefile @@ -0,0 +1,38 @@ +# +# 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. +# + +PROGRAM := device_request_listener + +CFLAGS += -Wall + +CFLAGS += $(shell pkg-config --cflags cloudconnector) +LDLIBS += $(shell pkg-config --libs cloudconnector) + +all: $(PROGRAM) + +OBJS = device_request.o main.o + +$(PROGRAM): $(OBJS) + $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@ + +.PHONY: install +install: $(PROGRAM) + install -d $(DESTDIR)/usr/bin + install -m 0755 $(PROGRAM) $(DESTDIR)/usr/bin/ + +.PHONY: clean +clean: + -rm -f *.o $(PROGRAM) diff --git a/meta-digi-dey/recipes-digi/dey-examples/files/cloudconnector_test/device_request_listener/device_request.c b/meta-digi-dey/recipes-digi/dey-examples/files/cloudconnector_test/device_request_listener/device_request.c new file mode 100644 index 000000000..cf74e209e --- /dev/null +++ b/meta-digi-dey/recipes-digi/dey-examples/files/cloudconnector_test/device_request_listener/device_request.c @@ -0,0 +1,88 @@ +/* + * 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 +#include + +#include "device_request.h" + +#define MAX_RESPONSE_SIZE 256 + +#if !(defined UNUSED_ARGUMENT) +#define UNUSED_ARGUMENT(a) (void)(a) +#endif + +/* + * get_time_cb() - Data callback for 'get_time' device requests + * + * @target: Target ID of the device request (get_time). + * @transport: Communication transport used by the device request. + * @request_buffer_info: Buffer containing the device request. + * @response_buffer_info: Buffer to store the answer of the request. + * + * Logs information about the received request and executes the corresponding + * command. + */ +void get_time_cb(char const *const target, + ccapi_transport_t const transport, + ccapi_buffer_info_t const *const request_buffer_info, + ccapi_buffer_info_t *const response_buffer_info) +{ + UNUSED_ARGUMENT(request_buffer_info); + log_debug("get_time_cb(): target='%s' - transport='%d'", target, transport); + + response_buffer_info->buffer = malloc(sizeof(char) * MAX_RESPONSE_SIZE + 1); + if (response_buffer_info->buffer == NULL) { + log_error("%s\n", "get_time_cb(): response_buffer_info malloc error"); + return; + } + + time_t t = time(NULL); + response_buffer_info->length = snprintf(response_buffer_info->buffer, + MAX_RESPONSE_SIZE, "Time: %s", ctime(&t)); +} + +/* + * get_time_status_cb() - Status callback for 'get_time' device requests + * + * @target: Target ID of the device request (get_time) + * @transport: Communication transport used by the device request. + * @response_buffer_info: Buffer containing the response data. + * @receive_error: The error status of the receive process. + * + * This callback is executed when the response process has finished. It doesn't + * matter if everything worked or there was an error during the process. + * + * Cleans and frees the response buffer. + */ +void get_time_status_cb(char const *const target, + ccapi_transport_t const transport, + ccapi_buffer_info_t *const response_buffer_info, + ccapi_receive_error_t receive_error) +{ + log_debug( + "get_time_status_cb(): target='%s' - transport='%d' - error='%d'", + target, transport, receive_error); + + /* Free the response buffer */ + if (response_buffer_info != NULL) + free(response_buffer_info->buffer); +} diff --git a/meta-digi-dey/recipes-digi/dey-examples/files/cloudconnector_test/device_request_listener/device_request.h b/meta-digi-dey/recipes-digi/dey-examples/files/cloudconnector_test/device_request_listener/device_request.h new file mode 100644 index 000000000..26951204f --- /dev/null +++ b/meta-digi-dey/recipes-digi/dey-examples/files/cloudconnector_test/device_request_listener/device_request.h @@ -0,0 +1,35 @@ +/* + * 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 + * ======================================================================= + */ + +#ifndef DEVICE_REQUEST_H_ +#define DEVICE_REQUEST_H_ + +#include + +#define TARGET_GET_TIME "get_time" + +void get_time_cb(char const *const target, ccapi_transport_t const transport, + ccapi_buffer_info_t const *const request_buffer_info, + ccapi_buffer_info_t *const response_buffer_info); +void get_time_status_cb(char const *const target, + ccapi_transport_t const transport, + ccapi_buffer_info_t *const response_buffer_info, + ccapi_receive_error_t receive_error); + +#endif /* DEVICE_REQUEST_H_ */ diff --git a/meta-digi-dey/recipes-digi/dey-examples/files/cloudconnector_test/device_request_listener/main.c b/meta-digi-dey/recipes-digi/dey-examples/files/cloudconnector_test/device_request_listener/main.c new file mode 100644 index 000000000..f2aee82f5 --- /dev/null +++ b/meta-digi-dey/recipes-digi/dey-examples/files/cloudconnector_test/device_request_listener/main.c @@ -0,0 +1,106 @@ +/* + * 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 +#include +#include + +#include "device_request.h" + +static void sigint_handler(int signum) +{ + log_debug("sigint_handler(): received signal %d to close Cloud connection.\n", signum); + + exit(0); +} + +static void graceful_shutdown(void) +{ + stop_cloud_connection(); + wait_for_ccimp_threads(); +} + +static void add_sigkill_signal(void) +{ + struct sigaction new_action; + struct sigaction old_action; + + atexit(graceful_shutdown); + + /* Setup signal hander. */ + 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); +} + + +/* + * Use the following SCI request to test this example (insert your Device ID): + * + * + * + * + * + * + * + * + * + * + * + * + */ + +int main(void) +{ + cc_init_error_t init_error; + cc_start_error_t start_error; + ccapi_receive_error_t receive_error; + + add_sigkill_signal(); + + init_error = init_cloud_connection(NULL); + if (init_error != CC_INIT_ERROR_NONE) { + log_error("Cannot initialize cloud connection, error %d\n", init_error); + return EXIT_FAILURE; + } + + start_error = start_cloud_connection(); + if (start_error != CC_START_ERROR_NONE) { + log_error("Cannot start cloud connection, error %d\n", start_error); + return EXIT_FAILURE; + } + + receive_error = ccapi_receive_add_target(TARGET_GET_TIME, get_time_cb, + get_time_status_cb, 0); + if (receive_error != CCAPI_RECEIVE_ERROR_NONE) { + log_error("Cannot register target '%s', error %d\n", TARGET_GET_TIME, + receive_error); + return EXIT_FAILURE; + } + + printf("Waiting for Device Cloud request...\n"); + printf("Press a key to exit\n"); + getchar(); + + return EXIT_SUCCESS; +} diff --git a/meta-digi-dey/recipes-digi/dey-examples/files/cloudconnector_test/upload_data_points/Makefile b/meta-digi-dey/recipes-digi/dey-examples/files/cloudconnector_test/upload_data_points/Makefile new file mode 100644 index 000000000..6e01b0f6f --- /dev/null +++ b/meta-digi-dey/recipes-digi/dey-examples/files/cloudconnector_test/upload_data_points/Makefile @@ -0,0 +1,38 @@ +# +# 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. +# + +PROGRAM := upload_data_points + +CFLAGS += -Wall + +CFLAGS += $(shell pkg-config --cflags cloudconnector) +LDLIBS += $(shell pkg-config --libs cloudconnector) + +all: $(PROGRAM) + +OBJS = data_point.o main.o + +$(PROGRAM): $(OBJS) + $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@ + +.PHONY: install +install: $(PROGRAM) + install -d $(DESTDIR)/usr/bin + install -m 0755 $(PROGRAM) $(DESTDIR)/usr/bin/ + +.PHONY: clean +clean: + -rm -f *.o $(PROGRAM) diff --git a/meta-digi-dey/recipes-digi/dey-examples/files/cloudconnector_test/upload_data_points/data_point.c b/meta-digi-dey/recipes-digi/dey-examples/files/cloudconnector_test/upload_data_points/data_point.c new file mode 100644 index 000000000..b5f9428c9 --- /dev/null +++ b/meta-digi-dey/recipes-digi/dey-examples/files/cloudconnector_test/upload_data_points/data_point.c @@ -0,0 +1,143 @@ + /* + * 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 + +#include "data_point.h" + +#define STREAM_NAME "incremental" + +/* + * get_incremental() - Retrieves an incremental value each time + */ +static int get_incremental(void) +{ + static int incremental = -1; + + if (incremental == INT_MAX) + incremental = 0; + else + incremental++; + + log_debug("Incremental = %d\n", incremental); + + return incremental; +} + +/* + * get_timestamp() - Get the current timestamp of the system + * + * Return: The timestamp of the system. + */ +static ccapi_timestamp_t *get_timestamp(void) +{ + ccapi_timestamp_t *timestamp = NULL; + size_t len = strlen("2016-09-27T07:07:09.546Z") + 1; + char *date = NULL; + time_t now; + + timestamp = (ccapi_timestamp_t*) malloc(sizeof(ccapi_timestamp_t)); + if (timestamp == NULL) + return NULL; + + date = (char*) malloc(sizeof(char) * len); + if (date == NULL) { + free(timestamp); + return NULL; + } + + time(&now); + if (strftime(date, len, "%FT%TZ", gmtime(&now)) > 0) { + timestamp->iso8601 = date; + } else { + free(date); + timestamp->iso8601 = NULL; + } + + return timestamp; +} + +ccapi_dp_error_t init_data_stream(ccapi_dp_collection_handle_t *dp_collection) +{ + ccapi_dp_collection_handle_t collection; + ccapi_dp_error_t dp_error; + + dp_error = ccapi_dp_create_collection(&collection); + if (dp_error != CCAPI_DP_ERROR_NONE) { + log_error("ccapi_dp_create_collection() error %d\n", dp_error); + return dp_error; + } else { + *dp_collection = collection; + } + + dp_error = ccapi_dp_add_data_stream_to_collection_extra(collection, + STREAM_NAME, "int32 ts_iso", "counts", NULL); + if (dp_error != CCAPI_DP_ERROR_NONE) { + log_error("ccapi_dp_add_data_stream_to_collection_extra() error %d\n", + dp_error); + free(collection); + } + + return dp_error; +} + +ccapi_dp_error_t add_data_point(ccapi_dp_collection_handle_t dp_collection) +{ + ccapi_dp_error_t dp_error; + + ccapi_timestamp_t *timestamp = get_timestamp(); + + dp_error = ccapi_dp_add(dp_collection, STREAM_NAME, get_incremental(), timestamp); + if (dp_error != CCAPI_DP_ERROR_NONE) { + log_error("ccapi_dp_add() failed with error: %d\n", dp_error); + } + + if (timestamp != NULL) { + if (timestamp->iso8601 != NULL) { + free((char *) timestamp->iso8601); + timestamp->iso8601 = NULL; + } + free(timestamp); + timestamp = NULL; + } + + return dp_error; +} + +ccapi_dp_error_t send_data_stream(ccapi_dp_collection_handle_t dp_collection) +{ + ccapi_dp_error_t dp_error; + + log_debug("%s", "Sending Data Stream with new incremental value\n"); + + dp_error = ccapi_dp_send_collection(CCAPI_TRANSPORT_TCP, dp_collection); + if (dp_error != CCAPI_DP_ERROR_NONE) { + log_error("ccapi_dp_send_collection() error %d\n", dp_error); + } + + return dp_error; +} + +ccapi_dp_error_t destroy_data_stream(ccapi_dp_collection_handle_t dp_collection) +{ + log_debug("%s", "Destroying Data Stream\n"); + return ccapi_dp_destroy_collection(dp_collection); +} diff --git a/meta-digi-dey/recipes-digi/dey-examples/files/cloudconnector_test/upload_data_points/data_point.h b/meta-digi-dey/recipes-digi/dey-examples/files/cloudconnector_test/upload_data_points/data_point.h new file mode 100644 index 000000000..5b66bb181 --- /dev/null +++ b/meta-digi-dey/recipes-digi/dey-examples/files/cloudconnector_test/upload_data_points/data_point.h @@ -0,0 +1,30 @@ +/* + * 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 + * ======================================================================= + */ + +#ifndef DATA_POINT_H_ +#define DATA_POINT_H_ + +#include + +ccapi_dp_error_t init_data_stream(ccapi_dp_collection_handle_t *dp_collection); +ccapi_dp_error_t add_data_point(ccapi_dp_collection_handle_t dp_collection); +ccapi_dp_error_t send_data_stream(ccapi_dp_collection_handle_t dp_collection); +ccapi_dp_error_t destroy_data_stream(ccapi_dp_collection_handle_t dp_collection); + +#endif /* DATA_POINT_H_ */ diff --git a/meta-digi-dey/recipes-digi/dey-examples/files/cloudconnector_test/upload_data_points/main.c b/meta-digi-dey/recipes-digi/dey-examples/files/cloudconnector_test/upload_data_points/main.c new file mode 100644 index 000000000..857b07b49 --- /dev/null +++ b/meta-digi-dey/recipes-digi/dey-examples/files/cloudconnector_test/upload_data_points/main.c @@ -0,0 +1,116 @@ +/* + * 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 +#include + +#include "data_point.h" + +#define DP_SLEEP_TIME 5 +#define DP_NUMBER 10 + +static int running = 1; +static ccapi_dp_collection_handle_t dp_collection; + +static void sigint_handler(int signum) +{ + log_debug("sigint_handler(): received signal %d to close Cloud connection.\n", signum); + + exit(0); +} + +static void graceful_shutdown(void) +{ + if (running == 1) { + destroy_data_stream(dp_collection); + } + + running = 0; + stop_cloud_connection(); + wait_for_ccimp_threads(); +} + +static void add_sigkill_signal(void) +{ + struct sigaction new_action; + struct sigaction old_action; + + atexit(graceful_shutdown); + + /* Setup signal hander. */ + 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); +} + + +int main(void) +{ + cc_init_error_t init_error; + cc_start_error_t start_error; + ccapi_dp_error_t dp_error; + int i; + + add_sigkill_signal(); + + init_error = init_cloud_connection(NULL); + if (init_error != CC_INIT_ERROR_NONE) { + log_error("Cannot initialize cloud connection, error %d\n", init_error); + return EXIT_FAILURE; + } + + start_error = start_cloud_connection(); + if (start_error != CC_START_ERROR_NONE) { + log_error("Cannot start cloud connection, error %d\n", start_error); + return EXIT_FAILURE; + } + + dp_error = init_data_stream(&dp_collection); + if (dp_error != CCAPI_DP_ERROR_NONE) { + log_error("Cannot initialize data stream, error %d\n", start_error); + return EXIT_FAILURE; + } + + running = CCAPI_TRUE; + while (running != CCAPI_FALSE) { + + /* Collect DP_NUMBER data points sampled each DP_SLEEP_TIME seconds */ + for (i = 0; i < DP_NUMBER; i++) { + dp_error = add_data_point(dp_collection); + + if (dp_error != CCAPI_DP_ERROR_NONE) { + log_error("Cannot add data point, error %d\n", start_error); + i--; + } + + sleep(DP_SLEEP_TIME); + } + + /* Send the block of collected data points */ + dp_error = send_data_stream(dp_collection); + if (dp_error != CCAPI_DP_ERROR_NONE) + log_error("Cannot send data stream, error %d\n", start_error); + } + + return EXIT_SUCCESS; +} diff --git a/meta-digi-dey/recipes-digi/dey-examples/files/cloudconnector_test/upload_file/Makefile b/meta-digi-dey/recipes-digi/dey-examples/files/cloudconnector_test/upload_file/Makefile new file mode 100644 index 000000000..3478cf988 --- /dev/null +++ b/meta-digi-dey/recipes-digi/dey-examples/files/cloudconnector_test/upload_file/Makefile @@ -0,0 +1,38 @@ +# +# 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. +# + +PROGRAM := upload_file + +CFLAGS += -Wall + +CFLAGS += $(shell pkg-config --cflags cloudconnector) +LDLIBS += $(shell pkg-config --libs cloudconnector) + +all: $(PROGRAM) + +OBJS = main.o + +$(PROGRAM): $(OBJS) + $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@ + +.PHONY: install +install: $(PROGRAM) + install -d $(DESTDIR)/usr/bin + install -m 0755 $(PROGRAM) $(DESTDIR)/usr/bin/ + +.PHONY: clean +clean: + -rm -f *.o $(PROGRAM) diff --git a/meta-digi-dey/recipes-digi/dey-examples/files/cloudconnector_test/upload_file/main.c b/meta-digi-dey/recipes-digi/dey-examples/files/cloudconnector_test/upload_file/main.c new file mode 100644 index 000000000..e7d90c595 --- /dev/null +++ b/meta-digi-dey/recipes-digi/dey-examples/files/cloudconnector_test/upload_file/main.c @@ -0,0 +1,86 @@ +/* + * 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 +#include + +#define UNUSED_ARGUMENT(a) (void)(a) + +#define STREAM_NAME "examples/uploaded_file" + +static void sigint_handler(int signum) +{ + log_debug("sigint_handler(): received signal %d to close Cloud connection.\n", signum); + + exit(0); +} + +static void graceful_shutdown(void) +{ + stop_cloud_connection(); + wait_for_ccimp_threads(); +} + +static void add_sigkill_signal(void) +{ + struct sigaction new_action; + struct sigaction old_action; + + atexit(graceful_shutdown); + + /* Setup signal hander. */ + 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); +} + +int main(int argc, char *argv[]) +{ + cc_init_error_t init_error; + cc_start_error_t start_error; + ccapi_dp_b_error_t send_error; + + UNUSED_ARGUMENT(argc); + + add_sigkill_signal(); + + init_error = init_cloud_connection(NULL); + if (init_error != CC_INIT_ERROR_NONE) { + log_error("Cannot initialize cloud connection, error %d\n", init_error); + return EXIT_FAILURE; + } + + start_error = start_cloud_connection(); + if (start_error != CC_START_ERROR_NONE) { + log_error("Cannot start cloud connection, error %d\n", start_error); + return EXIT_FAILURE; + } + + send_error = ccapi_dp_binary_send_file(CCAPI_TRANSPORT_TCP, argv[0], STREAM_NAME); + if (send_error != CCAPI_DP_B_ERROR_NONE) { + log_error("ccapi_dp_binary_send_file() failed, error %d\n", send_error); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} diff --git a/meta-digi-dey/recipes-digi/packagegroups/packagegroup-dey-examples.bb b/meta-digi-dey/recipes-digi/packagegroups/packagegroup-dey-examples.bb index c7147fb14..5df5449fb 100644 --- a/meta-digi-dey/recipes-digi/packagegroups/packagegroup-dey-examples.bb +++ b/meta-digi-dey/recipes-digi/packagegroups/packagegroup-dey-examples.bb @@ -30,6 +30,7 @@ RDEPENDS_${PN}_append_ccimx6 = "\ ${@bb.utils.contains("MACHINE_FEATURES", "bluetooth", "dey-examples-bt", "", d)} \ ${@bb.utils.contains("MACHINE_FEATURES", "bluetooth", "dey-examples-btconfig", "", d)} \ dey-examples-can \ + dey-examples-cloudconnector \ ${@bb.utils.contains("MACHINE_FEATURES", "bluetooth", "dey-examples-hdp", "", d)} \ ${@bb.utils.contains("MACHINE_FEATURES", "accel-graphics", "dey-examples-opengles", "", d)} \ dey-examples-v4l2 \ @@ -40,6 +41,7 @@ RDEPENDS_${PN}_append_ccimx6ul = "\ ${@bb.utils.contains("MACHINE_FEATURES", "bluetooth", "dey-examples-btconfig", "", d)} \ dey-examples-adc \ dey-examples-can \ + dey-examples-cloudconnector \ ${@bb.utils.contains("MACHINE_FEATURES", "bluetooth", "dey-examples-hdp", "", d)} \ dey-examples-tamper \ "