From 1d1e7aeac75704141b317076d0487ee7996aeaee Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Sun, 3 Sep 2017 17:22:21 +0900 Subject: [PATCH 02/11] Add Job Scheduler API The Job Scheduler API provides the functions to define job-execution conditions. The API improves the Service-application's performance and system health(such as battery life). Change-Id: Ice176e964c63a15c1a45ead63b8f1faccb72896a Signed-off-by: Hwankyu Jhun --- CMakeLists.txt | 43 + LICENSE | 204 ++++ capi-appfw-job-scheduler.pc.in | 13 + doc/appfw_job_scheduler_doc.h | 44 + include/job_error.h | 55 + include/job_info.h | 284 ++++++ include/job_scheduler.h | 243 +++++ packaging/capi-appfw-job-scheduler.manifest | 5 + packaging/capi-appfw-job-scheduler.spec | 63 ++ src/CMakeLists.txt | 66 ++ src/job_error.c | 65 ++ src/job_error_private.h | 57 ++ src/job_info.c | 575 +++++++++++ src/job_info_private.h | 57 ++ src/job_scheduler.c | 1464 +++++++++++++++++++++++++++ src/job_scheduler_private.h | 34 + 16 files changed, 3272 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 LICENSE create mode 100644 capi-appfw-job-scheduler.pc.in create mode 100755 doc/appfw_job_scheduler_doc.h create mode 100644 include/job_error.h create mode 100644 include/job_info.h create mode 100644 include/job_scheduler.h create mode 100644 packaging/capi-appfw-job-scheduler.manifest create mode 100644 packaging/capi-appfw-job-scheduler.spec create mode 100644 src/CMakeLists.txt create mode 100644 src/job_error.c create mode 100644 src/job_error_private.h create mode 100644 src/job_info.c create mode 100644 src/job_info_private.h create mode 100644 src/job_scheduler.c create mode 100644 src/job_scheduler_private.h diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..085faf4 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,43 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) + +SET(INC_DIR include) + +ADD_SUBDIRECTORY(src) + +INSTALL( + DIRECTORY ${INC_DIR}/ DESTINATION include/appfw + FILES_MATCHING + PATTERN "*_private.h" EXCLUDE + PATTERN "${INC_DIR}/*.h" + ) + +IF(UNIX) + +ADD_CUSTOM_TARGET (distclean @echo cleaning for source distribution) +ADD_CUSTOM_COMMAND( + DEPENDS clean + COMMENT "distribution clean" + COMMAND find + ARGS . + -not -name config.cmake -and \( + -name tester.c -or + -name Testing -or + -name CMakeFiles -or + -name cmake.depends -or + -name cmake.check_depends -or + -name CMakeCache.txt -or + -name cmake.check_cache -or + -name *.cmake -or + -name Makefile -or + -name core -or + -name core.* -or + -name gmon.out -or + -name install_manifest.txt -or + -name *.pc -or + -name *~ \) + | grep -v TC | xargs rm -rf + TARGET distclean + VERBATIM +) + +ENDIF(UNIX) diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..a559bd2 --- /dev/null +++ b/LICENSE @@ -0,0 +1,204 @@ +Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved. + + 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/capi-appfw-job-scheduler.pc.in b/capi-appfw-job-scheduler.pc.in new file mode 100644 index 0000000..f4504d4 --- /dev/null +++ b/capi-appfw-job-scheduler.pc.in @@ -0,0 +1,13 @@ +# Package Information for pkg-config + +prefix=@PREFIX@ +exec_prefix=/usr +libdir=@LIB_INSTALL_DIR@ +includedir=@INCLUDE_INSTALL_DIR@/appfw + +Name: @PC_NAME@ +Description: @PACKAGE_DESCRIPTION@ +Version: @VERSION@ +Requires: @PC_REQUIRED@ +Libs: -L${libdir} @PC_LDFLAGS@ +Cflags: -I${includedir} diff --git a/doc/appfw_job_scheduler_doc.h b/doc/appfw_job_scheduler_doc.h new file mode 100755 index 0000000..211f172 --- /dev/null +++ b/doc/appfw_job_scheduler_doc.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd 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 __TIZEN_APPFW_JOB_SCHEDULER_DOC_H__ +#define __TIZEN_APPFW_JOB_SCHEDULER_DOC_H__ + +/** + * @ingroup CAPI_APPLICATION_MODULE + * @defgroup CAPI_JOB_SCHEDULER_MODULE Job scheduler + * @brief The @ref CAPI_JOB_SCHEDULER_MODULE API contains the job scheduler library. + * + * @section CAPI_JOB_SCHEDULER_MODULE_HEADER Required Header + * \#include + * + * @section CAPI_JOB_SCHEDULER_MODULE_OVERVIEW Overview + * It provides functions to define job-execution conditions. It improves the applications' performance and system health. + * + * @section CAPI_JOB_SCHEDULER_MODULE_FEATURE Related Features + * Some job trigger events and some functions are related with the following features: + * - http://tizen.org/feature/network.wifi + * - http://tizen.org/feature/location.gps + * - http://tizen.org/feature/battery + * - http://tizen.org/feature/usb.host + * + * It is recommended to design feature related code in your application for reliability. + * You can check if the device supports the related features for this API by using @ref CAPI_SYSTEM_INFO_MODULE, thereby controlling the procedure of your application. + * To ensure your application is only running on the device with specific features, please define the features in your manifest file using the manifest editor in the SDK. + * More details on featuring your application can be found from Feature Element. + */ + +#endif /* __TIZEN_APPFW_JOB_SCHEDULER_DOC_H__ */ diff --git a/include/job_error.h b/include/job_error.h new file mode 100644 index 0000000..a58aa50 --- /dev/null +++ b/include/job_error.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd 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 __TIZEN_APPFW_JOB_ERROR_H__ +#define __TIZEN_APPFW_JOB_ERROR_H__ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @addtogroup CAPI_APPLICATION_MODULE + * @{ + */ + +/** + * @brief Enumeration for job error. + * @since_tizen 4.0 + */ +typedef enum { + JOB_ERROR_NONE = TIZEN_ERROR_NONE, /**< Successful */ + JOB_ERROR_INVALID_PARAMETER = TIZEN_ERROR_INVALID_PARAMETER, /**< Invalid parameter */ + JOB_ERROR_OUT_OF_MEMORY = TIZEN_ERROR_OUT_OF_MEMORY, /**< Out of memory */ + JOB_ERROR_IO_ERROR = TIZEN_ERROR_IO_ERROR, /**< IO error */ + JOB_ERROR_NOT_INITIALIZED = TIZEN_ERROR_JOB_SCHEDULER | 0x21, /**< Not initialized */ + JOB_ERROR_NO_SUCH_JOB = TIZEN_ERROR_JOB_SCHEDULER | 0x22, /**< No such job */ + JOB_ERROR_ALREADY_EXIST = TIZEN_ERROR_JOB_SCHEDULER | 0x23, /**< Already exist */ + JOB_ERROR_PERMISSION_DENIED = TIZEN_ERROR_PERMISSION_DENIED, /**< Permission denied */ + JOB_ERROR_NOT_SUPPORTED = TIZEN_ERROR_NOT_SUPPORTED /**< Not supported */ +} job_error_e; + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __TIZEN_APPFW_JOB_ERROR_H__ */ diff --git a/include/job_info.h b/include/job_info.h new file mode 100644 index 0000000..27bbf5e --- /dev/null +++ b/include/job_info.h @@ -0,0 +1,284 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd 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 __TIZEN_APPFW_JOB_INFO_H__ +#define __TIZEN_APPFW_JOB_INFO_H__ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @addtogroup CAPI_APPLICATION_MODULE + * @{ + */ + +/** + * @brief Enumeration for the job trigger events. + * @since_tizen 4.0 + */ +typedef enum { + JOB_TRIGGER_EVENT_BATTERY_LEVEL_EMPTY, /**< The battery level status: Empty */ + JOB_TRIGGER_EVENT_BATTERY_LEVEL_CRITICAL, /**< The battery level status: Critical */ + JOB_TRIGGER_EVENT_BATTERY_LEVEL_LOW, /**< The battery level status: Low */ + JOB_TRIGGER_EVENT_BATTERY_LEVEL_HIGH, /**< The battery level status: High */ + JOB_TRIGGER_EVENT_CHARGER_STATE_CONNECTED, /**< The charger state: Connected */ + JOB_TRIGGER_EVENT_CHARGER_STATE_DISCONNECTED, /**< The charger state: Disconnected */ + JOB_TRIGGER_EVENT_DISPLAY_STATE_ON, /**< The display state: On */ + JOB_TRIGGER_EVENT_DISPLAY_STATE_OFF, /**< The display state: Off */ + JOB_TRIGGER_EVENT_DISPLAY_STATE_DIM, /**< The display state: Dim */ + JOB_TRIGGER_EVENT_EARJACK_STATE_CONNECTED, /**< The earjack state: Connected */ + JOB_TRIGGER_EVENT_EARJACK_STATE_DISCONNECTED, /**< The earjack state: Disconnected */ + JOB_TRIGGER_EVENT_GPS_STATE_DISABLED, /**< The GPS state: Disabled */ + JOB_TRIGGER_EVENT_GPS_STATE_SEARCHING, /**< The GPS state: Searching */ + JOB_TRIGGER_EVENT_GPS_STATE_CONNECTED, /**< The GPS state: Connected */ + JOB_TRIGGER_EVENT_POWERSAVE_MODE_ENABLED, /**< The Power-saving mode: Enabled */ + JOB_TRIGGER_EVENT_POWERSAVE_MODE_DISABLED, /**< The Power-saving mode: Disabled */ + JOB_TRIGGER_EVENT_USB_STATE_CONNECTED, /**< The USB state: Connected */ + JOB_TRIGGER_EVENT_USB_STATE_DISCONNECTED, /**< The USB state: Disconnected */ + JOB_TRIGGER_EVENT_WIFI_STATE_DISABLED, /**< The WiFi state: Disabled @n Privilege: http://tizen.org/privilege/network.get */ + JOB_TRIGGER_EVENT_WIFI_STATE_DISCONNECTED, /**< The WiFi state: Disconnected @n Privilege: http://tizen.org/privilege/network.get */ + JOB_TRIGGER_EVENT_WIFI_STATE_CONNECTED, /**< The WiFi state: Connected @n Privilege: http://tizen.org/privilege/network.get */ +} job_trigger_event_e; + +/** + * @brief The job info handle. + * @since_tizen 4.0 + */ +typedef struct job_info_s *job_info_h; + +/** + * @brief Creates the job info handle. + * @since_tizen 4.0 + * @remarks The @a job_info must be released by using job_info_destroy(). + * The job will be started by the periodic interval or the trigger events. + * @param[out] job_info The job info handle to be newly created on success + * @return @c 0 on success, + * otherwise a negative error value + * @retval #JOB_ERROR_NONE Successful + * @retval #JOB_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #JOB_ERROR_OUT_OF_MEMORY Out of memory + * @see job_info_destroy() + * @see job_info_set_periodic() + * @see job_info_add_trigger_event() + */ +int job_info_create(job_info_h *job_info); + +/** + * @brief Destroys the job info handle and releases all its resources. + * @since_tizen 4.0 + * @param[in] job_info The job info handle + * @return @c 0 on success, + * otherwise a negative error value + * @retval #JOB_ERROR_NONE Successful + * @retval #JOB_ERROR_INVALID_PARAMETER Invalid parameter + * @see job_info_create() + */ +int job_info_destroy(job_info_h job_info); + +/** + * @brief Sets the periodic interval of the job. + * @since_tizen 4.0 + * @remarks The platform does not guarantee the accuracy of the given interval for minimizing the wakeups of the device. + * Thus you should not rely on it for timing. + * If this function is called after adding the job trigger events by using job_info_add_trigger_event(), + * the function returns a negative error value. + * @param[in] job_info The job info handle + * @param[in] interval The periodic interval (in minutes) + * @return @c 0 on success, + * otherwise a negative error value + * @retval #JOB_ERROR_NONE Successful + * @retval #JOB_ERROR_INVALID_PARAMETER Invalid parameter + * @see job_info_destroy() + */ +int job_info_set_periodic(job_info_h job_info, unsigned int interval); + +/** + * @brief Sets the job to be automatically scheduled after the device reboots. + * @since_tizen 4.0 + * @param[in] job_info The job info handle + * @param[in] persistent @c true, if the job needs to be automatically scheduled + * @return @c 0 on success, + * otherwise a negative error value + * @retval #JOB_ERROR_NONE Successful + * @retval #JOB_ERROR_INVALID_PARAMETER Invalid parameter + */ +int job_info_set_persistent(job_info_h job_info, bool persistent); + +/** + * @brief Sets the job not to be repeated. + * @since_tizen 4.0 + * @param[in] job_info The job info handle + * @param[in] once @c true, if the job does not need to be repeated + * @return @c 0 on success, + * otherwise a negative error value + * @retval #JOB_ERROR_NONE Successful + * @retval #JOB_ERROR_INVALID_PARAMETER Invalid parameter + */ +int job_info_set_once(job_info_h job_info, bool once); + +/** + * @brief Sets the timeout interval of the requirements. + * @since_tizen 4.0 + * @param[in] job_info The job info handle + * @param[in] timeout The timeout interval (ms) + * @return @c 0 on success, + * otherwise a negative error value + * @retval #JOB_ERROR_NONE Successful + * @retval #JOB_ERROR_INVALID_PARAMETER Invalid parameter + */ +int job_info_set_requirement_timeout(job_info_h job_info, unsigned int timeout); + +/** + * @brief Sets that the battery level must not be low to run the job. + * @since_tizen 4.0 + * @param[in] job_info The job info handle + * @param[in] battery_not_low @c true, the battery level must not be low. + * @return @c 0 on success, + * otherwise a negative error value + * @retval #JOB_ERROR_NONE Successful + * @retval #JOB_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #JOB_ERROR_IO_ERROR IO error + * @retval #JOB_ERROR_NOT_INITIALIZED Not initialized + * @retval #JOB_ERROR_NOT_SUPPORTED Not supported + * @pre job_scheduler_init() must be called. + * @see job_scheduler_init() + */ +int job_info_set_requires_battery_not_low(job_info_h job_info, bool battery_not_low); + +/** + * @brief Sets that the battery must be charging to run the job. + * @since_tizen 4.0 + * @param[in] job_info The job info handle + * @param[in] charging @c true, the battery must be charging. + * @return @c 0 on success, + * otherwise a negative error value + * @retval #JOB_ERROR_NONE Successful + * @retval #JOB_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #JOB_ERROR_IO_ERROR IO error + * @retval #JOB_ERROR_NOT_INITIALIZED Not initialized + * @retval #JOB_ERROR_NOT_SUPPORTED Not supported + * @pre job_scheduler_init() must be called. + * @see job_scheduler_init() + */ +int job_info_set_requires_charging(job_info_h job_info, bool charging); + +/** + * @brief Sets that the WiFi must be connected to run the job. + * @since_tizen 4.0 + * @privlevel public + * @privilege %http://tizen.org/privilege/network.get + * @param[in] job_info The job info handle + * @param[in] wifi_connection @c true, the WiFi must be connected. + * @return @c 0 on success, + * otherwise a negative error value + * @retval #JOB_ERROR_NONE Successful + * @retval #JOB_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #JOB_ERROR_IO_ERROR IO error + * @retval #JOB_ERROR_NOT_INITIALIZED Not initialized + * @retval #JOB_ERROR_NOT_SUPPORTED Not supported + * @retval #JOB_ERROR_PERMISSION_DENIED Permission denied + * @pre job_scheduler_init() must be called. + * @see job_scheduler_init() + */ +int job_info_set_requires_wifi_connection(job_info_h job_info, bool wifi_connection); + +/** + * @brief Adds the trigger event to the job info handle. + * @since_tizen 4.0 + * @remarks The periodic interval of the job info handle must be 0. + * If the interval of the job info handle is not 0, this function returns a negative error value. + * Regarding the @a event, the application may require the following privilege: + * http://tizen.org/privilege/network.get @n + * If the application does not have the necessary privilege, this function returns #JOB_ERROR_PERMISSION_DENIED. + * See #job_trigger_event_e to find the corresponding privilege of each event. + * @param[in] job_info The job info handle + * @param[in] event The job trigger event + * @return @c 0 on success, + * otherwise a negative error value + * @retval #JOB_ERROR_NONE Successful + * @retval #JOB_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #JOB_ERROR_IO_ERROR IO error + * @retval #JOB_ERROR_NOT_INITIALIZED Not initialized + * @retval #JOB_ERROR_NOT_SUPPORTED Not supported + * @retval #JOB_ERROR_ALREADY_EXIST Already exist + * @retval #JOB_ERROR_PERMISSION_DENIED Permission denied + * @pre job_scheduler_init() must be called. + * @see job_scheduler_init() + * @see job_info_remove_trigger_event() + * @see job_trigger_event_e + */ +int job_info_add_trigger_event(job_info_h job_info, job_trigger_event_e event); + +/** + * @brief Removes the trigger event from the job info handle. + * @since_tizen 4.0 + * @remarks The periodic interval of the job info handle must be 0. + * If the interval of the job info handle is not 0, this function returns a negative error value. + * @param[in] job_info The job info handle + * @param[in] event The job trigger event + * @return @c 0 on success, + * otherwise a negative error value + * @retval #JOB_ERROR_NONE Successful + * @retval #JOB_ERROR_INVALID_PARAMETER Invalid parameter + * @see job_info_add_trigger_event() + * @see job_trigger_event_e + */ +int job_info_remove_trigger_event(job_info_h job_info, job_trigger_event_e event); + +/** + * @brief Gets the ID of the job. + * @since_tizen 4.0 + * @remarks The @a job_info must be scheduled by using job_scheduler_schedule(). + * If the job_info is not scheduled, this function returns a negative error value. + * The @a job_id must be released by using free(). + * @param[in] job_info The job info handle + * @param[out] job_id The ID of the job + * @return @c 0 on success, + * otherwise a negative error value + * @retval #JOB_ERROR_NONE Successful + * @retval #JOB_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #JOB_ERROR_OUT_OF_MEMORY Out of memory + * @see job_scheduler_schedule() + */ +int job_info_get_job_id(job_info_h job_info, char **job_id); + +/** + * @brief Clones the given job_info handle. + * @since_tizen 4.0 + * @remarks The newly created job_info handle must be released by using job_info_destroy(). + * @param[in] job_info The job info handle + * @param[out] clone If successful, a newly created job info handle will be returned + * @return @c 0 on success, + * otherwise a negative error value + * @retval #JOB_ERROR_NONE Successful + * @retval #JOB_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #JOB_ERROR_OUT_OF_MEMORY Out of memory + * @see job_info_destroy() + */ +int job_info_clone(job_info_h job_info, job_info_h *clone); + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __TIZEN_APPFW_JOB_INFO_H__ */ diff --git a/include/job_scheduler.h b/include/job_scheduler.h new file mode 100644 index 0000000..3ff74cd --- /dev/null +++ b/include/job_scheduler.h @@ -0,0 +1,243 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd 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 __TIZEN_APPFW_JOB_SCHEDULER_H__ +#define __TIZEN_APPFW_JOB_SCHEDULER_H__ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @addtogroup CAPI_APPLICATION_MODULE + * @{ + */ + +/** + * @brief The job service handle. + * @since_tizen 4.0 + */ +typedef struct job_service_s *job_service_h; + +/** + * @brief Called when the job starts. + * @since_tizen 4.0 + * @remarks The @a job_info MUST NOT be deallocated by the application. + * The @a job_info is managed by the platform and is valid only in the callback. + * @param[in] job_info The job info handle + * @param[in] user_data The user data passed from the callback registration function + * @see job_scheduler_service_add() + * @see #job_service_callback_s + */ +typedef void (*job_service_start_cb)(job_info_h job_info, void *user_data); + +/** + * @brief Called when the job stops. + * @since_tizen 4.0 + * @remarks The @a job_info MUST not be deallocated by the application. + * The @a job_info is managed by the platform and is valid only in the callback. + * @param[in] job_info The job info handle + * @param[in] user_data The user data passed from the callback registration function + * @see job_scheduler_service_add() + * @see #job_service_callback_s + */ +typedef void (*job_service_stop_cb)(job_info_h job_info, void *user_data); + +/** + * @brief The structure type containing the set of callback functions for handling jobs. + * @details It is one of the input parameter of the job_scheduler_service_add() function. + * @since_tizen 4.0 + * @see job_scheduler_service_add() + * @see job_service_start_cb() + * @see job_service_stop_cb() + */ +typedef struct { + job_service_start_cb start; /**< This callback function is called at the start of the job. */ + job_service_stop_cb stop; /**< This callback function is called at the stop of the job. */ +} job_service_callback_s; + +/** + * @brief Called to retrieve the job information. + * @since_tizen 4.0 + * @remarks The @a job_info must not be deallocated by the application. + * @param[in] job_info The job_info handle + * @param[in] user_data The user data passed from the foreach function + * @return @c true continue with the next iteration of the loop, + * otherwise @c false to break out of the loop + * @pre job_scheduler_foreach_job() will invoke this callback. + * @see job_scheduler_foreach_job() + */ +typedef bool (*job_scheduler_foreach_job_cb)(job_info_h job_info, void *user_data); + +/** + * @brief Initializes the job scheduler. + * @since_tizen 4.0 + * @return @c 0 on success, + * otherwise a negative error value + * @retval #JOB_ERROR_NONE Successful + * @retval #JOB_ERROR_IO_ERROR IO error + * @retval #JOB_ERROR_OUT_OF_MEMORY Out of memory + * @see job_scheduler_finish() + */ +int job_scheduler_init(void); + +/** + * @brief Finalizes the job scheduler. + * @since_tizen 4.0 + * @return @c 0 on success, + * otherwise a negative error value + * @retval #JOB_ERROR_NONE Successful + * @retval #JOB_ERROR_NOT_INITIALIZED Not initialized + * @see job_scheduler_init() + */ +int job_scheduler_finish(void); + +/** + * @brief Schedules the job. + * @since_tizen 4.0 + * @privlevel public + * @privilege %http://tizen.org/privilege/appmanager.launch + * @param[in] job_info The job info handle + * @param[in] job_id The ID of the job + * @return @c 0 on success, + * otherwise a negative error value + * @retval #JOB_ERROR_NONE Successful + * @retval #JOB_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #JOB_ERROR_NOT_INITIALIZED Not initialized + * @retval #JOB_ERROR_IO_ERROR IO error + * @retval #JOB_ERROR_ALREADY_EXIST Already exist + * @retval #JOB_ERROR_OUT_OF_MEMORY Out of memory + * @retval #JOB_ERROR_PERMISSION_DENIED Permission denied + * @pre job_scheduler_init() MUST be called. + * @see job_scheduler_init() + * @see job_info_create() + */ +int job_scheduler_schedule(job_info_h job_info, const char *job_id); + +/** + * @brief Cancels the scheduled job. + * @since_tizen 4.0 + * @param[in] job_id The ID of the scheduled job + * @return @c 0 on success, + * otherwise a negative error value + * @retval #JOB_ERROR_NONE Successful + * @retval #JOB_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #JOB_ERROR_NOT_INITIALIZED Not initialized + * @retval #JOB_ERROR_IO_ERROR IO error + * @retval #JOB_ERROR_NO_SUCH_JOB No such job + * @pre job_scheduler_init() MUST be called. + * @see job_scheduler_init() + */ +int job_scheduler_cancel(const char *job_id); + +/** + * @brief Cancels all scheduled jobs. + * @since_tizen 4.0 + * @return @c 0 on success, + * otherwise a nagative error value + * @retval #JOB_ERROR_NONE Successful + * @retval #JOB_ERROR_NOT_INITIALIZED Not initialized + * @retval #JOB_ERROR_IO_ERROR IO error + * @pre job_scheduler_init() MUST be called. + * @see job_scheduler_init() + */ +int job_scheduler_cancel_all(void); + +/** + * @brief Notify that the job is finished. + * @since_tizen 4.0 + * @remarks If the job has been launched, the system kept awake for a while. + * It is highly recommended to notify that the job is done, + * the system thus knows it does not need to be kept awake. + * @param[in] job_id The ID of the scheduled job + * @return @c 0 on success, + * otherwise a negative error value + * @retval #JOB_ERROR_NONE Successful + * @retval #JOB_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #JOB_ERROR_NOT_INITIALIZED Not initialized + * @retval #JOB_ERROR_NO_SUCH_JOB No such job + * @retval #JOB_ERROR_IO_ERROR IO error + * @pre job_scheduler_init() MUST be called. + * @see job_scheduler_init() + */ +int job_scheduler_notify_job_finished(const char *job_id); + +/** + * @brief Retrieves all scheduled jobs. + * @since_tizen 4.0 + * @param[in] callback The iteration callback function + * @param[in] user_data The user data to be passed to the callback function + * @return @c 0 on success, + * otherwise a negative error value + * @retval #JOB_ERROR_NONE Successful + * @retval #JOB_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #JOB_ERROR_NOT_INITIALIZED Not initialized + * @retval #JOB_ERROR_NO_SUCH_JOB No scheduled jobs + * @pre job_scheduler_init() MUST be called. + * @see job_scheduler_init() + */ +int job_scheduler_foreach_job(job_scheduler_foreach_job_cb callback, void *user_data); + +/** + * @brief Adds job service handler for receiving the information of the scheduled job. + * @since_tizen 4.0 + * @param[in] job_id The ID of the job + * @param[in] callback The set of callback functions to handle jobs + * @param[in] user_data The user data to be passed to the callback function + * @param[out] job_service The job service handle + * @return @c 0 on success, + * otherwise a negative error value + * @retval #JOB_ERROR_NONE Successful + * @retval #JOB_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #JOB_ERROR_NOT_INITIALIZED Not initialized + * @retval #JOB_ERROR_OUT_OF_MEMORY Out of memory + * @pre job_scheduler_init() MUST be called. + * @see job_scheduler_init() + * @see job_scheduler_service_remove() + * @see job_service_start_cb() + * @see job_service_stop_cb() + * @see job_scheduler_job_finished() + * @see #job_service_callback_s + */ +int job_scheduler_service_add(const char *job_id, job_service_callback_s *callback, void *user_data, job_service_h *job_service); + +/** + * @brief Removes registered job service handler. + * @since_tizen 4.0 + * @param[in] job_service The job service handle + * @return @c 0 on success, + * otherwise a negative error value + * @retval #JOB_ERROR_NONE Successful + * @retval #JOB_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #JOB_ERROR_NOT_INITIALIZED Not initialzed + * @pre job_scheduler_init() MUST be called. + * @see job_scheduler_init() + * @see job_scheduler_service_add() + */ +int job_scheduler_service_remove(job_service_h job_service); + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __TIZEN_APPFW_JOB_SCHEDULER_H__ */ diff --git a/packaging/capi-appfw-job-scheduler.manifest b/packaging/capi-appfw-job-scheduler.manifest new file mode 100644 index 0000000..017d22d --- /dev/null +++ b/packaging/capi-appfw-job-scheduler.manifest @@ -0,0 +1,5 @@ + + + + + diff --git a/packaging/capi-appfw-job-scheduler.spec b/packaging/capi-appfw-job-scheduler.spec new file mode 100644 index 0000000..8e82fc9 --- /dev/null +++ b/packaging/capi-appfw-job-scheduler.spec @@ -0,0 +1,63 @@ +Name: capi-appfw-job-scheduler +Summary: Job Scheduler library +Version: 0.1.0 +Release: 0 +Group: System/API +License: Apache-2.0 +Source0: %{name}-%{version}.tar.gz +Source1001: capi-appfw-job-scheduler.manifest +BuildRequires: cmake +BuildRequires: pkgconfig(dlog) +BuildRequires: pkgconfig(bundle) +BuildRequires: pkgconfig(aul) +BuildRequires: pkgconfig(glib-2.0) +BuildRequires: pkgconfig(capi-appfw-app-common) +BuildRequires: pkgconfig(context-job-scheduler-client) +BuildRequires: pkgconfig(context-common-client) +BuildRequires: pkgconfig(capi-base-common) +BuildRequires: pkgconfig(capi-appfw-service-application) +BuildRequires: pkgconfig(cynara-creds-self) +BuildRequires: pkgconfig(cynara-session) +BuildRequires: pkgconfig(cynara-client) + +%description +Job Scheduler library in Tizen C API + +%package devel +Summary: Job Scheduler library in Tizen C API (Development) +Group: System/API +Requires: %{name} = %{version}-%{release} + +%description devel +An Application library in SLP C API (Development) package. + + +%prep +%setup -q +cp %{SOURCE1001} . + +%build +MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'` +%cmake -DFULLVER=%{version} \ + -DMAJORVER=${MAJORVER} \ + . + +%__make %{?jobs:-j%jobs} + +%install +rm -rf %{buildroot} +%make_install + +%post -p /sbin/ldconfig +%postun -p /sbin/ldconfig + +%files +%manifest %{name}.manifest +%{_libdir}/libcapi-appfw-job-scheduler.so.* +%license LICENSE + +%files devel +%manifest %{name}.manifest +%{_includedir}/appfw/*.h +%{_libdir}/pkgconfig/capi-appfw-job-scheduler.pc +%{_libdir}/libcapi-appfw-job-scheduler.so diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..a03f03e --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,66 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +SET(fw_name "capi-appfw-job-scheduler") + +PROJECT(${fw_name}) + +SET(CMAKE_INSTALL_PREFIX /usr) +SET(PREFIX ${CMAKE_INSTALL_PREFIX}) + +SET(INC_DIR ${CMAKE_SOURCE_DIR}/include) +INCLUDE_DIRECTORIES(${INC_DIR}) + +SET(requires "dlog bundle aul glib-2.0 capi-appfw-app-common context-job-scheduler-client capi-base-common context-common-client capi-appfw-service-application cynara-creds-self cynara-session cynara-client") +SET(pc_requires "dlog") + +INCLUDE(FindPkgConfig) +pkg_check_modules(${fw_name} REQUIRED ${requires}) +FOREACH(flag ${${fw_name}_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag} -Werror") +ENDFOREACH(flag) + +SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden") +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -fPIC ") +SET(CMAKE_C_FLAGS_DEBUG "-O0 -g -Wall -Werror") + +IF("${ARCH}" STREQUAL "arm") + ADD_DEFINITIONS("-DTARGET") +ENDIF("${ARCH}" STREQUAL "arm") + +ADD_DEFINITIONS("-DPREFIX=\"${CMAKE_INSTALL_PREFIX}\"") +ADD_DEFINITIONS("-DSLP_DEBUG") + +SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -Wl,--rpath=${LIB_INSTALL_DIR}") + +ADD_LIBRARY(${fw_name} SHARED + job_scheduler.c + job_info.c + job_error.c + ) + +TARGET_LINK_LIBRARIES(${fw_name} ${${fw_name}_LDFLAGS} capi-appfw-app-common) + +SET_TARGET_PROPERTIES(${fw_name} + PROPERTIES + VERSION ${FULLVER} + SOVERSION ${MAJORVER} + CLEAN_DIRECT_OUTPUT 1 +) + +INSTALL(TARGETS ${fw_name} DESTINATION ${LIB_INSTALL_DIR}) +INSTALL( + DIRECTORY ${INC_DIR}/ DESTINATION include/appfw + FILES_MATCHING + PATTERN "*_private.h" EXCLUDE + PATTERN "${INC_DIR}/*.h" + ) + +SET(PC_NAME ${fw_name}) +SET(PC_REQUIRED ${pc_requires}) +SET(PC_LDFLAGS -l${fw_name}) + +CONFIGURE_FILE( + ${CMAKE_SOURCE_DIR}/${fw_name}.pc.in + ${CMAKE_CURRENT_SOURCE_DIR}/${fw_name}.pc + @ONLY +) +INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${fw_name}.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig) diff --git a/src/job_error.c b/src/job_error.c new file mode 100644 index 0000000..a2d00d9 --- /dev/null +++ b/src/job_error.c @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd 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. + */ + +#define _GNU_SOURCE +#include +#include +#include +#include + +#include "job_error_private.h" +#include "job_error.h" + +static const char *__job_error_to_string(job_error_e error) +{ + switch (error) { + case JOB_ERROR_NONE: + return "None"; + case JOB_ERROR_INVALID_PARAMETER: + return "Invalid parameter"; + case JOB_ERROR_OUT_OF_MEMORY: + return "Out of memory"; + case JOB_ERROR_IO_ERROR: + return "IO error"; + case JOB_ERROR_NOT_INITIALIZED: + return "Not initialized"; + case JOB_ERROR_NO_SUCH_JOB: + return "No such job"; + case JOB_ERROR_ALREADY_EXIST: + return "Already exist"; + case JOB_ERROR_PERMISSION_DENIED: + return "Permission denied"; + default: + return "Unknown"; + } +} + +int job_error(job_error_e error, const char *function, const int line, + const char *description) +{ + if (description) { + LOGE("[%s][%d] %s(0x%08x) - %s", + function, line, + __job_error_to_string(error), error, + description); + } else { + LOGE("[%s][%d] %s(0x%08x)", + function, line, + __job_error_to_string(error), error); + } + + return error; +} diff --git a/src/job_error_private.h b/src/job_error_private.h new file mode 100644 index 0000000..2cf848c --- /dev/null +++ b/src/job_error_private.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd 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 __TIZEN_APPFW_JOB_ERROR_PRIVATE_H__ +#define __TIZEN_APPFW_JOB_ERROR_PRIVATE_H__ + +#include + +#include "job_error.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef LOG_TAG +#undef LOG_TAG +#endif + +#define LOG_TAG "CAPI_JOB_SCHEDULER" + +#ifndef _E +#define _E LOGE +#endif + +#ifndef _W +#define _W LOGW +#endif + +#ifndef _I +#define _I LOGI +#endif + +#ifndef _D +#define _D LOGD +#endif + +int job_error(job_error_e error, const char *function, const int line, + const char *description); + +#ifdef __cplusplus +} +#endif + +#endif /* __TIZEN_APPFW_JOB_ERROR_PRIVATE_H__ */ diff --git a/src/job_info.c b/src/job_info.c new file mode 100644 index 0000000..5031882 --- /dev/null +++ b/src/job_info.c @@ -0,0 +1,575 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd 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. + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "job_error_private.h" +#include "job_info_private.h" +#include "job_scheduler_private.h" + +#ifdef API +#undef API +#endif +#define API __attribute__((visibility("default"))) + +#define ARRAY_SIZE(x) ((sizeof(x)) / (sizeof(x[0]))) + +struct job_info_s { + char *job_id; + unsigned int interval; /* Periodic */ + bool persistent; + bool once; + unsigned int timeout; + int battery_not_low; + int charging; + int wifi_connection; + bool trigger_list[JOB_TRIGGER_EVENT_WIFI_STATE_CONNECTED + 1]; +}; + +static const char *trigger_uri[] = { + [JOB_TRIGGER_EVENT_BATTERY_LEVEL_EMPTY] = CTX_SCHED_URI_BATTERY, + [JOB_TRIGGER_EVENT_BATTERY_LEVEL_CRITICAL] = CTX_SCHED_URI_BATTERY, + [JOB_TRIGGER_EVENT_BATTERY_LEVEL_LOW] = CTX_SCHED_URI_BATTERY, + [JOB_TRIGGER_EVENT_BATTERY_LEVEL_HIGH] = CTX_SCHED_URI_BATTERY, + [JOB_TRIGGER_EVENT_CHARGER_STATE_CONNECTED] = CTX_SCHED_URI_CHARGER, + [JOB_TRIGGER_EVENT_CHARGER_STATE_DISCONNECTED] = CTX_SCHED_URI_CHARGER, + [JOB_TRIGGER_EVENT_DISPLAY_STATE_ON] = CTX_SCHED_URI_DISPLAY, + [JOB_TRIGGER_EVENT_DISPLAY_STATE_OFF] = CTX_SCHED_URI_DISPLAY, + [JOB_TRIGGER_EVENT_DISPLAY_STATE_DIM] = CTX_SCHED_URI_DISPLAY, + [JOB_TRIGGER_EVENT_EARJACK_STATE_CONNECTED] = CTX_SCHED_URI_EARJACK, + [JOB_TRIGGER_EVENT_EARJACK_STATE_DISCONNECTED] = CTX_SCHED_URI_EARJACK, + [JOB_TRIGGER_EVENT_GPS_STATE_DISABLED] = CTX_SCHED_URI_GPS, + [JOB_TRIGGER_EVENT_GPS_STATE_SEARCHING] = CTX_SCHED_URI_GPS, + [JOB_TRIGGER_EVENT_GPS_STATE_CONNECTED] = CTX_SCHED_URI_GPS, + [JOB_TRIGGER_EVENT_POWERSAVE_MODE_ENABLED] = CTX_SCHED_URI_POWERSAVE, + [JOB_TRIGGER_EVENT_POWERSAVE_MODE_DISABLED] = CTX_SCHED_URI_POWERSAVE, + [JOB_TRIGGER_EVENT_USB_STATE_CONNECTED] = CTX_SCHED_URI_USB, + [JOB_TRIGGER_EVENT_USB_STATE_DISCONNECTED] = CTX_SCHED_URI_USB, + [JOB_TRIGGER_EVENT_WIFI_STATE_DISABLED] = CTX_SCHED_URI_WIFI, + [JOB_TRIGGER_EVENT_WIFI_STATE_DISCONNECTED] = CTX_SCHED_URI_WIFI, + [JOB_TRIGGER_EVENT_WIFI_STATE_CONNECTED] = CTX_SCHED_URI_WIFI, +}; + +API int job_info_create(job_info_h *job_info) +{ + struct job_info_s *info; + + if (job_info == NULL) { + return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, + __LINE__, NULL); + } + + info = calloc(1, sizeof(struct job_info_s)); + if (info == NULL) { + return job_error(JOB_ERROR_OUT_OF_MEMORY, __FUNCTION__, + __LINE__, NULL); + } + + info->battery_not_low = -1; + info->charging = -1; + info->wifi_connection = -1; + + *job_info = info; + + return JOB_ERROR_NONE; +} + +API int job_info_destroy(job_info_h job_info) +{ + if (job_info == NULL) { + return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, + __LINE__, NULL); + } + + if (job_info->job_id) + free(job_info->job_id); + + free(job_info); + + return JOB_ERROR_NONE; +} + +static bool __exist_trigger_event(job_info_h job_info) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(job_info->trigger_list); ++i) { + if (job_info->trigger_list[i]) + return true; + } + + return false; +} + +API int job_info_set_periodic(job_info_h job_info, unsigned int interval) +{ + if (job_info == NULL || __exist_trigger_event(job_info)) { + return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, + __LINE__, NULL); + } + + job_info->interval = interval; + + return JOB_ERROR_NONE; +} + +int job_info_get_periodic(job_info_h job_info, unsigned int *interval) +{ + if (job_info == NULL || interval == NULL) { + return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, + __LINE__, NULL); + } + + *interval = job_info->interval; + + return JOB_ERROR_NONE; +} + +API int job_info_set_persistent(job_info_h job_info, bool persistent) +{ + if (job_info == NULL) { + return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, + __LINE__, NULL); + } + + job_info->persistent = persistent; + + return JOB_ERROR_NONE; +} + +int job_info_get_persistent(job_info_h job_info, bool *persistent) +{ + if (job_info == NULL || persistent == NULL) { + return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, + __LINE__, NULL); + } + + *persistent = job_info->persistent; + + return JOB_ERROR_NONE; +} + +API int job_info_set_once(job_info_h job_info, bool once) +{ + if (job_info == NULL) { + return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, + __LINE__, NULL); + } + + job_info->once = once; + + return JOB_ERROR_NONE; +} + +int job_info_is_once(job_info_h job_info, bool *once) +{ + if (job_info == NULL || once == NULL) { + return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, + __LINE__, NULL); + } + + *once = job_info->once; + + return JOB_ERROR_NONE; +} + +API int job_info_set_requirement_timeout(job_info_h job_info, + unsigned int timeout) +{ + if (job_info == NULL) { + return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, + __LINE__, NULL); + } + + job_info->timeout = timeout; + + return JOB_ERROR_NONE; +} + +int job_info_get_requirement_timeout(job_info_h job_info, unsigned int *timeout) +{ + if (job_info == NULL || timeout == NULL) { + return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, + __LINE__, NULL); + } + + *timeout = job_info->timeout; + + return JOB_ERROR_NONE; +} + +API int job_info_set_requires_battery_not_low(job_info_h job_info, + bool battery_not_low) +{ + int r; + + if (job_info == NULL) { + return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, + __LINE__, NULL); + } + + r = job_scheduler_verify_requirement(CTX_SCHED_URI_BATTERY); + if (r != JOB_ERROR_NONE) + return job_error(r, __FUNCTION__, __LINE__, NULL); + + job_info->battery_not_low = battery_not_low; + + return JOB_ERROR_NONE; +} + +int job_info_is_requires_battery_not_low(job_info_h job_info, + int *battery_not_low) +{ + if (job_info == NULL || battery_not_low == NULL) { + return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, + __LINE__, NULL); + } + + *battery_not_low = job_info->battery_not_low; + + return JOB_ERROR_NONE; +} + +API int job_info_set_requires_charging(job_info_h job_info, bool charging) +{ + int r; + + if (job_info == NULL) { + return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, + __LINE__, NULL); + } + + r = job_scheduler_verify_requirement(CTX_SCHED_URI_BATTERY); + if (r != JOB_ERROR_NONE) + return job_error(r, __FUNCTION__, __LINE__, NULL); + + job_info->charging = charging; + + return JOB_ERROR_NONE; +} + +int job_info_is_requires_charging(job_info_h job_info, int *charging) +{ + if (job_info == NULL || charging == NULL) { + return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, + __LINE__, NULL); + } + + *charging = job_info->charging; + + return JOB_ERROR_NONE; +} + +static const char *__get_privilege(job_trigger_event_e event) +{ + if (event == JOB_TRIGGER_EVENT_WIFI_STATE_DISABLED || + event == JOB_TRIGGER_EVENT_WIFI_STATE_DISCONNECTED || + event == JOB_TRIGGER_EVENT_WIFI_STATE_CONNECTED) + return "http://tizen.org/privilege/network.get"; + + return NULL; +} + +API int job_info_set_requires_wifi_connection(job_info_h job_info, + bool wifi_connection) +{ + int r; + const char *privilege; + + if (job_info == NULL) { + return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, + __LINE__, NULL); + } + + privilege = __get_privilege(JOB_TRIGGER_EVENT_WIFI_STATE_CONNECTED); + r = job_scheduler_verify_privilege(privilege); + if (r != JOB_ERROR_NONE) + return job_error(r, __FUNCTION__, __LINE__, NULL); + + r = job_scheduler_verify_requirement(CTX_SCHED_URI_WIFI); + if (r != JOB_ERROR_NONE) + return job_error(r, __FUNCTION__, __LINE__, NULL); + + job_info->wifi_connection = wifi_connection; + + return JOB_ERROR_NONE; +} + +int job_info_is_requires_wifi_connection(job_info_h job_info, + int *wifi_connection) +{ + if (job_info == NULL || wifi_connection == NULL) { + return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, + __LINE__, NULL); + } + + *wifi_connection = job_info->wifi_connection; + + return JOB_ERROR_NONE; +} + +API int job_info_add_trigger_event(job_info_h job_info, + job_trigger_event_e event) +{ + int r; + const char *privilege; + + if (job_info == NULL || job_info->interval > 0 || + event < JOB_TRIGGER_EVENT_BATTERY_LEVEL_EMPTY || + event > JOB_TRIGGER_EVENT_WIFI_STATE_CONNECTED) { + return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, + __LINE__, NULL); + } + + privilege = __get_privilege(event); + r = job_scheduler_verify_privilege(privilege); + if (r != JOB_ERROR_NONE) + return job_error(r, __FUNCTION__, __LINE__, NULL); + + r = job_scheduler_verify_trigger(trigger_uri[event]); + if (r != JOB_ERROR_NONE) + return job_error(r, __FUNCTION__, __LINE__, NULL); + + job_info->trigger_list[event] = true; + + return JOB_ERROR_NONE; +} + +API int job_info_remove_trigger_event(job_info_h job_info, + job_trigger_event_e event) +{ + if (job_info == NULL || job_info->interval > 0 || + event < JOB_TRIGGER_EVENT_BATTERY_LEVEL_EMPTY || + event > JOB_TRIGGER_EVENT_WIFI_STATE_CONNECTED) { + return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, + __LINE__, NULL); + } + + job_info->trigger_list[event] = false; + + return JOB_ERROR_NONE; +} + +API int job_info_clone(job_info_h job_info, job_info_h *clone) +{ + struct job_info_s *info; + int i; + + if (job_info == NULL || clone == NULL) { + return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, + __LINE__, NULL); + } + + info = calloc(1, sizeof(struct job_info_s)); + if (info == NULL) { + return job_error(JOB_ERROR_OUT_OF_MEMORY, __FUNCTION__, + __LINE__, NULL); + } + + if (job_info->job_id) { + info->job_id = strdup(job_info->job_id); + if (info->job_id == NULL) { + free(info); + return job_error(JOB_ERROR_OUT_OF_MEMORY, __FUNCTION__, + __LINE__, NULL); + } + } + + info->interval = job_info->interval; + info->persistent = job_info->persistent; + info->once = job_info->once; + info->timeout = job_info->timeout; + info->battery_not_low = job_info->battery_not_low; + info->charging = job_info->charging; + info->wifi_connection = job_info->wifi_connection; + + for (i = 0; i < ARRAY_SIZE(job_info->trigger_list); ++i) + info->trigger_list[i] = job_info->trigger_list[i]; + + *clone = info; + + return JOB_ERROR_NONE; +} + +int job_info_get_trigger_list(job_info_h job_info, bool **trigger_list) +{ + if (job_info == NULL || trigger_list == NULL) { + return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, + __LINE__, NULL); + } + + *trigger_list = job_info->trigger_list; + + return JOB_ERROR_NONE; +} + +int job_info_set_job_id(job_info_h job_info, const char *job_id) +{ + if (job_info == NULL || job_id == NULL) { + return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, + __LINE__, NULL); + } + + if (job_info->job_id) + free(job_info->job_id); + + job_info->job_id = strdup(job_id); + if (job_info->job_id == NULL) { + return job_error(JOB_ERROR_OUT_OF_MEMORY, __FUNCTION__, + __LINE__, NULL); + } + + return JOB_ERROR_NONE; +} + +API int job_info_get_job_id(job_info_h job_info, char **job_id) +{ + if (job_info == NULL || job_info->job_id == NULL || job_id == NULL) { + return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, + __LINE__, NULL); + } + + *job_id = strdup(job_info->job_id); + if (*job_id == NULL) { + return job_error(JOB_ERROR_OUT_OF_MEMORY, __FUNCTION__, + __LINE__, NULL); + } + + return JOB_ERROR_NONE; +} + +static void __set_requires_value(const char *str, int *value) +{ + if (str && strcmp(str, "true") == 0) + *value = true; + else if (str && strcmp(str, "false") == 0) + *value = false; + else + *value = -1; +} + +static void __set_job_info(job_info_h job_info, bundle *b) +{ + int i; + const char *val; + static const char *key_list[] = { + [JOB_TRIGGER_EVENT_BATTERY_LEVEL_EMPTY] = + AUL_K_JOB_TRIG_BATTERY_LEVEL_EMPTY, + [JOB_TRIGGER_EVENT_BATTERY_LEVEL_CRITICAL] = + AUL_K_JOB_TRIG_BATTERY_LEVEL_CRITICAL, + [JOB_TRIGGER_EVENT_BATTERY_LEVEL_LOW] = + AUL_K_JOB_TRIG_BATTERY_LEVEL_LOW, + [JOB_TRIGGER_EVENT_BATTERY_LEVEL_HIGH] = + AUL_K_JOB_TRIG_BATTERY_LEVEL_HIGH, + [JOB_TRIGGER_EVENT_CHARGER_STATE_CONNECTED] = + AUL_K_JOB_TRIG_CHARGER_STATE_CONNECTED, + [JOB_TRIGGER_EVENT_CHARGER_STATE_DISCONNECTED] = + AUL_K_JOB_TRIG_CHARGER_STATE_DISCONNECTED, + [JOB_TRIGGER_EVENT_DISPLAY_STATE_ON] = + AUL_K_JOB_TRIG_DISPLAY_STATE_ON, + [JOB_TRIGGER_EVENT_DISPLAY_STATE_OFF] = + AUL_K_JOB_TRIG_DISPLAY_STATE_OFF, + [JOB_TRIGGER_EVENT_DISPLAY_STATE_DIM] = + AUL_K_JOB_TRIG_DISPLAY_STATE_DIM, + [JOB_TRIGGER_EVENT_EARJACK_STATE_CONNECTED] = + AUL_K_JOB_TRIG_EARJACK_STATE_CONNECTED, + [JOB_TRIGGER_EVENT_EARJACK_STATE_DISCONNECTED] = + AUL_K_JOB_TRIG_EARJACK_STATE_DISCONNECTED, + [JOB_TRIGGER_EVENT_GPS_STATE_DISABLED] = + AUL_K_JOB_TRIG_GPS_STATE_DISABLED, + [JOB_TRIGGER_EVENT_GPS_STATE_SEARCHING] = + AUL_K_JOB_TRIG_GPS_STATE_SEARCHING, + [JOB_TRIGGER_EVENT_GPS_STATE_CONNECTED] = + AUL_K_JOB_TRIG_GPS_STATE_CONNECTED, + [JOB_TRIGGER_EVENT_POWERSAVE_MODE_ENABLED] = + AUL_K_JOB_TRIG_POWERSAVE_MODE_ENABLED, + [JOB_TRIGGER_EVENT_POWERSAVE_MODE_DISABLED] = + AUL_K_JOB_TRIG_POWERSAVE_MODE_DISABLED, + [JOB_TRIGGER_EVENT_USB_STATE_CONNECTED] = + AUL_K_JOB_TRIG_USB_STATE_CONNECTED, + [JOB_TRIGGER_EVENT_USB_STATE_DISCONNECTED] = + AUL_K_JOB_TRIG_USB_STATE_DISCONNECTED, + [JOB_TRIGGER_EVENT_WIFI_STATE_DISABLED] = + AUL_K_JOB_TRIG_WIFI_STATE_DISABLED, + [JOB_TRIGGER_EVENT_WIFI_STATE_DISCONNECTED] = + AUL_K_JOB_TRIG_WIFI_STATE_DISCONNECTED, + [JOB_TRIGGER_EVENT_WIFI_STATE_CONNECTED] = + AUL_K_JOB_TRIG_WIFI_STATE_CONNECTED, + }; + + val = bundle_get_val(b, AUL_K_JOB_PERIODIC); + if (val && isdigit(*val)) + job_info->interval = strtoul(val, NULL, 10); + + val = bundle_get_val(b, AUL_K_JOB_PERSISTENT); + if (val) + job_info->persistent = true; + + val = bundle_get_val(b, AUL_K_JOB_IS_ONCE); + if (val) + job_info->once = true; + + val = bundle_get_val(b, AUL_K_JOB_REQ_TIMEOUT); + if (val && isdigit(*val)) + job_info->timeout = strtoul(val, NULL, 10); + + val = bundle_get_val(b, AUL_K_JOB_REQ_BATTERY_NOT_LOW); + __set_requires_value(val, &job_info->battery_not_low); + + val = bundle_get_val(b, AUL_K_JOB_REQ_CHARGING); + __set_requires_value(val, &job_info->charging); + + val = bundle_get_val(b, AUL_K_JOB_REQ_WIFI_CONNECTION); + __set_requires_value(val, &job_info->wifi_connection); + + for (i = 0; i < ARRAY_SIZE(key_list); ++i) { + val = bundle_get_val(b, key_list[i]); + if (val) + job_info->trigger_list[i] = true; + } +} + +int job_info_create_with_bundle(bundle *b, job_info_h *job_info) +{ + struct job_info_s *info; + + if (b == NULL || job_info == NULL) { + return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, + __LINE__, NULL); + } + + info = calloc(1, sizeof(struct job_info_s)); + if (info == NULL) { + return job_error(JOB_ERROR_OUT_OF_MEMORY, __FUNCTION__, + __LINE__, NULL); + } + + __set_job_info(info, b); + + *job_info = info; + + return JOB_ERROR_NONE; +} diff --git a/src/job_info_private.h b/src/job_info_private.h new file mode 100644 index 0000000..47f0619 --- /dev/null +++ b/src/job_info_private.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd 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 __TIZEN_APPFW_JOB_INFO_PRIVATE_H__ +#define __TIZEN_APPFW_JOB_INFO_PRIVATE_H__ + +#include +#include + +#include "job_info.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int job_info_create_with_bundle(bundle *b, job_info_h *job_info); + +int job_info_set_job_id(job_info_h job_info, const char *job_id); + +int job_info_get_periodic(job_info_h job_info, unsigned int *periodic); + +int job_info_get_persistent(job_info_h job_info, bool *persistent); + +int job_info_is_once(job_info_h job_info, bool *once); + +int job_info_get_requirement_timeout(job_info_h job_info, + unsigned int *timeout); + +int job_info_is_requires_battery_not_low(job_info_h job_info, + int *battery_not_low); + +int job_info_is_requires_charging(job_info_h job_info, int *charging); + +int job_info_is_requires_wifi_connection(job_info_h job_info, + int *wifi_connection); + +/* The type of the trigger_list is (bool (*)[JOB_TRIGGER_EVENT_WIFI_STATE_CONNECTED]) */ +int job_info_get_trigger_list(job_info_h job_info, bool **trigger_list); + +#ifdef __cplusplus +} +#endif + +#endif /* __TIZEN_APPFW_JOB_INFO_PRIVATE_H__ */ diff --git a/src/job_scheduler.c b/src/job_scheduler.c new file mode 100644 index 0000000..349d330 --- /dev/null +++ b/src/job_scheduler.c @@ -0,0 +1,1464 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd 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. + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "job_error_private.h" +#include "job_info_private.h" +#include "job_scheduler.h" + +#ifdef API +#undef API +#endif + +#define API __attribute__((visibility("default"))) + +#define ARRAY_SIZE(x) ((sizeof(x)) / (sizeof(x[0]))) + +typedef int (*job_add_cb)(ctx_sched_job_h job, job_info_h job_info, + void *data); + +enum attr_type_e { + ATTR_TYPE_STRING, + ATTR_TYPE_INTEGER, +}; + +struct job_service_s { + service_app_job_h handle; + job_service_callback_s callback; + void *user_data; +}; + +struct job_scheduler_s { + char *app_id; + ctx_sched_h scheduler; + GHashTable *job_id_table; + GList *job_service_list; + bool initialized; +}; + +struct job_trigger_map_s { + const char *key; + const char *uri; + const char *attr_name; + int attr_type; + void *attr_value; +}; + +struct job_cynara_s { + cynara *handle; + char *client; + char *user; + char *session; + bool initialized; +}; + +static struct job_cynara_s __cynara; +static struct job_scheduler_s __handle; +static struct job_trigger_map_s job_trigger_map[] = { + [JOB_TRIGGER_EVENT_BATTERY_LEVEL_EMPTY] = { + .key = AUL_K_JOB_TRIG_BATTERY_LEVEL_EMPTY, + .uri = CTX_SCHED_URI_BATTERY, + .attr_name = CTX_SCHED_ATTR_NAME_LEVEL, + .attr_type = ATTR_TYPE_STRING, + .attr_value = CTX_SCHED_ATTR_VALUE_EMPTY + }, + [JOB_TRIGGER_EVENT_BATTERY_LEVEL_CRITICAL] = { + .key = AUL_K_JOB_TRIG_BATTERY_LEVEL_CRITICAL, + .uri = CTX_SCHED_URI_BATTERY, + .attr_name = CTX_SCHED_ATTR_NAME_LEVEL, + .attr_type = ATTR_TYPE_STRING, + .attr_value = CTX_SCHED_ATTR_VALUE_CRITICAL + }, + [JOB_TRIGGER_EVENT_BATTERY_LEVEL_LOW] = { + .key = AUL_K_JOB_TRIG_BATTERY_LEVEL_LOW, + .uri = CTX_SCHED_URI_BATTERY, + .attr_name = CTX_SCHED_ATTR_NAME_LEVEL, + .attr_type = ATTR_TYPE_STRING, + .attr_value = CTX_SCHED_ATTR_VALUE_LOW + }, + [JOB_TRIGGER_EVENT_BATTERY_LEVEL_HIGH] = { + .key = AUL_K_JOB_TRIG_BATTERY_LEVEL_HIGH, + .uri = CTX_SCHED_URI_BATTERY, + .attr_name = CTX_SCHED_ATTR_NAME_LEVEL, + .attr_type = ATTR_TYPE_STRING, + .attr_value = CTX_SCHED_ATTR_VALUE_HIGH + }, + [JOB_TRIGGER_EVENT_CHARGER_STATE_CONNECTED] = { + .key = AUL_K_JOB_TRIG_CHARGER_STATE_CONNECTED, + .uri = CTX_SCHED_URI_CHARGER, + .attr_name = CTX_SCHED_ATTR_NAME_IS_CONNECTED, + .attr_type = ATTR_TYPE_INTEGER, + .attr_value = GINT_TO_POINTER(CTX_SCHED_ATTR_VALUE_TRUE) + }, + [JOB_TRIGGER_EVENT_CHARGER_STATE_DISCONNECTED] = { + .key = AUL_K_JOB_TRIG_CHARGER_STATE_DISCONNECTED, + .uri = CTX_SCHED_URI_CHARGER, + .attr_name = CTX_SCHED_ATTR_NAME_IS_CONNECTED, + .attr_type = ATTR_TYPE_INTEGER, + .attr_value = CTX_SCHED_ATTR_VALUE_FALSE + }, + [JOB_TRIGGER_EVENT_DISPLAY_STATE_ON] = { + .key = AUL_K_JOB_TRIG_DISPLAY_STATE_ON, + .uri = CTX_SCHED_URI_DISPLAY, + .attr_name = CTX_SCHED_ATTR_NAME_STATE, + .attr_type = ATTR_TYPE_STRING, + .attr_value = CTX_SCHED_ATTR_VALUE_ON + }, + [JOB_TRIGGER_EVENT_DISPLAY_STATE_OFF] = { + .key = AUL_K_JOB_TRIG_DISPLAY_STATE_OFF, + .uri = CTX_SCHED_URI_DISPLAY, + .attr_name = CTX_SCHED_ATTR_NAME_STATE, + .attr_type = ATTR_TYPE_STRING, + .attr_value = CTX_SCHED_ATTR_VALUE_OFF + }, + [JOB_TRIGGER_EVENT_DISPLAY_STATE_DIM] = { + .key = AUL_K_JOB_TRIG_DISPLAY_STATE_DIM, + .uri = CTX_SCHED_URI_DISPLAY, + .attr_name = CTX_SCHED_ATTR_NAME_STATE, + .attr_type = ATTR_TYPE_STRING, + .attr_value = CTX_SCHED_ATTR_VALUE_DIM + }, + [JOB_TRIGGER_EVENT_EARJACK_STATE_CONNECTED] = { + .key = AUL_K_JOB_TRIG_EARJACK_STATE_CONNECTED, + .uri = CTX_SCHED_URI_EARJACK, + .attr_name = CTX_SCHED_ATTR_NAME_IS_CONNECTED, + .attr_type = ATTR_TYPE_INTEGER, + .attr_value = GINT_TO_POINTER(CTX_SCHED_ATTR_VALUE_TRUE) + }, + [JOB_TRIGGER_EVENT_EARJACK_STATE_DISCONNECTED] = { + .key = AUL_K_JOB_TRIG_EARJACK_STATE_DISCONNECTED, + .uri = CTX_SCHED_URI_EARJACK, + .attr_name = CTX_SCHED_ATTR_NAME_IS_CONNECTED, + .attr_type = ATTR_TYPE_INTEGER, + .attr_value = CTX_SCHED_ATTR_VALUE_FALSE + }, + [JOB_TRIGGER_EVENT_GPS_STATE_DISABLED] = { + .key = AUL_K_JOB_TRIG_GPS_STATE_DISABLED, + .uri = CTX_SCHED_URI_GPS, + .attr_name = CTX_SCHED_ATTR_NAME_STATE, + .attr_type = ATTR_TYPE_STRING, + .attr_value = CTX_SCHED_ATTR_VALUE_DISABLED + }, + [JOB_TRIGGER_EVENT_GPS_STATE_SEARCHING] = { + .key = AUL_K_JOB_TRIG_GPS_STATE_SEARCHING, + .uri = CTX_SCHED_URI_GPS, + .attr_name = CTX_SCHED_ATTR_NAME_STATE, + .attr_type = ATTR_TYPE_STRING, + .attr_value = CTX_SCHED_ATTR_VALUE_SEARCHING + }, + [JOB_TRIGGER_EVENT_GPS_STATE_CONNECTED] = { + .key = AUL_K_JOB_TRIG_GPS_STATE_CONNECTED, + .uri = CTX_SCHED_URI_GPS, + .attr_name = CTX_SCHED_ATTR_NAME_STATE, + .attr_type = ATTR_TYPE_STRING, + .attr_value = CTX_SCHED_ATTR_VALUE_CONNECTED + }, + [JOB_TRIGGER_EVENT_POWERSAVE_MODE_ENABLED] = { + .key = AUL_K_JOB_TRIG_POWERSAVE_MODE_ENABLED, + .uri = CTX_SCHED_URI_POWERSAVE, + .attr_name = CTX_SCHED_ATTR_NAME_IS_ENABLED, + .attr_type = ATTR_TYPE_INTEGER, + .attr_value = GINT_TO_POINTER(CTX_SCHED_ATTR_VALUE_TRUE) + }, + [JOB_TRIGGER_EVENT_POWERSAVE_MODE_DISABLED] = { + .key = AUL_K_JOB_TRIG_POWERSAVE_MODE_DISABLED, + .uri = CTX_SCHED_URI_POWERSAVE, + .attr_name = CTX_SCHED_ATTR_NAME_IS_ENABLED, + .attr_type = ATTR_TYPE_INTEGER, + .attr_value = CTX_SCHED_ATTR_VALUE_FALSE + }, + [JOB_TRIGGER_EVENT_USB_STATE_CONNECTED] = { + .key = AUL_K_JOB_TRIG_USB_STATE_CONNECTED, + .uri = CTX_SCHED_URI_USB, + .attr_name = CTX_SCHED_ATTR_NAME_IS_CONNECTED, + .attr_type = ATTR_TYPE_INTEGER, + .attr_value = GINT_TO_POINTER(CTX_SCHED_ATTR_VALUE_TRUE) + }, + [JOB_TRIGGER_EVENT_USB_STATE_DISCONNECTED] = { + .key = AUL_K_JOB_TRIG_USB_STATE_DISCONNECTED, + .uri = CTX_SCHED_URI_USB, + .attr_name = CTX_SCHED_ATTR_NAME_IS_CONNECTED, + .attr_type = ATTR_TYPE_INTEGER, + .attr_value = CTX_SCHED_ATTR_VALUE_FALSE + }, + [JOB_TRIGGER_EVENT_WIFI_STATE_DISABLED] = { + .key = AUL_K_JOB_TRIG_WIFI_STATE_DISABLED, + .uri = CTX_SCHED_URI_WIFI, + .attr_name = CTX_SCHED_ATTR_NAME_STATE, + .attr_type = ATTR_TYPE_STRING, + .attr_value = CTX_SCHED_ATTR_VALUE_DISABLED + }, + [JOB_TRIGGER_EVENT_WIFI_STATE_DISCONNECTED] = { + .key = AUL_K_JOB_TRIG_WIFI_STATE_DISCONNECTED, + .uri = CTX_SCHED_URI_WIFI, + .attr_name = CTX_SCHED_ATTR_NAME_STATE, + .attr_type = ATTR_TYPE_STRING, + .attr_value = CTX_SCHED_ATTR_VALUE_UNCONNECTED + }, + [JOB_TRIGGER_EVENT_WIFI_STATE_CONNECTED] = { + .key = AUL_K_JOB_TRIG_WIFI_STATE_CONNECTED, + .uri = CTX_SCHED_URI_WIFI, + .attr_name = CTX_SCHED_ATTR_NAME_STATE, + .attr_type = ATTR_TYPE_STRING, + .attr_value = CTX_SCHED_ATTR_VALUE_CONNECTED + }, +}; + +static void __destroy_job_service(gpointer data); +static void __finish_cynara(void); + +static bundle *__get_bundle_from_job(ctx_sched_job_h job) +{ + bundle *b; + bundle_raw *b_raw = NULL; + size_t len = 0; + int r; + + r = ctx_sched_job_get_user_data(job, (char **)&b_raw, &len); + if (r != 0) { + _E("Failed to get user data"); + return NULL; + } + + b = bundle_decode(b_raw, len); + if (b == NULL) + _E("Failed to decode bundle raw(%s)", b_raw); + + free(b_raw); + + return b; +} + +static void __add_job(ctx_sched_job_h job) +{ + bundle *b = NULL; + const char *app_id; + const char *job_id; + int id = 0; + int r; + + b = __get_bundle_from_job(job); + if (b == NULL) + return; + + app_id = aul_svc_get_appid(b); + if (app_id == NULL || strcmp(app_id, __handle.app_id) != 0) + goto end; + + job_id = bundle_get_val(b, AUL_K_JOB_ID); + if (job_id == NULL) + goto end; + + r = ctx_sched_job_get_id(job, &id); + if (r != 0) + goto end; + + if (g_hash_table_contains(__handle.job_id_table, job_id)) { + g_hash_table_replace(__handle.job_id_table, strdup(job_id), + GINT_TO_POINTER(id)); + } else { + g_hash_table_insert(__handle.job_id_table, strdup(job_id), + GINT_TO_POINTER(id)); + } + +end: + if (b) + bundle_free(b); +} + +static bool __foreach_job_cb(ctx_sched_h scheduler, ctx_sched_job_h job, + void *user_data) +{ + __add_job(job); + ctx_sched_job_destroy(job); + return true; +} + +static void __start_job_cb(ctx_sched_h scheduler, ctx_sched_job_h job, + bool timeout, void *user_data) +{ + bundle *b; + const char *job_id; + + _D("[__JOB__] Start Job"); + + b = __get_bundle_from_job(job); + if (b == NULL) { + ctx_sched_job_destroy(job); + return; + } + + job_id = bundle_get_val(b, AUL_K_JOB_ID); + if (job_id == NULL) { + ctx_sched_job_destroy(job); + bundle_free(b); + return; + } + + if (!g_hash_table_contains(__handle.job_id_table, job_id)) + __add_job(job); + + service_app_job_raise(SERVICE_APP_JOB_STATUS_START, job_id, b); + ctx_sched_job_destroy(job); + bundle_free(b); +} + +static void __stop_job_cb(ctx_sched_h scheduler, ctx_sched_job_h job, + void *user_data) +{ + bundle *b; + const char *job_id; + + _D("[__JOB__] Stop Job"); + + b = __get_bundle_from_job(job); + if (b == NULL) { + ctx_sched_job_destroy(job); + return; + } + + job_id = bundle_get_val(b, AUL_K_JOB_ID); + if (job_id == NULL) { + ctx_sched_job_destroy(job); + bundle_free(b); + return; + } + + service_app_job_raise(SERVICE_APP_JOB_STATUS_STOP, job_id, b); + ctx_sched_job_destroy(job); + bundle_free(b); +} + +static int __init_scheduler(ctx_sched_h *scheduler) +{ + ctx_sched_h handle = NULL; + int r; + + r = ctx_sched_create(&handle); + if (r != 0) + return r; + + r = ctx_sched_foreach_job(handle, __foreach_job_cb, NULL); + if (r != 0 && r != TIZEN_ERROR_NO_DATA) { + ctx_sched_destroy(handle); + return r; + } + + r = ctx_sched_set_job_cb(handle, __start_job_cb, __stop_job_cb, NULL); + if (r != 0) { + ctx_sched_destroy(handle); + return r; + } + + *scheduler = handle; + + return 0; +} + +API int job_scheduler_init(void) +{ + int r; + + if (__handle.initialized) + return JOB_ERROR_NONE; + + r = app_get_id(&__handle.app_id); + if (r != APP_ERROR_NONE) { + return job_error(JOB_ERROR_IO_ERROR, __FUNCTION__, + __LINE__, NULL); + } + + __handle.job_id_table = g_hash_table_new_full(g_str_hash, g_str_equal, + free, NULL); + if (__handle.job_id_table == NULL) { + free(__handle.app_id); + return job_error(JOB_ERROR_OUT_OF_MEMORY, __FUNCTION__, + __LINE__, NULL); + } + + r = __init_scheduler(&__handle.scheduler); + if (r != 0) { + g_hash_table_destroy(__handle.job_id_table); + free(__handle.app_id); + return job_error(JOB_ERROR_IO_ERROR, __FUNCTION__, + __LINE__, NULL); + } + + __handle.initialized = true; + + return JOB_ERROR_NONE; +} + +API int job_scheduler_finish(void) +{ + if (!__handle.initialized) + return JOB_ERROR_NOT_INITIALIZED; + + if (__handle.job_service_list) { + g_list_free_full(__handle.job_service_list, + __destroy_job_service); + __handle.job_service_list = NULL; + } + + if (__handle.job_id_table) { + g_hash_table_destroy(__handle.job_id_table); + __handle.job_id_table = NULL; + } + + if (__handle.scheduler) { + ctx_sched_destroy(__handle.scheduler); + __handle.scheduler = NULL; + + } + + if (__handle.app_id) { + free(__handle.app_id); + __handle.app_id = NULL; + } + + __finish_cynara(); + + __handle.initialized = false; + + return JOB_ERROR_NONE; +} + +static int __job_add_app_control(ctx_sched_job_h job, job_info_h job_info, + void *user_data) +{ + bundle *b = (bundle *)user_data; + int r; + + r = aul_svc_set_operation(b, AUL_SVC_OPERATION_DEFAULT); + if (r != AUL_SVC_RET_OK) { + _E("Failed to set operation"); + return JOB_ERROR_OUT_OF_MEMORY; + } + + r = aul_svc_set_appid(b, __handle.app_id); + if (r != AUL_SVC_RET_OK) { + _E("Failed to add appid(%s)", __handle.app_id); + return JOB_ERROR_OUT_OF_MEMORY; + } + + r = ctx_sched_job_set_app_control(job, b); + if (r != 0) { + _E("Failed to set app control (%d)", r); + return JOB_ERROR_IO_ERROR; + } + + return JOB_ERROR_NONE; +} + +static int __job_add_user_data(ctx_sched_job_h job, job_info_h job_info, + void *user_data) +{ + bundle *b = (bundle *)user_data; + bundle_raw *b_raw = NULL; + int len = 0; + int r; + + r = bundle_encode(b, &b_raw, &len); + if (r != BUNDLE_ERROR_NONE) { + _E("Failed to encode bundle"); + return JOB_ERROR_IO_ERROR; + } + + r = ctx_sched_job_set_user_data(job, (const char *)b_raw, len); + if (r != 0) { + _E("Failed to set user data"); + free(b_raw); + return JOB_ERROR_IO_ERROR; + } + free(b_raw); + + return JOB_ERROR_NONE; +} + +static int __get_id(const char *job_id) +{ + gpointer id; + + id = g_hash_table_lookup(__handle.job_id_table, job_id); + if (id == NULL) + return -1; + + return GPOINTER_TO_INT(id); +} + +static bool __exist_job(const char *job_id) +{ + ctx_sched_job_h job = NULL; + int id; + int r; + + id = __get_id(job_id); + if (id < 0) + return false; + + r = ctx_sched_get_job(__handle.scheduler, id, &job); + if (r != 0) { + g_hash_table_remove(__handle.job_id_table, job_id); + return false; + } + + ctx_sched_job_destroy(job); + + return true; +} + +static int __job_add_persistent(ctx_sched_job_h job, job_info_h job_info, + void *data) +{ + bundle *b = (bundle *)data; + bool persistent = false; + int r; + + r = job_info_get_persistent(job_info, &persistent); + if (r != JOB_ERROR_NONE) + return r; + + r = ctx_sched_job_set_persistent(job, persistent); + if (r != 0) { + _E("Failed to set persistent (%d)", r); + return r; + } + + if (persistent) + bundle_add(b, AUL_K_JOB_PERSISTENT, "true"); + + return JOB_ERROR_NONE; +} + +static int __job_add_one_time(ctx_sched_job_h job, job_info_h job_info, + void *data) +{ + bundle *b = (bundle *)data; + bool once = false; + int r; + + r = job_info_is_once(job_info, &once); + if (r != JOB_ERROR_NONE) + return r; + + r = ctx_sched_job_set_one_time(job, once); + if (r != 0) { + _E("Failed to set one time (%d)", r); + return r; + } + + if (once) + bundle_add(b, AUL_K_JOB_IS_ONCE, "true"); + + return JOB_ERROR_NONE; +} + +static int __job_add_requirement_timeout(ctx_sched_job_h job, + job_info_h job_info, void *data) +{ + bundle *b = (bundle *)data; + unsigned int timeout = 0; + char buf[32]; + int r; + + r = job_info_get_requirement_timeout(job_info, &timeout); + if (r != JOB_ERROR_NONE) + return r; + + r = ctx_sched_job_set_requirement_timeout(job, timeout); + if (r != 0) { + _E("Failed to set requirement timeout (%d)", r); + return r; + } + + if (timeout) { + snprintf(buf, sizeof(buf), "%u", timeout); + bundle_add(b, AUL_K_JOB_REQ_TIMEOUT, buf); + } + + return JOB_ERROR_NONE; +} + +static int __add_str_attr_ne(ctx_sched_job_context_h context, + const char *name, const char *value) +{ + int r; + + r = ctx_sched_job_context_prepare_attribute_str(context, name); + if (r != 0) { + _E("Failed to prepare string attribute(%s) - %d", name, r); + return r; + } + + r = ctx_sched_job_context_attribute_add_ne_str(context, name, value); + if (r != 0) { + _E("Failed to add string value(%s) - %d", value, r); + return r; + } + + return JOB_ERROR_NONE; +} + +static int __add_str_attr_eq(ctx_sched_job_context_h context, + const char *name, const char *value) +{ + int r; + + r = ctx_sched_job_context_prepare_attribute_str(context, name); + if (r != 0) { + _E("Failed to prepare string attribute(%s) - %d", name, r); + return r; + } + + r = ctx_sched_job_context_attribute_add_eq_str(context, name, value); + if (r != 0) { + _E("Failed to add string value(%s) - %d", value, r); + return r; + } + + return JOB_ERROR_NONE; +} + +static int __add_int_attr_eq(ctx_sched_job_context_h context, + const char *name, int value) +{ + int r; + + r = ctx_sched_job_context_prepare_attribute_int(context, name); + if (r != 0) { + _E("Failed to prepare integer attribute(%s) - %d", name, r); + return r; + } + + r = ctx_sched_job_context_attribute_add_eq_int(context, name, value); + if (r != 0) { + _E("Failed to add integer value(%s) - %d", value, r); + return r; + } + + return JOB_ERROR_NONE; +} + +static int __job_add_requires_battery_not_low(ctx_sched_job_h job, + job_info_h job_info, void *data) +{ + bundle *b = (bundle *)data; + ctx_sched_job_context_h requirement = NULL; + int battery_not_low = -1; + int r; + + r = job_info_is_requires_battery_not_low(job_info, &battery_not_low); + if (r != JOB_ERROR_NONE) + return r; + + if (battery_not_low == -1) + return JOB_ERROR_NONE; + + r = ctx_sched_job_requirement_create(CTX_SCHED_URI_BATTERY, false, + &requirement); + if (r != 0) { + _E("Failed to create requirement (%d)", r); + return r; + } + + if (battery_not_low) { + r = __add_str_attr_eq(requirement, CTX_SCHED_ATTR_NAME_LEVEL, + CTX_SCHED_ATTR_VALUE_HIGH); + } else { + r = __add_str_attr_ne(requirement, CTX_SCHED_ATTR_NAME_LEVEL, + CTX_SCHED_ATTR_VALUE_HIGH); + } + if (r != JOB_ERROR_NONE) { + ctx_sched_job_context_destroy(requirement); + return r; + } + + r = ctx_sched_job_add_requirement(job, requirement); + if (r != 0) { + _E("Failed to add requirement (%d)", r); + ctx_sched_job_context_destroy(requirement); + return r; + } + + if (battery_not_low) + bundle_add(b, AUL_K_JOB_REQ_BATTERY_NOT_LOW, "true"); + else + bundle_add(b, AUL_K_JOB_REQ_BATTERY_NOT_LOW, "false"); + + return JOB_ERROR_NONE; +} + +static int __job_add_requires_charging(ctx_sched_job_h job, + job_info_h job_info, void *data) +{ + bundle *b = (bundle *)data; + ctx_sched_job_context_h requirement = NULL; + int charging = -1; + int r; + + r = job_info_is_requires_charging(job_info, &charging); + if (r != JOB_ERROR_NONE) + return r; + + if (charging == -1) + return JOB_ERROR_NONE; + + r = ctx_sched_job_requirement_create(CTX_SCHED_URI_BATTERY, false, + &requirement); + if (r != 0) { + _E("Failed to create requirement (%d)", r); + return r; + } + + r = __add_int_attr_eq(requirement, CTX_SCHED_ATTR_NAME_IS_CHARGING, + charging); + if (r != JOB_ERROR_NONE) { + ctx_sched_job_context_destroy(requirement); + return r; + } + + r = ctx_sched_job_add_requirement(job, requirement); + if (r != 0) { + _E("Failed to add requirement (%d)", r); + ctx_sched_job_context_destroy(requirement); + return r; + } + + if (charging) + bundle_add(b, AUL_K_JOB_REQ_CHARGING, "true"); + else + bundle_add(b, AUL_K_JOB_REQ_CHARGING, "false"); + + return JOB_ERROR_NONE; +} + +static int __job_add_requires_wifi_connection(ctx_sched_job_h job, + job_info_h job_info, void *data) +{ + bundle *b = (bundle *)data; + ctx_sched_job_context_h requirement = NULL; + int wifi_connection = -1; + int r; + + r = job_info_is_requires_wifi_connection(job_info, &wifi_connection); + if (r != JOB_ERROR_NONE) + return r; + + if (wifi_connection == -1) + return JOB_ERROR_NONE; + + r = ctx_sched_job_requirement_create(CTX_SCHED_URI_WIFI, false, + &requirement); + if (r != 0) { + _E("Failed to create requirement (%d)", r); + return r; + } + + if (wifi_connection) { + r = __add_str_attr_eq(requirement, CTX_SCHED_ATTR_NAME_STATE, + CTX_SCHED_ATTR_VALUE_CONNECTED); + } else { + r = __add_str_attr_ne(requirement, CTX_SCHED_ATTR_NAME_STATE, + CTX_SCHED_ATTR_VALUE_CONNECTED); + } + if (r != JOB_ERROR_NONE) { + ctx_sched_job_context_destroy(requirement); + return r; + } + + r = ctx_sched_job_add_requirement(job, requirement); + if (r != 0) { + _E("Failed to add requirement (%d)", r); + ctx_sched_job_context_destroy(requirement); + return r; + } + + if (wifi_connection) + bundle_add(b, AUL_K_JOB_REQ_WIFI_CONNECTION, "true"); + else + bundle_add(b, AUL_K_JOB_REQ_WIFI_CONNECTION, "false"); + + return JOB_ERROR_NONE; +} + +static int __add_trigger_event(ctx_sched_job_h job, job_trigger_event_e event, + bundle *b) +{ + ctx_sched_job_context_h trigger = NULL; + int r; + + r = ctx_sched_job_trigger_create(job_trigger_map[event].uri, + &trigger); + if (r != 0) { + _E("Failed to create trigger (%d)", r); + return r; + } + + if (job_trigger_map[event].attr_type == ATTR_TYPE_INTEGER) { + r = __add_int_attr_eq(trigger, job_trigger_map[event].attr_name, + GPOINTER_TO_INT(job_trigger_map[event].attr_value)); + } else { + r = __add_str_attr_eq(trigger, job_trigger_map[event].attr_name, + (const char *)job_trigger_map[event].attr_value); + } + if (r != JOB_ERROR_NONE) { + ctx_sched_job_context_destroy(trigger); + return r; + } + + r = ctx_sched_job_add_trigger(job, trigger); + if (r != 0) { + _E("Failed to add trigger (%d)", r); + ctx_sched_job_context_destroy(trigger); + return r; + } + + bundle_add(b, job_trigger_map[event].key, "true"); + + return JOB_ERROR_NONE; +} + +static int __job_add_trigger_events(ctx_sched_job_h job, + job_info_h job_info, void *data) +{ + bundle *b = (bundle *)data; + bool *trigger_list = NULL; + unsigned int interval = 0; + int i; + int r; + + job_info_get_periodic(job_info, &interval); + if (interval != 0) + return JOB_ERROR_NONE; + + r = job_info_get_trigger_list(job_info, &trigger_list); + if (r != JOB_ERROR_NONE) + return r; + + for (i = 0; i < ARRAY_SIZE(job_trigger_map); ++i) { + if (trigger_list[i]) { + _D("[%d] true", i); + r = __add_trigger_event(job, i, b); + if (r != JOB_ERROR_NONE) + return r; + } + } + + return JOB_ERROR_NONE; +} + +static job_add_cb job_constructor[] = { + __job_add_persistent, + __job_add_one_time, + __job_add_requirement_timeout, + __job_add_requires_battery_not_low, + __job_add_requires_charging, + __job_add_requires_wifi_connection, + __job_add_trigger_events, + __job_add_app_control, + __job_add_user_data, +}; + +static ctx_sched_job_h __create_job(job_info_h job_info, const char *job_id) +{ + unsigned int interval = 0; + ctx_sched_job_h job = NULL; + char buf[32]; + bundle *b; + int i; + int r; + + b = bundle_create(); + if (b == NULL) { + _E("Failed to create bundle"); + return NULL; + } + + r = bundle_add(b, AUL_K_JOB_ID, job_id); + if (r != BUNDLE_ERROR_NONE) { + _E("Failed to add job id(%s)", job_id); + bundle_free(b); + return NULL; + } + + job_info_get_periodic(job_info, &interval); + if (interval == 0) { + r = ctx_sched_job_create_on_demand(&job); + } else { + snprintf(buf, sizeof(buf), "%u", interval); + bundle_add(b, AUL_K_JOB_PERIODIC, buf); + r = ctx_sched_job_create_periodic(interval, 0, &job); + } + if (r != 0) { + _E("Failed to create job (%d)", r); + bundle_free(b); + return NULL; + } + + for (i = 0; i < ARRAY_SIZE(job_constructor); ++i) { + if (job_constructor[i]) { + r = job_constructor[i](job, job_info, b); + if (r != JOB_ERROR_NONE) { + ctx_sched_job_destroy(job); + bundle_free(b); + return NULL; + } + } + } + bundle_free(b); + + return job; +} + +API int job_scheduler_schedule(job_info_h job_info, const char *job_id) +{ + ctx_sched_job_h job; + int id = 0; + int r; + + if (job_info == NULL || job_id == NULL) { + return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, + __LINE__, NULL); + } + + if (!__handle.initialized) { + return job_error(JOB_ERROR_NOT_INITIALIZED, __FUNCTION__, + __LINE__, NULL); + } + + if (__exist_job(job_id)) { + return job_error(JOB_ERROR_ALREADY_EXIST, __FUNCTION__, + __LINE__, NULL); + } + + job = __create_job(job_info, job_id); + if (job == NULL) { + return job_error(JOB_ERROR_OUT_OF_MEMORY, __FUNCTION__, + __LINE__, NULL); + } + + r = job_info_set_job_id(job_info, job_id); + if (r != JOB_ERROR_NONE) { + ctx_sched_job_destroy(job); + return job_error(r, __FUNCTION__, __LINE__, NULL); + } + + r = ctx_sched_schedule(__handle.scheduler, job, &id); + if (r != 0) { + ctx_sched_job_destroy(job); + if (r == JOB_ERROR_PERMISSION_DENIED) { + return job_error(JOB_ERROR_PERMISSION_DENIED, + __FUNCTION__, __LINE__, NULL); + } else { + return job_error(JOB_ERROR_IO_ERROR, + __FUNCTION__, __LINE__, NULL); + } + } + ctx_sched_job_destroy(job); + g_hash_table_insert(__handle.job_id_table, strdup(job_id), + GINT_TO_POINTER(id)); + + return JOB_ERROR_NONE; +} + +API int job_scheduler_cancel(const char *job_id) +{ + int id; + int r; + + if (job_id == NULL) { + return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, + __LINE__, NULL); + } + + if (!__handle.initialized) { + return job_error(JOB_ERROR_NOT_INITIALIZED, __FUNCTION__, + __LINE__, NULL); + } + + id = __get_id(job_id); + if (id < 0) { + return job_error(JOB_ERROR_NO_SUCH_JOB, __FUNCTION__, + __LINE__, NULL); + } + + r = ctx_sched_cancel(__handle.scheduler, id); + if (r != 0) { + return job_error(JOB_ERROR_IO_ERROR, __FUNCTION__, + __LINE__, NULL); + } + + g_hash_table_remove(__handle.job_id_table, job_id); + + return JOB_ERROR_NONE; +} + +API int job_scheduler_cancel_all(void) +{ + int r; + + if (!__handle.initialized) { + return job_error(JOB_ERROR_NOT_INITIALIZED, __FUNCTION__, + __LINE__, NULL); + } + + r = ctx_sched_cancel_all(__handle.scheduler); + if (r != 0) { + return job_error(JOB_ERROR_IO_ERROR, __FUNCTION__, + __LINE__, NULL); + } + + g_hash_table_remove_all(__handle.job_id_table); + + return JOB_ERROR_NONE; +} + +API int job_scheduler_notify_job_finished(const char *job_id) +{ + int id; + int r; + + if (job_id == NULL) { + return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, + __LINE__, NULL); + } + + if (!__handle.initialized) { + return job_error(JOB_ERROR_NOT_INITIALIZED, __FUNCTION__, + __LINE__, NULL); + } + + id = __get_id(job_id); + if (id < 0) { + return job_error(JOB_ERROR_NO_SUCH_JOB, __FUNCTION__, + __LINE__, NULL); + } + + r = ctx_sched_job_finished(__handle.scheduler, id); + if (r != 0) { + return job_error(JOB_ERROR_IO_ERROR, __FUNCTION__, + __LINE__, NULL); + } + + r = service_app_job_finished(job_id); + if (r != 0) { + return job_error(JOB_ERROR_IO_ERROR, __FUNCTION__, + __LINE__, NULL); + } + + return JOB_ERROR_NONE; +} + +static void __foreach_job_id_cb(gpointer key, gpointer value, + gpointer user_data) +{ + const char *job_id = (const char *)key; + int id = GPOINTER_TO_INT(value); + GList **list = (GList **)user_data; + ctx_sched_job_h job = NULL; + job_info_h job_info = NULL; + bundle *b; + int r; + + r = ctx_sched_get_job(__handle.scheduler, id, &job); + if (r != 0) { + _E("Failed to get job from job id(%s:%d)", job_id, id); + return; + } + + b = __get_bundle_from_job(job); + if (b == NULL) { + ctx_sched_job_destroy(job); + return; + } + ctx_sched_job_destroy(job); + + r = job_info_create_with_bundle(b, &job_info); + if (r != JOB_ERROR_NONE) { + bundle_free(b); + return; + } + bundle_free(b); + + r = job_info_set_job_id(job_info, job_id); + if (r != JOB_ERROR_NONE) { + job_info_destroy(job_info); + return; + } + + *list = g_list_append(*list, job_info); +} + +API int job_scheduler_foreach_job(job_scheduler_foreach_job_cb callback, + void *user_data) +{ + job_info_h job_info; + GList *list = NULL; + GList *iter; + + if (callback == NULL) { + return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, + __LINE__, NULL); + } + + if (!__handle.initialized) { + return job_error(JOB_ERROR_NOT_INITIALIZED, __FUNCTION__, + __LINE__, NULL); + } + + g_hash_table_foreach(__handle.job_id_table, __foreach_job_id_cb, &list); + + if (list == NULL) { + return job_error(JOB_ERROR_NO_SUCH_JOB, __FUNCTION__, + __LINE__, NULL); + } + + iter = list; + while (iter) { + job_info = (job_info_h)iter->data; + iter = iter->next; + if (!callback(job_info, user_data)) + break; + } + + g_list_free_full(list, (GDestroyNotify)job_info_destroy); + + return JOB_ERROR_NONE; +} + +static int __job_service_cb(int event, const char *job_id, bundle *job_data, + void *user_data) +{ + struct job_service_s *handle = (struct job_service_s *)user_data; + job_info_h job_info = NULL; + int r; + + r = job_info_create_with_bundle(job_data, &job_info); + if (r != JOB_ERROR_NONE) + return -1; + + r = job_info_set_job_id(job_info, job_id); + if (r != JOB_ERROR_NONE) { + job_info_destroy(job_info); + return -1; + } + + if (event == SERVICE_APP_JOB_STATUS_START) + handle->callback.start(job_info, handle->user_data); + else + handle->callback.stop(job_info, handle->user_data); + + job_info_destroy(job_info); + + return 0; +} + +static struct job_service_s *__create_job_service(const char *job_id, + job_service_callback_s *callback, void *user_data) +{ + struct job_service_s *service; + + service = calloc(1, sizeof(struct job_service_s)); + if (service == NULL) { + _E("Out of memory"); + return NULL; + } + + service->handle = service_app_add_job_handler(job_id, __job_service_cb, + service); + if (service->handle == NULL) { + _E("Out of memory"); + free(service); + return NULL; + } + + service->callback = *callback; + service->user_data = user_data; + + return service; +} + +static void __destroy_job_service(gpointer data) +{ + struct job_service_s *service = (struct job_service_s *)data; + + if (service == NULL) + return; + + if (service->handle) + service_app_remove_job_handler(service->handle); + + free(service); +} + +API int job_scheduler_service_add(const char *job_id, + job_service_callback_s *callback, void *user_data, + job_service_h *job_service) +{ + struct job_service_s *service; + + if (job_service == NULL || job_id == NULL || callback == NULL || + callback->start == NULL || callback->stop == NULL) { + return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, + __LINE__, NULL); + } + + if (!__handle.initialized) { + return job_error(JOB_ERROR_NOT_INITIALIZED, __FUNCTION__, + __LINE__, NULL); + } + + service = __create_job_service(job_id, callback, user_data); + if (service == NULL) { + return job_error(JOB_ERROR_OUT_OF_MEMORY, __FUNCTION__, + __LINE__, NULL); + } + + __handle.job_service_list = g_list_append(__handle.job_service_list, + service); + + *job_service = service; + + return JOB_ERROR_NONE; +} + +API int job_scheduler_service_remove(job_service_h job_service) +{ + gint index; + + if (job_service == NULL) { + return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, + __LINE__, NULL); + } + + index = g_list_index(__handle.job_service_list, job_service); + if (index < 0) { + return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, + __LINE__, NULL); + } + + if (!__handle.initialized) { + return job_error(JOB_ERROR_NOT_INITIALIZED, __FUNCTION__, + __LINE__, NULL); + } + + __handle.job_service_list = g_list_remove(__handle.job_service_list, + job_service); + + __destroy_job_service(job_service); + + return JOB_ERROR_NONE; +} + +int job_scheduler_verify_requirement(const char *uri) +{ + bool supported = false; + int r; + + if (uri == NULL) { + return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, + __LINE__, NULL); + } + + if (!__handle.initialized) { + return job_error(JOB_ERROR_NOT_INITIALIZED, __FUNCTION__, + __LINE__, NULL); + } + + r = ctx_sched_job_requirement_is_supported(__handle.scheduler, + uri, &supported); + if (r != 0) { + return job_error(JOB_ERROR_IO_ERROR, __FUNCTION__, + __LINE__, NULL); + } + + if (!supported) { + return job_error(JOB_ERROR_NOT_SUPPORTED, __FUNCTION__, + __LINE__, NULL); + } + + return JOB_ERROR_NONE; +} + +int job_scheduler_verify_trigger(const char *uri) +{ + bool supported = false; + int r; + + if (uri == NULL) { + return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, + __LINE__, NULL); + } + + if (!__handle.initialized) { + return job_error(JOB_ERROR_NOT_INITIALIZED, __FUNCTION__, + __LINE__, NULL); + } + + r = ctx_sched_job_trigger_is_supported(__handle.scheduler, + uri, &supported); + if (r != 0) { + return job_error(JOB_ERROR_IO_ERROR, __FUNCTION__, + __LINE__, NULL); + } + + if (!supported) { + return job_error(JOB_ERROR_NOT_SUPPORTED, __FUNCTION__, + __LINE__, NULL); + } + + return JOB_ERROR_NONE; +} + +static int __init_cynara(void) +{ + cynara_configuration *conf = NULL; + int r; + + if (__cynara.initialized) + return 0; + + r = cynara_creds_self_get_client(CLIENT_METHOD_DEFAULT, + &__cynara.client); + if (r != CYNARA_API_SUCCESS) { + _E("Failed to get client"); + return -1; + } + + r = cynara_creds_self_get_user(CLIENT_METHOD_DEFAULT, &__cynara.user); + if (r != CYNARA_API_SUCCESS) { + _E("Failed to get user"); + goto err; + } + + __cynara.session = cynara_session_from_pid(getpid()); + if (__cynara.session == NULL) { + _E("Failed to get session"); + goto err; + } + + r = cynara_configuration_create(&conf); + if (r != CYNARA_API_SUCCESS) { + _E("Failed to create cynara configuration"); + goto err; + } + + r = cynara_configuration_set_cache_size(conf, 10); + if (r != CYNARA_API_SUCCESS) { + _E("Failed to set cache size"); + goto err; + } + + r = cynara_initialize(&__cynara.handle, conf); + cynara_configuration_destroy(conf); + conf = NULL; + if (r != CYNARA_API_SUCCESS) { + _E("Failed to initialize cynara"); + goto err; + } + + __cynara.initialized = true; + + _I("Cynara initialized"); + + return 0; +err: + if (conf) + cynara_configuration_destroy(conf); + + if (__cynara.client) { + free(__cynara.client); + __cynara.client = NULL; + } + + if (__cynara.user) { + free(__cynara.user); + __cynara.user = NULL; + } + + if (__cynara.session) { + free(__cynara.session); + __cynara.session = NULL; + } + + return -1; +} + +static void __finish_cynara(void) +{ + if (!__cynara.initialized) + return; + + if (__cynara.handle) { + cynara_finish(__cynara.handle); + __cynara.handle = NULL; + } + + if (__cynara.client) { + free(__cynara.client); + __cynara.client = NULL; + } + + if (__cynara.user) { + free(__cynara.user); + __cynara.user = NULL; + } + + if (__cynara.session) { + free(__cynara.session); + __cynara.session = NULL; + } + + __cynara.initialized = false; +} + +int job_scheduler_verify_privilege(const char *priv) +{ + int r; + + if (priv == NULL) + return JOB_ERROR_NONE; + + r = __init_cynara(); + if (r != 0) + return JOB_ERROR_IO_ERROR; + + r = cynara_check(__cynara.handle, __cynara.client, __cynara.session, + __cynara.user, priv); + if (r != CYNARA_API_ACCESS_ALLOWED) { + _E("Failed to check cynara access (%d)", r); + return JOB_ERROR_PERMISSION_DENIED; + } + + return JOB_ERROR_NONE; +} diff --git a/src/job_scheduler_private.h b/src/job_scheduler_private.h new file mode 100644 index 0000000..f8fe088 --- /dev/null +++ b/src/job_scheduler_private.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd 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 __TIZEN_APPFW_JOB_SCHEDULER_PRIVATE_H__ +#define __TIZEN_APPFW_JOB_SCHEDULER_PRIVATE_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +int job_scheduler_verify_requirement(const char *uri); + +int job_scheduler_verify_trigger(const char *uri); + +int job_scheduler_verify_privilege(const char *priv); + +#ifdef __cplusplus +} +#endif + +#endif /* __TIZEN_APPFW_JOB_SCHEDULER_PRIVATE_H__ */ -- 2.7.4 From edc2ccf95b4472e9ae49362030956796bd9bd48b Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Tue, 26 Sep 2017 16:42:57 +0900 Subject: [PATCH 03/11] Requires amd-mod-job-scheduler package To use this API, the amd-mod-job-scheduler package is needed. Change-Id: I8240d0175c462d1701516d7303b844fabbe96076 Signed-off-by: Hwankyu Jhun --- packaging/capi-appfw-job-scheduler.spec | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packaging/capi-appfw-job-scheduler.spec b/packaging/capi-appfw-job-scheduler.spec index 8e82fc9..0888cdf 100644 --- a/packaging/capi-appfw-job-scheduler.spec +++ b/packaging/capi-appfw-job-scheduler.spec @@ -20,6 +20,8 @@ BuildRequires: pkgconfig(cynara-creds-self) BuildRequires: pkgconfig(cynara-session) BuildRequires: pkgconfig(cynara-client) +Requires: amd-mod-job-scheduler + %description Job Scheduler library in Tizen C API -- 2.7.4 From 2d79c42328bb3fec905be1f1e881c1708933ca43 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Tue, 26 Sep 2017 17:00:37 +0900 Subject: [PATCH 04/11] Fix the descriptions Change-Id: I912e4a77a610fc7188ad6e23571ef4e337b5c7b7 Signed-off-by: Hwankyu Jhun --- doc/appfw_job_scheduler_doc.h | 8 ++++---- include/job_error.h | 2 +- include/job_info.h | 2 +- include/job_scheduler.h | 9 +++++---- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/doc/appfw_job_scheduler_doc.h b/doc/appfw_job_scheduler_doc.h index 211f172..62d8e15 100755 --- a/doc/appfw_job_scheduler_doc.h +++ b/doc/appfw_job_scheduler_doc.h @@ -35,10 +35,10 @@ * - http://tizen.org/feature/battery * - http://tizen.org/feature/usb.host * - * It is recommended to design feature related code in your application for reliability. - * You can check if the device supports the related features for this API by using @ref CAPI_SYSTEM_INFO_MODULE, thereby controlling the procedure of your application. - * To ensure your application is only running on the device with specific features, please define the features in your manifest file using the manifest editor in the SDK. - * More details on featuring your application can be found from Feature Element. + * It is recommended to use features in your application for reliability. + * You can check if the device supports the related features for this API by using @ref CAPI_SYSTEM_INFO_MODULE, and control your application's actions accordingly. + * To ensure your application is only running on devices with specific features, please define the features in your manifest file using the manifest editor in the SDK. + * More details on using features in your application can be found in Feature Element. */ #endif /* __TIZEN_APPFW_JOB_SCHEDULER_DOC_H__ */ diff --git a/include/job_error.h b/include/job_error.h index a58aa50..3d5f6b5 100644 --- a/include/job_error.h +++ b/include/job_error.h @@ -24,7 +24,7 @@ extern "C" { #endif /** - * @addtogroup CAPI_APPLICATION_MODULE + * @addtogroup CAPI_JOB_SCHEDULER_MODULE * @{ */ diff --git a/include/job_info.h b/include/job_info.h index 27bbf5e..cf4a6d2 100644 --- a/include/job_info.h +++ b/include/job_info.h @@ -24,7 +24,7 @@ extern "C" { #endif /** - * @addtogroup CAPI_APPLICATION_MODULE + * @addtogroup CAPI_JOB_SCHEDULER_MODULE * @{ */ diff --git a/include/job_scheduler.h b/include/job_scheduler.h index 3ff74cd..ff3ae46 100644 --- a/include/job_scheduler.h +++ b/include/job_scheduler.h @@ -25,7 +25,7 @@ extern "C" { #endif /** - * @addtogroup CAPI_APPLICATION_MODULE + * @addtogroup CAPI_JOB_SCHEDULER_MODULE * @{ */ @@ -150,7 +150,7 @@ int job_scheduler_cancel(const char *job_id); * @brief Cancels all scheduled jobs. * @since_tizen 4.0 * @return @c 0 on success, - * otherwise a nagative error value + * otherwise a negative error value * @retval #JOB_ERROR_NONE Successful * @retval #JOB_ERROR_NOT_INITIALIZED Not initialized * @retval #JOB_ERROR_IO_ERROR IO error @@ -160,7 +160,7 @@ int job_scheduler_cancel(const char *job_id); int job_scheduler_cancel_all(void); /** - * @brief Notify that the job is finished. + * @brief Notifies that the job is finished. * @since_tizen 4.0 * @remarks If the job has been launched, the system kept awake for a while. * It is highly recommended to notify that the job is done, @@ -197,6 +197,7 @@ int job_scheduler_foreach_job(job_scheduler_foreach_job_cb callback, void *user_ /** * @brief Adds job service handler for receiving the information of the scheduled job. * @since_tizen 4.0 + * @remarks The @a job_service handle should be released using job_scheduler_service_remove(). * @param[in] job_id The ID of the job * @param[in] callback The set of callback functions to handle jobs * @param[in] user_data The user data to be passed to the callback function @@ -225,7 +226,7 @@ int job_scheduler_service_add(const char *job_id, job_service_callback_s *callba * otherwise a negative error value * @retval #JOB_ERROR_NONE Successful * @retval #JOB_ERROR_INVALID_PARAMETER Invalid parameter - * @retval #JOB_ERROR_NOT_INITIALIZED Not initialzed + * @retval #JOB_ERROR_NOT_INITIALIZED Not initialized * @pre job_scheduler_init() MUST be called. * @see job_scheduler_init() * @see job_scheduler_service_add() -- 2.7.4 From e408fce8ed41f786a5214d69822f1c3684f979d4 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Tue, 26 Sep 2017 16:49:22 +0900 Subject: [PATCH 05/11] Release version 0.1.1 Changes: - Fix the descriptions - Requires amd-mod-job-scheduler package - Add Job Scheduler API Change-Id: I3e36d32113e5a4e4b8a8ff8f5f60b5477c79bb43 Signed-off-by: Hwankyu Jhun --- packaging/capi-appfw-job-scheduler.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/capi-appfw-job-scheduler.spec b/packaging/capi-appfw-job-scheduler.spec index 0888cdf..2d61eb7 100644 --- a/packaging/capi-appfw-job-scheduler.spec +++ b/packaging/capi-appfw-job-scheduler.spec @@ -1,6 +1,6 @@ Name: capi-appfw-job-scheduler Summary: Job Scheduler library -Version: 0.1.0 +Version: 0.1.1 Release: 0 Group: System/API License: Apache-2.0 -- 2.7.4 From 44661f5cb8005df810e9943af7bf1b61c9c98a0f Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Tue, 10 Oct 2017 14:56:27 +0900 Subject: [PATCH 06/11] Fix the description Change-Id: I064a60a1d3cdb1cd1ea196e3cea6514392169bbe Signed-off-by: Hwankyu Jhun --- doc/appfw_job_scheduler_doc.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/appfw_job_scheduler_doc.h b/doc/appfw_job_scheduler_doc.h index 62d8e15..d0ee39b 100755 --- a/doc/appfw_job_scheduler_doc.h +++ b/doc/appfw_job_scheduler_doc.h @@ -36,7 +36,7 @@ * - http://tizen.org/feature/usb.host * * It is recommended to use features in your application for reliability. - * You can check if the device supports the related features for this API by using @ref CAPI_SYSTEM_INFO_MODULE, and control your application's actions accordingly. + * You can check if the device supports the related features for this API by using @ref CAPI_SYSTEM_SYSTEM_INFO_MODULE, and control your application's actions accordingly. * To ensure your application is only running on devices with specific features, please define the features in your manifest file using the manifest editor in the SDK. * More details on using features in your application can be found in Feature Element. */ -- 2.7.4 From baa29ead0af914d14462307ccaa36fbecc4cdfe2 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Wed, 11 Oct 2017 09:38:41 +0900 Subject: [PATCH 07/11] Exclude some lines from lcov Change-Id: Ib090411d2be328f31153b393a660a52cd2711989 Signed-off-by: Hwankyu Jhun --- src/job_error.c | 10 ++-- src/job_info.c | 38 +++++++++++++-- src/job_scheduler.c | 132 +++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 171 insertions(+), 9 deletions(-) diff --git a/src/job_error.c b/src/job_error.c index a2d00d9..f3661cb 100644 --- a/src/job_error.c +++ b/src/job_error.c @@ -31,9 +31,9 @@ static const char *__job_error_to_string(job_error_e error) case JOB_ERROR_INVALID_PARAMETER: return "Invalid parameter"; case JOB_ERROR_OUT_OF_MEMORY: - return "Out of memory"; + return "Out of memory"; /* LCOV_EXCL_LINE */ case JOB_ERROR_IO_ERROR: - return "IO error"; + return "IO error"; /* LCOV_EXCL_LINE */ case JOB_ERROR_NOT_INITIALIZED: return "Not initialized"; case JOB_ERROR_NO_SUCH_JOB: @@ -41,9 +41,9 @@ static const char *__job_error_to_string(job_error_e error) case JOB_ERROR_ALREADY_EXIST: return "Already exist"; case JOB_ERROR_PERMISSION_DENIED: - return "Permission denied"; + return "Permission denied"; /* LCOV_EXCL_LINE */ default: - return "Unknown"; + return "Unknown"; /* LCOV_EXCL_LINE */ } } @@ -51,10 +51,12 @@ int job_error(job_error_e error, const char *function, const int line, const char *description) { if (description) { + /* LCOV_EXCL_START */ LOGE("[%s][%d] %s(0x%08x) - %s", function, line, __job_error_to_string(error), error, description); + /* LCOV_EXCL_STOP */ } else { LOGE("[%s][%d] %s(0x%08x)", function, line, diff --git a/src/job_info.c b/src/job_info.c index 5031882..2396d73 100644 --- a/src/job_info.c +++ b/src/job_info.c @@ -84,8 +84,10 @@ API int job_info_create(job_info_h *job_info) info = calloc(1, sizeof(struct job_info_s)); if (info == NULL) { + /* LCOV_EXCL_START */ return job_error(JOB_ERROR_OUT_OF_MEMORY, __FUNCTION__, __LINE__, NULL); + /* LCOV_EXCL_STOP */ } info->battery_not_low = -1; @@ -139,8 +141,10 @@ API int job_info_set_periodic(job_info_h job_info, unsigned int interval) int job_info_get_periodic(job_info_h job_info, unsigned int *interval) { if (job_info == NULL || interval == NULL) { + /* LCOV_EXCL_START */ return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, __LINE__, NULL); + /* LCOV_EXCL_STOP */ } *interval = job_info->interval; @@ -163,8 +167,10 @@ API int job_info_set_persistent(job_info_h job_info, bool persistent) int job_info_get_persistent(job_info_h job_info, bool *persistent) { if (job_info == NULL || persistent == NULL) { + /* LCOV_EXCL_START */ return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, __LINE__, NULL); + /* LCOV_EXCL_STOP */ } *persistent = job_info->persistent; @@ -187,8 +193,10 @@ API int job_info_set_once(job_info_h job_info, bool once) int job_info_is_once(job_info_h job_info, bool *once) { if (job_info == NULL || once == NULL) { + /* LCOV_EXCL_START */ return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, __LINE__, NULL); + /* LCOV_EXCL_STOP */ } *once = job_info->once; @@ -212,8 +220,10 @@ API int job_info_set_requirement_timeout(job_info_h job_info, int job_info_get_requirement_timeout(job_info_h job_info, unsigned int *timeout) { if (job_info == NULL || timeout == NULL) { + /* LCOV_EXCL_START */ return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, __LINE__, NULL); + /* LCOV_EXCL_STOP */ } *timeout = job_info->timeout; @@ -244,8 +254,10 @@ int job_info_is_requires_battery_not_low(job_info_h job_info, int *battery_not_low) { if (job_info == NULL || battery_not_low == NULL) { + /* LCOV_EXCL_START */ return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, __LINE__, NULL); + /* LCOV_EXCL_STOP */ } *battery_not_low = job_info->battery_not_low; @@ -274,8 +286,10 @@ API int job_info_set_requires_charging(job_info_h job_info, bool charging) int job_info_is_requires_charging(job_info_h job_info, int *charging) { if (job_info == NULL || charging == NULL) { + /* LCOV_EXCL_START */ return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, __LINE__, NULL); + /* LCOV_EXCL_STOP */ } *charging = job_info->charging; @@ -311,7 +325,7 @@ API int job_info_set_requires_wifi_connection(job_info_h job_info, r = job_scheduler_verify_requirement(CTX_SCHED_URI_WIFI); if (r != JOB_ERROR_NONE) - return job_error(r, __FUNCTION__, __LINE__, NULL); + return job_error(r, __FUNCTION__, __LINE__, NULL); /* LCOV_EXCL_LINE */ job_info->wifi_connection = wifi_connection; @@ -322,8 +336,10 @@ int job_info_is_requires_wifi_connection(job_info_h job_info, int *wifi_connection) { if (job_info == NULL || wifi_connection == NULL) { + /* LCOV_EXCL_START */ return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, __LINE__, NULL); + /* LCOV_EXCL_STOP */ } *wifi_connection = job_info->wifi_connection; @@ -347,7 +363,7 @@ API int job_info_add_trigger_event(job_info_h job_info, privilege = __get_privilege(event); r = job_scheduler_verify_privilege(privilege); if (r != JOB_ERROR_NONE) - return job_error(r, __FUNCTION__, __LINE__, NULL); + return job_error(r, __FUNCTION__, __LINE__, NULL); /* LCOV_EXCL_LINE */ r = job_scheduler_verify_trigger(trigger_uri[event]); if (r != JOB_ERROR_NONE) @@ -385,16 +401,20 @@ API int job_info_clone(job_info_h job_info, job_info_h *clone) info = calloc(1, sizeof(struct job_info_s)); if (info == NULL) { + /* LCOV_EXCL_START */ return job_error(JOB_ERROR_OUT_OF_MEMORY, __FUNCTION__, __LINE__, NULL); + /* LCOV_EXCL_STOP */ } if (job_info->job_id) { info->job_id = strdup(job_info->job_id); if (info->job_id == NULL) { + /* LCOV_EXCL_START */ free(info); return job_error(JOB_ERROR_OUT_OF_MEMORY, __FUNCTION__, __LINE__, NULL); + /* LCOV_EXCL_STOP */ } } @@ -417,8 +437,10 @@ API int job_info_clone(job_info_h job_info, job_info_h *clone) int job_info_get_trigger_list(job_info_h job_info, bool **trigger_list) { if (job_info == NULL || trigger_list == NULL) { + /* LCOV_EXCL_START */ return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, __LINE__, NULL); + /* LCOV_EXCL_STOP */ } *trigger_list = job_info->trigger_list; @@ -429,17 +451,21 @@ int job_info_get_trigger_list(job_info_h job_info, bool **trigger_list) int job_info_set_job_id(job_info_h job_info, const char *job_id) { if (job_info == NULL || job_id == NULL) { + /* LCOV_EXCL_START */ return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, __LINE__, NULL); + /* LCOV_EXCL_STOP */ } if (job_info->job_id) - free(job_info->job_id); + free(job_info->job_id); /* LCOV_EXCL_LINE */ job_info->job_id = strdup(job_id); if (job_info->job_id == NULL) { + /* LCOV_EXCL_START */ return job_error(JOB_ERROR_OUT_OF_MEMORY, __FUNCTION__, __LINE__, NULL); + /* LCOV_EXCL_STOP */ } return JOB_ERROR_NONE; @@ -454,8 +480,10 @@ API int job_info_get_job_id(job_info_h job_info, char **job_id) *job_id = strdup(job_info->job_id); if (*job_id == NULL) { + /* LCOV_EXCL_START */ return job_error(JOB_ERROR_OUT_OF_MEMORY, __FUNCTION__, __LINE__, NULL); + /* LCOV_EXCL_STOP */ } return JOB_ERROR_NONE; @@ -557,14 +585,18 @@ int job_info_create_with_bundle(bundle *b, job_info_h *job_info) struct job_info_s *info; if (b == NULL || job_info == NULL) { + /* LCOV_EXCL_START */ return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, __LINE__, NULL); + /* LCOV_EXCL_STOP */ } info = calloc(1, sizeof(struct job_info_s)); if (info == NULL) { + /* LCOV_EXCL_START */ return job_error(JOB_ERROR_OUT_OF_MEMORY, __FUNCTION__, __LINE__, NULL); + /* LCOV_EXCL_STOP */ } __set_job_info(info, b); diff --git a/src/job_scheduler.c b/src/job_scheduler.c index 349d330..97345fd 100644 --- a/src/job_scheduler.c +++ b/src/job_scheduler.c @@ -252,7 +252,7 @@ static bundle *__get_bundle_from_job(ctx_sched_job_h job) b = bundle_decode(b_raw, len); if (b == NULL) - _E("Failed to decode bundle raw(%s)", b_raw); + _E("Failed to decode bundle raw(%s)", b_raw); /* LCOV_EXCL_LINE */ free(b_raw); @@ -314,15 +314,19 @@ static void __start_job_cb(ctx_sched_h scheduler, ctx_sched_job_h job, b = __get_bundle_from_job(job); if (b == NULL) { + /* LCOV_EXCL_START */ ctx_sched_job_destroy(job); return; + /* LCOV_EXCL_STOP */ } job_id = bundle_get_val(b, AUL_K_JOB_ID); if (job_id == NULL) { + /* LCOV_EXCL_START */ ctx_sched_job_destroy(job); bundle_free(b); return; + /* LCOV_EXCL_STOP */ } if (!g_hash_table_contains(__handle.job_id_table, job_id)) @@ -343,15 +347,19 @@ static void __stop_job_cb(ctx_sched_h scheduler, ctx_sched_job_h job, b = __get_bundle_from_job(job); if (b == NULL) { + /* LCOV_EXCL_START */ ctx_sched_job_destroy(job); return; + /* LCOV_EXCL_STOP */ } job_id = bundle_get_val(b, AUL_K_JOB_ID); if (job_id == NULL) { + /* LCOV_EXCL_START */ ctx_sched_job_destroy(job); bundle_free(b); return; + /* LCOV_EXCL_STOP */ } service_app_job_raise(SERVICE_APP_JOB_STATUS_STOP, job_id, b); @@ -366,18 +374,22 @@ static int __init_scheduler(ctx_sched_h *scheduler) r = ctx_sched_create(&handle); if (r != 0) - return r; + return r; /* LCOV_EXCL_LINE */ r = ctx_sched_foreach_job(handle, __foreach_job_cb, NULL); if (r != 0 && r != TIZEN_ERROR_NO_DATA) { + /* LCOV_EXCL_START */ ctx_sched_destroy(handle); return r; + /* LCOV_EXCL_STOP */ } r = ctx_sched_set_job_cb(handle, __start_job_cb, __stop_job_cb, NULL); if (r != 0) { + /* LCOV_EXCL_START */ ctx_sched_destroy(handle); return r; + /* LCOV_EXCL_STOP */ } *scheduler = handle; @@ -394,24 +406,30 @@ API int job_scheduler_init(void) r = app_get_id(&__handle.app_id); if (r != APP_ERROR_NONE) { + /* LCOV_EXCL_START */ return job_error(JOB_ERROR_IO_ERROR, __FUNCTION__, __LINE__, NULL); + /* LCOV_EXCL_STOP */ } __handle.job_id_table = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL); if (__handle.job_id_table == NULL) { + /* LCOV_EXCL_START */ free(__handle.app_id); return job_error(JOB_ERROR_OUT_OF_MEMORY, __FUNCTION__, __LINE__, NULL); + /* LCOV_EXCL_STOP */ } r = __init_scheduler(&__handle.scheduler); if (r != 0) { + /* LCOV_EXCL_START */ g_hash_table_destroy(__handle.job_id_table); free(__handle.app_id); return job_error(JOB_ERROR_IO_ERROR, __FUNCTION__, __LINE__, NULL); + /* LCOV_EXCL_STOP */ } __handle.initialized = true; @@ -461,20 +479,26 @@ static int __job_add_app_control(ctx_sched_job_h job, job_info_h job_info, r = aul_svc_set_operation(b, AUL_SVC_OPERATION_DEFAULT); if (r != AUL_SVC_RET_OK) { + /* LCOV_EXCL_START */ _E("Failed to set operation"); return JOB_ERROR_OUT_OF_MEMORY; + /* LCOV_EXCL_STOP */ } r = aul_svc_set_appid(b, __handle.app_id); if (r != AUL_SVC_RET_OK) { + /* LCOV_EXCL_START */ _E("Failed to add appid(%s)", __handle.app_id); return JOB_ERROR_OUT_OF_MEMORY; + /* LCOV_EXCL_STOP */ } r = ctx_sched_job_set_app_control(job, b); if (r != 0) { + /* LCOV_EXCL_START */ _E("Failed to set app control (%d)", r); return JOB_ERROR_IO_ERROR; + /* LCOV_EXCL_STOP */ } return JOB_ERROR_NONE; @@ -496,9 +520,11 @@ static int __job_add_user_data(ctx_sched_job_h job, job_info_h job_info, r = ctx_sched_job_set_user_data(job, (const char *)b_raw, len); if (r != 0) { + /* LCOV_EXCL_START */ _E("Failed to set user data"); free(b_raw); return JOB_ERROR_IO_ERROR; + /* LCOV_EXCL_STOP */ } free(b_raw); @@ -550,8 +576,10 @@ static int __job_add_persistent(ctx_sched_job_h job, job_info_h job_info, r = ctx_sched_job_set_persistent(job, persistent); if (r != 0) { + /* LCOV_EXCL_START */ _E("Failed to set persistent (%d)", r); return r; + /* LCOV_EXCL_STOP */ } if (persistent) @@ -573,8 +601,10 @@ static int __job_add_one_time(ctx_sched_job_h job, job_info_h job_info, r = ctx_sched_job_set_one_time(job, once); if (r != 0) { + /* LCOV_EXCL_START */ _E("Failed to set one time (%d)", r); return r; + /* LCOV_EXCL_STOP */ } if (once) @@ -597,8 +627,10 @@ static int __job_add_requirement_timeout(ctx_sched_job_h job, r = ctx_sched_job_set_requirement_timeout(job, timeout); if (r != 0) { + /* LCOV_EXCL_START */ _E("Failed to set requirement timeout (%d)", r); return r; + /* LCOV_EXCL_STOP */ } if (timeout) { @@ -616,14 +648,18 @@ static int __add_str_attr_ne(ctx_sched_job_context_h context, r = ctx_sched_job_context_prepare_attribute_str(context, name); if (r != 0) { + /* LCOV_EXCL_START */ _E("Failed to prepare string attribute(%s) - %d", name, r); return r; + /* LCOV_EXCL_STOP */ } r = ctx_sched_job_context_attribute_add_ne_str(context, name, value); if (r != 0) { + /* LCOV_EXCL_START */ _E("Failed to add string value(%s) - %d", value, r); return r; + /* LCOV_EXCL_STOP */ } return JOB_ERROR_NONE; @@ -636,14 +672,18 @@ static int __add_str_attr_eq(ctx_sched_job_context_h context, r = ctx_sched_job_context_prepare_attribute_str(context, name); if (r != 0) { + /* LCOV_EXCL_START */ _E("Failed to prepare string attribute(%s) - %d", name, r); return r; + /* LCOV_EXCL_STOP */ } r = ctx_sched_job_context_attribute_add_eq_str(context, name, value); if (r != 0) { + /* LCOV_EXCL_START */ _E("Failed to add string value(%s) - %d", value, r); return r; + /* LCOV_EXCL_STOP */ } return JOB_ERROR_NONE; @@ -656,14 +696,18 @@ static int __add_int_attr_eq(ctx_sched_job_context_h context, r = ctx_sched_job_context_prepare_attribute_int(context, name); if (r != 0) { + /* LCOV_EXCL_START */ _E("Failed to prepare integer attribute(%s) - %d", name, r); return r; + /* LCOV_EXCL_STOP */ } r = ctx_sched_job_context_attribute_add_eq_int(context, name, value); if (r != 0) { + /* LCOV_EXCL_START */ _E("Failed to add integer value(%s) - %d", value, r); return r; + /* LCOV_EXCL_STOP */ } return JOB_ERROR_NONE; @@ -687,8 +731,10 @@ static int __job_add_requires_battery_not_low(ctx_sched_job_h job, r = ctx_sched_job_requirement_create(CTX_SCHED_URI_BATTERY, false, &requirement); if (r != 0) { + /* LCOV_EXCL_START */ _E("Failed to create requirement (%d)", r); return r; + /* LCOV_EXCL_STOP */ } if (battery_not_low) { @@ -699,15 +745,19 @@ static int __job_add_requires_battery_not_low(ctx_sched_job_h job, CTX_SCHED_ATTR_VALUE_HIGH); } if (r != JOB_ERROR_NONE) { + /* LCOV_EXCL_START */ ctx_sched_job_context_destroy(requirement); return r; + /* LCOV_EXCL_STOP */ } r = ctx_sched_job_add_requirement(job, requirement); if (r != 0) { + /* LCOV_EXCL_START */ _E("Failed to add requirement (%d)", r); ctx_sched_job_context_destroy(requirement); return r; + /* LCOV_EXCL_STOP */ } if (battery_not_low) @@ -736,22 +786,28 @@ static int __job_add_requires_charging(ctx_sched_job_h job, r = ctx_sched_job_requirement_create(CTX_SCHED_URI_BATTERY, false, &requirement); if (r != 0) { + /* LCOV_EXCL_START */ _E("Failed to create requirement (%d)", r); return r; + /* LCOV_EXCL_STOP */ } r = __add_int_attr_eq(requirement, CTX_SCHED_ATTR_NAME_IS_CHARGING, charging); if (r != JOB_ERROR_NONE) { + /* LCOV_EXCL_START */ ctx_sched_job_context_destroy(requirement); return r; + /* LCOV_EXCL_STOP */ } r = ctx_sched_job_add_requirement(job, requirement); if (r != 0) { + /* LCOV_EXCL_START */ _E("Failed to add requirement (%d)", r); ctx_sched_job_context_destroy(requirement); return r; + /* LCOV_EXCL_STOP */ } if (charging) @@ -780,8 +836,10 @@ static int __job_add_requires_wifi_connection(ctx_sched_job_h job, r = ctx_sched_job_requirement_create(CTX_SCHED_URI_WIFI, false, &requirement); if (r != 0) { + /* LCOV_EXCL_START */ _E("Failed to create requirement (%d)", r); return r; + /* LCOV_EXCL_STOP */ } if (wifi_connection) { @@ -792,15 +850,19 @@ static int __job_add_requires_wifi_connection(ctx_sched_job_h job, CTX_SCHED_ATTR_VALUE_CONNECTED); } if (r != JOB_ERROR_NONE) { + /* LCOV_EXCL_START */ ctx_sched_job_context_destroy(requirement); return r; + /* LCOV_EXCL_STOP */ } r = ctx_sched_job_add_requirement(job, requirement); if (r != 0) { + /* LCOV_EXCL_START */ _E("Failed to add requirement (%d)", r); ctx_sched_job_context_destroy(requirement); return r; + /* LCOV_EXCL_STOP */ } if (wifi_connection) @@ -820,8 +882,10 @@ static int __add_trigger_event(ctx_sched_job_h job, job_trigger_event_e event, r = ctx_sched_job_trigger_create(job_trigger_map[event].uri, &trigger); if (r != 0) { + /* LCOV_EXCL_START */ _E("Failed to create trigger (%d)", r); return r; + /* LCOV_EXCL_STOP */ } if (job_trigger_map[event].attr_type == ATTR_TYPE_INTEGER) { @@ -838,9 +902,11 @@ static int __add_trigger_event(ctx_sched_job_h job, job_trigger_event_e event, r = ctx_sched_job_add_trigger(job, trigger); if (r != 0) { + /* LCOV_EXCL_START */ _E("Failed to add trigger (%d)", r); ctx_sched_job_context_destroy(trigger); return r; + /* LCOV_EXCL_STOP */ } bundle_add(b, job_trigger_map[event].key, "true"); @@ -900,15 +966,19 @@ static ctx_sched_job_h __create_job(job_info_h job_info, const char *job_id) b = bundle_create(); if (b == NULL) { + /* LCOV_EXCL_START */ _E("Failed to create bundle"); return NULL; + /* LCOV_EXCL_STOP */ } r = bundle_add(b, AUL_K_JOB_ID, job_id); if (r != BUNDLE_ERROR_NONE) { + /* LCOV_EXCL_START */ _E("Failed to add job id(%s)", job_id); bundle_free(b); return NULL; + /* LCOV_EXCL_STOP */ } job_info_get_periodic(job_info, &interval); @@ -920,18 +990,22 @@ static ctx_sched_job_h __create_job(job_info_h job_info, const char *job_id) r = ctx_sched_job_create_periodic(interval, 0, &job); } if (r != 0) { + /* LCOV_EXCL_START */ _E("Failed to create job (%d)", r); bundle_free(b); return NULL; + /* LCOV_EXCL_STOP */ } for (i = 0; i < ARRAY_SIZE(job_constructor); ++i) { if (job_constructor[i]) { r = job_constructor[i](job, job_info, b); if (r != JOB_ERROR_NONE) { + /* LCOV_EXCL_START */ ctx_sched_job_destroy(job); bundle_free(b); return NULL; + /* LCOV_EXCL_STOP */ } } } @@ -963,18 +1037,23 @@ API int job_scheduler_schedule(job_info_h job_info, const char *job_id) job = __create_job(job_info, job_id); if (job == NULL) { + /* LCOV_EXCL_START */ return job_error(JOB_ERROR_OUT_OF_MEMORY, __FUNCTION__, __LINE__, NULL); + /* LCOV_EXCL_STOP */ } r = job_info_set_job_id(job_info, job_id); if (r != JOB_ERROR_NONE) { + /* LCOV_EXCL_START */ ctx_sched_job_destroy(job); return job_error(r, __FUNCTION__, __LINE__, NULL); + /* LCOV_EXCL_STOP */ } r = ctx_sched_schedule(__handle.scheduler, job, &id); if (r != 0) { + /* LCOV_EXCL_START */ ctx_sched_job_destroy(job); if (r == JOB_ERROR_PERMISSION_DENIED) { return job_error(JOB_ERROR_PERMISSION_DENIED, @@ -983,6 +1062,7 @@ API int job_scheduler_schedule(job_info_h job_info, const char *job_id) return job_error(JOB_ERROR_IO_ERROR, __FUNCTION__, __LINE__, NULL); } + /* LCOV_EXCL_STOP */ } ctx_sched_job_destroy(job); g_hash_table_insert(__handle.job_id_table, strdup(job_id), @@ -1014,8 +1094,10 @@ API int job_scheduler_cancel(const char *job_id) r = ctx_sched_cancel(__handle.scheduler, id); if (r != 0) { + /* LCOV_EXCL_START */ return job_error(JOB_ERROR_IO_ERROR, __FUNCTION__, __LINE__, NULL); + /* LCOV_EXCL_STOP */ } g_hash_table_remove(__handle.job_id_table, job_id); @@ -1034,8 +1116,10 @@ API int job_scheduler_cancel_all(void) r = ctx_sched_cancel_all(__handle.scheduler); if (r != 0) { + /* LCOV_EXCL_START */ return job_error(JOB_ERROR_IO_ERROR, __FUNCTION__, __LINE__, NULL); + /* LCOV_EXCL_STOP */ } g_hash_table_remove_all(__handle.job_id_table); @@ -1066,14 +1150,18 @@ API int job_scheduler_notify_job_finished(const char *job_id) r = ctx_sched_job_finished(__handle.scheduler, id); if (r != 0) { + /* LCOV_EXCL_START */ return job_error(JOB_ERROR_IO_ERROR, __FUNCTION__, __LINE__, NULL); + /* LCOV_EXCL_STOP */ } r = service_app_job_finished(job_id); if (r != 0) { + /* LCOV_EXCL_START */ return job_error(JOB_ERROR_IO_ERROR, __FUNCTION__, __LINE__, NULL); + /* LCOV_EXCL_STOP */ } return JOB_ERROR_NONE; @@ -1092,28 +1180,36 @@ static void __foreach_job_id_cb(gpointer key, gpointer value, r = ctx_sched_get_job(__handle.scheduler, id, &job); if (r != 0) { + /* LCOV_EXCL_START */ _E("Failed to get job from job id(%s:%d)", job_id, id); return; + /* LCOV_EXCL_STOP */ } b = __get_bundle_from_job(job); if (b == NULL) { + /* LCOV_EXCL_START */ ctx_sched_job_destroy(job); return; + /* LCOV_EXCL_STOP */ } ctx_sched_job_destroy(job); r = job_info_create_with_bundle(b, &job_info); if (r != JOB_ERROR_NONE) { + /* LCOV_EXCL_START */ bundle_free(b); return; + /* LCOV_EXCL_STOP */ } bundle_free(b); r = job_info_set_job_id(job_info, job_id); if (r != JOB_ERROR_NONE) { + /* LCOV_EXCL_START */ job_info_destroy(job_info); return; + /* LCOV_EXCL_STOP */ } *list = g_list_append(*list, job_info); @@ -1190,16 +1286,20 @@ static struct job_service_s *__create_job_service(const char *job_id, service = calloc(1, sizeof(struct job_service_s)); if (service == NULL) { + /* LCOV_EXCL_START */ _E("Out of memory"); return NULL; + /* LCOV_EXCL_STOP */ } service->handle = service_app_add_job_handler(job_id, __job_service_cb, service); if (service->handle == NULL) { + /* LCOV_EXCL_START */ _E("Out of memory"); free(service); return NULL; + /* LCOV_EXCL_STOP */ } service->callback = *callback; @@ -1240,8 +1340,10 @@ API int job_scheduler_service_add(const char *job_id, service = __create_job_service(job_id, callback, user_data); if (service == NULL) { + /* LCOV_EXCL_START */ return job_error(JOB_ERROR_OUT_OF_MEMORY, __FUNCTION__, __LINE__, NULL); + /* LCOV_EXCL_STOP */ } __handle.job_service_list = g_list_append(__handle.job_service_list, @@ -1268,8 +1370,10 @@ API int job_scheduler_service_remove(job_service_h job_service) } if (!__handle.initialized) { + /* LCOV_EXCL_START */ return job_error(JOB_ERROR_NOT_INITIALIZED, __FUNCTION__, __LINE__, NULL); + /* LCOV_EXCL_STOP */ } __handle.job_service_list = g_list_remove(__handle.job_service_list, @@ -1298,13 +1402,17 @@ int job_scheduler_verify_requirement(const char *uri) r = ctx_sched_job_requirement_is_supported(__handle.scheduler, uri, &supported); if (r != 0) { + /* LCOV_EXCL_START */ return job_error(JOB_ERROR_IO_ERROR, __FUNCTION__, __LINE__, NULL); + /* LCOV_EXCL_STOP */ } if (!supported) { + /* LCOV_EXCL_START */ return job_error(JOB_ERROR_NOT_SUPPORTED, __FUNCTION__, __LINE__, NULL); + /* LCOV_EXCL_STOP */ } return JOB_ERROR_NONE; @@ -1328,13 +1436,17 @@ int job_scheduler_verify_trigger(const char *uri) r = ctx_sched_job_trigger_is_supported(__handle.scheduler, uri, &supported); if (r != 0) { + /* LCOV_EXCL_START */ return job_error(JOB_ERROR_IO_ERROR, __FUNCTION__, __LINE__, NULL); + /* LCOV_EXCL_STOP */ } if (!supported) { + /* LCOV_EXCL_START */ return job_error(JOB_ERROR_NOT_SUPPORTED, __FUNCTION__, __LINE__, NULL); + /* LCOV_EXCL_STOP */ } return JOB_ERROR_NONE; @@ -1351,40 +1463,52 @@ static int __init_cynara(void) r = cynara_creds_self_get_client(CLIENT_METHOD_DEFAULT, &__cynara.client); if (r != CYNARA_API_SUCCESS) { + /* LCOV_EXCL_START */ _E("Failed to get client"); return -1; + /* LCOV_EXCL_STOP */ } r = cynara_creds_self_get_user(CLIENT_METHOD_DEFAULT, &__cynara.user); if (r != CYNARA_API_SUCCESS) { + /* LCOV_EXCL_START */ _E("Failed to get user"); goto err; + /* LCOV_EXCL_STOP */ } __cynara.session = cynara_session_from_pid(getpid()); if (__cynara.session == NULL) { + /* LCOV_EXCL_START */ _E("Failed to get session"); goto err; + /* LCOV_EXCL_STOP */ } r = cynara_configuration_create(&conf); if (r != CYNARA_API_SUCCESS) { + /* LCOV_EXCL_START */ _E("Failed to create cynara configuration"); goto err; + /* LCOV_EXCL_STOP */ } r = cynara_configuration_set_cache_size(conf, 10); if (r != CYNARA_API_SUCCESS) { + /* LCOV_EXCL_START */ _E("Failed to set cache size"); goto err; + /* LCOV_EXCL_STOP */ } r = cynara_initialize(&__cynara.handle, conf); cynara_configuration_destroy(conf); conf = NULL; if (r != CYNARA_API_SUCCESS) { + /* LCOV_EXCL_START */ _E("Failed to initialize cynara"); goto err; + /* LCOV_EXCL_STOP */ } __cynara.initialized = true; @@ -1393,6 +1517,7 @@ static int __init_cynara(void) return 0; err: + /* LCOV_EXCL_START */ if (conf) cynara_configuration_destroy(conf); @@ -1412,6 +1537,7 @@ err: } return -1; + /* LCOV_EXCL_STOP */ } static void __finish_cynara(void) @@ -1456,8 +1582,10 @@ int job_scheduler_verify_privilege(const char *priv) r = cynara_check(__cynara.handle, __cynara.client, __cynara.session, __cynara.user, priv); if (r != CYNARA_API_ACCESS_ALLOWED) { + /* LCOV_EXCL_START */ _E("Failed to check cynara access (%d)", r); return JOB_ERROR_PERMISSION_DENIED; + /* LCOV_EXCL_STOP */ } return JOB_ERROR_NONE; -- 2.7.4 From 559d9c8355acd875f5a58a4ecb37d871f31e1b72 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Mon, 16 Oct 2017 08:39:25 +0900 Subject: [PATCH 08/11] Fix a logical error Change-Id: I35261e1ed01c67eb284c0968d583e859a15638db Signed-off-by: Hwankyu Jhun --- src/job_info.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/job_info.c b/src/job_info.c index 2396d73..12e9f94 100644 --- a/src/job_info.c +++ b/src/job_info.c @@ -354,7 +354,6 @@ API int job_info_add_trigger_event(job_info_h job_info, const char *privilege; if (job_info == NULL || job_info->interval > 0 || - event < JOB_TRIGGER_EVENT_BATTERY_LEVEL_EMPTY || event > JOB_TRIGGER_EVENT_WIFI_STATE_CONNECTED) { return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, __LINE__, NULL); @@ -378,7 +377,6 @@ API int job_info_remove_trigger_event(job_info_h job_info, job_trigger_event_e event) { if (job_info == NULL || job_info->interval > 0 || - event < JOB_TRIGGER_EVENT_BATTERY_LEVEL_EMPTY || event > JOB_TRIGGER_EVENT_WIFI_STATE_CONNECTED) { return job_error(JOB_ERROR_INVALID_PARAMETER, __FUNCTION__, __LINE__, NULL); -- 2.7.4 From f3135ee0c9067b1ada8b51c426b7f1d1d2e2be89 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Mon, 16 Oct 2017 10:16:12 +0900 Subject: [PATCH 09/11] Release version 0.1.2 Changes: - Fix the description - Exclude some lines from lcov - Fix a logical error Change-Id: Ie655c557bf47e123e0290e0a3b14586e46ee397d Signed-off-by: Hwankyu Jhun --- packaging/capi-appfw-job-scheduler.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/capi-appfw-job-scheduler.spec b/packaging/capi-appfw-job-scheduler.spec index 2d61eb7..5fdea2c 100644 --- a/packaging/capi-appfw-job-scheduler.spec +++ b/packaging/capi-appfw-job-scheduler.spec @@ -1,6 +1,6 @@ Name: capi-appfw-job-scheduler Summary: Job Scheduler library -Version: 0.1.1 +Version: 0.1.2 Release: 0 Group: System/API License: Apache-2.0 -- 2.7.4 From acf42114129aed41d1de86c86cb5ced66e736fc0 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Tue, 31 Oct 2017 14:39:16 +0900 Subject: [PATCH 10/11] Add exceptions and descriptions - The minimum value of the periodic interval is 10 minutes. - The maximum value of the requirement timeout is 10000 ms. - If the developer sets the job to a one-time and persistent, calling job_scheduler_schedule() will be failed. Change-Id: I060f33732a521c94d569294cf9c9b2e4d1bfce2e Signed-off-by: Hwankyu Jhun --- include/job_info.h | 10 ++++++++++ src/job_info.c | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/include/job_info.h b/include/job_info.h index cf4a6d2..84e0eee 100644 --- a/include/job_info.h +++ b/include/job_info.h @@ -111,30 +111,40 @@ int job_info_set_periodic(job_info_h job_info, unsigned int interval); /** * @brief Sets the job to be automatically scheduled after the device reboots. * @since_tizen 4.0 + * @remarks If it's a one-time job, setting it to a persistent job returns + * a negative error value on scheduling by using job_scheduler_schedule(). * @param[in] job_info The job info handle * @param[in] persistent @c true, if the job needs to be automatically scheduled * @return @c 0 on success, * otherwise a negative error value * @retval #JOB_ERROR_NONE Successful * @retval #JOB_ERROR_INVALID_PARAMETER Invalid parameter + * @see job_info_set_once() + * @see job_scheduler_schedule() */ int job_info_set_persistent(job_info_h job_info, bool persistent); /** * @brief Sets the job not to be repeated. * @since_tizen 4.0 + * @remarks If it's a persistent job, setting it to a one-time job returns + * a negative error value on scheduling by using job_scheduler_schedule(). * @param[in] job_info The job info handle * @param[in] once @c true, if the job does not need to be repeated * @return @c 0 on success, * otherwise a negative error value * @retval #JOB_ERROR_NONE Successful * @retval #JOB_ERROR_INVALID_PARAMETER Invalid parameter + * @see job_info_set_persistent() + * @see job_scheduler_schedule() */ int job_info_set_once(job_info_h job_info, bool once); /** * @brief Sets the timeout interval of the requirements. * @since_tizen 4.0 + * @remarks If the requirements are not satisfied within the timeout value, + * the job does not start. * @param[in] job_info The job info handle * @param[in] timeout The timeout interval (ms) * @return @c 0 on success, diff --git a/src/job_info.c b/src/job_info.c index 12e9f94..534e39c 100644 --- a/src/job_info.c +++ b/src/job_info.c @@ -37,6 +37,9 @@ #define ARRAY_SIZE(x) ((sizeof(x)) / (sizeof(x[0]))) +#define MAX_REQ_TIMEOUT 10000 +#define MIN_PERIODIC_INTERVAL 10 + struct job_info_s { char *job_id; unsigned int interval; /* Periodic */ @@ -133,6 +136,9 @@ API int job_info_set_periodic(job_info_h job_info, unsigned int interval) __LINE__, NULL); } + if (interval < MIN_PERIODIC_INTERVAL) + interval = MIN_PERIODIC_INTERVAL; + job_info->interval = interval; return JOB_ERROR_NONE; @@ -212,6 +218,9 @@ API int job_info_set_requirement_timeout(job_info_h job_info, __LINE__, NULL); } + if (timeout > MAX_REQ_TIMEOUT) + timeout = MAX_REQ_TIMEOUT; + job_info->timeout = timeout; return JOB_ERROR_NONE; -- 2.7.4 From a6f8e85ab71b8bff7f1140952a2f9b93ec31e26d Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Thu, 9 Nov 2017 11:32:17 +0900 Subject: [PATCH 11/11] Release version 0.1.3 Changes: - Add exceptions and descriptions Change-Id: I40d746d1f91c9067e2574eff35f5b83305a3b9a6 Signed-off-by: Hwankyu Jhun --- packaging/capi-appfw-job-scheduler.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/capi-appfw-job-scheduler.spec b/packaging/capi-appfw-job-scheduler.spec index 5fdea2c..e92d3e8 100644 --- a/packaging/capi-appfw-job-scheduler.spec +++ b/packaging/capi-appfw-job-scheduler.spec @@ -1,6 +1,6 @@ Name: capi-appfw-job-scheduler Summary: Job Scheduler library -Version: 0.1.2 +Version: 0.1.3 Release: 0 Group: System/API License: Apache-2.0 -- 2.7.4