5 #include "occloudprovisioning.h"
8 #include "oic_malloc.h"
11 #include "ocpayload.h"
12 #include "pmutility.h"
13 #include "cacommonutil.h"
15 #define TAG "CLOUD-ACL-INVITE"
18 * This helper function parses "name" : { "gid":[], "mid":[] } payload
20 * @param[in] payload received payload
21 * @param[in] name property name
22 * @param[out] out string array pair to fill
23 * @return OCStackResult application result
25 static OCStackResult parseInvitePayload(const OCRepPayload *payload, const char *name, stringArrayPair_t *out)
27 OCStackResult result = OC_STACK_NO_MEMORY;
28 size_t dimensions[MAX_REP_ARRAY_DEPTH] = { 0 };
29 OCRepPayload **heplerPayload = NULL;
32 if (!OCRepPayloadGetPropObjectArray(payload, name, &heplerPayload, dimensions))
34 OIC_LOG_V(ERROR, TAG, "Can't get: %s", name);
35 return OC_STACK_MALFORMED_RESPONSE;
38 size_t count = calcDimTotal(dimensions);
40 stringArray_t *gidlist = &out->gidlist;
41 stringArray_t *midlist = &out->midlist;
43 gidlist->length = count;
44 midlist->length = count;
46 gidlist->array = OICCalloc(gidlist->length, sizeof(char *));
47 if (NULL == gidlist->array)
49 OIC_LOG(ERROR, TAG, "Can't allocate gidlist->array");
53 midlist->array = OICCalloc(midlist->length, sizeof(char *));
54 if (NULL == midlist->array)
56 OIC_LOG(ERROR, TAG, "Can't allocate midlist->array");
60 for (i = 0; i < gidlist->length; i++)
62 const OCRepPayload *payload = heplerPayload[i];
64 if (!OCRepPayloadGetPropString(payload, OC_RSRVD_GROUP_ID, &gidlist->array[i]))
66 OIC_LOG_V(ERROR, TAG, "Can't get: %s", OC_RSRVD_GROUP_ID);
67 result = OC_STACK_MALFORMED_RESPONSE;
71 if (!OCRepPayloadGetPropString(payload, OC_RSRVD_MEMBER_ID, &midlist->array[i]))
73 OIC_LOG_V(ERROR, TAG, "Can't get: %s", OC_RSRVD_MEMBER_ID);
74 result = OC_STACK_MALFORMED_RESPONSE;
77 OCRepPayloadDestroy(heplerPayload[i]);
83 if (result != OC_STACK_OK)
85 clearStringArray(gidlist);
86 clearStringArray(midlist);
88 for (size_t k = i; k < gidlist->length; k++)
90 OCRepPayloadDestroy(heplerPayload[i]);
93 OICFree(heplerPayload);
98 * ACL get invitation request received data handler
100 * @param[in] ctx context
101 * @param[out] data data required to external application
102 * @param[in] response peer response
103 * @return OCStackResult application result
105 static OCStackResult handleAclGetInvitationResponse(void *ctx, void **data, OCClientResponse *response)
108 OCStackResult result = OC_STACK_OK;
110 if (NULL == response->payload)
112 OIC_LOG(ERROR, TAG, "Receive NULL payload");
113 return OC_STACK_INVALID_PARAM;
116 inviteResponse_t *answer = OICCalloc(1, sizeof(inviteResponse_t));
119 OIC_LOG(ERROR, TAG, "Can't allocate answer");
120 return OC_STACK_NO_MEMORY;
123 const OCRepPayload *payload = (const OCRepPayload *)response->payload;
125 result = parseInvitePayload(payload, OC_RSRVD_INVITE, &answer->invite);
126 if (result != OC_STACK_OK)
131 result = parseInvitePayload(payload, OC_RSRVD_INVITED, &answer->invited);
132 if (result != OC_STACK_OK)
140 if (result != OC_STACK_OK)
148 * ACL policy check request received data handler
150 * @param[in] ctx context
151 * @param[out] data data required to external application
152 * @param[in] response peer response
153 * @return OCStackResult application result
155 static OCStackResult handleAclPolicyCheckResponse(void *ctx, void **data, OCClientResponse *response)
159 if (NULL == response->payload)
161 OIC_LOG(ERROR, TAG, "Receive NULL payload");
162 return OC_STACK_INVALID_PARAM;
167 if (!OCRepPayloadGetPropString((const OCRepPayload *)response->payload, OC_RSRVD_GROUP_PERMISSION, &gp))
169 OIC_LOG_V(ERROR, TAG, "Can't get: %s", OC_RSRVD_GROUP_PERMISSION);
170 return OC_STACK_MALFORMED_RESPONSE;
177 OCStackResult OCCloudAclInviteUser(void* ctx,
179 const stringArray_t *groupIds,
180 const stringArray_t *memberIds,
181 const OCDevAddr *endPoint,
182 OCCloudResponseCB callback)
184 OCStackResult result = OC_STACK_ERROR;
185 char uri[MAX_URI_LENGTH] = { 0 };
188 VERIFY_NON_NULL_RET(endPoint, TAG, "NULL endpoint", OC_STACK_INVALID_PARAM);
189 VERIFY_NON_NULL_RET(groupIds, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
190 VERIFY_NON_NULL_RET(memberIds, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
192 if (groupIds->length != memberIds->length)
194 OIC_LOG(ERROR, TAG, "members and groups lists should have the same length!!!");
195 return OC_STACK_INVALID_PARAM;
198 snprintf(uri, MAX_URI_LENGTH, "%s%s:%d%s", DEFAULT_PREFIX,
199 endPoint->addr, endPoint->port, OC_RSRVD_ACL_INVITE_URL);
201 OCCallbackData cbData;
202 fillCallbackData(&cbData, ctx, callback, NULL, NULL);
204 OCRepPayload *payload = OCRepPayloadCreate();
207 return OC_STACK_NO_MEMORY;
210 OCRepPayload **heplerPayload = OICCalloc(groupIds->length, sizeof(OCRepPayload *));
212 for (i = 0; i < groupIds->length; i++)
214 heplerPayload[i] = OCRepPayloadCreate();
215 if (!heplerPayload[i])
219 OCRepPayloadSetPropString(heplerPayload[i], OC_RSRVD_GROUP_ID, groupIds->array[i]);
220 OCRepPayloadSetPropString(heplerPayload[i], OC_RSRVD_MEMBER_ID, memberIds->array[i]);
223 //add next fields if they were filled
224 if (userId) OCRepPayloadSetPropString(payload, OC_RSRVD_USER_UUID, userId);
226 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {groupIds->length, 0, 0};
227 OCRepPayloadSetPropObjectArray(payload, OC_RSRVD_INVITE,
228 (const struct OCRepPayload **)heplerPayload, dimensions);
230 return OCDoResource(NULL, OC_REST_POST, uri, NULL, (OCPayload *)payload,
231 CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
233 OCRepPayloadDestroy(payload);
234 for (size_t k = 0; k < i; k++)
236 OCRepPayloadDestroy(heplerPayload[k]);
238 OCRepPayloadDestroy(*heplerPayload);
242 OCStackResult OCCloudAclGetInvitation(void* ctx,
244 const OCDevAddr *endPoint,
245 OCCloudResponseCB callback)
247 char uri[MAX_URI_LENGTH] = { 0 };
249 VERIFY_NON_NULL_RET(endPoint, TAG, "NULL endpoint", OC_STACK_INVALID_PARAM);
251 snprintf(uri, MAX_URI_LENGTH, "%s%s:%d%s", DEFAULT_PREFIX,
252 endPoint->addr, endPoint->port, OC_RSRVD_ACL_INVITE_URL);
256 size_t len = strlen(uri);
257 snprintf(uri + len, MAX_URI_LENGTH -len, "?%s=%s", OC_RSRVD_USER_UUID, userId);
260 OCCallbackData cbData;
261 fillCallbackData(&cbData, ctx, callback, handleAclGetInvitationResponse, NULL);
263 return OCDoResource(NULL, OC_REST_GET, uri, NULL, NULL,
264 CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
267 OCStackResult OCCloudAclDeleteInvitation(void* ctx,
270 const OCDevAddr *endPoint,
271 OCCloudResponseCB callback)
273 char uri[MAX_URI_LENGTH] = { 0 };
275 VERIFY_NON_NULL_RET(endPoint, TAG, "NULL endpoint", OC_STACK_INVALID_PARAM);
276 VERIFY_NON_NULL_RET(groupId, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
278 snprintf(uri, MAX_URI_LENGTH, "%s%s:%d%s", DEFAULT_PREFIX,
279 endPoint->addr, endPoint->port, OC_RSRVD_ACL_INVITE_URL);
283 size_t len = strlen(uri);
284 snprintf(uri + len, MAX_URI_LENGTH - len, "?%s=%s", OC_RSRVD_USER_UUID, userId);
287 size_t len = strlen(uri);
288 snprintf(uri + len, MAX_URI_LENGTH - len, "%c%s=%s", userId?'&':'?', OC_RSRVD_GROUP_ID, groupId);
290 OCCallbackData cbData;
291 fillCallbackData(&cbData, ctx, callback, NULL, NULL);
293 return OCDoResource(NULL, OC_REST_DELETE, uri, NULL, NULL,
294 CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
297 OCStackResult OCCloudAclCancelInvitation(void* ctx,
300 const char *memberId,
301 const OCDevAddr *endPoint,
302 OCCloudResponseCB callback)
304 char uri[MAX_URI_LENGTH] = { 0 };
307 VERIFY_NON_NULL_RET(endPoint, TAG, "NULL endpoint", OC_STACK_INVALID_PARAM);
308 VERIFY_NON_NULL_RET(groupId, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
309 VERIFY_NON_NULL_RET(memberId, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
311 snprintf(uri, MAX_URI_LENGTH, "%s%s:%d%s", DEFAULT_PREFIX,
312 endPoint->addr, endPoint->port, OC_RSRVD_ACL_INVITE_URL);
316 size_t len = strlen(uri);
317 snprintf(uri + len, MAX_URI_LENGTH - len, "?%s=%s", OC_RSRVD_USER_UUID, userId);
321 snprintf(uri + len, MAX_URI_LENGTH - len, "%c%s=%s", userId?'&':'?', OC_RSRVD_GROUP_ID, groupId);
323 snprintf(uri + len, MAX_URI_LENGTH - len, "&%s=%s", OC_RSRVD_MEMBER_ID, memberId);
325 OCCallbackData cbData;
326 fillCallbackData(&cbData, ctx, callback, NULL, NULL);
328 return OCDoResource(NULL, OC_REST_DELETE, uri, NULL, NULL,
329 CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
332 OCStackResult OCCloudAclPolicyCheck(void* ctx,
333 const char *subjectId,
334 const char *deviceId,
336 const char *user_uri,
337 const OCDevAddr *endPoint,
338 OCCloudResponseCB callback)
340 char uri[MAX_URI_LENGTH] = { 0 };
343 VERIFY_NON_NULL_RET(endPoint, TAG, "NULL endpoint", OC_STACK_INVALID_PARAM);
344 VERIFY_NON_NULL_RET(subjectId, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
345 VERIFY_NON_NULL_RET(deviceId, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
346 VERIFY_NON_NULL_RET(method, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
347 VERIFY_NON_NULL_RET(user_uri, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
349 snprintf(uri, MAX_URI_LENGTH, "%s%s:%d%s", DEFAULT_PREFIX,
350 endPoint->addr, endPoint->port, OC_RSRVD_ACL_VERIFY_URL);
353 snprintf(uri + len, MAX_URI_LENGTH - len, "?%s=%s", OC_RSRVD_SUBJECT_ID, subjectId);
355 snprintf(uri + len, MAX_URI_LENGTH - len, "&%s=%s", OC_RSRVD_DEVICE_ID, deviceId);
357 snprintf(uri + len, MAX_URI_LENGTH - len, "&%s=%s", OC_RSRVD_REQUEST_METHOD, method);
359 snprintf(uri + len, MAX_URI_LENGTH - len, "&%s=%s", OC_RSRVD_REQUEST_URI, user_uri);
361 OCCallbackData cbData;
362 fillCallbackData(&cbData, ctx, callback, handleAclPolicyCheckResponse, NULL);
364 return OCDoResource(NULL, OC_REST_GET, uri, NULL, NULL,
365 CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);