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");
77 NS_LOG(NS_ERROR, "Resource URI cannot be NULL");
81 if (strcmp(uri, NS_ROOT_URI) == 0)
83 NotificationResource.policy = true;
84 (NotificationResource.providerId)[0] = '\0';
85 NotificationResource.message_uri = NS_COLLECTION_MESSAGE_URI;
86 NotificationResource.sync_uri = NS_COLLECTION_SYNC_URI;
87 NotificationResource.topic_uri = NS_COLLECTION_TOPIC_URI;
88 NotificationResource.version = VERSION;
89 NotificationResource.handle = NULL;
91 if (OCCreateResource(&NotificationResource.handle, NS_ROOT_TYPE, NS_DEFAULT_INTERFACE,
92 NS_ROOT_URI, NSEntityHandlerNotificationCb, NULL, OC_DISCOVERABLE) != OC_STACK_OK)
94 NS_LOG(NS_ERROR, "Fail to Create Notification Resource");
98 else if (strcmp(uri, NS_COLLECTION_MESSAGE_URI) == 0)
101 NotificationMessageResource.messageId = 0;
103 (NotificationMessageResource.providerId)[0] = '\0';
104 NotificationMessageResource.type = 0;
105 NotificationMessageResource.dateTime = NULL;
106 NotificationMessageResource.ttl = 0;
107 NotificationMessageResource.title = NULL;
108 NotificationMessageResource.contentText = NULL;
109 NotificationMessageResource.sourceName = NULL;
110 NotificationMessageResource.topicName = NULL;
111 NotificationMessageResource.mediaContents = NULL;
113 if (OCCreateResource(&NotificationMessageResource.handle, NS_COLLECTION_MESSAGE_TYPE,
114 NS_DEFAULT_INTERFACE, NS_COLLECTION_MESSAGE_URI, NSEntityHandlerMessageCb, NULL,
115 OC_OBSERVABLE) != OC_STACK_OK)
117 NS_LOG(NS_ERROR, "Fail to Create Notification Message Resource");
121 else if (strcmp(uri, NS_COLLECTION_SYNC_URI) == 0)
123 NotificationSyncResource.id = NULL;
124 (NotificationSyncResource.providerId)[0] = '\0';
125 NotificationSyncResource.state = NULL;
126 NotificationSyncResource.handle = NULL;
128 if (OCCreateResource(&(NotificationSyncResource.handle), NS_COLLECTION_SYNC_TYPE,
129 NS_DEFAULT_INTERFACE, NS_COLLECTION_SYNC_URI, NSEntityHandlerSyncCb, NULL,
130 OC_OBSERVABLE) != OC_STACK_OK)
132 NS_LOG(NS_ERROR, "Fail to Create Notification Sync Resource");
136 else if (strcmp(uri, NS_COLLECTION_TOPIC_URI) == 0)
138 (NotificationTopicResource.providerId)[0] = '\0';
139 (NotificationTopicResource.consumerId)[0] = '\0';
140 NotificationTopicResource.TopicList = NULL;
141 NotificationTopicResource.handle = NULL;
143 if (OCCreateResource(&(NotificationTopicResource.handle), NS_COLLECTION_TOPIC_TYPE,
144 NS_DEFAULT_INTERFACE, NS_COLLECTION_TOPIC_URI, NSEntityHandlerTopicCb, NULL,
145 OC_DISCOVERABLE) != OC_STACK_OK)
147 NS_LOG(NS_ERROR, "Fail to Create Notification Sync Resource");
153 NS_LOG(ERROR, "Fail to create resource with invalid URI");
157 NS_LOG(DEBUG, "NSCreateResource - OUT");
161 NSResult NSRegisterResource()
163 NS_LOG(DEBUG, "NSRegisterResource - IN");
165 if (NSCreateResource(NS_COLLECTION_TOPIC_URI) != NS_OK)
167 NS_LOG(ERROR, "Fail to register Topic Resource");
171 if (NSCreateResource(NS_COLLECTION_SYNC_URI) != NS_OK)
173 NS_LOG(ERROR, "Fail to register Sync Resource");
177 if (NSCreateResource(NS_COLLECTION_MESSAGE_URI) != NS_OK)
179 NS_LOG(ERROR, "Fail to register Message Resource");
183 if (NSCreateResource(NS_ROOT_URI) != NS_OK)
185 NS_LOG(ERROR, "Fail to register Notification Resource");
189 NS_LOG(DEBUG, "NSRegisterResource - OUT");
193 NSResult NSUnRegisterResource()
195 NS_LOG(DEBUG, "NSUnRegisterResource - IN");
197 if (OCDeleteResource(NotificationResource.handle) != OC_STACK_OK)
199 NS_LOG(ERROR, "Fail to Delete Notification Resource");
203 if (OCDeleteResource(NotificationMessageResource.handle) != OC_STACK_OK)
205 NS_LOG(ERROR, "Fail to Delete Notification Message Resource");
209 if (OCDeleteResource(NotificationSyncResource.handle) != OC_STACK_OK)
211 NS_LOG(ERROR, "Fail to Delete Notification Sync Resource");
215 if (OCDeleteResource(NotificationTopicResource.handle) != OC_STACK_OK)
217 NS_LOG(ERROR, "Fail to Delete Notification Topic Resource");
221 NotificationResource.handle = NULL;
222 NotificationMessageResource.handle = NULL;
223 NotificationSyncResource.handle = NULL;
224 NotificationTopicResource.handle = NULL;
226 NS_LOG(DEBUG, "NSUnRegisterResource - OUT");
230 NSResult NSPutNotificationResource(bool policy, OCResourceHandle * handle)
232 NS_LOG(DEBUG, "NSPutNotificationResource - IN");
234 NotificationResource.policy = policy;
235 OICStrcpy(NotificationResource.providerId, UUID_STRING_SIZE,
236 NSGetProviderInfo()->providerId);
237 NotificationResource.message_uri = NS_COLLECTION_MESSAGE_URI;
238 NotificationResource.sync_uri = NS_COLLECTION_SYNC_URI;
239 NotificationResource.topic_uri = NS_COLLECTION_TOPIC_URI;
240 NotificationResource.version = VERSION;
242 *handle = NotificationResource.handle;
244 NS_LOG(DEBUG, "NSPutNotificationResource - OUT");
248 NSResult NSPutMessageResource(NSMessage *msg, OCResourceHandle * handle)
250 NS_LOG(DEBUG, "NSPutMessageResource - IN");
254 NS_LOG(DEBUG, "NSMessage is valid");
256 NotificationMessageResource.messageId = msg->messageId;
257 OICStrcpy(NotificationMessageResource.providerId, UUID_STRING_SIZE, msg->providerId);
258 NotificationMessageResource.type = msg->type;
259 NotificationMessageResource.dateTime = msg->dateTime;
260 NotificationMessageResource.ttl = msg->ttl;
261 NotificationMessageResource.title = msg->title;
262 NotificationMessageResource.contentText = msg->contentText;
263 NotificationMessageResource.sourceName = msg->sourceName;
264 NotificationMessageResource.topicName = msg->topic;
265 NotificationMessageResource.mediaContents = msg->mediaContents;
269 NS_LOG(ERROR, "NSMessage is NULL");
272 *handle = NotificationMessageResource.handle;
274 NS_LOG(DEBUG, "NSPutMessageResource - OUT");
278 NSResult NSPutSyncResource(NSSyncInfo *sync, OCResourceHandle * handle)
280 NS_LOG(DEBUG, "NSPutSyncResource - IN");
284 *handle = NotificationSyncResource.handle;
286 NS_LOG(DEBUG, "NSPutSyncResource - OUT");
290 NSResult NSPutTopicResource(NSTopicList *topicList, OCResourceHandle * handle)
292 NS_LOG(DEBUG, "NSPutTopicResource - IN");
296 *handle = NotificationTopicResource.handle;
298 NS_LOG(DEBUG, "NSPutTopicResource - OUT");