NSDuplicateMessage copy logic not working(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     NSDevice device;
58 } NSSyncResource;
59
60 NSNotificationResource NotificationResource;
61 NSMessageResource NotificationMessageResource;
62 NSSyncResource NotificationSyncResource;
63
64 NSResult NSCreateResource(char *uri)
65 {
66     NS_LOG(DEBUG, "NSCreateResource - IN");
67     if (!uri)
68     {
69         OIC_LOG(ERROR, RESOURCE_TAG, "Resource URI cannot be NULL");
70         NS_LOG(NS_ERROR, "Resource URI cannot be NULL");
71         return NS_ERROR;
72     }
73
74     if (strcmp(uri, NSUri) == 0)
75     {
76
77         NotificationResource.accepter = 0;
78         NotificationResource.message_uri = NSMessageUri;
79         NotificationResource.sync_uri = NSSyncUri;
80         NotificationResource.handle = NULL;
81
82         if (OCCreateResource(&NotificationResource.handle, NSType, NSInterface, NSUri,
83                 NSEntityHandlerNotificationCb, NULL, OC_DISCOVERABLE) != OC_STACK_OK)
84         {
85             OIC_LOG(ERROR, RESOURCE_TAG, PCF("Fail to Create Notification Resource"));
86             NS_LOG(NS_ERROR, "Fail to Create Notification Resource");
87             return NS_ERROR;
88         }
89     }
90     else if (strcmp(uri, NSMessageUri) == 0)
91     {
92
93         NotificationMessageResource.id = NULL;
94         NotificationMessageResource.title = NULL;
95         NotificationMessageResource.body = NULL;
96         NotificationMessageResource.handle = NULL;
97
98         if (OCCreateResource(&NotificationMessageResource.handle, NSMessageType, NSInterface,
99                 NSMessageUri, NSEntityHandlerMessageCb, NULL, OC_OBSERVABLE) != OC_STACK_OK)
100         {
101             OIC_LOG(ERROR, RESOURCE_TAG, PCF("Fail to Create Notification Message Resource"));
102             NS_LOG(NS_ERROR, "Fail to Create Notification Message Resource");
103             return NS_ERROR;
104         }
105     }
106     else if (strcmp(uri, NSSyncUri) == 0)
107     {
108         NotificationSyncResource.id = NULL;
109         NotificationSyncResource.state = NULL;
110         memset(&NotificationSyncResource.device, 0, sizeof(NSDevice));
111         NotificationSyncResource.handle = NULL;
112
113         if (OCCreateResource(&(NotificationSyncResource.handle), NSSyncType, NSInterface, NSSyncUri,
114                 NSEntityHandlerSyncCb, NULL, OC_OBSERVABLE) != OC_STACK_OK)
115         {
116             OIC_LOG(ERROR, RESOURCE_TAG, PCF("Fail to Create Notification Sync Resource"));
117             NS_LOG(NS_ERROR, "Fail to Create Notification Sync Resource");
118             return NS_ERROR;
119         }
120     }
121     else
122     {
123         OIC_LOG(ERROR, RESOURCE_TAG, PCF("Fail to create resource with invalid URI"));
124         NS_LOG(DEBUG, "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     OIC_LOG(INFO, RESOURCE_TAG, "NSRegisterResource");
135     NS_LOG(DEBUG, "NSRegisterResource - IN");
136
137     if (NSCreateResource(NSSyncUri) != NS_OK)
138     {
139         OIC_LOG(ERROR, RESOURCE_TAG, PCF("Fail to register Sync Resource"));
140         NS_LOG(DEBUG, "Fail to register Sync Resource");
141         return NS_ERROR;
142     }
143
144     if (NSCreateResource(NSMessageUri) != NS_OK)
145     {
146         OIC_LOG(ERROR, RESOURCE_TAG, PCF("Fail to register Message Resource"));
147         NS_LOG(DEBUG, "Fail to register Message Resource");
148         return NS_ERROR;
149     }
150
151     if (NSCreateResource(NSUri) != NS_OK)
152     {
153         OIC_LOG(ERROR, RESOURCE_TAG, PCF("Fail to register Notification Resource"));
154         NS_LOG(DEBUG, "Fail to register Notification Resource");
155         return NS_ERROR;
156     }
157
158     NS_LOG(DEBUG, "NSRegisterResource - OUT");
159     return NS_OK;
160 }
161
162 NSResult NSPutNotificationResource(int accepter, OCResourceHandle * handle)
163 {
164     NS_LOG(DEBUG, "NSPutNotificationResource - IN");
165
166     NotificationResource.accepter = accepter;
167     NotificationResource.message_uri = NSMessageUri;
168     NotificationResource.sync_uri = NSSyncUri;
169
170     *handle = NotificationResource.handle;
171
172     NS_LOG(DEBUG, "NSPutNotificationResource - OUT");
173     return NS_OK;
174 }
175
176 NSResult NSPutMessageResource(NSMessage *msg, OCResourceHandle * handle)
177 {
178     OIC_LOG(INFO, RESOURCE_TAG, "Put notification message to Resource");
179     NS_LOG(DEBUG, "NSPutMessageResource - IN");
180
181     if(msg != NULL)
182     {
183         NS_LOG(DEBUG, "NSMessage is valid");
184         NS_LOG_V(DEBUG, "msg -> mId : %s", msg->mId);
185         NS_LOG_V(DEBUG, "msg -> mTitle : %s", msg->mTitle);
186         NS_LOG_V(DEBUG, "msg -> mContentText : %s", msg->mContentText);
187
188         NotificationMessageResource.id = OICStrdup(msg->mId);
189         NotificationMessageResource.title = OICStrdup(msg->mTitle);
190         NotificationMessageResource.body = OICStrdup(msg->mContentText);
191     }
192     else
193     {
194         NS_LOG(ERROR, "NSMessage is NULL");
195     }
196
197     *handle = NotificationMessageResource.handle;
198     NS_LOG(DEBUG, "NSPutMessageResource - OUT");
199
200     return NS_OK;
201 }
202
203 NSResult NSPutSyncResource(NSSync *sync, OCResourceHandle * handle)
204 {
205     OIC_LOG(INFO, RESOURCE_TAG, "Put notification sync to Resource");
206     NS_LOG(DEBUG, "NSPutSyncResource - IN");
207
208     (void) sync;
209
210     *handle = NotificationSyncResource.handle;
211
212     NS_LOG(DEBUG, "NSPutSyncResource - OUT");
213     return NS_OK;
214 }
215
216 const char* NSGetNotificationUri()
217 {
218     return NSUri;
219 }
220
221 const char* NSGetNotificationMessageUri()
222 {
223     return NSMessageUri;
224 }
225
226 const char* NSGetNotificationSyncUri()
227 {
228     return NSSyncUri;
229 }
230
231 NSResult NSCopyString(char** targetString, const char* sourceString)
232 {
233     if (sourceString)
234     {
235         *targetString = (char *) malloc(strlen(sourceString) + 1);
236
237         if (*targetString)
238         {
239             strncpy(*targetString, sourceString, (strlen(sourceString) + 1));
240             return NS_SUCCESS;
241         }
242     }
243
244     return NS_FAIL;
245 }