Show data_replay.c syntax highlighted
#include "sim_sensor_nims.h"
#include "data_replay.h"
char *usageString = "usage: sdev_sim \n"
" -r <sampling-rate [samples/sec]> \n"
" [-s <size of ring-buff [bytes]>] \n"
" [-z <sample size [bytes] >] \n"
" For non-isochronous data use: -r 0 \n"
" For variable-length samples use: -z 0 \n"
"-f <sensor data file> \n"
"example: sdev_sim -r 48000 -s 20000 \n"
"\n";
int sdev_sim_usage(buf_t* buf)
{
char *sampleUsage = "Add usage information for extra functionality \n"
"implemented by sdev_sim here\n";
bufcpy(buf,sampleUsage,strlen(sampleUsage));
return 0;
}
int parse_sensor_options(int argc, char *argv[],replay_Opts_t* rbO)
{
char *key;
if ((key = misc_parse_out_option(&argc, argv, "help", 'h'))) {
sensor_dev_usage(argv[0],NULL);
}
if ((key = misc_parse_out_option(&argc, argv, "rate", 'r'))) {
rbO->sampleRate = atof(key);
} else {
elog(LOG_ERR,"ERROR No sample rate specified!\n");
sensor_dev_usage(argv[0],NULL);
}
if ((key = misc_parse_out_option(&argc, argv, "push-data", 'p'))) {
rbO->pushRate = atof(key);
} else {
elog(LOG_ERR,"ERROR No push data rate specified!\n");
sensor_dev_usage(argv[0],NULL);
}
if ((key = misc_parse_out_option(&argc, argv, "file", 'f'))) {
rbO->sensor_data_fname = key;
} else {
elog(LOG_ERR,"ERROR No sensor data file specified!\n");
sensor_dev_usage(argv[0],NULL);
}
if ((key = misc_parse_out_option(&argc, argv, "smplSz",'z'))) {
rbO->sampleSz = atoi(key);
if (rbO->sampleSz == 0) {
elog(LOG_NOTICE,"Assuming variable length samples\n");
}
} else {
rbO->sampleSz = 0;
elog(LOG_NOTICE,"Assuming variable length samples\n");
}
if ((key = misc_parse_out_option(&argc, argv, "size", 's'))) {
rbO->size = atoi(key);
} else {
elog(LOG_NOTICE,"Using default size for ring-buff: %d\n", RB_DEF_SIZE);
rbO->size = RB_DEF_SIZE;
}
return(1);
}
int sdev_sim_open(sdev_context_t* sdev)
{
/*
char * fname;
fname = (( sensor_replay_dev_t *) sdev_data(sdev) )->sensor_data_fname;
(( sensor_replay_dev_t *) sdev_data(sdev) )->data = read_input( fname );
*/
return 0;
}
int sdev_sim_close(sdev_context_t* sdev)
{
return 0;
}
void sdev_sim_destroy(void* data, g_event_t* event)
{
sensor_replay_dev_t *sde = (sensor_replay_dev_t *)data;
g_free(sde);
}
int sdevNonisoWrite(void *data, int interval, g_event_t *event)
{
sensor_replay_dev_t* sde = (sensor_replay_dev_t*) data;
sdev_context_t* ctx = sde->sdev;
int smplSz;
NimsSensorDataType *dataElem;
struct timeval t;
float pos_x, pos_y;
loc_world loc;
void * data_array = sde->data;
if ( data_array == NULL )
{
elog(LOG_DEBUG(1), "NO data to push\n");
return EVENT_RENEW;
}
if ( ! ctx )
{
elog(LOG_ERR,"fail to initialize ctx!\n");
}
smplSz = (sdev_get_opts(ctx))->r_opts.sample_size;
// Make variable length samples.
// if (smplSz == 0)
smplSz = 1;
dataElem = g_new0(NimsSensorDataType, smplSz);
pos_x = sde->mns->pos.cx;
pos_y = sde->mns->pos.cy;
// elog(LOG_DEBUG(1), "Current motor position: %f, %f\n", pos_x, pos_y);
loc.x = pos_x;
loc.y = pos_y;
* dataElem = value_at_loc(data_array, loc);
elog(LOG_DEBUG(1), "Current motor position: %f, %f: %f\n", pos_x, pos_y, *dataElem);
if ( * dataElem == INVALID_VALUE )
{
elog( LOG_ERR, "Invalid sensor data returned\n" );
return EVENT_RENEW;
}
sde->startSample++;
gettimeofday(&t,NULL);
// elog(LOG_ERR, "Pushing more data onto RB, TS: %s\n",misc_tv_to_str(&t));
sdev_noniso_push(ctx, dataElem, smplSz * sizeof(NimsSensorDataType), &t, NULL, NULL);
free(dataElem);
return EVENT_RENEW;
}
void sensor_dev_usage(const char *s,buf_t *buf)
{
buf_t* tmpBuf = buf_new();
if (buf == NULL) {
fprintf(stderr, usageString);
exit(1);
} else {
bufprintf(tmpBuf,usageString);
bufcpy(buf,tmpBuf->buf,tmpBuf->len);
}
buf_free( tmpBuf );
}
See more files for this project here