1 //******************************************************************
3 // Copyright 2015 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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20 #include "rd_storage.h"
25 #include "payload_logging.h"
26 #include "oic_malloc.h"
28 #include "rdpayload.h"
30 #define TAG PCF("RDStorage")
32 pthread_mutex_t storageMutex = PTHREAD_MUTEX_INITIALIZER;
33 // This variable holds the published resources on the RD.
34 static OCRDStorePublishResources *g_rdStorage = NULL;
36 static void printStoragedResources(OCRDStorePublishResources *payload)
38 OC_LOG(DEBUG, TAG, "Print Storage Resources ... ");
39 for (OCRDStorePublishResources *temp = payload; temp; temp = temp->next)
41 if (temp->publishedResource)
43 OCTagsLog(DEBUG, temp->publishedResource->tags);
44 OCLinksLog(DEBUG, temp->publishedResource->setLinks);
49 OCStackResult OCRDStorePublishedResources(const OCResourceCollectionPayload *payload)
51 OCResourceCollectionPayload *storeResource = (OCResourceCollectionPayload *)OICCalloc(1, sizeof(OCResourceCollectionPayload));
54 OC_LOG(ERROR, TAG, "Failed allocating memory for OCRDStorePublishResources.");
55 return OC_STACK_NO_MEMORY;
58 OC_LOG(DEBUG, TAG, "Storing Resources ... ");
60 OCTagsPayload *tags = payload->tags;
61 storeResource->tags = OCCopyTagsResources(tags->n.deviceName, tags->di.id, tags->baseURI,
62 tags->bitmap, tags->port, tags->ins, tags->rts, tags->drel, tags->ttl);
63 if (!storeResource->tags)
65 OC_LOG(ERROR, TAG, "Failed allocating memory for tags.");
66 OCFreeCollectionResource(storeResource);
67 return OC_STACK_NO_MEMORY;
70 for (OCLinksPayload *links = payload->setLinks; links; links = links->next)
72 if (!storeResource->setLinks)
74 storeResource->setLinks = OCCopyLinksResources(links->href, links->rt, links->itf,
75 links->rel, links->obs, links->title, links->uri, links->ins, links->mt);
76 if (!storeResource->setLinks)
78 OC_LOG(ERROR, TAG, "Failed allocating memory for links.");
79 OCFreeCollectionResource(storeResource);
80 return OC_STACK_NO_MEMORY;
85 OCLinksPayload *temp = storeResource->setLinks;
90 temp->next = OCCopyLinksResources(links->href, links->rt, links->itf, links->rel,
91 links->obs, links->title, links->uri, links->ins, links->mt);
94 OC_LOG(ERROR, TAG, "Failed allocating memory for links.");
95 OCFreeCollectionResource(storeResource);
96 return OC_STACK_NO_MEMORY;
101 storeResource->next = NULL;
102 OCRDStorePublishResources *resources = (OCRDStorePublishResources *)OICCalloc(1, sizeof(OCRDStorePublishResources));
105 OCFreeCollectionResource(storeResource);
106 return OC_STACK_NO_MEMORY;
108 resources->publishedResource = storeResource;
110 pthread_mutex_lock(&storageMutex);
113 OCRDStorePublishResources *temp = g_rdStorage;
118 temp->next = resources;
122 g_rdStorage = resources;
124 pthread_mutex_unlock(&storageMutex);
126 printStoragedResources(g_rdStorage);
130 OCStackResult OCRDCheckPublishedResource(const char *interfaceType, const char *resourceType,
131 OCResourceCollectionPayload **payload)
133 // ResourceType and InterfaceType if both are NULL it will return. If either is
134 // not null it will continue execution.
135 if (!resourceType && !interfaceType)
137 OC_LOG(DEBUG, TAG, "Missing resource type and interace type.");
138 return OC_STACK_INVALID_PARAM;
141 OC_LOG(DEBUG, TAG, "Check Resource in RD");
142 if (g_rdStorage && g_rdStorage->publishedResource)
144 for (OCRDStorePublishResources *pResource = g_rdStorage;
145 pResource; pResource = pResource->next)
147 if (pResource->publishedResource->setLinks)
149 for (OCLinksPayload *tLinks = pResource->publishedResource->setLinks; tLinks; tLinks = tLinks->next)
151 // If either rt or itf are NULL, it should skip remaining code execution.
152 if (!tLinks->rt || !tLinks->itf)
154 OC_LOG(DEBUG, TAG, "Either resource type and interface type are missing.");
159 OCStringLL *temp = tLinks->rt;
162 OC_LOG_V(DEBUG, TAG, "Resource Type: %s %s", resourceType, temp->value);
163 if (strcmp(resourceType, temp->value) == 0)
165 OCTagsPayload *tag = pResource->publishedResource->tags;
166 OCTagsPayload *tags = OCCopyTagsResources(tag->n.deviceName, tag->di.id, tag->baseURI,
167 tag->bitmap, tag->port, tag->ins, tag->rts, tag->drel, tag->ttl);
170 return OC_STACK_NO_MEMORY;
172 OCLinksPayload *links = OCCopyLinksResources(tLinks->href, tLinks->rt, tLinks->itf,
173 tLinks->rel, tLinks->obs, tLinks->title, tLinks->uri, tLinks->ins, tLinks->mt);
176 OCFreeTagsResource(tags);
177 return OC_STACK_NO_MEMORY;
179 *payload = OCCopyCollectionResource(tags, links);
182 OCFreeTagsResource(tags);
183 OCFreeLinksResource(links);
184 return OC_STACK_NO_MEMORY;
193 OCStringLL *temp = tLinks->itf;
196 OC_LOG_V(DEBUG, TAG, "Interface Type: %s %s", interfaceType, temp->value);
197 if (strcmp(interfaceType, temp->value) == 0)
199 OCTagsPayload *tag = pResource->publishedResource->tags;
200 OCTagsPayload *tags = OCCopyTagsResources(tag->n.deviceName, tag->di.id, tag->baseURI,
201 tag->bitmap, tag->port, tag->ins, tag->rts, tag->drel, tag->ttl);
204 return OC_STACK_NO_MEMORY;
206 OCLinksPayload *links = OCCopyLinksResources(tLinks->uri, tLinks->rt, tLinks->itf,
207 tLinks->rel, tLinks->obs, tLinks->title, tLinks->uri, tLinks->ins, tLinks->mt);
210 OCFreeTagsResource(tags);
211 return OC_STACK_NO_MEMORY;
213 *payload = OCCopyCollectionResource(tags, links);
216 OCFreeTagsResource(tags);
217 OCFreeLinksResource(links);
218 return OC_STACK_NO_MEMORY;
229 return OC_STACK_ERROR;