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;
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.accepter = 0;
84 NotificationResource.message_uri = NS_COLLECTION_MESSAGE_URI;
85 NotificationResource.sync_uri = NS_COLLECTION_SYNC_URI;
86 NotificationResource.topic_uri = NS_COLLECTION_TOPIC_URI;
87 NotificationResource.handle = NULL;
89 if (OCCreateResource(&NotificationResource.handle, NS_ROOT_TYPE, NS_DEFAULT_INTERFACE,
90 NS_ROOT_URI, NSEntityHandlerNotificationCb, NULL, OC_DISCOVERABLE) != OC_STACK_OK)
92 NS_LOG(NS_ERROR, "Fail to Create Notification Resource");
96 else if (strcmp(uri, NS_COLLECTION_MESSAGE_URI) == 0)
99 NotificationMessageResource.messageId = 0;
101 (NotificationMessageResource.providerId)[0] = '\0';
102 NotificationMessageResource.type = 0;
103 NotificationMessageResource.dateTime = NULL;
104 NotificationMessageResource.ttl = 0;
105 NotificationMessageResource.title = NULL;
106 NotificationMessageResource.contentText = NULL;
107 NotificationMessageResource.sourceName = NULL;
108 NotificationMessageResource.mediaContents = NULL;
110 if (OCCreateResource(&NotificationMessageResource.handle, NS_COLLECTION_MESSAGE_TYPE,
111 NS_DEFAULT_INTERFACE, NS_COLLECTION_MESSAGE_URI, NSEntityHandlerMessageCb, NULL,
112 OC_OBSERVABLE) != OC_STACK_OK)
114 NS_LOG(NS_ERROR, "Fail to Create Notification Message Resource");
118 else if (strcmp(uri, NS_COLLECTION_SYNC_URI) == 0)
120 NotificationSyncResource.id = NULL;
121 NotificationSyncResource.state = NULL;
122 NotificationSyncResource.handle = NULL;
124 if (OCCreateResource(&(NotificationSyncResource.handle), NS_COLLECTION_SYNC_TYPE,
125 NS_DEFAULT_INTERFACE, NS_COLLECTION_SYNC_URI, NSEntityHandlerSyncCb, NULL,
126 OC_OBSERVABLE) != OC_STACK_OK)
128 NS_LOG(NS_ERROR, "Fail to Create Notification Sync Resource");
132 else if (strcmp(uri, NS_COLLECTION_TOPIC_URI) == 0)
134 (NotificationTopicResource.consumerId)[0] = '\0';
135 NotificationTopicResource.TopicList = NULL;
136 NotificationTopicResource.handle = NULL;
138 if (OCCreateResource(&(NotificationTopicResource.handle), NS_COLLECTION_TOPIC_TYPE,
139 NS_DEFAULT_INTERFACE, NS_COLLECTION_TOPIC_URI, NSEntityHandlerTopicCb, NULL,
140 OC_DISCOVERABLE) != OC_STACK_OK)
142 NS_LOG(NS_ERROR, "Fail to Create Notification Sync Resource");
148 NS_LOG(ERROR, "Fail to create resource with invalid URI");
152 NS_LOG(DEBUG, "NSCreateResource - OUT");
156 NSResult NSRegisterResource()
158 NS_LOG(DEBUG, "NSRegisterResource - IN");
160 if (NSCreateResource(NS_COLLECTION_TOPIC_URI) != NS_OK)
162 NS_LOG(ERROR, "Fail to register Topic Resource");
166 if (NSCreateResource(NS_COLLECTION_SYNC_URI) != NS_OK)
168 NS_LOG(ERROR, "Fail to register Sync Resource");
172 if (NSCreateResource(NS_COLLECTION_MESSAGE_URI) != NS_OK)
174 NS_LOG(ERROR, "Fail to register Message Resource");
178 if (NSCreateResource(NS_ROOT_URI) != NS_OK)
180 NS_LOG(ERROR, "Fail to register Notification Resource");
184 NS_LOG(DEBUG, "NSRegisterResource - OUT");
188 NSResult NSUnRegisterResource()
190 NS_LOG(DEBUG, "NSUnRegisterResource - IN");
192 if (OCDeleteResource(NotificationResource.handle) != OC_STACK_OK)
194 NS_LOG(ERROR, "Fail to Delete Notification Resource");
198 if (OCDeleteResource(NotificationMessageResource.handle) != OC_STACK_OK)
200 NS_LOG(ERROR, "Fail to Delete Notification Message Resource");
204 if (OCDeleteResource(NotificationSyncResource.handle) != OC_STACK_OK)
206 NS_LOG(ERROR, "Fail to Delete Notification Sync Resource");
210 if (OCDeleteResource(NotificationTopicResource.handle) != OC_STACK_OK)
212 NS_LOG(ERROR, "Fail to Delete Notification Topic Resource");
216 NotificationResource.handle = NULL;
217 NotificationMessageResource.handle = NULL;
218 NotificationSyncResource.handle = NULL;
219 NotificationTopicResource.handle = NULL;
221 NS_LOG(DEBUG, "NSUnRegisterResource - OUT");
225 NSResult NSPutNotificationResource(int accepter, OCResourceHandle * handle)
227 NS_LOG(DEBUG, "NSPutNotificationResource - IN");
229 NotificationResource.accepter = accepter;
230 NotificationResource.message_uri = NS_COLLECTION_MESSAGE_URI;
231 NotificationResource.sync_uri = NS_COLLECTION_SYNC_URI;
232 NotificationResource.topic_uri = NS_COLLECTION_TOPIC_URI;
234 *handle = NotificationResource.handle;
236 NS_LOG(DEBUG, "NSPutNotificationResource - OUT");
240 NSResult NSPutMessageResource(NSMessage *msg, OCResourceHandle * handle)
242 NS_LOG(DEBUG, "NSPutMessageResource - IN");
246 NS_LOG(DEBUG, "NSMessage is valid");
248 NotificationMessageResource.messageId = msg->messageId;
249 OICStrcpy(NotificationMessageResource.providerId, UUID_STRING_SIZE, msg->providerId);
250 NotificationMessageResource.type = msg->type;
251 NotificationMessageResource.dateTime = msg->dateTime;
252 NotificationMessageResource.ttl = msg->ttl;
253 NotificationMessageResource.title = msg->title;
254 NotificationMessageResource.contentText = msg->contentText;
255 NotificationMessageResource.sourceName = msg->sourceName;
256 NotificationMessageResource.mediaContents = msg->mediaContents;
260 NS_LOG(ERROR, "NSMessage is NULL");
263 *handle = NotificationMessageResource.handle;
264 NS_LOG(DEBUG, "NSPutMessageResource - OUT");
269 NSResult NSPutSyncResource(NSSyncInfo *sync, OCResourceHandle * handle)
271 NS_LOG(DEBUG, "NSPutSyncResource - IN");
275 *handle = NotificationSyncResource.handle;
277 NS_LOG(DEBUG, "NSPutSyncResource - OUT");
281 NSResult NSPutTopicResource(NSTopicList *topicList, OCResourceHandle * handle)
283 NS_LOG(DEBUG, "NSPutTopicResource - IN");
287 *handle = NotificationTopicResource.handle;
289 NS_LOG(DEBUG, "NSPutTopicResource - OUT");