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 "NSConsumerNotification.h"
23 #include "NSConstants.h"
24 #include "NSConsumerCommon.h"
25 #include "oic_malloc.h"
26 #include "oic_string.h"
27 #include "ocpayload.h"
29 NSMessage_consumer * NSBuildOICNotification(OCClientResponse * clientResponse);
30 NSSync * NSBuildOICNotificationSync(OCClientResponse * clientResponse);
32 NSProvider * NSGetProvider(OCClientResponse * clientResponse);
33 NSResult NSPushToCache(OCClientResponse * clientResponse, NSTaskType type);
35 NSResult NSConsumerSubscribeProvider(NSProvider * provider)
37 if (OC_STACK_OK != NSRequestToResourceIntrospection(&(provider->messageHandle),
38 OC_REST_OBSERVE, (OCDevAddr *) provider->mUserData,
39 provider->messageUri, NULL, NSConsumerNotificationListener))
44 if (OC_STACK_OK != NSRequestToResourceIntrospection(&(provider->syncHandle),
45 OC_REST_OBSERVE, (OCDevAddr *) provider->mUserData,
46 provider->syncUri, NULL, NSConsumerSyncListener))
54 NSResult NSConsumerPostProvider(OCDevAddr * addr, OCPayload * payload, const char * uri)
57 if (OC_STACK_OK != NSRequestToResourceIntrospection(NULL, OC_REST_POST, addr,
66 OCStackApplicationResult NSConsumerSyncListener(
67 OCDoHandle handle, OCClientResponse * clientResponse)
71 NSSync * newNoti = NULL;
72 NSProvider * provider = NSGetProvider(clientResponse);
75 NS_LOG(ERROR, "getting provider is failed");
76 return OC_STACK_KEEP_TRANSACTION;
79 newNoti = NSBuildOICNotificationSync(clientResponse);
82 return OC_STACK_KEEP_TRANSACTION;
85 NSTaskType taskType = TASK_RECV_READ;
87 if (newNoti->mState != Notification_Read)
89 NS_LOG(DEBUG, "newNoti->type : Dismiss");
90 taskType = TASK_RECV_DISMISS;
94 NS_LOG(DEBUG, "newNoti->type : Read");
97 NSNotificationSync(provider, newNoti);
99 if (NS_OK != NSPushToCache(clientResponse, taskType))
101 return OC_STACK_KEEP_TRANSACTION;
104 return OC_STACK_KEEP_TRANSACTION;
107 OCStackApplicationResult NSConsumerNotificationListener(
108 OCDoHandle handle, OCClientResponse * clientResponse)
112 NSProvider * provider = NSGetProvider(clientResponse);
115 NS_LOG(ERROR, "getting provider is failed");
116 return OC_STACK_KEEP_TRANSACTION;
119 NSMessage_consumer * newNoti = NSBuildOICNotification(clientResponse);
122 return OC_STACK_KEEP_TRANSACTION;
125 NSTaskType taskType = TASK_CONSUMER_RECV_NOTIFICATION;
127 NS_LOG(DEBUG, "newNoti->type == Notification");
128 NSNotificationPost(provider, (NSMessage *) newNoti);
130 if (NS_OK != NSPushToCache(clientResponse, taskType))
132 NSRemoveMessage(newNoti);
133 return OC_STACK_KEEP_TRANSACTION;
136 return OC_STACK_KEEP_TRANSACTION;
139 NSResult NSPushToCache(OCClientResponse * clientResponse, NSTaskType type)
141 NSMessage_consumer * cachedNoti = NSBuildOICNotification(clientResponse);
146 NSTask * task = NSMakeTask(type, (void *) cachedNoti);
149 NS_LOG(ERROR, "NSTask allocation fail");
150 NSRemoveMessage(cachedNoti);
153 NSConsumerPushEvent(task);
158 NSMessage_consumer * NSBuildOICNotification(OCClientResponse * clientResponse)
160 if(!clientResponse->payload)
165 NSMessage_consumer * retNoti = (NSMessage_consumer *)OICMalloc(sizeof(NSMessage_consumer));
172 retNoti->mTitle = NULL;
173 retNoti->mContentText = NULL;
175 OCRepPayload * payload = (OCRepPayload *)clientResponse->payload;
176 if (!OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_ID, &retNoti->mId))
178 NS_LOG(ERROR, "id of received notification is null");
183 OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_TITLE, &retNoti->mTitle);
184 OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_TEXT, &retNoti->mContentText);
186 NS_LOG_V(DEBUG, "Msg Address : %s", clientResponse->addr->addr);
187 NS_LOG_V(DEBUG, "Msg ID : %s", retNoti->mId);
188 NS_LOG_V(DEBUG, "Msg Title : %s", retNoti->mTitle);
189 NS_LOG_V(DEBUG, "Msg Content : %s", retNoti->mContentText);
191 retNoti->addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr));
192 memcpy(retNoti->addr, clientResponse->addr, sizeof(OCDevAddr));
194 retNoti->type = Notification;
199 NSSync * NSBuildOICNotificationSync(OCClientResponse * clientResponse)
201 if(!clientResponse->payload)
205 NSSync * retSync = (NSSync *)OICMalloc(sizeof(NSSync));
211 retSync->mMessageId = NULL;
212 retSync->mState = Notification_Read;
214 OCRepPayload * payload = (OCRepPayload *)clientResponse->payload;
215 if (!OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_ID, &retSync->mMessageId))
217 NS_LOG(ERROR, "id of received sync is null");
222 if (!OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_STATE, & state))
224 NS_LOG(ERROR, "state of received sync is null");
225 OICFree(retSync->mMessageId);
230 retSync->mState = (NSSyncTypes) state;
232 NS_LOG_V(DEBUG, "Sync ID : %s", retSync->mMessageId);
233 NS_LOG_V(DEBUG, "Sync State : %d", (int) retSync->mState);
238 NSProvider * NSGetProvider(OCClientResponse * clientResponse)
240 NSProvider * newProvider = (NSProvider *)OICMalloc(sizeof(NSProvider));
243 NS_LOG(DEBUG, "NSProvider allocation fail");
248 newProvider->mId = NULL;
249 newProvider->mUserData = (void *)OICMalloc(sizeof(OCDevAddr));
250 if (!newProvider->mUserData)
252 NS_LOG(DEBUG, "OCDevAddr allocation fail");
253 OICFree(newProvider);
256 memcpy(newProvider->mUserData, clientResponse->addr, sizeof(OCDevAddr));
261 void NSConsumerNotificationHandleMsg(NSTask * task)
265 NS_LOG(ERROR, "task is null");
269 NS_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType);
270 if (task->taskType == TASK_CONSUMER_REQ_SUBSCRIBE)
272 if (NS_OK != NSConsumerSubscribeProvider((NSProvider *)task->taskData))
274 NS_LOG(ERROR, "Subscribe fail");
278 else if (task->taskType == TASK_SEND_READ || task->taskType == TASK_SEND_DISMISS)
280 NSMessage_consumer * nsConsumer = (NSMessage_consumer *) task->taskData;
283 NS_LOG(ERROR, "taskData is NULL");
287 OCRepPayload * payload = OCRepPayloadCreate ();
290 NS_LOG(ERROR, "Failed to create POST payload object");
294 int type = (task->taskType == TASK_SEND_READ) ? 0 : 1;
295 OCRepPayloadSetPropString(payload, "ID", (char *) nsConsumer->mId);
296 OCRepPayloadSetPropInt(payload, "STATE", type);
298 // TODO fix param for uri
299 if (NS_OK != NSConsumerPostProvider(
300 (OCDevAddr *) nsConsumer->addr, (OCPayload *) payload, "/notification/sync"))
302 NS_LOG(ERROR, "Subscribe fail");
306 else if (task->taskType == TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL)
308 NSProvider * provider = (NSProvider *)task->taskData;
310 OCCancel(provider->messageHandle, NS_QOS, NULL, 0);
311 OCCancel(provider->syncHandle, NS_QOS, NULL, 0);
315 NS_LOG(ERROR, "Unknown type message");