c4d2b58a93b8b0966c175c72e4d4d4393cdf3625
[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
50     NS_LOG(DEBUG, "NSPublishResourceToCloud - IN");
51     NS_LOG_V(INFO_PRIVATE, "Remote Server Address: %s", serverAddress);
52
53     OCCallbackData cbData;
54     cbData.cb = NSHandlePublishCb;
55     cbData.context = (void *)DEFAULT_CONTEXT_VALUE;
56     cbData.cd = NULL;
57
58     OCResourceHandle resourceHandles[1] = { NotificationResource.handle };
59     OCStackResult res = OCRDPublish(NULL, serverAddress, CT_ADAPTER_TCP, resourceHandles, 1,
60             &cbData, OC_LOW_QOS);
61
62     if (res != OC_STACK_OK)
63     {
64         NS_LOG_V(DEBUG, "Unable to publish resources to cloud: %d", res);
65     }
66
67     NS_LOG(DEBUG, "NSPublishResourceToCloud - OUT");
68     return NS_OK;
69 }
70 #endif
71
72 NSResult NSCreateResource(char *uri)
73 {
74     NS_LOG(DEBUG, "NSCreateResource - IN");
75
76     if (!uri)
77     {
78         NS_LOG(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_INTERFACE_BASELINE,
105                 NS_ROOT_URI, NSEntityHandlerNotificationCb, NULL,
106                 resourceProperties) != OC_STACK_OK)
107         {
108             NS_LOG(ERROR, "Fail to Create Notification Resource");
109             return NS_ERROR;
110         }
111
112         if (OCBindResourceInterfaceToResource(NotificationResource.handle, NS_INTERFACE_READ)
113             != OC_STACK_OK)
114         {
115             NS_LOG(ERROR, "Fail to bind Notification Resource Type");
116             return NS_ERROR;
117         }
118     }
119     else if (strcmp(uri, NS_COLLECTION_MESSAGE_URI) == 0)
120     {
121
122         NotificationMessageResource.messageId = 0;
123
124         (NotificationMessageResource.providerId)[0] = '\0';
125         NotificationMessageResource.type = 0;
126         NotificationMessageResource.dateTime = NULL;
127         NotificationMessageResource.ttl = 0;
128         NotificationMessageResource.title = NULL;
129         NotificationMessageResource.contentText = NULL;
130         NotificationMessageResource.sourceName = NULL;
131         NotificationMessageResource.topicName = NULL;
132         NotificationMessageResource.mediaContents = NULL;
133
134         if (NSGetResourceSecurity())
135         {
136             NS_LOG(DEBUG, "Create secured resource");
137             resourceProperties = OC_OBSERVABLE | OC_SECURE;
138         }
139         else
140         {
141             resourceProperties = OC_OBSERVABLE;
142         }
143
144         if (OCCreateResource(&NotificationMessageResource.handle, NS_COLLECTION_MESSAGE_TYPE,
145                 NS_INTERFACE_BASELINE, NS_COLLECTION_MESSAGE_URI, NSEntityHandlerMessageCb, NULL,
146                 resourceProperties) != OC_STACK_OK)
147         {
148             NS_LOG(ERROR, "Fail to Create Notification Message Resource");
149             return NS_ERROR;
150         }
151
152         if (OCBindResourceInterfaceToResource(NotificationMessageResource.handle, NS_INTERFACE_READ)
153             != OC_STACK_OK)
154         {
155             NS_LOG(ERROR, "Fail to bind Notification Message Resource Type");
156             return NS_ERROR;
157         }
158     }
159     else if (strcmp(uri, NS_COLLECTION_SYNC_URI) == 0)
160     {
161         NotificationSyncResource.messageId = 0;
162         (NotificationSyncResource.providerId)[0] = '\0';
163         NotificationSyncResource.state = NULL;
164         NotificationSyncResource.handle = NULL;
165
166         if (NSGetResourceSecurity())
167         {
168             NS_LOG(DEBUG, "Create secured resource");
169             resourceProperties = OC_OBSERVABLE | OC_SECURE;
170         }
171         else
172         {
173             resourceProperties = OC_OBSERVABLE;
174         }
175
176         if (OCCreateResource(&(NotificationSyncResource.handle), NS_COLLECTION_SYNC_TYPE,
177                 NS_INTERFACE_BASELINE, NS_COLLECTION_SYNC_URI, NSEntityHandlerSyncCb, NULL,
178                 resourceProperties) != OC_STACK_OK)
179         {
180             NS_LOG(ERROR, "Fail to Create Notification Sync Resource");
181             return NS_ERROR;
182         }
183
184         if (OCBindResourceInterfaceToResource(NotificationSyncResource.handle,
185                 NS_INTERFACE_READWRITE)
186             != OC_STACK_OK)
187         {
188             NS_LOG(ERROR, "Fail to bind Notification Sync Resource Type");
189             return NS_ERROR;
190         }
191     }
192     else if (strcmp(uri, NS_COLLECTION_TOPIC_URI) == 0)
193     {
194         (NotificationTopicResource.providerId)[0] = '\0';
195         (NotificationTopicResource.consumerId)[0] = '\0';
196         NotificationTopicResource.TopicList = NULL;
197         NotificationTopicResource.handle = NULL;
198
199         if (NSGetResourceSecurity())
200         {
201             NS_LOG(DEBUG, "Create secured resource");
202             resourceProperties = OC_RES_PROP_NONE | OC_SECURE;
203         }
204         else
205         {
206             resourceProperties = OC_RES_PROP_NONE;
207         }
208
209         if (OCCreateResource(&(NotificationTopicResource.handle), NS_COLLECTION_TOPIC_TYPE,
210                 NS_INTERFACE_BASELINE, NS_COLLECTION_TOPIC_URI, NSEntityHandlerTopicCb, NULL,
211                 resourceProperties) != OC_STACK_OK)
212         {
213             NS_LOG(ERROR, "Fail to Create Notification Sync Resource");
214             return NS_ERROR;
215         }
216
217         if (OCBindResourceInterfaceToResource(NotificationTopicResource.handle,
218                 NS_INTERFACE_READWRITE)
219             != OC_STACK_OK)
220         {
221             NS_LOG(ERROR, "Fail to bind Notification Topic Resource Type");
222             return NS_ERROR;
223         }
224     }
225     else
226     {
227         NS_LOG(ERROR, "Fail to create resource with invalid URI");
228         return NS_ERROR;
229     }
230
231     NS_LOG(DEBUG, "NSCreateResource - OUT");
232     return NS_OK;
233 }
234
235 NSResult NSRegisterResource()
236 {
237     NS_LOG(DEBUG, "NSRegisterResource - IN");
238
239     NS_CREATE_RESOURCE(
240             NSCreateResource(NS_COLLECTION_TOPIC_URI), "Fail to register Topic Resource");
241     NS_CREATE_RESOURCE(
242             NSCreateResource(NS_COLLECTION_SYNC_URI), "Fail to register Sync Resource");
243     NS_CREATE_RESOURCE(
244             NSCreateResource(NS_COLLECTION_MESSAGE_URI), "Fail to register Message Resource");
245     NS_CREATE_RESOURCE(
246             NSCreateResource(NS_ROOT_URI), "Fail to register Notification Resource");
247
248     NS_LOG(DEBUG, "NSRegisterResource - OUT");
249     return NS_OK;
250 }
251
252 NSResult NSUnRegisterResource()
253 {
254     NS_LOG(DEBUG, "NSUnRegisterResource - IN");
255
256     NS_DELETE_RESOURCE(
257             OCDeleteResource(NotificationResource.handle), "Fail to Delete Notification Resource");
258     NS_DELETE_RESOURCE(OCDeleteResource(NotificationMessageResource.handle),
259             "Fail to Delete Notification Message Resource");
260     NS_DELETE_RESOURCE(OCDeleteResource(NotificationSyncResource.handle),
261             "Fail to Delete Notification Sync Resource");
262     NS_DELETE_RESOURCE(OCDeleteResource(NotificationTopicResource.handle),
263             "Fail to Delete Notification Topic Resource");
264
265     NotificationResource.handle = NULL;
266     NotificationMessageResource.handle = NULL;
267     NotificationSyncResource.handle = NULL;
268     NotificationTopicResource.handle = NULL;
269
270     NS_LOG(DEBUG, "NSUnRegisterResource - OUT");
271     return NS_OK;
272 }
273
274 NSResult NSPutNotificationResource(bool policy, OCResourceHandle * handle)
275 {
276     NS_LOG(DEBUG, "NSPutNotificationResource - IN");
277
278     NotificationResource.policy = policy;
279     OICStrcpy(NotificationResource.providerId, UUID_STRING_SIZE,
280         NSGetProviderInfo()->providerId);
281     NotificationResource.message_uri = NS_COLLECTION_MESSAGE_URI;
282     NotificationResource.sync_uri = NS_COLLECTION_SYNC_URI;
283     NotificationResource.topic_uri = NS_COLLECTION_TOPIC_URI;
284     NotificationResource.version = VERSION;
285
286     *handle = NotificationResource.handle;
287
288     NS_LOG(DEBUG, "NSPutNotificationResource - OUT");
289     return NS_OK;
290 }
291
292 NSResult NSPutMessageResource(NSMessage *msg, OCResourceHandle * handle)
293 {
294     NS_LOG(DEBUG, "NSPutMessageResource - IN");
295
296     if(msg != NULL)
297     {
298         NS_LOG(DEBUG, "NSMessage is valid");
299
300         NotificationMessageResource.messageId = msg->messageId;
301         OICStrcpy(NotificationMessageResource.providerId, UUID_STRING_SIZE, msg->providerId);
302         NotificationMessageResource.type = msg->type;
303         NotificationMessageResource.dateTime = msg->dateTime;
304         NotificationMessageResource.ttl = msg->ttl;
305         NotificationMessageResource.title = msg->title;
306         NotificationMessageResource.contentText = msg->contentText;
307         NotificationMessageResource.sourceName = msg->sourceName;
308         NotificationMessageResource.topicName = msg->topic;
309         NotificationMessageResource.mediaContents = msg->mediaContents;
310     }
311     else
312     {
313         NS_LOG(ERROR, "NSMessage is NULL");
314     }
315
316     *handle = NotificationMessageResource.handle;
317
318     NS_LOG(DEBUG, "NSPutMessageResource - OUT");
319     return NS_OK;
320 }
321
322 NSResult NSPutSyncResource(NSSyncInfo *sync, OCResourceHandle * handle)
323 {
324     NS_LOG(DEBUG, "NSPutSyncResource - IN");
325
326     (void) sync;
327
328     *handle = NotificationSyncResource.handle;
329
330     NS_LOG(DEBUG, "NSPutSyncResource - OUT");
331     return NS_OK;
332 }
333
334 NSResult NSPutTopicResource(NSTopicList *topicList, OCResourceHandle * handle)
335 {
336     NS_LOG(DEBUG, "NSPutTopicResource - IN");
337
338     (void) topicList;
339
340     *handle = NotificationTopicResource.handle;
341
342     NS_LOG(DEBUG, "NSPutTopicResource - OUT");
343     return NS_OK;
344 }