1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
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 ******************************************************************/
27 #include "oic_malloc.h"
30 #define TAG PCF("UQUEUE")
32 u_queue_t* u_queue_create()
34 u_queue_t* queuePtr = (u_queue_t*) OICMalloc(sizeof(u_queue_t));
37 OIC_LOG(DEBUG, TAG, "QueueCreate FAIL");
41 queuePtr->count = NO_MESSAGES;
42 queuePtr->element = NULL;
47 CAResult_t u_queue_add_element(u_queue_t* queue, u_queue_message_t *message)
49 u_queue_element* element = NULL;
50 u_queue_element* ptr = NULL;
54 OIC_LOG(DEBUG, TAG, "QueueAddElement FAIL, Invalid Queue");
55 return CA_STATUS_FAILED;
60 OIC_LOG(DEBUG, TAG, "QueueAddElement : FAIL, NULL Message");
61 return CA_STATUS_FAILED;
64 element = (u_queue_element*) OICMalloc(sizeof(u_queue_element));
67 OIC_LOG(DEBUG, TAG, "QueueAddElement FAIL, memory allocation failed");
68 return CA_MEMORY_ALLOC_FAILED;
71 element->message = message;
78 while (NULL != ptr->next)
86 OIC_LOG_V(DEBUG, TAG, "Queue Count : %d", queue->count);
90 if (NO_MESSAGES != queue->count)
92 OIC_LOG(DEBUG, TAG, "QueueAddElement : FAIL, count is not zero");
94 /* error in queue, free the allocated memory*/
96 return CA_STATUS_FAILED;
99 queue->element = element;
101 OIC_LOG_V(DEBUG, TAG, "Queue Count : %d", queue->count);
109 u_queue_message_t* u_queue_get_element(u_queue_t* queue)
111 u_queue_element* next = NULL;
112 u_queue_element* element = NULL;
113 u_queue_message_t* message = NULL;
117 OIC_LOG(DEBUG, TAG, "QueueAddElement FAIL, Invalid Queue");
121 element = queue->element;
125 OIC_LOG(DEBUG, TAG, "QueueGetElement : FAIL, no messages");
129 next = element->next;
130 queue->element = next;
133 message = element->message;
138 CAResult_t u_queue_remove_element(u_queue_t* queue)
140 u_queue_element* next = NULL;
141 u_queue_element* remove = NULL;
145 OIC_LOG(DEBUG, TAG, "QueueRemoveElement FAIL, Invalid Queue");
146 return CA_STATUS_FAILED;
149 remove = queue->element;
153 OIC_LOG(DEBUG, TAG, "QueueRemoveElement : no messages");
159 OICFree(remove->message);
162 queue->element = next;
168 uint32_t u_queue_get_size(u_queue_t* queue)
172 OIC_LOG(DEBUG, TAG, "QueueGetSize FAIL, Invalid Queue");
179 CAResult_t u_queue_reset(u_queue_t* queue)
181 CAResult_t error = CA_STATUS_FAILED;
185 OIC_LOG(DEBUG, TAG, "QueueReset FAIL, Invalid Queue");
186 return CA_STATUS_FAILED;
189 if (NO_MESSAGES == queue->count)
191 OIC_LOG(DEBUG, TAG, "QueueReset, no elements in the queue");
195 while (NULL != queue->element)
197 error = u_queue_remove_element(queue);
198 if (error == CA_STATUS_FAILED)
202 if (NO_MESSAGES != queue->count)
204 OIC_LOG(DEBUG, TAG, "QueueReset : FAIL, count is non zero");
205 return CA_STATUS_FAILED;
212 CAResult_t u_queue_delete(u_queue_t* queue)
214 CAResult_t error = CA_STATUS_FAILED;
218 OIC_LOG(DEBUG, TAG, "QueueDelete FAIL, Invalid Queue");
219 return CA_STATUS_FAILED;
222 error = u_queue_reset(queue);
223 if (error != CA_STATUS_OK)
225 OIC_LOG(DEBUG, TAG, "QueueDelete : FAIL, error in QueueReset");
230 return (CA_STATUS_OK);
233 u_queue_message_t* u_queue_get_head(u_queue_t* queue)
237 OIC_LOG(DEBUG, TAG, "QueueGetHead FAIL, Invalid Queue");
241 if (NULL == queue->element)
243 OIC_LOG(DEBUG, TAG, "QueueGetHead : no messages in queue");
246 return queue->element->message;