From 2ed270e5325fe2bf7e17f8b3c2d5ce018bd523e6 Mon Sep 17 00:00:00 2001
From: Sven Robertz <sven@cs.lth.se>
Date: Fri, 16 Dec 2011 09:42:23 +0100
Subject: [PATCH] read channel ID to reply on the same channel

---
 examples/simple/datagram/thr_example2.c       | 7 ++++---
 lib/c/experimental/ThrottleDrv/throttle_drv.c | 4 ++++
 lib/c/experimental/ThrottleDrv/throttle_drv.h | 1 +
 3 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/examples/simple/datagram/thr_example2.c b/examples/simple/datagram/thr_example2.c
index 74f752c..3dc209d 100644
--- a/examples/simple/datagram/thr_example2.c
+++ b/examples/simple/datagram/thr_example2.c
@@ -40,8 +40,8 @@ static int encode(int argc, char *argv[]) {
   else
   {
     p_thr_chn = thr_open_chn(dest_mac, chn_id, frag_size, freq,NULL);
-    p_thr_chn2 = thr_open_chn(dest_mac, 17, frag_size, 100,(thr_msg_handler_t)labcomm_decoder_decode_one);
-    encoder = labcomm_encoder_new(labcomm_thr_writer, p_thr_chn);
+    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);
     labcomm_encoder_register_simple_TwoInts(encoder);
     labcomm_encoder_register_simple_IntString(encoder);
     decoder = labcomm_decoder_new(labcomm_thr_reader, p_thr_chn2);
@@ -79,9 +79,10 @@ static int encode(int argc, char *argv[]) {
 
 static void handle_simple_TwoInts(simple_TwoInts *v,void *context) {
   unsigned char *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]);
-  struct thr_chan_t *ch = thr_open_chn(src, 17, (unsigned short)50, (unsigned short)100, NULL);
+  struct thr_chan_t *ch = thr_open_chn(src, ch_id, (unsigned short)50, (unsigned short)100, NULL);
   struct labcomm_encoder *enc = labcomm_encoder_new(labcomm_thr_writer, ch);
   labcomm_encoder_register_simple_TwoInts(enc);
   v->a *= 2;
diff --git a/lib/c/experimental/ThrottleDrv/throttle_drv.c b/lib/c/experimental/ThrottleDrv/throttle_drv.c
index 5852d73..5ecbd18 100644
--- a/lib/c/experimental/ThrottleDrv/throttle_drv.c
+++ b/lib/c/experimental/ThrottleDrv/throttle_drv.c
@@ -404,3 +404,7 @@ int thr_read(struct thr_chn_t* thr_chn, unsigned char* data, int length)
 unsigned char* get_sender_addr(struct thr_chn_t* ch) {
 	return ch->last_sender_adr;
 }
+
+unsigned char get_channel(struct thr_chn_t* ch) {
+	return ch->id;
+}
diff --git a/lib/c/experimental/ThrottleDrv/throttle_drv.h b/lib/c/experimental/ThrottleDrv/throttle_drv.h
index e231ba0..7ab0439 100644
--- a/lib/c/experimental/ThrottleDrv/throttle_drv.h
+++ b/lib/c/experimental/ThrottleDrv/throttle_drv.h
@@ -16,4 +16,5 @@ int thr_send(const struct thr_chn_t* thr_chn, const char* data, unsigned int len
 int thr_read(struct thr_chn_t* thr_chn, unsigned char* data, int length);
 
 unsigned char* get_sender_addr(struct thr_chn_t* ch);
+unsigned char get_channel(struct thr_chn_t* ch);
 #endif
-- 
GitLab