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 OCStackApplicationResult NSHandlePublishCb(void *ctx, OCDoHandle handle,
31 OCClientResponse *clientResponse)
36 NS_LOG_V(DEBUG, "Publish resource response received code: %d", clientResponse->result);
38 return OC_STACK_KEEP_TRANSACTION;
41 NSResult NSPublishResourceToCloud(char *serverAddress)
44 NS_LOG(DEBUG, "NSPublishResourceToCloud - IN");
45 NS_LOG_V(DEBUG, "Remote Server Address: %s", serverAddress);
47 OCResourceHandle resourceHandles[1] = {NotificationResource.handle};
48 OCStackResult res = OCRDPublish(serverAddress, CT_ADAPTER_TCP, resourceHandles, 1,
49 &NSHandlePublishCb, OC_LOW_QOS);
51 if (res != OC_STACK_OK)
53 NS_LOG(DEBUG, "Unable to publish resources to cloud");
56 NS_LOG(DEBUG, "NSPublishResourceToCloud - OUT");
61 NSResult NSCreateResource(char *uri)
63 NS_LOG(DEBUG, "NSCreateResource - IN");
66 NS_LOG(NS_ERROR, "Resource URI cannot be NULL");
70 if (strcmp(uri, NS_ROOT_URI) == 0)
72 NotificationResource.accepter = 0;
73 NotificationResource.message_uri = NS_COLLECTION_MESSAGE_URI;
74 NotificationResource.sync_uri = NS_COLLECTION_SYNC_URI;
75 NotificationResource.topic_uri = NS_COLLECTION_TOPIC_URI;
76 NotificationResource.handle = NULL;
78 if (OCCreateResource(&NotificationResource.handle, NS_ROOT_TYPE, NS_DEFAULT_INTERFACE,
79 NS_ROOT_URI, NSEntityHandlerNotificationCb, NULL, OC_DISCOVERABLE) != OC_STACK_OK)
81 NS_LOG(NS_ERROR, "Fail to Create Notification Resource");
85 else if (strcmp(uri, NS_COLLECTION_MESSAGE_URI) == 0)
88 NotificationMessageResource.messageId = 0;
90 (NotificationMessageResource.providerId)[0] = '\0';
91 NotificationMessageResource.type = 0;
92 NotificationMessageResource.dateTime = NULL;
93 NotificationMessageResource.ttl = 0;
94 NotificationMessageResource.title = NULL;
95 NotificationMessageResource.contentText = NULL;
96 NotificationMessageResource.sourceName = NULL;
97 NotificationMessageResource.mediaContents = NULL;
99 if (OCCreateResource(&NotificationMessageResource.handle, NS_COLLECTION_MESSAGE_TYPE,
100 NS_DEFAULT_INTERFACE, NS_COLLECTION_MESSAGE_URI, NSEntityHandlerMessageCb, NULL,
101 OC_OBSERVABLE) != OC_STACK_OK)
103 NS_LOG(NS_ERROR, "Fail to Create Notification Message Resource");
107 else if (strcmp(uri, NS_COLLECTION_SYNC_URI) == 0)
109 NotificationSyncResource.id = NULL;
110 NotificationSyncResource.state = NULL;
111 NotificationSyncResource.handle = NULL;
113 if (OCCreateResource(&(NotificationSyncResource.handle), NS_COLLECTION_SYNC_TYPE,
114 NS_DEFAULT_INTERFACE, NS_COLLECTION_SYNC_URI, NSEntityHandlerSyncCb, NULL,
115 OC_OBSERVABLE) != OC_STACK_OK)
117 NS_LOG(NS_ERROR, "Fail to Create Notification Sync Resource");
121 else if (strcmp(uri, NS_COLLECTION_TOPIC_URI) == 0)
123 (NotificationTopicResource.consumerId)[0] = '\0';
124 NotificationTopicResource.TopicList = NULL;
125 NotificationTopicResource.handle = NULL;
127 if (OCCreateResource(&(NotificationTopicResource.handle), NS_COLLECTION_TOPIC_TYPE,
128 NS_DEFAULT_INTERFACE, NS_COLLECTION_TOPIC_URI, NSEntityHandlerTopicCb, NULL,
129 OC_DISCOVERABLE) != OC_STACK_OK)
131 NS_LOG(NS_ERROR, "Fail to Create Notification Sync Resource");
137 NS_LOG(ERROR, "Fail to create resource with invalid URI");
141 NS_LOG(DEBUG, "NSCreateResource - OUT");
145 NSResult NSRegisterResource()
147 NS_LOG(DEBUG, "NSRegisterResource - IN");
149 if (NSCreateResource(NS_COLLECTION_TOPIC_URI) != NS_OK)
151 NS_LOG(ERROR, "Fail to register Topic Resource");
155 if (NSCreateResource(NS_COLLECTION_SYNC_URI) != NS_OK)
157 NS_LOG(ERROR, "Fail to register Sync Resource");
161 if (NSCreateResource(NS_COLLECTION_MESSAGE_URI) != NS_OK)
163 NS_LOG(ERROR, "Fail to register Message Resource");
167 if (NSCreateResource(NS_ROOT_URI) != NS_OK)
169 NS_LOG(ERROR, "Fail to register Notification Resource");
173 NS_LOG(DEBUG, "NSRegisterResource - OUT");
177 NSResult NSUnRegisterResource()
179 NS_LOG(DEBUG, "NSUnRegisterResource - IN");
181 if (OCDeleteResource(NotificationResource.handle) != OC_STACK_OK)
183 NS_LOG(ERROR, "Fail to Delete Notification Resource");
187 if (OCDeleteResource(NotificationMessageResource.handle) != OC_STACK_OK)
189 NS_LOG(ERROR, "Fail to Delete Notification Message Resource");
193 if (OCDeleteResource(NotificationSyncResource.handle) != OC_STACK_OK)
195 NS_LOG(ERROR, "Fail to Delete Notification Sync Resource");
199 if (OCDeleteResource(NotificationTopicResource.handle) != OC_STACK_OK)
201 NS_LOG(ERROR, "Fail to Delete Notification Topic Resource");
205 NotificationResource.handle = NULL;
206 NotificationMessageResource.handle = NULL;
207 NotificationSyncResource.handle = NULL;
208 NotificationTopicResource.handle = NULL;
210 NS_LOG(DEBUG, "NSUnRegisterResource - OUT");
214 NSResult NSPutNotificationResource(int accepter, OCResourceHandle * handle)
216 NS_LOG(DEBUG, "NSPutNotificationResource - IN");
218 NotificationResource.accepter = accepter;
219 NotificationResource.message_uri = NS_COLLECTION_MESSAGE_URI;
220 NotificationResource.sync_uri = NS_COLLECTION_SYNC_URI;
221 NotificationResource.topic_uri = NS_COLLECTION_TOPIC_URI;
223 *handle = NotificationResource.handle;
225 NS_LOG(DEBUG, "NSPutNotificationResource - OUT");
229 NSResult NSPutMessageResource(NSMessage *msg, OCResourceHandle * handle)
231 NS_LOG(DEBUG, "NSPutMessageResource - IN");
235 NS_LOG(DEBUG, "NSMessage is valid");
237 NotificationMessageResource.messageId = msg->messageId;
238 OICStrcpy(NotificationMessageResource.providerId, UUID_STRING_SIZE, msg->providerId);
239 NotificationMessageResource.type = msg->type;
240 NotificationMessageResource.dateTime = msg->dateTime;
241 NotificationMessageResource.ttl = msg->ttl;
242 NotificationMessageResource.title = msg->title;
243 NotificationMessageResource.contentText = msg->contentText;
244 NotificationMessageResource.sourceName = msg->sourceName;
245 NotificationMessageResource.mediaContents = msg->mediaContents;
249 NS_LOG(ERROR, "NSMessage is NULL");
252 *handle = NotificationMessageResource.handle;
253 NS_LOG(DEBUG, "NSPutMessageResource - OUT");
258 NSResult NSPutSyncResource(NSSyncInfo *sync, OCResourceHandle * handle)
260 NS_LOG(DEBUG, "NSPutSyncResource - IN");
264 *handle = NotificationSyncResource.handle;
266 NS_LOG(DEBUG, "NSPutSyncResource - OUT");
270 NSResult NSPutTopicResource(NSTopicList *topicList, OCResourceHandle * handle)
272 NS_LOG(DEBUG, "NSPutTopicResource - IN");
276 *handle = NotificationTopicResource.handle;
278 NS_LOG(DEBUG, "NSPutTopicResource - OUT");