1 //******************************************************************
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "NSProviderResource.h"
24 NSNotificationResource NotificationResource;
25 NSMessageResource NotificationMessageResource;
26 NSSyncResource NotificationSyncResource;
27 NSTopicResource NotificationTopicResource;
29 #if(defined WITH_CLOUD && defined RD_CLIENT)
30 #define DEFAULT_CONTEXT_VALUE 0x99
32 OCStackApplicationResult NSHandlePublishCb(void *ctx, OCDoHandle handle,
33 OCClientResponse *clientResponse)
36 if(ctx != (void *)DEFAULT_CONTEXT_VALUE)
38 NS_LOG(DEBUG, "Invalid Publish callback received");
41 NS_LOG_V(DEBUG, "Publish resource response received code: %d", clientResponse->result);
43 return OC_STACK_KEEP_TRANSACTION;
46 NSResult NSPublishResourceToCloud(char *serverAddress)
49 NS_LOG(DEBUG, "NSPublishResourceToCloud - IN");
50 NS_LOG_V(DEBUG, "Remote Server Address: %s", serverAddress);
52 OCCallbackData cbData;
53 cbData.cb = NSHandlePublishCb;
54 cbData.context = (void *)DEFAULT_CONTEXT_VALUE;
57 OCResourceHandle resourceHandles[1] = {NotificationResource.handle};
58 OCStackResult res = OCRDPublish(serverAddress, CT_ADAPTER_TCP, resourceHandles, 1,
61 if (res != OC_STACK_OK)
63 NS_LOG_V(DEBUG, "Unable to publish resources to cloud: %d", res);
66 NS_LOG(DEBUG, "NSPublishResourceToCloud - OUT");
71 NSResult NSCreateResource(char *uri)
73 NS_LOG(DEBUG, "NSCreateResource - IN");
77 NS_LOG(NS_ERROR, "Resource URI cannot be NULL");
81 uint8_t resourceProperties;
83 if (strcmp(uri, NS_ROOT_URI) == 0)
85 NotificationResource.policy = true;
86 (NotificationResource.providerId)[0] = '\0';
87 NotificationResource.message_uri = NS_COLLECTION_MESSAGE_URI;
88 NotificationResource.sync_uri = NS_COLLECTION_SYNC_URI;
89 NotificationResource.topic_uri = NS_COLLECTION_TOPIC_URI;
90 NotificationResource.version = VERSION;
91 NotificationResource.handle = NULL;
93 if(NSGetResourceSecurity())
95 NS_LOG(DEBUG, "Create secured resource");
96 resourceProperties = OC_DISCOVERABLE | OC_SECURE;
100 resourceProperties = OC_DISCOVERABLE;
103 if (OCCreateResource(&NotificationResource.handle, NS_ROOT_TYPE, NS_DEFAULT_INTERFACE,
104 NS_ROOT_URI, NSEntityHandlerNotificationCb, NULL,
105 resourceProperties) != OC_STACK_OK)
107 NS_LOG(NS_ERROR, "Fail to Create Notification Resource");
111 if (OCBindResourceInterfaceToResource(NotificationResource.handle, NS_INTERFACE_READ)
114 NS_LOG(NS_ERROR, "Fail to bind Notification Resource Type");
118 else if (strcmp(uri, NS_COLLECTION_MESSAGE_URI) == 0)
121 NotificationMessageResource.messageId = 0;
123 (NotificationMessageResource.providerId)[0] = '\0';
124 NotificationMessageResource.type = 0;
125 NotificationMessageResource.dateTime = NULL;
126 NotificationMessageResource.ttl = 0;
127 NotificationMessageResource.title = NULL;
128 NotificationMessageResource.contentText = NULL;
129 NotificationMessageResource.sourceName = NULL;
130 NotificationMessageResource.topicName = NULL;
131 NotificationMessageResource.mediaContents = NULL;
133 if(NSGetResourceSecurity())
135 NS_LOG(DEBUG, "Create secured resource");
136 resourceProperties = OC_OBSERVABLE | OC_SECURE;
140 resourceProperties = OC_OBSERVABLE;
143 if (OCCreateResource(&NotificationMessageResource.handle, NS_COLLECTION_MESSAGE_TYPE,
144 NS_DEFAULT_INTERFACE, NS_COLLECTION_MESSAGE_URI, NSEntityHandlerMessageCb, NULL,
145 resourceProperties) != OC_STACK_OK)
147 NS_LOG(NS_ERROR, "Fail to Create Notification Message Resource");
151 if (OCBindResourceInterfaceToResource(NotificationMessageResource.handle, NS_INTERFACE_READ)
154 NS_LOG(NS_ERROR, "Fail to bind Notification Message Resource Type");
158 else if (strcmp(uri, NS_COLLECTION_SYNC_URI) == 0)
160 NotificationSyncResource.id = NULL;
161 (NotificationSyncResource.providerId)[0] = '\0';
162 NotificationSyncResource.state = NULL;
163 NotificationSyncResource.handle = NULL;
165 if(NSGetResourceSecurity())
167 NS_LOG(DEBUG, "Create secured resource");
168 resourceProperties = OC_OBSERVABLE | OC_SECURE;
172 resourceProperties = OC_OBSERVABLE;
175 if (OCCreateResource(&(NotificationSyncResource.handle), NS_COLLECTION_SYNC_TYPE,
176 NS_DEFAULT_INTERFACE, NS_COLLECTION_SYNC_URI, NSEntityHandlerSyncCb, NULL,
177 resourceProperties) != OC_STACK_OK)
179 NS_LOG(NS_ERROR, "Fail to Create Notification Sync Resource");
183 if (OCBindResourceInterfaceToResource(NotificationSyncResource.handle,
184 NS_INTERFACE_READWRITE)
187 NS_LOG(NS_ERROR, "Fail to bind Notification Sync Resource Type");
191 else if (strcmp(uri, NS_COLLECTION_TOPIC_URI) == 0)
193 (NotificationTopicResource.providerId)[0] = '\0';
194 (NotificationTopicResource.consumerId)[0] = '\0';
195 NotificationTopicResource.TopicList = NULL;
196 NotificationTopicResource.handle = NULL;
198 if(NSGetResourceSecurity())
200 NS_LOG(DEBUG, "Create secured resource");
201 resourceProperties = OC_DISCOVERABLE | OC_SECURE;
205 resourceProperties = OC_DISCOVERABLE;
208 if (OCCreateResource(&(NotificationTopicResource.handle), NS_COLLECTION_TOPIC_TYPE,
209 NS_DEFAULT_INTERFACE, NS_COLLECTION_TOPIC_URI, NSEntityHandlerTopicCb, NULL,
210 resourceProperties) != OC_STACK_OK)
212 NS_LOG(NS_ERROR, "Fail to Create Notification Sync Resource");
216 if (OCBindResourceInterfaceToResource(NotificationTopicResource.handle,
217 NS_INTERFACE_READWRITE)
220 NS_LOG(NS_ERROR, "Fail to bind Notification Topic Resource Type");
226 NS_LOG(ERROR, "Fail to create resource with invalid URI");
230 NS_LOG(DEBUG, "NSCreateResource - OUT");
234 NSResult NSRegisterResource()
236 NS_LOG(DEBUG, "NSRegisterResource - IN");
239 NSCreateResource(NS_COLLECTION_TOPIC_URI), "Fail to register Topic Resource");
241 NSCreateResource(NS_COLLECTION_SYNC_URI), "Fail to register Sync Resource");
243 NSCreateResource(NS_COLLECTION_MESSAGE_URI), "Fail to register Message Resource");
245 NSCreateResource(NS_ROOT_URI), "Fail to register Notification Resource");
247 NS_LOG(DEBUG, "NSRegisterResource - OUT");
251 NSResult NSUnRegisterResource()
253 NS_LOG(DEBUG, "NSUnRegisterResource - IN");
256 OCDeleteResource(NotificationResource.handle), "Fail to Delete Notification Resource");
257 NS_DELETE_RESOURCE(OCDeleteResource(NotificationMessageResource.handle),
258 "Fail to Delete Notification Message Resource");
259 NS_DELETE_RESOURCE(OCDeleteResource(NotificationSyncResource.handle),
260 "Fail to Delete Notification Sync Resource");
261 NS_DELETE_RESOURCE(OCDeleteResource(NotificationTopicResource.handle),
262 "Fail to Delete Notification Topic Resource");
264 NotificationResource.handle = NULL;
265 NotificationMessageResource.handle = NULL;
266 NotificationSyncResource.handle = NULL;
267 NotificationTopicResource.handle = NULL;
269 NS_LOG(DEBUG, "NSUnRegisterResource - OUT");
273 NSResult NSPutNotificationResource(bool policy, OCResourceHandle * handle)
275 NS_LOG(DEBUG, "NSPutNotificationResource - IN");
277 NotificationResource.policy = policy;
278 OICStrcpy(NotificationResource.providerId, UUID_STRING_SIZE,
279 NSGetProviderInfo()->providerId);
280 NotificationResource.message_uri = NS_COLLECTION_MESSAGE_URI;
281 NotificationResource.sync_uri = NS_COLLECTION_SYNC_URI;
282 NotificationResource.topic_uri = NS_COLLECTION_TOPIC_URI;
283 NotificationResource.version = VERSION;
285 *handle = NotificationResource.handle;
287 NS_LOG(DEBUG, "NSPutNotificationResource - OUT");
291 NSResult NSPutMessageResource(NSMessage *msg, OCResourceHandle * handle)
293 NS_LOG(DEBUG, "NSPutMessageResource - IN");
297 NS_LOG(DEBUG, "NSMessage is valid");
299 NotificationMessageResource.messageId = msg->messageId;
300 OICStrcpy(NotificationMessageResource.providerId, UUID_STRING_SIZE, msg->providerId);
301 NotificationMessageResource.type = msg->type;
302 NotificationMessageResource.dateTime = msg->dateTime;
303 NotificationMessageResource.ttl = msg->ttl;
304 NotificationMessageResource.title = msg->title;
305 NotificationMessageResource.contentText = msg->contentText;
306 NotificationMessageResource.sourceName = msg->sourceName;
307 NotificationMessageResource.topicName = msg->topic;
308 NotificationMessageResource.mediaContents = msg->mediaContents;
312 NS_LOG(ERROR, "NSMessage is NULL");
315 *handle = NotificationMessageResource.handle;
317 NS_LOG(DEBUG, "NSPutMessageResource - OUT");
321 NSResult NSPutSyncResource(NSSyncInfo *sync, OCResourceHandle * handle)
323 NS_LOG(DEBUG, "NSPutSyncResource - IN");
327 *handle = NotificationSyncResource.handle;
329 NS_LOG(DEBUG, "NSPutSyncResource - OUT");
333 NSResult NSPutTopicResource(NSTopicList *topicList, OCResourceHandle * handle)
335 NS_LOG(DEBUG, "NSPutTopicResource - IN");
339 *handle = NotificationTopicResource.handle;
341 NS_LOG(DEBUG, "NSPutTopicResource - OUT");