Commit 15d856a7 authored by Sven Robertz's avatar Sven Robertz
Browse files

more cleanup

parent 904b2663
......@@ -10,8 +10,7 @@ static int encode(int argc, char *argv[]) {
struct thr_chn_t *p_thr_chn = NULL;
struct labcomm_encoder *encoder;
int i, j;
// unsigned char dest_mac[ETH_ADR_SIZE] = {0x00, 0x09, 0x6b, 0x10, 0xf3, 0x80}; /* other host MAC address, hardcoded...... :-( */
unsigned char dest_mac[ETH_ADR_SIZE] = {0x00, 0x09, 0x6b, 0xe3, 0x81, 0xbf}; /* other host MAC address, hardcoded...... :-( */
struct ether_addr dest_mac;
unsigned char chn_id = 0x01;
unsigned short frag_size = 60;
unsigned short freq = 1000; /* milliseconds */
......@@ -19,7 +18,7 @@ static int encode(int argc, char *argv[]) {
char *ifname = argv[1];
char *dest_mac_str = argv[2];
if(parse_MAC_address(dest_mac_str, dest_mac)) {
if(parse_MAC_address(dest_mac_str, &dest_mac)) {
printf("failed to parse dest MAC address\n");
return 1;
}
......@@ -30,7 +29,7 @@ static int encode(int argc, char *argv[]) {
}
else
{
p_thr_chn = thr_open_chn(dest_mac, chn_id, frag_size, freq, NULL);
p_thr_chn = thr_open_chn(&dest_mac, chn_id, frag_size, freq, NULL);
encoder = labcomm_encoder_new(labcomm_thr_writer, p_thr_chn);
labcomm_encoder_register_simple_TwoInts(encoder);
labcomm_encoder_register_simple_IntString(encoder);
......@@ -62,7 +61,7 @@ static int decode(int argc, char *argv[]) {
struct thr_chn_t *p_thr_chn = NULL;
struct labcomm_decoder *decoder;
void *context = NULL;
unsigned char dest_mac[ETH_ADR_SIZE] = {0x00, 0x09, 0x6b, 0x10, 0xf3, 0x80}; /* other host MAC address, hardcoded...... :-( */
struct ether_addr dest_mac;
int ret = 0;
unsigned char chn_id = 0x01;
unsigned short frag_size = 60;
......@@ -76,7 +75,7 @@ static int decode(int argc, char *argv[]) {
}
else
{
p_thr_chn = thr_open_chn(dest_mac, chn_id, frag_size, freq, (thr_msg_handler_t)labcomm_decoder_decode_one);
p_thr_chn = thr_open_chn(&dest_mac, chn_id, frag_size, freq, (thr_msg_handler_t)labcomm_decoder_decode_one);
decoder = labcomm_decoder_new(labcomm_thr_reader, p_thr_chn);
if (!decoder)
{
......
......@@ -8,18 +8,17 @@
static void handle_simple_TwoInts_foo(simple_TwoInts *v,void *context) {
unsigned char *src = get_sender_addr((struct thr_chn_t *)context);
struct ether_addr *src = get_sender_addr((struct thr_chn_t *)context);
printf("Got TwoInts. a=%d, b=%d\n", v->a, v->b);
printf("... src addr: %x:%x:%x:%x:%x:%x\n", src[0], src[1], src[2], src[3], src[4], src[5]);
}
static int encode(int argc, char *argv[]) {
struct thr_chn_t *p_thr_chn2 = NULL;
struct thr_chn_t *p_thr_chn = NULL;
struct labcomm_encoder *encoder;
struct labcomm_decoder *decoder;
int i, j;
// unsigned char dest_mac[ETH_ADR_SIZE] = {0x00, 0x09, 0x6b, 0x10, 0xf3, 0x80}; /* other host MAC address, hardcoded...... :-( */
unsigned char dest_mac[ETH_ADR_SIZE] = {0x00, 0x09, 0x6b, 0xe3, 0x81, 0xbf}; /* other host MAC address, hardcoded...... :-( */
struct ether_addr dest_mac;
unsigned char chn_id = 0x01;
unsigned short frag_size = 60;
unsigned short freq = 1000; /* milliseconds */
......@@ -27,7 +26,7 @@ static int encode(int argc, char *argv[]) {
char *ifname = argv[1];
char *dest_mac_str = argv[2];
if(parse_MAC_address(dest_mac_str, dest_mac)) {
if(parse_MAC_address(dest_mac_str, &dest_mac)) {
printf("failed to parse dest MAC address\n");
return 1;
}
......@@ -38,17 +37,17 @@ static int encode(int argc, char *argv[]) {
}
else
{
p_thr_chn2 = thr_open_chn(dest_mac, chn_id, frag_size, freq,(thr_msg_handler_t)labcomm_decoder_decode_one);
encoder = labcomm_encoder_new(labcomm_thr_writer, p_thr_chn2);
p_thr_chn = thr_open_chn(&dest_mac, chn_id, frag_size, freq,(thr_msg_handler_t)labcomm_decoder_decode_one);
encoder = labcomm_encoder_new(labcomm_thr_writer, p_thr_chn);
labcomm_encoder_register_simple_TwoInts(encoder);
labcomm_encoder_register_simple_IntString(encoder);
decoder = labcomm_decoder_new(labcomm_thr_reader, p_thr_chn2);
decoder = labcomm_decoder_new(labcomm_thr_reader, p_thr_chn);
if (!decoder)
{
printf("Failed to allocate decoder %s:%d\n", __FUNCTION__, __LINE__);
return 1;
}
labcomm_decoder_register_simple_TwoInts(decoder, handle_simple_TwoInts_foo, p_thr_chn2);
labcomm_decoder_register_simple_TwoInts(decoder, handle_simple_TwoInts_foo, p_thr_chn);
int ret;
unsigned char data[200];
......@@ -64,18 +63,18 @@ static int encode(int argc, char *argv[]) {
printf("Encoding TwoInts, a=%d, b=%d\n", ti.a, ti.b);
labcomm_encode_simple_TwoInts(encoder, &ti);
printf("Decoding:\n");
thr_receive(p_thr_chn2, data, decoder);
thr_receive(p_thr_chn2, data, decoder);
thr_receive(p_thr_chn, data, decoder);
thr_receive(p_thr_chn, data, decoder);
labcomm_encoder_free(encoder);
labcomm_decoder_free(decoder);
thr_close_chn(p_thr_chn2);
thr_close_chn(p_thr_chn);
//client_exit(fd);
}
}
static void handle_simple_TwoInts(simple_TwoInts *v,void *context) {
unsigned char *src = get_sender_addr((struct thr_chn_t *)context);
struct ether_addr *src = get_sender_addr((struct thr_chn_t *)context);
unsigned char ch_id = get_channel((struct thr_chn_t *)context);
printf("Got TwoInts. a=%d, b=%d\n", v->a, v->b);
printf("... src addr: %x:%x:%x:%x:%x:%x\n", src[0], src[1], src[2], src[3], src[4], src[5]);
......@@ -91,14 +90,15 @@ static void handle_simple_TwoInts(simple_TwoInts *v,void *context) {
static void handle_simple_IntString(simple_IntString *v,void *context) {
printf("Got IntString. x=%d, s=%s\n", v->x, v->s);
unsigned char *src = get_sender_addr((struct thr_chn_t *)context);
struct ether_addr *src = get_sender_addr((struct thr_chn_t *)context);
printf("... src addr: %x:%x:%x:%x:%x:%x\n", src[0], src[1], src[2], src[3], src[4], src[5]);
}
static int decode(int argc, char *argv[]) {
struct thr_chn_t *p_thr_chn = NULL;
struct labcomm_decoder *decoder;
unsigned char dest_mac[ETH_ADR_SIZE] = {0x00, 0x09, 0x6b, 0x10, 0xf3, 0x80}; /* other host MAC address, hardcoded...... :-( */
struct ether_addr dest_mac; // Not used in this simple decoder example.
// The return channel is opened in the handler
int ret = 0;
unsigned char chn_id = 0x01;
unsigned short frag_size = 60;
......@@ -112,7 +112,7 @@ static int decode(int argc, char *argv[]) {
}
else
{
p_thr_chn = thr_open_chn(dest_mac, chn_id, frag_size, freq, (thr_msg_handler_t)labcomm_decoder_decode_one);
p_thr_chn = thr_open_chn(&dest_mac, chn_id, frag_size, freq, (thr_msg_handler_t)labcomm_decoder_decode_one);
decoder = labcomm_decoder_new(labcomm_thr_reader, p_thr_chn);
if (!decoder)
{
......
......@@ -5,10 +5,9 @@
#include "stdlib.h"
#include <string.h>
#include <time.h>
#include <arpa/inet.h>
// #include <arpa/inet.h>
#define THROTTLENET_PROTO 0x544e
#define ETH_ADR_LEN 6
#define THR_DST_ADR_POS 0
#define THR_SRC_ADR_POS (THR_DST_ADR_POS + sizeof(((thr_header_t*)0)->dst_adr)) //6
......@@ -20,8 +19,8 @@
#define THR_PAYLOAD_POS (THR_FRAG_LEN_POS + sizeof(((thr_header_t*)0)->frag_len)) //21
#define THR_MSG_HEADER_LEN THR_PAYLOAD_POS
#define THR_MSG_DST_ADR(thr_msg) (unsigned char*)(&thr_msg[THR_DST_ADR_POS])
#define THR_MSG_SRC_ADR(thr_msg) (unsigned char*)(&thr_msg[THR_SRC_ADR_POS])
#define THR_MSG_DST_ADR(thr_msg) (struct ether_addr*)(&thr_msg[THR_DST_ADR_POS])
#define THR_MSG_SRC_ADR(thr_msg) (struct ether_addr*)(&thr_msg[THR_SRC_ADR_POS])
#define THR_MSG_ETH_TYP(thr_msg) *(unsigned short*)(&thr_msg[THR_ETH_TYP_POS])
#define THR_MSG_CHN_ID(thr_msg) *(unsigned char*)(&thr_msg[THR_CHN_ID_POS])
#define THR_MSG_FRAG_NUM(thr_msg) *(unsigned short*)(&thr_msg[THR_FRAG_NUM_POS])
......@@ -33,21 +32,21 @@
/* local type for the Throttle Channel structure */
struct thr_chn_t
{
unsigned char dst_adr[ETH_ADR_LEN];/* destination MAC address */
struct ether_addr dst_adr; /* destination MAC address */
unsigned char id; /* Channel id */
unsigned short frag_size; /* Fragment size */
unsigned short freq; /* Message transmission frequency */
thr_msg_handler_t funct; /* Callback function invoked at the reception */
unsigned short msg_length;
unsigned char* p_msg;
unsigned char last_sender_adr[ETH_ADR_LEN];/* src MAC address of last message */
struct ether_addr last_sender_adr;/* src MAC address of last message */
};
/* Type for the Throttle message */
typedef struct
{
unsigned char dst_adr[ETH_ADR_LEN]; /* destination MAC address */
unsigned char src_adr[ETH_ADR_LEN]; /* source MAC address */
struct ether_addr dst_adr; /* destination MAC address */
struct ether_addr src_adr; /* source MAC address */
unsigned short eth_type; /* ethernet packet type */
unsigned char chn_id; /* channel identification */
unsigned short frag_num; /* fragment number */
......@@ -105,7 +104,7 @@ int thr_init(const char* eth_int_name)
* @retval struct thr_chn_t*: pointer to the Channel structure. NULL on error and errno is set appropriately.
*
*******************************************************************************/
struct thr_chn_t* thr_open_chn(const unsigned char* dst_adr, unsigned char chn_id, unsigned char frag_size, unsigned short freq, const thr_msg_handler_t funct)
struct thr_chn_t* thr_open_chn(const struct ether_addr* dst_adr, unsigned char chn_id, unsigned char frag_size, unsigned short freq, const thr_msg_handler_t funct)
{
struct thr_chn_t* tmp_chn = NULL; /* pointer to Channel structure */
......@@ -128,7 +127,7 @@ struct thr_chn_t* thr_open_chn(const unsigned char* dst_adr, unsigned char chn_i
else
{
/* Update the Channel structure */
memcpy(tmp_chn->dst_adr, dst_adr, sizeof(tmp_chn->dst_adr));
memcpy(&tmp_chn->dst_adr, dst_adr, sizeof(tmp_chn->dst_adr));
tmp_chn->id = chn_id;
tmp_chn->frag_size = frag_size;
tmp_chn->freq = freq;
......@@ -192,7 +191,7 @@ int thr_send(const struct thr_chn_t* thr_chn, const char* data, unsigned int len
else
{
/* Compose the Ethernet Frame to be sent */
memcpy(THR_MSG_DST_ADR(thr_msg), &thr_chn->dst_adr[0], ETH_ADR_LEN); /* Destiantion MAC Address */
memcpy(THR_MSG_DST_ADR(thr_msg), &thr_chn->dst_adr, sizeof(thr_chn->dst_adr)); /* Destiantion MAC Address */
eth_getMACadr(eth_int, THR_MSG_SRC_ADR(thr_msg)); /* Source MAC Address */
THR_MSG_ETH_TYP(thr_msg) = htons(THROTTLENET_PROTO); /* Ethernet Packet Type */
THR_MSG_CHN_ID(thr_msg) = thr_chn->id; /* Channel identification */
......@@ -321,7 +320,7 @@ int thr_receive(struct thr_chn_t* thr_chn, unsigned char* data, void* param)
#endif
thr_chn->msg_length = ret;
memcpy(thr_chn->p_msg, data, ret); /* copy the msg into the thr structure */
memcpy(thr_chn->last_sender_adr, THR_MSG_SRC_ADR(thr_msg), 6); /* ... and the address of the sender */
memcpy(&thr_chn->last_sender_adr, THR_MSG_SRC_ADR(thr_msg), 6); /* ... and the address of the sender */
#ifdef DEBUG
printf("thr_receive: calling %x\n", thr_chn->funct);
#endif
......@@ -401,8 +400,8 @@ int thr_read(struct thr_chn_t* thr_chn, unsigned char* data, int length)
return(ret);
}
unsigned char* get_sender_addr(struct thr_chn_t* ch) {
return ch->last_sender_adr;
struct ether_addr* get_sender_addr(struct thr_chn_t* ch) {
return &ch->last_sender_adr;
}
unsigned char get_channel(struct thr_chn_t* ch) {
......
#ifndef _THROTTLE_DRV_H_
#define _THROTTLE_DRV_H_
#define ETH_ADR_SIZE 6
//#define ETH_ADR_SIZE 6
#include <net/ethernet.h>
/* typedef of the callback used to manage the received Ethernet Frame (the User Data)*/
typedef int (*thr_msg_handler_t)(void* data);
......@@ -9,12 +13,12 @@ struct thr_chn_t;
int thr_init(const char* eth_int);
struct thr_chn_t* thr_open_chn(const unsigned char* dst_adr, unsigned char chn_id, unsigned char frag_size, unsigned short freq, thr_msg_handler_t funct);
struct thr_chn_t* thr_open_chn(const struct ether_addr* dst_adr, unsigned char chn_id, unsigned char frag_size, unsigned short freq, thr_msg_handler_t funct);
void thr_close_chn(struct thr_chn_t* c);
int thr_receive (struct thr_chn_t* thr_chn, unsigned char* data, void* param);
int thr_send(const struct thr_chn_t* thr_chn, const char* data, unsigned int length);
int thr_read(struct thr_chn_t* thr_chn, unsigned char* data, int length);
unsigned char* get_sender_addr(struct thr_chn_t* ch);
struct ether_addr * get_sender_addr(struct thr_chn_t* ch);
unsigned char get_channel(struct thr_chn_t* ch);
#endif
#include <stdio.h>
#include "ethaddr.h"
#if ETH_ALEN != 6
#warning "Assumption that ETH_ALEN == 6 appears false. Here be dragons."
#endif
/* parse a string on the format 00:01:02:0d:0e:0f into a char array
* the mac_addr argument must be at least six bytes.
* returns 0 on success, or -1 on error
*/
int parse_MAC_address(const char *str, unsigned char mac_addr[])
int parse_MAC_address(const char *str, struct ether_addr *mac_addr)
{
int res = sscanf(str, "%x:%x:%x:%x:%x:%x", &mac_addr[0], &mac_addr[1],
&mac_addr[2], &mac_addr[3], &mac_addr[4], &mac_addr[5]);
int res = sscanf(str, "%x:%x:%x:%x:%x:%x", &mac_addr->ether_addr_octet[0],
&mac_addr->ether_addr_octet[1],
&mac_addr->ether_addr_octet[2],
&mac_addr->ether_addr_octet[3],
&mac_addr->ether_addr_octet[4],
&mac_addr->ether_addr_octet[5]);
return (res == 6? 0 : -1);
}
......
#ifndef ETHADDR_H
#define ETHADDR_H
#include <net/ethernet.h>
/* parse a string on the format 00:01:02:0d:0e:0f into a char array
* the mac_addr argument must be at least six bytes.
* returns 0 on success, or -1 on error
*/
int parse_MAC_address(const char *str, unsigned char mac_addr[]);
int parse_MAC_address(const char *str, struct ether_addr *mac_addr);
#endif
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment