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