4 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include "sensor_utils.h"
25 #include <sys/types.h>
31 #include <sensor_types.h>
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"},
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"},
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"},
77 {EXTERNAL_EXERCISE_SENSOR, "http://tizen.org/sensor/external_exercise"},
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"},
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"},
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"},
93 {HRM_CTRL_SENSOR, "http://tizen.org/sensor/hrm_ctrl"},
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"},
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"},
125 const char *sensor::utils::get_uri(sensor_type_t type)
127 auto it = types.find(type);
128 if (it == types.end())
129 return "Unknown Type";
133 unsigned long long sensor::utils::get_timestamp(void)
136 clock_gettime(CLOCK_MONOTONIC, &t);
137 return ((unsigned long long)(t.tv_sec)*1000000000LL + t.tv_nsec) / 1000;
140 unsigned long long sensor::utils::get_timestamp(timeval *t)
145 return ((unsigned long long)(t->tv_sec)*1000000LL +t->tv_usec);
149 bool sensor::utils::get_proc_name(pid_t pid, char *process_name)
153 char filename[PATH_MAX];
155 sprintf(filename, "/proc/%d/stat", pid);
156 fp = fopen(filename, "r");
161 if (fscanf(fp, "%*s (%[^)]", buf) < 1) {
166 strncpy(process_name, buf, NAME_MAX-1);
167 process_name[NAME_MAX-1] = '\0';
173 bool sensor::utils::get_proc_name(pid_t pid, char *process_name)
177 if (snprintf(buf, sizeof(buf), "%d process", pid) < 1) {
181 strncpy(process_name, buf, NAME_MAX-1);
182 process_name[NAME_MAX-1] = '\0';
188 const char* sensor::utils::get_client_name(void)
190 const int pid_string_size = 10;
191 static pid_t pid = -1;
192 static char client_name[NAME_MAX + pid_string_size];
194 char proc_name[NAME_MAX];
199 get_proc_name(pid, proc_name);
200 snprintf(client_name, sizeof(client_name), "%s(%d)", proc_name, pid);
206 std::vector<std::string> sensor::utils::tokenize(const std::string &in, const char *delim)
208 std::vector<std::string> tokens;
209 char *input = g_strdup(in.c_str());
212 char *token = strtok_r(input, delim, &save);
214 while (token != NULL) {
215 tokens.push_back(token);
216 token = strtok_r(NULL, delim, &save);