From dbd097b9065bebf4a93215bb984874317353abfb Mon Sep 17 00:00:00 2001 From: Heejin Kim Date: Fri, 22 Sep 2017 23:32:34 +0900 Subject: [PATCH] Add st_things example Example files for st_things. --- apps/examples/st_things/Kconfig | 41 +++++++ apps/examples/st_things/Make.defs | 21 ++++ apps/examples/st_things/Makefile | 122 ++++++++++++++++++++ apps/examples/st_things/README.md | 17 +++ apps/examples/st_things/contents/sampleDevice.json | 78 +++++++++++++ apps/examples/st_things/sample_light_things.c | 126 +++++++++++++++++++++ apps/examples/st_things/sample_light_user.c | 72 ++++++++++++ apps/examples/st_things/st_things_sample.h | 28 +++++ apps/examples/st_things/st_things_sample_main.c | 55 +++++++++ 9 files changed, 560 insertions(+) create mode 100644 apps/examples/st_things/Kconfig create mode 100644 apps/examples/st_things/Make.defs create mode 100644 apps/examples/st_things/Makefile create mode 100644 apps/examples/st_things/README.md create mode 100644 apps/examples/st_things/contents/sampleDevice.json create mode 100644 apps/examples/st_things/sample_light_things.c create mode 100644 apps/examples/st_things/sample_light_user.c create mode 100644 apps/examples/st_things/st_things_sample.h create mode 100644 apps/examples/st_things/st_things_sample_main.c diff --git a/apps/examples/st_things/Kconfig b/apps/examples/st_things/Kconfig new file mode 100644 index 0000000..33481ec --- /dev/null +++ b/apps/examples/st_things/Kconfig @@ -0,0 +1,41 @@ +# +# For a description of the syntax of this configuration file, +# see kconfig-language at https://www.kernel.org/doc/Documentation/kbuild/kconfig-language.txt +# + +config EXAMPLES_ST_THINGS + bool "SmartThings Things example" + default n + ---help--- + Enable the SmartThings Things example + +if EXAMPLES_ST_THINGS + +config EXAMPLES_THINGSEASYSETUP_PROGNAME + string "Program name" + default "st_things_sample" + depends on BUILD_KERNEL + ---help--- + This is the name of the program that will be use when the NSH ELF + program is installed. + +config RESET_BUTTON + bool "Reset_Button" + default y + depends on EXAMPLES_ST_THINGS + ---help--- + Decide whether to use the button to reset the connection on your board. + +config RESET_BUTTON_PIN_NUMBER + int "Pin Number" + default 44 + depends on RESET_BUTTON + ---help--- + The default is the reset button pin number 44 for the ARTIK 053. + If you use another board, you can set the pin number for the board. + +endif + +config USER_ENTRYPOINT + string + default "st_things_sample_main" if EXAMPLES_ST_THINGS diff --git a/apps/examples/st_things/Make.defs b/apps/examples/st_things/Make.defs new file mode 100644 index 0000000..728df9d --- /dev/null +++ b/apps/examples/st_things/Make.defs @@ -0,0 +1,21 @@ +########################################################################### +# +# Copyright 2017 Samsung Electronics All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, +# either express or implied. See the License for the specific +# language governing permissions and limitations under the License. +# +########################################################################### + +ifeq ($(CONFIG_EXAMPLES_ST_THINGS),y) +CONFIGURED_APPS += examples/st_things +endif diff --git a/apps/examples/st_things/Makefile b/apps/examples/st_things/Makefile new file mode 100644 index 0000000..13457bc --- /dev/null +++ b/apps/examples/st_things/Makefile @@ -0,0 +1,122 @@ +########################################################################### +# +# Copyright 2017 Samsung Electronics All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, +# either express or implied. See the License for the specific +# language governing permissions and limitations under the License. +# +########################################################################### + +-include $(TOPDIR)/.config +-include $(TOPDIR)/Make.defs +include $(APPDIR)/Make.defs + +# built-in application info + +APPNAME = st_things_sample +FUNCNAME = st_things_sample_main +THREADEXEC = TASH_EXECMD_SYNC + +# SmartThings Things example + +ASRCS = +CSRCS = sample_light_user.c sample_light_things.c +MAINSRC = st_things_sample_main.c + +AOBJS = $(ASRCS:.S=$(OBJEXT)) +COBJS = $(CSRCS:.c=$(OBJEXT)) +MAINOBJ = $(MAINSRC:.c=$(OBJEXT)) + +SRCS = $(ASRCS) $(CSRCS) $(MAINSRC) +OBJS = $(AOBJS) $(COBJS) + +ifneq ($(CONFIG_BUILD_KERNEL),y) + OBJS += $(MAINOBJ) +endif + +ifeq ($(CONFIG_WINDOWS_NATIVE),y) + BIN = ..\..\libapps$(LIBEXT) +else +ifeq ($(WINTOOL),y) + BIN = ..\\..\\libapps$(LIBEXT) +else + BIN = ../../libapps$(LIBEXT) +endif +endif + +ifeq ($(WINTOOL),y) + INSTALL_DIR = "${shell cygpath -w $(BIN_DIR)}" +else + INSTALL_DIR = $(BIN_DIR) +endif + +CONFIG_EXAMPLES_ST_THINGS_PROGNAME ?= st_things$(EXEEXT) +PROGNAME = $(CONFIG_EXAMPLES_ST_THINGS_PROGNAME) + +ROOTDEPPATH = --dep-path . + +# Common build + +all: .built +.PHONY: clean depend distclean + +$(AOBJS): %$(OBJEXT): %.S + $(call ASSEMBLE, $<, $@) + +$(COBJS) $(MAINOBJ): %$(OBJEXT): %.c + $(call COMPILE, $<, $@) + +.built: $(OBJS) + $(call ARCHIVE, $(BIN), $(OBJS)) + @touch .built + +ifeq ($(CONFIG_BUILD_KERNEL),y) +$(BIN_DIR)$(DELIM)$(PROGNAME): $(OBJS) $(MAINOBJ) + @echo "LD: $(PROGNAME)" + $(Q) $(LD) $(LDELFFLAGS) $(LDLIBPATH) -o $(INSTALL_DIR)$(DELIM)$(PROGNAME) $(ARCHCRT0OBJ) $(MAINOBJ) $(LDLIBS) + $(Q) $(NM) -u $(INSTALL_DIR)$(DELIM)$(PROGNAME) + +install: $(BIN_DIR)$(DELIM)$(PROGNAME) + +else +install: + +endif + +ifeq ($(CONFIG_BUILTIN_APPS)$(CONFIG_EXAMPLES_ST_THINGS),yy) +$(BUILTIN_REGISTRY)$(DELIM)$(FUNCNAME).bdat: $(DEPCONFIG) Makefile + $(call REGISTER,$(APPNAME),$(FUNCNAME),$(THREADEXEC)) + +context: $(BUILTIN_REGISTRY)$(DELIM)$(FUNCNAME).bdat + +else +context: + +endif + +.depend: Makefile $(SRCS) + @$(MKDEP) $(ROOTDEPPATH) "$(CC)" -- $(CFLAGS) -- $(SRCS) >Make.dep + @touch $@ + +depend: .depend + +clean: + $(call DELFILE, .built) + $(call CLEAN) + +distclean: clean + $(call DELFILE, Make.dep) + $(call DELFILE, .depend) + +-include Make.dep +.PHONY: preconfig +preconfig: diff --git a/apps/examples/st_things/README.md b/apps/examples/st_things/README.md new file mode 100644 index 0000000..7e18102 --- /dev/null +++ b/apps/examples/st_things/README.md @@ -0,0 +1,17 @@ +# st_things sample + +This is an example for a st_things. + +## How to build +Configure the build from $TIZENRT_BASEDIR/os/tools directory. +```bash +./configure.sh artik053/st_things +cd .. +make +``` + +## Download +Add 'privatekey', 'certificate', and json file(ex. sampleDevice.json) to $TIZENRT_BASEDIR/tools/fs/contents. +```bash +TIZENRT_BASEDIR/os$ make download ALL +``` diff --git a/apps/examples/st_things/contents/sampleDevice.json b/apps/examples/st_things/contents/sampleDevice.json new file mode 100644 index 0000000..ada6138 --- /dev/null +++ b/apps/examples/st_things/contents/sampleDevice.json @@ -0,0 +1,78 @@ +{ + "device": [ + { + "specification": { + "device": { + "deviceType": "oic.d.light", + "deviceName": "Samsung Lamp", + "specVersion": "core.1.1.0", + "dataModelVersion": "res.1.1.0" + }, + "platform": { + "manufacturerName": "Samsung Electronics", + "manufacturerUrl": "http://www.samsung.com/sec/", + "manufacturingDate": "2017-08-31", + "modelNumber": "NWSP-01", + "platformVersion": "1.0", + "osVersion": "1.0", + "hardwareVersion": "1.0", + "firmwareVersion": "1.0", + "vendorId": "SWC-LIGHT-2017" + } + }, + + "resources": { + "single": [ + { + "uri": "/switch", + "types": [ + "oic.r.switch.binary" + ], + "interfaces": [ + "oic.if.a", + "oic.if.baseline" + ], + "policy": 3 + } + ] + } + } + ], + + "resourceTypes": [ + { + "type": "oic.r.switch.binary", + "properties": [ + { + "key": "value", + "type": 0, + "mandatory": true, + "rw": 3 + } + ] + } ], + "configuration": { + "easySetup": { + "connectivity": { + "type": 1, + "softAP": { + "manufacturerId": "AAA1", + "setupId": "001", + "artik": false + } + }, + "ownershipTransferMethod": 2 + }, + "wifi": { + "interfaces": 15, + "frequency": 1 + }, + "filePath":{ + "svrdb": "/mnt/artikserversecured.dat", + "provisioning": "/mnt/provisioning.dat", + "certificate": "/rom/certificate", + "privateKey": "/rom/privatekey" + } + } +} + diff --git a/apps/examples/st_things/sample_light_things.c b/apps/examples/st_things/sample_light_things.c new file mode 100644 index 0000000..65dbdfe --- /dev/null +++ b/apps/examples/st_things/sample_light_things.c @@ -0,0 +1,126 @@ +/* **************************************************************** + * + * Copyright 2017 Samsung Electronics All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ + +#include +#include +#include +#include +#include +#include + +#define TAG "LIGHT_THINGS" + +static const char *g_res_switch = "/switch"; + +extern bool handle_get_request_on_switch(st_things_get_request_message_s * req_msg, st_things_representation_s * resp_rep); +extern bool handle_set_request_on_switch(st_things_set_request_message_s * req_msg, st_things_representation_s * resp_rep); + +#ifdef CONFIG_RESET_BUTTON +static bool check_reset_button_pin_number(void) +{ + printf("Current pin number : %d\n", CONFIG_RESET_BUTTON_PIN_NUMBER); + return CONFIG_RESET_BUTTON_PIN_NUMBER >= 0 ? true : false; +} + +static void gpio_callback_event(void *user_data) +{ + printf("gpio_callback_event!!\n"); + printf("reset :: %d\n", st_things_reset()); +} +#endif + +static void handle_reset_result(bool result) +{ + printf("[%s]Reset %s.\n", result ? "succeeded" : "failed", TAG); +} + +static void handle_things_status_change(st_things_status_e things_status) +{ + printf("[%s]Things status is changed: %d\n", TAG, things_status); +} + +static bool handle_reset_request(void) +{ + printf("[%s]Received a request for RESET.\n", TAG); + return true; +} + +static bool handle_ownership_transfer_request(void) +{ + printf("[%s]Received a request for Ownership-transfer. \n", TAG); + return true; +} + +static bool handle_get_request(st_things_get_request_message_s * req_msg, st_things_representation_s * resp_rep) +{ + bool ret = false; + + printf("Received a GET request on %s\n", req_msg->resource_uri); + + if (0 == strncmp(req_msg->resource_uri, g_res_switch, strlen(g_res_switch))) { + ret = handle_get_request_on_switch(req_msg, resp_rep); + } else { + printf("Not supported uri.\n"); + } + + return ret; +} + +static bool handle_set_request(st_things_set_request_message_s * req_msg, st_things_representation_s * resp_rep) +{ + printf("Received a SET request on %s\n", req_msg->resource_uri); + + if (0 == strncmp(req_msg->resource_uri, g_res_switch, strlen(g_res_switch))) { + handle_set_request_on_switch(req_msg, resp_rep); + return true; + } else { + return false; + } + + return true; +} + +int ess_process(void) +{ +#ifdef CONFIG_RESET_BUTTON + if (!check_reset_button_pin_number()) { + printf("Error : Invalid pin number.\n"); + return 0; + } + + iotbus_gpio_context_h m_gpio = iotbus_gpio_open(CONFIG_RESET_BUTTON_PIN_NUMBER); + iotbus_gpio_register_cb(m_gpio, IOTBUS_GPIO_EDGE_RISING, gpio_callback_event, (void *)m_gpio); +#endif + + bool easysetup_complete = false; + st_things_initialize("/rom/sampleDevice.json", &easysetup_complete); + iotapi_initialize(); + + st_things_register_request_cb(handle_get_request, handle_set_request); + st_things_register_reset_cb(handle_reset_request, handle_reset_result); + st_things_register_user_confirm_cb(handle_ownership_transfer_request); + st_things_register_things_status_change_cb(handle_things_status_change); + + st_things_start(); + + printf("[%s]=====================================================\n", TAG); + printf("[%s] Stack Started \n", TAG); + printf("[%s]=====================================================\n", TAG); + + return 0; +} diff --git a/apps/examples/st_things/sample_light_user.c b/apps/examples/st_things/sample_light_user.c new file mode 100644 index 0000000..fcda4e0 --- /dev/null +++ b/apps/examples/st_things/sample_light_user.c @@ -0,0 +1,72 @@ +/* **************************************************************** +* +* Copyright 2017 Samsung Electronics All Rights Reserved. +* +* +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* +******************************************************************/ + +#include +#include +#include +#include +#include + +#define TAG "LIGHT_USER" + +static const char *PROPERTY_VALUE = "value"; + +static bool g_switch_value = true; + +bool handle_get_request_on_switch(st_things_get_request_message_s *req_msg, st_things_representation_s *resp_rep) +{ + printf("[%s]IN-handle_get_request_on_switch() called..\n", TAG); + + if (req_msg->has_property_key(req_msg, PROPERTY_VALUE)) { + printf("[%s]current switch value: %d\n", TAG, g_switch_value); + resp_rep->set_bool_value(resp_rep, PROPERTY_VALUE, g_switch_value); + } + + printf("[%s]OUT-handle_get_request_on_switch() called..\n", TAG); + return true; +} + +bool handle_set_request_on_switch(st_things_set_request_message_s *req_msg, st_things_representation_s *resp_rep) +{ + printf("[%s]IN-handle_set_request_on_switch() called..\n", TAG); + + bool is_value_changed = false; + + bool desired_value = false; + if (req_msg->rep->get_bool_value(req_msg->rep, PROPERTY_VALUE, &desired_value)) { + printf("[%s]current switch value: %d\n", TAG, g_switch_value); + printf("[%s]desired switch value: %d\n", TAG, desired_value); + + if (g_switch_value != desired_value) { + g_switch_value = desired_value; + printf("[%s]changed switch value: %d\n", TAG, g_switch_value); + is_value_changed = true; + } + + resp_rep->set_bool_value(resp_rep, PROPERTY_VALUE, g_switch_value); + } + + if (is_value_changed) { + st_things_notify_observers(req_msg->resource_uri); + } + + printf("[%s]OUT-handle_set_request_on_switch() called..\n", TAG); + return true; +} diff --git a/apps/examples/st_things/st_things_sample.h b/apps/examples/st_things/st_things_sample.h new file mode 100644 index 0000000..23eaa6f --- /dev/null +++ b/apps/examples/st_things/st_things_sample.h @@ -0,0 +1,28 @@ +/* **************************************************************** +* +* Copyright 2017 Samsung Electronics All Rights Reserved. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* +******************************************************************/ + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +int ess_process(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + diff --git a/apps/examples/st_things/st_things_sample_main.c b/apps/examples/st_things/st_things_sample_main.c new file mode 100644 index 0000000..ac9a4cd --- /dev/null +++ b/apps/examples/st_things/st_things_sample_main.c @@ -0,0 +1,55 @@ +/* **************************************************************** +* +* Copyright 2017 Samsung Electronics All Rights Reserved. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* +******************************************************************/ + +#include + +#include +#include +#include +#include +#include "st_things_sample.h" + +int time_set(unsigned int second, unsigned int microsecond) +{ + struct timespec current_time; + + current_time.tv_sec = second; + current_time.tv_nsec = (long int)microsecond * 1000; + + if (clock_settime(CLOCK_REALTIME, ¤t_time) != 0) { + return 0; + } + + return 1; +} + +#ifdef CONFIG_BUILD_KERNEL +int main(int argc, FAR char *argv[]) +#else +int st_things_sample_main(int argc, char *argv[]) +#endif +{ + printf("st_things_sample!!\n"); + + // set system default time (2017/01/01 0:0:0) + time_set(1495089694, 0); + + ess_process(); + + return 0; +} -- 2.7.4