Update resource protocol specification
[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 #if(defined WITH_CLOUD && defined 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.policy = true;
84         (NotificationResource.providerId)[0] = '\0';
85         NotificationResource.message_uri = NS_COLLECTION_MESSAGE_URI;
86         NotificationResource.sync_uri = NS_COLLECTION_SYNC_URI;
87         NotificationResource.topic_uri = NS_COLLECTION_TOPIC_URI;
88         NotificationResource.version = VERSION;
89         NotificationResource.handle = NULL;
90
91         if (OCCreateResource(&NotificationResource.handle, NS_ROOT_TYPE, NS_DEFAULT_INTERFACE,
92                 NS_ROOT_URI, NSEntityHandlerNotificationCb, NULL, OC_DISCOVERABLE) != OC_STACK_OK)
93         {
94             NS_LOG(NS_ERROR, "Fail to Create Notification Resource");
95             return NS_ERROR;
96         }
97     }
98     else if (strcmp(uri, NS_COLLECTION_MESSAGE_URI) == 0)
99     {
100
101         NotificationMessageResource.messageId = 0;
102
103         (NotificationMessageResource.providerId)[0] = '\0';
104         NotificationMessageResource.type = 0;
105         NotificationMessageResource.dateTime = NULL;
106         NotificationMessageResource.ttl = 0;
107         NotificationMessageResource.title = NULL;
108         NotificationMessageResource.contentText = NULL;
109         NotificationMessageResource.sourceName = NULL;
110         NotificationMessageResource.topicName = NULL;
111         NotificationMessageResource.mediaContents = NULL;
112
113         if (OCCreateResource(&NotificationMessageResource.handle, NS_COLLECTION_MESSAGE_TYPE,
114                 NS_DEFAULT_INTERFACE, NS_COLLECTION_MESSAGE_URI, NSEntityHandlerMessageCb, NULL,
115                 OC_OBSERVABLE) != OC_STACK_OK)
116         {
117             NS_LOG(NS_ERROR, "Fail to Create Notification Message Resource");
118             return NS_ERROR;
119         }
120     }
121     else if (strcmp(uri, NS_COLLECTION_SYNC_URI) == 0)
122     {
123         NotificationSyncResource.id = NULL;
124         (NotificationSyncResource.providerId)[0] = '\0';
125         NotificationSyncResource.state = NULL;
126         NotificationSyncResource.handle = NULL;
127
128         if (OCCreateResource(&(NotificationSyncResource.handle), NS_COLLECTION_SYNC_TYPE,
129                 NS_DEFAULT_INTERFACE, NS_COLLECTION_SYNC_URI, NSEntityHandlerSyncCb, NULL,
130                 OC_OBSERVABLE) != OC_STACK_OK)
131         {
132             NS_LOG(NS_ERROR, "Fail to Create Notification Sync Resource");
133             return NS_ERROR;
134         }
135     }
136     else if (strcmp(uri, NS_COLLECTION_TOPIC_URI) == 0)
137     {
138         (NotificationTopicResource.providerId)[0] = '\0';
139         (NotificationTopicResource.consumerId)[0] = '\0';
140         NotificationTopicResource.TopicList = NULL;
141         NotificationTopicResource.handle = NULL;
142
143         if (OCCreateResource(&(NotificationTopicResource.handle), NS_COLLECTION_TOPIC_TYPE,
144                 NS_DEFAULT_INTERFACE, NS_COLLECTION_TOPIC_URI, NSEntityHandlerTopicCb, NULL,
145                 OC_DISCOVERABLE) != OC_STACK_OK)
146         {
147             NS_LOG(NS_ERROR, "Fail to Create Notification Sync Resource");
148             return NS_ERROR;
149         }
150     }
151     else
152     {
153         NS_LOG(ERROR, "Fail to create resource with invalid URI");
154         return NS_ERROR;
155     }
156
157     NS_LOG(DEBUG, "NSCreateResource - OUT");
158     return NS_OK;
159 }
160
161 NSResult NSRegisterResource()
162 {
163     NS_LOG(DEBUG, "NSRegisterResource - IN");
164
165     if (NSCreateResource(NS_COLLECTION_TOPIC_URI) != NS_OK)
166     {
167         NS_LOG(ERROR, "Fail to register Topic Resource");
168         return NS_ERROR;
169     }
170
171     if (NSCreateResource(NS_COLLECTION_SYNC_URI) != NS_OK)
172     {
173         NS_LOG(ERROR, "Fail to register Sync Resource");
174         return NS_ERROR;
175     }
176
177     if (NSCreateResource(NS_COLLECTION_MESSAGE_URI) != NS_OK)
178     {
179         NS_LOG(ERROR, "Fail to register Message Resource");
180         return NS_ERROR;
181     }
182
183     if (NSCreateResource(NS_ROOT_URI) != NS_OK)
184     {
185         NS_LOG(ERROR, "Fail to register Notification Resource");
186         return NS_ERROR;
187     }
188
189     NS_LOG(DEBUG, "NSRegisterResource - OUT");
190     return NS_OK;
191 }
192
193 NSResult NSUnRegisterResource()
194 {
195     NS_LOG(DEBUG, "NSUnRegisterResource - IN");
196
197     if (OCDeleteResource(NotificationResource.handle) != OC_STACK_OK)
198     {
199         NS_LOG(ERROR, "Fail to Delete Notification Resource");
200         return NS_ERROR;
201     }
202
203     if (OCDeleteResource(NotificationMessageResource.handle) != OC_STACK_OK)
204     {
205         NS_LOG(ERROR, "Fail to Delete Notification Message Resource");
206         return NS_ERROR;
207     }
208
209     if (OCDeleteResource(NotificationSyncResource.handle) != OC_STACK_OK)
210     {
211         NS_LOG(ERROR, "Fail to Delete Notification Sync Resource");
212         return NS_ERROR;
213     }
214
215     if (OCDeleteResource(NotificationTopicResource.handle) != OC_STACK_OK)
216     {
217         NS_LOG(ERROR, "Fail to Delete Notification Topic Resource");
218         return NS_ERROR;
219     }
220
221     NotificationResource.handle = NULL;
222     NotificationMessageResource.handle = NULL;
223     NotificationSyncResource.handle = NULL;
224     NotificationTopicResource.handle = NULL;
225
226     NS_LOG(DEBUG, "NSUnRegisterResource - OUT");
227     return NS_OK;
228 }
229
230 NSResult NSPutNotificationResource(bool policy, OCResourceHandle * handle)
231 {
232     NS_LOG(DEBUG, "NSPutNotificationResource - IN");
233
234     NotificationResource.policy = policy;
235     OICStrcpy(NotificationResource.providerId, UUID_STRING_SIZE,
236         NSGetProviderInfo()->providerId);
237     NotificationResource.message_uri = NS_COLLECTION_MESSAGE_URI;
238     NotificationResource.sync_uri = NS_COLLECTION_SYNC_URI;
239     NotificationResource.topic_uri = NS_COLLECTION_TOPIC_URI;
240     NotificationResource.version = VERSION;
241
242     *handle = NotificationResource.handle;
243
244     NS_LOG(DEBUG, "NSPutNotificationResource - OUT");
245     return NS_OK;
246 }
247
248 NSResult NSPutMessageResource(NSMessage *msg, OCResourceHandle * handle)
249 {
250     NS_LOG(DEBUG, "NSPutMessageResource - IN");
251
252     if(msg != NULL)
253     {
254         NS_LOG(DEBUG, "NSMessage is valid");
255
256         NotificationMessageResource.messageId = msg->messageId;
257         OICStrcpy(NotificationMessageResource.providerId, UUID_STRING_SIZE, msg->providerId);
258         NotificationMessageResource.type = msg->type;
259         NotificationMessageResource.dateTime = msg->dateTime;
260         NotificationMessageResource.ttl = msg->ttl;
261         NotificationMessageResource.title = msg->title;
262         NotificationMessageResource.contentText = msg->contentText;
263         NotificationMessageResource.sourceName = msg->sourceName;
264         NotificationMessageResource.topicName = msg->topic;
265         NotificationMessageResource.mediaContents = msg->mediaContents;
266     }
267     else
268     {
269         NS_LOG(ERROR, "NSMessage is NULL");
270     }
271
272     *handle = NotificationMessageResource.handle;
273
274     NS_LOG(DEBUG, "NSPutMessageResource - OUT");
275     return NS_OK;
276 }
277
278 NSResult NSPutSyncResource(NSSyncInfo *sync, OCResourceHandle * handle)
279 {
280     NS_LOG(DEBUG, "NSPutSyncResource - IN");
281
282     (void) sync;
283
284     *handle = NotificationSyncResource.handle;
285
286     NS_LOG(DEBUG, "NSPutSyncResource - OUT");
287     return NS_OK;
288 }
289
290 NSResult NSPutTopicResource(NSTopicList *topicList, OCResourceHandle * handle)
291 {
292     NS_LOG(DEBUG, "NSPutTopicResource - IN");
293
294     (void) topicList;
295
296     *handle = NotificationTopicResource.handle;
297
298     NS_LOG(DEBUG, "NSPutTopicResource - OUT");
299     return NS_OK;
300 }