update for iot-5.0
[apps/native/st-things-co2-meter.git] / src / sensor-data.c
index 9caec1a..1435e18 100644 (file)
@@ -1,18 +1,20 @@
-/*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+/* ****************************************************************
  *
- * Licensed under the Flora License, Version 1.1 (the License);
+ * Copyright 2017 Samsung Electronics All Rights Reserved.
+ *
+ * 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://floralicense.org/license/
+ *      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,
+ * 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 <stdlib.h>
 #include <pthread.h>
@@ -24,6 +26,7 @@ struct __sensor_data_s {
        union {
                int int_val;
                unsigned int uint_val;
+               bool b_val;
                double d_val;
                char *str_val;
        } value;
@@ -36,7 +39,7 @@ sensor_data *sensor_data_new(sensor_data_type_e type)
        retv_if(type == SENSOR_DATA_TYPE_NONE, NULL);
 
        data = calloc(1, sizeof(sensor_data));
-       retv_if(type == SENSOR_DATA_TYPE_NONE, NULL);
+       retv_if(!data, NULL);
 
        data->type = type;
        pthread_mutex_init(&data->mutex, NULL);
@@ -83,6 +86,18 @@ int sensor_data_set_uint(sensor_data *data, unsigned int value)
        return 0;
 }
 
+int sensor_data_set_bool(sensor_data *data, bool value)
+{
+       retv_if(!data, -1);
+       retv_if(data->type != SENSOR_DATA_TYPE_BOOL, -1);
+
+       pthread_mutex_lock(&data->mutex);
+       data->value.b_val = value;
+       pthread_mutex_unlock(&data->mutex);
+
+       return 0;
+}
+
 int sensor_data_set_double(sensor_data *data, double value)
 {
        retv_if(!data, -1);
@@ -104,6 +119,8 @@ int sensor_data_set_string(sensor_data *data, const char *value, unsigned int si
        retv_if(size == 0, -1);
 
        temp = strndup(value, size);
+       retv_if(!temp, -1);
+
        pthread_mutex_lock(&data->mutex);
        free(data->value.str_val);
        data->value.str_val = temp;
@@ -112,6 +129,13 @@ int sensor_data_set_string(sensor_data *data, const char *value, unsigned int si
        return 0;
 }
 
+sensor_data_type_e sensor_data_get_type(sensor_data *data)
+{
+       retv_if(!data, SENSOR_DATA_TYPE_NONE);
+
+       return data->type;
+}
+
 int sensor_data_get_int(sensor_data *data, int *value)
 {
        retv_if(!data, -1);
@@ -138,6 +162,19 @@ int sensor_data_get_uint(sensor_data *data, unsigned int *value)
        return 0;
 }
 
+int sensor_data_get_bool(sensor_data *data, bool *value)
+{
+       retv_if(!data, -1);
+       retv_if(!value, -1);
+       retv_if(data->type != SENSOR_DATA_TYPE_BOOL, -1);
+
+       pthread_mutex_lock(&data->mutex);
+       *value = data->value.b_val;
+       pthread_mutex_unlock(&data->mutex);
+
+       return 0;
+}
+
 int sensor_data_get_double(sensor_data *data, double *value)
 {
        retv_if(!data, -1);