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