labcomm.c 7.64 KB
Newer Older
1 2 3 4 5 6
/*
  labcomm.c -- runtime for handling encoding and decoding of
               labcomm samples.

  Copyright 2006-2013 Anders Blomdell <anders.blomdell@control.lth.se>

7 8 9
  This file is part of LabComm.

  LabComm is free software: you can redistribute it and/or modify
10 11 12 13
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

14
  LabComm is distributed in the hope that it will be useful,
15 16 17 18 19 20 21 22
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

23 24 25
#ifdef LABCOMM_COMPAT
  #include LABCOMM_COMPAT
#else
Oscar Olsson's avatar
Oscar Olsson committed
26
  #include <stdio.h>
27
  #include <strings.h>
Anders Nilsson's avatar
Anders Nilsson committed
28
#endif
Oscar Olsson's avatar
Oscar Olsson committed
29

30 31
#include <errno.h>
#include <string.h>
32
#include <stdarg.h>
33
#include <stddef.h>
34

Anders Nilsson's avatar
Anders Nilsson committed
35 36
#include "labcomm.h"
#include "labcomm_private.h"
37
#include "labcomm_ioctl.h"
38
#include "labcomm_dynamic_buffer_writer.h"
Anders Nilsson's avatar
Anders Nilsson committed
39

40
#define CURRENT_VERSION "LabComm2014"
41

42
/* Unwrapping reader/writer functions */
43
#define UNWRAP_ac(rw, ac, ...) ac
44 45
#define UNWRAP(func, ...)	     \
  while (1) {								\
46 47 48 49
    if (UNWRAP_ac(__VA_ARGS__)->action->func) {				\
      return UNWRAP_ac(__VA_ARGS__)->action->func(__VA_ARGS__); }	\
    if (UNWRAP_ac(__VA_ARGS__)->next == NULL) { return -ENOSYS; }	\
    UNWRAP_ac( __VA_ARGS__) = UNWRAP_ac(__VA_ARGS__)->next;		\
50 51 52
  }

int labcomm_reader_alloc(struct labcomm_reader *r, 
53
                         struct labcomm_reader_action_context *action_context)
54
{
55
  UNWRAP(alloc, r, action_context);
56 57 58 59 60 61 62 63 64
}

int labcomm_reader_free(struct labcomm_reader *r, 
                        struct labcomm_reader_action_context *action_context)
{
  UNWRAP(free, r, action_context);
}

int labcomm_reader_start(struct labcomm_reader *r, 
65
                         struct labcomm_reader_action_context *action_context,
Anders Blomdell's avatar
Anders Blomdell committed
66 67
			 int local_index, int remote_index,
			 struct labcomm_signature *signature,
68
			 void *value)
69
{
Anders Blomdell's avatar
Anders Blomdell committed
70
  UNWRAP(start, r, action_context, local_index, remote_index, signature, value);
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
}

int labcomm_reader_end(struct labcomm_reader *r, 
                       struct labcomm_reader_action_context *action_context)
{
  UNWRAP(end, r, action_context);
}

int labcomm_reader_fill(struct labcomm_reader *r, 
                        struct labcomm_reader_action_context *action_context)
{
  UNWRAP(fill, r, action_context);
}

int labcomm_reader_ioctl(struct labcomm_reader *r, 
                         struct labcomm_reader_action_context *action_context,
87
                         int local_index, int remote_index,
88 89 90
                         struct labcomm_signature *signature, 
                         uint32_t ioctl_action, va_list args)
{
91 92
  UNWRAP(ioctl, r, action_context, 
	 local_index, remote_index, signature, ioctl_action, args);
93 94 95
}

int labcomm_writer_alloc(struct labcomm_writer *w, 
96
                         struct labcomm_writer_action_context *action_context)
97
{
98
  UNWRAP(alloc, w, action_context);
99 100 101 102 103 104 105 106 107 108 109 110 111
}

int labcomm_writer_free(struct labcomm_writer *w, 
                        struct labcomm_writer_action_context *action_context)
{
  UNWRAP(free, w, action_context);
}

int labcomm_writer_start(struct labcomm_writer *w, 
                         struct labcomm_writer_action_context *action_context,
                         int index, struct labcomm_signature *signature,
                         void *value)
{
112
  UNWRAP(start, w, action_context, index, signature, value);
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
}

int labcomm_writer_end(struct labcomm_writer *w, 
                       struct labcomm_writer_action_context *action_context)
{
  UNWRAP(end, w, action_context);
} 

int labcomm_writer_flush(struct labcomm_writer *w, 
                         struct labcomm_writer_action_context *action_context)
{
  UNWRAP(flush, w, action_context);
} 

int labcomm_writer_ioctl(struct labcomm_writer *w, 
                         struct labcomm_writer_action_context *action_context, 
                         int index, 
                         struct labcomm_signature *signature, 
                         uint32_t ioctl_action, va_list args)
{
  UNWRAP(ioctl, w, action_context, index, signature, ioctl_action, args);
} 

136 137
#undef UNWRAP
#undef UNWRAP_ac
138 139 140



Oscar Olsson's avatar
Oscar Olsson committed
141

Anders Blomdell's avatar
Anders Blomdell committed
142 143 144 145
static const char *labcomm_error_string[] = { 
#define LABCOMM_ERROR(name, description) description ,
#include "labcomm_error.h"
#undef LABCOMM_ERROR
Oscar Olsson's avatar
Oscar Olsson committed
146
};
Anders Blomdell's avatar
Anders Blomdell committed
147 148 149
static const int labcomm_error_string_count = (sizeof(labcomm_error_string) / 
					       sizeof(labcomm_error_string[0]));

Oscar Olsson's avatar
Oscar Olsson committed
150 151 152 153 154

const char *labcomm_error_get_str(enum labcomm_error error_id)
{
  const char *error_str = NULL;
  // Check if this is a known error ID.
Anders Blomdell's avatar
Anders Blomdell committed
155
  if (error_id < labcomm_error_string_count) {
Anders Blomdell's avatar
Anders Blomdell committed
156
    error_str = labcomm_error_string[error_id];
Oscar Olsson's avatar
Oscar Olsson committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
  }
  return error_str;
}

void on_error_fprintf(enum labcomm_error error_id, size_t nbr_va_args, ...)
{
#ifndef LABCOMM_NO_STDIO
  const char *err_msg = labcomm_error_get_str(error_id); // The final string to print.
  if (err_msg == NULL) {
    err_msg = "Error with an unknown error ID occured.";
  }
  fprintf(stderr, "%s\n", err_msg);

 if (nbr_va_args > 0) {
   va_list arg_pointer;
   va_start(arg_pointer, nbr_va_args);

   fprintf(stderr, "%s\n", "Extra info {");
   char *print_format = va_arg(arg_pointer, char *);
   vfprintf(stderr, print_format, arg_pointer);
   fprintf(stderr, "}\n");

   va_end(arg_pointer);
 } 
#else
 ; // If labcomm can't be compiled with stdio the user will have to make an own error callback functionif he/she needs error reporting.
#endif
}


187

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
#if 0
static void dump(void *p, int size, int first, int last)
{
  int i, j;

  printf("%d %d (%p): ", first, last, p);
  for (i = first ; i < last ; i++) {
    for (j = 0 ; j < size ; j++) {
      printf("%2.2d", ((char*)p)[(i-first)*size + j]);
    }
    printf(" ");
  }
  printf("\n");
}
#endif

204 205
void *labcomm_signature_array_ref(struct labcomm_memory *memory,
				  int *first, int *last, void **data,
206 207 208 209 210
				  int size, int index)
{
  if (*first == 0 && *last == 0) {
    *first = index;
    *last = index + 1;
211
    *data = labcomm_memory_alloc(memory, 0, size);
212 213 214 215 216 217 218 219 220 221 222
    if (*data) { 
      memset(*data, 0, size); 
    }
  } else if (index < *first || *last <= index) {
    void *old_data = *data;
    int old_first = *first;
    int old_last = *last;
    int n;
    *first = (index<old_first)?index:old_first;
    *last = (old_last<=index)?index+1:old_last;
    n = (*last - *first);
223
    *data = labcomm_memory_alloc(memory, 0, n * size);
224 225 226 227 228 229
    if (*data) {
      memset(*data, 0, n * size);
      memcpy(*data + (old_first - *first) * size, 
	     old_data, 
	     (old_last - old_first) * size);
    }
230
//    dump(old_data, size, old_first, old_last);
231
    labcomm_memory_free(memory, 0, old_data);
232 233
  }
  if (*data) {
234
//    dump(*data, size, *first, *last);
235 236 237 238 239
    return *data + (index - *first) * size;
  } else {
    return NULL;
  }
}
240 241 242 243 244 245 246

static int local_index = 0x40;

void labcomm_set_local_index(struct labcomm_signature *signature)
{
  if (signature->index != 0) {
    labcomm_error_fatal_global(LABCOMM_ERROR_SIGNATURE_ALREADY_SET,
247
			       "Signature already set: %s\n", signature->name);
248 249 250 251 252 253 254 255 256
  }
  signature->index = local_index;
  local_index++;
}

int labcomm_get_local_index(struct labcomm_signature *signature)
{
  if (signature->index == 0) {
    labcomm_error_fatal_global(LABCOMM_ERROR_SIGNATURE_NOT_SET,
257
			       "Signature not set: %s\n", signature->name);
258 259 260
  }
  return signature->index;
}
261 262 263 264 265 266 267 268 269 270

int labcomm_internal_sizeof(struct labcomm_signature *signature,
                            void *v)
{
  int length = signature->encoded_size(v);
  return (labcomm_size_packed32(signature->index) +
          labcomm_size_packed32(length) +
          length);
}