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 NSResult NSConsumerSubscribeProvider(NSProvider * provider)
40 NSProvider_internal * provider_internal = (NSProvider_internal *) provider;
41 NS_VERIFY_NOT_NULL(provider_internal, NS_ERROR);
43 NS_LOG(DEBUG, "get subscribe message query");
44 char * query = NSMakeRequestUriWithConsumerId(provider_internal->messageUri);
45 NS_VERIFY_NOT_NULL(query, NS_ERROR);
47 NS_LOG(DEBUG, "subscribe message");
48 NS_LOG_V(DEBUG, "subscribe query : %s", query);
49 OCStackResult ret = NSInvokeRequest(&(provider_internal->i_messageHandle),
50 OC_REST_OBSERVE, provider_internal->i_addr,
51 query, NULL, NSConsumerMessageListener, NULL, CT_DEFAULT);
52 NS_VERIFY_STACK_OK(ret, NS_ERROR);
55 NS_LOG(DEBUG, "get subscribe sync query");
56 query = NSMakeRequestUriWithConsumerId(provider_internal->syncUri);
57 NS_VERIFY_NOT_NULL(query, NS_ERROR);
59 NS_LOG(DEBUG, "subscribe sync");
60 NS_LOG_V(DEBUG, "subscribe query : %s", query);
61 ret = NSInvokeRequest(&(provider_internal->i_syncHandle),
62 OC_REST_OBSERVE, provider_internal->i_addr,
63 query, NULL, NSConsumerSyncInfoListener, NULL, CT_DEFAULT);
64 NS_VERIFY_STACK_OK(ret, NS_ERROR);
70 OCStackApplicationResult NSConsumerCheckPostResult(
71 void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
76 NS_VERIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
77 NS_VERIFY_STACK_OK(clientResponse->result, OC_STACK_KEEP_TRANSACTION);
79 return OC_STACK_KEEP_TRANSACTION;
82 void NSRemoveSyncInfoObj(NSSyncInfo * sync)
87 OCStackApplicationResult NSConsumerSyncInfoListener(
88 void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
93 NS_VERIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
94 NS_VERIFY_STACK_OK(clientResponse->result, OC_STACK_KEEP_TRANSACTION);
96 NS_LOG(DEBUG, "get NSSyncInfo");
97 NSSyncInfo * newSync = NSGetSyncInfoc(clientResponse);
98 NS_VERIFY_NOT_NULL(newSync, OC_STACK_KEEP_TRANSACTION);
100 NSTaskType taskType = TASK_RECV_SYNCINFO;
102 NS_LOG(DEBUG, "build NSTask");
103 NSTask * task = NSMakeTask(taskType, (void *) newSync);
104 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(task,
105 OC_STACK_KEEP_TRANSACTION, NSRemoveSyncInfoObj(newSync));
107 NSConsumerPushEvent(task);
109 return OC_STACK_KEEP_TRANSACTION;
112 OCStackApplicationResult NSConsumerMessageListener(
113 void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
118 NS_VERIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
119 NS_VERIFY_STACK_OK(clientResponse->result, OC_STACK_KEEP_TRANSACTION);
121 NS_LOG(DEBUG, "build NSMessage");
122 NSMessage_consumer * newNoti = NSGetMessage(clientResponse);
123 NS_VERIFY_NOT_NULL(newNoti, OC_STACK_KEEP_TRANSACTION);
125 NSTaskType type = TASK_CONSUMER_RECV_MESSAGE;
127 if (newNoti->messageId == NS_MESSAGE_ACCEPTANCE)
129 NS_LOG(DEBUG, "Receive Subscribe confirm");
130 type = TASK_CONSUMER_RECV_SUBSCRIBE_CONFIRMED;
134 NS_LOG(DEBUG, "Receive new message");
137 NS_LOG(DEBUG, "build NSTask");
138 NSTask * task = NSMakeTask(type, (void *) newNoti);
139 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(task, NS_ERROR, NSRemoveMessage(newNoti));
141 NSConsumerPushEvent(task);
143 return OC_STACK_KEEP_TRANSACTION;
146 void NSGetMessagePostClean(char * pId, OCDevAddr * addr)
152 NSMessage_consumer * NSGetMessage(OCClientResponse * clientResponse)
154 NS_VERIFY_NOT_NULL(clientResponse->payload, NULL);
155 OCRepPayload * payload = (OCRepPayload *)clientResponse->payload;
157 NS_LOG(DEBUG, "get msg id");
159 bool getResult = OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_MESSAGE_ID, (int64_t *)&id);
160 NS_VERIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
162 NS_LOG(DEBUG, "get provider id");
164 getResult = OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_PROVIDER_ID, &pId);
165 NS_LOG_V (DEBUG, "provider id: %s", pId);
166 NS_VERIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
168 NS_LOG(DEBUG, "get provider address");
169 OCDevAddr * addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr));
170 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(addr, NULL, NSGetMessagePostClean(pId, addr));
171 memcpy(addr, clientResponse->addr, sizeof(OCDevAddr));
173 NS_LOG(DEBUG, "create NSMessage");
174 NSMessage_consumer * retMsg = NSCreateMessage_internal(id, pId);
175 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(retMsg, NULL, NSGetMessagePostClean(pId, addr));
178 retMsg->i_addr = addr;
179 retMsg->i_messageTypes = NS_SYNC_UNREAD;
181 NS_LOG(DEBUG, "get msg optional field");
182 OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_TITLE, &retMsg->title);
183 OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_TEXT, &retMsg->contentText);
184 OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_SOURCE, &retMsg->sourceName);
186 OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_TYPE, (int64_t *)&retMsg->type);
187 OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_DATETIME, &retMsg->dateTime);
188 OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_TTL, (int64_t *)&retMsg->ttl);
190 NS_LOG_V(DEBUG, "Msg Address : %s", retMsg->i_addr->addr);
191 NS_LOG_V(DEBUG, "Msg ID : %lu", retMsg->messageId);
192 NS_LOG_V(DEBUG, "Msg Title : %s", retMsg->title);
193 NS_LOG_V(DEBUG, "Msg Content : %s", retMsg->contentText);
194 NS_LOG_V(DEBUG, "Msg Source : %s", retMsg->sourceName);
195 NS_LOG_V(DEBUG, "Msg Type : %d", retMsg->type);
196 NS_LOG_V(DEBUG, "Msg Date : %s", retMsg->dateTime);
197 NS_LOG_V(DEBUG, "Msg ttl : %lu", retMsg->ttl);
202 NSSyncInfo * NSGetSyncInfoc(OCClientResponse * clientResponse)
204 NS_VERIFY_NOT_NULL(clientResponse->payload, NULL);
206 OCRepPayload * payload = (OCRepPayload *)clientResponse->payload;
208 NS_LOG(DEBUG, "get msg id");
210 bool getResult = OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_MESSAGE_ID, (int64_t *)&id);
211 NS_VERIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
213 NS_LOG(DEBUG, "get provider id");
215 getResult = OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_PROVIDER_ID, &pId);
216 NS_VERIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
218 NS_LOG(DEBUG, "get state");
220 getResult = OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_STATE, & state);
221 NS_VERIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
223 NS_LOG(DEBUG, "create NSSyncInfo");
224 NSSyncInfo * retSync = NSCreateSyncInfo_consumer(id, pId, (NSSyncType)state);
225 NS_VERIFY_NOT_NULL(retSync, NULL);
227 NS_LOG_V(DEBUG, "Sync ID : %lu", retSync->messageId);
228 NS_LOG_V(DEBUG, "Sync State : %d", (int) retSync->state);
229 NS_LOG_V(DEBUG, "Sync Provider ID : %s", retSync->providerId);
234 NSMessage_consumer * NSCreateMessage_internal(uint64_t id, const char * providerId)
236 NSMessage_consumer * retMsg = (NSMessage_consumer *)OICMalloc(sizeof(NSMessage_consumer));
237 NS_VERIFY_NOT_NULL(retMsg, NULL);
239 retMsg->messageId = id;
240 OICStrcpy(retMsg->providerId, sizeof(char) * NS_DEVICE_ID_LENGTH, providerId);
241 retMsg->title = NULL;
242 retMsg->contentText = NULL;
243 retMsg->sourceName = NULL;
244 retMsg->type = NS_MESSAGE_INFO;
245 retMsg->dateTime = NULL;
247 retMsg->i_addr = NULL;
252 NSSyncInfo * NSCreateSyncInfo_consumer(uint64_t msgId, const char * providerId, NSSyncType state)
254 NSSyncInfo * retSync = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));
255 NS_VERIFY_NOT_NULL(retSync, NULL);
257 retSync->messageId = msgId;
258 retSync->state = state;
259 OICStrcpy(retSync->providerId, sizeof(char) * NS_DEVICE_ID_LENGTH, providerId);
264 OCStackResult NSSendSyncInfo(NSSyncInfo * syncInfo, OCDevAddr * addr)
266 OCRepPayload * payload = OCRepPayloadCreate();
267 NS_VERIFY_NOT_NULL(payload, OC_STACK_ERROR);
269 OCRepPayloadSetPropInt(payload, NS_ATTRIBUTE_MESSAGE_ID, (int64_t)syncInfo->messageId);
270 OCRepPayloadSetPropInt(payload, NS_ATTRIBUTE_STATE, syncInfo->state);
271 OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_PROVIDER_ID, syncInfo->providerId);
273 return NSInvokeRequest(NULL, OC_REST_POST, addr,
274 NS_SYNC_URI, (OCPayload*)payload,
275 NSConsumerCheckPostResult, NULL, addr->adapter);
278 void NSConsumerCommunicationTaskProcessing(NSTask * task)
280 NS_VERIFY_NOT_NULL_V(task);
282 NS_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType);
283 if (task->taskType == TASK_CONSUMER_REQ_SUBSCRIBE)
285 NS_LOG(DEBUG, "Request Subscribe");
286 NSResult ret = NSConsumerSubscribeProvider((NSProvider *)task->taskData);
287 NS_VERIFY_NOT_NULL_V(ret == NS_OK ? (void *)1 : NULL);
289 else if (task->taskType == TASK_SEND_SYNCINFO)
291 // TODO find target OCDevAddr using provider Id.
292 NSSyncInfo_internal * syncInfo = (NSSyncInfo_internal *)task->taskData;
293 OCDevAddr * addr = syncInfo->i_addr;
294 OCStackResult ret = NSSendSyncInfo((NSSyncInfo *)(task->taskData), addr);
295 NS_VERIFY_STACK_OK_V(ret);
297 NSOICFree(syncInfo->i_addr);
300 else if (task->taskType == TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL)
302 NSProvider_internal * provider = (NSProvider_internal *)task->taskData;
304 OCCancel(provider->i_messageHandle, NS_QOS, NULL, 0);
305 OCCancel(provider->i_syncHandle, NS_QOS, NULL, 0);
309 NS_LOG(ERROR, "Unknown type message");