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