BuildRequires: pkgconfig(glib-2.0)
BuildRequires: pkgconfig(gio-2.0)
BuildRequires: pkgconfig(dlog)
+BuildRequires: pkgconfig(sensor)
BuildRequires: pkgconfig(capi-base-common)
+BuildRequires: pkgconfig(capi-system-info)
BuildRequires: pkgconfig(context-common-server)
BuildRequires: pkgconfig(context-common-client)
--- /dev/null
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <SensorRecorderTypesPrivate.h>
+#include "QueryResultListener.h"
+
+using namespace ctx;
+
+QueryResultListener::QueryResultListener(const char* subject, ctx_sensor_rec_data_cb cb, void* userData) :
+ __subject(subject),
+ __callback(cb),
+ __userData(userData)
+{
+}
+
+void QueryResultListener::onSuccess(const std::string& methodName, GVariant* param)
+{
+ const char* schema = NULL;
+ GVariant* vals = NULL;
+ g_variant_get(param, "(&sv)", &schema, &vals);
+
+ auto records = Tuple::buildFrom(vals);
+ int remains = records.size();
+
+ ctx_sensor_rec_data_s data;
+ data.keys = util::tokenizeString(schema, ",");
+
+ _D("%d records received", remains);
+
+ for (auto& tuple : records) {
+ data.tuple = tuple;
+ if (!__callback(__subject.c_str(), &data, --remains, E_NONE, __userData))
+ break;
+ }
+
+ delete this;
+}
+
+void QueryResultListener::onError(const std::string& methodName, int error)
+{
+ __callback(__subject.c_str(), NULL, 0, error, __userData);
+
+ delete this;
+}
--- /dev/null
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __SENSOR_RECORDER_QUERY_RESULT_LISTENER_H__
+#define __SENSOR_RECORDER_QUERY_RESULT_LISTENER_H__
+
+#include <Tuple.h>
+#include <IServiceResultListener.h>
+#include <sensor_recorder_internal.h>
+
+namespace ctx {
+
+ struct ctx_sensor_rec_data_s {
+ std::vector<std::string> keys;
+ std::shared_ptr<Tuple> tuple;
+ };
+
+ class QueryResultListener : public IServiceResultListener {
+ public:
+ QueryResultListener(const char* subject, ctx_sensor_rec_data_cb cb, void* userData);
+
+ void onSuccess(const std::string& methodName, GVariant* param);
+ void onError(const std::string& methodName, int error);
+
+ private:
+ std::string __subject;
+ ctx_sensor_rec_data_cb __callback;
+ void* __userData;
+ };
+
+}
+
+#endif
* limitations under the License.
*/
+#include <map>
#include <sensor_recorder_internal.h>
#include <SensorRecorderTypesPrivate.h>
#include <ServiceProxy.h>
-#include <map>
-#include <cmath>
+#include "QueryResultListener.h"
-typedef std::map<std::string, int> ctx_sensor_rec_option_t;
-typedef std::map<std::string, int> ctx_sensor_rec_query_t;
+#define STR_BUFFER_SIZE 128
using namespace ctx;
+typedef std::map<std::string, int64_t> ctx_sensor_rec_option_t;
+typedef std::map<std::string, int64_t> ctx_sensor_rec_query_t;
+
+static ServiceProxy* __getServiceProxy()
+{
+ static ServiceProxy proxy(CTX_SENSOR_RECORDER);
+ return &proxy;
+}
+
+static std::string __mapToJson(const std::map<std::string, int64_t>& input)
+{
+ std::string output("{");
+ char buf[STR_BUFFER_SIZE];
+
+ for (auto& elem : input) {
+ snprintf(buf, STR_BUFFER_SIZE, "\"%s\":%lld,", elem.first.c_str(), elem.second);
+ output += buf;
+ }
+
+ if (output.back() == ',')
+ output.back() = '}';
+ else
+ output += "}";
+
+ return output;
+}
+
EXPORT_API int ctx_sensor_rec_is_supported(const char* subject, bool *supported)
{
- *supported = false;
- return E_SUPPORT;
+ IF_FAIL_RETURN(subject && supported, E_PARAM);
+
+ int error = __getServiceProxy()->call(METHOD_IS_SUPPORTED, g_variant_new("(s)", subject));
+ *supported = (error == E_NONE);
+
+ if (error == E_SUPPORT)
+ error = E_NONE;
+
+ return error;
}
EXPORT_API int ctx_sensor_rec_start(const char* subject, ctx_sensor_rec_option_h option)
{
- return E_SUPPORT;
+ IF_FAIL_RETURN(subject && option, E_PARAM);
+
+ std::string optionStr = __mapToJson(*static_cast<ctx_sensor_rec_option_t*>(option));
+ GVariant* param = g_variant_new("(ss)", subject, optionStr.c_str());
+
+ return __getServiceProxy()->call(METHOD_START_REC, param);
}
EXPORT_API int ctx_sensor_rec_stop(const char* subject)
{
- return E_SUPPORT;
+ IF_FAIL_RETURN(subject, E_PARAM);
+ return __getServiceProxy()->call(METHOD_STOP_REC, g_variant_new("(s)", subject));
}
EXPORT_API int ctx_sensor_rec_create_option(ctx_sensor_rec_option_h *option)
EXPORT_API int ctx_sensor_rec_option_set_int(ctx_sensor_rec_option_h option, const char* param, int value)
{
- IF_FAIL_RETURN(option, E_PARAM);
+ IF_FAIL_RETURN(option && param, E_PARAM);
IF_FAIL_RETURN(STR_EQ(param, CTX_SENSOR_RECORDER_KEY_INTERVAL) ||
STR_EQ(param, CTX_SENSOR_RECORDER_KEY_RETENTION), E_PARAM);
+ (*static_cast<ctx_sensor_rec_option_t*>(option))[param] = static_cast<int64_t>(value);
+
return E_NONE;
}
EXPORT_API int ctx_sensor_rec_query_set_int(ctx_sensor_rec_query_h query, const char* param, int value)
{
- IF_FAIL_RETURN(query, E_PARAM);
+ IF_FAIL_RETURN(query && param, E_PARAM);
IF_FAIL_RETURN(STR_EQ(param, CTX_SENSOR_RECORDER_KEY_ANCHOR) ||
STR_EQ(param, CTX_SENSOR_RECORDER_KEY_INTERVAL) ||
STR_EQ(param, CTX_SENSOR_RECORDER_KEY_START_TIME) ||
STR_EQ(param, CTX_SENSOR_RECORDER_KEY_END_TIME), E_PARAM);
+ (*static_cast<ctx_sensor_rec_query_t*>(query))[param] = static_cast<int64_t>(value);
+
return E_NONE;
}
EXPORT_API int ctx_sensor_rec_query_set_time(ctx_sensor_rec_query_h query, const char* param, time_t t)
{
- IF_FAIL_RETURN(query, E_PARAM);
+ IF_FAIL_RETURN(query && param, E_PARAM);
IF_FAIL_RETURN(STR_EQ(param, CTX_SENSOR_RECORDER_KEY_ANCHOR) ||
STR_EQ(param, CTX_SENSOR_RECORDER_KEY_START_TIME) ||
STR_EQ(param, CTX_SENSOR_RECORDER_KEY_END_TIME), E_PARAM);
+ (*static_cast<ctx_sensor_rec_query_t*>(query))[param] = static_cast<int64_t>(t);
+
return E_NONE;
}
EXPORT_API int ctx_sensor_rec_read(const char* subject, ctx_sensor_rec_query_h query, ctx_sensor_rec_data_cb cb, void *user_data)
{
- IF_FAIL_RETURN(query, E_PARAM);
- IF_FAIL_RETURN(cb, E_PARAM);
+ IF_FAIL_RETURN(subject && query && cb, E_PARAM);
- return E_SUPPORT;
+ std::string queryStr = __mapToJson(*static_cast<ctx_sensor_rec_query_t*>(query));
+ GVariant* param = g_variant_new("(ss)", subject, queryStr.c_str());
+
+ QueryResultListener* listener = new QueryResultListener(subject, cb, user_data);
+ int error = __getServiceProxy()->call(METHOD_READ_REC, param, listener);
+
+ if (error != E_NONE)
+ delete listener;
+
+ return error;
}
EXPORT_API int ctx_sensor_rec_read_sync(const char* subject, ctx_sensor_rec_query_h query, ctx_sensor_rec_data_cb cb, void *user_data)
{
- IF_FAIL_RETURN(query, E_PARAM);
- IF_FAIL_RETURN(cb, E_PARAM);
+ IF_FAIL_RETURN(subject && query && cb, E_PARAM);
+
+ std::string queryStr = __mapToJson(*static_cast<ctx_sensor_rec_query_t*>(query));
+ GVariant* param = g_variant_new("(ss)", subject, queryStr.c_str());
+
+ GVariant* outParam = NULL;
+ int error = __getServiceProxy()->call(METHOD_READ_REC, param, &outParam);
+ IF_FAIL_RETURN(error == E_NONE, error);
- return E_SUPPORT;
+ QueryResultListener* listener = new QueryResultListener(subject, cb, user_data);
+ listener->onSuccess(METHOD_READ_REC, outParam);
+
+ g_variant_unref(outParam);
+ return E_NONE;
+}
+
+static bool __getValueFromData(ctx_sensor_rec_data_s* data, const char* key, const char** value)
+{
+ for (unsigned int i = 0; i < data->keys.size(); ++i) {
+ if (data->keys[i] != key) continue;
+ return data->tuple->getAt(i, value);
+ }
+ return false;
}
EXPORT_API int ctx_sensor_rec_data_get_time(ctx_sensor_rec_data_h data, time_t *start_time, time_t *end_time)
{
- return E_SUPPORT;
+ IF_FAIL_RETURN(data && start_time && end_time, E_PARAM);
+
+ const char* buf = NULL;
+
+ if (__getValueFromData(static_cast<ctx_sensor_rec_data_s*>(data), CTX_SENSOR_RECORDER_KEY_START_TIME, &buf))
+ *start_time = static_cast<time_t>(g_ascii_strtoll(buf, NULL, 10));
+ else
+ return E_PARAM;
+
+ if (__getValueFromData(static_cast<ctx_sensor_rec_data_s*>(data), CTX_SENSOR_RECORDER_KEY_END_TIME, &buf))
+ *end_time = static_cast<time_t>(g_ascii_strtoll(buf, NULL, 10));
+ else
+ return E_PARAM;
+
+ return E_NONE;
}
EXPORT_API int ctx_sensor_rec_data_get_int(ctx_sensor_rec_data_h data, const char* key, int *value)
{
- return E_SUPPORT;
+ IF_FAIL_RETURN(data && key && value, E_PARAM);
+
+ const char* buf = NULL;
+
+ if (__getValueFromData(static_cast<ctx_sensor_rec_data_s*>(data), key, &buf))
+ *value = static_cast<int>(g_ascii_strtoll(buf, NULL, 10));
+ else
+ return E_PARAM;
+
+ return E_NONE;
}
EXPORT_API int ctx_sensor_rec_data_get_double(ctx_sensor_rec_data_h data, const char* key, double *value)
{
- return E_SUPPORT;
+ IF_FAIL_RETURN(data && key && value, E_PARAM);
+
+ const char* buf = NULL;
+
+ if (__getValueFromData(static_cast<ctx_sensor_rec_data_s*>(data), key, &buf))
+ *value = g_ascii_strtod(buf, NULL);
+ else
+ return E_PARAM;
+
+ return E_NONE;
}
SET(target "${PROJECT_NAME}-server-genuine")
-SET(DEPS "${DEPS} context-common-server")
+SET(DEPS "${DEPS} context-common-server capi-system-info sensor")
-FILE(GLOB SRCS *.cpp ../shared/*.cpp)
+FILE(GLOB_RECURSE SRCS *.cpp ../shared/*.cpp)
MESSAGE("Sources: ${SRCS}")
INCLUDE(FindPkgConfig)
--- /dev/null
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <PlatformDatabase.h>
+#include "SensorDatabase.h"
+
+using namespace ctx;
+
+Database* SensorDatabase::__userDatabase = NULL;
+
+SensorDatabase::SensorDatabase()
+{
+}
+
+bool SensorDatabase::open(uid_t uid)
+{
+ if (isReady()) {
+ _W("Closing the current DB");
+ close();
+ }
+
+ __userDatabase = new PlatformDatabase(BASE_PATH, uid);
+
+ if (!__userDatabase->open()) {
+ _E("DB open failed");
+ delete __userDatabase;
+ __userDatabase = NULL;
+ return false;
+ }
+
+ _I("User DB opened");
+ return true;
+}
+
+void SensorDatabase::close()
+{
+ if (!__userDatabase)
+ return;
+
+ __userDatabase->close();
+ _I("User DB closed");
+
+ delete __userDatabase;
+ __userDatabase = NULL;
+}
+
+bool SensorDatabase::isReady()
+{
+ return (__userDatabase != NULL);
+}
+
+bool SensorDatabase::execute(const std::string& query, const std::string& columnTypes, std::vector<std::string>* columnNames, std::vector<std::shared_ptr<Tuple>>* queryResult)
+{
+ IF_FAIL_RETURN_TAG(isReady(), false, _W, "DB not ready");
+ return __userDatabase->execute(query, columnTypes, columnNames, queryResult);
+}
+
+bool SensorDatabase::execute(const std::string& query, std::vector<std::shared_ptr<Tuple>>* queryResult)
+{
+ IF_FAIL_RETURN_TAG(isReady(), false, _W, "DB not ready");
+ return __userDatabase->execute(query, queryResult);
+}
--- /dev/null
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __SENSOR_RECORDER_DATABASE_H__
+#define __SENSOR_RECORDER_DATABASE_H__
+
+#include <Database.h>
+#include <SensorRecorderTypesPrivate.h>
+
+namespace ctx {
+
+ class SensorDatabase {
+ public:
+ static bool open(uid_t uid);
+ static void close();
+ static bool isReady();
+ static bool execute(const std::string& query, const std::string& columnTypes, std::vector<std::string>* columnNames, std::vector<std::shared_ptr<Tuple>>* queryResult);
+ static bool execute(const std::string& query, std::vector<std::shared_ptr<Tuple>>* queryResult);
+
+ private:
+ SensorDatabase();
+
+ static Database* __userDatabase;
+ };
+
+}
+
+#endif /* __SENSOR_RECORDER_DATABASE_H__ */
#include <ServiceBase.h>
#include <MethodCall.h>
+#include <Tuple.h>
#include "SensorRecorderClient.h"
+#include "legacy/SensorProvider.h"
+
+#define IDX_SUBJECT 0
+#define IDX_OPTION 1
using namespace ctx;
void SensorRecorderClient::onMethodCalled(MethodCall* methodCall)
{
+ try {
+ __verifyUid(methodCall->getUid());
+ std::string subject = __extractParameter(methodCall->getParam(), IDX_SUBJECT);
+ _D("%s(%s)", methodCall->getMethodName().c_str(), subject.c_str());
+
+ SensorProvider* provider = SensorProvider::getInstance(subject);
+
+ if (methodCall->getMethodName() == METHOD_IS_SUPPORTED) {
+ methodCall->reply(E_NONE);
+
+ } else if (methodCall->getMethodName() == METHOD_START_REC) {
+ __startRecording(*provider, *methodCall);
+
+ } else if (methodCall->getMethodName() == METHOD_STOP_REC) {
+ __stopRecording(*provider, *methodCall);
+
+ } else if (methodCall->getMethodName() == METHOD_READ_REC) {
+ __readRecords(*provider, *methodCall);
+ }
+
+ } catch (const int error) {
+ _W("Catch: %s", CTX_ERROR_STR(error));
+ methodCall->reply(error);
+ }
+
delete methodCall;
}
void SensorRecorderClient::onDisconnected()
{
}
+
+void SensorRecorderClient::__verifyUid(uid_t uid)
+{
+ if (!isSystemUid(uid) && uid != ServiceBase::getActiveUser()) {
+ _E("Invalid Uid: %u != %u (ActiveUser)", uid, ServiceBase::getActiveUser());
+ throw static_cast<int>(E_ACCESS);
+ }
+}
+
+std::string SensorRecorderClient::__extractParameter(GVariant* param, unsigned int idx)
+{
+ const char* val = NULL;
+ g_variant_get_child(param, idx, "&s", &val);
+
+ if (!val)
+ throw static_cast<int>(E_PARAM);
+
+ return val;
+}
+
+void SensorRecorderClient::__startRecording(SensorProvider& provider, MethodCall& methodCall)
+{
+ if (!hasPrivilege(provider.getPrivilege()))
+ throw static_cast<int>(E_ACCESS);
+
+ // TODO: use the 'real' package id, instead of the smack label
+ std::string pkgId = getClientId();
+ std::string option = __extractParameter(methodCall.getParam(), IDX_OPTION);
+ _D("Option: %s", option.c_str());
+
+ int error = provider.startRecording(pkgId, option);
+ methodCall.reply(error);
+}
+
+void SensorRecorderClient::__stopRecording(SensorProvider& provider, MethodCall& methodCall)
+{
+ // TODO: use the 'real' package id, instead of the smack label
+ std::string pkgId = getClientId();
+
+ int error = provider.stopRecording(pkgId);
+ methodCall.reply(error);
+}
+
+void SensorRecorderClient::__readRecords(SensorProvider& provider, MethodCall& methodCall)
+{
+ if (!hasPrivilege(provider.getPrivilege()))
+ throw static_cast<int>(E_ACCESS);
+
+ // TODO: use the 'real' package id, instead of the smack label
+ std::string pkgId = getClientId();
+ std::string option = __extractParameter(methodCall.getParam(), IDX_OPTION);
+ _D("Option: %s", option.c_str());
+
+ std::string projection;
+ std::vector<std::shared_ptr<Tuple>> tuples;
+
+ int error = provider.readRecords(option, &projection, &tuples);
+ if (error != E_NONE)
+ throw error;
+ if (tuples.empty())
+ throw static_cast<int>(E_NO_DATA);
+
+ methodCall.reply(g_variant_new("(sv)", projection.c_str(), Tuple::toGVariant(tuples)));
+}
namespace ctx {
+ class SensorProvider;
+
class SensorRecorderClient : public ClientBase {
public:
SensorRecorderClient(ServiceBase* hostService, const std::string& busName);
void onMethodCalled(MethodCall* methodCall);
void onDisconnected();
+
+ private:
+ void __verifyUid(uid_t uid);
+ std::string __extractParameter(GVariant* param, unsigned int idx);
+ void __startRecording(SensorProvider& provider, MethodCall& methodCall);
+ void __stopRecording(SensorProvider& provider, MethodCall& methodCall);
+ void __readRecords(SensorProvider& provider, MethodCall& methodCall);
};
}
#include <SensorRecorderTypesPrivate.h>
#include <SensorRecorderService.h>
#include "SensorRecorderClient.h"
+#include "SensorDatabase.h"
+#include "SensorTimer.h"
+#include "legacy/SensorProvider.h"
+#include "legacy/RecorderClientInfo.h"
using namespace ctx;
bool SensorRecorderService::prepare()
{
- /* Service-specific initialization tasks */
+ RecorderClientInfo::setHostService(this);
+
+ if (!SensorDatabase::open(getActiveUser()))
+ return false;
+
+ SensorTimer::init(this);
+ SensorProvider::init();
+
return true;
}
void SensorRecorderService::cleanup()
{
+ SensorProvider::release();
+ SensorTimer::release();
+ SensorDatabase::close();
}
ClientBase* SensorRecorderService::createClient(const std::string& busName)
{
- return new(std::nothrow) SensorRecorderClient(this, busName);
+ return new SensorRecorderClient(this, busName);
}
--- /dev/null
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <Timer.h>
+#include <ITimerListener.h>
+#include <ServiceBase.h>
+#include <SensorRecorderTypesPrivate.h>
+#include "SensorTimer.h"
+
+using namespace ctx;
+
+Timer* SensorTimer::__timer = NULL;
+
+SensorTimer::SensorTimer()
+{
+}
+
+void SensorTimer::init(ServiceBase* hostService)
+{
+ __timer = new Timer(hostService);
+}
+
+void SensorTimer::release()
+{
+ delete __timer;
+}
+
+unsigned int SensorTimer::addIdle(GSourceFunc callback, gpointer userData)
+{
+ return __timer->addIdle(callback, userData);
+}
+
+unsigned int SensorTimer::addAlarm(unsigned int intervalMin, ITimerListener* listener)
+{
+ return __timer->addAlarm(intervalMin, listener, NULL);
+}
+
+void SensorTimer::cancel(unsigned int timerId)
+{
+ __timer->cancel(timerId);
+}
--- /dev/null
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __SENSOR_RECORDER_TIMER_H__
+#define __SENSOR_RECORDER_TIMER_H__
+
+namespace ctx {
+
+ class ServiceBase;
+ class Timer;
+ class ITimerListener;
+
+ class SensorTimer {
+ public:
+ static void init(ServiceBase* hostService);
+ static void release();
+
+ static unsigned int addIdle(GSourceFunc callback, gpointer userData);
+ static unsigned int addAlarm(unsigned int intervalMin, ITimerListener* listener);
+ static void cancel(unsigned int timerId);
+
+ private:
+ SensorTimer();
+
+ static Timer* __timer;
+ };
+
+}
+
+#endif /* __SENSOR_RECORDER_TIMER_H__ */
+++ /dev/null
-/*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <SensorRecorderTypes.h>
-#include <CreateProvider.h>
-#include "pedometer/Pedometer.h"
-#include "pressure/Pressure.h"
-#include "sleep/Sleep.h"
-#include "heartrate/HeartRate.h"
-
-using namespace ctx;
-
-extern "C" SO_EXPORT ContextProvider* CreateProvider(const char *subject)
-{
- ADD_PROVIDER(SUBJ_SENSOR_PEDOMETER, PedometerProvider);
- ADD_PROVIDER(SUBJ_SENSOR_PRESSURE, PressureProvider);
- ADD_PROVIDER(SUBJ_SENSOR_SLEEP_MONITOR, SleepProvider);
- ADD_PROVIDER(SUBJ_SENSOR_HEART_RATE, HeartRateProvider);
-
- return NULL;
-}
* limitations under the License.
*/
-#include <Types.h>
-#include <SensorRecorderTypes.h>
+#include <SensorRecorderTypesPrivate.h>
+#include "../SensorDatabase.h"
#include "Querier.h"
using namespace ctx;
-Querier::Querier(ContextProvider *provider, Json option) :
- __provider(provider),
- __option(option)
+Querier::Querier()
{
}
{
}
-int Querier::query(const char *sql)
+std::string Querier::getProjection()
{
- return __dbMgr.execute(0, sql, this) ? ERR_NONE : ERR_OPERATION_FAILED;
+ return "";
}
-int Querier::queryRaw(int startTime, int endTime)
+int Querier::query(const char *sql, std::vector<std::shared_ptr<Tuple>>* tuples)
{
- return ERR_INVALID_PARAMETER;
+ bool ret = SensorDatabase::execute(sql, tuples);
+ return ret ? E_NONE : E_FAILED;
}
-int Querier::query(int startTime, int endTime)
+int Querier::queryRaw(int startTime, int endTime, std::vector<std::shared_ptr<Tuple>>* tuples)
{
- return ERR_INVALID_PARAMETER;
+ return E_PARAM;
}
-int Querier::query(int startTime, int endTime, int anchor, int interval)
-{
- _D("Ignore anchor & interval");
- return query(startTime, endTime);
-}
-
-void Querier::onTableCreated(unsigned int queryId, int error)
-{
-}
-
-void Querier::onInserted(unsigned int queryId, int error, int64_t rowId)
+int Querier::query(int startTime, int endTime, std::vector<std::shared_ptr<Tuple>>* tuples)
{
+ return E_PARAM;
}
-void Querier::onExecuted(unsigned int queryId, int error, std::vector<Json>& records)
+int Querier::query(int startTime, int endTime, int anchor, int interval, std::vector<std::shared_ptr<Tuple>>* tuples)
{
- Json response;
- __convertToResponse(records, response);
- __provider->replyToRead(__option, error, response);
- delete this;
-}
-
-void Querier::__convertToResponse(std::vector<Json> &sqlResult, Json &response)
-{
- std::list<std::string> keys;
- std::string val;
-
- response.set(NULL, KEY_RESULT_SIZE, static_cast<int>(sqlResult.size()));
-
- for (Json &tuple : sqlResult) {
- tuple.getKeys(&keys);
- for (std::string &key : keys) {
- tuple.get(NULL, key.c_str(), &val);
- response.append(NULL, key.c_str(), val);
- }
- keys.clear();
- }
+ _D("Ignore anchor & interval");
+ return query(startTime, endTime, tuples);
}
#ifndef __CONTEXT_QUERIER_H__
#define __CONTEXT_QUERIER_H__
-#include <ContextProvider.h>
-#include <DatabaseManager.h>
+#include <vector>
+#include <Tuple.h>
namespace ctx {
- class Querier : public IDatabaseListener {
+ class Querier {
public:
- Querier(ContextProvider *provider, Json option);
+ Querier();
virtual ~Querier();
- virtual int queryRaw(int startTime, int endTime);
- virtual int query(int startTime, int endTime);
- virtual int query(int startTime, int endTime, int anchor, int interval);
+ virtual std::string getProjection();
- protected:
- int query(const char *sql);
- void onTableCreated(unsigned int queryId, int error);
- void onInserted(unsigned int queryId, int error, int64_t rowId);
- void onExecuted(unsigned int queryId, int error, std::vector<Json>& records);
-
- private:
- void __convertToResponse(std::vector<Json> &sqlResult, Json &response);
+ virtual int queryRaw(int startTime, int endTime, std::vector<std::shared_ptr<Tuple>>* tuples);
+ virtual int query(int startTime, int endTime, std::vector<std::shared_ptr<Tuple>>* tuples);
+ virtual int query(int startTime, int endTime, int anchor, int interval, std::vector<std::shared_ptr<Tuple>>* tuples);
- DatabaseManager __dbMgr;
- ContextProvider *__provider;
- Json __option;
+ protected:
+ int query(const char *sql, std::vector<std::shared_ptr<Tuple>>* tuples);
};
}
*/
#include <sqlite3.h>
-#include <Types.h>
-#include <SensorRecorderTypes.h>
+#include <SensorRecorderService.h>
+#include "../SensorDatabase.h"
#include "TypesInternal.h"
#include "SensorProvider.h"
-#include "ClientInfo.h"
+#include "RecorderClientInfo.h"
using namespace ctx;
-unsigned int ClientInfo::__refCnt = 0;
-DatabaseManager *ClientInfo::__dbMgr = NULL;
-UninstallMonitor *ClientInfo::__uninstallMonitor = NULL;
+unsigned int RecorderClientInfo::__refCnt = 0;
+UninstallMonitor *RecorderClientInfo::__uninstallMonitor = NULL;
+SensorRecorderService* RecorderClientInfo::__hostService = NULL;
-ClientInfo::ClientInfo()
+RecorderClientInfo::RecorderClientInfo()
{
if (++__refCnt != 1)
return;
- __uninstallMonitor = new(std::nothrow) UninstallMonitor();
- IF_FAIL_VOID_TAG(__uninstallMonitor, _E, "Memory allocation failed");
+ __uninstallMonitor = new UninstallMonitor(__hostService);
- __dbMgr = new(std::nothrow) DatabaseManager();
- IF_FAIL_VOID_TAG(__dbMgr, _E, "Memory allocation failed");
-
- bool ret = __dbMgr->executeSync(
+ bool ret = SensorDatabase::execute(
"CREATE TABLE IF NOT EXISTS " CLIENT_INFO " (" \
KEY_SUBJECT " TEXT NOT NULL," \
KEY_PKG_ID " TEXT NOT NULL," \
IF_FAIL_VOID_TAG(ret, _E, "Table creation failed");
}
-ClientInfo::~ClientInfo()
+RecorderClientInfo::~RecorderClientInfo()
{
if (--__refCnt != 0)
return;
- delete __dbMgr;
- __dbMgr = NULL;
-
delete __uninstallMonitor;
__uninstallMonitor = NULL;
}
-int ClientInfo::get(std::string subject, std::string pkgId, Json& option)
+int RecorderClientInfo::get(std::string subject, std::string pkgId, Json& option)
{
- IF_FAIL_RETURN_TAG(__dbMgr, ERR_OPERATION_FAILED, _W, "DB not initialized");
-
bool ret;
std::string optStr;
- std::vector<Json> records;
+ std::vector<std::shared_ptr<Tuple>> tuples;
+
char *query = sqlite3_mprintf(
"SELECT " KEY_OPTION " FROM " CLIENT_INFO " WHERE " \
KEY_SUBJECT "='%q' AND " KEY_PKG_ID "='%q'",
subject.c_str(), pkgId.c_str());
- ret = __dbMgr->executeSync(query, &records);
+ ret = SensorDatabase::execute(query, &tuples);
sqlite3_free(query);
- IF_FAIL_RETURN(ret, ERR_OPERATION_FAILED);
- IF_FAIL_RETURN(!records.empty(), ERR_NO_DATA);
- IF_FAIL_RETURN(records[0].get(NULL, KEY_OPTION, &optStr), ERR_OPERATION_FAILED);
+ IF_FAIL_RETURN(ret, E_FAILED);
+ IF_FAIL_RETURN(!tuples.empty(), E_NO_DATA);
+ tuples[0]->getAt(0, &optStr);
option = optStr;
- return ERR_NONE;
+ return E_NONE;
}
-int ClientInfo::get(std::string subject, std::vector<Json>& options)
+int RecorderClientInfo::get(std::string subject, std::vector<Json>& options)
{
- IF_FAIL_RETURN_TAG(__dbMgr, ERR_OPERATION_FAILED, _W, "DB not initialized");
-
bool ret;
std::string optStr;
- std::vector<Json> records;
+ std::vector<std::shared_ptr<Tuple>> tuples;
+
char *query = sqlite3_mprintf(
"SELECT " KEY_OPTION " FROM " CLIENT_INFO " WHERE " \
KEY_SUBJECT "='%q'",
subject.c_str());
- ret = __dbMgr->executeSync(query, &records);
+ ret = SensorDatabase::execute(query, &tuples);
sqlite3_free(query);
- IF_FAIL_RETURN(ret, ERR_OPERATION_FAILED);
- IF_FAIL_RETURN(!records.empty(), ERR_NO_DATA);
+ IF_FAIL_RETURN(ret, E_FAILED);
+ IF_FAIL_RETURN(!tuples.empty(), E_NO_DATA);
- for (Json& jObj : records) {
- if (!jObj.get(NULL, KEY_OPTION, &optStr))
- continue;
+ for (auto& tuple : tuples) {
+ tuple->getAt(0, &optStr);
options.push_back(Json(optStr));
}
- return ERR_NONE;
+ return E_NONE;
}
-bool ClientInfo::exist(std::string subject)
+bool RecorderClientInfo::exist(std::string subject)
{
- IF_FAIL_RETURN_TAG(__dbMgr, ERR_OPERATION_FAILED, _W, "DB not initialized");
-
bool ret;
- std::vector<Json> records;
+ std::vector<std::shared_ptr<Tuple>> tuples;
+
char *query = sqlite3_mprintf(
"SELECT " KEY_PKG_ID " FROM " CLIENT_INFO " WHERE " \
KEY_SUBJECT "='%q' LIMIT 1",
subject.c_str());
- ret = __dbMgr->executeSync(query, &records);
+ ret = SensorDatabase::execute(query, &tuples);
sqlite3_free(query);
IF_FAIL_RETURN(ret, false);
- IF_FAIL_RETURN(!records.empty(), false);
+ IF_FAIL_RETURN(!tuples.empty(), false);
return true;
}
-bool ClientInfo::set(std::string subject, std::string pkgId, Json option, int retentionPeriod)
+bool RecorderClientInfo::set(std::string subject, std::string pkgId, Json option, int retentionPeriod)
{
- IF_FAIL_RETURN_TAG(__dbMgr, false, _W, "DB not initialized");
-
bool ret;
char *query = sqlite3_mprintf(
"INSERT INTO " CLIENT_INFO " VALUES ('%q', '%q', '%q', %d)",
subject.c_str(), pkgId.c_str(), option.str().c_str(), retentionPeriod);
- ret = __dbMgr->executeSync(query, NULL);
+ ret = SensorDatabase::execute(query, NULL);
sqlite3_free(query);
return ret;
}
-bool ClientInfo::remove(std::string subject, std::string pkgId)
+bool RecorderClientInfo::remove(std::string subject, std::string pkgId)
{
- IF_FAIL_RETURN_TAG(__dbMgr, false, _W, "DB not initialized");
-
bool ret;
char *query = sqlite3_mprintf(
"DELETE FROM " CLIENT_INFO " WHERE " \
KEY_SUBJECT "='%q' AND " KEY_PKG_ID "='%q'",
subject.c_str(), pkgId.c_str());
- ret = __dbMgr->executeSync(query, NULL);
+ ret = SensorDatabase::execute(query, NULL);
sqlite3_free(query);
return ret;
}
-void ClientInfo::getParam(std::vector<Json> &options, const char *key, float *min, float *max)
+void RecorderClientInfo::getParam(std::vector<Json> &options, const char *key, float *min, float *max)
{
double val;
}
}
-void ClientInfo::purgeClient(std::string pkgId)
+void RecorderClientInfo::purgeClient(std::string pkgId)
{
- IF_FAIL_VOID_TAG(__dbMgr, _W, "DB not initialized");
-
bool ret;
std::string subject;
- std::vector<Json> records;
+ std::vector<std::shared_ptr<Tuple>> tuples;
char *query = sqlite3_mprintf(
"SELECT " KEY_SUBJECT " FROM " CLIENT_INFO " WHERE " KEY_PKG_ID "='%q'",
pkgId.c_str());
- ret = __dbMgr->executeSync(query, &records);
+ ret = SensorDatabase::execute(query, &tuples);
sqlite3_free(query);
IF_FAIL_VOID(ret);
- for (Json& jObj : records) {
- if (!jObj.get(NULL, KEY_SUBJECT, &subject))
- continue;
+ for (auto& tuple : tuples) {
+ tuple->getAt(0, &subject);
_I("Stop recording '%s' for '%s'", subject.c_str(), pkgId.c_str());
SensorProvider::removeClient(subject, pkgId);
}
}
+
+void RecorderClientInfo::setHostService(SensorRecorderService* hostService)
+{
+ __hostService = hostService;
+}
* limitations under the License.
*/
-#ifndef __CONTEXT_CLIENT_INFO_H__
-#define __CONTEXT_CLIENT_INFO_H__
+#ifndef __SENSOR_RECORDER_CLIENT_INFO_H__
+#define __SENSOR_RECORDER_CLIENT_INFO_H__
#include <string>
#include <vector>
#include <Json.h>
-#include <DatabaseManager.h>
+#include <SensorRecorderTypesPrivate.h>
#include "UninstallMonitor.h"
namespace ctx {
- class ClientInfo {
+ class SensorRecorderService;
+
+ class RecorderClientInfo {
public:
- ClientInfo();
- ~ClientInfo();
+ RecorderClientInfo();
+ ~RecorderClientInfo();
int get(std::string subject, std::string pkgId, Json& option);
int get(std::string subject, std::vector<Json>& options);
void getParam(std::vector<Json>& options, const char *key, float *min, float *max);
static void purgeClient(std::string pkgId);
+ static void setHostService(SensorRecorderService* hostService);
private:
static unsigned int __refCnt;
- static DatabaseManager *__dbMgr;
static UninstallMonitor *__uninstallMonitor;
+ static SensorRecorderService* __hostService;
};
}
-#endif /* _CONTEXT_CLIENT_INFO_H_ */
+#endif
*/
#include <sqlite3.h>
-#include <SensorRecorderTypes.h>
+#include <SensorRecorderTypesPrivate.h>
+#include "../SensorDatabase.h"
#include "TypesInternal.h"
#include "TimeUtil.h"
#include "SensorLogger.h"
bool SensorLogger::executeQuery(const char *query)
{
- return __dbMgr.execute(0, query, NULL);
+ return SensorDatabase::execute(query, NULL);
}
void SensorLogger::removeExpired(const char *subject, const char *tableName, const char *timeKey)
" WHERE " KEY_SUBJECT "='%s')",
tableName, timeKey, timestamp, subject);
- __dbMgr.execute(0, query, NULL);
+ SensorDatabase::execute(query, NULL);
sqlite3_free(query);
__lastRemovalTime = timestamp;
#ifndef __CONTEXT_SENSOR_LOGGER_H__
#define __CONTEXT_SENSOR_LOGGER_H__
-#include <DatabaseManager.h>
-
namespace ctx {
class SensorLogger {
private:
uint64_t __lastRemovalTime;
- DatabaseManager __dbMgr;
};
}
#include <ctime>
#include <cmath>
-#include <SensorRecorderTypes.h>
+#include <system_info.h>
+#include <SensorRecorderTypesPrivate.h>
#include "TypesInternal.h"
#include "SensorProvider.h"
+#include "heartrate/HeartRate.h"
+#include "pedometer/Pedometer.h"
+#include "pressure/Pressure.h"
+#include "sleep/Sleep.h"
+
+#define CREATE_PROVIDER(PROVIDERTYPE) \
+ do { \
+ SensorProvider* prvd = new PROVIDERTYPE(); \
+ if (prvd->isSupported()) { \
+ __providerMap[prvd->getSubject()] = prvd; \
+ } else { \
+ delete prvd; \
+ } \
+ } while (false)
+
using namespace ctx;
std::map<std::string, SensorProvider*> SensorProvider::__providerMap;
SensorProvider::SensorProvider(const char *subject) :
- ContextProvider(subject),
- sensorLogger(NULL)
+ __subject(subject)
{
- __providerMap[subject] = this;
}
SensorProvider::~SensorProvider()
{
- __providerMap.erase(getSubject());
- delete sensorLogger;
}
-int SensorProvider::subscribe(Json option, Json *requestResult)
+void SensorProvider::init()
+{
+ CREATE_PROVIDER(HeartRateProvider);
+ CREATE_PROVIDER(PedometerProvider);
+ CREATE_PROVIDER(PressureProvider);
+ CREATE_PROVIDER(SleepProvider);
+}
+
+void SensorProvider::release()
+{
+ for (auto& prvd : __providerMap) {
+ delete prvd.second;
+ }
+ __providerMap.clear();
+}
+
+SensorProvider* SensorProvider::getInstance(const std::string& subject)
{
- return ERR_NONE;
+ auto it = __providerMap.find(subject);
+
+ if (it == __providerMap.end())
+ throw static_cast<int>(E_SUPPORT);
+
+ return it->second;
}
-int SensorProvider::unsubscribe(Json option)
+const char* SensorProvider::getSubject()
{
- return ERR_NONE;
+ return __subject.c_str();
}
-int SensorProvider::read(Json option, Json *requestResult)
+bool SensorProvider::isSupported()
+{
+ return false;
+}
+
+bool SensorProvider::getSystemInfo(const char* key)
+{
+ bool supported = false;
+ int ret = system_info_get_platform_bool(key, &supported);
+ IF_FAIL_RETURN_TAG(ret == SYSTEM_INFO_ERROR_NONE, false, _E, "system_info_get_platform_bool() failed");
+ return supported;
+}
+
+const char* SensorProvider::getPrivilege()
+{
+ return NULL;
+}
+
+int SensorProvider::readRecords(Json option, std::string* projection, std::vector<std::shared_ptr<Tuple>>* tuples)
{
int endTime = static_cast<int>(time(NULL)) + 1;
int startTime = endTime - DEFAULT_QUERY_PERIOD - 1;
int interval = -1;
if (option.get(NULL, KEY_START_TIME, &startTime))
- IF_FAIL_RETURN(startTime >= 0, ERR_INVALID_PARAMETER);
+ IF_FAIL_RETURN(startTime >= 0, E_PARAM);
if (option.get(NULL, KEY_END_TIME, &endTime))
- IF_FAIL_RETURN(endTime >= 0, ERR_INVALID_PARAMETER);
+ IF_FAIL_RETURN(endTime >= 0, E_PARAM);
if (option.get(NULL, KEY_ANCHOR, &anchor))
- IF_FAIL_RETURN(anchor >= 0, ERR_INVALID_PARAMETER);
+ IF_FAIL_RETURN(anchor >= 0, E_PARAM);
if (option.get(NULL, KEY_INTERVAL, &interval))
- IF_FAIL_RETURN(interval >= 0, ERR_INVALID_PARAMETER);
+ IF_FAIL_RETURN(interval >= 0, E_PARAM);
if (endTime >= 0 && startTime >= endTime)
- return ERR_INVALID_PARAMETER;
+ return E_PARAM;
if (interval > 0 && anchor < 0)
anchor = endTime;
if (anchor >= 0 && interval < 0)
interval = static_cast<int>(ceil(static_cast<double>(endTime - startTime) / SECONDS_PER_MINUTE));
- int ret;
- Querier *querier = getQuerier(option);
- IF_FAIL_RETURN(querier, ERR_OPERATION_FAILED);
+ int ret = E_FAILED;
- sensorLogger->flushCache(true);
+ getLogger()->flushCache(true);
+ *projection = getQuerier()->getProjection();
if (interval == 0)
- ret = querier->queryRaw(startTime, endTime);
+ ret = getQuerier()->queryRaw(startTime, endTime, tuples);
else if (interval > 0)
- ret = querier->query(startTime, endTime, anchor, interval * SECONDS_PER_MINUTE);
+ ret = getQuerier()->query(startTime, endTime, anchor, interval * SECONDS_PER_MINUTE, tuples);
else
- ret = querier->query(startTime, endTime);
-
- if (ret != ERR_NONE)
- delete querier;
+ ret = getQuerier()->query(startTime, endTime, tuples);
return ret;
}
-int SensorProvider::write(Json data, Json *requestResult)
+int SensorProvider::startRecording(const std::string& pkgId, Json option)
{
- IF_FAIL_RETURN(sensorLogger, ERR_OPERATION_FAILED);
-
- std::string operation;
- std::string pkgId;
- Json option;
int retentionPeriod = DEFAULT_RETENTION;
- _J("Data", data);
-
- IF_FAIL_RETURN(data.get(NULL, KEY_OPERATION, &operation), ERR_INVALID_PARAMETER);
- IF_FAIL_RETURN(data.get(NULL, KEY_CLIENT_PKG_ID, &pkgId), ERR_INVALID_PARAMETER);
-
- data.get(NULL, KEY_OPTION, &option);
+ _D("PkgId: %s", pkgId.c_str());
+ _J("Option", option);
if (option.get(NULL, KEY_RETENTION, &retentionPeriod)) {
retentionPeriod *= SECONDS_PER_HOUR;
option.remove(NULL, KEY_RETENTION);
}
- IF_FAIL_RETURN(verifyOption(option), ERR_INVALID_PARAMETER);
+ IF_FAIL_RETURN(verifyOption(option), E_PARAM);
- if (operation == VAL_START)
- return __addClient(pkgId, retentionPeriod, option);
- else if (operation == VAL_STOP)
- return __removeClient(pkgId);
+ return __addClient(pkgId, retentionPeriod, option);
+}
+
+int SensorProvider::stopRecording(const std::string& pkgId)
+{
+ _D("PkgId: %s", pkgId.c_str());
- return ERR_NOT_SUPPORTED;
+ return __removeClient(pkgId);
}
bool SensorProvider::verifyOption(Json option)
int ret;
/* Validate the retention period */
- IF_FAIL_RETURN(retentionPeriod > 0 && retentionPeriod <= MAX_RETENTION_PERIOD, ERR_INVALID_PARAMETER);
+ IF_FAIL_RETURN(retentionPeriod > 0 && retentionPeriod <= MAX_RETENTION_PERIOD, E_PARAM);
/* Check if the app already started Sensor recording */
ret = __clientInfo.get(getSubject(), pkgId, tmp);
- IF_FAIL_RETURN(ret != ERR_NONE, ERR_ALREADY_STARTED);
- IF_FAIL_RETURN(ret == ERR_NO_DATA, ERR_OPERATION_FAILED);
+ IF_FAIL_RETURN(ret != E_NONE, E_STARTED);
+ IF_FAIL_RETURN(ret == E_NO_DATA, E_FAILED);
/* Store the app's request */
if (!__clientInfo.set(getSubject(), pkgId, option, retentionPeriod))
- return ERR_OPERATION_FAILED;
+ return E_FAILED;
/* If not listening the sensor yet, start */
- sensorLogger->start();
+ getLogger()->start();
- return ERR_NONE;
+ return E_NONE;
}
int SensorProvider::__removeClient(std::string pkgId)
int ret;
/* Remove the app's request first */
- IF_FAIL_RETURN(__clientInfo.remove(getSubject(), pkgId), ERR_OPERATION_FAILED);
+ IF_FAIL_RETURN(__clientInfo.remove(getSubject(), pkgId), E_FAILED);
/* Check if there is no client anymore */
ret = __clientInfo.get(getSubject(), options);
- if (ret == ERR_NONE) {
+ if (ret == E_NONE) {
/* Still, one or more clients exist */
/* If necessary, the logger restarts its logging logic with updated parameters */
- sensorLogger->start();
- return ERR_NONE;
+ getLogger()->start();
+ return E_NONE;
- } else if (ret == ERR_NO_DATA) {
+ } else if (ret == E_NO_DATA) {
/* No client */
- sensorLogger->stop();
- return ERR_NONE;
+ getLogger()->stop();
+ return E_NONE;
}
- return ERR_OPERATION_FAILED;
+ return E_FAILED;
}
void SensorProvider::removeClient(std::string subject, std::string pkgId)
#ifndef __CONTEXT_SENSOR_PROVIDER_H__
#define __CONTEXT_SENSOR_PROVIDER_H__
+#include <string>
#include <map>
-#include <ContextProvider.h>
-#include "ClientInfo.h"
+#include <vector>
+#include <Tuple.h>
+#include "RecorderClientInfo.h"
#include "SensorLogger.h"
#include "Querier.h"
namespace ctx {
- class SensorProvider : public ContextProvider {
+ class SensorRecorderService;
+
+ class SensorProvider {
public:
- SensorProvider(const char *subject);
virtual ~SensorProvider();
- virtual int subscribe(Json option, Json *requestResult);
- virtual int unsubscribe(Json option);
- virtual int read(Json option, Json *requestResult);
- virtual int write(Json data, Json *requestResult);
+ const char* getSubject();
+
+ int readRecords(Json option, std::string* projection, std::vector<std::shared_ptr<Tuple>>* tuples);
+ int startRecording(const std::string& pkgId, Json option);
+ int stopRecording(const std::string& pkgId);
+
+ virtual bool isSupported();
+ virtual const char* getPrivilege();
+
+ static void init();
+ static void release();
+ static SensorProvider* getInstance(const std::string& subject);
static void removeClient(std::string subject, std::string pkgId);
protected:
- virtual Querier* getQuerier(Json option) = 0;
- virtual bool verifyOption(Json option);
+ SensorProvider(const char *subject);
- SensorLogger *sensorLogger;
+ bool getSystemInfo(const char* key);
+
+ virtual SensorLogger* getLogger() = 0;
+ virtual Querier* getQuerier() = 0;
+ virtual bool verifyOption(Json option);
private:
int __addClient(std::string pkgId, int retentionPeriod, Json option);
int __removeClient(std::string pkgId);
- ClientInfo __clientInfo;
+ std::string __subject;
+ RecorderClientInfo __clientInfo;
static std::map<std::string, SensorProvider*> __providerMap;
};
*
*/
-#include <Types.h>
+#include <ContextTypes.h>
+#include "../SensorTimer.h"
#include "SensorProxy.h"
#define SENSOR_EVENT(X) (((int)(X) << 16) | 0x01)
using namespace ctx;
+struct SensorEvent {
+ SensorProxy* listener;
+ sensor_data_t* data;
+};
+
SensorProxy::SensorProxy() :
sensorHandle(-1),
sensorType(UNKNOWN_SENSOR),
void SensorProxy::__eventCb(sensor_t sensor, unsigned int eventType, sensor_data_t *eventData, void *cbData)
{
- SensorProxy *instance = static_cast<SensorProxy*>(cbData);
- instance->onEvent(eventData);
+ SensorEvent* sensorEvent = new SensorEvent;
+ sensorEvent->listener = static_cast<SensorProxy*>(cbData);
+
+ if (sensorEvent->listener->sensorType == HUMAN_PEDOMETER_SENSOR) {
+ sensorEvent->data = static_cast<sensor_data_t*>(g_memdup(eventData, sizeof(sensor_pedometer_data_t)));
+ } else {
+ sensorEvent->data = static_cast<sensor_data_t*>(g_memdup(eventData, sizeof(sensor_data_t)));
+ }
+
+ if (!sensorEvent->data) {
+ _E_ALLOC;
+ delete sensorEvent;
+ return;
+ }
+
+ SensorTimer::addIdle(__threadSwitcher, sensorEvent);
+}
+
+gboolean SensorProxy::__threadSwitcher(gpointer data)
+{
+ SensorEvent* sensorEvent = static_cast<SensorEvent*>(data);
+
+ sensorEvent->listener->onEvent(sensorEvent->data);
+
+ g_free(sensorEvent->data);
+ delete sensorEvent;
+
+ return G_SOURCE_REMOVE;
}
private:
static void __eventCb(sensor_t sensor, unsigned int eventType, sensor_data_t *eventData, void *cbData);
+ static gboolean __threadSwitcher(gpointer data);
};
}
#define HEART_RATE_RECORD "SensorHeartRateRecord"
/* Privileges */
-#define PRIV_HEALTHINFO "healthinfo"
+#define PRIV_HEALTHINFO "http://tizen.org/privilege/healthinfo"
/* Constants */
#define SECONDS_PER_MINUTE 60
/* SQL Helper */
#define TIME_QUANTIZER(key) "ROUND((CAST(" key " AS REAL) - %llu)/CAST(%llu AS REAL) + 0.5)"
+/* Sensor Subjects */
+#define SUBJ_SENSOR "sensor/"
+#define SUBJ_SENSOR_HEART_RATE SUBJ_SENSOR "heart_rate"
+#define SUBJ_SENSOR_PEDOMETER SUBJ_SENSOR "pedometer"
+#define SUBJ_SENSOR_SLEEP_MONITOR SUBJ_SENSOR "sleep_monitor"
+#define SUBJ_SENSOR_PRESSURE SUBJ_SENSOR "pressure"
+#define SUBJ_SENSOR_EXERCISE SUBJ_SENSOR "exercise"
+
+/* Keys */
+#define KEY_SUBJECT "Subject"
+#define KEY_OPERATION "Operation"
+#define KEY_OPTION "Option"
+
+#define KEY_CLIENT_PKG_ID "_ClientPkgId_" /* Special key for internal use */
+#define KEY_PKG_ID "PkgId"
+#define KEY_START_TIME "StartTime"
+#define KEY_END_TIME "EndTime"
+#define KEY_UNIV_TIME "UTC"
+#define KEY_LOCAL_TIME "LocalTime"
+#define KEY_STATE "State"
+
+#define KEY_RETENTION "Retention"
+#define KEY_INTERVAL "Interval"
+#define KEY_ANCHOR "Anchor"
+
+#define KEY_STEPS "Steps"
+#define KEY_WALK_STEPS "WalkSteps"
+#define KEY_RUN_STEPS "RunSteps"
+#define KEY_DISTANCE "Dist"
+#define KEY_CALORIES "Cal"
+#define KEY_PRESSURE "Pressure"
+#define KEY_AVG_PRESSURE "AvgPressure"
+#define KEY_MIN_PRESSURE "MinPressure"
+#define KEY_MAX_PRESSURE "MaxPressure"
+#define KEY_SLEEP_STATE KEY_STATE
+#define KEY_HEART_RATE "HeartRate"
+
+/* Values */
+#define VAL_START "Start"
+#define VAL_STOP "Stop"
+
#endif /* __CONTEXT_SENSOR_RECORDER_TYPES_INTERNAL_H__ */
* limitations under the License.
*/
-#include "ClientInfo.h"
+#include <SensorRecorderService.h>
+#include "RecorderClientInfo.h"
#include "UninstallMonitor.h"
using namespace ctx;
-UninstallMonitor::UninstallMonitor() :
- __dbusSignalId(-1),
- __dbusWatcher(DBusType::SYSTEM)
+UninstallMonitor::UninstallMonitor(SensorRecorderService* hostService) :
+ __dbusMonitor(hostService->getConnection())
{
- __dbusSignalId = __dbusWatcher.watch(NULL,
+ __dbusMonitor.subscribe(NULL,
"/org/tizen/pkgmgr/signal", "org.tizen.pkgmgr.signal", "uninstall", this);
}
UninstallMonitor::~UninstallMonitor()
{
- if (__dbusSignalId > 0)
- __dbusWatcher.unwatch(__dbusSignalId);
}
-void UninstallMonitor::onSignal(const char *sender, const char *path, const char *iface, const char *name, GVariant *param)
+void UninstallMonitor::onSignal(const std::string& sender, const std::string& objPath,
+ const std::string& interface, const std::string& signalName, GVariant *param)
{
const gchar *pkgId = NULL;
const gchar *key = NULL;
IF_FAIL_VOID(pkgId && STR_EQ(key, "end") && STR_EQ(val, "ok"));
_I("'%s' has been removed", pkgId);
- ClientInfo::purgeClient(pkgId);
+ RecorderClientInfo::purgeClient(pkgId);
}
#ifndef __CONTEXT_UNINSTALL_MONITOR_H__
#define __CONTEXT_UNINSTALL_MONITOR_H__
-#include <DBusSignalWatcher.h>
+#include <DBusMonitor.h>
+#include <IDBusSignalListener.h>
namespace ctx {
+ class SensorRecorderService;
+
class UninstallMonitor : public IDBusSignalListener {
public:
- UninstallMonitor();
+ UninstallMonitor(SensorRecorderService* hostService);
~UninstallMonitor();
- void onSignal(const char *sender, const char *path, const char *iface, const char *name, GVariant *param);
+ void onSignal(const std::string& sender, const std::string& objPath,
+ const std::string& interface, const std::string& signalName, GVariant *param);
private:
- int64_t __dbusSignalId;
- DBusSignalWatcher __dbusWatcher;
+ DBusMonitor __dbusMonitor;
};
}
* limitations under the License.
*/
-#include <SensorRecorderTypes.h>
-#include <Util.h>
+#include <SensorRecorderTypesPrivate.h>
#include "../TypesInternal.h"
-#include "HeartRateLogger.h"
-#include "HeartRateQuerier.h"
#include "HeartRate.h"
using namespace ctx;
HeartRateProvider::HeartRateProvider() :
SensorProvider(SUBJ_SENSOR_HEART_RATE)
{
- IF_FAIL_VOID(isSupported());
-
- sensorLogger = new(std::nothrow) HeartRateLogger();
- IF_FAIL_VOID_TAG(sensorLogger, _E, "Memory allocation failed");
}
HeartRateProvider::~HeartRateProvider()
{
}
-void HeartRateProvider::getPrivilege(std::vector<const char*> &privilege)
+const char* HeartRateProvider::getPrivilege()
{
- privilege.push_back(PRIV_HEALTHINFO);
+ return PRIV_HEALTHINFO;
}
bool HeartRateProvider::isSupported()
{
- return util::getSystemInfoBool("tizen.org/feature/sensor.heart_rate_monitor");
+ return getSystemInfo("tizen.org/feature/sensor.heart_rate_monitor");
+}
+
+SensorLogger* HeartRateProvider::getLogger()
+{
+ return &__logger;
}
-Querier* HeartRateProvider::getQuerier(Json option)
+Querier* HeartRateProvider::getQuerier()
{
- HeartRateQuerier *querier = new(std::nothrow) HeartRateQuerier(this, option);
- IF_FAIL_RETURN_TAG(querier, NULL, _E, "Memory allocation failed");
- return querier;
+ return &__querier;
}
bool HeartRateProvider::verifyOption(Json option)
#define __CONTEXT_HEARTRATE_PROVIDER_H__
#include "../SensorProvider.h"
+#include "HeartRateLogger.h"
+#include "HeartRateQuerier.h"
namespace ctx {
~HeartRateProvider();
bool isSupported();
- void getPrivilege(std::vector<const char*> &privilege);
+ const char* getPrivilege();
protected:
- Querier* getQuerier(Json option);
+ SensorLogger* getLogger();
+ Querier* getQuerier();
bool verifyOption(Json option);
+
+ private:
+ HeartRateLogger __logger;
+ HeartRateQuerier __querier;
};
}
#include <climits>
#include <sqlite3.h>
-#include <SensorRecorderTypes.h>
+#include <SensorRecorderTypesPrivate.h>
+#include "../../SensorTimer.h"
#include "../TypesInternal.h"
-#include "../ClientInfo.h"
+#include "../RecorderClientInfo.h"
#include "../TimeUtil.h"
#include "HeartRateLogger.h"
using namespace ctx;
HeartRateLogger::HeartRateLogger() :
- __timerMgr(NULL),
- __timerId(-1),
+ __timerId(0),
__timerInterval(INT_MAX),
__expiredTime(0)
{
KEY_HEART_RATE " REAL NOT NULL" \
")");
- ClientInfo clientInfo;
+ RecorderClientInfo clientInfo;
if (clientInfo.exist(SUBJ_SENSOR_HEART_RATE))
start();
}
bool HeartRateLogger::start()
{
std::vector<Json> options;
- ClientInfo clientInfo;
+ RecorderClientInfo clientInfo;
float interval = MAX_MEASURING_INTERVAL;
- if (clientInfo.get(SUBJ_SENSOR_HEART_RATE, options) != ERR_NONE)
+ if (clientInfo.get(SUBJ_SENSOR_HEART_RATE, options) != E_NONE)
return false;
clientInfo.getParam(options, KEY_INTERVAL, &interval, NULL);
- if (!__timerMgr) {
- __timerMgr = new(std::nothrow) TimerManager;
- IF_FAIL_RETURN_TAG(__timerMgr, false, _E, "Memory allocation failed");
- }
-
- if (interval == __timerInterval)
+ if (interval == static_cast<float>(__timerInterval))
return true;
- __timerInterval = interval;
+ __timerInterval = static_cast<unsigned int>(interval);
- _I(GREEN("Start to record (at every %d minutes)"), __timerInterval);
+ _I(GREEN("Start to record (at every %u minutes)"), __timerInterval);
- if (__timerId > 0)
- __timerMgr->remove(__timerId);
+ if (__timerId != 0)
+ SensorTimer::cancel(__timerId);
- __timerId = __timerMgr->setFor(__timerInterval, this);
+ __timerId = SensorTimer::addAlarm(__timerInterval, this);
- if (__timerId < 0) {
+ if (__timerId == 0) {
_E("Setting timer failed");
__timerInterval = INT_MAX;
return false;
{
_I(GREEN("Stop recording"));
- if (__timerMgr)
- delete __timerMgr;
-
- __timerMgr = NULL;
- __timerId = -1;
+ __timerId = 0;
__timerInterval = INT_MAX;
unlisten();
{
}
-bool HeartRateLogger::onTimerExpired(int timerId)
+bool HeartRateLogger::onTimerExpired(unsigned int timerId, unsigned int intervalMs, void* data)
{
IF_FAIL_RETURN(!isRunning(), true);
#ifndef __CONTEXT_HEARTRATE_LOGGER_H__
#define __CONTEXT_HEARTRATE_LOGGER_H__
-#include <TimerManager.h>
+#include <ITimerListener.h>
#include "../SensorLogger.h"
#include "../SensorProxy.h"
namespace ctx {
+ class SensorRecorderService;
+
class HeartRateLogger : public SensorLogger, public SensorProxy, public ITimerListener {
public:
HeartRateLogger();
void flushCache(bool force = false);
protected:
- bool onTimerExpired(int timerId);
+ bool onTimerExpired(unsigned int timerId, unsigned int intervalMs, void* data);
void onEvent(sensor_data_t *eventData);
private:
void __record(float heartrate, uint64_t eventTime);
- TimerManager *__timerMgr;
- int __timerId;
- int __timerInterval;
+ unsigned int __timerId;
+ unsigned int __timerInterval;
uint64_t __expiredTime;
};
}
*/
#include <sqlite3.h>
-#include <SensorRecorderTypes.h>
+#include <SensorRecorderTypesPrivate.h>
#include "../TypesInternal.h"
#include "HeartRateQuerier.h"
using namespace ctx;
-HeartRateQuerier::HeartRateQuerier(ContextProvider *provider, Json option) :
- Querier(provider, option)
+HeartRateQuerier::HeartRateQuerier()
{
}
{
}
-int HeartRateQuerier::queryRaw(int startTime, int endTime)
+std::string HeartRateQuerier::getProjection()
{
- return query(startTime, endTime);
+ return KEY_HEART_RATE "," KEY_START_TIME "," KEY_END_TIME;
}
-int HeartRateQuerier::query(int startTime, int endTime)
+int HeartRateQuerier::queryRaw(int startTime, int endTime, std::vector<std::shared_ptr<Tuple>>* tuples)
+{
+ return query(startTime, endTime, tuples);
+}
+
+int HeartRateQuerier::query(int startTime, int endTime, std::vector<std::shared_ptr<Tuple>>* tuples)
{
char *sql = sqlite3_mprintf(
"SELECT " PROJECTION \
" WHERE " KEY_UNIV_TIME " > %llu AND " KEY_UNIV_TIME " <= %llu",
SEC_TO_MS(static_cast<uint64_t>(startTime)), SEC_TO_MS(static_cast<uint64_t>(endTime)));
- int ret = Querier::query(sql);
+ int ret = Querier::query(sql, tuples);
sqlite3_free(sql);
return ret;
class HeartRateQuerier : public Querier {
public:
- HeartRateQuerier(ContextProvider *provider, Json option);
+ HeartRateQuerier();
~HeartRateQuerier();
- int queryRaw(int startTime, int endTime);
- int query(int startTime, int endTime);
+ std::string getProjection();
+
+ int queryRaw(int startTime, int endTime, std::vector<std::shared_ptr<Tuple>>* tuples);
+ int query(int startTime, int endTime, std::vector<std::shared_ptr<Tuple>>* tuples);
};
}
* limitations under the License.
*/
-#include <SensorRecorderTypes.h>
-#include <Util.h>
+#include <SensorRecorderTypesPrivate.h>
#include "../TypesInternal.h"
-#include "PedometerLogger.h"
-#include "PedometerQuerier.h"
#include "Pedometer.h"
using namespace ctx;
PedometerProvider::PedometerProvider() :
SensorProvider(SUBJ_SENSOR_PEDOMETER)
{
- IF_FAIL_VOID(isSupported());
-
- sensorLogger = new(std::nothrow) PedometerLogger();
- IF_FAIL_VOID_TAG(sensorLogger, _E, "Memory allocation failed");
}
PedometerProvider::~PedometerProvider()
{
}
-void PedometerProvider::getPrivilege(std::vector<const char*> &privilege)
+const char* PedometerProvider::getPrivilege()
{
- privilege.push_back(PRIV_HEALTHINFO);
+ return PRIV_HEALTHINFO;
}
bool PedometerProvider::isSupported()
{
- return util::getSystemInfoBool("tizen.org/feature/sensor.pedometer");
+ return getSystemInfo("tizen.org/feature/sensor.pedometer");
+}
+
+SensorLogger* PedometerProvider::getLogger()
+{
+ return &__logger;
}
-Querier* PedometerProvider::getQuerier(Json option)
+Querier* PedometerProvider::getQuerier()
{
- PedometerQuerier *querier = new(std::nothrow) PedometerQuerier(this, option);
- IF_FAIL_RETURN_TAG(querier, NULL, _E, "Memory allocation failed");
- return querier;
+ return &__querier;
}
#define __CONTEXT_PEDOMETER_PROVIDER_H__
#include "../SensorProvider.h"
+#include "PedometerLogger.h"
+#include "PedometerQuerier.h"
namespace ctx {
~PedometerProvider();
bool isSupported();
- void getPrivilege(std::vector<const char*> &privilege);
+ const char* getPrivilege();
protected:
- Querier* getQuerier(Json option);
+ SensorLogger* getLogger();
+ Querier* getQuerier();
+
+ private:
+ PedometerLogger __logger;
+ PedometerQuerier __querier;
};
}
#include <inttypes.h>
#include <sqlite3.h>
-#include <SensorRecorderTypes.h>
+#include <SensorRecorderTypesPrivate.h>
#include "../TypesInternal.h"
-#include "../ClientInfo.h"
+#include "../RecorderClientInfo.h"
#include "../TimeUtil.h"
#include "PedometerLogger.h"
KEY_CALORIES " REAL NOT NULL" \
")");
- ClientInfo clientInfo;
+ RecorderClientInfo clientInfo;
if (clientInfo.exist(SUBJ_SENSOR_PEDOMETER))
start();
}
unsigned int runSteps;
float distance;
float calories;
+ DataRecord() : timestamp(0), walkSteps(0), runSteps(0), distance(0), calories(0) {}
};
void __recordSingle(sensor_pedometer_data_t *eventData, uint64_t timestamp);
*/
#include <sqlite3.h>
-#include <SensorRecorderTypes.h>
+#include <SensorRecorderTypesPrivate.h>
#include "../TypesInternal.h"
#include "PedometerQuerier.h"
using namespace ctx;
-PedometerQuerier::PedometerQuerier(ContextProvider *provider, Json option) :
- Querier(provider, option)
+PedometerQuerier::PedometerQuerier()
{
}
{
}
-int PedometerQuerier::queryRaw(int startTime, int endTime)
+std::string PedometerQuerier::getProjection()
+{
+ return \
+ KEY_STEPS "," \
+ KEY_WALK_STEPS "," \
+ KEY_RUN_STEPS "," \
+ KEY_DISTANCE "," \
+ KEY_CALORIES "," \
+ KEY_START_TIME "," \
+ KEY_END_TIME;
+}
+
+int PedometerQuerier::queryRaw(int startTime, int endTime, std::vector<std::shared_ptr<Tuple>>* tuples)
{
char *sql = sqlite3_mprintf(
"SELECT " PROJECTION_RAW \
" ORDER BY " KEY_END_TIME " ASC",
SEC_TO_MS(static_cast<uint64_t>(startTime)), SEC_TO_MS(static_cast<uint64_t>(endTime)));
- int ret = Querier::query(sql);
+ int ret = Querier::query(sql, tuples);
sqlite3_free(sql);
return ret;
}
-int PedometerQuerier::query(int startTime, int endTime)
+int PedometerQuerier::query(int startTime, int endTime, std::vector<std::shared_ptr<Tuple>>* tuples)
{
char *sql = sqlite3_mprintf(
"SELECT " PROJECTION \
" WHERE " KEY_END_TIME " > %llu AND " KEY_END_TIME " <= %llu",
SEC_TO_MS(static_cast<uint64_t>(startTime)), SEC_TO_MS(static_cast<uint64_t>(endTime)));
- int ret = Querier::query(sql);
+ int ret = Querier::query(sql, tuples);
sqlite3_free(sql);
return ret;
}
-int PedometerQuerier::query(int startTime, int endTime, int anchor, int interval)
+int PedometerQuerier::query(int startTime, int endTime, int anchor, int interval, std::vector<std::shared_ptr<Tuple>>* tuples)
{
char *sql = sqlite3_mprintf(
"SELECT " PROJECTION \
SEC_TO_MS(static_cast<uint64_t>(startTime)), SEC_TO_MS(static_cast<uint64_t>(endTime)),
SEC_TO_MS(static_cast<uint64_t>(anchor)), SEC_TO_MS(static_cast<uint64_t>(interval)));
- int ret = Querier::query(sql);
+ int ret = Querier::query(sql, tuples);
sqlite3_free(sql);
return ret;
class PedometerQuerier : public Querier {
public:
- PedometerQuerier(ContextProvider *provider, Json option);
+ PedometerQuerier();
~PedometerQuerier();
- int queryRaw(int startTime, int endTime);
- int query(int startTime, int endTime);
- int query(int startTime, int endTime, int anchor, int interval);
+ std::string getProjection();
+
+ int queryRaw(int startTime, int endTime, std::vector<std::shared_ptr<Tuple>>* tuples);
+ int query(int startTime, int endTime, std::vector<std::shared_ptr<Tuple>>* tuples);
+ int query(int startTime, int endTime, int anchor, int interval, std::vector<std::shared_ptr<Tuple>>* tuples);
};
}
* limitations under the License.
*/
-#include <SensorRecorderTypes.h>
-#include <Util.h>
+#include <SensorRecorderTypesPrivate.h>
#include "../TypesInternal.h"
-#include "PressureLogger.h"
-#include "PressureQuerier.h"
#include "Pressure.h"
using namespace ctx;
PressureProvider::PressureProvider() :
SensorProvider(SUBJ_SENSOR_PRESSURE)
{
- IF_FAIL_VOID(isSupported());
-
- sensorLogger = new(std::nothrow) PressureLogger();
- IF_FAIL_VOID_TAG(sensorLogger, _E, "Memory allocation failed");
}
PressureProvider::~PressureProvider()
bool PressureProvider::isSupported()
{
- return util::getSystemInfoBool("tizen.org/feature/sensor.barometer");
+ return getSystemInfo("tizen.org/feature/sensor.barometer");
+}
+
+SensorLogger* PressureProvider::getLogger()
+{
+ return &__logger;
}
-Querier* PressureProvider::getQuerier(Json option)
+Querier* PressureProvider::getQuerier()
{
- PressureQuerier *querier = new(std::nothrow) PressureQuerier(this, option);
- IF_FAIL_RETURN_TAG(querier, NULL, _E, "Memory allocation failed");
- return querier;
+ return &__querier;
}
#define __CONTEXT_PRESSURE_PROVIDER_H__
#include "../SensorProvider.h"
+#include "PressureLogger.h"
+#include "PressureQuerier.h"
namespace ctx {
bool isSupported();
protected:
- Querier* getQuerier(Json option);
+ SensorLogger* getLogger();
+ Querier* getQuerier();
+
+ private:
+ PressureLogger __logger;
+ PressureQuerier __querier;
};
}
#include <inttypes.h>
#include <sqlite3.h>
-#include <SensorRecorderTypes.h>
+#include <SensorRecorderTypesPrivate.h>
#include "../TypesInternal.h"
-#include "../ClientInfo.h"
+#include "../RecorderClientInfo.h"
#include "../TimeUtil.h"
#include "PressureLogger.h"
using namespace ctx;
-PressureLogger::PressureLogger()
+PressureLogger::PressureLogger() :
+ __lastEventTime(0),
+ __cacheCount(0)
{
setSensor(PRESSURE_SENSOR);
setPowerSave(false);
KEY_PRESSURE " REAL NOT NULL" \
")");
- ClientInfo clientInfo;
+ RecorderClientInfo clientInfo;
if (clientInfo.exist(SUBJ_SENSOR_PRESSURE))
start();
}
void PressureLogger::flushCache(bool force)
{
- IF_FAIL_VOID(force || __cacheCount > CACHE_LIMIT);
+ IF_FAIL_VOID((force && __cacheCount > 0) || __cacheCount > CACHE_LIMIT);
__insertionQuery.resize(__insertionQuery.size() - 1);
if (__insertionQuery.at(__insertionQuery.size() - 1) == ')')
*/
#include <sqlite3.h>
-#include <SensorRecorderTypes.h>
+#include <Tuple.h>
+#include <SensorRecorderTypesPrivate.h>
#include "../TypesInternal.h"
#include "PressureQuerier.h"
using namespace ctx;
-PressureQuerier::PressureQuerier(ContextProvider *provider, Json option) :
- Querier(provider, option)
+PressureQuerier::PressureQuerier()
{
}
{
}
-int PressureQuerier::queryRaw(int startTime, int endTime)
+std::string PressureQuerier::getProjection()
+{
+ return \
+ KEY_PRESSURE "," \
+ KEY_MIN_PRESSURE "," \
+ KEY_MAX_PRESSURE "," \
+ KEY_AVG_PRESSURE "," \
+ KEY_START_TIME "," \
+ KEY_END_TIME;
+}
+
+int PressureQuerier::queryRaw(int startTime, int endTime, std::vector<std::shared_ptr<Tuple>>* tuples)
{
char *sql = sqlite3_mprintf(
"SELECT " PROJECTION_RAW \
" WHERE " KEY_UNIV_TIME " > %llu AND " KEY_UNIV_TIME " <= %llu",
SEC_TO_MS(static_cast<uint64_t>(startTime)), SEC_TO_MS(static_cast<uint64_t>(endTime)));
- int ret = Querier::query(sql);
+ int ret = Querier::query(sql, tuples);
sqlite3_free(sql);
return ret;
}
-int PressureQuerier::query(int startTime, int endTime)
+int PressureQuerier::query(int startTime, int endTime, std::vector<std::shared_ptr<Tuple>>* tuples)
{
char *sql = sqlite3_mprintf(
"SELECT " PROJECTION \
" WHERE " KEY_UNIV_TIME " > %llu AND " KEY_UNIV_TIME " <= %llu",
SEC_TO_MS(static_cast<uint64_t>(startTime)), SEC_TO_MS(static_cast<uint64_t>(endTime)));
- int ret = Querier::query(sql);
+ int ret = Querier::query(sql, tuples);
sqlite3_free(sql);
return ret;
}
-int PressureQuerier::query(int startTime, int endTime, int anchor, int interval)
+int PressureQuerier::query(int startTime, int endTime, int anchor, int interval, std::vector<std::shared_ptr<Tuple>>* tuples)
{
char *sql = sqlite3_mprintf(
"SELECT " PROJECTION \
SEC_TO_MS(static_cast<uint64_t>(startTime)), SEC_TO_MS(static_cast<uint64_t>(endTime)),
SEC_TO_MS(static_cast<uint64_t>(anchor)), SEC_TO_MS(static_cast<uint64_t>(interval)));
- int ret = Querier::query(sql);
+ int ret = Querier::query(sql, tuples);
sqlite3_free(sql);
return ret;
class PressureQuerier : public Querier {
public:
- PressureQuerier(ContextProvider *provider, Json option);
+ PressureQuerier();
~PressureQuerier();
- int queryRaw(int startTime, int endTime);
- int query(int startTime, int endTime);
- int query(int startTime, int endTime, int anchor, int interval);
+ std::string getProjection();
+
+ int queryRaw(int startTime, int endTime, std::vector<std::shared_ptr<Tuple>>* tuples);
+ int query(int startTime, int endTime, std::vector<std::shared_ptr<Tuple>>* tuples);
+ int query(int startTime, int endTime, int anchor, int interval, std::vector<std::shared_ptr<Tuple>>* tuples);
};
}
* limitations under the License.
*/
-#include <SensorRecorderTypes.h>
-#include <Util.h>
+#include <SensorRecorderTypesPrivate.h>
#include "../TypesInternal.h"
-#include "SleepLogger.h"
-#include "SleepQuerier.h"
#include "Sleep.h"
using namespace ctx;
SleepProvider::SleepProvider() :
SensorProvider(SUBJ_SENSOR_SLEEP_MONITOR)
{
- IF_FAIL_VOID(isSupported());
-
- sensorLogger = new(std::nothrow) SleepLogger();
- IF_FAIL_VOID_TAG(sensorLogger, _E, "Memory allocation failed");
}
SleepProvider::~SleepProvider()
{
}
-void SleepProvider::getPrivilege(std::vector<const char*> &privilege)
+const char* SleepProvider::getPrivilege()
{
- privilege.push_back(PRIV_HEALTHINFO);
+ return PRIV_HEALTHINFO;
}
bool SleepProvider::isSupported()
{
- return util::getSystemInfoBool("tizen.org/feature/sensor.sleep_monitor");
+ return (getSystemInfo("tizen.org/feature/sensor.sleep_monitor")
+ && getSystemInfo("tizen.org/feature/sensor.sleep_detector"));
+}
+
+SensorLogger* SleepProvider::getLogger()
+{
+ return &__logger;
}
-Querier* SleepProvider::getQuerier(Json option)
+Querier* SleepProvider::getQuerier()
{
- SleepQuerier *querier = new(std::nothrow) SleepQuerier(this, option);
- IF_FAIL_RETURN_TAG(querier, NULL, _E, "Memory allocation failed");
- return querier;
+ return &__querier;
}
#define __CONTEXT_SLEEP_PROVIDER_H__
#include "../SensorProvider.h"
+#include "SleepLogger.h"
+#include "SleepQuerier.h"
namespace ctx {
SleepProvider();
~SleepProvider();
- void getPrivilege(std::vector<const char*> &privilege);
+ const char* getPrivilege();
bool isSupported();
protected:
- Querier* getQuerier(Json option);
+ SensorLogger* getLogger();
+ Querier* getQuerier();
+
+ private:
+ SleepLogger __logger;
+ SleepQuerier __querier;
};
}
* limitations under the License.
*/
-#include <Types.h>
+#include <ContextTypes.h>
#include "../TimeUtil.h"
#include "SleepDetector.h"
#include <inttypes.h>
#include <sqlite3.h>
-#include <SensorRecorderTypes.h>
+#include <SensorRecorderTypesPrivate.h>
#include "../TypesInternal.h"
-#include "../ClientInfo.h"
+#include "../RecorderClientInfo.h"
#include "../TimeUtil.h"
#include "SleepDetector.h"
#include "SleepMonitor.h"
__sleepDetector = NULL;
__sleepMonitor = NULL;
+ __resetInsertionQuery();
+
/* Create the log table */
executeQuery(
"CREATE TABLE IF NOT EXISTS " SLEEP_MONITOR_RECORD " (" \
KEY_STATE " INTEGER NOT NULL DEFAULT 1" \
")");
- ClientInfo clientInfo;
+ RecorderClientInfo clientInfo;
if (clientInfo.exist(SUBJ_SENSOR_SLEEP_MONITOR))
start();
}
#ifndef __CONTEXT_SLEEP_LOGGER_H__
#define __CONTEXT_SLEEP_LOGGER_H__
+#include <string>
#include "../SensorLogger.h"
#define STATE_SLEEP 1
* limitations under the License.
*/
-#include <Types.h>
+#include <ContextTypes.h>
#include "../TypesInternal.h"
#include "../TimeUtil.h"
#include "SleepMonitor.h"
*/
#include <sqlite3.h>
-#include <SensorRecorderTypes.h>
+#include <Tuple.h>
+#include <SensorRecorderTypesPrivate.h>
#include "../TypesInternal.h"
#include "SleepQuerier.h"
using namespace ctx;
-SleepQuerier::SleepQuerier(ContextProvider *provider, Json option) :
- Querier(provider, option)
+SleepQuerier::SleepQuerier()
{
}
{
}
-int SleepQuerier::queryRaw(int startTime, int endTime)
+std::string SleepQuerier::getProjection()
{
- return query(startTime, endTime);
+ return KEY_STATE "," KEY_START_TIME "," KEY_END_TIME;
}
-int SleepQuerier::query(int startTime, int endTime)
+int SleepQuerier::queryRaw(int startTime, int endTime, std::vector<std::shared_ptr<Tuple>>* tuples)
+{
+ return query(startTime, endTime, tuples);
+}
+
+int SleepQuerier::query(int startTime, int endTime, std::vector<std::shared_ptr<Tuple>>* tuples)
{
char *sql = sqlite3_mprintf(
"SELECT " PROJECTION \
" WHERE " KEY_END_TIME " > %llu AND " KEY_START_TIME " <= %llu",
SEC_TO_MS(static_cast<uint64_t>(startTime)), SEC_TO_MS(static_cast<uint64_t>(endTime)));
- int ret = Querier::query(sql);
+ int ret = Querier::query(sql, tuples);
sqlite3_free(sql);
return ret;
class SleepQuerier : public Querier {
public:
- SleepQuerier(ContextProvider *provider, Json option);
+ SleepQuerier();
~SleepQuerier();
- int queryRaw(int startTime, int endTime);
- int query(int startTime, int endTime);
+ std::string getProjection();
+
+ int queryRaw(int startTime, int endTime, std::vector<std::shared_ptr<Tuple>>* tuples);
+ int query(int startTime, int endTime, std::vector<std::shared_ptr<Tuple>>* tuples);
};
}
#define CTX_SENSOR_RECORDER "SensorRecorder"
#define CTX_SENSOR_RECORDER_SPEC \
- "<method name='test'>" \
- " <arg type='i' name='attr' direction='in'/>" \
- " <arg type='i' name='return' direction='out'/>" \
+ "<method name='" METHOD_IS_SUPPORTED "'>" \
+ " <arg type='s' name='sensor' direction='in'/>" \
+ "</method>" \
+ "<method name='" METHOD_START_REC "'>" \
+ " <arg type='s' name='sensor' direction='in'/>" \
+ " <arg type='s' name='options' direction='in'/>" \
+ "</method>" \
+ "<method name='" METHOD_STOP_REC "'>" \
+ " <arg type='s' name='sensor' direction='in'/>" \
+ "</method>" \
+ "<method name='" METHOD_READ_REC "'>" \
+ " <arg type='s' name='sensor' direction='in'/>" \
+ " <arg type='s' name='query' direction='in'/>" \
+ " <arg type='s' name='keys' direction='out'/>" \
+ " <arg type='v' name='records' direction='out'/>" \
"</method>"
-#ifndef STR_EQ
-#define STR_EQ(X, Y) (g_strcmp0((X), (Y)) == 0)
-#endif
+#define METHOD_IS_SUPPORTED "IsSupported"
+#define METHOD_START_REC "StartRecording"
+#define METHOD_STOP_REC "StopRecording"
+#define METHOD_READ_REC "ReadRecords"
+
+#define BASE_PATH "sensor-recorder"
#endif