Divide code regarding Sensor
authorjunkyu han <junkyu.han@samsung.com>
Tue, 9 Jan 2018 08:49:05 +0000 (17:49 +0900)
committerjunkyu han <junkyu.han@samsung.com>
Tue, 9 Jan 2018 08:49:05 +0000 (17:49 +0900)
inc/resource.h [new file with mode: 0644]
inc/resource_accelerometer.h [new file with mode: 0644]
src/resource.c [new file with mode: 0644]
src/resource_accelerometer.c [new file with mode: 0644]

diff --git a/inc/resource.h b/inc/resource.h
new file mode 100644 (file)
index 0000000..6ce5527
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Contact: junkyu Han <junkyu.han@samsung.com>
+ *
+ * Licensed under the Flora License, Version 1.1 (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/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * 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.
+ */
+
+#ifndef __CAR_CONTROLLER_RESOURCE_H__
+#define __CAR_CONTROLLER_RESOURCE_H__
+
+#include <sensor.h>
+
+#define RESOURCE_MAX 35
+
+typedef struct __resource_info {
+       sensor_h sensor;
+       sensor_listener_h listener;
+       void (*start_sensing)(void);
+       void (*stop_sensing)(void);
+       void (*finalize)(void);
+} resource_info;
+
+void resource_stop_sensing(void);
+void resource_start_sensing(void);
+void resource_info_free(resource_info *sensor_info);
+resource_info *resource_info_new(void);
+void resource_finalize_sensor(sensor_type_e type);
+void resource_finalize_all_sensor(void);
+int resource_initialize_sensor(sensor_type_e type);
+
+#endif /* __CAR_CONTROLLER_RESOURCE_H__ */
diff --git a/inc/resource_accelerometer.h b/inc/resource_accelerometer.h
new file mode 100644 (file)
index 0000000..3363cf7
--- /dev/null
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Contact: junkyu Han <junkyu.han@samsung.com>
+ *
+ * Licensed under the Flora License, Version 1.1 (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/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * 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.
+ */
+
+#ifndef __CAR_CONTROLLER_RESOURCE_ACCELEROMETER_H__
+#define __CAR_CONTROLLER_RESOURCE_ACCELEROMETER_H__
+
+#include "resource.h"
+
+resource_info *resource_accelerometer_init_sensor(resource_info *sensor_info);
+
+#endif /* __CAR_CONTROLLER_RESOURCE_ACCELEROMETER_H__ */
diff --git a/src/resource.c b/src/resource.c
new file mode 100644 (file)
index 0000000..dad5928
--- /dev/null
@@ -0,0 +1,112 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Contact: junkyu Han <junkyu.han@samsung.com>
+ *
+ * Licensed under the Flora License, Version 1.1 (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/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * 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 <resource_accelerometer.h>
+#include <resource.h>
+#include <stdlib.h>
+
+#include "log.h"
+
+static resource_info *__resource_info_new(void);
+
+static resource_info *resources[RESOURCE_MAX] = { NULL, };
+
+int resource_initialize_sensor(sensor_type_e type)
+{
+       _D("Initialize Resource");
+
+       resource_info *res_info = __resource_info_new();
+
+       switch (type) {
+       case SENSOR_ACCELEROMETER:
+               resources[type] = resource_accelerometer_init_sensor(res_info);
+               if (resources[type] == NULL) {
+                       _E("Failed to initialize Accelerometer Sensor");
+               }
+               break;
+       default:
+               break;
+       }
+
+       return 0;
+}
+
+void resource_finalize_all_sensor(void)
+{
+       int i = 0;
+
+       for (i = 0; i < RESOURCE_MAX; i++) {
+               if (resources[i])
+                       resources[i]->finalize();
+       }
+}
+
+void resource_finalize_sensor(sensor_type_e type)
+{
+       if (resources[type])
+               resources[type]->finalize();
+}
+
+void resource_info_free(resource_info *sensor_info)
+{
+       if (sensor_info == NULL) {
+               _E("This sensor info is already freed");
+               return;
+       }
+
+       free(sensor_info);
+       sensor_info == NULL;
+}
+
+void resource_start_sensing(void)
+{
+       int i = 0;
+
+       for (i = 0; i < RESOURCE_MAX; i++)
+       {
+               if (resources[i])
+                       resources[i]->start_sensing();
+       }
+}
+
+void resource_stop_sensing(void)
+{
+       int i = 0;
+
+       for (i = 0; i < RESOURCE_MAX; i++)
+       {
+               if (resources[i])
+                       resources[i]->stop_sensing();
+       }
+}
+
+static resource_info *__resource_info_new(void)
+{
+       resource_info *sensor_info = (resource_info *)malloc(sizeof(resource_info));
+       if (sensor_info == NULL) {
+               _E("Failed to allocate memory for resource info");
+               return NULL;
+       }
+
+       sensor_info->sensor = NULL;
+       sensor_info->listener = NULL;
+       sensor_info->start_sensing = NULL;
+       sensor_info->stop_sensing = NULL;
+
+       return sensor_info;
+}
diff --git a/src/resource_accelerometer.c b/src/resource_accelerometer.c
new file mode 100644 (file)
index 0000000..15d5d59
--- /dev/null
@@ -0,0 +1,159 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Contact: junkyu Han <junkyu.han@samsung.com>
+ *
+ * Licensed under the Flora License, Version 1.1 (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/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * 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 <sensor.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "communication_center.h"
+#include "communication_by_udp.h"
+#include "resource.h"
+#include "view.h"
+#include "log.h"
+
+static void __finalize(void);
+static void __start_sensing(void);
+static void __stop_sensing(void);
+static void __sensor_value_changed_cb(sensor_h sensor, sensor_event_s *event, void *data);
+
+static struct __accel_info_s {
+       resource_info *sensor_info;
+} accel_info = {
+       .sensor_info = NULL,
+};
+
+resource_info *resource_accelerometer_init_sensor(resource_info *sensor_info)
+{
+       bool is_supported = false;
+       int ret = 0;
+
+       if (sensor_info == NULL) {
+               _E("There's no allocated memory for Accelerometer Sensor");
+               return NULL;
+       }
+
+       ret = sensor_is_supported(SENSOR_ACCELEROMETER, &is_supported);
+       if (ret != SENSOR_ERROR_NONE) {
+               _E("Failed to check sensor support");
+               goto ERROR;
+       }
+
+       if (!is_supported) {
+               _E("This Device Do Not Support Accelerometer Sensor");
+               goto ERROR;
+       }
+       _D("Accelerometer is available");
+
+       ret = sensor_get_default_sensor(SENSOR_ACCELEROMETER, &(sensor_info->sensor));
+       if (ret != SENSOR_ERROR_NONE) {
+               _E("Failed to get sensor handler");
+               goto ERROR;
+       }
+
+       ret = sensor_create_listener(sensor_info->sensor, &(sensor_info->listener));
+       if (ret != SENSOR_ERROR_NONE) {
+               _E("Failed to create sensor listener");
+               goto ERROR;
+       }
+
+       ret = sensor_listener_set_event_cb(sensor_info->listener, 1, __sensor_value_changed_cb, NULL);
+       if (ret != SENSOR_ERROR_NONE) {
+               _E("Failed to set sensor listener event");
+               goto ERROR;
+       }
+
+       sensor_info->finalize = __finalize;
+       sensor_info->start_sensing = __start_sensing;
+       sensor_info->stop_sensing = __stop_sensing;
+
+       accel_info.sensor_info = sensor_info;
+
+       return sensor_info;
+
+ERROR:
+       if (sensor_info->listener) {
+               ret = sensor_destroy_listener(sensor_info->listener);
+               if (ret != SENSOR_ERROR_NONE)
+                       _E("Failed to destroy sensor listener");
+       }
+
+       resource_info_free(sensor_info);
+
+       return NULL;
+}
+
+static void __finalize(void)
+{
+       int ret = 0;
+
+       ret = sensor_listener_unset_event_cb(accel_info.sensor_info->listener);
+       if (ret != SENSOR_ERROR_NONE) {
+               _E("Failed to unset sensor listener event");
+               return;
+       }
+
+       ret = sensor_listener_stop(accel_info.sensor_info->listener);
+       if (ret != SENSOR_ERROR_NONE) {
+               _E("Failed to stop sensor listener event");
+               return;
+       }
+
+       ret = sensor_destroy_listener(accel_info.sensor_info->listener);
+       if (ret != SENSOR_ERROR_NONE) {
+               _E("Failed to destroy sensor listener event");
+               return;
+       }
+
+       resource_info_free(accel_info.sensor_info);
+}
+
+static void __sensor_value_changed_cb(sensor_h sensor, sensor_event_s *event, void *data)
+{
+       _D("Time: [%llu], X: [%f], Y: [%f]", event->timestamp, event->values[0], event->values[1]);
+
+       commu_data_s temp = { NULL, NULL };
+
+       temp.x = event->values[0] * 100;
+       temp.y = event->values[1] * 100;
+
+       view_update_view_with_data((void *)&temp);
+       communication_center_send_data((void *)&temp);
+}
+
+static void __start_sensing(void)
+{
+       int ret = 0;
+
+       ret = sensor_listener_start(accel_info.sensor_info->listener);
+       if (ret != SENSOR_ERROR_NONE) {
+               _E("Failed to start sensor listener");
+               return;
+       }
+}
+
+static void __stop_sensing(void)
+{
+       int ret = 0;
+
+       ret = sensor_listener_stop(accel_info.sensor_info->listener);
+       if (ret != SENSOR_ERROR_NONE) {
+               _E("Failed to stop sensor listener event");
+               return;
+       }
+}
+