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;
28 OCStackApplicationResult NSHandlePublishCb(void *ctx, OCDoHandle handle,
29 OCClientResponse *clientResponse)
33 if (ctx != (void *)DEFAULT_CONTEXT_VALUE)
35 NS_LOG(DEBUG, "Invalid publish callback received");
38 NS_LOG_V(DEBUG, "Publish resource response received code: %d", clientResponse->result);
40 return OC_STACK_KEEP_TRANSACTION;
43 NSResult NSPublishResourceToCloud(char *serverAddress)
46 NS_LOG(DEBUG, "NSPublishResourceToCloud - IN");
47 NS_LOG_V(DEBUG, "Cloud address: %s", serverAddress);
49 const char * publishQuery = "/oic/rd?rt=oic.wk.rdpub";
51 if (NSCloudPublish(serverAddress, publishQuery, &NSHandlePublishCb, 1,
52 NotificationResource.handle) != OC_STACK_OK)
54 NS_LOG(DEBUG, "Unable to publish resources to cloud");
57 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.handle = NULL;
77 if (OCCreateResource(&NotificationResource.handle, NS_ROOT_TYPE, NS_DEFAULT_INTERFACE,
78 NS_ROOT_URI, NSEntityHandlerNotificationCb, NULL, OC_DISCOVERABLE) != OC_STACK_OK)
80 NS_LOG(NS_ERROR, "Fail to Create Notification Resource");
84 else if (strcmp(uri, NS_COLLECTION_MESSAGE_URI) == 0)
87 NotificationMessageResource.messageId = 0;
89 (NotificationMessageResource.providerId)[0] = '\0';
90 NotificationMessageResource.type = 0;
91 NotificationMessageResource.dateTime = NULL;
92 NotificationMessageResource.ttl = 0;
93 NotificationMessageResource.title = NULL;
94 NotificationMessageResource.contentText = NULL;
95 NotificationMessageResource.sourceName = NULL;
96 NotificationMessageResource.mediaContents = NULL;
98 if (OCCreateResource(&NotificationMessageResource.handle, NS_COLLECTION_MESSAGE_TYPE,
99 NS_DEFAULT_INTERFACE, NS_COLLECTION_MESSAGE_URI, NSEntityHandlerMessageCb, NULL,
100 OC_OBSERVABLE) != OC_STACK_OK)
102 NS_LOG(NS_ERROR, "Fail to Create Notification Message Resource");
106 else if (strcmp(uri, NS_COLLECTION_SYNC_URI) == 0)
108 NotificationSyncResource.id = NULL;
109 NotificationSyncResource.state = NULL;
110 NotificationSyncResource.handle = NULL;
112 if (OCCreateResource(&(NotificationSyncResource.handle), NS_COLLECTION_SYNC_TYPE,
113 NS_DEFAULT_INTERFACE, NS_COLLECTION_SYNC_URI, NSEntityHandlerSyncCb, NULL,
114 OC_OBSERVABLE) != OC_STACK_OK)
116 NS_LOG(NS_ERROR, "Fail to Create Notification Sync Resource");
122 NS_LOG(ERROR, "Fail to create resource with invalid URI");
126 NS_LOG(DEBUG, "NSCreateResource - OUT");
130 NSResult NSRegisterResource()
132 NS_LOG(DEBUG, "NSRegisterResource - IN");
134 if (NSCreateResource(NS_COLLECTION_SYNC_URI) != NS_OK)
136 NS_LOG(ERROR, "Fail to register Sync Resource");
140 if (NSCreateResource(NS_COLLECTION_MESSAGE_URI) != NS_OK)
142 NS_LOG(ERROR, "Fail to register Message Resource");
146 if (NSCreateResource(NS_ROOT_URI) != NS_OK)
148 NS_LOG(ERROR, "Fail to register Notification Resource");
152 NS_LOG(DEBUG, "NSRegisterResource - OUT");
156 NSResult NSUnRegisterResource()
158 NS_LOG(DEBUG, "NSUnRegisterResource - IN");
160 if (OCDeleteResource(NotificationResource.handle) != OC_STACK_OK)
162 NS_LOG(ERROR, "Fail to Delete Notification Resource");
166 if (OCDeleteResource(NotificationMessageResource.handle) != OC_STACK_OK)
168 NS_LOG(ERROR, "Fail to Delete Notification Message Resource");
172 if (OCDeleteResource(NotificationSyncResource.handle) != OC_STACK_OK)
174 NS_LOG(ERROR, "Fail to Delete Notification Sync Resource");
178 NotificationResource.handle = NULL;
179 NotificationMessageResource.handle = NULL;
180 NotificationSyncResource.handle = NULL;
182 NS_LOG(DEBUG, "NSUnRegisterResource - OUT");
186 NSResult NSPutNotificationResource(int accepter, OCResourceHandle * handle)
188 NS_LOG(DEBUG, "NSPutNotificationResource - IN");
190 NotificationResource.accepter = accepter;
191 NotificationResource.message_uri = NS_COLLECTION_MESSAGE_URI;
192 NotificationResource.sync_uri = NS_COLLECTION_SYNC_URI;
194 *handle = NotificationResource.handle;
196 NS_LOG(DEBUG, "NSPutNotificationResource - OUT");
200 NSResult NSPutMessageResource(NSMessage *msg, OCResourceHandle * handle)
202 NS_LOG(DEBUG, "NSPutMessageResource - IN");
206 NS_LOG(DEBUG, "NSMessage is valid");
208 NotificationMessageResource.messageId = msg->messageId;
209 OICStrcpy(NotificationMessageResource.providerId, UUID_STRING_SIZE, msg->providerId);
210 NotificationMessageResource.type = msg->type;
211 NotificationMessageResource.dateTime = msg->dateTime;
212 NotificationMessageResource.ttl = msg->ttl;
213 NotificationMessageResource.title = msg->title;
214 NotificationMessageResource.contentText = msg->contentText;
215 NotificationMessageResource.sourceName = msg->sourceName;
216 NotificationMessageResource.mediaContents = msg->mediaContents;
220 NS_LOG(ERROR, "NSMessage is NULL");
223 *handle = NotificationMessageResource.handle;
224 NS_LOG(DEBUG, "NSPutMessageResource - OUT");
229 NSResult NSPutSyncResource(NSSyncInfo *sync, OCResourceHandle * handle)
231 NS_LOG(DEBUG, "NSPutSyncResource - IN");
235 *handle = NotificationSyncResource.handle;
237 NS_LOG(DEBUG, "NSPutSyncResource - OUT");