Add sensor APIs related to batch event 59/217959/1
authorBoram Bae <boram21.bae@samsung.com>
Tue, 12 Nov 2019 09:43:13 +0000 (18:43 +0900)
committerBoram Bae <boram21.bae@samsung.com>
Mon, 18 Nov 2019 01:13:27 +0000 (10:13 +0900)
Change-Id: I90041ed2f66469814c752cc9364d165d972a1a44
Signed-off-by: Boram Bae <boram21.bae@samsung.com>
include/sensor.h
src/sensor.cpp
src/sensor_provider.cpp

index 565c0d1..0e822e5 100644 (file)
@@ -748,6 +748,18 @@ typedef void (*sensor_event_cb)(sensor_h sensor, sensor_event_s *event, void *da
 
 
 /**
+ * @brief   Called when a sensor event occurs.
+ * @since_tizen 5.5
+ *
+ * @param[in] sensor    The corresponding sensor handle
+ * @param[in] event     A sensor event
+ * @param[in] data      The user data had passed to sensor_listener_set_event_cb()
+ *
+ * @pre     The sensor needs to be started regarding a listener handle, using sensor_listener_start().
+ */
+typedef void (*sensor_events_cb)(sensor_h sensor, sensor_event_s* events[], int events_count, void *data);
+
+/**
  * @brief   Called when the accuracy of a sensor changes.
  * @details Sensors can be affected by the environment.
  *          For example, #SENSOR_MAGNETIC is sensitive to any surrounding objects that can influence
@@ -884,6 +896,38 @@ int sensor_listener_unset_event_cb(sensor_listener_h listener);
 
 
 /**
+ * @brief   Registers the callback function to be invoked when sensor events are delivered via a sensor listener.
+ * @since_tizen 5.5
+ *
+ * @param[in]   listener    A listener handle
+ * @param[in]   callback    A callback function to attach with the @a listener handle
+ * @param[in]   data        A user data to be passed to the callback function
+ *
+ * @return  #SENSOR_ERROR_NONE on success, otherwise a negative error value
+ * @retval  #SENSOR_ERROR_NONE                 Successful
+ * @retval  #SENSOR_ERROR_INVALID_PARAMETER    Invalid parameter
+ * @retval  #SENSOR_ERROR_OPERATION_FAILED     Operation failed
+ *
+ * @see sensor_listener_unset_events_cb()
+ */
+int sensor_listener_set_events_cb(sensor_listener_h listener, sensor_events_cb callback, void *data);
+
+/**
+ * @brief   Unregisters the sensor events callback function attached to a given sensor listener.
+ * @since_tizen 5.5
+ *
+ * @param[in]   listener    A listener handle
+ *
+ * @return  #SENSOR_ERROR_NONE on success, otherwise a negative error value
+ * @retval  #SENSOR_ERROR_NONE                 Successful
+ * @retval  #SENSOR_ERROR_INVALID_PARAMETER    Invalid parameter
+ * @retval  #SENSOR_ERROR_OPERATION_FAILED     Operation failed
+ *
+ * @see     sensor_listener_set_events_cb()
+ */
+int sensor_listener_unset_events_cb(sensor_listener_h listener);
+
+/**
  * @brief   Registers the callback function to be invoked when the accuracy of a sensor changes.
  * @details In addition to sensor_event_cb(), sensor_accuracy_changed_cb() also can be attached
  *          to sensor listeners. With this accuracy callback function, applications can be notified
@@ -1319,7 +1363,7 @@ int sensor_provider_set_interval_changed_cb(sensor_provider_h provider,
  * @since_tizen 4.0
  *
  * @param[in]   provider    The sensor provider handle
- * @param[out]  event       The sensor event
+ * @param[in]   event       The sensor event
  *
  * @return  #SENSOR_ERROR_NONE on success, otherwise a negative error value
  * @retval  #SENSOR_ERROR_NONE                 Successful
@@ -1329,6 +1373,22 @@ int sensor_provider_set_interval_changed_cb(sensor_provider_h provider,
 int sensor_provider_publish(sensor_provider_h provider, sensor_event_s event);
 
 /**
+ * @brief   Publishes a sensor event through the declared sensor.
+ * @details This function publishes a sensor's data to its listeners.
+ * @since_tizen 5.5
+ *
+ * @param[in]   provider    The sensor provider handle
+ * @param[in]   events      The sensor events
+ * @param[in]   count       The number of event
+ *
+ * @return  #SENSOR_ERROR_NONE on success, otherwise a negative error value
+ * @retval  #SENSOR_ERROR_NONE                 Successful
+ * @retval  #SENSOR_ERROR_INVALID_PARAMETER    Invalid parameter
+ * @retval  #SENSOR_ERROR_OPERATION_FAILED     Operation failed
+ */
+int sensor_provider_publish_events(sensor_provider_h provider, sensor_event_s events[], int count);
+
+/**
  * @}
  */
 
index f22b3de..fc3e283 100644 (file)
@@ -92,6 +92,113 @@ static int sensor_connect(sensor_h sensor, sensor_listener_h listener)
        return id;
 }
 
+static void sensor_callback(sensor_t sensor, unsigned int event_type, sensor_data_t *data, void *user_data)
+{
+       sensor_event_s *event;
+       sensor_listener_h listener;
+       listener = (sensor_listener_h)user_data;
+
+       if (!sensor || !listener->callback)
+               return;
+
+       event = (sensor_event_s *)data;
+
+       ((sensor_event_cb) listener->callback)(sensor, event, listener->user_data);
+       return;
+}
+
+static void sensor_events_callback(sensor_t sensor, unsigned int event_type, sensor_data_t* datas[], int events_count, void *user_data)
+{
+       sensor_event_s **events;
+       sensor_listener_h listener;
+       listener = (sensor_listener_h)user_data;
+
+       if (!sensor || !listener->callback)
+               return;
+
+       events = (sensor_event_s **)datas;
+
+       ((sensor_events_cb) listener->callback)(sensor, events, events_count, listener->user_data);
+       return;
+}
+
+static inline int sensor_listener_unset_event_cb_impl(sensor_listener_h listener, bool is_events_callback)
+{
+       int id;
+       int type;
+       unsigned int event_id;
+
+       _D("called sensor_listener_unset_event_cb_impl : listener[%p]", listener);
+
+       if (!listener)
+               return SENSOR_ERROR_INVALID_PARAMETER;
+
+       if (listener->magic != SENSOR_LISTENER_MAGIC)
+               return SENSOR_ERROR_INVALID_PARAMETER;
+
+       id = listener->id;
+       type = (int)listener->type;
+       event_id = type << SENSOR_SHIFT_TYPE | 0x1;
+
+       int ret = false;
+       if (is_events_callback) {
+               ret = sensord_unregister_events(id, event_id);
+       } else {
+               ret = sensord_unregister_event(id, event_id);
+       }
+
+       if (!ret)
+               return SENSOR_ERROR_OPERATION_FAILED;
+
+       listener->callback = NULL;
+       listener->user_data = NULL;
+
+       _D("success sensor_unregister_event");
+
+       return SENSOR_ERROR_NONE;
+}
+
+static inline int sensor_listener_set_event_cb_impl(sensor_listener_h listener,
+               unsigned int interval, void* callback, bool is_events_callback, void *user_data)
+{
+       int id;
+       unsigned int event_id;
+       unsigned int batch_latency;
+
+       if (!listener || !callback)
+               return SENSOR_ERROR_INVALID_PARAMETER;
+
+       if (listener->magic != SENSOR_LISTENER_MAGIC)
+               return SENSOR_ERROR_INVALID_PARAMETER;
+
+       _D("called sensor_listener_set_event_cb_impl : listener[%p], interval[%u], callback[%p], user_data[%p], id[%d]",
+                       listener, interval, callback, user_data, listener->id);
+
+       id = listener->id;
+       event_id = (listener->type) << SENSOR_SHIFT_TYPE | 0x1;
+       batch_latency = listener->batch_latency;
+
+       listener->callback = (void *)callback;
+       listener->user_data = user_data;
+
+       int ret = false;
+       if (is_events_callback) {
+               ret = sensord_register_events(id, event_id, batch_latency, sensor_events_callback, listener);
+       } else {
+               ret = sensord_register_event(id, event_id, interval, batch_latency, sensor_callback, listener);
+       }
+
+       if (!ret) {
+               listener->callback = NULL;
+               listener->user_data = NULL;
+               return SENSOR_ERROR_OPERATION_FAILED;
+       }
+
+       _D("success sensor_listener_set_event");
+
+       return SENSOR_ERROR_NONE;
+}
+
 int sensor_is_supported(sensor_type_e type, bool *supported)
 {
        int result;
@@ -405,84 +512,29 @@ int sensor_listener_stop(sensor_listener_h listener)
        return SENSOR_ERROR_NONE;
 }
 
-static void sensor_callback(sensor_t sensor, unsigned int event_type, sensor_data_t *data, void *user_data)
-{
-       sensor_event_s *event;
-       sensor_listener_h listener;
-       listener = (sensor_listener_h)user_data;
-
-       if (!sensor || !listener->callback)
-               return;
-
-       event = (sensor_event_s *)data;
-
-       ((sensor_event_cb) listener->callback)(sensor, event, listener->user_data);
-       return;
-}
-
 int sensor_listener_set_event_cb(sensor_listener_h listener,
                unsigned int interval, sensor_event_cb callback, void *user_data)
 {
-       int id;
-       unsigned int event_id;
-       unsigned int batch_latency;
-
-       if (!listener || !callback)
-               return SENSOR_ERROR_INVALID_PARAMETER;
-
-       if (listener->magic != SENSOR_LISTENER_MAGIC)
-               return SENSOR_ERROR_INVALID_PARAMETER;
-
-       _D("called sensor_listener_set_event : listener[%p], interval[%u], callback[%p], user_data[%p], id[%d]",
-                       listener, interval, callback, user_data, listener->id);
-
-       id = listener->id;
-       event_id = (listener->type) << SENSOR_SHIFT_TYPE | 0x1;
-       batch_latency = listener->batch_latency;
-
-       listener->callback = (void *)callback;
-       listener->user_data = user_data;
-
-       if (!sensord_register_event(id, event_id, interval, batch_latency,
-                               sensor_callback, listener)) {
-               listener->callback = NULL;
-               listener->user_data = NULL;
-
-               return SENSOR_ERROR_OPERATION_FAILED;
-       }
-
-       _D("success sensor_listener_set_event");
-
-       return SENSOR_ERROR_NONE;
+       _D("called sensor_listener_set_event_cb : listener[%p]", listener);
+       return sensor_listener_set_event_cb_impl(listener, interval, (void*) callback, false, user_data);
 }
 
 int sensor_listener_unset_event_cb(sensor_listener_h listener)
 {
-       int id;
-       int type;
-       unsigned int event_id;
-
-       _D("called sensor_unregister_event : listener[%p]", listener);
-
-       if (!listener)
-               return SENSOR_ERROR_INVALID_PARAMETER;
-
-       if (listener->magic != SENSOR_LISTENER_MAGIC)
-               return SENSOR_ERROR_INVALID_PARAMETER;
-
-       id = listener->id;
-       type = (int)listener->type;
-       event_id = type << SENSOR_SHIFT_TYPE | 0x1;
-
-       if (!sensord_unregister_event(id, event_id))
-               return SENSOR_ERROR_OPERATION_FAILED;
-
-       listener->callback = NULL;
-       listener->user_data = NULL;
+       _D("called sensor_listener_unset_event_cb : listener[%p]", listener);
+       return sensor_listener_unset_event_cb_impl(listener, false);
+}
 
-       _D("success sensor_unregister_event");
+int sensor_listener_set_events_cb(sensor_listener_h listener, sensor_events_cb callback, void *user_data)
+{
+       _D("called sensor_listener_set_events_cb : listener[%p]", listener);
+       return sensor_listener_set_event_cb_impl(listener, 0, (void*) callback, true, user_data);
+}
 
-       return SENSOR_ERROR_NONE;
+int sensor_listener_unset_events_cb(sensor_listener_h listener)
+{
+       _D("called sensor_listener_unset_events_cb : listener[%p]", listener);
+       return sensor_listener_unset_event_cb_impl(listener, true);
 }
 
 static void accuracy_changed_callback(sensor_t sensor,
index a33aa72..a3be72f 100644 (file)
@@ -179,12 +179,25 @@ int sensor_provider_publish(sensor_provider_h provider, sensor_event_s event)
        RETV_IF(!provider, SENSOR_ERROR_INVALID_PARAMETER);
 
        int result;
-       sensor_data_t data;
-       memcpy(&data, &event, sizeof(data));
+       sensor_data_t* data = (sensor_data_t *)&event;
+
+       result = sensord_provider_publish(provider->sensor, *data);
 
-       result = sensord_provider_publish(provider->sensor, data);
        RETV_IF(result == -EINVAL, SENSOR_ERROR_INVALID_PARAMETER);
        RETV_IF(result < 0, SENSOR_ERROR_OPERATION_FAILED);
 
        return SENSOR_ERROR_NONE;
 }
+
+int sensor_provider_publish_events(sensor_provider_h provider, sensor_event_s events[], int count)
+{
+       RETV_IF(!provider, SENSOR_ERROR_INVALID_PARAMETER);
+
+       int result;
+       result = sensord_provider_publish_events(provider->sensor, (sensor_data_t*) events, count);
+
+       RETV_IF(result == -EINVAL, SENSOR_ERROR_INVALID_PARAMETER);
+       RETV_IF(result < 0, SENSOR_ERROR_OPERATION_FAILED);
+
+       return SENSOR_ERROR_NONE;
+}
\ No newline at end of file