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

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

Oscar Olsson's avatar
Oscar Olsson committed
26
#include <stdarg.h>
27 28 29 30 31 32 33 34

#ifdef LABCOMM_COMPAT
  #include LABCOMM_COMPAT
#else
  #include <stdint.h>
  #include <unistd.h>
#endif

Anders Blomdell's avatar
Anders Blomdell committed
35 36
#include "labcomm_error.h"
#include "labcomm_scheduler.h"
Oscar Olsson's avatar
Oscar Olsson committed
37 38 39 40

/* Forward declaration */
struct labcomm_encoder;
struct labcomm_decoder;
Anders Nilsson's avatar
Anders Nilsson committed
41

42
#include "labcomm_type_signature.h"
Oscar Olsson's avatar
Oscar Olsson committed
43 44 45 46
/*
 * Error handling.
 */

47
/* The callback prototype for error handling.
Oscar Olsson's avatar
Oscar Olsson committed
48
 * First parameter is the error ID.
49 50 51 52
 * 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
53
 */
54 55
typedef void (*labcomm_error_handler_callback)(enum labcomm_error error_id, 
					       size_t nbr_va_args, ...); 
Oscar Olsson's avatar
Oscar Olsson committed
56 57 58 59

/* 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. 
 */
Anders Blomdell's avatar
Anders Blomdell committed
60
void labcomm2014_on_error_fprintf(enum labcomm_error error_id, size_t nbr_va_args, ...);
Oscar Olsson's avatar
Oscar Olsson committed
61 62 63 64 65 66 67 68 69 70

/* 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);

71 72
typedef int (*labcomm_handle_new_datatype_callback)(
  struct labcomm_decoder *decoder,
73
  struct labcomm_signature *sig);
Oscar Olsson's avatar
Oscar Olsson committed
74

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
/*
 * 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
 */
struct labcomm_memory;

void *labcomm_memory_alloc(struct labcomm_memory *m, int lifetime, size_t size);
void *labcomm_memory_realloc(struct labcomm_memory *m, int lifetime, 
			     void *ptr, size_t size);
void labcomm_memory_free(struct labcomm_memory *m, int lifetime, void *ptr);

Anders Nilsson's avatar
Anders Nilsson committed
90 91 92
/*
 * Decoder
 */
93 94
struct labcomm_reader;

Anders Nilsson's avatar
Anders Nilsson committed
95
struct labcomm_decoder *labcomm_decoder_new(
96
  struct labcomm_reader *reader,
Anders Blomdell's avatar
Anders Blomdell committed
97 98 99 100 101
  struct labcomm_error_handler *error,
  struct labcomm_memory *memory,
  struct labcomm_scheduler *scheduler);
void labcomm_decoder_free(
  struct labcomm_decoder *decoder);
Anders Nilsson's avatar
Anders Nilsson committed
102 103 104 105
int labcomm_decoder_decode_one(
  struct labcomm_decoder *decoder);
void labcomm_decoder_run(
  struct labcomm_decoder *decoder);
106 107 108
int labcomm_decoder_sample_ref_register(
  struct labcomm_decoder *decoder,
  const struct labcomm_signature *signature);
Anders Nilsson's avatar
Anders Nilsson committed
109

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

Anders Nilsson's avatar
Anders Nilsson committed
115 116 117
/*
 * Encoder
 */
118 119
struct labcomm_writer;

Anders Nilsson's avatar
Anders Nilsson committed
120
struct labcomm_encoder *labcomm_encoder_new(
121
  struct labcomm_writer *writer,
Anders Blomdell's avatar
Anders Blomdell committed
122 123 124
  struct labcomm_error_handler *error,
  struct labcomm_memory *memory,
  struct labcomm_scheduler *scheduler);
Anders Nilsson's avatar
Anders Nilsson committed
125 126
void labcomm_encoder_free(
  struct labcomm_encoder *encoder);
127 128 129
int labcomm_encoder_sample_ref_register(
  struct labcomm_encoder *encoder,
  const struct labcomm_signature *signature);
130

131 132
/* See labcomm_ioctl.h for predefined ioctl_action values */
int labcomm_encoder_ioctl(struct labcomm_encoder *encoder, 
133
			  uint32_t ioctl_action,
134
			  ...);
Anders Nilsson's avatar
Anders Nilsson committed
135

136 137
#define LABCOMM_VOID ((void*)1)

Anders Nilsson's avatar
Anders Nilsson committed
138
#endif