1 /* *****************************************************************
3 * Copyright 2016 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * *****************************************************************/
26 #include "oic_malloc.h"
29 #include "ocpayload.h"
30 #include "pmutility.h"
31 #include "cacommonutil.h"
33 #define TAG "CLOUD-ACL-GROUP"
36 * ACL create group request received data handler
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
43 static OCStackResult handleAclCreateGroupResponse(void *ctx, void **data, OCClientResponse *response)
46 if (NULL == response->payload)
48 OIC_LOG(ERROR, TAG, "Receive NULL payload");
49 return OC_STACK_INVALID_PARAM;
54 if (!OCRepPayloadGetPropString((const OCRepPayload *)response->payload, OC_RSRVD_GROUP_ID, &gid))
56 OIC_LOG_V(ERROR, TAG, "Can't get: %s", OC_RSRVD_GROUP_ID);
57 return OC_STACK_MALFORMED_RESPONSE;
65 * ACL find my group received data handler
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
72 static OCStackResult handleAclFindMyGroupResponse(void *ctx, void **data, OCClientResponse *response)
75 if (NULL == response->payload)
77 OIC_LOG(ERROR, TAG, "Receive NULL payload");
78 return OC_STACK_INVALID_PARAM;
81 const OCRepPayload *payload = (const OCRepPayload *)response->payload;
82 size_t dimensions[MAX_REP_ARRAY_DEPTH] = { 0 };
84 stringArray_t *gidlist = OICCalloc(1, sizeof(stringArray_t));
87 OIC_LOG(ERROR, TAG, "Can't allocate gidlist");
88 return OC_STACK_NO_MEMORY;
91 if (!OCRepPayloadGetStringArray(payload, OC_RSRVD_GROUP_ID_LIST, &gidlist->array, dimensions))
93 OIC_LOG_V(ERROR, TAG, "Can't get: %s", OC_RSRVD_GROUP_ID_LIST);
95 return OC_STACK_MALFORMED_RESPONSE;
98 gidlist->length = calcDimTotal(dimensions);
104 OCStackResult OCCloudAclCreateGroup(void* ctx,
105 const char *groupType,
106 const char *groupMasterId,
107 const OCDevAddr *endPoint,
108 OCCloudResponseCB callback)
110 char uri[MAX_URI_LENGTH] = { 0 };
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);
115 snprintf(uri, MAX_URI_LENGTH, "%s%s:%d%s", DEFAULT_PREFIX,
116 endPoint->addr, endPoint->port, OC_RSRVD_ACL_GROUP_URL);
118 OCCallbackData cbData;
119 fillCallbackData(&cbData, ctx, callback, handleAclCreateGroupResponse, NULL);
121 OCRepPayload *payload = OCRepPayloadCreate();
124 return OC_STACK_NO_MEMORY;
127 OCRepPayloadSetPropString(payload, OC_RSRVD_GROUP_TYPE, groupType);
129 //add next fields if they were filled
130 if (groupMasterId) OCRepPayloadSetPropString(payload, OC_RSRVD_GROUP_MASTER_ID, groupMasterId);
132 return OCDoResource(NULL, OC_REST_POST, uri, NULL, (OCPayload *)payload,
133 CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
136 OCStackResult OCCloudAclFindMyGroup(void* ctx,
137 const char *memberId,
138 const OCDevAddr *endPoint,
139 OCCloudResponseCB callback)
141 char uri[MAX_URI_LENGTH] = { 0 };
143 VERIFY_NON_NULL_RET(endPoint, TAG, "NULL endpoint", OC_STACK_INVALID_PARAM);
145 snprintf(uri, MAX_URI_LENGTH, "%s%s:%d%s", DEFAULT_PREFIX,
146 endPoint->addr, endPoint->port, OC_RSRVD_ACL_GROUP_URL);
150 size_t len = strlen(uri);
151 snprintf(uri + len, MAX_URI_LENGTH - len, "?%s=%s", OC_RSRVD_MEMBER_ID, memberId);
154 OCCallbackData cbData;
155 fillCallbackData(&cbData, ctx, callback, handleAclFindMyGroupResponse, NULL);
157 return OCDoResource(NULL, OC_REST_GET, uri, NULL, NULL,
158 CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
161 OCStackResult OCCloudAclDeleteGroup(void* ctx,
163 const char *groupMasterId,
164 const OCDevAddr *endPoint,
165 OCCloudResponseCB callback)
167 char uri[MAX_URI_LENGTH] = { 0 };
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);
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);
177 size_t len = strlen(uri);
178 snprintf(uri + len, MAX_URI_LENGTH - len, "&%s=%s", OC_RSRVD_GROUP_MASTER_ID, groupMasterId);
181 OCCallbackData cbData;
182 fillCallbackData(&cbData, ctx, callback, NULL, NULL);
184 return OCDoResource(NULL, OC_REST_DELETE, uri, NULL, NULL,
185 CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
188 OCStackResult OCCloudAclJoinToInvitedGroup(void* ctx,
190 const OCDevAddr *endPoint,
191 OCCloudResponseCB callback)
193 char uri[MAX_URI_LENGTH] = { 0 };
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);
198 snprintf(uri, MAX_URI_LENGTH, "%s%s:%d%s/%s", DEFAULT_PREFIX,
199 endPoint->addr, endPoint->port, OC_RSRVD_ACL_GROUP_URL, groupId);
201 OCCallbackData cbData;
202 fillCallbackData(&cbData, ctx, callback, NULL, NULL);
204 return OCDoResource(NULL, OC_REST_POST, uri, NULL, NULL,
205 CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
208 OCStackResult OCCloudAclObserveGroup(void* ctx,
210 const OCDevAddr *endPoint,
211 OCCloudResponseCB callback)
213 char uri[MAX_URI_LENGTH] = { 0 };
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);
218 snprintf(uri, MAX_URI_LENGTH, "%s%s:%d%s/%s", DEFAULT_PREFIX,
219 endPoint->addr, endPoint->port, OC_RSRVD_ACL_GROUP_URL, groupId);
221 OCCallbackData cbData;
222 fillCallbackData(&cbData, ctx, callback, NULL, NULL);
224 return OCDoResource(NULL, OC_REST_OBSERVE, uri, NULL, NULL,
225 CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
228 OCStackResult OCCloudAclShareDeviceIntoGroup(void* ctx,
230 const stringArray_t *memberIds,
231 const stringArray_t *deviceIds,
232 const OCDevAddr *endPoint,
233 OCCloudResponseCB callback)
235 size_t dimensions[MAX_REP_ARRAY_DEPTH] = { 0 };
236 char uri[MAX_URI_LENGTH] = { 0 };
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);
243 snprintf(uri, MAX_URI_LENGTH, "%s%s:%d%s/%s", DEFAULT_PREFIX,
244 endPoint->addr, endPoint->port, OC_RSRVD_ACL_GROUP_URL, groupId);
246 OCCallbackData cbData;
247 fillCallbackData(&cbData, ctx, callback, NULL, NULL);
249 OCRepPayload *payload = OCRepPayloadCreate();
252 return OC_STACK_NO_MEMORY;
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);
262 return OCDoResource(NULL, OC_REST_POST, uri, NULL, (OCPayload *)payload,
263 CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
266 OCStackResult OCCloudAclDeleteDeviceFromGroup(void* ctx,
268 const stringArray_t *memberIds,
269 const stringArray_t *deviceIds,
270 const OCDevAddr *endPoint,
271 OCCloudResponseCB callback)
274 char uri[MAX_URI_LENGTH * 4] = { 0 };
275 int max_size = sizeof(uri);
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);
282 snprintf(uri, max_size, "%s%s:%d%s/%s", DEFAULT_PREFIX,
283 endPoint->addr, endPoint->port, OC_RSRVD_ACL_GROUP_URL, groupId);
285 for (size_t i = 0; i < memberIds->length; i++)
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]);
292 for (size_t i = 0; i < deviceIds->length; i++)
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]);
300 OCCallbackData cbData;
301 fillCallbackData(&cbData, ctx, callback, NULL, NULL);
303 return OCDoResource(NULL, OC_REST_DELETE, uri, NULL, NULL,
304 CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
307 OCStackResult OCCloudAclGroupGetInfo(void* ctx,
309 const char *memberId,
310 const OCDevAddr *endPoint,
311 OCCloudResponseCB callback)
313 char uri[MAX_URI_LENGTH] = { 0 };
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);
318 snprintf(uri, MAX_URI_LENGTH, "%s%s:%d%s/%s", DEFAULT_PREFIX,
319 endPoint->addr, endPoint->port, OC_RSRVD_ACL_GROUP_URL, groupId);
323 size_t len = strlen(uri);
324 snprintf(uri + len, MAX_URI_LENGTH - len, "?%s=%s", OC_RSRVD_MEMBER_ID, memberId);
327 OCCallbackData cbData;
328 fillCallbackData(&cbData, ctx, callback, NULL, NULL);
330 return OCDoResource(NULL, OC_REST_GET, uri, NULL, NULL,
331 CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);