From 46f32216111080aa6ac4c0e7875d0a30d72ffd88 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Tue, 15 Mar 2016 11:39:53 +0900 Subject: [PATCH] sensord: add sensord_flush internal API for batching Change-Id: I9ce1ec8259b7c592ce269451a74a9a018ee5700f Signed-off-by: kibak.yoon --- src/client/client.cpp | 36 ++++++++++++++++++++++++++++++++++++ src/client/command_channel.cpp | 33 +++++++++++++++++++++++++++++++++ src/client/command_channel.h | 2 ++ src/client/sensor_internal.h | 14 ++++++++++++++ src/server/command_worker.cpp | 29 +++++++++++++++++++++++++++++ src/server/command_worker.h | 1 + src/shared/command_common.h | 4 ++++ 7 files changed, 119 insertions(+) diff --git a/src/client/client.cpp b/src/client/client.cpp index 0a3024e..fcd4c0c 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -1116,3 +1116,39 @@ API bool sensord_get_data(int handle, unsigned int data_id, sensor_data_t* senso return true; } + +API bool sensord_flush(int handle) +{ + 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; + } + + return true; +} \ No newline at end of file diff --git a/src/client/command_channel.cpp b/src/client/command_channel.cpp index d1c91c3..ca6b025 100644 --- a/src/client/command_channel.cpp +++ b/src/client/command_channel.cpp @@ -684,3 +684,36 @@ bool command_channel::cmd_set_attribute_str(int attribute, const char* value, in 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 index b99891b..15b579e 100644 --- a/src/client/command_channel.h +++ b/src/client/command_channel.h @@ -49,6 +49,8 @@ public: 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 value_len); + bool cmd_flush(void); + private: csocket m_command_socket; int m_client_id; diff --git a/src/client/sensor_internal.h b/src/client/sensor_internal.h index 94a05b5..cd1db11 100644 --- a/src/client/sensor_internal.h +++ b/src/client/sensor_internal.h @@ -355,6 +355,20 @@ bool sensord_send_command(int handle, const char *command, int command_len); bool sensord_get_data(int handle, unsigned int data_id, sensor_data_t* sensor_data); /** + * @brief flush sensor data from a connected sensor + * + * @param[in] handle a handle represensting a connected context sensor. + * @return true on success, otherwise false. + */ +bool sensord_flush(int handle); + +typedef void (*sensor_external_command_cb_t)(int handle, const char* data, int data_cnt, void *user_data); + +int sensord_external_connect(const char *key, sensor_external_command_cb_t cb, void *user_data); +bool sensord_external_disconnect(int handle); +bool sensord_external_post(int handle, unsigned long long timestamp, const float* data, int data_cnt); + +/** * @} */ diff --git a/src/server/command_worker.cpp b/src/server/command_worker.cpp index c8804d7..1f87342 100644 --- a/src/server/command_worker.cpp +++ b/src/server/command_worker.cpp @@ -87,6 +87,7 @@ void command_worker::init_cmd_handlers(void) 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; } void command_worker::get_sensor_list(int permissions, cpacket &sensor_list) @@ -812,6 +813,34 @@ out: 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 [0x%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; diff --git a/src/server/command_worker.h b/src/server/command_worker.h index c3ce538..3e01883 100644 --- a/src/server/command_worker.h +++ b/src/server/command_worker.h @@ -73,6 +73,7 @@ private: 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); diff --git a/src/shared/command_common.h b/src/shared/command_common.h index 48dd750..4a73f1d 100644 --- a/src/shared/command_common.h +++ b/src/shared/command_common.h @@ -46,6 +46,7 @@ enum packet_type_t { CMD_GET_DATA, CMD_SET_ATTRIBUTE_INT, CMD_SET_ATTRIBUTE_STR, + CMD_FLUSH, CMD_CNT, }; @@ -126,6 +127,9 @@ typedef struct { char value[0]; } cmd_set_attribute_str_t; +typedef struct { +} cmd_flush_t; + #define CHANNEL_MAGIC_NUM 0xCAFECAFE typedef struct { -- 2.7.4