LCOV - code coverage report
Current view: top level - nnstreamer-edge-0.2.6/src/libnnstreamer-edge - nnstreamer-edge-event.c (source / functions) Coverage Total Hit
Test: NNStreamer-edge 0.2.6-1 nnstreamer/nnstreamer-edge#121619a22eefb07aef74ab765d1eb0ec59b1416e Lines: 93.4 % 121 113
Test Date: 2025-06-06 05:20:40 Functions: 100.0 % 8 8

            Line data    Source code
       1              : /* SPDX-License-Identifier: Apache-2.0 */
       2              : /**
       3              :  * Copyright (C) 2022 Samsung Electronics Co., Ltd. All Rights Reserved.
       4              :  *
       5              :  * @file   nnstreamer-edge-event.c
       6              :  * @date   7 Sep 2022
       7              :  * @brief  Util functions for nnstreamer edge event.
       8              :  * @see    https://github.com/nnstreamer/nnstreamer
       9              :  * @author Gichan Jang <gichan2.jang@samsung.com>
      10              :  * @bug    No known bugs except for NYI items
      11              :  */
      12              : 
      13              : #include "nnstreamer-edge-data.h"
      14              : #include "nnstreamer-edge-event.h"
      15              : #include "nnstreamer-edge-log.h"
      16              : #include "nnstreamer-edge-util.h"
      17              : 
      18              : /**
      19              :  * @brief Internal data structure for edge event.
      20              :  */
      21              : typedef struct
      22              : {
      23              :   uint32_t magic;
      24              :   pthread_mutex_t lock;
      25              :   nns_edge_event_e event;
      26              :   nns_edge_raw_data_s data;
      27              : } nns_edge_event_s;
      28              : 
      29              : /**
      30              :  * @brief Util function to invoke event callback.
      31              :  */
      32              : int
      33           68 : nns_edge_event_invoke_callback (nns_edge_event_cb event_cb, void *user_data,
      34              :     nns_edge_event_e event, void *data, nns_size_t data_len,
      35              :     nns_edge_data_destroy_cb destroy_cb)
      36              : {
      37              :   nns_edge_event_h event_h;
      38              :   int ret;
      39              : 
      40              :   /* If event callback is null, return ok. */
      41           68 :   if (!event_cb) {
      42           15 :     nns_edge_logw ("The event callback is null, do nothing!");
      43           68 :     return NNS_EDGE_ERROR_NONE;
      44              :   }
      45              : 
      46           53 :   ret = nns_edge_event_create (event, &event_h);
      47           53 :   if (ret != NNS_EDGE_ERROR_NONE) {
      48            0 :     nns_edge_loge ("Failed to create new edge event.");
      49            0 :     return ret;
      50              :   }
      51              : 
      52           53 :   if (data) {
      53           43 :     ret = nns_edge_event_set_data (event_h, data, data_len, destroy_cb);
      54           43 :     if (ret != NNS_EDGE_ERROR_NONE) {
      55            0 :       nns_edge_loge ("Failed to handle edge event due to invalid event data.");
      56            0 :       goto error;
      57              :     }
      58              :   }
      59              : 
      60           53 :   ret = event_cb (event_h, user_data);
      61           53 :   if (ret != NNS_EDGE_ERROR_NONE)
      62            0 :     nns_edge_logw ("The event callback returns error (%d).", ret);
      63              : 
      64           53 : error:
      65           53 :   nns_edge_event_destroy (event_h);
      66           53 :   return ret;
      67              : }
      68              : 
      69              : /**
      70              :  * @brief Create nnstreamer edge event.
      71              :  */
      72              : int
      73           74 : nns_edge_event_create (nns_edge_event_e event, nns_edge_event_h * event_h)
      74              : {
      75              :   nns_edge_event_s *ee;
      76              : 
      77           74 :   if (!event_h) {
      78            1 :     nns_edge_loge ("Invalid param, event_h should not be null.");
      79            1 :     return NNS_EDGE_ERROR_INVALID_PARAMETER;
      80              :   }
      81              : 
      82           73 :   if (event <= NNS_EDGE_EVENT_UNKNOWN) {
      83            1 :     nns_edge_loge ("Invalid param, given event type is invalid.");
      84            1 :     return NNS_EDGE_ERROR_INVALID_PARAMETER;
      85              :   }
      86              : 
      87           72 :   ee = (nns_edge_event_s *) calloc (1, sizeof (nns_edge_event_s));
      88           72 :   if (!ee) {
      89            0 :     nns_edge_loge ("Failed to allocate memory for edge event.");
      90            0 :     return NNS_EDGE_ERROR_OUT_OF_MEMORY;
      91              :   }
      92              : 
      93           72 :   nns_edge_lock_init (ee);
      94           72 :   nns_edge_handle_set_magic (ee, NNS_EDGE_MAGIC);
      95           72 :   ee->event = event;
      96              : 
      97           72 :   *event_h = ee;
      98           72 :   return NNS_EDGE_ERROR_NONE;
      99              : }
     100              : 
     101              : /**
     102              :  * @brief Destroy nnstreamer edge event.
     103              :  */
     104              : int
     105           74 : nns_edge_event_destroy (nns_edge_event_h event_h)
     106              : {
     107              :   nns_edge_event_s *ee;
     108              : 
     109           74 :   ee = (nns_edge_event_s *) event_h;
     110              : 
     111           74 :   if (!nns_edge_handle_is_valid (ee)) {
     112            2 :     nns_edge_loge ("Invalid param, given edge event is invalid.");
     113            2 :     return NNS_EDGE_ERROR_INVALID_PARAMETER;
     114              :   }
     115              : 
     116           72 :   nns_edge_lock (ee);
     117           72 :   nns_edge_handle_set_magic (ee, NNS_EDGE_MAGIC_DEAD);
     118              : 
     119           72 :   if (ee->data.destroy_cb)
     120            1 :     ee->data.destroy_cb (ee->data.data);
     121              : 
     122           72 :   nns_edge_unlock (ee);
     123           72 :   nns_edge_lock_destroy (ee);
     124              : 
     125           72 :   SAFE_FREE (ee);
     126           72 :   return NNS_EDGE_ERROR_NONE;
     127              : }
     128              : 
     129              : /**
     130              :  * @brief Set event data.
     131              :  */
     132              : int
     133           51 : nns_edge_event_set_data (nns_edge_event_h event_h, void *data,
     134              :     nns_size_t data_len, nns_edge_data_destroy_cb destroy_cb)
     135              : {
     136              :   nns_edge_event_s *ee;
     137              : 
     138           51 :   ee = (nns_edge_event_s *) event_h;
     139              : 
     140           51 :   if (!nns_edge_handle_is_valid (ee)) {
     141            2 :     nns_edge_loge ("Invalid param, given edge event is invalid.");
     142            2 :     return NNS_EDGE_ERROR_INVALID_PARAMETER;
     143              :   }
     144              : 
     145           49 :   if (!data || data_len <= 0) {
     146            2 :     nns_edge_loge ("Invalid param, data should not be null.");
     147            2 :     return NNS_EDGE_ERROR_INVALID_PARAMETER;
     148              :   }
     149              : 
     150           47 :   nns_edge_lock (ee);
     151              : 
     152              :   /* Clear old data and set new one. */
     153           47 :   if (ee->data.destroy_cb)
     154            0 :     ee->data.destroy_cb (ee->data.data);
     155              : 
     156           47 :   ee->data.data = data;
     157           47 :   ee->data.data_len = data_len;
     158           47 :   ee->data.destroy_cb = destroy_cb;
     159              : 
     160           47 :   nns_edge_unlock (ee);
     161           47 :   return NNS_EDGE_ERROR_NONE;
     162              : }
     163              : 
     164              : /**
     165              :  * @brief Get event data.
     166              :  */
     167              : int
     168            5 : nns_edge_event_get_data (nns_edge_event_h event_h, void **data,
     169              :     nns_size_t * data_len)
     170              : {
     171              :   nns_edge_event_s *ee;
     172              : 
     173            5 :   ee = (nns_edge_event_s *) event_h;
     174              : 
     175            5 :   if (!nns_edge_handle_is_valid (ee)) {
     176            2 :     nns_edge_loge ("Invalid param, given edge event is invalid.");
     177            2 :     return NNS_EDGE_ERROR_INVALID_PARAMETER;
     178              :   }
     179              : 
     180            3 :   if (!data || !data_len) {
     181            2 :     nns_edge_loge ("Invalid param, data and len should not be null.");
     182            2 :     return NNS_EDGE_ERROR_INVALID_PARAMETER;
     183              :   }
     184              : 
     185            1 :   nns_edge_lock (ee);
     186              : 
     187            1 :   *data = ee->data.data;
     188            1 :   *data_len = ee->data.data_len;
     189              : 
     190            1 :   nns_edge_unlock (ee);
     191            1 :   return NNS_EDGE_ERROR_NONE;
     192              : }
     193              : 
     194              : /**
     195              :  * @brief Get the nnstreamer edge event type.
     196              :  */
     197              : int
     198           57 : nns_edge_event_get_type (nns_edge_event_h event_h, nns_edge_event_e * event)
     199              : {
     200              :   nns_edge_event_s *ee;
     201              : 
     202           57 :   ee = (nns_edge_event_s *) event_h;
     203              : 
     204           57 :   if (!nns_edge_handle_is_valid (ee)) {
     205            2 :     nns_edge_loge ("Invalid param, given edge event is invalid.");
     206            2 :     return NNS_EDGE_ERROR_INVALID_PARAMETER;
     207              :   }
     208              : 
     209           55 :   if (!event) {
     210            1 :     nns_edge_loge ("Invalid param, event should not be null.");
     211            1 :     return NNS_EDGE_ERROR_INVALID_PARAMETER;
     212              :   }
     213              : 
     214           54 :   nns_edge_lock (ee);
     215              : 
     216           54 :   *event = ee->event;
     217              : 
     218           54 :   nns_edge_unlock (ee);
     219           54 :   return NNS_EDGE_ERROR_NONE;
     220              : }
     221              : 
     222              : /**
     223              :  * @brief Parse edge event (NNS_EDGE_EVENT_NEW_DATA_RECEIVED) and get received data.
     224              :  */
     225              : int
     226           45 : nns_edge_event_parse_new_data (nns_edge_event_h event_h,
     227              :     nns_edge_data_h * data_h)
     228              : {
     229              :   nns_edge_event_s *ee;
     230           45 :   int ret = NNS_EDGE_ERROR_NONE;
     231              : 
     232           45 :   ee = (nns_edge_event_s *) event_h;
     233              : 
     234           45 :   if (!nns_edge_handle_is_valid (ee)) {
     235            2 :     nns_edge_loge ("Invalid param, given edge event is invalid.");
     236            2 :     return NNS_EDGE_ERROR_INVALID_PARAMETER;
     237              :   }
     238              : 
     239           43 :   if (!data_h) {
     240            1 :     nns_edge_loge ("Invalid param, data_h should not be null.");
     241            1 :     return NNS_EDGE_ERROR_INVALID_PARAMETER;
     242              :   }
     243              : 
     244           42 :   nns_edge_lock (ee);
     245              : 
     246           42 :   if (ee->event == NNS_EDGE_EVENT_NEW_DATA_RECEIVED) {
     247           41 :     ret = nns_edge_data_copy ((nns_edge_data_h) ee->data.data, data_h);
     248              :   } else {
     249            1 :     nns_edge_loge ("The edge event has invalid event type.");
     250            1 :     ret = NNS_EDGE_ERROR_INVALID_PARAMETER;
     251              :   }
     252              : 
     253           42 :   nns_edge_unlock (ee);
     254           42 :   return ret;
     255              : }
     256              : 
     257              : /**
     258              :  * @brief Parse edge event (NNS_EDGE_EVENT_CAPABILITY) and get capability string.
     259              :  */
     260              : int
     261            5 : nns_edge_event_parse_capability (nns_edge_event_h event_h, char **capability)
     262              : {
     263              :   nns_edge_event_s *ee;
     264            5 :   int ret = NNS_EDGE_ERROR_NONE;
     265              : 
     266            5 :   ee = (nns_edge_event_s *) event_h;
     267              : 
     268            5 :   if (!nns_edge_handle_is_valid (ee)) {
     269            2 :     nns_edge_loge ("Invalid param, given edge event is invalid.");
     270            2 :     return NNS_EDGE_ERROR_INVALID_PARAMETER;
     271              :   }
     272              : 
     273            3 :   if (!capability) {
     274            1 :     nns_edge_loge ("Invalid param, capability should not be null.");
     275            1 :     return NNS_EDGE_ERROR_INVALID_PARAMETER;
     276              :   }
     277              : 
     278            2 :   nns_edge_lock (ee);
     279              : 
     280            2 :   if (ee->event == NNS_EDGE_EVENT_CAPABILITY) {
     281            1 :     *capability = nns_edge_strdup (ee->data.data);
     282              :   } else {
     283            1 :     nns_edge_loge ("The edge event has invalid event type.");
     284            1 :     ret = NNS_EDGE_ERROR_INVALID_PARAMETER;
     285              :   }
     286              : 
     287            2 :   nns_edge_unlock (ee);
     288            2 :   return ret;
     289              : }
        

Generated by: LCOV version 2.0-1