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