2 * Copyright (c) 2014 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.
21 #include <types_internal.h>
22 #include <context_mgr.h>
23 #include <place_context_provider.h>
24 #include "sub_provider_base.h"
27 #include "place_geofence/place_geofence.h"
30 #define CREATE_SUB_PROVIDER(subject, subprovider) do { \
31 ctx::sub_provider_base *sub = new ctx::subprovider(); \
32 if (sub->is_supported(NULL)) { \
33 subject_map[(subject)] = sub; \
35 _E("%s is not supported", subject); \
39 typedef std::map<std::string, ctx::sub_provider_base*> subject_map_t;
40 static subject_map_t subject_map;
42 static void load_subjects()
45 CREATE_SUB_PROVIDER(PLACE_SUBJ_GEOFENCE, place_geofence_detector);
49 static void unload_subjects()
51 for (subject_map_t::iterator it = subject_map.begin(); it != subject_map.end(); ++it) {
58 EXTAPI ctx::place_context_provider::place_context_provider()
62 EXTAPI ctx::place_context_provider::~place_context_provider()
67 bool ctx::place_context_provider::init()
69 IF_FAIL_RETURN(subject_map.empty(), true);
73 } catch (std::bad_alloc& ba) {
74 _E("Memory allocation failed");
78 for (subject_map_t::iterator it = subject_map.begin(); it != subject_map.end(); ++it) {
79 context_manager::register_provider(it->first.c_str(), this);
86 bool ctx::place_context_provider::is_supported(const char* subject, const char* zone)
88 subject_map_t::iterator it = subject_map.find(subject);
90 if (it != subject_map.end())
91 return it->second->is_supported(zone);
96 int ctx::place_context_provider::subscribe(const char* subject, ctx::json option, ctx::json* request_result, const char* zone)
98 subject_map_t::iterator it = subject_map.find(subject);
100 if (it != subject_map.end())
101 return it->second->subscribe(option, request_result, zone);
103 return ERR_NOT_SUPPORTED;
106 int ctx::place_context_provider::unsubscribe(const char* subject, ctx::json option, const char* zone)
108 subject_map_t::iterator it = subject_map.find(subject);
110 if (it != subject_map.end())
111 return it->second->unsubscribe(option, zone);
113 return ERR_NOT_SUPPORTED;
116 int ctx::place_context_provider::read(const char* subject, ctx::json option, ctx::json* request_result, const char* zone)
118 subject_map_t::iterator it = subject_map.find(subject);
120 if (it != subject_map.end())
121 return it->second->read(option, request_result, zone);
123 return ERR_NOT_SUPPORTED;
126 int ctx::place_context_provider::write(const char* subject, ctx::json data, ctx::json* request_result, const char* zone)
128 subject_map_t::iterator it = subject_map.find(subject);
130 if (it != subject_map.end())
131 return it->second->write(data, request_result, zone);
133 return ERR_NOT_SUPPORTED;