2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <ContextProvider.h>
20 #include <ProviderList.h>
21 #include "ProviderLoader.h"
23 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
27 typedef ContextProvider* (*create_t)(const char *subject);
29 std::map<const char*, const char*, CompareSubjectName> ProviderLoader::__providerLibMap;
31 ProviderLoader::ProviderLoader() :
36 ProviderLoader::~ProviderLoader()
41 ContextProvider* ProviderLoader::load(const char *subject)
43 ProviderLibMap::iterator it = __providerLibMap.find(subject);
44 if (it == __providerLibMap.end()) {
45 _W("No provider for '%s'", subject);
49 std::string path(_LIBDIR_ LIB_PREFIX);
50 path = path + it->second + LIB_EXTENSION;
52 return __load(path.c_str(), subject);
55 ContextProvider* ProviderLoader::__load(const char *soPath, const char *subject)
57 _SI("Load '%s' from '%s'", subject, soPath);
59 __soHandle = g_module_open(soPath, G_MODULE_BIND_LAZY);
60 IF_FAIL_RETURN_TAG(__soHandle, NULL, _E, "%s", g_module_error());
64 if (!g_module_symbol(__soHandle, "CreateProvider", &symbol) || symbol == NULL) {
65 _W("%s", g_module_error());
66 g_module_close(__soHandle);
71 create_t create = reinterpret_cast<create_t>(symbol);
73 ContextProvider *prvd = create(subject);
75 _W("No provider for '%s'", subject);
76 g_module_close(__soHandle);
84 void ProviderLoader::__unload()
89 g_module_close(__soHandle);
93 bool ProviderLoader::init()
95 int size = ARRAY_SIZE(subjectLibraryList);
97 for (int i = 0; i < size; ++i) {
98 __providerLibMap[subjectLibraryList[i].subject] = subjectLibraryList[i].library;
99 _SD("'%s' -> '%s'", subjectLibraryList[i].subject, subjectLibraryList[i].library);
105 bool ProviderLoader::popTriggerTemplate(std::string &subject, int &operation, Json &attribute, Json &option)
108 static int size = ARRAY_SIZE(triggerTemplateList);
113 subject = triggerTemplateList[i].subject;
114 operation = triggerTemplateList[i].operation;
115 attribute = triggerTemplateList[i].attribute;
116 option = triggerTemplateList[i].option;