extern "C" {
#endif
+/**
+ * @breif Enumeration for the service result.
+ * @since_tizen 10.0
+ */
typedef enum {
- SERVICE_ERROR_NONE = TIZEN_ERROR_NONE,
- SERVICE_ERROR_INVALID_PARAMETER = TIZEN_ERROR_INVALID_PARAMETER,
- SERVICE_ERROR_OUT_OF_MEMORY = TIZEN_ERROR_OUT_OF_MEMORY,
- SERVICE_ERROR_INVALID_CONTEXT = TIZEN_ERROR_APPLICATION | 0x01,
- SERVICE_ERROR_ALREADY_EXIST = TIZEN_ERROR_FILE_EXISTS,
+ SERVICE_ERROR_NONE = TIZEN_ERROR_NONE, /**< Successful */
+ SERVICE_ERROR_INVALID_PARAMETER = TIZEN_ERROR_INVALID_PARAMETER, /**< Invalid parameter */
+ SERVICE_ERROR_OUT_OF_MEMORY = TIZEN_ERROR_OUT_OF_MEMORY, /**< Out of memory */
+ SERVICE_ERROR_INVALID_CONTEXT = TIZEN_ERROR_APPLICATION | 0x01, /**< Invalid context */
+ SERVICE_ERROR_ALREADY_EXIST = TIZEN_ERROR_FILE_EXISTS, /**<Service already exists */
} service_error_e;
+/**
+ * @brief Enumeration for the service state.
+ * @since_tizen 10.0
+ */
typedef enum {
- SERVICE_STATE_INITIALIZED,
- SERVICE_STATE_CREATED,
- SERVICE_STATE_RUNNING,
- SERVICE_STATE_DESTROYED,
+ SERVICE_STATE_INITIALIZED, /**< The service is initialized */
+ SERVICE_STATE_CREATED, /**< The service is created. */
+ SERVICE_STATE_RUNNING, /**< The service is running. */
+ SERVICE_STATE_DESTROYED, /**< The service is destroyed. */
} service_state_e;
+/**
+ * @brief Called when the service starts.
+ * @details The callback function is called when the main loop of the service starts.
+ * @since_tizen 10.0
+ * @param[in] user_data The user data passed from the registration function
+ * @pre @service_run() will invoke this callback function.
+ * @see service_run()
+ * @see #service_lifecycle_callback_s
+ */
typedef void (*service_create_cb)(void *user_data);
+/**
+ * @brief Called when the main loop of the service exits.
+ * @details You should release resources of the service in this function.
+ * @since_tizen 10.0
+ * @param[in] user_data The user data passed from the registration function
+ * @see service_quit()
+ * @see #service_lifecycle_callback_s
+ */
typedef void (*service_destroy_cb)(void *user_data);
+/**
+ * @brief Called when a message is delivered.
+ * @since_tizen 10.0
+ * @param[in] sender The name of the sender
+ * @param[in] envelope The message
+ * @param[in] user_data The user data passed from the registration function
+ * @see service_send_message()
+ * @see #service_lifecycle_callback_s
+ */
typedef void (*service_message_cb)(const char *sender, bundle *envelope,
void *user_data);
+/**
+ * @brief Thr structure type containing the set of callback functions for handling service lifecycle events.
+ * @details It's one of the input parmaeters of the service_create() function.
+ * @since_tizen 10.0
+ * @see service_create()
+ * @see service_create_cb()
+ * @see service_destroy_cb()
+ * @see service_message_cb()
+ */
typedef struct {
- service_create_cb create;
- service_destroy_cb destroy;
- service_message_cb message;
+ service_create_cb create; /**< This callback function is called at the start of the service. */
+ service_destroy_cb destroy; /**< This callback function is called once after the main loop of the service exits. */
+ service_message_cb message; /**< This callback function is called when the message is delivered. */
} service_lifecycle_callback_s;
+/**
+ * @brief The service handle.
+ * @since_tizen 10.0
+ * @see service_create()
+ * @see service_destroy()
+ * @see service_run()
+ * @see service_quit()
+ * @see service_send_message()
+ */
typedef void *service_h;
+/**
+ * @brief Creates a new service instance.
+ * @since_tizen 10.0
+ * @remarks @a service must be released using service_destroy().
+ * @param[in] name The name of the service
+ * @param[in] callback The set of callback functions for handling service lifecycle events
+ * @param[in] user_data The user data to be passed to the callback functions
+ * @param[out] service The service handle
+ * @return @c 0 on success,
+ * otherwise a negative error value
+ * @retval #SERVICE_ERROR_NONE Successful
+ * @retval #SERVICE_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #SERVICE_ERROR_OUT_OF_MEMORY Out of memory
+ * @retval #SERVICE_ERROR_ALREADY_EXIST Service already exists
+ * @see service_destroy()
+ * @see service_run()
+ * @see #service_lifecycle_callback_s
+ *
+ * @code
+#include <service.h>
+#include <dlog.h>
+
+#undef LOG_TAG
+#define LOG_TAG "ALARM_SERVICE"
+
+#undef EXPORT
+#define EXPORT __attribute__((visibility("default")))
+
+static service_h __service;
+
+static void ServiceCreateCb(void* user_data) {
+ // ...
+}
+
+static void ServiceDestroyCb(void* user_data) {
+ // ...
+}
+
+static void ServiceMessageCb(const char* sender, bundle* envelope, void* user_data) {
+ // ...
+}
+
+extern "C" service_h UNITED_SERVICE_INIT(const char* name) {
+ LOGD("UNITED_SERVICE_INIT. name=%s", name);
+ service_lifecycle_callback_s callback = {
+ .create = ServiceCreateCb,
+ .destroy = ServiceDestroyCb,
+ .message = ServiceMessageCb,
+ };
+
+ service_h service = nullptr;
+ int ret = service_create(name, &callback, nullptr, &service);
+ if (ret != SERVICE_ERROR_NONE) {
+ LOGE("Failed to create service");
+ return nullptr;
+ }
+
+ service_run(service);
+ __service = service;
+ return service;
+}
+
+extern "C" void UNITED_SERVICE_SHUTDOWN(void) {
+ // ...
+}
+ * @endcode
+ */
int service_create(const char *name, service_lifecycle_callback_s *callback,
void *user_data, service_h *service);
+/**
+ * @brief Runs the service instance.
+ * @since_tizen 10.0
+ * @param[in] service The service handle
+ * @return @c 0 on success,
+ * otherwise a negative error value
+ * @retval #SERVICE_ERROR_NONE Successful
+ * @retval #SERVICE_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #SERVICE_ERROR_INVALID_CONTEXT Invalid context
+ * @see service_create()
+ * @see service_quit()
+ * @see service_create_cb()
+ *
+ * @code
+#include <service.h>
+#include <dlog.h>
+
+#undef LOG_TAG
+#define LOG_TAG "ALARM_SERVICE"
+
+#undef EXPORT
+#define EXPORT __attribute__((visibility("default")))
+
+static service_h __service;
+
+static void ServiceCreateCb(void* user_data) {
+ LOGD("Alarm Service Created");
+}
+
+extern "C" service_h UNITED_SERVICE_INIT(const char* name) {
+ LOGD("UNITED_SERVICE_INIT. name=%s", name);
+ // ...
+
+ service_run(service);
+ __service = service;
+ return service;
+}
+ * @endcode
+ */
int service_run(service_h service);
+/**
+ * @brief Exits the service instance.
+ * @since_tizen 10.0
+ * @param[in] service The service handle
+ * @return @c 0 on success,
+ * otherwise a negative error value
+ * @retval #SERVICE_ERROR_NONE Successful
+ * @retval #SERVICE_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #SERVICE_ERROR_INVALID_CONTEXT Invalid context
+ * @see service_create()
+ * @see service_run()
+ * @see service_destroy_cb()
+ *
+ * @code
+#include <service.h>
+#include <dlog.h>
+
+#undef LOG_TAG
+#define LOG_TAG "ALARM_SERVICE"
+
+#undef EXPORT
+#define EXPORT __attribute__((visibility("default")))
+
+static service_h __service;
+
+static void ServiceDestroyCb(void* user_data) {
+ // ...
+}
+
+extern "C" service_h UNITED_SERVICE_INIT(const char* name) {
+ LOGD("UNITED_SERVICE_INIT. name=%s", name);
+ // ...
+ return service;
+}
+
+extern "C" void UNITED_SERVICE_SHUTDOWN(void) {
+ LOGD("UNITED_SERVICE_SHUTDOWN");
+ if (__service) {
+ service_quit(__service);
+ service_destroy(__service);
+ __service = nullptr;
+ }
+}
+ * @endcode
+ */
int service_quit(service_h service);
+/**
+ * @brief Sends a message to the specified service.
+ * @since_tizen 10.0
+ * @param[in] service The service handle
+ * @param[in] envelope The message
+ * @return @c 0 on success,
+ * otherwise a negative error value
+ * @retval #SERVICE_ERROR_NONE Successful
+ * @retval #SERVICE_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #SERVICE_ERROR_INVALID_CONTEXT Invalid context
+ * @see service_message_cb()
+ *
+ * @code
+#include <service.h>
+#include <dlog.h>
+
+#undef LOG_TAG
+#define LOG_TAG "ALARM_SERVICE"
+
+#undef EXPORT
+#define EXPORT __attribute__((visibility("default")))
+
+static service_h __service;
+
+static void ServiceMessageCb(const char* sender, bundle* envelope, void* user_data) {
+ char* message = nullptr;
+
+ if (bundle_get_str(envelope, "MESSAGE", &message) == BUNDLE_ERROR_NONE) {
+ LOGD("sender=%s, message=%s", sender, message);
+ }
+}
+
+void SendMessage(const char* message) {
+ if (!__service) {
+ LOGW("service is not running");
+ return;
+ }
+
+ if (!message) {
+ LOGE("Invalid parameter");
+ return;
+ }
+
+ bundle* envelope = bundle_create();
+ if (envelope == nullptr) {
+ LOGE("Failed to create bundle");
+ return;
+ }
+
+ bundle_add_str(envelope, "MESSAGE", message);
+ int ret = service_send_message(__service, envelope);
+ if (ret != SERVICE_LOADER_ERROR_NONE) {
+ LOGE("Failed to send message to loader. error=%d", ret);
+ }
+
+ bundle_free(envelope);
+}
+
+extern "C" service_h UNITED_SERVICE_INIT(const char* name) {
+ LOGD("UNITED_SERVICE_INIT. name=%s", name);
+ // ...
+ return service;
+}
+
+extern "C" void UNITED_SERVICE_SHUTDOWN(void) {
+ // ...
+}
+ * @endcode
+ */
int service_send_message(service_h service, bundle *envelope);
+/**
+ * @brief Destroys the service instance.
+ * @since_tizen 10.0
+ * @param[in] service The service handle
+ * @return @c 0 on success,
+ * otherwise a negative error value
+ * @retval #SERVICE_ERROR_NONE Successful
+ * @retval #SERVICE_ERROR_INVALID_PARAMETER Invalid parameter
+ * @see service_create()
+ *
+ * @code
+#include <service.h>
+#include <dlog.h>
+
+#undef LOG_TAG
+#define LOG_TAG "ALARM_SERVICE"
+
+#undef EXPORT
+#define EXPORT __attribute__((visibility("default")))
+
+static service_h __service;
+
+extern "C" service_h UNITED_SERVICE_INIT(const char* name) {
+ LOGD("UNITED_SERVICE_INIT. name=%s", name);
+ // ...
+ return service;
+}
+
+extern "C" void UNITED_SERVICE_SHUTDOWN(void) {
+ LOGD("UNITED_SERVICE_SHUTDOWN");
+ if (__service) {
+ service_destroy(__service);
+ __service = nullptr;
+ }
+}
+ * @endcode
+ */
int service_destroy(service_h service);
+/**
+ * @brief Gets the name of the service.
+ * @details The @a name must be released using free().
+ * @since_tizen 10.0
+ * @param[in] service The service handle
+ * @param[out] name The name of the service
+ * @return @c 0 on success,
+ * otherwise a negative error value
+ * @retval #SERVICE_ERROR_NONE Successful
+ * @retval #SERVICE_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #SERVICE_ERROR_OUT_OF_MEMORY Out of memory
+ * @see service_create()
+ *
+ * @code
+#include <service.h>
+#include <dlog.h>
+#include <stdlib.h>
+
+#include <string>
+
+#undef LOG_TAG
+#define LOG_TAG "ALARM_SERVICE"
+
+static service_h __service;
+
+// ...
+
+std::string GetServiceName(void) {
+ char* name = nullptr;
+ int ret = service_get_name(__service, &name);
+ if (ret != SERVICE_ERROR_NONE) {
+ LOGE("Failed to get service name. error=%d", ret);
+ return "";
+ }
+
+ LOGD("name=%s", name);
+ std::string result(name);
+ free(name);
+ return result;
+}
+ * @endcode
+ */
int service_get_name(service_h service, char **name);
+/**
+ * @brief Gets the current state of the service.
+ * @since_tizen 10.0
+ * @param[in] service The service handle
+ * @param[out] state The state of the service
+ * @return @c 0 on success,
+ * otherwise a negative error value
+ * @retval #SERVICE_ERROR_NONE Successful
+ * @retval #SERVICE_ERROR_INVALID_PARAMETER Invalid parameter
+ * @see service_run()
+ * @see #service_state_e
+ *
+ * @code
+#include <service.h>
+#include <dlog.h>
+#include <stdlib.h>
+
+#include <string>
+
+#undef LOG_TAG
+#define LOG_TAG "ALARM_SERVICE"
+
+static service_h __service;
+
+// ...
+
+service_state_e GetServiceState(void) {
+ service_state_e state;
+ int ret = service_get_state(__service, &state);
+ if (ret != SERVICE_ERROR_NONE) {
+ LOGE("Failed to get service state. error=%d", ret);
+ return state;
+ }
+
+ LOGD("state=%d", state);
+ return state;
+}
+ * @endcode
+ */
int service_get_state(service_h service, service_state_e *state);
+/**
+ * @brief Gets the tizen core handle of the service.
+ * @since_tizen 10.0
+ * @param[in] service The service handle
+ * @param[out] core The tizen core handle
+ * @return @c 0 on success,
+ * otherwise a negative error value
+ * @retval #SERVICE_ERROR_NONE Successful
+ * @retval #SERVICE_ERROR_INVALID_PARAMETER Invalid parameter
+ *
+ * @code
+#include <service.h>
+#include <dlog.h>
+
+#undef LOG_TAG
+#define LOG_TAG "ALARM_SERVICE"
+
+static service_h __service;
+
+// ...
+
+int AddIdler(tizen_core_task_cb callback, void* user_data) {
+ tizen_core_h core = nullptr;
+ int ret = service_get_tizen_core(__service, &core);
+ if (ret != SERVICE_ERROR_NONE) {
+ LOGE("Failed to get tizen core. error=%d", ret);
+ return ret;
+ }
+
+ tizen_core_source_h source = nullptr;
+ ret = tizen_core_add_idle_job(core, callback, user_data, &source);
+ if (ret != TIZEN_CORE_ERROR_NONE) {
+ LOGE("Failed to add idle job. error=%d", ret);
+ return ret;
+ }
+
+ return 0;
+}
+ * @endcode
+ */
int service_get_tizen_core(service_h service, tizen_core_h *core);
+/**
+ * @brief Gets the tizen core channel sender of the service.
+ * @since_tizen 10.0
+ * @param[in] service The service handle
+ * @param[out] receiver The tizen core channel sender
+ * @return @c 0 on success,
+ * otherwise a negative error value
+ * @retval #SERVICE_ERROR_NONE Successful
+ * @retval #SERVICE_ERROR_INVALID_PARAMETER Invalid parameter
+ *
+ * @code
+#include <service.h>
+#include <dlog.h>
+
+#undef LOG_TAG
+#define LOG_TAG "ALARM_SERVICE"
+
+static service_h __service;
+
+// ...
+
+tizen_core_channel_sender_h GetChannelSender(void) {
+ tizen_core_channel_sender_h sender = nullptr;
+ int ret = service_get_tizen_core_channel_sender(__service, &sender);
+ if (ret != SERVICE_ERROR_NONE) {
+ LOGE("Failed to get tizen core channel sender. error=%d", ret);
+ return nullptr;
+ }
+
+ tizen_core_channel_sender_h cloned_sender = nullptr;
+ ret = tizen_core_channel_sender_clone(sender, &cloned_sender);
+ if (ret != TIZEN_CORE_ERROR_NONE) {
+ LOGE("Failed to clone tizen core channel sender. error=%d", ret);
+ return nullptr;
+ }
+
+ return cloned_sender;
+}
+ * @endcode
+ */
int service_get_tizen_core_channel_sender(service_h service,
tizen_core_channel_sender_h *sender);
#define __SERVICE_MANAGER_H__
#include <service.h>
+#include <tizen.h>
#ifdef __cplusplus
extern "C" {
#endif
+/**
+ * @brief Enumeration for the service manager result.
+ * @since_tizen 10.0
+ */
+typedef enum {
+ SERVICE_MANAGER_ERROR_NONE = TIZEN_ERROR_NONE, /**< Successful */
+ SERVICE_MANAGER_ERROR_INVALID_PARAMETER = TIZEN_ERROR_INVALID_PARAMETER, /**< Invalid parameter */
+ SERVICE_MANAGER_ERROR_OUT_OF_MEMORY = TIZEN_ERROR_OUT_OF_MEMORY, /**< Out of memory */
+} service_manager_error_e;
+
+/**
+ * @brief The handle for the service manager event handler.
+ * @since_tizen 10.0
+ */
typedef void *service_manager_event_h;
+/**
+ * @brief Called when the service state changes.
+ * @since_tizen 10.0
+ * @param[in] service The service handle
+ * @param[in] state The state of the service
+ * @param[in] user_data The user data passed from the add event handler function
+ * @see service_manager_add_event_handler()
+ * @remarks The @a service should not be freed. The @a service can be used only in the callback.
+ */
typedef void (*service_state_changed_cb)(service_h service,
service_state_e state,
void *user_data);
+/**
+ * @brief Adds a service state change event handler.
+ * @since_tizen 10.0
+ * @param[in] callback The callback function to invoke
+ * @param[in] user_data The user data to be passed to the callback function
+ * @param[out] event_handler The event handler
+ * @return @c 0 on success,
+ * otherwise a negative error value
+ * @retval #SERVICE_MANAGER_ERROR_NONE Successful
+ * @retval #SERVICE_MANAGER_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #SERVICE_MANAGER_ERROR_OUT_OF_MEMORY Out of memory
+ * @see service_manager_remove_event_handler()
+ * @see service_state_changed_cb()
+ * @remarks The @a event_handler should be released using service_manager_remove_event_handler().
+ *
+ * @code
+#include <service_manager.h>
+#include <dlog.h>
+
+#undef LOG_TAG
+#define LOG_TAG "ALARM_SERVICE"
+
+static service_manager_event_h __event_handler;
+
+static void ServiceStateChangedCb(service_h service, service_state_e state,
+ void* user_data) {
+ char* name = nullptr;
+ service_get_name(service, &name);
+ LOGD("name=%s, state=%d", name, state);
+ free(name);
+}
+
+int AddEventHandler(void) {
+ service_manager_event_h event_handler = nullptr;
+ int ret = service_manager_add_event_handler(ServiceStateChangedCb, nullptr,
+ &event_handler);
+ if (ret != SERVICE_MANAGER_ERROR_NONE) {
+ LOGE("Failed to add event handler. error=%d", ret);
+ return ret;
+ }
+
+ return 0;
+}
+ * @endcode
+ */
int service_manager_add_event_handler(service_state_changed_cb callback,
void *user_data,
service_manager_event_h *event_handler);
+/**
+ * @brief Removes a service state change event handler.
+ * @since_tizen 10.0
+ * @param[in] event_handler The event handler
+ * @return @c 0 on success,
+ * otherwise a negative error value
+ * @retval #SERVICE_MANAGER_ERROR_NONE Successful
+ * @retval #SERVICE_MANAGER_ERROR_INVALID_PARAMETER Invalid parameter
+ * @see service_manager_add_event_handler()
+ *
+ * @code
+#include <service_manager.h>
+#include <dlog.h>
+
+#undef LOG_TAG
+#define LOG_TAG "ALARM_SERVICE"
+
+static service_manager_event_h __event_handler;
+
+// ...
+
+void RemoveEventHandler(void) {
+ service_manager_remove_event_handler(__event_handler);
+}
+ * @endcode
+ */
int service_manager_remove_event_handler(service_manager_event_h event_handler);
+/**
+ * @brief Gets the service handle by the name.
+ * @since_tizen 10.0
+ * @param[in] name The name of the service
+ * @param[out] service The service handle
+ * @return @c 0 on success,
+ * otherwise a negative error value
+ * @retval #SERVICE_MANAGER_ERROR_NONE Successful
+ * @retval #SERVICE_MANAGER_ERROR_INVALID_PARAMETER Invalid parameter
+ * @remarks The @a service should not be released using service_destroy().
+ *
+ * @code
+#include <service_manager.h>
+#include <dlog.h>
+
+#undef LOG_TAG
+#define LOG_TAG "ALARM_SERVICE"
+
+service_h GetServiceFromName(const char* name) {
+ service_h service = nullptr;
+ int ret = service_manager_get_service(name, &service);
+ if (ret != SERVICE_MANAGER_ERROR_NONE) {
+ LOGE("Failed to get service. error=%d", ret);
+ return nullptr;
+ }
+
+ return service;
+}
+ * @endcode
+ */
int service_manager_get_service(const char *name, service_h *service);
#ifdef __cplusplus