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"
23 NSNotificationResource NotificationResource;
24 NSMessageResource NotificationMessageResource;
25 NSSyncResource NotificationSyncResource;
27 OCStackApplicationResult NSHandlePublishCb(void *ctx, OCDoHandle handle,
28 OCClientResponse *clientResponse)
30 if (ctx != (void *)DEFAULT_CONTEXT_VALUE)
32 NS_LOG(DEBUG, "Invalid publish callback received");
35 NS_LOG_V(DEBUG, "Publish resource response received code: %d", clientResponse->result);
37 return OC_STACK_KEEP_TRANSACTION;
40 NSResult NSPublishResourceToCloud(char *serverAddress)
43 NS_LOG(DEBUG, "NSPublishResourceToCloud - IN");
44 NS_LOG_V(DEBUG, "Cloud address: %s", serverAddress);
46 const char * publishQuery = "/oic/rd?rt=oic.wk.rdpub";
48 if (NSCloudPublish(serverAddress, publishQuery, &NSHandlePublishCb, 1,
49 NotificationResource.handle) != OC_STACK_OK)
51 NS_LOG(DEBUG, "Unable to publish resources to cloud");
54 NS_LOG(DEBUG, "NSPublishResourceToCloud - OUT");
58 NSResult NSCreateResource(char *uri)
60 NS_LOG(DEBUG, "NSCreateResource - IN");
63 NS_LOG(NS_ERROR, "Resource URI cannot be NULL");
67 if (strcmp(uri, NS_ROOT_URI) == 0)
69 NotificationResource.accepter = 0;
70 NotificationResource.message_uri = NS_COLLECTION_MESSAGE_URI;
71 NotificationResource.sync_uri = NS_COLLECTION_SYNC_URI;
72 NotificationResource.handle = NULL;
74 if (OCCreateResource(&NotificationResource.handle, NS_ROOT_TYPE, NS_DEFAULT_INTERFACE,
75 NS_ROOT_URI, NSEntityHandlerNotificationCb, NULL, OC_DISCOVERABLE) != OC_STACK_OK)
77 NS_LOG(NS_ERROR, "Fail to Create Notification Resource");
81 else if (strcmp(uri, NS_COLLECTION_MESSAGE_URI) == 0)
84 NotificationMessageResource.messageId = 0;
86 (NotificationMessageResource.providerId)[0] = '\0';
87 NotificationMessageResource.type = 0;
88 NotificationMessageResource.dateTime = NULL;
89 NotificationMessageResource.ttl = 0;
90 NotificationMessageResource.title = NULL;
91 NotificationMessageResource.contentText = NULL;
92 NotificationMessageResource.sourceName = NULL;
93 NotificationMessageResource.mediaContents = NULL;
95 if (OCCreateResource(&NotificationMessageResource.handle, NS_COLLECTION_MESSAGE_TYPE,
96 NS_DEFAULT_INTERFACE, NS_COLLECTION_MESSAGE_URI, NSEntityHandlerMessageCb, NULL,
97 OC_OBSERVABLE) != OC_STACK_OK)
99 NS_LOG(NS_ERROR, "Fail to Create Notification Message Resource");
103 else if (strcmp(uri, NS_COLLECTION_SYNC_URI) == 0)
105 NotificationSyncResource.id = NULL;
106 NotificationSyncResource.state = NULL;
107 NotificationSyncResource.handle = NULL;
109 if (OCCreateResource(&(NotificationSyncResource.handle), NS_COLLECTION_SYNC_TYPE,
110 NS_DEFAULT_INTERFACE, NS_COLLECTION_SYNC_URI, NSEntityHandlerSyncCb, NULL,
111 OC_OBSERVABLE) != OC_STACK_OK)
113 NS_LOG(NS_ERROR, "Fail to Create Notification Sync Resource");
119 NS_LOG(ERROR, "Fail to create resource with invalid URI");
123 NS_LOG(DEBUG, "NSCreateResource - OUT");
127 NSResult NSRegisterResource()
129 NS_LOG(DEBUG, "NSRegisterResource - IN");
131 if (NSCreateResource(NS_COLLECTION_SYNC_URI) != NS_OK)
133 NS_LOG(ERROR, "Fail to register Sync Resource");
137 if (NSCreateResource(NS_COLLECTION_MESSAGE_URI) != NS_OK)
139 NS_LOG(ERROR, "Fail to register Message Resource");
143 if (NSCreateResource(NS_ROOT_URI) != NS_OK)
145 NS_LOG(ERROR, "Fail to register Notification Resource");
149 NS_LOG(DEBUG, "NSRegisterResource - OUT");
153 NSResult NSUnRegisterResource()
155 NS_LOG(DEBUG, "NSUnRegisterResource - IN");
157 if (OCDeleteResource(NotificationResource.handle) != OC_STACK_OK)
159 NS_LOG(ERROR, "Fail to Delete Notification Resource");
163 if (OCDeleteResource(NotificationMessageResource.handle) != OC_STACK_OK)
165 NS_LOG(ERROR, "Fail to Delete Notification Message Resource");
169 if (OCDeleteResource(NotificationSyncResource.handle) != OC_STACK_OK)
171 NS_LOG(ERROR, "Fail to Delete Notification Sync Resource");
175 NotificationResource.handle = NULL;
176 NotificationMessageResource.handle = NULL;
177 NotificationSyncResource.handle = NULL;
179 NS_LOG(DEBUG, "NSUnRegisterResource - OUT");
183 NSResult NSPutNotificationResource(int accepter, OCResourceHandle * handle)
185 NS_LOG(DEBUG, "NSPutNotificationResource - IN");
187 NotificationResource.accepter = accepter;
188 NotificationResource.message_uri = NS_COLLECTION_MESSAGE_URI;
189 NotificationResource.sync_uri = NS_COLLECTION_SYNC_URI;
191 *handle = NotificationResource.handle;
193 NS_LOG(DEBUG, "NSPutNotificationResource - OUT");
197 NSResult NSPutMessageResource(NSMessage *msg, OCResourceHandle * handle)
199 NS_LOG(DEBUG, "NSPutMessageResource - IN");
203 NS_LOG(DEBUG, "NSMessage is valid");
205 NotificationMessageResource.messageId = msg->messageId;
206 OICStrcpy(NotificationMessageResource.providerId, UUID_STRING_SIZE, msg->providerId);
207 NotificationMessageResource.type = msg->type;
208 NotificationMessageResource.dateTime = msg->dateTime;
209 NotificationMessageResource.ttl = msg->ttl;
210 NotificationMessageResource.title = msg->title;
211 NotificationMessageResource.contentText = msg->contentText;
212 NotificationMessageResource.sourceName = msg->sourceName;
213 NotificationMessageResource.mediaContents = msg->mediaContents;
217 NS_LOG(ERROR, "NSMessage is NULL");
220 *handle = NotificationMessageResource.handle;
221 NS_LOG(DEBUG, "NSPutMessageResource - OUT");
226 NSResult NSPutSyncResource(NSSyncInfo *sync, OCResourceHandle * handle)
228 NS_LOG(DEBUG, "NSPutSyncResource - IN");
232 *handle = NotificationSyncResource.handle;
234 NS_LOG(DEBUG, "NSPutSyncResource - OUT");