remove not required function and rename the function, const variable.
[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 NSNotificationResource NotificationResource;
24 NSMessageResource NotificationMessageResource;
25 NSSyncResource NotificationSyncResource;
26
27 NSResult NSCreateResource(char *uri)
28 {
29     NS_LOG(DEBUG, "NSCreateResource - IN");
30     if (!uri)
31     {
32         NS_LOG(NS_ERROR, "Resource URI cannot be NULL");
33         return NS_ERROR;
34     }
35
36     if (strcmp(uri, NS_ROOT_URI) == 0)
37     {
38         NotificationResource.accepter = 0;
39         NotificationResource.message_uri = NS_COLLECTION_MESSAGE_URI;
40         NotificationResource.sync_uri = NS_COLLECTION_SYNC_URI;
41         NotificationResource.handle = NULL;
42
43         if (OCCreateResource(&NotificationResource.handle, NS_ROOT_TYPE, NS_DEFAULT_INTERFACE,
44                 NS_ROOT_URI, NSEntityHandlerNotificationCb, NULL, OC_DISCOVERABLE) != OC_STACK_OK)
45         {
46             NS_LOG(NS_ERROR, "Fail to Create Notification Resource");
47             return NS_ERROR;
48         }
49     }
50     else if (strcmp(uri, NS_COLLECTION_MESSAGE_URI) == 0)
51     {
52
53         NotificationMessageResource.id = NULL;
54         NotificationMessageResource.title = NULL;
55         NotificationMessageResource.body = NULL;
56         NotificationMessageResource.handle = NULL;
57
58         if (OCCreateResource(&NotificationMessageResource.handle, NS_COLLECTION_MESSAGE_TYPE,
59                 NS_DEFAULT_INTERFACE, NS_COLLECTION_MESSAGE_URI, NSEntityHandlerMessageCb, NULL,
60                 OC_OBSERVABLE) != OC_STACK_OK)
61         {
62             NS_LOG(NS_ERROR, "Fail to Create Notification Message Resource");
63             return NS_ERROR;
64         }
65     }
66     else if (strcmp(uri, NS_COLLECTION_SYNC_URI) == 0)
67     {
68         NotificationSyncResource.id = NULL;
69         NotificationSyncResource.state = NULL;
70         NotificationSyncResource.handle = NULL;
71
72         if (OCCreateResource(&(NotificationSyncResource.handle), NS_COLLECTION_SYNC_TYPE,
73                 NS_DEFAULT_INTERFACE, NS_COLLECTION_SYNC_URI, NSEntityHandlerSyncCb, NULL,
74                 OC_OBSERVABLE) != OC_STACK_OK)
75         {
76             NS_LOG(NS_ERROR, "Fail to Create Notification Sync Resource");
77             return NS_ERROR;
78         }
79     }
80     else
81     {
82         NS_LOG(ERROR, "Fail to create resource with invalid URI");
83         return NS_ERROR;
84     }
85
86     NS_LOG(DEBUG, "NSCreateResource - OUT");
87     return NS_OK;
88 }
89
90 NSResult NSRegisterResource()
91 {
92     NS_LOG(DEBUG, "NSRegisterResource - IN");
93
94     if (NSCreateResource(NS_COLLECTION_SYNC_URI) != NS_OK)
95     {
96         NS_LOG(ERROR, "Fail to register Sync Resource");
97         return NS_ERROR;
98     }
99
100     if (NSCreateResource(NS_COLLECTION_MESSAGE_URI) != NS_OK)
101     {
102         NS_LOG(ERROR, "Fail to register Message Resource");
103         return NS_ERROR;
104     }
105
106     if (NSCreateResource(NS_ROOT_URI) != NS_OK)
107     {
108         NS_LOG(ERROR, "Fail to register Notification Resource");
109         return NS_ERROR;
110     }
111
112     NS_LOG(DEBUG, "NSRegisterResource - OUT");
113     return NS_OK;
114 }
115
116 NSResult NSUnRegisterResource()
117 {
118     NS_LOG(DEBUG, "NSUnRegisterResource - IN");
119
120     if (OCDeleteResource(NotificationResource.handle) != OC_STACK_OK)
121     {
122         NS_LOG(ERROR, "Fail to Delete Notification Resource");
123         return NS_ERROR;
124     }
125
126     if (OCDeleteResource(NotificationMessageResource.handle) != OC_STACK_OK)
127     {
128         NS_LOG(ERROR, "Fail to Delete Notification Message Resource");
129         return NS_ERROR;
130     }
131
132     if (OCDeleteResource(NotificationSyncResource.handle) != OC_STACK_OK)
133     {
134         NS_LOG(ERROR, "Fail to Delete Notification Sync Resource");
135         return NS_ERROR;
136     }
137
138     NS_LOG(DEBUG, "NSUnRegisterResource - OUT");
139     return NS_OK;
140 }
141
142 NSResult NSPutNotificationResource(int accepter, OCResourceHandle * handle)
143 {
144     NS_LOG(DEBUG, "NSPutNotificationResource - IN");
145
146     NotificationResource.accepter = accepter;
147     NotificationResource.message_uri = NS_COLLECTION_MESSAGE_URI;
148     NotificationResource.sync_uri = NS_COLLECTION_SYNC_URI;
149
150     *handle = NotificationResource.handle;
151
152     NS_LOG(DEBUG, "NSPutNotificationResource - OUT");
153     return NS_OK;
154 }
155
156 NSResult NSPutMessageResource(NSMessage *msg, OCResourceHandle * handle)
157 {
158     NS_LOG(DEBUG, "NSPutMessageResource - IN");
159
160     if(msg != NULL)
161     {
162         NS_LOG(DEBUG, "NSMessage is valid");
163
164         NotificationMessageResource.id = OICStrdup(msg->mId);
165         NotificationMessageResource.title = OICStrdup(msg->mTitle);
166         NotificationMessageResource.body = OICStrdup(msg->mContentText);
167     }
168     else
169     {
170         NS_LOG(ERROR, "NSMessage is NULL");
171     }
172
173     *handle = NotificationMessageResource.handle;
174     NS_LOG(DEBUG, "NSPutMessageResource - OUT");
175
176     return NS_OK;
177 }
178
179 NSResult NSPutSyncResource(NSSync *sync, OCResourceHandle * handle)
180 {
181     NS_LOG(DEBUG, "NSPutSyncResource - IN");
182
183     (void) sync;
184
185     *handle = NotificationSyncResource.handle;
186
187     NS_LOG(DEBUG, "NSPutSyncResource - OUT");
188     return NS_OK;
189 }