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