From e358d052833d6af22ce1c1acf835a0b4443fa226 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Thu, 24 Nov 2016 18:13:36 +0900 Subject: [PATCH 01/16] sensord: change file permission to remove execute permission Change-Id: I5cce417208269fdbae3899f643089ff1fbd990e5 Signed-off-by: kibak.yoon --- src/client/dbus_listener.cpp | 0 src/server/dbus_util.cpp | 0 src/server/server.cpp | 0 src/shared/macro.h | 0 4 files changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 src/client/dbus_listener.cpp mode change 100755 => 100644 src/server/dbus_util.cpp mode change 100755 => 100644 src/server/server.cpp mode change 100755 => 100644 src/shared/macro.h diff --git a/src/client/dbus_listener.cpp b/src/client/dbus_listener.cpp old mode 100755 new mode 100644 diff --git a/src/server/dbus_util.cpp b/src/server/dbus_util.cpp old mode 100755 new mode 100644 diff --git a/src/server/server.cpp b/src/server/server.cpp old mode 100755 new mode 100644 diff --git a/src/shared/macro.h b/src/shared/macro.h old mode 100755 new mode 100644 -- 2.7.4 From 530996c4a54010f2217862ac891207cb796ffd7f Mon Sep 17 00:00:00 2001 From: Akhil Date: Wed, 30 Nov 2016 22:16:08 +0900 Subject: [PATCH 02/16] [BugFix] Reset Sensor Fusion Backend if it returns NaN because of inconsitent input and high error Change-Id: I29fe84898d32f710cb914748d3111edfb2da38b7 Signed-off-by: Akhil --- src/sensor/rotation_vector/fusion_base.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/sensor/rotation_vector/fusion_base.cpp b/src/sensor/rotation_vector/fusion_base.cpp index 51e45d3..89b5a44 100644 --- a/src/sensor/rotation_vector/fusion_base.cpp +++ b/src/sensor/rotation_vector/fusion_base.cpp @@ -100,5 +100,10 @@ void fusion_base::store_orientation(void) m_y = m_orientation_filter.m_quaternion.m_quat.m_vec[1]; m_z = m_orientation_filter.m_quaternion.m_quat.m_vec[2]; m_w = m_orientation_filter.m_quaternion.m_quat.m_vec[3]; + + if (std::isnan(m_x) || std::isnan(m_y) || std::isnan(m_z) || std::isnan(m_w)) { + m_timestamp = 0; + m_orientation_filter = orientation_filter(); + } clear(); } -- 2.7.4 From 98584eab2e8e7270af197d064ca5aba166c676ea Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Fri, 2 Dec 2016 18:44:25 +0900 Subject: [PATCH 03/16] sensord: fix compiler warnings Change-Id: I6c702882e120c470ac69d6b0d510021d49ceb15c Signed-off-by: kibak.yoon --- src/sensorctl/injector_manager.cpp | 2 +- src/sensorctl/loopback_manager.cpp | 2 -- src/sensorctl/tester_manager.cpp | 2 +- src/sensorctl/tester_sensor.cpp | 4 ++-- 4 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/sensorctl/injector_manager.cpp b/src/sensorctl/injector_manager.cpp index 4f6b722..8aeebb0 100644 --- a/src/sensorctl/injector_manager.cpp +++ b/src/sensorctl/injector_manager.cpp @@ -103,7 +103,7 @@ bool injector_manager::process(int argc, char *argv[]) option_count = argc - ARGC_BASE; for (i = 0; i < option_count; ++i) { options[i] = new char[NAME_MAX_TEST]; - strncpy(options[i], argv[ARGC_BASE+i], sizeof(argv[ARGC_BASE+i])); + strncpy(options[i], argv[ARGC_BASE + i], strlen(argv[ARGC_BASE + i])); } result = injector->inject(option_count, options); diff --git a/src/sensorctl/loopback_manager.cpp b/src/sensorctl/loopback_manager.cpp index 4b78007..b7908b6 100644 --- a/src/sensorctl/loopback_manager.cpp +++ b/src/sensorctl/loopback_manager.cpp @@ -42,8 +42,6 @@ static void int_to_bytes(int32_t value, int length, char cmd[]) bool loopback_manager::process(int argc, char *argv[]) { - sensor_type_t type; - if (argc < DEFAULT_COMMAND_SIZE || argc > MAX_COMMAND_SIZE) { usage(); return false; diff --git a/src/sensorctl/tester_manager.cpp b/src/sensorctl/tester_manager.cpp index ffd9e57..c16140f 100644 --- a/src/sensorctl/tester_manager.cpp +++ b/src/sensorctl/tester_manager.cpp @@ -55,7 +55,7 @@ bool tester_manager::process(int argc, char *argv[]) option_count = argc - ARGC_BASE; for (i = 0; i < option_count; ++i) { options[i] = new char[NAME_MAX_TEST]; - strncpy(options[i], argv[ARGC_BASE+i], sizeof(argv[ARGC_BASE+i])); + strncpy(options[i], argv[ARGC_BASE + i], strlen(argv[ARGC_BASE + i])); } tester->test(type, option_count, options); diff --git a/src/sensorctl/tester_sensor.cpp b/src/sensorctl/tester_sensor.cpp index 5a75da6..7f324f6 100644 --- a/src/sensorctl/tester_sensor.cpp +++ b/src/sensorctl/tester_sensor.cpp @@ -36,8 +36,8 @@ static GMainLoop *mainloop; static int check_loop; static const char *result_str(bool result) { - if (result) return KGRN"[PASS]"RESET; - else return KRED"[FAIL]"RESET; + if (result) return KGRN "[PASS]" RESET; + else return KRED "[FAIL]" RESET; } bool tester_sensor::init(void) -- 2.7.4 From 0c9da3394ec379bd829a8d5e4712bcfb3198cb9a Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Tue, 6 Dec 2016 10:43:02 +0900 Subject: [PATCH 04/16] sensord: install required header files instead of all header files Change-Id: Ib826fe1e3464a560f58e5868c64fd4e2cf9a8136 Signed-off-by: kibak.yoon --- src/client/CMakeLists.txt | 12 ++++++++---- src/client/sensor_internal.h | 2 +- src/client/sensor_internal_deprecated.h | 2 +- src/shared/CMakeLists.txt | 15 +++++++++++---- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/client/CMakeLists.txt b/src/client/CMakeLists.txt index 2f32b9c..f14b841 100644 --- a/src/client/CMakeLists.txt +++ b/src/client/CMakeLists.txt @@ -22,6 +22,11 @@ ENDFOREACH(flag) SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden -fvisibility-inlines-hidden -g -fPIC") +SET (INSTALL_HEADERS + sensor_internal.h + sensor_internal_deprecated.h +) + INCLUDE_DIRECTORIES( ${CMAKE_SOURCE_DIR}/src/hal ${CMAKE_SOURCE_DIR}/src/shared @@ -37,9 +42,8 @@ SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES VERSION ${FULLVER}) CONFIGURE_FILE(${PROJECT_NAME}.pc.in ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}.pc @ONLY) INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT RuntimeLibraries) -INSTALL( - DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/sensor - FILES_MATCHING PATTERN "*.h" - ) +FOREACH(HEADER IN ITEMS ${INSTALL_HEADERS}) + INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${HEADER} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/sensor) +ENDFOREACH() INSTALL(FILES ${PROJECT_NAME}.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig) diff --git a/src/client/sensor_internal.h b/src/client/sensor_internal.h index 47452d4..b7e9469 100644 --- a/src/client/sensor_internal.h +++ b/src/client/sensor_internal.h @@ -24,7 +24,7 @@ #define API __attribute__((visibility("default"))) #endif -#include "stdbool.h" +#include #include /*header for common sensor type*/ diff --git a/src/client/sensor_internal_deprecated.h b/src/client/sensor_internal_deprecated.h index a2b5b30..851ccbc 100644 --- a/src/client/sensor_internal_deprecated.h +++ b/src/client/sensor_internal_deprecated.h @@ -24,7 +24,7 @@ #define DEPRECATED __attribute__((deprecated)) #endif -#include "stdbool.h" +#include #include diff --git a/src/shared/CMakeLists.txt b/src/shared/CMakeLists.txt index 8b8416e..5d094ec 100644 --- a/src/shared/CMakeLists.txt +++ b/src/shared/CMakeLists.txt @@ -11,6 +11,13 @@ FOREACH(flag ${SHARED_PKGS_CFLAGS}) ENDFOREACH(flag) SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") +SET (INSTALL_HEADERS + sensor_types.h + sensor_common.h + sensor_deprecated.h + enum_factory.h +) + INCLUDE_DIRECTORIES( ${CMAKE_SOURCE_DIR}/src/hal ${CMAKE_CURRENT_SOURCE_DIR} @@ -22,7 +29,7 @@ ADD_LIBRARY(${PROJECT_NAME} SHARED ${SRCS}) TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${SHARED_PKGS_LDFLAGS}) INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_LIBDIR}) -INSTALL( - DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/sensor - FILES_MATCHING PATTERN "*.h" -) + +FOREACH(HEADER IN ITEMS ${INSTALL_HEADERS}) + INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${HEADER} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/sensor) +ENDFOREACH() -- 2.7.4 From 77be7fb50f78d4858acd5c2aad3f32342cea0121 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Sat, 10 Dec 2016 14:57:24 +0900 Subject: [PATCH 05/16] sensord: add pre_start() that should be executed before starting regardless of clients Change-Id: I1cd8a70cdfcb5f838c94a1da7fced99835fbf872 Signed-off-by: kibak.yoon --- src/sensor/auto_rotation/auto_rotation_sensor.cpp | 6 ++++++ src/sensor/auto_rotation/auto_rotation_sensor.h | 1 + src/server/sensor_base.cpp | 8 ++++++++ src/server/sensor_base.h | 1 + 4 files changed, 16 insertions(+) diff --git a/src/sensor/auto_rotation/auto_rotation_sensor.cpp b/src/sensor/auto_rotation/auto_rotation_sensor.cpp index c3bd18e..71d10b4 100644 --- a/src/sensor/auto_rotation/auto_rotation_sensor.cpp +++ b/src/sensor/auto_rotation/auto_rotation_sensor.cpp @@ -194,6 +194,12 @@ bool auto_rotation_sensor::set_wakeup(int wakeup) return false; } +bool auto_rotation_sensor::pre_start(void) +{ + m_rotation = AUTO_ROTATION_DEGREE_UNKNOWN; + return true; +} + bool auto_rotation_sensor::on_start(void) { int length; diff --git a/src/sensor/auto_rotation/auto_rotation_sensor.h b/src/sensor/auto_rotation/auto_rotation_sensor.h index ddd4e3c..d50f728 100644 --- a/src/sensor/auto_rotation/auto_rotation_sensor.h +++ b/src/sensor/auto_rotation/auto_rotation_sensor.h @@ -56,6 +56,7 @@ private: virtual bool set_batch_latency(unsigned long latency); virtual bool set_wakeup(int wakeup); + virtual bool pre_start(void); virtual bool on_start(void); virtual bool on_stop(void); diff --git a/src/server/sensor_base.cpp b/src/server/sensor_base.cpp index a5a40bb..24e16a7 100644 --- a/src/server/sensor_base.cpp +++ b/src/server/sensor_base.cpp @@ -121,6 +121,9 @@ bool sensor_base::start(void) { AUTOLOCK(m_client_mutex); + if (!pre_start()) + return false; + ++m_client; if (m_client == 1) { @@ -348,6 +351,11 @@ bool sensor_base::set_batch_latency(unsigned long latency) return true; } +bool sensor_base::pre_start(void) +{ + return true; +} + bool sensor_base::on_start(void) { return true; diff --git a/src/server/sensor_base.h b/src/server/sensor_base.h index 04883d8..89b88ed 100644 --- a/src/server/sensor_base.h +++ b/src/server/sensor_base.h @@ -103,6 +103,7 @@ private: virtual bool set_interval(unsigned long interval); virtual bool set_batch_latency(unsigned long latency); + virtual bool pre_start(void); virtual bool on_start(void); virtual bool on_stop(void); -- 2.7.4 From ef5f8297e1557197bab236094a4882c08b46e1c9 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Tue, 13 Dec 2016 16:47:53 +0900 Subject: [PATCH 06/16] sensor: fix return value from false to 0 Change-Id: Icb84d1ec8f8e20b29f02dc86807a4fe535c79455 Signed-off-by: kibak.yoon --- src/shared/csocket.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/shared/csocket.cpp b/src/shared/csocket.cpp index 4a97a3c..ca9ec4f 100644 --- a/src/shared/csocket.cpp +++ b/src/shared/csocket.cpp @@ -324,15 +324,15 @@ ssize_t csocket::send(const void *buffer, size_t size) const if (ret == -1) { _ERRNO(errno, _E, "select error: sock_fd: %d\n for %s", m_sock_fd, get_client_name()); - return false; + return 0; } else if (!ret) { _ERRNO(errno, _E, "select timeout: %d seconds elapsed for %s", tv.tv_sec, get_client_name()); - return false; + return 0; } if (!FD_ISSET(m_sock_fd, &write_fds)) { _ERRNO(errno, _E, "select failed for %s, nothing to write, m_sock_fd : %d", get_client_name(), m_sock_fd); - return false; + return 0; } if (m_sock_type == SOCK_STREAM) -- 2.7.4 From 32032e4e333ebb3cfa5ee53320d0db858fd6f44e Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Tue, 13 Dec 2016 18:35:45 +0900 Subject: [PATCH 07/16] sensor: enable pedometer sensor - pedometer sensor needs healthinfo privilege Change-Id: I9ac4e2079fd0a6e640c92b63ce13f3c3f1c81ed0 Signed-off-by: kibak.yoon --- src/sensor/CMakeLists.txt | 6 ++++++ src/sensor/sensorhub/pedometer_sensor.cpp | 34 +++++++++++++++++++++++++++++++ src/sensor/sensorhub/pedometer_sensor.h | 32 +++++++++++++++++++++++++++++ src/server/sensor_loader.cpp | 6 ++++++ 4 files changed, 78 insertions(+) create mode 100644 src/sensor/sensorhub/pedometer_sensor.cpp create mode 100644 src/sensor/sensorhub/pedometer_sensor.h diff --git a/src/sensor/CMakeLists.txt b/src/sensor/CMakeLists.txt index 3ec60fd..272b547 100644 --- a/src/sensor/CMakeLists.txt +++ b/src/sensor/CMakeLists.txt @@ -10,6 +10,7 @@ SET(LINEAR_ACCEL "ON") SET(RV "ON") SET(ORIENTATION "ON") SET(FACE_DOWN "ON") +SET(SENSORHUB "ON") INCLUDE_DIRECTORIES( ${CMAKE_SOURCE_DIR}/src/shared @@ -63,6 +64,11 @@ IF("${FACE_DOWN}" STREQUAL "ON") SET(SENSOR_HEADERS ${SENSOR_HEADERS} ${CMAKE_CURRENT_SOURCE_DIR}/gesture) SET(SENSOR_DEFINITIONS ${SENSOR_DEFINITIONS} "-DENABLE_FACE_DOWN") ENDIF() +IF("${SENSORHUB}" STREQUAL "ON") + FILE(GLOB_RECURSE SENSOR_SRCS ${SENSOR_SRCS} ${CMAKE_CURRENT_SOURCE_DIR}/sensorhub/*.cpp) + SET(SENSOR_HEADERS ${SENSOR_HEADERS} ${CMAKE_CURRENT_SOURCE_DIR}/sensorhub) + SET(SENSOR_DEFINITIONS ${SENSOR_DEFINITIONS} "-DENABLE_SENSORHUB") +ENDIF() MESSAGE("${SENSOR_SRCS}") SET(SENSOR_SRCS ${SENSOR_SRCS} PARENT_SCOPE) diff --git a/src/sensor/sensorhub/pedometer_sensor.cpp b/src/sensor/sensorhub/pedometer_sensor.cpp new file mode 100644 index 0000000..b9d5c23 --- /dev/null +++ b/src/sensor/sensorhub/pedometer_sensor.cpp @@ -0,0 +1,34 @@ +/* + * sensord + * + * 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 +#include +#include "pedometer_sensor.h" + +pedometer_sensor::pedometer_sensor() +{ + set_permission(SENSOR_PERMISSION_BIO); + + _I("pedometer_sensor is created : %#x", this); +} + +pedometer_sensor::~pedometer_sensor() +{ + _I("pedometer_sensor is destroyed : %#x", this); +} diff --git a/src/sensor/sensorhub/pedometer_sensor.h b/src/sensor/sensorhub/pedometer_sensor.h new file mode 100644 index 0000000..5ae94c2 --- /dev/null +++ b/src/sensor/sensorhub/pedometer_sensor.h @@ -0,0 +1,32 @@ +/* + * sensord + * + * 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. + * + */ + +#ifndef _PEDOMETER_SENSOR_H_ +#define _PEDOMETER_SENSOR_H_ + +#include + +class pedometer_sensor : public physical_sensor { +public: + pedometer_sensor(); + virtual ~pedometer_sensor(); +}; + +#endif /* _PEDOMETER_SENSOR_H_ */ + diff --git a/src/server/sensor_loader.cpp b/src/server/sensor_loader.cpp index c25d07a..614e057 100644 --- a/src/server/sensor_loader.cpp +++ b/src/server/sensor_loader.cpp @@ -53,6 +53,9 @@ #ifdef ENABLE_FACE_DOWN #include #endif +#ifdef ENABLE_SENSORHUB +#include +#endif using std::vector; using std::string; @@ -169,6 +172,9 @@ void sensor_loader::create_sensors(void) create_physical_sensors(HRM_LED_GREEN_SENSOR); create_physical_sensors(HRM_LED_IR_SENSOR); create_physical_sensors(HRM_LED_RED_SENSOR); +#ifdef ENABLE_SENSORHUB + create_physical_sensors(HUMAN_PEDOMETER_SENSOR); +#endif create_physical_sensors(UNKNOWN_SENSOR); -- 2.7.4 From 5abd1da78db60bc3a8674b75db3b38b7e8c2f78a Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Tue, 13 Dec 2016 19:50:03 +0900 Subject: [PATCH 08/16] sensord: fix incorrect return type - int to size_t Change-Id: Ia1d5b0308e2a9a546c1980d0bb1896a1e4a88204 Signed-off-by: kibak.yoon --- src/server/command_worker.cpp | 2 +- src/server/command_worker.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/server/command_worker.cpp b/src/server/command_worker.cpp index 8d57968..2436ab8 100644 --- a/src/server/command_worker.cpp +++ b/src/server/command_worker.cpp @@ -91,7 +91,7 @@ void command_worker::init_cmd_handlers(void) m_cmd_handlers[CMD_FLUSH] = &command_worker::cmd_flush; } -int command_worker::create_sensor_raw_list(int client_perms, std::vector &raw_list) +size_t command_worker::create_sensor_raw_list(int client_perms, std::vector &raw_list) { size_t total_raw_data_size = 0; vector sensors; diff --git a/src/server/command_worker.h b/src/server/command_worker.h index 3e1b62c..e6dff1d 100644 --- a/src/server/command_worker.h +++ b/src/server/command_worker.h @@ -45,7 +45,7 @@ private: static sensor_raw_data_map m_sensor_raw_data_map; static void init_cmd_handlers(void); - static int create_sensor_raw_list(int client_perms, std::vector &raw_list); + static size_t create_sensor_raw_list(int client_perms, std::vector &raw_list); static void get_sensor_list(int permissions, cpacket &sensor_list); static bool working(void *ctx); -- 2.7.4 From 4217c2abce8ac882f8f3384c1dca8d97f37d5baf Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Tue, 13 Dec 2016 20:04:01 +0900 Subject: [PATCH 09/16] sensord: version up 2.0.9 Change-Id: I4d12ec6c0c12890c58407657dca42621cc128819 Signed-off-by: kibak.yoon --- packaging/sensord.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/sensord.spec b/packaging/sensord.spec index bb1cd3f..957ebcb 100644 --- a/packaging/sensord.spec +++ b/packaging/sensord.spec @@ -1,6 +1,6 @@ Name: sensord Summary: Sensor daemon -Version: 2.0.8 +Version: 2.0.9 Release: 0 Group: System/Sensor Framework License: Apache-2.0 -- 2.7.4 From e02c195ed699ebf0f0cf4869624a6734029dd762 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Wed, 28 Dec 2016 15:38:07 +0900 Subject: [PATCH 10/16] sensord: accumulate pedometer data - accumulated pedometer data should be provided to client Change-Id: Ib9a4bb5325867e1e55f1e8c9b9a32609915dbd08 Signed-off-by: kibak.yoon --- src/sensor/sensorhub/pedometer_sensor.cpp | 60 ++++++++++++++++++++++++++++++- src/sensor/sensorhub/pedometer_sensor.h | 15 ++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/src/sensor/sensorhub/pedometer_sensor.cpp b/src/sensor/sensorhub/pedometer_sensor.cpp index b9d5c23..c825e41 100644 --- a/src/sensor/sensorhub/pedometer_sensor.cpp +++ b/src/sensor/sensorhub/pedometer_sensor.cpp @@ -21,7 +21,32 @@ #include #include "pedometer_sensor.h" +enum value_index { + IDX_STEPS = 0, + IDX_WALK_STEPS, + IDX_RUN_STEPS, + IDX_DISTANCE, + IDX_CALORIES, + IDX_SPEED, + IDX_FREQUENCY, + IDX_STATE, + IDX_WALK_UP, + IDX_WALK_DOWN, + IDX_RUN_UP, + IDX_RUN_DOWN, + IDX_STATE_EX, +}; + pedometer_sensor::pedometer_sensor() +: m_steps(0) +, m_walk_steps(0) +, m_run_steps(0) +, m_walk_up_steps(0) +, m_walk_down_steps(0) +, m_run_up_steps(0) +, m_run_down_steps(0) +, m_distance(0) +, m_calories(0) { set_permission(SENSOR_PERMISSION_BIO); @@ -30,5 +55,38 @@ pedometer_sensor::pedometer_sensor() pedometer_sensor::~pedometer_sensor() { - _I("pedometer_sensor is destroyed : %#x", this); +} + +bool pedometer_sensor::on_event(const sensor_data_t *data, int data_len, int remains) +{ + if (data_len == sizeof(sensorhub_data_t)) + return false; + + accumulate((sensor_pedometer_data_t*)data); + return true; +} + +void pedometer_sensor::accumulate(sensor_pedometer_data_t *data) +{ + m_steps += data->values[IDX_STEPS]; + m_walk_steps += data->values[IDX_WALK_STEPS]; + m_run_steps += data->values[IDX_RUN_STEPS]; + m_distance += data->values[IDX_DISTANCE]; + m_calories += data->values[IDX_CALORIES]; + + m_walk_up_steps += data->values[IDX_WALK_UP]; + m_walk_down_steps += data->values[IDX_WALK_DOWN]; + m_run_up_steps += data->values[IDX_RUN_UP]; + m_run_down_steps += data->values[IDX_RUN_DOWN]; + + data->values[IDX_STEPS] = m_steps; + data->values[IDX_WALK_STEPS] = m_walk_steps; + data->values[IDX_RUN_STEPS] = m_run_steps; + data->values[IDX_DISTANCE] = m_distance; + data->values[IDX_CALORIES] = m_calories; + + data->values[IDX_WALK_UP] = m_walk_up_steps; + data->values[IDX_WALK_DOWN] = m_walk_down_steps; + data->values[IDX_RUN_UP] = m_run_up_steps; + data->values[IDX_RUN_DOWN] = m_run_down_steps; } diff --git a/src/sensor/sensorhub/pedometer_sensor.h b/src/sensor/sensorhub/pedometer_sensor.h index 5ae94c2..16f1ac7 100644 --- a/src/sensor/sensorhub/pedometer_sensor.h +++ b/src/sensor/sensorhub/pedometer_sensor.h @@ -26,6 +26,21 @@ class pedometer_sensor : public physical_sensor { public: pedometer_sensor(); virtual ~pedometer_sensor(); + + bool on_event(const sensor_data_t *data, int data_len, int remains); + +private: + unsigned long long m_steps; + unsigned long long m_walk_steps; + unsigned long long m_run_steps; + unsigned long long m_walk_up_steps; + unsigned long long m_walk_down_steps; + unsigned long long m_run_up_steps; + unsigned long long m_run_down_steps; + double m_distance; + double m_calories; + + void accumulate(sensor_pedometer_data_t *data); }; #endif /* _PEDOMETER_SENSOR_H_ */ -- 2.7.4 From e970cca5105790b1c3c70194a23c4c5176bd06a1 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Mon, 2 Jan 2017 14:39:03 +0900 Subject: [PATCH 11/16] sensord: add dummy api library for tv profile Change-Id: I610a6a2e43e475b20e96432d47b68aebe7785655 Signed-off-by: kibak.yoon --- packaging/sensord.spec | 12 ++- src/client/CMakeLists.txt | 11 ++ src/client/client_dummy.cpp | 204 +++++++++++++++++++++++++++++++++++ src/client/external_client_dummy.cpp | 39 +++++++ 4 files changed, 263 insertions(+), 3 deletions(-) create mode 100644 src/client/client_dummy.cpp create mode 100644 src/client/external_client_dummy.cpp diff --git a/packaging/sensord.spec b/packaging/sensord.spec index 957ebcb..cfd30af 100644 --- a/packaging/sensord.spec +++ b/packaging/sensord.spec @@ -20,6 +20,8 @@ BuildRequires: pkgconfig(cynara-client) BuildRequires: pkgconfig(cynara-session) Requires: libsensord = %{version}-%{release} +%define BUILD_PROFILE %{?profile}%{!?profile:%{?tizen_profile_name}} + %description Sensor daemon @@ -56,9 +58,8 @@ Sensor functional testing %prep %setup -q MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'` - cmake . -DCMAKE_INSTALL_PREFIX=%{_prefix} -DLIBDIR=%{_libdir} \ - -DMAJORVER=${MAJORVER} -DFULLVER=%{version} + -DMAJORVER=${MAJORVER} -DFULLVER=%{version} -DPROFILE=%{BUILD_PROFILE} %build make %{?jobs:-j%jobs} @@ -69,6 +70,7 @@ rm -rf %{buildroot} mkdir -p %{buildroot}%{_unitdir} +%if "%{?BUILD_PROFILE}" != "tv" install -m 0644 %SOURCE1 %{buildroot}%{_unitdir} install -m 0644 %SOURCE2 %{buildroot}%{_unitdir} install -m 0644 %SOURCE3 %{buildroot}%{_unitdir} @@ -76,6 +78,7 @@ install -m 0644 %SOURCE3 %{buildroot}%{_unitdir} %install_service multi-user.target.wants sensord.service %install_service sockets.target.wants sensord_event.socket %install_service sockets.target.wants sensord_command.socket +%endif %post systemctl daemon-reload @@ -93,13 +96,16 @@ ln -sf %{_libdir}/libsensor.so.%{version} %{_libdir}/libsensor.so.1 %files %manifest packaging/sensord.manifest %{_bindir}/sensord +%license LICENSE.APLv2 + +%if "%{?BUILD_PROFILE}" != "tv" %{_unitdir}/sensord.service %{_unitdir}/sensord_command.socket %{_unitdir}/sensord_event.socket %{_unitdir}/multi-user.target.wants/sensord.service %{_unitdir}/sockets.target.wants/sensord_command.socket %{_unitdir}/sockets.target.wants/sensord_event.socket -%license LICENSE.APLv2 +%endif %files -n libsensord %defattr(-,root,root,-) diff --git a/src/client/CMakeLists.txt b/src/client/CMakeLists.txt index f14b841..33ed7fa 100644 --- a/src/client/CMakeLists.txt +++ b/src/client/CMakeLists.txt @@ -34,6 +34,17 @@ INCLUDE_DIRECTORIES( ) FILE(GLOB_RECURSE SRCS *.cpp) + +IF("${PROFILE}" STREQUAL "tv") + LIST(REMOVE_ITEM SRCS + "${CMAKE_CURRENT_SOURCE_DIR}/client.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/external_client.cpp") +ELSE() + LIST(REMOVE_ITEM SRCS + "${CMAKE_CURRENT_SOURCE_DIR}/client_dummy.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/external_client_dummy.cpp") +ENDIF() + ADD_LIBRARY(${PROJECT_NAME} SHARED ${SRCS}) TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${PKGS_LDFLAGS} "sensord-shared") diff --git a/src/client/client_dummy.cpp b/src/client/client_dummy.cpp new file mode 100644 index 0000000..c7732a6 --- /dev/null +++ b/src/client/client_dummy.cpp @@ -0,0 +1,204 @@ +/* + * sensord + * + * Copyright (c) 2013 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 +#include + +#include "sensor_internal.h" +#include "sensor_internal_deprecated.h" + +API int sensord_get_sensors(sensor_type_t type, sensor_t **list, int *sensor_count) +{ + return -ENODATA; +} + +API int sensord_get_default_sensor(sensor_type_t type, sensor_t *sensor) +{ + return -ENODATA; +} + +API bool sensord_get_sensor_list(sensor_type_t type, sensor_t **list, int *sensor_count) +{ + return false; +} + +API sensor_t sensord_get_sensor(sensor_type_t type) +{ + return NULL; +} + +API bool sensord_get_type(sensor_t sensor, sensor_type_t *type) +{ + return false; +} + +API const char* sensord_get_name(sensor_t sensor) +{ + return NULL; +} + +API const char* sensord_get_vendor(sensor_t sensor) +{ + return NULL; +} + +API bool sensord_get_privilege(sensor_t sensor, sensor_privilege_t *privilege) +{ + return false; +} + +API bool sensord_get_min_range(sensor_t sensor, float *min_range) +{ + return false; +} + +API bool sensord_get_max_range(sensor_t sensor, float *max_range) +{ + return false; +} + +API bool sensord_get_resolution(sensor_t sensor, float *resolution) +{ + return false; +} + +API bool sensord_get_min_interval(sensor_t sensor, int *min_interval) +{ + return false; +} + +API bool sensord_get_fifo_count(sensor_t sensor, int *fifo_count) +{ + return false; +} + +API bool sensord_get_max_batch_count(sensor_t sensor, int *max_batch_count) +{ + return false; +} + +API bool sensord_get_supported_event_types(sensor_t sensor, unsigned int **event_types, int *count) +{ + return false; +} + +API bool sensord_is_supported_event_type(sensor_t sensor, unsigned int event_type, bool *supported) +{ + return false; +} + +API bool sensord_is_wakeup_supported(sensor_t sensor) +{ + return false; +} + +API int sensord_connect(sensor_t sensor) +{ + return OP_ERROR; +} + +API bool sensord_disconnect(int handle) +{ + return false; +} + +API bool sensord_register_event(int handle, unsigned int event_type, unsigned int interval, unsigned int max_batch_latency, sensor_cb_t cb, void *user_data) +{ + return false; +} + +API bool sensord_unregister_event(int handle, unsigned int event_type) +{ + return false; +} + +API bool sensord_register_accuracy_cb(int handle, sensor_accuracy_changed_cb_t cb, void *user_data) +{ + return false; +} + +API bool sensord_unregister_accuracy_cb(int handle) +{ + return false; +} + +API bool sensord_start(int handle, int option) +{ + return false; +} + +API bool sensord_stop(int handle) +{ + return false; +} + +API bool sensord_change_event_interval(int handle, unsigned int event_type, unsigned int interval) +{ + return false; +} + +API bool sensord_change_event_max_batch_latency(int handle, unsigned int event_type, unsigned int max_batch_latency) +{ + return false; +} + +API bool sensord_set_option(int handle, int option) +{ + return false; +} + +API int sensord_set_attribute_int(int handle, int attribute, int value) +{ + return OP_ERROR; +} + +API int sensord_set_attribute_str(int handle, int attribute, const char *value, int len) +{ + return OP_ERROR; +} + +API bool sensord_send_sensorhub_data(int handle, const char *data, int data_len) +{ + return false; +} + +API bool sensord_send_command(int handle, const char *command, int command_len) +{ + return false; +} + +API bool sensord_get_data(int handle, unsigned int data_id, sensor_data_t* sensor_data) +{ + return false; +} + +API bool sensord_flush(int handle) +{ + return false; +} + +API bool sensord_register_hub_event(int handle, unsigned int event_type, unsigned int interval, unsigned int max_batch_latency, sensorhub_cb_t cb, void *user_data) +{ + return false; +} + +API bool sensord_set_passive_mode(int handle, bool passive) +{ + return false; +} diff --git a/src/client/external_client_dummy.cpp b/src/client/external_client_dummy.cpp new file mode 100644 index 0000000..8f359d0 --- /dev/null +++ b/src/client/external_client_dummy.cpp @@ -0,0 +1,39 @@ +/* + * sensord + * + * Copyright (c) 2013 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 +#include + +#include "sensor_internal.h" +#include "sensor_internal_deprecated.h" + +API int sensord_external_connect(const char *key, sensor_external_command_cb_t cb, void *user_data) +{ + return OP_ERROR; +} + +API bool sensord_external_disconnect(int handle) +{ + return false; +} + +API bool sensord_external_post(int handle, unsigned long long timestamp, const float* data, int data_cnt) +{ + return false; +} -- 2.7.4 From 8648c87faa7c03362fb23e01cf34c7556202dc6f Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Mon, 2 Jan 2017 20:21:17 +0900 Subject: [PATCH 12/16] Remove profile build dependency Instead of the four packages, sensord, libsensord, libsensord-devel, and sensor-hal-devel, the following packages are produced: - sensord : dummy internal API library, which completely disable all sensor listener features - sensord-genuine : working internal API library and the sensor service daemon - sensord-devel : internal API headers and the pc file - sensor-hal-devel : API headers for Sensor HAL Change-Id: Ie3529d6e5eba0554fd86b4d7f95ea9726db00aa4 Signed-off-by: Mu-Woong Lee --- CMakeLists.txt | 12 ++- {src/shared => include}/enum_factory.h | 0 {src/shared => include}/sensor_common.h | 0 {src/shared => include}/sensor_deprecated.h | 0 {src/hal => include}/sensor_hal.h | 0 {src/hal => include}/sensor_hal_types.h | 0 {src/client => include}/sensor_internal.h | 0 .../sensor_internal_deprecated.h | 0 {src/shared => include}/sensor_types.h | 2 +- packaging/sensord.spec | 103 ++++++++++++--------- src/client-dummy/CMakeLists.txt | 39 ++++++++ src/client-dummy/client_deprecated.cpp | 84 +++++++++++++++++ src/{client => client-dummy}/client_dummy.cpp | 4 +- .../external_client_dummy.cpp | 4 +- src/{client => client-dummy}/sensor.pc.in | 0 src/client/CMakeLists.txt | 42 +-------- src/hal/CMakeLists.txt | 5 - src/sensorctl/CMakeLists.txt | 2 +- src/shared/CMakeLists.txt | 11 --- src/shared/sensor_types.cpp | 2 +- 20 files changed, 202 insertions(+), 108 deletions(-) rename {src/shared => include}/enum_factory.h (100%) rename {src/shared => include}/sensor_common.h (100%) rename {src/shared => include}/sensor_deprecated.h (100%) rename {src/hal => include}/sensor_hal.h (100%) rename {src/hal => include}/sensor_hal_types.h (100%) rename {src/client => include}/sensor_internal.h (100%) rename {src/client => include}/sensor_internal_deprecated.h (100%) rename {src/shared => include}/sensor_types.h (99%) create mode 100644 src/client-dummy/CMakeLists.txt create mode 100644 src/client-dummy/client_deprecated.cpp rename src/{client => client-dummy}/client_dummy.cpp (98%) rename src/{client => client-dummy}/external_client_dummy.cpp (93%) rename src/{client => client-dummy}/sensor.pc.in (100%) delete mode 100644 src/hal/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index bf31fa9..c5d9d08 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,8 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.6) PROJECT(sensord-main CXX) INCLUDE(GNUInstallDirs) +INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include) + # Common Options SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -O2 -omit-frame-pointer -std=gnu++0x") SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fdata-sections -ffunction-sections") @@ -13,8 +15,14 @@ MESSAGE("FLAGS: ${CMAKE_EXE_LINKER_FLAGS}") #add_definitions(-Wall -g -D_DEBUG) # Sub-directory +ADD_SUBDIRECTORY(src/shared) ADD_SUBDIRECTORY(src/server) ADD_SUBDIRECTORY(src/client) -ADD_SUBDIRECTORY(src/shared) -ADD_SUBDIRECTORY(src/hal) +ADD_SUBDIRECTORY(src/client-dummy) ADD_SUBDIRECTORY(src/sensorctl) + +INSTALL( + DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/ + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/sensor + FILES_MATCHING PATTERN "*.h" +) diff --git a/src/shared/enum_factory.h b/include/enum_factory.h similarity index 100% rename from src/shared/enum_factory.h rename to include/enum_factory.h diff --git a/src/shared/sensor_common.h b/include/sensor_common.h similarity index 100% rename from src/shared/sensor_common.h rename to include/sensor_common.h diff --git a/src/shared/sensor_deprecated.h b/include/sensor_deprecated.h similarity index 100% rename from src/shared/sensor_deprecated.h rename to include/sensor_deprecated.h diff --git a/src/hal/sensor_hal.h b/include/sensor_hal.h similarity index 100% rename from src/hal/sensor_hal.h rename to include/sensor_hal.h diff --git a/src/hal/sensor_hal_types.h b/include/sensor_hal_types.h similarity index 100% rename from src/hal/sensor_hal_types.h rename to include/sensor_hal_types.h diff --git a/src/client/sensor_internal.h b/include/sensor_internal.h similarity index 100% rename from src/client/sensor_internal.h rename to include/sensor_internal.h diff --git a/src/client/sensor_internal_deprecated.h b/include/sensor_internal_deprecated.h similarity index 100% rename from src/client/sensor_internal_deprecated.h rename to include/sensor_internal_deprecated.h diff --git a/src/shared/sensor_types.h b/include/sensor_types.h similarity index 99% rename from src/shared/sensor_types.h rename to include/sensor_types.h index dab36bc..ac1f13a 100644 --- a/src/shared/sensor_types.h +++ b/include/sensor_types.h @@ -20,7 +20,7 @@ #ifndef _SENSOR_TYPES_H_ #define _SENSOR_TYPES_H_ -#include "enum_factory.h" +#include #ifdef __cplusplus extern "C" diff --git a/packaging/sensord.spec b/packaging/sensord.spec index cfd30af..e0e45ec 100644 --- a/packaging/sensord.spec +++ b/packaging/sensord.spec @@ -1,7 +1,7 @@ Name: sensord Summary: Sensor daemon -Version: 2.0.9 -Release: 0 +Version: 2.0.10 +Release: 1 Group: System/Sensor Framework License: Apache-2.0 Source0: %{name}-%{version}.tar.gz @@ -18,28 +18,35 @@ BuildRequires: pkgconfig(libsystemd-daemon) BuildRequires: pkgconfig(cynara-creds-socket) BuildRequires: pkgconfig(cynara-client) BuildRequires: pkgconfig(cynara-session) -Requires: libsensord = %{version}-%{release} -%define BUILD_PROFILE %{?profile}%{!?profile:%{?tizen_profile_name}} +Provides: %{name}-profile_tv = %{version}-%{release} +# For backward compatibility +Provides: libsensord = %{version}-%{release} %description Sensor daemon -%package -n libsensord -Summary: Sensord library -Group: System/Libraries +%package genuine +Summary: Genuine Sensor Framework service daemon and shared library Requires: %{name} = %{version}-%{release} - -%description -n libsensord -Sensord library - -%package -n libsensord-devel -Summary: Sensord shared library +Provides: %{name}-profile_mobile = %{version}-%{release} +Provides: %{name}-profile_wearable = %{version}-%{release} +Provides: %{name}-profile_ivi = %{version}-%{release} +Provides: %{name}-profile_common = %{version}-%{release} + +%description genuine +Binary replacement for sensord. +This genuine sensord package contains actually working shared library +of the sensor internal APIs and the sensor service daemon. +If you want to keep using %{name} after uninstalling this, you need to reinstall %{name}. + +%package devel +Summary: Internal Sensor API (Development) Group: System/Development -Requires: libsensord = %{version}-%{release} +Requires: %{name} = %{version}-%{release} -%description -n libsensord-devel -Sensord shared library +%description devel +Internal Sensor API (Development) %package -n sensor-hal-devel Summary: Sensord HAL interface @@ -55,11 +62,20 @@ Group: System/Testing %description -n sensor-test Sensor functional testing +# This dummy package will be removed later. +%package -n libsensord-devel +Summary: Dummy package for backward compatibility +Requires: sensord-devel + +%description -n libsensord-devel +Some packages require libsensord-devel directly, and it causes local gbs build failures +with the old build snapshots. This is a temporal solution to handle such cases. + %prep %setup -q MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'` cmake . -DCMAKE_INSTALL_PREFIX=%{_prefix} -DLIBDIR=%{_libdir} \ - -DMAJORVER=${MAJORVER} -DFULLVER=%{version} -DPROFILE=%{BUILD_PROFILE} + -DMAJORVER=${MAJORVER} -DFULLVER=%{version} %build make %{?jobs:-j%jobs} @@ -70,7 +86,6 @@ rm -rf %{buildroot} mkdir -p %{buildroot}%{_unitdir} -%if "%{?BUILD_PROFILE}" != "tv" install -m 0644 %SOURCE1 %{buildroot}%{_unitdir} install -m 0644 %SOURCE2 %{buildroot}%{_unitdir} install -m 0644 %SOURCE3 %{buildroot}%{_unitdir} @@ -78,56 +93,52 @@ install -m 0644 %SOURCE3 %{buildroot}%{_unitdir} %install_service multi-user.target.wants sensord.service %install_service sockets.target.wants sensord_event.socket %install_service sockets.target.wants sensord_command.socket -%endif + +ln -s libsensor.so.2 %{buildroot}%{_libdir}/libsensor.so.1 %post -systemctl daemon-reload +/sbin/ldconfig -%postun -systemctl daemon-reload +%files +%manifest packaging/sensord.manifest +%{_libdir}/libsensor.so.* +%license LICENSE.APLv2 -%post -n libsensord -ln -sf %{_libdir}/libsensor.so.%{version} %{_libdir}/libsensor.so.1 +%post genuine +pushd %{_libdir} +ln -sf libsensor-genuine.so.%{version} libsensor.so.%{version} +chsmack -a "_" libsensor.so.%{version} +popd /sbin/ldconfig -%postun -n libsensord -/sbin/ldconfig +%preun genuine +echo "You need to reinstall %{name}, if you need to keep using the APIs after uinstalling this." -%files +%files genuine %manifest packaging/sensord.manifest +%{_libdir}/libsensord-shared.so +%{_libdir}/libsensor-genuine.so.* %{_bindir}/sensord -%license LICENSE.APLv2 - -%if "%{?BUILD_PROFILE}" != "tv" %{_unitdir}/sensord.service %{_unitdir}/sensord_command.socket %{_unitdir}/sensord_event.socket %{_unitdir}/multi-user.target.wants/sensord.service %{_unitdir}/sockets.target.wants/sensord_command.socket %{_unitdir}/sockets.target.wants/sensord_event.socket -%endif - -%files -n libsensord -%defattr(-,root,root,-) -%manifest packaging/libsensord.manifest -%{_libdir}/libsensor.so.* -%{_libdir}/libsensord-shared.so -%license LICENSE.APLv2 -%files -n libsensord-devel -%defattr(-,root,root,-) +%files devel +%manifest packaging/sensord.manifest +%exclude %{_includedir}/sensor/sensor_hal.h %{_includedir}/sensor/*.h %{_libdir}/libsensor.so %{_libdir}/pkgconfig/sensor.pc -%license LICENSE.APLv2 %files -n sensor-hal-devel -%defattr(-,root,root,-) -%{_includedir}/sensor/sensor_hal.h -%{_includedir}/sensor/sensor_hal_types.h -%license LICENSE.APLv2 +%manifest packaging/sensord.manifest +%{_includedir}/sensor/sensor_hal*.h %files -n sensor-test -%defattr(-,root,root,-) %{_bindir}/sensorctl + +%files -n libsensord-devel %license LICENSE.APLv2 diff --git a/src/client-dummy/CMakeLists.txt b/src/client-dummy/CMakeLists.txt new file mode 100644 index 0000000..b0f48c2 --- /dev/null +++ b/src/client-dummy/CMakeLists.txt @@ -0,0 +1,39 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +PROJECT(sensor CXX) + +SET(DEPENDENTS "glib-2.0 dlog") +SET(VERSION ${FULLVER}) +SET(PREFIX ${CMAKE_INSTALL_PREFIX}) + +SET(PC_NAME ${PROJECT_NAME}) +SET(PC_DESCRIPTION "Sensor Client library") +SET(PC_INCLUDEDIR "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}/sensor") +SET(PC_REQUIRES "${DEPENDENTS}") +SET(PC_LIBDIR "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}") +SET(PC_LDFLAGS "-l${PROJECT_NAME}") + +INCLUDE(FindPkgConfig) +PKG_CHECK_MODULES(DUMMY_PKGS REQUIRED ${DEPENDENTS}) + +FOREACH(flag ${DUMMY_PKGS_CFLAGS}) + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}") +ENDFOREACH(flag) + +SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden -fvisibility-inlines-hidden -g -fPIC") + +INCLUDE_DIRECTORIES( + ${CMAKE_SOURCE_DIR}/src/shared + ${CMAKE_CURRENT_SOURCE_DIR} +) + +FILE(GLOB_RECURSE SRCS *.cpp) + +ADD_LIBRARY(${PROJECT_NAME} SHARED ${SRCS}) + +TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${DUMMY_PKGS_LDFLAGS}) +SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES SOVERSION ${MAJORVER}) +SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES VERSION ${FULLVER}) + +CONFIGURE_FILE(${PROJECT_NAME}.pc.in ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}.pc @ONLY) +INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT RuntimeLibraries) +INSTALL(FILES ${PROJECT_NAME}.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig) diff --git a/src/client-dummy/client_deprecated.cpp b/src/client-dummy/client_deprecated.cpp new file mode 100644 index 0000000..e721bed --- /dev/null +++ b/src/client-dummy/client_deprecated.cpp @@ -0,0 +1,84 @@ +/* + * sensord + * + * 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 + +#ifndef API +#define API __attribute__((visibility("default"))) +#endif + +API int sf_connect(sensor_type_t sensor_type) +{ + return OP_ERROR; +} + +API int sf_disconnect(int handle) +{ + return OP_ERROR; +} + +API int sf_start(int handle, int option) +{ + return OP_ERROR; +} + +API int sf_stop(int handle) +{ + return OP_ERROR; +} + +API int sf_register_event(int handle, unsigned int event_type, event_condition_t *event_condition, sensor_callback_func_t cb, void *user_data) +{ + return OP_ERROR; +} + +API int sf_unregister_event(int handle, unsigned int event_type) +{ + return OP_ERROR; +} + +API int sf_change_event_condition(int handle, unsigned int event_type, event_condition_t *event_condition) +{ + return OP_ERROR; +} + +API int sf_change_sensor_option(int handle, int option) +{ + return OP_ERROR; +} + +API int sf_send_sensorhub_data(int handle, const char* data, int data_len) +{ + return OP_ERROR; +} + +API int sf_get_data(int handle, unsigned int data_id, sensor_data_t* sensor_data) +{ + return OP_ERROR; +} + +API int sf_check_rotation(unsigned long *rotation) +{ + return OP_ERROR; +} + +API int sf_is_sensor_event_available(sensor_type_t sensor_type, unsigned int event_type) +{ + return OP_ERROR; +} diff --git a/src/client/client_dummy.cpp b/src/client-dummy/client_dummy.cpp similarity index 98% rename from src/client/client_dummy.cpp rename to src/client-dummy/client_dummy.cpp index c7732a6..574079a 100644 --- a/src/client/client_dummy.cpp +++ b/src/client-dummy/client_dummy.cpp @@ -20,8 +20,8 @@ #include #include -#include "sensor_internal.h" -#include "sensor_internal_deprecated.h" +#include +#include API int sensord_get_sensors(sensor_type_t type, sensor_t **list, int *sensor_count) { diff --git a/src/client/external_client_dummy.cpp b/src/client-dummy/external_client_dummy.cpp similarity index 93% rename from src/client/external_client_dummy.cpp rename to src/client-dummy/external_client_dummy.cpp index 8f359d0..8eb9221 100644 --- a/src/client/external_client_dummy.cpp +++ b/src/client-dummy/external_client_dummy.cpp @@ -20,8 +20,8 @@ #include #include -#include "sensor_internal.h" -#include "sensor_internal_deprecated.h" +#include +#include API int sensord_external_connect(const char *key, sensor_external_command_cb_t cb, void *user_data) { diff --git a/src/client/sensor.pc.in b/src/client-dummy/sensor.pc.in similarity index 100% rename from src/client/sensor.pc.in rename to src/client-dummy/sensor.pc.in diff --git a/src/client/CMakeLists.txt b/src/client/CMakeLists.txt index 33ed7fa..9131d8d 100644 --- a/src/client/CMakeLists.txt +++ b/src/client/CMakeLists.txt @@ -1,60 +1,28 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.6) -PROJECT(sensor CXX) +PROJECT(sensor-genuine CXX) SET(DEPENDENTS "vconf glib-2.0 gio-2.0 dlog") -SET(VERSION ${FULLVER}) -SET(PREFIX ${CMAKE_INSTALL_PREFIX}) -SET(EXEC_PREFIX "${CMAKE_INSTALL_PREFIX}/bin") - -SET(PC_NAME ${PROJECT_NAME}) -SET(PC_DESCRIPTION "Sensor Client library") -SET(PC_INCLUDEDIR "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}/sensor") -SET(PC_REQUIRES "${DEPENDENTS}") -SET(PC_LIBDIR "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}") -SET(PC_LDFLAGS "-l${PROJECT_NAME}") INCLUDE(FindPkgConfig) -PKG_CHECK_MODULES(PKGS REQUIRED ${DEPENDENTS}) +PKG_CHECK_MODULES(CLIENT_PKGS REQUIRED ${DEPENDENTS}) -FOREACH(flag ${PKGS_CFLAGS}) +FOREACH(flag ${CLIENT_PKGS_CFLAGS}) SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}") ENDFOREACH(flag) SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden -fvisibility-inlines-hidden -g -fPIC") -SET (INSTALL_HEADERS - sensor_internal.h - sensor_internal_deprecated.h -) - INCLUDE_DIRECTORIES( - ${CMAKE_SOURCE_DIR}/src/hal ${CMAKE_SOURCE_DIR}/src/shared ${CMAKE_CURRENT_SOURCE_DIR} ) FILE(GLOB_RECURSE SRCS *.cpp) -IF("${PROFILE}" STREQUAL "tv") - LIST(REMOVE_ITEM SRCS - "${CMAKE_CURRENT_SOURCE_DIR}/client.cpp" - "${CMAKE_CURRENT_SOURCE_DIR}/external_client.cpp") -ELSE() - LIST(REMOVE_ITEM SRCS - "${CMAKE_CURRENT_SOURCE_DIR}/client_dummy.cpp" - "${CMAKE_CURRENT_SOURCE_DIR}/external_client_dummy.cpp") -ENDIF() - ADD_LIBRARY(${PROJECT_NAME} SHARED ${SRCS}) -TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${PKGS_LDFLAGS} "sensord-shared") +TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${CLIENT_PKGS_LDFLAGS} "sensord-shared") SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES SOVERSION ${MAJORVER}) SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES VERSION ${FULLVER}) -CONFIGURE_FILE(${PROJECT_NAME}.pc.in ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}.pc @ONLY) -INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT RuntimeLibraries) -FOREACH(HEADER IN ITEMS ${INSTALL_HEADERS}) - INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${HEADER} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/sensor) -ENDFOREACH() -INSTALL(FILES ${PROJECT_NAME}.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig) - +INSTALL(TARGETS ${PROJECT_NAME} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} NAMELINK_SKIP) diff --git a/src/hal/CMakeLists.txt b/src/hal/CMakeLists.txt deleted file mode 100644 index 7119639..0000000 --- a/src/hal/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -CMAKE_MINIMUM_REQUIRED(VERSION 2.6) -PROJECT(sensor-hal CXX) - -INSTALL(FILES sensor_hal.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/sensor/) -INSTALL(FILES sensor_hal_types.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/sensor/) diff --git a/src/sensorctl/CMakeLists.txt b/src/sensorctl/CMakeLists.txt index 1accf2a..2b9c675 100644 --- a/src/sensorctl/CMakeLists.txt +++ b/src/sensorctl/CMakeLists.txt @@ -25,4 +25,4 @@ FILE(GLOB_RECURSE SRCS *.cpp) ADD_EXECUTABLE(${PROJECT_NAME} ${SRCS}) SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES LINKER_LANGUAGE CXX) TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${pkgs_LDFLAGS} sensor) -INSTALL(TARGETS ${PROJECT_NAME} DESTINATION /usr/bin/) +INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR}) diff --git a/src/shared/CMakeLists.txt b/src/shared/CMakeLists.txt index 5d094ec..69b88ed 100644 --- a/src/shared/CMakeLists.txt +++ b/src/shared/CMakeLists.txt @@ -11,13 +11,6 @@ FOREACH(flag ${SHARED_PKGS_CFLAGS}) ENDFOREACH(flag) SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") -SET (INSTALL_HEADERS - sensor_types.h - sensor_common.h - sensor_deprecated.h - enum_factory.h -) - INCLUDE_DIRECTORIES( ${CMAKE_SOURCE_DIR}/src/hal ${CMAKE_CURRENT_SOURCE_DIR} @@ -29,7 +22,3 @@ ADD_LIBRARY(${PROJECT_NAME} SHARED ${SRCS}) TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${SHARED_PKGS_LDFLAGS}) INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_LIBDIR}) - -FOREACH(HEADER IN ITEMS ${INSTALL_HEADERS}) - INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${HEADER} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/sensor) -ENDFOREACH() diff --git a/src/shared/sensor_types.cpp b/src/shared/sensor_types.cpp index 084e6b8..037ddc9 100644 --- a/src/shared/sensor_types.cpp +++ b/src/shared/sensor_types.cpp @@ -17,6 +17,6 @@ * */ -#include "sensor_types.h" +#include DECLARE_SENSOR_ENUM_UTIL(sensor_type_t, SENSOR_TYPE) -- 2.7.4 From 12aa46db5eb55230e7bc2fc75817dd5984f14a1f Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Tue, 3 Jan 2017 16:32:08 +0900 Subject: [PATCH 13/16] Add vconf & gio-2.0 to the package config This is for backward compatibility. A package uses vconf, which is indirectly required by sensord-devel. Change-Id: I45f04a02f72323637bb026f5449cf2afc147e30d Signed-off-by: Mu-Woong Lee --- src/client-dummy/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client-dummy/CMakeLists.txt b/src/client-dummy/CMakeLists.txt index b0f48c2..626d61e 100644 --- a/src/client-dummy/CMakeLists.txt +++ b/src/client-dummy/CMakeLists.txt @@ -1,7 +1,7 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.6) PROJECT(sensor CXX) -SET(DEPENDENTS "glib-2.0 dlog") +SET(DEPENDENTS "glib-2.0 gio-2.0 dlog vconf") SET(VERSION ${FULLVER}) SET(PREFIX ${CMAKE_INSTALL_PREFIX}) -- 2.7.4 From 0e23fe9a349204d683c182c8af87376fb40dee36 Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Tue, 3 Jan 2017 18:42:50 +0900 Subject: [PATCH 14/16] Add dummy libsensord package to fix build unresolvable issues Change-Id: I33c0c0c4ce4cf739587ca7363970a376e360b4bf Signed-off-by: Mu-Woong Lee --- packaging/libsensord.manifest | 5 ----- packaging/sensord.spec | 14 ++++++++++++-- 2 files changed, 12 insertions(+), 7 deletions(-) delete mode 100644 packaging/libsensord.manifest diff --git a/packaging/libsensord.manifest b/packaging/libsensord.manifest deleted file mode 100644 index 75b0fa5..0000000 --- a/packaging/libsensord.manifest +++ /dev/null @@ -1,5 +0,0 @@ - - - - - diff --git a/packaging/sensord.spec b/packaging/sensord.spec index e0e45ec..213cb52 100644 --- a/packaging/sensord.spec +++ b/packaging/sensord.spec @@ -21,7 +21,6 @@ BuildRequires: pkgconfig(cynara-session) Provides: %{name}-profile_tv = %{version}-%{release} # For backward compatibility -Provides: libsensord = %{version}-%{release} %description Sensor daemon @@ -62,7 +61,15 @@ Group: System/Testing %description -n sensor-test Sensor functional testing -# This dummy package will be removed later. +# These dummy packages will be removed later. +%package -n libsensord +Summary: Dummy package for backward compatibility +Requires: sensord + +%description -n libsensord +Without this dummy package, obs may reports several 'unresolvable' issues. +This is a temporal solution to handle such cases. + %package -n libsensord-devel Summary: Dummy package for backward compatibility Requires: sensord-devel @@ -140,5 +147,8 @@ echo "You need to reinstall %{name}, if you need to keep using the APIs after ui %files -n sensor-test %{_bindir}/sensorctl +%files -n libsensord +%license LICENSE.APLv2 + %files -n libsensord-devel %license LICENSE.APLv2 -- 2.7.4 From a10b751195b8114c7b5600240bb816df8438a0cf Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Mon, 19 Dec 2016 22:52:37 +0900 Subject: [PATCH 15/16] sensord: refactoring sensorctl for testing modules - To refactoring, there was the following changes. * add test_bench to create testcases easily * clean tester/injector/info/loopback files - testing command examples $ sensorctl test accelerometer /* test accelerometer sensor manually */ $ sensorctl test auto all /* test all testcases automatically with full-log */ $ sensorctl test auto socket 0 /* test socket module automatically without full-log */ $ sensorctl info all $ sensorctl inject Change-Id: Ie888c81f85d282eab77a8e1227d26710a85e9c61 Signed-off-by: kibak.yoon --- include/sensor_hal_types.h | 1 + src/sensorctl/CMakeLists.txt | 9 +- src/sensorctl/dbus_util.cpp | 12 +- src/sensorctl/dbus_util.h | 6 +- src/sensorctl/{info_manager.cpp => info.cpp} | 53 +++-- src/sensorctl/{info_manager.h => info.h} | 8 +- src/sensorctl/injector.cpp | 108 ++++++++++ src/sensorctl/injector.h | 51 ++++- src/sensorctl/injector_context_orientation.cpp | 37 ++-- src/sensorctl/injector_manager.cpp | 130 ------------ src/sensorctl/injector_manager.h | 58 ------ src/sensorctl/injector_wrist_up_conf.cpp | 59 ------ ...wrist_up_algo.cpp => injector_wristup_algo.cpp} | 45 ++-- src/sensorctl/injector_wristup_conf.cpp | 65 ++++++ src/sensorctl/log.h | 85 ++++++++ .../{loopback_manager.cpp => loopback.cpp} | 19 +- src/sensorctl/{loopback_manager.h => loopback.h} | 4 +- .../{injector_wrist_up_conf.h => macro.h} | 17 +- src/sensorctl/mainloop.cpp | 69 +++++++ .../{injector_context_orientation.h => mainloop.h} | 20 +- src/sensorctl/sensor_adapter.cpp | 160 +++++++++++++++ src/sensorctl/sensor_adapter.h | 62 ++++++ src/sensorctl/sensor_manager.cpp | 133 ++++++------ src/sensorctl/sensor_manager.h | 15 +- src/sensorctl/sensorctl.cpp | 78 +++---- src/sensorctl/sensorctl_log.h | 49 ----- src/sensorctl/test_bench.cpp | 226 +++++++++++++++++++++ src/sensorctl/test_bench.h | 201 ++++++++++++++++++ src/sensorctl/testcase/accelerometer.cpp | 170 ++++++++++++++++ src/sensorctl/testcase/sensor_basic.cpp | 168 +++++++++++++++ .../sensor_interval.cpp} | 21 +- src/sensorctl/tester.cpp | 188 +++++++++++++++++ src/sensorctl/tester.h | 25 ++- src/sensorctl/tester_manager.cpp | 79 ------- src/sensorctl/tester_sensor.cpp | 151 -------------- src/sensorctl/tester_sensor.h | 34 ---- src/sensorctl/util.cpp | 59 ++++++ src/sensorctl/{tester_manager.h => util.h} | 16 +- 38 files changed, 1885 insertions(+), 806 deletions(-) rename src/sensorctl/{info_manager.cpp => info.cpp} (63%) rename src/sensorctl/{info_manager.h => info.h} (81%) create mode 100644 src/sensorctl/injector.cpp delete mode 100644 src/sensorctl/injector_manager.cpp delete mode 100644 src/sensorctl/injector_manager.h delete mode 100644 src/sensorctl/injector_wrist_up_conf.cpp rename src/sensorctl/{injector_wrist_up_algo.cpp => injector_wristup_algo.cpp} (59%) create mode 100644 src/sensorctl/injector_wristup_conf.cpp create mode 100644 src/sensorctl/log.h rename src/sensorctl/{loopback_manager.cpp => loopback.cpp} (83%) rename src/sensorctl/{loopback_manager.h => loopback.h} (91%) rename src/sensorctl/{injector_wrist_up_conf.h => macro.h} (64%) create mode 100644 src/sensorctl/mainloop.cpp rename src/sensorctl/{injector_context_orientation.h => mainloop.h} (70%) create mode 100644 src/sensorctl/sensor_adapter.cpp create mode 100644 src/sensorctl/sensor_adapter.h delete mode 100644 src/sensorctl/sensorctl_log.h create mode 100644 src/sensorctl/test_bench.cpp create mode 100644 src/sensorctl/test_bench.h create mode 100644 src/sensorctl/testcase/accelerometer.cpp create mode 100644 src/sensorctl/testcase/sensor_basic.cpp rename src/sensorctl/{injector_wrist_up_algo.h => testcase/sensor_interval.cpp} (62%) create mode 100644 src/sensorctl/tester.cpp delete mode 100644 src/sensorctl/tester_manager.cpp delete mode 100644 src/sensorctl/tester_sensor.cpp delete mode 100644 src/sensorctl/tester_sensor.h create mode 100644 src/sensorctl/util.cpp rename src/sensorctl/{tester_manager.h => util.h} (67%) diff --git a/include/sensor_hal_types.h b/include/sensor_hal_types.h index 6beb935..6ad991d 100644 --- a/include/sensor_hal_types.h +++ b/include/sensor_hal_types.h @@ -223,6 +223,7 @@ typedef struct { #define CONVERT_TYPE_ATTR(type, index) ((type) << 8 | 0x80 | (index)) enum sensor_attribute { + SENSOR_ATTR_ACCELEROMETER_INJECTION = CONVERT_TYPE_ATTR(SENSOR_DEVICE_ACCELEROMETER, 0xFF), SENSOR_ATTR_ACTIVITY = CONVERT_TYPE_ATTR(SENSOR_DEVICE_ACTIVITY_TRACKER, 0x1), SENSOR_ATTR_PEDOMETER_HEIGHT = CONVERT_TYPE_ATTR(SENSOR_DEVICE_HUMAN_PEDOMETER, 0x1), diff --git a/src/sensorctl/CMakeLists.txt b/src/sensorctl/CMakeLists.txt index 2b9c675..7b44303 100644 --- a/src/sensorctl/CMakeLists.txt +++ b/src/sensorctl/CMakeLists.txt @@ -2,19 +2,20 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.6) PROJECT(sensorctl CXX) INCLUDE(GNUInstallDirs) -SET(DEPS glib-2.0 gio-2.0) +SET(DEPENDENTS glib-2.0 gio-2.0) INCLUDE(FindPkgConfig) -pkg_check_modules(pkgs REQUIRED ${DEPS}) +PKG_CHECK_MODULES(PKGS REQUIRED ${DEPENDENTS}) INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_SOURCE_DIR}/src/ ${CMAKE_SOURCE_DIR}/src/hal/ ${CMAKE_SOURCE_DIR}/src/client/ ${CMAKE_SOURCE_DIR}/src/shared/ ) -FOREACH(flag ${pkgs_CFLAGS}) +FOREACH(flag ${PKGS_CFLAGS}) SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}") ENDFOREACH(flag) @@ -24,5 +25,5 @@ SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_CFLAGS}") FILE(GLOB_RECURSE SRCS *.cpp) ADD_EXECUTABLE(${PROJECT_NAME} ${SRCS}) SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES LINKER_LANGUAGE CXX) -TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${pkgs_LDFLAGS} sensor) +TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${PKGS_LDFLAGS} sensor sensord-shared) INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR}) diff --git a/src/sensorctl/dbus_util.cpp b/src/sensorctl/dbus_util.cpp index 886872e..a18d8c0 100644 --- a/src/sensorctl/dbus_util.cpp +++ b/src/sensorctl/dbus_util.cpp @@ -17,10 +17,13 @@ * */ +#include "dbus_util.h" + #include #include #include -#include + +#include "log.h" static GDBusConnection *connection; @@ -39,7 +42,7 @@ bool dbus_init(void) gaddr = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, &error); if (!gaddr) { - PRINT("ERROR: Failed to get dbus address : %s", error->message); + _E("Failed to get dbus address : %s\n", error->message); g_error_free(error); error = NULL; return false; @@ -52,13 +55,13 @@ bool dbus_init(void) g_free(gaddr); if (!connection) { - PRINT("ERROR: Failed to get dbus connection : %s", error->message); + _E("Failed to get dbus connection : %s\n", error->message); g_error_free(error); error = NULL; return false; } - PRINT("G-DBUS connected[%s]\n", + _I("G-DBUS connected[%s]\n", g_dbus_connection_get_unique_name(connection)); return true; } @@ -104,4 +107,3 @@ GVariant *make_variant_int(int count, char *options[]) return NULL; } - diff --git a/src/sensorctl/dbus_util.h b/src/sensorctl/dbus_util.h index a51d903..e03166a 100644 --- a/src/sensorctl/dbus_util.h +++ b/src/sensorctl/dbus_util.h @@ -17,11 +17,15 @@ * */ -#pragma once // _DBUS_UTIL_H_ +#pragma once /* __DBUS_UTIL_H__ */ #include #include +#define SENSORD_BUS_NAME "org.tizen.system.sensord" +#define SENSORD_OBJ_PATH "/Org/Tizen/System/SensorD" +#define SENSORD_INTERFACE_NAME "org.tizen.system.sensord" + bool dbus_init(void); bool dbus_fini(void); bool dbus_emit_signal(gchar *dest_bus_name, gchar *object_path, diff --git a/src/sensorctl/info_manager.cpp b/src/sensorctl/info.cpp similarity index 63% rename from src/sensorctl/info_manager.cpp rename to src/sensorctl/info.cpp index 676bc40..6597230 100644 --- a/src/sensorctl/info_manager.cpp +++ b/src/sensorctl/info.cpp @@ -1,7 +1,7 @@ /* * sensorctl * - * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * 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. @@ -17,37 +17,38 @@ * */ +#include "info.h" + #include -#include #include #include -#include -#include "info_manager.h" -bool info_manager::process(int argc, char *argv[]) +#include "log.h" + +#define INFO_ARGC 3 /* e.g. {sensorctl, info, accelerometer} */ + +bool info_manager::run(int argc, char *argv[]) { sensor_type_t type; + sensor_t *sensors; + int count; - if (argc == 2) { + if (argc < INFO_ARGC) { usage(); return false; } type = get_sensor_type(argv[2]); - if (type == UNKNOWN_SENSOR) - return false; - - sensor_t *sensors; - int count; + RETVM_IF(type == UNKNOWN_SENSOR, false, "Wrong argument : %s\n", argv[2]); sensord_get_sensor_list(type, &sensors, &count); - sensor_info(sensors, count); + show_info(sensors, count); - free(sensors); + delete sensors; return true; } -void info_manager::sensor_info(sensor_t *sensors, int count) +void info_manager::show_info(sensor_t *sensors, int count) { sensor_t sensor; char *vendor; @@ -71,24 +72,22 @@ void info_manager::sensor_info(sensor_t *sensors, int count) sensord_get_fifo_count(sensor, &fifo_count); sensord_get_max_batch_count(sensor, &max_batch_count); - PRINT("-------sensor[%d] information-------\n", i); - PRINT("vendor : %s\n", vendor); - PRINT("name : %s\n", name); - PRINT("min_range : %f\n", min_range); - PRINT("max_range : %f\n", max_range); - PRINT("resolution : %f\n", resolution); - PRINT("min_interval : %d\n", min_interval); - PRINT("fifo_count : %d\n", fifo_count); - PRINT("max_batch_count : %d\n", max_batch_count); - PRINT("--------------------------------\n"); + _N("-------sensor[%d] information-------\n", i); + _N("vendor : %s\n", vendor); + _N("name : %s\n", name); + _N("min_range : %f\n", min_range); + _N("max_range : %f\n", max_range); + _N("resolution : %f\n", resolution); + _N("min_interval : %d\n", min_interval); + _N("fifo_count : %d\n", fifo_count); + _N("max_batch_count : %d\n", max_batch_count); + _N("--------------------------------\n"); } } void info_manager::usage(void) { - PRINT("usage: sensorctl info \n"); - PRINT("\n"); + _N("usage: sensorctl info \n\n"); usage_sensors(); } - diff --git a/src/sensorctl/info_manager.h b/src/sensorctl/info.h similarity index 81% rename from src/sensorctl/info_manager.h rename to src/sensorctl/info.h index b71dfc0..b85b6ca 100644 --- a/src/sensorctl/info_manager.h +++ b/src/sensorctl/info.h @@ -1,7 +1,7 @@ /* * sensorctl * - * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * 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. @@ -17,7 +17,7 @@ * */ -#pragma once // _INFO_MANAGER_H_ +#pragma once /* __INFO_MANAGER_H__ */ #include #include "sensor_manager.h" @@ -27,8 +27,8 @@ public: info_manager() {} virtual ~info_manager() {} - bool process(int argc, char *argv[]); + bool run(int argc, char *argv[]); private: - void sensor_info(sensor_t *sensors, int count); + void show_info(sensor_t *sensors, int count); void usage(void); }; diff --git a/src/sensorctl/injector.cpp b/src/sensorctl/injector.cpp new file mode 100644 index 0000000..a4e7b75 --- /dev/null +++ b/src/sensorctl/injector.cpp @@ -0,0 +1,108 @@ +/* + * sensorctl + * + * 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 "injector.h" + +#include +#include +#include + +#include "log.h" +#include "dbus_util.h" + +std::vector injector_manager::injectors; + +injector::injector(sensor_type_t sensor_type, const char *event_name) +: m_type(sensor_type) +, m_name(event_name) +{ + injector_manager::register_injector(this); +} + +injector_manager::injector_manager() +{ + if (!dbus_init()) { + _E("Failed to init dbus"); + throw; + } +} + +injector_manager::~injector_manager() +{ + dbus_fini(); +} + +void injector_manager::register_injector(injector *inject) +{ + injectors.push_back(inject); +} + +bool injector_manager::run(int argc, char *argv[]) +{ + sensor_type_t type; + bool result; + + if (argc < INJECTOR_ARGC) { + usage(); + return false; + } + + /* 1. get sensor type */ + type = get_sensor_type(argv[2]); + RETVM_IF(type == UNKNOWN_SENSOR, false, "Invalid argument : %s\n", argv[2]); + + /* 2. set up injector */ + injector *_injector = get_injector(type, argv[3]); + RETVM_IF(!_injector, false, "Cannot find matched injector\n"); + + /* 3. set up injector */ + result = _injector->setup(); + RETVM_IF(!result, false, "Failed to init injector\n"); + + /* 4. inject event */ + result = _injector->inject(argc, argv); + RETVM_IF(!result, false, "Failed to run injector\n"); + + /* 5. tear down injector */ + result = _injector->teardown(); + RETVM_IF(!result, false, "Failed to tear down injector\n"); + + return true; +} + +injector *injector_manager::get_injector(sensor_type_t type, const char *name) +{ + int count; + + count = injectors.size(); + + for (int i = 0; i < count; ++i) { + if (type == injectors[i]->type() && + (injectors[i]->name() == name)) + return injectors[i]; + } + return NULL; +} + +void injector_manager::usage(void) +{ + _N("usage: sensorctl inject [] []\n\n"); + + usage_sensors(); +} diff --git a/src/sensorctl/injector.h b/src/sensorctl/injector.h index f9e8b40..ca942fc 100644 --- a/src/sensorctl/injector.h +++ b/src/sensorctl/injector.h @@ -1,7 +1,7 @@ /* * sensorctl * - * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * 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. @@ -17,16 +17,51 @@ * */ -#pragma once // _INJECTOR_H_ +#pragma once /* __INJECTOR_H__ */ #include +#include +#include -#define SENSORD_BUS_NAME "org.tizen.system.sensord" -#define SENSORD_OBJ_PATH "/Org/Tizen/System/SensorD" -#define SENSORD_INTERFACE_NAME "org.tizen.system.sensord" +#include "sensor_manager.h" + +#define NAME_MAX_TEST 32 +#define INJECTOR_ARGC 4 /* e.g. {sensorctl, inject, wristup, conf} */ + +#define REGISTER_INJECTOR(sensor_type, event_name, injector_type) \ +static injector_type injector(sensor_type, event_name); + +class injector { +public: + injector(sensor_type_t sensor_type, const char *event_name); + virtual ~injector() {} + + virtual bool setup(void) { return true; } + virtual bool teardown(void) { return true; } + + const std::string& name() const { return m_name; } + const sensor_type_t type() const { return m_type; } + + virtual bool inject(int argc, char *argv[]) = 0; + +private: + sensor_type_t m_type; + std::string m_name; +}; + +class injector_manager : public sensor_manager { +public: + static void register_injector(injector *injector); -class injector_interface { public: - virtual bool init(void) { return true; } - virtual bool inject(int option_count, char *options[]) = 0; + injector_manager(); + virtual ~injector_manager(); + + bool run(int argc, char *argv[]); + +private: + static std::vector injectors; + + injector *get_injector(sensor_type_t type, const char *name); + void usage(void); }; diff --git a/src/sensorctl/injector_context_orientation.cpp b/src/sensorctl/injector_context_orientation.cpp index 863b901..7219ea9 100644 --- a/src/sensorctl/injector_context_orientation.cpp +++ b/src/sensorctl/injector_context_orientation.cpp @@ -20,26 +20,33 @@ #include #include #include -#include +#include "log.h" #include "dbus_util.h" -#include "injector_manager.h" -#include "injector_context_orientation.h" +#include "injector.h" #define CONTEXT_ORIENTATION_SIGNAL "orientation" -bool injector_context_orientation::inject(int option_count, char *options[]) +class injector_context_orientation : public injector { +public: + injector_context_orientation(sensor_type_t sensor_type, const char *event_name); + virtual ~injector_context_orientation() {} + + bool inject(int argc, char *argv[]); +}; + +injector_context_orientation::injector_context_orientation(sensor_type_t sensor_type, const char *event_name) +: injector(sensor_type, event_name) { - GVariant *variant; +} - if (option_count == 0) { - _E("ERROR: invalid argument\n"); - return false; - } +bool injector_context_orientation::inject(int argc, char *argv[]) +{ + GVariant *variant; - variant = make_variant_int(option_count, options); + RETVM_IF(argc <= INJECTOR_ARGC, false, "Invalid argument\n"); - if (variant == NULL) - return false; + variant = make_variant_int(argc - INJECTOR_ARGC, &argv[INJECTOR_ARGC]); + RETVM_IF(!variant, false, "Cannot make variant\n"); dbus_emit_signal(NULL, (gchar *)SENSORD_OBJ_PATH, @@ -48,9 +55,9 @@ bool injector_context_orientation::inject(int option_count, char *options[]) variant, NULL); - PRINT("set options to context: \n"); - for (int i = 0; i < option_count; ++i) - PRINT("option %d: %s\n", i, options[i]); + _I("Set up options to wristup:"); + for (int i = 0; i < argc - INJECTOR_ARGC; ++i) + _I("option %d: %s\n", i, argv[i]); return true; } diff --git a/src/sensorctl/injector_manager.cpp b/src/sensorctl/injector_manager.cpp deleted file mode 100644 index 8aeebb0..0000000 --- a/src/sensorctl/injector_manager.cpp +++ /dev/null @@ -1,130 +0,0 @@ -/* - * sensorctl - * - * Copyright (c) 2015 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 -#include -#include -#include -#include "dbus_util.h" -#include "injector.h" -#include "injector_manager.h" - -#define NAME_MAX_TEST 32 -#define ARGC_BASE 4 /* e.g. {sensorctl, inject, wristup, conf} */ - -static std::vector injector_infos; - -injector_manager::injector_manager() -{ - if (!dbus_init()) { - _E("Failed to init dbus"); - throw; - } -} - -injector_manager::~injector_manager() -{ - dbus_fini(); -} - -bool injector_manager::register_injector(injector_info info) -{ - injector_infos.push_back(info); - return true; -} - -injector_interface *injector_manager::get_injector(sensor_type_t type, const char *name) -{ - int injector_count; - injector_count = injector_infos.size(); - - for (int i = 0; i < injector_count; ++i) { - if (type == injector_infos[i].type && - !strcmp(injector_infos[i].name, name)) - return injector_infos[i].injector; - } - return NULL; -} - -bool injector_manager::process(int argc, char *argv[]) -{ - int option_count; - char *options[8]; - bool result; - sensor_type_t type; - char *event_name; - int i; - - if (argc < 4) { - usage(); - return false; - } - - /* 1. get sensor type */ - type = get_sensor_type(argv[2]); - if (type == UNKNOWN_SENSOR) { - _E("ERROR : failed to process injector\n"); - return false; - } - - /* 2. set up injector */ - event_name = argv[3]; - - injector_interface *injector = get_injector(type, event_name); - if (injector == NULL) { - _E("ERROR: cannot find matched injector\n"); - return false; - } - - /* 3. init injector */ - result = injector->init(); - if (!result) { - _E("ERROR: failed to init injector\n"); - return false; - } - - /* 4. inject event with options */ - option_count = argc - ARGC_BASE; - for (i = 0; i < option_count; ++i) { - options[i] = new char[NAME_MAX_TEST]; - strncpy(options[i], argv[ARGC_BASE + i], strlen(argv[ARGC_BASE + i])); - } - - result = injector->inject(option_count, options); - if (!result) { - _E("ERROR : failed to process injector\n"); - for (i = 0; i < option_count; ++i) - delete [] options[i]; - - return false; - } - - for (i = 0; i < option_count; ++i) - delete [] options[i]; - - return true; -} - -void injector_manager::usage(void) -{ - PRINT("usage: sensorctl inject [] []\n\n"); - - usage_sensors(); -} - diff --git a/src/sensorctl/injector_manager.h b/src/sensorctl/injector_manager.h deleted file mode 100644 index 380c486..0000000 --- a/src/sensorctl/injector_manager.h +++ /dev/null @@ -1,58 +0,0 @@ -/* - * sensorctl - * - * Copyright (c) 2015 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. - * - */ - -#pragma once // _INJECT_MANAGER_H_ - -#include -#include -#include "injector.h" -#include "sensor_manager.h" - -#define REGISTER_INJECTOR(sensor_type, sensor_name, injector_type) \ -static void __attribute__((constructor)) reg_injector(void) \ -{ \ - struct injector_info info; \ - info.type = (sensor_type); \ - info.name = (sensor_name); \ - info.injector = new(std::nothrow) (injector_type)(); \ - if (!info.injector) { \ - _E("ERROR: Failed to allocate memory(%s)", #injector_type); \ - return; \ - } \ - injector_manager::register_injector(info); \ -} - -struct injector_info { - sensor_type_t type; - const char *name; - injector_interface *injector; -}; - -class injector_manager : public sensor_manager { -public: - injector_manager(); - virtual ~injector_manager(); - - bool process(int argc, char *argv[]); - - static bool register_injector(injector_info info); -private: - injector_interface *get_injector(sensor_type_t type, const char *name); - void usage(void); -}; diff --git a/src/sensorctl/injector_wrist_up_conf.cpp b/src/sensorctl/injector_wrist_up_conf.cpp deleted file mode 100644 index 11e7a2f..0000000 --- a/src/sensorctl/injector_wrist_up_conf.cpp +++ /dev/null @@ -1,59 +0,0 @@ -/* - * sensorctl - * - * Copyright (c) 2015 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 -#include -#include -#include -#include "dbus_util.h" -#include "injector_manager.h" -#include "injector_wrist_up_conf.h" - -#define WRIST_UP_CONF_SIGNAL "conf" - -bool injector_wrist_up_conf::inject(int option_count, char *options[]) -{ - GVariant *variant; - - if (option_count == 0) { - _E("ERROR: invalid argument\n"); - return false; - } - - variant = make_variant_int(option_count, options); - - if (variant == NULL) - return false; - - dbus_emit_signal(NULL, - (gchar *)SENSORD_OBJ_PATH, - (gchar *)SENSORD_INTERFACE_NAME, - (gchar *)WRIST_UP_CONF_SIGNAL, - variant, - NULL); - - PRINT("set options to wristup: \n"); - for (int i = 0; i < option_count; ++i) - PRINT("option %d: %s\n", i, options[i]); - - return true; -} - -REGISTER_INJECTOR(MOTION_SENSOR, "conf", injector_wrist_up_conf) - diff --git a/src/sensorctl/injector_wrist_up_algo.cpp b/src/sensorctl/injector_wristup_algo.cpp similarity index 59% rename from src/sensorctl/injector_wrist_up_algo.cpp rename to src/sensorctl/injector_wristup_algo.cpp index 766e6ec..2585bd8 100644 --- a/src/sensorctl/injector_wrist_up_algo.cpp +++ b/src/sensorctl/injector_wristup_algo.cpp @@ -23,18 +23,32 @@ #include #include #include -#include + +#include "log.h" #include "dbus_util.h" -#include "injector_manager.h" -#include "injector_wrist_up_algo.h" +#include "injector.h" -#define WRIST_UP_ALGO_SIGNAL "algo" +#define WRISTUP_ALGO_SIGNAL "algo" #define OPTION_INDEX 0 typedef std::map option_map_t; static option_map_t option_map; -bool injector_wrist_up_algo::init(void) +class injector_wristup_algo : public injector { +public: + injector_wristup_algo(sensor_type_t sensor_type, const char *event_name); + virtual ~injector_wristup_algo() {} + + bool setup(void); + bool inject(int argc, char *argv[]); +}; + +injector_wristup_algo::injector_wristup_algo(sensor_type_t sensor_type, const char *event_name) +: injector(sensor_type, event_name) +{ +} + +bool injector_wristup_algo::setup(void) { option_map.insert(option_map_t::value_type("auto", 0)); option_map.insert(option_map_t::value_type("green", 1)); @@ -44,34 +58,27 @@ bool injector_wrist_up_algo::init(void) return true; } -bool injector_wrist_up_algo::inject(int option_count, char *options[]) +bool injector_wristup_algo::inject(int argc, char *argv[]) { int option; - if (option_count == 0) { - _E("ERROR: invalid argument\n"); - return false; - } + RETVM_IF(argc == 0, false, "Invalid argument\n"); option_map_t::iterator it; - it = option_map.find(options[OPTION_INDEX]); - - if (it == option_map.end()) { - _E("ERROR: no matched-option: %s\n", options[OPTION_INDEX]); - return false; - } + it = option_map.find(argv[INJECTOR_ARGC]); + RETVM_IF(it == option_map.end(), false, "No matched option: %s\n", argv[INJECTOR_ARGC]); option = it->second; dbus_emit_signal(NULL, (gchar *)SENSORD_OBJ_PATH, (gchar *)SENSORD_INTERFACE_NAME, - (gchar *)WRIST_UP_ALGO_SIGNAL, + (gchar *)WRISTUP_ALGO_SIGNAL, g_variant_new("(i)", option), NULL); - _I("set [%s] mode to wristup (%d)", options[OPTION_INDEX], option); + _I("Set up [%s] mode to wristup (%d)\n", argv[INJECTOR_ARGC], option); return true; } -REGISTER_INJECTOR(MOTION_SENSOR, "algo", injector_wrist_up_algo) +REGISTER_INJECTOR(GESTURE_WRIST_UP_SENSOR, WRISTUP_ALGO_SIGNAL, injector_wristup_algo) diff --git a/src/sensorctl/injector_wristup_conf.cpp b/src/sensorctl/injector_wristup_conf.cpp new file mode 100644 index 0000000..5d356c5 --- /dev/null +++ b/src/sensorctl/injector_wristup_conf.cpp @@ -0,0 +1,65 @@ +/* + * sensorctl + * + * Copyright (c) 2015 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 +#include +#include +#include "log.h" +#include "dbus_util.h" +#include "injector.h" + +#define WRISTUP_CONF_SIGNAL "conf" + +class injector_wristup_conf : public injector { +public: + injector_wristup_conf(sensor_type_t sensor_type, const char *event_name); + virtual ~injector_wristup_conf() {} + + bool inject(int argc, char *argv[]); +}; + +injector_wristup_conf::injector_wristup_conf(sensor_type_t sensor_type, const char *event_name) +: injector(sensor_type, event_name) +{ +} + +bool injector_wristup_conf::inject(int argc, char *argv[]) +{ + GVariant *variant; + + RETVM_IF(argc <= INJECTOR_ARGC, false, "Invalid argument\n"); + + variant = make_variant_int(argc - INJECTOR_ARGC, &argv[INJECTOR_ARGC]); + RETVM_IF(!variant, false, "Cannot make variant\n"); + + dbus_emit_signal(NULL, + (gchar *)SENSORD_OBJ_PATH, + (gchar *)SENSORD_INTERFACE_NAME, + (gchar *)WRISTUP_CONF_SIGNAL, + variant, + NULL); + + _I("Set up options to wristup:"); + for (int i = 0; i < argc - INJECTOR_ARGC; ++i) + _I("option %d: %s\n", i, argv[i]); + + return true; +} + +REGISTER_INJECTOR(GESTURE_WRIST_UP_SENSOR, WRISTUP_CONF_SIGNAL, injector_wristup_conf) diff --git a/src/sensorctl/log.h b/src/sensorctl/log.h new file mode 100644 index 0000000..00cefbd --- /dev/null +++ b/src/sensorctl/log.h @@ -0,0 +1,85 @@ +/* + * sensorctl + * + * 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. + * + */ + +#pragma once /* __LOG_H__ */ + +#include +#include + +#define _NRM "\x1B[0m" +#define _RED "\x1B[31m" +#define _GRN "\x1B[32m" +#define _YEL "\x1B[33m" +#define _BLU "\x1B[34m" +#define _MAG "\x1B[35m" +#define _CYN "\x1B[36m" +#define _WHT "\x1B[37m" +#define _RST "\033[0m" + +#define _N(fmt, args...) \ +do { \ + g_print(fmt, ##args); \ +} while (0) + +#define _E(fmt, args...) \ +do { \ + g_print("\x1B[31m" fmt "\033[0m", ##args); \ +} while (0) + +#define _I(fmt, args...) \ +do { \ + g_print("\x1B[32m" fmt "\033[0m", ##args); \ +} while (0) + +#define WARN_IF(expr, fmt, arg...) \ +do { \ + if(expr) { \ + _E(fmt, ##arg); \ + } \ +} while (0) + +#define RET_IF(expr) \ +do { \ + if(expr) { \ + return; \ + } \ +} while (0) + +#define RETV_IF(expr, val) \ +do { \ + if(expr) { \ + return (val); \ + } \ +} while (0) + +#define RETM_IF(expr, fmt, arg...) \ +do { \ + if(expr) { \ + _E(fmt, ##arg); \ + return; \ + } \ +} while (0) + +#define RETVM_IF(expr, val, fmt, arg...) \ +do { \ + if(expr) { \ + _E(fmt, ##arg); \ + return (val); \ + } \ +} while (0) diff --git a/src/sensorctl/loopback_manager.cpp b/src/sensorctl/loopback.cpp similarity index 83% rename from src/sensorctl/loopback_manager.cpp rename to src/sensorctl/loopback.cpp index b7908b6..ee177b8 100644 --- a/src/sensorctl/loopback_manager.cpp +++ b/src/sensorctl/loopback.cpp @@ -1,7 +1,7 @@ /* * sensorctl * - * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * 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. @@ -21,8 +21,9 @@ #include #include #include -#include -#include "loopback_manager.h" + +#include "log.h" +#include "loopback.h" #define SHUB_INST_LIB_ADD ((char)-79) #define SHUB_INST_LIB_REMOVE ((char)-78) @@ -40,7 +41,7 @@ static void int_to_bytes(int32_t value, int length, char cmd[]) } } -bool loopback_manager::process(int argc, char *argv[]) +bool loopback_manager::run(int argc, char *argv[]) { if (argc < DEFAULT_COMMAND_SIZE || argc > MAX_COMMAND_SIZE) { usage(); @@ -88,10 +89,10 @@ bool loopback_manager::process(int argc, char *argv[]) void loopback_manager::usage(void) { - PRINT("usage: sensorctl loopback [start/stop] [14byte sensor char data]\n"); - PRINT("ex: sensorctl loopback start 15000 1 1 19 1 (after 15000ms, wrist up event)\n"); - PRINT("ex: sensorctl loopback stop\n"); - PRINT(" * if not enought 14byte, remain bytes are filled with 0\n"); - PRINT("\n"); + _N("usage: sensorctl loopback [start/stop] [14byte sensor char data]\n"); + _N("ex: sensorctl loopback start 15000 1 1 19 1 (after 15000ms, wrist up event)\n"); + _N("ex: sensorctl loopback stop\n"); + _N(" * if not enought 14byte, remain bytes are filled with 0\n"); + _N("\n"); } diff --git a/src/sensorctl/loopback_manager.h b/src/sensorctl/loopback.h similarity index 91% rename from src/sensorctl/loopback_manager.h rename to src/sensorctl/loopback.h index 2aa3297..3117e27 100644 --- a/src/sensorctl/loopback_manager.h +++ b/src/sensorctl/loopback.h @@ -17,7 +17,7 @@ * */ -#pragma once // _LOOPBACK_MANAGER_H_ +#pragma once /* __LOOPBACK_MANAGER_H__ */ #include #include "sensor_manager.h" @@ -27,7 +27,7 @@ public: loopback_manager() {} virtual ~loopback_manager() {} - bool process(int argc, char *argv[]); + bool run(int argc, char *argv[]); private: void usage(void); }; diff --git a/src/sensorctl/injector_wrist_up_conf.h b/src/sensorctl/macro.h similarity index 64% rename from src/sensorctl/injector_wrist_up_conf.h rename to src/sensorctl/macro.h index 93b75f4..4e624a5 100644 --- a/src/sensorctl/injector_wrist_up_conf.h +++ b/src/sensorctl/macro.h @@ -1,7 +1,7 @@ /* * sensorctl * - * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * 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. @@ -17,18 +17,7 @@ * */ -#pragma once // _WRISTUP_INJECTOR_H_ - -#include -#include -#include "injector.h" +#pragma once /* __MACRO_H__ */ #define NAME_MAX_TEST 32 - -class injector_wrist_up_conf: public injector_interface { -public: - injector_wrist_up_conf() {} - virtual ~injector_wrist_up_conf() {} - - bool inject(int option_count, char *options[]); -}; +#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) diff --git a/src/sensorctl/mainloop.cpp b/src/sensorctl/mainloop.cpp new file mode 100644 index 0000000..8ea1d4a --- /dev/null +++ b/src/sensorctl/mainloop.cpp @@ -0,0 +1,69 @@ +/* + * sensorctl + * + * 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 "mainloop.h" + +void mainloop::run(void) +{ + instance().start_loop(); +} + +void mainloop::stop(void) +{ + instance().stop_loop(); +} + +bool mainloop::is_running(void) +{ + return instance().is_loop_running(); +} + +mainloop& mainloop::instance(void) +{ + static mainloop loop; + return loop; +} + +void mainloop::start_loop(void) +{ + if (is_loop_running()) + return; + + m_mainloop = g_main_loop_new(NULL, false); + m_running = true; + + g_main_loop_run(m_mainloop); +} + +void mainloop::stop_loop(void) +{ + if (!is_loop_running()) + return; + + g_main_loop_quit(m_mainloop); + g_main_loop_unref(m_mainloop); + m_mainloop = NULL; + m_running = false; +} + +bool mainloop::is_loop_running(void) +{ + return m_running; +} + diff --git a/src/sensorctl/injector_context_orientation.h b/src/sensorctl/mainloop.h similarity index 70% rename from src/sensorctl/injector_context_orientation.h rename to src/sensorctl/mainloop.h index 01d2d18..70e3e53 100644 --- a/src/sensorctl/injector_context_orientation.h +++ b/src/sensorctl/mainloop.h @@ -17,16 +17,24 @@ * */ -#pragma once // _CONTEXT_INJECTOR_H_ +#pragma once /* __MAINLOOP_H__ */ #include #include -#include "injector.h" -class injector_context_orientation: public injector_interface { +class mainloop { public: - injector_context_orientation() {} - virtual ~injector_context_orientation() {} + static void run(void); + static void stop(void); + static bool is_running(void); - bool inject(int option_count, char *options[]); +private: + static mainloop& instance(); + + void start_loop(void); + void stop_loop(void); + bool is_loop_running(void); + + GMainLoop *m_mainloop; + bool m_running; }; diff --git a/src/sensorctl/sensor_adapter.cpp b/src/sensorctl/sensor_adapter.cpp new file mode 100644 index 0000000..056b5a6 --- /dev/null +++ b/src/sensorctl/sensor_adapter.cpp @@ -0,0 +1,160 @@ +/* + * sensorctl + * + * 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 "sensor_adapter.h" + +#include + +#include "log.h" +#include "mainloop.h" +#include "test_bench.h" + +#define SENSOR_EVENT(type) ((type) << 16 | 0x1) + +bool sensor_adapter::is_supported(sensor_type_t type) +{ + sensor_t sensor; + int ret; + + ret = sensord_get_default_sensor(type, &sensor); + ASSERT_TRUE(ret); + + return true; +} + +int sensor_adapter::get_count(sensor_type_t type) +{ + sensor_t *sensors; + int count = 0; + + if (sensord_get_sensors(type, &sensors, &count) == 0) + free(sensors); + + return count; +} + +bool sensor_adapter::get_handle(sensor_info info, int &handle) +{ + int err; + int count; + sensor_t *sensors; + + err = sensord_get_sensors(info.type, &sensors, &count); + ASSERT_EQ(err, 0); + + handle = sensord_connect(sensors[info.index]); + ASSERT_GE(handle, 0); + + return true; +} + +bool sensor_adapter::start(sensor_info info, int &handle) +{ + sensor_t *sensors; + int count; + int err; + bool ret; + + err = sensord_get_sensors(info.type, &sensors, &count); + ASSERT_EQ(err, 0); + + handle = sensord_connect(sensors[info.index]); + ASSERT_GE(handle, 0); + + ret = sensord_register_event(handle, SENSOR_EVENT(info.type), info.interval, info.batch_latency, info.cb, NULL); + ASSERT_TRUE(ret); + + ret = sensord_start(handle, info.powersave); + ASSERT_TRUE(ret); + + free(sensors); + + return true; +} + +bool sensor_adapter::stop(sensor_info info, int handle) +{ + bool ret; + + ret = sensord_unregister_event(handle, SENSOR_EVENT(info.type)); + EXPECT_TRUE(ret); + + ret = sensord_stop(handle); + EXPECT_TRUE(ret); + + ret = sensord_disconnect(handle); + EXPECT_TRUE(ret); + + return true; +} + +bool sensor_adapter::change_interval(int handle, int interval) +{ + return true; +} + +bool sensor_adapter::change_batch_latency(int handle, int batch_latency) +{ + return true; +} + +bool sensor_adapter::change_powersave(int handle, int powersave) +{ + return true; +} + +bool sensor_adapter::set_attribute(int handle, int attribute, int value) +{ + bool ret; + + ret = sensord_set_attribute_int(handle, attribute, value); + ASSERT_TRUE(ret); + + return true; +} + +bool sensor_adapter::set_attribute(int handle, int attribute, char *value, int size) +{ + int ret; + + ret = sensord_set_attribute_str(handle, attribute, value, size); + + return ((ret == 0) ? true : false); +} + +bool sensor_adapter::get_data(int handle, sensor_type_t type, sensor_data_t &data) +{ + bool ret; + + ret = sensord_get_data(handle, SENSOR_EVENT(type), &data); + ASSERT_TRUE(ret); + + return true; +} + +bool sensor_adapter::flush(int handle) +{ + bool ret; + + ret = sensord_flush(handle); + ASSERT_TRUE(ret); + + return true; +} + diff --git a/src/sensorctl/sensor_adapter.h b/src/sensorctl/sensor_adapter.h new file mode 100644 index 0000000..631c4e5 --- /dev/null +++ b/src/sensorctl/sensor_adapter.h @@ -0,0 +1,62 @@ +/* + * sensorctl + * + * 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. + * + */ + +#pragma once /* __SENSOR_ADAPTER_H__ */ + +#include + +class sensor_info { +public: + sensor_info(sensor_type_t _type, int _index, int _interval, int _batch_latency, int _powersave, sensor_cb_t _cb, void *_user_data) + : type(_type) + , index(_index) + , interval(_interval) + , batch_latency(_batch_latency) + , powersave(_powersave) + , cb(_cb) + , user_data(_user_data) + { } + + sensor_type_t type; + int index; + int interval; + int batch_latency; + int powersave; + sensor_cb_t cb; + void *user_data; +}; + +class sensor_adapter { +public: + static bool is_supported(sensor_type_t type); + static int get_count(sensor_type_t type); + static bool get_handle(sensor_info info, int &handle); + + static bool start(sensor_info info, int &handle); + static bool stop(sensor_info info, int handle); + + static bool change_interval(int handle, int interval); + static bool change_batch_latency(int handle, int batch_latency); + static bool change_powersave(int handle, int powersave); + static bool set_attribute(int handle, int attribute, int value); + static bool set_attribute(int handle, int attribute, char *value, int size); + + static bool get_data(int handle, sensor_type_t type, sensor_data_t &data); + static bool flush(int handle); +}; diff --git a/src/sensorctl/sensor_manager.cpp b/src/sensorctl/sensor_manager.cpp index ce2e4ef..25ff923 100644 --- a/src/sensorctl/sensor_manager.cpp +++ b/src/sensorctl/sensor_manager.cpp @@ -1,7 +1,7 @@ /* * sensorctl * - * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * 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. @@ -17,12 +17,14 @@ * */ -#include -#include -#include #include "sensor_manager.h" -#define NAME_MAX_TEST 32 +#include +#include + +#include "log.h" +#include "util.h" +#include "macro.h" struct sensor_info { sensor_type_t type; @@ -30,86 +32,97 @@ struct sensor_info { }; static struct sensor_info sensor_infos[] = { - {ALL_SENSOR, "all"}, + {ALL_SENSOR, "all"}, // General Sensors - {ACCELEROMETER_SENSOR, "accelerometer"}, - {GEOMAGNETIC_SENSOR, "magnetic"}, - {LIGHT_SENSOR, "light"}, - {PROXIMITY_SENSOR, "proximity"}, - {GYROSCOPE_SENSOR, "gyroscope"}, - {PRESSURE_SENSOR, "pressure"}, - {BIO_SENSOR, "bio"}, - {BIO_HRM_SENSOR, "hrm"}, - {AUTO_ROTATION_SENSOR, "auto_rotation"}, - {GRAVITY_SENSOR, "gravity"}, - {LINEAR_ACCEL_SENSOR, "linear_accel"}, - {ROTATION_VECTOR_SENSOR, "rotation_vector"}, - {ORIENTATION_SENSOR, "orientation"}, - {TEMPERATURE_SENSOR, "temperature"}, - {HUMIDITY_SENSOR, "humidity"}, - {ULTRAVIOLET_SENSOR, "ultraviolet"}, - {BIO_LED_GREEN_SENSOR, "hrm_led_green"}, - {BIO_LED_IR_SENSOR, "hrm_led_ir"}, - {BIO_LED_RED_SENSOR, "hrm_led_red"}, - {GYROSCOPE_UNCAL_SENSOR, "gyro_uncal"}, - {GEOMAGNETIC_UNCAL_SENSOR, "mag_uncal"}, - {GYROSCOPE_RV_SENSOR, "gyro_rv"}, - {GEOMAGNETIC_RV_SENSOR, "mag_rv"}, - /* If WRIST_UP_SENSOR is created, it has to be changed to WRIST_UP_SENSOR */ - {MOTION_SENSOR, "motion"}, - {CONTEXT_SENSOR, "context"}, - {EXERCISE_SENSOR, "exercise"}, - {GESTURE_WRIST_UP_SENSOR, "wristup"}, + {ACCELEROMETER_SENSOR, "accelerometer"}, + {GRAVITY_SENSOR, "gravity"}, + {LINEAR_ACCEL_SENSOR, "linear_accel"}, + {GEOMAGNETIC_SENSOR, "magnetic"}, + {ROTATION_VECTOR_SENSOR, "rotation_vector"}, + {ORIENTATION_SENSOR, "orientation"}, + {GYROSCOPE_SENSOR, "gyroscope"}, + {LIGHT_SENSOR, "light"}, + {PROXIMITY_SENSOR, "proximity"}, + {PRESSURE_SENSOR, "pressure"}, + {ULTRAVIOLET_SENSOR, "uv"}, + {TEMPERATURE_SENSOR, "temperature"}, + {HUMIDITY_SENSOR, "humidity"}, + {HRM_SENSOR, "hrm"}, + {HRM_RAW_SENSOR, "hrm_raw"}, + {HRM_LED_GREEN_SENSOR, "hrm_led_green"}, + {HRM_LED_IR_SENSOR, "hrm_led_ir"}, + {HRM_LED_RED_SENSOR, "hrm_led_red"}, + {GYROSCOPE_UNCAL_SENSOR, "gyro_uncal"}, + {GEOMAGNETIC_UNCAL_SENSOR, "mag_uncal"}, + {GYROSCOPE_RV_SENSOR, "gyro_rv"}, + {GEOMAGNETIC_RV_SENSOR, "mag_rv"}, + + {HUMAN_PEDOMETER_SENSOR, "pedo"}, + {HUMAN_SLEEP_MONITOR_SENSOR, "sleep_monitor"}, + + {AUTO_ROTATION_SENSOR, "auto_rotation"}, + //{AUTO_BRIGHTENESS_SENSOR, "auto_brighteness"}, + {MOTION_SENSOR, "motion"}, + {CONTEXT_SENSOR, "context"}, + + {GESTURE_MOVEMENT_SENSOR, "movement"}, + {GESTURE_WRIST_UP_SENSOR, "wristup"}, + {GESTURE_WRIST_DOWN_SENSOR, "wristdown"}, + {GESTURE_MOVEMENT_STATE_SENSOR, "movement_state"}, + + {WEAR_STATUS_SENSOR, "wear_status"}, + {WEAR_ON_MONITOR_SENSOR, "wear_on"}, + {GPS_BATCH_SENSOR, "gps"}, + {ACTIVITY_TRACKER_SENSOR, "activity"}, + {SLEEP_DETECTOR_SENSOR, "sleep_detector"}, }; -bool sensor_manager::process(int argc, char *argv[]) +sensor_manager::~sensor_manager() { - return false; } -void sensor_manager::usage_sensors(void) +bool sensor_manager::run(int argc, char *argv[]) { - PRINT("The sensor types are:\n"); - int sensor_count = ARRAY_SIZE(sensor_infos); + return true; +} - for (int i = 0; i < sensor_count; ++i) - PRINT(" %d: %s(%d)\n", i, sensor_infos[i].name, sensor_infos[i].type); - PRINT("\n"); +void sensor_manager::stop(void) +{ } -sensor_type_t sensor_manager::get_sensor_type(char *name) +sensor_type_t sensor_manager::get_sensor_type(const char *name) { int index; - int sensor_count = ARRAY_SIZE(sensor_infos); + int count; + + if (util::is_hex(name)) + return (sensor_type_t) (strtol(name, NULL, 16)); + + if (util::is_number(name)) + return (sensor_type_t) (atoi(name)); - for (index = 0; index < sensor_count; ++index) { + count = ARRAY_SIZE(sensor_infos); + + for (index = 0; index < count; ++index) { if (!strcmp(sensor_infos[index].name, name)) break; } - if (index == sensor_count) { - _E("ERROR: sensor name is wrong\n"); + if (index == count) { + _E("Invaild sensor name\n"); usage_sensors(); return UNKNOWN_SENSOR; } return sensor_infos[index].type; } -const char *sensor_manager::get_sensor_name(sensor_type_t type) +void sensor_manager::usage_sensors(void) { - int index; + _N("The sensor types are:\n"); int sensor_count = ARRAY_SIZE(sensor_infos); - for (index = 0; index < sensor_count; ++index) { - if (sensor_infos[index].type == type) - break; - } - - if (index == sensor_count) { - _E("ERROR: sensor name is wrong\n"); - usage_sensors(); - return "UNKNOWN SENSOR"; - } - return sensor_infos[index].name; + for (int i = 0; i < sensor_count; ++i) + _N("%3d: %s(%#x)\n", i, sensor_infos[i].name, sensor_infos[i].type); + _N("\n"); } diff --git a/src/sensorctl/sensor_manager.h b/src/sensorctl/sensor_manager.h index 9fd3476..152abcd 100644 --- a/src/sensorctl/sensor_manager.h +++ b/src/sensorctl/sensor_manager.h @@ -1,7 +1,7 @@ /* * sensorctl * - * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * 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. @@ -17,16 +17,19 @@ * */ -#pragma once // _SENSOR_MANAGER_H_ +#pragma once /* __SENSOR_MANAGER_H__ */ #include class sensor_manager { public: - virtual bool process(int argc, char *argv[]); + virtual ~sensor_manager(); + + virtual bool run(int argc, char *argv[]); + virtual void stop(void); + protected: - void usage_sensors(void); + sensor_type_t get_sensor_type(const char *name); - sensor_type_t get_sensor_type(char *name); - const char * get_sensor_name(sensor_type_t type); + void usage_sensors(void); }; diff --git a/src/sensorctl/sensorctl.cpp b/src/sensorctl/sensorctl.cpp index aa4543e..a35e286 100644 --- a/src/sensorctl/sensorctl.cpp +++ b/src/sensorctl/sensorctl.cpp @@ -22,79 +22,85 @@ #include #include -#include +#include "log.h" #include "sensor_manager.h" -#include "tester_manager.h" -#include "injector_manager.h" -#include "info_manager.h" -#include "loopback_manager.h" +#include "tester.h" +#include "injector.h" +#include "info.h" +#include "loopback.h" -static void good_bye(void) -{ -} - -static void signal_handler(int signo) -{ - _E("\nReceived SIGNAL(%d)\n", signo); - exit(EXIT_SUCCESS); - - return; -} +static sensor_manager *manager; -void usage(void) +static void usage(void) { - PRINT("usage: sensorctl []\n"); + _N("usage: sensorctl []\n"); - PRINT("The sensorctl commands are:\n"); - PRINT(" test: test sensor(s)\n"); - PRINT(" inject: inject the event to sensor\n"); - PRINT(" info: show sensor infos\n"); - PRINT(" loopback: sensor loopback test\n"); + _N("The sensorctl commands are:\n"); + _N(" test: test sensor(s)\n"); + _N(" inject: inject the event to sensor\n"); + _N(" info: show sensor infos\n"); } -sensor_manager *create_manager(int argc, char *argv[2]) +static sensor_manager *create_manager(char *command) { sensor_manager *manager = NULL; - if (!strcmp(argv[1], "test")) + if (!strcmp(command, "test")) manager = new(std::nothrow) tester_manager; - if (!strcmp(argv[1], "inject")) + if (!strcmp(command, "inject")) manager = new(std::nothrow) injector_manager; - if (!strcmp(argv[1], "info")) + if (!strcmp(command, "info")) manager = new(std::nothrow) info_manager; - if (!strcmp(argv[1], "loopback")) + if (!strcmp(command, "loopback")) manager = new(std::nothrow) loopback_manager; if (!manager) { - _E("failed to allocate memory for manager"); + _E("failed to allocate memory for manager\n"); return NULL; } return manager; } -int main(int argc, char *argv[]) +static void destroy_manager(sensor_manager *manager) +{ + if (!manager) + return; + + delete manager; + manager = NULL; +} + +static void signal_handler(int signo) { - atexit(good_bye); + _E("\nReceived SIGNAL(%d)\n", signo); + manager->stop(); +} + +int main(int argc, char *argv[]) +{ signal(SIGINT, signal_handler); signal(SIGHUP, signal_handler); signal(SIGTERM, signal_handler); signal(SIGQUIT, signal_handler); signal(SIGABRT, signal_handler); + signal(SIGTSTP, signal_handler); if (argc < 2) { usage(); - return 0; + return EXIT_SUCCESS; } - sensor_manager *manager = create_manager(argc, argv); + manager = create_manager(argv[1]); if (!manager) { usage(); - return 0; + return EXIT_SUCCESS; } - manager->process(argc, argv); + manager->run(argc, argv); + + destroy_manager(manager); - return 0; + return EXIT_SUCCESS; } diff --git a/src/sensorctl/sensorctl_log.h b/src/sensorctl/sensorctl_log.h deleted file mode 100644 index f5f8f25..0000000 --- a/src/sensorctl/sensorctl_log.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * sensorctl - * - * Copyright (c) 2015 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. - * - */ - -#pragma once // _SENSORCTL_LOG_H_ - -#include -#include - -#define KNRM "\x1B[0m" -#define KRED "\x1B[31m" -#define KGRN "\x1B[32m" -#define KYEL "\x1B[33m" -#define KBLU "\x1B[34m" -#define KMAG "\x1B[35m" -#define KCYN "\x1B[36m" -#define KWHT "\x1B[37m" -#define RESET "\033[0m" - -#define PRINT(fmt, args...) \ - do { \ - g_print(fmt, ##args); \ - } while (0) - -#define _E(fmt, args...) \ - do { \ - g_print("\x1B[31m" fmt "\033[0m", ##args); \ - } while (0) - -#define _I(fmt, args...) \ - do { \ - g_print("\x1B[32m" fmt "\033[0m", ##args); \ - } while (0) - diff --git a/src/sensorctl/test_bench.cpp b/src/sensorctl/test_bench.cpp new file mode 100644 index 0000000..f675133 --- /dev/null +++ b/src/sensorctl/test_bench.cpp @@ -0,0 +1,226 @@ +/* + * sensorctl + * + * 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 "log.h" +#include "test_bench.h" + +/* + * Implementation of test_option + */ +bool test_option::full_log = false; +std::string test_option::group = ""; + +void test_option::show_full_log(bool show) +{ + full_log = show; +} + +void test_option::set_group(const char *gname) +{ + group = gname; +} + +void test_option::set_options(int argc, char *argv[]) +{ + /* TODO: use getopt() */ + if (argc > 3) + set_group(argv[3]); + if (argc > 4) + show_full_log(atoi(argv[4])); +} + +/* + * Implementation of test_case + */ +test_case::test_case(const std::string &group, const std::string &name) +: m_group(group) +, m_name(name) +{ + test_bench::register_testcase(group, this); +} + +void test_case::started(void) +{ + _I("[----------]\n"); + _I("[ RUN ] "); + _N("%s.%s\n", m_group.c_str(), m_name.c_str()); +} + +void test_case::stopped(void) +{ + _I("[ OK ] "); + _N("%s.%s\n", m_group.c_str(), m_name.c_str()); + _I("[----------]\n"); +} + +void test_case::show(bool result) +{ + if (result) + _I("[ PASSED ] "); + else + _E("[ FAILED ] "); + _N("%s.%s\n", m_group.c_str(), m_name.c_str()); +} + +void test_case::run_testcase(void) +{ + bool result; + + started(); + result = (this->*m_func)(); + stopped(); + show(result); +} + +void test_case::register_func(test_func func) +{ + m_func = func; +} + +/* + * Implementation of test_bench + */ +test_bench& test_bench::instance() +{ + static test_bench bench; + return bench; +} + +void test_bench::register_testcase(const std::string &group, test_case *testcase) +{ + instance().add_testcase(group, testcase); +} + +void test_bench::push_failure(const std::string &function, long line, const std::string &msg) +{ + instance().add_failure(function, line, msg); +} + +void test_bench::run_all_testcase(void) +{ + instance().run(); +} + +void test_bench::stop_all_testcase(void) +{ + instance().stop(); +} + +void test_bench::add_testcase(const std::string &group, test_case *testcase) +{ + if (!testcase) + return; + + testcases.insert(std::pair(group, testcase)); +} + +void test_bench::add_failure(const std::string &function, long line, const std::string &msg) +{ + test_result fail(function, line, msg); + + results.push_back(fail); + m_failure_count++; +} + +void test_bench::started(void) +{ + _I("[==========] "); + _N("Running %d testcases\n", count(test_option::group)); +} + +void test_bench::stopped(void) +{ + _I("[==========] "); + _N("%d testcases ran\n", count(test_option::group)); +} + +void test_bench::show_failures(void) +{ + _N("================================\n"); + + if (m_failure_count == 0) { + _N("there was no fail case\n"); + return; + } + + _N("%d case(s) are failed, listed below:\n", m_failure_count); + + for (unsigned int i = 0; i < results.size(); ++i) { + _E("[ FAILED ] "); + _N("%s(%ld) -> %s\n", + results[i].function.c_str(), + results[i].line, + results[i].msg.c_str()); + } +} + +void test_bench::run(void) +{ + std::size_t found; + m_failure_count = 0; + + started(); + + for (auto it = testcases.begin(); it != testcases.end(); ++it) { + if (m_stop) + break; + + found = it->first.find("skip"); + + if (test_option::group.empty() && found != std::string::npos) + continue; + + found = it->first.find(test_option::group); + + if (!test_option::group.empty() && found == std::string::npos) + continue; + + it->second->run_testcase(); + } + + stopped(); + show_failures(); +} + +void test_bench::stop(void) +{ + m_stop = true; +} + +unsigned int test_bench::count(std::string &group) +{ + if (group.empty()) + return testcases.size() - count_by_key("skip"); + + return count_by_key(group.c_str()); +} + +unsigned int test_bench::count_by_key(const char *key) +{ + int count = 0; + + for (auto it = testcases.begin(); it != testcases.end(); ++it) { + std::size_t found = it->first.find(key); + + if (found != std::string::npos) + count++; + } + + return count; +} diff --git a/src/sensorctl/test_bench.h b/src/sensorctl/test_bench.h new file mode 100644 index 0000000..8525e39 --- /dev/null +++ b/src/sensorctl/test_bench.h @@ -0,0 +1,201 @@ +/* + * sensorctl + * + * 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. + * + */ + +#pragma once /* __TEST_BENCH_H__ */ + +#include +#include +#include +#include +#include + +#define FAIL(left, comp, right) \ +do { \ + _E("[ FAIL ] "); \ + std::ostringstream os; \ + os << __FUNCTION__ << "(" << __LINE__ << ") : " \ + << #left << "(" << left << ") " \ + << #comp << " " << #right << "(" << right << ")"; \ + std::cout << os.str() << std::endl; \ + test_bench::push_failure(__FUNCTION__, __LINE__, os.str()); \ +} while (0) + +#define PASS(left, comp, right) \ +do { \ + if (test_option::full_log) { \ + _I("[ PASS ] "); \ + std::ostringstream os; \ + os << __FUNCTION__ << "(" << __LINE__ << ") : " \ + << #left << "(" << left << ") " \ + << #comp << " " << #right << "(" << right << ")"; \ + std::cout << os.str() << std::endl; \ + } \ +} while (0) + +#define ASSERT(left, comp, right) \ +do { \ + if (!((left) comp (right))) { \ + FAIL(left, comp, right); \ + return false; \ + } \ + PASS(left, comp, right); \ +} while (0) + + +#define EXPECT(left, comp, right) \ +do { \ + if (!((left) comp (right))) { \ + FAIL(left, comp, right); \ + } else { \ + PASS(left, comp, right); \ + } \ +} while (0) + +#define ASSERT_TRUE(condition) ASSERT(condition, ==, true) +#define ASSERT_FALSE(condition) ASSERT(condition, ==, false) +#define ASSERT_EQ(left, right) ASSERT(left, ==, right) +#define ASSERT_NE(left, right) ASSERT(left, !=, right) +#define ASSERT_LT(left, right) ASSERT(left, <, right) +#define ASSERT_LE(left, right) ASSERT(left, <=, right) +#define ASSERT_GT(left, right) ASSERT(left, >, right) +#define ASSERT_GE(left, right) ASSERT(left, >=, right) +#define ASSERT_NEAR(left, right, err) \ +do { \ + ASSERT(left, >=, (right - (err))); \ + ASSERT(left, <=, (right + (err))); \ +} while (0) + +#define EXPECT_TRUE(condition) EXPECT(condition, ==, true) +#define EXPECT_FALSE(condition) EXPECT(condition, ==, false) +#define EXPECT_EQ(left, right) EXPECT(left, ==, right) +#define EXPECT_NE(left, right) EXPECT(left, !=, right) +#define EXPECT_LT(left, right) EXPECT(left, <, right) +#define EXPECT_LE(left, right) EXPECT(left, <=, right) +#define EXPECT_GT(left, right) EXPECT(left, >, right) +#define EXPECT_GE(left, right) EXPECT(left, >=, right) +#define EXPECT_NEAR(left, right, err) \ +do { \ + EXPECT(left, >=, (right - (err))); \ + EXPECT(left, <=, (right + (err))); \ +} while (0) + +#define TESTCASE(group, name) \ +class test_case_##group_##name : public test_case { \ +public: \ + test_case_##group_##name() \ + : test_case(#group, #name) \ + { \ + register_func(static_cast(&test_case_##group_##name::test)); \ + } \ + bool test(void); \ +} test_case_##group_##name##_instance; \ +bool test_case_##group_##name::test(void) + +/* + * Declaration of test_option + */ +class test_option { +public: + static bool full_log; + static std::string group; + + static void show_full_log(bool show); + static void set_group(const char *gname); + static void set_options(int argc, char *argv[]); +}; + +/* + * Decloaration of test_result + */ +class test_result { +public: + test_result(const std::string &_function, long _line, const std::string &_msg) + : function(_function) + , line(_line) + , msg(_msg) { } + + std::string function; + long line; + std::string msg; +}; + +/* + * Declaration of test_case + */ +class test_case { +public: + test_case(const std::string &group, const std::string &name); + + void run_testcase(void); + + const std::string& group() const { return m_group; } + const std::string& name() const { return m_name; } + +protected: + typedef bool (test_case::*test_func)(); + + void started(void); + void stopped(void); + void show(bool result); + void register_func(test_func func); + +private: + const std::string m_group; + const std::string m_name; + test_func m_func; +}; + +/* + * Declaration of test_bench + */ +class test_bench { +public: + test_bench() + : m_failure_count(0) + , m_stop(false) + {} + + static void register_testcase(const std::string &group, test_case *testcase); + + static void run_all_testcase(void); + static void stop_all_testcase(void); + + static void push_failure(const std::string &function, long line, const std::string &msg); + +private: + static test_bench& instance(); + + void add_failure(const std::string &function, long line, const std::string &msg); + + void started(void); + void stopped(void); + void show_failures(void); + + void add_testcase(const std::string &group, test_case *testcase); + void run(void); + void stop(void); + + unsigned int count(std::string &group); + unsigned int count_by_key(const char *key); + + std::multimap testcases; + std::vector results; + int m_failure_count; + bool m_stop; +}; diff --git a/src/sensorctl/testcase/accelerometer.cpp b/src/sensorctl/testcase/accelerometer.cpp new file mode 100644 index 0000000..ed526c3 --- /dev/null +++ b/src/sensorctl/testcase/accelerometer.cpp @@ -0,0 +1,170 @@ +/* + * sensorctl + * + * 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 + +#include "log.h" +#include "mainloop.h" +#include "test_bench.h" +#include "sensor_adapter.h" + +static void basic_cb(sensor_t sensor, unsigned int event_type, sensor_data_t *data, void *user_data) +{ + EXPECT_GT(data->timestamp, 0); + EXPECT_NEAR(data->values[0], 0, 19.6); + EXPECT_NEAR(data->values[1], 0, 19.6); + EXPECT_NEAR(data->values[2], 0, 19.6); + + mainloop::stop(); +} + +TESTCASE(accelerometer_basic, start_stop_p) +{ + bool ret; + int handle; + + sensor_info info(ACCELEROMETER_SENSOR, 0, + 100, 1000, SENSOR_OPTION_ALWAYS_ON, basic_cb, NULL); + + ret = sensor_adapter::start(info, handle); + ASSERT_TRUE(ret); + + mainloop::run(); + + ret = sensor_adapter::stop(info, handle); + ASSERT_TRUE(ret); + + return true; +} + +static void get_data_cb(sensor_t sensor, unsigned int event_type, sensor_data_t *data, void *user_data) +{ + mainloop::stop(); +} + +TESTCASE(accelerometer_basic, get_data_p) +{ + bool ret; + int handle; + sensor_data_t data; + + sensor_info info(ACCELEROMETER_SENSOR, 0, + 100, 1000, SENSOR_OPTION_ALWAYS_ON, get_data_cb, NULL); + + ret = sensor_adapter::start(info, handle); + ASSERT_TRUE(ret); + + mainloop::run(); + + ret = sensor_adapter::get_data(handle, info.type, data); + ASSERT_TRUE(ret); + + ret = sensor_adapter::stop(info, handle); + ASSERT_TRUE(ret); + + return true; +} + +static unsigned long long prev_prev_ts; +static unsigned long long prev_ts; +static int event_count; + +static void accel_regular_interval_cb(sensor_t sensor, unsigned int event_type, sensor_data_t *data, void *user_data) +{ + int prev_gap; + int current_gap; + if (prev_prev_ts == 0) { + prev_prev_ts = data->timestamp; + return; + } + + if (prev_ts == 0) { + prev_ts = data->timestamp; + return; + } + + prev_gap = prev_ts - prev_prev_ts; + current_gap = data->timestamp - prev_ts; + + EXPECT_NEAR(current_gap, prev_gap, 10000); + prev_prev_ts = prev_ts; + prev_ts = data->timestamp; + + if (event_count++ > 3) + mainloop::stop(); +} + +TESTCASE(accelerometer_interval, regular_interval_p) +{ + bool ret; + int handle; + prev_prev_ts = 0; + prev_ts = 0; + event_count = 0; + + sensor_info info(ACCELEROMETER_SENSOR, 0, + 100, 1000, SENSOR_OPTION_ALWAYS_ON, accel_regular_interval_cb, NULL); + + ret = sensor_adapter::start(info, handle); + ASSERT_TRUE(ret); + + mainloop::run(); + + ret = sensor_adapter::stop(info, handle); + ASSERT_TRUE(ret); + + return true; +} + +static void accel_interval_100ms_cb(sensor_t sensor, unsigned int event_type, sensor_data_t *data, void *user_data) +{ + if (prev_ts == 0) { + prev_ts = data->timestamp; + return; + } + + /* 100ms + 20ms(error) */ + EXPECT_LE(data->timestamp - prev_ts, 120000); + prev_ts = data->timestamp; + + if (event_count++ > 3) + mainloop::stop(); +} + +TESTCASE(accelerometer_interval, 100ms_interval_p) +{ + bool ret; + int handle; + + prev_ts = 0; + event_count = 0; + + sensor_info info(ACCELEROMETER_SENSOR, 0, + 100, 1000, SENSOR_OPTION_ALWAYS_ON, accel_interval_100ms_cb, NULL); + + ret = sensor_adapter::start(info, handle); + ASSERT_TRUE(ret); + + mainloop::run(); + + ret = sensor_adapter::stop(info, handle); + ASSERT_TRUE(ret); + + return true; +} diff --git a/src/sensorctl/testcase/sensor_basic.cpp b/src/sensorctl/testcase/sensor_basic.cpp new file mode 100644 index 0000000..466b80a --- /dev/null +++ b/src/sensorctl/testcase/sensor_basic.cpp @@ -0,0 +1,168 @@ +/* + * sensorctl + * + * 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 +#include + +#include "log.h" +#include "mainloop.h" +#include "test_bench.h" +#include "sensor_adapter.h" + +static void basic_cb(sensor_t sensor, unsigned int event_type, sensor_data_t *data, void *user_data) +{ + EXPECT_GT(data->timestamp, 0); + //_N("[ DATA ] %f\n", data->values[0]); + mainloop::stop(); +} + +TESTCASE(all_sensor_test, scenario_basic_p) +{ + int err; + bool ret; + int count; + int handle; + sensor_t *sensors; + sensor_type_t type; + + err = sensord_get_sensors(ALL_SENSOR, &sensors, &count); + ASSERT_EQ(err, 0); + + for (int i = 0; i < count; ++i) { + sensord_get_type(sensors[i], &type); + /* TODO */ + _N("[ TYPE ] %s\n", "UNKNOWN_SENSOR"); + + sensor_info info(type, 0, 100, 1000, SENSOR_OPTION_ALWAYS_ON, basic_cb, NULL); + + ret = sensor_adapter::start(info, handle); + EXPECT_TRUE(ret); + + mainloop::run(); + + ret = sensor_adapter::stop(info, handle); + EXPECT_TRUE(ret); + } + + free(sensors); + + return true; +} + +typedef bool (*process_func_t)(const char *msg, int size, int count); + +static pid_t run_process(process_func_t func, const char *msg, int size, int count) +{ + pid_t pid = fork(); + if (pid < 0) + return -1; + + if (pid == 0) { + if (!func(msg, size, count)) + _E("Failed to run process\n"); + exit(0); + } + + return pid; +} + +static bool run_echo_command_test(const char *str, int size, int cout) +{ + bool ret = true; + int handle; + char buf[4096] = {'1', '1', '1', }; + + sensor_info info(ACCELEROMETER_SENSOR, 0, + 100, 1000, SENSOR_OPTION_ALWAYS_ON, basic_cb, NULL); + sensor_adapter::get_handle(info, handle); + + usleep(10000); + for (int i = 0; i < 1024; ++i) + ret &= sensor_adapter::set_attribute(handle, SENSOR_ATTR_ACCELEROMETER_INJECTION, buf, 4096); + ASSERT_TRUE(ret); + + return true; +} + +TESTCASE(echo_command_test, echo_command_p) +{ + pid_t pid; + + usleep(100000); + + for (int i = 0; i < 100; ++i) { + pid = run_process(run_echo_command_test, NULL, 0, 0); + EXPECT_GE(pid, 0); + } + + pid = run_process(run_echo_command_test, NULL, 0, 0); + EXPECT_GE(pid, 0); + + ASSERT_TRUE(true); + usleep(100000); + + return true; +} + +#if 0 +TESTCASE(gyroscope_value_p) +{ + scenario_basic_p(GYROSCOPE_SENSOR); +} + +TESTCASE(gravitye_value_p) +{ + scenario_basic_p(GRAVITY_SENSOR); +} + +TESTCASE(linear_accel_value_p) +{ + scenario_basic_p(LINEAR_ACCEL_SENSOR); +} + +TESTCASE(proximity_value_p) +{ + scenario_basic_p(PROXIMITY_SENSOR); +} + +TESTCASE(pressure_value_p) +{ + scenario_basic_p(PRESSURE_SENSOR); +} + +TESTCASE(hrm_value_p) +{ + scenario_basic_p(HRM_SENSOR); +} + +TESTCASE(hrm_raw_value_p) +{ + scenario_basic_p(HRM_RAW_SENSOR); +} + +TESTCASE(hrm_led_green_value_p) +{ + scenario_basic_p(HRM_LED_GREEN_SENSOR); +} + +TESTCASE(wrist_up_value_p) +{ + scenario_basic_p(GESTURE_WRIST_UP_SENSOR); +} +#endif diff --git a/src/sensorctl/injector_wrist_up_algo.h b/src/sensorctl/testcase/sensor_interval.cpp similarity index 62% rename from src/sensorctl/injector_wrist_up_algo.h rename to src/sensorctl/testcase/sensor_interval.cpp index a5bd6fe..8318fd5 100644 --- a/src/sensorctl/injector_wrist_up_algo.h +++ b/src/sensorctl/testcase/sensor_interval.cpp @@ -1,7 +1,7 @@ /* * sensorctl * - * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * 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. @@ -17,17 +17,10 @@ * */ -#pragma once // _WRISTUP_INJECTOR_H_ +#include "test_bench.h" -#include -#include -#include "injector.h" - -class injector_wrist_up_algo: public injector_interface { -public: - injector_wrist_up_algo() {} - virtual ~injector_wrist_up_algo() {} - - bool init(void); - bool inject(int option_count, char *options[]); -}; +TESTCASE(sensor_interval, all_sensor_interval_10ms_p) +{ + /* TODO: test 10ms interval */ + return true; +} diff --git a/src/sensorctl/tester.cpp b/src/sensorctl/tester.cpp new file mode 100644 index 0000000..c164a57 --- /dev/null +++ b/src/sensorctl/tester.cpp @@ -0,0 +1,188 @@ +/* + * sensorctl + * + * 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 "tester.h" + +#include +#include +#include +#include + +#include "log.h" +#include "macro.h" +#include "mainloop.h" +#include "test_bench.h" +#include "sensor_adapter.h" + +#define TESTER_ARGC 3 /* e.g. {sensorctl, test, accelerometer} */ + +#define MAX_COUNT 999999 +#define DEFAULT_INTERVAL 100 +#define DEFAULT_LATENCY 0 +#define DEFAULT_POWERSAVE_OPTION SENSOR_OPTION_ALWAYS_ON + +static sensor_type_t type; +static int interval; +static int latency; +static int powersave; +static int repeat; + +static int event_count = 0; + +tester_manager::tester_manager() +{ +} + +tester_manager::~tester_manager() +{ +} + +bool tester_manager::run(int argc, char *argv[]) +{ + if (argc < TESTER_ARGC) { + usage(); + return false; + } + + if (!setup(argc, argv)) { + usage(); + return false; + } + + test_bench::run_all_testcase(); + + return true; +} + +bool tester_manager::setup(int argc, char *argv[]) +{ + if (strncmp(argv[2], "auto", 4) == 0) + return setup_auto(argc, argv); + + return setup_manual(argc, argv); +} + +bool tester_manager::setup_auto(int argc, char *argv[]) +{ + if (argc > 5) + repeat = atoi(argv[5]); + + test_option::show_full_log(true); + test_option::set_options(argc, argv); + + return true; +} + +bool tester_manager::setup_manual(int argc, char *argv[]) +{ + type = get_sensor_type(argv[2]); + RETVM_IF(type == UNKNOWN_SENSOR, false, "Invalid argument\n"); + + interval = DEFAULT_INTERVAL; + latency = DEFAULT_LATENCY; + powersave = DEFAULT_POWERSAVE_OPTION; + event_count = 0; + + if (argc >= TESTER_ARGC + 1) + interval = atoi(argv[TESTER_ARGC]); + if (argc >= TESTER_ARGC + 2) + latency = atoi(argv[TESTER_ARGC + 1]); + if (argc >= TESTER_ARGC + 3) + powersave = atoi(argv[TESTER_ARGC + 2]); + + test_option::show_full_log(true); + test_option::set_group("skip_manual"); + /* test_option::set_options(argc, argv); */ + + return true; +} + +void tester_manager::stop(void) +{ + if (mainloop::is_running()) + mainloop::stop(); +} + +void tester_manager::usage(void) +{ + _N("usage: sensorctl test auto [group] [log]\n"); + _N("usage: sensorctl test [interval] [batch_latency] [event_count] [test_count]\n"); + + usage_sensors(); + + _N("auto:\n"); + _N(" test sensors automatically.\n"); + _N("group:\n"); + _N(" a group name(or a specific word contained in the group name).\n"); + _N("log:\n"); + _N(" enable(1) or disable(0). default value is 1.\n"); + _N("sensor_type: specific sensor\n"); + _N(" test specific sensor manually.\n"); + _N("interval_ms:\n"); + _N(" interval. default value is 100ms.\n"); + _N("batch_latency_ms:\n"); + _N(" batch_latency. default value is 1000ms.\n"); + _N("event count(n):\n"); + _N(" test sensor until it gets n event. default is 999999(infinitly).\n"); + _N("test count(n):\n"); + _N(" test sensor in n times repetitively, default is 1.\n\n"); +} + +/* manual test case */ +static void test_cb(sensor_t sensor, unsigned int event_type, sensor_data_t *data, void *user_data) +{ + if (event_count >= MAX_COUNT) { + mainloop::stop(); + return; + } + + _N("%llu ", data->timestamp); + for (int i = 0; i < data->value_count; ++i) + _N(" %10f", data->values[i]); + _N("\n"); +} + +TESTCASE(skip_manual, sensor_test) +{ + int handle; + bool ret; + int index = 0; + sensor_data_t data; + + if (sensor_adapter::get_count(type) > 1) { + _N("There are more than 2 sensors. please enter the index : "); + scanf("%d", &index); + } + + sensor_info info(type, index, interval, latency, powersave, test_cb, NULL); + + ret = sensor_adapter::start(info, handle); + ASSERT_TRUE(ret); + + ret = sensor_adapter::get_data(handle, type, data); + EXPECT_TRUE(ret); + + mainloop::run(); + + ret = sensor_adapter::stop(info, handle); + ASSERT_TRUE(ret); + + return true; +} + diff --git a/src/sensorctl/tester.h b/src/sensorctl/tester.h index 2d912c9..148a402 100644 --- a/src/sensorctl/tester.h +++ b/src/sensorctl/tester.h @@ -1,7 +1,7 @@ /* * sensorctl * - * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * 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. @@ -17,12 +17,27 @@ * */ -#pragma once // _TESTER_H_ +#pragma once /* __TESTER_H__ */ #include +#include +#include -class tester_interface { +#include "sensor_manager.h" + +class tester_manager : public sensor_manager { public: - virtual bool init(void) = 0; - virtual bool test(sensor_type_t type, int option_count, char *options[]) = 0; + tester_manager(); + virtual ~tester_manager(); + + bool run(int argc, char *argv[]); + void stop(void); + +private: + bool setup(int argc, char *argv[]); + bool setup_auto(int argc, char *argv[]); + bool setup_manual(int argc, char *argv[]); + + void usage(void); }; + diff --git a/src/sensorctl/tester_manager.cpp b/src/sensorctl/tester_manager.cpp deleted file mode 100644 index c16140f..0000000 --- a/src/sensorctl/tester_manager.cpp +++ /dev/null @@ -1,79 +0,0 @@ -/* - * sensorctl - * - * Copyright (c) 2015 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 -#include -#include -#include -#include "tester.h" -#include "tester_manager.h" -#include "tester_sensor.h" - -#define NAME_MAX_TEST 32 -#define ARGC_BASE 3 /* e.g. {sensorctl, test, accelerometer} */ - -bool tester_manager::process(int argc, char *argv[]) -{ - int option_count; - char *options[8]; - sensor_type_t type; - int i; - - if (argc == 2) { - usage(); - return false; - } - - /* 1. get sensor type */ - type = get_sensor_type(argv[2]); - if (type == UNKNOWN_SENSOR) { - _E("ERROR : failed to process injector\n"); - return false; - } - - /* 2. set up injector */ - tester_interface *tester = new tester_sensor(); - tester->init(); - - /* 3. test sensor with options */ - option_count = argc - ARGC_BASE; - for (i = 0; i < option_count; ++i) { - options[i] = new char[NAME_MAX_TEST]; - strncpy(options[i], argv[ARGC_BASE + i], strlen(argv[ARGC_BASE + i])); - } - - tester->test(type, option_count, options); - - return true; -} - -void tester_manager::usage(void) -{ - PRINT("usage: sensorctl test [interval] [event_count] [test_count]\n\n"); - - usage_sensors(); - - PRINT("interval_ms:\n"); - PRINT(" interval. default value is 100ms.\n"); - PRINT("event count(n):\n"); - PRINT(" test sensor until it gets n event. default is 999999(infinitly).\n"); - PRINT("test count(n):\n"); - PRINT(" test sensor in n times repetitively, default is 1.\n\n"); -} - diff --git a/src/sensorctl/tester_sensor.cpp b/src/sensorctl/tester_sensor.cpp deleted file mode 100644 index 7f324f6..0000000 --- a/src/sensorctl/tester_sensor.cpp +++ /dev/null @@ -1,151 +0,0 @@ -/* - * sensorctl - * - * Copyright (c) 2015 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 -#include -#include -#include -#include -#include -#include "tester_sensor.h" - -#define DEFAULT_INTERVAL 100 -#define DEFAULT_LATENCY 0 -#define DEFAULT_TEST_COUNT 1 -#define DEFAULT_EVENT_COUNT 9999 - -#define SENSOR_SHIFT_TYPE 16 - -static GMainLoop *mainloop; -static int check_loop; - -static const char *result_str(bool result) { - if (result) return KGRN "[PASS]" RESET; - else return KRED "[FAIL]" RESET; -} - -bool tester_sensor::init(void) -{ - return true; -} - -void tester_sensor::test_cb(sensor_t sensor, unsigned int event_type, sensor_data_t *data, void *user_data) -{ - sensor_type_t type; - int *cnt_event; - - sensord_get_type(sensor, &type); - - cnt_event = (int *)user_data; - - if (check_loop++ >= *cnt_event) { - if (!mainloop) - return; - - g_main_loop_quit(mainloop); - g_main_loop_unref(mainloop); - mainloop = NULL; - return; - } - - PRINT("[%llu] %s:", data->timestamp, sensord_get_name(sensor)); - - if (type == GESTURE_WRIST_UP_SENSOR) { - PRINT("[%d]\n", ((sensorhub_data_t *)data)->hub_data[0]); - return; - } - - for (int i = 0; i < data->value_count; ++i) - PRINT(" [%f]", data->values[i]); - PRINT("\n"); -} - -void tester_sensor::test_sensor(sensor_type_t type, int interval, int latency, int cnt_test, int cnt_event) -{ - bool result; - sensor_t sensor; - unsigned int event_id; - sensor_data_t data; - int handle; - int count = 0; - - event_id = type << SENSOR_SHIFT_TYPE | 0x1; - - while (count++ < cnt_test) { - mainloop = g_main_loop_new(NULL, FALSE); - check_loop = 0; - - PRINT("=======================================\n"); - PRINT("TEST(%d/%d)\n", count, cnt_test); - PRINT("=======================================\n"); - - sensor = sensord_get_sensor(type); - PRINT("%s sensord_get_sensor: sensor(%p)\n", result_str((sensor == NULL)? 0 : 1), sensor); - - handle = sensord_connect(sensor); - PRINT("%s sensord_connect: handle(%d)\n", result_str((handle >= 0)), handle); - - result = sensord_register_event(handle, event_id, interval, latency, test_cb, (void *)&cnt_event); - PRINT("%s sensord_register_event\n", result_str(result)); - - result = sensord_start(handle, 3); - PRINT("%s sensord_start\n", result_str(result)); - - result = sensord_get_data(handle, event_id, &data); - PRINT("%s sensord_get_data\n", result_str(result)); - - result = sensord_flush(handle); - PRINT("%s sensord_flush\n", result_str(result)); - - if (result) { - for (int i = 0; i < data.value_count; ++i) - PRINT("[%f] ", data.values[i]); - PRINT("\n"); - } - - g_main_loop_run(mainloop); - - result = sensord_unregister_event(handle, event_id); - PRINT("%s sensord_unregister_event: handle(%d)\n", result_str(result), handle); - result = sensord_stop(handle); - PRINT("%s sensord_stop: handle(%d)\n", result_str(result), handle); - result = sensord_disconnect(handle); - PRINT("%s sensord_disconnect: handle(%d)\n", result_str(result), handle); - } -} - -bool tester_sensor::test(sensor_type_t type, int option_count, char *options[]) -{ - int interval = DEFAULT_INTERVAL; - int latency = DEFAULT_LATENCY; - int cnt_test = DEFAULT_TEST_COUNT; - int cnt_event = DEFAULT_EVENT_COUNT; - - sensor_type_t sensor_type = type; - - if (option_count >= 1) - interval = atoi(options[0]); - if (option_count >= 2) - cnt_event = atoi(options[1]); - if (option_count >= 3) - cnt_test = atoi(options[2]); - - test_sensor(sensor_type, interval, latency, cnt_test, cnt_event); - return true; -} diff --git a/src/sensorctl/tester_sensor.h b/src/sensorctl/tester_sensor.h deleted file mode 100644 index 68b76b4..0000000 --- a/src/sensorctl/tester_sensor.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * sensorctl - * - * Copyright (c) 2015 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. - * - */ - -#pragma once // _SENSOR_TESTER_H_ - -#include "tester.h" - -class tester_sensor : public tester_interface { -public: - tester_sensor() {} - virtual ~tester_sensor() {} - - virtual bool init(void); - virtual bool test(sensor_type_t type, int option_count, char *options[]); -private: - void test_sensor(sensor_type_t type, int interval, int latency, int cnt_test, int cnt_event); - static void test_cb(sensor_t sensor, unsigned int event_type, sensor_data_t *data, void *user_data); -}; diff --git a/src/sensorctl/util.cpp b/src/sensorctl/util.cpp new file mode 100644 index 0000000..9b2016f --- /dev/null +++ b/src/sensorctl/util.cpp @@ -0,0 +1,59 @@ +/* + * sensorctl + * + * 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 +#include "util.h" + +bool util::is_number(const char *value) +{ + if (value == NULL || *value == 0) + return false; + + while (*value) { + if (*value < '0' || *value > '9') + return false; + value++; + } + + return true; +} + +bool util::is_hex(const char *value) +{ + if (value == NULL || *value == 0) + return false; + + if (value[0] != '0') + return false; + + if (value[1] != 'x' && value[1] != 'X') + return false; + + value += 2; + + while (*value) { + if ((*value < '0' || *value > '9') && + (*value < 'a' || *value > 'f') && + (*value < 'A' || *value > 'F')) + return false; + value++; + } + + return true; +} diff --git a/src/sensorctl/tester_manager.h b/src/sensorctl/util.h similarity index 67% rename from src/sensorctl/tester_manager.h rename to src/sensorctl/util.h index 16f4b16..b2dcbc8 100644 --- a/src/sensorctl/tester_manager.h +++ b/src/sensorctl/util.h @@ -1,7 +1,7 @@ /* * sensorctl * - * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * 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. @@ -17,16 +17,10 @@ * */ -#pragma once // _TEST_MANAGER_H_ +#pragma once /* __UTIL_H__ */ -#include "sensor_manager.h" - -class tester_manager : public sensor_manager { +class util { public: - tester_manager() {} - virtual ~tester_manager() {} - - bool process(int argc, char *argv[]); -private: - void usage(void); + static bool is_number(const char *value); + static bool is_hex(const char *value); }; -- 2.7.4 From d7e92f5d659cc9e2863638b3d6c70ef4dfb71ca0 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Thu, 9 Feb 2017 20:18:20 +0900 Subject: [PATCH 16/16] sensord: refactoring: remove regacy code Change-Id: I4ed2735fd80ab5c9345a03d87ce312c00502c3e2 Signed-off-by: kibak.yoon --- include/enum_factory.h | 47 -- include/sensor_types.h | 208 +++-- src/client/client.cpp | 1324 ++++++------------------------ src/client/client_common.cpp | 127 --- src/client/client_common.h | 59 -- src/client/command_channel.cpp | 713 ---------------- src/client/command_channel.h | 61 -- src/client/dbus_listener.cpp | 135 --- src/client/dbus_listener.h | 46 -- src/client/external_client.cpp | 256 ------ src/client/external_data_channel.cpp | 266 ------ src/client/external_data_channel.h | 46 -- src/client/external_sensor_manager.cpp | 543 ------------ src/client/external_sensor_manager.h | 135 --- src/client/reg_event_info.h | 46 -- src/client/sensor_callback_deliverer.cpp | 155 ---- src/client/sensor_callback_deliverer.h | 54 -- src/client/sensor_client_info.cpp | 723 ---------------- src/client/sensor_client_info.h | 120 --- src/client/sensor_event_listener.cpp | 477 ----------- src/client/sensor_event_listener.h | 120 --- src/client/sensor_handle_info.cpp | 230 ------ src/client/sensor_handle_info.h | 90 -- src/client/sensor_info_manager.cpp | 100 --- src/client/sensor_info_manager.h | 54 -- src/sensor/CMakeLists.txt | 19 +- src/server/client_info_manager.cpp | 438 ---------- src/server/client_info_manager.h | 75 -- src/server/client_sensor_record.cpp | 262 ------ src/server/client_sensor_record.h | 77 -- src/server/command_queue.cpp | 55 -- src/server/command_queue.h | 50 -- src/server/command_worker.cpp | 872 -------------------- src/server/command_worker.h | 96 --- src/server/external_client_manager.cpp | 227 ----- src/server/external_client_manager.h | 55 -- src/server/external_sensor.cpp | 86 -- src/server/external_sensor.h | 47 -- src/server/external_sensor_record.cpp | 99 --- src/server/external_sensor_record.h | 56 -- src/server/external_sensor_service.cpp | 152 ---- src/server/external_sensor_service.h | 52 -- src/server/external_sensor_worker.cpp | 325 -------- src/server/external_sensor_worker.h | 67 -- src/server/main.cpp | 88 +- src/server/permission_checker.cpp | 115 +-- src/server/permission_checker.h | 36 - src/server/physical_sensor.cpp | 192 ----- src/server/physical_sensor.h | 40 +- src/server/sensor_base.cpp | 384 --------- src/server/sensor_base.h | 113 --- src/server/sensor_event_dispatcher.cpp | 311 ------- src/server/sensor_event_dispatcher.h | 79 -- src/server/sensor_event_poller.cpp | 188 ----- src/server/sensor_event_poller.h | 50 -- src/server/sensor_event_queue.cpp | 60 -- src/server/sensor_event_queue.h | 52 -- src/server/sensor_fusion.cpp | 104 --- src/server/sensor_fusion.h | 59 -- src/server/sensor_info_list.cpp | 161 ---- src/server/sensor_info_list.h | 67 -- src/server/sensor_loader.cpp | 419 +--------- src/server/sensor_loader.h | 44 - src/server/sensor_usage.cpp | 75 -- src/server/sensor_usage.h | 44 - src/server/server.cpp | 331 +------- src/server/server.h | 41 +- src/server/virtual_sensor.cpp | 23 +- src/server/virtual_sensor.h | 32 +- src/server/worker_thread.cpp | 226 ----- src/server/worker_thread.h | 79 -- src/shared/cbase_lock.cpp | 154 ---- src/shared/cbase_lock.h | 83 -- src/shared/cmutex.cpp | 64 -- src/shared/cmutex.h | 42 - src/shared/command_common.h | 1 - src/shared/cpacket.cpp | 104 --- src/shared/cpacket.h | 51 -- src/shared/csocket.cpp | 548 ------------- src/shared/csocket.h | 80 -- src/shared/poller.cpp | 142 ---- src/shared/poller.h | 52 -- src/shared/sensor_types.cpp | 22 - 83 files changed, 365 insertions(+), 13636 deletions(-) delete mode 100644 include/enum_factory.h delete mode 100644 src/client/client_common.cpp delete mode 100644 src/client/client_common.h delete mode 100644 src/client/command_channel.cpp delete mode 100644 src/client/command_channel.h delete mode 100644 src/client/dbus_listener.cpp delete mode 100644 src/client/dbus_listener.h delete mode 100644 src/client/external_client.cpp delete mode 100644 src/client/external_data_channel.cpp delete mode 100644 src/client/external_data_channel.h delete mode 100644 src/client/external_sensor_manager.cpp delete mode 100644 src/client/external_sensor_manager.h delete mode 100644 src/client/reg_event_info.h delete mode 100644 src/client/sensor_callback_deliverer.cpp delete mode 100644 src/client/sensor_callback_deliverer.h delete mode 100644 src/client/sensor_client_info.cpp delete mode 100644 src/client/sensor_client_info.h delete mode 100644 src/client/sensor_event_listener.cpp delete mode 100644 src/client/sensor_event_listener.h delete mode 100644 src/client/sensor_handle_info.cpp delete mode 100644 src/client/sensor_handle_info.h delete mode 100644 src/client/sensor_info_manager.cpp delete mode 100644 src/client/sensor_info_manager.h delete mode 100644 src/server/client_info_manager.cpp delete mode 100644 src/server/client_info_manager.h delete mode 100644 src/server/client_sensor_record.cpp delete mode 100644 src/server/client_sensor_record.h delete mode 100644 src/server/command_queue.cpp delete mode 100644 src/server/command_queue.h delete mode 100644 src/server/command_worker.cpp delete mode 100644 src/server/command_worker.h delete mode 100644 src/server/external_client_manager.cpp delete mode 100644 src/server/external_client_manager.h delete mode 100644 src/server/external_sensor.cpp delete mode 100644 src/server/external_sensor.h delete mode 100644 src/server/external_sensor_record.cpp delete mode 100644 src/server/external_sensor_record.h delete mode 100644 src/server/external_sensor_service.cpp delete mode 100644 src/server/external_sensor_service.h delete mode 100644 src/server/external_sensor_worker.cpp delete mode 100644 src/server/external_sensor_worker.h delete mode 100644 src/server/sensor_base.cpp delete mode 100644 src/server/sensor_base.h delete mode 100644 src/server/sensor_event_dispatcher.cpp delete mode 100644 src/server/sensor_event_dispatcher.h delete mode 100644 src/server/sensor_event_poller.cpp delete mode 100644 src/server/sensor_event_poller.h delete mode 100644 src/server/sensor_event_queue.cpp delete mode 100644 src/server/sensor_event_queue.h delete mode 100644 src/server/sensor_fusion.cpp delete mode 100644 src/server/sensor_fusion.h delete mode 100644 src/server/sensor_info_list.cpp delete mode 100644 src/server/sensor_info_list.h delete mode 100644 src/server/sensor_usage.cpp delete mode 100644 src/server/sensor_usage.h delete mode 100644 src/server/worker_thread.cpp delete mode 100644 src/server/worker_thread.h delete mode 100644 src/shared/cbase_lock.cpp delete mode 100644 src/shared/cbase_lock.h delete mode 100644 src/shared/cmutex.cpp delete mode 100644 src/shared/cmutex.h delete mode 100644 src/shared/cpacket.cpp delete mode 100644 src/shared/cpacket.h delete mode 100644 src/shared/csocket.cpp delete mode 100644 src/shared/csocket.h delete mode 100644 src/shared/poller.cpp delete mode 100644 src/shared/poller.h delete mode 100644 src/shared/sensor_types.cpp diff --git a/include/enum_factory.h b/include/enum_factory.h deleted file mode 100644 index e67d71b..0000000 --- a/include/enum_factory.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * sensord - * - * 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. - * - */ - -#ifndef _ENUM_FACTORY_H_ -#define _ENUM_FACTORY_H_ - -#define ENUM_SENSOR(name) name, -#define ENUM_SENSOR_VALUE(name, assign) name = (assign), - -#define ENUM_CASE(name) case (name): return #name; -#define ENUM_CASE_VALUE(name, assign) ENUM_CASE(name) - -#define DECLARE_SENSOR_ENUM(ENUM_TYPE, ENUM_DEF) \ - typedef enum ENUM_TYPE { \ - ENUM_DEF(ENUM_SENSOR, ENUM_SENSOR_VALUE) \ - } ENUM_TYPE; - -#define DECLARE_SENSOR_ENUM_UTIL_NS(ENUM_TYPE) \ - namespace util_##ENUM_TYPE { \ - const char *get_string(ENUM_TYPE type); \ - }; - -#define DECLARE_SENSOR_ENUM_UTIL(ENUM_TYPE, ENUM_DEF) \ - const char *util_##ENUM_TYPE::get_string(ENUM_TYPE type) { \ - switch (type) { \ - ENUM_DEF(ENUM_CASE, ENUM_CASE_VALUE) \ - } \ - return "UNKNOWN"; \ - } - -#endif /* _ENUM_FACTORY_H_ */ diff --git a/include/sensor_types.h b/include/sensor_types.h index ac1f13a..5b752af 100644 --- a/include/sensor_types.h +++ b/include/sensor_types.h @@ -17,114 +17,112 @@ * */ -#ifndef _SENSOR_TYPES_H_ -#define _SENSOR_TYPES_H_ - -#include +#ifndef __SENSOR_TYPES_H__ +#define __SENSOR_TYPES_H__ #ifdef __cplusplus extern "C" { #endif -#define SENSOR_TYPE(DEF_SENSOR, DEF_SENSOR_VALUE) \ - DEF_SENSOR_VALUE(UNKNOWN_SENSOR, -2) \ - DEF_SENSOR_VALUE(ALL_SENSOR, -1) \ - DEF_SENSOR_VALUE(ACCELEROMETER_SENSOR, 0) \ - DEF_SENSOR(GRAVITY_SENSOR) \ - DEF_SENSOR(LINEAR_ACCEL_SENSOR) \ - DEF_SENSOR(GEOMAGNETIC_SENSOR) \ - DEF_SENSOR(ROTATION_VECTOR_SENSOR) \ - DEF_SENSOR(ORIENTATION_SENSOR) \ - DEF_SENSOR(GYROSCOPE_SENSOR) \ - DEF_SENSOR(LIGHT_SENSOR) \ - DEF_SENSOR(PROXIMITY_SENSOR) \ - DEF_SENSOR(PRESSURE_SENSOR) \ - DEF_SENSOR(ULTRAVIOLET_SENSOR) \ - DEF_SENSOR(TEMPERATURE_SENSOR) \ - DEF_SENSOR(HUMIDITY_SENSOR) \ - DEF_SENSOR(HRM_SENSOR) \ - DEF_SENSOR(HRM_LED_GREEN_SENSOR) \ - DEF_SENSOR(HRM_LED_IR_SENSOR) \ - DEF_SENSOR(HRM_LED_RED_SENSOR) \ - DEF_SENSOR(GYROSCOPE_UNCAL_SENSOR) \ - DEF_SENSOR(GEOMAGNETIC_UNCAL_SENSOR) \ - DEF_SENSOR(GYROSCOPE_RV_SENSOR) \ - DEF_SENSOR(GEOMAGNETIC_RV_SENSOR) \ - \ - DEF_SENSOR_VALUE(HUMAN_PEDOMETER_SENSOR, 0x300) \ - DEF_SENSOR(HUMAN_SLEEP_MONITOR_SENSOR) \ - DEF_SENSOR(HUMAN_SLEEP_DETECTOR_SENSOR) \ - DEF_SENSOR(HUMAN_STRESS_MONITOR_SENSOR) \ - \ - DEF_SENSOR_VALUE(EXERCISE_WALKING_SENSOR, 0x400) \ - DEF_SENSOR(EXERCISE_RUNNING_SENSOR) \ - DEF_SENSOR(EXERCISE_HIKING_SENSOR) \ - DEF_SENSOR(EXERCISE_CYCLING_SENSOR) \ - DEF_SENSOR(EXERCISE_ELLIPTICAL_SENSOR) \ - DEF_SENSOR(EXERCISE_INDOOR_CYCLING_SENSOR) \ - DEF_SENSOR(EXERCISE_ROWING_SENSOR) \ - DEF_SENSOR(EXERCISE_STEPPER_SENSOR) \ - \ - DEF_SENSOR_VALUE(EXTERNAL_EXERCISE_SENSOR, 0x800) \ - \ - DEF_SENSOR_VALUE(FUSION_SENSOR, 0x900) \ - DEF_SENSOR(AUTO_ROTATION_SENSOR) \ - DEF_SENSOR(AUTO_BRIGHTNESS_SENSOR) \ - \ - DEF_SENSOR_VALUE(GESTURE_MOVEMENT_SENSOR, 0x1200) \ - DEF_SENSOR(GESTURE_WRIST_UP_SENSOR) \ - DEF_SENSOR(GESTURE_WRIST_DOWN_SENSOR) \ - DEF_SENSOR(GESTURE_MOVEMENT_STATE_SENSOR) \ - DEF_SENSOR(GESTURE_FACE_DOWN_SENSOR) \ - \ - DEF_SENSOR_VALUE(ACTIVITY_TRACKER_SENSOR, 0x1A00) \ - DEF_SENSOR(ACTIVITY_LEVEL_MONITOR_SENSOR) \ - DEF_SENSOR(GPS_BATCH_SENSOR) \ - \ - DEF_SENSOR_VALUE(HRM_CTRL_SENSOR, 0x1A80) \ - \ - DEF_SENSOR_VALUE(WEAR_STATUS_SENSOR, 0x2000) \ - DEF_SENSOR(WEAR_ON_MONITOR_SENSOR) \ - DEF_SENSOR(NO_MOVE_DETECTOR_SENSOR) \ - DEF_SENSOR(RESTING_HR_SENSOR) \ - DEF_SENSOR(STEP_LEVEL_MONITOR_SENSOR) \ - DEF_SENSOR(EXERCISE_STANDALONE_SENSOR) \ - DEF_SENSOR(EXERCISE_HR_SENSOR) \ - DEF_SENSOR(WORKOUT_SENSOR) \ - DEF_SENSOR(CYCLE_MONITOR_SENSOR) \ - DEF_SENSOR(STAIR_TRACKER_SENSOR) \ - DEF_SENSOR(PRESSURE_INDICATOR_SENSOR) \ - DEF_SENSOR(PRESSURE_ALERT_SENSOR) \ - DEF_SENSOR(HR_CALORIE_SENSOR) \ - \ - DEF_SENSOR_VALUE(CONTEXT_SENSOR, 0x7000) \ - DEF_SENSOR(MOTION_SENSOR) \ - DEF_SENSOR(PIR_SENSOR) \ - DEF_SENSOR(PIR_LONG_SENSOR) \ - DEF_SENSOR(DUST_SENSOR) \ - DEF_SENSOR(THERMOMETER_SENSOR) \ - DEF_SENSOR(PEDOMETER_SENSOR) \ - DEF_SENSOR(FLAT_SENSOR) \ - DEF_SENSOR(HRM_RAW_SENSOR) \ - DEF_SENSOR(TILT_SENSOR) \ - DEF_SENSOR(RV_RAW_SENSOR) \ - DEF_SENSOR(GSR_SENSOR) \ - DEF_SENSOR(SIMSENSE_SENSOR) \ - DEF_SENSOR(PPG_SENSOR) \ - -#define BIO_HRM_SENSOR HRM_SENSOR -#define BIO_LED_GREEN_SENSOR HRM_LED_GREEN_SENSOR -#define BIO_LED_IR_SENSOR HRM_LED_IR_SENSOR -#define BIO_LED_RED_SENSOR HRM_LED_RED_SENSOR -#define BIO_SENSOR HRM_RAW_SENSOR -#define SLEEP_DETECTOR_SENSOR HUMAN_SLEEP_DETECTOR_SENSOR -#define STRESS_MONITOR_SENSOR HUMAN_STRESS_MONITOR_SENSOR -#define AUTOSESSION_EXERCISE_SENSOR WORKOUT_SENSOR -#define EXERCISE_COACH_SENSOR EXERCISE_STANDALONE_SENSOR -#define EXERCISE_SENSOR EXTERNAL_EXERCISE_SENSOR - -DECLARE_SENSOR_ENUM(sensor_type_t, SENSOR_TYPE) +typedef enum sensor_type_t { + UNKNOWN_SENSOR = -2, + ALL_SENSOR = -1, + ACCELEROMETER_SENSOR = 0, + GRAVITY_SENSOR, + LINEAR_ACCEL_SENSOR, + GEOMAGNETIC_SENSOR, + ROTATION_VECTOR_SENSOR, + ORIENTATION_SENSOR, + GYROSCOPE_SENSOR, + LIGHT_SENSOR, + PROXIMITY_SENSOR, + PRESSURE_SENSOR, + ULTRAVIOLET_SENSOR, + TEMPERATURE_SENSOR, + HUMIDITY_SENSOR, + HRM_SENSOR, + BIO_HRM_SENSOR = HRM_SENSOR, + HRM_LED_GREEN_SENSOR, + BIO_LED_GREEN_SENSOR = HRM_LED_GREEN_SENSOR, + HRM_LED_IR_SENSOR, + BIO_LED_IR_SENSOR = HRM_LED_IR_SENSOR, + HRM_LED_RED_SENSOR, + BIO_LED_RED_SENSOR = HRM_LED_RED_SENSOR, + GYROSCOPE_UNCAL_SENSOR, + GEOMAGNETIC_UNCAL_SENSOR, + GYROSCOPE_RV_SENSOR, + GEOMAGNETIC_RV_SENSOR, + + HUMAN_PEDOMETER_SENSOR = 0x300, + HUMAN_SLEEP_MONITOR_SENSOR, + HUMAN_SLEEP_DETECTOR_SENSOR, + SLEEP_DETECTOR_SENSOR = HUMAN_SLEEP_DETECTOR_SENSOR, + HUMAN_STRESS_MONITOR_SENSOR, + STRESS_MONITOR_SENSOR = HUMAN_STRESS_MONITOR_SENSOR, + + EXERCISE_WALKING_SENSOR = 0x400, + EXERCISE_RUNNING_SENSOR, + EXERCISE_HIKING_SENSOR, + EXERCISE_CYCLING_SENSOR, + EXERCISE_ELLIPTICAL_SENSOR, + EXERCISE_INDOOR_CYCLING_SENSOR, + EXERCISE_ROWING_SENSOR, + EXERCISE_STEPPER_SENSOR, + + EXTERNAL_EXERCISE_SENSOR = 0x800, + EXERCISE_SENSOR = EXTERNAL_EXERCISE_SENSOR, + + FUSION_SENSOR = 0x900, + AUTO_ROTATION_SENSOR, + AUTO_BRIGHTNESS_SENSOR, + + GESTURE_MOVEMENT_SENSOR = 0x1200, + GESTURE_WRIST_UP_SENSOR, + GESTURE_WRIST_DOWN_SENSOR, + GESTURE_MOVEMENT_STATE_SENSOR, + GESTURE_FACE_DOWN_SENSOR, + + ACTIVITY_TRACKER_SENSOR = 0x1A00, + ACTIVITY_LEVEL_MONITOR_SENSOR, + GPS_BATCH_SENSOR, + + HRM_CTRL_SENSOR = 0x1A80, + + WEAR_STATUS_SENSOR = 0x2000, + WEAR_ON_MONITOR_SENSOR, + NO_MOVE_DETECTOR_SENSOR, + RESTING_HR_SENSOR, + STEP_LEVEL_MONITOR_SENSOR, + EXERCISE_STANDALONE_SENSOR, + EXERCISE_COACH_SENSOR = EXERCISE_STANDALONE_SENSOR, + EXERCISE_HR_SENSOR, + WORKOUT_SENSOR, + AUTOSESSION_EXERCISE_SENSOR = WORKOUT_SENSOR, + CYCLE_MONITOR_SENSOR, + STAIR_TRACKER_SENSOR, + PRESSURE_INDICATOR_SENSOR, + PRESSURE_ALERT_SENSOR, + HR_CALORIE_SENSOR, + + CONTEXT_SENSOR = 0x7000, + MOTION_SENSOR, + PIR_SENSOR, + PIR_LONG_SENSOR, + DUST_SENSOR, + THERMOMETER_SENSOR, + PEDOMETER_SENSOR, + FLAT_SENSOR, + HRM_RAW_SENSOR, + BIO_SENSOR = HRM_RAW_SENSOR, + TILT_SENSOR, + RV_RAW_SENSOR, + GSR_SENSOR, + SIMSENSE_SENSOR, + PPG_SENSOR, + + CUSTOM_SENSOR = 0X9000, +} sensor_type_t; enum proxi_change_state { PROXIMITY_STATE_NEAR = 0, @@ -143,10 +141,6 @@ enum auto_rotation_state { } #endif -#ifdef __cplusplus -DECLARE_SENSOR_ENUM_UTIL_NS(sensor_type_t) -#endif - #include -#endif /* _SENSOR_TYPES_H_ */ +#endif /* __SENSOR_TYPES_H__ */ diff --git a/src/client/client.cpp b/src/client/client.cpp index 7fd43f5..0864b14 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -17,1258 +17,422 @@ * */ -#include -#include -#include -#include -#include -#include -#include -#include +#include "sensor_internal.h" +#include #include -#include -#include -#include -#include -#include "dbus_listener.h" -using std::vector; - -#ifndef API -#define API __attribute__((visibility("default"))) -#endif - -#ifndef VCONFKEY_SETAPPL_PSMODE -#define VCONFKEY_SETAPPL_PSMODE "db/setting/psmode" -#endif - -#define DEFAULT_INTERVAL POLL_10HZ_MS - -#define CONVERT_OPTION_PAUSE_POLICY(option) ((option) ^ 0b11) - -static cmutex lock; - -static int g_power_save_state = SENSORD_PAUSE_NONE; - -static int get_power_save_state(void); -static void power_save_state_cb(keynode_t *node, void *data); -static void clean_up(void); -static bool change_sensor_rep(sensor_id_t sensor_id, sensor_rep &prev_rep, sensor_rep &cur_rep); -static void restore_session(void); -static bool register_event(int handle, unsigned int event_type, unsigned int interval, int max_batch_latency, void* cb, void *user_data); - -class initiator { -public: - initiator() - { - sensor_event_listener::get_instance().set_hup_observer(restore_session); - } -}; - -void good_bye(void) -{ - _I("Good bye! %s\n", get_client_name()); - clean_up(); -} - -static initiator g_initiator; - -static int g_power_save_state_cb_cnt = 0; - -static void set_power_save_state_cb(void) -{ - if (g_power_save_state_cb_cnt < 0) - _E("g_power_save_state_cb_cnt(%d) is wrong", g_power_save_state_cb_cnt); - - ++g_power_save_state_cb_cnt; - - if (g_power_save_state_cb_cnt == 1) { - _D("Power save callback is registered"); - g_power_save_state = get_power_save_state(); - _D("power_save_state = [%d]", g_power_save_state); - vconf_notify_key_changed(VCONFKEY_PM_STATE, power_save_state_cb, NULL); - vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE, power_save_state_cb, NULL); - } -} - -static void unset_power_save_state_cb(void) -{ - --g_power_save_state_cb_cnt; - - if (g_power_save_state_cb_cnt < 0) - _E("g_power_save_state_cb_cnt(%d) is wrong", g_power_save_state_cb_cnt); - - if (g_power_save_state_cb_cnt == 0) { - _D("Power save callback is unregistered"); - vconf_ignore_key_changed(VCONFKEY_PM_STATE, power_save_state_cb); - vconf_ignore_key_changed(VCONFKEY_SETAPPL_PSMODE, power_save_state_cb); - } -} - -void clean_up(void) -{ - handle_vector handles; - - sensor_client_info::get_instance().get_all_handles(handles); - - auto it_handle = handles.begin(); - - while (it_handle != handles.end()) { - sensord_disconnect(*it_handle); - ++it_handle; - } - - sensor_event_listener::get_instance().clear(); -} - -static int get_power_save_state(void) -{ - int ret; - int state = 0; - int pm_state, ps_state; - - ret = vconf_get_int(VCONFKEY_PM_STATE, &pm_state); - - if (!ret && pm_state == VCONFKEY_PM_STATE_LCDOFF) - state |= SENSORD_PAUSE_ON_DISPLAY_OFF; - - ret = vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &ps_state); - - if (!ret && ps_state != SETTING_PSMODE_NORMAL) - state |= SENSORD_PAUSE_ON_POWERSAVE_MODE; - - return state; -} - -static void power_save_state_cb(keynode_t *node, void *data) -{ - int cur_power_save_state; - sensor_id_vector sensors; - sensor_rep prev_rep, cur_rep; - - AUTOLOCK(lock); - - cur_power_save_state = get_power_save_state(); - - if (cur_power_save_state == g_power_save_state) { - _D("g_power_save_state NOT changed : [%d]", cur_power_save_state); - return; - } - - g_power_save_state = cur_power_save_state; - _D("power_save_state: %d noti to %s", g_power_save_state, get_client_name()); - - sensor_client_info::get_instance().get_listening_sensors(sensors); - - auto it_sensor = sensors.begin(); - - while (it_sensor != sensors.end()) { - sensor_client_info::get_instance().get_sensor_rep(*it_sensor, prev_rep); - sensor_client_info::get_instance().set_pause_policy(*it_sensor, cur_power_save_state); - sensor_client_info::get_instance().get_sensor_rep(*it_sensor, cur_rep); - change_sensor_rep(*it_sensor, prev_rep, cur_rep); - - ++it_sensor; - } -} - -bool restore_attributes(int client_id, sensor_id_t sensor, command_channel *cmd_channel) -{ - sensor_handle_info_map handle_infos; - - sensor_client_info::get_instance().get_sensor_handle_info(sensor, handle_infos); - - for (auto it_handles = handle_infos.begin(); it_handles != handle_infos.end(); ++it_handles) { - sensor_handle_info info = it_handles->second; - - for (auto it = info.attributes_int.begin(); it != info.attributes_int.end(); ++it) { - int attribute = it->first; - int value = it->second; - if (!cmd_channel->cmd_set_attribute_int(attribute, value)) { - _E("Failed to send cmd_set_attribute_int(%d, %d) for %s", - client_id, value, get_client_name()); - return false; - } - } - - for (auto it = info.attributes_str.begin(); it != info.attributes_str.end(); ++it) { - int attribute = it->first; - const char *value = it->second->get(); - int len = it->second->size(); - if (!cmd_channel->cmd_set_attribute_str(attribute, value, len)) { - _E("Failed to send cmd_set_attribute_str(%d, %d, %s) for %s", - client_id, len, value, get_client_name()); - return false; - } - } - } - - return true; -} - -void restore_session(void) -{ - AUTOLOCK(lock); - - _I("Trying to restore sensor client session for %s", get_client_name()); - - command_channel *cmd_channel; - int client_id; - - sensor_client_info::get_instance().close_command_channel(); - sensor_client_info::get_instance().set_client_id(CLIENT_ID_INVALID); - - sensor_id_vector sensors; - - sensor_client_info::get_instance().get_listening_sensors(sensors); - - bool first_connection = true; - - auto it_sensor = sensors.begin(); - - while (it_sensor != sensors.end()) { - cmd_channel = new(std::nothrow) command_channel(); - retm_if(!cmd_channel, "Failed to allocate memory"); - - if (!cmd_channel->create_channel()) { - _E("%s failed to create command channel for %s", get_client_name(), get_sensor_name(*it_sensor)); - delete cmd_channel; - goto FAILED; - } - - sensor_client_info::get_instance().add_command_channel(*it_sensor, cmd_channel); - - if (first_connection) { - first_connection = false; - if (!cmd_channel->cmd_get_id(client_id)) { - _E("Failed to get client id"); - goto FAILED; - } - - sensor_client_info::get_instance().set_client_id(client_id); - sensor_event_listener::get_instance().start_event_listener(); - } - - cmd_channel->set_client_id(client_id); - - if (!cmd_channel->cmd_hello(*it_sensor)) { - _E("Sending cmd_hello(%s, %d) failed for %s", get_sensor_name(*it_sensor), client_id, get_client_name()); - goto FAILED; - } - - sensor_rep prev_rep, cur_rep; - prev_rep.active = false; - prev_rep.pause_policy = SENSORD_PAUSE_ALL; - prev_rep.interval = 0; - - sensor_client_info::get_instance().get_sensor_rep(*it_sensor, cur_rep); - if (!change_sensor_rep(*it_sensor, prev_rep, cur_rep)) { - _E("Failed to change rep(%s) for %s", get_sensor_name(*it_sensor), get_client_name()); - goto FAILED; - } - - if (!restore_attributes(client_id, *it_sensor, cmd_channel)) { - _E("Failed to restore attributes(%s) for %s", get_sensor_name(*it_sensor), get_client_name()); - goto FAILED; - } - ++it_sensor; - } - - _I("Succeeded to restore sensor client session for %s", get_client_name()); - - return; - -FAILED: - sensor_event_listener::get_instance().clear(); - _E("Failed to restore session for %s", get_client_name()); -} - -static bool get_events_diff(event_type_vector &a_vec, event_type_vector &b_vec, event_type_vector &add_vec, event_type_vector &del_vec) -{ - sort(a_vec.begin(), a_vec.end()); - sort(b_vec.begin(), b_vec.end()); - - set_difference(a_vec.begin(), a_vec.end(), b_vec.begin(), b_vec.end(), back_inserter(del_vec)); - set_difference(b_vec.begin(), b_vec.end(), a_vec.begin(), a_vec.end(), back_inserter(add_vec)); - - return !(add_vec.empty() && del_vec.empty()); -} - -static bool change_sensor_rep(sensor_id_t sensor_id, sensor_rep &prev_rep, sensor_rep &cur_rep) -{ - int client_id; - command_channel *cmd_channel; - event_type_vector add_event_types, del_event_types; - - if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) { - _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id)); - return false; - } - - client_id = sensor_client_info::get_instance().get_client_id(); - retvm_if((client_id < 0), false, "Invalid client id : %d, %s, %s", client_id, get_sensor_name(sensor_id), get_client_name()); - - get_events_diff(prev_rep.event_types, cur_rep.event_types, add_event_types, del_event_types); - - if (cur_rep.active) { - if (prev_rep.pause_policy != cur_rep.pause_policy) { - if (!cmd_channel->cmd_set_pause_policy(cur_rep.pause_policy)) { - _E("Sending cmd_set_pause_policy(%d, %s, %d) failed for %s", client_id, get_sensor_name(sensor_id), cur_rep.pause_policy, get_client_name()); - return false; - } - } - - if ( (prev_rep.interval != cur_rep.interval) || (prev_rep.latency != cur_rep.latency)) { - if (!cmd_channel->cmd_set_batch(cur_rep.interval, cur_rep.latency)) { - _E("Sending cmd_set_batch(%d, %s, %d, %d) failed for %s", client_id, get_sensor_name(sensor_id), cur_rep.interval, cur_rep.latency, get_client_name()); - return false; - } - } - - if (!add_event_types.empty()) { - if (!cmd_channel->cmd_register_events(add_event_types)) { - _E("Sending cmd_register_events(%d, add_event_types) failed for %s", client_id, get_client_name()); - return false; - } - } - } - - if (prev_rep.active && !del_event_types.empty()) { - if (!cmd_channel->cmd_unregister_events(del_event_types)) { - _E("Sending cmd_unregister_events(%d, del_event_types) failed for %s", client_id, get_client_name()); - return false; - } - } - - if (prev_rep.active != cur_rep.active) { - if (cur_rep.active) { - if (!cmd_channel->cmd_start()) { - _E("Sending cmd_start(%d, %s) failed for %s", client_id, get_sensor_name(sensor_id), get_client_name()); - return false; - } - } else { - if (!cmd_channel->cmd_unset_batch()) { - _E("Sending cmd_unset_interval(%d, %s) failed for %s", client_id, get_sensor_name(sensor_id), get_client_name()); - return false; - } - - if (!cmd_channel->cmd_stop()) { - _E("Sending cmd_stop(%d, %s) failed for %s", client_id, get_sensor_name(sensor_id), get_client_name()); - return false; - } - } - } - - return true; -} - -static bool get_sensor_list(void) -{ - static cmutex l; - static bool init = false; - - AUTOLOCK(l); - - if (!init) { - command_channel cmd_channel; - - if (!cmd_channel.create_channel()) { - _E("%s failed to create command channel", get_client_name()); - return false; - } - - if (!cmd_channel.cmd_get_sensor_list()) - return false; - - init = true; - } - - return true; -} +/* + * TO-DO-LIST: + * 1. restore session + * 1.1 close socket + * 1.2 listener : start, interval, batch latency, attributes + * 2. power save option / lcd vconf : move to server + * 3. clean up data + * 4. thread-safe : ipc_client + * 5. client-id + * 6. cmd_hello + * 7. stop : unset interval, batch + * 8. get sensor list + */ -API int sensord_get_sensors(sensor_type_t type, sensor_t **list, int *sensor_count) +API int sensord_get_sensors(sensor_type_t type, sensor_t **list, int *count) { - retvm_if(!get_sensor_list(), -EPERM, "Fail to get sensor list from server"); - - vector sensor_infos = sensor_info_manager::get_instance().get_infos(type); - - if (sensor_infos.empty()) { - *sensor_count = 0; - return -ENODATA; - } - - if (type != ALL_SENSOR) { - if (sensor_infos[0]->get_id() == (sensor_id_t)(-EACCES)) - return -EACCES; - } - - *list = (sensor_t *) malloc(sizeof(sensor_info *) * sensor_infos.size()); - retvm_if(!*list, false, "Failed to allocate memory"); - - for (unsigned int i = 0; i < sensor_infos.size(); ++i) - *(*list + i) = sensor_info_to_sensor(sensor_infos[i]); - - *sensor_count = sensor_infos.size(); + /* + * 1. get sensor list() + * 2. if sensor list is empty, return -ENODATA + * 3. if id is -EACCESS, return -EACCESS (except ALL_SENSOR) + * 4. list : memory allocation + * 5. return list, count + */ return OP_SUCCESS; } API int sensord_get_default_sensor(sensor_type_t type, sensor_t *sensor) { - retvm_if(!get_sensor_list(), -EPERM, "Fail to get sensor list from server"); - - const sensor_info *info; - - info = sensor_info_manager::get_instance().get_info(type); - if (info == NULL) { - *sensor = NULL; - return -ENODATA; - } - - if ((const_cast(info))->get_id() == (sensor_id_t)(-EACCES)) - return -EACCES; - - *sensor = sensor_info_to_sensor(info); + /* + * 1. get sensor list() + * 2. if there is no sensor, return -ENODATA + * 3. if id is -EACCESS, return -EACCESS + * 4. if SENSOR_ALL, ??? + * 5. return sensor + */ return OP_SUCCESS; } -API bool sensord_get_sensor_list(sensor_type_t type, sensor_t **list, int *sensor_count) -{ - return (sensord_get_sensors(type, list, sensor_count) == OP_SUCCESS); -} - -API sensor_t sensord_get_sensor(sensor_type_t type) -{ - sensor_t sensor; - sensord_get_default_sensor(type, &sensor); - - return sensor; -} - API bool sensord_get_type(sensor_t sensor, sensor_type_t *type) { - sensor_info* info = sensor_to_sensor_info(sensor); - - retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !type, - NULL, "Invalid param: sensor (%p), type(%p)", sensor, type); - - *type = info->get_type(); + /* + * 1. check parameter + * 2. if there is no sensor, return false + */ return true; } API const char* sensord_get_name(sensor_t sensor) { - sensor_info* info = sensor_to_sensor_info(sensor); - - retvm_if(!sensor_info_manager::get_instance().is_valid(info), - NULL, "Invalid param: sensor (%p)", sensor); + /* + * 1. if there is no sensor, return NULL + */ - return info->get_name(); + return NULL; } API const char* sensord_get_vendor(sensor_t sensor) { - sensor_info* info = sensor_to_sensor_info(sensor); + /* + * 1. if there is no sensor, return NULL + */ - retvm_if(!sensor_info_manager::get_instance().is_valid(info), - NULL, "Invalid param: sensor (%p)", sensor); - - return info->get_vendor(); + return NULL; } API bool sensord_get_privilege(sensor_t sensor, sensor_privilege_t *privilege) { - sensor_info* info = sensor_to_sensor_info(sensor); - - retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !privilege, - false, "Invalid param: sensor (%p), privilege(%p)", sensor, privilege); - - *privilege = info->get_privilege(); + /* + * 1. check parameter + * 2. if there is no sensor, return false + */ return true; } API bool sensord_get_min_range(sensor_t sensor, float *min_range) { - sensor_info* info = sensor_to_sensor_info(sensor); - - retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !min_range, - false, "Invalid param: sensor (%p), min_range(%p)", sensor, min_range); - - *min_range = info->get_min_range(); + /* + * 1. check parameter + * 2. if there is no sensor, return false + */ return true; } API bool sensord_get_max_range(sensor_t sensor, float *max_range) { - sensor_info* info = sensor_to_sensor_info(sensor); - - retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !max_range, - false, "Invalid param: sensor (%p), max_range(%p)", sensor, max_range); - - *max_range = info->get_max_range(); + /* + * 1. check parameter + * 2. if there is no sensor, return false + */ return true; } API bool sensord_get_resolution(sensor_t sensor, float *resolution) { - sensor_info* info = sensor_to_sensor_info(sensor); - - retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !resolution, - false, "Invalid param: sensor (%p), resolution(%p)", sensor, resolution); - - *resolution = info->get_resolution(); + /* + * 1. check parameter + * 2. if there is no sensor, return false + */ return true; } API bool sensord_get_min_interval(sensor_t sensor, int *min_interval) { - sensor_info* info = sensor_to_sensor_info(sensor); - - retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !min_interval, - false, "Invalid param: sensor (%p), min_interval(%p)", sensor, min_interval); - - *min_interval = info->get_min_interval(); + /* + * 1. check parameter + * 2. if there is no sensor, return false + */ return true; } API bool sensord_get_fifo_count(sensor_t sensor, int *fifo_count) { - sensor_info* info = sensor_to_sensor_info(sensor); - - retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !fifo_count, - false, "Invalid param: sensor (%p), fifo_count(%p)", sensor, fifo_count); - - *fifo_count = info->get_fifo_count(); + /* + * 1. check parameter + * 2. if there is no sensor, return false + */ return true; } API bool sensord_get_max_batch_count(sensor_t sensor, int *max_batch_count) { - sensor_info* info = sensor_to_sensor_info(sensor); - - retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !max_batch_count, - false, "Invalid param: sensor (%p), max_batch_count(%p)", sensor, max_batch_count); - - *max_batch_count = info->get_max_batch_count(); - - return true; -} - -API bool sensord_get_supported_event_types(sensor_t sensor, unsigned int **event_types, int *count) -{ - sensor_info* info = sensor_to_sensor_info(sensor); - - retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !event_types || !count, - false, "Invalid param: sensor (%p), event_types(%p), count(%p)", sensor, event_types, count); - - unsigned int event_type; - event_type = info->get_supported_event(); - *event_types = (unsigned int *)malloc(sizeof(unsigned int)); - - retvm_if(!*event_types, false, "Failed to allocate memory"); - - (*event_types)[0] = event_type; - *count = 1; - - return true; -} - -API bool sensord_is_supported_event_type(sensor_t sensor, unsigned int event_type, bool *supported) -{ - sensor_info* info = sensor_to_sensor_info(sensor); - - retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !event_type || !supported, - false, "Invalid param: sensor (%p), event_type(%p), supported(%p)", sensor, event_type, supported); - - *supported = info->is_supported_event(event_type); + /* + * 1. check parameter + * 2. if there is no sensor, return false + */ return true; } API bool sensord_is_wakeup_supported(sensor_t sensor) { - sensor_info* info = sensor_to_sensor_info(sensor); - - retvm_if(!sensor_info_manager::get_instance().is_valid(info), - false, "Invalid param: sensor (%p)", sensor); + /* + * 1. check parameter + * 2. if there is no sensor, return false + */ - return info->is_wakeup_supported(); + return true; } API int sensord_connect(sensor_t sensor) { - command_channel *cmd_channel = NULL; - int handle; - int client_id; - bool sensor_registered; - bool first_connection = false; - - sensor_info* info = sensor_to_sensor_info(sensor); - retv_if(!sensor_info_manager::get_instance().is_valid(info), OP_ERROR); - - sensor_id_t sensor_id = info->get_id(); - - AUTOLOCK(lock); - - sensor_registered = sensor_client_info::get_instance().is_sensor_registered(sensor_id); - - // lazy loading after creating static variables - atexit(good_bye); - - handle = sensor_client_info::get_instance().create_handle(sensor_id); - if (handle == MAX_HANDLE) { - _E("Maximum number of handles reached, sensor: %s in client %s", get_sensor_name(sensor_id), get_client_name()); - return OP_ERROR; - } - - if (!sensor_registered) { - cmd_channel = new(std::nothrow) command_channel(); - if (!cmd_channel) { - _E("Failed to allocated memory"); - sensor_client_info::get_instance().delete_handle(handle); - return OP_ERROR; - } - - if (!cmd_channel->create_channel()) { - _E("%s failed to create command channel for %s", get_client_name(), get_sensor_name(sensor_id)); - sensor_client_info::get_instance().delete_handle(handle); - delete cmd_channel; - return OP_ERROR; - } - - sensor_client_info::get_instance().add_command_channel(sensor_id, cmd_channel); - } - - if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) { - _E("%s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id)); - sensor_client_info::get_instance().delete_handle(handle); - return OP_ERROR; - } - - if (!sensor_client_info::get_instance().has_client_id()) { - first_connection = true; - if (!cmd_channel->cmd_get_id(client_id)) { - _E("Sending cmd_get_id() failed for %s", get_sensor_name(sensor_id)); - sensor_client_info::get_instance().close_command_channel(sensor_id); - sensor_client_info::get_instance().delete_handle(handle); - return OP_ERROR; - } - - sensor_client_info::get_instance().set_client_id(client_id); - _I("%s gets client_id [%d]", get_client_name(), client_id); - sensor_event_listener::get_instance().start_event_listener(); - _I("%s starts listening events with client_id [%d]", get_client_name(), client_id); - } - - client_id = sensor_client_info::get_instance().get_client_id(); - cmd_channel->set_client_id(client_id); - - _I("%s[%d] connects with %s[%d]", get_client_name(), client_id, get_sensor_name(sensor_id), handle); - - sensor_client_info::get_instance().set_sensor_params(handle, SENSOR_STATE_STOPPED, SENSORD_PAUSE_ALL); - - if (!sensor_registered) { - if (!cmd_channel->cmd_hello(sensor_id)) { - _E("Sending cmd_hello(%s, %d) failed for %s", get_sensor_name(sensor_id), client_id, get_client_name()); - sensor_client_info::get_instance().close_command_channel(sensor_id); - sensor_client_info::get_instance().delete_handle(handle); - if (first_connection) { - sensor_client_info::get_instance().set_client_id(CLIENT_ID_INVALID); - sensor_event_listener::get_instance().stop_event_listener(); - } - return OP_ERROR; - } - } - - set_power_save_state_cb(); - dbus_listener::init(); - return handle; + /* + * 1. check parameter + * 2. if there is no sensor, return -EPERM + * 3. sensor is already registered(sensor), it doesn't need to connect server + * 4. create integer handle for only this client + * 5. if it is first connection(client), get client id from server + * 6. if it is first connection(client), start sensor event listener + * 7. sensor initialization : stop, pause_all + * 8. if it is first connection(client), send cmd hello + * 9. if cmd hello is failed and it is first connection(client) stop listener, remove id + */ + + return 0; } API bool sensord_disconnect(int handle) { - command_channel *cmd_channel; - sensor_id_t sensor_id; - int client_id; - int sensor_state; - - AUTOLOCK(lock); - - if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state)|| - !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) { - _E("client %s failed to get handle information", get_client_name()); - return false; - } - - if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) { - _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id)); - return false; - } - - client_id = sensor_client_info::get_instance().get_client_id(); - retvm_if((client_id < 0), false, "Invalid client id : %d, handle: %d, %s, %s", client_id, handle, get_sensor_name(sensor_id), get_client_name()); - - _I("%s disconnects with %s[%d]", get_client_name(), get_sensor_name(sensor_id), handle); - - if (sensor_client_info::get_instance().get_passive_mode(handle)) { - _W("%s[%d] for %s is on passive mode while disconnecting.", - get_sensor_name(sensor_id), handle, get_client_name()); - - command_channel *cmd_channel; - event_type_vector event_types; - sensor_client_info::get_instance().get_active_event_types(sensor_id, event_types); - - for (auto it = event_types.begin(); it != event_types.end(); ++it) - sensord_unregister_event(handle, *it); - - if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) { - _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id)); - return false; - } - - if (!cmd_channel->cmd_unset_batch()) { - _E("Sending cmd_unset_interval(%d, %s) failed for %s", client_id, get_sensor_name(sensor_id), get_client_name()); - return false; - } - } - - if (sensor_state != SENSOR_STATE_STOPPED) { - _W("%s[%d] for %s is not stopped before disconnecting.", - get_sensor_name(sensor_id), handle, get_client_name()); - sensord_stop(handle); - } - - if (!sensor_client_info::get_instance().delete_handle(handle)) - return false; - - if (!sensor_client_info::get_instance().is_active()) - sensor_client_info::get_instance().set_client_id(CLIENT_ID_INVALID); - - if (!sensor_client_info::get_instance().is_sensor_registered(sensor_id)) { - if (!cmd_channel->cmd_byebye()) { - _E("Sending cmd_byebye(%d, %s) failed for %s", client_id, get_sensor_name(sensor_id), get_client_name()); - return false; - } - sensor_client_info::get_instance().close_command_channel(sensor_id); - } - - if (!sensor_client_info::get_instance().is_active()) { - _I("Stop listening events for client %s with client id [%d]", get_client_name(), sensor_client_info::get_instance().get_client_id()); - sensor_event_listener::get_instance().stop_event_listener(); - } - - unset_power_save_state_cb(); + /* + * 1. check parameter(check handle???) + * 2. check state of this handle + * 3. if it is on passive mode, unregister event and unset interval/latency + * 4. if it is not stop, stop it + * 5. if there is no active sensor(client), reset id & byebye and stop listener + */ return true; } -static bool register_event(int handle, unsigned int event_type, unsigned int interval, int max_batch_latency, void* cb, void *user_data) +API bool sensord_register_event(int handle, unsigned int event_type, + unsigned int interval, unsigned int max_batch_latency, sensor_cb_t cb, void *user_data) { - sensor_id_t sensor_id; - sensor_rep prev_rep, cur_rep; - bool ret; - - retvm_if(!cb, false, "callback is NULL"); + /* + * 1. check parameter + * 2. if interval is 0, default interval + * ** if cb is null, how to handle it? + * ** event type is deprecated + */ - AUTOLOCK(lock); - - if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) { - _W("client %s failed to get handle information", get_client_name()); - return false; - } - - if (interval == 0) - interval = DEFAULT_INTERVAL; - - _I("%s registers event %s[%#x] for sensor %s[%d] with interval: %d, latency: %d, cb: %#x, user_data: %#x", - get_client_name(), get_event_name(event_type), event_type, get_sensor_name(sensor_id), - handle, interval, max_batch_latency, cb, user_data); - - sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep); - sensor_client_info::get_instance().register_event(handle, event_type, interval, max_batch_latency, cb, user_data); - sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep); - ret = change_sensor_rep(sensor_id, prev_rep, cur_rep); - - if (!ret) - sensor_client_info::get_instance().unregister_event(handle, event_type); - - return ret; -} - -API bool sensord_register_event(int handle, unsigned int event_type, unsigned int interval, unsigned int max_batch_latency, sensor_cb_t cb, void *user_data) -{ - return register_event(handle, event_type, interval, max_batch_latency, (void *)cb, user_data); + return true; } API bool sensord_unregister_event(int handle, unsigned int event_type) { - sensor_id_t sensor_id; - sensor_rep prev_rep, cur_rep; - bool ret; - unsigned int prev_interval, prev_latency; - void *prev_cb; - void *prev_user_data; - - AUTOLOCK(lock); - - if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) { - _E("client %s failed to get handle information", get_client_name()); - return false; - } - - _I("%s unregisters event %s[%#x] for sensor %s[%d]", get_client_name(), get_event_name(event_type), - event_type, get_sensor_name(sensor_id), handle); - - sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep); - sensor_client_info::get_instance().get_event_info(handle, event_type, prev_interval, prev_latency, prev_cb, prev_user_data); - - if (!sensor_client_info::get_instance().unregister_event(handle, event_type)) { - _E("%s try to unregister non registered event %s[%#x] for sensor %s[%d]", - get_client_name(), get_event_name(event_type), event_type, get_sensor_name(sensor_id), handle); - return false; - } - - sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep); - ret = change_sensor_rep(sensor_id, prev_rep, cur_rep); - - if (sensor_client_info::get_instance().get_passive_mode(handle)) - sensor_client_info::get_instance().set_passive_mode(handle, false); - - if (!ret) - sensor_client_info::get_instance().register_event(handle, event_type, prev_interval, prev_latency, prev_cb, prev_user_data); + /* + * 1. check parameter + * 2. check previous interval, latency, cb, user_data + * 3. if passive mode is true, set false + * 4. if ret is false, register event + */ - return ret; + return true; } API bool sensord_register_accuracy_cb(int handle, sensor_accuracy_changed_cb_t cb, void *user_data) { - sensor_id_t sensor_id; - - retvm_if(!cb, false, "callback is NULL"); - - AUTOLOCK(lock); - - if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) { - _E("client %s failed to get handle information", get_client_name()); - return false; - } - - _I("%s registers accuracy_changed_cb for sensor %s[%d] with cb: %#x, user_data: %#x", - get_client_name(), get_sensor_name(sensor_id), handle, cb, user_data); - - sensor_client_info::get_instance().register_accuracy_cb(handle, cb , user_data); + /* + * 1. check parameter + */ return true; } API bool sensord_unregister_accuracy_cb(int handle) { - sensor_id_t sensor_id; - - AUTOLOCK(lock); - - if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) { - _E("client %s failed to get handle information", get_client_name()); - return false; - } - - _I("%s unregisters accuracy_changed_cb for sensor %s[%d]", - get_client_name(), get_sensor_name(sensor_id), handle); - - sensor_client_info::get_instance().unregister_accuracy_cb(handle); + /* + * 1. check parameter + */ return true; } API bool sensord_start(int handle, int option) { - sensor_id_t sensor_id; - sensor_rep prev_rep, cur_rep; - bool ret; - int prev_state, prev_pause; - int pause; - - AUTOLOCK(lock); - - if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) { - _E("client %s failed to get handle information", get_client_name()); - return false; - } - - retvm_if((option < 0) || (option >= SENSOR_OPTION_END), false, "Invalid option value : %d, handle: %d, %s, %s", - option, handle, get_sensor_name(sensor_id), get_client_name()); - - _I("%s starts %s[%d], with option: %d, power save state: %d", get_client_name(), get_sensor_name(sensor_id), - handle, option, g_power_save_state); - - pause = CONVERT_OPTION_PAUSE_POLICY(option); - - if (g_power_save_state && (g_power_save_state & pause)) { - sensor_client_info::get_instance().set_sensor_params(handle, SENSOR_STATE_PAUSED, pause); - return true; - } - - sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep); - sensor_client_info::get_instance().get_sensor_params(handle, prev_state, prev_pause); - sensor_client_info::get_instance().set_sensor_params(handle, SENSOR_STATE_STARTED, pause); - sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep); - - ret = change_sensor_rep(sensor_id, prev_rep, cur_rep); - - if (!ret) - sensor_client_info::get_instance().set_sensor_params(handle, prev_state, prev_pause); + /* + * 1. check parameter + * 2. pause = CONVERT_OPTION_PAUSE_POLICY(option) + * 3. start listener + */ - return ret; + return true; } API bool sensord_stop(int handle) { - sensor_id_t sensor_id; - int sensor_state; - bool ret; - int prev_state, prev_pause; - - sensor_rep prev_rep, cur_rep; - - AUTOLOCK(lock); - - if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state)|| - !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) { - _E("client %s failed to get handle information", get_client_name()); - return false; - } - - retvm_if((sensor_state == SENSOR_STATE_STOPPED), true, "%s already stopped with %s[%d]", - get_client_name(), get_sensor_name(sensor_id), handle); - - _I("%s stops sensor %s[%d]", get_client_name(), get_sensor_name(sensor_id), handle); - - sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep); - sensor_client_info::get_instance().get_sensor_params(handle, prev_state, prev_pause); - sensor_client_info::get_instance().set_sensor_state(handle, SENSOR_STATE_STOPPED); - sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep); - - ret = change_sensor_rep(sensor_id, prev_rep, cur_rep); - - if (!ret) - sensor_client_info::get_instance().set_sensor_params(handle, prev_state, prev_pause); + /* + * 1. check parameter + * 2. check sensor state, id + * 2.1. if sensor is already stopped, return true + * 3. stop listener + */ - return ret; -} - -static bool change_event_batch(int handle, unsigned int event_type, unsigned int interval, unsigned int latency) -{ - sensor_id_t sensor_id; - sensor_rep prev_rep, cur_rep; - bool ret; - unsigned int prev_interval, prev_latency; - void *prev_cb; - void *prev_user_data; - - AUTOLOCK(lock); - - if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) { - _E("client %s failed to get handle information", get_client_name()); - return false; - } - - if (interval == 0) - interval = DEFAULT_INTERVAL; - - _I("%s changes batch of event %s[%#x] for %s[%d] to (%d, %d)", get_client_name(), get_event_name(event_type), - event_type, get_sensor_name(sensor_id), handle, interval, latency); - - sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep); - - sensor_client_info::get_instance().get_event_info(handle, event_type, prev_interval, prev_latency, prev_cb, prev_user_data); - - if (!sensor_client_info::get_instance().set_event_batch(handle, event_type, interval, latency)) - return false; - - sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep); - - ret = change_sensor_rep(sensor_id, prev_rep, cur_rep); - - if (!ret) - sensor_client_info::get_instance().set_event_batch(handle, event_type, prev_interval, prev_latency); - - return ret; + return true; } API bool sensord_change_event_interval(int handle, unsigned int event_type, unsigned int interval) { - unsigned int prev_interval, prev_latency; - void *prev_cb; - void *prev_user_data; - - AUTOLOCK(lock); + /* + * 1. check parameter + * 2. if interval is 0, default interval + * 3. if previous interval is lower than interval, return true + * 4. change interval + */ - if (!sensor_client_info::get_instance().get_event_info(handle, event_type, prev_interval, prev_latency, prev_cb, prev_user_data)) { - _E("Failed to get event info with handle = %d, event_type = %#x", handle, event_type); - return false; - } - - _I("handle = %d, event_type = %#x, interval = %d, prev_latency = %d", handle, event_type, interval, prev_latency); - return change_event_batch(handle, event_type, interval, prev_latency); + return true; } API bool sensord_change_event_max_batch_latency(int handle, unsigned int event_type, unsigned int max_batch_latency) { - unsigned int prev_interval, prev_latency; - void *prev_cb; - void *prev_user_data; + /* + * 1. check parameter + * 2. if previous interval is lower than interval, return true + * 3. change batch latency + */ - AUTOLOCK(lock); - - if (!sensor_client_info::get_instance().get_event_info(handle, event_type, prev_interval, prev_latency, prev_cb, prev_user_data)) { - _E("Failed to get event info with handle = %d, event_type = %#x", handle, event_type); - return false; - } - - return change_event_batch(handle, event_type, prev_interval, max_batch_latency); + return true; } -static int change_pause_policy(int handle, int pause) +API bool sensord_set_option(int handle, int option) { - sensor_id_t sensor_id; - sensor_rep prev_rep, cur_rep; - int sensor_state; - bool ret; - int prev_state, prev_pause; - - AUTOLOCK(lock); - - retvm_if((pause < 0) || (pause >= SENSORD_PAUSE_END), -EINVAL, - "Invalid pause value : %d, handle: %d, %s, %s", - pause, handle, get_sensor_name(sensor_id), get_client_name()); - - if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state)|| - !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) { - _E("client %s failed to get handle information", get_client_name()); - return -EPERM; - } - - sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep); - sensor_client_info::get_instance().get_sensor_params(handle, prev_state, prev_pause); - - if (g_power_save_state) { - if ((pause & g_power_save_state) && (sensor_state == SENSOR_STATE_STARTED)) - sensor_client_info::get_instance().set_sensor_state(handle, SENSOR_STATE_PAUSED); - else if (!(pause & g_power_save_state) && (sensor_state == SENSOR_STATE_PAUSED)) - sensor_client_info::get_instance().set_sensor_state(handle, SENSOR_STATE_STARTED); - } - sensor_client_info::get_instance().set_sensor_pause_policy(handle, pause); - - sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep); - ret = change_sensor_rep(sensor_id, prev_rep, cur_rep); - - if (!ret) - sensor_client_info::get_instance().set_sensor_pause_policy(handle, prev_pause); - - return (ret ? OP_SUCCESS : OP_ERROR); + /* + * change option, always-on/power save option/lcd off/default + */ + + return true; } -static int change_axis_orientation(int handle, int axis_orientation) +API int sensord_set_attribute_int(int handle, int attribute, int value) { - sensor_event_listener::get_instance().set_sensor_axis(axis_orientation); + /* + * 1. if ATTRIBUTE_PAUSE_POLICY + * 2. if ATTRIBUTE_AXIS_ORIENTATION + * 3. else attribute + */ + return OP_SUCCESS; } -static int change_attribute_int(int handle, int attribute, int value) +API int sensord_set_attribute_str(int handle, int attribute, const char *value, int len) { - sensor_id_t sensor_id; - command_channel *cmd_channel; - int client_id; + /* + * 1. check parameter + * 2. if client id is invalid, return -EPERM + * 3. if there is other problems, return -EPERM + */ - AUTOLOCK(lock); + return OP_SUCCESS; +} - if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) { - _E("client %s failed to get handle information", get_client_name()); - return -EINVAL; - } +API bool sensord_get_data(int handle, unsigned int data_id, sensor_data_t* sensor_data) +{ + /* + * 1. check parameter + * 2. check sensor state(is it really needed?) + * 3. get data + */ - if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) { - _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id)); - return -EPERM; - } + return true; +} - client_id = sensor_client_info::get_instance().get_client_id(); - retvm_if((client_id < 0), -EPERM, - "Invalid client id : %d, handle: %d, %s, %s", - client_id, handle, get_sensor_name(sensor_id), get_client_name()); +API bool sensord_flush(int handle) +{ + /* + * 1. check parameter + * 2. check sensor state(is it really needed?) + * 3. flush sensor + */ - if (!cmd_channel->cmd_set_attribute_int(attribute, value)) { - _E("Sending cmd_set_attribute_int(%d, %d) failed for %s", - client_id, value, get_client_name()); - return -EPERM; - } + return true; +} - sensor_client_info::get_instance().set_attribute(handle, attribute, value); +API bool sensord_set_passive_mode(int handle, bool passive) +{ + /* set passive mode*/ - return OP_SUCCESS; + return true; } -API bool sensord_set_option(int handle, int option) +API int sensord_external_connect(const char *key, sensor_external_command_cb_t cb, void *user_data) { - return (change_pause_policy(handle, CONVERT_OPTION_PAUSE_POLICY(option)) == OP_SUCCESS); + /* + * 1. check parameter + * 2. create handle in this client + * 3. first connection(client) + * 4. cmd_connect for external sensor with key + */ + retvm_if(!key, -EPERM, "Invalid key"); + return 0; } -API int sensord_set_attribute_int(int handle, int attribute, int value) +API bool sensord_external_disconnect(int handle) { - switch (attribute) { - case SENSORD_ATTRIBUTE_PAUSE_POLICY: - return change_pause_policy(handle, value); - case SENSORD_ATTRIBUTE_AXIS_ORIENTATION: - return change_axis_orientation(handle, value); - default: - break; - } - - return change_attribute_int(handle, attribute, value); + /* + * 1. check parameter + * 2. create handle in this client + * 3. first connection(client) + * 4. cmd_connect for external sensor with key + * 5. disconnect this handle + * 6. if there is no active sensor, remove client id and stop listener + */ + return true; } -API int sensord_set_attribute_str(int handle, int attribute, const char *value, int len) +API bool sensord_external_post(int handle, unsigned long long timestamp, const float* data, int data_cnt) { - sensor_id_t sensor_id; - command_channel *cmd_channel; - int client_id; - - AUTOLOCK(lock); + /* + * 1. check parameter + * 1.1 (data_cnt <= 0) || (data_cnt > POST_DATA_LEN_MAX)), return false + * 2. cmd_post + */ - if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) { - _E("Client %s failed to get handle information", get_client_name()); - return -EINVAL; - } - - if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) { - _E("Client %s failed to get command channel for %s", - get_client_name(), get_sensor_name(sensor_id)); - return -EPERM; - } - - retvm_if((len < 0) || (value == NULL), -EINVAL, - "Invalid value_len: %d, value: %#x, handle: %d, %s, %s", - len, value, handle, get_sensor_name(sensor_id), get_client_name()); - - client_id = sensor_client_info::get_instance().get_client_id(); - retvm_if((client_id < 0), -EPERM, - "Invalid client id : %d, handle: %d, %s, %s", - client_id, handle, get_sensor_name(sensor_id), get_client_name()); - - if (!cmd_channel->cmd_set_attribute_str(attribute, value, len)) { - _E("Sending cmd_set_attribute_str(%d, %d, %#x) failed for %s", - client_id, len, value, get_client_name()); - return -EPERM; - } - - sensor_client_info::get_instance().set_attribute(handle, attribute, value, len); - - return OP_SUCCESS; + return true; } -API bool sensord_send_sensorhub_data(int handle, const char *data, int data_len) +/* deperecated */ +API sensor_t sensord_get_sensor(sensor_type_t type) { - return (sensord_set_attribute_str(handle, 0, data, data_len) == OP_SUCCESS); + return NULL; } -API bool sensord_send_command(int handle, const char *command, int command_len) +/* deprecated */ +API bool sensord_get_sensor_list(sensor_type_t type, sensor_t **list, int *sensor_count) { - return (sensord_set_attribute_str(handle, 0, command, command_len) == OP_SUCCESS); + return (sensord_get_sensors(type, list, sensor_count) == OP_SUCCESS); } -API bool sensord_get_data(int handle, unsigned int data_id, sensor_data_t* sensor_data) +/* deprecated */ +API bool sensord_register_hub_event(int handle, unsigned int event_type, + unsigned int interval, unsigned int max_batch_latency, sensorhub_cb_t cb, void *user_data) { - sensor_id_t sensor_id; - command_channel *cmd_channel; - int sensor_state; - int client_id; - - retvm_if((!sensor_data), false, "sensor_data is NULL"); - - AUTOLOCK(lock); - - if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state)|| - !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) { - _E("client %s failed to get handle information", get_client_name()); - return false; - } - - if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) { - _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id)); - return false; - } - - client_id = sensor_client_info::get_instance().get_client_id(); - retvm_if((client_id < 0), false, "Invalid client id : %d, handle: %d, %s, %s", client_id, handle, get_sensor_name(sensor_id), get_client_name()); - - if (sensor_state != SENSOR_STATE_STARTED) { - _E("Sensor %s is not started for client %s with handle: %d, sensor_state: %d", get_sensor_name(sensor_id), get_client_name(), handle, sensor_state); - return false; - } - - if (!cmd_channel->cmd_get_data(data_id, sensor_data)) { - _E("cmd_get_data(%d, %d, %#x) failed for %s", client_id, data_id, sensor_data, get_client_name()); - return false; - } + return false; +} +/* deprecated */ +API bool sensord_get_supported_event_types(sensor_t sensor, unsigned int **event_types, int *count) +{ + /* + * 1. check parameter + * 2. if there is no sensor, return false + * 3. memory allocation + */ return true; } -API bool sensord_flush(int handle) +/* deprecated(BUT it is used in C-API....) */ +API bool sensord_is_supported_event_type(sensor_t sensor, unsigned int event_type, bool *supported) { - sensor_id_t sensor_id; - command_channel *cmd_channel; - int sensor_state; - int client_id; - - AUTOLOCK(lock); - - if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state) || - !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) { - _E("client %s failed to get handle information", get_client_name()); - return false; - } - - if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) { - _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id)); - return false; - } - - client_id = sensor_client_info::get_instance().get_client_id(); - retvm_if((client_id < 0), false, "Invalid client id : %d, handle: %d, %s, %s", client_id, handle, get_sensor_name(sensor_id), get_client_name()); - - if (sensor_state != SENSOR_STATE_STARTED) { - _E("Sensor %s is not started for client %s with handle: %d, sensor_state: %d", get_sensor_name(sensor_id), get_client_name(), handle, sensor_state); - return false; - } - - if (!cmd_channel->cmd_flush()) { - _E("cmd_flush() failed for %s", get_client_name()); - return false; - } - + /* + * 1. check parameter + * 2. if there is no sensor, return false + */ return true; } -API bool sensord_register_hub_event(int handle, unsigned int event_type, unsigned int interval, unsigned int max_batch_latency, sensorhub_cb_t cb, void *user_data) +/* deprecated */ +API bool sensord_send_sensorhub_data(int handle, const char *data, int data_len) { - return false; + return (sensord_set_attribute_str(handle, 0, data, data_len) == OP_SUCCESS); } -API bool sensord_set_passive_mode(int handle, bool passive) +/* deprecated */ +API bool sensord_send_command(int handle, const char *command, int command_len) { - if (!sensor_client_info::get_instance().set_passive_mode(handle, passive)) { - _E("Failed to set passive mode %d", passive); - return false; - } - - return true; + return (sensord_set_attribute_str(handle, 0, command, command_len) == OP_SUCCESS); } + diff --git a/src/client/client_common.cpp b/src/client/client_common.cpp deleted file mode 100644 index 96ce4ff..0000000 --- a/src/client/client_common.cpp +++ /dev/null @@ -1,127 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2013 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 -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define LOG_PER_COUNT_EVERY_EVENT 1 -#define LOG_PER_COUNT_MAX 25 - -static std::map sensor_log_count; - -const char* get_sensor_name(sensor_id_t id) -{ - sensor_type_t type = (sensor_type_t) (id >> SENSOR_TYPE_SHIFT); - - return util_sensor_type_t::get_string(type); -} - -const char* get_event_name(unsigned int event_type) -{ - sensor_type_t type = (sensor_type_t) (event_type >> SENSOR_EVENT_SHIFT); - - return util_sensor_type_t::get_string(type); -} - -unsigned int get_calibration_event_type(unsigned int event_type) -{ - sensor_type_t type = (sensor_type_t)(event_type >> SENSOR_EVENT_SHIFT); - - switch (type) { - case GEOMAGNETIC_SENSOR: - case ROTATION_VECTOR_SENSOR: - case RV_RAW_SENSOR: - case ORIENTATION_SENSOR: - return CALIBRATION_EVENT(type); - default: - return 0; - } -} - -unsigned int get_log_per_count(sensor_id_t id) -{ - sensor_type_t type = (sensor_type_t)(id >> SENSOR_TYPE_SHIFT); - - switch (type) { - /* on_changed_event type sensors */ - case PROXIMITY_SENSOR: - case GESTURE_WRIST_UP_SENSOR: - case GESTURE_WRIST_DOWN_SENSOR: - case GESTURE_MOVEMENT_SENSOR: - case WEAR_STATUS_SENSOR: - case PRESSURE_SENSOR: - return LOG_PER_COUNT_EVERY_EVENT; - default: - break; - } - return LOG_PER_COUNT_MAX; -} - -void print_event_occurrence_log(sensor_handle_info &info) -{ - unsigned int count; - unsigned int log_per_count; - - auto it_count = sensor_log_count.find(info.m_sensor_id); - if (it_count == sensor_log_count.end()) - sensor_log_count[info.m_sensor_id] = 0; - - count = ++sensor_log_count[info.m_sensor_id]; - log_per_count = get_log_per_count(info.m_sensor_id); - - if ((count != 1) && (count % log_per_count != 0)) - return; - - _D("%s receives %s[%d][state: %d, pause policy: %d, count: %d]", get_client_name(), - get_sensor_name(info.m_sensor_id), info.m_handle, - info.m_sensor_state, info.m_pause_policy, count); -} - -/* - * To prevent user mistakenly freeing sensor_info using sensor_t - */ -static const int SENSOR_TO_SENSOR_INFO = 4; -static const int SENSOR_INFO_TO_SENSOR = -SENSOR_TO_SENSOR_INFO; - -sensor_info *sensor_to_sensor_info(sensor_t sensor) -{ - if (!sensor) - return NULL; - - sensor_info* info = (sensor_info *)((char *)sensor + SENSOR_TO_SENSOR_INFO); - - return info; -} - -sensor_t sensor_info_to_sensor(const sensor_info *info) -{ - if (!info) - return NULL; - - sensor_t sensor = (sensor_t)((char *)info + SENSOR_INFO_TO_SENSOR); - - return sensor; -} diff --git a/src/client/client_common.h b/src/client/client_common.h deleted file mode 100644 index 5671987..0000000 --- a/src/client/client_common.h +++ /dev/null @@ -1,59 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2014 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 _CLIENT_COMMON_H_ -#define _CLIENT_COMMON_H_ - -#include -#include -#include -#include - -#include - -#define BASE_GATHERING_INTERVAL 100 -#define THREAD_TERMINATION -1 - -typedef struct { - unsigned long long event_id; - int handle; - sensor_t sensor; - unsigned int event_type; - sensor_cb_t cb; - std::shared_ptr sensor_data; - void *user_data; - sensor_accuracy_changed_cb_t accuracy_cb; - unsigned long long timestamp; - int accuracy; - void *accuracy_user_data; -} client_callback_info; - -const char *get_sensor_name(sensor_id_t sensor_id); -const char *get_event_name(unsigned int event_type); - -unsigned int get_calibration_event_type(unsigned int event_type); -unsigned int get_log_per_count(sensor_id_t id); - -void print_event_occurrence_log(sensor_handle_info &sensor_handle_info); - -class sensor_info; -sensor_info *sensor_to_sensor_info(sensor_t sensor); -sensor_t sensor_info_to_sensor(const sensor_info *info); - -#endif /* _CLIENT_COMMON_H_ */ diff --git a/src/client/command_channel.cpp b/src/client/command_channel.cpp deleted file mode 100644 index 683c017..0000000 --- a/src/client/command_channel.cpp +++ /dev/null @@ -1,713 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2013 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 -#include -#include -#include -#include -#include - -command_channel::command_channel() -: m_client_id(CLIENT_ID_INVALID) -, m_sensor_id(UNKNOWN_SENSOR) -{ -} - -command_channel::~command_channel() -{ - if (m_command_socket.is_valid()) - m_command_socket.close(); -} - -bool command_channel::command_handler(cpacket *packet, void **return_payload) -{ - if (!m_command_socket.is_valid()) { - _E("Command socket(%d) is not valid for client %s", m_command_socket.get_socket_fd(), get_client_name()); - return false; - } - - if (packet->size() == 0) { - _E("Packet is not valid for client %s", get_client_name()); - return false; - } - - if (m_command_socket.send(packet->packet(), packet->size()) <= 0) { - m_command_socket.close(); - _E("Failed to send command in client %s", get_client_name()); - return false; - } - - packet_header header; - - if (m_command_socket.recv(&header, sizeof(header)) <= 0) { - m_command_socket.close(); - _E("Failed to receive header for command reply packet in client %s", get_client_name()); - return false; - } - - char *buffer = new(std::nothrow) char[header.size]; - retvm_if(!buffer, false, "Failed to allocate memory"); - - if (m_command_socket.recv(buffer, header.size) <= 0) { - m_command_socket.close(); - _E("Failed to receive command reply packet in client %s", get_client_name()); - delete[] buffer; - return false; - } - - *return_payload = buffer; - - return true; -} - -bool command_channel::create_channel(void) -{ - const int cllient_type = CLIENT_TYPE_SENSOR_CLIENT; - if (!m_command_socket.create(SOCK_STREAM)) - return false; - - if (!m_command_socket.connect(COMMAND_CHANNEL_PATH)) { - _E("Failed to connect command channel for client %s, command socket fd[%d]", get_client_name(), m_command_socket.get_socket_fd()); - return false; - } - - m_command_socket.set_connection_mode(); - - if (m_command_socket.send(&cllient_type, sizeof(cllient_type)) <= 0) { - _E("Failed to send client type in client %s, command socket fd[%d]", get_client_name(), m_command_socket.get_socket_fd()); - return false; - } - - return true; -} - -void command_channel::set_client_id(int client_id) -{ - m_client_id = client_id; -} - -bool command_channel::cmd_get_id(int &client_id) -{ - cpacket *packet; - cmd_get_id_t *cmd_get_id; - cmd_get_id_done_t *cmd_get_id_done; - - packet = new(std::nothrow) cpacket(sizeof(cmd_get_id_t)); - retvm_if(!packet, false, "Failed to allocate memory"); - - packet->set_cmd(CMD_GET_ID); - - cmd_get_id = (cmd_get_id_t *)packet->data(); - - get_proc_name(getpid(), cmd_get_id->name); - - _I("%s send cmd_get_id()", get_client_name()); - - if (!command_handler(packet, (void **)&cmd_get_id_done)) { - _E("Client %s failed to send/receive command", get_client_name()); - delete packet; - return false; - } - - if (cmd_get_id_done->client_id < 0) { - _E("Client %s failed to get client_id[%d] from server", - get_client_name(), cmd_get_id_done->client_id); - delete[] (char *)cmd_get_id_done; - delete packet; - return false; - } - - client_id = cmd_get_id_done->client_id; - - delete[] (char *)cmd_get_id_done; - delete packet; - - return true; -} - -bool command_channel::cmd_get_sensor_list(void) -{ - cpacket packet; - cmd_get_sensor_list_done_t *cmd_get_sensor_list_done; - - packet.set_payload_size(sizeof(cmd_get_sensor_list_t)); - packet.set_cmd(CMD_GET_SENSOR_LIST); - - _I("%s send cmd_get_sensor_list", get_client_name()); - - if (!command_handler(&packet, (void **)&cmd_get_sensor_list_done)) { - _E("Client %s failed to send/receive command", get_client_name()); - return false; - } - - int sensor_cnt; - const size_t *size_field; - const char *raw_data_field; - - sensor_cnt = cmd_get_sensor_list_done->sensor_cnt; - size_field = (const size_t *)cmd_get_sensor_list_done->data; - raw_data_field = (const char *)size_field + (sizeof(size_t) * sensor_cnt); - - sensor_info *info; - - int idx = 0; - for (int i = 0; i < sensor_cnt; ++i) { - info = new(std::nothrow) sensor_info; - - if (!info) { - _E("Failed to allocate memory"); - delete[] (char *)cmd_get_sensor_list_done; - return false; - } - - info->set_raw_data(raw_data_field + idx, size_field[i]); - sensor_info_manager::get_instance().add_info(info); - idx += size_field[i]; - } - - delete[] (char *)cmd_get_sensor_list_done; - return true; -} - -bool command_channel::cmd_hello(sensor_id_t sensor) -{ - cpacket *packet; - cmd_hello_t *cmd_hello; - cmd_done_t *cmd_done; - - packet = new(std::nothrow) cpacket(sizeof(cmd_hello_t)); - retvm_if(!packet, false, "Failed to allocate memory"); - - packet->set_cmd(CMD_HELLO); - - cmd_hello = (cmd_hello_t*)packet->data(); - cmd_hello->client_id = m_client_id; - cmd_hello->sensor = sensor; - - _I("%s send cmd_hello(client_id=%d, %s)", - get_client_name(), m_client_id, get_sensor_name(sensor)); - - if (!command_handler(packet, (void **)&cmd_done)) { - _E("Client %s failed to send/receive command for sensor[%s]", - get_client_name(), get_sensor_name(sensor)); - delete packet; - return false; - } - - if (cmd_done->value < 0) { - _W("client %s got value[%d] from server with sensor [%s]", - get_client_name(), cmd_done->value, get_sensor_name(sensor)); - - delete[] (char *)cmd_done; - delete packet; - return false; - } - - delete[] (char *)cmd_done; - delete packet; - - m_sensor_id = sensor; - - return true; -} - -bool command_channel::cmd_byebye(void) -{ - cpacket *packet; - cmd_done_t *cmd_done; - - packet = new(std::nothrow) cpacket(sizeof(cmd_byebye_t)); - retvm_if(!packet, false, "Failed to allocate memory"); - - packet->set_cmd(CMD_BYEBYE); - - _I("%s send cmd_byebye(client_id=%d, %s)", - get_client_name(), m_client_id, get_sensor_name(m_sensor_id)); - - if (!command_handler(packet, (void **)&cmd_done)) { - _E("Client %s failed to send/receive command for sensor[%s] with client_id [%d]", - get_client_name(), get_sensor_name(m_sensor_id), m_client_id); - delete packet; - return false; - } - - if (cmd_done->value < 0) { - _E("Client %s got error[%d] from server for sensor[%s] with client_id [%d]", - get_client_name(), cmd_done->value, get_sensor_name(m_sensor_id), m_client_id); - - delete[] (char *)cmd_done; - delete packet; - return false; - } - - delete[] (char *)cmd_done; - delete packet; - - if (m_command_socket.is_valid()) - m_command_socket.close(); - - m_client_id = CLIENT_ID_INVALID; - m_sensor_id = UNKNOWN_SENSOR; - - return true; -} - -bool command_channel::cmd_start(void) -{ - cpacket *packet; - cmd_done_t *cmd_done; - - packet = new(std::nothrow) cpacket(sizeof(cmd_start_t)); - retvm_if(!packet, false, "Failed to allocate memory"); - - packet->set_cmd(CMD_START); - - _I("%s send cmd_start(client_id=%d, %s)", - get_client_name(), m_client_id, get_sensor_name(m_sensor_id)); - - if (!command_handler(packet, (void **)&cmd_done)) { - _E("Client %s failed to send/receive command for sensor[%s] with client_id [%d]", - get_client_name(), get_sensor_name(m_sensor_id), m_client_id); - delete packet; - return false; - } - - if (cmd_done->value < 0) { - _E("Client %s got error[%d] from server for sensor[%s] with client_id [%d]", - get_client_name(), cmd_done->value, get_sensor_name(m_sensor_id), m_client_id); - - delete[] (char *)cmd_done; - delete packet; - return false; - } - - delete[] (char *)cmd_done; - delete packet; - - return true; -} - -bool command_channel::cmd_stop(void) -{ - cpacket *packet; - cmd_done_t *cmd_done; - - packet = new(std::nothrow) cpacket(sizeof(cmd_stop_t)); - retvm_if(!packet, false, "Failed to allocate memory"); - - packet->set_cmd(CMD_STOP); - - _I("%s send cmd_stop(client_id=%d, %s)", - get_client_name(), m_client_id, get_sensor_name(m_sensor_id)); - - if (!command_handler(packet, (void **)&cmd_done)) { - _E("Client %s failed to send/receive command for sensor[%s] with client_id [%d]", - get_client_name(), get_sensor_name(m_sensor_id), m_client_id); - delete packet; - return false; - } - - if (cmd_done->value < 0) { - _E("Client %s got error[%d] from server for sensor[%s] with client_id [%d]", - get_client_name(), cmd_done->value, get_sensor_name(m_sensor_id), m_client_id); - - delete[] (char *)cmd_done; - delete packet; - return false; - } - - delete[] (char *)cmd_done; - delete packet; - - return true; -} - -bool command_channel::cmd_set_pause_policy(int pause_policy) -{ - cpacket *packet; - cmd_set_pause_policy_t *cmd_set_pause_policy; - cmd_done_t *cmd_done; - - packet = new(std::nothrow) cpacket(sizeof(cmd_set_pause_policy_t)); - retvm_if(!packet, false, "Failed to allocate memory"); - - packet->set_cmd(CMD_SET_PAUSE_POLICY); - - cmd_set_pause_policy = (cmd_set_pause_policy_t*)packet->data(); - cmd_set_pause_policy->pause_policy = pause_policy; - - _I("%s send cmd_set_pause_policy(client_id=%d, %s, pause_policy=%d)", - get_client_name(), m_client_id, get_sensor_name(m_sensor_id), pause_policy); - - if (!command_handler(packet, (void **)&cmd_done)) { - _E("Client %s failed to send/receive command for sensor[%s] with client_id [%d], pause_policy[%d]", - get_client_name(), get_sensor_name(m_sensor_id), m_client_id, pause_policy); - delete packet; - return false; - } - - if (cmd_done->value < 0) { - _E("Client %s got error[%d] from server for sensor[%s] with client_id [%d], pause_policy[%d]", - get_client_name(), cmd_done->value, get_sensor_name(m_sensor_id), m_client_id, pause_policy); - - delete[] (char *)cmd_done; - delete packet; - return false; - } - - delete[] (char *)cmd_done; - delete packet; - - return true; -} - -bool command_channel::cmd_register_event(unsigned int event_type) -{ - cpacket *packet; - cmd_reg_t *cmd_reg; - cmd_done_t *cmd_done; - - packet = new(std::nothrow) cpacket(sizeof(cmd_reg_t)); - retvm_if(!packet, false, "Failed to allocate memory"); - - packet->set_cmd(CMD_REG); - - cmd_reg = (cmd_reg_t*)packet->data(); - cmd_reg->event_type = event_type; - - _I("%s send cmd_register_event(client_id=%d, %s)", - get_client_name(), m_client_id, get_event_name(event_type)); - - if (!command_handler(packet, (void **)&cmd_done)) { - _E("Client %s failed to send/receive command with client_id [%d], event_type[%s]", - get_client_name(), m_client_id, get_event_name(event_type)); - delete packet; - return false; - } - - if (cmd_done->value < 0) { - _E("Client %s got error[%d] from server with client_id [%d], event_type[%s]", - get_client_name(), cmd_done->value, m_client_id, get_event_name(event_type)); - - delete[] (char *)cmd_done; - delete packet; - return false; - } - - delete[] (char *)cmd_done; - delete packet; - - return true; -} - -bool command_channel::cmd_register_events(event_type_vector &event_vec) -{ - auto it_event = event_vec.begin(); - - while (it_event != event_vec.end()) { - if (!cmd_register_event(*it_event)) - return false; - - ++it_event; - } - - return true; -} - -bool command_channel::cmd_unregister_event(unsigned int event_type) -{ - cpacket *packet; - cmd_unreg_t *cmd_unreg; - cmd_done_t *cmd_done; - - packet = new(std::nothrow) cpacket(sizeof(cmd_unreg_t)); - retvm_if(!packet, false, "Failed to allocate memory"); - - packet->set_cmd(CMD_UNREG); - - cmd_unreg = (cmd_unreg_t*)packet->data(); - cmd_unreg->event_type = event_type; - - _I("%s send cmd_unregister_event(client_id=%d, %s)", - get_client_name(), m_client_id, get_event_name(event_type)); - - if (!command_handler(packet, (void **)&cmd_done)) { - _E("Client %s failed to send/receive command with client_id [%d], event_type[%s]", - get_client_name(), m_client_id, get_event_name(event_type)); - delete packet; - return false; - } - - if (cmd_done->value < 0) { - _E("Client %s got error[%d] from server with client_id [%d], event_type[%s]", - get_client_name(), cmd_done->value, m_client_id, get_event_name(event_type)); - - delete[] (char *)cmd_done; - delete packet; - return false; - } - - delete[] (char *)cmd_done; - delete packet; - - return true; -} - -bool command_channel::cmd_unregister_events(event_type_vector &event_vec) -{ - auto it_event = event_vec.begin(); - - while (it_event != event_vec.end()) { - if (!cmd_unregister_event(*it_event)) - return false; - - ++it_event; - } - - return true; -} - -bool command_channel::cmd_set_batch(unsigned int interval, unsigned int latency) -{ - cpacket *packet; - cmd_set_batch_t *cmd_set_batch; - cmd_done_t *cmd_done; - - packet = new(std::nothrow) cpacket(sizeof(cmd_set_batch_t)); - retvm_if(!packet, false, "Failed to allocate memory"); - - packet->set_cmd(CMD_SET_BATCH); - - cmd_set_batch = (cmd_set_batch_t*)packet->data(); - cmd_set_batch->interval = interval; - cmd_set_batch->latency = latency; - - _I("%s send cmd_set_batch(client_id=%d, %s, interval=%d, latency = %d)", - get_client_name(), m_client_id, get_sensor_name(m_sensor_id), interval, latency); - - if (!command_handler(packet, (void **)&cmd_done)) { - _E("%s failed to send/receive command for sensor[%s] with client_id [%d], interval[%d], latency[%d]", - get_client_name(), get_sensor_name(m_sensor_id), m_client_id, interval, latency); - delete packet; - return false; - } - - if (cmd_done->value < 0) { - _E("%s got error[%d] from server for sensor[%s] with client_id [%d], interval[%d], latency[%d]", - get_client_name(), cmd_done->value, get_sensor_name(m_sensor_id), m_client_id, interval, latency); - - delete[] (char *)cmd_done; - delete packet; - return false; - } - - delete[] (char *)cmd_done; - delete packet; - - return true; -} - -bool command_channel::cmd_unset_batch(void) -{ - cpacket *packet; - cmd_done_t *cmd_done; - - packet = new(std::nothrow) cpacket(sizeof(cmd_unset_batch_t)); - retvm_if(!packet, false, "Failed to allocate memory"); - - packet->set_cmd(CMD_UNSET_BATCH); - - _I("%s send cmd_unset_batch(client_id=%d, %s)", - get_client_name(), m_client_id, get_sensor_name(m_sensor_id)); - - if (!command_handler(packet, (void **)&cmd_done)) { - _E("Client %s failed to send/receive command for sensor[%s] with client_id [%d]", - get_client_name(), get_sensor_name(m_sensor_id), m_client_id); - delete packet; - return false; - } - - if (cmd_done->value < 0) { - _E("Client %s got error[%d] from server for sensor[%s] with client_id [%d]", - get_client_name(), cmd_done->value, get_sensor_name(m_sensor_id), m_client_id); - - delete[] (char *)cmd_done; - delete packet; - return false; - } - - delete[] (char *)cmd_done; - delete packet; - - return true; -} - -bool command_channel::cmd_get_data(unsigned int type, sensor_data_t* sensor_data) -{ - cpacket *packet; - cmd_get_data_t *cmd_get_data; - cmd_get_data_done_t *cmd_get_data_done; - - packet = new(std::nothrow) cpacket(sizeof(cmd_get_data_done_t)); - retvm_if(!packet, false, "Failed to allocate memory"); - - packet->set_cmd(CMD_GET_DATA); - - cmd_get_data = (cmd_get_data_t*)packet->data(); - cmd_get_data->type = type; - - if (!command_handler(packet, (void **)&cmd_get_data_done)) { - _E("Client %s failed to send/receive command with client_id [%d]", - get_client_name(), m_client_id); - delete packet; - return false; - } - - if (cmd_get_data_done->state < 0 ) { - _E("Client %s got error[%d] from server with client_id [%d]", - get_client_name(), cmd_get_data_done->state, m_client_id); - sensor_data->accuracy = SENSOR_ACCURACY_UNDEFINED; - sensor_data->timestamp = 0; - sensor_data->value_count = 0; - delete[] (char *)cmd_get_data_done; - delete packet; - return false; - } - - memcpy(sensor_data, &cmd_get_data_done->base_data, sizeof(sensor_data_t)); - - delete[] (char *)cmd_get_data_done; - delete packet; - - return true; -} - -bool command_channel::cmd_set_attribute_int(int attribute, int value) -{ - cpacket *packet; - cmd_set_attribute_int_t *cmd_set_attribute_int; - cmd_done_t *cmd_done; - - packet = new(std::nothrow) cpacket(sizeof(cmd_set_attribute_int_t)); - retvm_if(!packet, false, "Failed to allocate memory"); - - packet->set_cmd(CMD_SET_ATTRIBUTE_INT); - - cmd_set_attribute_int = (cmd_set_attribute_int_t*)packet->data(); - cmd_set_attribute_int->attribute = attribute; - cmd_set_attribute_int->value = value; - - _I("%s send cmd_set_attribute_int(client_id=%d, %s, %#x, %d)", - get_client_name(), m_client_id, get_sensor_name(m_sensor_id), attribute, value); - - if (!command_handler(packet, (void **)&cmd_done)) { - _E("Client %s failed to send/receive command for sensor[%s] with client_id [%d], attribute[%#x], value[%d]", - get_client_name(), get_sensor_name(m_sensor_id), m_client_id, attribute, value); - delete packet; - return false; - } - - if (cmd_done->value < 0) { - _E("Client %s got error[%d] from server for sensor[%s] with attribute[%#x], value[%d]", - get_client_name(), cmd_done->value, get_sensor_name(m_sensor_id), attribute, value); - - delete[] (char *)cmd_done; - delete packet; - return false; - } - - delete[] (char *)cmd_done; - delete packet; - - return true; -} - -bool command_channel::cmd_set_attribute_str(int attribute, const char* value, int len) -{ - cpacket *packet; - cmd_set_attribute_str_t *cmd_set_attribute_str; - cmd_done_t *cmd_done; - - packet = new(std::nothrow) cpacket(sizeof(cmd_set_attribute_str_t) + len); - retvm_if(!packet, false, "Failed to allocate memory"); - - packet->set_cmd(CMD_SET_ATTRIBUTE_STR); - - cmd_set_attribute_str = (cmd_set_attribute_str_t*)packet->data(); - cmd_set_attribute_str->attribute = attribute; - cmd_set_attribute_str->len = len; - memcpy(cmd_set_attribute_str->value, value, len); - - _I("%s send cmd_set_attribute_str(client_id=%d, attribute = %#x, value_len = %d, value = %#x)", - get_client_name(), m_client_id, attribute, len, value); - - if (!command_handler(packet, (void **)&cmd_done)) { - _E("%s failed to send/receive command with client_id [%d]", - get_client_name(), m_client_id); - delete packet; - return false; - } - - if (cmd_done->value < 0) { - _E("%s got error[%d] from server with client_id [%d]", - get_client_name(), cmd_done->value, m_client_id); - - delete[] (char *)cmd_done; - delete packet; - return false; - } - - delete[] (char *)cmd_done; - delete packet; - return true; -} - -bool command_channel::cmd_flush(void) -{ - cpacket *packet; - cmd_done_t *cmd_done; - - packet = new(std::nothrow) cpacket(sizeof(cmd_flush_t)); - retvm_if(!packet, false, "Failed to allocate memory"); - - packet->set_cmd(CMD_FLUSH); - - _I("%s send cmd_flush(client_id=%d)", get_client_name(), m_client_id); - - if (!command_handler(packet, (void **)&cmd_done)) { - _E("%s failed to send flush with client_id [%d]", - get_client_name(), m_client_id); - delete packet; - return false; - } - - if (cmd_done->value < 0) { - _E("%s got error[%d] from server with client_id [%d]", - get_client_name(), cmd_done->value, m_client_id); - - delete [] (char *)cmd_done; - delete packet; - return false; - } - - delete [] (char *)cmd_done; - delete packet; - return true; -} diff --git a/src/client/command_channel.h b/src/client/command_channel.h deleted file mode 100644 index 0a9cbc4..0000000 --- a/src/client/command_channel.h +++ /dev/null @@ -1,61 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2013 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 _COMMAND_CHANNEL_H_ -#define _COMMAND_CHANNEL_H_ - -#include -#include -#include -#include - -class command_channel { -public: - command_channel(); - ~command_channel(); - - bool create_channel(void); - void set_client_id(int client_id); - - bool cmd_get_id(int &client_id); - bool cmd_get_sensor_list(void); - bool cmd_hello(sensor_id_t sensor); - bool cmd_byebye(void); - bool cmd_start(void); - bool cmd_stop(void); - bool cmd_set_pause_policy(int pause_policy); - bool cmd_register_event(unsigned int event_type); - bool cmd_register_events(event_type_vector &event_vec); - bool cmd_unregister_event(unsigned int event_type); - bool cmd_unregister_events(event_type_vector &event_vec); - bool cmd_set_batch(unsigned int interval, unsigned int latency); - bool cmd_unset_batch(void); - bool cmd_get_data(unsigned int type, sensor_data_t *values); - bool cmd_set_attribute_int(int attribute, int value); - bool cmd_set_attribute_str(int attribute, const char *value, int len); - bool cmd_flush(void); - -private: - csocket m_command_socket; - int m_client_id; - sensor_id_t m_sensor_id; - bool command_handler(cpacket *packet, void **return_payload); -}; - -#endif /* _COMMAND_CHANNEL_H_ */ diff --git a/src/client/dbus_listener.cpp b/src/client/dbus_listener.cpp deleted file mode 100644 index 7f96ef1..0000000 --- a/src/client/dbus_listener.cpp +++ /dev/null @@ -1,135 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2013 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 -#include "sensor_event_listener.h" -#include "dbus_listener.h" - -#define HANDLE_GERROR(Err) \ - do { \ - if ((Err)) { \ - _E("GError: %s", Err->message); \ - g_error_free(Err); \ - Err = NULL; \ - } \ - } while (0) - -#define ROTATION_DBUS_DEST "org.tizen.system.coord" -#define ROTATION_DBUS_OBJ_PATH "/Org/Tizen/System/Coord/Rotation" -#define ROTATION_DBUS_IFACE "org.tizen.system.coord.rotation" -#define ROTATION_DBUS_SIGNAL "Changed" -#define ROTATION_DBUS_METHOD "Degree" - -static void rotation_signal_cb(GDBusConnection *conn, const gchar *sender, - const gchar *obj_path, const gchar *iface, const gchar *signal_name, - GVariant *param, gpointer user_data) -{ - gint state; - g_variant_get(param, "(i)", &state); - sensor_event_listener::get_instance().set_display_rotation(state); -} - -static void rotation_read_cb(GObject *source_object, GAsyncResult *res, gpointer user_data) -{ - GError *error = NULL; - GDBusConnection *conn = G_DBUS_CONNECTION(source_object); - GVariant *result = g_dbus_connection_call_finish(conn, res, &error); - HANDLE_GERROR(error); - ret_if(result == NULL); - - gint state; - g_variant_get(result, "(i)", &state); - g_variant_unref(result); - sensor_event_listener::get_instance().set_display_rotation(state); -} - -dbus_listener::dbus_listener() -: m_connection(NULL) -{ -#ifndef GLIB_VERSION_2_36 - g_type_init(); -#endif -} - -dbus_listener::~dbus_listener() -{ - disconnect(); -} - -void dbus_listener::init(void) -{ - static dbus_listener listener; - static bool done = false; - ret_if(done); - listener.connect(); - done = true; -} - -void dbus_listener::connect(void) -{ - GError *gerr = NULL; - - gchar *addr = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, &gerr); - HANDLE_GERROR(gerr); - retm_if(addr == NULL, "Getting address failed"); - - g_dbus_connection_new_for_address(addr, - (GDBusConnectionFlags)(G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT | G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION), - NULL, NULL, on_connection_ready, this); - g_free(addr); -} - -void dbus_listener::on_connection_ready(GObject *source_object, GAsyncResult *res, gpointer user_data) -{ - GError *gerr = NULL; - dbus_listener *listener = static_cast(user_data); - - GDBusConnection *conn = g_dbus_connection_new_finish(res, &gerr); - HANDLE_GERROR(gerr); - - retm_if(conn == NULL, "Connection failed"); - _D("Dbus connection established: %s", g_dbus_connection_get_unique_name(conn)); - - listener->m_connection = conn; - listener->get_current_state(); - listener->subscribe(); -} - -void dbus_listener::disconnect(void) -{ - ret_if(!m_connection); - g_dbus_connection_close_sync(m_connection, NULL, NULL); - g_object_unref(m_connection); -} - -void dbus_listener::subscribe(void) -{ - /* Diplay rotation */ - g_dbus_connection_signal_subscribe(m_connection, - ROTATION_DBUS_DEST, ROTATION_DBUS_IFACE, ROTATION_DBUS_SIGNAL, ROTATION_DBUS_OBJ_PATH, - NULL, G_DBUS_SIGNAL_FLAGS_NONE, (GDBusSignalCallback)rotation_signal_cb, NULL, NULL); -} - -void dbus_listener::get_current_state(void) -{ - /* Display rotation */ - g_dbus_connection_call(m_connection, - ROTATION_DBUS_DEST, ROTATION_DBUS_OBJ_PATH, ROTATION_DBUS_IFACE, ROTATION_DBUS_METHOD, - NULL, NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, (GAsyncReadyCallback)rotation_read_cb, NULL); -} diff --git a/src/client/dbus_listener.h b/src/client/dbus_listener.h deleted file mode 100644 index 3d4a804..0000000 --- a/src/client/dbus_listener.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2013 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 _DBUS_LISTENER_H_ -#define _DBUS_LISTENER_H_ - -#include -#include -#include - -class dbus_listener { -public: - static void init(void); - -private: - GDBusConnection *m_connection; - - dbus_listener(); - ~dbus_listener(); - - void connect(void); - void disconnect(void); - - void subscribe(void); - void get_current_state(void); - - static void on_connection_ready(GObject *source_object, GAsyncResult *res, gpointer user_data); -}; - -#endif /* _DBUS_LISTENER_H_ */ diff --git a/src/client/external_client.cpp b/src/client/external_client.cpp deleted file mode 100644 index 1a3e2da..0000000 --- a/src/client/external_client.cpp +++ /dev/null @@ -1,256 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2015 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 -#include -#include -#include -#include -#include - -using std::vector; -using std::string; - -static cmutex lock; - -static void clean_up(void); -static void restore_session(void); - -class initiator { -public: - initiator() - { - external_sensor_manager::get_instance().set_hup_observer(restore_session); - } -}; - -static initiator g_initiator; - -void clean_up(void) -{ - vector handles; - - external_sensor_manager::get_instance().get_all_handles(handles); - - auto it_handle = handles.begin(); - - while (it_handle != handles.end()) { - sensord_external_disconnect(*it_handle); - ++it_handle; - } -} - -void restore_session(void) -{ - AUTOLOCK(lock); - - _I("Trying to restore external source session for %s", get_client_name()); - - external_data_channel *data_channel; - int client_id; - - external_sensor_manager::get_instance().close_data_channel(); - external_sensor_manager::get_instance().set_client_id(CLIENT_ID_INVALID); - - vector handles; - - external_sensor_manager::get_instance().get_all_handles(handles); - - bool first_connection = true; - - auto it_handle = handles.begin(); - - while (it_handle != handles.end()) { - data_channel = new(std::nothrow) external_data_channel(); - retm_if(!data_channel, "Failed to allocate memory"); - - if (!data_channel->create_channel()) { - _E("%s failed to create data channel", get_client_name()); - delete data_channel; - goto FAILED; - } - - external_sensor_manager::get_instance().add_data_channel(*it_handle, data_channel); - - if (first_connection) { - first_connection = false; - if (!data_channel->cmd_get_id(client_id)) { - _E("Failed to get client id"); - goto FAILED; - } - - external_sensor_manager::get_instance().set_client_id(client_id); - external_sensor_manager::get_instance().start_command_listener(); - } - - data_channel->set_client_id(client_id); - - sensor_id_t dummy; - if (!data_channel->cmd_connect(external_sensor_manager::get_instance().get_key(*it_handle), dummy)) { - _E("Sending cmd_connect(%s) failed for %s", external_sensor_manager::get_instance().get_key(*it_handle).c_str(), get_client_name()); - goto FAILED; - } - - ++it_handle; - } - - _I("Succeeded to restore external source session for %s", get_client_name()); - - return; - -FAILED: - external_sensor_manager::get_instance().clear(); - _E("Failed to restore external source session for %s", get_client_name()); -} - -API int sensord_external_connect(const char *key, sensor_external_command_cb_t cb, void *user_data) -{ - external_data_channel *channel = NULL; - int handle; - int client_id; - bool first_connection = false; - - retvm_if(!key, OP_ERROR, "client %s passes null key", get_client_name()); - - AUTOLOCK(lock); - - handle = external_sensor_manager::get_instance().create_handle(); - - // lazy loading after creating static variables - atexit(clean_up); - - if (handle == MAX_HANDLE) { - _E("Maximum number of handles reached, key %s in client %s", key, get_client_name()); - return OP_ERROR; - } - - channel = new(std::nothrow) external_data_channel(); - if (!channel) { - _E("Failed to allocated memory"); - external_sensor_manager::get_instance().delete_handle(handle); - return OP_ERROR; - } - - if (!channel->create_channel()) { - _E("%s failed to create data channel for %s", get_client_name(), key); - external_sensor_manager::get_instance().delete_handle(handle); - delete channel; - return OP_ERROR; - } - - external_sensor_manager::get_instance().add_data_channel(handle, channel); - - if (!external_sensor_manager::get_instance().has_client_id()) { - first_connection = true; - if (!channel->cmd_get_id(client_id)) { - _E("Sending cmd_get_id() failed for %s", key); - external_sensor_manager::get_instance().close_data_channel(handle); - external_sensor_manager::get_instance().delete_handle(handle); - return OP_ERROR; - } - - external_sensor_manager::get_instance().set_client_id(client_id); - _I("%s gets client_id [%d]", get_client_name(), client_id); - external_sensor_manager::get_instance().start_command_listener(); - _I("%s starts listening command with client_id [%d]", get_client_name(), client_id); - } - - client_id = external_sensor_manager::get_instance().get_client_id(); - channel->set_client_id(client_id); - - sensor_id_t sensor; - - if (!channel->cmd_connect(key, sensor)) { - _E("Failed to connect %s for %s", key, get_client_name()); - external_sensor_manager::get_instance().close_data_channel(handle); - external_sensor_manager::get_instance().delete_handle(handle); - - if (first_connection) { - external_sensor_manager::get_instance().set_client_id(CLIENT_ID_INVALID); - external_sensor_manager::get_instance().stop_command_listener(); - } - - return OP_ERROR; - } - - _I("%s[%d] connects with %s[%d]", get_client_name(), client_id, key, handle); - - external_sensor_manager::get_instance().set_handle(handle, sensor, string(key), (void *)cb, user_data); - - return handle; -} - -API bool sensord_external_disconnect(int handle) -{ - external_data_channel *channel; - - AUTOLOCK(lock); - - retvm_if(!external_sensor_manager::get_instance().is_valid(handle), false, "Handle %d is not valid for %s", - handle, get_client_name()); - - if (!external_sensor_manager::get_instance().get_data_channel(handle, &channel)) { - _E("client %s failed to get data channel", get_client_name()); - return false; - } - - _I("%s disconnects with %s[%d]", get_client_name(), external_sensor_manager::get_instance().get_key(handle).c_str(), handle); - - if (!external_sensor_manager::get_instance().delete_handle(handle)) - return false; - - if (!channel->cmd_disconnect()) { - _E("Sending cmd_disconnect() failed for %s", get_client_name()); - return false; - } - - external_sensor_manager::get_instance().close_data_channel(handle); - - if (!external_sensor_manager::get_instance().is_active()) { - _I("Stop listening command for client %s with client id [%d]", get_client_name(), external_sensor_manager::get_instance().get_client_id()); - external_sensor_manager::get_instance().set_client_id(CLIENT_ID_INVALID); - external_sensor_manager::get_instance().stop_command_listener(); - } - - return true; -} - -API bool sensord_external_post(int handle, unsigned long long timestamp, const float* data, int data_cnt) -{ - external_data_channel *channel; - - retvm_if(((data_cnt <= 0) || (data_cnt > POST_DATA_LEN_MAX)), false, - "data_cnt(%d) is invalid for %s", data_cnt, get_client_name()); - - AUTOLOCK(lock); - - retvm_if(!external_sensor_manager::get_instance().is_valid(handle), false, "Handle %d is not valid for %s", - handle, get_client_name()); - - if (!external_sensor_manager::get_instance().get_data_channel(handle, &channel)) { - _E("client %s failed to get data channel", get_client_name()); - return false; - } - - if (!channel->cmd_post(timestamp, data, data_cnt)) { - _E("Failed to post data:%#x, data_cnt:%d", data, data_cnt); - return false; - } - - return true; -} diff --git a/src/client/external_data_channel.cpp b/src/client/external_data_channel.cpp deleted file mode 100644 index 70a7325..0000000 --- a/src/client/external_data_channel.cpp +++ /dev/null @@ -1,266 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2015 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 -#include -#include - -using std::string; - -external_data_channel::external_data_channel() -: m_client_id(CLIENT_ID_INVALID) -, m_sensor_id(UNKNOWN_SENSOR) -{ -} - -external_data_channel::~external_data_channel() -{ - m_socket.close(); -} - -bool external_data_channel::command_handler(cpacket *packet, void **return_payload) -{ - packet_header header; - char *buffer = NULL; - - if (!m_socket.is_valid()) { - _E("Socket(%d) is not valid for client %s", m_socket.get_socket_fd(), get_client_name()); - return false; - } - - if (!packet->size()) { - _E("Packet is not valid for client %s", get_client_name()); - return false; - } - - if (m_socket.send(packet->packet(), packet->size()) <= 0) { - m_socket.close(); - _E("Failed to send command in client %s", get_client_name()); - return false; - } - - if (m_socket.recv(&header, sizeof(header)) <= 0) { - m_socket.close(); - _E("Failed to receive header for command packet in client %s", get_client_name()); - return false; - } - - buffer = new(std::nothrow) char[header.size]; - retvm_if(!buffer, false, "Failed to allocate memory"); - - if (m_socket.recv(buffer, header.size) <= 0) { - m_socket.close(); - _E("Failed to receive command packet in client %s", get_client_name()); - delete[] buffer; - return false; - } - - *return_payload = buffer; - - return true; -} - -bool external_data_channel::create_channel(void) -{ - const int client_type = CLIENT_TYPE_EXTERNAL_SOURCE; - - if (!m_socket.create(SOCK_STREAM)) { - _E("Failed to create external data channel for client %s", get_client_name()); - return false; - } - - if (!m_socket.connect(COMMAND_CHANNEL_PATH)) { - _E("Failed to connect external data channel for client %s, command socket fd[%d]", get_client_name(), m_socket.get_socket_fd()); - return false; - } - - m_socket.set_connection_mode(); - - if (m_socket.send(&client_type, sizeof(client_type)) <= 0) { - _E("Failed to send client type in client %s, command socket fd[%d]", get_client_name(), m_socket.get_socket_fd()); - return false; - } - - return true; -} - -void external_data_channel::set_client_id(int client_id) -{ - m_client_id = client_id; -} - -bool external_data_channel::cmd_get_id(int &client_id) -{ - cpacket *packet; - cmd_ext_get_id_t *cmd_ext_get_id; - cmd_ext_get_id_done_t *cmd_ext_get_id_done; - - packet = new(std::nothrow) cpacket(sizeof(cmd_ext_get_id_t)); - retvm_if(!packet, false, "Failed to allocate memory"); - - packet->set_cmd(CMD_EXT_GET_ID); - - cmd_ext_get_id = (cmd_ext_get_id_t *)packet->data(); - - get_proc_name(getpid(), cmd_ext_get_id->name); - - _I("%s send cmd_get_id()", get_client_name()); - - if (!command_handler(packet, (void **)&cmd_ext_get_id_done)) { - _E("Client %s failed to send/receive command", get_client_name()); - delete packet; - return false; - } - - if (cmd_ext_get_id_done->client_id < 0) { - _E("Client %s failed to get client_id[%d] from server", - get_client_name(), cmd_ext_get_id_done->client_id); - delete[] (char *)cmd_ext_get_id_done; - delete packet; - return false; - } - - client_id = cmd_ext_get_id_done->client_id; - - delete[] (char *)cmd_ext_get_id_done; - delete packet; - - return true; -} - -bool external_data_channel::cmd_connect(const string &key, sensor_id_t &sensor_id) -{ - cpacket *packet; - cmd_ext_connect_t *cmd_ext_connect; - cmd_ext_connect_done_t *cmd_ext_connect_done; - - int key_size = key.size(); - - if ((key_size == 0) || (key_size >= NAME_MAX)) { - _I("Key(%s) is not valid", key.c_str()); - return false; - } - - packet = new(std::nothrow) cpacket(sizeof(cmd_ext_connect_t)); - retvm_if(!packet, false, "Failed to allocate memory"); - - packet->set_cmd(CMD_EXT_CONNECT); - - cmd_ext_connect = (cmd_ext_connect_t *)packet->data(); - cmd_ext_connect->client_id = m_client_id; - strncpy(cmd_ext_connect->key, key.c_str(), NAME_MAX-1); - - _I("%s send cmd_get_connect(key = %s, client_id = %d)", get_client_name(), key.c_str(), m_client_id); - - if (!command_handler(packet, (void **)&cmd_ext_connect_done)) { - _E("Client %s failed to send/receive command", get_client_name()); - delete packet; - return false; - } - - if (cmd_ext_connect_done->sensor_id == UNKNOWN_SENSOR) { - _E("Client %s failed to connect to external sensor", get_client_name()); - delete[] (char *)cmd_ext_connect_done; - delete packet; - return false; - } - - m_sensor_id = sensor_id = cmd_ext_connect_done->sensor_id; - - delete[] (char *)cmd_ext_connect_done; - delete packet; - - return true; -} - -bool external_data_channel::cmd_disconnect(void) -{ - cpacket *packet; - cmd_ext_done_t *cmd_ext_done; - - packet = new(std::nothrow) cpacket(sizeof(cmd_ext_disconnect_t)); - retvm_if(!packet, false, "Failed to allocate memory"); - - packet->set_cmd(CMD_EXT_DISCONNECT); - - _I("%s send cmd_disconnect(client_id=%d)", get_client_name(), m_client_id); - - if (!command_handler(packet, (void **)&cmd_ext_done)) { - _E("Client %s failed to send/receive command with client_id [%d]", - get_client_name(), m_client_id); - delete packet; - return false; - } - - if (cmd_ext_done->value < 0) { - _E("Client %s got error[%d] from server with client_id [%d]", - get_client_name(), cmd_ext_done->value, m_client_id); - - delete[] (char *)cmd_ext_done; - delete packet; - return false; - } - - delete[] (char *)cmd_ext_done; - delete packet; - - m_socket.close(); - m_client_id = CLIENT_ID_INVALID; - return true; -} - -bool external_data_channel::cmd_post(unsigned long long timestamp, const float *data, int data_cnt) -{ - cpacket *packet; - cmd_ext_post_t *cmd_ext_post; - cmd_done_t *cmd_done; - - packet = new(std::nothrow) cpacket(sizeof(cmd_ext_post_t) + sizeof(float) * data_cnt); - retvm_if(!packet, false, "Failed to allocate memory"); - - packet->set_cmd(CMD_EXT_POST); - - cmd_ext_post = (cmd_ext_post_t*)packet->data(); - cmd_ext_post->timestamp = timestamp; - cmd_ext_post->data_cnt = data_cnt; - memcpy(cmd_ext_post->data, data, sizeof(float) * data_cnt); - - _I("%s send cmd_post(client_id=%d, data = %#x, data_cnt = %d)", - get_client_name(), m_client_id, data, data_cnt); - - if (!command_handler(packet, (void **)&cmd_done)) { - _E("%s failed to send/receive command with client_id [%d]", - get_client_name(), m_client_id); - delete packet; - return false; - } - - if (cmd_done->value < 0) { - _E("%s got error[%d] from server with client_id [%d]", - get_client_name(), cmd_done->value, m_client_id); - - delete[] (char *)cmd_done; - delete packet; - return false; - } - - delete[] (char *)cmd_done; - delete packet; - - return true; -} diff --git a/src/client/external_data_channel.h b/src/client/external_data_channel.h deleted file mode 100644 index 19a2a41..0000000 --- a/src/client/external_data_channel.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2015 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 _EXTERNAL_DATA_CHANNEL_H_ -#define _EXTERNAL_DATA_CHANNEL_H_ - -#include -#include - -class cpacket; - -class external_data_channel { -public: - external_data_channel(); - ~external_data_channel(); - - bool create_channel(void); - void set_client_id(int client_id); - bool cmd_get_id(int &client_id); - bool cmd_connect(const std::string &key, sensor_id_t &sensor_id); - bool cmd_disconnect(void); - bool cmd_post(unsigned long long timestamp, const float *data, int data_cnt); -private: - csocket m_socket; - int m_client_id; - sensor_id_t m_sensor_id; - bool command_handler(cpacket *packet, void **return_payload); -}; - -#endif /* _EXTERNAL_DATA_CHANNEL_H_ */ diff --git a/src/client/external_sensor_manager.cpp b/src/client/external_sensor_manager.cpp deleted file mode 100644 index 9a0b3c4..0000000 --- a/src/client/external_sensor_manager.cpp +++ /dev/null @@ -1,543 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2015 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 -#include -#include -#include -#include -#include -#include - -using std::pair; -using std::vector; -using std::thread; -using std::string; - -external_sensor_manager::external_sensor_manager() -: m_client_id(CLIENT_ID_INVALID) -, m_poller(NULL) -, m_thread_state(THREAD_STATE_TERMINATE) -, m_hup_observer(NULL) -{ -} - -external_sensor_manager::~external_sensor_manager() -{ - stop_command_listener(); -} - -external_sensor_manager& external_sensor_manager::get_instance(void) -{ - static external_sensor_manager inst; - return inst; -} - -int external_sensor_manager::create_handle(void) -{ - sensor_ext_handle_info handle_info; - int handle = 0; - - AUTOLOCK(m_handle_info_lock); - - while (m_sensor_handle_infos.count(handle) > 0) - handle++; - - if (handle >= MAX_HANDLE) { - _E("Handles of client %s are full", get_client_name()); - return MAX_HANDLE_REACHED; - } - - handle_info.m_handle = handle; - handle_info.m_sensor = UNKNOWN_SENSOR; - handle_info.m_cb = NULL; - handle_info.m_user_data = NULL; - - m_sensor_handle_infos.insert(pair(handle, handle_info)); - - return handle; -} - -bool external_sensor_manager::delete_handle(int handle) -{ - AUTOLOCK(m_handle_info_lock); - - auto it_handle = m_sensor_handle_infos.find(handle); - - if (it_handle == m_sensor_handle_infos.end()) { - _E("Handle[%d] is not found for client %s", handle, get_client_name()); - return false; - } - - m_sensor_handle_map.erase(it_handle->second.m_sensor); - m_sensor_handle_infos.erase(it_handle); - - return true; -} - -bool external_sensor_manager::set_handle(int handle, sensor_id_t sensor, const string& key, void* cb, void* user_data) -{ - AUTOLOCK(m_handle_info_lock); - - auto it_handle = m_sensor_handle_infos.find(handle); - - if (it_handle == m_sensor_handle_infos.end()) { - _E("Handle[%d] is not found for client %s", handle, get_client_name()); - return false; - } - - it_handle->second.m_sensor = sensor; - it_handle->second.m_key = key; - it_handle->second.m_cb = cb; - it_handle->second.m_user_data = user_data; - - m_sensor_handle_map.insert(pair(sensor, handle)); - - return true; -} - -bool external_sensor_manager::get_sensor(int handle, sensor_id_t &sensor) -{ - AUTOLOCK(m_handle_info_lock); - - auto it_handle = m_sensor_handle_infos.find(handle); - - if (it_handle == m_sensor_handle_infos.end()) { - _E("Handle[%d] is not found for client %s", handle, get_client_name()); - return false; - } - - sensor = it_handle->second.m_sensor; - - return true; -} - -int external_sensor_manager::get_handle(sensor_id_t sensor) -{ - AUTOLOCK(m_handle_info_lock); - - auto it_handle = m_sensor_handle_map.find(sensor); - - if (it_handle == m_sensor_handle_map.end()) { - _E("Handle is not found for client %s with sensor: %d", get_client_name(), sensor); - return -1; - } - - return it_handle->second; -} - -bool external_sensor_manager::get_handle_info(int handle, const sensor_ext_handle_info*& handle_info) -{ - AUTOLOCK(m_handle_info_lock); - - auto it_handle = m_sensor_handle_infos.find(handle); - - if (it_handle == m_sensor_handle_infos.end()) { - _E("Handle[%d] is not found for client %s", handle, get_client_name()); - return false; - } - - handle_info = &(it_handle->second); - return true; -} - -string external_sensor_manager::get_key(int handle) -{ - AUTOLOCK(m_handle_info_lock); - - auto it_handle = m_sensor_handle_infos.find(handle); - - if (it_handle == m_sensor_handle_infos.end()) { - return string("INVALID_KEY"); - } - - return it_handle->second.m_key; -} - -bool external_sensor_manager::has_client_id(void) -{ - return (m_client_id != CLIENT_ID_INVALID); -} - -int external_sensor_manager::get_client_id(void) -{ - return m_client_id; -} - -void external_sensor_manager::set_client_id(int client_id) -{ - m_client_id = client_id; -} - -bool external_sensor_manager::add_data_channel(int handle, external_data_channel *channel) -{ - auto it_channel = m_data_channels.find(handle); - - if (it_channel != m_data_channels.end()) { - _E("%s alreay has data_channel for %s", get_client_name(), get_key(handle).c_str()); - return false; - } - - m_data_channels.insert(pair(handle, channel)); - return true; -} - -bool external_sensor_manager::get_data_channel(int handle, external_data_channel **channel) -{ - auto it_channel = m_data_channels.find(handle); - - if (it_channel == m_data_channels.end()) { - _E("%s doesn't have data_channel for %s", get_client_name(), get_key(handle).c_str()); - return false; - } - - *channel = it_channel->second; - - return true; -} - -bool external_sensor_manager::close_data_channel(void) -{ - auto it_channel = m_data_channels.begin(); - - if (it_channel != m_data_channels.end()) { - delete it_channel->second; - ++it_channel; - } - - m_data_channels.clear(); - - return true; -} - -bool external_sensor_manager::close_data_channel(int handle) -{ - auto it_channel = m_data_channels.find(handle); - - if (it_channel == m_data_channels.end()) { - _E("%s doesn't have data_channel for %s", get_client_name(), get_key(handle).c_str()); - return false; - } - - delete it_channel->second; - - m_data_channels.erase(it_channel); - - return true; -} - -bool external_sensor_manager::is_valid(int handle) -{ - AUTOLOCK(m_handle_info_lock); - - auto it_handle = m_sensor_handle_infos.find(handle); - - if (it_handle == m_sensor_handle_infos.end()) - return false; - - return true; -} - -bool external_sensor_manager::is_active(void) -{ - AUTOLOCK(m_handle_info_lock); - - return !m_sensor_handle_infos.empty(); -} - -void external_sensor_manager::get_all_handles(vector &handles) -{ - AUTOLOCK(m_handle_info_lock); - - auto it_handle = m_sensor_handle_infos.begin(); - - while (it_handle != m_sensor_handle_infos.end()) { - handles.push_back(it_handle->first); - ++it_handle; - } -} - -bool external_sensor_manager::create_command_channel(void) -{ - const int client_type = CLIENT_TYPE_EXTERNAL_SOURCE; - int client_id; - channel_ready_t channel_ready; - - if (!m_command_socket.create(SOCK_SEQPACKET)) - return false; - - if (!m_command_socket.connect(EVENT_CHANNEL_PATH)) { - _E("Failed to connect command channel for client %s, command socket fd[%d]", get_client_name(), m_command_socket.get_socket_fd()); - return false; - } - - m_command_socket.set_connection_mode(); - - if (m_command_socket.send(&client_type, sizeof(client_type)) <= 0) { - _E("Failed to send client type in client %s, event socket fd[%d]", get_client_name(), m_command_socket.get_socket_fd()); - return false; - } - - client_id = get_client_id(); - - if (m_command_socket.send(&client_id, sizeof(client_id)) <= 0) { - _E("Failed to send client id for client %s on command socket[%d]", get_client_name(), m_command_socket.get_socket_fd()); - return false; - } - - if (m_command_socket.recv(&channel_ready, sizeof(channel_ready)) <= 0) { - _E("%s failed to recv command channel ready packet on command socket[%d] with client id [%d]", - get_client_name(), m_command_socket.get_socket_fd(), client_id); - return false; - } - - if ((channel_ready.magic != CHANNEL_MAGIC_NUM) || (channel_ready.client_id != client_id)) { - _E("Command channel ready packet is wrong, magic = %#x, client id = %d", - channel_ready.magic, channel_ready.client_id); - return false; - } - - _I("Command channel is established for client %s on socket[%d] with client id : %d", - get_client_name(), m_command_socket.get_socket_fd(), client_id); - - return true; -} - -void external_sensor_manager::close_command_channel(void) -{ - m_command_socket.close(); -} - -bool external_sensor_manager::start_command_listener(void) -{ - if (!create_command_channel()) { - _E("Command channel is not established for %s", get_client_name()); - return false; - } - - m_command_socket.set_transfer_mode(); - - m_poller = new(std::nothrow) poller(m_command_socket.get_socket_fd()); - retvm_if(!m_poller, false, "Failed to allocate memory"); - - set_thread_state(THREAD_STATE_START); - - thread listener(&external_sensor_manager::listen_command, this); - listener.detach(); - - return true; -} - -void external_sensor_manager::stop_command_listener(void) -{ - const int THREAD_TERMINATING_TIMEOUT = 2; - - ulock u(m_thread_mutex); - - if (m_thread_state != THREAD_STATE_TERMINATE) { - m_thread_state = THREAD_STATE_STOP; - - _D("%s is waiting listener thread[state: %d] to be terminated", get_client_name(), m_thread_state); - if (m_thread_cond.wait_for(u, std::chrono::seconds(THREAD_TERMINATING_TIMEOUT)) - == std::cv_status::timeout) - _E("Fail to stop listener thread after waiting %d seconds", THREAD_TERMINATING_TIMEOUT); - else - _D("Listener thread for %s is terminated", get_client_name()); - } -} - -void external_sensor_manager::set_thread_state(thread_state state) -{ - lock l(m_thread_mutex); - m_thread_state = state; -} - -bool external_sensor_manager::get_cb_info(sensor_id_t sensor, char* data, int data_cnt, command_cb_info &cb_info) -{ - int handle; - const sensor_ext_handle_info *handle_info; - - AUTOLOCK(m_handle_info_lock); - - handle = get_handle(sensor); - - if (handle < 0) - return false; - - get_handle_info(handle, handle_info); - - cb_info.handle = handle_info->m_handle; - cb_info.sensor = handle_info->m_sensor; - cb_info.cb = handle_info->m_cb; - cb_info.data = data; - cb_info.data_cnt = data_cnt; - cb_info.user_data = handle_info->m_user_data; - - return true; -} - -bool external_sensor_manager::sensor_command_poll(void* buffer, int buffer_len, struct epoll_event &event) -{ - ssize_t len; - - len = m_command_socket.recv(buffer, buffer_len); - - if (!len) { - if (!m_poller->poll(event)) - return false; - len = m_command_socket.recv(buffer, buffer_len); - - if (len <= 0) { - _I("%s failed to read after poll!", get_client_name()); - return false; - } - } else if (len < 0) { - _I("%s failed to recv command from command socket", get_client_name()); - return false; - } - - return true; -} - -void external_sensor_manager::post_callback_to_main_loop(command_cb_info* cb_info) -{ - g_idle_add_full(G_PRIORITY_DEFAULT, callback_dispatcher, cb_info, NULL); -} - -void external_sensor_manager::handle_command(sensor_id_t sensor, char* data, int data_cnt) -{ - command_cb_info *cb_info = NULL; - - { /* scope for the lock */ - AUTOLOCK(m_handle_info_lock); - - cb_info = new(std::nothrow) command_cb_info; - if (!cb_info) { - _E("Failed to allocate memory"); - delete[] data; - return; - } - - if (!get_cb_info(sensor, data, data_cnt, *cb_info)) { - delete[] data; - delete cb_info; - _E("Sensor %d is not connected, so command is discarded", sensor); - return; - } - } - - if (cb_info) - post_callback_to_main_loop(cb_info); -} - -void external_sensor_manager::listen_command(void) -{ - external_command_header_t command_header; - struct epoll_event event; - event.events = EPOLLIN | EPOLLPRI; - - do { - lock l(m_thread_mutex); - if (m_thread_state == THREAD_STATE_START) { - if (!sensor_command_poll(&command_header, sizeof(command_header), event)) { - _I("Failed to poll command header"); - break; - } - - char *command = new(std::nothrow) char[command_header.command_len]; - if (!command) { - _E("Failed to allocated memory"); - break; - } - - if (!sensor_command_poll(command, command_header.command_len, event)) { - _I("Failed to poll command data"); - delete []command; - break; - } - - handle_command(command_header.sensor_id, command, command_header.command_len); - } else { - break; - } - } while (true); - - if (m_poller != NULL) { - delete m_poller; - m_poller = NULL; - } - - close_command_channel(); - - { /* the scope for the lock */ - lock l(m_thread_mutex); - m_thread_state = THREAD_STATE_TERMINATE; - m_thread_cond.notify_one(); - } - - _I("Command listener thread is terminated."); - - if (has_client_id() && (event.events & EPOLLHUP)) { - if (m_hup_observer) - m_hup_observer(); - } -} - -bool external_sensor_manager::is_valid_callback(const command_cb_info *cb_info) -{ - sensor_id_t sensor; - - if (!external_sensor_manager::get_instance().get_sensor(cb_info->handle, sensor)) - return false; - - return (cb_info->sensor == sensor); -} - -gboolean external_sensor_manager::callback_dispatcher(gpointer data) -{ - const command_cb_info *cb_info = reinterpret_cast(data); - - if (external_sensor_manager::get_instance().is_valid_callback(cb_info)) { - reinterpret_cast(cb_info->cb)(cb_info->handle, cb_info->data, cb_info->data_cnt, cb_info->user_data); - } else { - _W("Discard invalid callback cb(%#x)(%d, %#x, %d, %#x)", - cb_info->cb, cb_info->handle, cb_info->data, cb_info->data_cnt, cb_info->user_data); - } - - delete[] cb_info->data; - delete cb_info; - -/* -* To be called only once, it returns false -*/ - return false; -} - -void external_sensor_manager::clear(void) -{ - close_command_channel(); - stop_command_listener(); - close_data_channel(); - m_sensor_handle_infos.clear(); - set_client_id(CLIENT_ID_INVALID); -} - -void external_sensor_manager::set_hup_observer(hup_observer_t observer) -{ - m_hup_observer = observer; -} diff --git a/src/client/external_sensor_manager.h b/src/client/external_sensor_manager.h deleted file mode 100644 index 9259c2c..0000000 --- a/src/client/external_sensor_manager.h +++ /dev/null @@ -1,135 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2015 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 _EXTERNAL_SENSOR_MANAGER_H_ -#define _EXTERNAL_SENSOR_MANAGER_H_ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -class external_data_channel; -class poller; - -class sensor_ext_handle_info { -public: - int m_handle; - sensor_id_t m_sensor; - std::string m_key; - void *m_cb; - void *m_user_data; -}; - -typedef struct { - int handle; - sensor_id_t sensor; - char *data; - int data_cnt; - void *cb; - void *user_data; -} command_cb_info; - -class external_sensor_manager { -public: - typedef void (*hup_observer_t)(void); - - static external_sensor_manager& get_instance(void); - int create_handle(void); - bool delete_handle(int handle); - - bool set_handle(int handle, sensor_id_t sensor, const std::string& key, void* cb, void* user_data); - - bool get_sensor(int handle, sensor_id_t &sensor_id); - int get_handle(sensor_id_t sensor); - bool get_handle_info(int handle, const sensor_ext_handle_info*& handle_info); - std::string get_key(int handle); - - bool has_client_id(void); - int get_client_id(void); - void set_client_id(int client_id); - - bool add_data_channel(int handle, external_data_channel *channel); - bool get_data_channel(int handle, external_data_channel **channel); - bool close_data_channel(void); - bool close_data_channel(int handle); - - bool is_valid(int handle); - bool is_active(void); - - void get_all_handles(std::vector &handles); - - bool start_command_listener(void); - void stop_command_listener(void); - void clear(void); - - void set_hup_observer(hup_observer_t observer); -private: - enum thread_state { - THREAD_STATE_START, - THREAD_STATE_STOP, - THREAD_STATE_TERMINATE, - }; - - typedef std::lock_guard lock; - typedef std::unique_lock ulock; - - external_sensor_manager(); - ~external_sensor_manager(); - - external_sensor_manager(const external_sensor_manager&) {}; - external_sensor_manager& operator=(const external_sensor_manager&); - - bool create_command_channel(void); - void close_command_channel(void); - void set_thread_state(thread_state state); - - bool sensor_command_poll(void* buffer, int buffer_len, struct epoll_event &event); - - bool get_cb_info(sensor_id_t sensor, char* data, int data_cnt, command_cb_info &cb_info); - void post_callback_to_main_loop(command_cb_info* cb_info); - void handle_command(sensor_id_t sensor, char* data, int data_cnt); - void listen_command(void); - - bool is_valid_callback(const command_cb_info *cb_info); - static gboolean callback_dispatcher(gpointer data); - - int m_client_id; - - csocket m_command_socket; - poller *m_poller; - - cmutex m_handle_info_lock; - - thread_state m_thread_state; - std::mutex m_thread_mutex; - std::condition_variable m_thread_cond; - - hup_observer_t m_hup_observer; - - std::unordered_map m_sensor_handle_infos; - std::unordered_map m_sensor_handle_map; - std::unordered_map m_data_channels; -}; -#endif /* _EXTERNAL_SENSOR_MANAGER_H_ */ diff --git a/src/client/reg_event_info.h b/src/client/reg_event_info.h deleted file mode 100644 index 1f65f1d..0000000 --- a/src/client/reg_event_info.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2013 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 _REG_EVENT_INFO_H_ -#define _REG_EVENT_INFO_H_ - -#include - -class reg_event_info { -public: - unsigned long long m_id; - int m_handle; - unsigned int type; - unsigned int m_interval; - unsigned int m_latency; - void *m_cb; - void *m_user_data; - unsigned long long m_previous_event_time; - bool m_fired; - - reg_event_info() - : m_id(0), m_handle(-1), - type(0), m_interval(POLL_1HZ_MS), m_latency(0), - m_cb(NULL), m_user_data(NULL), - m_previous_event_time(0), m_fired(false) {} - - ~reg_event_info() {} -}; - -#endif /* _REG_EVENT_INFO_H_ */ diff --git a/src/client/sensor_callback_deliverer.cpp b/src/client/sensor_callback_deliverer.cpp deleted file mode 100644 index 9af3790..0000000 --- a/src/client/sensor_callback_deliverer.cpp +++ /dev/null @@ -1,155 +0,0 @@ -/* - * sensord - * - * 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 -#include -#include - -sensor_callback_deliverer::sensor_callback_deliverer() -: m_running(false) -, m_callbacks(NULL) -{ -} - -sensor_callback_deliverer::~sensor_callback_deliverer() -{ -} - -bool sensor_callback_deliverer::start(void) -{ - if (is_running()) - return true; - - m_callbacks = g_async_queue_new(); - retvm_if(!m_callbacks, false, "Failed to allocated memory"); - - m_deliverer = new(std::nothrow) std::thread(&sensor_callback_deliverer::run, this); - - if (!m_deliverer) { - g_async_queue_unref(m_callbacks); - _E("Failed to allocated memory"); - return false; - } - - m_running = true; - - _I("Succeeded to start"); - - return true; -} - -bool sensor_callback_deliverer::stop(void) -{ - if (!is_running()) - return true; - - if (!terminate()) - return false; - - m_deliverer->join(); - delete m_deliverer; - - g_async_queue_unref(m_callbacks); - - _I("Succeeded to stop"); - - return true; -} - -bool sensor_callback_deliverer::is_running(void) -{ - return m_running.load(); -} - -bool sensor_callback_deliverer::push(client_callback_info *ci) -{ - if (!is_running()) - return false; - - retvm_if(!ci, false, "Invalid callback"); - - g_async_queue_push(m_callbacks, ci); - - return true; -} - -gboolean sensor_callback_deliverer::callback_dispatcher(gpointer data) -{ - bool ret; - client_callback_info *ci = (client_callback_info *)data; - - ret = sensor_client_info::get_instance().is_event_active(ci->handle, ci->event_type, ci->event_id); - - if (!ret) { - _W("Discard invalid callback cb(%#x)(%s, %#x, %#x) with id: %llu", - ci->cb, get_event_name(ci->event_type), ci->sensor_data.get(), - ci->user_data, ci->event_id); - - delete ci; - return FALSE; - } - - if (ci->accuracy_cb) - ci->accuracy_cb(ci->sensor, ci->timestamp, ci->accuracy, ci->accuracy_user_data); - - if (ci->cb) - ci->cb(ci->sensor, ci->event_type, (sensor_data_t *)ci->sensor_data.get(), ci->user_data); - - delete ci; - - /* To be called only once, it returns false */ - return FALSE; -} - -void sensor_callback_deliverer::run(void) -{ - client_callback_info *ci; - - while (is_running()) { - ci = static_cast(g_async_queue_pop(m_callbacks)); - - if (ci->handle == THREAD_TERMINATION) { - m_running = false; - delete ci; - return; - } - - deliver_to_main_loop(ci); - } -} - -bool sensor_callback_deliverer::terminate(void) -{ - client_callback_info *ci = new(std::nothrow) client_callback_info; - retvm_if(!ci, false, "Failed to allocated memory"); - - ci->handle = THREAD_TERMINATION; - g_async_queue_push(m_callbacks, ci); - - return true; -} - -void sensor_callback_deliverer::deliver_to_main_loop(client_callback_info *ci) -{ - /* - * Because callback function always returns FALSE, - * it is unnecessary to manage g_source id returned from g_idle_add(). - */ - g_idle_add(callback_dispatcher, ci); -} diff --git a/src/client/sensor_callback_deliverer.h b/src/client/sensor_callback_deliverer.h deleted file mode 100644 index db2145d..0000000 --- a/src/client/sensor_callback_deliverer.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * sensord - * - * 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. - * - */ - -#ifndef _SENSOR_CALLBACK_DELIVERER_H_ -#define _SENSOR_CALLBACK_DELIVERER_H_ - -#include -#include - -#include -#include - -class sensor_callback_deliverer { -public: - sensor_callback_deliverer(); - ~sensor_callback_deliverer(); - - bool start(void); - bool stop(void); - bool is_running(void); - - bool push(client_callback_info *ci); - -private: - static gboolean callback_dispatcher(gpointer data); - - std::atomic_bool m_running; - - GAsyncQueue *m_callbacks; - std::thread *m_deliverer; - - void run(void); - bool terminate(void); - - void deliver_to_main_loop(client_callback_info *ci); -}; - -#endif /* _SENSOR_CALLBACK_DELIVERER_H_ */ diff --git a/src/client/sensor_client_info.cpp b/src/client/sensor_client_info.cpp deleted file mode 100644 index 2a755aa..0000000 --- a/src/client/sensor_client_info.cpp +++ /dev/null @@ -1,723 +0,0 @@ -/* - * sensord - * - * 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 -#include -#include - -#include -#include - -using std::thread; -using std::pair; - -sensor_client_info::sensor_client_info() -: m_client_id(CLIENT_ID_INVALID) -{ -} - -sensor_client_info::~sensor_client_info() -{ -} - -sensor_client_info& sensor_client_info::get_instance(void) -{ - static sensor_client_info inst; - return inst; -} - -int sensor_client_info::create_handle(sensor_id_t sensor) -{ - sensor_handle_info handle_info; - int handle = 0; - - AUTOLOCK(m_handle_info_lock); - - while (m_sensor_handle_infos.count(handle) > 0) - handle++; - - if (handle == MAX_HANDLE) { - _W("Handles of client %s are full", get_client_name()); - return MAX_HANDLE_REACHED; - } - - handle_info.m_sensor_id = sensor; - handle_info.m_sensor_state = SENSOR_STATE_STOPPED; - handle_info.m_pause_policy = SENSORD_PAUSE_ALL; - handle_info.m_handle = handle; - handle_info.m_accuracy = -1; - handle_info.m_accuracy_cb = NULL; - handle_info.m_accuracy_user_data = NULL; - - m_sensor_handle_infos.insert(pair(handle, handle_info)); - - return handle; -} - -bool sensor_client_info::delete_handle(int handle) -{ - AUTOLOCK(m_handle_info_lock); - - auto it_handle = m_sensor_handle_infos.find(handle); - - if (it_handle == m_sensor_handle_infos.end()) { - _W("Handle[%d] is not found for client %s", handle, get_client_name()); - return false; - } - - m_sensor_handle_infos.erase(it_handle); - return true; -} - -bool sensor_client_info::is_active(void) -{ - AUTOLOCK(m_handle_info_lock); - - return !m_sensor_handle_infos.empty(); -} - -bool sensor_client_info::register_event(int handle, unsigned int event_type, - unsigned int interval, unsigned int latency, void *cb, void* user_data) -{ - AUTOLOCK(m_handle_info_lock); - - auto it_handle = m_sensor_handle_infos.find(handle); - - if (it_handle == m_sensor_handle_infos.end()) { - _W("Handle[%d] is not found for client %s", handle, get_client_name()); - return false; - } - - if (!it_handle->second.add_reg_event_info(event_type, interval, latency, cb, user_data)) - return false; - - return true; -} - -bool sensor_client_info::unregister_event(int handle, unsigned int event_type) -{ - AUTOLOCK(m_handle_info_lock); - - auto it_handle = m_sensor_handle_infos.find(handle); - - if (it_handle == m_sensor_handle_infos.end()) { - _W("Handle[%d] is not found for client %s", handle, get_client_name()); - return false; - } - - if (!it_handle->second.delete_reg_event_info(event_type)) - return false; - - return true; -} - -bool sensor_client_info::register_accuracy_cb(int handle, sensor_accuracy_changed_cb_t cb, void* user_data) -{ - AUTOLOCK(m_handle_info_lock); - - auto it_handle = m_sensor_handle_infos.find(handle); - - if (it_handle == m_sensor_handle_infos.end()) { - _W("Handle[%d] is not found for client %s", handle, get_client_name()); - return false; - } - - it_handle->second.m_accuracy = -1; - it_handle->second.m_accuracy_cb = cb; - it_handle->second.m_accuracy_user_data = user_data; - - return true; -} - -bool sensor_client_info::unregister_accuracy_cb(int handle) -{ - AUTOLOCK(m_handle_info_lock); - - auto it_handle = m_sensor_handle_infos.find(handle); - - if (it_handle == m_sensor_handle_infos.end()) { - _W("Handle[%d] is not found for client %s", handle, get_client_name()); - return false; - } - - it_handle->second.m_accuracy = -1; - it_handle->second.m_accuracy_cb = NULL; - it_handle->second.m_accuracy_user_data = NULL; - - return true; -} - -bool sensor_client_info::set_sensor_params(int handle, int sensor_state, int pause_policy) -{ - AUTOLOCK(m_handle_info_lock); - - auto it_handle = m_sensor_handle_infos.find(handle); - - if (it_handle == m_sensor_handle_infos.end()) { - _W("Handle[%d] is not found for client %s", handle, get_client_name()); - return false; - } - - it_handle->second.m_sensor_state = sensor_state; - it_handle->second.m_pause_policy = pause_policy; - - return true; -} - -bool sensor_client_info::get_sensor_params(int handle, int &sensor_state, int &pause_policy) -{ - AUTOLOCK(m_handle_info_lock); - - auto it_handle = m_sensor_handle_infos.find(handle); - - if (it_handle == m_sensor_handle_infos.end()) { - _W("Handle[%d] is not found for client %s", handle, get_client_name()); - return false; - } - - sensor_state = it_handle->second.m_sensor_state; - pause_policy = it_handle->second.m_pause_policy; - - return true; -} - -bool sensor_client_info::set_sensor_state(int handle, int sensor_state) -{ - AUTOLOCK(m_handle_info_lock); - - auto it_handle = m_sensor_handle_infos.find(handle); - - if (it_handle == m_sensor_handle_infos.end()) { - _W("Handle[%d] is not found for client %s", handle, get_client_name()); - return false; - } - - it_handle->second.m_sensor_state = sensor_state; - - return true; -} - -bool sensor_client_info::get_passive_mode(int handle) -{ - AUTOLOCK(m_handle_info_lock); - - auto it_handle = m_sensor_handle_infos.find(handle); - - if (it_handle == m_sensor_handle_infos.end()) { - _W("Handle[%d] is not found for client %s", handle, get_client_name()); - return false; - } - - return it_handle->second.get_passive_mode(); -} - -bool sensor_client_info::set_passive_mode(int handle, bool passive) -{ - AUTOLOCK(m_handle_info_lock); - - auto it_handle = m_sensor_handle_infos.find(handle); - - if (it_handle == m_sensor_handle_infos.end()) { - _W("Handle[%d] is not found for client %s", handle, get_client_name()); - return false; - } - - it_handle->second.set_passive_mode(passive); - - return true; -} - -bool sensor_client_info::set_sensor_pause_policy(int handle, int pause_policy) -{ - AUTOLOCK(m_handle_info_lock); - - auto it_handle = m_sensor_handle_infos.find(handle); - - if (it_handle == m_sensor_handle_infos.end()) { - _W("Handle[%d] is not found for client %s", handle, get_client_name()); - return false; - } - - it_handle->second.m_pause_policy = pause_policy; - - return true; -} - -bool sensor_client_info::set_event_batch(int handle, unsigned int event_type, unsigned int interval, unsigned int latency) -{ - AUTOLOCK(m_handle_info_lock); - - auto it_handle = m_sensor_handle_infos.find(handle); - - if (it_handle == m_sensor_handle_infos.end()) { - _W("Handle[%d] is not found for client %s", handle, get_client_name()); - return false; - } - - if (!it_handle->second.change_reg_event_batch(event_type, interval, latency)) - return false; - - return true; -} - -bool sensor_client_info::set_accuracy(int handle, int accuracy) -{ - AUTOLOCK(m_handle_info_lock); - - auto it_handle = m_sensor_handle_infos.find(handle); - - if (it_handle == m_sensor_handle_infos.end()) { - _W("Handle[%d] is not found for client %s", handle, get_client_name()); - return false; - } - - it_handle->second.m_accuracy = accuracy; - - return true; -} - -bool sensor_client_info::set_bad_accuracy(int handle, int bad_accuracy) -{ - AUTOLOCK(m_handle_info_lock); - - auto it_handle = m_sensor_handle_infos.find(handle); - - if (it_handle == m_sensor_handle_infos.end()) { - _W("Handle[%d] is not found for client %s", handle, get_client_name()); - return false; - } - - it_handle->second.m_bad_accuracy = bad_accuracy; - - return true; -} - -bool sensor_client_info::get_event_info(int handle, unsigned int event_type, unsigned int &interval, unsigned int &latency, void* &cb, void* &user_data) -{ - AUTOLOCK(m_handle_info_lock); - - auto it_handle = m_sensor_handle_infos.find(handle); - - if (it_handle == m_sensor_handle_infos.end()) { - _W("Handle[%d] is not found for client %s", handle, get_client_name()); - return false; - } - - const reg_event_info *event_info; - - event_info = it_handle->second.get_reg_event_info(event_type); - - if (!event_info) - return NULL; - - interval = event_info->m_interval; - cb = event_info->m_cb; - user_data = event_info->m_user_data; - latency = event_info->m_latency; - - return true; -} - -void sensor_client_info::get_listening_sensors(sensor_id_vector &sensors) -{ - AUTOLOCK(m_handle_info_lock); - - auto it_handle = m_sensor_handle_infos.begin(); - - while (it_handle != m_sensor_handle_infos.end()) { - sensors.push_back(it_handle->second.m_sensor_id); - ++it_handle; - } - - sort(sensors.begin(), sensors.end()); - unique(sensors.begin(), sensors.end()); -} - -void sensor_client_info::get_sensor_rep(sensor_id_t sensor, sensor_rep& rep) { - const unsigned int INVALID_BATCH_VALUE = std::numeric_limits::max(); - - rep.active = is_sensor_active(sensor); - rep.pause_policy = get_active_pause_policy(sensor); - if (!get_active_batch(sensor, rep.interval, rep.latency)) { - rep.interval = INVALID_BATCH_VALUE; - rep.latency = INVALID_BATCH_VALUE; - } - - get_active_event_types(sensor, rep.event_types); -} - -bool sensor_client_info::add_command_channel(sensor_id_t sensor, command_channel *cmd_channel) -{ - auto it_channel = m_command_channels.find(sensor); - - if (it_channel != m_command_channels.end()) { - _W("%s alreay has command_channel for %s", get_client_name(), get_sensor_name(sensor)); - return false; - } - - m_command_channels.insert(pair(sensor, cmd_channel)); - - return true; -} - -bool sensor_client_info::get_command_channel(sensor_id_t sensor, command_channel **cmd_channel) -{ - auto it_channel = m_command_channels.find(sensor); - - if (it_channel == m_command_channels.end()) { - _W("%s doesn't have command_channel for %s", get_client_name(), get_sensor_name(sensor)); - return false; - } - - *cmd_channel = it_channel->second; - - return true; -} - -bool sensor_client_info::close_command_channel(void) -{ - auto it_channel = m_command_channels.begin(); - - if (it_channel != m_command_channels.end()) { - delete it_channel->second; - ++it_channel; - } - - m_command_channels.clear(); - - return true; -} - -bool sensor_client_info::close_command_channel(sensor_id_t sensor_id) -{ - auto it_channel = m_command_channels.find(sensor_id); - - if (it_channel == m_command_channels.end()) { - _W("%s doesn't have command_channel for %s", get_client_name(), get_sensor_name(sensor_id)); - return false; - } - - delete it_channel->second; - - m_command_channels.erase(it_channel); - - return true; -} - -bool sensor_client_info::has_client_id(void) -{ - return (m_client_id != CLIENT_ID_INVALID); -} - -int sensor_client_info::get_client_id(void) -{ - return m_client_id; -} - -void sensor_client_info::set_client_id(int client_id) -{ - m_client_id = client_id; -} - -bool sensor_client_info::get_active_batch(sensor_id_t sensor, unsigned int &interval, unsigned int &latency) -{ - unsigned int min_interval = POLL_MAX_HZ_MS; - unsigned int min_latency = std::numeric_limits::max(); - - unsigned int _interval; - unsigned int _latency; - - AUTOLOCK(m_handle_info_lock); - - auto it_handle = m_sensor_handle_infos.begin(); - - while (it_handle != m_sensor_handle_infos.end()) { - if ((it_handle->second.m_sensor_id == sensor) && - it_handle->second.is_started()) { - it_handle->second.get_batch(_interval, _latency); - min_interval = (_interval < min_interval) ? _interval : min_interval; - min_latency = (_latency < min_latency) ? _latency : min_latency; - } - - ++it_handle; - } - - if (!is_sensor_active(sensor)) { - _D("Active sensor[%#llx] is not found for client %s", sensor, get_client_name()); - return false; - } - - interval = min_interval; - latency = min_latency; - - return true; -} - -unsigned int sensor_client_info::get_active_pause_policy(sensor_id_t sensor) -{ - int active_pause = SENSORD_PAUSE_ALL; - int pause; - - AUTOLOCK(m_handle_info_lock); - - auto it_handle = m_sensor_handle_infos.begin(); - - while (it_handle != m_sensor_handle_infos.end()) { - if ((it_handle->second.m_sensor_id == sensor) && - it_handle->second.is_started()) { - pause = it_handle->second.m_pause_policy; - active_pause = (pause < active_pause) ? pause: active_pause; - } - - ++it_handle; - } - - if (!is_sensor_active(sensor)) - _D("Active sensor[%#llx] is not found for client %s", sensor, get_client_name()); - - return active_pause; -} - -bool sensor_client_info::get_sensor_id(int handle, sensor_id_t &sensor) -{ - AUTOLOCK(m_handle_info_lock); - - auto it_handle = m_sensor_handle_infos.find(handle); - - if (it_handle == m_sensor_handle_infos.end()) { - _W("Handle[%d] is not found for client %s", handle, get_client_name()); - return false; - } - - sensor = it_handle->second.m_sensor_id; - - return true; -} - -bool sensor_client_info::get_sensor_state(int handle, int &sensor_state) -{ - AUTOLOCK(m_handle_info_lock); - - auto it_handle = m_sensor_handle_infos.find(handle); - - if (it_handle == m_sensor_handle_infos.end()) { - _W("Handle[%d] is not found for client %s", handle, get_client_name()); - return false; - } - - sensor_state = it_handle->second.m_sensor_state; - - return true; -} - -void sensor_client_info::get_active_event_types(sensor_id_t sensor, event_type_vector &active_event_types) -{ - event_type_vector event_types; - - AUTOLOCK(m_handle_info_lock); - - auto it_handle = m_sensor_handle_infos.begin(); - - while (it_handle != m_sensor_handle_infos.end()) { - if ((it_handle->second.m_sensor_id == sensor) && - it_handle->second.is_started()) - it_handle->second.get_reg_event_types(event_types); - - ++it_handle; - } - - if (event_types.empty()) - return; - - sort(event_types.begin(), event_types.end()); - - unique_copy(event_types.begin(), event_types.end(), back_inserter(active_event_types)); -} - -void sensor_client_info::get_all_handles(handle_vector &handles) -{ - AUTOLOCK(m_handle_info_lock); - - auto it_handle = m_sensor_handle_infos.begin(); - - while (it_handle != m_sensor_handle_infos.end()) { - handles.push_back(it_handle->first); - ++it_handle; - } -} - -void sensor_client_info::get_sensor_handle_info(sensor_id_t sensor, sensor_handle_info_map &handles_info) -{ - AUTOLOCK(m_handle_info_lock); - - auto it_handle = m_sensor_handle_infos.begin(); - - while (it_handle != m_sensor_handle_infos.end()) { - if (it_handle->second.m_sensor_id == sensor) - handles_info.insert(pair(it_handle->first, it_handle->second)); - - ++it_handle; - } -} - -void sensor_client_info::get_all_handle_info(sensor_handle_info_map &handles_info) -{ - AUTOLOCK(m_handle_info_lock); - - auto it_handle = m_sensor_handle_infos.begin(); - - while (it_handle != m_sensor_handle_infos.end()) { - handles_info.insert(pair(it_handle->first, it_handle->second)); - ++it_handle; - } -} - -bool sensor_client_info::is_sensor_registered(sensor_id_t sensor) -{ - AUTOLOCK(m_handle_info_lock); - - auto it_handle = m_sensor_handle_infos.begin(); - - while (it_handle != m_sensor_handle_infos.end()) { - if (it_handle->second.m_sensor_id == sensor) - return true; - - ++it_handle; - } - - return false; -} - -bool sensor_client_info::is_sensor_active(sensor_id_t sensor) -{ - AUTOLOCK(m_handle_info_lock); - - auto it_handle = m_sensor_handle_infos.begin(); - - while (it_handle != m_sensor_handle_infos.end()) { - if ((it_handle->second.m_sensor_id == sensor) && - it_handle->second.is_started()) - return true; - - ++it_handle; - } - - return false; -} - -bool sensor_client_info::is_event_active(int handle, unsigned int event_type, unsigned long long event_id) -{ - reg_event_info *event_info; - - AUTOLOCK(m_handle_info_lock); - - auto it_handle = m_sensor_handle_infos.find(handle); - - if (it_handle == m_sensor_handle_infos.end()) - return false; - - event_info = it_handle->second.get_reg_event_info(event_type); - if (!event_info) - return false; - - if (event_info->m_id != event_id) - return false; - - return true; -} - -void sensor_client_info::set_pause_policy(sensor_id_t sensor, int pause_policy) -{ - sensor_handle_info_map handles_info; - - get_sensor_handle_info(sensor, handles_info); - - for (auto it_handle = handles_info.begin(); it_handle != handles_info.end(); ++it_handle) { - if (it_handle->second.m_sensor_id != sensor) - continue; - - if (pause_policy && (it_handle->second.m_pause_policy & pause_policy)) { - if (it_handle->second.m_sensor_state == SENSOR_STATE_STARTED) { - set_sensor_state(it_handle->first, SENSOR_STATE_PAUSED); - _I("%s's %s[%d] is paused", get_client_name(), get_sensor_name(sensor), it_handle->first); - } - } else { - if (it_handle->second.m_sensor_state == SENSOR_STATE_PAUSED) { - set_sensor_state(it_handle->first, SENSOR_STATE_STARTED); - _I("%s's %s[%d] is resumed", get_client_name(), get_sensor_name(sensor), it_handle->first); - } - } - } -} - -bool sensor_client_info::set_attribute(int handle, int attribute, int value) -{ - AUTOLOCK(m_handle_info_lock); - - auto it_handle = m_sensor_handle_infos.find(handle); - - if (it_handle == m_sensor_handle_infos.end()) { - _W("Handle[%d] is not found for client %s", handle, get_client_name()); - return false; - } - - it_handle->second.attributes_int[attribute] = value; - - return true; -} - -bool sensor_client_info::set_attribute(int handle, int attribute, const char *value, int len) -{ - AUTOLOCK(m_handle_info_lock); - - auto it_handle = m_sensor_handle_infos.find(handle); - - if (it_handle == m_sensor_handle_infos.end()) { - _W("Handle[%d] is not found for client %s", handle, get_client_name()); - return false; - } - - auto it_attr = it_handle->second.attributes_str.find(attribute); - - if (it_attr != it_handle->second.attributes_str.end()) { - it_attr->second->set(value, len); - return true; - } - - attribute_info *info = new(std::nothrow) attribute_info(); - retvm_if(!info, false, "Failed to allocate memory"); - - info->set(value, len); - it_handle->second.attributes_str[attribute] = info; - - return true; -} - -void sensor_client_info::clear(void) -{ - auto it_handle = m_sensor_handle_infos.begin(); - - while (it_handle != m_sensor_handle_infos.end()) - it_handle->second.clear(); - - close_command_channel(); - m_sensor_handle_infos.clear(); - m_command_channels.clear(); - set_client_id(CLIENT_ID_INVALID); -} diff --git a/src/client/sensor_client_info.h b/src/client/sensor_client_info.h deleted file mode 100644 index 15f2677..0000000 --- a/src/client/sensor_client_info.h +++ /dev/null @@ -1,120 +0,0 @@ -/* - * sensord - * - * 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. - * - */ - -#ifndef _SENSOR_CLIENT_INFO_H_ -#define _SENSOR_CLIENT_INFO_H_ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef std::vector handle_vector; -typedef std::vector sensor_id_vector; -typedef std::unordered_map sensor_handle_info_map; -typedef std::unordered_map sensor_command_channel_map; - -typedef struct sensor_rep { - bool active; - int pause_policy; - unsigned int interval; - unsigned int latency; - event_type_vector event_types; -} sensor_rep; - -class sensor_client_info { -public: - static sensor_client_info& get_instance(void); - int create_handle(sensor_id_t sensor_id); - bool delete_handle(int handle); - bool register_event(int handle, unsigned int event_type, - unsigned int interval, unsigned int latency, void *cb, void* user_data); - bool unregister_event(int handle, unsigned int event_type); - - bool register_accuracy_cb(int handle, sensor_accuracy_changed_cb_t cb, void* user_data); - bool unregister_accuracy_cb(int handle); - - bool set_sensor_params(int handle, int sensor_state, int sensor_pause_policy); - bool get_sensor_params(int handle, int &sensor_state, int &sensor_pause_policy); - bool set_sensor_state(int handle, int sensor_state); - - bool get_passive_mode(int handle); - bool set_passive_mode(int handle, bool passive); - - bool set_attribute(int handle, int attribute, int value); - bool set_attribute(int handle, int attribute, const char *value, int len); - - bool set_sensor_pause_policy(int handle, int pause_policy); - bool set_event_batch(int handle, unsigned int event_type, unsigned int interval, unsigned int latency); - bool set_accuracy(int handle, int accuracy); - bool set_bad_accuracy(int handle, int bad_accuracy); - bool get_event_info(int handle, unsigned int event_type, unsigned int &interval, unsigned int &latency, void* &cb, void* &user_data); - void get_listening_sensors(sensor_id_vector &sensors); - void get_sensor_rep(sensor_id_t sensor, sensor_rep& rep); - - bool get_active_batch(sensor_id_t sensor, unsigned int &interval, unsigned int &latency); - unsigned int get_active_pause_policy(sensor_id_t sensor_id); - void get_active_event_types(sensor_id_t sensor_id, event_type_vector &active_event_types); - - bool get_sensor_id(int handle, sensor_id_t &sensor_id); - bool get_sensor_state(int handle, int &state); - - bool has_client_id(void); - int get_client_id(void); - void set_client_id(int client_id); - - bool is_active(void); - bool is_sensor_registered(sensor_id_t sensor_id); - bool is_sensor_active(sensor_id_t sensor_id); - bool is_event_active(int handle, unsigned int event_type, unsigned long long event_id); - - bool add_command_channel(sensor_id_t sensor_id, command_channel *cmd_channel); - bool get_command_channel(sensor_id_t sensor_id, command_channel **cmd_channel); - bool close_command_channel(void); - bool close_command_channel(sensor_id_t sensor_id); - - void get_all_handles(handle_vector &handles); - void get_sensor_handle_info(sensor_id_t sensor, sensor_handle_info_map &handles_info); - void get_all_handle_info(sensor_handle_info_map &handles_info); - - void set_pause_policy(sensor_id_t sensor, int power_save_state); - - void clear(void); - - sensor_client_info(); - ~sensor_client_info(); - -private: - sensor_handle_info_map m_sensor_handle_infos; - sensor_command_channel_map m_command_channels; - - int m_client_id; - - cmutex m_handle_info_lock; -}; - -#endif /* _SENSOR_CLIENT_INFO_H_ */ diff --git a/src/client/sensor_event_listener.cpp b/src/client/sensor_event_listener.cpp deleted file mode 100644 index fd11d4f..0000000 --- a/src/client/sensor_event_listener.cpp +++ /dev/null @@ -1,477 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2014 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 -#include -#include - -#include -#include -#include - -#include - -/* TODO: this macro should be adjusted(4224 = 4096(data) + 128(header)) */ -#define EVENT_BUFFER_SIZE sizeof(sensor_event_t) - -using std::thread; -using std::pair; -using std::vector; - -struct free_data { - void operator()(void *data) { - free(data); - } -}; - -sensor_event_listener::sensor_event_listener() -: m_poller(NULL) -, m_thread_state(THREAD_STATE_TERMINATE) -, m_hup_observer(NULL) -, m_client_info(sensor_client_info::get_instance()) -, m_axis(SENSORD_AXIS_DISPLAY_ORIENTED) -, m_display_rotation(AUTO_ROTATION_DEGREE_UNKNOWN) -{ -} - -sensor_event_listener::~sensor_event_listener() -{ - stop_event_listener(); -} - -sensor_event_listener& sensor_event_listener::get_instance(void) -{ - static sensor_event_listener inst; - return inst; -} - -client_callback_info* sensor_event_listener::handle_calibration_cb(sensor_handle_info &handle_info, unsigned event_type, unsigned long long time, int accuracy) -{ - unsigned int cal_event_type = get_calibration_event_type(event_type); - reg_event_info *event_info = NULL; - reg_event_info *cal_event_info = NULL; - client_callback_info* cal_callback_info = NULL; - - if (!cal_event_type) - return NULL; - - cal_event_info = handle_info.get_reg_event_info(cal_event_type); - if ((accuracy == SENSOR_ACCURACY_BAD) && !handle_info.m_bad_accuracy && cal_event_info) { - cal_event_info->m_previous_event_time = time; - - event_info = handle_info.get_reg_event_info(event_type); - if (!event_info) - return NULL; - - sensor_data_t *cal_data = (sensor_data_t *)malloc(sizeof(sensor_data_t)); - retvm_if(!cal_data, NULL, "Failed to allocate memory"); - - cal_data->accuracy = accuracy; - cal_data->timestamp = time; - cal_data->values[0] = accuracy; - cal_data->value_count = 1; - std::shared_ptr cal_sensor_data(cal_data, free_data()); - - cal_callback_info = get_callback_info(handle_info.m_sensor_id, cal_event_info, cal_sensor_data); - - m_client_info.set_bad_accuracy(handle_info.m_handle, true); - - print_event_occurrence_log(handle_info); - } - - if ((accuracy != SENSOR_ACCURACY_BAD) && handle_info.m_bad_accuracy) - m_client_info.set_bad_accuracy(handle_info.m_handle, false); - - return cal_callback_info; -} - -void sensor_event_listener::handle_events(void* event) -{ - unsigned long long cur_time; - reg_event_info *event_info = NULL; - sensor_id_t sensor_id; - sensor_handle_info_map handles_info; - - int accuracy = SENSOR_ACCURACY_GOOD; - - unsigned int event_type = *((unsigned int *)(event)); - - client_callback_info* callback_info = NULL; - - sensor_event_t *sensor_event = (sensor_event_t *)event; - sensor_id = sensor_event->sensor_id; - cur_time = sensor_event->data->timestamp; - accuracy = sensor_event->data->accuracy; - - align_sensor_axis(sensor_id, sensor_event->data); - - std::shared_ptr sensor_data(sensor_event->data, free_data()); - - { /* scope for the lock */ - m_client_info.get_all_handle_info(handles_info); - - for (auto it_handle = handles_info.begin(); it_handle != handles_info.end(); ++it_handle) { - sensor_handle_info &sensor_handle_info = it_handle->second; - - event_info = sensor_handle_info.get_reg_event_info(event_type); - if ((sensor_handle_info.m_sensor_id != sensor_id) || - !sensor_handle_info.is_started() || - !event_info) - continue; - - if (event_info->m_fired) - continue; - - event_info->m_previous_event_time = cur_time; - - client_callback_info* cal_callback_info = handle_calibration_cb(sensor_handle_info, event_type, cur_time, accuracy); - - if (cal_callback_info) - m_cb_deliverer->push(cal_callback_info); - - callback_info = get_callback_info(sensor_id, event_info, sensor_data); - - if (!callback_info) { - _E("Failed to get callback_info"); - continue; - } - - if (sensor_handle_info.m_accuracy != accuracy) { - m_client_info.set_accuracy(sensor_handle_info.m_handle, accuracy); - - callback_info->accuracy_cb = sensor_handle_info.m_accuracy_cb; - callback_info->timestamp = cur_time; - callback_info->accuracy = accuracy; - callback_info->accuracy_user_data = sensor_handle_info.m_accuracy_user_data; - } - - m_cb_deliverer->push(callback_info); - - print_event_occurrence_log(sensor_handle_info); - } - } -} - -client_callback_info* sensor_event_listener::get_callback_info(sensor_id_t sensor_id, const reg_event_info *event_info, std::shared_ptr sensor_data) -{ - client_callback_info* callback_info; - - callback_info = new(std::nothrow)client_callback_info; - retvm_if(!callback_info, NULL, "Failed to allocate memory"); - - callback_info->sensor = sensor_info_to_sensor(sensor_info_manager::get_instance().get_info(sensor_id)); - callback_info->event_id = event_info->m_id; - callback_info->handle = event_info->m_handle; - callback_info->cb = (sensor_cb_t)(event_info->m_cb); - callback_info->event_type = event_info->type; - callback_info->user_data = event_info->m_user_data; - callback_info->accuracy_cb = NULL; - callback_info->timestamp = 0; - callback_info->accuracy = -1; - callback_info->accuracy_user_data = NULL; - callback_info->sensor_data = sensor_data; - - return callback_info; -} - -void sensor_event_listener::set_sensor_axis(int axis) -{ - m_axis = axis; -} - -void sensor_event_listener::align_sensor_axis(sensor_id_t sensor, sensor_data_t *data) -{ - sensor_id_t type = CONVERT_ID_TYPE(sensor); - - if (m_axis != SENSORD_AXIS_DISPLAY_ORIENTED) - return; - - if (type != ACCELEROMETER_SENSOR && type != GYROSCOPE_SENSOR && type != GRAVITY_SENSOR && type != LINEAR_ACCEL_SENSOR) - return; - - float x, y; - - switch (m_display_rotation) { - case AUTO_ROTATION_DEGREE_90: /* Landscape Left */ - x = -data->values[1]; - y = data->values[0]; - break; - case AUTO_ROTATION_DEGREE_180: /* Portrait Bottom */ - x = -data->values[0]; - y = -data->values[1]; - break; - case AUTO_ROTATION_DEGREE_270: /* Landscape Right */ - x = data->values[1]; - y = -data->values[0]; - break; - default: - return; - } - - data->values[0] = x; - data->values[1] = y; -} - -ssize_t sensor_event_listener::sensor_event_poll(void* buffer, int buffer_len, struct epoll_event &event) -{ - ssize_t len; - - len = m_event_socket.recv(buffer, buffer_len); - - if (!len) { - if (!m_poller->poll(event)) - return -1; - len = m_event_socket.recv(buffer, buffer_len); - - if (!len) { - _E("%s failed to read after poll!", get_client_name()); - return -1; - } - } - - if (len < 0) { - _E("%s failed to recv event from event socket", get_client_name()); - return -1; - } - - return len; -} - -void sensor_event_listener::listen_events(void) -{ - char buffer[EVENT_BUFFER_SIZE]; - struct epoll_event event; - ssize_t len = -1; - - event.events = EPOLLIN | EPOLLPRI; - - do { - void *buffer_data; - int data_len; - - lock l(m_thread_mutex); - if (m_thread_state != THREAD_STATE_START) - break; - - len = sensor_event_poll(buffer, sizeof(sensor_event_t), event); - if (len <= 0) { - _E("Failed to sensor_event_poll()"); - break; - } - - sensor_event_t *sensor_event = reinterpret_cast(buffer); - data_len = sensor_event->data_length; - - if (data_len == 0) - continue; - - buffer_data = malloc(data_len); - - len = sensor_event_poll(buffer_data, data_len, event); - if (len <= 0) { - _E("Failed to sensor_event_poll() for sensor_data"); - free(buffer_data); - break; - } - - sensor_event->data = reinterpret_cast(buffer_data); - - handle_events((void *)buffer); - } while (true); - - if (m_poller) { - delete m_poller; - m_poller = NULL; - } - - close_event_channel(); - - { /* the scope for the lock */ - lock l(m_thread_mutex); - m_thread_state = THREAD_STATE_TERMINATE; - m_thread_cond.notify_one(); - } - - _I("Event listener thread is terminated."); - - if (m_client_info.has_client_id() && (event.events & EPOLLHUP)) { - if (m_hup_observer) - m_hup_observer(); - } -} - -bool sensor_event_listener::create_event_channel(void) -{ - const int client_type = CLIENT_TYPE_SENSOR_CLIENT; - int client_id; - channel_ready_t event_channel_ready; - - if (!m_event_socket.create(SOCK_SEQPACKET)) - return false; - - if (!m_event_socket.connect(EVENT_CHANNEL_PATH)) { - _E("Failed to connect event channel for client %s, event socket fd[%d]", get_client_name(), m_event_socket.get_socket_fd()); - return false; - } - - if (!m_event_socket.set_connection_mode()) { - _E("Failed to set connection mode for client %s", get_client_name()); - return false; - } - - if (m_event_socket.send(&client_type, sizeof(client_type)) <= 0) { - _E("Failed to send client type in client %s, event socket fd[%d]", get_client_name(), m_event_socket.get_socket_fd()); - return false; - } - - client_id = m_client_info.get_client_id(); - - if (m_event_socket.send(&client_id, sizeof(client_id)) <= 0) { - _E("Failed to send client id for client %s on event socket[%d]", get_client_name(), m_event_socket.get_socket_fd()); - return false; - } - - if (m_event_socket.recv(&event_channel_ready, sizeof(event_channel_ready)) <= 0) { - _E("%s failed to recv event_channel_ready packet on event socket[%d] with client id [%d]", - get_client_name(), m_event_socket.get_socket_fd(), client_id); - return false; - } - - if ((event_channel_ready.magic != CHANNEL_MAGIC_NUM) || (event_channel_ready.client_id != client_id)) { - _E("Event_channel_ready packet is wrong, magic = %#x, client id = %d", - event_channel_ready.magic, event_channel_ready.client_id); - return false; - } - - _I("Event channel is established for client %s on socket[%d] with client id : %d", - get_client_name(), m_event_socket.get_socket_fd(), client_id); - - return true; -} - -void sensor_event_listener::close_event_channel(void) -{ - m_event_socket.close(); -} - -void sensor_event_listener::set_thread_state(thread_state state) -{ - lock l(m_thread_mutex); - m_thread_state = state; -} - -void sensor_event_listener::clear(void) -{ - close_event_channel(); - stop_event_listener(); - m_client_info.close_command_channel(); - m_client_info.clear(); - m_client_info.set_client_id(CLIENT_ID_INVALID); -} - -void sensor_event_listener::set_hup_observer(hup_observer_t observer) -{ - m_hup_observer = observer; -} - -bool sensor_event_listener::start_event_listener(void) -{ - if (!create_event_channel()) { - _E("Event channel is not established for %s", get_client_name()); - return false; - } - - if (!start_deliverer()) - return false; - - m_event_socket.set_transfer_mode(); - - m_poller = new(std::nothrow) poller(m_event_socket.get_socket_fd()); - retvm_if(!m_poller, false, "Failed to allocate memory"); - - set_thread_state(THREAD_STATE_START); - - thread listener(&sensor_event_listener::listen_events, this); - listener.detach(); - - return true; -} - -void sensor_event_listener::stop_event_listener(void) -{ - const int THREAD_TERMINATING_TIMEOUT = 2; - std::cv_status status; - - ulock u(m_thread_mutex); - - /* TOBE: it can be changed to join() simply */ - if (m_thread_state != THREAD_STATE_TERMINATE) { - m_thread_state = THREAD_STATE_STOP; - - _D("%s is waiting listener thread[state: %d] to be terminated", get_client_name(), m_thread_state); - - status = m_thread_cond.wait_for(u, std::chrono::seconds(THREAD_TERMINATING_TIMEOUT)); - if (status == std::cv_status::timeout) - _E("Fail to stop listener thread after waiting %d seconds", THREAD_TERMINATING_TIMEOUT); - else - _D("Listener thread for %s is terminated", get_client_name()); - } - - if (m_poller) { - delete m_poller; - m_poller = NULL; - } - - stop_deliverer(); - close_event_channel(); -} - -bool sensor_event_listener::start_deliverer(void) -{ - if (!m_cb_deliverer) { - m_cb_deliverer = new(std::nothrow) sensor_callback_deliverer(); - retvm_if(!m_cb_deliverer, false, "Failed to allocated memory"); - } - - m_cb_deliverer->start(); - return true; -} - -bool sensor_event_listener::stop_deliverer(void) -{ - if (!m_cb_deliverer) - return false; - - if (!m_cb_deliverer->stop()) - return false; - - delete m_cb_deliverer; - m_cb_deliverer = NULL; - return true; -} - -void sensor_event_listener::set_display_rotation(int rt) -{ - _D("New display rotation: %d", rt); - - if (rt < AUTO_ROTATION_DEGREE_0 || rt > AUTO_ROTATION_DEGREE_270) - return; - - m_display_rotation = rt; -} diff --git a/src/client/sensor_event_listener.h b/src/client/sensor_event_listener.h deleted file mode 100644 index 63dd6d9..0000000 --- a/src/client/sensor_event_listener.h +++ /dev/null @@ -1,120 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2013 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_EVENT_LISTENER_H_ -#define _SENSOR_EVENT_LISTENER_H_ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -typedef std::vector handle_vector; -typedef std::vector sensor_id_vector; -typedef std::unordered_map sensor_handle_info_map; -typedef std::unordered_map sensor_command_channel_map; - -typedef void (*hup_observer_t)(void); - -class sensor_event_listener { -public: - static sensor_event_listener& get_instance(void); - - void get_listening_sensors(sensor_id_vector &sensors); - - bool start_event_listener(void); - void stop_event_listener(void); - void clear(void); - - void set_hup_observer(hup_observer_t observer); - - void set_sensor_axis(int axis); - void set_display_rotation(int rt); - -private: - enum thread_state { - THREAD_STATE_START, - THREAD_STATE_STOP, - THREAD_STATE_TERMINATE, - }; - typedef std::lock_guard lock; - typedef std::unique_lock ulock; - - csocket m_event_socket; - poller *m_poller; - - thread_state m_thread_state; - std::mutex m_thread_mutex; - std::condition_variable m_thread_cond; - - hup_observer_t m_hup_observer; - - sensor_client_info &m_client_info; - - sensor_callback_deliverer *m_cb_deliverer; - - /* WC1's rotation control */ - /* SENSORD_AXIS_DEVICE_ORIENTED, SENSORD_AXIS_DISPLAY_ORIENTED */ - int m_axis; - int m_display_rotation; - - sensor_event_listener(); - ~sensor_event_listener(); - - bool create_event_channel(void); - void close_event_channel(void); - - ssize_t sensor_event_poll(void *buffer, int buffer_len, struct epoll_event &event); - - void listen_events(void); - void handle_events(void* event); - - client_callback_info* handle_calibration_cb(sensor_handle_info &handle_info, unsigned event_type, unsigned long long time, int accuracy); - client_callback_info* get_callback_info(sensor_id_t sensor_id, const reg_event_info *event_info, std::shared_ptr sensor_data); - - unsigned long long renew_event_id(void); - - void set_thread_state(thread_state state); - - /* WC1's sensor axis alignment */ - void align_sensor_axis(sensor_id_t sensor, sensor_data_t *data); - - bool start_deliverer(void); - bool stop_deliverer(void); -}; - -#endif /* _SENSOR_EVENT_LISTENER_H_ */ diff --git a/src/client/sensor_handle_info.cpp b/src/client/sensor_handle_info.cpp deleted file mode 100644 index 5f10930..0000000 --- a/src/client/sensor_handle_info.cpp +++ /dev/null @@ -1,230 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2013 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 -#include -#include - -using std::pair; - -unsigned long long sensor_handle_info::m_event_id = 0; - -attribute_info::attribute_info() -: m_attr(NULL) -, m_len(0) -{ -} - -attribute_info::~attribute_info() -{ - if (m_attr) { - delete m_attr; - m_attr = NULL; - } - m_len = 0; -} - -bool attribute_info::set(const char *value, int len) -{ - retvm_if(len < 0, false, "Invalid length"); - - if (m_attr) - delete m_attr; - - m_attr = new(std::nothrow) char[len]; - retvm_if(!m_attr, false, "Failed to allocate memory"); - - memcpy(m_attr, value, (unsigned int)len); - m_len = len; - - return true; -} - -char *attribute_info::get(void) -{ - return m_attr; -} - -unsigned int attribute_info::size(void) -{ - return m_len; -} - -sensor_handle_info::sensor_handle_info() -: m_handle(0) -, m_sensor_id(UNKNOWN_SENSOR) -, m_sensor_state(SENSOR_STATE_UNKNOWN) -, m_pause_policy(SENSORD_PAUSE_ALL) -, m_bad_accuracy(false) -, m_accuracy(-1) -, m_accuracy_cb(NULL) -, m_accuracy_user_data(NULL) -, m_passive(false) -{ -} - -sensor_handle_info::~sensor_handle_info() -{ - clear_all_events(); -} - -reg_event_info* sensor_handle_info::get_reg_event_info(unsigned int event_type) -{ - auto it_event = m_reg_event_infos.find(event_type); - - if (it_event == m_reg_event_infos.end()) - return NULL; - - return &(it_event->second); -} - -void sensor_handle_info::get_reg_event_types(event_type_vector &event_types) -{ - auto it_event = m_reg_event_infos.begin(); - - while (it_event != m_reg_event_infos.end()) { - event_types.push_back(it_event->first); - ++it_event; - } -} - -bool sensor_handle_info::add_reg_event_info(unsigned int event_type, unsigned int interval, unsigned int latency, void *cb, void *user_data) -{ - reg_event_info event_info; - - auto it_event = m_reg_event_infos.find(event_type); - - if (it_event != m_reg_event_infos.end()) { - _E("Event %s[%#x] is already registered for client %s", get_event_name(event_type), event_type, get_client_name()); - return false; - } - - event_info.m_id = renew_event_id(); - event_info.m_handle = m_handle; - event_info.type = event_type; - event_info.m_interval = interval; - event_info.m_latency = latency; - event_info.m_cb = cb; - event_info.m_user_data = user_data; - - m_reg_event_infos.insert(pair(event_type, event_info)); - - return true; -} - -bool sensor_handle_info::delete_reg_event_info(unsigned int event_type) -{ - auto it_event = m_reg_event_infos.find(event_type); - - if (it_event == m_reg_event_infos.end()) { - _E("Event %s[%#x] is not registered for client %s", get_event_name(event_type), event_type, get_client_name()); - return false; - } - - m_reg_event_infos.erase(it_event); - - return true; -} - -void sensor_handle_info::clear(void) -{ - sensor_attribute_str_map::iterator it_attr; - - for (it_attr = attributes_str.begin(); it_attr != attributes_str.end(); ++it_attr) - delete it_attr->second; - - attributes_int.clear(); - attributes_str.clear(); -} - -void sensor_handle_info::clear_all_events(void) -{ - m_reg_event_infos.clear(); -} - -unsigned long long sensor_handle_info::renew_event_id(void) -{ - return m_event_id++; -} - -bool sensor_handle_info::change_reg_event_batch(unsigned int event_type, unsigned int interval, unsigned int latency) -{ - auto it_event = m_reg_event_infos.find(event_type); - - if (it_event == m_reg_event_infos.end()) { - _E("Event %s[%#x] is not registered for client %s", get_event_name(event_type), event_type, get_client_name()); - return false; - } - - it_event->second.m_id = renew_event_id(); - it_event->second.m_interval = interval; - it_event->second.m_latency = latency; - - return true; -} - -void sensor_handle_info::get_batch(unsigned int &interval, unsigned int &latency) -{ - if (m_reg_event_infos.empty()) { - _D("No events are registered for client %s", get_client_name()); - interval = POLL_10HZ_MS; - latency = 0; - return; - } - - unsigned int min_interval = POLL_MAX_HZ_MS; - unsigned int min_latency = std::numeric_limits::max(); - - unsigned int _interval; - unsigned int _latency; - - auto it_event = m_reg_event_infos.begin(); - - while (it_event != m_reg_event_infos.end()) { - _interval = it_event->second.m_interval; - _latency = it_event->second.m_latency; - - min_interval = (_interval < min_interval) ? _interval : min_interval; - min_latency = (_latency < min_latency) ? _latency : min_latency; - ++it_event; - } - - interval = min_interval; - latency = min_latency; -} - -unsigned int sensor_handle_info::get_reg_event_count(void) -{ - return m_reg_event_infos.size(); -} - -bool sensor_handle_info::get_passive_mode(void) -{ - return m_passive; -} - -void sensor_handle_info::set_passive_mode(bool passive) -{ - m_passive = passive; -} - -bool sensor_handle_info::is_started(void) -{ - return (m_sensor_state == SENSOR_STATE_STARTED) || m_passive; -} diff --git a/src/client/sensor_handle_info.h b/src/client/sensor_handle_info.h deleted file mode 100644 index 6ede304..0000000 --- a/src/client/sensor_handle_info.h +++ /dev/null @@ -1,90 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2013 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_HANDLE_INFO_H_ -#define _SENSOR_HANDLE_INFO_H_ - -#include -#include -#include -#include -#include -#include -#include - -class attribute_info { -public: - attribute_info(); - ~attribute_info(); - - char *get(void); - bool set(const char *value, int len); - - unsigned int size(void); - -private: - char *m_attr; - unsigned int m_len; -}; - -typedef std::unordered_map event_info_map; -typedef std::map sensor_attribute_int_map; -typedef std::map sensor_attribute_str_map; - -class sensor_handle_info { -public: - sensor_handle_info(); - ~sensor_handle_info(); - - bool add_reg_event_info(unsigned int event_type, unsigned int interval, unsigned int latency, void *cb, void *user_data); - bool delete_reg_event_info(unsigned int event_type); - - bool change_reg_event_batch(unsigned int event_type, unsigned int interval, unsigned int latency); - - reg_event_info* get_reg_event_info(const unsigned int event_type); - void get_reg_event_types(event_type_vector &event_types); - void get_batch(unsigned int &interval, unsigned int &latency); - unsigned int get_reg_event_count(void); - - void clear(void); - void clear_all_events(void); - static unsigned long long renew_event_id(void); - - bool get_passive_mode(void); - void set_passive_mode(bool passive); - bool is_started(void); - - int m_handle; - sensor_id_t m_sensor_id; - int m_sensor_state; - int m_pause_policy; - int m_bad_accuracy; - int m_accuracy; - sensor_accuracy_changed_cb_t m_accuracy_cb; - void *m_accuracy_user_data; - bool m_passive; - sensor_attribute_int_map attributes_int; - sensor_attribute_str_map attributes_str; - -private: - event_info_map m_reg_event_infos; - static unsigned long long m_event_id; -}; - -#endif /* _SENSOR_HANDLE_INFO_H_ */ diff --git a/src/client/sensor_info_manager.cpp b/src/client/sensor_info_manager.cpp deleted file mode 100644 index a7a7810..0000000 --- a/src/client/sensor_info_manager.cpp +++ /dev/null @@ -1,100 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2013 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 -#include -#include - -using std::pair; -using std::make_pair; -using std::vector; - -sensor_info_manager::sensor_info_manager() -{ -} - -sensor_info_manager::~sensor_info_manager() -{ - auto it_info = m_sensor_infos.begin(); - - while (it_info != m_sensor_infos.end()) { - delete it_info->second; - ++it_info; - } -} - -sensor_info_manager& sensor_info_manager::get_instance(void) -{ - static sensor_info_manager inst; - return inst; -} - -const sensor_info* sensor_info_manager::get_info(sensor_type_t type) -{ - auto it_info = m_sensor_infos.find(type); - - if (it_info == m_sensor_infos.end()) - return NULL; - - return it_info->second; -} - -void sensor_info_manager::add_info(sensor_info* info) -{ - m_sensor_infos.insert(make_pair(info->get_type(), info)); - m_id_to_info_map.insert(make_pair(info->get_id(), info)); - m_info_set.insert(info); -} - -vector sensor_info_manager::get_infos(sensor_type_t type) -{ - vector sensor_infos; - - pair ret; - - if (type == ALL_SENSOR) - ret = std::make_pair(m_sensor_infos.begin(), m_sensor_infos.end()); - else - ret = m_sensor_infos.equal_range(type); - - for (auto it_info = ret.first; it_info != ret.second; ++it_info) - sensor_infos.push_back(it_info->second); - - return sensor_infos; -} - -const sensor_info* sensor_info_manager::get_info(sensor_id_t id) -{ - auto it_info = m_id_to_info_map.find(id); - - if (it_info == m_id_to_info_map.end()) - return NULL; - - return it_info->second; -} - -bool sensor_info_manager::is_valid(sensor_info* info) -{ - auto it_info = m_info_set.find(info); - - if (it_info == m_info_set.end()) - return false; - - return true; -} diff --git a/src/client/sensor_info_manager.h b/src/client/sensor_info_manager.h deleted file mode 100644 index 86fa8fe..0000000 --- a/src/client/sensor_info_manager.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2013 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_INFO_MANAGER_H_ -#define _SENSOR_INFO_MANAGER_H_ - -#include -#include -#include -#include -#include - -class sensor_info_manager { -public: - static sensor_info_manager& get_instance(void); - const sensor_info* get_info(sensor_type_t type); - std::vector get_infos(sensor_type_t type); - const sensor_info* get_info(sensor_id_t id); - bool is_valid(sensor_info* info); - void add_info(sensor_info* info); - -private: - typedef std::multimap sensor_infos; - typedef std::unordered_map id_to_info_map; - typedef std::unordered_set info_set; - - sensor_info_manager(); - ~sensor_info_manager(); - - sensor_info_manager(const sensor_info_manager&) {}; - sensor_info_manager& operator=(const sensor_info_manager&); - - sensor_infos m_sensor_infos; - id_to_info_map m_id_to_info_map; - info_set m_info_set; -}; - -#endif /* _SENSOR_INFO_MANAGER_H_ */ diff --git a/src/sensor/CMakeLists.txt b/src/sensor/CMakeLists.txt index 272b547..0339c23 100644 --- a/src/sensor/CMakeLists.txt +++ b/src/sensor/CMakeLists.txt @@ -2,15 +2,16 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.6) PROJECT(sensors CXX) INCLUDE(GNUInstallDirs) -SET(HRM "ON") +SET(ACC "OFF") +SET(HRM "OFF") SET(HRM_VIRT "OFF") -SET(AUTO_ROTATION "ON") -SET(GRAVITY "ON") -SET(LINEAR_ACCEL "ON") -SET(RV "ON") -SET(ORIENTATION "ON") -SET(FACE_DOWN "ON") -SET(SENSORHUB "ON") +SET(AUTO_ROTATION "OFF") +SET(GRAVITY "OFF") +SET(LINEAR_ACCEL "OFF") +SET(RV "OFF") +SET(ORIENTATION "OFF") +SET(FACE_DOWN "OFF") +SET(SENSORHUB "OFF") INCLUDE_DIRECTORIES( ${CMAKE_SOURCE_DIR}/src/shared @@ -21,9 +22,11 @@ INCLUDE_DIRECTORIES( FILE(GLOB SENSOR_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) SET(SENSOR_HEADERS ${SENSOR_HEADERS} ${CMAKE_CURRENT_SOURCE_DIR}) +IF("${ACC}" STREQUAL "ON") FILE(GLOB_RECURSE SENSOR_SRCS ${SENSOR_SRCS} ${CMAKE_CURRENT_SOURCE_DIR}/accel/*.cpp) SET(SENSOR_HEADERS ${SENSOR_HEADERS} ${CMAKE_CURRENT_SOURCE_DIR}/accel) SET(SENSOR_DEFINITIONS ${SENSOR_DEFINITIONS} "-DENABLE_ACCEL") +ENDIF() IF("${HRM}" STREQUAL "ON") FILE(GLOB_RECURSE SENSOR_SRCS ${SENSOR_SRCS} ${CMAKE_CURRENT_SOURCE_DIR}/hrm/*.cpp) diff --git a/src/server/client_info_manager.cpp b/src/server/client_info_manager.cpp deleted file mode 100644 index 4a72308..0000000 --- a/src/server/client_info_manager.cpp +++ /dev/null @@ -1,438 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2013 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 -#include -#include -#include - -using std::pair; -using std::string; - -client_info_manager::client_info_manager() -{ -} - -client_info_manager::~client_info_manager() -{ - AUTOLOCK(m_mutex); - - m_clients.clear(); -} - -client_info_manager& client_info_manager::get_instance(void) -{ - static client_info_manager inst; - return inst; -} - -bool client_info_manager::get_registered_events(int client_id, sensor_id_t sensor_id, event_type_vector &event_vec) -{ - AUTOLOCK(m_mutex); - - retvm_if(m_clients.empty(), false, "client list is empty"); - - auto it_record = m_clients.find(client_id); - - if (it_record == m_clients.end()) { - _E("Client[%d] is not found", client_id); - return false; - } - - if (!it_record->second.get_registered_events(sensor_id, event_vec)) - return false; - - return true; -} - -bool client_info_manager::register_event(int client_id, sensor_id_t sensor_id, unsigned int event_type) -{ - AUTOLOCK(m_mutex); - - retvm_if(m_clients.empty(), false, "client list is empty"); - - auto it_record = m_clients.find(client_id); - - if (it_record == m_clients.end()) { - _E("Client[%d] is not found", client_id); - return false; - } - - if (!it_record->second.register_event(sensor_id, event_type)) - return false; - - return true; -} - -bool client_info_manager::unregister_event(int client_id, sensor_id_t sensor_id, unsigned int event_type) -{ - AUTOLOCK(m_mutex); - - retvm_if(m_clients.empty(), false, "client list is empty"); - - auto it_record = m_clients.find(client_id); - - if (it_record == m_clients.end()) { - _E("Client[%d] is not found", client_id); - return false; - } - - if (!it_record->second.unregister_event(sensor_id, event_type)) - return false; - - return true; -} - -bool client_info_manager::set_batch(int client_id, sensor_id_t sensor_id, unsigned int interval, unsigned latency) -{ - AUTOLOCK(m_mutex); - - retvm_if(m_clients.empty(), false, "client list is empty"); - - auto it_record = m_clients.find(client_id); - - if (it_record == m_clients.end()) { - _E("Client[%d] is not found", client_id); - return false; - } - - return it_record->second.set_batch(sensor_id, interval, latency); -} - -bool client_info_manager::get_batch(int client_id, sensor_id_t sensor_id, unsigned int &interval, unsigned int &latency) -{ - AUTOLOCK(m_mutex); - - retvm_if(m_clients.empty(), false, "client list is empty"); - - auto it_record = m_clients.find(client_id); - - if (it_record == m_clients.end()) { - _E("Client[%d] is not found", client_id); - return false; - } - - return it_record->second.get_batch(sensor_id, interval, latency); -} - -bool client_info_manager::set_pause_policy(int client_id, sensor_id_t sensor_id, int pause_policy) -{ - AUTOLOCK(m_mutex); - - retvm_if(m_clients.empty(), false, "client list is empty"); - - auto it_record = m_clients.find(client_id); - - if (it_record == m_clients.end()) { - _E("Client[%d] is not found", client_id); - return false; - } - - if (!it_record->second.set_pause_policy(sensor_id, pause_policy)) - return false; - - return true; -} - -bool client_info_manager::set_start(int client_id, sensor_id_t sensor_id, bool start) -{ - AUTOLOCK(m_mutex); - - retvm_if(m_clients.empty(), false, "client list is empty"); - - auto it_record = m_clients.find(client_id); - - if (it_record == m_clients.end()) { - _E("Client[%d] is not found", client_id); - return false; - } - - if (!it_record->second.set_start(sensor_id, start)) - return false; - - return true; -} - -bool client_info_manager::is_started(int client_id, sensor_id_t sensor_id) -{ - AUTOLOCK(m_mutex); - - retvm_if(m_clients.empty(), false, "client list is empty"); - - auto it_record = m_clients.find(client_id); - - if (it_record == m_clients.end()) { - _E("Client[%d] is not found", client_id); - return false; - } - - return it_record->second.is_started(sensor_id); -} - -int client_info_manager::create_client_record(void) -{ - AUTOLOCK(m_mutex); - - int client_id = 0; - - client_sensor_record client_record; - - while (m_clients.count(client_id) > 0) - client_id++; - - if (client_id == MAX_HANDLE) { - _E("Sensor records of clients are full"); - return MAX_HANDLE_REACHED; - } - - client_record.set_client_id(client_id); - - m_clients.insert(pair(client_id, client_record)); - - return client_id; -} - -bool client_info_manager::remove_client_record(int client_id) -{ - AUTOLOCK(m_mutex); - - retvm_if(m_clients.empty(), false, "client list is empty"); - - auto it_record = m_clients.find(client_id); - - if (it_record == m_clients.end()) { - _E("Client[%d] is not found", client_id); - return false; - } - - m_clients.erase(it_record); - - _I("Client record for client[%d] is removed from client info manager", client_id); - - return true; -} - -bool client_info_manager::has_client_record(int client_id) -{ - AUTOLOCK(m_mutex); - - retvm_if(m_clients.empty(), false, "client list is empty"); - - auto it_record = m_clients.find(client_id); - - return (it_record != m_clients.end()); -} - -void client_info_manager::set_client_info(int client_id, pid_t pid, const string &name) -{ - AUTOLOCK(m_mutex); - - auto it_record = m_clients.find(client_id); - - if (it_record == m_clients.end()) { - _E("Client[%d] is not found", client_id); - return; - } - - it_record->second.set_client_info(pid, name); - - return; -} - -const char* client_info_manager::get_client_info(int client_id) -{ - AUTOLOCK(m_mutex); - - retvm_if(m_clients.empty(), NULL, "client list is empty"); - - auto it_record = m_clients.find(client_id); - - if (it_record == m_clients.end()) { - _D("Client[%d] is not found", client_id); - return NULL; - } - - return it_record->second.get_client_info(); -} - -bool client_info_manager::set_permission(int client_id, int permission) -{ - AUTOLOCK(m_mutex); - - retvm_if(m_clients.empty(), false, "client list is empty"); - - auto it_record = m_clients.find(client_id); - - if (it_record == m_clients.end()) { - _D("Client[%d] is not found", client_id); - return false; - } - - it_record->second.set_permission(permission); - return true; -} - -bool client_info_manager::get_permission(int client_id, int &permission) -{ - AUTOLOCK(m_mutex); - - retvm_if(m_clients.empty(), false, "client list is empty"); - - auto it_record = m_clients.find(client_id); - - if (it_record == m_clients.end()) { - _D("Client[%d] is not found", client_id); - return false; - } - - permission = it_record->second.get_permission(); - return true; -} - -bool client_info_manager::create_sensor_record(int client_id, sensor_id_t sensor_id) -{ - AUTOLOCK(m_mutex); - - retvm_if(m_clients.empty(), false, "client list is empty"); - - auto it_record = m_clients.find(client_id); - - if (it_record == m_clients.end()) { - _E("Client record[%d] is not registered", client_id); - return false; - } - - it_record->second.add_sensor_usage(sensor_id); - - return true; -} - -bool client_info_manager::remove_sensor_record(int client_id, sensor_id_t sensor_id) -{ - AUTOLOCK(m_mutex); - - retvm_if(m_clients.empty(), false, "client list is empty"); - - auto it_record = m_clients.find(client_id); - - if (it_record == m_clients.end()) { - _E("Client[%d] is not found", client_id); - return false; - } - - if (!it_record->second.remove_sensor_usage(sensor_id)) - return false; - - if (!it_record->second.has_sensor_usage()) - remove_client_record(client_id); - - return true; -} - -bool client_info_manager::has_sensor_record(int client_id, sensor_id_t sensor_id) -{ - AUTOLOCK(m_mutex); - - retvm_if(m_clients.empty(), false, "client list is empty"); - - auto it_record = m_clients.find(client_id); - - if (it_record == m_clients.end()) { - _D("Client[%d] is not found", client_id); - return false; - } - - if (!it_record->second.has_sensor_usage(sensor_id)) - return false; - - return true; -} - -bool client_info_manager::has_sensor_record(int client_id) -{ - AUTOLOCK(m_mutex); - - retvm_if(m_clients.empty(), false, "client list is empty"); - - auto it_record = m_clients.find(client_id); - - if (it_record == m_clients.end()) { - _D("Client[%d] is not found", client_id); - return false; - } - - if (!it_record->second.has_sensor_usage()) - return false; - - return true; -} - -bool client_info_manager::get_listener_ids(sensor_id_t sensor_id, unsigned int event_type, client_id_vec &id_vec) -{ - AUTOLOCK(m_mutex); - - retvm_if(m_clients.empty(), false, "client list is empty"); - - auto it_record = m_clients.begin(); - - while (it_record != m_clients.end()) { - if (it_record->second.is_listening_event(sensor_id, event_type)) - id_vec.push_back(it_record->first); - - ++it_record; - } - - return true; -} - -bool client_info_manager::get_event_socket(int client_id, csocket &socket) -{ - AUTOLOCK(m_mutex); - - retvm_if(m_clients.empty(), false, "client list is empty"); - - auto it_record = m_clients.find(client_id); - - if (it_record == m_clients.end()) { - _E("Client[%d] is not found", client_id); - return false; - } - - it_record->second.get_event_socket(socket); - - return true; -} - -bool client_info_manager::set_event_socket(int client_id, const csocket &socket) -{ - AUTOLOCK(m_mutex); - - retvm_if(m_clients.empty(), false, "client list is empty"); - - auto it_record = m_clients.find(client_id); - - if (it_record == m_clients.end()) { - _E("Client[%d] is not found", client_id); - return false; - } - - it_record->second.set_event_socket(socket); - - return true; -} diff --git a/src/server/client_info_manager.h b/src/server/client_info_manager.h deleted file mode 100644 index ad7add8..0000000 --- a/src/server/client_info_manager.h +++ /dev/null @@ -1,75 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2013 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 _CLIENT_INFO_MANAGER_H_ -#define _CLIENT_INFO_MANAGER_H_ - -#include -#include -#include -#include -#include - -typedef std::unordered_map client_id_sensor_record_map; -typedef std::vector client_id_vec; - -class client_info_manager { -public: - static client_info_manager& get_instance(void); - int create_client_record(void); - bool remove_client_record(int client_id); - bool has_client_record(int client_id); - - void set_client_info(int client_id, pid_t pid, const std::string &name); - const char* get_client_info(int client_id); - - bool set_permission(int client_id, int permission); - bool get_permission(int client_id, int &permission); - - bool create_sensor_record(int client_id, sensor_id_t sensor_id); - bool remove_sensor_record(int client_id, sensor_id_t sensor_id); - bool has_sensor_record(int client_id, sensor_id_t sensor_id); - bool has_sensor_record(int client_id); - - bool register_event(int client_id, sensor_id_t sensor_id, unsigned int event_type); - bool unregister_event(int client_id, sensor_id_t sensor_id, unsigned int event_type); - - bool set_batch(int client_id, sensor_id_t sensor_id, unsigned int interval, unsigned int latency); - bool get_batch(int client_id, sensor_id_t sensor_id, unsigned int &interval, unsigned int &latency); - bool set_pause_policy(int client_id, sensor_id_t sensor_id, int pause_policy); - - bool set_start(int client_id, sensor_id_t sensor_id, bool start); - bool is_started(int client_id, sensor_id_t sensor_id); - - bool get_registered_events(int client_id, sensor_id_t sensor_id, event_type_vector &event_vec); - - bool get_listener_ids(sensor_id_t sensor_id, unsigned int event_type, client_id_vec &id_vec); - bool get_event_socket(int client_id, csocket &sock); - bool set_event_socket(int client_id, const csocket &sock); -private: - client_id_sensor_record_map m_clients; - cmutex m_mutex; - - client_info_manager(); - ~client_info_manager(); - client_info_manager(client_info_manager const&) {}; - client_info_manager& operator=(client_info_manager const&); -}; - -#endif /* _CLIENT_INFO_MANAGER_H_ */ diff --git a/src/server/client_sensor_record.cpp b/src/server/client_sensor_record.cpp deleted file mode 100644 index 8eeafca..0000000 --- a/src/server/client_sensor_record.cpp +++ /dev/null @@ -1,262 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2013 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 -#include - -using std::pair; -using std::string; - -client_sensor_record::client_sensor_record() -: m_client_id(0) -, m_pid(-1) -, m_permission(SENSOR_PERMISSION_NONE) -{ -} - -client_sensor_record::~client_sensor_record() -{ - m_sensor_usages.clear(); - close_event_socket(); -} - -bool client_sensor_record::register_event(sensor_id_t sensor_id, unsigned int event_type) -{ - auto it_usage = m_sensor_usages.find(sensor_id); - - if (it_usage == m_sensor_usages.end()) { - sensor_usage usage; - usage.register_event(event_type); - m_sensor_usages.insert(pair(sensor_id, usage)); - return true; - } - - if (!it_usage->second.register_event(event_type)) { - _E("Event[%#x] is already registered", event_type); - return false; - } - - return true; -} - -bool client_sensor_record::unregister_event(sensor_id_t sensor_id, unsigned int event_type) -{ - auto it_usage = m_sensor_usages.find(sensor_id); - - if (it_usage == m_sensor_usages.end()) { - _E("Sensor[%#x] is not registered", sensor_id); - return false; - } - - if (!it_usage->second.unregister_event(event_type)) { - _E("Event[%#x] is already registered", event_type); - return false; - } - - return true; -} - -bool client_sensor_record::set_pause_policy(sensor_id_t sensor_id, int pause_policy) -{ - auto it_usage = m_sensor_usages.find(sensor_id); - - if (it_usage == m_sensor_usages.end()) { - sensor_usage usage; - usage.m_pause_policy = pause_policy; - m_sensor_usages.insert(pair(sensor_id, usage)); - } else { - it_usage->second.m_pause_policy = pause_policy; - } - - return true; -} - -bool client_sensor_record::set_start(sensor_id_t sensor_id, bool start) -{ - auto it_usage = m_sensor_usages.find(sensor_id); - - if (it_usage == m_sensor_usages.end()) { - sensor_usage usage; - usage.m_start = start; - m_sensor_usages.insert(pair(sensor_id, usage)); - } else { - it_usage->second.m_start = start; - } - - return true; -} - -bool client_sensor_record::is_started(sensor_id_t sensor_id) -{ - auto it_usage = m_sensor_usages.find(sensor_id); - - if (it_usage == m_sensor_usages.end()) - return false; - - return it_usage->second.m_start; -} - -bool client_sensor_record::set_batch(sensor_id_t sensor_id, unsigned int interval, unsigned int latency) -{ - auto it_usage = m_sensor_usages.find(sensor_id); - - if (it_usage == m_sensor_usages.end()) { - sensor_usage usage; - usage.m_interval = interval; - usage.m_latency = latency; - m_sensor_usages.insert(pair(sensor_id, usage)); - } else { - it_usage->second.m_interval = interval; - it_usage->second.m_latency = latency; - } - - return true; -} - -bool client_sensor_record::get_batch(sensor_id_t sensor_id, unsigned int &interval, unsigned int &latency) -{ - auto it_usage = m_sensor_usages.find(sensor_id); - - if (it_usage == m_sensor_usages.end()) { - _E("Sensor[%#x] is not found", sensor_id); - return false; - } - - interval = it_usage->second.m_interval; - latency = it_usage->second.m_latency; - - return true; -} - -bool client_sensor_record::is_listening_event(sensor_id_t sensor_id, unsigned int event_type) -{ - auto it_usage = m_sensor_usages.find(sensor_id); - - if (it_usage == m_sensor_usages.end()) - return false; - - if (it_usage->second.is_event_registered(event_type)) - return true; - - return false; -} - -bool client_sensor_record::add_sensor_usage(sensor_id_t sensor_id) -{ - auto it_usage = m_sensor_usages.find(sensor_id); - - if (it_usage != m_sensor_usages.end()) { - _E("Sensor[%#x] is already registered", sensor_id); - return false; - } - - sensor_usage usage; - m_sensor_usages.insert(pair(sensor_id, usage)); - return true; -} - -bool client_sensor_record::remove_sensor_usage(sensor_id_t sensor_id) -{ - auto it_usage = m_sensor_usages.find(sensor_id); - - if (it_usage == m_sensor_usages.end()) { - _E("Sensor[%#x] is not found", sensor_id); - return false; - } - m_sensor_usages.erase(it_usage); - return true; -} - -bool client_sensor_record::has_sensor_usage(void) -{ - if (m_sensor_usages.empty()) - return false; - - return true; -} - -bool client_sensor_record::has_sensor_usage(sensor_id_t sensor_id) -{ - auto it_usage = m_sensor_usages.find(sensor_id); - - if (it_usage == m_sensor_usages.end()) { - _D("Sensor[%#x] is not found", sensor_id); - return false; - } - - return true; -} - -bool client_sensor_record::get_registered_events(sensor_id_t sensor_id, event_type_vector &event_vec) -{ - auto it_usage = m_sensor_usages.find(sensor_id); - - if (it_usage == m_sensor_usages.end()) { - _D("Sensor[%#x] is not found", sensor_id); - return false; - } - - copy(it_usage->second.m_reg_events.begin(), it_usage->second.m_reg_events.end(), back_inserter(event_vec)); - - return true; -} - -void client_sensor_record::set_client_id(int client_id) -{ - m_client_id = client_id; -} - -void client_sensor_record::set_client_info(pid_t pid, const string &name) -{ - char client_info[NAME_MAX + 32]; - m_pid = pid; - - snprintf(client_info, sizeof(client_info), "%s[pid=%d, id=%d]", name.c_str(), m_pid, m_client_id); - m_client_info.assign(client_info); -} - -const char* client_sensor_record::get_client_info(void) -{ - return m_client_info.c_str(); -} - -void client_sensor_record::set_permission(int permission) -{ - m_permission = permission; -} - -int client_sensor_record::get_permission(void) -{ - return m_permission; -} - -void client_sensor_record::set_event_socket(const csocket &socket) -{ - m_event_socket = socket; -} - -void client_sensor_record::get_event_socket(csocket &socket) -{ - socket = m_event_socket; -} - -bool client_sensor_record::close_event_socket(void) -{ - return m_event_socket.close(); -} diff --git a/src/server/client_sensor_record.h b/src/server/client_sensor_record.h deleted file mode 100644 index ad4835c..0000000 --- a/src/server/client_sensor_record.h +++ /dev/null @@ -1,77 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2013 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 _CLIENT_SENSOR_RECORD_H_ -#define _CLIENT_SENSOR_RECORD_H_ - -#include -#include -#include -#include -#include -#include - -typedef std::unordered_map sensor_usage_map; - -class client_sensor_record { -public: - client_sensor_record(); - ~client_sensor_record(); - - void set_client_id(int client_id); - - void set_client_info(pid_t pid, const std::string &name); - const char* get_client_info(void); - - void set_permission(int permission); - int get_permission(void); - - bool register_event(sensor_id_t sensor_id, unsigned int event_type); - bool unregister_event(sensor_id_t sensor_id, unsigned int event_type); - - bool set_batch(sensor_id_t sensor_id, unsigned int interval, unsigned int latency); - bool get_batch(sensor_id_t sensor_id, unsigned int &interval, unsigned int &latency); - bool set_pause_policy(sensor_id_t sensor_id, int pause_policy); - - bool set_start(sensor_id_t sensor_id, bool start); - bool is_started(sensor_id_t sensor_id); - - bool is_listening_event(sensor_id_t sensor_id, unsigned int event_type); - bool has_sensor_usage(void); - bool has_sensor_usage(sensor_id_t sensor_id); - - bool get_registered_events(sensor_id_t sensor_id, event_type_vector &event_vec); - - bool add_sensor_usage(sensor_id_t sensor_id); - bool remove_sensor_usage(sensor_id_t sensor_id); - - void set_event_socket(const csocket &socket); - void get_event_socket(csocket &socket); - bool close_event_socket(void); - -private: - int m_client_id; - pid_t m_pid; - int m_permission; - std::string m_client_info; - csocket m_event_socket; - sensor_usage_map m_sensor_usages; -}; - -#endif /* _CLIENT_SENSOR_RECORD_H_ */ diff --git a/src/server/command_queue.cpp b/src/server/command_queue.cpp deleted file mode 100644 index 0eadaab..0000000 --- a/src/server/command_queue.cpp +++ /dev/null @@ -1,55 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2015 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 -#include - -command_queue& command_queue::get_instance(void) -{ - static command_queue inst; - return inst; -} - -void command_queue::push(std::shared_ptr &command) -{ - lock l(m_mutex); - - bool wake = m_queue.empty(); - - if (m_queue.size() >= QUEUE_FULL_SIZE) { - _E("Queue is full, drop it!"); - } else { - m_queue.push(command); - } - - if (wake) - m_cond_var.notify_one(); -} - -std::shared_ptr command_queue::pop(void) -{ - ulock u(m_mutex); - - while (m_queue.empty()) - m_cond_var.wait(u); - - std::shared_ptr command = m_queue.front(); - m_queue.pop(); - return command; -} diff --git a/src/server/command_queue.h b/src/server/command_queue.h deleted file mode 100644 index d5ab195..0000000 --- a/src/server/command_queue.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2015 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 _COMMAND_QUEUE_H_ -#define _COMMAND_QUEUE_H_ - -#include -#include -#include -#include -#include - -class command_queue { -private: - command_queue() {}; - ~command_queue() {}; - command_queue(const command_queue&) {}; - command_queue& operator=(const command_queue&); - - static const unsigned int QUEUE_FULL_SIZE = 1000; - - std::queue> m_queue; - std::mutex m_mutex; - std::condition_variable m_cond_var; - - typedef std::lock_guard lock; - typedef std::unique_lock ulock; -public: - static command_queue& get_instance(); - void push(std::shared_ptr &command); - std::shared_ptr pop(void); -}; - -#endif diff --git a/src/server/command_worker.cpp b/src/server/command_worker.cpp deleted file mode 100644 index 2436ab8..0000000 --- a/src/server/command_worker.cpp +++ /dev/null @@ -1,872 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2014 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 -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define WAIT_TIME(X) ((1 << ((X) < 4 ? (X) : 4)) * 10000) /* 20, 40, 80, 160, 160ms, ... */ - -using std::string; -using std::vector; -using std::make_pair; - -command_worker::cmd_handler_t command_worker::m_cmd_handlers[]; -sensor_raw_data_map command_worker::m_sensor_raw_data_map; -cpacket command_worker::m_sensor_list; -cmutex command_worker::m_shared_mutex; - -command_worker::command_worker(const csocket& socket) -: m_client_id(CLIENT_ID_INVALID) -, m_permission(SENSOR_PERMISSION_NONE) -, m_socket(socket) -, m_module(NULL) -, m_sensor_id(UNKNOWN_SENSOR) -{ - static bool init = false; - - AUTOLOCK(m_shared_mutex); - - if (!init) { - init_cmd_handlers(); - - init = true; - } - - m_worker.set_context(this); - m_worker.set_working(working); - m_worker.set_stopped(stopped); -} - -command_worker::~command_worker() -{ - m_socket.close(); -} - -bool command_worker::start(void) -{ - return m_worker.start(); -} - -void command_worker::init_cmd_handlers(void) -{ - m_cmd_handlers[CMD_GET_ID] = &command_worker::cmd_get_id; - m_cmd_handlers[CMD_GET_SENSOR_LIST] = &command_worker::cmd_get_sensor_list; - m_cmd_handlers[CMD_HELLO] = &command_worker::cmd_hello; - m_cmd_handlers[CMD_BYEBYE] = &command_worker::cmd_byebye; - m_cmd_handlers[CMD_START] = &command_worker::cmd_start; - m_cmd_handlers[CMD_STOP] = &command_worker::cmd_stop; - m_cmd_handlers[CMD_REG] = &command_worker::cmd_register_event; - m_cmd_handlers[CMD_UNREG] = &command_worker::cmd_unregister_event; - m_cmd_handlers[CMD_SET_PAUSE_POLICY] = &command_worker::cmd_set_pause_policy; - m_cmd_handlers[CMD_SET_BATCH] = &command_worker::cmd_set_batch; - m_cmd_handlers[CMD_UNSET_BATCH] = &command_worker::cmd_unset_batch; - m_cmd_handlers[CMD_GET_DATA] = &command_worker::cmd_get_data; - m_cmd_handlers[CMD_SET_ATTRIBUTE_INT] = &command_worker::cmd_set_attribute_int; - m_cmd_handlers[CMD_SET_ATTRIBUTE_STR] = &command_worker::cmd_set_attribute_str; - m_cmd_handlers[CMD_FLUSH] = &command_worker::cmd_flush; -} - -size_t command_worker::create_sensor_raw_list(int client_perms, std::vector &raw_list) -{ - size_t total_raw_data_size = 0; - vector sensors; - vector types; - sensor_info info; - int permission; - - types = sensor_loader::get_instance().get_sensor_types(); - - for (auto it_type = types.begin(); it_type != types.end(); ++it_type) { - sensor_type_t type = *it_type; - sensors = sensor_loader::get_instance().get_sensors(type); - - for (auto it_sensor = sensors.begin(); it_sensor != sensors.end(); ++it_sensor) { - (*it_sensor)->get_sensor_info(info); - permission = (*it_sensor)->get_permission(); - permission = (client_perms & permission); - - if (!permission) { - info.clear(); - info.set_id((sensor_id_t)-EACCES); - info.set_type(type); - } - - raw_data_t *raw_data = new(std::nothrow) raw_data_t(); - retvm_if(!raw_data, -1, "Failed to allocated memory"); - info.get_raw_data(*raw_data); - - total_raw_data_size += raw_data->size(); - raw_list.push_back(raw_data); - - info.clear(); - } - } - - return total_raw_data_size; -} - -void command_worker::get_sensor_list(int client_perms, cpacket &sensor_list) -{ - size_t total_raw_data_size = 0; - vector raw_list; - int sensor_cnt; - int idx = 0; - - total_raw_data_size = create_sensor_raw_list(client_perms, raw_list); - - sensor_cnt = raw_list.size(); - - sensor_list.set_payload_size(sizeof(cmd_get_sensor_list_done_t) + (sizeof(size_t) * sensor_cnt) + total_raw_data_size); - sensor_list.set_cmd(CMD_GET_SENSOR_LIST); - - cmd_get_sensor_list_done_t *cmd_get_sensor_list_done; - - cmd_get_sensor_list_done = (cmd_get_sensor_list_done_t*)sensor_list.data(); - cmd_get_sensor_list_done->sensor_cnt = sensor_cnt; - size_t* size_field = (size_t *) cmd_get_sensor_list_done->data; - - for (int i = 0; i < sensor_cnt; ++i) - size_field[i] = raw_list[i]->size(); - - char* raw_data_field = cmd_get_sensor_list_done->data + (sizeof(size_t) * sensor_cnt); - - for (int i = 0; i < sensor_cnt; ++i) { - copy(raw_list[i]->begin(), raw_list[i]->end(), raw_data_field + idx); - idx += raw_list[i]->size(); - } - - for (auto it = raw_list.begin(); it != raw_list.end(); ++it) - delete *it; -} - -bool command_worker::working(void *ctx) -{ - bool ret; - command_worker *inst = (command_worker *)ctx; - - packet_header header; - char *payload; - - if (inst->m_socket.recv(&header, sizeof(header)) <= 0) { - string info; - inst->get_info(info); - _D("%s failed to receive header", info.c_str()); - return false; - } - - if (header.size > 0) { - payload = new(std::nothrow) char[header.size]; - retvm_if(!payload, false, "Failed to allocate memory"); - - if (inst->m_socket.recv(payload, header.size) <= 0) { - string info; - inst->get_info(info); - _D("%s failed to receive data of packet", info.c_str()); - delete[] payload; - return false; - } - } else { - payload = NULL; - } - - ret = inst->dispatch_command(header.cmd, payload); - - if (payload) - delete[] payload; - - return ret; -} - -bool command_worker::stopped(void *ctx) -{ - string info; - command_worker *inst = (command_worker *)ctx; - - inst->get_info(info); - _I("%s is stopped", info.c_str()); - - if ((inst->m_module) && (inst->m_client_id != CLIENT_ID_INVALID)) { - if (get_client_info_manager().is_started(inst->m_client_id, inst->m_sensor_id)) { - _W("Does not receive cmd_stop before connection broken for [%s]!!", inst->m_module->get_name()); - inst->m_module->delete_interval(inst->m_client_id, false); - inst->m_module->delete_batch(inst->m_client_id); - inst->m_module->delete_attribute(inst->m_client_id); - inst->m_module->stop(); - } - - if (inst->m_sensor_id > UNKNOWN_SENSOR) { - if (get_client_info_manager().has_sensor_record(inst->m_client_id, inst->m_sensor_id)) { - _I("Removing sensor[%#llx] record for client_id[%d]", inst->m_sensor_id, inst->m_client_id); - get_client_info_manager().remove_sensor_record(inst->m_client_id, inst->m_sensor_id); - } - } - } - - delete inst; - return true; -} - -bool command_worker::dispatch_command(int cmd, void* payload) -{ - int ret = false; - - if (!(cmd > 0 && cmd < CMD_CNT)) { - _E("Unknown command: %d", cmd); - } else { - cmd_handler_t cmd_handler; - cmd_handler = command_worker::m_cmd_handlers[cmd]; - if (cmd_handler) - ret = (this->*cmd_handler)(payload); - } - - return ret; -} - -bool command_worker::send_cmd_done(long value) -{ - cpacket* ret_packet; - cmd_done_t *cmd_done; - - ret_packet = new(std::nothrow) cpacket(sizeof(cmd_done_t)); - retvm_if(!ret_packet, false, "Failed to allocate memory"); - - ret_packet->set_cmd(CMD_DONE); - - cmd_done = (cmd_done_t*)ret_packet->data(); - cmd_done->value = value; - - if (m_socket.send(ret_packet->packet(), ret_packet->size()) <= 0) { - _E("Failed to send a cmd_done to client_id [%d] with value [%ld]", m_client_id, value); - delete ret_packet; - return false; - } - - delete ret_packet; - return true; -} - -bool command_worker::send_cmd_get_id_done(int client_id) -{ - cpacket* ret_packet; - cmd_get_id_done_t *cmd_get_id_done; - - ret_packet = new(std::nothrow) cpacket(sizeof(cmd_get_id_done_t)); - retvm_if(!ret_packet, false, "Failed to allocate memory"); - - ret_packet->set_cmd(CMD_GET_ID); - - cmd_get_id_done = (cmd_get_id_done_t*)ret_packet->data(); - cmd_get_id_done->client_id = client_id; - - if (m_socket.send(ret_packet->packet(), ret_packet->size()) <= 0) { - _E("Failed to send a cmd_get_id_done with client_id [%d]", client_id); - delete ret_packet; - return false; - } - - delete ret_packet; - return true; -} - -bool command_worker::send_cmd_get_data_done(int state, sensor_data_t *data) -{ - cpacket* ret_packet; - cmd_get_data_done_t *cmd_get_data_done; - - ret_packet = new(std::nothrow) cpacket(sizeof(cmd_get_data_done_t)); - retvm_if(!ret_packet, false, "Failed to allocate memory"); - - ret_packet->set_cmd(CMD_GET_DATA); - - cmd_get_data_done = (cmd_get_data_done_t*)ret_packet->data(); - cmd_get_data_done->state = state; - - if (data) - memcpy(&cmd_get_data_done->base_data, data, sizeof(sensor_data_t)); - - if (m_socket.send(ret_packet->packet(), ret_packet->size()) <= 0) { - _E("Failed to send a cmd_get_data_done"); - free(data); - delete ret_packet; - return false; - } - - free(data); - delete ret_packet; - return true; -} - -bool command_worker::send_cmd_get_sensor_list_done(void) -{ - cpacket sensor_list; - - int permission = get_permission(); - - _I("permission = %#x", permission); - - get_sensor_list(permission, sensor_list); - - if (m_socket.send(sensor_list.packet(), sensor_list.size()) <= 0) { - _E("Failed to send a cmd_get_sensor_list_done"); - return false; - } - - /* - * TODO: get_sensor_list() command is processed on one-time connection in the current architecture. - * but it doesn't need to use one-time connection. - */ - m_socket.close(); - - return true; -} - -bool command_worker::cmd_get_id(void *payload) -{ - cmd_get_id_t *cmd; - int client_id; - struct ucred cr; - socklen_t opt_len = sizeof(cr); - - _D("CMD_GET_ID Handler invoked\n"); - cmd = (cmd_get_id_t*)payload; - - if (getsockopt(m_socket.get_socket_fd(), SOL_SOCKET, SO_PEERCRED, &cr, &opt_len)) { - _E("Failed to get socket option with SO_PEERCRED"); - return false; - } - - client_id = get_client_info_manager().create_client_record(); - - get_client_info_manager().set_client_info(client_id, cr.pid, cmd->name); - - m_permission = get_permission(); - - get_client_info_manager().set_permission(client_id, m_permission); - - _I("New client id [%d] created", client_id); - - if (!send_cmd_get_id_done(client_id)) - _E("Failed to send cmd_done to a client"); - - return true; -} - -bool command_worker::cmd_get_sensor_list(void *payload) -{ - _D("CMD_GET_SENSOR_LIST Handler invoked\n"); - - if (!send_cmd_get_sensor_list_done()) - _E("Failed to send cmd_get_sensor_list_done to a client"); - - return true; -} - -bool command_worker::cmd_hello(void *payload) -{ - cmd_hello_t *cmd; - long ret_value = OP_ERROR; - - _D("CMD_HELLO Handler invoked\n"); - cmd = (cmd_hello_t*)payload; - - m_sensor_id = cmd->sensor; - m_client_id = cmd->client_id; - - if (m_permission == SENSOR_PERMISSION_NONE) - get_client_info_manager().get_permission(m_client_id, m_permission); - - m_module = (sensor_base *)sensor_loader::get_instance().get_sensor(cmd->sensor); - - if (!m_module) { - _W("Sensor type[%d] is not supported", cmd->sensor); - if (!get_client_info_manager().has_sensor_record(m_client_id)) - get_client_info_manager().remove_client_record(m_client_id); - - ret_value = OP_ERROR; - goto out; - } - - if (!is_permission_allowed()) { - _E("Permission denied to connect sensor[%#llx] for client [%d]", m_sensor_id, m_client_id); - ret_value = OP_ERROR; - goto out; - } - - _D("Hello sensor [%#llx], client id [%d]", m_sensor_id, m_client_id); - get_client_info_manager().create_sensor_record(m_client_id, m_sensor_id); - _I("New sensor record created for sensor [%#llx], sensor name [%s] on client id [%d]\n", m_sensor_id, m_module->get_name(), m_client_id); - ret_value = OP_SUCCESS; -out: - if (!send_cmd_done(ret_value)) - _E("Failed to send cmd_done to a client"); - - return true; -} - -bool command_worker::cmd_byebye(void *payload) -{ - long ret_value = OP_ERROR; - - if (!is_permission_allowed()) { - _E("Permission denied to stop sensor[%#llx] for client [%d]", m_sensor_id, m_client_id); - ret_value = OP_ERROR; - goto out; - } - - _D("CMD_BYEBYE for client [%d], sensor [%#llx]", m_client_id, m_sensor_id); - - if (!get_client_info_manager().remove_sensor_record(m_client_id, m_sensor_id)) { - _E("Error removing sensor_record for client [%d]", m_client_id); - ret_value = OP_ERROR; - goto out; - } - - m_client_id = CLIENT_ID_INVALID; - ret_value = OP_SUCCESS; - -out: - if (!send_cmd_done(ret_value)) - _E("Failed to send cmd_done to a client"); - - if (ret_value == OP_SUCCESS) - return false; - - return true; -} - -bool command_worker::cmd_start(void *payload) -{ - long ret_value = OP_ERROR; - - if (!is_permission_allowed()) { - _E("Permission denied to start sensor[%#llx] for client [%d]", m_sensor_id, m_client_id); - ret_value = OP_ERROR; - goto out; - } - - _D("START Sensor [%#llx], called from client [%d]", m_sensor_id, m_client_id); - - if (m_module->start()) { - get_client_info_manager().set_start(m_client_id, m_sensor_id, true); -/* - * Rotation could be changed even LCD is off by pop sync rotation - * and a client listening rotation event with always-on option. - * To reflect the last rotation state, request it to event dispatcher. - */ - get_event_dispathcher().request_last_event(m_client_id, m_sensor_id); - ret_value = OP_SUCCESS; - } else { - _E("Failed to start sensor [%#llx] for client [%d]", m_sensor_id, m_client_id); - ret_value = OP_ERROR; - } - -out: - if (!send_cmd_done(ret_value)) - _E("Failed to send cmd_done to a client"); - - return true; -} - -bool command_worker::cmd_stop(void *payload) -{ - long ret_value = OP_ERROR; - - if (!is_permission_allowed()) { - _E("Permission denied to stop sensor[%#llx] for client [%d]", m_sensor_id, m_client_id); - ret_value = OP_ERROR; - goto out; - } - - _D("STOP Sensor [%#llx], called from client [%d]", m_sensor_id, m_client_id); - - if (m_module->stop()) { - get_client_info_manager().set_start(m_client_id, m_sensor_id, false); - m_module->delete_attribute(m_client_id); - ret_value = OP_SUCCESS; - } else { - _E("Failed to stop sensor [%#llx] for client [%d]", m_sensor_id, m_client_id); - ret_value = OP_ERROR; - } - -out: - if (!send_cmd_done(ret_value)) - _E("Failed to send cmd_done to a client"); - - return true; -} - -bool command_worker::cmd_register_event(void *payload) -{ - cmd_reg_t *cmd; - long ret_value = OP_ERROR; - - cmd = (cmd_reg_t*)payload; - - if (!is_permission_allowed()) { - _E("Permission denied to register event [%#x] for client [%d] to client info manager", - cmd->event_type, m_client_id); - ret_value = OP_ERROR; - goto out; - } - - if (!get_client_info_manager().register_event(m_client_id, m_sensor_id, cmd->event_type)) { - _I("Failed to register event [%#x] for client [%d] to client info manager", - cmd->event_type, m_client_id); - ret_value = OP_ERROR; - goto out; - } - - ret_value = OP_SUCCESS; - _D("Registering Event [%#x] is done for client [%d]", cmd->event_type, m_client_id); - -out: - if (!send_cmd_done(ret_value)) - _E("Failed to send cmd_done to a client"); - - return true; -} - -bool command_worker::cmd_unregister_event(void *payload) -{ - cmd_unreg_t *cmd; - long ret_value = OP_ERROR; - - cmd = (cmd_unreg_t*)payload; - - if (!is_permission_allowed()) { - _E("Permission denied to unregister event [%#x] for client [%d] to client info manager", - cmd->event_type, m_client_id); - ret_value = OP_ERROR; - goto out; - } - - if (!get_client_info_manager().unregister_event(m_client_id, m_sensor_id, cmd->event_type)) { - _E("Failed to unregister event [%#x] for client [%d] from client info manager", - cmd->event_type, m_client_id); - ret_value = OP_ERROR; - goto out; - } - - ret_value = OP_SUCCESS; - _D("Unregistering Event [%#x] is done for client [%d]", - cmd->event_type, m_client_id); - -out: - if (!send_cmd_done(ret_value)) - _E("Failed to send cmd_done to a client"); - - return true; -} - -bool command_worker::cmd_set_batch(void *payload) -{ - cmd_set_batch_t *cmd; - long ret_value = OP_ERROR; - - cmd = (cmd_set_batch_t*)payload; - - if (!is_permission_allowed()) { - _E("Permission denied to set batch for client [%d], for sensor [%#llx] with batch [%d, %d] to client info manager", - m_client_id, m_sensor_id, cmd->interval, cmd->latency); - ret_value = OP_ERROR; - goto out; - } - - if (!get_client_info_manager().set_batch(m_client_id, m_sensor_id, cmd->interval, cmd->latency)) { - _E("Failed to set batch for client [%d], for sensor [%#llx] with batch [%d, %d] to client info manager", - m_client_id, m_sensor_id, cmd->interval, cmd->latency); - ret_value = OP_ERROR; - goto out; - } - - if (!m_module->add_interval(m_client_id, cmd->interval, false)) { - _E("Failed to set interval for client [%d], for sensor [%#llx] with interval [%d]", - m_client_id, m_sensor_id, cmd->interval); - ret_value = OP_ERROR; - goto out; - } - - if (!m_module->add_batch(m_client_id, cmd->latency)) { - _E("Failed to set latency for client [%d], for sensor [%#llx] with latency [%d]", - m_client_id, m_sensor_id, cmd->latency); - ret_value = OP_ERROR; - goto out; - } - - ret_value = OP_SUCCESS; - -out: - if (!send_cmd_done(ret_value)) - _E("Failed to send cmd_done to a client"); - - return true; -} - -bool command_worker::cmd_unset_batch(void *payload) -{ - long ret_value = OP_ERROR; - - if (!is_permission_allowed()) { - _E("Permission denied to unset batch for client [%d], for sensor [%#llx] to client info manager", - m_client_id, m_sensor_id); - ret_value = OP_ERROR; - goto out; - } - - if (!get_client_info_manager().set_batch(m_client_id, m_sensor_id, 0, 0)) { - _E("Failed to unset batch for client [%d], for sensor [%#llx] to client info manager", - m_client_id, m_sensor_id); - ret_value = OP_ERROR; - goto out; - } - - if (!m_module->delete_interval(m_client_id, false)) { - _E("Failed to delete interval for client [%d]", m_client_id); - ret_value = OP_ERROR; - goto out; - } - - if (!m_module->delete_batch(m_client_id)) { - _E("Failed to delete latency for client [%d]", m_client_id); - ret_value = OP_ERROR; - goto out; - } - - ret_value = OP_SUCCESS; - -out: - if (!send_cmd_done(ret_value)) - _E("Failed to send cmd_done to a client"); - - return true; -} - -bool command_worker::cmd_set_pause_policy(void *payload) -{ - cmd_set_pause_policy_t *cmd; - long ret_value = OP_ERROR; - - cmd = (cmd_set_pause_policy_t*)payload; - - if (!is_permission_allowed()) { - _E("Permission denied to set interval for client [%d], for sensor [%#llx] with pause_policy [%d] to client info manager", - m_client_id, m_sensor_id, cmd->pause_policy); - ret_value = OP_ERROR; - goto out; - } - - if (!get_client_info_manager().set_pause_policy(m_client_id, m_sensor_id, cmd->pause_policy)) { - _E("Failed to set pause_policy for client [%d], for sensor [%#llx] with pause_policy [%d] to client info manager", - m_client_id, m_sensor_id, cmd->pause_policy); - ret_value = OP_ERROR; - goto out; - } - - ret_value = OP_SUCCESS; -out: - if (!send_cmd_done(ret_value)) - _E("Failed to send cmd_done to a client"); - - return true; -} - -bool command_worker::cmd_get_data(void *payload) -{ - const unsigned int GET_DATA_MIN_INTERVAL = 10; - int state; - bool adjusted = false; - - sensor_data_t *data = NULL; - - _D("CMD_GET_VALUE Handler invoked\n"); - - if (!is_permission_allowed()) { - _E("Permission denied to get data for client [%d], for sensor [%#llx]", - m_client_id, m_sensor_id); - state = -EACCES; - goto out; - } - - state = m_module->get_cache(&data); - - /* if there is no cached data, wait short time and retry to get data again */ - if (state == -ENODATA) { - const int RETRY_CNT = 10; - int retry = 0; - - unsigned int interval = m_module->get_interval(m_client_id, false); - - /* 1. change interval to 10ms. */ - if (interval > GET_DATA_MIN_INTERVAL) { - m_module->add_interval(m_client_id, GET_DATA_MIN_INTERVAL, false); - adjusted = true; - } - - /* 2. try to get sensor data increasing the waited time(20ms, 40ms, 80ms, 160ms, 160ms...) */ - /* 3. if data cannot be found in 10 times, stop it. */ - while ((state == -ENODATA) && (retry++ < RETRY_CNT)) { - _I("Wait sensor[%#llx] data updated for client [%d] #%d", m_sensor_id, m_client_id, retry); - usleep(WAIT_TIME(retry)); - state = m_module->get_cache(&data); - } - - /* 4. revert to original interval */ - if (adjusted) - m_module->add_interval(m_client_id, interval, false); - } - - if (state < 0) { - _E("Failed to get data for client [%d], for sensor [%#llx]", - m_client_id, m_sensor_id); - } - -out: - send_cmd_get_data_done(state < 0 ? OP_ERROR : OP_SUCCESS, data); - - return true; -} - -bool command_worker::cmd_set_attribute_int(void *payload) -{ - cmd_set_attribute_int_t *cmd; - long ret_value = OP_ERROR; - - _D("CMD_SET_COMMAND Handler invoked\n"); - - cmd = (cmd_set_attribute_int_t*)payload; - - if (!is_permission_allowed()) { - _E("Permission denied to set attribute for client [%d], for sensor [%#llx] with attribute [%d]", - m_client_id, m_sensor_id, cmd->attribute); - ret_value = OP_ERROR; - goto out; - } - - ret_value = m_module->add_attribute(m_client_id, cmd->attribute, cmd->value); - -out: - if (!send_cmd_done(ret_value)) - _E("Failed to send cmd_done to a client"); - - return true; -} - -bool command_worker::cmd_set_attribute_str(void *payload) -{ - cmd_set_attribute_str_t *cmd; - long ret_value = OP_ERROR; - - _D("CMD_SEND_SENSORHUB_DATA Handler invoked"); - - cmd = (cmd_set_attribute_str_t*)payload; - - if (!is_permission_allowed()) { - _E("Permission denied to set attribute for client [%d], for sensor [%#llx]", - m_client_id, m_sensor_id); - ret_value = OP_ERROR; - goto out; - } - - ret_value = m_module->add_attribute(m_client_id, cmd->attribute, cmd->value, cmd->len); - -out: - if (!send_cmd_done(ret_value)) - _E("Failed to send cmd_done to a client"); - - return true; -} - -bool command_worker::cmd_flush(void *payload) -{ - long ret_value = OP_ERROR; - - _D("CMD_FLUSH Handler invoked"); - - if (!is_permission_allowed()) { - _E("Permission denied to flush sensor data for client [%d], for sensor [%#llx]", - m_client_id, m_sensor_id); - ret_value = OP_ERROR; - goto out; - } - - if (!m_module->flush()) { - _E("Failed to flush sensor_data [%d]", m_client_id); - ret_value = OP_ERROR; - goto out; - } - - ret_value = OP_SUCCESS; - -out: - if (!send_cmd_done(ret_value)) - _E("Failed to send cmd_done to a client"); - - return true; -} - -void command_worker::get_info(string &info) -{ - const char *client_info = NULL; - const char *sensor_info = NULL; - - if (m_client_id != CLIENT_ID_INVALID) - client_info = get_client_info_manager().get_client_info(m_client_id); - - if (m_module) - sensor_info = m_module->get_name(); - - info = string("Command worker for ") + (client_info ? client_info : "Unknown") + "'s " - + (sensor_info ? sensor_info : "Unknown"); -} - -int command_worker::get_permission(void) -{ - return permission_checker::get_instance().get_permission(m_socket.get_socket_fd()); -} - -bool command_worker::is_permission_allowed(void) -{ - if (!m_module) - return false; - - if (m_module->get_permission() & m_permission) - return true; - - return false; -} - -client_info_manager& command_worker::get_client_info_manager(void) -{ - return client_info_manager::get_instance(); -} - -sensor_event_dispatcher& command_worker::get_event_dispathcher(void) -{ - return sensor_event_dispatcher::get_instance(); -} - diff --git a/src/server/command_worker.h b/src/server/command_worker.h deleted file mode 100644 index e6dff1d..0000000 --- a/src/server/command_worker.h +++ /dev/null @@ -1,96 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2014 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 _COMMAND_WORKER_H_ -#define _COMMAND_WORKER_H_ - -#include -#include -#include -#include -#include -#include -#include - -typedef std::multimap sensor_raw_data_map; - -class command_worker { -private: - typedef bool (command_worker::*cmd_handler_t)(void *payload); - - int m_client_id; - int m_permission; - csocket m_socket; - worker_thread m_worker; - sensor_base *m_module; - sensor_id_t m_sensor_id; - static cmd_handler_t m_cmd_handlers[CMD_CNT]; - static cpacket m_sensor_list; - static sensor_raw_data_map m_sensor_raw_data_map; - - static void init_cmd_handlers(void); - static size_t create_sensor_raw_list(int client_perms, std::vector &raw_list); - static void get_sensor_list(int permissions, cpacket &sensor_list); - - static bool working(void *ctx); - static bool stopped(void *ctx); - - bool dispatch_command(int cmd, void *payload); - - bool send_cmd_done(long value); - bool send_cmd_get_id_done(int client_id); - bool send_cmd_get_data_done(int state, sensor_data_t *data); - bool send_cmd_get_sensor_list_done(void); - - bool cmd_get_id(void *payload); - bool cmd_get_sensor_list(void *payload); - bool cmd_hello(void *payload); - bool cmd_byebye(void *payload); - bool cmd_get_value(void *payload); - bool cmd_start(void *payload); - bool cmd_stop(void *payload); - bool cmd_register_event(void *payload); - bool cmd_unregister_event(void *payload); - bool cmd_set_batch(void *payload); - bool cmd_unset_batch(void *payload); - bool cmd_set_pause_policy(void *payload); - bool cmd_get_data(void *payload); - bool cmd_set_attribute_int(void *payload); - bool cmd_set_attribute_str(void *payload); - bool cmd_flush(void *payload); - - void get_info(std::string &info); - - int get_permission(void); - bool is_permission_allowed(void); - - static client_info_manager& get_client_info_manager(void); - static sensor_event_dispatcher& get_event_dispathcher(void); - -protected: - static cmutex m_shared_mutex; - -public: - command_worker(const csocket& socket); - virtual ~command_worker(); - - bool start(void); -}; - -#endif /* _COMMAND_WORKER_H_ */ diff --git a/src/server/external_client_manager.cpp b/src/server/external_client_manager.cpp deleted file mode 100644 index a19d502..0000000 --- a/src/server/external_client_manager.cpp +++ /dev/null @@ -1,227 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2015 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 -#include -#include - -using std::shared_ptr; -using std::make_shared; -using std::pair; -using std::string; - -external_client_manager::external_client_manager() -{ -} -external_client_manager::~external_client_manager() -{ -} - -external_client_manager& external_client_manager::get_instance(void) -{ - static external_client_manager instance; - return instance; -} - -int external_client_manager::create_client_record(void) -{ - AUTOLOCK(m_mutex); - - int client_id = 0; - - shared_ptr client_record = make_shared(); - - while (m_clients.count(client_id) > 0) - client_id++; - - if (client_id == MAX_HANDLE) { - _E("Sensor records of clients are full"); - return MAX_HANDLE_REACHED; - } - - client_record->set_client_id(client_id); - - m_clients.insert(pair>(client_id, client_record)); - - return client_id; -} - -bool external_client_manager::remove_client_record(int client_id) -{ - AUTOLOCK(m_mutex); - - if (!m_clients.erase(client_id)) { - _E("Client[%d] is not found", client_id); - return false; - } - - _I("Client record for client[%d] is removed from external client manager", client_id); - return true; -} - -bool external_client_manager::has_client_record(int client_id) -{ - AUTOLOCK(m_mutex); - - auto it_record = m_clients.find(client_id); - - return (it_record != m_clients.end()); -} - -void external_client_manager::set_client_info(int client_id, pid_t pid, const string &name) -{ - AUTOLOCK(m_mutex); - - auto it_record = m_clients.find(client_id); - - if (it_record == m_clients.end()) { - _E("Client[%d] is not found", client_id); - return; - } - - it_record->second->set_client_info(pid, name); - - return; -} - -const char* external_client_manager::get_client_info(int client_id) -{ - AUTOLOCK(m_mutex); - - auto it_record = m_clients.find(client_id); - - if (it_record == m_clients.end()) { - _D("Client[%d] is not found", client_id); - return NULL; - } - - return it_record->second->get_client_info(); -} - -bool external_client_manager::create_sensor_record(int client_id, sensor_id_t sensor) -{ - AUTOLOCK(m_mutex); - - auto it_record = m_clients.find(client_id); - - if (it_record == m_clients.end()) { - _E("Client record[%d] is not registered", client_id); - return false; - } - - return it_record->second->add_usage(sensor); -} - -bool external_client_manager::remove_sensor_record(int client_id, sensor_id_t sensor) -{ - AUTOLOCK(m_mutex); - - auto it_record = m_clients.find(client_id); - - if (it_record == m_clients.end()) { - _E("Client[%d] is not found", client_id); - return false; - } - - if (!it_record->second->remove_usage(sensor)) - return false; - - if (!it_record->second->has_usage()) - remove_client_record(client_id); - - return true; -} - -bool external_client_manager::has_sensor_record(int client_id, sensor_id_t sensor) -{ - AUTOLOCK(m_mutex); - - auto it_record = m_clients.find(client_id); - - if (it_record == m_clients.end()) { - _D("Client[%d] is not found", client_id); - return false; - } - - return it_record->second->has_usage(sensor); -} - -bool external_client_manager::has_sensor_record(int client_id) -{ - AUTOLOCK(m_mutex); - - auto it_record = m_clients.find(client_id); - - if (it_record == m_clients.end()) { - _D("Client[%d] is not found", client_id); - return false; - } - - return it_record->second->has_usage(); -} - -bool external_client_manager::get_listener_socket(sensor_id_t sensor, csocket &sock) -{ - AUTOLOCK(m_mutex); - - auto it_record = m_clients.begin(); - - while (it_record != m_clients.end()) { - if (it_record->second->has_usage(sensor)) { - it_record->second->get_command_socket(sock); - return true; - } - - ++it_record; - } - - return false; -} - -bool external_client_manager::get_command_socket(int client_id, csocket &socket) -{ - AUTOLOCK(m_mutex); - - auto it_record = m_clients.find(client_id); - - if (it_record == m_clients.end()) { - _E("Client[%d] is not found", client_id); - return false; - } - - it_record->second->get_command_socket(socket); - - return true; -} - -bool external_client_manager::set_command_socket(int client_id, const csocket &socket) -{ - AUTOLOCK(m_mutex); - - auto it_record = m_clients.find(client_id); - - if (it_record == m_clients.end()) { - _E("Client[%d] is not found", client_id); - return false; - } - - it_record->second->set_command_socket(socket); - - return true; -} diff --git a/src/server/external_client_manager.h b/src/server/external_client_manager.h deleted file mode 100644 index 9934d8d..0000000 --- a/src/server/external_client_manager.h +++ /dev/null @@ -1,55 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2015 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 _EXTERNAL_CLIENT_MANAGER_H_ -#define _EXTERNAL_CLIENT_MANAGER_H_ - -#include -#include -#include -#include - -class external_client_manager { -public: - static external_client_manager& get_instance(void); - int create_client_record(void); - bool remove_client_record(int client_id); - bool has_client_record(int client_id); - - void set_client_info(int client_id, pid_t pid, const std::string &name); - const char* get_client_info(int client_id); - - bool create_sensor_record(int client_id, sensor_id_t sensor); - bool remove_sensor_record(int client_id, sensor_id_t sensor); - bool has_sensor_record(int client_id, sensor_id_t sensor); - bool has_sensor_record(int client_id); - - bool get_listener_socket(sensor_id_t sensor, csocket &sock); - bool get_command_socket(int client_id, csocket &sock); - bool set_command_socket(int client_id, const csocket &sock); -private: - external_client_manager(); - ~external_client_manager(); - external_client_manager(const external_client_manager&) {}; - external_client_manager& operator=(const external_client_manager&); - - std::unordered_map> m_clients; - cmutex m_mutex; -}; -#endif /* _EXTERNAL_CLIENT_MANAGER_H_ */ diff --git a/src/server/external_sensor.cpp b/src/server/external_sensor.cpp deleted file mode 100644 index 9046739..0000000 --- a/src/server/external_sensor.cpp +++ /dev/null @@ -1,86 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2015 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 -#include -#include -#include - -using std::string; -using std::shared_ptr; -using std::make_shared; - -external_sensor::external_sensor() -: m_source_connected(false) -{ -} - -external_sensor::~external_sensor() -{ - unregister_key(); -} - -bool external_sensor::register_key(const string &key) -{ - m_key = key; - return external_sensor_service::get_instance().register_sensor(this); -} - -bool external_sensor::unregister_key(void) -{ - return external_sensor_service::get_instance().unregister_sensor(this); -} - -string external_sensor::get_key(void) -{ - return m_key; -} - -bool external_sensor::set_source_connected(bool connected) -{ - AUTOLOCK(m_source_mutex); - - if (m_source_connected && connected) { - _E("Source is already connected"); - return false; - } - - m_source_connected = connected; - return true; -} - -bool external_sensor::get_source_connected(void) -{ - AUTOLOCK(m_source_mutex); - - return m_source_connected; -} - -int external_sensor::set_attribute(int32_t attribute, char *value, int value_size) -{ - shared_ptr external_command = make_shared(); - - external_command->header.sensor_id = get_id(); - external_command->header.command_len = value_size; - external_command->command.assign(value, value + value_size); - - command_queue::get_instance().push(external_command); - - return 0; -} diff --git a/src/server/external_sensor.h b/src/server/external_sensor.h deleted file mode 100644 index c52b325..0000000 --- a/src/server/external_sensor.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2015 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 _EXTERNAL_SENSOR_H_ -#define _EXTERNAL_SENSOR_H_ - -#include -#include -#include -#include - -class external_sensor : public sensor_base { -public: - external_sensor(); - virtual ~external_sensor(); - int send_data(const char* data, int data_cnt); - std::string get_key(void); - bool set_source_connected(bool connected); - bool get_source_connected(void); - virtual void on_receive(unsigned long long timestamp, const float* data, int data_cnt) = 0; - virtual int set_attribute(int32_t attribute, char *value, int value_size); -protected: - bool register_key(const std::string &key); -private: - bool unregister_key(void); - - std::string m_key; - bool m_source_connected; - cmutex m_source_mutex; -}; -#endif /* _EXTERNAL_SENSOR_H_ */ diff --git a/src/server/external_sensor_record.cpp b/src/server/external_sensor_record.cpp deleted file mode 100644 index 98d3c79..0000000 --- a/src/server/external_sensor_record.cpp +++ /dev/null @@ -1,99 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2015 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 - -using std::string; - -external_sensor_record::external_sensor_record() -: m_client_id(0) -, m_pid(-1) -{ -} - -external_sensor_record::~external_sensor_record() -{ - close_command_socket(); -} - -bool external_sensor_record::add_usage(sensor_id_t sensor) -{ - if (!m_usages.insert(sensor).second) { - _E("Sensor[%#x] is already registered", sensor); - return false; - } - - return true; -} - -bool external_sensor_record::remove_usage(sensor_id_t sensor) -{ - if (!m_usages.erase(sensor)) { - _E("Sensor[%#x] is not found", sensor); - return false; - } - - return true; -} - -bool external_sensor_record::has_usage(void) -{ - return !m_usages.empty(); -} - -bool external_sensor_record::has_usage(sensor_id_t sensor) -{ - auto it_usage = m_usages.find(sensor); - - return (it_usage != m_usages.end()); -} - -void external_sensor_record::set_client_id(int client_id) -{ - m_client_id = client_id; -} - -void external_sensor_record::set_client_info(pid_t pid, const string &name) -{ - char client_info[NAME_MAX + 32]; - m_pid = pid; - - snprintf(client_info, sizeof(client_info), "%s[pid=%d, id=%d]", name.c_str(), m_pid, m_client_id); - m_client_info.assign(client_info); -} - -const char* external_sensor_record::get_client_info(void) -{ - return m_client_info.c_str(); -} - -void external_sensor_record::set_command_socket(const csocket &socket) -{ - m_command_socket = socket; -} - -void external_sensor_record::get_command_socket(csocket &socket) -{ - socket = m_command_socket; -} - -bool external_sensor_record::close_command_socket(void) -{ - return m_command_socket.close(); -} - diff --git a/src/server/external_sensor_record.h b/src/server/external_sensor_record.h deleted file mode 100644 index 9b85640..0000000 --- a/src/server/external_sensor_record.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2015 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 _EXTERNAL_SENSOR_RECORD_H_ -#define _EXTERNAL_SENSOR_RECORD_H_ - -#include -#include -#include -#include - -class external_sensor_record { -public: - external_sensor_record(); - ~external_sensor_record(); - - void set_client_id(int client_id); - - void set_client_info(pid_t pid, const std::string &name); - const char* get_client_info(void); - - bool has_usage(void); - bool has_usage(sensor_id_t sensor); - - bool add_usage(sensor_id_t sensor); - bool remove_usage(sensor_id_t sensor); - - void set_command_socket(const csocket &socket); - void get_command_socket(csocket &socket); - bool close_command_socket(void); - -private: - int m_client_id; - pid_t m_pid; - std::string m_client_info; - csocket m_command_socket; - std::unordered_set m_usages; -}; - -#endif /* _EXTERNAL_SENSOR_RECORD_H_ */ diff --git a/src/server/external_sensor_service.cpp b/src/server/external_sensor_service.cpp deleted file mode 100644 index c7d1e8e..0000000 --- a/src/server/external_sensor_service.cpp +++ /dev/null @@ -1,152 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2015 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 -#include -#include -#include -#include - -using std::thread; -using std::pair; -using std::string; -using std::shared_ptr; - -external_sensor_service::external_sensor_service() -{ -} - -external_sensor_service::~external_sensor_service() -{ -} - -external_sensor_service& external_sensor_service::get_instance(void) -{ - static external_sensor_service instance; - return instance; -} - -bool external_sensor_service::run(void) -{ - thread dispatcher(&external_sensor_service::dispatch_command, this); - dispatcher.detach(); - - return true; -} - -external_client_manager& external_sensor_service::get_client_manager(void) -{ - return external_client_manager::get_instance(); -} - -void external_sensor_service::accept_command_channel(csocket client_socket) -{ - thread th = thread([&, client_socket]() mutable { - int client_id; - channel_ready_t command_channel_ready; - external_client_manager& client_manager = get_client_manager(); - - client_socket.set_connection_mode(); - - if (client_socket.recv(&client_id, sizeof(client_id)) <= 0) { - _E("Failed to receive client id on socket fd[%d]", client_socket.get_socket_fd()); - return; - } - - client_socket.set_transfer_mode(); - - if (!client_manager.set_command_socket(client_id, client_socket)) { - _E("Failed to store event socket[%d] for %s", client_socket.get_socket_fd(), - client_manager.get_client_info(client_id)); - return; - } - - command_channel_ready.magic = CHANNEL_MAGIC_NUM; - command_channel_ready.client_id = client_id; - - _I("Command channel is accepted for %s on socket[%d]", - client_manager.get_client_info(client_id), client_socket.get_socket_fd()); - - if (client_socket.send(&command_channel_ready, sizeof(command_channel_ready)) <= 0) { - _E("Failed to send command channel_ready packet to %s on socket fd[%d]", - client_manager.get_client_info(client_id), client_socket.get_socket_fd()); - return; - } - }); - - th.detach(); -} - -void external_sensor_service::dispatch_command(void) -{ - while (true) { - shared_ptr command = command_queue::get_instance().pop(); - csocket client_sock; - sensor_id_t sensor_id = command->header.sensor_id; - bool ret; - - ret = external_client_manager::get_instance().get_listener_socket(sensor_id, client_sock); - - if (!ret) { - _E("Failed to get listener socket for sensor[%d]", sensor_id); - continue; - } - - if (client_sock.send(&(command->header), sizeof(command->header)) <= 0) { - _E("Failed to send command header to the client of sensor[%d]", sensor_id); - continue; - } - - if (client_sock.send(command->command.data(), command->header.command_len) <= 0) { - _E("Failed to send command header to the client of sensor[%d]", sensor_id); - continue; - } - } -} - -bool external_sensor_service::register_sensor(external_sensor *sensor) -{ - if (!m_external_sensors.insert(pair(sensor->get_key(), sensor)).second) { - _E("Failed to register sensor, key: %s", sensor->get_key().c_str()); - return false; - } - - return true; -} - -bool external_sensor_service::unregister_sensor(external_sensor *sensor) -{ - if (!m_external_sensors.erase(sensor->get_key())) { - _E("Failed to unregister sensor, key: %s", sensor->get_key().c_str()); - return false; - } - - return true; -} - -external_sensor* external_sensor_service::get_sensor(const string& key) -{ - auto it_sensor = m_external_sensors.find(key); - - if (it_sensor == m_external_sensors.end()) { - _E("Sensor(key:%s) is not found", key.c_str()); - return NULL; - } - - return it_sensor->second; -} diff --git a/src/server/external_sensor_service.h b/src/server/external_sensor_service.h deleted file mode 100644 index 28b1aa4..0000000 --- a/src/server/external_sensor_service.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2015 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 _EXTERNAL_SENSOR_SERVICE_H_ -#define _EXTERNAL_SENSOR_SERVICE_H_ - -#include -#include -#include -#include - -class external_client_manager; - -class external_sensor_service { -public: - static external_sensor_service& get_instance(); - bool register_sensor(external_sensor *sensor); - bool unregister_sensor(external_sensor *sensor); - external_sensor* get_sensor(const std::string& key); - - void accept_command_channel(csocket client_socket); - bool run(void); -private: - external_sensor_service(); - ~external_sensor_service(); - external_sensor_service(const external_sensor_service&) {}; - external_sensor_service& operator=(const external_sensor_service&); - - static external_client_manager& get_client_manager(void); - - void dispatch_command(void); - - std::unordered_map m_external_sensors; -}; -#endif /* _EXTERNAL_SENSOR_SERVICE_H_ */ - diff --git a/src/server/external_sensor_worker.cpp b/src/server/external_sensor_worker.cpp deleted file mode 100644 index f62dc28..0000000 --- a/src/server/external_sensor_worker.cpp +++ /dev/null @@ -1,325 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2015 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 -#include -#include -#include -#include -#include - -using std::string; - -external_sensor_worker::cmd_handler_t external_sensor_worker::m_cmd_handlers[]; - -external_sensor_worker::external_sensor_worker(const csocket& socket) -: m_client_id(CLIENT_ID_INVALID) -, m_socket(socket) -, m_sensor(NULL) -, m_sensor_id(UNKNOWN_SENSOR) -{ - static bool init = false; - - if (!init) { - init_cmd_handlers(); - init = true; - } - - m_worker.set_context(this); - m_worker.set_working(working); - m_worker.set_stopped(stopped); -} - -external_sensor_worker::~external_sensor_worker() -{ - m_socket.close(); -} - -bool external_sensor_worker::start(void) -{ - return m_worker.start(); -} - -void external_sensor_worker::init_cmd_handlers(void) -{ - m_cmd_handlers[CMD_EXT_GET_ID] = &external_sensor_worker::cmd_get_id; - m_cmd_handlers[CMD_EXT_CONNECT] = &external_sensor_worker::cmd_connect; - m_cmd_handlers[CMD_EXT_DISCONNECT] = &external_sensor_worker::cmd_disconnect; - m_cmd_handlers[CMD_EXT_POST] = &external_sensor_worker::cmd_post; -} - -bool external_sensor_worker::working(void *ctx) -{ - bool ret; - external_sensor_worker *inst = (external_sensor_worker *)ctx; - - packet_header header; - char *payload; - - if (inst->m_socket.recv(&header, sizeof(header)) <= 0) { - string info; - inst->get_info(info); - _D("%s failed to receive header", info.c_str()); - return false; - } - - retvm_if(header.size == 0, false, "Invalid header size"); - - payload = new(std::nothrow) char[header.size]; - retvm_if(!payload, false, "Failed to allocate memory"); - - if (inst->m_socket.recv(payload, header.size) <= 0) { - string info; - inst->get_info(info); - _D("%s failed to receive data of packet", info.c_str()); - delete[] payload; - return false; - } - - ret = inst->dispatch_command(header.cmd, payload); - - delete[] payload; - return ret; -} - -bool external_sensor_worker::stopped(void *ctx) -{ - string info; - external_sensor_worker *inst = (external_sensor_worker *)ctx; - - inst->get_info(info); - _I("%s is stopped", info.c_str()); - - if (inst->m_client_id != CLIENT_ID_INVALID) { - _I("Client:%d leaves without disconnecting", inst->m_client_id); - if (get_client_manager().has_sensor_record(inst->m_client_id, inst->m_sensor_id)) { - _I("Removing sensor[%#x] record for client_id[%d]", inst->m_sensor_id, inst->m_client_id); - get_client_manager().remove_sensor_record(inst->m_client_id, inst->m_sensor_id); - - if (inst->m_sensor) - inst->m_sensor->set_source_connected(false); - } - } - - delete inst; - return true; -} - -bool external_sensor_worker::dispatch_command(int cmd, void* payload) -{ - int ret = false; - - if (!(cmd > 0 && cmd < CMD_EXT_CNT)) { - _E("Unknown command: %d", cmd); - } else { - cmd_handler_t cmd_handler; - cmd_handler = external_sensor_worker::m_cmd_handlers[cmd]; - if (cmd_handler) - ret = (this->*cmd_handler)(payload); - } - - return ret; -} - -bool external_sensor_worker::send_cmd_done(long value) -{ - cpacket* ret_packet; - cmd_ext_done_t *cmd_ext_done; - - ret_packet = new(std::nothrow) cpacket(sizeof(cmd_ext_done_t)); - retvm_if(!ret_packet, false, "Failed to allocate memory"); - - ret_packet->set_cmd(CMD_EXT_DONE); - - cmd_ext_done = (cmd_ext_done_t*)ret_packet->data(); - cmd_ext_done->value = value; - - if (m_socket.send(ret_packet->packet(), ret_packet->size()) <= 0) { - _E("Failed to send a cmd_done to client_id [%d] with value [%ld]", m_client_id, value); - delete ret_packet; - return false; - } - - delete ret_packet; - return true; -} - -bool external_sensor_worker::send_cmd_get_id_done(int client_id) -{ - cpacket* ret_packet; - cmd_ext_get_id_done_t *cmd_ext_get_id_done; - - ret_packet = new(std::nothrow) cpacket(sizeof(cmd_ext_get_id_done_t)); - retvm_if(!ret_packet, false, "Failed to allocate memory"); - - ret_packet->set_cmd(CMD_EXT_GET_ID); - - cmd_ext_get_id_done = (cmd_ext_get_id_done_t*)ret_packet->data(); - cmd_ext_get_id_done->client_id = client_id; - - if (m_socket.send(ret_packet->packet(), ret_packet->size()) <= 0) { - _E("Failed to send a cmd_get_id_done with client_id [%d]", client_id); - delete ret_packet; - return false; - } - - delete ret_packet; - return true; -} - -bool external_sensor_worker::send_cmd_connect_done(sensor_id_t sensor_id) -{ - cpacket* ret_packet; - cmd_ext_connect_done_t *cmd_ext_connect_done; - - ret_packet = new(std::nothrow) cpacket(sizeof(cmd_ext_connect_done_t)); - retvm_if(!ret_packet, false, "Failed to allocate memory"); - - ret_packet->set_cmd(CMD_EXT_CONNECT); - - cmd_ext_connect_done = (cmd_ext_connect_done_t*)ret_packet->data(); - cmd_ext_connect_done->sensor_id = sensor_id; - - if (m_socket.send(ret_packet->packet(), ret_packet->size()) <= 0) { - _E("Failed to send a cmd_connect done"); - delete ret_packet; - return false; - } - - delete ret_packet; - return true; -} - -bool external_sensor_worker::cmd_get_id(void *payload) -{ - cmd_ext_get_id_t *cmd = static_cast(payload); - int client_id; - struct ucred cr; - socklen_t opt_len = sizeof(cr); - - if (getsockopt(m_socket.get_socket_fd(), SOL_SOCKET, SO_PEERCRED, &cr, &opt_len)) { - _E("Failed to get socket option with SO_PEERCRED"); - return false; - } - - client_id = get_client_manager().create_client_record(); - - if (client_id != MAX_HANDLE_REACHED) { - get_client_manager().set_client_info(client_id, cr.pid, cmd->name); - _I("New client id [%d] created", client_id); - } - - if (!send_cmd_get_id_done(client_id)) - _E("Failed to send cmd_done to a client"); - - return true; -} - -bool external_sensor_worker::cmd_connect(void *payload) -{ - cmd_ext_connect_t *cmd = static_cast(payload); - m_client_id = cmd->client_id; - - external_sensor *sensor; - sensor = external_sensor_service::get_instance().get_sensor(string(cmd->key)); - if (!sensor) { - _E("No matched external sensor with key: %s", cmd->key); - goto out; - } - - if (!sensor->set_source_connected(true)) { - _E("External sensor(%s) is already connected", cmd->key); - goto out; - } - - m_sensor = sensor; - m_sensor_id = sensor->get_id(); - - if (!get_client_manager().create_sensor_record(m_client_id, m_sensor_id)) { - _E("Failed to create sensor record for client: %d, sensor_id: %d", m_client_id, m_sensor_id); - m_sensor_id = UNKNOWN_SENSOR; - goto out; - } - -out: - if (!send_cmd_connect_done(m_sensor_id)) - _E("Failed to send cmd_connect_done to a client : %d", m_client_id); - - return true; -} - -bool external_sensor_worker::cmd_disconnect(void *payload) -{ - long ret_value = OP_ERROR; - - if (!m_sensor) { - _E("External sensor is not connected"); - ret_value = OP_ERROR; - goto out; - } - - if (!get_client_manager().remove_sensor_record(m_client_id, m_sensor_id)) { - _E("Failed to remove sensor record for client [%d]", m_client_id); - ret_value = OP_ERROR; - goto out; - } - - m_sensor->set_source_connected(false); - - m_sensor = NULL; - m_client_id = CLIENT_ID_INVALID; - m_sensor_id = UNKNOWN_SENSOR; - ret_value = OP_SUCCESS; -out: - if (!send_cmd_done(ret_value)) - _E("Failed to send cmd_done to a client"); - - if (ret_value == OP_SUCCESS) - return false; - - return true; -} - -bool external_sensor_worker::cmd_post(void *payload) -{ - long ret_value = OP_SUCCESS; - cmd_ext_post_t *cmd = static_cast(payload); - - m_sensor->on_receive(cmd->timestamp, cmd->data, cmd->data_cnt); - - if (!send_cmd_done(ret_value)) - _E("Failed to send cmd_done to a client"); - - return true; -} - -external_client_manager& external_sensor_worker::get_client_manager(void) -{ - return external_client_manager::get_instance(); -} - -void external_sensor_worker::get_info(string &info) -{ - const char *client_info = NULL; - - if (m_client_id != CLIENT_ID_INVALID) - client_info = get_client_manager().get_client_info(m_client_id); - - info = string("Command worker for ") + (client_info ? string(client_info) : string("Unknown")); -} diff --git a/src/server/external_sensor_worker.h b/src/server/external_sensor_worker.h deleted file mode 100644 index 982dd09..0000000 --- a/src/server/external_sensor_worker.h +++ /dev/null @@ -1,67 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2015 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 _EXTERNAL_SENSOR_WORKER_H_ -#define _EXTERNAL_SENSOR_WORKER_H_ - -#include -#include -#include -#include - -class external_client_manager; -class external_sensor; - -class external_sensor_worker { -private: - typedef bool (external_sensor_worker::*cmd_handler_t)(void *payload); - - int m_client_id; - csocket m_socket; - worker_thread m_worker; - external_sensor *m_sensor; - sensor_id_t m_sensor_id; - static cmd_handler_t m_cmd_handlers[CMD_EXT_CNT]; - - static void init_cmd_handlers(void); - static bool working(void *ctx); - static bool stopped(void *ctx); - - bool dispatch_command(int cmd, void *payload); - - bool send_cmd_done(long value); - bool send_cmd_get_id_done(int client_id); - bool send_cmd_connect_done(sensor_id_t sensor_id); - - bool cmd_get_id(void *payload); - bool cmd_connect(void *payload); - bool cmd_disconnect(void *payload); - bool cmd_post(void *payload); - - static external_client_manager& get_client_manager(void); - - void get_info(std::string &info); -public: - external_sensor_worker(const csocket& socket); - ~external_sensor_worker(); - - bool start(void); -}; - -#endif /* _EXTERNAL_SENSOR_WORKER_H_ */ diff --git a/src/server/main.cpp b/src/server/main.cpp index e3e9f73..2ce210c 100644 --- a/src/server/main.cpp +++ b/src/server/main.cpp @@ -17,96 +17,10 @@ * */ -#include -#include -#include -#include -#include -#include - +/* TODO: create conf file for calibration paths */ #define CAL_NODE_PATH "/sys/class/sensors/ssp_sensor/set_cal_data" -#define SET_CAL 1 - -#define TIMEOUT 10 - -static void sig_term_handler(int signo, siginfo_t *info, void *data) -{ - char proc_name[NAME_MAX]; - - get_proc_name(info->si_pid, proc_name); - - _E("Received SIGTERM(%d) from %s(%d)\n", signo, proc_name, info->si_pid); - - server::get_instance().stop(); -} - -static void signal_init(void) -{ - struct sigaction sig_act; - memset(&sig_act, 0, sizeof(struct sigaction)); - - sig_act.sa_handler = SIG_IGN; - sigaction(SIGCHLD, &sig_act, NULL); - sigaction(SIGPIPE, &sig_act, NULL); - - sig_act.sa_handler = NULL; - sig_act.sa_sigaction = sig_term_handler; - sig_act.sa_flags = SA_SIGINFO; - sigaction(SIGTERM, &sig_act, NULL); - sigaction(SIGABRT, &sig_act, NULL); - sigaction(SIGINT, &sig_act, NULL); -} - -static void set_cal_data(void) -{ - FILE *fp = fopen(CAL_NODE_PATH, "w"); - - if (!fp) { - _I("Not support calibration_node"); - return; - } - - fprintf(fp, "%d", SET_CAL); - - if (fp) - fclose(fp); - - _I("Succeeded to set calibration data"); - - return; -} - -static gboolean terminate(gpointer data) -{ - std::vector sensors = sensor_loader::get_instance().get_sensors(ALL_SENSOR); - - if (sensors.size() == 0) { - _I("Terminating sensord.."); - server::get_instance().stop(); - } - - return FALSE; -} int main(int argc, char *argv[]) { - _I("Sensord started"); - - signal_init(); - - init_dbus(); - - set_cal_data(); - - /* TODO: loading sequence has to be moved to server */ - sensor_loader::get_instance().load(); - g_timeout_add_seconds(TIMEOUT, terminate, NULL); - - server::get_instance().run(); - server::get_instance().stop(); - - fini_dbus(); - - _I("Sensord terminated"); return 0; } diff --git a/src/server/permission_checker.cpp b/src/server/permission_checker.cpp index 3da9ac6..20b03ec 100644 --- a/src/server/permission_checker.cpp +++ b/src/server/permission_checker.cpp @@ -17,133 +17,22 @@ * */ +#include #include #include #include -#include -#include -#include -#include -#include -#include #define CACHE_SIZE 16 -static cynara *cynara_env = NULL; - static bool check_privilege_by_sockfd(int sock_fd, const char *priv, const char *access) { - retvm_if(cynara_env == NULL, false, "Cynara not initialized"); - - int ret; - int pid = -1; - char *client = NULL; - char *session = NULL; - char *user = NULL; - - retvm_if(cynara_creds_socket_get_pid(sock_fd, &pid) != CYNARA_API_SUCCESS, false, "Getting PID failed"); - - if (cynara_creds_socket_get_client(sock_fd, CLIENT_METHOD_DEFAULT, &client) != CYNARA_API_SUCCESS || - cynara_creds_socket_get_user(sock_fd, USER_METHOD_DEFAULT, &user) != CYNARA_API_SUCCESS || - (session = cynara_session_from_pid(pid)) == NULL) { - _E("Getting client info failed"); - free(client); - free(user); - free(session); - return false; - } - - ret = cynara_check(cynara_env, client, session, user, priv); - - free(client); - free(session); - free(user); - - return (ret == CYNARA_API_ACCESS_ALLOWED); + return true; } permission_checker::permission_checker() -: m_permission_set(0) { - init(); - init_cynara(); } permission_checker::~permission_checker() { - deinit_cynara(); -} - -permission_checker& permission_checker::get_instance(void) -{ - static permission_checker inst; - return inst; -} - -void permission_checker::init(void) -{ - m_permission_infos.push_back(std::make_shared(SENSOR_PERMISSION_BIO, "http://tizen.org/privilege/healthinfo", "")); - - std::vector sensors; - sensors = sensor_loader::get_instance().get_sensors(ALL_SENSOR); - - for (unsigned int i = 0; i < sensors.size(); ++i) - m_permission_set |= sensors[i]->get_permission(); - - _I("Permission Set = %d", m_permission_set); -} - -void permission_checker::init_cynara(void) -{ - AUTOLOCK(m_mutex); - - cynara_configuration *conf; - - int err = cynara_configuration_create(&conf); - retm_if(err != CYNARA_API_SUCCESS, "Failed to create cynara configuration"); - - err = cynara_configuration_set_cache_size(conf, CACHE_SIZE); - if (err != CYNARA_API_SUCCESS) { - _E("Failed to set cynara cache"); - cynara_configuration_destroy(conf); - return; - } - - err = cynara_initialize(&cynara_env, conf); - cynara_configuration_destroy(conf); - - if (err != CYNARA_API_SUCCESS) { - _E("Failed to initialize cynara"); - cynara_env = NULL; - return; - } - - _I("Cynara initialized"); -} - -void permission_checker::deinit_cynara(void) -{ - AUTOLOCK(m_mutex); - - if (cynara_env) - cynara_finish(cynara_env); - - cynara_env = NULL; -} - -int permission_checker::get_permission(int sock_fd) -{ - AUTOLOCK(m_mutex); - - int permission = SENSOR_PERMISSION_STANDARD; - - for (unsigned int i = 0; i < m_permission_infos.size(); ++i) { - if (!(m_permission_set & m_permission_infos[i]->permission)) - continue; - - if (check_privilege_by_sockfd(sock_fd, m_permission_infos[i]->privilege.c_str(), m_permission_infos[i]->access.c_str())) - permission |= m_permission_infos[i]->permission; - } - - return permission; } diff --git a/src/server/permission_checker.h b/src/server/permission_checker.h index d42ba09..01cc8b4 100644 --- a/src/server/permission_checker.h +++ b/src/server/permission_checker.h @@ -20,49 +20,13 @@ #ifndef _PERMISSION_CHECKER_H_ #define _PERMISSION_CHECKER_H_ -#include #include #include #include class permission_checker { -public: - static permission_checker& get_instance(void); - - int get_permission(int sock_fd); - -private: - class permission_info { - public: - permission_info(int _permission, std::string _priv, std::string _access) - : permission(_permission) - , privilege(_priv) - , access(_access) - { - } - int permission; - std::string privilege; - std::string access; - }; - - typedef std::vector> permission_info_vector; - permission_checker(); - permission_checker(permission_checker const&) {}; - permission_checker& operator=(permission_checker const&); - ~permission_checker(); - - void init(void); - -private: - permission_info_vector m_permission_infos; - int m_permission_set; - cmutex m_mutex; - -private: - void init_cynara(void); - void deinit_cynara(void); }; #endif /* _PERMISSION_CHECKER_H_ */ diff --git a/src/server/physical_sensor.cpp b/src/server/physical_sensor.cpp index 65ab74e..f1943f6 100644 --- a/src/server/physical_sensor.cpp +++ b/src/server/physical_sensor.cpp @@ -17,205 +17,13 @@ * */ -#include #include -#include - -#define UNKNOWN_NAME "UNKNOWN_SENSOR" - -cmutex physical_sensor::m_mutex; physical_sensor::physical_sensor() -: m_sensor_device(NULL) { } physical_sensor::~physical_sensor() { - _I("physical sensor is destroyed"); -} - -void physical_sensor::set_sensor_info(const sensor_info_t *info) -{ - m_info = info; -} - -void physical_sensor::set_sensor_device(sensor_device *device) -{ - m_sensor_device = device; -} - -sensor_type_t physical_sensor::get_type(void) -{ - return static_cast(m_info->type); -} - -unsigned int physical_sensor::get_event_type(void) -{ - return m_info->event_type; -} - -const char* physical_sensor::get_name(void) -{ - retv_if(!m_info, UNKNOWN_NAME); - retv_if(!m_info->name, UNKNOWN_NAME); - - return m_info->name; -} - -uint32_t physical_sensor::get_hal_id(void) -{ - return m_info->id; -} - -int physical_sensor::get_poll_fd(void) -{ - AUTOLOCK(m_mutex); - - if (!m_sensor_device) - return OP_ERROR; - - return m_sensor_device->get_poll_fd(); -} - -bool physical_sensor::on_event(const sensor_data_t *data, int data_len, int remains) -{ - return true; -} - -bool physical_sensor::read_fd(std::vector &ids) -{ - AUTOLOCK(m_mutex); - int size; - uint32_t *_ids; - - if (!m_sensor_device) - return false; - - size = m_sensor_device->read_fd(&_ids); - - if (size == 0) - return false; - - for (int i = 0; i < size; ++i) - ids.push_back(_ids[i]); - - return true; -} - -int physical_sensor::get_data(sensor_data_t **data, int *length) -{ - AUTOLOCK(m_mutex); - - if (!m_sensor_device) - return OP_ERROR; - - int remains = 0; - remains = m_sensor_device->get_data(m_info->id, data, length); - - if (*length < 0) { - _E("Failed to get sensor event"); - return OP_ERROR; - } - - return remains; -} - -bool physical_sensor::flush(void) -{ - AUTOLOCK(m_mutex); - - if (!m_sensor_device) - return false; - - return m_sensor_device->flush(m_info->id); -} - -bool physical_sensor::set_interval(unsigned long interval) -{ - AUTOLOCK(m_mutex); - - if (!m_sensor_device) - return false; - - _I("Polling interval is set to %dms", interval); - - return m_sensor_device->set_interval(m_info->id, interval); -} - -bool physical_sensor::set_batch_latency(unsigned long latency) -{ - AUTOLOCK(m_mutex); - - if (!m_sensor_device) - return false; - - _I("Batch latency is set to %dms", latency); - - return m_sensor_device->set_batch_latency(m_info->id, latency); } -int physical_sensor::set_attribute(int32_t attribute, int32_t value) -{ - AUTOLOCK(m_mutex); - - if (!m_sensor_device) - return OP_ERROR; - - if (!m_sensor_device->set_attribute_int(m_info->id, attribute, value)) - return OP_ERROR; - - return OP_SUCCESS; -} - -int physical_sensor::set_attribute(int32_t attribute, char *value, int len) -{ - AUTOLOCK(m_mutex); - - if (!m_sensor_device) - return OP_ERROR; - - if (!m_sensor_device->set_attribute_str(m_info->id, attribute, value, len)) - return OP_ERROR; - - return OP_SUCCESS; -} - -bool physical_sensor::on_start(void) -{ - AUTOLOCK(m_mutex); - - if (!m_sensor_device) - return false; - - return m_sensor_device->enable(m_info->id); -} - -bool physical_sensor::on_stop(void) -{ - AUTOLOCK(m_mutex); - - if (!m_sensor_device) - return false; - - return m_sensor_device->disable(m_info->id); -} - -bool physical_sensor::get_sensor_info(sensor_info &info) -{ - info.set_type(get_type()); - info.set_id(get_id()); - info.set_privilege(SENSOR_PRIVILEGE_PUBLIC); // FIXME - info.set_name(m_info->model_name); - info.set_vendor(m_info->vendor); - info.set_min_range(m_info->min_range); - info.set_max_range(m_info->max_range); - info.set_resolution(m_info->resolution); - info.set_min_interval(m_info->min_interval); - info.set_fifo_count(0); - info.set_max_batch_count(m_info->max_batch_count); - info.set_supported_event(get_event_type()); - info.set_wakeup_supported(m_info->wakeup_supported); - - return true; -} diff --git a/src/server/physical_sensor.h b/src/server/physical_sensor.h index 1264931..669bbeb 100644 --- a/src/server/physical_sensor.h +++ b/src/server/physical_sensor.h @@ -20,48 +20,10 @@ #ifndef _PHYSICAL_SENSOR_H_ #define _PHYSICAL_SENSOR_H_ -#include -#include -#include - -class physical_sensor : public sensor_base { +class physical_sensor { public: physical_sensor(); virtual ~physical_sensor(); - - /* setting module */ - void set_sensor_info(const sensor_info_t *info); - void set_sensor_device(sensor_device *device); - - /* module info */ - virtual sensor_type_t get_type(void); - virtual unsigned int get_event_type(void); - virtual const char* get_name(void); - virtual uint32_t get_hal_id(void); - - int get_poll_fd(void); - - virtual bool on_event(const sensor_data_t *data, int data_len, int remains); - - virtual bool read_fd(std::vector &ids); - virtual int get_data(sensor_data_t **data, int *length); - virtual bool flush(void); - -protected: - const sensor_info_t *m_info; - sensor_device *m_sensor_device; - uint32_t hal_id; - - virtual bool on_start(void); - virtual bool on_stop(void); - virtual int set_attribute(int32_t attribute, int32_t value); - virtual int set_attribute(int32_t attribute, char *value, int len); - virtual bool set_interval(unsigned long interval); - virtual bool set_batch_latency(unsigned long latency); - virtual bool get_sensor_info(sensor_info &info); - -private: - static cmutex m_mutex; }; #endif /* _PHYSICAL_SENSOR_H_ */ diff --git a/src/server/sensor_base.cpp b/src/server/sensor_base.cpp deleted file mode 100644 index 24e16a7..0000000 --- a/src/server/sensor_base.cpp +++ /dev/null @@ -1,384 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2014 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 -#include -#include -#include -#include -#include - -#include -#include -#include - -using std::make_pair; -using std::vector; - -sensor_base::sensor_base() -: m_last_data(NULL) -, m_id(SENSOR_ID_INVALID) -, m_permission(SENSOR_PERMISSION_STANDARD) -, m_started(false) -, m_client(0) -{ -} - -sensor_base::~sensor_base() -{ -} - -void sensor_base::set_id(sensor_id_t id) -{ - m_id = id; -} - -sensor_id_t sensor_base::get_id(void) -{ - if (m_id == SENSOR_ID_INVALID) - return UNKNOWN_SENSOR; - - return m_id; -} - -sensor_type_t sensor_base::get_type(void) -{ - return UNKNOWN_SENSOR; -} - -unsigned int sensor_base::get_event_type(void) -{ - return -1; -} - -const char* sensor_base::get_name(void) -{ - return NULL; -} - -bool sensor_base::get_sensor_info(sensor_info &info) -{ - return false; -} - -bool sensor_base::is_virtual(void) -{ - return false; -} - -int sensor_base::get_data(sensor_data_t **data, int *length) -{ - return OP_ERROR; -} - -bool sensor_base::flush(void) -{ - return true; -} - -int sensor_base::add_attribute(int client_id, int32_t attribute, int32_t value) -{ - return set_attribute(attribute, value); -} - -int sensor_base::add_attribute(int client_id, int32_t attribute, char *value, int value_size) -{ - return set_attribute(attribute, value, value_size); -} - -bool sensor_base::delete_attribute(int client_id) -{ - return true; -} - -int sensor_base::set_attribute(int32_t attribute, int32_t value) -{ - return OP_SUCCESS; -} - -int sensor_base::set_attribute(int32_t attribute, char *value, int value_size) -{ - return OP_SUCCESS; -} - -bool sensor_base::start(void) -{ - AUTOLOCK(m_client_mutex); - - if (!pre_start()) - return false; - - ++m_client; - - if (m_client == 1) { - if (!on_start()) { - _E("[%s] sensor failed to start", get_name()); - return false; - } - - m_started = true; - } - - _I("[%s] sensor started, #client = %d", get_name(), m_client); - - return true; -} - -bool sensor_base::stop(void) -{ - AUTOLOCK(m_client_mutex); - - --m_client; - - if (m_client == 0) { - if (!on_stop()) { - _E("[%s] sensor faild to stop", get_name()); - return false; - } - - m_started = false; - - free(m_last_data); - m_last_data = NULL; - } - - _I("[%s] sensor stopped, #client = %d", get_name(), m_client); - - return true; -} - -bool sensor_base::is_started(void) -{ - AUTOLOCK(m_client_mutex); - - return m_started; -} - -bool sensor_base::add_interval(int client_id, unsigned int interval, bool is_processor) -{ - unsigned int prev_min, cur_min; - - AUTOLOCK(m_sensor_info_list_mutex); - - prev_min = m_sensor_info_list.get_min_interval(); - - if (!m_sensor_info_list.add_interval(client_id, interval, is_processor)) - return false; - - cur_min = m_sensor_info_list.get_min_interval(); - - if (cur_min != prev_min) { - _I("Min interval for sensor[%#llx] is changed from %dms to %dms" - " by%sclient[%d] adding interval", - get_id(), prev_min, cur_min, - is_processor ? " processor " : " ", client_id); - - set_interval(cur_min); - } - - return true; -} - -bool sensor_base::delete_interval(int client_id, bool is_processor) -{ - unsigned int prev_min, cur_min; - AUTOLOCK(m_sensor_info_list_mutex); - - prev_min = m_sensor_info_list.get_min_interval(); - - if (!m_sensor_info_list.delete_interval(client_id, is_processor)) - return false; - - cur_min = m_sensor_info_list.get_min_interval(); - - if (!cur_min) { - _I("No interval for sensor[%#llx] by%sclient[%d] deleting interval, " - "so set to default %dms", - get_id(), is_processor ? " processor " : " ", - client_id, POLL_1HZ_MS); - - set_interval(POLL_1HZ_MS); - } else if (cur_min != prev_min) { - _I("Min interval for sensor[%#llx] is changed from %dms to %dms" - " by%sclient[%d] deleting interval", - get_id(), prev_min, cur_min, - is_processor ? " processor " : " ", client_id); - - set_interval(cur_min); - } - - return true; -} - -unsigned int sensor_base::get_interval(int client_id, bool is_processor) -{ - AUTOLOCK(m_sensor_info_list_mutex); - - return m_sensor_info_list.get_interval(client_id, is_processor); -} - -bool sensor_base::add_batch(int client_id, unsigned int latency) -{ - unsigned int prev_max, cur_max; - - AUTOLOCK(m_sensor_info_list_mutex); - - prev_max = m_sensor_info_list.get_max_batch(); - - if (!m_sensor_info_list.add_batch(client_id, latency)) - return false; - - cur_max = m_sensor_info_list.get_max_batch(); - - if (cur_max != prev_max) { - _I("Max latency for sensor[%#llx] is changed from %dms to %dms by client[%d] adding latency", - get_id(), prev_max, cur_max, client_id); - set_batch_latency(cur_max); - } - - return true; -} - -bool sensor_base::delete_batch(int client_id) -{ - unsigned int prev_max, cur_max; - AUTOLOCK(m_sensor_info_list_mutex); - - prev_max = m_sensor_info_list.get_max_batch(); - - if (!m_sensor_info_list.delete_batch(client_id)) - return false; - - cur_max = m_sensor_info_list.get_max_batch(); - - if (!cur_max) { - _I("No latency for sensor[%#llx] by client[%d] deleting latency, so set to default count", - get_id(), client_id); - - set_batch_latency(UINT_MAX); - } else if (cur_max != prev_max) { - _I("Max latency for sensor[%#llx] is changed from %dms to %dms by client[%d] deleting latency", - get_id(), prev_max, cur_max, client_id); - - set_batch_latency(cur_max); - } - - return true; -} - -unsigned int sensor_base::get_batch(int client_id) -{ - AUTOLOCK(m_sensor_info_list_mutex); - - return m_sensor_info_list.get_batch(client_id); -} - -int sensor_base::get_permission(void) -{ - return m_permission; -} - -void sensor_base::set_permission(int permission) -{ - m_permission = permission; -} - -bool sensor_base::push(sensor_event_t *event) -{ - if (!event || !(event->data)) - return false; - - set_cache(event->data); - - AUTOLOCK(m_client_mutex); - - if (m_client <= 0) - return false; - - sensor_event_queue::get_instance().push(event); - return true; -} - -void sensor_base::set_cache(sensor_data_t *data) -{ - AUTOLOCK(m_data_cache_mutex); - - /* Caching the last known data for sync-read support */ - if (m_last_data == NULL) { - m_last_data = (sensor_data_t*)malloc(sizeof(sensor_data_t)); - retm_if(m_last_data == NULL, "Memory allocation failed"); - } - - memcpy(m_last_data, data, sizeof(sensor_data_t)); -} - -int sensor_base::get_cache(sensor_data_t **data) -{ - retv_if(m_last_data == NULL, -ENODATA); - - *data = (sensor_data_t *)malloc(sizeof(sensor_data_t)); - retvm_if(*data == NULL, -ENOMEM, "Memory allocation failed"); - - AUTOLOCK(m_data_cache_mutex); - - memcpy(*data, m_last_data, sizeof(sensor_data_t)); - return 0; -} - -bool sensor_base::set_interval(unsigned long interval) -{ - return true; -} - -bool sensor_base::set_batch_latency(unsigned long latency) -{ - return true; -} - -bool sensor_base::pre_start(void) -{ - return true; -} - -bool sensor_base::on_start(void) -{ - return true; -} - -bool sensor_base::on_stop(void) -{ - return true; -} - -unsigned long long sensor_base::get_timestamp(void) -{ - struct timespec t; - clock_gettime(CLOCK_MONOTONIC, &t); - return ((unsigned long long)(t.tv_sec)*1000000000LL + t.tv_nsec) / 1000; -} - -unsigned long long sensor_base::get_timestamp(timeval *t) -{ - if (!t) { - _E("t is NULL"); - return 0; - } - - return ((unsigned long long)(t->tv_sec)*1000000LL +t->tv_usec); -} diff --git a/src/server/sensor_base.h b/src/server/sensor_base.h deleted file mode 100644 index 89b88ed..0000000 --- a/src/server/sensor_base.h +++ /dev/null @@ -1,113 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2014 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_BASE_H_ -#define _SENSOR_BASE_H_ - -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -class sensor_base { -public: - sensor_base(); - virtual ~sensor_base(); - - /* id */ - void set_id(sensor_id_t id); - sensor_id_t get_id(void); - - /* sensor info */ - virtual sensor_type_t get_type(void); - virtual unsigned int get_event_type(void); - virtual const char* get_name(void); - virtual bool is_virtual(void); - - virtual bool get_sensor_info(sensor_info &info); - - /* set/get data */ - virtual int get_data(sensor_data_t **data, int *length); - int get_cache(sensor_data_t **data); - - virtual bool flush(void); - virtual int add_attribute(int client_id, int32_t attribute, int32_t value); - virtual int add_attribute(int client_id, int32_t attribute, char *value, int value_size); - virtual bool delete_attribute(int client_id); - - /* start/stop */ - bool start(void); - bool stop(void); - bool is_started(void); - - /* interval / batch */ - virtual bool add_interval(int client_id, unsigned int interval, bool is_processor); - virtual bool delete_interval(int client_id, bool is_processor); - unsigned int get_interval(int client_id, bool is_processor); - - virtual bool add_batch(int client_id, unsigned int latency); - virtual bool delete_batch(int client_id); - unsigned int get_batch(int client_id); - - bool push(sensor_event_t *event); - - /* permission(privilege) */ - int get_permission(void); - -protected: - sensor_data_t *m_last_data; - - void set_permission(int permission); - - unsigned long long get_timestamp(void); - unsigned long long get_timestamp(timeval *t); - -private: - sensor_id_t m_id; - int m_permission; - - sensor_info_list m_sensor_info_list; - cmutex m_sensor_info_list_mutex; - - bool m_started; - unsigned int m_client; - cmutex m_client_mutex; - - cmutex m_data_cache_mutex; - - virtual int set_attribute(int32_t attribute, int32_t value); - virtual int set_attribute(int32_t attribute, char *value, int value_size); - - virtual bool set_interval(unsigned long interval); - virtual bool set_batch_latency(unsigned long latency); - - virtual bool pre_start(void); - virtual bool on_start(void); - virtual bool on_stop(void); - - void set_cache(sensor_data_t *data); -}; - -#endif /* _SENSOR_BASE_H_ */ diff --git a/src/server/sensor_event_dispatcher.cpp b/src/server/sensor_event_dispatcher.cpp deleted file mode 100644 index eeb4236..0000000 --- a/src/server/sensor_event_dispatcher.cpp +++ /dev/null @@ -1,311 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2013 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 -#include -#include -#include -#include -#include - -using std::thread; -using std::vector; -using std::pair; - -#define MAX_PENDING_CONNECTION 32 - -sensor_event_dispatcher::sensor_event_dispatcher() -: m_running(false) -{ -} - -sensor_event_dispatcher::~sensor_event_dispatcher() -{ -} - -sensor_event_dispatcher& sensor_event_dispatcher::get_instance(void) -{ - static sensor_event_dispatcher inst; - return inst; -} - -bool sensor_event_dispatcher::run(void) -{ - m_running = true; - - thread dispatcher(&sensor_event_dispatcher::dispatch_event, this); - dispatcher.detach(); - - return true; -} - -bool sensor_event_dispatcher::stop(void) -{ - m_running = false; - - return true; -} - -void sensor_event_dispatcher::accept_event_channel(csocket client_socket) -{ - int client_id; - channel_ready_t event_channel_ready; - client_info_manager& client_info_manager = get_client_info_manager(); - - client_socket.set_connection_mode(); - - if (client_socket.recv(&client_id, sizeof(client_id)) <= 0) { - _E("Failed to receive client id on socket fd[%d]", client_socket.get_socket_fd()); - return; - } - - client_socket.set_transfer_mode(); - - if (!get_client_info_manager().set_event_socket(client_id, client_socket)) { - _E("Failed to store event socket[%d] for %s", client_socket.get_socket_fd(), - client_info_manager.get_client_info(client_id)); - return; - } - - event_channel_ready.magic = CHANNEL_MAGIC_NUM; - event_channel_ready.client_id = client_id; - - _I("Event channel is accepted for %s on socket[%d]", - client_info_manager.get_client_info(client_id), client_socket.get_socket_fd()); - - if (client_socket.send(&event_channel_ready, sizeof(event_channel_ready)) <= 0) { - _E("Failed to send event_channel_ready packet to %s on socket fd[%d]", - client_info_manager.get_client_info(client_id), client_socket.get_socket_fd()); - return; - } -} - -void sensor_event_dispatcher::accept_event_connections(csocket client_socket) -{ - thread event_channel_creator(&sensor_event_dispatcher::accept_event_channel, this, client_socket); - event_channel_creator.detach(); -} - -void sensor_event_dispatcher::dispatch_event(void) -{ - const int MAX_SYNTH_PER_SENSOR = 5; - - vector v_sensor_events(MAX_SYNTH_PER_SENSOR); - - _I("Event Dispatcher started"); - - while (m_running) { - void *seed_event = get_event_queue().pop(); - - vector sensor_events; - sensor_events.push_back(seed_event); - - virtual_sensors v_sensors = get_active_virtual_sensors(); - - auto it_v_sensor = v_sensors.begin(); - - while (it_v_sensor != v_sensors.end()) { - int synthesized_cnt; - v_sensor_events.clear(); - (*it_v_sensor)->synthesize(*((sensor_event_t *)seed_event)); - synthesized_cnt = v_sensor_events.size(); - - for (int i = 0; i < synthesized_cnt; ++i) { - sensor_event_t *v_event = (sensor_event_t*)malloc(sizeof(sensor_event_t)); - if (!v_event) { - _E("Failed to allocate memory"); - continue; - } - - memcpy(v_event, &v_sensor_events[i], sizeof(sensor_event_t)); - sensor_events.push_back(v_event); - } - - ++it_v_sensor; - } - - sort_sensor_events(sensor_events); - - for (unsigned int i = 0; i < sensor_events.size(); ++i) { - if (is_record_event(((sensor_event_t *)(sensor_events[i]))->event_type)) - put_last_event(((sensor_event_t *)(sensor_events[i]))->event_type, *((sensor_event_t *)(sensor_events[i]))); - } - - send_sensor_events(sensor_events); - } -} - -void sensor_event_dispatcher::send_sensor_events(vector &events) -{ - sensor_event_t *sensor_event = NULL; - client_info_manager& client_info_manager = get_client_info_manager(); - - const int RESERVED_CLIENT_CNT = 20; - static client_id_vec id_vec(RESERVED_CLIENT_CNT); - - for (unsigned int i = 0; i < events.size(); ++i) { - sensor_id_t sensor_id; - unsigned int event_type; - - sensor_event = (sensor_event_t*)events[i]; - sensor_id = sensor_event->sensor_id; - event_type = sensor_event->event_type; - - id_vec.clear(); - client_info_manager.get_listener_ids(sensor_id, event_type, id_vec); - - auto it_client_id = id_vec.begin(); - - while (it_client_id != id_vec.end()) { - csocket client_socket; - client_info_manager.get_event_socket(*it_client_id, client_socket); - bool ret = (client_socket.send(sensor_event, sizeof(sensor_event_t)) > 0); - - ret = (ret & (client_socket.send(sensor_event->data, sensor_event->data_length) > 0)); - - if (!ret) - _E("Failed to send event[%#x] to %s on socket[%d]", event_type, client_info_manager.get_client_info(*it_client_id), client_socket.get_socket_fd()); - - ++it_client_id; - } - - free(sensor_event->data); - free(sensor_event); - } -} - -client_info_manager& sensor_event_dispatcher::get_client_info_manager(void) -{ - return client_info_manager::get_instance(); -} - -sensor_event_queue& sensor_event_dispatcher::get_event_queue(void) -{ - return sensor_event_queue::get_instance(); -} - -bool sensor_event_dispatcher::is_record_event(unsigned int event_type) -{ - return false; -} - -void sensor_event_dispatcher::put_last_event(unsigned int event_type, const sensor_event_t &event) -{ - AUTOLOCK(m_last_events_mutex); - m_last_events[event_type] = event; -} - -bool sensor_event_dispatcher::get_last_event(unsigned int event_type, sensor_event_t &event) -{ - AUTOLOCK(m_last_events_mutex); - - auto it_event = m_last_events.find(event_type); - - if (it_event == m_last_events.end()) - return false; - - event = it_event->second; - return true; -} - -bool sensor_event_dispatcher::has_active_virtual_sensor(virtual_sensor *sensor) -{ - AUTOLOCK(m_active_virtual_sensors_mutex); - - auto it_v_sensor = find(m_active_virtual_sensors.begin(), m_active_virtual_sensors.end(), sensor); - - return (it_v_sensor != m_active_virtual_sensors.end()); -} - -virtual_sensors sensor_event_dispatcher::get_active_virtual_sensors(void) -{ - AUTOLOCK(m_active_virtual_sensors_mutex); - - return m_active_virtual_sensors; -} - -struct sort_comp { - bool operator()(const void *left, const void *right) { - return ((sensor_event_t *)left)->data->timestamp < ((sensor_event_t *)right)->data->timestamp; - } -}; - -void sensor_event_dispatcher::sort_sensor_events(vector &events) -{ - std::sort(events.begin(), events.end(), sort_comp()); -} - -void sensor_event_dispatcher::request_last_event(int client_id, sensor_id_t sensor_id) -{ - client_info_manager& client_info_manager = get_client_info_manager(); - event_type_vector event_vec; - csocket client_socket; - - if (client_info_manager.get_registered_events(client_id, sensor_id, event_vec)) { - if (!client_info_manager.get_event_socket(client_id, client_socket)) { - _E("Failed to get event socket from %s", - client_info_manager.get_client_info(client_id)); - return; - } - - auto it_event = event_vec.begin(); - while (it_event != event_vec.end()) { - sensor_event_t event; - if (is_record_event(*it_event) && get_last_event(*it_event, event)) { - if (client_socket.send(&event, sizeof(event)) > 0) - _I("Send the last event[%#x] to %s on socket[%d]", event.event_type, - client_info_manager.get_client_info(client_id), client_socket.get_socket_fd()); - else - _E("Failed to send event[%#x] to %s on socket[%d]", event.event_type, - client_info_manager.get_client_info(client_id), client_socket.get_socket_fd()); - } - ++it_event; - } - } -} - -bool sensor_event_dispatcher::add_active_virtual_sensor(virtual_sensor *sensor) -{ - AUTOLOCK(m_active_virtual_sensors_mutex); - - if (!m_running) - return true; - - if (has_active_virtual_sensor(sensor)) { - _E("[%s] sensor is already added on active virtual sensors", sensor->get_name()); - return false; - } - - m_active_virtual_sensors.push_back(sensor); - - return true; -} - -bool sensor_event_dispatcher::delete_active_virtual_sensor(virtual_sensor *sensor) -{ - AUTOLOCK(m_active_virtual_sensors_mutex); - - if (!m_running) - return true; - - if (sensor) - m_active_virtual_sensors.remove(sensor); - - return true; -} diff --git a/src/server/sensor_event_dispatcher.h b/src/server/sensor_event_dispatcher.h deleted file mode 100644 index 6d79e44..0000000 --- a/src/server/sensor_event_dispatcher.h +++ /dev/null @@ -1,79 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2013 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_EVENT_DISPATCHER_H_ -#define _SENSOR_EVENT_DISPATCHER_H_ - -#include -#include -#include -#include -#include -#include -#include -#include - -typedef std::unordered_map event_type_last_event_map; -typedef std::list virtual_sensors; - -class sensor_event_dispatcher { -public: - static sensor_event_dispatcher& get_instance(void); - - bool run(void); - bool stop(void); - void accept_event_connections(csocket client_socket); - - void request_last_event(int client_id, sensor_id_t sensor_id); - - bool add_active_virtual_sensor(virtual_sensor *sensor); - bool delete_active_virtual_sensor(virtual_sensor *sensor); - -private: - csocket m_accept_socket; - cmutex m_mutex; - cmutex m_last_events_mutex; - event_type_last_event_map m_last_events; - virtual_sensors m_active_virtual_sensors; - cmutex m_active_virtual_sensors_mutex; - bool m_running; - - sensor_event_dispatcher(); - ~sensor_event_dispatcher(); - sensor_event_dispatcher(sensor_event_dispatcher const&) {}; - sensor_event_dispatcher& operator=(sensor_event_dispatcher const&); - - void accept_event_channel(csocket client_socket); - - void dispatch_event(void); - void send_sensor_events(std::vector &events); - static client_info_manager& get_client_info_manager(void); - static sensor_event_queue& get_event_queue(void); - - bool is_record_event(unsigned int event_type); - void put_last_event(unsigned int event_type, const sensor_event_t &event); - bool get_last_event(unsigned int event_type, sensor_event_t &event); - - bool has_active_virtual_sensor(virtual_sensor *sensor); - virtual_sensors get_active_virtual_sensors(void); - - void sort_sensor_events(std::vector &events); -}; - -#endif /* _SENSOR_EVENT_DISPATCHER_H_ */ diff --git a/src/server/sensor_event_poller.cpp b/src/server/sensor_event_poller.cpp deleted file mode 100644 index fa23e6f..0000000 --- a/src/server/sensor_event_poller.cpp +++ /dev/null @@ -1,188 +0,0 @@ -/* - * sensord - * - * 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 -#include -#include -#include -#include -#include -#include -#include - -sensor_event_poller::sensor_event_poller() -{ - init_sensor_map(); - init_fd(); - init_signal_fd(); -} - -sensor_event_poller::~sensor_event_poller() -{ - fd_sensors_t::iterator it; - for (it = m_fd_sensors.begin(); it != m_fd_sensors.end(); it = m_fd_sensors.upper_bound(it->first)) - m_poller.del_fd(it->first); -} - -void sensor_event_poller::init_sensor_map(void) -{ - int fd; - physical_sensor *sensor; - - std::vector sensors; - sensors = sensor_loader::get_instance().get_sensors(ALL_SENSOR); - - std::vector::iterator it; - - for (it = sensors.begin(); it != sensors.end(); ++it) { - sensor = dynamic_cast(*it); - if (sensor == NULL) - continue; - - fd = sensor->get_poll_fd(); - - if (fd < 0) - continue; - - m_fd_sensors.insert(std::make_pair(fd, sensor)); - } -} - -void sensor_event_poller::init_fd(void) -{ - fd_sensors_t::iterator it; - for (it = m_fd_sensors.begin(); it != m_fd_sensors.end(); it = m_fd_sensors.upper_bound(it->first)) { - /* if fd is not valid, it is not added to poller */ - add_poll_fd(it->first); - } -} - -void sensor_event_poller::init_signal_fd(void) -{ - int sfd; - sigset_t mask; - - sigemptyset(&mask); - sigaddset(&mask, SIGTERM); - sigaddset(&mask, SIGABRT); - sigaddset(&mask, SIGINT); - - sfd = signalfd(-1, &mask, 0); - m_poller.add_signal_fd(sfd); -} - -bool sensor_event_poller::add_poll_fd(int fd) -{ - return m_poller.add_fd(fd); -} - -bool sensor_event_poller::poll(void) -{ - std::vector ids; - while (true) { - int fd; - struct epoll_event poll_event; - - if (!m_poller.poll(poll_event)) - return false; - - fd = poll_event.data.fd; - ids.clear(); - - if (!read_fd(fd, ids)) - continue; - - if (!process_event(fd, ids)) - continue; - } -} - -bool sensor_event_poller::read_fd(int fd, std::vector &ids) -{ - fd_sensors_t::iterator it; - physical_sensor *sensor; - - it = m_fd_sensors.find(fd); - sensor = dynamic_cast(it->second); - - if (!sensor) { - _E("Failed to get sensor"); - return false; - } - - if (!sensor->read_fd(ids)) - return false; - - return true; -} - -bool sensor_event_poller::process_event(int fd, const std::vector &ids) -{ - physical_sensor *sensor; - std::pair ret; - - /* find sensors which is based on same device(fd) */ - ret = m_fd_sensors.equal_range(fd); - - for (auto it_sensor = ret.first; it_sensor != ret.second; ++it_sensor) { - sensor_event_t *event; - sensor_data_t *data; - int data_length; - int remains = 1; - - sensor = it_sensor->second; - - /* check whether the id of this sensor is in id list(parameter) or not */ - auto result = std::find(std::begin(ids), std::end(ids), sensor->get_hal_id()); - - if (result == std::end(ids)) - continue; - - while (remains > 0) { - remains = sensor->get_data(&data, &data_length); - if (remains < 0) { - _E("Failed to get sensor data"); - break; - } - - if (!sensor->on_event(data, data_length, remains)) { - free(data); - continue; - } - - event = (sensor_event_t *)malloc(sizeof(sensor_event_t)); - if (!event) { - _E("Memory allocation failed"); - break; - } - - event->sensor_id = sensor->get_id(); - event->event_type = sensor->get_event_type(); - event->data_length = data_length; - event->data = data; - - if (!sensor->push(event)) { - free(event); - free(data); - } - } - } - - return true; -} diff --git a/src/server/sensor_event_poller.h b/src/server/sensor_event_poller.h deleted file mode 100644 index c6c9ade..0000000 --- a/src/server/sensor_event_poller.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - * sensord - * - * 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. - * - */ - -#ifndef _SENSOR_EVENT_POLLER_H_ -#define _SENSOR_EVENT_POLLER_H_ - -#include -#include -#include - -typedef std::multimap fd_sensors_t; - -class sensor_event_poller { -public: - sensor_event_poller(); - virtual ~sensor_event_poller(); - - bool poll(void); - -private: - poller m_poller; - fd_sensors_t m_fd_sensors; - -private: - void init_sensor_map(void); - void init_fd(void); - void init_signal_fd(void); - - bool add_poll_fd(int fd); - bool read_fd(int fd, std::vector &ids); - bool process_event(int fd, const std::vector &ids); -}; - -#endif /* _SENSOR_EVENT_POLLER_H_ */ diff --git a/src/server/sensor_event_queue.cpp b/src/server/sensor_event_queue.cpp deleted file mode 100644 index d143d49..0000000 --- a/src/server/sensor_event_queue.cpp +++ /dev/null @@ -1,60 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2013 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 -#include - -sensor_event_queue& sensor_event_queue::get_instance(void) -{ - static sensor_event_queue inst; - return inst; -} - -void sensor_event_queue::push_internal(void *event) -{ - lock l(m_mutex); - bool wake = m_queue.empty(); - - if (m_queue.size() >= QUEUE_FULL_SIZE) { - _E("Queue is full, drop it!"); - free(event); - } else { - m_queue.push(event); - } - - if (wake) - m_cond_var.notify_one(); -} - -void* sensor_event_queue::pop(void) -{ - ulock u(m_mutex); - while (m_queue.empty()) - m_cond_var.wait(u); - - void *event = m_queue.front(); - m_queue.pop(); - - return event; -} - -void sensor_event_queue::push(sensor_event_t *event) -{ - push_internal(event); -} diff --git a/src/server/sensor_event_queue.h b/src/server/sensor_event_queue.h deleted file mode 100644 index 4f776ef..0000000 --- a/src/server/sensor_event_queue.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2013 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_EVENT_QUEUE_H_ -#define _SENSOR_EVENT_QUEUE_H_ - -#include -#include -#include -#include - -class sensor_event_queue { -public: - static sensor_event_queue& get_instance(void); - - void push(sensor_event_t *event); - void* pop(void); - -private: - static const unsigned int QUEUE_FULL_SIZE = 1000; - - std::queue m_queue; - std::mutex m_mutex; - std::condition_variable m_cond_var; - - typedef std::lock_guard lock; - typedef std::unique_lock ulock; - - sensor_event_queue() {} - ~sensor_event_queue() {} - sensor_event_queue(const sensor_event_queue &) {} - sensor_event_queue& operator=(const sensor_event_queue &); - void push_internal(void *event); -}; - -#endif /* _SENSOR_EVENT_QUEUE_H_*/ diff --git a/src/server/sensor_fusion.cpp b/src/server/sensor_fusion.cpp deleted file mode 100644 index c82e152..0000000 --- a/src/server/sensor_fusion.cpp +++ /dev/null @@ -1,104 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2014 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 - -sensor_fusion::sensor_fusion() -{ -} - -sensor_fusion::~sensor_fusion() -{ -} - -bool sensor_fusion::is_fusion(void) -{ - return true; -} - -bool sensor_fusion::is_virtual(void) -{ - return false; -} - -bool sensor_fusion::is_data_ready(void) -{ - return true; -} - -void sensor_fusion::clear_data(void) -{ - return; -} - -bool sensor_fusion::get_rotation_matrix(arr33_t &rot, int &accuracy) -{ - return false; -} - -bool sensor_fusion::get_attitude(float &x, float &y, float &z, float &w) -{ - return false; -} - -bool sensor_fusion::get_gyro_bias(float &x, float &y, float &z) -{ - return false; -} - -bool sensor_fusion::get_rotation_vector(float &x, float &y, float &z, float &w, float &heading_accuracy, int &accuracy) -{ - return false; -} - -bool sensor_fusion::get_linear_acceleration(float &x, float &y, float &z, int &accuracy) -{ - return false; -} - -bool sensor_fusion::get_gravity(float &x, float &y, float &z, int &accuracy) -{ - return false; -} - -bool sensor_fusion::get_rotation_vector_6axis(float &x, float &y, float &z, float &w, float &heading_accuracy, int &accuracy) -{ - return false; -} - -bool sensor_fusion::get_geomagnetic_rotation_vector(float &x, float &y, float &z, float &w, int &accuracy) -{ - return false; -} - -bool sensor_fusion::get_orientation(float &azimuth, float &pitch, float &roll, int &accuracy) -{ - return false; -} - -bool sensor_fusion::set_interval(unsigned int event_type, int client_id, unsigned int interval) -{ - return sensor_base::add_interval(client_id, interval, true); -} - -bool sensor_fusion::unset_interval(unsigned int event_type, int client_id) -{ - return sensor_base::delete_interval(client_id, true); -} - diff --git a/src/server/sensor_fusion.h b/src/server/sensor_fusion.h deleted file mode 100644 index c8a8a11..0000000 --- a/src/server/sensor_fusion.h +++ /dev/null @@ -1,59 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2014 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_FUSION_H_ -#define _SENSOR_FUSION_H_ - -#include -#include - -enum fusion_event_type { - FUSION_EVENT_AG = (FUSION_SENSOR << 16) | 0x0001, - FUSION_EVENT_AM = (FUSION_SENSOR << 16) | 0x0002, - FUSION_EVENT_AGM = (FUSION_SENSOR << 16) | 0x0004, -}; - -typedef std::array, 3> arr33_t; - -class sensor_fusion : public sensor_base { -public: - sensor_fusion(); - virtual ~sensor_fusion(); - - virtual void fuse(const sensor_event_t& event) = 0; - bool is_fusion(void); - bool is_virtual(void); - virtual bool is_data_ready(void); - virtual void clear_data(void); - virtual bool set_interval(unsigned int event_type, int client_id, unsigned int interval); - virtual bool unset_interval(unsigned int event_type, int client_id); - virtual unsigned long long get_data_timestamp(void) = 0; - - virtual bool get_rotation_matrix(arr33_t &rot, int &accuracy); - virtual bool get_attitude(float &x, float &y, float &z, float &w); - virtual bool get_gyro_bias(float &x, float &y, float &z); - virtual bool get_rotation_vector(float &x, float &y, float &z, float &w, float &heading_accuracy, int &accuracy); - virtual bool get_linear_acceleration(float &x, float &y, float &z, int &accuracy); - virtual bool get_gravity(float &x, float &y, float &z, int &accuracy); - virtual bool get_rotation_vector_6axis(float &x, float &y, float &z, float &w, float &heading_accuracy, int &accuracy); - virtual bool get_geomagnetic_rotation_vector(float &x, float &y, float &z, float &w, int &accuracy); - virtual bool get_orientation(float &azimuth, float &pitch, float &roll, int &accuracy); -}; - -#endif /* _SENSOR_FUSION_H_ */ diff --git a/src/server/sensor_info_list.cpp b/src/server/sensor_info_list.cpp deleted file mode 100644 index 8cbc879..0000000 --- a/src/server/sensor_info_list.cpp +++ /dev/null @@ -1,161 +0,0 @@ -/* - * sensord - * - * 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 -#include - -interval_info::interval_info(int client_id, bool is_processor, unsigned int interval) -{ - this->client_id = client_id; - this->is_processor = is_processor; - this->interval = interval; -} - -batch_info::batch_info(int client_id, unsigned int latency) -{ - this->client_id = client_id; - this->latency = latency; -} - -bool sensor_info_list::comp_interval_info(interval_info a, interval_info b) -{ - return a.interval < b.interval; -} - -bool sensor_info_list::comp_batch_info(batch_info a, batch_info b) -{ - return a.latency < b.latency; -} - -interval_info_iterator sensor_info_list::find_if_interval_info(int client_id, bool is_processor) -{ - auto iter = m_interval_info_list.begin(); - - while (iter != m_interval_info_list.end()) { - if ((iter->client_id == client_id) && (iter->is_processor == is_processor)) - break; - - ++iter; - } - - return iter; -} - -batch_info_iterator sensor_info_list::find_if_batch_info(int client_id) -{ - auto iter = m_batch_info_list.begin(); - - while (iter != m_batch_info_list.end()) { - if ((iter->client_id == client_id)) - break; - - ++iter; - } - - return iter; -} - -bool sensor_info_list::add_interval(int client_id, unsigned int interval, bool is_processor) -{ - auto iter = find_if_interval_info(client_id, is_processor); - - if (iter != m_interval_info_list.end()) - *iter = interval_info(client_id, is_processor, interval); - else - m_interval_info_list.push_back(interval_info(client_id, is_processor, interval)); - - return true; -} - -bool sensor_info_list::delete_interval(int client_id, bool is_processor) -{ - auto iter = find_if_interval_info(client_id, is_processor); - - if (iter == m_interval_info_list.end()) - return false; - - m_interval_info_list.erase(iter); - - return true; -} - -unsigned int sensor_info_list::get_interval(int client_id, bool is_processor) -{ - auto iter = find_if_interval_info(client_id, is_processor); - - if (iter == m_interval_info_list.end()) - return 0; - - return iter->interval; -} - -unsigned int sensor_info_list::get_min_interval(void) -{ - if (m_interval_info_list.empty()) - return 0; - - auto iter = min_element(m_interval_info_list.begin(), m_interval_info_list.end(), comp_interval_info); - - return iter->interval; -} - -bool sensor_info_list::add_batch(int client_id, unsigned int latency) -{ - auto iter = find_if_batch_info(client_id); - - if (iter != m_batch_info_list.end()) - *iter = batch_info(client_id, latency); - else - m_batch_info_list.push_back(batch_info(client_id, latency)); - - return true; -} - -bool sensor_info_list::delete_batch(int client_id) -{ - auto iter = find_if_batch_info(client_id); - - if (iter == m_batch_info_list.end()) - return false; - - m_batch_info_list.erase(iter); - - return true; -} - -unsigned int sensor_info_list::get_batch(int client_id) -{ - auto iter = find_if_batch_info(client_id); - - if (iter == m_batch_info_list.end()) - return 0; - - return iter->latency; -} - -unsigned int sensor_info_list::get_max_batch(void) -{ - if (m_batch_info_list.empty()) - return 0; - - auto iter = max_element(m_batch_info_list.begin(), m_batch_info_list.end(), comp_batch_info); - - return iter->latency; -} - diff --git a/src/server/sensor_info_list.h b/src/server/sensor_info_list.h deleted file mode 100644 index 7e90743..0000000 --- a/src/server/sensor_info_list.h +++ /dev/null @@ -1,67 +0,0 @@ -/* - * sensord - * - * 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. - * - */ - -#ifndef _SENSOR_INFO_LIST_H_ -#define _SENSOR_INFO_LIST_H_ - -#include - -class interval_info { -public: - interval_info(int client_id, bool is_processor, unsigned int interval); - int client_id; - bool is_processor; - unsigned int interval; -}; - -typedef std::list::iterator interval_info_iterator; - -class batch_info { -public: - batch_info(int client_id, unsigned int latency); - int client_id; - unsigned int latency; -}; - -typedef std::list::iterator batch_info_iterator; - -class sensor_info_list { -public: - bool add_interval(int client_id, unsigned int interval, bool is_processor); - bool delete_interval(int client_id, bool is_processor); - unsigned int get_interval(int client_id, bool is_processor); - unsigned int get_min_interval(void); - - bool add_batch(int client_id, unsigned int latency); - bool delete_batch(int client_id); - unsigned int get_batch(int client_id); - unsigned int get_max_batch(void); - -private: - static bool comp_interval_info(interval_info a, interval_info b); - interval_info_iterator find_if_interval_info(int client_id, bool is_processor); - - static bool comp_batch_info(batch_info a, batch_info b); - batch_info_iterator find_if_batch_info(int client_id); - - std::list m_interval_info_list; - std::list m_batch_info_list; -}; - -#endif /* _SENSOR_INFO_LIST_H_ */ diff --git a/src/server/sensor_loader.cpp b/src/server/sensor_loader.cpp index 614e057..7676f40 100644 --- a/src/server/sensor_loader.cpp +++ b/src/server/sensor_loader.cpp @@ -17,50 +17,7 @@ * */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#ifdef ENABLE_AUTO_ROTATION -#include -#endif -#ifdef ENABLE_GRAVITY -#include -#endif -#ifdef ENABLE_LINEAR_ACCEL -#include -#endif -#ifdef ENABLE_ORIENTATION -#include -#endif -#ifdef ENABLE_ROTATION_VECTOR -#include -#include -#include -#endif -#ifdef ENABLE_FACE_DOWN -#include -#endif -#ifdef ENABLE_SENSORHUB -#include -#endif - -using std::vector; -using std::string; - -#define DEVICE_HAL_DIR_PATH LIBDIR "/sensor" +#include "sensor_loader.h" sensor_loader::sensor_loader() { @@ -68,378 +25,4 @@ sensor_loader::sensor_loader() sensor_loader::~sensor_loader() { - sensor_device_map_t::iterator it_device; - std::vector::iterator it_handle; - - for (it_device = m_active_devices.begin(); it_device != m_active_devices.end();) - it_device = m_active_devices.erase(it_device); - - for (it_handle = m_handles.begin(); it_handle != m_handles.end(); ++it_handle) - dlclose(*it_handle); - - m_handles.clear(); -} - -sensor_loader& sensor_loader::get_instance(void) -{ - static sensor_loader inst; - return inst; -} - -bool sensor_loader::load(void) -{ - std::vector device_hal_paths; - std::vector unique_device_hal_paths; - - get_paths_from_dir(string(DEVICE_HAL_DIR_PATH), device_hal_paths); - - std::unordered_set s; - auto unique = [&s](vector &paths, const string &path) { - if (s.insert(path).second) - paths.push_back(path); - }; - - for_each(device_hal_paths.begin(), device_hal_paths.end(), - [&](const string &path) { - unique(unique_device_hal_paths, path); - } - ); - - for_each(unique_device_hal_paths.begin(), unique_device_hal_paths.end(), - [&](const string &path) { - void *handle; - if (load_sensor_devices(path, handle)) - m_handles.push_back(handle); - } - ); - - create_sensors(); - show_sensor_info(); - - return true; -} - -bool sensor_loader::load_sensor_devices(const string &path, void* &handle) -{ - sensor_device_t *_devices = NULL; - sensor_device *device = NULL; - const sensor_info_t *infos; - - _I("load device: [%s]", path.c_str()); - - void *_handle = dlopen(path.c_str(), RTLD_NOW); - if (!_handle) { - _E("Failed to dlopen(%s), dlerror : %s", path.c_str(), dlerror()); - return false; - } - - dlerror(); - - /* TODO: The out-param of the create function should be const */ - create_t create_devices = (create_t) dlsym(_handle, "create"); - if (!create_devices) { - _E("Failed to find symbols in %s", path.c_str()); - dlclose(_handle); - return false; - } - - int device_size = create_devices(&_devices); - if (!_devices) { - _E("Failed to create devices, path is %s\n", path.c_str()); - dlclose(_handle); - return false; - } - - for (int i = 0; i < device_size; ++i) { - device = static_cast(_devices[i]); - std::shared_ptr device_ptr(device); - - int info_size = device_ptr->get_sensors(&infos); - for (int j = 0; j < info_size; ++j) - m_devices[&infos[j]] = device_ptr; - } - - handle = _handle; - - return true; -} - -void sensor_loader::create_sensors(void) -{ - /* HRM sensors need SENSOR_PERMISSION_BIO */ - create_physical_sensors(HRM_RAW_SENSOR); - create_physical_sensors(HRM_SENSOR); - create_physical_sensors(HRM_LED_GREEN_SENSOR); - create_physical_sensors(HRM_LED_IR_SENSOR); - create_physical_sensors(HRM_LED_RED_SENSOR); -#ifdef ENABLE_SENSORHUB - create_physical_sensors(HUMAN_PEDOMETER_SENSOR); -#endif - - create_physical_sensors(UNKNOWN_SENSOR); - -#ifdef ENABLE_AUTO_ROTATION - create_virtual_sensors("Auto Rotation"); -#endif -#ifdef ENABLE_ROTATION_VECTOR - create_virtual_sensors("Rotation Vector"); - create_virtual_sensors("Magnetic Rotation Vector"); - create_virtual_sensors("Gyroscope Rotation Vector"); -#endif -#ifdef ENABLE_GRAVITY - create_virtual_sensors("Gravity"); -#endif -#ifdef ENABLE_LINEAR_ACCEL - create_virtual_sensors("Linear Accel"); -#endif -#ifdef ENABLE_ORIENTATION - create_virtual_sensors("Orientation"); -#endif -#ifdef ENABLE_FACE_DOWN - create_virtual_sensors("Face Down"); -#endif -} - -template -void sensor_loader::create_physical_sensors(sensor_type_t type) -{ - int32_t index; - const sensor_info_t *info; - physical_sensor *sensor; - sensor_device *device; - - sensor_device_map_t::iterator it; - - for (it = m_devices.begin(); it != m_devices.end(); ++it) { - info = it->first; - device = it->second.get(); - - if (type != UNKNOWN_SENSOR) { - if (type != (sensor_type_t)(info->type)) - continue; - } - - sensor = dynamic_cast(create_sensor<_sensor>()); - - if (!sensor) { - _E("Memory allocation failed[%s]", info->name); - return; - } - - sensor_type_t _type = (sensor_type_t)info->type; - index = (int32_t)m_sensors.count(_type); - - sensor->set_id(((int64_t)_type << SENSOR_TYPE_SHIFT) | index); - sensor->set_sensor_info(info); - sensor->set_sensor_device(device); - - std::shared_ptr sensor_ptr(sensor); - m_sensors.insert(std::make_pair(_type, sensor_ptr)); - - m_active_devices[it->first] = it->second; - m_devices.erase(it->first); - - _I("created [%s] sensor", sensor->get_name()); - } -} - -template -void sensor_loader::create_virtual_sensors(const char *name) -{ - int32_t index; - sensor_type_t type; - virtual_sensor *instance; - - instance = dynamic_cast(create_sensor<_sensor>()); - if (!instance) { - _E("Memory allocation failed[%s]", name); - return; - } - - if (!instance->init()) { - _W("Failed to init %s", name); - delete instance; - return; - } - - std::shared_ptr sensor(instance); - type = sensor->get_type(); - index = (int32_t)(m_sensors.count(type)); - - sensor->set_id((int64_t)type << SENSOR_TYPE_SHIFT | index); - - m_sensors.insert(std::make_pair(type, sensor)); - - _I("created [%s] sensor", sensor->get_name()); -} - -template -void sensor_loader::create_external_sensors(const char *name) -{ - int32_t index; - sensor_type_t type; - external_sensor *instance; - - instance = dynamic_cast(create_sensor<_sensor>()); - if (!instance) { - _E("Memory allocation failed[%s]", name); - return; - } - - std::shared_ptr sensor(instance); - type = sensor->get_type(); - index = (int32_t)(m_sensors.count(type)); - - sensor->set_id((int64_t)type << SENSOR_TYPE_SHIFT | index); - - m_sensors.insert(std::make_pair(type, sensor)); - - _I("created [%s] sensor", sensor->get_name()); -} - -template -sensor_base* sensor_loader::create_sensor(void) -{ - sensor_base *instance = NULL; - - try { - instance = new _sensor; - } catch (std::exception &e) { - _E("Failed to create sensor, exception: %s", e.what()); - return NULL; - } catch (int err) { - _ERRNO(errno, _E, "Failed to create sensor"); - return NULL; - } - - return instance; -} - -void sensor_loader::show_sensor_info(void) -{ - _I("========== Loaded sensor information ==========\n"); - - int index = 0; - - auto it = m_sensors.begin(); - - while (it != m_sensors.end()) { - sensor_base *sensor = it->second.get(); - - sensor_info info; - sensor->get_sensor_info(info); - _I("No:%d [%s]\n", ++index, sensor->get_name()); - info.show(); - it++; - } - - _I("===============================================\n"); -} - -bool sensor_loader::get_paths_from_dir(const string &dir_path, vector &hal_paths) -{ - DIR *dir = NULL; - struct dirent dir_entry; - struct dirent *result; - string name; - int ret; - - dir = opendir(dir_path.c_str()); - - if (!dir) { - _E("Failed to open dir: %s", dir_path.c_str()); - return false; - } - - while (true) { - ret = readdir_r(dir, &dir_entry, &result); - - if (ret != 0) - continue; - - if (result == NULL) - break; - - name = string(dir_entry.d_name); - - if (name == "." || name == "..") - continue; - - hal_paths.push_back(dir_path + "/" + name); - } - - closedir(dir); - return true; -} - -sensor_base* sensor_loader::get_sensor(sensor_type_t type) -{ - auto it = m_sensors.find(type); - - if (it == m_sensors.end()) - return NULL; - - return it->second.get(); -} - -sensor_base* sensor_loader::get_sensor(sensor_id_t id) -{ - vector sensors; - - sensor_type_t type = static_cast(id >> SENSOR_TYPE_SHIFT); - unsigned int index = (id & SENSOR_INDEX_MASK); - - sensors = get_sensors(type); - - if (index >= sensors.size()) - return NULL; - - return sensors[index]; -} - -vector sensor_loader::get_sensor_types(void) -{ - vector sensor_types; - - auto it = m_sensors.begin(); - - while (it != m_sensors.end()) { - sensor_types.push_back((sensor_type_t)(it->first)); - it = m_sensors.upper_bound(it->first); - } - - return sensor_types; -} - -vector sensor_loader::get_sensors(sensor_type_t type) -{ - vector sensor_list; - std::pair ret; - - if ((int)(type) == (int)SENSOR_DEVICE_ALL) - ret = std::make_pair(m_sensors.begin(), m_sensors.end()); - else - ret = m_sensors.equal_range(type); - - for (auto it = ret.first; it != ret.second; ++it) - sensor_list.push_back(it->second.get()); - - return sensor_list; -} - -vector sensor_loader::get_virtual_sensors(void) -{ - vector virtual_list; - sensor_base* sensor; - - for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) { - sensor = it->second.get(); - - if (!sensor || !sensor->is_virtual()) - continue; - - virtual_list.push_back(sensor); - } - - return virtual_list; } diff --git a/src/server/sensor_loader.h b/src/server/sensor_loader.h index 4d9f179..22efa62 100644 --- a/src/server/sensor_loader.h +++ b/src/server/sensor_loader.h @@ -20,54 +20,10 @@ #ifndef _SENSOR_LOADER_H_ #define _SENSOR_LOADER_H_ -#include -#include -#include - -#include -#include - -#include -#include -#include -#include -#include - -class sensor_base; - -typedef std::multimap> sensor_map_t; -typedef std::map> sensor_device_map_t; - class sensor_loader { private: sensor_loader(); virtual ~sensor_loader(); - - bool load_sensor_devices(const std::string &path, void* &handle); - - void create_sensors(void); - template void create_physical_sensors(sensor_type_t type); - template void create_virtual_sensors(const char *name); - template void create_external_sensors(const char *name); - template sensor_base* create_sensor(void); - - void show_sensor_info(void); - bool get_paths_from_dir(const std::string &dir_path, std::vector &hal_paths); - - sensor_map_t m_sensors; - sensor_device_map_t m_devices; - sensor_device_map_t m_active_devices; - std::vector m_handles; -public: - static sensor_loader& get_instance(void); - bool load(void); - - sensor_base* get_sensor(sensor_type_t type); - sensor_base* get_sensor(sensor_id_t id); - - std::vector get_sensor_types(void); - std::vector get_sensors(sensor_type_t type); - std::vector get_virtual_sensors(void); }; #endif /* _SENSOR_LOADER_H_ */ diff --git a/src/server/sensor_usage.cpp b/src/server/sensor_usage.cpp deleted file mode 100644 index e4f0ffc..0000000 --- a/src/server/sensor_usage.cpp +++ /dev/null @@ -1,75 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2013 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 -#include -#include -#include - -sensor_usage::sensor_usage() -: m_interval(POLL_1HZ_MS) -, m_latency(0) -, m_pause_policy(SENSORD_PAUSE_ALL) -, m_start(false) -{ -} - -sensor_usage::~sensor_usage() -{ - m_reg_events.clear(); -} - -bool sensor_usage::register_event(unsigned int event_type) -{ - auto it_event = find(m_reg_events.begin(), m_reg_events.end(), event_type); - - if (it_event != m_reg_events.end()) { - _E("Event[%#x] is already registered", event_type); - return false; - } - - m_reg_events.push_back(event_type); - return true; -} - -bool sensor_usage::unregister_event(unsigned int event_type) -{ - auto it_event = find(m_reg_events.begin(), m_reg_events.end(), event_type); - - if (it_event == m_reg_events.end()) { - _E("Event[%#x] is not found", event_type); - return false; - } - - m_reg_events.erase(it_event); - - return true; -} - -bool sensor_usage::is_event_registered(unsigned int event_type) -{ - auto it_event = find(m_reg_events.begin(), m_reg_events.end(), event_type); - - if (it_event == m_reg_events.end()) { - _D("Event[%#x] is not registered", event_type); - return false; - } - - return true; -} diff --git a/src/server/sensor_usage.h b/src/server/sensor_usage.h deleted file mode 100644 index e3fd247..0000000 --- a/src/server/sensor_usage.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2013 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_USAGE_H_ -#define _SENSOR_USAGE_H_ - -#include -#include - -typedef std::vector reg_event_vector; - -class sensor_usage { -public: - unsigned int m_interval; - unsigned int m_latency; - int m_pause_policy; - reg_event_vector m_reg_events; - bool m_start; - - sensor_usage(); - ~sensor_usage(); - - bool register_event(unsigned int event_type); - bool unregister_event(unsigned int event_type); - bool is_event_registered(unsigned int event_type); -}; - -#endif /* _SENSOR_USAGE_H_ */ diff --git a/src/server/server.cpp b/src/server/server.cpp index 3e30174..0c1e624 100644 --- a/src/server/server.cpp +++ b/src/server/server.cpp @@ -17,27 +17,9 @@ * */ -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define SYSTEMD_SOCKET_MAX 2 - -using std::thread; +#include "server.h" server::server() -: m_mainloop(NULL) -, m_running(false) { } @@ -45,317 +27,6 @@ server::~server() { } -int server::get_systemd_socket(const char *name) -{ - int type = SOCK_STREAM; - int listening = 1; - size_t length = 0; - int fd = -1; - int fd_env = 1; - int fd_index; - - if (!strcmp(name, EVENT_CHANNEL_PATH)) { - type = SOCK_SEQPACKET; - listening = -1; - fd_env = 0; - } - - if (sd_listen_fds(fd_env) < 0) { - _E("Failed to listen fds from systemd"); - return -1; - } - - for (fd_index = 0; fd_index < SYSTEMD_SOCKET_MAX; ++fd_index) { - fd = SD_LISTEN_FDS_START + fd_index; - - if (sd_is_socket_unix(fd, type, listening, name, length) > 0) - return fd; - } - - return -1; -} - -void server::accept_command_channel(void) -{ - _I("Command channel acceptor is started"); - - while (m_running) { - csocket client_command_socket; - - if (!m_command_channel_accept_socket.is_valid()) { - _E("Failed to accept, event_channel_accept_socket is closed"); - break; - } - - if (!m_command_channel_accept_socket.accept(client_command_socket)) { - _E("Failed to accept command channel from a client"); - continue; - } - - if (!m_running) { - _E("server die"); - break; - } - - _D("New client (socket_fd : %d) connected", client_command_socket.get_socket_fd()); - - /* TODO: if socket is closed, it should be erased */ - client_command_sockets.push_back(client_command_socket); - - thread worker_dispatcher(&server::dispatch_worker, this, client_command_socket); - worker_dispatcher.detach(); - } - - _I("Command channel acceptor is terminated"); -} - -void server::accept_event_channel(void) -{ - _I("Event channel acceptor is started!"); - - while (m_running) { - csocket client_event_socket; - - if (!m_event_channel_accept_socket.is_valid()) { - _E("Failed to accept, event_channel_accept_socket is closed"); - break; - } - - if (!m_event_channel_accept_socket.accept(client_event_socket)) { - _E("Failed to accept event channel from a client"); - continue; - } - - if (!m_running) { - _E("server die"); - break; - } - - /* TODO: if socket is closed, it should be erased */ - client_event_sockets.push_back(client_event_socket); - - _D("New client(socket_fd : %d) connected", client_event_socket.get_socket_fd()); - - thread event_channel_creator(&server::dispatch_event_channel_creator, this, client_event_socket); - event_channel_creator.detach(); - } - - _I("Event channel acceptor is terminated"); -} - -void server::dispatch_worker(csocket socket) -{ - int worker_type; - - if (socket.recv(&worker_type, sizeof(worker_type)) <= 0) { - _E("Failed to get worker type"); - socket.close(); - return; - } - - if (worker_type == CLIENT_TYPE_SENSOR_CLIENT) { - command_worker *worker; - worker = new(std::nothrow) command_worker(socket); - - if (!worker) { - _E("Failed to allocate memory"); - socket.close(); - return; - } - - if (!worker->start()) { - _E("Failed to start command worker"); - delete worker; - } - } else if (worker_type == CLIENT_TYPE_EXTERNAL_SOURCE) { - external_sensor_worker *worker; - worker = new(std::nothrow) external_sensor_worker(socket); - - if (!worker) { - _E("Failed to allocate memory"); - socket.close(); - return; - } - - if (!worker->start()) { - _E("Failed to start external worker"); - delete worker; - } - } else { - _E("Not supported worker type: %d", worker_type); - socket.close(); - } -} - -void server::dispatch_event_channel_creator(csocket socket) -{ - int client_type; - - if (socket.recv(&client_type, sizeof(client_type)) <= 0) { - _E("Failed to get client type"); - socket.close(); - return; - } - - if (client_type == CLIENT_TYPE_SENSOR_CLIENT) { - sensor_event_dispatcher::get_instance().accept_event_connections(socket); - } else if (client_type == CLIENT_TYPE_EXTERNAL_SOURCE) { - external_sensor_service::get_instance().accept_command_channel(socket); - } else { - _E("Not supported client type: %d", client_type); - socket.close(); - } -} - -void server::poll_event(void) -{ - _I("Event poller is started"); - - sensor_event_poller poller; - - if (!poller.poll()) { - _E("Failed to poll event"); - return; - } -} - -bool server::listen_command_channel(void) -{ - int sock_fd = -1; - const int MAX_PENDING_CONNECTION = 10; - - sock_fd = get_systemd_socket(COMMAND_CHANNEL_PATH); - - if (sock_fd >= 0) { - _I("Succeeded to get systemd socket(%d)", sock_fd); - m_command_channel_accept_socket = csocket(sock_fd); - return true; - } - - INFO("Failed to get systemd socket, create it by myself!"); - if (!m_command_channel_accept_socket.create(SOCK_STREAM)) { - _E("Failed to create command channel"); - return false; - } - - if (!m_command_channel_accept_socket.bind(COMMAND_CHANNEL_PATH)) { - _E("Failed to bind command channel"); - m_command_channel_accept_socket.close(); - return false; - } - - if (!m_command_channel_accept_socket.listen(MAX_PENDING_CONNECTION)) { - _E("Failed to listen command channel"); - return false; - } - - return true; -} - -bool server::listen_event_channel(void) -{ - int sock_fd = -1; - const int MAX_PENDING_CONNECTION = 32; - - sock_fd = get_systemd_socket(EVENT_CHANNEL_PATH); - - if (sock_fd >= 0) { - _I("Succeeded to get systemd socket(%d)", sock_fd); - m_event_channel_accept_socket = csocket(sock_fd); - return true; - } - - INFO("Failed to get systemd socket, create it by myself!"); - - if (!m_event_channel_accept_socket.create(SOCK_SEQPACKET)) { - _E("Failed to create event channel"); - return false; - } - - if (!m_event_channel_accept_socket.bind(EVENT_CHANNEL_PATH)) { - _E("Failed to bind event channel"); - m_event_channel_accept_socket.close(); - return false; - } - - if (!m_event_channel_accept_socket.listen(MAX_PENDING_CONNECTION)) { - _E("Failed to listen event channel"); - m_event_channel_accept_socket.close(); - return false; - } - - return true; -} - -void server::close_socket(void) -{ - m_command_channel_accept_socket.close(); - m_event_channel_accept_socket.close(); - - for (unsigned int i = 0; i < client_command_sockets.size(); ++i) - client_command_sockets[i].close(); - - for (unsigned int i = 0; i < client_event_sockets.size(); ++i) - client_event_sockets[i].close(); - - client_command_sockets.clear(); - client_event_sockets.clear(); -} - -void server::initialize(void) -{ - m_running = true; - m_mainloop = g_main_loop_new(NULL, false); - - sensor_event_dispatcher::get_instance().run(); - external_sensor_service::get_instance().run(); - - listen_command_channel(); - listen_event_channel(); - - std::thread event_channel_accepter(&server::accept_event_channel, this); - event_channel_accepter.detach(); - - std::thread command_channel_accepter(&server::accept_command_channel, this); - command_channel_accepter.detach(); - - std::thread event_poller(&server::poll_event, this); - event_poller.detach(); - - sd_notify(0, "READY=1"); - - g_main_loop_run(m_mainloop); -} - -void server::terminate(void) -{ - sensor_event_dispatcher::get_instance().stop(); - - close_socket(); -} - -void server::run(void) -{ - initialize(); - terminate(); -} - -void server::stop(void) -{ - if (!m_running) - return; - - m_running = false; - - if (m_mainloop) { - g_main_loop_quit(m_mainloop); - g_main_loop_unref(m_mainloop); - m_mainloop = NULL; - } - - _I("Sensord server stopped"); -} - server& server::get_instance(void) { static server inst; diff --git a/src/server/server.h b/src/server/server.h index 8f9daf7..017f461 100644 --- a/src/server/server.h +++ b/src/server/server.h @@ -20,51 +20,12 @@ #ifndef _SERVER_H_ #define _SERVER_H_ -#include -#include -#include -#include - class server { public: - static server& get_instance(void); - -public: - void run(void); - void stop(void); - -private: - GMainLoop *m_mainloop; - csocket m_command_channel_accept_socket; - csocket m_event_channel_accept_socket; - - std::vector client_command_sockets; - std::vector client_event_sockets; - - bool m_running; - -private: server(); virtual ~server(); - void initialize(void); - void terminate(void); - - void poll_event(void); - - bool listen_command_channel(void); - bool listen_event_channel(void); - - void accept_command_channel(void); - void accept_event_channel(void); - - void dispatch_worker(csocket socket); - void dispatch_event_channel_creator(csocket socket); - - void close_socket(void); - - /* TODO: move to socket class */ - int get_systemd_socket(const char *name); + static server& get_instance(void); }; #endif /* _SERVER_H_ */ diff --git a/src/server/virtual_sensor.cpp b/src/server/virtual_sensor.cpp index 0d297b3..59546b6 100644 --- a/src/server/virtual_sensor.cpp +++ b/src/server/virtual_sensor.cpp @@ -17,8 +17,7 @@ * */ -#include -#include +#include "virtual_sensor.h" virtual_sensor::virtual_sensor() { @@ -27,23 +26,3 @@ virtual_sensor::virtual_sensor() virtual_sensor::~virtual_sensor() { } - -bool virtual_sensor::init(void) -{ - return false; -} - -bool virtual_sensor::is_virtual(void) -{ - return true; -} - -bool virtual_sensor::activate(void) -{ - return sensor_event_dispatcher::get_instance().add_active_virtual_sensor(this); -} - -bool virtual_sensor::deactivate(void) -{ - return sensor_event_dispatcher::get_instance().delete_active_virtual_sensor(this); -} diff --git a/src/server/virtual_sensor.h b/src/server/virtual_sensor.h index a1d4363..745a0b4 100644 --- a/src/server/virtual_sensor.h +++ b/src/server/virtual_sensor.h @@ -20,40 +20,10 @@ #ifndef _VIRTUAL_SENSOR_H_ #define _VIRTUAL_SENSOR_H_ -#include - -class virtual_sensor : public sensor_base { +class virtual_sensor { public: virtual_sensor(); virtual ~virtual_sensor(); - - /* initialize sensor */ - virtual bool init(); - - /* module info */ - virtual sensor_type_t get_type() = 0; - virtual unsigned int get_event_type(void) = 0; - virtual const char* get_name(void) = 0; - - virtual bool get_sensor_info(sensor_info &info) = 0; - - /* synthesize event */ - virtual void synthesize(const sensor_event_t& event) = 0; - - /* get data */ - virtual int get_data(sensor_data_t **data, int *length) = 0; - - bool is_virtual(void); - -protected: - bool activate(void); - bool deactivate(void); - -private: - virtual bool set_interval(unsigned long interval) = 0; - virtual bool set_batch_latency(unsigned long latency) = 0; - virtual bool on_start(void) = 0; - virtual bool on_stop(void) = 0; }; #endif /* _VIRTUAL_SENSOR_H_ */ diff --git a/src/server/worker_thread.cpp b/src/server/worker_thread.cpp deleted file mode 100644 index 9108016..0000000 --- a/src/server/worker_thread.cpp +++ /dev/null @@ -1,226 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2014 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 -#include -#include - -using std::thread; - -worker_thread::worker_thread() -: m_state(WORKER_STATE_INITIAL) -, m_context(NULL) -, m_thread_created(false) -{ - for (int i = 0; i < TRANS_FUNC_CNT; ++i) - m_trans_func[i] = NULL; -} - -worker_thread::~worker_thread() -{ - stop(); -} - -bool worker_thread::transition_function(trans_func_index index) -{ - if (m_trans_func[index] != NULL) { - if (!m_trans_func[index](m_context)) { - _W("Transition[%d] function returning false", index); - return false; - } - } - - return true; -} - -worker_thread::worker_state_t worker_thread::get_state(void) -{ - lock l(m_mutex); - return m_state; -} - -bool worker_thread::start(void) -{ - lock l(m_mutex); - - if (m_state == WORKER_STATE_WORKING) { - _I("Worker thread is already working"); - return true; - } - - if ((m_state == WORKER_STATE_INITIAL) || (m_state == WORKER_STATE_STOPPED)) { - m_state = WORKER_STATE_WORKING; - - if (!m_thread_created) { - thread th(&worker_thread::main, this); - th.detach(); - } - return true; - } - - if (m_state == WORKER_STATE_PAUSED) { - m_state = WORKER_STATE_WORKING; - m_cond_working.notify_one(); - return true; - } - - _E("Failed to start, because current state(%d) is not for START", m_state); - - return false; -} - -bool worker_thread::stop(void) -{ - lock l(m_mutex); - - if (m_state == WORKER_STATE_STOPPED) { - _I("Worker thread is already stopped"); - return true; - } - - if ((m_state == WORKER_STATE_WORKING) || (m_state == WORKER_STATE_PAUSED)) { - if (m_state == WORKER_STATE_PAUSED) - m_cond_working.notify_one(); - - m_state = WORKER_STATE_STOPPED; - return true; - } - - _E("Failed to stop, because current state(%d) is not for STOP", m_state); - return false; -} - -bool worker_thread::pause(void) -{ - lock l(m_mutex); - - if (m_state == WORKER_STATE_PAUSED) { - _I("Worker thread is already paused"); - return true; - } - - if (m_state == WORKER_STATE_WORKING) { - m_state = WORKER_STATE_PAUSED; - return true; - } - - _E("Failed to pause, because current state(%d) is not for PAUSE", m_state); - - return false; -} - -bool worker_thread::resume(void) -{ - lock l(m_mutex); - - if (m_state == WORKER_STATE_WORKING) { - _I("Worker thread is already working"); - return true; - } - - if (m_state == WORKER_STATE_PAUSED) { - m_state = WORKER_STATE_WORKING; - m_cond_working.notify_one(); - return true; - } - - _E("Failed to resume, because current state(%d) is not for RESUME", m_state); - return false; -} - -/* - * After state changed to STOPPED, it should not access member fields, - because some transition funciton of STOPPED delete this pointer - */ - -void worker_thread::main(void) -{ - _D("Worker thread(%#x) is created", std::this_thread::get_id()); - - transition_function(STARTED); - - while (true) { - worker_state_t state; - state = get_state(); - - if (state == WORKER_STATE_WORKING) { - if (!transition_function(WORKING)) { - m_state = WORKER_STATE_STOPPED; - _D("Worker thread(%#x) exits from working state", std::this_thread::get_id()); - m_thread_created = false; - transition_function(STOPPED); - break; - } - continue; - } - - ulock u(m_mutex); - - if (m_state == WORKER_STATE_PAUSED) { - transition_function(PAUSED); - - _D("Worker thread(%#x) is paused", std::this_thread::get_id()); - m_cond_working.wait(u); - - if (m_state == WORKER_STATE_WORKING) { - transition_function(RESUMED); - _D("Worker thread(%#x) is resumed", std::this_thread::get_id()); - } else if (m_state == WORKER_STATE_STOPPED) { - m_thread_created = false; - transition_function(STOPPED); - break; - } - } else if (m_state == WORKER_STATE_STOPPED) { - m_thread_created = false; - transition_function(STOPPED); - break; - } - } - _I("Worker thread(%#x)'s main is terminated", std::this_thread::get_id()); -} - -void worker_thread::set_started(trans_func_t func) -{ - m_trans_func[STARTED] = func; -} - -void worker_thread::set_stopped(trans_func_t func) -{ - m_trans_func[STOPPED] = func; -} - -void worker_thread::set_paused(trans_func_t func) -{ - m_trans_func[PAUSED] = func; -} - -void worker_thread::set_resumed(trans_func_t func) -{ - m_trans_func[RESUMED] = func; -} - -void worker_thread::set_working(trans_func_t func) -{ - m_trans_func[WORKING] = func; -} - -void worker_thread::set_context(void *ctx) -{ - m_context = ctx; -} diff --git a/src/server/worker_thread.h b/src/server/worker_thread.h deleted file mode 100644 index 0aeff29..0000000 --- a/src/server/worker_thread.h +++ /dev/null @@ -1,79 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2014 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 _WORKER_THREAD_H_ -#define _WORKER_THREAD_H_ - -#include -#include - -class worker_thread { -public: - enum worker_state_t { - WORKER_STATE_INITIAL, - WORKER_STATE_WORKING, - WORKER_STATE_PAUSED, - WORKER_STATE_STOPPED, - }; - - typedef bool(*trans_func_t)(void *data); -private: - enum trans_func_index { - STARTED = 0, - STOPPED, - PAUSED, - RESUMED, - WORKING, - TRANS_FUNC_CNT, - }; - - typedef std::lock_guard lock; - typedef std::unique_lock ulock; - - worker_state_t m_state; - void *m_context; - std::mutex m_mutex; - std::condition_variable m_cond_working; - bool m_thread_created; - - trans_func_t m_trans_func[TRANS_FUNC_CNT]; - - bool transition_function(trans_func_index index); - void main(void); -public: - worker_thread(); - virtual ~worker_thread(); - - bool start(void); - bool stop(void); - bool pause(void); - bool resume(void); - - worker_state_t get_state(void); - - void set_started(trans_func_t func); - void set_stopped(trans_func_t func); - void set_paused(trans_func_t func); - void set_resumed(trans_func_t func); - void set_working(trans_func_t func); - - void set_context(void *ctx); -}; - -#endif /* _WORKER_THREAD_H_ */ diff --git a/src/shared/cbase_lock.cpp b/src/shared/cbase_lock.cpp deleted file mode 100644 index 38f2f84..0000000 --- a/src/shared/cbase_lock.cpp +++ /dev/null @@ -1,154 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2013 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 -#include -#include -#include -#include -#include - -cbase_lock::cbase_lock() -{ - m_history_mutex = PTHREAD_MUTEX_INITIALIZER; -} - -cbase_lock::~cbase_lock() -{ - pthread_mutex_destroy(&m_history_mutex); -} - -void cbase_lock::lock(lock_type type, const char* expr, const char *module, const char *func, int line) -{ - int ret = 0; - char m_curent_info[OWNER_INFO_LEN]; - struct timeval sv; - unsigned long long lock_waiting_start_time = 0; - unsigned long long lock_acquired_time = 0; - unsigned long long waiting_time = 0; - - snprintf(m_curent_info, OWNER_INFO_LEN, "%s:%s(%d)", module, func, line); - - if (type == LOCK_TYPE_MUTEX) - ret = try_lock_impl(); - else if (type == LOCK_TYPE_READ) - ret = try_read_lock_impl(); - else if (type == LOCK_TYPE_WRITE) - ret = try_write_lock_impl(); - - if (ret == 0) { - pthread_mutex_lock(&m_history_mutex); - snprintf(m_owner_info, OWNER_INFO_LEN, "%s", m_curent_info); - pthread_mutex_unlock(&m_history_mutex); - return; - } - - gettimeofday(&sv, NULL); - lock_waiting_start_time = MICROSECONDS(sv); - - pthread_mutex_lock(&m_history_mutex); - _I("%s is waiting for getting %s(%#x) owned in %s", - m_curent_info, expr, this, m_owner_info); - pthread_mutex_unlock(&m_history_mutex); - - if (type == LOCK_TYPE_MUTEX) - lock_impl(); - else if (type == LOCK_TYPE_READ) - read_lock_impl(); - else if (type == LOCK_TYPE_WRITE) - write_lock_impl(); - - gettimeofday(&sv, NULL); - lock_acquired_time = MICROSECONDS(sv); - - waiting_time = lock_acquired_time - lock_waiting_start_time; - - pthread_mutex_lock(&m_history_mutex); - _I("%s acquires lock after waiting %lluus, %s(%#x) was previously owned in %s", - m_curent_info, waiting_time, expr, this, m_owner_info); - snprintf(m_owner_info, OWNER_INFO_LEN, "%s", m_curent_info); - pthread_mutex_unlock(&m_history_mutex); -} - -void cbase_lock::lock(lock_type type) -{ - if (type == LOCK_TYPE_MUTEX) - lock_impl(); - else if (type == LOCK_TYPE_READ) - read_lock_impl(); - else if (type == LOCK_TYPE_WRITE) - write_lock_impl(); -} - -void cbase_lock::unlock(void) -{ - unlock_impl(); -} - -int cbase_lock::lock_impl(void) -{ - return 0; -} - -int cbase_lock::read_lock_impl(void) -{ - return 0; -} - -int cbase_lock::write_lock_impl(void) -{ - return 0; -} - -int cbase_lock::try_lock_impl(void) -{ - return 0; -} - -int cbase_lock::try_read_lock_impl(void) -{ - return 0; -} - -int cbase_lock::try_write_lock_impl(void) -{ - return 0; -} - -int cbase_lock::unlock_impl(void) -{ - return 0; -} - -Autolock::Autolock(cbase_lock &m, lock_type type, const char* expr, const char *module, const char *func, int line) -: m_lock(m) -{ - m_lock.lock(type, expr, module, func, line); -} - -Autolock::Autolock(cbase_lock &m, lock_type type) -: m_lock(m) -{ - m_lock.lock(type); -} - -Autolock::~Autolock() -{ - m_lock.unlock(); -} diff --git a/src/shared/cbase_lock.h b/src/shared/cbase_lock.h deleted file mode 100644 index ad57562..0000000 --- a/src/shared/cbase_lock.h +++ /dev/null @@ -1,83 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2013 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 _CBASE_LOCK_H_ -#define _CBASE_LOCK_H_ - -#include - -enum lock_type { - LOCK_TYPE_MUTEX, - LOCK_TYPE_READ, - LOCK_TYPE_WRITE, -}; - -#ifdef _LOCK_DEBUG -#define AUTOLOCK(x) Autolock x##_autolock((x), LOCK_TYPE_MUTEX, #x, __MODULE__, __func__, __LINE__) -#define AUTOLOCK_R(x) Autolock x##_autolock_r((x), LOCK_TYPE_READ, #x, __MODULE__, __func__, __LINE__) -#define AUTOLOCK_W(x) Autolock x##_autolock_w((x), LOCK_TYPE_WRITE, #x, __MODULE__, __func__, __LINE__) -#define LOCK(x) (x).lock(#x, __MODULE__, __func__, __LINE__) -#define LOCK_R(x) (x).lock(LOCK_TYPE_READ, #x, __MODULE__, __func__, __LINE__) -#define LOCK_W(x) (x).lock(LOCK_TYPE_WRITE, #x, __MODULE__, __func__, __LINE__) -#define UNLOCK(x) (x).unlock() -#else -#define AUTOLOCK(x) Autolock x##_autolock((x), LOCK_TYPE_MUTEX) -#define AUTOLOCK_R(x) Autolock x##_autolock_r((x), LOCK_TYPE_READ) -#define AUTOLOCK_W(x) Autolock x##_autolock_w((x), LOCK_TYPE_WRITE) -#define LOCK(x) (x).lock() -#define LOCK_R(x) (x).lock(LOCK_TYPE_READ) -#define LOCK_W(x) (x).lock(LOCK_TYPE_WRITE) -#define UNLOCK(x) (x).unlock() -#endif - -class cbase_lock { -public: - cbase_lock(); - virtual ~cbase_lock(); - - void lock(lock_type type, const char* expr, const char *module, const char *func, int line); - void lock(lock_type type); - void unlock(void); - -protected: - virtual int lock_impl(void); - virtual int read_lock_impl(void); - virtual int write_lock_impl(void); - - virtual int try_lock_impl(void); - virtual int try_read_lock_impl(void); - virtual int try_write_lock_impl(void); - - virtual int unlock_impl(void); -private: - pthread_mutex_t m_history_mutex; - static const int OWNER_INFO_LEN = 256; - char m_owner_info[OWNER_INFO_LEN]; -}; - -class Autolock { -private: - cbase_lock& m_lock; -public: - Autolock(cbase_lock &m, lock_type type, const char* expr, const char *module, const char *func, int line); - Autolock(cbase_lock &m, lock_type type); - ~Autolock(); -}; - -#endif /* _CBASE_LOCK_H_ */ diff --git a/src/shared/cmutex.cpp b/src/shared/cmutex.cpp deleted file mode 100644 index 6245b6e..0000000 --- a/src/shared/cmutex.cpp +++ /dev/null @@ -1,64 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2013 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 -#include - -cmutex::cmutex() -{ - pthread_mutexattr_t mutex_attr; - pthread_mutexattr_init(&mutex_attr); - pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE); - pthread_mutex_init(&m_mutex, &mutex_attr); - pthread_mutexattr_destroy(&mutex_attr); -} - -cmutex::~cmutex() -{ - pthread_mutex_destroy(&m_mutex); -} - -void cmutex::lock(void) -{ -#ifdef _LOCK_DEBUG - cbase_lock::lock(LOCK_TYPE_MUTEX, "mutex", __MODULE__, __func__, __LINE__); -#else - cbase_lock::lock(LOCK_TYPE_MUTEX); -#endif -} - -void cmutex::lock(const char* expr, const char *module, const char *func, int line) -{ - cbase_lock::lock(LOCK_TYPE_MUTEX, expr, module, func, line); -} - -int cmutex::lock_impl(void) -{ - return pthread_mutex_lock(&m_mutex); -} - -int cmutex::try_lock_impl(void) -{ - return pthread_mutex_trylock(&m_mutex); -} - -int cmutex::unlock_impl(void) -{ - return pthread_mutex_unlock(&m_mutex); -} diff --git a/src/shared/cmutex.h b/src/shared/cmutex.h deleted file mode 100644 index 94aa2b6..0000000 --- a/src/shared/cmutex.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2013 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 _CMUTEX_H_ -#define _CMUTEX_H_ - -#include "cbase_lock.h" - -class cmutex : public cbase_lock { -public: - cmutex(); - virtual ~cmutex(); - - void lock(void); - void lock(const char* expr, const char *module, const char *func, int line); - -protected: - int lock_impl(void); - int try_lock_impl(void); - int unlock_impl(void); - -private: - pthread_mutex_t m_mutex; -}; - -#endif /* _CMUTEX_H_ */ diff --git a/src/shared/command_common.h b/src/shared/command_common.h index 47c47b1..679bf08 100644 --- a/src/shared/command_common.h +++ b/src/shared/command_common.h @@ -20,7 +20,6 @@ #ifndef _COMMAND_COMMON_H_ #define _COMMAND_COMMON_H_ -#include #include #include #include diff --git a/src/shared/cpacket.cpp b/src/shared/cpacket.cpp deleted file mode 100644 index a2ce0e0..0000000 --- a/src/shared/cpacket.cpp +++ /dev/null @@ -1,104 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2013 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 -#include -#include -#include -#include -#include -#include - -cpacket::cpacket() -{ - m_packet = NULL; -} - -cpacket::cpacket(size_t size) -{ - m_packet = NULL; - set_payload_size(size); -} - -cpacket::~cpacket() -{ - delete[] (char*)m_packet; -} - -void cpacket::set_cmd(int cmd) -{ - if (!m_packet) - set_payload_size(0); - - m_packet->cmd = cmd; -} - -int cpacket::cmd(void) -{ - if (!m_packet) - return CMD_NONE; - - return m_packet->cmd; -} - -void *cpacket::data(void) -{ - if (!m_packet) - return NULL; - - return m_packet->data; -} - -void *cpacket::packet(void) -{ - return (void*)m_packet; -} - -size_t cpacket::size(void) -{ - if (!m_packet) - return 0; - - return m_packet->size + sizeof(packet_header); -} - -size_t cpacket::payload_size(void) -{ - if (!m_packet) - return 0; - - return m_packet->size; -} - -void cpacket::set_payload_size(size_t size) -{ - int prev_cmd = CMD_NONE; - - if (m_packet) { - prev_cmd = m_packet->cmd; - delete []m_packet; - } - - m_packet = (packet_header*) new(std::nothrow) char[size + sizeof(packet_header)]; - retm_if(!m_packet, "Failed to allocate memory"); - m_packet->size = size; - - if (prev_cmd != CMD_NONE) - m_packet->cmd = prev_cmd; -} diff --git a/src/shared/cpacket.h b/src/shared/cpacket.h deleted file mode 100644 index 64845e8..0000000 --- a/src/shared/cpacket.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2013 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 _CPACKET_H_ -#define _CPACKET_H_ - -#include - -typedef struct packet_header { - int cmd; - size_t size; - char data[]; -} packet_header; - -class cpacket { -public: - cpacket(); - explicit cpacket(size_t size); - ~cpacket(); - - void set_cmd(int cmd); - int cmd(void); - - void *data(void); - void *packet(void); - - size_t size(void); - size_t payload_size(void); - - void set_payload_size(size_t size); -private: - packet_header *m_packet; -}; - -#endif /* _CPACKET_H_ */ diff --git a/src/shared/csocket.cpp b/src/shared/csocket.cpp deleted file mode 100644 index ca9ec4f..0000000 --- a/src/shared/csocket.cpp +++ /dev/null @@ -1,548 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2013 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 -#include -#include -#include -#include - -csocket::csocket() -: m_sock_fd(-1) -, m_sock_type(SOCK_SEQPACKET) -, m_send_flags(MSG_NOSIGNAL) -, m_recv_flags(MSG_NOSIGNAL) -{ - memset(&m_addr, 0, sizeof(m_addr)); -} - -csocket::csocket(int sock_fd) -: m_sock_fd(-1) -, m_sock_type(SOCK_SEQPACKET) -, m_send_flags(MSG_NOSIGNAL) -, m_recv_flags(MSG_NOSIGNAL) -{ - m_sock_fd = sock_fd; - set_sock_type(); - memset(&m_addr, 0, sizeof(m_addr)); -} - -csocket::csocket(const csocket &sock) -: m_sock_fd(-1) -, m_sock_type(SOCK_SEQPACKET) -, m_send_flags(MSG_NOSIGNAL) -, m_recv_flags(MSG_NOSIGNAL) -{ - if (this == &sock) - return; - - m_sock_fd = sock.m_sock_fd; - m_sock_type = sock.m_sock_type; - m_send_flags = sock.m_send_flags; - m_recv_flags = sock.m_recv_flags; - - memcpy(&m_addr, &sock.m_addr, sizeof(sockaddr_un)); -} - -csocket::~csocket() -{ -} - -bool csocket::create(int sock_type) -{ - m_sock_fd = socket(AF_UNIX, sock_type, 0); - - if (!is_valid()) { - _ERRNO(errno, _E, "Failed to create socket for %s", get_client_name()); - return false; - } - - m_sock_type = sock_type; - - return true; -} - -bool csocket::bind(const char *sock_path) -{ - int length; - mode_t socket_mode; - - if (!is_valid()) - return false; - - if (!access(sock_path, F_OK)) { - unlink(sock_path); - } - - m_addr.sun_family = AF_UNIX; - - strncpy(m_addr.sun_path, sock_path, strlen(sock_path)); - - length = strlen(m_addr.sun_path) + sizeof(m_addr.sun_family); - - if (::bind(m_sock_fd, (struct sockaddr *)&m_addr, length) < 0) { - _ERRNO(errno, _E, "Failed to bind for socket[%d]", m_sock_fd); - close(); - return false; - } - - socket_mode = (S_IRWXU | S_IRWXG | S_IRWXO); - if (chmod(sock_path, socket_mode) < 0) { - _ERRNO(errno, _E, "Failed to chmod for socket[%d]", m_sock_fd); - close(); - return false; - } - - return true; -} - -bool csocket::listen(const int max_connections) -{ - if (!is_valid()) - return false; - - if (::listen(m_sock_fd, max_connections) < 0) { - _ERRNO(errno, _E, "Failed to listen for socket[%d]", m_sock_fd); - close(); - return false; - } - - return true; -} - -bool csocket::accept(csocket& client_socket) const -{ - const int TIMEOUT = 1; - struct timeval tv; - int addr_length = sizeof(m_addr); - int err = 0; - - fd_set read_fds; - - while (true) { - FD_ZERO(&read_fds); - FD_SET(m_sock_fd, &read_fds); - - tv.tv_sec = TIMEOUT; - tv.tv_usec = 0; - - err = ::select(m_sock_fd + 1, &read_fds, NULL, NULL, &tv); - if (err == -1) { - _ERRNO(errno, _E, "Failed to select(), m_sock_fd : %d", m_sock_fd); - return false; - } - - if (!is_valid()) { - _E("socket is closed, m_sock_fd : %d", m_sock_fd); - return false; - } - - /* timeout */ - if (!err) - continue; - - if (FD_ISSET(m_sock_fd, &read_fds)) - break; - - _ERRNO(errno, _E, "Failed to select(), msock_fd : %d", m_sock_fd); - } - - if (!is_valid()) { - _E("socket is closed, m_sock_fd : %d", m_sock_fd); - return false; - } - - do { - client_socket.m_sock_fd = ::accept(m_sock_fd, (sockaddr *)&m_addr, (socklen_t *)&addr_length); - client_socket.set_sock_type(); - if (!client_socket.is_valid()) - err = errno; - } while (err == EINTR); - - if (!client_socket.is_valid()) { - _ERRNO(errno, _E, "Failed to accept for socket[%d]", m_sock_fd); - return false; - } - - return true; -} - -ssize_t csocket::send_for_seqpacket(const void *buffer, size_t size) const -{ - ssize_t err, len; - - do { - len = ::send(m_sock_fd, buffer, size, m_send_flags); - err = len < 0 ? errno : 0; - } while (err == EINTR); - - if (err) { - _ERRNO(errno, _E, "Failed to send(%d, %#x, %d, %#x) = %d", - m_sock_fd, buffer, size, m_send_flags, len); - } - - return err == 0 ? len : -err; -} - -ssize_t csocket::recv_for_seqpacket(void* buffer, size_t size) const -{ - ssize_t err, len; - - do { - len = ::recv(m_sock_fd, buffer, size, m_recv_flags); - - if (len > 0) { - err = 0; - } else if (len == 0) { - _E("recv(%d, %#p , %d) = %d, because the peer performed shutdown!", - m_sock_fd, buffer, size, len); - err = 1; - } else { - err = errno; - } - } while (err == EINTR); - - if ((err == EAGAIN) || (err == EWOULDBLOCK)) - return 0; - - if (err) { - _ERRNO(err, _E, "Failed to recv(%d, %#x, %d, %#x) = %d", - m_sock_fd, buffer, size, m_recv_flags, len); - } - - return err == 0 ? len : -err; -} - -ssize_t csocket::send_for_stream(const void *buffer, size_t size) const -{ - ssize_t len; - ssize_t err = 0; - size_t total_sent_size = 0; - - do { - len = ::send(m_sock_fd, (const void *)((uint8_t *)buffer + total_sent_size), size - total_sent_size, m_send_flags); - - if (len < 0) { - /* - * If socket is not available to use it temporarily, - * EAGAIN(EWOULDBLOCK) or EINTR is returned by ::send(). - * so in order to prevent that data are omitted, sleep&retry to send it - */ - if ((errno == EINTR) || (errno == EAGAIN) || (errno == EWOULDBLOCK)) { - usleep(10000); - continue; - } - - _ERRNO(errno, _E, "Failed to send(%d, %#p + %d, %d - %d) = %d for %s", - m_sock_fd, buffer, total_sent_size, size, total_sent_size, - len, get_client_name()); - - err = errno; - break; - } - - total_sent_size += len; - } while (total_sent_size < size); - - return err == 0 ? total_sent_size : -err; -} - -ssize_t csocket::recv_for_stream(void* buffer, size_t size) const -{ - ssize_t len; - ssize_t err = 0; - size_t total_recv_size = 0; - - do { - len = ::recv(m_sock_fd, (void *)((uint8_t *)buffer + total_recv_size), size - total_recv_size, m_recv_flags); - - if (len == 0) { - _E("recv(%d, %#p + %d, %d - %d) = %d, because the peer of %s performed shutdown!", - m_sock_fd, buffer, total_recv_size, size, total_recv_size, len, get_client_name()); - err = 1; - break; - } - - if (len < 0) { - /* - * If socket is not available to use it temporarily, - * EAGAIN(EWOULDBLOCK) is returned by ::recv(). - * so in order to prevent that data are omitted, sleep&retry to receive it - */ - if ((errno != EINTR) || (errno == EAGAIN) || (errno == EWOULDBLOCK)) { - usleep(10000); - continue; - } - - _ERRNO(errno, _E, "Failed to recv(%d, %#p + %d, %d - %d) = %d for %s", - m_sock_fd, buffer, total_recv_size, size, total_recv_size, - len, get_client_name()); - - err = errno; - break; - } - - total_recv_size += len; - } while (total_recv_size < size); - - return err == 0 ? total_recv_size : -err; -} - -ssize_t csocket::send(const void *buffer, size_t size) const -{ - const int TIMEOUT = 5; - fd_set write_fds; - struct timeval tv; - - if (!is_valid()) - return -EINVAL; - - FD_ZERO(&write_fds); - FD_SET(m_sock_fd, &write_fds); - tv.tv_sec = TIMEOUT; - tv.tv_usec = 0; - - int ret; - - ret = select(m_sock_fd + 1, NULL, &write_fds, NULL, &tv); - - if (ret == -1) { - _ERRNO(errno, _E, "select error: sock_fd: %d\n for %s", m_sock_fd, get_client_name()); - return 0; - } else if (!ret) { - _ERRNO(errno, _E, "select timeout: %d seconds elapsed for %s", tv.tv_sec, get_client_name()); - return 0; - } - - if (!FD_ISSET(m_sock_fd, &write_fds)) { - _ERRNO(errno, _E, "select failed for %s, nothing to write, m_sock_fd : %d", get_client_name(), m_sock_fd); - return 0; - } - - if (m_sock_type == SOCK_STREAM) - return send_for_stream(buffer, size); - - return send_for_seqpacket(buffer, size); -} - -ssize_t csocket::recv(void* buffer, size_t size) const -{ - if (!is_valid()) - return -EINVAL; - - if (m_sock_type == SOCK_STREAM) - return recv_for_stream(buffer, size); - - return recv_for_seqpacket(buffer, size); -} - -bool csocket::connect(const char *sock_path) -{ - const int TIMEOUT = 5; - fd_set write_fds; - struct timeval tv; - int addr_len; - bool prev_blocking_mode; - - if (!is_valid()) - return false; - - prev_blocking_mode = is_blocking_mode(); - - set_blocking_mode(false); - - m_addr.sun_family = AF_UNIX; - - strncpy(m_addr.sun_path, sock_path, strlen(sock_path)); - - addr_len = strlen(m_addr.sun_path) + sizeof(m_addr.sun_family); - - if (::connect(m_sock_fd, (sockaddr *)&m_addr, addr_len) < 0) { - _ERRNO(errno, _E, "Failed to connect sock_fd: %d for %s", - m_sock_fd, get_client_name()); - return false; - } - - FD_ZERO(&write_fds); - FD_SET(m_sock_fd, &write_fds); - tv.tv_sec = TIMEOUT; - tv.tv_usec = 0; - - int ret; - - ret = select(m_sock_fd + 1, NULL, &write_fds, NULL, &tv); - - if (ret == -1) { - _ERRNO(errno, _E, "select error: sock_fd: %d\n for %s", m_sock_fd, get_client_name()); - close(); - return false; - } else if (!ret) { - _ERRNO(errno, _E, "select timeout: %d seconds elapsed for %s", tv.tv_sec, get_client_name()); - close(); - return false; - } - - if (!FD_ISSET(m_sock_fd, &write_fds)) { - _ERRNO(errno, _E, "select failed for %s, nothing to write, m_sock_fd : %d", get_client_name(), m_sock_fd); - close(); - return false; - } - - int so_error; - socklen_t len = sizeof(so_error); - - if (getsockopt(m_sock_fd, SOL_SOCKET, SO_ERROR, &so_error, &len) == -1) { - _ERRNO(errno, _E, "getsockopt failed for %s, m_sock_fd : %d", get_client_name(), m_sock_fd); - close(); - return false; - } - - if (so_error) { - _ERRNO(errno, _E, "SO_ERROR occurred for %s, m_sock_fd : %d, so_error : %d", - get_client_name(), m_sock_fd, so_error); - close(); - return false; - } - - if (prev_blocking_mode) - set_blocking_mode(true); - - return true; -} - -bool csocket::set_blocking_mode(bool blocking) -{ - int flags; - - if (!is_valid()) - return false; - - flags = fcntl(m_sock_fd, F_GETFL); - - if (flags == -1) { - _ERRNO(errno, _E, "fcntl(F_GETFL) failed for %s, m_sock_fd: %d", get_client_name(), m_sock_fd); - return false; - } - - flags = blocking ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK); - - flags = fcntl(m_sock_fd, F_SETFL, flags); - - if (flags == -1) { - _ERRNO(errno, _E, "fcntl(F_SETFL) failed for %s, m_sock_fd: %d", get_client_name(), m_sock_fd); - return false; - } - - return true; -} - -bool csocket::set_sock_type(void) -{ - socklen_t opt_len; - int sock_type; - - opt_len = sizeof(sock_type); - - if (!is_valid()) - return false; - - if (getsockopt(m_sock_fd, SOL_SOCKET, SO_TYPE, &sock_type, &opt_len) < 0) { - _ERRNO(errno, _E, "getsockopt(SOL_SOCKET, SO_TYPE) failed for %s, m_sock_fd: %d", - get_client_name(), m_sock_fd); - return false; - } - - m_sock_type = sock_type; - return true; -} - -bool csocket::set_connection_mode(void) -{ - struct timeval tv; - const int TIMEOUT = 5; - - set_blocking_mode(true); - - tv.tv_sec = TIMEOUT; - tv.tv_usec = 0; - - if (!is_valid()) - return false; - - if (setsockopt(m_sock_fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) { - _ERRNO(errno, _E, "Set SO_RCVTIMEO failed for %s, m_sock_fd : %d", get_client_name(), m_sock_fd); - close(); - return false; - } - - m_send_flags = MSG_NOSIGNAL; - m_recv_flags = MSG_NOSIGNAL; - - return true; -} - -bool csocket::set_transfer_mode(void) -{ - set_blocking_mode(false); - - m_send_flags = MSG_DONTWAIT | MSG_NOSIGNAL; - m_recv_flags = MSG_DONTWAIT | MSG_NOSIGNAL; - - return true; -} - -bool csocket::is_blocking_mode(void) -{ - int flags; - - if (!is_valid()) - return false; - - flags = fcntl(m_sock_fd, F_GETFL); - - if (flags == -1) { - _ERRNO(errno, _E, "fcntl(F_GETFL) failed for %s, m_sock_fd: %d", get_client_name(), m_sock_fd); - return false; - } - - return !(flags & O_NONBLOCK); -} - -bool csocket::is_valid(void) const -{ - return (m_sock_fd >= 0); -} - -int csocket::get_socket_fd(void) const -{ - return m_sock_fd; -} - -bool csocket::close(void) -{ - if (m_sock_fd >= 0) { - if (::close(m_sock_fd) < 0) { - _ERRNO(errno, _W, "Failed to close socket[%d]", m_sock_fd); - return false; - } - m_sock_fd = -1; - } - - return true; -} diff --git a/src/shared/csocket.h b/src/shared/csocket.h deleted file mode 100644 index 1391531..0000000 --- a/src/shared/csocket.h +++ /dev/null @@ -1,80 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2013 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 _CSOCKET_H_ -#define _CSOCKET_H_ - -#include -#include -#include -#include -#include -#include -#include - -class csocket { -public: - csocket(); - virtual ~csocket(); - csocket(int sock_fd); - csocket(const csocket &sock); - - //Server - bool create(int sock_type); - bool bind(const char *sock_path); - bool listen(const int max_connections); - bool accept(csocket& client_socket) const; - - //Client - bool connect(const char *sock_path); - - //Data Transfer - ssize_t send(const void *buffer, size_t size) const; - ssize_t recv(void* buffer, size_t size) const; - - bool set_connection_mode(void); - bool set_transfer_mode(void); - bool is_blocking_mode(void); - - //check if socket is created - bool is_valid(void) const; - int get_socket_fd(void) const; - - bool close(void); - - bool is_block_mode(void); - -private: - bool set_blocking_mode(bool blocking); - bool set_sock_type(void); - - ssize_t send_for_seqpacket(const void *buffer, size_t size) const; - ssize_t send_for_stream(const void *buffer, size_t size) const; - ssize_t recv_for_seqpacket(void* buffer, size_t size) const; - ssize_t recv_for_stream(void* buffer, size_t size) const; - -private: - int m_sock_fd; - int m_sock_type; - sockaddr_un m_addr; - int m_send_flags; - int m_recv_flags; -}; - -#endif /* _CSOCKET_H_ */ diff --git a/src/shared/poller.cpp b/src/shared/poller.cpp deleted file mode 100644 index 8e05af8..0000000 --- a/src/shared/poller.cpp +++ /dev/null @@ -1,142 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2013 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 -#include -#include -#include - -#define EPOLL_MAX 32 - -poller::poller() -: m_epfd(-1) -, sfd(-1) -{ - init_poll_fd(); -} - -poller::poller(int fd) -: m_epfd(-1) -{ - init_poll_fd(); - add_fd(fd); -} - -poller::~poller() -{ - if (m_epfd >= 0) - ::close(m_epfd); -} - -void poller::init_poll_fd(void) -{ - m_epfd = epoll_create(EPOLL_MAX); -} - -bool poller::add_signal_fd(int fd) -{ - sfd = fd; - return add_fd(fd); -} - -bool poller::add_fd(int fd) -{ - struct epoll_event event; - - event.data.fd = fd; - event.events = EPOLLIN | EPOLLERR | EPOLLHUP; - - if (epoll_ctl(m_epfd, EPOLL_CTL_ADD, fd, &event)) { - _ERRNO(errno, _E, "Failed to add fd[%d]", fd); - return false; - } - - return true; -} - -bool poller::del_fd(int fd) -{ - struct epoll_event event; - - event.data.fd = fd; - - if (epoll_ctl(m_epfd, EPOLL_CTL_DEL, fd, &event)) { - _ERRNO(errno, _E, "Failed to del fd[%d]", fd); - return false; - } - - return true; -} - -bool poller::fill_event_queue(void) -{ - const int EPOLL_MAX_EVENT = 1; - - struct epoll_event event_items[EPOLL_MAX_EVENT]; - int nr_events = epoll_wait(m_epfd, event_items, EPOLL_MAX_EVENT, -1); - - if (nr_events < 0) { - if (errno == EINTR) - return true; - - _ERRNO(errno, _E, "Failed to fill event queue"); - return false; - } - - if (nr_events == 0) { - _E("Epoll timeout!"); - return false; - } - - for (int i = 0; i < nr_events; i++) - m_event_queue.push(event_items[i]); - - return true; -} - -bool poller::poll(struct epoll_event &event) -{ - while (true) { - if (m_event_queue.empty()) { - if (!fill_event_queue()) - return false; - } - - if (!m_event_queue.empty()) { - event = m_event_queue.front(); - m_event_queue.pop(); - - if (event.events & EPOLLERR) { - _E("Poll error!"); - return false; - } - - if (event.events & EPOLLHUP) { - _I("Poll: Connetion is closed from the other side"); - return false; - } - - if (event.data.fd == sfd) { - _E("received signal"); - return false; - } - - return true; - } - } -} diff --git a/src/shared/poller.h b/src/shared/poller.h deleted file mode 100644 index e61408d..0000000 --- a/src/shared/poller.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2013 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 _POLLER_H_ -#define _POLLER_H_ - -#include -#include -#include -#include - -class poller { -public: - poller(); - poller(int fd); - virtual ~poller(); - - bool add_signal_fd(int fd); - - bool add_fd(int fd); - bool del_fd(int fd); - - bool poll(struct epoll_event &event); - -private: - int m_epfd; - int sfd; - - std::queue m_event_queue; - -private: - void init_poll_fd(void); - bool fill_event_queue(void); -}; - -#endif /* _POLLER_H_ */ diff --git a/src/shared/sensor_types.cpp b/src/shared/sensor_types.cpp deleted file mode 100644 index 037ddc9..0000000 --- a/src/shared/sensor_types.cpp +++ /dev/null @@ -1,22 +0,0 @@ -/* - * sensord - * - * 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 - -DECLARE_SENSOR_ENUM_UTIL(sensor_type_t, SENSOR_TYPE) -- 2.7.4