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"
29 #include "rdpayload.h"
31 #define TAG PCF("RDStorage")
33 pthread_mutex_t storageMutex = PTHREAD_MUTEX_INITIALIZER;
34 // This variable holds the published resources on the RD.
35 static OCRDStorePublishResources *g_rdStorage = NULL;
37 static void printStoragedResources(OCRDStorePublishResources *payload)
39 OIC_LOG(DEBUG, TAG, "Print Storage Resources ... ");
40 for (OCRDStorePublishResources *temp = payload; temp; temp = temp->next)
42 if (temp->publishedResource)
44 OCTagsLog(DEBUG, temp->publishedResource->tags);
45 OCLinksLog(DEBUG, temp->publishedResource->setLinks);
50 OCStackResult OCRDStorePublishedResources(const OCResourceCollectionPayload *payload, const OCDevAddr *address)
52 OCResourceCollectionPayload *storeResource = (OCResourceCollectionPayload *)OICCalloc(1, sizeof(OCResourceCollectionPayload));
55 OIC_LOG(ERROR, TAG, "Failed allocating memory for OCRDStorePublishResources.");
56 return OC_STACK_NO_MEMORY;
59 OIC_LOG_V(DEBUG, TAG, "Storing Resources for %s:%u", address->addr, address->port);
61 OCTagsPayload *tags = payload->tags;
62 storeResource->tags = OCCopyTagsResources(tags->n.deviceName, tags->di.id, tags->baseURI,
63 tags->bitmap, address->port, tags->ins, tags->rts, tags->drel, tags->ttl);
64 if (!storeResource->tags)
66 OIC_LOG(ERROR, TAG, "Failed allocating memory for tags.");
67 OCFreeCollectionResource(storeResource);
68 return OC_STACK_NO_MEMORY;
71 for (OCLinksPayload *links = payload->setLinks; links; links = links->next)
73 if (!storeResource->setLinks)
75 storeResource->setLinks = OCCopyLinksResources(links->href, links->rt, links->itf,
76 links->rel, links->obs, links->title, links->uri, links->ins, links->mt);
77 if (!storeResource->setLinks)
79 OIC_LOG(ERROR, TAG, "Failed allocating memory for links.");
80 OCFreeCollectionResource(storeResource);
81 return OC_STACK_NO_MEMORY;
86 OCLinksPayload *temp = storeResource->setLinks;
91 temp->next = OCCopyLinksResources(links->href, links->rt, links->itf, links->rel,
92 links->obs, links->title, links->uri, links->ins, links->mt);
95 OIC_LOG(ERROR, TAG, "Failed allocating memory for links.");
96 OCFreeCollectionResource(storeResource);
97 return OC_STACK_NO_MEMORY;
102 storeResource->next = NULL;
103 OCRDStorePublishResources *resources = (OCRDStorePublishResources *)OICCalloc(1, sizeof(OCRDStorePublishResources));
106 OCFreeCollectionResource(storeResource);
107 return OC_STACK_NO_MEMORY;
109 resources->publishedResource = storeResource;
110 resources->devAddr = *address;
112 pthread_mutex_lock(&storageMutex);
115 OCRDStorePublishResources *temp = g_rdStorage;
120 temp->next = resources;
124 g_rdStorage = resources;
126 pthread_mutex_unlock(&storageMutex);
128 printStoragedResources(g_rdStorage);
132 OCStackResult OCRDCheckPublishedResource(const char *interfaceType, const char *resourceType,
133 OCResourceCollectionPayload **payload, OCDevAddr *devAddr)
135 // ResourceType and InterfaceType if both are NULL it will return. If either is
136 // not null it will continue execution.
137 if (!resourceType && !interfaceType)
139 OIC_LOG(DEBUG, TAG, "Missing resource type or interace type.");
140 return OC_STACK_INVALID_PARAM;
143 OIC_LOG(DEBUG, TAG, "Check Resource in RD");
144 if (g_rdStorage && g_rdStorage->publishedResource)
146 for (OCRDStorePublishResources *pResource = g_rdStorage;
147 pResource; pResource = pResource->next)
149 if (pResource->publishedResource->setLinks)
151 for (OCLinksPayload *tLinks = pResource->publishedResource->setLinks; tLinks; tLinks = tLinks->next)
153 // If either rt or itf are NULL, it should skip remaining code execution.
154 if (!tLinks->rt || !tLinks->itf)
156 OIC_LOG(DEBUG, TAG, "Either resource type or interface type is missing.");
161 OCStringLL *temp = tLinks->rt;
164 OIC_LOG_V(DEBUG, TAG, "Resource Type: %s %s", resourceType, temp->value);
165 if (strcmp(resourceType, temp->value) == 0)
167 OCTagsPayload *tag = pResource->publishedResource->tags;
168 OCTagsPayload *tags = OCCopyTagsResources(tag->n.deviceName, tag->di.id, tag->baseURI,
169 tag->bitmap, tag->port, tag->ins, tag->rts, tag->drel, tag->ttl);
172 return OC_STACK_NO_MEMORY;
174 OCLinksPayload *links = OCCopyLinksResources(tLinks->href, tLinks->rt, tLinks->itf,
175 tLinks->rel, tLinks->obs, tLinks->title, tLinks->uri, tLinks->ins, tLinks->mt);
178 OCFreeTagsResource(tags);
179 return OC_STACK_NO_MEMORY;
181 *payload = OCCopyCollectionResource(tags, links);
184 OCFreeTagsResource(tags);
185 OCFreeLinksResource(links);
186 return OC_STACK_NO_MEMORY;
188 memcpy(devAddr, &pResource->devAddr, sizeof(*devAddr));
196 OCStringLL *temp = tLinks->itf;
199 OIC_LOG_V(DEBUG, TAG, "Interface Type: %s %s", interfaceType, temp->value);
200 if (strcmp(interfaceType, temp->value) == 0)
202 OCTagsPayload *tag = pResource->publishedResource->tags;
203 OCTagsPayload *tags = OCCopyTagsResources(tag->n.deviceName, tag->di.id, tag->baseURI,
204 tag->bitmap, tag->port, tag->ins, tag->rts, tag->drel, tag->ttl);
207 return OC_STACK_NO_MEMORY;
209 OCLinksPayload *links = OCCopyLinksResources(tLinks->uri, tLinks->rt, tLinks->itf,
210 tLinks->rel, tLinks->obs, tLinks->title, tLinks->uri, tLinks->ins, tLinks->mt);
213 OCFreeTagsResource(tags);
214 return OC_STACK_NO_MEMORY;
216 *payload = OCCopyCollectionResource(tags, links);
219 OCFreeTagsResource(tags);
220 OCFreeLinksResource(links);
221 return OC_STACK_NO_MEMORY;
223 devAddr = &pResource->devAddr;
233 return OC_STACK_ERROR;