Skip to content
Snippets Groups Projects
Commit f1cf1aa5 authored by Sven Gestegård Robertz's avatar Sven Gestegård Robertz
Browse files

refactored c pragma. still TODO: share decoder registry

parent 5340978e
No related branches found
No related tags found
No related merge requests found
...@@ -117,7 +117,7 @@ void labcomm_decoder_free( ...@@ -117,7 +117,7 @@ void labcomm_decoder_free(
struct labcomm_decoder *decoder); struct labcomm_decoder *decoder);
int labcomm_decoder_decode_one( int labcomm_decoder_decode_one(
struct labcomm_decoder *decoder); struct labcomm_decoder *decoder);
void labcomm_decoder_run( int labcomm_decoder_run(
struct labcomm_decoder *decoder); struct labcomm_decoder *decoder);
/* See labcomm_ioctl.h for predefined ioctl_action values */ /* See labcomm_ioctl.h for predefined ioctl_action values */
...@@ -137,12 +137,12 @@ struct labcomm_pragma_handler { ...@@ -137,12 +137,12 @@ struct labcomm_pragma_handler {
int foo; int foo;
}; };
typedef int (*labcomm_handle_pragma_callback)( typedef int (*labcomm_pragma_handler_callback)(
struct labcomm_decoder *decoder, struct labcomm_decoder *decoder,
char *pragma_type); char *pragma_type);
void labcomm_decoder_register_pragma_handler(struct labcomm_decoder *d, void labcomm_decoder_register_pragma_handler(struct labcomm_decoder *d,
labcomm_handle_pragma_callback pragma_dispatcher); labcomm_pragma_handler_callback pragma_dispatcher);
/* /*
* Encoder * Encoder
......
...@@ -35,6 +35,17 @@ struct sample_entry { ...@@ -35,6 +35,17 @@ struct sample_entry {
void *context; void *context;
}; };
//XXX move to pragma.ch
//int default_pragma_handler(struct labcomm_decoder *d, char *pragma_type);
int default_pragma_handler(struct labcomm_decoder *d, char *pragma_type)
{
printf("Default pragma handler got pragma: %s\n", pragma_type);
int res = labcomm_decoder_run(d);
printf("... %d:%s\n",res,strerror(-res));
return LABCOMM_PRAGMA;
}
struct labcomm_decoder { struct labcomm_decoder {
struct labcomm_reader *reader; struct labcomm_reader *reader;
int reader_allocated; int reader_allocated;
...@@ -46,6 +57,7 @@ struct labcomm_decoder { ...@@ -46,6 +57,7 @@ struct labcomm_decoder {
labcomm_handle_new_datatype_callback on_new_datatype; labcomm_handle_new_datatype_callback on_new_datatype;
LABCOMM_SIGNATURE_ARRAY_DEF(local, struct sample_entry); LABCOMM_SIGNATURE_ARRAY_DEF(local, struct sample_entry);
LABCOMM_SIGNATURE_ARRAY_DEF(remote_to_local, int); LABCOMM_SIGNATURE_ARRAY_DEF(remote_to_local, int);
labcomm_pragma_handler_callback pragma_handler;
}; };
struct labcomm_decoder *labcomm_decoder_new( struct labcomm_decoder *labcomm_decoder_new(
...@@ -73,6 +85,7 @@ struct labcomm_decoder *labcomm_decoder_new( ...@@ -73,6 +85,7 @@ struct labcomm_decoder *labcomm_decoder_new(
result->on_error = on_error_fprintf; result->on_error = on_error_fprintf;
LABCOMM_SIGNATURE_ARRAY_INIT(result->local, struct sample_entry); LABCOMM_SIGNATURE_ARRAY_INIT(result->local, struct sample_entry);
LABCOMM_SIGNATURE_ARRAY_INIT(result->remote_to_local, int); LABCOMM_SIGNATURE_ARRAY_INIT(result->remote_to_local, int);
result->pragma_handler = default_pragma_handler;
} }
return result; return result;
} }
...@@ -253,8 +266,7 @@ static int decode_pragma(struct labcomm_decoder *d, int len) ...@@ -253,8 +266,7 @@ static int decode_pragma(struct labcomm_decoder *d, int len)
} }
int bytes = labcomm_size_string(pragma_type); int bytes = labcomm_size_string(pragma_type);
int psize = len-bytes; int psize = len-bytes;
printf("got pragma: %s\n", pragma_type); if(d->pragma_handler) {
if(0 /*d->pragma_handler*/) {
//read the entire packet to a buffer and then run //read the entire packet to a buffer and then run
// decode on that through a bytearray_reader. // decode on that through a bytearray_reader.
// (to easily handle multiple labcomm packets in one metadata packet) // (to easily handle multiple labcomm packets in one metadata packet)
...@@ -267,18 +279,19 @@ static int decode_pragma(struct labcomm_decoder *d, int len) ...@@ -267,18 +279,19 @@ static int decode_pragma(struct labcomm_decoder *d, int len)
goto out; goto out;
} }
} }
printf("read %d bytes\n", psize); //printf("read %d bytes\n", psize);
//TODO: add wrapped decoders, and create pragma decoder that does not //TODO: add wrapped decoders, and create pragma decoder that does not
// expect a version packet // expect a version packet
struct labcomm_reader *pr = labcomm_bytearray_reader_new( struct labcomm_reader *pr = labcomm_bytearray_reader_new(
d->reader->memory, pragma_data, psize); d->reader->memory, pragma_data, psize);
struct labcomm_decoder *pd = labcomm_decoder_new( struct labcomm_decoder *pd = labcomm_decoder_new(
pr, d->error, d->memory, d->scheduler); pr, d->error, d->memory, d->scheduler);
/* d->prama_handler(pd, ptype, plen); */ pd->version_ok = 1;
labcomm_decoder_run(pd); printf("calling pragma_handler\n");
result = d->pragma_handler(pd, pragma_type);
printf("returned from pragma_handler\n");
internal_labcomm_decoder_free(pd); internal_labcomm_decoder_free(pd);
result = 0;
} else { } else {
result = decoder_skip(d, psize, LABCOMM_PRAGMA); result = decoder_skip(d, psize, LABCOMM_PRAGMA);
} }
...@@ -356,10 +369,12 @@ out: ...@@ -356,10 +369,12 @@ out:
return result; return result;
} }
void labcomm_decoder_run(struct labcomm_decoder *d) int labcomm_decoder_run(struct labcomm_decoder *d)
{ {
while (labcomm_decoder_decode_one(d) > 0) { int res;
while ( (res=labcomm_decoder_decode_one(d)) > 0) {
} }
return res;
} }
int labcomm_decoder_ioctl(struct labcomm_decoder *d, int labcomm_decoder_ioctl(struct labcomm_decoder *d,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment