code cleanup.
[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 #include <string.h>
23
24 NSNotificationResource NotificationResource;
25 NSMessageResource NotificationMessageResource;
26 NSSyncResource NotificationSyncResource;
27
28 OCStackApplicationResult NSHandlePublishCb(void *ctx, OCDoHandle handle,
29     OCClientResponse *clientResponse)
30 {
31     (void) handle;
32
33     if (ctx != (void *)DEFAULT_CONTEXT_VALUE)
34     {
35         NS_LOG(DEBUG, "Invalid publish callback received");
36     }
37
38     NS_LOG_V(DEBUG, "Publish resource response received code: %d", clientResponse->result);
39
40     return OC_STACK_KEEP_TRANSACTION;
41 }
42
43 NSResult NSPublishResourceToCloud(char *serverAddress)
44 {
45
46     NS_LOG(DEBUG, "NSPublishResourceToCloud - IN");
47     NS_LOG_V(DEBUG, "Cloud address: %s", serverAddress);
48
49     const char * publishQuery = "/oic/rd?rt=oic.wk.rdpub";
50
51     if (NSCloudPublish(serverAddress, publishQuery, &NSHandlePublishCb, 1,
52             NotificationResource.handle) != OC_STACK_OK)
53     {
54         NS_LOG(DEBUG, "Unable to publish resources to cloud");
55     }
56
57     NS_LOG(DEBUG, "NSPublishResourceToCloud - OUT");
58     return NS_OK;
59 }
60
61 NSResult NSCreateResource(char *uri)
62 {
63     NS_LOG(DEBUG, "NSCreateResource - IN");
64     if (!uri)
65     {
66         NS_LOG(NS_ERROR, "Resource URI cannot be NULL");
67         return NS_ERROR;
68     }
69
70     if (strcmp(uri, NS_ROOT_URI) == 0)
71     {
72         NotificationResource.accepter = 0;
73         NotificationResource.message_uri = NS_COLLECTION_MESSAGE_URI;
74         NotificationResource.sync_uri = NS_COLLECTION_SYNC_URI;
75         NotificationResource.handle = NULL;
76
77         if (OCCreateResource(&NotificationResource.handle, NS_ROOT_TYPE, NS_DEFAULT_INTERFACE,
78                 NS_ROOT_URI, NSEntityHandlerNotificationCb, NULL, OC_DISCOVERABLE) != OC_STACK_OK)
79         {
80             NS_LOG(NS_ERROR, "Fail to Create Notification Resource");
81             return NS_ERROR;
82         }
83     }
84     else if (strcmp(uri, NS_COLLECTION_MESSAGE_URI) == 0)
85     {
86
87         NotificationMessageResource.messageId = 0;
88
89         (NotificationMessageResource.providerId)[0] = '\0';
90         NotificationMessageResource.type = 0;
91         NotificationMessageResource.dateTime = NULL;
92         NotificationMessageResource.ttl = 0;
93         NotificationMessageResource.title = NULL;
94         NotificationMessageResource.contentText = NULL;
95         NotificationMessageResource.sourceName = NULL;
96         NotificationMessageResource.mediaContents = NULL;
97
98         if (OCCreateResource(&NotificationMessageResource.handle, NS_COLLECTION_MESSAGE_TYPE,
99                 NS_DEFAULT_INTERFACE, NS_COLLECTION_MESSAGE_URI, NSEntityHandlerMessageCb, NULL,
100                 OC_OBSERVABLE) != OC_STACK_OK)
101         {
102             NS_LOG(NS_ERROR, "Fail to Create Notification Message Resource");
103             return NS_ERROR;
104         }
105     }
106     else if (strcmp(uri, NS_COLLECTION_SYNC_URI) == 0)
107     {
108         NotificationSyncResource.id = NULL;
109         NotificationSyncResource.state = NULL;
110         NotificationSyncResource.handle = NULL;
111
112         if (OCCreateResource(&(NotificationSyncResource.handle), NS_COLLECTION_SYNC_TYPE,
113                 NS_DEFAULT_INTERFACE, NS_COLLECTION_SYNC_URI, NSEntityHandlerSyncCb, NULL,
114                 OC_OBSERVABLE) != OC_STACK_OK)
115         {
116             NS_LOG(NS_ERROR, "Fail to Create Notification Sync Resource");
117             return NS_ERROR;
118         }
119     }
120     else
121     {
122         NS_LOG(ERROR, "Fail to create resource with invalid URI");
123         return NS_ERROR;
124     }
125
126     NS_LOG(DEBUG, "NSCreateResource - OUT");
127     return NS_OK;
128 }
129
130 NSResult NSRegisterResource()
131 {
132     NS_LOG(DEBUG, "NSRegisterResource - IN");
133
134     if (NSCreateResource(NS_COLLECTION_SYNC_URI) != NS_OK)
135     {
136         NS_LOG(ERROR, "Fail to register Sync Resource");
137         return NS_ERROR;
138     }
139
140     if (NSCreateResource(NS_COLLECTION_MESSAGE_URI) != NS_OK)
141     {
142         NS_LOG(ERROR, "Fail to register Message Resource");
143         return NS_ERROR;
144     }
145
146     if (NSCreateResource(NS_ROOT_URI) != NS_OK)
147     {
148         NS_LOG(ERROR, "Fail to register Notification Resource");
149         return NS_ERROR;
150     }
151
152     NS_LOG(DEBUG, "NSRegisterResource - OUT");
153     return NS_OK;
154 }
155
156 NSResult NSUnRegisterResource()
157 {
158     NS_LOG(DEBUG, "NSUnRegisterResource - IN");
159
160     if (OCDeleteResource(NotificationResource.handle) != OC_STACK_OK)
161     {
162         NS_LOG(ERROR, "Fail to Delete Notification Resource");
163         return NS_ERROR;
164     }
165
166     if (OCDeleteResource(NotificationMessageResource.handle) != OC_STACK_OK)
167     {
168         NS_LOG(ERROR, "Fail to Delete Notification Message Resource");
169         return NS_ERROR;
170     }
171
172     if (OCDeleteResource(NotificationSyncResource.handle) != OC_STACK_OK)
173     {
174         NS_LOG(ERROR, "Fail to Delete Notification Sync Resource");
175         return NS_ERROR;
176     }
177
178     NotificationResource.handle = NULL;
179     NotificationMessageResource.handle = NULL;
180     NotificationSyncResource.handle = NULL;
181
182     NS_LOG(DEBUG, "NSUnRegisterResource - OUT");
183     return NS_OK;
184 }
185
186 NSResult NSPutNotificationResource(int accepter, OCResourceHandle * handle)
187 {
188     NS_LOG(DEBUG, "NSPutNotificationResource - IN");
189
190     NotificationResource.accepter = accepter;
191     NotificationResource.message_uri = NS_COLLECTION_MESSAGE_URI;
192     NotificationResource.sync_uri = NS_COLLECTION_SYNC_URI;
193
194     *handle = NotificationResource.handle;
195
196     NS_LOG(DEBUG, "NSPutNotificationResource - OUT");
197     return NS_OK;
198 }
199
200 NSResult NSPutMessageResource(NSMessage *msg, OCResourceHandle * handle)
201 {
202     NS_LOG(DEBUG, "NSPutMessageResource - IN");
203
204     if(msg != NULL)
205     {
206         NS_LOG(DEBUG, "NSMessage is valid");
207
208         NotificationMessageResource.messageId = msg->messageId;
209         OICStrcpy(NotificationMessageResource.providerId, UUID_STRING_SIZE, msg->providerId);
210         NotificationMessageResource.type = msg->type;
211         NotificationMessageResource.dateTime = msg->dateTime;
212         NotificationMessageResource.ttl = msg->ttl;
213         NotificationMessageResource.title = msg->title;
214         NotificationMessageResource.contentText = msg->contentText;
215         NotificationMessageResource.sourceName = msg->sourceName;
216         NotificationMessageResource.mediaContents = msg->mediaContents;
217     }
218     else
219     {
220         NS_LOG(ERROR, "NSMessage is NULL");
221     }
222
223     *handle = NotificationMessageResource.handle;
224     NS_LOG(DEBUG, "NSPutMessageResource - OUT");
225
226     return NS_OK;
227 }
228
229 NSResult NSPutSyncResource(NSSyncInfo *sync, OCResourceHandle * handle)
230 {
231     NS_LOG(DEBUG, "NSPutSyncResource - IN");
232
233     (void) sync;
234
235     *handle = NotificationSyncResource.handle;
236
237     NS_LOG(DEBUG, "NSPutSyncResource - OUT");
238     return NS_OK;
239 }