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 "NSConsumerInternalTaskController.h"
24 #include "NSStructs.h"
26 #include "oic_malloc.h"
27 #include "oic_string.h"
29 NSCacheList ** NSGetMessageCacheList()
31 static NSCacheList * messageCache = NULL;
32 return & messageCache;
35 void NSSetMessageCacheList(NSCacheList * cache)
37 *(NSGetMessageCacheList()) = cache;
40 NSCacheList ** NSGetProviderCacheList()
42 static NSCacheList * providerCache = NULL;
43 return & providerCache;
46 void NSSetProviderCacheList(NSCacheList * cache)
48 *(NSGetProviderCacheList()) = cache;
51 void NSDestroyMessageCacheList()
53 NSCacheList * cache = *(NSGetMessageCacheList());
56 NSStorageDestroy(cache);
60 void NSDestroyProviderCacheList()
62 NSCacheList * cache = *(NSGetProviderCacheList());
65 NSStorageDestroy(cache);
69 NSMessage_consumer * NSMessageCacheFind(const char * messageId)
71 NS_VERIFY_NOT_NULL(messageId, NULL);
73 NSCacheList * MessageCache = *(NSGetMessageCacheList());
76 NS_LOG(DEBUG, "Message Cache Init");
77 MessageCache = NSStorageCreate();
78 NS_VERIFY_NOT_NULL(MessageCache, NULL);
80 MessageCache->cacheType = NS_CONSUMER_CACHE_MESSAGE;
81 NSSetMessageCacheList(MessageCache);
84 NSCacheElement * cacheElement = NSStorageRead(MessageCache, messageId);
86 return (NSMessage_consumer *) cacheElement->data;
89 NSProvider_internal * NSProviderCacheFind(const char * providerId)
91 NS_VERIFY_NOT_NULL(providerId, NULL);
93 NSCacheList * ProviderCache = *(NSGetProviderCacheList());
96 NS_LOG(DEBUG, "Provider Cache Init");
97 ProviderCache = NSStorageCreate();
98 NS_VERIFY_NOT_NULL(ProviderCache, NULL);
100 ProviderCache->cacheType = NS_CONSUMER_CACHE_PROVIDER;
101 NSSetMessageCacheList(ProviderCache);
104 NSCacheElement * cacheElement = NSStorageRead(ProviderCache, providerId);
106 return (NSProvider_internal *) cacheElement->data;
110 NSResult NSMessageCacheUpdate(NSMessage_consumer * msg, NSSyncType type)
112 NSCacheList * MessageCache = *(NSGetMessageCacheList());
115 NS_LOG(DEBUG, "Message Cache Init");
116 MessageCache = NSStorageCreate();
117 NS_VERIFY_NOT_NULL(MessageCache, NS_ERROR);
119 MessageCache->cacheType = NS_CONSUMER_CACHE_MESSAGE;
120 NSSetMessageCacheList(MessageCache);
123 NS_VERIFY_NOT_NULL(msg, NS_ERROR);
127 NSCacheElement * obj = (NSCacheElement *)OICMalloc(sizeof(NSCacheElement));
128 NS_VERIFY_NOT_NULL(obj, NS_ERROR);
130 obj->data = (NSCacheData *) msg;
133 NS_LOG(DEBUG, "try to write to storage");
134 NSResult ret = NSStorageWrite(MessageCache, obj);
135 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(ret == NS_OK ? (void *) 1 : NULL,
136 NS_ERROR, NSRemoveMessage(msg));
143 NSResult NSProviderCacheUpdate(NSProvider_internal * provider)
145 NSCacheList * ProviderCache = *(NSGetProviderCacheList());
148 NS_LOG(DEBUG, "Provider Cache Init");
149 ProviderCache = NSStorageCreate();
150 NS_VERIFY_NOT_NULL(ProviderCache, NS_ERROR);
152 ProviderCache->cacheType = NS_CONSUMER_CACHE_PROVIDER;
153 NSSetProviderCacheList(ProviderCache);
156 NS_VERIFY_NOT_NULL(provider, NS_ERROR);
158 NSCacheElement * obj = (NSCacheElement *)OICMalloc(sizeof(NSCacheElement));
159 NS_VERIFY_NOT_NULL(obj, NS_ERROR);
161 obj->data = (NSCacheData *) provider;
164 NS_LOG(DEBUG, "try to write to storage");
165 NSResult ret = NSStorageWrite(ProviderCache, obj);
166 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(ret == NS_OK ? (void *) 1 : NULL,
167 NS_ERROR, NSRemoveProvider(provider));
174 void NSConsumerHandleProviderDiscovered(NSProvider_internal * provider)
176 NS_VERIFY_NOT_NULL_V(provider);
178 NSProvider_internal * providerCacheData = NSProviderCacheFind(provider->providerId);
179 NS_VERIFY_NOT_NULL_V(providerCacheData == NULL ? (void *)1 : NULL);
181 NS_LOG (ERROR, "New provider is discovered");
182 NSResult ret = NSProviderCacheUpdate(provider);
183 NS_VERIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL);
186 if (provider->accessPolicy == NS_ACCESS_DENY)
188 NS_LOG(DEBUG, "accepter is NS_ACCEPTER_CONSUMER, Callback to user");
189 NSDiscoveredProvider((NSProvider *) provider);
193 NS_LOG(DEBUG, "accepter is NS_ACCEPTER_PROVIDER, request subscribe");
194 NSTask * task = NSMakeTask(TASK_CONSUMER_REQ_SUBSCRIBE, (void *) provider);
195 NS_VERIFY_NOT_NULL_V(task);
197 NSConsumerPushEvent(task);
201 void NSConsumerHandleRecvSubscriptionConfirmed(NSMessage_consumer * msg)
203 NS_VERIFY_NOT_NULL_V(msg);
205 NSProvider_internal * provider = NSProviderCacheFind(msg->providerId);
206 NS_VERIFY_NOT_NULL_V(provider);
208 NSSubscriptionAccepted((NSProvider *) provider);
211 void NSConsumerHandleRecvMessage(NSMessage_consumer * msg)
213 NS_VERIFY_NOT_NULL_V(msg);
215 NSResult ret = NSMessageCacheUpdate(msg, NS_SYNC_UNREAD);
216 NS_VERIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL);
218 NSMessagePost((NSMessage *) msg);
221 void NSConsumerHandleRecvSyncInfo(NSSyncInfo * sync)
223 NS_VERIFY_NOT_NULL_V(sync);
225 NSProvider_internal * provider = NSProviderCacheFind(sync->providerId);
226 NS_VERIFY_NOT_NULL_V(provider);
228 char msgId[NS_DEVICE_ID_LENGTH] = { 0, };
229 snprintf(msgId, NS_DEVICE_ID_LENGTH, "%lu", sync->messageId);
231 NSMessage_consumer * msg = NSMessageCacheFind(msgId);
232 NS_VERIFY_NOT_NULL_V(msg);
234 NSResult ret = NSMessageCacheUpdate(msg, sync->state);
235 NS_VERIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL);
237 NSNotificationSync(sync);
240 void NSConsumerHandleMakeSyncInfo(NSSyncInfo * sync)
242 NS_VERIFY_NOT_NULL_V(sync);
244 NSProvider_internal * provider = NSProviderCacheFind(sync->providerId);
245 NS_VERIFY_NOT_NULL_V (provider);
247 NSSyncInfo_internal * syncInfo = (NSSyncInfo_internal *)OICMalloc(sizeof(NSSyncInfo_internal));
248 NS_VERIFY_NOT_NULL_V(syncInfo);
250 OICStrcpy(syncInfo->providerId, sizeof(char) * NS_DEVICE_ID_LENGTH, sync->providerId);
251 syncInfo->messageId = sync->messageId;
252 syncInfo->state = sync->state;
253 syncInfo->i_addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr));
254 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(syncInfo->i_addr, NSOICFree(syncInfo));
255 memcpy(syncInfo->i_addr, provider->i_addr, sizeof(OCDevAddr));
257 NSTask * syncTask = NSMakeTask(TASK_SEND_SYNCINFO, (void *) syncInfo);
258 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(syncTask, NSOICFree(syncInfo));
260 NSConsumerPushEvent(syncTask);
265 void NSConsumerInternalTaskProcessing(NSTask * task)
267 NS_VERIFY_NOT_NULL_V(task);
269 NS_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType);
270 switch (task->taskType)
272 case TASK_CONSUMER_RECV_SUBSCRIBE_CONFIRMED:
274 NS_LOG(DEBUG, "Receive Subscribe confirm from provider.");
275 NSConsumerHandleRecvSubscriptionConfirmed((NSMessage_consumer *)task->taskData);
278 case TASK_CONSUMER_RECV_MESSAGE:
280 NS_LOG(DEBUG, "Receive New Notification");
281 NSConsumerHandleRecvMessage((NSMessage_consumer *)task->taskData);
285 case TASK_CONSUMER_PROVIDER_DISCOVERED:
287 NS_LOG(DEBUG, "Receive New Provider is discovered.");
288 NSConsumerHandleProviderDiscovered((NSProvider_internal *)task->taskData);
291 case TASK_RECV_SYNCINFO:
293 NS_LOG(DEBUG, "Receive SyncInfo.");
294 NSConsumerHandleRecvSyncInfo((NSSyncInfo *)task->taskData);
297 case TASK_MAKE_SYNCINFO:
299 NS_LOG(DEBUG, "Make SyncInfo, get Provider's Addr");
300 NSConsumerHandleMakeSyncInfo((NSSyncInfo *)task->taskData);
305 NS_LOG(ERROR, "Unknown TASK Type");