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(groupIds, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
189 VERIFY_NON_NULL_RET(memberIds, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
191 if (groupIds->length != memberIds->length)
193 OIC_LOG(ERROR, TAG, "members and groups lists should have the same length!!!");
194 return OC_STACK_INVALID_PARAM;
197 snprintf(uri, MAX_URI_LENGTH, "%s%s:%d%s", DEFAULT_PREFIX,
198 endPoint->addr, endPoint->port, OC_RSRVD_ACL_INVITE_URL);
200 OCCallbackData cbData;
201 fillCallbackData(&cbData, ctx, callback, NULL, NULL);
203 OCRepPayload *payload = OCRepPayloadCreate();
206 return OC_STACK_NO_MEMORY;
209 OCRepPayload **heplerPayload = OICCalloc(groupIds->length, sizeof(OCRepPayload *));
211 for (i = 0; i < groupIds->length; i++)
213 heplerPayload[i] = OCRepPayloadCreate();
214 if (!heplerPayload[i])
218 OCRepPayloadSetPropString(heplerPayload[i], OC_RSRVD_GROUP_ID, groupIds->array[i]);
219 OCRepPayloadSetPropString(heplerPayload[i], OC_RSRVD_MEMBER_ID, memberIds->array[i]);
222 //add next fields if they were filled
223 if (userId) OCRepPayloadSetPropString(payload, OC_RSRVD_USER_UUID, userId);
225 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {groupIds->length, 0, 0};
226 OCRepPayloadSetPropObjectArray(payload, OC_RSRVD_INVITE,
227 (const struct OCRepPayload **)heplerPayload, dimensions);
229 return OCDoResource(NULL, OC_REST_POST, uri, NULL, (OCPayload *)payload,
230 CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
232 OCRepPayloadDestroy(payload);
233 for (size_t k = 0; k < i; k++)
235 OCRepPayloadDestroy(heplerPayload[k]);
237 OCRepPayloadDestroy(*heplerPayload);
241 OCStackResult OCCloudAclGetInvitation(void* ctx,
243 const OCDevAddr *endPoint,
244 OCCloudResponseCB callback)
246 char uri[MAX_URI_LENGTH] = { 0 };
248 snprintf(uri, MAX_URI_LENGTH, "%s%s:%d%s", DEFAULT_PREFIX,
249 endPoint->addr, endPoint->port, OC_RSRVD_ACL_INVITE_URL);
253 size_t len = strlen(uri);
254 snprintf(uri + len, MAX_URI_LENGTH -len, "?%s=%s", OC_RSRVD_USER_UUID, userId);
257 OCCallbackData cbData;
258 fillCallbackData(&cbData, ctx, callback, handleAclGetInvitationResponse, NULL);
260 return OCDoResource(NULL, OC_REST_GET, uri, NULL, NULL,
261 CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
264 OCStackResult OCCloudAclDeleteInvitation(void* ctx,
267 const OCDevAddr *endPoint,
268 OCCloudResponseCB callback)
270 char uri[MAX_URI_LENGTH] = { 0 };
272 VERIFY_NON_NULL_RET(groupId, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
274 snprintf(uri, MAX_URI_LENGTH, "%s%s:%d%s", DEFAULT_PREFIX,
275 endPoint->addr, endPoint->port, OC_RSRVD_ACL_INVITE_URL);
279 size_t len = strlen(uri);
280 snprintf(uri + len, MAX_URI_LENGTH - len, "?%s=%s", OC_RSRVD_USER_UUID, userId);
283 size_t len = strlen(uri);
284 snprintf(uri + len, MAX_URI_LENGTH - len, "%c%s=%s", userId?'&':'?', OC_RSRVD_GROUP_ID, groupId);
286 OCCallbackData cbData;
287 fillCallbackData(&cbData, ctx, callback, NULL, NULL);
289 return OCDoResource(NULL, OC_REST_DELETE, uri, NULL, NULL,
290 CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
293 OCStackResult OCCloudAclCancelInvitation(void* ctx,
296 const char *memberId,
297 const OCDevAddr *endPoint,
298 OCCloudResponseCB callback)
300 char uri[MAX_URI_LENGTH] = { 0 };
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(subjectId, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
339 VERIFY_NON_NULL_RET(deviceId, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
340 VERIFY_NON_NULL_RET(method, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
341 VERIFY_NON_NULL_RET(user_uri, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
343 snprintf(uri, MAX_URI_LENGTH, "%s%s:%d%s", DEFAULT_PREFIX,
344 endPoint->addr, endPoint->port, OC_RSRVD_ACL_VERIFY_URL);
347 snprintf(uri + len, MAX_URI_LENGTH - len, "?%s=%s", OC_RSRVD_SUBJECT_ID, subjectId);
349 snprintf(uri + len, MAX_URI_LENGTH - len, "&%s=%s", OC_RSRVD_DEVICE_ID, deviceId);
351 snprintf(uri + len, MAX_URI_LENGTH - len, "&%s=%s", OC_RSRVD_REQUEST_METHOD, method);
353 snprintf(uri + len, MAX_URI_LENGTH - len, "&%s=%s", OC_RSRVD_REQUEST_URI, user_uri);
355 OCCallbackData cbData;
356 fillCallbackData(&cbData, ctx, callback, handleAclPolicyCheckResponse, NULL);
358 return OCDoResource(NULL, OC_REST_GET, uri, NULL, NULL,
359 CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);