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");
72 NSResult NSCreateResource(char *uri)
74 NS_LOG(DEBUG, "NSCreateResource - IN");
78 NS_LOG(NS_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_DEFAULT_INTERFACE,
105 NS_ROOT_URI, NSEntityHandlerNotificationCb, NULL, resourceProperties) != OC_STACK_OK)
107 NS_LOG(NS_ERROR, "Fail to Create Notification Resource");
111 else if (strcmp(uri, NS_COLLECTION_MESSAGE_URI) == 0)
114 NotificationMessageResource.messageId = 0;
116 (NotificationMessageResource.providerId)[0] = '\0';
117 NotificationMessageResource.type = 0;
118 NotificationMessageResource.dateTime = NULL;
119 NotificationMessageResource.ttl = 0;
120 NotificationMessageResource.title = NULL;
121 NotificationMessageResource.contentText = NULL;
122 NotificationMessageResource.sourceName = NULL;
123 NotificationMessageResource.topicName = NULL;
124 NotificationMessageResource.mediaContents = NULL;
126 if(NSGetResourceSecurity())
128 NS_LOG(DEBUG, "Create secured resource");
129 resourceProperties = OC_OBSERVABLE | OC_SECURE;
133 resourceProperties = OC_OBSERVABLE;
136 if (OCCreateResource(&NotificationMessageResource.handle, NS_COLLECTION_MESSAGE_TYPE,
137 NS_DEFAULT_INTERFACE, NS_COLLECTION_MESSAGE_URI, NSEntityHandlerMessageCb, NULL,
138 resourceProperties) != OC_STACK_OK)
140 NS_LOG(NS_ERROR, "Fail to Create Notification Message Resource");
144 else if (strcmp(uri, NS_COLLECTION_SYNC_URI) == 0)
146 NotificationSyncResource.id = NULL;
147 (NotificationSyncResource.providerId)[0] = '\0';
148 NotificationSyncResource.state = NULL;
149 NotificationSyncResource.handle = NULL;
151 if(NSGetResourceSecurity())
153 NS_LOG(DEBUG, "Create secured resource");
154 resourceProperties = OC_OBSERVABLE | OC_SECURE;
158 resourceProperties = OC_OBSERVABLE;
161 if (OCCreateResource(&(NotificationSyncResource.handle), NS_COLLECTION_SYNC_TYPE,
162 NS_DEFAULT_INTERFACE, NS_COLLECTION_SYNC_URI, NSEntityHandlerSyncCb, NULL,
163 resourceProperties) != OC_STACK_OK)
165 NS_LOG(NS_ERROR, "Fail to Create Notification Sync Resource");
169 else if (strcmp(uri, NS_COLLECTION_TOPIC_URI) == 0)
171 (NotificationTopicResource.providerId)[0] = '\0';
172 (NotificationTopicResource.consumerId)[0] = '\0';
173 NotificationTopicResource.TopicList = NULL;
174 NotificationTopicResource.handle = NULL;
176 if(NSGetResourceSecurity())
178 NS_LOG(DEBUG, "Create secured resource");
179 resourceProperties = OC_DISCOVERABLE | OC_SECURE;
183 resourceProperties = OC_DISCOVERABLE;
186 if (OCCreateResource(&(NotificationTopicResource.handle), NS_COLLECTION_TOPIC_TYPE,
187 NS_DEFAULT_INTERFACE, NS_COLLECTION_TOPIC_URI, NSEntityHandlerTopicCb, NULL,
188 resourceProperties) != OC_STACK_OK)
190 NS_LOG(NS_ERROR, "Fail to Create Notification Sync Resource");
196 NS_LOG(ERROR, "Fail to create resource with invalid URI");
200 NS_LOG(DEBUG, "NSCreateResource - OUT");
204 NSResult NSRegisterResource()
206 NS_LOG(DEBUG, "NSRegisterResource - IN");
209 NSCreateResource(NS_COLLECTION_TOPIC_URI), "Fail to register Topic Resource");
211 NSCreateResource(NS_COLLECTION_SYNC_URI), "Fail to register Sync Resource");
213 NSCreateResource(NS_COLLECTION_MESSAGE_URI), "Fail to register Message Resource");
215 NSCreateResource(NS_ROOT_URI), "Fail to register Notification Resource");
217 NS_LOG(DEBUG, "NSRegisterResource - OUT");
221 NSResult NSUnRegisterResource()
223 NS_LOG(DEBUG, "NSUnRegisterResource - IN");
226 OCDeleteResource(NotificationResource.handle), "Fail to Delete Notification Resource");
227 NS_DELETE_RESOURCE(OCDeleteResource(NotificationMessageResource.handle),
228 "Fail to Delete Notification Message Resource");
229 NS_DELETE_RESOURCE(OCDeleteResource(NotificationSyncResource.handle),
230 "Fail to Delete Notification Sync Resource");
231 NS_DELETE_RESOURCE(OCDeleteResource(NotificationTopicResource.handle),
232 "Fail to Delete Notification Topic Resource");
234 NotificationResource.handle = NULL;
235 NotificationMessageResource.handle = NULL;
236 NotificationSyncResource.handle = NULL;
237 NotificationTopicResource.handle = NULL;
239 NS_LOG(DEBUG, "NSUnRegisterResource - OUT");
243 NSResult NSPutNotificationResource(bool policy, OCResourceHandle * handle)
245 NS_LOG(DEBUG, "NSPutNotificationResource - IN");
247 NotificationResource.policy = policy;
248 OICStrcpy(NotificationResource.providerId, UUID_STRING_SIZE,
249 NSGetProviderInfo()->providerId);
250 NotificationResource.message_uri = NS_COLLECTION_MESSAGE_URI;
251 NotificationResource.sync_uri = NS_COLLECTION_SYNC_URI;
252 NotificationResource.topic_uri = NS_COLLECTION_TOPIC_URI;
253 NotificationResource.version = VERSION;
255 *handle = NotificationResource.handle;
257 NS_LOG(DEBUG, "NSPutNotificationResource - OUT");
261 NSResult NSPutMessageResource(NSMessage *msg, OCResourceHandle * handle)
263 NS_LOG(DEBUG, "NSPutMessageResource - IN");
267 NS_LOG(DEBUG, "NSMessage is valid");
269 NotificationMessageResource.messageId = msg->messageId;
270 OICStrcpy(NotificationMessageResource.providerId, UUID_STRING_SIZE, msg->providerId);
271 NotificationMessageResource.type = msg->type;
272 NotificationMessageResource.dateTime = msg->dateTime;
273 NotificationMessageResource.ttl = msg->ttl;
274 NotificationMessageResource.title = msg->title;
275 NotificationMessageResource.contentText = msg->contentText;
276 NotificationMessageResource.sourceName = msg->sourceName;
277 NotificationMessageResource.topicName = msg->topic;
278 NotificationMessageResource.mediaContents = msg->mediaContents;
282 NS_LOG(ERROR, "NSMessage is NULL");
285 *handle = NotificationMessageResource.handle;
287 NS_LOG(DEBUG, "NSPutMessageResource - OUT");
291 NSResult NSPutSyncResource(NSSyncInfo *sync, OCResourceHandle * handle)
293 NS_LOG(DEBUG, "NSPutSyncResource - IN");
297 *handle = NotificationSyncResource.handle;
299 NS_LOG(DEBUG, "NSPutSyncResource - OUT");
303 NSResult NSPutTopicResource(NSTopicList *topicList, OCResourceHandle * handle)
305 NS_LOG(DEBUG, "NSPutTopicResource - IN");
309 *handle = NotificationTopicResource.handle;
311 NS_LOG(DEBUG, "NSPutTopicResource - OUT");