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;
60 NSNotificationResource NotificationResource;
61 NSMessageResource NotificationMessageResource;
62 NSSyncResource NotificationSyncResource;
64 NSResult NSCreateResource(char *uri)
66 NS_LOG(DEBUG, "NSCreateResource - IN");
69 OIC_LOG(ERROR, RESOURCE_TAG, "Resource URI cannot be NULL");
70 NS_LOG(NS_ERROR, "Resource URI cannot be NULL");
74 if (strcmp(uri, NSUri) == 0)
77 NotificationResource.accepter = 0;
78 NotificationResource.message_uri = NSMessageUri;
79 NotificationResource.sync_uri = NSSyncUri;
80 NotificationResource.handle = NULL;
82 if (OCCreateResource(&NotificationResource.handle, NSType, NSInterface, NSUri,
83 NSEntityHandlerNotificationCb, NULL, OC_DISCOVERABLE) != OC_STACK_OK)
85 OIC_LOG(ERROR, RESOURCE_TAG, PCF("Fail to Create Notification Resource"));
86 NS_LOG(NS_ERROR, "Fail to Create Notification Resource");
90 else if (strcmp(uri, NSMessageUri) == 0)
93 NotificationMessageResource.id = NULL;
94 NotificationMessageResource.title = NULL;
95 NotificationMessageResource.body = NULL;
96 NotificationMessageResource.handle = NULL;
98 if (OCCreateResource(&NotificationMessageResource.handle, NSMessageType, NSInterface,
99 NSMessageUri, NSEntityHandlerMessageCb, NULL, OC_OBSERVABLE) != OC_STACK_OK)
101 OIC_LOG(ERROR, RESOURCE_TAG, PCF("Fail to Create Notification Message Resource"));
102 NS_LOG(NS_ERROR, "Fail to Create Notification Message Resource");
106 else if (strcmp(uri, NSSyncUri) == 0)
108 NotificationSyncResource.id = NULL;
109 NotificationSyncResource.state = NULL;
110 memset(&NotificationSyncResource.device, 0, sizeof(NSDevice));
111 NotificationSyncResource.handle = NULL;
113 if (OCCreateResource(&(NotificationSyncResource.handle), NSSyncType, NSInterface, NSSyncUri,
114 NSEntityHandlerSyncCb, NULL, OC_OBSERVABLE) != OC_STACK_OK)
116 OIC_LOG(ERROR, RESOURCE_TAG, PCF("Fail to Create Notification Sync Resource"));
117 NS_LOG(NS_ERROR, "Fail to Create Notification Sync Resource");
123 OIC_LOG(ERROR, RESOURCE_TAG, PCF("Fail to create resource with invalid URI"));
124 NS_LOG(DEBUG, "Fail to create resource with invalid URI");
128 NS_LOG(DEBUG, "NSCreateResource - OUT");
132 NSResult NSRegisterResource()
134 OIC_LOG(INFO, RESOURCE_TAG, "NSRegisterResource");
135 NS_LOG(DEBUG, "NSRegisterResource - IN");
137 if (NSCreateResource(NSSyncUri) != NS_OK)
139 OIC_LOG(ERROR, RESOURCE_TAG, PCF("Fail to register Sync Resource"));
140 NS_LOG(DEBUG, "Fail to register Sync Resource");
144 if (NSCreateResource(NSMessageUri) != NS_OK)
146 OIC_LOG(ERROR, RESOURCE_TAG, PCF("Fail to register Message Resource"));
147 NS_LOG(DEBUG, "Fail to register Message Resource");
151 if (NSCreateResource(NSUri) != NS_OK)
153 OIC_LOG(ERROR, RESOURCE_TAG, PCF("Fail to register Notification Resource"));
154 NS_LOG(DEBUG, "Fail to register Notification Resource");
158 NS_LOG(DEBUG, "NSRegisterResource - OUT");
162 NSResult NSPutNotificationResource(int accepter, OCResourceHandle * handle)
164 NS_LOG(DEBUG, "NSPutNotificationResource - IN");
166 NotificationResource.accepter = accepter;
167 NotificationResource.message_uri = NSMessageUri;
168 NotificationResource.sync_uri = NSSyncUri;
170 *handle = NotificationResource.handle;
172 NS_LOG(DEBUG, "NSPutNotificationResource - OUT");
176 NSResult NSPutMessageResource(NSMessage *msg, OCResourceHandle * handle)
178 OIC_LOG(INFO, RESOURCE_TAG, "Put notification message to Resource");
179 NS_LOG(DEBUG, "NSPutMessageResource - IN");
183 NS_LOG(ERROR, "NSMessage is valid");
184 NotificationMessageResource.id = strdup(msg->mId);
185 NotificationMessageResource.title = strdup(msg->mTitle);
186 NotificationMessageResource.body = strdup(msg->mContentText);
190 NS_LOG(ERROR, "NSMessage is NULL");
193 *handle = NotificationMessageResource.handle;
194 NS_LOG(DEBUG, "NSPutMessageResource - OUT");
199 NSResult NSPutSyncResource(NSSync *sync, OCResourceHandle * handle)
201 OIC_LOG(INFO, RESOURCE_TAG, "Put notification sync to Resource");
202 NS_LOG(DEBUG, "NSPutSyncResource - IN");
206 *handle = NotificationSyncResource.handle;
208 NS_LOG(DEBUG, "NSPutSyncResource - OUT");
212 const char* NSGetNotificationUri()
217 const char* NSGetNotificationMessageUri()
222 const char* NSGetNotificationSyncUri()
227 NSResult NSCopyString(char** targetString, const char* sourceString)
231 *targetString = (char *) malloc(strlen(sourceString) + 1);
235 strncpy(*targetString, sourceString, (strlen(sourceString) + 1));