Contiki 2.5
timetable-aggregate.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * $Id: timetable-aggregate.c,v 1.2 2008/03/13 14:27:34 fros4943 Exp $
32  */
33 
34 /**
35  * \file
36  * A brief description of what this file is.
37  * \author
38  * Adam Dunkels <adam@sics.se>
39  */
40 
42 
43 #define XXX_HACK_MAX_CATEGORIES 32
44 
45 #include <stdio.h>
46 
47 /*---------------------------------------------------------------------------*/
48 /*
49  *
50  * Find an aggregation category in the list of aggregates. If the
51  * category could not be found, the function returns a pointer to an
52  * empty entry. If the list is full, the function returns NULL.
53  *
54  */
55 static struct timetable_aggregate_entry *
56 find_aggregate_category(struct timetable_aggregate *a,
57  const uint16_t cat)
58 {
59  int i;
60  uint16_t acat;
61 
62  for(i = 0; i < a->ptr; ++i) {
63  acat = (a->entries[i].id[0] << 8) + a->entries[i].id[1];
64  if(acat == cat) {
65 
66  return &a->entries[i];
67  }
68  }
69 
70  if(i == a->size) {
71  return NULL;
72  }
73 
74  a->entries[a->ptr].id = NULL;
75  return &a->entries[a->ptr++];
76 }
77 /*---------------------------------------------------------------------------*/
78 /*
79  *
80  * Find a specific aggregate ID in the list of aggregates.
81  *
82  */
83 static struct timetable_aggregate_entry *
84 find_aggregate(struct timetable_aggregate *a,
85  const char *id)
86 {
87  int i;
88  for(i = 0; i < a->ptr; ++i) {
89  if(a->entries[i].id == id) {
90  return &a->entries[i];
91  }
92  }
93  if(i == a->size) {
94  return NULL;
95  }
96  a->entries[a->ptr].id = NULL;
97  return &a->entries[a->ptr++];
98 }
99 /*---------------------------------------------------------------------------*/
100 void
101 timetable_aggregate_print_detailed(struct timetable_aggregate *a)
102 {
103  int i;
104  /* printf("timetable_aggregate_print_detailed: a ptr %d\n", a->ptr);*/
105  for(i = 0; i < a->ptr; ++i) {
106  printf("-- %s: %lu / %u = %lu\n", a->entries[i].id,
107  a->entries[i].time,
108  a->entries[i].episodes,
109  a->entries[i].time / a->entries[i].episodes);
110  }
111 
112  printf("Memory for entries: %d * %d = %d\n",
113  (int)sizeof(struct timetable_aggregate), a->ptr,
114  (int)sizeof(struct timetable_aggregate) * a->ptr);
115 }
116 /*---------------------------------------------------------------------------*/
117 void
118 timetable_aggregate_reset(struct timetable_aggregate *a)
119 {
120  int i;
121  for(i = 0; i < a->ptr; ++i) {
122  a->entries[i].time = 0;
123  a->entries[i].episodes = 0;
124  }
125 }
126 /*---------------------------------------------------------------------------*/
127 void
128 timetable_aggregate_print_categories(struct timetable_aggregate *a)
129 {
130  int i;
131 
132  /* printf("timetable_aggregate_print_categories: a ptr %d\n", a->ptr);*/
133  for(i = 0; i < a->ptr; ++i) {
134  printf("-- %c%c: %lu / %u = %lu\n",
135  a->entries[i].id[0], a->entries[i].id[1],
136  a->entries[i].time,
137  a->entries[i].episodes,
138  a->entries[i].time / a->entries[i].episodes);
139  }
140 
141  printf("Memory for entries: %d * %d = %d\n",
142  (int)sizeof(struct timetable_aggregate), a->ptr,
143  (int)sizeof(struct timetable_aggregate) * a->ptr);
144 }
145 /*---------------------------------------------------------------------------*/
146 void
147 timetable_aggregate_compute_detailed(struct timetable_aggregate *a,
148  struct timetable *timetable)
149 {
150  unsigned int i;
151  rtimer_clock_t t;
152 
153  t = timetable->timestamps[0].time;
154 
155  for(i = 1; i < *timetable->ptr; ++i) {
156  struct timetable_aggregate_entry *entry;
157  entry = find_aggregate(a, timetable->timestamps[i - 1].id);
158  if(entry == NULL) {
159  /* The list is full, skip this entry */
160  /* printf("detailed_timetable_aggregate_compute: list full\n");*/
161  } else if(entry->id == NULL) {
162  /* The id was found in the list, so we add it. */
163  entry->id = timetable->timestamps[i - 1].id;
164  entry->time = (unsigned long)(timetable->timestamps[i].time - t -
166  entry->episodes = 1;
167  /* printf("New entry %s %lu\n", entry->id, entry->time);*/
168  } else {
169  entry->time += (unsigned long)(timetable->timestamps[i].time - t -
171  entry->episodes++;
172  }
173  t = timetable->timestamps[i].time;
174  /* printf("a ptr %d\n", a->ptr);*/
175  }
176 }
177 /*---------------------------------------------------------------------------*/
178 void
179 timetable_aggregate_compute_categories(struct timetable_aggregate *a,
180  struct timetable *timetable)
181 {
182  unsigned int i;
183  int j;
184  rtimer_clock_t t;
185  uint16_t categories[XXX_HACK_MAX_CATEGORIES];
186  int categories_ptr = 0;
187 
188  t = timetable->timestamps[0].time;
189 
190  for(i = 1; i < *timetable->ptr; ++i) {
191  struct timetable_aggregate_entry *entry;
192  uint16_t cat;
193 
194  /* printf("category_timetable_aggregate_compute %s %d\n",
195  timetable->timestamps[i - 1].id, i);*/
196  cat = (timetable->timestamps[i - 1].id[0] << 8) +
197  (timetable->timestamps[i - 1].id[1] & 0xff);
198  entry = find_aggregate_category(a, cat);
199  if(entry == NULL) {
200  /* The list is full, skip this entry */
201  /* printf("category_timetable_aggregate_compute: list full\n");*/
202  } else if(entry->id == NULL) {
203  /* The category was not found in the list, so we add it. */
204  entry->id = timetable->timestamps[i - 1].id;
205  entry->time = (unsigned long)(timetable->timestamps[i].time - t -
207  entry->episodes = 1;
208  /* printf("New category %c%c time %lu\n",
209  timetable->timestamps[i - 1].id[0],
210  timetable->timestamps[i - 1].id[1], entry->time);*/
211  } else {
212 
213  entry->time += (unsigned long)(timetable->timestamps[i].time - t -
215  /* printf("Adding time to %c%c time %lu\n",
216  timetable->timestamps[i - 1].id[0],
217  timetable->timestamps[i - 1].id[1], entry->time);*/
218 
219  /* Make sure that we only update the episodes of each category
220  once per run. We keep track of all updated categories in the
221  "categories" array. If the category is already present in the
222  array, we do not update it. Otherwise, we insert the category
223  in the array and update the episodes counter of the
224  category. */
225 
226  for(j = 0; j < categories_ptr; ++j) {
227  if(categories[j] == cat) {
228  break;
229  }
230  }
231  if(j == categories_ptr) {
232  categories[j] = cat;
233  categories_ptr++;
234  entry->episodes++;
235  }
236  }
237  t = timetable->timestamps[i].time;
238  }
239 }
240 /*---------------------------------------------------------------------------*/