Update snapshot(2017-12-14)
[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 #include "rd_client.h"
24
25 NSNotificationResource NotificationResource;
26 NSMessageResource NotificationMessageResource;
27 NSSyncResource NotificationSyncResource;
28 NSTopicResource NotificationTopicResource;
29
30 #if (defined WITH_CLOUD)
31 #define DEFAULT_CONTEXT_VALUE 0x99
32
33 OCStackApplicationResult NSHandlePublishCb(void *ctx, OCDoHandle handle,
34     OCClientResponse *clientResponse)
35 {
36     (void) handle;
37     if (ctx != (void *)DEFAULT_CONTEXT_VALUE)
38     {
39         NS_LOG(DEBUG, "Invalid Publish callback received");
40     }
41
42     NS_LOG_V(DEBUG, "Publish resource response received code: %d", clientResponse->result);
43
44     return OC_STACK_KEEP_TRANSACTION;
45 }
46
47 NSResult NSPublishResourceToCloud(char *serverAddress)
48 {
49     NS_LOG(DEBUG, "NSPublishResourceToCloud - IN");
50     NS_LOG_V(INFO_PRIVATE, "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(NULL, 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 NSResult NSCreateResource(char *uri)
72 {
73     NS_LOG(DEBUG, "NSCreateResource - IN");
74
75     if (!uri)
76     {
77         NS_LOG(ERROR, "Resource URI cannot be NULL");
78         return NS_ERROR;
79     }
80
81     uint8_t resourceProperties;
82
83     if (strcmp(uri, NS_ROOT_URI) == 0)
84     {
85         NotificationResource.policy = true;
86         (NotificationResource.providerId)[0] = '\0';
87         NotificationResource.message_uri = NS_COLLECTION_MESSAGE_URI;
88         NotificationResource.sync_uri = NS_COLLECTION_SYNC_URI;
89         NotificationResource.topic_uri = NS_COLLECTION_TOPIC_URI;
90         NotificationResource.version = VERSION;
91         NotificationResource.handle = NULL;
92
93         if (NSGetResourceSecurity())
94         {
95             NS_LOG(DEBUG, "Create secured resource");
96             resourceProperties = OC_DISCOVERABLE | OC_SECURE;
97         }
98         else
99         {
100             resourceProperties = OC_DISCOVERABLE;
101         }
102
103         if (OCCreateResource(&NotificationResource.handle, NS_ROOT_TYPE, NS_INTERFACE_BASELINE,
104                 NS_ROOT_URI, NSEntityHandlerNotificationCb, NULL,
105                 resourceProperties) != OC_STACK_OK)
106         {
107             NS_LOG(ERROR, "Fail to Create Notification Resource");
108             return NS_ERROR;
109         }
110
111         if (OCBindResourceInterfaceToResource(NotificationResource.handle, NS_INTERFACE_READ)
112             != OC_STACK_OK)
113         {
114             NS_LOG(ERROR, "Fail to bind Notification Resource Type");
115             return NS_ERROR;
116         }
117     }
118     else if (strcmp(uri, NS_COLLECTION_MESSAGE_URI) == 0)
119     {
120
121         NotificationMessageResource.messageId = 0;
122
123         (NotificationMessageResource.providerId)[0] = '\0';
124         NotificationMessageResource.type = 0;
125         NotificationMessageResource.dateTime = NULL;
126         NotificationMessageResource.ttl = 0;
127         NotificationMessageResource.title = NULL;
128         NotificationMessageResource.contentText = NULL;
129         NotificationMessageResource.sourceName = NULL;
130         NotificationMessageResource.topicName = NULL;
131         NotificationMessageResource.mediaContents = NULL;
132
133         if (NSGetResourceSecurity())
134         {
135             NS_LOG(DEBUG, "Create secured resource");
136             resourceProperties = OC_OBSERVABLE | OC_SECURE;
137         }
138         else
139         {
140             resourceProperties = OC_OBSERVABLE;
141         }
142
143         if (OCCreateResource(&NotificationMessageResource.handle, NS_COLLECTION_MESSAGE_TYPE,
144                 NS_INTERFACE_BASELINE, NS_COLLECTION_MESSAGE_URI, NSEntityHandlerMessageCb, NULL,
145                 resourceProperties) != OC_STACK_OK)
146         {
147             NS_LOG(ERROR, "Fail to Create Notification Message Resource");
148             return NS_ERROR;
149         }
150
151         if (OCBindResourceInterfaceToResource(NotificationMessageResource.handle, NS_INTERFACE_READ)
152             != OC_STACK_OK)
153         {
154             NS_LOG(ERROR, "Fail to bind Notification Message Resource Type");
155             return NS_ERROR;
156         }
157     }
158     else if (strcmp(uri, NS_COLLECTION_SYNC_URI) == 0)
159     {
160         NotificationSyncResource.messageId = 0;
161         (NotificationSyncResource.providerId)[0] = '\0';
162         NotificationSyncResource.state = NULL;
163         NotificationSyncResource.handle = NULL;
164
165         if (NSGetResourceSecurity())
166         {
167             NS_LOG(DEBUG, "Create secured resource");
168             resourceProperties = OC_OBSERVABLE | OC_SECURE;
169         }
170         else
171         {
172             resourceProperties = OC_OBSERVABLE;
173         }
174
175         if (OCCreateResource(&(NotificationSyncResource.handle), NS_COLLECTION_SYNC_TYPE,
176                 NS_INTERFACE_BASELINE, NS_COLLECTION_SYNC_URI, NSEntityHandlerSyncCb, NULL,
177                 resourceProperties) != OC_STACK_OK)
178         {
179             NS_LOG(ERROR, "Fail to Create Notification Sync Resource");
180             return NS_ERROR;
181         }
182
183         if (OCBindResourceInterfaceToResource(NotificationSyncResource.handle,
184                 NS_INTERFACE_READWRITE)
185             != OC_STACK_OK)
186         {
187             NS_LOG(ERROR, "Fail to bind Notification Sync Resource Type");
188             return NS_ERROR;
189         }
190     }
191     else if (strcmp(uri, NS_COLLECTION_TOPIC_URI) == 0)
192     {
193         (NotificationTopicResource.providerId)[0] = '\0';
194         (NotificationTopicResource.consumerId)[0] = '\0';
195         NotificationTopicResource.TopicList = NULL;
196         NotificationTopicResource.handle = NULL;
197
198         if (NSGetResourceSecurity())
199         {
200             NS_LOG(DEBUG, "Create secured resource");
201             resourceProperties = OC_RES_PROP_NONE | OC_SECURE;
202         }
203         else
204         {
205             resourceProperties = OC_RES_PROP_NONE;
206         }
207
208         if (OCCreateResource(&(NotificationTopicResource.handle), NS_COLLECTION_TOPIC_TYPE,
209                 NS_INTERFACE_BASELINE, NS_COLLECTION_TOPIC_URI, NSEntityHandlerTopicCb, NULL,
210                 resourceProperties) != OC_STACK_OK)
211         {
212             NS_LOG(ERROR, "Fail to Create Notification Sync Resource");
213             return NS_ERROR;
214         }
215
216         if (OCBindResourceInterfaceToResource(NotificationTopicResource.handle,
217                 NS_INTERFACE_READWRITE)
218             != OC_STACK_OK)
219         {
220             NS_LOG(ERROR, "Fail to bind Notification Topic Resource Type");
221             return NS_ERROR;
222         }
223     }
224     else
225     {
226         NS_LOG(ERROR, "Fail to create resource with invalid URI");
227         return NS_ERROR;
228     }
229
230     NS_LOG(DEBUG, "NSCreateResource - OUT");
231     return NS_OK;
232 }
233
234 NSResult NSRegisterResource()
235 {
236     NS_LOG(DEBUG, "NSRegisterResource - IN");
237
238     NS_CREATE_RESOURCE(
239             NSCreateResource(NS_COLLECTION_TOPIC_URI), "Fail to register Topic Resource");
240     NS_CREATE_RESOURCE(
241             NSCreateResource(NS_COLLECTION_SYNC_URI), "Fail to register Sync Resource");
242     NS_CREATE_RESOURCE(
243             NSCreateResource(NS_COLLECTION_MESSAGE_URI), "Fail to register Message Resource");
244     NS_CREATE_RESOURCE(
245             NSCreateResource(NS_ROOT_URI), "Fail to register Notification Resource");
246
247     NS_LOG(DEBUG, "NSRegisterResource - OUT");
248     return NS_OK;
249 }
250
251 NSResult NSUnRegisterResource()
252 {
253     NS_LOG(DEBUG, "NSUnRegisterResource - IN");
254
255     NS_DELETE_RESOURCE(
256             OCDeleteResource(NotificationResource.handle), "Fail to Delete Notification Resource");
257     NS_DELETE_RESOURCE(OCDeleteResource(NotificationMessageResource.handle),
258             "Fail to Delete Notification Message Resource");
259     NS_DELETE_RESOURCE(OCDeleteResource(NotificationSyncResource.handle),
260             "Fail to Delete Notification Sync Resource");
261     NS_DELETE_RESOURCE(OCDeleteResource(NotificationTopicResource.handle),
262             "Fail to Delete Notification Topic Resource");
263
264     NotificationResource.handle = NULL;
265     NotificationMessageResource.handle = NULL;
266     NotificationSyncResource.handle = NULL;
267     NotificationTopicResource.handle = NULL;
268
269     NS_LOG(DEBUG, "NSUnRegisterResource - OUT");
270     return NS_OK;
271 }
272
273 NSResult NSPutNotificationResource(bool policy, OCResourceHandle * handle)
274 {
275     NS_LOG(DEBUG, "NSPutNotificationResource - IN");
276
277     NotificationResource.policy = policy;
278     OICStrcpy(NotificationResource.providerId, UUID_STRING_SIZE,
279         NSGetProviderInfo()->providerId);
280     NotificationResource.message_uri = NS_COLLECTION_MESSAGE_URI;
281     NotificationResource.sync_uri = NS_COLLECTION_SYNC_URI;
282     NotificationResource.topic_uri = NS_COLLECTION_TOPIC_URI;
283     NotificationResource.version = VERSION;
284
285     *handle = NotificationResource.handle;
286
287     NS_LOG(DEBUG, "NSPutNotificationResource - OUT");
288     return NS_OK;
289 }
290
291 NSResult NSPutMessageResource(NSMessage *msg, OCResourceHandle * handle)
292 {
293     NS_LOG(DEBUG, "NSPutMessageResource - IN");
294
295     if(msg != NULL)
296     {
297         NS_LOG(DEBUG, "NSMessage is valid");
298
299         NotificationMessageResource.messageId = msg->messageId;
300         OICStrcpy(NotificationMessageResource.providerId, UUID_STRING_SIZE, msg->providerId);
301         NotificationMessageResource.type = msg->type;
302         NotificationMessageResource.dateTime = msg->dateTime;
303         NotificationMessageResource.ttl = msg->ttl;
304         NotificationMessageResource.title = msg->title;
305         NotificationMessageResource.contentText = msg->contentText;
306         NotificationMessageResource.sourceName = msg->sourceName;
307         NotificationMessageResource.topicName = msg->topic;
308         NotificationMessageResource.mediaContents = msg->mediaContents;
309     }
310     else
311     {
312         NS_LOG(ERROR, "NSMessage is NULL");
313     }
314
315     *handle = NotificationMessageResource.handle;
316
317     NS_LOG(DEBUG, "NSPutMessageResource - OUT");
318     return NS_OK;
319 }
320
321 NSResult NSPutSyncResource(NSSyncInfo *sync, OCResourceHandle * handle)
322 {
323     NS_LOG(DEBUG, "NSPutSyncResource - IN");
324
325     (void) sync;
326
327     *handle = NotificationSyncResource.handle;
328
329     NS_LOG(DEBUG, "NSPutSyncResource - OUT");
330     return NS_OK;
331 }
332
333 NSResult NSPutTopicResource(NSTopicList *topicList, OCResourceHandle * handle)
334 {
335     NS_LOG(DEBUG, "NSPutTopicResource - IN");
336
337     (void) topicList;
338
339     *handle = NotificationTopicResource.handle;
340
341     NS_LOG(DEBUG, "NSPutTopicResource - OUT");
342     return NS_OK;
343 }