changed log in provider side
[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(ERROR, "NSMessage is valid");
184         NotificationMessageResource.id = strdup(msg->mId);
185         NotificationMessageResource.title = strdup(msg->mTitle);
186         NotificationMessageResource.body = strdup(msg->mContentText);
187     }
188     else
189     {
190         NS_LOG(ERROR, "NSMessage is NULL");
191     }
192
193     *handle = NotificationMessageResource.handle;
194     NS_LOG(DEBUG, "NSPutMessageResource - OUT");
195
196     return NS_OK;
197 }
198
199 NSResult NSPutSyncResource(NSSync *sync, OCResourceHandle * handle)
200 {
201     OIC_LOG(INFO, RESOURCE_TAG, "Put notification sync to Resource");
202     NS_LOG(DEBUG, "NSPutSyncResource - IN");
203
204     (void) sync;
205
206     *handle = NotificationSyncResource.handle;
207
208     NS_LOG(DEBUG, "NSPutSyncResource - OUT");
209     return NS_OK;
210 }
211
212 const char* NSGetNotificationUri()
213 {
214     return NSUri;
215 }
216
217 const char* NSGetNotificationMessageUri()
218 {
219     return NSMessageUri;
220 }
221
222 const char* NSGetNotificationSyncUri()
223 {
224     return NSSyncUri;
225 }
226
227 NSResult NSCopyString(char** targetString, const char* sourceString)
228 {
229     if (sourceString)
230     {
231         *targetString = (char *) malloc(strlen(sourceString) + 1);
232
233         if (*targetString)
234         {
235             strncpy(*targetString, sourceString, (strlen(sourceString) + 1));
236             return NS_SUCCESS;
237         }
238     }
239
240     return NS_FAIL;
241 }