Add asynchronous client api
[platform/core/security/cynara.git] / src / client-async / api / client-async-api.cpp
1 /*
2  *  Copyright (c) 2014 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  * @file        client-async-api.cpp
18  * @author      Marcin Niesluchowski <m.niesluchow@samsung.com>
19  * @version     1.0
20  * @brief       Implementation of external libcynara-client-async API
21  */
22
23 #include <new>
24
25 #include <common.h>
26 #include <log/log.h>
27
28 #include <api/ApiInterface.h>
29 #include <cynara-client-async.h>
30 #include <logic/Logic.h>
31
32 struct cynara_async {
33     Cynara::ApiInterface *impl;
34
35     cynara_async(Cynara::ApiInterface *_impl) : impl(_impl) {
36     }
37
38     ~cynara_async() {
39         delete impl;
40     }
41 };
42
43 CYNARA_API
44 int cynara_async_initialize(cynara_async **pp_cynara,
45                             const cynara_async_configuration *p_conf UNUSED)
46 {
47     if (!pp_cynara)
48         return CYNARA_ASYNC_API_INVALID_PARAM;
49
50     try {
51         *pp_cynara = new cynara_async(new Cynara::Logic);
52     } catch (const std::bad_alloc &ex) {
53         return CYNARA_ASYNC_API_OUT_OF_MEMORY;
54     }
55
56     init_log();
57
58     LOGD("Cynara client async initialized");
59
60     return CYNARA_ASYNC_API_SUCCESS;
61 }
62
63 CYNARA_API
64 int cynara_async_finish(cynara_async *p_cynara)
65 {
66     delete p_cynara;
67
68     return CYNARA_ASYNC_API_SUCCESS;
69 }
70
71 CYNARA_API
72 int cynara_async_connect(cynara_async *p_cynara, int *p_sock_fd)
73 {
74     if (!p_cynara || !p_cynara->impl)
75         return CYNARA_ASYNC_API_INVALID_PARAM;
76     if (!p_sock_fd)
77         return CYNARA_ASYNC_API_INVALID_PARAM;
78
79     return p_cynara->impl->connect(*p_sock_fd);
80 }
81
82 CYNARA_API
83 int cynara_async_check(cynara_async *p_cynara,
84                        const char *client, const char *client_session,
85                        const char *user, const char *privilege,
86                        cynara_check_id *p_check_id)
87 {
88     if (!p_cynara || !p_cynara->impl)
89         return CYNARA_ASYNC_API_INVALID_PARAM;
90     if (!client || !client_session || !user || !privilege)
91         return CYNARA_ASYNC_API_INVALID_PARAM;
92     if (!p_check_id)
93         return CYNARA_ASYNC_API_INVALID_PARAM;
94
95     return p_cynara->impl->check(client, client_session,
96                                  user, privilege,
97                                  *p_check_id);
98 }
99
100 CYNARA_API
101 int cynara_async_receive(cynara_async *p_cynara, cynara_check_id *p_check_id)
102 {
103     if (!p_cynara || !p_cynara->impl)
104         return CYNARA_ASYNC_API_INVALID_PARAM;
105     if (!p_check_id)
106         return CYNARA_ASYNC_API_INVALID_PARAM;
107
108     return p_cynara->impl->receive(*p_check_id);
109 }
110
111 CYNARA_API
112 int cynara_async_cancel(cynara_async *p_cynara, const cynara_check_id check_id)
113 {
114     if (!p_cynara || !p_cynara->impl)
115         return CYNARA_ASYNC_API_INVALID_PARAM;
116
117     return p_cynara->impl->cancel(check_id);
118 }