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"
23 #include "rd_client.h"
25 NSNotificationResource NotificationResource;
26 NSMessageResource NotificationMessageResource;
27 NSSyncResource NotificationSyncResource;
28 NSTopicResource NotificationTopicResource;
30 #if (defined WITH_CLOUD)
31 #define DEFAULT_CONTEXT_VALUE 0x99
33 OCStackApplicationResult NSHandlePublishCb(void *ctx, OCDoHandle handle,
34 OCClientResponse *clientResponse)
37 if (ctx != (void *)DEFAULT_CONTEXT_VALUE)
39 NS_LOG(DEBUG, "Invalid Publish callback received");
42 NS_LOG_V(DEBUG, "Publish resource response received code: %d", clientResponse->result);
44 return OC_STACK_KEEP_TRANSACTION;
47 NSResult NSPublishResourceToCloud(char *serverAddress)
50 NS_LOG(DEBUG, "NSPublishResourceToCloud - IN");
51 NS_LOG_V(INFO_PRIVATE, "Remote Server Address: %s", serverAddress);
53 OCCallbackData cbData;
54 cbData.cb = NSHandlePublishCb;
55 cbData.context = (void *)DEFAULT_CONTEXT_VALUE;
58 OCResourceHandle resourceHandles[1] = { NotificationResource.handle };
59 OCStackResult res = OCRDPublish(NULL, serverAddress, CT_ADAPTER_TCP, resourceHandles, 1,
62 if (res != OC_STACK_OK)
64 NS_LOG_V(DEBUG, "Unable to publish resources to cloud: %d", res);
67 NS_LOG(DEBUG, "NSPublishResourceToCloud - OUT");
72 NSResult NSCreateResource(char *uri)
74 NS_LOG(DEBUG, "NSCreateResource - IN");
78 NS_LOG(ERROR, "Resource URI cannot be NULL");
82 uint8_t resourceProperties;
84 if (strcmp(uri, NS_ROOT_URI) == 0)
86 NotificationResource.policy = true;
87 (NotificationResource.providerId)[0] = '\0';
88 NotificationResource.message_uri = NS_COLLECTION_MESSAGE_URI;
89 NotificationResource.sync_uri = NS_COLLECTION_SYNC_URI;
90 NotificationResource.topic_uri = NS_COLLECTION_TOPIC_URI;
91 NotificationResource.version = VERSION;
92 NotificationResource.handle = NULL;
94 if (NSGetResourceSecurity())
96 NS_LOG(DEBUG, "Create secured resource");
97 resourceProperties = OC_DISCOVERABLE | OC_SECURE;
101 resourceProperties = OC_DISCOVERABLE;
104 if (OCCreateResource(&NotificationResource.handle, NS_ROOT_TYPE, NS_INTERFACE_BASELINE,
105 NS_ROOT_URI, NSEntityHandlerNotificationCb, NULL,
106 resourceProperties) != OC_STACK_OK)
108 NS_LOG(ERROR, "Fail to Create Notification Resource");
112 if (OCBindResourceInterfaceToResource(NotificationResource.handle, NS_INTERFACE_READ)
115 NS_LOG(ERROR, "Fail to bind Notification Resource Type");
119 else if (strcmp(uri, NS_COLLECTION_MESSAGE_URI) == 0)
122 NotificationMessageResource.messageId = 0;
124 (NotificationMessageResource.providerId)[0] = '\0';
125 NotificationMessageResource.type = 0;
126 NotificationMessageResource.dateTime = NULL;
127 NotificationMessageResource.ttl = 0;
128 NotificationMessageResource.title = NULL;
129 NotificationMessageResource.contentText = NULL;
130 NotificationMessageResource.sourceName = NULL;
131 NotificationMessageResource.topicName = NULL;
132 NotificationMessageResource.mediaContents = NULL;
134 if (NSGetResourceSecurity())
136 NS_LOG(DEBUG, "Create secured resource");
137 resourceProperties = OC_OBSERVABLE | OC_SECURE;
141 resourceProperties = OC_OBSERVABLE;
144 if (OCCreateResource(&NotificationMessageResource.handle, NS_COLLECTION_MESSAGE_TYPE,
145 NS_INTERFACE_BASELINE, NS_COLLECTION_MESSAGE_URI, NSEntityHandlerMessageCb, NULL,
146 resourceProperties) != OC_STACK_OK)
148 NS_LOG(ERROR, "Fail to Create Notification Message Resource");
152 if (OCBindResourceInterfaceToResource(NotificationMessageResource.handle, NS_INTERFACE_READ)
155 NS_LOG(ERROR, "Fail to bind Notification Message Resource Type");
159 else if (strcmp(uri, NS_COLLECTION_SYNC_URI) == 0)
161 NotificationSyncResource.messageId = 0;
162 (NotificationSyncResource.providerId)[0] = '\0';
163 NotificationSyncResource.state = NULL;
164 NotificationSyncResource.handle = NULL;
166 if (NSGetResourceSecurity())
168 NS_LOG(DEBUG, "Create secured resource");
169 resourceProperties = OC_OBSERVABLE | OC_SECURE;
173 resourceProperties = OC_OBSERVABLE;
176 if (OCCreateResource(&(NotificationSyncResource.handle), NS_COLLECTION_SYNC_TYPE,
177 NS_INTERFACE_BASELINE, NS_COLLECTION_SYNC_URI, NSEntityHandlerSyncCb, NULL,
178 resourceProperties) != OC_STACK_OK)
180 NS_LOG(ERROR, "Fail to Create Notification Sync Resource");
184 if (OCBindResourceInterfaceToResource(NotificationSyncResource.handle,
185 NS_INTERFACE_READWRITE)
188 NS_LOG(ERROR, "Fail to bind Notification Sync Resource Type");
192 else if (strcmp(uri, NS_COLLECTION_TOPIC_URI) == 0)
194 (NotificationTopicResource.providerId)[0] = '\0';
195 (NotificationTopicResource.consumerId)[0] = '\0';
196 NotificationTopicResource.TopicList = NULL;
197 NotificationTopicResource.handle = NULL;
199 if (NSGetResourceSecurity())
201 NS_LOG(DEBUG, "Create secured resource");
202 resourceProperties = OC_RES_PROP_NONE | OC_SECURE;
206 resourceProperties = OC_RES_PROP_NONE;
209 if (OCCreateResource(&(NotificationTopicResource.handle), NS_COLLECTION_TOPIC_TYPE,
210 NS_INTERFACE_BASELINE, NS_COLLECTION_TOPIC_URI, NSEntityHandlerTopicCb, NULL,
211 resourceProperties) != OC_STACK_OK)
213 NS_LOG(ERROR, "Fail to Create Notification Sync Resource");
217 if (OCBindResourceInterfaceToResource(NotificationTopicResource.handle,
218 NS_INTERFACE_READWRITE)
221 NS_LOG(ERROR, "Fail to bind Notification Topic Resource Type");
227 NS_LOG(ERROR, "Fail to create resource with invalid URI");
231 NS_LOG(DEBUG, "NSCreateResource - OUT");
235 NSResult NSRegisterResource()
237 NS_LOG(DEBUG, "NSRegisterResource - IN");
240 NSCreateResource(NS_COLLECTION_TOPIC_URI), "Fail to register Topic Resource");
242 NSCreateResource(NS_COLLECTION_SYNC_URI), "Fail to register Sync Resource");
244 NSCreateResource(NS_COLLECTION_MESSAGE_URI), "Fail to register Message Resource");
246 NSCreateResource(NS_ROOT_URI), "Fail to register Notification Resource");
248 NS_LOG(DEBUG, "NSRegisterResource - OUT");
252 NSResult NSUnRegisterResource()
254 NS_LOG(DEBUG, "NSUnRegisterResource - IN");
257 OCDeleteResource(NotificationResource.handle), "Fail to Delete Notification Resource");
258 NS_DELETE_RESOURCE(OCDeleteResource(NotificationMessageResource.handle),
259 "Fail to Delete Notification Message Resource");
260 NS_DELETE_RESOURCE(OCDeleteResource(NotificationSyncResource.handle),
261 "Fail to Delete Notification Sync Resource");
262 NS_DELETE_RESOURCE(OCDeleteResource(NotificationTopicResource.handle),
263 "Fail to Delete Notification Topic Resource");
265 NotificationResource.handle = NULL;
266 NotificationMessageResource.handle = NULL;
267 NotificationSyncResource.handle = NULL;
268 NotificationTopicResource.handle = NULL;
270 NS_LOG(DEBUG, "NSUnRegisterResource - OUT");
274 NSResult NSPutNotificationResource(bool policy, OCResourceHandle * handle)
276 NS_LOG(DEBUG, "NSPutNotificationResource - IN");
278 NotificationResource.policy = policy;
279 OICStrcpy(NotificationResource.providerId, UUID_STRING_SIZE,
280 NSGetProviderInfo()->providerId);
281 NotificationResource.message_uri = NS_COLLECTION_MESSAGE_URI;
282 NotificationResource.sync_uri = NS_COLLECTION_SYNC_URI;
283 NotificationResource.topic_uri = NS_COLLECTION_TOPIC_URI;
284 NotificationResource.version = VERSION;
286 *handle = NotificationResource.handle;
288 NS_LOG(DEBUG, "NSPutNotificationResource - OUT");
292 NSResult NSPutMessageResource(NSMessage *msg, OCResourceHandle * handle)
294 NS_LOG(DEBUG, "NSPutMessageResource - IN");
298 NS_LOG(DEBUG, "NSMessage is valid");
300 NotificationMessageResource.messageId = msg->messageId;
301 OICStrcpy(NotificationMessageResource.providerId, UUID_STRING_SIZE, msg->providerId);
302 NotificationMessageResource.type = msg->type;
303 NotificationMessageResource.dateTime = msg->dateTime;
304 NotificationMessageResource.ttl = msg->ttl;
305 NotificationMessageResource.title = msg->title;
306 NotificationMessageResource.contentText = msg->contentText;
307 NotificationMessageResource.sourceName = msg->sourceName;
308 NotificationMessageResource.topicName = msg->topic;
309 NotificationMessageResource.mediaContents = msg->mediaContents;
313 NS_LOG(ERROR, "NSMessage is NULL");
316 *handle = NotificationMessageResource.handle;
318 NS_LOG(DEBUG, "NSPutMessageResource - OUT");
322 NSResult NSPutSyncResource(NSSyncInfo *sync, OCResourceHandle * handle)
324 NS_LOG(DEBUG, "NSPutSyncResource - IN");
328 *handle = NotificationSyncResource.handle;
330 NS_LOG(DEBUG, "NSPutSyncResource - OUT");
334 NSResult NSPutTopicResource(NSTopicList *topicList, OCResourceHandle * handle)
336 NS_LOG(DEBUG, "NSPutTopicResource - IN");
340 *handle = NotificationTopicResource.handle;
342 NS_LOG(DEBUG, "NSPutTopicResource - OUT");