list.c
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2009, 2010 Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
35 /*
36  * @file list.c
37  *
38  */
39 #include <stdlib.h>
40 #include "wge100_camera/list.h"
42 
50 int wge100CamListInit( IpCamList *ipCamList ) {
51  INIT_LIST_HEAD(&ipCamList->list);
52  return 0;
53 }
54 
55 
65 int wge100CamListAdd( IpCamList *ipCamList, IpCamList *newItem) {
66  IpCamList *listIterator;
67 
68  int isInList = 0;
69 
70  // Scan through the list, looking for a serial number that matches the one we're trying to add
71  list_for_each_entry(listIterator, &(ipCamList->list), list) {
72  if( newItem->serial == listIterator->serial) {
73  // This camera is already in the list
74  isInList = 1;
75  break;
76  }
77  }
78 
79  if(isInList == 1) {
80  wge100_debug("Serial number %d already exists in list, dropping.\n", newItem->serial);
81  return CAMLIST_ADD_DUP;
82  } else {
83  wge100_debug("Serial number %d is new, adding to list.\n", newItem->serial);
84  list_add_tail( &(newItem->list), &(ipCamList->list) );
85  return CAMLIST_ADD_OK;
86  }
87 
88 }
89 
96 int wge100CamListFind( IpCamList *ipCamList, uint32_t serial ) {
97  int count;
98 
99  IpCamList *listIterator;
100  count = 0;
101 
102  list_for_each_entry(listIterator, &(ipCamList->list), list) {
103  if(listIterator->serial == serial) {
104  return count;
105  }
106  count++;
107  }
108 
109  return -1;
110 }
111 
123 IpCamList *wge100CamListGetEntry( const IpCamList *ipCamList, int index ) {
124  IpCamList *listIterator;
125 
126  // Iterate through the list until we have counted off 'index' entries
127  list_for_each_entry(listIterator, &(ipCamList->list), list) {
128  if(index-- == 0) {
129  break;
130  }
131  }
132 
133  return listIterator;
134 }
135 
146 int wge100CamListDelEntry( IpCamList *ipCamList, int index ) {
147  int count;
148 
149  IpCamList *tmpListItem;
150  struct list_head *pos, *q;
151  count = 0;
152 
153  list_for_each_safe(pos, q,&(ipCamList->list)) {
154  if(count++ == index) {
155  tmpListItem = list_entry(pos, IpCamList, list);
156  list_del(pos);
157  free(tmpListItem);
158  return 0;
159  }
160  }
161  return -1;
162 }
163 
171 int wge100CamListNumEntries( const IpCamList *ipCamList ) {
172  int count;
173 
174  IpCamList *listIterator;
175  count = 0;
176 
177  list_for_each_entry(listIterator, &(ipCamList->list), list) {
178  count++;
179  }
180 
181  return count;
182 }
183 
191 void wge100CamListDelAll( IpCamList *ipCamList ) {
192  int count;
193 
194  IpCamList *tmpListItem;
195  struct list_head *pos, *q;
196  count = 0;
197 
198  list_for_each_safe(pos, q,&(ipCamList->list)) {
199  tmpListItem = list_entry(pos, IpCamList, list);
200  list_del(pos);
201  free(tmpListItem);
202  }
203 }
#define CAMLIST_ADD_OK
Definition: list.h:315
void wge100CamListDelAll(IpCamList *ipCamList)
Definition: list.c:191
struct list_head list
Definition: list.h:300
int wge100CamListFind(IpCamList *ipCamList, uint32_t serial)
Definition: list.c:96
#define CAMLIST_ADD_DUP
Definition: list.h:316
Definition: list.h:47
static void list_del(struct list_head *entry)
Definition: list.h:120
#define list_for_each_entry(pos, head, member)
Definition: list.h:252
IpCamList * wge100CamListGetEntry(const IpCamList *ipCamList, int index)
Definition: list.c:123
static void list_add_tail(struct list_head *new_item, struct list_head *head)
Definition: list.h:97
int wge100CamListDelEntry(IpCamList *ipCamList, int index)
Definition: list.c:146
int wge100CamListNumEntries(const IpCamList *ipCamList)
Definition: list.c:171
int wge100CamListAdd(IpCamList *ipCamList, IpCamList *newItem)
Definition: list.c:65
#define list_entry(ptr, type, member)
Definition: list.h:216
uint32_t serial
Definition: list.h:275
int wge100CamListInit(IpCamList *ipCamList)
Definition: list.c:50
#define INIT_LIST_HEAD(ptr)
Definition: list.h:56
#define list_for_each_safe(pos, n, head)
Definition: list.h:242


wge100_camera
Author(s): Blaise Gassend, Patrick Mihelich, Eric MacIntosh, David Palchak
autogenerated on Mon Jun 10 2019 15:44:15