Merge "Merge remote-tracking branch 'origin/notification-service' Updated with static...
[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     NS_CREATE_RESOURCE(
166             NSCreateResource(NS_COLLECTION_TOPIC_URI), "Fail to register Topic Resource");
167     NS_CREATE_RESOURCE(
168             NSCreateResource(NS_COLLECTION_SYNC_URI), "Fail to register Sync Resource");
169     NS_CREATE_RESOURCE(
170             NSCreateResource(NS_COLLECTION_MESSAGE_URI), "Fail to register Message Resource");
171     NS_CREATE_RESOURCE(
172             NSCreateResource(NS_ROOT_URI), "Fail to register Notification Resource");
173
174     NS_LOG(DEBUG, "NSRegisterResource - OUT");
175     return NS_OK;
176 }
177
178 NSResult NSUnRegisterResource()
179 {
180     NS_LOG(DEBUG, "NSUnRegisterResource - IN");
181
182     NS_DELETE_RESOURCE(
183             OCDeleteResource(NotificationResource.handle), "Fail to Delete Notification Resource");
184     NS_DELETE_RESOURCE(OCDeleteResource(NotificationMessageResource.handle),
185             "Fail to Delete Notification Message Resource");
186     NS_DELETE_RESOURCE(OCDeleteResource(NotificationSyncResource.handle),
187             "Fail to Delete Notification Sync Resource");
188     NS_DELETE_RESOURCE(OCDeleteResource(NotificationTopicResource.handle),
189             "Fail to Delete Notification Topic Resource");
190
191     NotificationResource.handle = NULL;
192     NotificationMessageResource.handle = NULL;
193     NotificationSyncResource.handle = NULL;
194     NotificationTopicResource.handle = NULL;
195
196     NS_LOG(DEBUG, "NSUnRegisterResource - OUT");
197     return NS_OK;
198 }
199
200 NSResult NSPutNotificationResource(bool policy, OCResourceHandle * handle)
201 {
202     NS_LOG(DEBUG, "NSPutNotificationResource - IN");
203
204     NotificationResource.policy = policy;
205     OICStrcpy(NotificationResource.providerId, UUID_STRING_SIZE,
206         NSGetProviderInfo()->providerId);
207     NotificationResource.message_uri = NS_COLLECTION_MESSAGE_URI;
208     NotificationResource.sync_uri = NS_COLLECTION_SYNC_URI;
209     NotificationResource.topic_uri = NS_COLLECTION_TOPIC_URI;
210     NotificationResource.version = VERSION;
211
212     *handle = NotificationResource.handle;
213
214     NS_LOG(DEBUG, "NSPutNotificationResource - OUT");
215     return NS_OK;
216 }
217
218 NSResult NSPutMessageResource(NSMessage *msg, OCResourceHandle * handle)
219 {
220     NS_LOG(DEBUG, "NSPutMessageResource - IN");
221
222     if(msg != NULL)
223     {
224         NS_LOG(DEBUG, "NSMessage is valid");
225
226         NotificationMessageResource.messageId = msg->messageId;
227         OICStrcpy(NotificationMessageResource.providerId, UUID_STRING_SIZE, msg->providerId);
228         NotificationMessageResource.type = msg->type;
229         NotificationMessageResource.dateTime = msg->dateTime;
230         NotificationMessageResource.ttl = msg->ttl;
231         NotificationMessageResource.title = msg->title;
232         NotificationMessageResource.contentText = msg->contentText;
233         NotificationMessageResource.sourceName = msg->sourceName;
234         NotificationMessageResource.topicName = msg->topic;
235         NotificationMessageResource.mediaContents = msg->mediaContents;
236     }
237     else
238     {
239         NS_LOG(ERROR, "NSMessage is NULL");
240     }
241
242     *handle = NotificationMessageResource.handle;
243
244     NS_LOG(DEBUG, "NSPutMessageResource - OUT");
245     return NS_OK;
246 }
247
248 NSResult NSPutSyncResource(NSSyncInfo *sync, OCResourceHandle * handle)
249 {
250     NS_LOG(DEBUG, "NSPutSyncResource - IN");
251
252     (void) sync;
253
254     *handle = NotificationSyncResource.handle;
255
256     NS_LOG(DEBUG, "NSPutSyncResource - OUT");
257     return NS_OK;
258 }
259
260 NSResult NSPutTopicResource(NSTopicList *topicList, OCResourceHandle * handle)
261 {
262     NS_LOG(DEBUG, "NSPutTopicResource - IN");
263
264     (void) topicList;
265
266     *handle = NotificationTopicResource.handle;
267
268     NS_LOG(DEBUG, "NSPutTopicResource - OUT");
269     return NS_OK;
270 }