Configure resource security
[platform/upstream/iotivity.git] / service / notification / src / provider / NSProviderResource.c
1 //******************************************************************
2 //
3 // Copyright 2016 Samsung Electronics 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 "NSProviderResource.h"
22 #include <string.h>
23
24 NSNotificationResource NotificationResource;
25 NSMessageResource NotificationMessageResource;
26 NSSyncResource NotificationSyncResource;
27 NSTopicResource NotificationTopicResource;
28
29 #if(defined WITH_CLOUD && defined RD_CLIENT)
30 #define DEFAULT_CONTEXT_VALUE 0x99
31
32 OCStackApplicationResult NSHandlePublishCb(void *ctx, OCDoHandle handle,
33     OCClientResponse *clientResponse)
34 {
35     (void) handle;
36     if(ctx != (void *)DEFAULT_CONTEXT_VALUE)
37     {
38         NS_LOG(DEBUG, "Invalid Publish callback received");
39     } 
40
41     NS_LOG_V(DEBUG, "Publish resource response received code: %d", clientResponse->result);
42
43     return OC_STACK_KEEP_TRANSACTION;
44 }
45
46 NSResult NSPublishResourceToCloud(char *serverAddress)
47 {
48
49     NS_LOG(DEBUG, "NSPublishResourceToCloud - IN");
50     NS_LOG_V(DEBUG, "Remote Server Address: %s", serverAddress);
51
52     OCCallbackData cbData;
53     cbData.cb = NSHandlePublishCb;
54     cbData.context = (void *)DEFAULT_CONTEXT_VALUE;
55     cbData.cd = NULL;
56
57     OCResourceHandle resourceHandles[1] = {NotificationResource.handle};
58     OCStackResult res = OCRDPublish(serverAddress, CT_ADAPTER_TCP, resourceHandles, 1,
59             &cbData, OC_LOW_QOS);
60
61     if (res != OC_STACK_OK)
62     {
63         NS_LOG_V(DEBUG, "Unable to publish resources to cloud: %d", res);
64     }
65
66     NS_LOG(DEBUG, "NSPublishResourceToCloud - OUT");
67     return NS_OK;
68 }
69 #endif
70
71
72 NSResult NSCreateResource(char *uri)
73 {
74     NS_LOG(DEBUG, "NSCreateResource - IN");
75
76     if (!uri)
77     {
78         NS_LOG(NS_ERROR, "Resource URI cannot be NULL");
79         return NS_ERROR;
80     }
81
82     uint8_t resourceProperties;
83
84     if (strcmp(uri, NS_ROOT_URI) == 0)
85     {
86         NotificationResource.policy = true;
87         (NotificationResource.providerId)[0] = '\0';
88         NotificationResource.message_uri = NS_COLLECTION_MESSAGE_URI;
89         NotificationResource.sync_uri = NS_COLLECTION_SYNC_URI;
90         NotificationResource.topic_uri = NS_COLLECTION_TOPIC_URI;
91         NotificationResource.version = VERSION;
92         NotificationResource.handle = NULL;
93
94         if(NSGetResourceSecurity())
95         {
96             NS_LOG(DEBUG, "Create secured resource");
97             resourceProperties = OC_DISCOVERABLE | OC_SECURE;
98         }
99         else
100         {
101             resourceProperties = OC_DISCOVERABLE;
102         }
103
104         if (OCCreateResource(&NotificationResource.handle, NS_ROOT_TYPE, NS_DEFAULT_INTERFACE,
105                 NS_ROOT_URI, NSEntityHandlerNotificationCb, NULL, resourceProperties) != OC_STACK_OK)
106         {
107             NS_LOG(NS_ERROR, "Fail to Create Notification Resource");
108             return NS_ERROR;
109         }
110     }
111     else if (strcmp(uri, NS_COLLECTION_MESSAGE_URI) == 0)
112     {
113
114         NotificationMessageResource.messageId = 0;
115
116         (NotificationMessageResource.providerId)[0] = '\0';
117         NotificationMessageResource.type = 0;
118         NotificationMessageResource.dateTime = NULL;
119         NotificationMessageResource.ttl = 0;
120         NotificationMessageResource.title = NULL;
121         NotificationMessageResource.contentText = NULL;
122         NotificationMessageResource.sourceName = NULL;
123         NotificationMessageResource.topicName = NULL;
124         NotificationMessageResource.mediaContents = NULL;
125
126         if(NSGetResourceSecurity())
127         {
128             NS_LOG(DEBUG, "Create secured resource");
129             resourceProperties = OC_OBSERVABLE | OC_SECURE;
130         }
131         else
132         {
133             resourceProperties = OC_OBSERVABLE;
134         }
135
136         if (OCCreateResource(&NotificationMessageResource.handle, NS_COLLECTION_MESSAGE_TYPE,
137                 NS_DEFAULT_INTERFACE, NS_COLLECTION_MESSAGE_URI, NSEntityHandlerMessageCb, NULL,
138                 resourceProperties) != OC_STACK_OK)
139         {
140             NS_LOG(NS_ERROR, "Fail to Create Notification Message Resource");
141             return NS_ERROR;
142         }
143     }
144     else if (strcmp(uri, NS_COLLECTION_SYNC_URI) == 0)
145     {
146         NotificationSyncResource.id = NULL;
147         (NotificationSyncResource.providerId)[0] = '\0';
148         NotificationSyncResource.state = NULL;
149         NotificationSyncResource.handle = NULL;
150
151         if(NSGetResourceSecurity())
152         {
153             NS_LOG(DEBUG, "Create secured resource");
154             resourceProperties = OC_OBSERVABLE | OC_SECURE;
155         }
156         else
157         {
158             resourceProperties = OC_OBSERVABLE;
159         }
160
161         if (OCCreateResource(&(NotificationSyncResource.handle), NS_COLLECTION_SYNC_TYPE,
162                 NS_DEFAULT_INTERFACE, NS_COLLECTION_SYNC_URI, NSEntityHandlerSyncCb, NULL,
163                 resourceProperties) != OC_STACK_OK)
164         {
165             NS_LOG(NS_ERROR, "Fail to Create Notification Sync Resource");
166             return NS_ERROR;
167         }
168     }
169     else if (strcmp(uri, NS_COLLECTION_TOPIC_URI) == 0)
170     {
171         (NotificationTopicResource.providerId)[0] = '\0';
172         (NotificationTopicResource.consumerId)[0] = '\0';
173         NotificationTopicResource.TopicList = NULL;
174         NotificationTopicResource.handle = NULL;
175
176         if(NSGetResourceSecurity())
177         {
178             NS_LOG(DEBUG, "Create secured resource");
179             resourceProperties = OC_DISCOVERABLE | OC_SECURE;
180         }
181         else
182         {
183             resourceProperties = OC_DISCOVERABLE;
184         }
185
186         if (OCCreateResource(&(NotificationTopicResource.handle), NS_COLLECTION_TOPIC_TYPE,
187                 NS_DEFAULT_INTERFACE, NS_COLLECTION_TOPIC_URI, NSEntityHandlerTopicCb, NULL,
188                 resourceProperties) != OC_STACK_OK)
189         {
190             NS_LOG(NS_ERROR, "Fail to Create Notification Sync Resource");
191             return NS_ERROR;
192         }
193     }
194     else
195     {
196         NS_LOG(ERROR, "Fail to create resource with invalid URI");
197         return NS_ERROR;
198     }
199
200     NS_LOG(DEBUG, "NSCreateResource - OUT");
201     return NS_OK;
202 }
203
204 NSResult NSRegisterResource()
205 {
206     NS_LOG(DEBUG, "NSRegisterResource - IN");
207
208     NS_CREATE_RESOURCE(
209             NSCreateResource(NS_COLLECTION_TOPIC_URI), "Fail to register Topic Resource");
210     NS_CREATE_RESOURCE(
211             NSCreateResource(NS_COLLECTION_SYNC_URI), "Fail to register Sync Resource");
212     NS_CREATE_RESOURCE(
213             NSCreateResource(NS_COLLECTION_MESSAGE_URI), "Fail to register Message Resource");
214     NS_CREATE_RESOURCE(
215             NSCreateResource(NS_ROOT_URI), "Fail to register Notification Resource");
216
217     NS_LOG(DEBUG, "NSRegisterResource - OUT");
218     return NS_OK;
219 }
220
221 NSResult NSUnRegisterResource()
222 {
223     NS_LOG(DEBUG, "NSUnRegisterResource - IN");
224
225     NS_DELETE_RESOURCE(
226             OCDeleteResource(NotificationResource.handle), "Fail to Delete Notification Resource");
227     NS_DELETE_RESOURCE(OCDeleteResource(NotificationMessageResource.handle),
228             "Fail to Delete Notification Message Resource");
229     NS_DELETE_RESOURCE(OCDeleteResource(NotificationSyncResource.handle),
230             "Fail to Delete Notification Sync Resource");
231     NS_DELETE_RESOURCE(OCDeleteResource(NotificationTopicResource.handle),
232             "Fail to Delete Notification Topic Resource");
233
234     NotificationResource.handle = NULL;
235     NotificationMessageResource.handle = NULL;
236     NotificationSyncResource.handle = NULL;
237     NotificationTopicResource.handle = NULL;
238
239     NS_LOG(DEBUG, "NSUnRegisterResource - OUT");
240     return NS_OK;
241 }
242
243 NSResult NSPutNotificationResource(bool policy, OCResourceHandle * handle)
244 {
245     NS_LOG(DEBUG, "NSPutNotificationResource - IN");
246
247     NotificationResource.policy = policy;
248     OICStrcpy(NotificationResource.providerId, UUID_STRING_SIZE,
249         NSGetProviderInfo()->providerId);
250     NotificationResource.message_uri = NS_COLLECTION_MESSAGE_URI;
251     NotificationResource.sync_uri = NS_COLLECTION_SYNC_URI;
252     NotificationResource.topic_uri = NS_COLLECTION_TOPIC_URI;
253     NotificationResource.version = VERSION;
254
255     *handle = NotificationResource.handle;
256
257     NS_LOG(DEBUG, "NSPutNotificationResource - OUT");
258     return NS_OK;
259 }
260
261 NSResult NSPutMessageResource(NSMessage *msg, OCResourceHandle * handle)
262 {
263     NS_LOG(DEBUG, "NSPutMessageResource - IN");
264
265     if(msg != NULL)
266     {
267         NS_LOG(DEBUG, "NSMessage is valid");
268
269         NotificationMessageResource.messageId = msg->messageId;
270         OICStrcpy(NotificationMessageResource.providerId, UUID_STRING_SIZE, msg->providerId);
271         NotificationMessageResource.type = msg->type;
272         NotificationMessageResource.dateTime = msg->dateTime;
273         NotificationMessageResource.ttl = msg->ttl;
274         NotificationMessageResource.title = msg->title;
275         NotificationMessageResource.contentText = msg->contentText;
276         NotificationMessageResource.sourceName = msg->sourceName;
277         NotificationMessageResource.topicName = msg->topic;
278         NotificationMessageResource.mediaContents = msg->mediaContents;
279     }
280     else
281     {
282         NS_LOG(ERROR, "NSMessage is NULL");
283     }
284
285     *handle = NotificationMessageResource.handle;
286
287     NS_LOG(DEBUG, "NSPutMessageResource - OUT");
288     return NS_OK;
289 }
290
291 NSResult NSPutSyncResource(NSSyncInfo *sync, OCResourceHandle * handle)
292 {
293     NS_LOG(DEBUG, "NSPutSyncResource - IN");
294
295     (void) sync;
296
297     *handle = NotificationSyncResource.handle;
298
299     NS_LOG(DEBUG, "NSPutSyncResource - OUT");
300     return NS_OK;
301 }
302
303 NSResult NSPutTopicResource(NSTopicList *topicList, OCResourceHandle * handle)
304 {
305     NS_LOG(DEBUG, "NSPutTopicResource - IN");
306
307     (void) topicList;
308
309     *handle = NotificationTopicResource.handle;
310
311     NS_LOG(DEBUG, "NSPutTopicResource - OUT");
312     return NS_OK;
313 }