2 * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
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 * @file BoxDaemonImpl.cpp
18 * @author Yunchan Cho (yunchan.cho@samsung.com)
20 #include <sys/types.h>
27 #include <livebox-service.h>
28 #include <Core/Util/Log.h>
29 #include <Plugin/IBoxPluginConnector.h>
30 #include <Plugin/BoxPluginConnector.h>
31 #include <API/web_provider_livebox_info.h>
32 #include "BoxDaemonImpl.h"
34 #define MASTER_PROVIDER_PING_TIME 120.0f
36 BoxDaemonImpl::BoxDaemonImpl()
37 : m_pluginConnector(BoxPluginConnector::create())
41 BoxDaemonImpl::~BoxDaemonImpl()
45 bool BoxDaemonImpl::start(std::string& name)
49 // initialize existing plugins for web livebox
50 if (!m_pluginConnector->initialize()) {
51 LogD("failed to initialize plugins");
59 ProviderCallbacks callbacks;
60 setProviderCallbacks(callbacks);
62 int ret = provider_init(ecore_x_display_get(),
67 LogD("failed to initialize provider");
74 bool BoxDaemonImpl::stop()
78 // deinitialize provider
81 // deinitialize existing plugins for web livebox
82 if (!m_pluginConnector->shutdown()) {
83 LogD("failed to shutdown plugins");
90 int BoxDaemonImpl::connectedCallback(ProviderEventArgPtr arg, void* data)
94 BoxDaemonImpl* This = static_cast<BoxDaemonImpl*>(data);
95 if (!provider_send_hello()) {
97 ecore_timer_add(MASTER_PROVIDER_PING_TIME, pingToMasterCallback, This);
103 int BoxDaemonImpl::disconnectedCallback(ProviderEventArgPtr arg, void* data)
107 aul_terminate_pid(getpid());
112 int BoxDaemonImpl::boxCreateCallback(
113 ProviderEventArgPtr arg,
114 int* width, int* height,
115 double* priority, void* data)
119 BoxDaemonImpl* This = static_cast<BoxDaemonImpl*>(data);
120 BoxInfoPtr info = This->initializeBoxInfo(arg);
125 if (!(arg->info.lb_create.width) && !(arg->info.lb_create.height)) {
126 livebox_service_get_size(LB_SIZE_TYPE_1x1, width, height);
129 info->boxWidth = *width;
130 info->boxHeight = *height;
131 info->priority = 1.0f;
132 info->period = arg->info.lb_create.period;
133 if (arg->info.lb_create.content) {
134 info->contentInfo = std::string(arg->info.lb_create.content);
138 LogD("--------------------------------------------");
139 LogD("boxId: %s", info->boxId.c_str());
140 LogD("InstanceId: %s", info->instanceId.c_str());
141 LogD("width: %d", info->boxWidth);
142 LogD("height: %d", info->boxHeight);
143 LogD("update period: %d", info->period);
144 LogD("--------------------------------------------");
146 JobInfo* jobInfo = new JobInfo(REQUEST_CMD_ADD_BOX, info, This);
147 Ecore_Job* ret = ecore_job_add(requestBoxJobCallback, jobInfo);
152 int BoxDaemonImpl::boxReCreateCallback(ProviderEventArgPtr arg, void* data)
156 BoxDaemonImpl* This = static_cast<BoxDaemonImpl*>(data);
157 BoxInfoPtr info = This->initializeBoxInfo(arg);
162 info->boxWidth = arg->info.lb_recreate.width;
163 info->boxHeight = arg->info.lb_recreate.height;
164 info->priority = 1.0f;
165 info->period = arg->info.lb_recreate.period;
166 if (arg->info.lb_recreate.content) {
167 info->contentInfo = std::string(arg->info.lb_recreate.content);
171 LogD("--------------------------------------------");
172 LogD("boxId: %s", info->boxId.c_str());
173 LogD("InstanceId: %s", info->instanceId.c_str());
174 LogD("width: %d", info->boxWidth);
175 LogD("height: %d", info->boxHeight);
176 LogD("update period: %d", info->period);
177 LogD("--------------------------------------------");
179 JobInfo* jobInfo = new JobInfo(REQUEST_CMD_ADD_BOX, info, This);
180 Ecore_Job* ret = ecore_job_add(requestBoxJobCallback, jobInfo);
185 int BoxDaemonImpl::boxDestroyCallback(ProviderEventArgPtr arg, void* data)
189 BoxDaemonImpl* This = static_cast<BoxDaemonImpl*>(data);
190 BoxInfoPtr info = This->initializeBoxInfo(arg);
195 LogD("--------------------------------------------");
196 LogD("boxId: %s", info->boxId.c_str());
197 LogD("InstanceId: %s", info->instanceId.c_str());
198 LogD("--------------------------------------------");
200 JobInfo* jobInfo = new JobInfo(REQUEST_CMD_REMOVE_BOX, info, This);
201 Ecore_Job* ret = ecore_job_add(requestBoxJobCallback, jobInfo);
206 int BoxDaemonImpl::pdCreateCallback(ProviderEventArgPtr arg, void* data)
210 BoxDaemonImpl* This = static_cast<BoxDaemonImpl*>(data);
211 BoxInfoPtr info = This->initializeBoxInfo(arg);
216 info->pdWidth = arg->info.pd_create.w;
217 info->pdHeight = arg->info.pd_create.h;
219 LogD("--------------------------------------------");
220 LogD("boxId: %s", info->boxId.c_str());
221 LogD("InstanceId: %s", info->instanceId.c_str());
222 LogD("width: %d", info->pdWidth);
223 LogD("height: %d", info->pdHeight);
224 LogD("--------------------------------------------");
226 JobInfo* jobInfo = new JobInfo(REQUEST_CMD_OPEN_PD, info, This);
227 Ecore_Job* ret = ecore_job_add(requestBoxJobCallback, jobInfo);
232 int BoxDaemonImpl::pdDestroyCallback(ProviderEventArgPtr arg, void* data)
236 BoxDaemonImpl* This = static_cast<BoxDaemonImpl*>(data);
237 BoxInfoPtr info = This->initializeBoxInfo(arg);
242 LogD("--------------------------------------------");
243 LogD("boxId: %s", info->boxId.c_str());
244 LogD("InstanceId: %s", info->instanceId.c_str());
245 LogD("--------------------------------------------");
247 JobInfo* jobInfo = new JobInfo(REQUEST_CMD_CLOSE_PD, info, This);
248 Ecore_Job* ret = ecore_job_add(requestBoxJobCallback, jobInfo);
253 int BoxDaemonImpl::clickedCallback(ProviderEventArgPtr arg, void* data)
260 int BoxDaemonImpl::resizeCallback(ProviderEventArgPtr arg, void* data)
264 BoxDaemonImpl* This = static_cast<BoxDaemonImpl*>(data);
265 BoxInfoPtr info = This->initializeBoxInfo(arg);
270 info->boxWidth = arg->info.resize.w;
271 info->boxHeight = arg->info.resize.h;
274 LogD("--------------------------------------------");
275 LogD("boxId: %s", info->boxId.c_str());
276 LogD("InstanceId: %s", info->instanceId.c_str());
277 LogD("width: %d", info->boxWidth);
278 LogD("height: %d", info->boxHeight);
279 LogD("--------------------------------------------");
281 JobInfo* jobInfo = new JobInfo(REQUEST_CMD_RESIZE_BOX, info, This);
282 Ecore_Job* ret = ecore_job_add(requestBoxJobCallback, jobInfo);
287 int BoxDaemonImpl::boxPauseCallback(ProviderEventArgPtr arg, void* data)
291 BoxDaemonImpl* This = static_cast<BoxDaemonImpl*>(data);
292 BoxInfoPtr info = This->initializeBoxInfo(arg);
298 LogD("--------------------------------------------");
299 LogD("boxId: %s", info->boxId.c_str());
300 LogD("InstanceId: %s", info->instanceId.c_str());
301 LogD("--------------------------------------------");
303 JobInfo* jobInfo = new JobInfo(REQUEST_CMD_PAUSE_BOX, info, This);
304 Ecore_Job* ret = ecore_job_add(requestBoxJobCallback, jobInfo);
309 int BoxDaemonImpl::boxResumeCallback(ProviderEventArgPtr arg, void* data)
313 BoxDaemonImpl* This = static_cast<BoxDaemonImpl*>(data);
314 BoxInfoPtr info = This->initializeBoxInfo(arg);
320 LogD("--------------------------------------------");
321 LogD("boxId: %s", info->boxId.c_str());
322 LogD("InstanceId: %s", info->instanceId.c_str());
323 LogD("--------------------------------------------");
325 JobInfo* jobInfo = new JobInfo(REQUEST_CMD_RESUME_BOX, info, This);
326 Ecore_Job* ret = ecore_job_add(requestBoxJobCallback, jobInfo);
331 int BoxDaemonImpl::pauseCallback(ProviderEventArgPtr arg, void* data)
335 BoxDaemonImpl* This = static_cast<BoxDaemonImpl*>(data);
337 LogD("--------------------------------------------");
338 LogD("web-provider is paused");
339 LogD("--------------------------------------------");
341 JobInfo* jobInfo = new JobInfo(REQUEST_CMD_PAUSE_ALL, BoxInfoPtr(), This);
342 Ecore_Job* ret = ecore_job_add(requestBoxJobCallback, jobInfo);
347 int BoxDaemonImpl::resumeCallback(ProviderEventArgPtr arg, void* data)
351 BoxDaemonImpl* This = static_cast<BoxDaemonImpl*>(data);
353 LogD("--------------------------------------------");
354 LogD("web-provider is resumed");
355 LogD("--------------------------------------------");
357 JobInfo* jobInfo = new JobInfo(REQUEST_CMD_RESUME_ALL, BoxInfoPtr(), This);
358 Ecore_Job* ret = ecore_job_add(requestBoxJobCallback, jobInfo);
363 int BoxDaemonImpl::updateContentCallback(ProviderEventArgPtr arg, void* data)
370 int BoxDaemonImpl::changePeriodCallback(ProviderEventArgPtr arg, void* data)
373 BoxDaemonImpl* This = static_cast<BoxDaemonImpl*>(data);
374 BoxInfoPtr info = This->initializeBoxInfo(arg);
378 info->period = arg->info.set_period.period;
380 LogD("--------------------------------------------");
381 LogD("boxId: %s", info->boxId.c_str());
382 LogD("InstanceId: %s", info->instanceId.c_str());
383 LogD("period: %f", info->period);
384 LogD("--------------------------------------------");
386 JobInfo* jobInfo = new JobInfo(REQUEST_CMD_CHANGE_PERIOD, info, This);
387 Ecore_Job* ret = ecore_job_add(requestBoxJobCallback, jobInfo);
392 void BoxDaemonImpl::setProviderCallbacks(ProviderCallbacks& callbacks)
396 memset(&callbacks, 0, sizeof(callbacks));
397 callbacks.connected = BoxDaemonImpl::connectedCallback;
398 callbacks.disconnected = BoxDaemonImpl::disconnectedCallback;
399 callbacks.pause = BoxDaemonImpl::pauseCallback;
400 callbacks.resume = BoxDaemonImpl::resumeCallback;
401 callbacks.lb_create = BoxDaemonImpl::boxCreateCallback;
402 callbacks.lb_recreate = BoxDaemonImpl::boxReCreateCallback;
403 callbacks.lb_destroy = BoxDaemonImpl::boxDestroyCallback;
404 callbacks.lb_pause = BoxDaemonImpl::boxPauseCallback;
405 callbacks.lb_resume = BoxDaemonImpl::boxResumeCallback;
406 callbacks.pd_create = BoxDaemonImpl::pdCreateCallback;
407 callbacks.pd_destroy = BoxDaemonImpl::pdDestroyCallback;
408 callbacks.clicked = BoxDaemonImpl::clickedCallback;
409 callbacks.resize = BoxDaemonImpl::resizeCallback;
410 callbacks.update_content = BoxDaemonImpl::updateContentCallback;
411 callbacks.set_period = BoxDaemonImpl::changePeriodCallback;
414 const char* BoxDaemonImpl::getBoxType(const char* boxId)
422 const char* type = web_provider_livebox_get_box_type(boxId);
424 std::string serviceBoxId(boxId);
425 std::string boxType = m_pluginConnector->getBoxType(serviceBoxId);
426 if (boxType.empty()) {
427 LogD("unrecognized box id");
430 type = boxType.c_str();
433 LogD("box id: %s, type: %s", boxId, type);
437 BoxInfoPtr BoxDaemonImpl::initializeBoxInfo(ProviderEventArgPtr arg)
445 if (!arg->pkgname || !arg->id) {
446 LogD("pkgname or id don't exist");
450 const char* type = getBoxType(arg->pkgname);
455 return BoxInfoPtr(new BoxInfo(type, arg->pkgname, arg->id));
458 Eina_Bool BoxDaemonImpl::pingToMasterCallback(void* data)
462 provider_send_ping();
464 return ECORE_CALLBACK_RENEW;
467 void BoxDaemonImpl::requestBoxJobCallback(void* data)
469 JobInfo* jobInfo = static_cast<JobInfo*>(data);
471 LogD("no information for job");
475 // just for debugging
476 //volatile int flag = 0;
477 //while(flag == 0) {;}
480 jobInfo->daemonImpl->m_pluginConnector->requestCommand(
481 jobInfo->cmdType, jobInfo->boxInfo);