labcomm.h 4.06 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
/*
  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/>.
*/

Anders Nilsson's avatar
Anders Nilsson committed
23
24
25
#ifndef _LABCOMM_H_
#define _LABCOMM_H_

Oscar Olsson's avatar
Oscar Olsson committed
26
#include <stdarg.h>
27
#include <stdint.h>
28
#include <unistd.h>
Anders Blomdell's avatar
Anders Blomdell committed
29
#include <labcomm_error.h>
Oscar Olsson's avatar
Oscar Olsson committed
30
31
32
33

/* Forward declaration */
struct labcomm_encoder;
struct labcomm_decoder;
Anders Nilsson's avatar
Anders Nilsson committed
34
35
36
37

/*
 * Signature entry
 */
38
struct labcomm_signature {
Anders Nilsson's avatar
Anders Nilsson committed
39
40
  int type;
  char *name;
41
  int (*encoded_size)(struct labcomm_signature *, void *); // void * == encoded_sample *
Anders Nilsson's avatar
Anders Nilsson committed
42
43
  int size;
  unsigned char *signature; 
44
  int cached_encoded_size; // -1 if not initialized or type is variable size
45
};
Anders Nilsson's avatar
Anders Nilsson committed
46

Oscar Olsson's avatar
Oscar Olsson committed
47
48
49
50
/*
 * Error handling.
 */

51
/* The callback prototype for error handling.
Oscar Olsson's avatar
Oscar Olsson committed
52
 * First parameter is the error ID.
53
54
55
56
 * 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.
Oscar Olsson's avatar
Oscar Olsson committed
57
 */
58
59
typedef void (*labcomm_error_handler_callback)(enum labcomm_error error_id, 
					       size_t nbr_va_args, ...); 
Oscar Olsson's avatar
Oscar Olsson committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

/* 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. 
 */
void on_error_fprintf(enum labcomm_error error_id, size_t nbr_va_args, ...);

/* Register a callback for the error handler for this encoder. */
void labcomm_register_error_handler_encoder(struct labcomm_encoder *encoder, labcomm_error_handler_callback callback);

/* Register a callback for the error handler for this decoder. */
void labcomm_register_error_handler_decoder(struct labcomm_decoder *decoder, labcomm_error_handler_callback callback);

/* Get a string describing the supplied standrad labcomm error. */
const char *labcomm_error_get_str(enum labcomm_error error_id);

75
76
typedef int (*labcomm_handle_new_datatype_callback)(
  struct labcomm_decoder *decoder,
77
  struct labcomm_signature *sig);
Oscar Olsson's avatar
Oscar Olsson committed
78
79
80
81

void labcomm_decoder_register_new_datatype_handler(struct labcomm_decoder *d,
		labcomm_handle_new_datatype_callback on_new_datatype);

82
83
84
/*
 * Locking support (optional)
 */
Anders Blomdell's avatar
Anders Blomdell committed
85
struct labcomm_lock;
86

87
88
89
90
91
92
int labcomm_lock_free(struct labcomm_lock *lock);
int labcomm_lock_lock(struct labcomm_lock *lock);
int labcomm_lock_unlock(struct labcomm_lock *lock);
int labcomm_lock_wait(struct labcomm_lock *lock, useconds_t usec);
int labcomm_lock_notify_all(struct labcomm_lock *lock);

Anders Nilsson's avatar
Anders Nilsson committed
93
94
95
/*
 * Decoder
 */
96
97
struct labcomm_reader;

Anders Nilsson's avatar
Anders Nilsson committed
98
struct labcomm_decoder *labcomm_decoder_new(
99
  struct labcomm_reader *reader,
100
  struct labcomm_lock *lock);
Anders Nilsson's avatar
Anders Nilsson committed
101
102
103
104
105
106
107
int labcomm_decoder_decode_one(
  struct labcomm_decoder *decoder);
void labcomm_decoder_run(
  struct labcomm_decoder *decoder);
void labcomm_decoder_free(
  struct labcomm_decoder *decoder);

108
109
/* See labcomm_ioctl.h for predefined ioctl_action values */
int labcomm_decoder_ioctl(struct labcomm_decoder *decoder, 
110
			  uint32_t ioctl_action,
111
112
			  ...);

Anders Nilsson's avatar
Anders Nilsson committed
113
114
115
/*
 * Encoder
 */
116
117
struct labcomm_writer;

Anders Nilsson's avatar
Anders Nilsson committed
118
struct labcomm_encoder *labcomm_encoder_new(
119
  struct labcomm_writer *writer,
120
  struct labcomm_lock *lock);
Anders Nilsson's avatar
Anders Nilsson committed
121
122
void labcomm_encoder_free(
  struct labcomm_encoder *encoder);
123

124
125
/* See labcomm_ioctl.h for predefined ioctl_action values */
int labcomm_encoder_ioctl(struct labcomm_encoder *encoder, 
126
			  uint32_t ioctl_action,
127
			  ...);
Anders Nilsson's avatar
Anders Nilsson committed
128
129

#endif