Some svace issues fixed
[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         OICFree(cbData.context);
125         return OC_STACK_NO_MEMORY;
126     }
127
128     OCRepPayloadSetPropString(payload, OC_RSRVD_GROUP_TYPE, groupType);
129
130     //add next fields if they were filled
131     if (groupMasterId) OCRepPayloadSetPropString(payload, OC_RSRVD_GROUP_MASTER_ID, groupMasterId);
132
133     return OCDoResource(NULL, OC_REST_POST, uri, NULL, (OCPayload *)payload,
134                         CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
135 }
136
137 OCStackResult OCCloudAclFindMyGroup(void* ctx,
138                                     const char *memberId,
139                                     const OCDevAddr *endPoint,
140                                     OCCloudResponseCB callback)
141 {
142     char uri[MAX_URI_LENGTH] = { 0 };
143
144     VERIFY_NON_NULL_RET(endPoint, TAG, "NULL endpoint", OC_STACK_INVALID_PARAM);
145
146     snprintf(uri, MAX_URI_LENGTH, "%s%s:%d%s", DEFAULT_PREFIX,
147             endPoint->addr, endPoint->port, OC_RSRVD_ACL_GROUP_URL);
148
149     if (memberId)
150     {
151         size_t len = strlen(uri);
152         snprintf(uri + len, MAX_URI_LENGTH - len, "?%s=%s", OC_RSRVD_MEMBER_ID, memberId);
153     }
154
155     OCCallbackData cbData;
156     fillCallbackData(&cbData, ctx, callback, handleAclFindMyGroupResponse, NULL);
157
158     return OCDoResource(NULL, OC_REST_GET, uri, NULL, NULL,
159                         CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
160 }
161
162 OCStackResult OCCloudAclDeleteGroup(void* ctx,
163                                     const char *groupId,
164                                     const char *groupMasterId,
165                                     const OCDevAddr *endPoint,
166                                     OCCloudResponseCB callback)
167 {
168     char uri[MAX_URI_LENGTH] = { 0 };
169
170     VERIFY_NON_NULL_RET(endPoint, TAG, "NULL endpoint", OC_STACK_INVALID_PARAM);
171     VERIFY_NON_NULL_RET(groupId, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
172
173     snprintf(uri, MAX_URI_LENGTH, "%s%s:%d%s?%s=%s", DEFAULT_PREFIX,
174             endPoint->addr, endPoint->port, OC_RSRVD_ACL_GROUP_URL, OC_RSRVD_GROUP_ID, groupId);
175
176     if (groupMasterId)
177     {
178         size_t len = strlen(uri);
179         snprintf(uri + len, MAX_URI_LENGTH - len, "&%s=%s", OC_RSRVD_GROUP_MASTER_ID, groupMasterId);
180     }
181
182     OCCallbackData cbData;
183     fillCallbackData(&cbData, ctx, callback, NULL, NULL);
184
185     return OCDoResource(NULL, OC_REST_DELETE, uri, NULL, NULL,
186                         CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
187 }
188
189 OCStackResult OCCloudAclJoinToInvitedGroup(void* ctx,
190                                            const char *groupId,
191                                            const OCDevAddr *endPoint,
192                                            OCCloudResponseCB callback)
193 {
194     char uri[MAX_URI_LENGTH] = { 0 };
195
196     VERIFY_NON_NULL_RET(endPoint, TAG, "NULL endpoint", OC_STACK_INVALID_PARAM);
197     VERIFY_NON_NULL_RET(groupId, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
198
199     snprintf(uri, MAX_URI_LENGTH, "%s%s:%d%s/%s", DEFAULT_PREFIX,
200             endPoint->addr, endPoint->port, OC_RSRVD_ACL_GROUP_URL, groupId);
201
202     OCCallbackData cbData;
203     fillCallbackData(&cbData, ctx, callback, NULL, NULL);
204
205     return OCDoResource(NULL, OC_REST_POST, uri, NULL, NULL,
206                         CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
207 }
208
209 OCStackResult OCCloudAclObserveGroup(void* ctx,
210                                      const char *groupId,
211                                      const OCDevAddr *endPoint,
212                                      OCCloudResponseCB callback)
213 {
214     char uri[MAX_URI_LENGTH] = { 0 };
215
216     VERIFY_NON_NULL_RET(endPoint, TAG, "NULL endpoint", OC_STACK_INVALID_PARAM);
217     VERIFY_NON_NULL_RET(groupId, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
218
219     snprintf(uri, MAX_URI_LENGTH, "%s%s:%d%s/%s", DEFAULT_PREFIX,
220             endPoint->addr, endPoint->port, OC_RSRVD_ACL_GROUP_URL, groupId);
221
222     OCCallbackData cbData;
223     fillCallbackData(&cbData, ctx, callback, NULL, NULL);
224
225     return OCDoResource(NULL, OC_REST_OBSERVE, uri, NULL, NULL,
226                         CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
227 }
228
229 OCStackResult OCCloudAclShareDeviceIntoGroup(void* ctx,
230                                              const char *groupId,
231                                              const stringArray_t *memberIds,
232                                              const stringArray_t *deviceIds,
233                                              const OCDevAddr *endPoint,
234                                              OCCloudResponseCB callback)
235 {
236     size_t dimensions[MAX_REP_ARRAY_DEPTH] = { 0 };
237     char uri[MAX_URI_LENGTH] = { 0 };
238
239     VERIFY_NON_NULL_RET(endPoint, TAG, "NULL endpoint", OC_STACK_INVALID_PARAM);
240     VERIFY_NON_NULL_RET(groupId, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
241     VERIFY_NON_NULL_RET(memberIds, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
242     VERIFY_NON_NULL_RET(deviceIds, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
243
244     snprintf(uri, MAX_URI_LENGTH, "%s%s:%d%s/%s", DEFAULT_PREFIX,
245             endPoint->addr, endPoint->port, OC_RSRVD_ACL_GROUP_URL, groupId);
246
247     OCCallbackData cbData;
248     fillCallbackData(&cbData, ctx, callback, NULL, NULL);
249
250     OCRepPayload *payload = OCRepPayloadCreate();
251     if (!payload)
252     {
253         OICFree(cbData.context);
254         return OC_STACK_NO_MEMORY;
255     }
256
257     dimensions[0] = memberIds->length;
258     OCRepPayloadSetStringArray(payload, OC_RSRVD_MEMBER_ID_LIST,
259                                (const char **)memberIds->array, dimensions);
260     dimensions[0] = deviceIds->length;
261     OCRepPayloadSetStringArray(payload, OC_RSRVD_DEVICE_ID_LIST,
262                                (const char **)deviceIds->array, dimensions);
263
264     return OCDoResource(NULL, OC_REST_POST, uri, NULL, (OCPayload *)payload,
265                         CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
266 }
267
268 OCStackResult OCCloudAclDeleteDeviceFromGroup(void* ctx,
269                                               const char *groupId,
270                                               const stringArray_t *memberIds,
271                                               const stringArray_t *deviceIds,
272                                               const OCDevAddr *endPoint,
273                                               OCCloudResponseCB callback)
274
275 {
276     char uri[MAX_URI_LENGTH * 4] = { 0 };
277     int max_size = sizeof(uri);
278
279     VERIFY_NON_NULL_RET(endPoint, TAG, "NULL endpoint", OC_STACK_INVALID_PARAM);
280     VERIFY_NON_NULL_RET(groupId, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
281     VERIFY_NON_NULL_RET(memberIds, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
282     VERIFY_NON_NULL_RET(deviceIds, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
283
284     snprintf(uri, max_size, "%s%s:%d%s/%s", DEFAULT_PREFIX,
285             endPoint->addr, endPoint->port, OC_RSRVD_ACL_GROUP_URL, groupId);
286
287     for (size_t i = 0; i < memberIds->length; i++)
288     {
289         size_t len = strlen(uri);
290         snprintf(uri + len, max_size - len, "%c%s=%s", (0 == i)?'?':'&',
291                 OC_RSRVD_MEMBER_ID_LIST, memberIds->array[i]);
292     }
293
294     for (size_t i = 0; i < deviceIds->length; i++)
295     {
296         size_t len = strlen(uri);
297         snprintf(uri + len, max_size - len, "%c%s=%s",
298                 (0 == i && 0 == memberIds->length)?'?':'&',
299                 OC_RSRVD_DEVICE_ID_LIST, deviceIds->array[i]);
300     }
301
302     OCCallbackData cbData;
303     fillCallbackData(&cbData, ctx, callback, NULL, NULL);
304
305     return OCDoResource(NULL, OC_REST_DELETE, uri, NULL, NULL,
306                         CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
307 }
308
309 OCStackResult OCCloudAclGroupGetInfo(void* ctx,
310                                      const char *groupId,
311                                      const char *memberId,
312                                      const OCDevAddr *endPoint,
313                                      OCCloudResponseCB callback)
314 {
315     char uri[MAX_URI_LENGTH] = { 0 };
316
317     VERIFY_NON_NULL_RET(endPoint, TAG, "NULL endpoint", OC_STACK_INVALID_PARAM);
318     VERIFY_NON_NULL_RET(groupId, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
319
320     snprintf(uri, MAX_URI_LENGTH, "%s%s:%d%s/%s", DEFAULT_PREFIX,
321             endPoint->addr, endPoint->port, OC_RSRVD_ACL_GROUP_URL, groupId);
322
323     if (memberId)
324     {
325         size_t len = strlen(uri);
326         snprintf(uri + len, MAX_URI_LENGTH - len, "?%s=%s", OC_RSRVD_MEMBER_ID, memberId);
327     }
328
329     OCCallbackData cbData;
330     fillCallbackData(&cbData, ctx, callback, NULL, NULL);
331
332     return OCDoResource(NULL, OC_REST_GET, uri, NULL, NULL,
333                         CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
334 }
335