refactoring

This commit is contained in:
Olaf Rempel 2008-03-19 02:54:27 +01:00
parent 3aaa30cb63
commit f2ac3923e7
6 changed files with 269 additions and 89 deletions

View File

@ -1,5 +1,6 @@
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
@ -19,34 +20,6 @@ static gint socket_tag;
static void (*socket_status_cb)(int status); static void (*socket_status_cb)(int status);
static GByteArray *socket_stream; static GByteArray *socket_stream;
static int check_size_helper(int available,
int packet_size,
int expected_base_size,
int packet_data_size,
const char *name)
{
/* not enough data in buffer */
if (available < expected_base_size) {
printf("received incomplete %s\n", name);
return 0;
}
/* retest, after we know that packet_data_size is valid */
if (available < (expected_base_size + packet_data_size)) {
printf("received incomplete %s\n", name);
return 0;
}
/* packet has wrong size */
if (packet_size != (expected_base_size + packet_data_size)) {
printf("received invalid %s\n", name);
return -1;
}
// printf("received %s\n", name);
return packet_size;
}
static int tdcparser_send(struct tdc_pkt_header *pkt) static int tdcparser_send(struct tdc_pkt_header *pkt)
{ {
return write(socket_fd, pkt, pkt->size); return write(socket_fd, pkt, pkt->size);
@ -73,6 +46,77 @@ int tdcparser_send_getvars(int address)
return tdcparser_send(&pkt); return tdcparser_send(&pkt);
} }
int tdcparser_send_getvalue(int address, int id)
{
struct tdc_getvalue_request pkt;
pkt.cmd = (address << 4) | TDC_GETVALUE;
pkt.size = sizeof(pkt);
pkt.id = (id & 0xFF);
return tdcparser_send((struct tdc_pkt_header *)&pkt);
}
int tdcparser_send_setvalue(int address, int id)
{
struct tdc_var *var = tdcstore_get_var(address, id);
if (var == NULL)
return -1;
int datasize = (var->flags & TDC_SIZEMASK);
struct tdc_setvalue_request *pkt;
pkt = g_malloc0(sizeof(struct tdc_setvalue_request) + datasize);
pkt->cmd = (address << 4) | TDC_SETVALUE;
pkt->size = sizeof(pkt) + datasize;
pkt->id = (id & 0xFF);
memcpy(pkt->data, &var->data, datasize);
int retval = tdcparser_send((struct tdc_pkt_header *)pkt);
g_free(pkt);
return retval;
}
int tdcparser_send_request(int address, int interval, uint32_t *bitmap)
{
struct tdc_reqvalues_request pkt;
pkt.cmd = (address << 4) | TDC_REQVALUES;
pkt.size = sizeof(pkt);
pkt.interval = interval;
memcpy(pkt.varmap, bitmap, sizeof(pkt.varmap));
return tdcparser_send((struct tdc_pkt_header *)&pkt);
}
static int check_size_helper(int available,
int packet_size,
int expected_base_size,
int packet_data_size,
const char *name)
{
/* not enough data in buffer */
if (available < expected_base_size) {
// printf("received incomplete %s\n", name);
return 0;
}
/* retest, after we know that packet_data_size is valid */
if (available < (expected_base_size + packet_data_size)) {
// printf("received incomplete %s\n", name);
return 0;
}
/* packet has wrong size */
if (packet_size != (expected_base_size + packet_data_size)) {
// printf("received invalid %s\n", name);
return -1;
}
// printf("received %s\n", name);
return packet_size;
}
static int tdcparser_parse(void) static int tdcparser_parse(void)
{ {
int address = (socket_stream->data[0] & TDC_ADDRMASK) >> 4; int address = (socket_stream->data[0] & TDC_ADDRMASK) >> 4;
@ -92,7 +136,8 @@ static int tdcparser_parse(void)
if (remove <= 0) if (remove <= 0)
break; break;
tdcstore_set_name(address, pkt->name); tdcstore_create_board(address, pkt->name);
tdcstore_refresh_values(address, 250);
tdcparser_send_getvars(address); tdcparser_send_getvars(address);
} break; } break;
@ -110,7 +155,7 @@ static int tdcparser_parse(void)
if (remove <= 0) if (remove <= 0)
break; break;
tdcstore_set_var(address, tdcvar_create(pkt->id, pkt->flags, pkt->name, pkt->name_len)); tdcstore_create_var(address, tdcvar_create(pkt->id, pkt->flags, pkt->name, pkt->name_len));
} break; } break;
case TDC_GETVALUE: { case TDC_GETVALUE: {
@ -121,44 +166,52 @@ static int tdcparser_parse(void)
case (TDC_GETVALUE | TDC_REPLY): { case (TDC_GETVALUE | TDC_REPLY): {
struct tdc_getvalue_reply *pkt = (struct tdc_getvalue_reply *)socket_stream->data; struct tdc_getvalue_reply *pkt = (struct tdc_getvalue_reply *)socket_stream->data;
remove = check_size_helper(socket_stream->len, pkt->size, sizeof(*pkt), 0, "TDC_GETVALUE REPLY (1)");
/* check base packet */ /* at this point: invalid packet */
remove = check_size_helper(socket_stream->len, pkt->size, sizeof(*pkt), 0, "TDC_GETVALUE REPLY"); if (remove > 0)
if (remove <= 0) remove = 1;
/* retry with more bytes */
else if (remove == 0)
break; break;
/* base packet is complete, but exact size is wrong, get it from storage */
struct tdc_var *var = tdcstore_get_var(address, pkt->id); struct tdc_var *var = tdcstore_get_var(address, pkt->id);
if (var == NULL) { if (var == NULL) {
printf("unknown variable %d\n", pkt->id); printf("unknown variable %d\n", pkt->id);
remove = 1;
break; break;
} }
int varsize = (var->flags & TDC_SIZEMASK); int varsize = (var->flags & TDC_SIZEMASK);
remove = check_size_helper(socket_stream->len, pkt->size, sizeof(*pkt), varsize, "TDC_GETVALUE REPLY"); remove = check_size_helper(socket_stream->len, pkt->size, sizeof(*pkt), varsize, "TDC_GETVALUE REPLY (2)");
// TODO: adjust remove on error (valid reply with invalid data size)
if (remove <= 0) if (remove <= 0)
break; break;
tdcvar_update(var, pkt->data, varsize); tdcstore_update_var(address, pkt->id, pkt->data, varsize);
tdcstore_trigger_update(address, pkt->id);
} break; } break;
case TDC_SETVALUE: { case TDC_SETVALUE: {
struct tdc_setvalue_request *pkt = (struct tdc_setvalue_request *)socket_stream->data; struct tdc_setvalue_request *pkt = (struct tdc_setvalue_request *)socket_stream->data;
remove = check_size_helper(socket_stream->len, pkt->size, sizeof(*pkt), 0, "TDC_SETVALUE"); remove = check_size_helper(socket_stream->len, pkt->size, sizeof(*pkt), 0, "TDC_SETVALUE REPLY (1)");
if (remove <= 0) /* at this point: invalid packet */
if (remove > 0)
remove = 1;
/* retry with more bytes */
else if (remove == 0)
break; break;
/* base packet is complete, but exact size is wrong, get it from storage */
struct tdc_var *var = tdcstore_get_var(address, pkt->id); struct tdc_var *var = tdcstore_get_var(address, pkt->id);
if (var == NULL) { if (var == NULL) {
printf("unknown variable %d\n", pkt->id); printf("unknown variable %d\n", pkt->id);
remove = 1;
break; break;
} }
int varsize = (var->flags & TDC_SIZEMASK); int varsize = (var->flags & TDC_SIZEMASK);
remove = check_size_helper(socket_stream->len, pkt->size, sizeof(*pkt), varsize, "TDC_GETVALUE REPLY"); remove = check_size_helper(socket_stream->len, pkt->size, sizeof(*pkt), varsize, "TDC_SETVALUE REPLY (2)");
// TODO: adjust remove on error (valid reply with invalid data size) if (remove <= 0)
break;
/* nothing to do */ /* nothing to do */
} break; } break;

View File

@ -1,14 +1,19 @@
#ifndef TDC_PARSER_H_ #ifndef TDC_PARSER_H_
#define TDC_PARSER_H_ #define TDC_PARSER_H_
#include <stdint.h>
enum { enum {
STAT_DISCONNECTED = 0, STAT_DISCONNECTED = 0,
STAT_CONNECTING, STAT_CONNECTING,
STAT_CONNECTED STAT_CONNECTED
}; };
int tdcparser_send_getvars(int address);
void tdcparser_send_hello(void); void tdcparser_send_hello(void);
int tdcparser_send_getvars(int address);
int tdcparser_send_getvalue(int address, int id);
int tdcparser_send_setvalue(int address, int id);
int tdcparser_send_request(int address, int interval, uint32_t *bitmap);
void tdcparser_connect(char *addr_str, void (*cb)(int status)); void tdcparser_connect(char *addr_str, void (*cb)(int status));
void tdcparser_disconnect(void); void tdcparser_disconnect(void);

View File

@ -1,63 +1,180 @@
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "tdc_parser.h"
#include "tdc_proto.h"
#include "tdc_store.h" #include "tdc_store.h"
#include "tdc_variable.h" #include "tdc_variable.h"
#include "gui_variable_tab.h" #include "gui_variable_tab.h"
static char *boardname[8]; struct tdc_board {
static struct tdc_var *varmap[256]; char name[32];
struct tdc_var *varmap[256];
void tdcstore_set_name(int address, char *name) int refresh_graph_interval;
int refresh_interval;
gint refresh_tag;
};
static struct tdc_board *board_arr[8];
struct tdc_board * tdcstore_get_board(int address)
{ {
if (boardname[address & 0x7] != NULL) return board_arr[address & 0x7];
free(boardname[address & 0x7]);
boardname[address & 0x7] = strdup(name);
printf("ADDR%d:%s\n", address, boardname[address & 0x7]);
}
char * tdcstore_get_name(int address)
{
return boardname[address & 0x7];
}
int tdcstore_set_var(int address, struct tdc_var *var)
{
if (varmap[var->id] != NULL) {
gui_vartab_remove_var(var);
tdcvar_destroy(varmap[var->id]);
}
varmap[var->id] = var;
gui_vartab_add_var(var);
return 0;
} }
struct tdc_var * tdcstore_get_var(int address, int id) struct tdc_var * tdcstore_get_var(int address, int id)
{ {
return varmap[id & 0xFF]; struct tdc_board *board = tdcstore_get_board(address);
return (board != NULL) ? board->varmap[id & 0xFF] : NULL;
} }
void tdcstore_trigger_update(int address, int id) static int tdcstore_destroy_var(struct tdc_board *board, int id)
{ {
gui_vartab_update_var(varmap[id & 0xFF]); struct tdc_var *var = board->varmap[id & 0xFF];
if (var == NULL)
return -1;
gui_vartab_remove_var(var);
tdcvar_destroy(var);
board->varmap[id & 0xFF] = NULL;
return 0;
}
static int tdcstore_destroy_board(int address)
{
struct tdc_board *board = tdcstore_get_board(address);
if (board == NULL)
return -1;
int i;
for (i = 0; i < 256; i++) {
if (board->varmap[i] == NULL)
continue;
tdcstore_destroy_var(board, i);
}
g_free(board);
board_arr[address & 0x7] = NULL;
return 0;
}
void tdcstore_create_board(int address, char *name)
{
struct tdc_board *old = tdcstore_get_board(address);
if (old != NULL)
tdcstore_destroy_board(address);
struct tdc_board *board = g_malloc0(sizeof(struct tdc_board));
strncpy(board->name, name, 32);
printf("ADDR%d:%s\n", address, board->name);
board_arr[address & 0x7] = board;
}
int tdcstore_create_var(int address, struct tdc_var *var)
{
struct tdc_board *board = tdcstore_get_board(address);
if (board == NULL)
return -1;
struct tdc_var *old = tdcstore_get_var(address, var->id);
if (old != NULL)
tdcstore_destroy_var(board, var->id);
board->varmap[var->id] = var;
gui_vartab_add_var(var);
return 0;
}
int tdcstore_update_var(int address, int id, uint8_t *data, int len)
{
struct tdc_var *var = tdcstore_get_var(address, id);
if (var == NULL)
return -1;
tdcvar_update(var, data, len);
gui_vartab_update_var(var);
return 0;
}
static gint refresh_helper(gpointer data)
{
int i;
int address = (int)data;
for (i = 0; i < 256; i++) {
struct tdc_var *var = tdcstore_get_var(address, i);
/* unknown var, or value updated via refresh_graph */
if ((var == NULL) || (var->flags & TDC_GUI_GRAPH))
continue;
/* error while sending -> bail out, disable timeout */
if (tdcparser_send_getvalue(address, i) < 0)
return FALSE;
}
return TRUE;
}
int tdcstore_refresh_values(int address, int interval)
{
struct tdc_board *board = tdcstore_get_board(address);
if (board == NULL)
return -1;
int old_interval = board->refresh_interval;
if (old_interval != 0)
g_source_remove(board->refresh_tag);
board->refresh_tag = g_timeout_add(interval, refresh_helper, (gpointer)address);
return 0;
}
static void tdcstore_get_bitmap(int address, uint32_t *bitmap)
{
int i, cnt = 0;
uint32_t tmp = 0;
for (i = 0; i < 256; i++) {
struct tdc_var *var = tdcstore_get_var(address, i);
if ((var != NULL) && (var->flags & TDC_GUI_GRAPH))
tmp = (tmp << 1) | 0x01;
else
tmp = (tmp << 1);
cnt = (cnt +1) % 32;
if (cnt == 0) {
bitmap[i >> 5] = tmp;
tmp = 0;
}
}
}
int tdcstore_graph_refresh(int address, int interval)
{
struct tdc_board *board = tdcstore_get_board(address);
if (board == NULL)
return -1;
if (interval == -1)
interval = board->refresh_graph_interval;
uint32_t bitmap[8];
tdcstore_get_bitmap(address, bitmap);
tdcparser_send_request(address, interval, bitmap);
board->refresh_graph_interval = interval;
return 0;
} }
void tdcstore_flush(void) void tdcstore_flush(void)
{ {
int i; int i;
for (i = 0; i < 8; i++) { for (i = 0; i < 8; i++)
free(boardname[i]); tdcstore_destroy_board(i);
boardname[i] = NULL;
}
for (i = 0; i < 256; i++) {
gui_vartab_remove_var(varmap[i]);
tdcvar_destroy(varmap[i]);
varmap[i] = NULL;
}
} }

View File

@ -1,15 +1,19 @@
#ifndef TDC_STORE_H_ #ifndef TDC_STORE_H_
#define TDC_STORE_H_ #define TDC_STORE_H_
#include <stdint.h>
#include "tdc_variable.h" #include "tdc_variable.h"
void tdcstore_set_name(int address, char *name); struct tdc_board * tdcstore_get_board(int address);
char * tdcstore_get_name(int address);
int tdcstore_set_var(int address, struct tdc_var *var);
struct tdc_var * tdcstore_get_var(int address, int id); struct tdc_var * tdcstore_get_var(int address, int id);
void tdcstore_trigger_update(int address, int id); void tdcstore_create_board(int address, char *name);
int tdcstore_create_var(int address, struct tdc_var *var);
int tdcstore_update_var(int address, int id, uint8_t *data, int len);
int tdcstore_refresh_values(int address, int interval);
int tdcstore_graph_refresh(int address, int interval);
void tdcstore_flush(void); void tdcstore_flush(void);

View File

@ -170,7 +170,7 @@ void tdcvar_update(struct tdc_var *var, uint8_t *data, int len)
if ((var->flags & TDC_SIZEMASK) != len) if ((var->flags & TDC_SIZEMASK) != len)
return; return;
memcpy(&var->data_uint8, data, len); memcpy(&var->data, data, len);
} }
void tdcvar_destroy(struct tdc_var *var) void tdcvar_destroy(struct tdc_var *var)

View File

@ -7,6 +7,7 @@ struct tdc_var {
uint32_t id; uint32_t id;
uint32_t flags; uint32_t flags;
union { union {
uint8_t data;
uint8_t data_uint8; uint8_t data_uint8;
uint16_t data_uint16; uint16_t data_uint16;
uint32_t data_uint32; uint32_t data_uint32;