Merge "Merge branch 'master' into resource-manipulation" into resource-manipulation
[platform/upstream/iotivity.git] / resource / csdk / security / src / resourcemanager.c
1 //******************************************************************
2 //
3 // Copyright 2015 Intel Mobile Communications GmbH 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
21 #include "resourcemanager.h"
22 #include "securevirtualresourcetypes.h"
23 #include "aclresource.h"
24 #include "pstatresource.h"
25 #include "doxmresource.h"
26 #include "credresource.h"
27 #include "oic_malloc.h"
28 #include "logger.h"
29 #include "utlist.h"
30 #include <string.h>
31
32 #define TAG PCF("SRM-RM")
33
34 /**
35  * This method is used by all secure resource modules to send responses to REST queries.
36  *
37  * @param ehRequest pointer to entity handler request data structure.
38  * @param ehRet result code from entity handler.
39  * @param rspPayload response payload in JSON.
40  *
41  * @retval  OC_STACK_OK for Success, otherwise some error value
42  */
43 OCStackResult SendSRMResponse(const OCEntityHandlerRequest *ehRequest,
44         OCEntityHandlerResult ehRet, const char *rspPayload)
45 {
46     OC_LOG (INFO, TAG, PCF("SRM sending SRM response"));
47     OCEntityHandlerResponse response = {};
48     if (ehRequest)
49     {
50         response.requestHandle = ehRequest->requestHandle;
51         response.resourceHandle = ehRequest->resource;
52         response.ehResult = ehRet;
53         response.payload = (char *)rspPayload;
54         response.payloadSize = (rspPayload ? strlen(rspPayload) : 0);
55         response.persistentBufferFlag = 0;
56
57         return OCDoResponse(&response);
58     }
59     return OC_STACK_ERROR;
60 }
61
62 /**
63  * Initialize all secure resources ( /oic/sec/cred, /oic/sec/acl, /oic/sec/pstat etc).
64  *
65  * @retval  OC_STACK_OK for Success, otherwise some error value
66  */
67 OCStackResult InitSecureResources( )
68 {
69     OCStackResult ret;
70
71     /*
72      * doxm resource should be initialized first as it contains the DeviceID
73      * which MAY be used during initialization of other resources.
74      */
75
76     ret = InitDoxmResource();
77
78     if(OC_STACK_OK == ret)
79     {
80         ret = InitPstatResource();
81     }
82     if(OC_STACK_OK == ret)
83     {
84         ret = InitACLResource();
85     }
86     if(OC_STACK_OK == ret)
87     {
88         ret = InitCredResource();
89     }
90     if(OC_STACK_OK != ret)
91     {
92         //TODO: Update the default behavior if one of the SVR fails
93         DestroySecureResources();
94     }
95     return ret;
96 }
97
98 /**
99  * Perform cleanup for secure resources ( /oic/sec/cred, /oic/sec/acl, /oic/sec/pstat etc).
100  *
101  * @retval  OC_STACK_OK for Success, otherwise some error value
102  */
103 OCStackResult DestroySecureResources( )
104 {
105     DeInitACLResource();
106     DeInitCredResource();
107     DeInitDoxmResource();
108     DeInitPstatResource();
109
110     return OC_STACK_OK;
111 }