2 * Copyright (c) 2017 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 <ServerUtil.h>
19 #include "MethodCallHandler.h"
20 #include "legacy/SensorProvider.h"
27 MethodCallHandler::MethodCallHandler(IClient* client) :
32 MethodCallHandler::~MethodCallHandler()
36 void MethodCallHandler::onMethodCalled(IMethodCall* methodCall)
39 __verifyUid(methodCall->getUid());
40 std::string subject = __extractParameter(methodCall->getParam(), IDX_SUBJECT);
41 _D("%s(%s)", methodCall->getMethodName().c_str(), subject.c_str());
43 SensorProvider* provider = SensorProvider::getInstance(subject);
45 if (methodCall->getMethodName() == METHOD_IS_SUPPORTED) {
46 methodCall->reply(E_NONE);
48 } else if (methodCall->getMethodName() == METHOD_START_REC) {
49 __startRecording(*provider, *methodCall);
51 } else if (methodCall->getMethodName() == METHOD_STOP_REC) {
52 __stopRecording(*provider, *methodCall);
54 } else if (methodCall->getMethodName() == METHOD_READ_REC) {
55 __readRecords(*provider, *methodCall);
58 } catch (const int error) {
59 _W("Catch: %s", CTX_ERROR_STR(error));
60 methodCall->reply(error);
66 void MethodCallHandler::onDisconnected()
70 void MethodCallHandler::__verifyUid(uid_t uid)
72 if (!util::isSystemUid(uid) && uid != util::getActiveUid()) {
73 _E("Invalid Uid: %u != %u (ActiveUser)", uid, util::getActiveUid());
74 throw static_cast<int>(E_ACCESS);
78 std::string MethodCallHandler::__extractParameter(GVariant* param, unsigned int idx)
80 const char* val = NULL;
81 g_variant_get_child(param, idx, "&s", &val);
84 throw static_cast<int>(E_PARAM);
89 void MethodCallHandler::__startRecording(SensorProvider& provider, IMethodCall& methodCall)
91 if (!methodCall.hasPrivilege(provider.getPrivilege()))
92 throw static_cast<int>(E_ACCESS);
94 // TODO: use the 'real' package id, instead of the smack label
95 std::string pkgId = methodCall.getCallerId();
96 std::string option = __extractParameter(methodCall.getParam(), IDX_OPTION);
97 _D("Option: %s", option.c_str());
99 int error = provider.startRecording(pkgId, option);
100 methodCall.reply(error);
103 void MethodCallHandler::__stopRecording(SensorProvider& provider, IMethodCall& methodCall)
105 // TODO: use the 'real' package id, instead of the smack label
106 std::string pkgId = methodCall.getCallerId();
108 int error = provider.stopRecording(pkgId);
109 methodCall.reply(error);
112 void MethodCallHandler::__readRecords(SensorProvider& provider, IMethodCall& methodCall)
114 if (!methodCall.hasPrivilege(provider.getPrivilege()))
115 throw static_cast<int>(E_ACCESS);
117 // TODO: use the 'real' package id, instead of the smack label
118 std::string pkgId = methodCall.getCallerId();
119 std::string option = __extractParameter(methodCall.getParam(), IDX_OPTION);
120 _D("Option: %s", option.c_str());
122 std::string projection;
123 std::vector<std::shared_ptr<Tuple>> tuples;
125 int error = provider.readRecords(option, &projection, &tuples);
129 throw static_cast<int>(E_NO_DATA);
131 methodCall.reply(g_variant_new("(sv)", projection.c_str(), Tuple::toGVariant(tuples)));