Merge "sensord: delete batch latency/attribute when client is terminated unexpectedly...
[platform/core/system/sensord.git] / src / server / external_sensor_record.cpp
1 /*
2  * sensord
3  *
4  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19 #include <external_sensor_record.h>
20
21 using std::string;
22
23 external_sensor_record::external_sensor_record()
24 : m_client_id(0)
25 , m_pid(-1)
26 {
27 }
28
29 external_sensor_record::~external_sensor_record()
30 {
31         close_command_socket();
32 }
33
34 bool external_sensor_record::add_usage(sensor_id_t sensor)
35 {
36         if (!m_usages.insert(sensor).second) {
37                 _E("Sensor[%#x] is already registered", sensor);
38                 return false;
39         }
40
41         return true;
42 }
43
44 bool external_sensor_record::remove_usage(sensor_id_t sensor)
45 {
46         if (!m_usages.erase(sensor)) {
47                 _E("Sensor[%#x] is not found", sensor);
48                 return false;
49         }
50
51         return true;
52 }
53
54 bool external_sensor_record::has_usage(void)
55 {
56         return !m_usages.empty();
57 }
58
59 bool external_sensor_record::has_usage(sensor_id_t sensor)
60 {
61         auto it_usage = m_usages.find(sensor);
62
63         return (it_usage != m_usages.end());
64 }
65
66 void external_sensor_record::set_client_id(int client_id)
67 {
68         m_client_id = client_id;
69 }
70
71 void external_sensor_record::set_client_info(pid_t pid, const string &name)
72 {
73         char client_info[NAME_MAX + 32];
74         m_pid = pid;
75
76         snprintf(client_info, sizeof(client_info), "%s[pid=%d, id=%d]", name.c_str(), m_pid, m_client_id);
77         m_client_info.assign(client_info);
78 }
79
80 const char* external_sensor_record::get_client_info(void)
81 {
82         return m_client_info.c_str();
83 }
84
85 void external_sensor_record::set_command_socket(const csocket &socket)
86 {
87         m_command_socket = socket;
88 }
89
90 void external_sensor_record::get_command_socket(csocket &socket)
91 {
92         socket = m_command_socket;
93 }
94
95 bool external_sensor_record::close_command_socket(void)
96 {
97         return m_command_socket.close();
98 }
99