Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include <dlfcn.h>
     18 
     19 #include "base/logging.h"
     20 #include "core/native_frame.h"
     21 #include "core/native_program.h"
     22 
     23 #include <string>
     24 #include <vector>
     25 
     26 namespace android {
     27 namespace filterfw {
     28 
     29 NativeProgram::NativeProgram()
     30     : lib_handle_(NULL),
     31       init_function_(NULL),
     32       setvalue_function_(NULL),
     33       getvalue_function_(NULL),
     34       process_function_(NULL),
     35       reset_function_(NULL),
     36       teardown_function_(NULL),
     37       user_data_(NULL) {
     38 }
     39 
     40 NativeProgram::~NativeProgram() {
     41   if (lib_handle_)
     42     dlclose(lib_handle_);
     43 }
     44 
     45 bool NativeProgram::OpenLibrary(const std::string& lib_name) {
     46   if (!lib_handle_) {
     47     lib_handle_ = dlopen(lib_name.c_str(), RTLD_NOW);
     48     if (!lib_handle_) {
     49       ALOGE("NativeProgram: Error opening library: '%s': %s", lib_name.c_str(), dlerror());
     50       return false;
     51     }
     52     return true;
     53   }
     54   return false;
     55 }
     56 
     57 bool NativeProgram::BindProcessFunction(const std::string& func_name) {
     58   if (!lib_handle_)
     59     return false;
     60   process_function_ = reinterpret_cast<ProcessFunctionPtr>(dlsym(lib_handle_, func_name.c_str()));
     61   if (!process_function_) {
     62     ALOGE("NativeProgram: Could not find process function symbol: '%s'!", func_name.c_str());
     63     return false;
     64   }
     65   return true;
     66 }
     67 
     68 bool NativeProgram::BindInitFunction(const std::string& func_name) {
     69   if (!lib_handle_)
     70     return false;
     71   init_function_ = reinterpret_cast<InitFunctionPtr>(dlsym(lib_handle_, func_name.c_str()));
     72   return init_function_ != NULL;
     73 }
     74 
     75 bool NativeProgram::BindSetValueFunction(const std::string& func_name) {
     76   if (!lib_handle_)
     77     return false;
     78   setvalue_function_ = reinterpret_cast<SetValueFunctionPtr>(dlsym(lib_handle_, func_name.c_str()));
     79   return setvalue_function_ != NULL;
     80 }
     81 
     82 bool NativeProgram::BindGetValueFunction(const std::string& func_name) {
     83   if (!lib_handle_)
     84     return false;
     85   getvalue_function_ = reinterpret_cast<GetValueFunctionPtr>(dlsym(lib_handle_, func_name.c_str()));
     86   return getvalue_function_ != NULL;
     87 }
     88 
     89 bool NativeProgram::BindResetFunction(const std::string& func_name) {
     90   if (!lib_handle_)
     91     return false;
     92   reset_function_ = reinterpret_cast<ResetFunctionPtr>(dlsym(lib_handle_, func_name.c_str()));
     93   return reset_function_ != NULL;
     94 }
     95 
     96 bool NativeProgram::BindTeardownFunction(const std::string& func_name) {
     97   if (!lib_handle_)
     98     return false;
     99   teardown_function_ = reinterpret_cast<TeardownFunctionPtr>(dlsym(lib_handle_, func_name.c_str()));
    100   return teardown_function_ != NULL;
    101 }
    102 
    103 bool NativeProgram::CallProcess(const std::vector<const char*>& inputs,
    104                                 const std::vector<int>& input_sizes,
    105                                 char* output,
    106                                 int output_size) {
    107   if (process_function_) {
    108     return process_function_(const_cast<const char**>(&inputs[0]),
    109                              &input_sizes[0],
    110                              inputs.size(),
    111                              output,
    112                              output_size,
    113                              user_data_) == 1;
    114   }
    115   return false;
    116 }
    117 
    118 bool NativeProgram::CallInit() {
    119   if (init_function_) {
    120     init_function_(&user_data_);
    121     return true;
    122   }
    123   return false;
    124 }
    125 
    126 bool NativeProgram::CallSetValue(const std::string& key, const std::string& value) {
    127   if (setvalue_function_) {
    128     setvalue_function_(key.c_str(), value.c_str(), user_data_);
    129     return true;
    130   }
    131   return false;
    132 }
    133 
    134 std::string NativeProgram::CallGetValue(const std::string& key) {
    135   if (getvalue_function_) {
    136     static const int buffer_size = 1024;
    137     char result[buffer_size];
    138     result[buffer_size - 1] = '\0';
    139     getvalue_function_(key.c_str(), result, buffer_size, user_data_);
    140     return std::string(result);
    141   }
    142   return std::string();
    143 }
    144 
    145 bool NativeProgram::CallReset() {
    146   if (reset_function_) {
    147     reset_function_(user_data_);
    148     return true;
    149   }
    150   return false;
    151 }
    152 
    153 bool NativeProgram::CallTeardown() {
    154   if (teardown_function_) {
    155     teardown_function_(user_data_);
    156     return true;
    157   }
    158   return false;
    159 }
    160 
    161 } // namespace filterfw
    162 } // namespace android
    163