Change peripheral io APIs
[apps/native/position-finder-server.git] / src / controller.c
index 27c3015..92be7f3 100644 (file)
@@ -1,72 +1,80 @@
 /*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
  *
  * Contact: Jin Yoon <jinny.yoon@samsung.com>
  *          Geunsun Lee <gs86.lee@samsung.com>
  *          Eunyoung Lee <ey928.lee@samsung.com>
  *          Junkyu Han <junkyu.han@samsung.com>
  *
- * Licensed under the Apache License, Version 2.0 (the "License");
+ * 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://www.apache.org/licenses/LICENSE-2.0
+ * 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,
+ * 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 <tizen.h>
-#include <Ecore.h>
-#include <service_app.h>
 #include <unistd.h>
 #include <glib.h>
+#include <Ecore.h>
+#include <tizen.h>
+#include <service_app.h>
+
+#include <iotcon.h> // Please remove this after test
 
 #include "log.h"
+#include "resource.h"
+#include "connectivity.h"
 #include "controller.h"
-#include "model.h"
 
-#define I2C_BUS_1 0x1
-#define GPIO_NOT_USED -1
 #define GPIO_ULTRASONIC_TRIG_NUM_1 20
 #define GPIO_ULTRASONIC_ECHO_NUM_1 21
-#define GPIO_INFRARED_MOTION_NUM_1 4
-#define I2C_ILLUMINANCE_FIRST_PIN_1 3
+#define MULTIPLE_SENSOR_NUMBER 5
+#define CONNECTIVITY_KEY "opened"
 
 typedef struct app_data_s {
-       Ecore_Timer *getter_timer[PIN_MAX];
+       Ecore_Timer *getter_timer;
+       connectivity_resource_s *resource_info;
 } app_data;
 
-static Eina_Bool _infrared_motion_getter_timer(void *data)
+static Eina_Bool control_sensors_cb(void *data)
 {
-       int value = 0;
-
-       retv_if(model_read_infrared_motion_sensor(GPIO_INFRARED_MOTION_NUM_1, &value) == -1, ECORE_CALLBACK_CANCEL);
-       _I("Infrared Motion Value is [%d]", value);
-
-       return ECORE_CALLBACK_RENEW;
-}
-
-static Eina_Bool _ultrasonic_getter_timer(void *data)
-{
-       double value = 0;
-
-       retv_if(model_read_ultrasonic_sensor(GPIO_ULTRASONIC_TRIG_NUM_1, GPIO_ULTRASONIC_ECHO_NUM_1, &value) == -1, ECORE_CALLBACK_CANCEL);
-       _I("Ultra Sonic Distance is [%d cm]", value);
-
-       return ECORE_CALLBACK_RENEW;
-}
+       uint32_t value[MULTIPLE_SENSOR_NUMBER] = { 0, };
+       int total = 0;
+       int gpio_num[MULTIPLE_SENSOR_NUMBER] = { 5, 6, 13, 19, 26 };
+       int i = 0;
+       app_data *ad = data;
 
-static Eina_Bool _illuminance_getter_timer(void *data)
-{
-       int value = 0;
+       /**
+        * This is the case when a number of the same sensors are installed.
+        * Each of the five infrared motion sensors will receive the value.
+        */
+       for (i = 0; i < MULTIPLE_SENSOR_NUMBER; i++) {
+               /**
+                * Infrared motion sensor outputs 1 if motion is detected, or 0 if motion is not detected.
+                */
+               if (resource_read_infrared_motion_sensor(gpio_num[i], &value[i]) == -1) {
+                       _E("Failed to get Infrared Motion value [GPIO:%d]", gpio_num[i]);
+                       continue;
+               }
+               /**
+                * If at least one of the five infrared motion sensors detects motion (1),
+                * it is judged that there is a person (total == 1).
+                */
+               total |= value[i];
+       }
+       _I("[5:%d] | [6:%d] | [13:%d] | [19:%d] | [26:%d] = [Total:%d]", value[0], value[1], value[2], value[3], value[4], total);
 
-       retv_if(model_read_illuminance_sensor(I2C_BUS_1, &value) == -1, ECORE_CALLBACK_CANCEL);
-       _I("Ultra Sonic Distance is [%d lux]", value);
+       /**
+        * Notifies specific clients that resource's attributes have changed.
+        */
+       if (connectivity_notify_bool(ad->resource_info, CONNECTIVITY_KEY, total) == -1)
+               _E("Cannot notify message");
 
        return ECORE_CALLBACK_RENEW;
 }
@@ -74,25 +82,27 @@ static Eina_Bool _illuminance_getter_timer(void *data)
 static bool service_app_create(void *data)
 {
        app_data *ad = data;
-
-       /* One Pin Sensor */
-       ad->getter_timer[GPIO_INFRARED_MOTION_NUM_1] = ecore_timer_add(3.0, _infrared_motion_getter_timer, ad);
-       if (!ad->getter_timer[GPIO_INFRARED_MOTION_NUM_1]) {
-               _D("Failed to add infrared motion getter timer");
-               return false;
-       }
-
-       /* Two Pins Sensor */
-       ad->getter_timer[GPIO_ULTRASONIC_TRIG_NUM_1] = ecore_timer_add(1.0, _ultrasonic_getter_timer, ad);
-       if (!ad->getter_timer[GPIO_ULTRASONIC_TRIG_NUM_1]) {
-               _D("Failed to add ultra sonic getter timer");
-               return false;
-       }
-
-       /* I2C Protocol Sensor */
-       ad->getter_timer[I2C_ILLUMINANCE_FIRST_PIN_1] = ecore_timer_add(1.0, _illuminance_getter_timer, ad);
-       if (!ad->getter_timer[I2C_ILLUMINANCE_FIRST_PIN_1]) {
-               _D("Failed to add ultra sonic getter timer");
+       int ret = -1;
+
+       /**
+        * No modification required!!!
+        * Access only when modifying internal functions.
+        */
+       controller_init_internal_functions();
+
+       /**
+        * Create a connectivity resource and registers the resource in server.
+        */
+       ret = connectivity_set_resource("/door/1", "org.tizen.door", &ad->resource_info);
+       if (ret == -1) _E("Cannot broadcast resource");
+
+       /**
+        * Creates a timer to call the given function in the given period of time.
+        * In the control_sensors_cb(), each sensor reads the measured value or writes a specific value to the sensor.
+        */
+       ad->getter_timer = ecore_timer_add(0.5f, control_sensors_cb, ad);
+       if (!ad->getter_timer) {
+               _E("Failed to add infrared motion getter timer");
                return false;
        }
 
@@ -104,13 +114,22 @@ static void service_app_terminate(void *data)
        app_data *ad = (app_data *)data;
 
        for (int i = 0; i < PIN_MAX; i++) {
-               if (ad->getter_timer[i]) {
-                       ecore_timer_del(ad->getter_timer[i]);
+               if (ad->getter_timer) {
+                       ecore_timer_del(ad->getter_timer);
                }
        }
-       model_close_infrared_motion_sensor(GPIO_INFRARED_MOTION_NUM_1);
-       model_close_ultrasonic_sensor(GPIO_ULTRASONIC_TRIG_NUM_1, GPIO_ULTRASONIC_ECHO_NUM_1);
-       model_close_illuminance_sensor();
+
+       /**
+        * Releases the resource about connectivity.
+        */
+       connectivity_unset_resource(ad->resource_info);
+
+       /**
+        * No modification required!!!
+        * Access only when modifying internal functions.
+        */
+       controller_fini_internal_functions();
+
        free(ad);
 }
 
@@ -147,6 +166,7 @@ int main(int argc, char* argv[])
        app_event_handler_h handlers[5] = {NULL, };
 
        ad = calloc(1, sizeof(app_data));
+       retv_if(!ad, -1);
 
        event_callback.create = service_app_create;
        event_callback.terminate = service_app_terminate;