labcomm2006.h 4.88 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
  labcomm.h -- user interface for handling encoding and decoding of
               labcomm samples.

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

  This file is part of LabComm.

  LabComm is free software: you can redistribute it and/or modify
  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.

  LabComm is distributed in the hope that it will be useful,
  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/>.
*/

#ifndef _LABCOMM_H_
#define _LABCOMM_H_

#include <stdarg.h>
#include <stdint.h>
#include <unistd.h>
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
29
30
#include "labcomm2006_error.h"
#include "labcomm2006_scheduler.h"
31
32

/* Forward declaration */
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
33
34
struct labcomm2006_encoder;
struct labcomm2006_decoder;
35
36
37
38

/*
 * Signature entry
 */
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
39
struct labcomm2006_signature {
40
41
  int type;
  char *name;
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
42
  int (*encoded_size)(struct labcomm2006_signature *, void *); // void * == encoded_sample *
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  int size;
  unsigned char *signature; 
  int index;
#ifdef LABCOMM_EXPERIMENTAL_CACHED_ENCODED_SIZE
  int cached_encoded_size; // -1 if not initialized or type is variable size
#endif
};

/*
 * Error handling.
 */

/* The callback prototype for error handling.
 * First parameter is the error ID.
 * The second paramters is the number of va_args that comes after this 
 * one. If none it should be 0.
 * Optionaly other paramters can be supplied depending on what is needed 
 * for this error ID.
 */
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
62
typedef void (*labcomm2006_error_handler_callback)(enum labcomm2006_error error_id, 
63
64
65
66
67
					       size_t nbr_va_args, ...); 

/* Default error handler, prints message to stderr. 
 * Extra info about the error can be supplied as char* as VA-args. Especially user defined errors should supply a describing string. if nbr_va_args > 1 the first variable argument must be a printf format string and the possibly following arguments are passed as va_args to vprintf. 
 */
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
68
void on_error_fprintf(enum labcomm2006_error error_id, size_t nbr_va_args, ...);
69
70

/* Register a callback for the error handler for this encoder. */
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
71
void labcomm2006_register_error_handler_encoder(struct labcomm2006_encoder *encoder, labcomm2006_error_handler_callback callback);
72
73

/* Register a callback for the error handler for this decoder. */
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
74
void labcomm2006_register_error_handler_decoder(struct labcomm2006_decoder *decoder, labcomm2006_error_handler_callback callback);
75
76

/* Get a string describing the supplied standrad labcomm error. */
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
77
const char *labcomm2006_error_get_str(enum labcomm2006_error error_id);
78

Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
79
80
81
typedef int (*labcomm2006_handle_new_datatype_callback)(
  struct labcomm2006_decoder *decoder,
  struct labcomm2006_signature *sig);
82

Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
83
84
void labcomm2006_decoder_register_new_datatype_handler(struct labcomm2006_decoder *d,
		labcomm2006_handle_new_datatype_callback on_new_datatype);
85
86
87
88
89
90
91
92
93

/*
 * Dynamic memory handling
 *   lifetime == 0     memory that will live for as long as the 
 *                     encoder/decoder or that are allocated/deallocated 
 *                     during the communication setup phase
 *   otherwise         memory will live for approximately this number of
 *                     sent/received samples
 */
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
94
struct labcomm2006_memory;
95

Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
96
97
void *labcomm2006_memory_alloc(struct labcomm2006_memory *m, int lifetime, size_t size);
void *labcomm2006_memory_realloc(struct labcomm2006_memory *m, int lifetime, 
98
			     void *ptr, size_t size);
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
99
void labcomm2006_memory_free(struct labcomm2006_memory *m, int lifetime, void *ptr);
100
101
102
103

/*
 * Decoder
 */
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
struct labcomm2006_reader;

struct labcomm2006_decoder *labcomm2006_decoder_new(
  struct labcomm2006_reader *reader,
  struct labcomm2006_error_handler *error,
  struct labcomm2006_memory *memory,
  struct labcomm2006_scheduler *scheduler);
void labcomm2006_decoder_free(
  struct labcomm2006_decoder *decoder);
int labcomm2006_decoder_decode_one(
  struct labcomm2006_decoder *decoder);
void labcomm2006_decoder_run(
  struct labcomm2006_decoder *decoder);

/* See labcomm2006_ioctl.h for predefined ioctl_action values */
int labcomm2006_decoder_ioctl(struct labcomm2006_decoder *decoder, 
120
121
122
123
124
125
			  uint32_t ioctl_action,
			  ...);

/*
 * Encoder
 */
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
126
127
128
129
130
131
132
133
134
135
136
137
struct labcomm2006_writer;

struct labcomm2006_encoder *labcomm2006_encoder_new(
  struct labcomm2006_writer *writer,
  struct labcomm2006_error_handler *error,
  struct labcomm2006_memory *memory,
  struct labcomm2006_scheduler *scheduler);
void labcomm2006_encoder_free(
  struct labcomm2006_encoder *encoder);

/* See labcomm2006_ioctl.h for predefined ioctl_action values */
int labcomm2006_encoder_ioctl(struct labcomm2006_encoder *encoder, 
138
139
140
141
142
143
			  uint32_t ioctl_action,
			  ...);

#define LABCOMM_VOID ((void*)1)

#endif