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");
166 NSCreateResource(NS_COLLECTION_TOPIC_URI), "Fail to register Topic Resource");
168 NSCreateResource(NS_COLLECTION_SYNC_URI), "Fail to register Sync Resource");
170 NSCreateResource(NS_COLLECTION_MESSAGE_URI), "Fail to register Message Resource");
172 NSCreateResource(NS_ROOT_URI), "Fail to register Notification Resource");
174 NS_LOG(DEBUG, "NSRegisterResource - OUT");
178 NSResult NSUnRegisterResource()
180 NS_LOG(DEBUG, "NSUnRegisterResource - IN");
183 OCDeleteResource(NotificationResource.handle), "Fail to Delete Notification Resource");
184 NS_DELETE_RESOURCE(OCDeleteResource(NotificationMessageResource.handle),
185 "Fail to Delete Notification Message Resource");
186 NS_DELETE_RESOURCE(OCDeleteResource(NotificationSyncResource.handle),
187 "Fail to Delete Notification Sync Resource");
188 NS_DELETE_RESOURCE(OCDeleteResource(NotificationTopicResource.handle),
189 "Fail to Delete Notification Topic Resource");
191 NotificationResource.handle = NULL;
192 NotificationMessageResource.handle = NULL;
193 NotificationSyncResource.handle = NULL;
194 NotificationTopicResource.handle = NULL;
196 NS_LOG(DEBUG, "NSUnRegisterResource - OUT");
200 NSResult NSPutNotificationResource(bool policy, OCResourceHandle * handle)
202 NS_LOG(DEBUG, "NSPutNotificationResource - IN");
204 NotificationResource.policy = policy;
205 OICStrcpy(NotificationResource.providerId, UUID_STRING_SIZE,
206 NSGetProviderInfo()->providerId);
207 NotificationResource.message_uri = NS_COLLECTION_MESSAGE_URI;
208 NotificationResource.sync_uri = NS_COLLECTION_SYNC_URI;
209 NotificationResource.topic_uri = NS_COLLECTION_TOPIC_URI;
210 NotificationResource.version = VERSION;
212 *handle = NotificationResource.handle;
214 NS_LOG(DEBUG, "NSPutNotificationResource - OUT");
218 NSResult NSPutMessageResource(NSMessage *msg, OCResourceHandle * handle)
220 NS_LOG(DEBUG, "NSPutMessageResource - IN");
224 NS_LOG(DEBUG, "NSMessage is valid");
226 NotificationMessageResource.messageId = msg->messageId;
227 OICStrcpy(NotificationMessageResource.providerId, UUID_STRING_SIZE, msg->providerId);
228 NotificationMessageResource.type = msg->type;
229 NotificationMessageResource.dateTime = msg->dateTime;
230 NotificationMessageResource.ttl = msg->ttl;
231 NotificationMessageResource.title = msg->title;
232 NotificationMessageResource.contentText = msg->contentText;
233 NotificationMessageResource.sourceName = msg->sourceName;
234 NotificationMessageResource.topicName = msg->topic;
235 NotificationMessageResource.mediaContents = msg->mediaContents;
239 NS_LOG(ERROR, "NSMessage is NULL");
242 *handle = NotificationMessageResource.handle;
244 NS_LOG(DEBUG, "NSPutMessageResource - OUT");
248 NSResult NSPutSyncResource(NSSyncInfo *sync, OCResourceHandle * handle)
250 NS_LOG(DEBUG, "NSPutSyncResource - IN");
254 *handle = NotificationSyncResource.handle;
256 NS_LOG(DEBUG, "NSPutSyncResource - OUT");
260 NSResult NSPutTopicResource(NSTopicList *topicList, OCResourceHandle * handle)
262 NS_LOG(DEBUG, "NSPutTopicResource - IN");
266 *handle = NotificationTopicResource.handle;
268 NS_LOG(DEBUG, "NSPutTopicResource - OUT");