Line data Source code
1 : // SPDX-License-Identifier: Apache-2.0 2 : /** 3 : * Copyright (C) 2020 Parichay Kapoor <pk.kapoor@samsung.com> 4 : * 5 : * @file nnstreamer-capi-tizen-feature-check.cpp 6 : * @date 7 August 2020 7 : * @brief NNTrainer/C-API Tizen dependent functions. 8 : * @see https://github.com/nnstreamer/nntrainer 9 : * @author MyungJoo Ham <myungjoo.ham@samsung.com> 10 : * @author Parichay Kapoor <pk.kapoor@samsung.com> 11 : * @bug No known bugs except for NYI items 12 : */ 13 : 14 : #if !defined(__TIZEN__) || !defined(__FEATURE_CHECK_SUPPORT__) 15 : #error "This file can be included only in Tizen." 16 : #endif 17 : 18 : #include <pthread.h> 19 : #include <system_info.h> 20 : 21 : #include <nntrainer_internal.h> 22 : 23 : #ifdef __cplusplus 24 : extern "C" { 25 : #endif /* __cplusplus */ 26 : /** 27 : * @brief Tizen ML feature. 28 : */ 29 : #define ML_TRAIN_FEATURE_PATH "tizen.org/feature/machine_learning.training" 30 : 31 : /// please note that this is a improvised measure for nnstreamer/api#110 32 : /// proper way will need exposing this particular function in ml-api side 33 : #if (TIZENVERSION >= 7) && (TIZENVERSION < 9999) 34 : 35 : /** 36 : * @brief tizen set feature state from ml api side 37 : * 38 : * @param state -1 NOT checked yet, 0 supported, 1 not supported 39 : * @return int 0 if success 40 : */ 41 : int _ml_tizen_set_feature_state(ml_feature_e feature, int state); 42 : #define ml_api_set_feature_state(...) _ml_tizen_set_feature_state(__VA_ARGS__) 43 : 44 : #elif (TIZENVERSION >= 6) && (TIZENVERSION < 9999) 45 : 46 : /** 47 : * @brief tizen set feature state from ml api side 48 : * 49 : * @param state -1 NOT checked yet, 0 supported, 1 not supported 50 : * @return int 0 if success 51 : */ 52 : int ml_tizen_set_feature_state(ml_feature_e feature, int state); 53 : #define ml_api_set_feature_state(...) ml_tizen_set_feature_state(__VA_ARGS__) 54 : 55 : #elif (TIZENVERSION <= 5) 56 : 57 : #warning Tizen version under 5 does not support setting features for unittest 58 : #define ml_api_set_feature_state(...) 59 : 60 : #else /* TIZENVERSION */ 61 : #error Tizen version is not defined. 62 : #endif /* TIZENVERSION */ 63 : 64 : /** 65 : * @brief Internal struct to control tizen feature support 66 : * (machine_learning.training). -1: Not checked yet, 0: Not supported, 1: 67 : * Supported 68 : */ 69 : typedef struct _feature_info_s { 70 : pthread_mutex_t mutex; 71 : feature_state_t feature_state; 72 : 73 : _feature_info_s() : feature_state(NOT_CHECKED_YET) { 74 : pthread_mutex_init(&mutex, NULL); 75 : } 76 : 77 8 : ~_feature_info_s() { pthread_mutex_destroy(&mutex); } 78 : } feature_info_s; 79 : 80 : static feature_info_s feature_info; 81 : 82 : /** 83 : * @brief Set the feature status of machine_learning.training. 84 : */ 85 36 : void ml_train_tizen_set_feature_state(ml_feature_e feature, 86 : feature_state_t state) { 87 36 : pthread_mutex_lock(&feature_info.mutex); 88 : 89 36 : ml_api_set_feature_state(feature, (int)state); 90 : 91 : /** 92 : * Update feature status 93 : * -1: Not checked yet, 0: Not supported, 1: Supported 94 : */ 95 36 : feature_info.feature_state = state; 96 : 97 36 : pthread_mutex_unlock(&feature_info.mutex); 98 36 : } 99 : 100 : /** 101 : * @brief Checks whether machine_learning.training feature is enabled or not. 102 : */ 103 600 : int ml_tizen_get_feature_enabled(void) { 104 600 : int ret; 105 600 : int feature_enabled; 106 : 107 600 : pthread_mutex_lock(&feature_info.mutex); 108 600 : feature_enabled = feature_info.feature_state; 109 600 : pthread_mutex_unlock(&feature_info.mutex); 110 : 111 600 : if (NOT_SUPPORTED == feature_enabled) { 112 0 : ml_loge("machine_learning.training NOT supported"); 113 0 : return ML_ERROR_NOT_SUPPORTED; 114 600 : } else if (NOT_CHECKED_YET == feature_enabled) { 115 0 : bool ml_train_supported = false; 116 0 : ret = 117 0 : system_info_get_platform_bool(ML_TRAIN_FEATURE_PATH, &ml_train_supported); 118 0 : if (0 == ret) { 119 0 : if (false == ml_train_supported) { 120 0 : ml_loge("machine_learning.training NOT supported"); 121 0 : ml_train_tizen_set_feature_state(ML_FEATURE_TRAINING, NOT_SUPPORTED); 122 0 : return ML_ERROR_NOT_SUPPORTED; 123 : } 124 : 125 0 : ml_train_tizen_set_feature_state(ML_FEATURE_TRAINING, SUPPORTED); 126 : } else { 127 0 : switch (ret) { 128 0 : case SYSTEM_INFO_ERROR_INVALID_PARAMETER: 129 0 : ml_loge("failed to get feature value because feature key is not vaild"); 130 : ret = ML_ERROR_NOT_SUPPORTED; 131 : break; 132 : 133 0 : case SYSTEM_INFO_ERROR_IO_ERROR: 134 0 : ml_loge("failed to get feature value because of input/output error"); 135 : ret = ML_ERROR_NOT_SUPPORTED; 136 : break; 137 : 138 0 : case SYSTEM_INFO_ERROR_PERMISSION_DENIED: 139 0 : ml_loge("failed to get feature value because of permission denied"); 140 : ret = ML_ERROR_PERMISSION_DENIED; 141 : break; 142 : 143 0 : default: 144 0 : ml_loge("failed to get feature value because of unknown error"); 145 : ret = ML_ERROR_NOT_SUPPORTED; 146 : break; 147 : } 148 0 : return ret; 149 : } 150 : } 151 : 152 : return ML_ERROR_NONE; 153 : } 154 : 155 : #ifdef __cplusplus 156 : } 157 : #endif /* __cplusplus */