It does not need to be a NULL-terminate string anymore.
Change-Id: I52f99a2fbe025fd7af2dbc3c2fba7985c6984a82
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
* @remarks @c job should be destroyed using ctx_sched_job_destroy().
* @param[in] scheduler TBD
* @param[in] job TBD
- * @param[in] user_data TBD
+ * @param[in] cb_data TBD
* @return @c true to continue to iterate, otherwise @c false.
*/
-typedef bool (*ctx_sched_foreach_job_cb)(ctx_sched_h scheduler, ctx_sched_job_h job, void* user_data);
+typedef bool (*ctx_sched_foreach_job_cb)(ctx_sched_h scheduler, ctx_sched_job_h job, void* cb_data);
/**
* @brief Called when a job has been executed.
* @param[in] scheduler TBD
* @param[in] job TBD
* @param[in] timeout @c true if only the mandatory requirements are satisfied; @c false otherwise.
- * @param[in] user_data TBD
+ * @param[in] cb_data TBD
*/
-typedef void (*ctx_sched_start_job_cb)(ctx_sched_h scheduler, ctx_sched_job_h job, bool timeout, void* user_data);
+typedef void (*ctx_sched_start_job_cb)(ctx_sched_h scheduler, ctx_sched_job_h job, bool timeout, void* cb_data);
/**
* @brief Called when a job needs to be stopped.
* @remarks @c job should be destroyed using ctx_sched_job_destroy().
* @param[in] scheduler TBD
* @param[in] job TBD
- * @param[in] user_data TBD
+ * @param[in] cb_data TBD
*/
-typedef void (*ctx_sched_stop_job_cb)(ctx_sched_h scheduler, ctx_sched_job_h job, void* user_data);
+typedef void (*ctx_sched_stop_job_cb)(ctx_sched_h scheduler, ctx_sched_job_h job, void* cb_data);
/**
* @brief Gets all job information handles. Synchronous.
* @param[in] scheduler TBD
* @param[in] callback TBD
- * @param[in] user_data TBD
+ * @param[in] cb_data TBD
* @return @c 0 on success, otherwise a negative error value
*/
-int ctx_sched_foreach_job(ctx_sched_h scheduler, ctx_sched_foreach_job_cb callback, void* user_data);
+int ctx_sched_foreach_job(ctx_sched_h scheduler, ctx_sched_foreach_job_cb callback, void* cb_data);
/**
* @param[in] scheduler TBD
* @param[in] start_cb TBD
* @param[in] stop_cb TBD
- * @param[in] user_data TBD
+ * @param[in] cb_data TBD
* @return @c 0 on success, otherwise a negative error value
*/
-int ctx_sched_set_job_cb(ctx_sched_h scheduler, ctx_sched_start_job_cb start_cb, ctx_sched_stop_job_cb stop_cb, void* user_data);
+int ctx_sched_set_job_cb(ctx_sched_h scheduler, ctx_sched_start_job_cb start_cb, ctx_sched_stop_job_cb stop_cb, void* cb_data);
/**
/**
* @brief Attaches user data that can be retrieved using ctx_sched_job_get_user_data().
- * @details A Null-terminate string can be attached to a job. The attached string can be accessed
- * using ctx_sched_job_get_user_data(), after acquiring a job handle using ctx_sched_get_job().
- * Note that, the string should be a valid Json data string, i.e., containing no double-quotes
- * and other Json special characters.
* @param[in] job Job handle
* @param[in] user_data User data
+ * @param[in] length Length of the data
* @return @c 0 on success, otherwise a negative error value
*/
-int ctx_sched_job_set_user_data(ctx_sched_job_h job, const char* user_data);
+int ctx_sched_job_set_user_data(ctx_sched_job_h job, const char* user_data, size_t length);
/**
/**
* @brief Gets the user data string attached to a job
- * @details This function returns a pointer to the string contained in the @c job.
- * The @c job should be not freed unless you do not reference the @c user_data anymore.
+ * @details If the job contains no user data, the @c user_data and the @c length are set to @c NULL and 0 respectively.
+ * @remarks If the @c user_data is not @c NULL, it should be freed using @c free().
* @param[in] job Job handle
* @param[out] user_data User data string
+ * @param[out] length Length of the data
* @return @c 0 on success, otherwise a negative error value
*/
-int ctx_sched_job_get_user_data(ctx_sched_job_h job, const char** user_data);
+int ctx_sched_job_get_user_data(ctx_sched_job_h job, char** user_data, size_t* length);
/**
return E_SUPPORT;
}
-EXPORT_API int ctx_sched_foreach_job(ctx_sched_h scheduler, ctx_sched_foreach_job_cb callback, void* user_data)
+EXPORT_API int ctx_sched_foreach_job(ctx_sched_h scheduler, ctx_sched_foreach_job_cb callback, void* cb_data)
{
return E_SUPPORT;
}
-EXPORT_API int ctx_sched_set_job_cb(ctx_sched_h scheduler, ctx_sched_start_job_cb start_cb, ctx_sched_stop_job_cb stop_cb, void* user_data)
+EXPORT_API int ctx_sched_set_job_cb(ctx_sched_h scheduler, ctx_sched_start_job_cb start_cb, ctx_sched_stop_job_cb stop_cb, void* cb_data)
{
return E_SUPPORT;
}
return E_SUPPORT;
}
-EXPORT_API int ctx_sched_job_set_user_data(ctx_sched_job_h job, const char* user_data)
+EXPORT_API int ctx_sched_job_set_user_data(ctx_sched_job_h job, const char* user_data, size_t length)
{
return E_SUPPORT;
}
return E_SUPPORT;
}
-EXPORT_API int ctx_sched_job_get_user_data(ctx_sched_job_h job, const char** user_data)
+EXPORT_API int ctx_sched_job_get_user_data(ctx_sched_job_h job, char** user_data, size_t* length)
{
return E_SUPPORT;
}
JobManagerProxy jobManager;
ctx_sched_start_job_cb startJobCb;
ctx_sched_stop_job_cb stopJobCb;
- void* userData;
+ void* cbData;
_ctx_sched_s() :
- startJobCb(NULL), stopJobCb(NULL), userData(NULL) {}
+ startJobCb(NULL), stopJobCb(NULL), cbData(NULL) {}
} ctx_sched_s;
return ctx_sched_remove_job(scheduler, job_id);
}
-static bool __cancelJob(ctx_sched_h scheduler, ctx_sched_job_h job, void* user_data)
+static bool __cancelJob(ctx_sched_h scheduler, ctx_sched_job_h job, void* cb_data)
{
int jid = -1;
return E_NONE;
}
-EXPORT_API int ctx_sched_foreach_job(ctx_sched_h scheduler, ctx_sched_foreach_job_cb callback, void* user_data)
+EXPORT_API int ctx_sched_foreach_job(ctx_sched_h scheduler, ctx_sched_foreach_job_cb callback, void* cb_data)
{
IF_FAIL_RETURN(scheduler && callback, E_PARAM);
jobHandle->jobInfo = jobInfo;
jobInfo = NULL;
- if (!callback(scheduler, jobHandle, user_data)) {
+ if (!callback(scheduler, jobHandle, cb_data)) {
std::for_each(jobInfos.begin(), jobInfos.end(), [](JobInfo*& j){ delete j; });
break;
}
return E_NONE;
}
-static void __startJob(JobInfo* jobInfo, bool timeout, void* userData)
+static void __startJob(JobInfo* jobInfo, bool timeout, void* cbData)
{
- ctx_sched_s* scheduler = static_cast<ctx_sched_s*>(userData);
+ ctx_sched_s* scheduler = static_cast<ctx_sched_s*>(cbData);
ctx_sched_job_s* job = new(std::nothrow) ctx_sched_job_s();
if (!job) {
}
job->jobInfo = jobInfo;
- scheduler->startJobCb(scheduler, job, timeout, scheduler->userData);
+ scheduler->startJobCb(scheduler, job, timeout, scheduler->cbData);
}
-static void __stopJob(JobInfo* jobInfo, void* userData)
+static void __stopJob(JobInfo* jobInfo, void* cbData)
{
- ctx_sched_s* scheduler = static_cast<ctx_sched_s*>(userData);
+ ctx_sched_s* scheduler = static_cast<ctx_sched_s*>(cbData);
ctx_sched_job_s* job = new(std::nothrow) ctx_sched_job_s();
if (!job) {
}
job->jobInfo = jobInfo;
- scheduler->stopJobCb(scheduler, job, scheduler->userData);
+ scheduler->stopJobCb(scheduler, job, scheduler->cbData);
}
-EXPORT_API int ctx_sched_set_job_cb(ctx_sched_h scheduler, ctx_sched_start_job_cb start_cb, ctx_sched_stop_job_cb stop_cb, void* user_data)
+EXPORT_API int ctx_sched_set_job_cb(ctx_sched_h scheduler, ctx_sched_start_job_cb start_cb, ctx_sched_stop_job_cb stop_cb, void* cb_data)
{
IF_FAIL_RETURN(scheduler && start_cb && stop_cb, E_PARAM);
scheduler->startJobCb = start_cb;
scheduler->stopJobCb = stop_cb;
- scheduler->userData = user_data;
+ scheduler->cbData = cb_data;
scheduler->jobManager.setJobCb(__startJob, __stopJob, scheduler);
return E_NONE;
return E_NONE;
}
-EXPORT_API int ctx_sched_job_set_user_data(ctx_sched_job_h job, const char* user_data)
+EXPORT_API int ctx_sched_job_set_user_data(ctx_sched_job_h job, const char* user_data, size_t length)
{
- IF_FAIL_RETURN(job && user_data, E_PARAM);
+ IF_FAIL_RETURN(job && user_data && length > 0, E_PARAM);
- job->jobInfo->setUserData(user_data);
+ job->jobInfo->setUserData(user_data, length);
return E_NONE;
}
return E_NONE;
}
-EXPORT_API int ctx_sched_job_get_user_data(ctx_sched_job_h job, const char** user_data)
+EXPORT_API int ctx_sched_job_get_user_data(ctx_sched_job_h job, char** user_data, size_t* length)
{
- IF_FAIL_RETURN(job && user_data, E_PARAM);
+ IF_FAIL_RETURN(job && user_data && length, E_PARAM);
- *user_data = job->jobInfo->getUserData().c_str();
+ *length = job->jobInfo->getUserData(user_data);
return E_NONE;
}
return __action;
}
-const std::string& JobInfo::getUserData() const
+size_t JobInfo::getUserData(char** data) const
{
- return __userData;
+ if (__userData.empty()) {
+ *data = NULL;
+ return 0;
+ }
+
+ gsize length = 0;
+ *data = reinterpret_cast<char*>(g_base64_decode(__userData.c_str(), &length));
+
+ return length;
}
JobInfo& JobInfo::setId(int jobId)
return *this;
}
-JobInfo& JobInfo::setUserData(const std::string& userData)
+JobInfo& JobInfo::setUserData(const char* data, size_t length)
{
- __userData = userData;
+ if (data == NULL || length == 0) {
+ __userData.clear();
+ return *this;
+ }
+
+ char* encoded = g_base64_encode(reinterpret_cast<const guchar*>(data), length);
+
+ if (encoded == NULL) {
+ _W("User data encoding failed");
+ return *this;
+ }
+
+ __userData.assign(encoded).shrink_to_fit();
+ g_free(encoded);
+
return *this;
}
JobAction* getAction();
- const std::string& getUserData() const;
+ size_t getUserData(char** data) const;
JobInfo& setId(int jobId);
JobInfo& setAction(JobAction* action);
- // UserData should be a string that can be a string element of Json
- JobInfo& setUserData(const std::string& userData);
+ JobInfo& setUserData(const char* data, size_t length);
std::string serialize() const;;
void toJson(Json::Value& jsonRoot) const;