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 "NSConstants.h"
22 #include "NSConsumerCommon.h"
23 #include "NSConsumerCommunication.h"
24 #include "oic_malloc.h"
25 #include "oic_string.h"
26 #include "ocpayload.h"
28 #define NS_SYNC_URI "/notification/sync"
30 unsigned long NS_MESSAGE_ACCEPTANCE = 1;
32 NSMessage_consumer * NSCreateMessage_internal(uint64_t msgId, const char * providerId);
33 NSSyncInfo * NSCreateSyncInfo_consumer(uint64_t msgId, const char * providerId, NSSyncType state);
35 NSMessage_consumer * NSGetMessage(OCClientResponse * clientResponse);
36 NSSyncInfo * NSGetSyncInfoc(OCClientResponse * clientResponse);
38 OCRepPayload * NSGetofSyncInfoPayload(NSMessage_consumer * message, int type);
39 OCStackResult NSSendSyncInfoUsingMessage(NSMessage_consumer * message, int type);
41 // TODO it seem to not to be this file
42 NSResult NSPushToCache(OCClientResponse * clientResponse, NSTaskType type);
44 NSResult NSConsumerSubscribeProvider(NSProvider * provider)
46 NSProvider_internal * provider_internal = (NSProvider_internal *) provider;
47 NS_VERIFY_NOT_NULL(provider_internal, NS_ERROR);
49 NS_LOG(DEBUG, "get subscribe message query");
50 char * query = NSMakeRequestUriWithConsumerId(provider_internal->messageUri);
51 NS_VERIFY_NOT_NULL(query, NS_ERROR);
53 NS_LOG(DEBUG, "subscribe message");
54 NS_LOG_V(DEBUG, "subscribe query : %s", query);
55 OCStackResult ret = NSInvokeRequest(&(provider_internal->i_messageHandle),
56 OC_REST_OBSERVE, provider_internal->i_addr,
57 query, NULL, NSConsumerMessageListener, NULL);
58 NS_VERIFY_STACK_OK(ret, NS_ERROR);
61 NS_LOG(DEBUG, "get subscribe sync query");
62 query = NSMakeRequestUriWithConsumerId(provider_internal->syncUri);
63 NS_VERIFY_NOT_NULL(query, NS_ERROR);
65 NS_LOG(DEBUG, "subscribe sync");
66 NS_LOG_V(DEBUG, "subscribe query : %s", query);
67 ret = NSInvokeRequest(&(provider_internal->i_syncHandle),
68 OC_REST_OBSERVE, provider_internal->i_addr,
69 query, NULL, NSConsumerSyncInfoListener, NULL);
70 NS_VERIFY_STACK_OK(ret, NS_ERROR);
76 OCStackApplicationResult NSConsumerCheckPostResult(
77 void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
82 NS_VERIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
83 NS_VERIFY_STACK_OK(clientResponse->result, OC_STACK_KEEP_TRANSACTION);
85 return OC_STACK_KEEP_TRANSACTION;
88 void NSRemoveSyncInfoObj(NSSyncInfo * sync)
93 OCStackApplicationResult NSConsumerSyncInfoListener(
94 void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
99 NS_VERIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
100 NS_VERIFY_STACK_OK(clientResponse->result, OC_STACK_KEEP_TRANSACTION);
102 NS_LOG(DEBUG, "get NSSyncInfo");
103 NSSyncInfo * newSync = NSGetSyncInfoc(clientResponse);
104 NS_VERIFY_NOT_NULL(newSync, OC_STACK_KEEP_TRANSACTION);
106 NSTaskType taskType = TASK_RECV_SYNCINFO;
108 NS_LOG(DEBUG, "build NSTask");
109 NSTask * task = NSMakeTask(taskType, (void *) newSync);
110 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(task,
111 OC_STACK_KEEP_TRANSACTION, NSRemoveSyncInfoObj(newSync));
113 NSConsumerPushEvent(task);
115 return OC_STACK_KEEP_TRANSACTION;
118 OCStackApplicationResult NSConsumerMessageListener(
119 void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
124 NS_VERIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
125 NS_VERIFY_STACK_OK(clientResponse->result, OC_STACK_KEEP_TRANSACTION);
127 NS_LOG(DEBUG, "build NSMessage");
128 NSMessage_consumer * newNoti = NSGetMessage(clientResponse);
129 NS_VERIFY_NOT_NULL(newNoti, OC_STACK_KEEP_TRANSACTION);
131 NSTaskType type = TASK_CONSUMER_RECV_MESSAGE;
133 if (newNoti->messageId == NS_MESSAGE_ACCEPTANCE)
135 NS_LOG(DEBUG, "Receive Subscribe confirm");
136 type = TASK_CONSUMER_RECV_SUBSCRIBE_CONFIRMED;
140 NS_LOG(DEBUG, "Receive new message");
143 NS_LOG(DEBUG, "build NSTask");
144 NSTask * task = NSMakeTask(type, (void *) newNoti);
145 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(task, NS_ERROR, NSRemoveMessage(newNoti));
147 NSConsumerPushEvent(task);
149 return OC_STACK_KEEP_TRANSACTION;
152 NSResult NSPushToCache(OCClientResponse * clientResponse, NSTaskType type)
154 NSMessage_consumer * cachedNoti = NSGetMessage(clientResponse);
155 NS_LOG(DEBUG, "build NSMessage");
156 NS_VERIFY_NOT_NULL(cachedNoti, NS_ERROR);
158 NS_LOG(DEBUG, "build NSTask");
159 NSTask * task = NSMakeTask(type, (void *) cachedNoti);
160 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(task, NS_ERROR, NSRemoveMessage(cachedNoti));
162 NSConsumerPushEvent(task);
167 void NSGetMessagePostClean(char * pId, OCDevAddr * addr)
173 NSMessage_consumer * NSGetMessage(OCClientResponse * clientResponse)
175 NS_VERIFY_NOT_NULL(clientResponse->payload, NULL);
176 OCRepPayload * payload = (OCRepPayload *)clientResponse->payload;
178 NS_LOG(DEBUG, "get msg id");
180 bool getResult = OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_MESSAGE_ID, (int64_t *)&id);
181 NS_VERIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
183 NS_LOG(DEBUG, "get provider id");
185 getResult = OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_PROVIDER_ID, &pId);
186 NS_LOG_V (DEBUG, "provider id: %s", pId);
187 NS_VERIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
189 NS_LOG(DEBUG, "get provider address");
190 OCDevAddr * addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr));
191 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(addr, NULL, NSGetMessagePostClean(pId, addr));
192 memcpy(addr, clientResponse->addr, sizeof(OCDevAddr));
194 NS_LOG(DEBUG, "create NSMessage");
195 NSMessage_consumer * retMsg = NSCreateMessage_internal(id, pId);
196 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(retMsg, NULL, NSGetMessagePostClean(pId, addr));
199 retMsg->i_addr = addr;
200 retMsg->i_messageTypes = NS_SYNC_UNREAD;
202 NS_LOG(DEBUG, "get msg optional field");
203 OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_TITLE, &retMsg->title);
204 OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_TEXT, &retMsg->contentText);
205 OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_SOURCE, &retMsg->sourceName);
207 OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_TYPE, (int64_t *)&retMsg->type);
208 OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_DATETIME, &retMsg->dateTime);
209 OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_TTL, (int64_t *)&retMsg->ttl);
211 NS_LOG_V(DEBUG, "Msg Address : %s", retMsg->i_addr->addr);
212 NS_LOG_V(DEBUG, "Msg ID : %lld", retMsg->messageId);
213 NS_LOG_V(DEBUG, "Msg Title : %s", retMsg->title);
214 NS_LOG_V(DEBUG, "Msg Content : %s", retMsg->contentText);
215 NS_LOG_V(DEBUG, "Msg Source : %s", retMsg->sourceName);
216 NS_LOG_V(DEBUG, "Msg Type : %d", retMsg->type);
217 NS_LOG_V(DEBUG, "Msg Date : %s", retMsg->dateTime);
218 NS_LOG_V(DEBUG, "Msg ttl : %lld", retMsg->ttl);
223 NSSyncInfo * NSGetSyncInfoc(OCClientResponse * clientResponse)
225 NS_VERIFY_NOT_NULL(clientResponse->payload, NULL);
227 OCRepPayload * payload = (OCRepPayload *)clientResponse->payload;
229 NS_LOG(DEBUG, "get msg id");
231 bool getResult = OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_MESSAGE_ID, (int64_t *)&id);
232 NS_VERIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
234 NS_LOG(DEBUG, "get provider id");
236 getResult = OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_PROVIDER_ID, &pId);
237 NS_VERIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
239 NS_LOG(DEBUG, "get state");
241 getResult = OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_STATE, & state);
242 NS_VERIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
244 NS_LOG(DEBUG, "create NSSyncInfo");
245 NSSyncInfo * retSync = NSCreateSyncInfo_consumer(id, pId, (NSSyncType)state);
246 NS_VERIFY_NOT_NULL(retSync, NULL);
248 NS_LOG_V(DEBUG, "Sync ID : %lld", retSync->messageId);
249 NS_LOG_V(DEBUG, "Sync State : %d", (int) retSync->state);
250 NS_LOG_V(DEBUG, "Sync Provider ID : %s", retSync->providerId);
255 NSMessage_consumer * NSCreateMessage_internal(uint64_t id, const char * providerId)
257 NSMessage_consumer * retMsg = (NSMessage_consumer *)OICMalloc(sizeof(NSMessage_consumer));
258 NS_VERIFY_NOT_NULL(retMsg, NULL);
260 retMsg->messageId = id;
261 OICStrcpy(retMsg->providerId, sizeof(char) * NS_DEVICE_ID_LENGTH, providerId);
262 retMsg->title = NULL;
263 retMsg->contentText = NULL;
264 retMsg->sourceName = NULL;
265 retMsg->type = NS_MESSAGE_INFO;
266 retMsg->dateTime = NULL;
268 retMsg->i_addr = NULL;
273 NSSyncInfo * NSCreateSyncInfo_consumer(uint64_t msgId, const char * providerId, NSSyncType state)
275 NSSyncInfo * retSync = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));
276 NS_VERIFY_NOT_NULL(retSync, NULL);
278 retSync->messageId = msgId;
279 retSync->state = state;
280 OICStrcpy(retSync->providerId, sizeof(char) * NS_DEVICE_ID_LENGTH, providerId);
285 OCStackResult NSSendSyncInfo(NSSyncInfo * syncInfo, OCDevAddr * addr)
287 OCRepPayload * payload = OCRepPayloadCreate();
288 NS_VERIFY_NOT_NULL(payload, OC_STACK_ERROR);
290 OCRepPayloadSetPropInt(payload, NS_ATTRIBUTE_MESSAGE_ID, (int64_t)syncInfo->messageId);
291 OCRepPayloadSetPropInt(payload, NS_ATTRIBUTE_STATE, syncInfo->state);
292 OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_PROVIDER_ID, syncInfo->providerId);
294 return NSInvokeRequest(NULL, OC_REST_POST, addr,
295 NS_SYNC_URI, (OCPayload*)payload,
296 NSConsumerCheckPostResult, NULL);
299 void NSConsumerCommunicationTaskProcessing(NSTask * task)
301 NS_VERIFY_NOT_NULL_V(task);
303 NS_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType);
304 if (task->taskType == TASK_CONSUMER_REQ_SUBSCRIBE)
306 NS_LOG(DEBUG, "Request Subscribe");
307 NSResult ret = NSConsumerSubscribeProvider((NSProvider *)task->taskData);
308 NS_VERIFY_NOT_NULL_V(ret == NS_OK ? (void *)1 : NULL);
310 else if (task->taskType == TASK_SEND_SYNCINFO)
312 // TODO find target OCDevAddr using provider Id.
313 NSSyncInfo_internal * syncInfo = (NSSyncInfo_internal *)task->taskData;
314 OCDevAddr * addr = syncInfo->i_addr;
315 OCStackResult ret = NSSendSyncInfo((NSSyncInfo *)(task->taskData), addr);
316 NS_VERIFY_STACK_OK_V(ret);
318 NSOICFree(syncInfo->i_addr);
321 else if (task->taskType == TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL)
323 NSProvider_internal * provider = (NSProvider_internal *)task->taskData;
325 OCCancel(provider->i_messageHandle, NS_QOS, NULL, 0);
326 OCCancel(provider->i_syncHandle, NS_QOS, NULL, 0);
330 NS_LOG(ERROR, "Unknown type message");