From 225da6f36fc7e6d75b888283d6710cb259e60d99 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Thu, 15 Jun 2017 16:58:32 +0900 Subject: [PATCH 01/16] sensord: fix bugs with using invalid format in logs Change-Id: Ide24272d63a24611aba81a5fe0114e62cebeb1fe Signed-off-by: kibak.yoon --- src/client/sensor_internal.cpp | 38 +++++++++++++++++++------------------- src/client/sensor_manager.cpp | 2 +- src/shared/seqpacket_socket.cpp | 6 +++--- src/shared/socket.cpp | 2 +- src/shared/stream_socket.cpp | 6 +++--- 5 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/client/sensor_internal.cpp b/src/client/sensor_internal.cpp index fe3e554..22d8840 100644 --- a/src/client/sensor_internal.cpp +++ b/src/client/sensor_internal.cpp @@ -115,10 +115,10 @@ API int sensord_get_default_sensor(sensor_type_t type, sensor_t *sensor) API bool sensord_get_type(sensor_t sensor, sensor_type_t *type) { - retvm_if(!type, false, "Invalid parameter[%#x]", type); + retvm_if(!type, false, "Invalid type"); retvm_if(!manager.connect(), false, "Failed to connect"); retvm_if(!manager.is_supported(sensor), false, - "Invalid sensor[%#x]", sensor); + "Invalid sensor[%p]", sensor); *type = static_cast(sensor)->get_type(); @@ -129,7 +129,7 @@ API const char* sensord_get_uri(sensor_t sensor) { retvm_if(!manager.connect(), NULL, "Failed to connect"); retvm_if(!manager.is_supported(sensor), NULL, - "Invalid sensor[%#x]", sensor); + "Invalid sensor[%p]", sensor); return static_cast(sensor)->get_uri().c_str(); } @@ -138,7 +138,7 @@ API const char* sensord_get_name(sensor_t sensor) { retvm_if(!manager.connect(), NULL, "Failed to connect"); retvm_if(!manager.is_supported(sensor), NULL, - "Invalid sensor[%#x]", sensor); + "Invalid sensor[%p]", sensor); return static_cast(sensor)->get_model().c_str(); } @@ -147,17 +147,17 @@ API const char* sensord_get_vendor(sensor_t sensor) { retvm_if(!manager.connect(), NULL, "Failed to connect"); retvm_if(!manager.is_supported(sensor), NULL, - "Invalid sensor[%#x]", sensor); + "Invalid sensor[%p]", sensor); return static_cast(sensor)->get_vendor().c_str(); } API bool sensord_get_min_range(sensor_t sensor, float *min_range) { - retvm_if(!min_range, false, "Invalid parameter[%#x]", min_range); + retvm_if(!min_range, false, "Invalid paramter"); retvm_if(!manager.connect(), false, "Failed to connect"); retvm_if(!manager.is_supported(sensor), false, - "Invalid sensor[%#x]", sensor); + "Invalid sensor[%p]", sensor); *min_range = static_cast(sensor)->get_min_range(); @@ -166,10 +166,10 @@ API bool sensord_get_min_range(sensor_t sensor, float *min_range) API bool sensord_get_max_range(sensor_t sensor, float *max_range) { - retvm_if(!max_range, false, "Invalid parameter[%#x]", max_range); + retvm_if(!max_range, false, "Invalid parameter"); retvm_if(!manager.connect(), false, "Failed to connect"); retvm_if(!manager.is_supported(sensor), false, - "Invalid sensor[%#x]", sensor); + "Invalid sensor[%p]", sensor); *max_range = static_cast(sensor)->get_max_range(); @@ -178,10 +178,10 @@ API bool sensord_get_max_range(sensor_t sensor, float *max_range) API bool sensord_get_resolution(sensor_t sensor, float *resolution) { - retvm_if(!resolution, false, "Invalid parameter[%#x]", resolution); + retvm_if(!resolution, false, "Invalid parameter"); retvm_if(!manager.connect(), false, "Failed to connect"); retvm_if(!manager.is_supported(sensor), false, - "Invalid sensor[%#x]", sensor); + "Invalid sensor[%p]", sensor); *resolution = static_cast(sensor)->get_resolution(); @@ -190,10 +190,10 @@ API bool sensord_get_resolution(sensor_t sensor, float *resolution) API bool sensord_get_min_interval(sensor_t sensor, int *min_interval) { - retvm_if(!min_interval, false, "Invalid parameter[%#x]", min_interval); + retvm_if(!min_interval, false, "Invalid parameter"); retvm_if(!manager.connect(), false, "Failed to connect"); retvm_if(!manager.is_supported(sensor), false, - "Invalid sensor[%#x]", sensor); + "Invalid sensor[%p]", sensor); *min_interval = static_cast(sensor)->get_min_interval(); @@ -202,10 +202,10 @@ API bool sensord_get_min_interval(sensor_t sensor, int *min_interval) API bool sensord_get_fifo_count(sensor_t sensor, int *fifo_count) { - retvm_if(!fifo_count, false, "Invalid parameter[%#x]", fifo_count); + retvm_if(!fifo_count, false, "Invalid parameter"); retvm_if(!manager.connect(), false, "Failed to connect"); retvm_if(!manager.is_supported(sensor), false, - "Invalid sensor[%#x]", sensor); + "Invalid sensor[%p]", sensor); *fifo_count = 0; @@ -214,10 +214,10 @@ API bool sensord_get_fifo_count(sensor_t sensor, int *fifo_count) API bool sensord_get_max_batch_count(sensor_t sensor, int *max_batch_count) { - retvm_if(!max_batch_count, false, "Invalid parameter[%#x]", max_batch_count); + retvm_if(!max_batch_count, false, "Invalid parameter"); retvm_if(!manager.connect(), false, "Failed to connect"); retvm_if(!manager.is_supported(sensor), false, - "Invalid sensor[%#x]", sensor); + "Invalid sensor[%p]", sensor); *max_batch_count = static_cast(sensor)->get_max_batch_count(); @@ -228,7 +228,7 @@ API bool sensord_is_wakeup_supported(sensor_t sensor) { retvm_if(!manager.connect(), false, "Failed to connect"); retvm_if(!manager.is_supported(sensor), false, - "Invalid sensor[%#x]", sensor); + "Invalid sensor[%p]", sensor); return static_cast(sensor)->is_wakeup_supported(); } @@ -237,7 +237,7 @@ API int sensord_connect(sensor_t sensor) { retvm_if(!manager.connect(), -EIO, "Failed to connect"); retvm_if(!manager.is_supported(sensor), -EINVAL, - "Invalid sensor[%#x]", sensor); + "Invalid sensor[%p]", sensor); sensor::sensor_listener *listener; diff --git a/src/client/sensor_manager.cpp b/src/client/sensor_manager.cpp index 760a90b..101b833 100644 --- a/src/client/sensor_manager.cpp +++ b/src/client/sensor_manager.cpp @@ -83,7 +83,7 @@ int sensor_manager::get_sensors(const char *uri, sensor_t **list, int *count) bool sensor_manager::is_supported(sensor_t sensor) { - retvm_if(!sensor, false, "Invalid parameter[%#x]", sensor); + retvm_if(!sensor, false, "Invalid sensor"); for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) { if (&*it == sensor) diff --git a/src/shared/seqpacket_socket.cpp b/src/shared/seqpacket_socket.cpp index ef6e01b..ad27394 100644 --- a/src/shared/seqpacket_socket.cpp +++ b/src/shared/seqpacket_socket.cpp @@ -54,7 +54,7 @@ ssize_t seqpacket_socket::on_send(const void *buffer, size_t size) const } while (err == EINTR); if (err) { - _ERRNO(errno, _E, "Failed to send(%d, %#x, %d) = %d", + _ERRNO(errno, _E, "Failed to send(%d, %p, %u) = %d", socket::get_fd(), buffer, size, len); } @@ -74,7 +74,7 @@ ssize_t seqpacket_socket::on_recv(void *buffer, size_t size) const if (len > 0) { err = 0; } else if (len == 0) { - _E("Failed to recv(%d, %#p , %d) = %d, because the peer performed shutdown!", + _E("Failed to recv(%d, %p , %u) = %d, because the peer performed shutdown!", socket::get_fd(), buffer, size, len); err = 1; } else { @@ -86,7 +86,7 @@ ssize_t seqpacket_socket::on_recv(void *buffer, size_t size) const return 0; if (err) { - _ERRNO(errno, _E, "Failed to recv(%d, %#x, %d) = %d", + _ERRNO(errno, _E, "Failed to recv(%d, %p, %u) = %d", socket::get_fd(), buffer, size, len); } diff --git a/src/shared/socket.cpp b/src/shared/socket.cpp index f053dbc..aed28df 100644 --- a/src/shared/socket.cpp +++ b/src/shared/socket.cpp @@ -422,7 +422,7 @@ bool socket::has_connected(void) } if (so_error) { - _E("Failed to connect[%d]: %d", so_error); + _E("Failed to connect[%d] : %d", m_sock_fd, so_error); return false; } diff --git a/src/shared/stream_socket.cpp b/src/shared/stream_socket.cpp index 8ec9f7c..e583f26 100644 --- a/src/shared/stream_socket.cpp +++ b/src/shared/stream_socket.cpp @@ -58,7 +58,7 @@ ssize_t stream_socket::on_send(const void *buffer, size_t size) const continue; } - _ERRNO(errno, _E, "Failed to send(%d, %#p, %x, %d) = %d", + _ERRNO(errno, _E, "Failed to send(%d, %p, %u, %u) = %d", get_fd(), buffer, total_size, size - total_size, len); return -errno; } @@ -81,7 +81,7 @@ ssize_t stream_socket::on_recv(void *buffer, size_t size) const socket::get_mode()); if (len == 0) { - _E("Failed to recv(%d, %#p + %x, %d) = %d, because the peer performed shutdown", + _E("Failed to recv(%d, %p + %x, %u) = %d, because the peer performed shutdown", get_fd(), buffer, total_size, size - total_size, len); return -1; } @@ -92,7 +92,7 @@ ssize_t stream_socket::on_recv(void *buffer, size_t size) const continue; } - _ERRNO(errno, _E, "Failed to recv(%d, %#p, %x, %d) = %d", + _ERRNO(errno, _E, "Failed to recv(%d, %p, %u, %u) = %d", get_fd(), buffer, total_size, size - total_size, len); return -errno; } -- 2.7.4 From 7497cf8b59310b0eb1414d8a9a2af44d4e76efe1 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Fri, 23 Jun 2017 14:44:27 +0900 Subject: [PATCH 02/16] sensord: remove termination routine when there is no sensor - serveral frameworks try to connect sensor continuously, so the sensor daemon runs every time they try. it's a overhead work. Change-Id: Ib203c69e4e53767256e71ca9aaa5f58c6d97a622 Signed-off-by: kibak.yoon --- src/server/server.cpp | 20 -------------------- src/server/server.h | 1 - 2 files changed, 21 deletions(-) diff --git a/src/server/server.cpp b/src/server/server.cpp index a7a2f03..22b3987 100644 --- a/src/server/server.cpp +++ b/src/server/server.cpp @@ -33,7 +33,6 @@ #define SET_CAL 1 //#define CAL_NODE_PATH "/sys/class/sensors/ssp_sensor/set_cal_data" -#define TIMEOUT_TERM 10 #define MAX_CONNECTION 1000 using namespace sensor; @@ -87,7 +86,6 @@ bool server::init(void) init_calibration(); init_server(); - init_termination(); is_running.store(true); sd_notify(0, "READY=1"); @@ -148,21 +146,3 @@ void server::init_server(void) m_server->set_option(SO_TYPE, SOCK_STREAM); m_server->bind(m_handler, &m_loop); } - -static gboolean terminate(gpointer data) -{ - sensor_manager *mgr = reinterpret_cast(data); - std::vector sensors = mgr->get_sensors(); - - if (sensors.size() <= 0) { - _I("Terminating.. because there is no sensors"); - server::stop(); - } - - return FALSE; -} - -void server::init_termination(void) -{ - g_timeout_add_seconds(TIMEOUT_TERM, terminate, m_manager); -} diff --git a/src/server/server.h b/src/server/server.h index 99856a1..e8a5771 100644 --- a/src/server/server.h +++ b/src/server/server.h @@ -46,7 +46,6 @@ private: void init_calibration(void); void init_server(void); - void init_termination(void); ipc::ipc_server *m_server; sensor_manager *m_manager; -- 2.7.4 From 2eeda580cd75630b43b53ca9e8f27762c1d2bbe4 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Mon, 17 Jul 2017 11:54:52 +0900 Subject: [PATCH 03/16] sensor: fix the type casting issue about integer division - the result of integer division converted into floating point Change-Id: I40d59f2976c1d65cd4c4c20ddb027edcbc70ab5b Signed-off-by: kibak.yoon --- src/sensor/rotation_vector/fusion_utils/orientation_filter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sensor/rotation_vector/fusion_utils/orientation_filter.cpp b/src/sensor/rotation_vector/fusion_utils/orientation_filter.cpp index be41328..e352f5b 100644 --- a/src/sensor/rotation_vector/fusion_utils/orientation_filter.cpp +++ b/src/sensor/rotation_vector/fusion_utils/orientation_filter.cpp @@ -40,7 +40,7 @@ #define ZIGMA_W (0.05 * DEG2RAD) #define TAU_W 1000 #define QWB_CONST ((2 * (ZIGMA_W * ZIGMA_W)) / TAU_W) -#define F_CONST (-1 / TAU_W) +#define F_CONST (-1.0 / TAU_W) #define SQUARE(T) (T * T) #define NEGLIGIBLE_VAL 0.0000001 -- 2.7.4 From 06207864e19a4f25f8ffb06a08261b953ab9c8d8 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Mon, 17 Jul 2017 11:59:39 +0900 Subject: [PATCH 04/16] sensor: check NULL if memory allocation is failed Change-Id: I1a85c8d036143f93f344be1e8eb2b5a5f7983470 Signed-off-by: kibak.yoon --- src/sensor/auto_rotation/auto_rotation_sensor.cpp | 1 + src/sensor/gesture/face_down_sensor.cpp | 6 ++---- src/sensor/gravity/gravity_comp_sensor.cpp | 1 + src/sensor/gravity/gravity_lowpass_sensor.cpp | 1 + src/sensor/linear_accel/linear_accel_sensor.cpp | 1 + src/sensor/orientation/orientation_sensor.cpp | 1 + src/sensor/rotation_vector/gyro_rv_sensor.cpp | 1 + src/sensor/rotation_vector/magnetic_rv_sensor.cpp | 1 + src/sensor/rotation_vector/rv_sensor.cpp | 1 + 9 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/sensor/auto_rotation/auto_rotation_sensor.cpp b/src/sensor/auto_rotation/auto_rotation_sensor.cpp index e02cad3..7fb6587 100644 --- a/src/sensor/auto_rotation/auto_rotation_sensor.cpp +++ b/src/sensor/auto_rotation/auto_rotation_sensor.cpp @@ -111,6 +111,7 @@ int auto_rotation_sensor::get_data(sensor_data_t **data, int *length) { sensor_data_t *sensor_data; sensor_data = (sensor_data_t *)malloc(sizeof(sensor_data_t)); + retvm_if(!sensor_data, -ENOMEM, "Failed to allocate memory"); sensor_data->accuracy = SENSOR_ACCURACY_GOOD; sensor_data->timestamp = m_rotation_time; diff --git a/src/sensor/gesture/face_down_sensor.cpp b/src/sensor/gesture/face_down_sensor.cpp index 03ddf7c..98c1ccb 100644 --- a/src/sensor/gesture/face_down_sensor.cpp +++ b/src/sensor/gesture/face_down_sensor.cpp @@ -119,10 +119,7 @@ void face_down_sensor::synthesize(const sensor_event_t & event) int data_length; face_down_event = (sensor_event_t *)malloc(sizeof(sensor_event_t)); - if (!face_down_event) { - _E("Failed to allocate memory"); - return; - } + retm_if(!face_down_event, "Failed to allocate memory"); get_data(&face_down_data, &data_length); face_down_event->sensor_id = get_id(); @@ -139,6 +136,7 @@ int face_down_sensor::get_data(sensor_data_t ** data, int *length) { sensor_data_t *sensor_data; sensor_data = (sensor_data_t *)malloc(sizeof(sensor_data_t)); + retvm_if(!sensor_data, -ENOMEM, "Failed to allocate memory"); sensor_data->accuracy = SENSOR_ACCURACY_GOOD; sensor_data->timestamp = m_time; diff --git a/src/sensor/gravity/gravity_comp_sensor.cpp b/src/sensor/gravity/gravity_comp_sensor.cpp index 36b2171..1fbf747 100644 --- a/src/sensor/gravity/gravity_comp_sensor.cpp +++ b/src/sensor/gravity/gravity_comp_sensor.cpp @@ -229,6 +229,7 @@ int gravity_comp_sensor::get_data(sensor_data_t **data, int *len) { sensor_data_t *sensor_data; sensor_data = (sensor_data_t *)malloc(sizeof(sensor_data_t)); + retvm_if(!sensor_data, -ENOMEM, "Failed to allocate memory"); sensor_data->accuracy = m_accuracy; sensor_data->timestamp = m_time; diff --git a/src/sensor/gravity/gravity_lowpass_sensor.cpp b/src/sensor/gravity/gravity_lowpass_sensor.cpp index 5401934..eb3a48f 100644 --- a/src/sensor/gravity/gravity_lowpass_sensor.cpp +++ b/src/sensor/gravity/gravity_lowpass_sensor.cpp @@ -116,6 +116,7 @@ int gravity_lowpass_sensor::get_data(sensor_data_t **data, int *len) { sensor_data_t *sensor_data; sensor_data = (sensor_data_t *)malloc(sizeof(sensor_data_t)); + retvm_if(!sensor_data, -ENOMEM, "Failed to allocate memory"); sensor_data->accuracy = m_accuracy; sensor_data->timestamp = m_time; diff --git a/src/sensor/linear_accel/linear_accel_sensor.cpp b/src/sensor/linear_accel/linear_accel_sensor.cpp index 2dac280..99b7b1e 100644 --- a/src/sensor/linear_accel/linear_accel_sensor.cpp +++ b/src/sensor/linear_accel/linear_accel_sensor.cpp @@ -104,6 +104,7 @@ int linear_accel_sensor::get_data(sensor_data_t **data, int *length) { sensor_data_t *sensor_data; sensor_data = (sensor_data_t *)malloc(sizeof(sensor_data_t)); + retvm_if(!sensor_data, -ENOMEM, "Failed to allocate memory"); sensor_data->accuracy = SENSOR_ACCURACY_GOOD; sensor_data->timestamp = m_time; diff --git a/src/sensor/orientation/orientation_sensor.cpp b/src/sensor/orientation/orientation_sensor.cpp index acdf12b..7cec3f2 100644 --- a/src/sensor/orientation/orientation_sensor.cpp +++ b/src/sensor/orientation/orientation_sensor.cpp @@ -96,6 +96,7 @@ int orientation_sensor::get_data(sensor_data_t **data, int *length) { sensor_data_t *sensor_data; sensor_data = (sensor_data_t *)malloc(sizeof(sensor_data_t)); + retvm_if(!sensor_data, -ENOMEM, "Failed to allocate memory"); sensor_data->accuracy = m_accuracy; sensor_data->timestamp = m_time; diff --git a/src/sensor/rotation_vector/gyro_rv_sensor.cpp b/src/sensor/rotation_vector/gyro_rv_sensor.cpp index 850118f..1a0aaaa 100644 --- a/src/sensor/rotation_vector/gyro_rv_sensor.cpp +++ b/src/sensor/rotation_vector/gyro_rv_sensor.cpp @@ -104,6 +104,7 @@ int gyro_rv_sensor::get_data(sensor_data_t **data, int *length) { sensor_data_t *sensor_data; sensor_data = (sensor_data_t *)malloc(sizeof(sensor_data_t)); + retvm_if(!sensor_data, -ENOMEM, "Failed to allocate memory"); sensor_data->accuracy = m_accuracy; sensor_data->timestamp = m_time; diff --git a/src/sensor/rotation_vector/magnetic_rv_sensor.cpp b/src/sensor/rotation_vector/magnetic_rv_sensor.cpp index 5e4cd74..8a40705 100644 --- a/src/sensor/rotation_vector/magnetic_rv_sensor.cpp +++ b/src/sensor/rotation_vector/magnetic_rv_sensor.cpp @@ -104,6 +104,7 @@ int magnetic_rv_sensor::get_data(sensor_data_t **data, int *length) { sensor_data_t *sensor_data; sensor_data = (sensor_data_t *)malloc(sizeof(sensor_data_t)); + retvm_if(!sensor_data, -ENOMEM, "Failed to allocate memory"); sensor_data->accuracy = m_accuracy; sensor_data->timestamp = m_time; diff --git a/src/sensor/rotation_vector/rv_sensor.cpp b/src/sensor/rotation_vector/rv_sensor.cpp index c69cb6b..20b0226 100644 --- a/src/sensor/rotation_vector/rv_sensor.cpp +++ b/src/sensor/rotation_vector/rv_sensor.cpp @@ -110,6 +110,7 @@ int rv_sensor::get_data(sensor_data_t **data, int *length) { sensor_data_t *sensor_data; sensor_data = (sensor_data_t *)malloc(sizeof(sensor_data_t)); + retvm_if(!sensor_data, -ENOMEM, "Failed to allocate memory"); sensor_data->accuracy = m_accuracy; sensor_data->timestamp = m_time; -- 2.7.4 From 75d0f1cdd8bb150dd45b670d94874931a14908ae Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Mon, 17 Jul 2017 12:52:01 +0900 Subject: [PATCH 05/16] sensord: version 4.0.1 Change-Id: I03330c92359414f7a2c5d9b881c57a1acf04a8ba 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 cd7496c..d081956 100644 --- a/packaging/sensord.spec +++ b/packaging/sensord.spec @@ -1,6 +1,6 @@ Name: sensord Summary: Sensor daemon -Version: 4.0.0 +Version: 4.0.1 Release: 1 Group: System/Sensor Framework License: Apache-2.0 -- 2.7.4 From e46c594e2ce78c4b08b38be3058301e8ec396853 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Tue, 18 Jul 2017 18:59:59 +0900 Subject: [PATCH 06/16] sensord: check type whether it is a regular file or not Change-Id: I8b72cd6702def0a57d5ce570cb183d90f43519f6 Signed-off-by: kibak.yoon --- src/server/server.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/server/server.cpp b/src/server/server.cpp index 22b3987..237f658 100644 --- a/src/server/server.cpp +++ b/src/server/server.cpp @@ -20,6 +20,7 @@ #include "server.h" #include +#include #include #include #include @@ -112,6 +113,14 @@ void server::deinit(void) static void set_cal_data(const char *path) { + struct stat file_stat; + + if (lstat(path, &file_stat) != 0) + return; + + if (!S_ISREG(file_stat.st_mode)) + return; + FILE *fp = fopen(path, "w"); retm_if(!fp, "There is no calibration file[%s]", path); -- 2.7.4 From 01e3b4baaf5a9a11bea7ae2f71b369f4b6d76484 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Tue, 18 Jul 2017 19:01:18 +0900 Subject: [PATCH 07/16] sensord: remove unused debugging function Change-Id: I9486d4b7db1cf42eddd26d36c447108eff893a24 Signed-off-by: kibak.yoon --- src/shared/sensor_utils.cpp | 26 -------------------------- 1 file changed, 26 deletions(-) diff --git a/src/shared/sensor_utils.cpp b/src/shared/sensor_utils.cpp index 16341cc..9cd8141 100644 --- a/src/shared/sensor_utils.cpp +++ b/src/shared/sensor_utils.cpp @@ -211,31 +211,6 @@ unsigned long long sensor::utils::get_timestamp(timeval *t) return ((unsigned long long)(t->tv_sec)*1000000LL +t->tv_usec); } -#ifdef _DEBUG -bool sensor::utils::get_proc_name(pid_t pid, char *process_name) -{ - FILE *fp; - char buf[NAME_MAX]; - char filename[PATH_MAX]; - - sprintf(filename, "/proc/%d/stat", pid); - fp = fopen(filename, "r"); - - if (fp == NULL) - return false; - - if (fscanf(fp, "%*s (%[^)]", buf) < 1) { - fclose(fp); - return false; - } - - strncpy(process_name, buf, NAME_MAX-1); - process_name[NAME_MAX-1] = '\0'; - fclose(fp); - - return true; -} -#else bool sensor::utils::get_proc_name(pid_t pid, char *process_name) { char buf[NAME_MAX]; @@ -249,7 +224,6 @@ bool sensor::utils::get_proc_name(pid_t pid, char *process_name) return true; } -#endif const char* sensor::utils::get_client_name(void) { -- 2.7.4 From d096d640331205a41eda989f5edbb3252d157410 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Fri, 21 Jul 2017 11:57:11 +0900 Subject: [PATCH 08/16] sensord: increase timeout time a specific command takes more than a second sometimes (e.g., permission checking with cynara initialization). so timeout time should be increased. (3sec) Change-Id: Ib886303eb4c847568f2ebbc9db797873b0779329 Signed-off-by: kibak.yoon --- src/shared/socket.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/shared/socket.cpp b/src/shared/socket.cpp index aed28df..8849181 100644 --- a/src/shared/socket.cpp +++ b/src/shared/socket.cpp @@ -29,6 +29,8 @@ #include "sensor_log.h" +#define SOCK_TIMEOUT 3 + using namespace ipc; static bool set_close_on_exec(int fd) @@ -136,7 +138,6 @@ socket::~socket() bool socket::connect(void) { - const int TIMEOUT = 3; sockaddr_un addr; fd_set write_fds; FD_ZERO(&write_fds); @@ -157,7 +158,7 @@ bool socket::connect(void) return false; } - if (!select_fds(m_sock_fd, NULL, &write_fds, TIMEOUT)) { + if (!select_fds(m_sock_fd, NULL, &write_fds, SOCK_TIMEOUT)) { _E("Failed to select for socket[%d]", m_sock_fd); close(); return false; @@ -294,12 +295,11 @@ bool socket::create(const std::string &path) ssize_t socket::send(const void *buffer, size_t size, bool select) const { if (select) { - const int TIMEOUT = 1; fd_set write_fds; FD_ZERO(&write_fds); FD_SET(m_sock_fd, &write_fds); - if (!select_fds(m_sock_fd, NULL, &write_fds, TIMEOUT)) { + if (!select_fds(m_sock_fd, NULL, &write_fds, SOCK_TIMEOUT)) { _E("Failed to send message(timeout)"); return 0; } @@ -311,12 +311,11 @@ ssize_t socket::send(const void *buffer, size_t size, bool select) const ssize_t socket::recv(void* buffer, size_t size, bool select) const { if (select) { - const int TIMEOUT = 1; fd_set read_fds; FD_ZERO(&read_fds); FD_SET(m_sock_fd, &read_fds); - if (!select_fds(m_sock_fd, &read_fds, NULL, TIMEOUT)) { + if (!select_fds(m_sock_fd, &read_fds, NULL, SOCK_TIMEOUT)) { _E("Failed to receive message(timeout)"); return 0; } -- 2.7.4 From 17238ab0c43b389fb66869f938e61d071e791d37 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Wed, 17 May 2017 13:48:38 +0900 Subject: [PATCH 09/16] sensord: enable samsung pedometer sensor for fused location - URI = http://samsung.com/sensor/healthinfo/pedometer/samsung_pedometer - Model = samsung_pedometer - Vendor = samsung.com Change-Id: Ic6220e0001bc5923e392a7a0dec99860ba47227d Signed-off-by: kibak.yoon --- src/sensor/CMakeLists.txt | 6 + src/sensor/create.cpp | 7 + src/sensor/pedometer/average_filter.cpp | 64 +++++ src/sensor/pedometer/average_filter.h | 48 ++++ src/sensor/pedometer/common.h | 25 ++ src/sensor/pedometer/pedometer.cpp | 84 +++++++ src/sensor/pedometer/pedometer.h | 75 ++++++ src/sensor/pedometer/pedometer_info.h | 46 ++++ src/sensor/pedometer/pedometer_sensor.cpp | 134 ++++++++++ src/sensor/pedometer/pedometer_sensor.h | 50 ++++ src/sensor/pedometer/pedometer_speed_filter.cpp | 161 ++++++++++++ src/sensor/pedometer/pedometer_speed_filter.h | 116 +++++++++ src/sensor/pedometer/savitzky_golay_filter15.cpp | 71 ++++++ src/sensor/pedometer/savitzky_golay_filter15.h | 50 ++++ src/sensor/pedometer/step_detection.cpp | 271 +++++++++++++++++++++ src/sensor/pedometer/step_detection.h | 82 +++++++ src/sensor/pedometer/step_event.h | 33 +++ .../pedometer/zero_crossing_step_detection.cpp | 65 +++++ .../pedometer/zero_crossing_step_detection.h | 56 +++++ 19 files changed, 1444 insertions(+) create mode 100644 src/sensor/pedometer/average_filter.cpp create mode 100644 src/sensor/pedometer/average_filter.h create mode 100644 src/sensor/pedometer/common.h create mode 100644 src/sensor/pedometer/pedometer.cpp create mode 100644 src/sensor/pedometer/pedometer.h create mode 100644 src/sensor/pedometer/pedometer_info.h create mode 100644 src/sensor/pedometer/pedometer_sensor.cpp create mode 100644 src/sensor/pedometer/pedometer_sensor.h create mode 100644 src/sensor/pedometer/pedometer_speed_filter.cpp create mode 100644 src/sensor/pedometer/pedometer_speed_filter.h create mode 100644 src/sensor/pedometer/savitzky_golay_filter15.cpp create mode 100644 src/sensor/pedometer/savitzky_golay_filter15.h create mode 100644 src/sensor/pedometer/step_detection.cpp create mode 100644 src/sensor/pedometer/step_detection.h create mode 100644 src/sensor/pedometer/step_event.h create mode 100644 src/sensor/pedometer/zero_crossing_step_detection.cpp create mode 100644 src/sensor/pedometer/zero_crossing_step_detection.h diff --git a/src/sensor/CMakeLists.txt b/src/sensor/CMakeLists.txt index 9735d8d..388cb3a 100644 --- a/src/sensor/CMakeLists.txt +++ b/src/sensor/CMakeLists.txt @@ -7,6 +7,7 @@ SET(GRAVITY "ON") SET(LINEAR_ACCEL "ON") SET(RV "ON") SET(ORIENTATION "ON") +SET(PEDOMETER "ON") INCLUDE_DIRECTORIES( ${CMAKE_SOURCE_DIR}/src/shared @@ -59,6 +60,11 @@ FILE(GLOB_RECURSE SRCS ${SRCS} orientation/*.cpp) ADD_DEFINITIONS(-DENABLE_ORIENTATION) ENDIF() +IF("${PEDOMETER}" STREQUAL "ON") +FILE(GLOB_RECURSE SRCS ${SRCS} pedometer/*.cpp) +ADD_DEFINITIONS(-DENABLE_PEDOMETER) +ENDIF() + MESSAGE("Sources: ${SRCS}") ADD_LIBRARY(${PROJECT_NAME} SHARED ${SRCS}) TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${PLUGINS_PKGS_LDFLAGS}) diff --git a/src/sensor/create.cpp b/src/sensor/create.cpp index 0dd39b5..3c9b372 100644 --- a/src/sensor/create.cpp +++ b/src/sensor/create.cpp @@ -37,6 +37,9 @@ #ifdef ENABLE_ORIENTATION #include "orientation/orientation_sensor.h" #endif +#ifdef ENABLE_PEDOMETER +#include "pedometer/pedometer_sensor.h" +#endif static std::vector sensors; @@ -82,6 +85,10 @@ extern "C" int create(fusion_sensor_t **fsensors) create_sensor("Orientation Sensor"); #endif +#ifdef ENABLE_PEDOMETER + create_sensor("Pedometer"); +#endif + *fsensors = &sensors[0]; return sensors.size(); } diff --git a/src/sensor/pedometer/average_filter.cpp b/src/sensor/pedometer/average_filter.cpp new file mode 100644 index 0000000..81ca075 --- /dev/null +++ b/src/sensor/pedometer/average_filter.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2016-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 "average_filter.h" + +#include + +static double mean(double *array, int size) +{ + double avrg = 0; + + for (int i = 0; i < size; ++i) + avrg = avrg + array[i]; + + return avrg / size; +} + +average_filter::average_filter(int sz) +: m_size(sz) +, m_index(0) +, m_ready(false) +{ + m_array = (double *)calloc(sz, sizeof(double)); +} + +average_filter::~average_filter() +{ + if (m_array == NULL) + return; + + free(m_array); + m_array = NULL; + m_size = 0; +} + +double average_filter::filter(double value) +{ + m_array[m_index++] = value; + + if (m_index >= m_size) { + m_ready = true; + m_index = 0; + } + return mean(m_array, (m_ready ? m_size : m_index)); +} + +void average_filter::reset(void) +{ + m_index = 0; + m_ready = false; +} diff --git a/src/sensor/pedometer/average_filter.h b/src/sensor/pedometer/average_filter.h new file mode 100644 index 0000000..e083ce0 --- /dev/null +++ b/src/sensor/pedometer/average_filter.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __AVERAGE_FILTER_H__ +#define __AVERAGE_FILTER_H__ + +#include +#include "common.h" + +class average_filter { +public: + average_filter(int size); + ~average_filter(); + + /************************************************************************ + * Filters input data. + */ + double filter(double value); + + /************************************************************************ + * Resets average filter to initial state. + */ + void reset(void); + +private: + /************************************************************************ + * Average filter state. + */ + double *m_array; + int m_size; + int m_index; + bool m_ready; +}; + +#endif /* __AVERAGE_FILTER_H__ */ diff --git a/src/sensor/pedometer/common.h b/src/sensor/pedometer/common.h new file mode 100644 index 0000000..b1a0fef --- /dev/null +++ b/src/sensor/pedometer/common.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __PEDOMETER_COMMON_H__ +#define __PEDOMETER_COMMON_H__ + +#include + +typedef long long timestamp_t; +#define UNKNOWN_TIMESTAMP ((long long)0x8000000000000000) + +#endif /* __PEDOMETER_COMMON_H__ */ diff --git a/src/sensor/pedometer/pedometer.cpp b/src/sensor/pedometer/pedometer.cpp new file mode 100644 index 0000000..b642746 --- /dev/null +++ b/src/sensor/pedometer/pedometer.cpp @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2016-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 "pedometer.h" +#include "step_event.h" + +#include + +pedometer::pedometer() +: m_step_detection() +, m_total_length(0) +, m_step_count(0) +, m_pedometer_filter() +, m_some_speed(false) +{ +} + +pedometer::~pedometer() +{ +} + +void pedometer::set_savitzky_filter(bool enable) +{ + m_step_detection.set_use_savitzky(enable); +} + +void pedometer::reset(void) +{ + m_total_length = 0; + m_step_count = 0; + m_step_detection.reset(); + m_pedometer_filter.reset(); + m_some_speed = false; +} + +bool pedometer::get_pedometer(timestamp_t timestamp, double acc, pedometer_info *info) +{ + bool result = false; + step_event event; + + if (m_step_detection.get_step(timestamp, acc, &event)) { + if (event.m_timestamp != UNKNOWN_TIMESTAMP) { + m_step_count++; + m_total_length += event.m_step_length; + m_pedometer_filter.get_step(timestamp, event.m_step_length); + double speed = m_pedometer_filter.get_speed(timestamp); + info->timestamp = timestamp; + info->is_step_detected = true; + info->step_count = m_step_count; + info->step_length = event.m_step_length; + info->total_step_length = m_total_length; + info->step_speed = speed; + result = true; + m_some_speed = speed != 0; + } + } + if (m_some_speed) { + double speed = m_pedometer_filter.get_speed(timestamp); + if (speed == 0) { + m_some_speed = false; + info->timestamp = timestamp; + info->is_step_detected = true; + info->step_count = m_step_count; + info->step_length = 0; + info->total_step_length = m_total_length; + info->step_speed = 0; + result = true; + } + } + return result; +} diff --git a/src/sensor/pedometer/pedometer.h b/src/sensor/pedometer/pedometer.h new file mode 100644 index 0000000..3f904d7 --- /dev/null +++ b/src/sensor/pedometer/pedometer.h @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __PEDOMETER_H__ +#define __PEDOMETER_H__ + +#include "common.h" +#include "step_detection.h" +#include "pedometer_info.h" +#include "pedometer_speed_filter.h" + +/************************************************************************ + * stores pedometer engine state. + */ +class pedometer { +public: + pedometer(); + ~pedometer(); + + /************************************************************************ + * enables/disables savitzky filter. + */ + void set_savitzky_filter(bool enable); + + /************************************************************************ + * resets {@link pedometer} object to initial state. + */ + void reset(void); + + /************************************************************************ + * called on new acceleration event. + * + * @param info + * result of pedometer algorithm. valid only it method returns true. + * @param timestamp + * timestamp of acceleration event in ns. + * @param acc + * vertical component of global acceleration. + * + * @result + * true if new step event was detected. + */ + bool get_pedometer(timestamp_t timestamp, double acc, pedometer_info *info); + +private: + /** detects step and estimates step length. */ + step_detection m_step_detection; + + /** sum of lengths all steps from start. */ + double m_total_length; + + /** number of steps from start. */ + long long m_step_count; + + /** estimates current speed from step length. */ + pedometer_speed_filter m_pedometer_filter; + + /** some non zero speed was detected. */ + bool m_some_speed; +}; + +#endif /* __PEDOMETER_H__ */ diff --git a/src/sensor/pedometer/pedometer_info.h b/src/sensor/pedometer/pedometer_info.h new file mode 100644 index 0000000..ac521db --- /dev/null +++ b/src/sensor/pedometer/pedometer_info.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __PEDOMETER_INFO_H__ +#define __PEDOMETER_INFO_H__ + +#include "common.h" + +/************************************************************************ + * stores information about pedometer event detected. + */ +class pedometer_info { +public: + /** timestamp this event was detected in ns. */ + timestamp_t timestamp; + + /** is step detected. */ + bool is_step_detected; + + /** step count from scanner start. */ + long long step_count; + + /** step length in meters. */ + double step_length; + + /** total length of all steps detected from scanner start in meters. */ + double total_step_length; + + /** current mean speed in m/s. */ + double step_speed; +}; + +#endif /* __PEDOMETER_INFO_H__ */ diff --git a/src/sensor/pedometer/pedometer_sensor.cpp b/src/sensor/pedometer/pedometer_sensor.cpp new file mode 100644 index 0000000..bbd9b67 --- /dev/null +++ b/src/sensor/pedometer/pedometer_sensor.cpp @@ -0,0 +1,134 @@ +/* + * 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 "pedometer_sensor.h" + +#include +#include +#include +#include + +#define NAME_SENSOR "http://samsung.com/sensor/healthinfo/pedometer/samsung_pedometer" +#define NAME_VENDOR "samsung.com" + +#define SRC_ID_ACC 0x1 +#define SRC_STR_ACC "http://tizen.org/sensor/general/accelerometer" + +#define NORM(x, y, z) sqrt((x)*(x) + (y)*(y) + (z)*(z)) + +#define US_TO_NS(x) (x * 1000) + +/* Sensor information */ +static sensor_info2_t sensor_info = { + id: 0x1, + type: HUMAN_PEDOMETER_SENSOR, + uri: NAME_SENSOR, + vendor: NAME_VENDOR, + min_range: 0, + max_range: 1, + resolution: 1, + min_interval: 0, + max_batch_count: 0, + wakeup_supported: false, + privilege: "http://tizen.org/privilege/healthinfo", +}; + +/* Required sensor list */ +static required_sensor_s required_sensors[] = { + {SRC_ID_ACC, SRC_STR_ACC} +}; + +pedometer_sensor::pedometer_sensor() +: m_step_count(-1) +, m_step_length(-1) +, m_step_total_length(-1) +, m_step_speed(-1) +, m_time(0) +{ +} + +pedometer_sensor::~pedometer_sensor() +{ +} + +int pedometer_sensor::get_sensor_info(const sensor_info2_t **info) +{ + *info = &sensor_info; + return OP_SUCCESS; +} + +int pedometer_sensor::get_required_sensors(const required_sensor_s **sensors) +{ + *sensors = required_sensors; + + /* You should return the number of required sensor */ + return 1; +} + +int pedometer_sensor::update(uint32_t id, sensor_data_t *data, int len) +{ + pedometer_info info; + double acc = NORM(data->values[0], data->values[1], data->values[2]); + + if (!m_pedometer.get_pedometer(US_TO_NS(data->timestamp), acc, &info)) + return OP_ERROR; + + m_step_count = info.step_count; + m_step_length = info.step_length; + m_step_total_length = info.total_step_length; + m_step_speed = info.step_speed; + m_time = data->timestamp; + + _D("[%lld] %lld %f %f %f", data->timestamp, + info.step_count, info.step_length, info.total_step_length, info.step_speed); + + return OP_SUCCESS; +} + +int pedometer_sensor::get_data(sensor_data_t **data, int *len) +{ + sensor_data_t *sensor_data; + sensor_data = (sensor_data_t *)malloc(sizeof(sensor_data_t)); + + sensor_data->accuracy = SENSOR_ACCURACY_GOOD; + sensor_data->timestamp = m_time; + sensor_data->value_count = 8; + sensor_data->values[0] = (float)m_step_count; + sensor_data->values[1] = (float)m_step_count; + sensor_data->values[2] = 0; + sensor_data->values[3] = m_step_total_length; + sensor_data->values[4] = 0; + sensor_data->values[5] = m_step_speed; + sensor_data->values[6] = 0; + sensor_data->values[7] = 0; + + *data = sensor_data; + *len = sizeof(sensor_data_t); + + return 0; +} + +int pedometer_sensor::start(observer_h ob) +{ + m_pedometer.reset(); + return OP_DEFAULT; +} + +int pedometer_sensor::stop(observer_h ob) +{ + return OP_DEFAULT; +} diff --git a/src/sensor/pedometer/pedometer_sensor.h b/src/sensor/pedometer/pedometer_sensor.h new file mode 100644 index 0000000..32153b3 --- /dev/null +++ b/src/sensor/pedometer/pedometer_sensor.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef __PEDOMETER_SENSOR_H__ +#define __PEDOMETER_SENSOR_H__ + +#include +#include + +#include "pedometer.h" + +class pedometer_sensor : public fusion_sensor { +public: + pedometer_sensor(); + virtual ~pedometer_sensor(); + + int get_sensor_info(const sensor_info2_t **info); + int get_required_sensors(const required_sensor_s **sensors); + + int update(uint32_t id, sensor_data_t *data, int len); + int get_data(sensor_data_t **data, int *len); + + int start(observer_h ob); + int stop(observer_h ob); + +private: + long long m_step_count; + float m_step_length; + float m_step_total_length; + float m_step_speed; + unsigned long long m_time; + + pedometer m_pedometer; +}; + +#endif /* __PEDOMETER_SENSOR_H__ */ diff --git a/src/sensor/pedometer/pedometer_speed_filter.cpp b/src/sensor/pedometer/pedometer_speed_filter.cpp new file mode 100644 index 0000000..d6ac774 --- /dev/null +++ b/src/sensor/pedometer/pedometer_speed_filter.cpp @@ -0,0 +1,161 @@ +/* + * Copyright (c) 2016-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 "pedometer_speed_filter.h" + +/** default for maximum step duration in ns. currently 2s. */ +#define STEP_MAX_DURATION 2000000000L + +pedometer_speed_filter::pedometer_speed_filter() +: m_last_timestamp(UNKNOWN_TIMESTAMP) +, m_current_speed(0) +, m_last_step_length(0) +, m_last_step_duration(0) +, m_step_max_duration(STEP_MAX_DURATION) +{ +} + +pedometer_speed_filter::~pedometer_speed_filter() +{ +} + +/************************************************************************ + * clears current speed, last step length and last step duration. + */ +void pedometer_speed_filter::clear_speed(void) +{ + m_current_speed = 0; + m_last_step_length = 0; + m_last_step_duration = 0; +} + +/************************************************************************ + * sets new maximum step duration in ns. + * if there is no new speed during this time current speed is cleared. + * 0 disables this feature. + * + * @param step_max_duration + * maximum step duration in ns. + * 0 to disable step duration checking. + */ +void pedometer_speed_filter::set_step_max_duration(long long step_max_duration) +{ + m_step_max_duration = step_max_duration; +} + +/************************************************************************ + * called when new step detection event occurs. + * + * @param timestamp + * timestamp of step detection event in ns. + * @param steplength + * length of detected step in m. + */ +void pedometer_speed_filter::get_step(timestamp_t timestamp, double step_length) +{ + if (m_last_timestamp == UNKNOWN_TIMESTAMP || timestamp == UNKNOWN_TIMESTAMP) { + clear_speed(); + } else if (m_step_max_duration != 0 && timestamp - m_last_timestamp > m_step_max_duration) { + clear_speed(); + } else if (m_last_timestamp < timestamp) { + double step_duration = (timestamp - m_last_timestamp) / 1e9; + m_current_speed = + (step_length + m_last_step_length) / (step_duration + m_last_step_duration); + m_last_step_length = step_length; + m_last_step_duration = step_duration; + } else { + return; + } + m_last_timestamp = timestamp; +} + +/************************************************************************ + * reports new speed. + * + * @param timestamp + * timestamp of speed event. + * @param speed + * current speed in m/s. + */ +void pedometer_speed_filter::new_speed(timestamp_t timestamp, double speed) +{ + if (m_last_timestamp == UNKNOWN_TIMESTAMP || timestamp == UNKNOWN_TIMESTAMP + || timestamp > m_last_timestamp) { + m_last_timestamp = timestamp; + m_current_speed = speed; + m_last_step_length = 0; + m_last_step_duration = 0; + } +} + +/************************************************************************ + * returns current speed. + * + * @param timestamp + * timestamp for which speed should be calculated. + * @return speed for given timestamp in m/s. + */ +double pedometer_speed_filter::get_speed(timestamp_t timestamp) +{ + if (m_step_max_duration != 0 && (m_last_timestamp == UNKNOWN_TIMESTAMP || + timestamp == UNKNOWN_TIMESTAMP || + timestamp - m_last_timestamp > m_step_max_duration)) { + clear_speed(); + } + return m_current_speed; +} + +/************************************************************************ + * changes current speed. + * + * @param speed + * current speed in m/s. + */ +void pedometer_speed_filter::set_current_speed(double speed) +{ + m_current_speed = speed; +} + +/************************************************************************ + * @return estimated current speed in m/s. + */ +double pedometer_speed_filter::get_current_speed(void) +{ + return m_current_speed; +} + +/************************************************************************ + */ +bool pedometer_speed_filter::is_known_timestamp(void) +{ + return m_last_timestamp != UNKNOWN_TIMESTAMP; +} + +/************************************************************************ + */ +timestamp_t pedometer_speed_filter::get_timestamp(void) +{ + return m_last_timestamp; +} + +/************************************************************************ + * resets filter to initial state. + */ +void pedometer_speed_filter::reset(void) +{ + m_last_timestamp = UNKNOWN_TIMESTAMP; + clear_speed(); +} diff --git a/src/sensor/pedometer/pedometer_speed_filter.h b/src/sensor/pedometer/pedometer_speed_filter.h new file mode 100644 index 0000000..4c6fde1 --- /dev/null +++ b/src/sensor/pedometer/pedometer_speed_filter.h @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __PEDOMETER_SPEED_FILTER_H__ +#define __PEDOMETER_SPEED_FILTER_H__ + +#include "common.h" + +/************************************************************************ + * stores pedometer speed filter state. + */ +class pedometer_speed_filter { +public: + pedometer_speed_filter(); + ~pedometer_speed_filter(); + + void clear_speed(void); + + /************************************************************************ + * sets new maximum step duration in ns. + * if there is no new speed during this time current speed is cleared. + * 0 disables this feature. + * + * @param step_max_duration + * maximum step duration in ns. + * 0 to disable step duration checking. + */ + void set_step_max_duration(long long step_max_duration); + + /************************************************************************ + * called when new step detection event occurs. + * + * @param timestamp + * timestamp of step detection event in ns. + * @param steplength + * length of detected step in m. + */ + void get_step(timestamp_t timestamp, double step_length); + + /************************************************************************ + * reports new speed. + * + * @param timestamp + * timestamp of speed event. + * @param speed + * current speed in m/s. + */ + void new_speed(timestamp_t timestamp, double speed); + + /************************************************************************ + * returns current speed. + * + * @param timestamp + * timestamp for which speed should be calculated. + * @return speed for given timestamp in m/s. + */ + double get_speed(timestamp_t timestamp); + + /************************************************************************ + * changes current speed. + * + * @param speed + * current speed in m/s. + */ + void set_current_speed(double speed); + + /************************************************************************ + * @return estimated current speed in m/s. + */ + double get_current_speed(void); + + /************************************************************************ + */ + bool is_known_timestamp(void); + + /************************************************************************ + * @return timestamp of last step detection event in ns. + */ + timestamp_t get_timestamp(void); + + /************************************************************************ + * resets filter to initial state. + */ + void reset(void); + +private: + /** timestamp of last step detection event in ns. */ + timestamp_t m_last_timestamp; + + /** estimated current speed in m/s. */ + double m_current_speed; + + /** length of last step in m. */ + double m_last_step_length; + + /** duration of last step in s. */ + double m_last_step_duration; + + /** maximum step duration in ns. 0 to disable step duration checking. */ + long long m_step_max_duration; +}; + +#endif /* __PEDOMETER_SPEED_FILTER_H__ */ diff --git a/src/sensor/pedometer/savitzky_golay_filter15.cpp b/src/sensor/pedometer/savitzky_golay_filter15.cpp new file mode 100644 index 0000000..ced5d8f --- /dev/null +++ b/src/sensor/pedometer/savitzky_golay_filter15.cpp @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2016-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 "savitzky_golay_filter15.h" + +#include + +/* length of filter. changing it requires changing coef_array! */ +#define ARRAY_SIZE 15 + +/* sum of numerators of elements in coef_array. */ +#define SUM_COEF 1105.0 + +/* array with coefficients for savitzky-golay filter. must be length of n! */ +static double coef_array[] = { + -78 / SUM_COEF, -13 / SUM_COEF, 42 / SUM_COEF, 87 / SUM_COEF, + 122 / SUM_COEF, 147 / SUM_COEF, 162 / SUM_COEF, 167 / SUM_COEF, + 162 / SUM_COEF, 147 / SUM_COEF, 122 / SUM_COEF, 87 / SUM_COEF, + 42 / SUM_COEF, -13 / SUM_COEF, -78 / SUM_COEF }; + +savitzky_golay_filter15::savitzky_golay_filter15() +: m_empty(true) +{ + m_array = (double *)calloc(ARRAY_SIZE, sizeof(double)); +} + +savitzky_golay_filter15::~savitzky_golay_filter15() +{ + if (m_array == NULL) + return; + + free(m_array); + m_array = NULL; +} + +double savitzky_golay_filter15::filter(double value) +{ + if (m_empty) { + for (int i = 0; i < ARRAY_SIZE; i++) + m_array[i] = value; + m_empty = false; + return value; + } + + for (int i = 1; i < ARRAY_SIZE; i++) + m_array[i - 1] = m_array[i]; + m_array[ARRAY_SIZE - 1] = value; + + double avrg = 0; + for (int i = 0; i < ARRAY_SIZE; i++) + avrg += m_array[i] * coef_array[i]; + return avrg; +} + +void savitzky_golay_filter15::reset(void) +{ + m_empty = true; +} diff --git a/src/sensor/pedometer/savitzky_golay_filter15.h b/src/sensor/pedometer/savitzky_golay_filter15.h new file mode 100644 index 0000000..a9a8899 --- /dev/null +++ b/src/sensor/pedometer/savitzky_golay_filter15.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __SAVITZKYGOLAYFILTER15_H__ +#define __SAVITZKYGOLAYFILTER15_H__ + +#include +#include "common.h" + +/************************************************************************ + * stores savitzky-golay filter state. + */ +class savitzky_golay_filter15 { +public: + savitzky_golay_filter15(); + ~savitzky_golay_filter15(); + + /************************************************************************ + * filters input data. + * + * @param value + * data to filter. + * @result filtered data. + */ + double filter(double value); + + /************************************************************************ + * resets savitzky-golay filter to initial state. + */ + void reset(void); + +private: + double *m_array; + bool m_empty; +}; + +#endif /* __SAVITZKYGOLAYFILTER15_H__ */ diff --git a/src/sensor/pedometer/step_detection.cpp b/src/sensor/pedometer/step_detection.cpp new file mode 100644 index 0000000..afabc84 --- /dev/null +++ b/src/sensor/pedometer/step_detection.cpp @@ -0,0 +1,271 @@ +/* + * Copyright (c) 2016-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 "step_detection.h" + +#include +#include + +/* Size of average filter. */ +#define AV_FILTER_SIZE 7 + +#define AV_GFILTER_SIZE 500 + +#define FAST_PEAK_THRESHOLD 0.77 + +#define SLOW_PEAK_THRESHOLD 0.3 + +#define PEAK_THRESHOLD_FOR_MEAN 2.0 + +#define B_COEF 1.08 + +#define SLOPE_PARAM -0.75 + +step_detection::step_detection() +: m_average_filter(AV_FILTER_SIZE) +, m_average_gfilter(AV_GFILTER_SIZE) +, m_zero_crossing_up(true) +, m_zero_crossing_down(false) +, m_zc_filter() +, m_peak_threshold(FAST_PEAK_THRESHOLD) +, m_use_savitzky(false) +, m_last_step_timestamp(UNKNOWN_TIMESTAMP) +, m_zero_crossing_up_detected(false) +, m_zero_crossing_down_detected(false) +, m_minimum_acceleration(0) +, m_maximum_acceleration(0) +, m_is_slow_step_detected(false) +{ +} + +step_detection::~step_detection() +{ +} + +/************************************************************************ + */ +void step_detection::reset(void) +{ + m_average_filter.reset(); + m_average_gfilter.reset(); + m_zero_crossing_up.reset(); + m_zero_crossing_down.reset(); + + m_average_gfilter.filter(9.81); + m_zc_filter.reset(); + + m_zero_crossing_up_detected = false; + m_zero_crossing_down_detected = false; + m_minimum_acceleration = 0; + m_maximum_acceleration = 0; + m_is_slow_step_detected = false; +} + +/************************************************************************ + */ +void step_detection::set_peak_threshold(double threshold) +{ + m_peak_threshold = threshold; +} + +/************************************************************************ + */ +void step_detection::set_use_savitzky(bool use_savitzky) +{ + m_use_savitzky = use_savitzky; +} + +/************************************************************************ + */ +bool step_detection::is_slow_step(void) +{ + return m_is_slow_step_detected; +} + +/************************************************************************ + */ +static double cal_step_length(double time, double sqrt4peak_valley_diff) +{ + double step_length = 0; + if (time <= 0 || time > 1.00) { + step_length = 0.50 * sqrt4peak_valley_diff; + } else if (time < 0.3) { + step_length = 0; + } else { + step_length = B_COEF + SLOPE_PARAM * time; + } + + if (step_length > 1.5) + step_length = 0; + + return step_length; +} + +/************************************************************************ + */ +bool step_detection::add_acc_sensor_values_average( + timestamp_t timestamp, double acc, step_event* step) +{ + double acceleration = m_average_filter.filter(acc - 9.8066f); + + bool n_zero_up = m_zero_crossing_up.detect_step(timestamp, acceleration); + bool n_zero_down = m_zero_crossing_down.detect_step(timestamp, acceleration); + + double sqrt4peak_valley_diff = 0; + bool is_step_detected = false; + + if (n_zero_up) { + m_zero_crossing_up_detected = true; + m_zero_crossing_down_detected = false; + } + if (n_zero_down) { + m_zero_crossing_up_detected = false; + m_zero_crossing_down_detected = true; + } + if (m_zero_crossing_up_detected) { + if (m_maximum_acceleration < acceleration) { + m_maximum_acceleration = acceleration; + } + } + if (m_zero_crossing_down_detected) { + if (m_minimum_acceleration > acceleration) { + m_minimum_acceleration = acceleration; + } + } + + double peak_threshold; + if (m_zero_crossing_up.m_time_sum / 1E9 < 1.2) { + peak_threshold = m_peak_threshold; + m_is_slow_step_detected = false; + } else { + peak_threshold = SLOW_PEAK_THRESHOLD; + m_is_slow_step_detected = true; + } + + if (n_zero_down) { + if (m_maximum_acceleration > peak_threshold + || (m_maximum_acceleration - m_minimum_acceleration > PEAK_THRESHOLD_FOR_MEAN)) { + sqrt4peak_valley_diff = pow(m_maximum_acceleration - m_minimum_acceleration, 0.25); + m_minimum_acceleration = 0; + m_maximum_acceleration = 0; + is_step_detected = true; + } + } + + if (m_zero_crossing_up.m_time_sum / 1E9 < 0.3) + is_step_detected = false; + + if (is_step_detected) { + if (m_last_step_timestamp == UNKNOWN_TIMESTAMP) + m_last_step_timestamp = timestamp; + + double time = (timestamp - m_last_step_timestamp) / 1E9; + m_last_step_timestamp = timestamp; + m_zero_crossing_up.m_time_sum = 0; + step->m_timestamp = timestamp; + step->m_step_length = cal_step_length(time, sqrt4peak_valley_diff); + + return true; + } + return false; +} + +/************************************************************************ + */ +bool step_detection::add_acc_sensor_values_savitzky( + timestamp_t timestamp, double acc, step_event* step) +{ + double acceleration = m_zc_filter.filter(acc - m_average_gfilter.filter(acc)); + bool n_zero_up = m_zero_crossing_up.detect_step(timestamp, acceleration); + bool n_zero_down = m_zero_crossing_down.detect_step(timestamp, acceleration); + + double sqrt4peak_valley_diff = 0; + bool is_step_detected = false; + + if (n_zero_up) { + m_zero_crossing_up_detected = true; + m_zero_crossing_down_detected = false; + } + if (n_zero_down) { + m_zero_crossing_up_detected = false; + m_zero_crossing_down_detected = true; + } + if (m_zero_crossing_up_detected) { + if (m_maximum_acceleration < acceleration) { + m_maximum_acceleration = acceleration; + } + } + if (m_zero_crossing_down_detected) { + if (m_minimum_acceleration > acceleration) { + m_minimum_acceleration = acceleration; + } + } + + bool zup = m_zero_crossing_up.m_time_sum / 1E9 > 1.2; + if (n_zero_down) { + is_step_detected = false; + + if ((m_maximum_acceleration > 0.6 && + m_minimum_acceleration < -0.852) + || (m_maximum_acceleration > 0.714 + && m_minimum_acceleration < -0.455)) { + is_step_detected = true; + m_is_slow_step_detected = false; + } + if (m_maximum_acceleration - m_minimum_acceleration > 3.32) { + is_step_detected = true; + m_is_slow_step_detected = false; + } + if (zup && m_maximum_acceleration > 0.764 && m_minimum_acceleration < -0.0235) {// slow steps + is_step_detected = true; + m_is_slow_step_detected = true; + } + + if (is_step_detected) { + sqrt4peak_valley_diff = pow(m_maximum_acceleration - m_minimum_acceleration, 0.25); + m_minimum_acceleration = 0.0; + m_maximum_acceleration = 0.0; + } + } + + if (m_zero_crossing_up.m_time_sum / 1E9 < 0.3) + is_step_detected = false; + + if (is_step_detected) { + if (m_last_step_timestamp == UNKNOWN_TIMESTAMP) + m_last_step_timestamp = timestamp; + + double time = (timestamp - m_last_step_timestamp) / 1E9; + + m_last_step_timestamp = timestamp; + m_zero_crossing_up.m_time_sum = 0; + step->m_timestamp = timestamp; + step->m_step_length = cal_step_length(time, sqrt4peak_valley_diff); + + return true; + } + + return false; +} + +/************************************************************************ + */ +bool step_detection::get_step(timestamp_t timestamp, double acc, step_event* step) +{ + return m_use_savitzky + ? add_acc_sensor_values_savitzky(timestamp, acc, step) + : add_acc_sensor_values_average(timestamp, acc, step); +} diff --git a/src/sensor/pedometer/step_detection.h b/src/sensor/pedometer/step_detection.h new file mode 100644 index 0000000..addd786 --- /dev/null +++ b/src/sensor/pedometer/step_detection.h @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __STEP_DETECTION_H__ +#define __STEP_DETECTION_H__ + +#include "average_filter.h" +#include "zero_crossing_step_detection.h" +#include "savitzky_golay_filter15.h" +#include "step_event.h" +#include "common.h" + +/************************************************************************ + * step detection engine state. + */ +class step_detection { +public: + step_detection(); + ~step_detection(); + + /************************************************************************ + */ + void set_peak_threshold(double threshold); + + /************************************************************************ + */ + void set_use_savitzky(bool use_savitzky); + + /************************************************************************ + */ + bool is_slow_step(void); + + /************************************************************************ + */ + bool add_acc_sensor_values_average( + timestamp_t timestamp, double acc, step_event* step); + + /************************************************************************ + */ + bool add_acc_sensor_values_savitzky( + timestamp_t timestamp, double acc, step_event* step); + + /************************************************************************ + */ + bool get_step(timestamp_t timestamp, double acc, step_event* step); + + /************************************************************************ + * resets step_detection object to initial state. + */ + void reset(void); + +private: + average_filter m_average_filter; + average_filter m_average_gfilter; + zero_crossing_step_detection m_zero_crossing_up; + zero_crossing_step_detection m_zero_crossing_down; + + savitzky_golay_filter15 m_zc_filter; + double m_peak_threshold; + bool m_use_savitzky; + timestamp_t m_last_step_timestamp; + bool m_zero_crossing_up_detected; + bool m_zero_crossing_down_detected; + double m_minimum_acceleration; + double m_maximum_acceleration; + bool m_is_slow_step_detected; +}; + +#endif /* __STEP_DETECTION_H__ */ diff --git a/src/sensor/pedometer/step_event.h b/src/sensor/pedometer/step_event.h new file mode 100644 index 0000000..724f262 --- /dev/null +++ b/src/sensor/pedometer/step_event.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __STEP_EVENT_H__ +#define __STEP_EVENT_H__ + +#include "common.h" + +class step_event { +public: + step_event() + : m_timestamp(UNKNOWN_TIMESTAMP) + , m_step_length(0) + {} + + timestamp_t m_timestamp; + double m_step_length; +}; + +#endif /* __STEP_EVENT_H__ */ diff --git a/src/sensor/pedometer/zero_crossing_step_detection.cpp b/src/sensor/pedometer/zero_crossing_step_detection.cpp new file mode 100644 index 0000000..aff2b3c --- /dev/null +++ b/src/sensor/pedometer/zero_crossing_step_detection.cpp @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2016-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 "zero_crossing_step_detection.h" + +static bool detect_zero_crossing(double last_accel, double new_accel, bool up) +{ + if (up) + return (last_accel < 0 && new_accel >= 0); + + return (last_accel > 0 && new_accel <= 0); +} + +zero_crossing_step_detection::zero_crossing_step_detection(bool up) +: m_time_sum(0) +, m_up(up) +, m_last_acceleration(0) +, m_last_timestamp(UNKNOWN_TIMESTAMP) +, m_last_zero_crossing_time(UNKNOWN_TIMESTAMP) +{ +} + +zero_crossing_step_detection::~zero_crossing_step_detection() +{ +} + +bool zero_crossing_step_detection::detect_step(timestamp_t timestamp, double acceleration) +{ + bool step_detected = false; + + if (m_last_timestamp != UNKNOWN_TIMESTAMP) { + // zero crossing detected + if (detect_zero_crossing(m_last_acceleration, acceleration, m_up)) { + m_time_sum += timestamp - m_last_timestamp; + m_last_timestamp = timestamp; + m_last_zero_crossing_time = timestamp; + step_detected = true; + } + } else { + m_last_timestamp = timestamp; + } + m_last_acceleration = acceleration; + return step_detected; +} + +void zero_crossing_step_detection::reset(void) +{ + m_last_acceleration = 0; + m_time_sum = 0; + m_last_timestamp = UNKNOWN_TIMESTAMP; + m_last_zero_crossing_time = UNKNOWN_TIMESTAMP; +} diff --git a/src/sensor/pedometer/zero_crossing_step_detection.h b/src/sensor/pedometer/zero_crossing_step_detection.h new file mode 100644 index 0000000..79ca518 --- /dev/null +++ b/src/sensor/pedometer/zero_crossing_step_detection.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __ZERO_CROSSING_STEP_DETECTION_H__ +#define __ZERO_CROSSING_STEP_DETECTION_H__ + +#include "common.h" + +/************************************************************************ + * zero crossing detection engine state. + */ +class zero_crossing_step_detection { +public: + zero_crossing_step_detection(bool up); + ~zero_crossing_step_detection(); + + /************************************************************************ + */ + bool detect_step(timestamp_t timestamp, double acceleration); + + /************************************************************************ + */ + void reset(void); + + timestamp_t m_time_sum; + +private: + /** + * for true detects zero up crossing, for false detects zero down crossing. + */ + bool m_up; + + /** acceleration in previous detect step. */ + double m_last_acceleration; + + /** timestamp of last acc event. unknown time if no one. */ + timestamp_t m_last_timestamp; + + /** timestamp of last detected zero crossing. unknown time if not yet detected. */ + timestamp_t m_last_zero_crossing_time; +}; + +#endif /* __ZERO_CROSSING_STEP_DETECTION_H__ */ -- 2.7.4 From c9e0b6aa6d893a9e6fc26e25d8426feb59dcdf94 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Thu, 3 Aug 2017 21:05:11 +0900 Subject: [PATCH 10/16] sensord: remove excessive sensor log when socket buffer is full Change-Id: I70930ff13ac406a6c8a5418f3404cbfb83e3c32b Signed-off-by: kibak.yoon --- src/shared/channel.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/shared/channel.cpp b/src/shared/channel.cpp index e407aee..446c881 100644 --- a/src/shared/channel.cpp +++ b/src/shared/channel.cpp @@ -155,9 +155,8 @@ bool channel::send(message *msg) { retv_if(!m_loop, false); - /* TODO: check buffer size(is there any linux api for this?) */ int cur_buffer_size = m_socket->get_current_buffer_size(); - retvm_if(cur_buffer_size > SYSTEMD_SOCK_BUF_SIZE, false, "Failed to send data"); + retv_if(cur_buffer_size > SYSTEMD_SOCK_BUF_SIZE, false); send_event_handler *handler = new(std::nothrow) send_event_handler(this, msg); retvm_if(!handler, false, "Failed to allocate memory"); -- 2.7.4 From c5aaf8cbb2b6bbbac2420636f41cba98d8ccc336 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Fri, 4 Aug 2017 00:37:13 +0900 Subject: [PATCH 11/16] sensord: ternimate sensord instantly when signal is received Change-Id: I1993404cf85cf92427ccaf69a28dbe88a461134d Signed-off-by: kibak.yoon --- src/server/server.cpp | 2 +- src/shared/event_loop.cpp | 7 +------ 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/src/server/server.cpp b/src/server/server.cpp index 237f658..a411340 100644 --- a/src/server/server.cpp +++ b/src/server/server.cpp @@ -96,8 +96,8 @@ bool server::init(void) void server::deinit(void) { - m_manager->deinit(); m_server->close(); + m_manager->deinit(); delete m_server; m_server = NULL; diff --git a/src/shared/event_loop.cpp b/src/shared/event_loop.cpp index 1b569a1..c3a698b 100644 --- a/src/shared/event_loop.cpp +++ b/src/shared/event_loop.cpp @@ -238,12 +238,7 @@ void event_loop::stop(void) { ret_if(!is_running() || m_terminating.load()); - uint64_t term = 1; - ssize_t size; - m_terminating.store(true); - size = write(m_term_fd, &term, sizeof(uint64_t)); - - retm_if(size != sizeof(ssize_t), "Failed to write[%d]", m_term_fd); + terminate(); } void event_loop::terminate(void) -- 2.7.4 From b0d9ce9e1eabd744274000bdd2934a2d4f5fb04c Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Fri, 4 Aug 2017 00:38:29 +0900 Subject: [PATCH 12/16] sensord: fix bug with incorrect bit operation Change-Id: I29c35c9b923d572fbe0b39aa7fe5913386f77bb2 Signed-off-by: kibak.yoon --- src/shared/event_loop.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/shared/event_loop.cpp b/src/shared/event_loop.cpp index c3a698b..4f9e7ed 100644 --- a/src/shared/event_loop.cpp +++ b/src/shared/event_loop.cpp @@ -43,7 +43,7 @@ static gboolean g_io_handler(GIOChannel *ch, GIOCondition condition, gpointer da cond = (unsigned int)condition; if (cond & (G_IO_HUP)) - cond &= ~(G_IO_IN & G_IO_OUT); + cond &= ~(G_IO_IN | G_IO_OUT); handler_info *info = (handler_info *)data; id = info->id; -- 2.7.4 From 59055558123e4d54d1407c48fa11a80575121092 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Fri, 4 Aug 2017 10:34:49 +0900 Subject: [PATCH 13/16] sensord: add null check after malloc Change-Id: I6f01e8f46be1e08999bc0f35871fc675ba7ebeb8 Signed-off-by: kibak.yoon --- src/sensor/pedometer/pedometer_sensor.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/sensor/pedometer/pedometer_sensor.cpp b/src/sensor/pedometer/pedometer_sensor.cpp index bbd9b67..d16bdcc 100644 --- a/src/sensor/pedometer/pedometer_sensor.cpp +++ b/src/sensor/pedometer/pedometer_sensor.cpp @@ -103,6 +103,7 @@ int pedometer_sensor::get_data(sensor_data_t **data, int *len) { sensor_data_t *sensor_data; sensor_data = (sensor_data_t *)malloc(sizeof(sensor_data_t)); + retvm_if(!sensor_data, -ENOMEM, "Failed to allocate memory"); sensor_data->accuracy = SENSOR_ACCURACY_GOOD; sensor_data->timestamp = m_time; -- 2.7.4 From 27411362d91803c8c0ab6e8d3688592b141e8037 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Fri, 4 Aug 2017 10:09:34 +0900 Subject: [PATCH 14/16] sensord: version 4.0.2 Change-Id: Id1c588dda1ae5d446f158afeea96b7d94bfff82e 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 d081956..b96b6fe 100644 --- a/packaging/sensord.spec +++ b/packaging/sensord.spec @@ -1,6 +1,6 @@ Name: sensord Summary: Sensor daemon -Version: 4.0.1 +Version: 4.0.2 Release: 1 Group: System/Sensor Framework License: Apache-2.0 -- 2.7.4 From 7c9813c0be7c00cf1feaf1718f01ecfde399892a Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Wed, 9 Aug 2017 19:39:52 +0900 Subject: [PATCH 15/16] sensord: fix double-free issue - If the server is terminated unexpectedly, sensor library releases the memory of channel automatically. At that time, if client calls disconnect(), double-free problem is invoked. - Because it is right situation to call disconnect() explicitly, the auto-release code should be removed. Change-Id: I31894354eb2d11549dbe0382ce865de6beea89d4 Signed-off-by: kibak.yoon --- src/shared/channel_event_handler.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/shared/channel_event_handler.cpp b/src/shared/channel_event_handler.cpp index 6ca1e25..cc8a2bb 100644 --- a/src/shared/channel_event_handler.cpp +++ b/src/shared/channel_event_handler.cpp @@ -46,14 +46,12 @@ bool channel_event_handler::handle(int fd, event_condition condition) if (condition & (EVENT_HUP)) { m_ch->disconnect(); - delete m_ch; m_ch = NULL; return false; } if (!m_ch->read_sync(msg, false)) { m_ch->disconnect(); - delete m_ch; m_ch = NULL; return false; } -- 2.7.4 From ff891178a7a8147538ff5de57fb2987555440689 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Thu, 10 Aug 2017 19:23:25 +0900 Subject: [PATCH 16/16] sensorctl: clean up log macros of sensorctl Change-Id: I7aff53066afae3659700f37ac32a536d39c53c62 Signed-off-by: kibak.yoon --- src/sensorctl/log.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/sensorctl/log.h b/src/sensorctl/log.h index 00cefbd..6bb1130 100644 --- a/src/sensorctl/log.h +++ b/src/sensorctl/log.h @@ -39,12 +39,17 @@ do { \ #define _E(fmt, args...) \ do { \ - g_print("\x1B[31m" fmt "\033[0m", ##args); \ + g_print(_RED fmt _RST, ##args); \ } while (0) #define _I(fmt, args...) \ do { \ - g_print("\x1B[32m" fmt "\033[0m", ##args); \ + g_print(_GRN fmt _RST, ##args); \ +} while (0) + +#define _W(fmt, args...) \ +do { \ + g_print(_YEL fmt _RST, ##args); \ } while (0) #define WARN_IF(expr, fmt, arg...) \ -- 2.7.4