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 char* NSType = "oic.r.notification";
24 char* NSMessageType = "oic.r.notification.message";
25 char* NSSyncType = "oic.r.notification.sync";
27 char* NSInterface = "oic.if.baseline";
28 char* NSMessgeInterface = "oic.if.baseline.message";
29 char* NSSyncInterface = "oic.if.baseline.sync";
31 char* NSUri = "/notification";
32 char* NSMessageUri = "/notification/message";
33 char* NSSyncUri = "/notification/sync";
35 /* Structure to represent notification resources */
38 OCResourceHandle handle;
42 } NSNotificationResource;
46 OCResourceHandle handle;
54 OCResourceHandle handle;
59 NSNotificationResource NotificationResource;
60 NSMessageResource NotificationMessageResource;
61 NSSyncResource NotificationSyncResource;
63 NSResult NSCreateResource(char *uri)
65 NS_LOG(DEBUG, "NSCreateResource - IN");
68 NS_LOG(NS_ERROR, "Resource URI cannot be NULL");
72 if (strcmp(uri, NSUri) == 0)
75 NotificationResource.accepter = 0;
76 NotificationResource.message_uri = NSMessageUri;
77 NotificationResource.sync_uri = NSSyncUri;
78 NotificationResource.handle = NULL;
80 if (OCCreateResource(&NotificationResource.handle, NSType, NSInterface, NSUri,
81 NSEntityHandlerNotificationCb, NULL, OC_DISCOVERABLE) != OC_STACK_OK)
83 NS_LOG(NS_ERROR, "Fail to Create Notification Resource");
87 else if (strcmp(uri, NSMessageUri) == 0)
90 NotificationMessageResource.id = NULL;
91 NotificationMessageResource.title = NULL;
92 NotificationMessageResource.body = NULL;
93 NotificationMessageResource.handle = NULL;
95 if (OCCreateResource(&NotificationMessageResource.handle, NSMessageType, NSInterface,
96 NSMessageUri, NSEntityHandlerMessageCb, NULL, OC_OBSERVABLE) != OC_STACK_OK)
98 NS_LOG(NS_ERROR, "Fail to Create Notification Message Resource");
102 else if (strcmp(uri, NSSyncUri) == 0)
104 NotificationSyncResource.id = NULL;
105 NotificationSyncResource.state = NULL;
106 NotificationSyncResource.handle = NULL;
108 if (OCCreateResource(&(NotificationSyncResource.handle), NSSyncType, NSInterface, NSSyncUri,
109 NSEntityHandlerSyncCb, NULL, OC_OBSERVABLE) != OC_STACK_OK)
111 NS_LOG(NS_ERROR, "Fail to Create Notification Sync Resource");
117 NS_LOG(ERROR, "Fail to create resource with invalid URI");
121 NS_LOG(DEBUG, "NSCreateResource - OUT");
125 NSResult NSRegisterResource()
127 NS_LOG(DEBUG, "NSRegisterResource - IN");
129 if (NSCreateResource(NSSyncUri) != NS_OK)
131 NS_LOG(ERROR, "Fail to register Sync Resource");
135 if (NSCreateResource(NSMessageUri) != NS_OK)
137 NS_LOG(ERROR, "Fail to register Message Resource");
141 if (NSCreateResource(NSUri) != NS_OK)
143 NS_LOG(ERROR, "Fail to register Notification Resource");
147 NS_LOG(DEBUG, "NSRegisterResource - OUT");
151 NSResult NSUnRegisterResource()
153 NS_LOG(DEBUG, "NSUnRegisterResource - IN");
155 if (OCDeleteResource(NotificationResource.handle) != OC_STACK_OK)
157 NS_LOG(ERROR, "Fail to Delete Notification Resource");
161 if (OCDeleteResource(NotificationMessageResource.handle) != OC_STACK_OK)
163 NS_LOG(ERROR, "Fail to Delete Notification Message Resource");
167 if (OCDeleteResource(NotificationSyncResource.handle) != OC_STACK_OK)
169 NS_LOG(ERROR, "Fail to Delete Notification Sync Resource");
173 NS_LOG(DEBUG, "NSUnRegisterResource - OUT");
177 NSResult NSPutNotificationResource(int accepter, OCResourceHandle * handle)
179 NS_LOG(DEBUG, "NSPutNotificationResource - IN");
181 NotificationResource.accepter = accepter;
182 NotificationResource.message_uri = NSMessageUri;
183 NotificationResource.sync_uri = NSSyncUri;
185 *handle = NotificationResource.handle;
187 NS_LOG(DEBUG, "NSPutNotificationResource - OUT");
191 NSResult NSPutMessageResource(NSMessage *msg, OCResourceHandle * handle)
193 NS_LOG(DEBUG, "NSPutMessageResource - IN");
197 NS_LOG(DEBUG, "NSMessage is valid");
199 NotificationMessageResource.id = OICStrdup(msg->mId);
200 NotificationMessageResource.title = OICStrdup(msg->mTitle);
201 NotificationMessageResource.body = OICStrdup(msg->mContentText);
205 NS_LOG(ERROR, "NSMessage is NULL");
208 *handle = NotificationMessageResource.handle;
209 NS_LOG(DEBUG, "NSPutMessageResource - OUT");
214 NSResult NSPutSyncResource(NSSync *sync, OCResourceHandle * handle)
216 NS_LOG(DEBUG, "NSPutSyncResource - IN");
220 *handle = NotificationSyncResource.handle;
222 NS_LOG(DEBUG, "NSPutSyncResource - OUT");
226 const char* NSGetNotificationUri()
231 const char* NSGetNotificationMessageUri()
236 const char* NSGetNotificationSyncUri()
241 NSResult NSCopyString(char** targetString, const char* sourceString)
245 *targetString = (char *) malloc(strlen(sourceString) + 1);
249 strncpy(*targetString, sourceString, (strlen(sourceString) + 1));