Contiki-NG
antelope.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, 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 
30 /**
31  * \file
32  * Main functions for Antelope, a DBMS for sensor devices.
33  *
34  * Antelope is described and evaluated in the paper A Database in
35  * Every Sensor, N. Tsiftes and A. Dunkels, in Proceedings of
36  * ACM SenSys 2011.
37  * \author
38  * Nicolas Tsiftes <nvt@sics.se>
39  */
40 
41 #include <stdio.h>
42 
43 #include "antelope.h"
44 
45 static db_output_function_t output = printf;
46 
47 void
48 db_init(void)
49 {
50  relation_init();
51  index_init();
52 }
53 
54 void
55 db_set_output_function(db_output_function_t f)
56 {
57  output = f;
58 }
59 
60 const char *
61 db_get_result_message(db_result_t code)
62 {
63  switch(code) {
64  case DB_FINISHED:
65  return "Iteration finished";
66  case DB_OK:
67  return "Operation succeeded";
68  case DB_LIMIT_ERROR:
69  return "Limit reached";
70  case DB_ALLOCATION_ERROR:
71  return "Allocation error";
72  case DB_STORAGE_ERROR:
73  return "Storage error";
74  case DB_PARSING_ERROR:
75  return "Parsing error";
76  case DB_NAME_ERROR:
77  return "Invalid name";
78  case DB_RELATIONAL_ERROR:
79  return "Semantic error";
80  case DB_TYPE_ERROR:
81  return "Type error";
82  case DB_IMPLEMENTATION_ERROR:
83  return "Implementation error";
84  case DB_INDEX_ERROR:
85  return "Index error";
86  case DB_BUSY_ERROR:
87  return "Busy with processing";
88  case DB_INCONSISTENCY_ERROR:
89  return "Inconsistent handle";
90  case DB_ARGUMENT_ERROR:
91  return "Invalid argument";
92  default:
93  return "Unknown result code";
94  };
95 }
96 
97 db_result_t
98 db_print_header(db_handle_t *handle)
99 {
100  int column;
101  attribute_t *attr;
102 
103  output("[relation = %s, attributes = (", handle->result_rel->name);
104  attr = list_head(handle->result_rel->attributes);
105  for(column = 0; column < handle->ncolumns; column++) {
106  if(attr == NULL) {
107  return DB_IMPLEMENTATION_ERROR;
108  } else if(attr->flags & ATTRIBUTE_FLAG_NO_STORE) {
109  continue;
110  }
111  output("%s%s", column > 0 ? ", " : "", attr->name);
112  attr = attr->next;
113  }
114  output(")]\n");
115  return DB_OK;
116 }
117 
118 db_result_t
119 db_print_tuple(db_handle_t *handle)
120 {
121  int column;
122  attribute_value_t value;
123  db_result_t result;
124 
125  output("Row %lu:\t", (unsigned long)handle->current_row);
126 
127  for(column = 0; column < handle->ncolumns; column++) {
128  result = db_get_value(&value, handle, column);
129  if(DB_ERROR(result)) {
130  output("Unable to get the value for row %lu, column %u: %s\n",
131  (unsigned long)handle->current_row, column,
132  db_get_result_message(result));
133  return result;
134  }
135 
136  switch(value.domain) {
137  case DOMAIN_STRING:
138  output("\"%s\"\t", VALUE_STRING(&value));
139  break;
140  case DOMAIN_INT:
141  output("%d\t", (int)VALUE_INT(&value));
142  break;
143  case DOMAIN_LONG:
144  output("%ld\t", (long)VALUE_LONG(&value));
145  break;
146  default:
147  output("\nUnrecognized domain: %d\n", value.domain);
148  return DB_IMPLEMENTATION_ERROR;
149  }
150  }
151  output("\n");
152 
153  return DB_OK;
154 }
155 
156 int
157 db_processing(db_handle_t *handle)
158 {
159  return handle->flags & DB_HANDLE_FLAG_PROCESSING;
160 }
static uint8_t output(const linkaddr_t *localdest)
Take an IP packet and format it to be sent on an 802.15.4 network using 6lowpan.
Definition: sicslowpan.c:1565
void * list_head(list_t list)
Get a pointer to the first element of a list.
Definition: list.c:82
Declarations of the main Antelope functions.