From ae4ae9e7a5f99df88593886d2f0ff000bbdb37ea Mon Sep 17 00:00:00 2001 From: andreyla Date: Tue, 7 Jul 2020 22:17:52 +0300 Subject: [PATCH 1/8] IBM deviceSample's cpp & Cmake files --- .../ibm-device/CMakeLists.txt | 25 ++ .../ibm-device/cpp/deviceSample.c | 244 ++++++++++++++++++ 2 files changed, 269 insertions(+) create mode 100644 Tools/IoTConnectionTools/ibm-device/CMakeLists.txt create mode 100644 Tools/IoTConnectionTools/ibm-device/cpp/deviceSample.c diff --git a/Tools/IoTConnectionTools/ibm-device/CMakeLists.txt b/Tools/IoTConnectionTools/ibm-device/CMakeLists.txt new file mode 100644 index 0000000000..9e4c8309cc --- /dev/null +++ b/Tools/IoTConnectionTools/ibm-device/CMakeLists.txt @@ -0,0 +1,25 @@ +cmake_minimum_required (VERSION 3.0) + +project (ibm-device) + +set(CMAKE_CXX_STANDARD "11") + +set(IOT_SDK_FOLDER "$ENV{HOME}/iot-c") + +include_directories( + "${IOT_SDK_FOLDER}/paho.mqtt.c/src" + "${IOT_SDK_FOLDER}/paho.mqtt.c/build/output" + "${IOT_SDK_FOLDER}/build" + "${IOT_SDK_FOLDER}/src/wiotp/sdk" +) + +file(GLOB LIB_SRC ${IOT_SDK_FOLDER}/src/wiotp/sdk/*.c ${IOT_SDK_FOLDER}/src/wiotp/sdk/*.h) + +add_library(iotp STATIC ${LIB_SRC}) + +set(device_sample_files cpp/deviceSample.c) + +add_executable(ibm-device ${device_sample_files}) + +target_link_libraries(ibm-device iotp paho-mqtt3as pthread ssl crypto) + diff --git a/Tools/IoTConnectionTools/ibm-device/cpp/deviceSample.c b/Tools/IoTConnectionTools/ibm-device/cpp/deviceSample.c new file mode 100644 index 0000000000..5769dcd38f --- /dev/null +++ b/Tools/IoTConnectionTools/ibm-device/cpp/deviceSample.c @@ -0,0 +1,244 @@ +/******************************************************************************* + * Copyright (c) 2018 IBM Corp. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Ranjan Dasgupta - Initial drop of deviceSample.c + * + *******************************************************************************/ + +/* + * This sample shows how-to develop a device code using Watson IoT Platform + * iot-c device client library, connect and interact with Watson IoT Platform Service. + * + * This sample includes the function/code snippets to perform the following actions: + * - Initiliaze client library + * - Configure device from configuration parameters specified in a configuration file + * - Set client logging + * - Enable error handling routines + * - Send device events to WIoTP service + * - Receive and process commands from WIoTP service + * + * SYNTAX: + * deviceSample --config + * + * Pre-requisite: + * 1. This sample requires a device configuration file. + * Refer to "Device Confioguration File" section of iot-c docs for details. + * 2. Register type and id (specified in the configuration file) with IBM Watson IoT Platform service. + * + */ + +#include +#include +#include +#include +#include + +/* Include header file of IBM Watson IoT platform C Client for devices */ +#include "iotp_device.h" + +char *configFilePath = NULL; +volatile int interrupt = 0; +char *progname = "deviceSample"; +int useEnv = 0; +int testCycle = 0; + +/* Usage text */ +void usage(void) { + fprintf(stderr, "Usage: %s --config config_file_path\n", progname); + exit(1); +} + +/* Signal handler - to support CTRL-C to quit */ +void sigHandler(int signo) { + signal(SIGINT, NULL); + fprintf(stdout, "Received signal: %d\n", signo); + interrupt = 1; +} + +/* Get and process command line options */ +void getopts(int argc, char** argv) +{ + int count = 1; + + while (count < argc) + { + if (strcmp(argv[count], "--config") == 0) + { + if (++count < argc) + configFilePath = argv[count]; + else + usage(); + } + if (strcmp(argv[count], "--useEnv") == 0) { + useEnv = 1; + } + if (strcmp(argv[count], "--testCycle") == 0) { + if (++count < argc) + testCycle = atoi(argv[count]); + else + usage(); + } + count++; + } +} + +/* + * Device command callback function + * Device developers can customize this function based on their use case + * to handle device commands sent by WIoTP. + * Set this callback function using API setCommandHandler(). + */ +void deviceCommandCallback (char* type, char* id, char* commandName, char *format, void* payload, size_t payloadSize) +{ + fprintf(stdout, "Received device command:\n"); + fprintf(stdout, "Type=%s ID=%s CommandName=%s Format=%s Len=%d\n", + type?type:"", id?id:"", commandName?commandName:"", format?format:"", (int)payloadSize); + fprintf(stdout, "Payload: %s\n", payload?(char *)payload:""); + + /* Device developers - add your custom code to process device commands */ +} + +void logCallback (int level, char * message) +{ + if ( level > 0 ) + fprintf(stdout, "%s\n", message? message:"NULL"); + fflush(stdout); +} + +void MQTTTraceCallback (int level, char * message) +{ + if ( level > 0 ) + fprintf(stdout, "%s\n", message? message:"NULL"); + fflush(stdout); +} + +/* Main program */ +int main(int argc, char *argv[]) +{ + int rc = 0; + int cycle = 0; + + /* + * DEV_NOTES: + * Specifiy variable for WIoT client object + */ + IoTPConfig *config = NULL; + IoTPDevice *device = NULL; + + /* check for args */ + if ( argc < 2 ) + usage(); + + /* Set signal handlers */ + signal(SIGINT, sigHandler); + signal(SIGTERM, sigHandler); + + /* get argument options */ + getopts(argc, argv); + + /* Set IoTP Client log handler */ + rc = IoTPConfig_setLogHandler(IoTPLog_FileDescriptor, stdout); + if ( rc != 0 ) { + fprintf(stderr, "WARN: Failed to set IoTP Client log handler: rc=%d\n", rc); + exit(1); + } + + /* Create IoTPConfig object using configuration options defined in the configuration file. */ + rc = IoTPConfig_create(&config, configFilePath); + if ( rc != 0 ) { + fprintf(stderr, "ERROR: Failed to initialize configuration: rc=%d\n", rc); + exit(1); + } + + /* read additional config from environment */ + if ( useEnv == 1 ) { + IoTPConfig_readEnvironment(config); + } + + /* Create IoTPDevice object */ + rc = IoTPDevice_create(&device, config); + if ( rc != 0 ) { + fprintf(stderr, "ERROR: Failed to configure IoTP device: rc=%d\n", rc); + exit(1); + } + + /* Set MQTT Trace handler */ + rc = IoTPDevice_setMQTTLogHandler(device, &MQTTTraceCallback); + if ( rc != 0 ) { + fprintf(stderr, "WARN: Failed to set MQTT Trace handler: rc=%d\n", rc); + } + + /* Invoke connection API IoTPDevice_connect() to connect to WIoTP. */ + rc = IoTPDevice_connect(device); + if ( rc != 0 ) { + fprintf(stderr, "ERROR: Failed to connect to Watson IoT Platform: rc=%d\n", rc); + fprintf(stderr, "ERROR: Returned error reason: %s\n", IOTPRC_toString(rc)); + exit(1); + } + + /* + * Set device command callback using API IoTPDevice_setCommandsHandler(). + * Refer to deviceCommandCallback() function DEV_NOTES for details on + * how to process device commands received from WIoTP. + */ + IoTPDevice_setCommandsHandler(device, deviceCommandCallback); + + /* + * Invoke device command subscription API IoTPDevice_subscribeToCommands(). + * The arguments for this API are commandName, format, QoS + * If you want to subscribe to all commands of any format, set commandName and format to "+" + */ + char *commandName = "+"; + char *format = "+"; + IoTPDevice_subscribeToCommands(device, commandName, format); + + + /* Use IoTPDevice_sendEvent() API to send device events to Watson IoT Platform. */ + + /* Sample event - this sample device will send this event once in every 10 seconds. */ + char *data = "{\"d\" : {\"SensorID\": \"Test\", \"Reading\": 7 }}"; + + while(!interrupt) + { + fprintf(stdout, "Send status event\n"); + rc = IoTPDevice_sendEvent(device,"status", data, "json", QoS0, NULL); + fprintf(stdout, "RC from publishEvent(): %d\n", rc); + + if ( testCycle > 0 ) { + cycle += 1; + if ( cycle >= testCycle ) { + break; + } + } + sleep(10); + } + + fprintf(stdout, "Publish event cycle is complete.\n"); + + /* Disconnect device */ + rc = IoTPDevice_disconnect(device); + if ( rc != IOTPRC_SUCCESS ) { + fprintf(stderr, "ERROR: Failed to disconnect from Watson IoT Platform: rc=%d\n", rc); + exit(1); + } + + /* Destroy client */ + IoTPDevice_destroy(device); + + /* Clear configuration */ + IoTPConfig_clear(config); + + return 0; + +} + From 384ee61767cb29eba47f81d8c8c6dc0418ff056b Mon Sep 17 00:00:00 2001 From: andreyla Date: Wed, 8 Jul 2020 15:05:14 +0300 Subject: [PATCH 2/8] Create README.md --- Tools/IoTConnectionTools/ibm-device/README.md | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Tools/IoTConnectionTools/ibm-device/README.md diff --git a/Tools/IoTConnectionTools/ibm-device/README.md b/Tools/IoTConnectionTools/ibm-device/README.md new file mode 100644 index 0000000000..de8cf1de9b --- /dev/null +++ b/Tools/IoTConnectionTools/ibm-device/README.md @@ -0,0 +1,23 @@ +# IBM Device + +## Introduction +This is a simple sample you could use for a test of IBM device connection. + +## What it is +This project shows how-to develop a device code using Watson IoT Platform iot-c device client library, connect and interact with Watson IoT Platform Service. + +## Software requirements +This sample is supported on Linux systems only. + +This version of the sample has been tested on Ubuntu Linux. This sample requires additional system configuration when using Ubuntu OS. Instructions on how to install the custom provided all dependency libraries for Linux can be [found here](https://github.com/ibm-watson-iot/iot-c#build-instructions). + +## Setup +By default the directory to install the 'iot-c' library is $ENV{HOME}. Otherwise you should enter valid path to this library as variable IOT_SDK_FOLDER in CMakeLists.txt file. +Configure device on [IBM Watson IoT Platform Page](https://ibm-watson-iot.github.io/iot-c/device/). +Download the configuration file with all the credentials according to [instructions](https://ibm-watson-iot.github.io/iot-c/device/). + +Build the sample executable. Enter the following line to run the sample: +`ibm-device deviceSample --config ` + +## Disclaimer +IMPORTANT NOTICE: This software is sample software. It is not designed or intended for use in any medical, life-saving or life-sustaining systems, transportation systems, nuclear systems, or for any other mission-critical application in which the failure of the system could lead to critical injury or death. The software may not be fully tested and may contain bugs or errors; it may not be intended or suitable for commercial release. No regulatory approvals for the software have been obtained, and therefore software may not be certified for use in certain countries or environments. From f370f2c42805c13b17fb88fd8c38b56eda594242 Mon Sep 17 00:00:00 2001 From: andreyla Date: Wed, 15 Jul 2020 23:42:02 +0300 Subject: [PATCH 3/8] Create sample.json --- Tools/IoTConnectionTools/ibm-device/sample.json | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 Tools/IoTConnectionTools/ibm-device/sample.json diff --git a/Tools/IoTConnectionTools/ibm-device/sample.json b/Tools/IoTConnectionTools/ibm-device/sample.json new file mode 100644 index 0000000000..a9bffa6129 --- /dev/null +++ b/Tools/IoTConnectionTools/ibm-device/sample.json @@ -0,0 +1,8 @@ +{ + "name": "IBM Device", + "categories": ["Toolkit/Intel® oneAPI IoT Toolkit/IoT Connection Tools"], + "description": "This project shows how-to develop a device code using Watson IoT Platform iot-c device client library, connect and interact with Watson IoT Platform Service.", + "dependencies": ["iot-c|https://github.com/ibm-watson-iot/iot-c"], + "languages": [{"cpp":{}}], + "os": ["linux"] +} From 443b5a1687300ecbcb0c1f13f007be4a238d4dd3 Mon Sep 17 00:00:00 2001 From: andreyla Date: Tue, 4 Aug 2020 20:59:59 +0300 Subject: [PATCH 4/8] Info files updated --- .../IoTConnectionTools/ibm-device/License.txt | 8 ++ Tools/IoTConnectionTools/ibm-device/README.md | 79 +++++++++++++++---- 2 files changed, 72 insertions(+), 15 deletions(-) create mode 100644 Tools/IoTConnectionTools/ibm-device/License.txt diff --git a/Tools/IoTConnectionTools/ibm-device/License.txt b/Tools/IoTConnectionTools/ibm-device/License.txt new file mode 100644 index 0000000000..8f608e972a --- /dev/null +++ b/Tools/IoTConnectionTools/ibm-device/License.txt @@ -0,0 +1,8 @@ +Copyright 2019 Intel Corporation + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + diff --git a/Tools/IoTConnectionTools/ibm-device/README.md b/Tools/IoTConnectionTools/ibm-device/README.md index de8cf1de9b..d9728c7153 100644 --- a/Tools/IoTConnectionTools/ibm-device/README.md +++ b/Tools/IoTConnectionTools/ibm-device/README.md @@ -1,23 +1,72 @@ -# IBM Device +# `IBM Device` Sample -## Introduction -This is a simple sample you could use for a test of IBM device connection. +`IBM Device` sample shows how-to develop a device code using Watson IoT Platform iot-c device client library, connect and interact with Watson IoT Platform Service. -## What it is -This project shows how-to develop a device code using Watson IoT Platform iot-c device client library, connect and interact with Watson IoT Platform Service. +| Optimized for | Description +|:--- |:--- +| OS | Linux* Ubuntu* 16.04, Linux* Ubuntu* 18.04, +| Software | Paho MQTT C library, OpenSSL development package +| What you will learn | Use protocol MQTT to send events from a device -## Software requirements -This sample is supported on Linux systems only. -This version of the sample has been tested on Ubuntu Linux. This sample requires additional system configuration when using Ubuntu OS. Instructions on how to install the custom provided all dependency libraries for Linux can be [found here](https://github.com/ibm-watson-iot/iot-c#build-instructions). -## Setup -By default the directory to install the 'iot-c' library is $ENV{HOME}. Otherwise you should enter valid path to this library as variable IOT_SDK_FOLDER in CMakeLists.txt file. -Configure device on [IBM Watson IoT Platform Page](https://ibm-watson-iot.github.io/iot-c/device/). +## Purpose +This is a simple sample you could use for a test of IBM device connection. This project shows how-to develop a device code using Watson IoT Platform iot-c device client library, connect and interact with Watson IoT Platform Service. + +## Key Implementation Details + This sample includes the function/code snippets to perform the following actions: + - Initiliaze client library + - Configure device from configuration parameters specified in a configuration file + - Set client logging + - Enable error handling routines + - Send device events to WIoTP service + - Receive and process commands from WIoTP service + +##License +Code sample uses Eclipse Public License - v1.0 + +##Building the `IBM Device` Sample + +### On a Linux* System + +The detailed instructions on how to install the custom provided all dependency libraries for Linux can be [found here](https://github.com/ibm-watson-iot/iot-c#build-instructions). + +Perform the following steps: +1. Run in the terminal: + ``` + cd $ENV{HOME} + git clone https://github.com/ibm-watson-iot/iot-c.git + cd iot-c + make + sudo make -C paho.mqtt.c install + sudo make install + ``` +> Note! By default the directory to install the 'iot-c' library is $ENV{HOME}. Otherwise you should enter valid path to this library as variable IOT_SDK_FOLDER in CMakeLists.txt file. + +2. Run the following lines from the sample folder 'ibm-device': + ``` + mkdir build + cd build + cmake .. + make all + ``` +3. Run the program using: + ``` + ibm-device deviceSample --config + ``` +4. Clean the program using: + ``` + make clean + ``` +## Running the Sample + +Configure IoT device on [IBM Watson IoT Platform Page](https://ibm-watson-iot.github.io/iot-c/device/). + +### Application Parameters + +The samples uses path to configuration file as a parameter. Download the configuration file with all the credentials according to [instructions](https://ibm-watson-iot.github.io/iot-c/device/). -Build the sample executable. Enter the following line to run the sample: -`ibm-device deviceSample --config ` +### Example of Output -## Disclaimer -IMPORTANT NOTICE: This software is sample software. It is not designed or intended for use in any medical, life-saving or life-sustaining systems, transportation systems, nuclear systems, or for any other mission-critical application in which the failure of the system could lead to critical injury or death. The software may not be fully tested and may contain bugs or errors; it may not be intended or suitable for commercial release. No regulatory approvals for the software have been obtained, and therefore software may not be certified for use in certain countries or environments. +TBD From c34f9a2ace8bc896256f42de481086105fb40f99 Mon Sep 17 00:00:00 2001 From: andreyla Date: Tue, 4 Aug 2020 22:23:52 +0300 Subject: [PATCH 5/8] Update sample.json --- Tools/IoTConnectionTools/ibm-device/sample.json | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Tools/IoTConnectionTools/ibm-device/sample.json b/Tools/IoTConnectionTools/ibm-device/sample.json index a9bffa6129..875c7f338e 100644 --- a/Tools/IoTConnectionTools/ibm-device/sample.json +++ b/Tools/IoTConnectionTools/ibm-device/sample.json @@ -4,5 +4,18 @@ "description": "This project shows how-to develop a device code using Watson IoT Platform iot-c device client library, connect and interact with Watson IoT Platform Service.", "dependencies": ["iot-c|https://github.com/ibm-watson-iot/iot-c"], "languages": [{"cpp":{}}], - "os": ["linux"] + "os": ["linux"], + "ciTests": { + "linux": [ + { "id": "ibm-device", + "env": [], + "steps": [ + "mkdir build", + "cd build", + "cmake ..", + "make" + ] + } + ] + } } From 8c27665cdebb6388ff1677a435e585ac096f2d22 Mon Sep 17 00:00:00 2001 From: andreyla Date: Tue, 18 Aug 2020 18:44:27 +0300 Subject: [PATCH 6/8] Copyright and GUID fixed --- Tools/IoTConnectionTools/ibm-device/License.txt | 2 +- Tools/IoTConnectionTools/ibm-device/sample.json | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Tools/IoTConnectionTools/ibm-device/License.txt b/Tools/IoTConnectionTools/ibm-device/License.txt index 8f608e972a..9cde07f558 100644 --- a/Tools/IoTConnectionTools/ibm-device/License.txt +++ b/Tools/IoTConnectionTools/ibm-device/License.txt @@ -1,4 +1,4 @@ -Copyright 2019 Intel Corporation +Copyright Intel Corporation Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/Tools/IoTConnectionTools/ibm-device/sample.json b/Tools/IoTConnectionTools/ibm-device/sample.json index 875c7f338e..e9891e39b9 100644 --- a/Tools/IoTConnectionTools/ibm-device/sample.json +++ b/Tools/IoTConnectionTools/ibm-device/sample.json @@ -1,4 +1,5 @@ { + "guid": "CF755377-7B46-47D3-89A3-AB624692B2E8" "name": "IBM Device", "categories": ["Toolkit/Intel® oneAPI IoT Toolkit/IoT Connection Tools"], "description": "This project shows how-to develop a device code using Watson IoT Platform iot-c device client library, connect and interact with Watson IoT Platform Service.", From 8e61784483f28793598b6d7f43185db631197ef3 Mon Sep 17 00:00:00 2001 From: andreyla Date: Tue, 18 Aug 2020 19:24:57 +0300 Subject: [PATCH 7/8] Update sample.json --- Tools/IoTConnectionTools/ibm-device/sample.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tools/IoTConnectionTools/ibm-device/sample.json b/Tools/IoTConnectionTools/ibm-device/sample.json index e9891e39b9..26e68ee8fe 100644 --- a/Tools/IoTConnectionTools/ibm-device/sample.json +++ b/Tools/IoTConnectionTools/ibm-device/sample.json @@ -1,5 +1,5 @@ { - "guid": "CF755377-7B46-47D3-89A3-AB624692B2E8" + "guid": "CF755377-7B46-47D3-89A3-AB624692B2E8", "name": "IBM Device", "categories": ["Toolkit/Intel® oneAPI IoT Toolkit/IoT Connection Tools"], "description": "This project shows how-to develop a device code using Watson IoT Platform iot-c device client library, connect and interact with Watson IoT Platform Service.", From 94006ab4df14424cff27b1bf6c03d0a2a8cb5910 Mon Sep 17 00:00:00 2001 From: JoeOster <52936608+JoeOster@users.noreply.github.com> Date: Mon, 24 Aug 2020 08:50:42 -0700 Subject: [PATCH 8/8] Update README.md --- Tools/IoTConnectionTools/ibm-device/README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/Tools/IoTConnectionTools/ibm-device/README.md b/Tools/IoTConnectionTools/ibm-device/README.md index d9728c7153..f1168423aa 100644 --- a/Tools/IoTConnectionTools/ibm-device/README.md +++ b/Tools/IoTConnectionTools/ibm-device/README.md @@ -8,8 +8,6 @@ | Software | Paho MQTT C library, OpenSSL development package | What you will learn | Use protocol MQTT to send events from a device - - ## Purpose This is a simple sample you could use for a test of IBM device connection. This project shows how-to develop a device code using Watson IoT Platform iot-c device client library, connect and interact with Watson IoT Platform Service.