Clean the warning message in 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 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     if (!uri)
67     {
68         OIC_LOG(ERROR, RESOURCE_TAG, "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             OIC_LOG(ERROR, RESOURCE_TAG, PCF("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             OIC_LOG(ERROR, RESOURCE_TAG, PCF("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         memset(&NotificationSyncResource.device, 0, sizeof(NSDevice));
107         NotificationSyncResource.handle = NULL;
108
109         if (OCCreateResource(&(NotificationSyncResource.handle), NSSyncType, NSInterface, NSSyncUri,
110                 NSEntityHandlerSyncCb, NULL, OC_OBSERVABLE) != OC_STACK_OK)
111         {
112             OIC_LOG(ERROR, RESOURCE_TAG, PCF("Fail to Create Notification Sync Resource"));
113             return NS_ERROR;
114         }
115     }
116     else
117     {
118         OIC_LOG(ERROR, RESOURCE_TAG, PCF("Fail to create resource with invalid URI"));
119         return NS_ERROR;
120     }
121
122     return NS_OK;
123 }
124
125 NSResult NSRegisterResource()
126 {
127     OIC_LOG(INFO, RESOURCE_TAG, "NSRegisterResource");
128
129     if (NSCreateResource(NSSyncUri) != NS_OK)
130     {
131         OIC_LOG(ERROR, RESOURCE_TAG, PCF("Fail to register Sync Resource"));
132         return NS_ERROR;
133     }
134
135     if (NSCreateResource(NSMessageUri) != NS_OK)
136     {
137         OIC_LOG(ERROR, RESOURCE_TAG, PCF("Fail to register Message Resource"));
138         return NS_ERROR;
139     }
140
141     if (NSCreateResource(NSUri) != NS_OK)
142     {
143         OIC_LOG(ERROR, RESOURCE_TAG, PCF("Fail to register Notification Resource"));
144         return NS_ERROR;
145     }
146
147     return NS_OK;
148 }
149
150 NSResult NSPutNotificationResource(int accepter, OCResourceHandle * handle)
151 {
152     NotificationResource.accepter = accepter;
153     NotificationResource.message_uri = NSMessageUri;
154     NotificationResource.sync_uri = NSSyncUri;
155
156     *handle = NotificationResource.handle;
157
158     return NS_OK;
159 }
160
161 NSResult NSPutMessageResource(NSMessage *msg, OCResourceHandle * handle)
162 {
163     OIC_LOG(INFO, RESOURCE_TAG, "Put notification message to Resource");
164
165     if(msg != NULL)
166     {
167         printf("msg is not null\n");
168         NotificationMessageResource.id = strdup(msg->mId);
169         NotificationMessageResource.title = strdup(msg->mTitle);
170         NotificationMessageResource.body = strdup(msg->mContentText);
171     }
172     else
173     {
174         printf("msg is null\n");
175     }
176
177     *handle = NotificationMessageResource.handle;
178
179     return NS_OK;
180 }
181
182 NSResult NSPutSyncResource(NSSync *sync, OCResourceHandle * handle)
183 {
184     OIC_LOG(INFO, RESOURCE_TAG, "Put notification sync to Resource");
185
186     //should be implementation
187     (void) sync;
188
189     *handle = NotificationSyncResource.handle;
190
191     return NS_OK;
192 }
193
194 const char* NSGetNotificationUri()
195 {
196     return NSUri;
197 }
198
199 const char* NSGetNotificationMessageUri()
200 {
201     return NSMessageUri;
202 }
203
204 const char* NSGetNotificationSyncUri()
205 {
206     return NSSyncUri;
207 }
208
209 NSResult NSCopyString(char** targetString, const char* sourceString)
210 {
211     if (sourceString)
212     {
213         *targetString = (char *) malloc(strlen(sourceString) + 1);
214
215         if (*targetString)
216         {
217             strncpy(*targetString, sourceString, (strlen(sourceString) + 1));
218             return NS_SUCCESS;
219         }
220     }
221
222     return NS_FAIL;
223 }