8313c4d53646b302c259f25d6467652469c236df
[platform/core/system/sensord.git] / src / shared / sensor_utils.cpp
1 /*
2  * sensord
3  *
4  * Copyright (c) 2017 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
20 #include "sensor_utils.h"
21
22 #include <glib.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <string.h>
28 #include <stddef.h>
29 #include <map>
30
31 #include <sensor_types.h>
32
33 #ifndef PATH_MAX
34 #define PATH_MAX 256
35 #endif
36
37 /* TODO: move and define string type to sensor_type.h */
38 static std::map<sensor_type_t, const char *> types = {
39         {UNKNOWN_SENSOR,                 "http://tizen.org/sensor/unknown"},
40         {ALL_SENSOR,                     "http://tizen.org/sensor/all"},
41         {ACCELEROMETER_SENSOR,           "http://tizen.org/sensor/accelerometer"},
42         {GRAVITY_SENSOR,                 "http://tizen.org/sensor/gravity"},
43         {LINEAR_ACCEL_SENSOR,            "http://tizen.org/sensor/linear_acceleration"},
44         {GEOMAGNETIC_SENSOR,             "http://tizen.org/sensor/magnetic"},
45         {ROTATION_VECTOR_SENSOR,         "http://tizen.org/sensor/rotation_vector"},
46         {ORIENTATION_SENSOR,             "http://tizen.org/sensor/orientation"},
47         {GYROSCOPE_SENSOR,               "http://tizen.org/sensor/gyroscope"},
48         {LIGHT_SENSOR,                   "http://tizen.org/sensor/light"},
49         {PROXIMITY_SENSOR,               "http://tizen.org/sensor/proximity"},
50         {PRESSURE_SENSOR,                "http://tizen.org/sensor/pressure"},
51         {ULTRAVIOLET_SENSOR,             "http://tizen.org/sensor/ultraviolet"},
52         {TEMPERATURE_SENSOR,             "http://tizen.org/sensor/temperature"},
53         {HUMIDITY_SENSOR,                "http://tizen.org/sensor/humidity"},
54         {HRM_SENSOR,                     "http://tizen.org/sensor/heart_rate_monitor"},
55         {HRM_LED_GREEN_SENSOR,           "http://tizen.org/sensor/heart_rate_monitor.led_green"},
56         {HRM_LED_IR_SENSOR,              "http://tizen.org/sensor/heart_rate_monitor.led_ir"},
57         {HRM_LED_RED_SENSOR,             "http://tizen.org/sensor/heart_rate_monitor.led_red"},
58         {GYROSCOPE_UNCAL_SENSOR,         "http://tizen.org/sensor/gyroscope.uncalibrated"},
59         {GEOMAGNETIC_UNCAL_SENSOR,       "http://tizen.org/sensor/geomagnetic.uncalibrated"},
60         {GYROSCOPE_RV_SENSOR,            "http://tizen.org/sensor/gyroscope_rotation_vector"},
61         {GEOMAGNETIC_RV_SENSOR,          "http://tizen.org/sensor/geomagnetic_rotation_vector"},
62
63         {HUMAN_PEDOMETER_SENSOR,         "http://tizen.org/sensor/human_pedometer"},
64         {HUMAN_SLEEP_MONITOR_SENSOR,     "http://tizen.org/sensor/human_sleep_monitor"},
65         {HUMAN_SLEEP_DETECTOR_SENSOR,    "http://tizen.org/sensor/human_sleep_detector"},
66         {HUMAN_STRESS_MONITOR_SENSOR,    "http://tizen.org/sensor/human_stress_monitor"},
67
68         {EXERCISE_WALKING_SENSOR,        "http://tizen.org/sensor/exercise.walking"},
69         {EXERCISE_RUNNING_SENSOR,        "http://tizen.org/sensor/exercise.running"},
70         {EXERCISE_HIKING_SENSOR,         "http://tizen.org/sensor/exercise.hiking"},
71         {EXERCISE_CYCLING_SENSOR,        "http://tizen.org/sensor/exercise.cycling"},
72         {EXERCISE_ELLIPTICAL_SENSOR,     "http://tizen.org/sensor/exercise.elliptical"},
73         {EXERCISE_INDOOR_CYCLING_SENSOR, "http://tizen.org/sensor/exercise.indoor_cycling"},
74         {EXERCISE_ROWING_SENSOR,         "http://tizen.org/sensor/exercise.rowing"},
75         {EXERCISE_STEPPER_SENSOR,        "http://tizen.org/sensor/exercise.stepper"},
76
77         {EXTERNAL_EXERCISE_SENSOR,       "http://tizen.org/sensor/external_exercise"},
78
79         {FUSION_SENSOR,                  "http://tizen.org/sensor/fusion"},
80         {AUTO_ROTATION_SENSOR,           "http://tizen.org/sensor/auto_rotation"},
81         {AUTO_BRIGHTNESS_SENSOR,         "http://tizen.org/sensor/auto_brightness"},
82
83         {GESTURE_MOVEMENT_SENSOR,        "http://tizen.org/sensor/gesture_movement"},
84         {GESTURE_WRIST_UP_SENSOR,        "http://tizen.org/sensor/gesture_wrist_up"},
85         {GESTURE_WRIST_DOWN_SENSOR,      "http://tizen.org/sensor/gesture_wrist_down"},
86         {GESTURE_MOVEMENT_STATE_SENSOR,  "http://tizen.org/sensor/gesture_movement_state"},
87         {GESTURE_FACE_DOWN_SENSOR,       "http://tizen.org/sensor/gesture_face_down"},
88
89         {ACTIVITY_TRACKER_SENSOR,        "http://tizen.org/sensor/activity_tracker"},
90         {ACTIVITY_LEVEL_MONITOR_SENSOR,  "http://tizen.org/sensor/activity_level_monitor"},
91         {GPS_BATCH_SENSOR,               "http://tizen.org/sensor/gps_batch"},
92
93         {HRM_CTRL_SENSOR,                "http://tizen.org/sensor/hrm_ctrl"},
94
95         {WEAR_STATUS_SENSOR,             "http://tizen.org/sensor/wear_status"},
96         {WEAR_ON_MONITOR_SENSOR,         "http://tizen.org/sensor/wear_on_monitor"},
97         {NO_MOVE_DETECTOR_SENSOR,        "http://tizen.org/sensor/no_move_detector"},
98         {RESTING_HR_SENSOR,              "http://tizen.org/sensor/resting_hr"},
99         {STEP_LEVEL_MONITOR_SENSOR,      "http://tizen.org/sensor/step_level_monitor"},
100         {EXERCISE_STANDALONE_SENSOR,     "http://tizen.org/sensor/exercise_standalone"},
101         {EXERCISE_HR_SENSOR,             "http://tizen.org/sensor/exercise_hr"},
102         {WORKOUT_SENSOR,                 "http://tizen.org/sensor/workout"},
103         {CYCLE_MONITOR_SENSOR,           "http://tizen.org/sensor/cycle_monitor"},
104         {STAIR_TRACKER_SENSOR,           "http://tizen.org/sensor/stair_tracker"},
105         {PRESSURE_INDICATOR_SENSOR,      "http://tizen.org/sensor/pressure_indicator"},
106         {PRESSURE_ALERT_SENSOR,          "http://tizen.org/sensor/pressure_alert"},
107         {HR_CALORIE_SENSOR,              "http://tizen.org/sensor/hr_calorie"},
108
109         {CONTEXT_SENSOR,                 "http://tizen.org/sensor/context"},
110         {MOTION_SENSOR,                  "http://tizen.org/sensor/motion"},
111         {PIR_SENSOR,                     "http://tizen.org/sensor/pir"},
112         {PIR_LONG_SENSOR,                "http://tizen.org/sensor/pir_long"},
113         {DUST_SENSOR,                    "http://tizen.org/sensor/dust"},
114         {THERMOMETER_SENSOR,             "http://tizen.org/sensor/thermometer"},
115         {PEDOMETER_SENSOR,               "http://tizen.org/sensor/pedometer"},
116         {FLAT_SENSOR,                    "http://tizen.org/sensor/flat"},
117         {HRM_RAW_SENSOR,                 "http://tizen.org/sensor/hrm_raw"},
118         {TILT_SENSOR,                    "http://tizen.org/sensor/tilt"},
119         {RV_RAW_SENSOR,                  "http://tizen.org/sensor/rv_raw"},
120         {GSR_SENSOR,                     "http://tizen.org/sensor/gsr"},
121         {SIMSENSE_SENSOR,                "http://tizen.org/sensor/simsense"},
122         {PPG_SENSOR,                     "http://tizen.org/sensor/ppg"},
123 };
124
125 const char *sensor::utils::get_uri(sensor_type_t type)
126 {
127         auto it = types.find(type);
128         if (it == types.end())
129                 return "Unknown Type";
130         return it->second;
131 }
132
133 unsigned long long sensor::utils::get_timestamp(void)
134 {
135         struct timespec t;
136         clock_gettime(CLOCK_MONOTONIC, &t);
137         return ((unsigned long long)(t.tv_sec)*1000000000LL + t.tv_nsec) / 1000;
138 }
139
140 unsigned long long sensor::utils::get_timestamp(timeval *t)
141 {
142         if (!t)
143                 return 0;
144
145         return ((unsigned long long)(t->tv_sec)*1000000LL +t->tv_usec);
146 }
147
148 #ifdef _DEBUG
149 bool sensor::utils::get_proc_name(pid_t pid, char *process_name)
150 {
151         FILE *fp;
152         char buf[NAME_MAX];
153         char filename[PATH_MAX];
154
155         sprintf(filename, "/proc/%d/stat", pid);
156         fp = fopen(filename, "r");
157
158         if (fp == NULL)
159                 return false;
160
161         if (fscanf(fp, "%*s (%[^)]", buf) < 1) {
162                 fclose(fp);
163                 return false;
164         }
165
166         strncpy(process_name, buf, NAME_MAX-1);
167         process_name[NAME_MAX-1] = '\0';
168         fclose(fp);
169
170         return true;
171 }
172 #else
173 bool sensor::utils::get_proc_name(pid_t pid, char *process_name)
174 {
175         char buf[NAME_MAX];
176
177         if (snprintf(buf, sizeof(buf), "%d process", pid) < 1) {
178                 return false;
179         }
180
181         strncpy(process_name, buf, NAME_MAX-1);
182         process_name[NAME_MAX-1] = '\0';
183
184         return true;
185 }
186 #endif
187
188 const char* sensor::utils::get_client_name(void)
189 {
190         const int pid_string_size = 10;
191         static pid_t pid = -1;
192         static char client_name[NAME_MAX + pid_string_size];
193
194         char proc_name[NAME_MAX];
195
196         if (pid == -1)
197         {
198                 pid = getpid();
199                 get_proc_name(pid, proc_name);
200                 snprintf(client_name, sizeof(client_name), "%s(%d)", proc_name, pid);
201         }
202
203         return client_name;
204 }
205
206 std::vector<std::string> sensor::utils::tokenize(const std::string &in, const char *delim)
207 {
208         std::vector<std::string> tokens;
209         char *input = g_strdup(in.c_str());
210
211         char *save = NULL;
212         char *token = strtok_r(input, delim, &save);
213
214         while (token != NULL) {
215                 tokens.push_back(token);
216                 token = strtok_r(NULL, delim, &save);
217         }
218
219         g_free(input);
220         return tokens;
221 }