implementation NSStopProvider and bug fixed.
[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 char* NSType = "oic.r.notification";
24 char* NSMessageType = "oic.r.notification.message";
25 char* NSSyncType = "oic.r.notification.sync";
26
27 char* NSInterface = "oic.if.baseline";
28 char* NSMessgeInterface = "oic.if.baseline.message";
29 char* NSSyncInterface = "oic.if.baseline.sync";
30
31 char* NSUri = "/notification";
32 char* NSMessageUri = "/notification/message";
33 char* NSSyncUri = "/notification/sync";
34
35 /* Structure to represent notification resources */
36 typedef struct
37 {
38     OCResourceHandle handle;
39     int accepter;
40     char* message_uri;
41     char* sync_uri;
42 } NSNotificationResource;
43
44 typedef struct
45 {
46     OCResourceHandle handle;
47     char* id;
48     char* title;
49     char* body;
50 } NSMessageResource;
51
52 typedef struct
53 {
54     OCResourceHandle handle;
55     char* id;
56     char* state;
57 } NSSyncResource;
58
59 NSNotificationResource NotificationResource;
60 NSMessageResource NotificationMessageResource;
61 NSSyncResource NotificationSyncResource;
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, NSUri) == 0)
73     {
74
75         NotificationResource.accepter = 0;
76         NotificationResource.message_uri = NSMessageUri;
77         NotificationResource.sync_uri = NSSyncUri;
78         NotificationResource.handle = NULL;
79
80         if (OCCreateResource(&NotificationResource.handle, NSType, NSInterface, NSUri,
81                 NSEntityHandlerNotificationCb, NULL, OC_DISCOVERABLE) != OC_STACK_OK)
82         {
83             NS_LOG(NS_ERROR, "Fail to Create Notification Resource");
84             return NS_ERROR;
85         }
86     }
87     else if (strcmp(uri, NSMessageUri) == 0)
88     {
89
90         NotificationMessageResource.id = NULL;
91         NotificationMessageResource.title = NULL;
92         NotificationMessageResource.body = NULL;
93         NotificationMessageResource.handle = NULL;
94
95         if (OCCreateResource(&NotificationMessageResource.handle, NSMessageType, NSInterface,
96                 NSMessageUri, NSEntityHandlerMessageCb, NULL, OC_OBSERVABLE) != OC_STACK_OK)
97         {
98             NS_LOG(NS_ERROR, "Fail to Create Notification Message Resource");
99             return NS_ERROR;
100         }
101     }
102     else if (strcmp(uri, NSSyncUri) == 0)
103     {
104         NotificationSyncResource.id = NULL;
105         NotificationSyncResource.state = NULL;
106         NotificationSyncResource.handle = NULL;
107
108         if (OCCreateResource(&(NotificationSyncResource.handle), NSSyncType, NSInterface, NSSyncUri,
109                 NSEntityHandlerSyncCb, NULL, OC_OBSERVABLE) != OC_STACK_OK)
110         {
111             NS_LOG(NS_ERROR, "Fail to Create Notification Sync Resource");
112             return NS_ERROR;
113         }
114     }
115     else
116     {
117         NS_LOG(ERROR, "Fail to create resource with invalid URI");
118         return NS_ERROR;
119     }
120
121     NS_LOG(DEBUG, "NSCreateResource - OUT");
122     return NS_OK;
123 }
124
125 NSResult NSRegisterResource()
126 {
127     NS_LOG(DEBUG, "NSRegisterResource - IN");
128
129     if (NSCreateResource(NSSyncUri) != NS_OK)
130     {
131         NS_LOG(ERROR, "Fail to register Sync Resource");
132         return NS_ERROR;
133     }
134
135     if (NSCreateResource(NSMessageUri) != NS_OK)
136     {
137         NS_LOG(ERROR, "Fail to register Message Resource");
138         return NS_ERROR;
139     }
140
141     if (NSCreateResource(NSUri) != NS_OK)
142     {
143         NS_LOG(ERROR, "Fail to register Notification Resource");
144         return NS_ERROR;
145     }
146
147     NS_LOG(DEBUG, "NSRegisterResource - OUT");
148     return NS_OK;
149 }
150
151 NSResult NSUnRegisterResource()
152 {
153     NS_LOG(DEBUG, "NSUnRegisterResource - IN");
154
155     if (OCDeleteResource(NotificationResource.handle) != OC_STACK_OK)
156     {
157         NS_LOG(ERROR, "Fail to Delete Notification Resource");
158         return NS_ERROR;
159     }
160
161     if (OCDeleteResource(NotificationMessageResource.handle) != OC_STACK_OK)
162     {
163         NS_LOG(ERROR, "Fail to Delete Notification Message Resource");
164         return NS_ERROR;
165     }
166
167     if (OCDeleteResource(NotificationSyncResource.handle) != OC_STACK_OK)
168     {
169         NS_LOG(ERROR, "Fail to Delete Notification Sync Resource");
170         return NS_ERROR;
171     }
172
173     NS_LOG(DEBUG, "NSUnRegisterResource - OUT");
174     return NS_OK;
175 }
176
177 NSResult NSPutNotificationResource(int accepter, OCResourceHandle * handle)
178 {
179     NS_LOG(DEBUG, "NSPutNotificationResource - IN");
180
181     NotificationResource.accepter = accepter;
182     NotificationResource.message_uri = NSMessageUri;
183     NotificationResource.sync_uri = NSSyncUri;
184
185     *handle = NotificationResource.handle;
186
187     NS_LOG(DEBUG, "NSPutNotificationResource - OUT");
188     return NS_OK;
189 }
190
191 NSResult NSPutMessageResource(NSMessage *msg, OCResourceHandle * handle)
192 {
193     NS_LOG(DEBUG, "NSPutMessageResource - IN");
194
195     if(msg != NULL)
196     {
197         NS_LOG(DEBUG, "NSMessage is valid");
198
199         NotificationMessageResource.id = OICStrdup(msg->mId);
200         NotificationMessageResource.title = OICStrdup(msg->mTitle);
201         NotificationMessageResource.body = OICStrdup(msg->mContentText);
202     }
203     else
204     {
205         NS_LOG(ERROR, "NSMessage is NULL");
206     }
207
208     *handle = NotificationMessageResource.handle;
209     NS_LOG(DEBUG, "NSPutMessageResource - OUT");
210
211     return NS_OK;
212 }
213
214 NSResult NSPutSyncResource(NSSync *sync, OCResourceHandle * handle)
215 {
216     NS_LOG(DEBUG, "NSPutSyncResource - IN");
217
218     (void) sync;
219
220     *handle = NotificationSyncResource.handle;
221
222     NS_LOG(DEBUG, "NSPutSyncResource - OUT");
223     return NS_OK;
224 }
225
226 const char* NSGetNotificationUri()
227 {
228     return NSUri;
229 }
230
231 const char* NSGetNotificationMessageUri()
232 {
233     return NSMessageUri;
234 }
235
236 const char* NSGetNotificationSyncUri()
237 {
238     return NSSyncUri;
239 }
240
241 NSResult NSCopyString(char** targetString, const char* sourceString)
242 {
243     if (sourceString)
244     {
245         *targetString = (char *) malloc(strlen(sourceString) + 1);
246
247         if (*targetString)
248         {
249             strncpy(*targetString, sourceString, (strlen(sourceString) + 1));
250             return NS_SUCCESS;
251         }
252     }
253
254     return NS_FAIL;
255 }