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.
17 #include "ServiceManager.h"
18 #include "service_provider/AppCommServiceProvider.h"
19 #include "service_provider/RemoteAppControlServiceProvider.h"
20 #include "access_control/Privilege.h"
24 static conv::ServiceManager *_instance;
25 static iotcon_resource_h iotcon_resource = NULL;
29 conv::ServiceManager::ServiceManager()
31 __activationState = 1;
34 conv::ServiceManager::~ServiceManager()
38 int conv::ServiceManager::handleVconfUpdate(keynode_t *node)
40 __activationState = vconf_keynode_get_int(node);
42 if ( __activationState == 1 ) {
43 registerDiscoveryInfo();
45 unregisterDiscoveryInfo();
48 return CONV_ERROR_NONE;
51 static void vconf_update_cb(keynode_t *node, void* user_data)
53 conv::ServiceManager* instance = static_cast<conv::ServiceManager*>(user_data);
54 IF_FAIL_VOID_TAG(instance, _E, "static_cast failed");
56 instance->handleVconfUpdate(node);
59 int conv::ServiceManager::init()
61 registerProvider(new(std::nothrow) conv::AppCommServiceProvider());
62 registerProvider(new(std::nothrow) conv::RemoteAppControlServiceProvider());
64 if (conv::util::isServiceActivated()) {
65 __activationState = 1;
66 registerDiscoveryInfo();
68 __activationState = 0;
69 unregisterDiscoveryInfo();
72 int error = vconf_notify_key_changed(VCONFKEY_SETAPPL_D2D_CONVERGENCE_SERVICE, vconf_update_cb, this);
74 IF_FAIL_RETURN_TAG(error >= 0, CONV_ERROR_INVALID_OPERATION, _E, "vconf error (%d)", error);
76 return CONV_ERROR_NONE;
79 int conv::ServiceManager::release()
81 unregisterDiscoveryInfo();
83 for (ServiceProviderList::iterator it = __providerList.begin(); it != __providerList.end(); ++it) {
87 for (ServiceProviderList::iterator it = __providerList.begin(); it != __providerList.end(); ++it) {
91 __providerList.clear();
93 return CONV_ERROR_NONE;
96 int conv::ServiceManager::handleRequest(Request* requestObj)
98 _D("handle_request called");
100 int error = CONV_ERROR_INVALID_OPERATION;
103 Json description = requestObj->getDescription();
104 result = description.get(NULL, CONV_JSON_TYPE, &type);
106 IF_FAIL_CATCH_TAG(result, _E, "json parse error : no type info");
108 for (ServiceProviderList::iterator it = __providerList.begin(); it != __providerList.end(); ++it) {
109 if ( (*it)->getType().compare(type) == 0 )
111 _D("found service provider");
112 error = (*it)->checkActivationState();
113 IF_FAIL_CATCH_TAG(error == CONV_ERROR_NONE, _E, "service provider is not activated");
115 error = (*it)->loadServiceInfo(requestObj);
116 IF_FAIL_CATCH_TAG(error == CONV_ERROR_NONE, _E, "%d, service_info load error", error);
118 if (!strcmp(requestObj->getSubject(), CONV_SUBJECT_COMMUNICATION_START)) {
119 if ( !conv::privilege_manager::isAllowed(requestObj->getCreds(), CONV_PRIVILEGE_INTERNET) ||
120 !conv::privilege_manager::isAllowed(requestObj->getCreds(), CONV_PRIVILEGE_BLUETOOTH) ||
121 !conv::privilege_manager::isAllowed(requestObj->getCreds(), CONV_PRIVILEGE_D2D_DATA_SHARING) )
123 _E("permission denied");
124 requestObj->reply(CONV_ERROR_PERMISSION_DENIED);
126 return CONV_ERROR_PERMISSION_DENIED;
128 if ( !conv::util::isApiActivated()) {
129 _E("API is not activated");
130 requestObj->reply(CONV_ERROR_INVALID_OPERATION);
132 return CONV_ERROR_INVALID_OPERATION;
135 error = (*it)->startRequest(requestObj);
136 } else if (!strcmp(requestObj->getSubject(), CONV_SUBJECT_COMMUNICATION_STOP)) {
137 if ( !conv::privilege_manager::isAllowed(requestObj->getCreds(), CONV_PRIVILEGE_INTERNET) ||
138 !conv::privilege_manager::isAllowed(requestObj->getCreds(), CONV_PRIVILEGE_BLUETOOTH) )
140 _E("permission denied");
141 requestObj->reply(CONV_ERROR_PERMISSION_DENIED);
143 return CONV_ERROR_PERMISSION_DENIED;
145 if ( !conv::util::isApiActivated()) {
146 _E("API is not activated");
147 requestObj->reply(CONV_ERROR_INVALID_OPERATION);
149 return CONV_ERROR_INVALID_OPERATION;
152 error = (*it)->stopRequest(requestObj);
153 } else if (!strcmp(requestObj->getSubject(), CONV_SUBJECT_COMMUNICATION_GET)) {
154 if ( !conv::privilege_manager::isAllowed(requestObj->getCreds(), CONV_PRIVILEGE_INTERNET) ||
155 !conv::privilege_manager::isAllowed(requestObj->getCreds(), CONV_PRIVILEGE_BLUETOOTH) )
157 _E("permission denied");
158 requestObj->reply(CONV_ERROR_PERMISSION_DENIED);
160 return CONV_ERROR_PERMISSION_DENIED;
162 if ( !conv::util::isApiActivated()) {
163 _E("API is not activated");
164 requestObj->reply(CONV_ERROR_INVALID_OPERATION);
166 return CONV_ERROR_INVALID_OPERATION;
169 error = (*it)->readRequest(requestObj);
170 } else if (!strcmp(requestObj->getSubject(), CONV_SUBJECT_COMMUNICATION_SET)) {
171 if ( !conv::privilege_manager::isAllowed(requestObj->getCreds(), CONV_PRIVILEGE_INTERNET) ||
172 !conv::privilege_manager::isAllowed(requestObj->getCreds(), CONV_PRIVILEGE_BLUETOOTH) ||
173 !conv::privilege_manager::isAllowed(requestObj->getCreds(), CONV_PRIVILEGE_D2D_DATA_SHARING) )
175 _E("permission denied");
176 requestObj->reply(CONV_ERROR_PERMISSION_DENIED);
178 return CONV_ERROR_PERMISSION_DENIED;
180 if ( !conv::util::isApiActivated()) {
181 _E("API is not activated");
182 requestObj->reply(CONV_ERROR_INVALID_OPERATION);
184 return CONV_ERROR_INVALID_OPERATION;
187 error = (*it)->publishRequest(requestObj);
188 } else if (!strcmp(requestObj->getSubject(), CONV_SUBJECT_COMMUNICATION_RECV)) {
189 return (*it)->registerRequest(requestObj);
190 } else if (!strcmp(requestObj->getSubject(), CONV_SUBJECT_COMMUNICATION_CHECK_STATE)) {
191 error = (*it)->checkStateRequest(requestObj);
193 IF_FAIL_CATCH_TAG(error == CONV_ERROR_NONE, _E, "service manager request handle error");
197 requestObj->reply(CONV_ERROR_NONE);
199 _D("requestObj deleted");
200 return CONV_ERROR_NONE;
203 requestObj->reply(error);
209 void conv::service_manager::setInstance(conv::ServiceManager* mgr)
214 int conv::service_manager::handleRequest(Request* requestObj)
216 IF_FAIL_RETURN_TAG(_instance, CONV_ERROR_INVALID_PARAMETER, _E, "Not initialized");
217 _instance->handleRequest(requestObj);
219 return CONV_ERROR_NONE;
222 int conv::ServiceManager::registerProvider(conv::IServiceProvider *provider)
226 return CONV_ERROR_INVALID_PARAMETER;
229 if (provider->checkActivationState() == CONV_ERROR_NONE) {
230 if (provider->init() != CONV_ERROR_NONE) {
231 _E("Provider initialization failed");
233 return CONV_ERROR_INVALID_OPERATION;
236 _D("provider is not activated. init pending");
239 __providerList.push_back(provider);
241 return CONV_ERROR_NONE;
246 static int _send_response(iotcon_request_h request, iotcon_representation_h repr,
247 iotcon_response_result_e result)
250 iotcon_response_h response;
252 ret = iotcon_response_create(request, &response);
253 if (IOTCON_ERROR_NONE != ret) {
254 _E("iotcon_response_create() Fail(%d)", ret);
255 return CONV_ERROR_INVALID_OPERATION;
258 ret = iotcon_response_set_result(response, result);
259 if (IOTCON_ERROR_NONE != ret) {
260 _E("iotcon_response_set_result() Fail(%d)", ret);
261 iotcon_response_destroy(response);
262 return CONV_ERROR_INVALID_OPERATION;
265 ret = iotcon_response_set_representation(response, repr);
266 if (IOTCON_ERROR_NONE != ret) {
267 _E("iotcon_response_set_representation() Fail(%d)", ret);
268 iotcon_response_destroy(response);
269 return CONV_ERROR_INVALID_OPERATION;
272 /* send Representation to the client */
273 ret = iotcon_response_send(response);
274 if (IOTCON_ERROR_NONE != ret) {
275 _E("iotcon_response_send() Fail(%d)", ret);
276 iotcon_response_destroy(response);
277 return CONV_ERROR_INVALID_OPERATION;
280 iotcon_response_destroy(response);
282 return CONV_ERROR_NONE;
286 static iotcon_representation_h _get_d2d_service_representation(conv::ServiceManager* instance)
289 iotcon_attributes_h attributes;
290 iotcon_representation_h repr;
292 ret = iotcon_representation_create(&repr);
293 if (IOTCON_ERROR_NONE != ret) {
294 _E("iotcon_representation_create() Fail(%d)", ret);
298 ret = iotcon_attributes_create(&attributes);
299 if (IOTCON_ERROR_NONE != ret) {
300 _E("iotcon_attributes_create() Fail(%d)", ret);
301 iotcon_representation_destroy(repr);
305 string deviceIdStr = conv::util::getDeviceId();
306 string deviceNameStr = conv::util::getDeviceName();
307 char* deviceId = new char[deviceIdStr.size() + 1];
308 char* deviceName = new char[deviceNameStr.size() + 1];
310 memset(deviceId, 0, deviceIdStr.size() + 1);
311 memset(deviceName, 0, deviceNameStr.size() + 1);
313 strncpy(deviceId, deviceIdStr.c_str(), deviceIdStr.size()+1);
314 strncpy(deviceName, deviceNameStr.c_str(), deviceNameStr.size()+1);
316 _D("device name : %s", deviceName);
317 iotcon_attributes_add_str(attributes, CONV_JSON_DEVICE_ID, deviceId);
318 iotcon_attributes_add_str(attributes, CONV_JSON_DEVICE_NAME, deviceName);
320 _D("device type : TV");
321 string deviceTypeStr(CONV_DEVICE_TYPE_TV);
322 #elif defined(_MOBILE_)
323 _D("device type : MOBILE");
324 string deviceTypeStr(CONV_DEVICE_TYPE_MOBILE);
325 #elif defined(_WEARABLE_)
326 _D("device type : WEARABLE");
327 string deviceTypeStr(CONV_DEVICE_TYPE_WEARABLE);
329 _D("device type : UNKNOWN");
330 string deviceTypeStr(CONV_DEVICE_TYPE_UNKNOWN);
332 char* deviceType = new char[deviceTypeStr.size() + 1];
334 memset(deviceType, 0, deviceTypeStr.size() + 1);
335 strncpy(deviceType, deviceTypeStr.c_str(), deviceTypeStr.size()+1);
337 iotcon_attributes_add_str(attributes, CONV_JSON_DEVICE_TYPE, deviceType);
343 conv::Json serviceJson;
344 instance->getServiceInfoForDiscovery(&serviceJson);
345 char* service_json_char = serviceJson.dupCstr();
347 iotcon_attributes_add_str(attributes, "serviceJson", service_json_char);
349 ret = iotcon_representation_set_attributes(repr, attributes);
350 g_free(service_json_char);
351 if (IOTCON_ERROR_NONE != ret) {
352 _E("iotcon_representation_set_attributes() Fail(%d)", ret);
353 iotcon_attributes_destroy(attributes);
354 iotcon_representation_destroy(repr);
358 iotcon_attributes_destroy(attributes);
363 int conv::ServiceManager::getServiceInfoForDiscovery(Json* serviceJson)
365 IF_FAIL_RETURN_TAG(serviceJson, CONV_ERROR_INVALID_OPERATION, _E, "serviceJson is NULL");
367 for (ServiceProviderList::iterator it = __providerList.begin(); it != __providerList.end(); ++it) {
369 if ((*it)->getServiceInfoForDiscovery(&serviceInfo) == CONV_ERROR_NONE) {
370 serviceJson->appendArray(NULL, "service_list", serviceInfo);
374 _D("service_info : %s", serviceJson->str().c_str());
376 return CONV_ERROR_NONE;
379 static void iotcon_request_cb(iotcon_resource_h resource, iotcon_request_h request, void *user_data)
381 _D("request cb called");
384 iotcon_request_type_e type;
387 conv::ServiceManager *instance = (conv::ServiceManager*)user_data;
389 IF_FAIL_VOID_TAG(request, _E, "request is NULL");
391 ret = iotcon_request_get_host_address(request, &host_address);
392 if (IOTCON_ERROR_NONE != ret) {
393 _E("iotcon_request_get_host_address() Fail(%d)", ret);
394 _send_response(request, NULL, IOTCON_RESPONSE_ERROR);
397 _I("host_address : %s", host_address);
399 ret = iotcon_request_get_request_type(request, &type);
400 if (IOTCON_ERROR_NONE != ret) {
401 _E("iotcon_request_get_types() Fail(%d)", ret);
402 _send_response(request, NULL, IOTCON_RESPONSE_ERROR);
406 if (IOTCON_REQUEST_GET == type) {
407 iotcon_representation_h resp_repr;
410 resp_repr = _get_d2d_service_representation(instance);
411 if (resp_repr == NULL) {
412 _I("_get_d2d_service_representation() Fail");
413 ret = CONV_ERROR_INVALID_OPERATION;
415 ret = _send_response(request, resp_repr, IOTCON_RESPONSE_OK);
416 if (ret != IOTCON_ERROR_NONE) {
417 _E("_send_response() Fail(%d)", ret);
418 iotcon_representation_destroy(resp_repr);
422 iotcon_representation_destroy(resp_repr);
426 if (IOTCON_ERROR_NONE != ret) {
427 _send_response(request, NULL, IOTCON_RESPONSE_ERROR);
432 int conv::ServiceManager::registerDiscoveryInfo()
434 IF_FAIL_RETURN_TAG(iotcon_resource == NULL, CONV_ERROR_INVALID_PARAMETER, _E, "resource for discovery is already registered");
438 iotcon_resource_interfaces_h resource_ifaces = NULL;
439 iotcon_resource_types_h resource_types = NULL;
442 properties = IOTCON_RESOURCE_DISCOVERABLE;
444 error = iotcon_resource_types_create(&resource_types);
445 IF_FAIL_RETURN_TAG(error == IOTCON_ERROR_NONE, CONV_ERROR_INVALID_OPERATION, _E, "rt creation failed");
447 iotcon_resource_types_add(resource_types, CONV_RESOURCE_TYPE_TIZEN_D2D_SERVICE);
449 error = iotcon_resource_interfaces_create(&resource_ifaces);
451 IF_FAIL_RETURN_TAG(error == IOTCON_ERROR_NONE, CONV_ERROR_INVALID_OPERATION, _E, "ri creation failed");
453 iotcon_resource_interfaces_add(resource_ifaces, IOTCON_INTERFACE_DEFAULT);
455 error = iotcon_resource_create(CONV_URI_TIZEN_D2D_SERVICE, resource_types, resource_ifaces, properties, iotcon_request_cb, this, &iotcon_resource);
456 IF_FAIL_RETURN_TAG(error == IOTCON_ERROR_NONE, CONV_ERROR_INVALID_OPERATION, _E, "resource creation failed");
458 _D("device info registered as resource");
459 iotcon_resource_types_destroy(resource_types);
460 iotcon_resource_interfaces_destroy(resource_ifaces);
462 return CONV_ERROR_NONE;
465 int conv::ServiceManager::unregisterDiscoveryInfo()
467 IF_FAIL_RETURN_TAG(iotcon_resource != NULL, CONV_ERROR_INVALID_PARAMETER, _E, "resource for discovery is already unregistered");
469 int error = iotcon_resource_destroy(iotcon_resource);
470 IF_FAIL_RETURN_TAG(error == IOTCON_ERROR_NONE, CONV_ERROR_INVALID_OPERATION, _E, "resource destroy failed");
471 iotcon_resource = NULL;
474 _D("device info unregistered");
475 return CONV_ERROR_NONE;