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)
143 * ACL policy check request received data handler
145 * @param[in] ctx context
146 * @param[out] data data required to external application
147 * @param[in] response peer response
148 * @return OCStackResult application result
150 static OCStackResult handleAclPolicyCheckResponse(void *ctx, void **data, OCClientResponse *response)
154 if (NULL == response->payload)
156 OIC_LOG(ERROR, TAG, "Receive NULL payload");
157 return OC_STACK_INVALID_PARAM;
162 if (!OCRepPayloadGetPropString((const OCRepPayload *)response->payload, OC_RSRVD_GROUP_PERMISSION, &gp))
164 OIC_LOG_V(ERROR, TAG, "Can't get: %s", OC_RSRVD_GROUP_PERMISSION);
165 return OC_STACK_MALFORMED_RESPONSE;
172 OCStackResult OCCloudAclInviteUser(void* ctx,
174 const stringArray_t *groupIds,
175 const stringArray_t *memberIds,
176 const OCDevAddr *endPoint,
177 OCCloudResponseCB callback)
179 OCStackResult result = OC_STACK_ERROR;
180 char uri[MAX_URI_LENGTH] = { 0 };
183 VERIFY_NON_NULL_RET(endPoint, TAG, "NULL endpoint", OC_STACK_INVALID_PARAM);
184 VERIFY_NON_NULL_RET(groupIds, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
185 VERIFY_NON_NULL_RET(memberIds, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
187 if (groupIds->length != memberIds->length)
189 OIC_LOG(ERROR, TAG, "members and groups lists should have the same length!!!");
190 return OC_STACK_INVALID_PARAM;
193 snprintf(uri, MAX_URI_LENGTH, "%s%s:%d%s", DEFAULT_PREFIX,
194 endPoint->addr, endPoint->port, OC_RSRVD_ACL_INVITE_URL);
196 OCCallbackData cbData;
197 fillCallbackData(&cbData, ctx, callback, NULL, NULL);
199 OCRepPayload *payload = OCRepPayloadCreate();
202 return OC_STACK_NO_MEMORY;
205 OCRepPayload **heplerPayload = OICCalloc(groupIds->length, sizeof(OCRepPayload *));
207 for (i = 0; i < groupIds->length; i++)
209 heplerPayload[i] = OCRepPayloadCreate();
210 if (!heplerPayload[i])
214 OCRepPayloadSetPropString(heplerPayload[i], OC_RSRVD_GROUP_ID, groupIds->array[i]);
215 OCRepPayloadSetPropString(heplerPayload[i], OC_RSRVD_MEMBER_ID, memberIds->array[i]);
218 //add next fields if they were filled
219 if (userId) OCRepPayloadSetPropString(payload, OC_RSRVD_USER_UUID, userId);
221 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {groupIds->length, 0, 0};
222 OCRepPayloadSetPropObjectArray(payload, OC_RSRVD_INVITE,
223 (const struct OCRepPayload **)heplerPayload, dimensions);
225 return OCDoResource(NULL, OC_REST_POST, uri, NULL, (OCPayload *)payload,
226 CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
228 OCRepPayloadDestroy(payload);
229 for (size_t k = 0; k < i; k++)
231 OCRepPayloadDestroy(heplerPayload[k]);
233 OCRepPayloadDestroy(*heplerPayload);
237 OCStackResult OCCloudAclGetInvitation(void* ctx,
239 const OCDevAddr *endPoint,
240 OCCloudResponseCB callback)
242 char uri[MAX_URI_LENGTH] = { 0 };
244 VERIFY_NON_NULL_RET(endPoint, TAG, "NULL endpoint", OC_STACK_INVALID_PARAM);
246 snprintf(uri, MAX_URI_LENGTH, "%s%s:%d%s", DEFAULT_PREFIX,
247 endPoint->addr, endPoint->port, OC_RSRVD_ACL_INVITE_URL);
251 size_t len = strlen(uri);
252 snprintf(uri + len, MAX_URI_LENGTH -len, "?%s=%s", OC_RSRVD_USER_UUID, userId);
255 OCCallbackData cbData;
256 fillCallbackData(&cbData, ctx, callback, handleAclGetInvitationResponse, NULL);
258 return OCDoResource(NULL, OC_REST_GET, uri, NULL, NULL,
259 CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
262 OCStackResult OCCloudAclDeleteInvitation(void* ctx,
265 const OCDevAddr *endPoint,
266 OCCloudResponseCB callback)
268 char uri[MAX_URI_LENGTH] = { 0 };
270 VERIFY_NON_NULL_RET(endPoint, TAG, "NULL endpoint", OC_STACK_INVALID_PARAM);
271 VERIFY_NON_NULL_RET(groupId, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
273 snprintf(uri, MAX_URI_LENGTH, "%s%s:%d%s", DEFAULT_PREFIX,
274 endPoint->addr, endPoint->port, OC_RSRVD_ACL_INVITE_URL);
278 size_t len = strlen(uri);
279 snprintf(uri + len, MAX_URI_LENGTH - len, "?%s=%s", OC_RSRVD_USER_UUID, userId);
282 size_t len = strlen(uri);
283 snprintf(uri + len, MAX_URI_LENGTH - len, "%c%s=%s", userId?'&':'?', OC_RSRVD_GROUP_ID, groupId);
285 OCCallbackData cbData;
286 fillCallbackData(&cbData, ctx, callback, NULL, NULL);
288 return OCDoResource(NULL, OC_REST_DELETE, uri, NULL, NULL,
289 CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
292 OCStackResult OCCloudAclCancelInvitation(void* ctx,
295 const char *memberId,
296 const OCDevAddr *endPoint,
297 OCCloudResponseCB callback)
299 char uri[MAX_URI_LENGTH] = { 0 };
302 VERIFY_NON_NULL_RET(endPoint, TAG, "NULL endpoint", OC_STACK_INVALID_PARAM);
303 VERIFY_NON_NULL_RET(groupId, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
304 VERIFY_NON_NULL_RET(memberId, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
306 snprintf(uri, MAX_URI_LENGTH, "%s%s:%d%s", DEFAULT_PREFIX,
307 endPoint->addr, endPoint->port, OC_RSRVD_ACL_INVITE_URL);
311 size_t len = strlen(uri);
312 snprintf(uri + len, MAX_URI_LENGTH - len, "?%s=%s", OC_RSRVD_USER_UUID, userId);
316 snprintf(uri + len, MAX_URI_LENGTH - len, "%c%s=%s", userId?'&':'?', OC_RSRVD_GROUP_ID, groupId);
318 snprintf(uri + len, MAX_URI_LENGTH - len, "&%s=%s", OC_RSRVD_MEMBER_ID, memberId);
320 OCCallbackData cbData;
321 fillCallbackData(&cbData, ctx, callback, NULL, NULL);
323 return OCDoResource(NULL, OC_REST_DELETE, uri, NULL, NULL,
324 CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
327 OCStackResult OCCloudAclPolicyCheck(void* ctx,
328 const char *subjectId,
329 const char *deviceId,
331 const char *user_uri,
332 const OCDevAddr *endPoint,
333 OCCloudResponseCB callback)
335 char uri[MAX_URI_LENGTH] = { 0 };
338 VERIFY_NON_NULL_RET(endPoint, TAG, "NULL endpoint", OC_STACK_INVALID_PARAM);
339 VERIFY_NON_NULL_RET(subjectId, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
340 VERIFY_NON_NULL_RET(deviceId, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
341 VERIFY_NON_NULL_RET(method, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
342 VERIFY_NON_NULL_RET(user_uri, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
344 snprintf(uri, MAX_URI_LENGTH, "%s%s:%d%s", DEFAULT_PREFIX,
345 endPoint->addr, endPoint->port, OC_RSRVD_ACL_VERIFY_URL);
348 snprintf(uri + len, MAX_URI_LENGTH - len, "?%s=%s", OC_RSRVD_SUBJECT_ID, subjectId);
350 snprintf(uri + len, MAX_URI_LENGTH - len, "&%s=%s", OC_RSRVD_DEVICE_ID, deviceId);
352 snprintf(uri + len, MAX_URI_LENGTH - len, "&%s=%s", OC_RSRVD_REQUEST_METHOD, method);
354 snprintf(uri + len, MAX_URI_LENGTH - len, "&%s=%s", OC_RSRVD_REQUEST_URI, user_uri);
356 OCCallbackData cbData;
357 fillCallbackData(&cbData, ctx, callback, handleAclPolicyCheckResponse, NULL);
359 return OCDoResource(NULL, OC_REST_GET, uri, NULL, NULL,
360 CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);