From: BoYeoN SoN Date: Sat, 21 Sep 2019 02:24:25 +0000 (+0900) Subject: Init master branch with timer code X-Git-Url: http://review.tizen.org/git/?p=apps%2Fnative%2Fst-things-light.git;a=commitdiff_plain;h=HEAD Init master branch with timer code Change-Id: I933880fde7fd667f43876d45bc905743eafdb773 --- diff --git a/.cproject b/.cproject new file mode 100644 index 0000000..65556d0 --- /dev/null +++ b/.cproject @@ -0,0 +1,643 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.exportMap b/.exportMap new file mode 100755 index 0000000..de30516 --- /dev/null +++ b/.exportMap @@ -0,0 +1,5 @@ +{ + global: main; + _IO_*; + local: *; +}; diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5f489ce --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +/Debug/ +/SA_Report/ +/.sign/ +/res/certificate.pem +/res/privatekey.der +/crash-info/ +/.package-stamp +/res/ diff --git a/.project b/.project new file mode 100644 index 0000000..1bffd91 --- /dev/null +++ b/.project @@ -0,0 +1,52 @@ + + + smart-light-basic-timer + + + + + + org.tizen.nativecore.apichecker.apicheckerbuilder + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.core.ccnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + org.tizen.nativecore.apichecker.apicheckernature + + + + 0 + + 26 + + org.eclipse.ui.ide.multiFilter + 1.0-projectRelativePath-matches-false-false-*/.tpk + + + + 0 + + 6 + + org.eclipse.ui.ide.multiFilter + 1.0-name-matches-false-false-project_def.prop + + + + diff --git a/.tproject b/.tproject new file mode 100644 index 0000000..7bd616a --- /dev/null +++ b/.tproject @@ -0,0 +1,12 @@ + + + + + iot-headless-5.0 + + + + + + + diff --git a/LICENSE.APLv2 b/LICENSE.APLv2 new file mode 100755 index 0000000..d645695 --- /dev/null +++ b/LICENSE.APLv2 @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + 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. diff --git a/NOTICE b/NOTICE new file mode 100755 index 0000000..ba98e27 --- /dev/null +++ b/NOTICE @@ -0,0 +1,3 @@ +Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved. +Except as noted, this software is licensed under Apache License, Version 2. +Please, see the LICENSE.APLv2 file for Apache License, Version 2 terms and conditions. diff --git a/basic-interrupted b/basic-interrupted deleted file mode 160000 index 71be3d8..0000000 --- a/basic-interrupted +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 71be3d8daf587a2a5221dcda7d16227577bd8578 diff --git a/basic-timer b/basic-timer deleted file mode 160000 index 9860e2f..0000000 --- a/basic-timer +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 9860e2f54df01e11ed05c38d50a573a4e55a176a diff --git a/inc/log.h b/inc/log.h new file mode 100644 index 0000000..e9e2b41 --- /dev/null +++ b/inc/log.h @@ -0,0 +1,97 @@ +/* **************************************************************** + * + * 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. + * + ******************************************************************/ +#ifndef __LOG_H__ +#define __LOG_H__ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef LOG_TAG +#undef LOG_TAG +#endif +#define LOG_TAG "SMART_LIGHT" + +#define _E(fmt, args...) dlog_print(DLOG_ERROR, LOG_TAG, "%s : %s(%d) > "fmt"\n", rindex(__FILE__, '/') + 1, __func__, __LINE__, ##args) +#define _W(fmt, args...) dlog_print(DLOG_WARN, LOG_TAG, "%s : %s(%d) > "fmt"\n", rindex(__FILE__, '/') + 1, __func__, __LINE__, ##args) +#define _I(fmt, args...) dlog_print(DLOG_INFO, LOG_TAG, "%s : %s(%d) > "fmt"\n", rindex(__FILE__, '/') + 1, __func__, __LINE__, ##args) +#define _D(fmt, args...) dlog_print(DLOG_DEBUG, LOG_TAG, "%s : %s(%d) > "fmt"\n", rindex(__FILE__, '/') + 1, __func__, __LINE__, ##args) + +#define FN_CALL dlog_print(DLOG_DEBUG, LOG_TAG, ">>>>>>>> %s() called\n", __func__) +#define FN_END dlog_print(DLOG_DEBUG, LOG_TAG, "<<<<<<<< %s() ended\n", __func__) + + +#define retvm_if(expr, val, fmt, arg...) do { \ + if (expr) { \ + _E(fmt, ##arg); \ + _E("(%s) -> %s() return", #expr, __FUNCTION__); \ + return val; \ + } \ +} while (0) + +#define retv_if(expr, val) do { \ + if (expr) { \ + _E("(%s) -> %s() return", #expr, __FUNCTION__); \ + return (val); \ + } \ +} while (0) + +#define retm_if(expr, fmt, arg...) do { \ + if (expr) { \ + _E(fmt, ##arg); \ + _E("(%s) -> %s() return", #expr, __FUNCTION__); \ + return; \ + } \ +} while (0) + +#define ret_if(expr) do { \ + if (expr) { \ + _E("(%s) -> %s() return", #expr, __FUNCTION__); \ + return; \ + } \ +} while (0) + +#define goto_if(expr, val) do { \ + if (expr) { \ + _E("(%s) -> goto", #expr); \ + goto val; \ + } \ +} while (0) + +#define break_if(expr) { \ + if (expr) { \ + _E("(%s) -> break", #expr); \ + break; \ + } \ +} + +#define continue_if(expr) { \ + if (expr) { \ + _E("(%s) -> continue", #expr); \ + continue; \ + } \ +} + +#ifdef __cplusplus +} +#endif + +#endif /* __LOG_H__ */ + diff --git a/inc/resource/resource_infrared_motion.h b/inc/resource/resource_infrared_motion.h new file mode 100755 index 0000000..115c634 --- /dev/null +++ b/inc/resource/resource_infrared_motion.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. + * + * 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. + */ + +#ifndef __RESOURCE_INFRARED_MOTION_H__ +#define __RESOURCE_INFRARED_MOTION_H__ + +typedef void (*resource_infrared_motion_interrupted_cb) (uint32_t motion_value, void *user_data); + +/** + * @brief Reads value of gpio connected infrared motion sensor (HC-SR501) + * @param[in] pin_num The gpio pin number for the infrared motion sensor + * @param[out] out_value The value of the gpio (zero or non-zero) + * @return 0 on success, otherwise a negative error value + * @see If the gpio pin is not open, create gpio handle before reading the value + */ +extern int resource_read_infrared_motion(int pin_num, uint32_t *out_value); + +/** + * @brief Releases the gpio handle + */ +extern void resource_close_infrared_motion(void); + +/** + * @brief Sets an interrupted callback to be invoked when interrupt is triggered on motion sensor + * @param[in] pin_num The gpio pin number for the infrared motion sensor + * @param[in] interrupted_cb interrupted user callback to be invoked + * @return 0 on success, otherwise a negative error value + * @see If the gpio pin is not open, create gpio handle before reading the value + */ +extern int resource_set_interrupted_cb_infrared_motion(int pin_num, resource_infrared_motion_interrupted_cb interrupted_cb, void *user_data); + +/** + * @brief Unsets the interrupted callback for motion sensor + * @param[in] pin_num The gpio pin number for the infrared motion sensor + */ +extern int resource_unset_interrupted_cb_infrared_motion(int pin_num); + +#endif /* __RESOURCE_INFRARED_MOTION_H__ */ diff --git a/inc/resource/resource_led.h b/inc/resource/resource_led.h new file mode 100755 index 0000000..4adf22f --- /dev/null +++ b/inc/resource/resource_led.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. + * + * 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. + */ + +#ifndef __RESOURCE_LED_H__ +#define __RESOURCE_LED_H__ + +/** + * @brief Writes value of gpio connected led light + * @param[in] pin_num The gpio pin number for the led light + * @param[out] out_value The value of turniing the led light on/off + * @return 0 on success, otherwise a negative error value + * @see If the gpio pin is not open, create gpio handle before writing the value + */ +extern int resource_write_led(int pin_num, int write_value); + +/** + * @brief Releases the gpio handle + */ +extern void resource_close_led(void); + +#endif /* __RESOURCE_LED_H__ */ diff --git a/lib/libst_thing_master_api.so b/lib/libst_thing_master_api.so new file mode 100644 index 0000000..aef594a Binary files /dev/null and b/lib/libst_thing_master_api.so differ diff --git a/lib/libst_thing_resource_api.so b/lib/libst_thing_resource_api.so new file mode 100644 index 0000000..245093f Binary files /dev/null and b/lib/libst_thing_resource_api.so differ diff --git a/project_def.prop b/project_def.prop new file mode 100644 index 0000000..b6a64c2 --- /dev/null +++ b/project_def.prop @@ -0,0 +1,11 @@ +APPNAME = smart-light + +type = app +profile = iot-headed-5.0 + +USER_SRCS = src/smart-light.c +USER_DEFS = +USER_INC_DIRS = inc +USER_OBJS = +USER_LIBS = lib/libst_thing_master_api.so, lib/libst_thing_resource_api.so +USER_EDCS = diff --git a/shared/res/icon.png b/shared/res/icon.png new file mode 100644 index 0000000..9765b1b Binary files /dev/null and b/shared/res/icon.png differ diff --git a/shared/res/master.json b/shared/res/master.json new file mode 100644 index 0000000..7d27d0f --- /dev/null +++ b/shared/res/master.json @@ -0,0 +1,18 @@ +{ + "devices": [ + { + "deviceType": "oic.d.light", + "deviceName": "***Your Device Name***", + "mnid": "***Your MNID***", + "vid": "***Your Vendor ID***" + } + ], + "configuration": { + "easySetup": { + "mode": 1, + "setupId": "***Your Setup ID***" + }, + "otm": 2, + "crtType": 1 + } +} \ No newline at end of file diff --git a/shared/res/resource.json b/shared/res/resource.json new file mode 100644 index 0000000..14d7ba5 --- /dev/null +++ b/shared/res/resource.json @@ -0,0 +1,52 @@ +{ + "resources": { + "single": [ + { + "uri": "/capability/motionSensor/main/0", + "types": [ + "oic.r.sensor.motion" + ], + "interfaces": [ + "oic.if.s", + "oic.if.baseline" + ] + }, + { + "uri": "/capability/switch/main/0", + "types": [ + "x.com.st.powerswitch" + ], + "interfaces": [ + "oic.if.a", + "oic.if.baseline" + ] + } + ] + }, + "resourceTypes": [ + { + "type": "oic.r.sensor.motion", + "properties": [ + { + "key": "value", + "type": "boolean", + "readOnly": 1, + "mandatory": false, + "isArray": false + } + ] + }, + { + "type": "x.com.st.powerswitch", + "properties": [ + { + "key": "power", + "type": "string", + "readOnly": 3, + "mandatory": true, + "isArray": false + } + ] + } + ] +} \ No newline at end of file diff --git a/src/controller.c b/src/controller.c new file mode 100755 index 0000000..6595ea2 --- /dev/null +++ b/src/controller.c @@ -0,0 +1,122 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. + * + * 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 + +#include "log.h" +#include "resource/resource_infrared_motion.h" +#include "resource/resource_led.h" + +// Timer duration +#define TIMER_GATHER_INTERVAL (2.0f) + +// Motion sensor information +#define SENSOR_MOTION_GPIO_NUMBER (18) + +// LED sensor information +#define SENSOR_LED_GPIO_NUMBER (24) + +Ecore_Timer *getter_timer = NULL; + +static int _change_led_state(int state) { + int ret = 0; + + // Write state to LED light + ret = resource_write_led(SENSOR_LED_GPIO_NUMBER, state); + if (ret != 0) { + _E("cannot write led data"); + return -1; + } + + _I("LED State : %s", state ? "ON":"OFF"); + + return 0; +} + +static Eina_Bool _get_motion_set_led(void *user_data) +{ + int ret = 0; + uint32_t value = 0; + + // Get value from motion sensor + ret = resource_read_infrared_motion(SENSOR_MOTION_GPIO_NUMBER, &value); + if (ret != 0) { + _E("cannot read data from the infrared motion sensor"); + return ECORE_CALLBACK_CANCEL; + } + + _I("Detected motion value : %u", value); + + // Set LED light with value + _change_led_state(value); + + return ECORE_CALLBACK_RENEW; +} + +static bool service_app_create(void *user_data) +{ + return true; +} + +static void service_app_control(app_control_h app_control, void *user_data) +{ + // Delete old timer if there is one + if (getter_timer) { + ecore_timer_del(getter_timer); + getter_timer = NULL; + } + + // Create a timer to call the given function in given period of time + getter_timer = ecore_timer_add(TIMER_GATHER_INTERVAL, _get_motion_set_led, user_data); + if (!getter_timer) { + _E("Failed to add getter timer"); + return; + } +} + +static void service_app_terminate(void *user_data) +{ + // Delete timer + if (getter_timer) { + ecore_timer_del(getter_timer); + getter_timer = NULL; + } + + // Turn off LED light with __set_led() + _change_led_state(0); + + // Close Motion and LED resources + resource_close_infrared_motion(); + resource_close_led(); + + FN_END; +} + +int main(int argc, char *argv[]) +{ + service_app_lifecycle_callback_s event_callback; + + event_callback.create = service_app_create; + event_callback.terminate = service_app_terminate; + event_callback.app_control = service_app_control; + + return service_app_main(argc, argv, &event_callback, NULL); +} diff --git a/src/resource/resource_infrared_motion.c b/src/resource/resource_infrared_motion.c new file mode 100755 index 0000000..9263143 --- /dev/null +++ b/src/resource/resource_infrared_motion.c @@ -0,0 +1,195 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. + * + * 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 "resource/resource_infrared_motion.h" +#include "log.h" + +enum { + MOTION_HANDLE_ERROR_NONE = 0, /**< Successful */ + MOTION_HANDLE_ERROR_NOT_OPEN, + MOTION_HANDLE_ERROR_INVALID_PIN +} motion_handle_error_e; + +struct resource_im_interrupted_data { + resource_infrared_motion_interrupted_cb interrupted_cb; + void *interrupted_cb_user_data; + uint32_t motion_value; +}; + +static peripheral_gpio_h g_sensor_h = NULL; +static int g_pin_num = -1; +static struct resource_im_interrupted_data g_interrupted_data; + +static int _resource_validate_infrared_motion(int pin_num) +{ + int ret = MOTION_HANDLE_ERROR_NONE; + + if (!g_sensor_h) + { + ret = MOTION_HANDLE_ERROR_NOT_OPEN; + } else if (g_pin_num != pin_num) { + ret = MOTION_HANDLE_ERROR_INVALID_PIN; + } + + return ret; +} + +static int resource_open_infrared_motion(int pin_num) +{ + int ret = PERIPHERAL_ERROR_NONE; + peripheral_gpio_h temp = NULL; + + ret = peripheral_gpio_open(pin_num, &temp); + if (ret != PERIPHERAL_ERROR_NONE) { + _E("peripheral_gpio_open failed."); + return -1; + } + + // It should be removed after peripheral-io is patched. + peripheral_gpio_set_direction(temp, PERIPHERAL_GPIO_DIRECTION_OUT_INITIALLY_LOW); + if (ret) { + peripheral_gpio_close(temp); + _E("peripheral_gpio_set_direction failed."); + return -1; + } + + ret = peripheral_gpio_set_direction(temp, PERIPHERAL_GPIO_DIRECTION_IN); + if (ret != PERIPHERAL_ERROR_NONE) { + peripheral_gpio_close(temp); + _E("peripheral_gpio_set_direction failed."); + return -1; + } + + g_sensor_h = temp; + g_pin_num = pin_num; + + return 0; +} + +int resource_read_infrared_motion(int pin_num, uint32_t *out_value) +{ + int ret = PERIPHERAL_ERROR_NONE; + int ret_valid = MOTION_HANDLE_ERROR_NONE; + + ret_valid = _resource_validate_infrared_motion(pin_num); + if (ret_valid != MOTION_HANDLE_ERROR_NONE) { + if (ret_valid == MOTION_HANDLE_ERROR_NOT_OPEN) { + ret = resource_open_infrared_motion(pin_num); + retv_if(ret != PERIPHERAL_ERROR_NONE, -1); + } else if (ret_valid == MOTION_HANDLE_ERROR_INVALID_PIN) { + _E("Invalid pin number."); + return -1; + } + } + + ret = peripheral_gpio_read(g_sensor_h, out_value); + retv_if(ret != PERIPHERAL_ERROR_NONE, -1); + + return 0; +} + +void resource_close_infrared_motion(void) +{ + if (!g_sensor_h) return; + + _I("Infrared Motion Sensor is finishing..."); + + if (g_interrupted_data.interrupted_cb) { + peripheral_gpio_unset_interrupted_cb(g_sensor_h); + g_interrupted_data.interrupted_cb = NULL; + g_interrupted_data.interrupted_cb_user_data = NULL; + g_interrupted_data.motion_value = 0; + } + + peripheral_gpio_close(g_sensor_h); + + g_sensor_h = NULL; + g_pin_num = -1; +} + +void _resoucre_motion_interrupted_cb (peripheral_gpio_h gpio, peripheral_error_e error, void *user_data) +{ + if (!g_sensor_h) return; + + g_interrupted_data.motion_value = !g_interrupted_data.motion_value; + + g_interrupted_data.interrupted_cb(g_interrupted_data.motion_value, g_interrupted_data.interrupted_cb_user_data); +} + +int resource_set_interrupted_cb_infrared_motion(int pin_num, resource_infrared_motion_interrupted_cb interrupted_cb, void *user_data) +{ + int ret = PERIPHERAL_ERROR_NONE; + int ret_valid = MOTION_HANDLE_ERROR_NONE; + + retv_if(!interrupted_cb, -1); + + ret_valid = _resource_validate_infrared_motion(pin_num); + if (ret_valid == MOTION_HANDLE_ERROR_NOT_OPEN) { + ret = resource_open_infrared_motion(pin_num); + retv_if(ret != PERIPHERAL_ERROR_NONE, -1); + } else if (ret_valid == MOTION_HANDLE_ERROR_INVALID_PIN) { + _E("Invalid pin number."); + return -1; + } + + ret = peripheral_gpio_set_edge_mode(g_sensor_h, PERIPHERAL_GPIO_EDGE_BOTH); + retv_if(ret != PERIPHERAL_ERROR_NONE, -1); + + g_interrupted_data.motion_value = 0; + ret = peripheral_gpio_read(g_sensor_h, &g_interrupted_data.motion_value); + retv_if(ret != PERIPHERAL_ERROR_NONE, -1); + + ret = peripheral_gpio_set_interrupted_cb(g_sensor_h, _resoucre_motion_interrupted_cb, &g_interrupted_data); + goto_if(ret != PERIPHERAL_ERROR_NONE, ERROR); + + g_interrupted_data.interrupted_cb = interrupted_cb; + g_interrupted_data.interrupted_cb_user_data = user_data; + + return 0; + +ERROR: + _E("failed to read gpio"); + peripheral_gpio_unset_interrupted_cb(g_sensor_h); + g_interrupted_data.interrupted_cb = NULL; + g_interrupted_data.interrupted_cb_user_data = NULL; + g_interrupted_data.motion_value = 0; + return -1; +} + +int resource_unset_interrupted_cb_infrared_motion(int pin_num) +{ + int ret = PERIPHERAL_ERROR_NONE; + int ret_valid = MOTION_HANDLE_ERROR_NONE; + + ret_valid = _resource_validate_infrared_motion(pin_num); + if (ret_valid == MOTION_HANDLE_ERROR_NOT_OPEN) { + _E("No open handle."); + return -1; + } else if (ret_valid == MOTION_HANDLE_ERROR_INVALID_PIN) { + _E("Invalid pin number."); + return -1; + } + + ret = peripheral_gpio_unset_interrupted_cb(g_sensor_h); + retv_if(ret != PERIPHERAL_ERROR_NONE, -1); + + g_interrupted_data.interrupted_cb = NULL; + g_interrupted_data.interrupted_cb_user_data = NULL; + g_interrupted_data.motion_value = 0; + + return 0; +} diff --git a/src/resource/resource_led.c b/src/resource/resource_led.c new file mode 100755 index 0000000..a6edd7c --- /dev/null +++ b/src/resource/resource_led.c @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. + * + * 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 "resource/resource_led.h" +#include "log.h" + +typedef enum { + LED_HANDLE_ERROR_NONE = 0, /**< Successful */ + LED_HANDLE_ERROR_NOT_OPEN, + LED_HANDLE_ERROR_INVALID_PIN +} led_handle_error_e; + +static peripheral_gpio_h g_sensor_h = NULL; +static int g_pin_num = -1; + +int _resource_validate_led(int pin_num) +{ + int ret = LED_HANDLE_ERROR_NONE; + + if (!g_sensor_h) + { + ret = LED_HANDLE_ERROR_NOT_OPEN; + } else if (g_pin_num != pin_num) { + ret = LED_HANDLE_ERROR_INVALID_PIN; + } + + return ret; +} + +int resource_open_led(int pin_num) +{ + int ret = PERIPHERAL_ERROR_NONE; + peripheral_gpio_h temp = NULL; + + ret = peripheral_gpio_open(pin_num, &temp); + if (ret != PERIPHERAL_ERROR_NONE) { + peripheral_gpio_close(temp); + _E("peripheral_gpio_open failed."); + return -1; + } + + ret = peripheral_gpio_set_direction(temp, PERIPHERAL_GPIO_DIRECTION_OUT_INITIALLY_LOW); + if (ret != PERIPHERAL_ERROR_NONE) { + peripheral_gpio_close(temp); + _E("peripheral_gpio_set_direction failed."); + return -1; + } + + g_sensor_h = temp; + g_pin_num = pin_num; + + return 0; +} + +void resource_close_led(void) +{ + if (!g_sensor_h) return; + + _I("LED is finishing..."); + + peripheral_gpio_close(g_sensor_h); + + g_sensor_h = NULL; + g_pin_num = -1; +} + +int resource_write_led(int pin_num, int write_value) +{ + int ret = PERIPHERAL_ERROR_NONE; + int ret_valid = LED_HANDLE_ERROR_NONE; + + ret_valid = _resource_validate_led(pin_num); + if (ret_valid != LED_HANDLE_ERROR_NONE) { + if (ret_valid == LED_HANDLE_ERROR_NOT_OPEN) { + ret = resource_open_led(pin_num); + retv_if(ret != PERIPHERAL_ERROR_NONE, -1); + } else if (ret_valid == LED_HANDLE_ERROR_INVALID_PIN) { + _E("Invalid pin number."); + return -1; + } + } + + ret = peripheral_gpio_write(g_sensor_h, write_value); + retv_if(ret < 0, -1); + + return 0; +} diff --git a/tizen-manifest.xml b/tizen-manifest.xml new file mode 100644 index 0000000..ffb309a --- /dev/null +++ b/tizen-manifest.xml @@ -0,0 +1,15 @@ + + + + + + icon.png + + + + + + + http://tizen.org/privilege/peripheralio + +