Add 3.0 APIs and sync APIs same as 2.4
[platform/core/convergence/service-adaptor.git] / client / sal_service_provider.c
1 /*
2  * Service Plugin Client
3  *
4  * Copyright (c) 2014 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <glib.h>
23
24 #include <app.h>
25
26 #include "service_adaptor_errors.h"
27 #include "service_adaptor_internal.h"
28 #include "sal_service_provider.h"
29
30 API int service_provider_create(service_provider_h *provider)
31 {
32         SAL_FN_CALL;
33
34         service_provider_h service_provider = (service_provider_h) g_malloc0(sizeof(service_provider_s));
35
36         service_provider->connect = NULL;
37         service_provider->disconnect = NULL;
38
39         *provider = service_provider;
40
41         return SERVICE_ADAPTOR_ERROR_NONE;
42 }
43
44 API int service_provider_destroy(service_provider_h provider)
45 {
46         SAL_FN_CALL;
47
48         // TODO: free internal value of provider
49         SAL_FREE(provider);
50
51         return SERVICE_ADAPTOR_ERROR_NONE;
52 }
53
54 API int service_provider_set_auth_provider(service_provider_h provider, auth_provider_h auth_provider)
55 {
56         SAL_FN_CALL;
57
58         RETV_IF(NULL == provider, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
59         RETV_IF(NULL == auth_provider, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
60
61         provider->auth_provider = auth_provider;
62
63         return SERVICE_ADAPTOR_ERROR_NONE;
64 }
65
66 API int service_provider_unset_auth_provider(service_provider_h provider)
67 {
68         SAL_FN_CALL;
69
70         RETV_IF(NULL == provider, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
71
72         provider->auth_provider = NULL;
73
74         return SERVICE_ADAPTOR_ERROR_NONE;
75 }
76
77 API int service_provider_set_storage_provider(service_provider_h provider, storage_provider_h storage_provider)
78 {
79         SAL_FN_CALL;
80
81         RETV_IF(NULL == provider, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
82         RETV_IF(NULL == storage_provider, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
83
84         provider->storage_provider = storage_provider;
85
86         return SERVICE_ADAPTOR_ERROR_NONE;
87 }
88
89 API int service_provider_unset_storage_provider(service_provider_h provider)
90 {
91         SAL_FN_CALL;
92
93         RETV_IF(NULL == provider, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
94
95         provider->storage_provider = NULL;
96
97         return SERVICE_ADAPTOR_ERROR_NONE;
98 }
99
100 API int service_provider_message(service_provider_h provider, app_control_h app_control, void *user_data)
101 {
102         SAL_FN_CALL;
103
104         RETV_IF(NULL == provider, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
105
106         char *operation = NULL;
107         app_control_get_operation(app_control, &operation);
108
109         RETV_IF(NULL == operation, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
110
111         int ret = SERVICE_ADAPTOR_ERROR_NONE;
112
113         app_control_h reply = NULL;
114
115         if (0 == strcmp(operation, PLUGIN_CONNECT_URI))
116         {
117                 app_control_create(&reply);
118                 ret = provider->connect();
119
120                 if (SERVICE_ADAPTOR_ERROR_NONE != ret)
121                 {
122                         SAL_ERR("connect() Fail (%d)", ret);
123                         app_control_add_extra_data(reply, PLUGIN_RESULT_KEY, PLUGIN_RESULT_VALUE_FAILURE);
124                         goto catch;
125                 }
126
127                 app_control_add_extra_data(reply, PLUGIN_RESULT_KEY, PLUGIN_RESULT_VALUE_SUCCESS);
128
129                 auth_provider_add_extra_data(provider->auth_provider, reply);
130                 storage_provider_add_extra_data(provider->storage_provider, reply);
131
132                 // TODO: another adaptor
133         }
134         else if (0 == strcmp(operation, PLUGIN_DISCONNECT_URI))
135         {
136                 app_control_create(&reply);
137                 ret = provider->disconnect();
138
139                 if (SERVICE_ADAPTOR_ERROR_NONE != ret)
140                 {
141                         SAL_ERR("connect() Fail (%d)", ret);
142                         app_control_add_extra_data(reply, PLUGIN_RESULT_KEY, PLUGIN_RESULT_VALUE_FAILURE);
143                         goto catch;
144                 }
145
146                 app_control_add_extra_data(reply, PLUGIN_RESULT_KEY, PLUGIN_RESULT_VALUE_SUCCESS);
147         }
148         else
149         {
150                 reply = auth_provider_message(provider->auth_provider, operation, app_control);
151                 TRY_IF(NULL != reply, "auth_plugin_client_message() Finded");
152
153                 reply = storage_provider_message(provider->storage_provider, operation, app_control);
154                 TRY_IF(NULL != reply, "storage_plugin_client_message() Finded");
155         }
156
157 catch:
158         app_control_reply_to_launch_request(reply, app_control, APP_CONTROL_RESULT_SUCCEEDED);
159         app_control_destroy(reply);
160
161         return SERVICE_ADAPTOR_ERROR_NONE;
162 }