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