Changes in spec file for WITH_PROCESS_EVENT flag
[platform/upstream/iotivity.git] / resource / csdk / security / provisioning / src / cloud / aclgroup.c
1 /* *****************************************************************
2  *
3  * Copyright 2016 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * *****************************************************************/
20 #include <string.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23
24 #include "utils.h"
25
26 #include "oic_malloc.h"
27 #include "logger.h"
28 #include "ocstack.h"
29 #include "ocpayload.h"
30 #include "pmutility.h"
31 #include "cacommonutil.h"
32
33 #define TAG "OIC_CLOUD_ACL_GROUP"
34
35 /**
36  * ACL create group request received data handler
37  *
38  * @param[in] ctx       context
39  * @param[out] data     data required to external application
40  * @param[in] response  peer response
41  * @return  OCStackResult application result
42  */
43 static OCStackResult handleAclCreateGroupResponse(void *ctx, void **data, OCClientResponse *response)
44 {
45     OC_UNUSED(ctx);
46     if (NULL == response->payload)
47     {
48         OIC_LOG(ERROR, TAG, "Receive NULL payload");
49         return OC_STACK_INVALID_PARAM;
50     }
51
52     char *gid = NULL;
53
54     if (!OCRepPayloadGetPropString((const OCRepPayload *)response->payload, OC_RSRVD_GROUP_ID, &gid))
55     {
56         OIC_LOG_V(ERROR, TAG, "Can't get: %s", OC_RSRVD_GROUP_ID);
57         return OC_STACK_MALFORMED_RESPONSE;
58     }
59
60     *data = gid;
61     return OC_STACK_OK;
62 }
63
64 /**
65  * ACL find my group received data handler
66  *
67  * @param[in] ctx       context
68  * @param[out] data     data required to external application
69  * @param[in] response  peer response
70  * @return  OCStackResult application result
71  */
72 static OCStackResult handleAclFindMyGroupResponse(void *ctx, void **data, OCClientResponse *response)
73 {
74     OC_UNUSED(ctx);
75     if (NULL == response->payload)
76     {
77         OIC_LOG(ERROR, TAG, "Receive NULL payload");
78         return OC_STACK_INVALID_PARAM;
79     }
80
81     const OCRepPayload *payload = (const OCRepPayload *)response->payload;
82     size_t dimensions[MAX_REP_ARRAY_DEPTH] = { 0 };
83
84     stringArray_t *gidlist = OICCalloc(1, sizeof(stringArray_t));
85     if (NULL == gidlist)
86     {
87         OIC_LOG(ERROR, TAG, "Can't allocate gidlist");
88         return OC_STACK_NO_MEMORY;
89     }
90
91     if (!OCRepPayloadGetStringArray(payload, OC_RSRVD_GROUP_ID_LIST, &gidlist->array, dimensions))
92     {
93         OIC_LOG_V(ERROR, TAG, "Can't get: %s", OC_RSRVD_GROUP_ID_LIST);
94         OICFree(gidlist);
95         return OC_STACK_MALFORMED_RESPONSE;
96     }
97
98     gidlist->length = calcDimTotal(dimensions);
99
100     *data = gidlist;
101     return OC_STACK_OK;
102 }
103
104 OCStackResult OCCloudAclCreateGroup(void* ctx,
105                                     const char *groupType,
106                                     const char *groupMasterId,
107                                     const OCDevAddr *endPoint,
108                                     OCCloudResponseCB callback)
109 {
110     char uri[MAX_URI_LENGTH] = { 0 };
111
112     VERIFY_NON_NULL_RET(endPoint, TAG, "NULL endpoint", OC_STACK_INVALID_PARAM);
113     VERIFY_NON_NULL_RET(groupType, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
114
115     snprintf(uri, MAX_URI_LENGTH, "%s%s:%d%s", DEFAULT_PREFIX,
116             endPoint->addr, endPoint->port, OC_RSRVD_ACL_GROUP_URL);
117
118     OCCallbackData cbData;
119     fillCallbackData(&cbData, ctx, callback, handleAclCreateGroupResponse, NULL);
120
121     OCRepPayload *payload = OCRepPayloadCreate();
122     if (!payload)
123     {
124         return OC_STACK_NO_MEMORY;
125     }
126
127     OCRepPayloadSetPropString(payload, OC_RSRVD_GROUP_TYPE, groupType);
128
129     //add next fields if they were filled
130     if (groupMasterId) OCRepPayloadSetPropString(payload, OC_RSRVD_GROUP_MASTER_ID, groupMasterId);
131
132     return OCDoResource(NULL, OC_REST_POST, uri, NULL, (OCPayload *)payload,
133                         CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
134 }
135
136 OCStackResult OCCloudAclFindMyGroup(void* ctx,
137                                     const char *memberId,
138                                     const OCDevAddr *endPoint,
139                                     OCCloudResponseCB callback)
140 {
141     char uri[MAX_URI_LENGTH] = { 0 };
142
143     VERIFY_NON_NULL_RET(endPoint, TAG, "NULL endpoint", OC_STACK_INVALID_PARAM);
144
145     snprintf(uri, MAX_URI_LENGTH, "%s%s:%d%s", DEFAULT_PREFIX,
146             endPoint->addr, endPoint->port, OC_RSRVD_ACL_GROUP_URL);
147
148     if (memberId)
149     {
150         size_t len = strlen(uri);
151         snprintf(uri + len, MAX_URI_LENGTH - len, "?%s=%s", OC_RSRVD_MEMBER_ID, memberId);
152     }
153
154     OCCallbackData cbData;
155     fillCallbackData(&cbData, ctx, callback, handleAclFindMyGroupResponse, NULL);
156
157     return OCDoResource(NULL, OC_REST_GET, uri, NULL, NULL,
158                         CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
159 }
160
161 OCStackResult OCCloudAclDeleteGroup(void* ctx,
162                                     const char *groupId,
163                                     const char *groupMasterId,
164                                     const OCDevAddr *endPoint,
165                                     OCCloudResponseCB callback)
166 {
167     char uri[MAX_URI_LENGTH] = { 0 };
168
169     VERIFY_NON_NULL_RET(endPoint, TAG, "NULL endpoint", OC_STACK_INVALID_PARAM);
170     VERIFY_NON_NULL_RET(groupId, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
171
172     snprintf(uri, MAX_URI_LENGTH, "%s%s:%d%s?%s=%s", DEFAULT_PREFIX,
173             endPoint->addr, endPoint->port, OC_RSRVD_ACL_GROUP_URL, OC_RSRVD_GROUP_ID, groupId);
174
175     if (groupMasterId)
176     {
177         size_t len = strlen(uri);
178         snprintf(uri + len, MAX_URI_LENGTH - len, "&%s=%s", OC_RSRVD_GROUP_MASTER_ID, groupMasterId);
179     }
180
181     OCCallbackData cbData;
182     fillCallbackData(&cbData, ctx, callback, NULL, NULL);
183
184     return OCDoResource(NULL, OC_REST_DELETE, uri, NULL, NULL,
185                         CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
186 }
187
188 OCStackResult OCCloudAclJoinToInvitedGroup(void* ctx,
189                                            const char *groupId,
190                                            const OCDevAddr *endPoint,
191                                            OCCloudResponseCB callback)
192 {
193     char uri[MAX_URI_LENGTH] = { 0 };
194
195     VERIFY_NON_NULL_RET(endPoint, TAG, "NULL endpoint", OC_STACK_INVALID_PARAM);
196     VERIFY_NON_NULL_RET(groupId, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
197
198     snprintf(uri, MAX_URI_LENGTH, "%s%s:%d%s/%s", DEFAULT_PREFIX,
199             endPoint->addr, endPoint->port, OC_RSRVD_ACL_GROUP_URL, groupId);
200
201     OCCallbackData cbData;
202     fillCallbackData(&cbData, ctx, callback, NULL, NULL);
203
204     return OCDoResource(NULL, OC_REST_POST, uri, NULL, NULL,
205                         CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
206 }
207
208 OCStackResult OCCloudAclObserveGroup(void* ctx,
209                                      const char *groupId,
210                                      const OCDevAddr *endPoint,
211                                      OCCloudResponseCB callback)
212 {
213     char uri[MAX_URI_LENGTH] = { 0 };
214
215     VERIFY_NON_NULL_RET(endPoint, TAG, "NULL endpoint", OC_STACK_INVALID_PARAM);
216     VERIFY_NON_NULL_RET(groupId, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
217
218     snprintf(uri, MAX_URI_LENGTH, "%s%s:%d%s/%s", DEFAULT_PREFIX,
219             endPoint->addr, endPoint->port, OC_RSRVD_ACL_GROUP_URL, groupId);
220
221     OCCallbackData cbData;
222     fillCallbackData(&cbData, ctx, callback, NULL, NULL);
223
224     return OCDoResource(NULL, OC_REST_OBSERVE, uri, NULL, NULL,
225                         CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
226 }
227
228 OCStackResult OCCloudAclShareDeviceIntoGroup(void* ctx,
229                                              const char *groupId,
230                                              const stringArray_t *memberIds,
231                                              const stringArray_t *deviceIds,
232                                              const OCDevAddr *endPoint,
233                                              OCCloudResponseCB callback)
234 {
235     size_t dimensions[MAX_REP_ARRAY_DEPTH] = { 0 };
236     char uri[MAX_URI_LENGTH] = { 0 };
237
238     VERIFY_NON_NULL_RET(endPoint, TAG, "NULL endpoint", OC_STACK_INVALID_PARAM);
239     VERIFY_NON_NULL_RET(groupId, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
240     VERIFY_NON_NULL_RET(memberIds, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
241     VERIFY_NON_NULL_RET(deviceIds, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
242
243     snprintf(uri, MAX_URI_LENGTH, "%s%s:%d%s/%s", DEFAULT_PREFIX,
244             endPoint->addr, endPoint->port, OC_RSRVD_ACL_GROUP_URL, groupId);
245
246     OCCallbackData cbData;
247     fillCallbackData(&cbData, ctx, callback, NULL, NULL);
248
249     OCRepPayload *payload = OCRepPayloadCreate();
250     if (!payload)
251     {
252         return OC_STACK_NO_MEMORY;
253     }
254
255     dimensions[0] = memberIds->length;
256     OCRepPayloadSetStringArray(payload, OC_RSRVD_MEMBER_ID_LIST,
257                                (const char **)memberIds->array, dimensions);
258     dimensions[0] = deviceIds->length;
259     OCRepPayloadSetStringArray(payload, OC_RSRVD_DEVICE_ID_LIST,
260                                (const char **)deviceIds->array, dimensions);
261
262     return OCDoResource(NULL, OC_REST_POST, uri, NULL, (OCPayload *)payload,
263                         CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
264 }
265
266 OCStackResult OCCloudAclDeleteDeviceFromGroup(void* ctx,
267                                               const char *groupId,
268                                               const stringArray_t *memberIds,
269                                               const stringArray_t *deviceIds,
270                                               const OCDevAddr *endPoint,
271                                               OCCloudResponseCB callback)
272
273 {
274     char uri[MAX_URI_LENGTH * 4] = { 0 };
275     int max_size = sizeof(uri);
276
277     VERIFY_NON_NULL_RET(endPoint, TAG, "NULL endpoint", OC_STACK_INVALID_PARAM);
278     VERIFY_NON_NULL_RET(groupId, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
279     VERIFY_NON_NULL_RET(memberIds, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
280     VERIFY_NON_NULL_RET(deviceIds, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
281
282     snprintf(uri, max_size, "%s%s:%d%s/%s", DEFAULT_PREFIX,
283             endPoint->addr, endPoint->port, OC_RSRVD_ACL_GROUP_URL, groupId);
284
285     for (size_t i = 0; i < memberIds->length; i++)
286     {
287         size_t len = strlen(uri);
288         snprintf(uri + len, max_size - len, "%c%s=%s", (0 == i)?'?':'&',
289                 OC_RSRVD_MEMBER_ID_LIST, memberIds->array[i]);
290     }
291
292     for (size_t i = 0; i < deviceIds->length; i++)
293     {
294         size_t len = strlen(uri);
295         snprintf(uri + len, max_size - len, "%c%s=%s",
296                 (0 == i && 0 == memberIds->length)?'?':'&',
297                 OC_RSRVD_DEVICE_ID_LIST, deviceIds->array[i]);
298     }
299
300     OCCallbackData cbData;
301     fillCallbackData(&cbData, ctx, callback, NULL, NULL);
302
303     return OCDoResource(NULL, OC_REST_DELETE, uri, NULL, NULL,
304                         CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
305 }
306
307 OCStackResult OCCloudAclGroupGetInfo(void* ctx,
308                                      const char *groupId,
309                                      const char *memberId,
310                                      const OCDevAddr *endPoint,
311                                      OCCloudResponseCB callback)
312 {
313     char uri[MAX_URI_LENGTH] = { 0 };
314
315     VERIFY_NON_NULL_RET(endPoint, TAG, "NULL endpoint", OC_STACK_INVALID_PARAM);
316     VERIFY_NON_NULL_RET(groupId, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
317
318     snprintf(uri, MAX_URI_LENGTH, "%s%s:%d%s/%s", DEFAULT_PREFIX,
319             endPoint->addr, endPoint->port, OC_RSRVD_ACL_GROUP_URL, groupId);
320
321     if (memberId)
322     {
323         size_t len = strlen(uri);
324         snprintf(uri + len, MAX_URI_LENGTH - len, "?%s=%s", OC_RSRVD_MEMBER_ID, memberId);
325     }
326
327     OCCallbackData cbData;
328     fillCallbackData(&cbData, ctx, callback, NULL, NULL);
329
330     return OCDoResource(NULL, OC_REST_GET, uri, NULL, NULL,
331                         CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
332 }
333