Publishing project from SPIN to public
[platform/core/convergence/remote-rsc-svc.git] / src / common / rrs.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <glib.h>
18 #include "rrs.h"
19 #include "Mutex.h"
20 #include "common.h"
21 #include "tzplatform_config.h"
22
23 extern bool __has_iot_privilege();
24
25 static bool handle_array[MAX_RRS_HANDLE] = {false};
26 static Mutex m_mutex;
27
28 #define RRS_SECURITY_FILEPATH tzplatform_mkpath(TZ_USER_SHARE, "rrs/rrs-svr-db.dat")
29
30 EXPORT_API int rrs_create(rrs_h *rrs)
31 {
32         int ret;
33         int value = 1;
34
35         /* Check if parameters are valid */
36         if (!rrs)
37                 return RRS_ERROR_INVALID_PARAMETER;
38
39         /* Check if privileges enough */
40         if (!__has_iot_privilege())
41                 return RRS_ERROR_PERMISSION_DENIED;
42
43         {
44                 AUTOLOCK(m_mutex);
45                 while (handle_array[value] == true)
46                         value++;
47
48                 *rrs = value;
49                 handle_array[value] = true;
50         }
51
52         if (*rrs > MAX_RRS_HANDLE) {
53                 ERR("Maximum limit for RRS handles reached");
54                 return RRS_ERROR_SERVICE_NOT_AVAILABLE;
55         }
56
57         INFO("Path: %s", RRS_SECURITY_FILEPATH);
58
59         /* connect iotcon */
60         ret = iotcon_initialize(RRS_SECURITY_FILEPATH);
61         if (ret != IOTCON_ERROR_NONE) {
62                 ERR("iotcon_initialize() Fail(%d)", ret);
63                 return RRS_ERROR_SERVICE_NOT_AVAILABLE;
64         }
65
66         return RRS_ERROR_NONE;
67 }
68
69 EXPORT_API int rrs_destroy(const rrs_h rrs)
70 {
71         /* Check if RRS handle is valid */
72         if (rrs < 1 || rrs > MAX_RRS_HANDLE)
73                 return RRS_ERROR_INVALID_PARAMETER;
74
75         /* Check if privileges enough */
76         if (!__has_iot_privilege())
77                 return RRS_ERROR_PERMISSION_DENIED;
78
79         {
80                 AUTOLOCK(m_mutex);
81                 handle_array[rrs] = false;
82         }
83
84         /* disconnect iotcon */
85         iotcon_deinitialize();
86
87         return RRS_ERROR_NONE;
88 }