Integrate Tizen 3.0 implementation to the current architecture 77/130077/22
authorMu-Woong Lee <muwoong.lee@samsung.com>
Fri, 19 May 2017 05:04:33 +0000 (14:04 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Fri, 19 May 2017 13:37:25 +0000 (22:37 +0900)
Change-Id: I047e83efa083994d2a004e7b8e9a76ca6d7fbb16
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
52 files changed:
packaging/context-sensor-recorder.spec
src/client/QueryResultListener.cpp [new file with mode: 0644]
src/client/QueryResultListener.h [new file with mode: 0644]
src/client/sensor_recorder.cpp
src/server/CMakeLists.txt
src/server/SensorDatabase.cpp [new file with mode: 0644]
src/server/SensorDatabase.h [new file with mode: 0644]
src/server/SensorRecorderClient.cpp
src/server/SensorRecorderClient.h
src/server/SensorRecorderService.cpp
src/server/SensorTimer.cpp [new file with mode: 0644]
src/server/SensorTimer.h [new file with mode: 0644]
src/server/legacy/CreateProvider.cpp [deleted file]
src/server/legacy/Querier.cpp
src/server/legacy/Querier.h
src/server/legacy/RecorderClientInfo.cpp [moved from src/server/legacy/ClientInfo.cpp with 54% similarity]
src/server/legacy/RecorderClientInfo.h [moved from src/server/legacy/ClientInfo.h with 78% similarity]
src/server/legacy/SensorLogger.cpp
src/server/legacy/SensorLogger.h
src/server/legacy/SensorProvider.cpp
src/server/legacy/SensorProvider.h
src/server/legacy/SensorProxy.cpp
src/server/legacy/SensorProxy.h
src/server/legacy/TypesInternal.h
src/server/legacy/UninstallMonitor.cpp
src/server/legacy/UninstallMonitor.h
src/server/legacy/heartrate/HeartRate.cpp
src/server/legacy/heartrate/HeartRate.h
src/server/legacy/heartrate/HeartRateLogger.cpp
src/server/legacy/heartrate/HeartRateLogger.h
src/server/legacy/heartrate/HeartRateQuerier.cpp
src/server/legacy/heartrate/HeartRateQuerier.h
src/server/legacy/pedometer/Pedometer.cpp
src/server/legacy/pedometer/Pedometer.h
src/server/legacy/pedometer/PedometerLogger.cpp
src/server/legacy/pedometer/PedometerLogger.h
src/server/legacy/pedometer/PedometerQuerier.cpp
src/server/legacy/pedometer/PedometerQuerier.h
src/server/legacy/pressure/Pressure.cpp
src/server/legacy/pressure/Pressure.h
src/server/legacy/pressure/PressureLogger.cpp
src/server/legacy/pressure/PressureQuerier.cpp
src/server/legacy/pressure/PressureQuerier.h
src/server/legacy/sleep/Sleep.cpp
src/server/legacy/sleep/Sleep.h
src/server/legacy/sleep/SleepDetector.cpp
src/server/legacy/sleep/SleepLogger.cpp
src/server/legacy/sleep/SleepLogger.h
src/server/legacy/sleep/SleepMonitor.cpp
src/server/legacy/sleep/SleepQuerier.cpp
src/server/legacy/sleep/SleepQuerier.h
src/shared/SensorRecorderTypesPrivate.h

index 223ebe8..3babfc1 100644 (file)
@@ -10,7 +10,9 @@ BuildRequires: cmake
 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)
 
diff --git a/src/client/QueryResultListener.cpp b/src/client/QueryResultListener.cpp
new file mode 100644 (file)
index 0000000..501856a
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * 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;
+}
diff --git a/src/client/QueryResultListener.h b/src/client/QueryResultListener.h
new file mode 100644 (file)
index 0000000..dd3a51f
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * 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
index 8b46ad5..f57fae3 100644 (file)
  * 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)
@@ -62,10 +101,12 @@ EXPORT_API int ctx_sensor_rec_destroy_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;
 }
 
@@ -90,52 +131,115 @@ EXPORT_API int ctx_sensor_rec_destroy_query(ctx_sensor_rec_query_h query)
 
 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;
 }
index df2de9c..ee007d8 100644 (file)
@@ -1,8 +1,8 @@
 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)
diff --git a/src/server/SensorDatabase.cpp b/src/server/SensorDatabase.cpp
new file mode 100644 (file)
index 0000000..b08ce19
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ * 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);
+}
diff --git a/src/server/SensorDatabase.h b/src/server/SensorDatabase.h
new file mode 100644 (file)
index 0000000..a61375c
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * 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__ */
index b61d0b9..87eed76 100644 (file)
 
 #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;
 
@@ -31,9 +36,98 @@ SensorRecorderClient::~SensorRecorderClient()
 
 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)));
+}
index 210d57f..45cc9fe 100644 (file)
@@ -21,6 +21,8 @@
 
 namespace ctx {
 
+       class SensorProvider;
+
        class SensorRecorderClient : public ClientBase {
        public:
                SensorRecorderClient(ServiceBase* hostService, const std::string& busName);
@@ -28,6 +30,13 @@ namespace ctx {
 
                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);
        };
 
 }
index bc68956..b9ddf11 100644 (file)
 #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;
 
@@ -36,15 +40,25 @@ bool SensorRecorderService::isUserService()
 
 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);
 }
diff --git a/src/server/SensorTimer.cpp b/src/server/SensorTimer.cpp
new file mode 100644 (file)
index 0000000..62f7da9
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * 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);
+}
diff --git a/src/server/SensorTimer.h b/src/server/SensorTimer.h
new file mode 100644 (file)
index 0000000..ba79daa
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * 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__ */
diff --git a/src/server/legacy/CreateProvider.cpp b/src/server/legacy/CreateProvider.cpp
deleted file mode 100644 (file)
index 73e9f19..0000000
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * 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;
-}
index feec0b3..37f16e8 100644 (file)
  * 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()
 {
 }
 
@@ -30,56 +28,29 @@ 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);
 }
index 0eed203..354681d 100644 (file)
 #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);
        };
 }
 
similarity index 54%
rename from src/server/legacy/ClientInfo.cpp
rename to src/server/legacy/RecorderClientInfo.cpp
index 6c5955d..0ab8619 100644 (file)
  */
 
 #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," \
@@ -50,121 +46,110 @@ ClientInfo::ClientInfo()
        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;
 
@@ -178,26 +163,28 @@ void ClientInfo::getParam(std::vector<Json> &options, const char *key, float *mi
        }
 }
 
-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;
+}
similarity index 78%
rename from src/server/legacy/ClientInfo.h
rename to src/server/legacy/RecorderClientInfo.h
index e3ee643..9a8bbc9 100644 (file)
  * 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);
@@ -40,12 +42,13 @@ namespace ctx {
                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
index 6bc92ea..c2d513d 100644 (file)
@@ -15,7 +15,8 @@
  */
 
 #include <sqlite3.h>
-#include <SensorRecorderTypes.h>
+#include <SensorRecorderTypesPrivate.h>
+#include "../SensorDatabase.h"
 #include "TypesInternal.h"
 #include "TimeUtil.h"
 #include "SensorLogger.h"
@@ -37,7 +38,7 @@ void SensorLogger::flushCache(bool force)
 
 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)
@@ -53,7 +54,7 @@ void SensorLogger::removeExpired(const char *subject, const char *tableName, con
                        " WHERE " KEY_SUBJECT "='%s')",
                        tableName, timeKey, timestamp, subject);
 
-       __dbMgr.execute(0, query, NULL);
+       SensorDatabase::execute(query, NULL);
        sqlite3_free(query);
 
        __lastRemovalTime = timestamp;
index 76d7ec6..879cd62 100644 (file)
@@ -17,8 +17,6 @@
 #ifndef __CONTEXT_SENSOR_LOGGER_H__
 #define __CONTEXT_SENSOR_LOGGER_H__
 
-#include <DatabaseManager.h>
-
 namespace ctx {
 
        class SensorLogger {
@@ -37,7 +35,6 @@ namespace ctx {
 
        private:
                uint64_t __lastRemovalTime;
-               DatabaseManager __dbMgr;
        };
 }
 
index 47d5269..2103376 100644 (file)
 
 #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;
@@ -55,19 +106,19 @@ int SensorProvider::read(Json option, Json *requestResult)
        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;
@@ -75,54 +126,43 @@ int SensorProvider::read(Json option, Json *requestResult)
        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)
@@ -138,21 +178,21 @@ int SensorProvider::__addClient(std::string pkgId, int retentionPeriod, Json opt
        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)
@@ -161,24 +201,24 @@ 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)
index 5b806a0..245ec31 100644 (file)
 #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;
        };
index 98dd0a6..53afe65 100644 (file)
  *
  */
 
-#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),
@@ -119,6 +125,32 @@ bool SensorProxy::isSupported(sensor_type_t type)
 
 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;
 }
index 7ae1c87..e2e4af5 100644 (file)
@@ -52,6 +52,7 @@ namespace ctx {
 
        private:
                static void __eventCb(sensor_t sensor, unsigned int eventType, sensor_data_t *eventData, void *cbData);
+               static gboolean __threadSwitcher(gpointer data);
        };
 
 }
index 0bd3cb5..5a8cbaf 100644 (file)
@@ -26,7 +26,7 @@
 #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__ */
index 026ea10..6b359ed 100644 (file)
  * 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;
@@ -45,5 +44,5 @@ void UninstallMonitor::onSignal(const char *sender, const char *path, const char
        IF_FAIL_VOID(pkgId && STR_EQ(key, "end") && STR_EQ(val, "ok"));
 
        _I("'%s' has been removed", pkgId);
-       ClientInfo::purgeClient(pkgId);
+       RecorderClientInfo::purgeClient(pkgId);
 }
index baa3659..f014fa3 100644 (file)
 #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;
        };
 
 }
index 3afa98e..c13b63f 100644 (file)
  * 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;
@@ -26,31 +23,30 @@ 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)
index 71f0812..03d1fb6 100644 (file)
@@ -18,6 +18,8 @@
 #define __CONTEXT_HEARTRATE_PROVIDER_H__
 
 #include "../SensorProvider.h"
+#include "HeartRateLogger.h"
+#include "HeartRateQuerier.h"
 
 namespace ctx {
 
@@ -27,11 +29,16 @@ 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;
        };
 }
 
index 8ef6895..c9bdc6e 100644 (file)
 
 #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"
 
@@ -30,8 +31,7 @@
 using namespace ctx;
 
 HeartRateLogger::HeartRateLogger() :
-       __timerMgr(NULL),
-       __timerId(-1),
+       __timerId(0),
        __timerInterval(INT_MAX),
        __expiredTime(0)
 {
@@ -46,7 +46,7 @@ HeartRateLogger::HeartRateLogger() :
                                KEY_HEART_RATE " REAL NOT NULL" \
                        ")");
 
-       ClientInfo clientInfo;
+       RecorderClientInfo clientInfo;
        if (clientInfo.exist(SUBJ_SENSOR_HEART_RATE))
                start();
 }
@@ -59,32 +59,27 @@ HeartRateLogger::~HeartRateLogger()
 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;
@@ -97,11 +92,7 @@ void HeartRateLogger::stop()
 {
        _I(GREEN("Stop recording"));
 
-       if (__timerMgr)
-               delete __timerMgr;
-
-       __timerMgr = NULL;
-       __timerId = -1;
+       __timerId = 0;
        __timerInterval = INT_MAX;
 
        unlisten();
@@ -111,7 +102,7 @@ void HeartRateLogger::flushCache(bool force)
 {
 }
 
-bool HeartRateLogger::onTimerExpired(int timerId)
+bool HeartRateLogger::onTimerExpired(unsigned int timerId, unsigned int intervalMs, void* data)
 {
        IF_FAIL_RETURN(!isRunning(), true);
 
index fa12aa6..c1b0fb7 100644 (file)
@@ -17,7 +17,7 @@
 #ifndef __CONTEXT_HEARTRATE_LOGGER_H__
 #define __CONTEXT_HEARTRATE_LOGGER_H__
 
-#include <TimerManager.h>
+#include <ITimerListener.h>
 #include "../SensorLogger.h"
 #include "../SensorProxy.h"
 
@@ -26,6 +26,8 @@
 
 namespace ctx {
 
+       class SensorRecorderService;
+
        class HeartRateLogger : public SensorLogger, public SensorProxy, public ITimerListener {
        public:
                HeartRateLogger();
@@ -36,15 +38,14 @@ namespace ctx {
                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;
        };
 }
index 1bae5a6..2bf1fba 100644 (file)
@@ -15,7 +15,7 @@
  */
 
 #include <sqlite3.h>
-#include <SensorRecorderTypes.h>
+#include <SensorRecorderTypesPrivate.h>
 #include "../TypesInternal.h"
 #include "HeartRateQuerier.h"
 
@@ -26,8 +26,7 @@
 
 using namespace ctx;
 
-HeartRateQuerier::HeartRateQuerier(ContextProvider *provider, Json option) :
-       Querier(provider, option)
+HeartRateQuerier::HeartRateQuerier()
 {
 }
 
@@ -35,12 +34,17 @@ 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 \
@@ -48,7 +52,7 @@ int HeartRateQuerier::query(int startTime, int endTime)
                        " 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;
index 335d902..9ca670e 100644 (file)
@@ -23,11 +23,13 @@ namespace ctx {
 
        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);
        };
 }
 
index 8752754..7c45c07 100644 (file)
  * 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;
@@ -26,29 +23,28 @@ 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;
 }
index 7fee01f..6df0f2a 100644 (file)
@@ -18,6 +18,8 @@
 #define __CONTEXT_PEDOMETER_PROVIDER_H__
 
 #include "../SensorProvider.h"
+#include "PedometerLogger.h"
+#include "PedometerQuerier.h"
 
 namespace ctx {
 
@@ -27,10 +29,15 @@ 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;
        };
 }
 
index cc44856..90918c7 100644 (file)
@@ -16,9 +16,9 @@
 
 #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"
 
@@ -43,7 +43,7 @@ PedometerLogger::PedometerLogger() :
                                KEY_CALORIES " REAL NOT NULL" \
                        ")");
 
-       ClientInfo clientInfo;
+       RecorderClientInfo clientInfo;
        if (clientInfo.exist(SUBJ_SENSOR_PEDOMETER))
                start();
 }
index f4b32f9..b7d118e 100644 (file)
@@ -41,6 +41,7 @@ namespace ctx {
                        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);
index 3abf121..648c5d0 100644 (file)
@@ -15,7 +15,7 @@
  */
 
 #include <sqlite3.h>
-#include <SensorRecorderTypes.h>
+#include <SensorRecorderTypesPrivate.h>
 #include "../TypesInternal.h"
 #include "PedometerQuerier.h"
 
@@ -39,8 +39,7 @@
 
 using namespace ctx;
 
-PedometerQuerier::PedometerQuerier(ContextProvider *provider, Json option) :
-       Querier(provider, option)
+PedometerQuerier::PedometerQuerier()
 {
 }
 
@@ -48,7 +47,19 @@ 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 \
@@ -57,13 +68,13 @@ int PedometerQuerier::queryRaw(int startTime, int endTime)
                        " 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 \
@@ -71,13 +82,13 @@ int PedometerQuerier::query(int startTime, int endTime)
                        " 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 \
@@ -88,7 +99,7 @@ int PedometerQuerier::query(int startTime, int endTime, int anchor, int interval
                        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;
index 21af1e1..057d0d4 100644 (file)
@@ -23,12 +23,14 @@ namespace ctx {
 
        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);
        };
 }
 
index 76b60f4..96fcc41 100644 (file)
  * 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;
@@ -26,10 +23,6 @@ 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()
@@ -38,12 +31,15 @@ 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;
 }
index 419cfba..767bff7 100644 (file)
@@ -18,6 +18,8 @@
 #define __CONTEXT_PRESSURE_PROVIDER_H__
 
 #include "../SensorProvider.h"
+#include "PressureLogger.h"
+#include "PressureQuerier.h"
 
 namespace ctx {
 
@@ -29,7 +31,12 @@ namespace ctx {
                bool isSupported();
 
        protected:
-               Querier* getQuerier(Json option);
+               SensorLogger* getLogger();
+               Querier* getQuerier();
+
+       private:
+               PressureLogger __logger;
+               PressureQuerier __querier;
        };
 }
 
index 0bb1fe4..14d5b53 100644 (file)
@@ -16,9 +16,9 @@
 
 #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"
 
@@ -30,7 +30,9 @@
 
 using namespace ctx;
 
-PressureLogger::PressureLogger()
+PressureLogger::PressureLogger() :
+       __lastEventTime(0),
+       __cacheCount(0)
 {
        setSensor(PRESSURE_SENSOR);
        setPowerSave(false);
@@ -44,7 +46,7 @@ PressureLogger::PressureLogger()
                                KEY_PRESSURE " REAL NOT NULL" \
                        ")");
 
-       ClientInfo clientInfo;
+       RecorderClientInfo clientInfo;
        if (clientInfo.exist(SUBJ_SENSOR_PRESSURE))
                start();
 }
@@ -77,7 +79,7 @@ void PressureLogger::stop()
 
 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) == ')')
index 8d19638..00f67bb 100644 (file)
@@ -15,7 +15,8 @@
  */
 
 #include <sqlite3.h>
-#include <SensorRecorderTypes.h>
+#include <Tuple.h>
+#include <SensorRecorderTypesPrivate.h>
 #include "../TypesInternal.h"
 #include "PressureQuerier.h"
 
@@ -37,8 +38,7 @@
 
 using namespace ctx;
 
-PressureQuerier::PressureQuerier(ContextProvider *provider, Json option) :
-       Querier(provider, option)
+PressureQuerier::PressureQuerier()
 {
 }
 
@@ -46,7 +46,18 @@ 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 \
@@ -54,13 +65,13 @@ int PressureQuerier::queryRaw(int startTime, int endTime)
                        " 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 \
@@ -68,13 +79,13 @@ int PressureQuerier::query(int startTime, int endTime)
                        " 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 \
@@ -85,7 +96,7 @@ int PressureQuerier::query(int startTime, int endTime, int anchor, int interval)
                        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;
index 1bd9aa6..f6380e6 100644 (file)
@@ -23,12 +23,14 @@ namespace ctx {
 
        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);
        };
 }
 
index 3ae40c9..25ec3f1 100644 (file)
  * 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;
@@ -26,29 +23,29 @@ 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;
 }
index c93310e..bf90e59 100644 (file)
@@ -18,6 +18,8 @@
 #define __CONTEXT_SLEEP_PROVIDER_H__
 
 #include "../SensorProvider.h"
+#include "SleepLogger.h"
+#include "SleepQuerier.h"
 
 namespace ctx {
 
@@ -26,11 +28,16 @@ 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;
        };
 }
 
index 5367dac..7d433a9 100644 (file)
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include <Types.h>
+#include <ContextTypes.h>
 #include "../TimeUtil.h"
 #include "SleepDetector.h"
 
index 171df5b..b9e5723 100644 (file)
@@ -16,9 +16,9 @@
 
 #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"
@@ -38,6 +38,8 @@ SleepLogger::SleepLogger() :
        __sleepDetector = NULL;
        __sleepMonitor = NULL;
 
+       __resetInsertionQuery();
+
        /* Create the log table */
        executeQuery(
                        "CREATE TABLE IF NOT EXISTS " SLEEP_MONITOR_RECORD " (" \
@@ -46,7 +48,7 @@ SleepLogger::SleepLogger() :
                                KEY_STATE " INTEGER NOT NULL DEFAULT 1" \
                        ")");
 
-       ClientInfo clientInfo;
+       RecorderClientInfo clientInfo;
        if (clientInfo.exist(SUBJ_SENSOR_SLEEP_MONITOR))
                start();
 }
index 5bf25d1..d0e1926 100644 (file)
@@ -17,6 +17,7 @@
 #ifndef __CONTEXT_SLEEP_LOGGER_H__
 #define __CONTEXT_SLEEP_LOGGER_H__
 
+#include <string>
 #include "../SensorLogger.h"
 
 #define STATE_SLEEP    1
index 9b0b227..040e8ee 100644 (file)
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include <Types.h>
+#include <ContextTypes.h>
 #include "../TypesInternal.h"
 #include "../TimeUtil.h"
 #include "SleepMonitor.h"
index 1a8f8dc..1e3e344 100644 (file)
@@ -15,7 +15,8 @@
  */
 
 #include <sqlite3.h>
-#include <SensorRecorderTypes.h>
+#include <Tuple.h>
+#include <SensorRecorderTypesPrivate.h>
 #include "../TypesInternal.h"
 #include "SleepQuerier.h"
 
@@ -26,8 +27,7 @@
 
 using namespace ctx;
 
-SleepQuerier::SleepQuerier(ContextProvider *provider, Json option) :
-       Querier(provider, option)
+SleepQuerier::SleepQuerier()
 {
 }
 
@@ -35,12 +35,17 @@ 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 \
@@ -48,7 +53,7 @@ int SleepQuerier::query(int startTime, int endTime)
                        " 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;
index 7ba674f..e76fc10 100644 (file)
@@ -23,11 +23,13 @@ namespace ctx {
 
        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);
        };
 }
 
index 77dcd10..11aec63 100644 (file)
 
 #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