From 912aba72803edb7aca8b78a8dfbd3b21d297f26a Mon Sep 17 00:00:00 2001
From: nifi <nifi>
Date: Mon, 22 Jun 2009 11:14:11 +0000
Subject: [PATCH] added init function in MAC driver for easier configuration

---
 core/net/mac/ctdma_mac.c        | 15 +++++++++++----
 core/net/mac/ctdma_mac.h        |  4 ++--
 core/net/mac/cxmac.c            | 17 ++++++++---------
 core/net/mac/cxmac.h            |  4 ++--
 core/net/mac/lpp.c              |  5 +++--
 core/net/mac/lpp.h              |  4 +++-
 core/net/mac/mac.h              |  9 +++++++--
 core/net/mac/nullmac.c          |  3 ++-
 core/net/mac/sicslowmac.c       |  3 ++-
 core/net/mac/tdma_mac.c         | 11 ++++++++---
 core/net/mac/xmac.c             |  3 ++-
 platform/sky/contiki-conf.h     |  5 ++++-
 platform/sky/contiki-sky-main.c | 25 ++++++++++++++++---------
 13 files changed, 70 insertions(+), 38 deletions(-)

diff --git a/core/net/mac/ctdma_mac.c b/core/net/mac/ctdma_mac.c
index 5d04467072..6ce287f1d6 100644
--- a/core/net/mac/ctdma_mac.c
+++ b/core/net/mac/ctdma_mac.c
@@ -28,7 +28,7 @@
  *
  * This file is part of the Contiki operating system.
  *
- * $Id: ctdma_mac.c,v 1.3 2009/03/12 21:58:20 adamdunkels Exp $
+ * $Id: ctdma_mac.c,v 1.4 2009/06/22 11:14:11 nifi Exp $
  */
 
 #include "contiki.h"
@@ -215,12 +215,16 @@ on(void)
 }
 /*---------------------------------------------------------------------------*/
 static int
-off(void)
+off(int keep_radio_on)
 {
-  return radio->off();
+  if(keep_radio_on) {
+    return radio->on();
+  } else {
+    return radio->off();
+  }
 }
 /*---------------------------------------------------------------------------*/
-void
+const struct mac_driver *
 ctdma_mac_init(const struct radio_driver *d)
 {
   int i;
@@ -232,9 +236,12 @@ ctdma_mac_init(const struct radio_driver *d)
   radio = d;
   radio->set_receive_function(input);
   radio->on();
+  return &ctdma_mac_driver;
 }
 /*---------------------------------------------------------------------------*/
 const struct mac_driver ctdma_mac_driver = {
+  "CTDMA",
+  ctdma_mac_init,
   send,
   read,
   set_receive_function,
diff --git a/core/net/mac/ctdma_mac.h b/core/net/mac/ctdma_mac.h
index 22e1f6daee..2e15653043 100644
--- a/core/net/mac/ctdma_mac.h
+++ b/core/net/mac/ctdma_mac.h
@@ -28,7 +28,7 @@
  *
  * This file is part of the Contiki operating system.
  *
- * $Id: ctdma_mac.h,v 1.1 2007/09/18 10:36:36 fros4943 Exp $
+ * $Id: ctdma_mac.h,v 1.2 2009/06/22 11:14:11 nifi Exp $
  */
 
 #ifndef __CTDMA_MAC_H__
@@ -39,6 +39,6 @@
 
 extern const struct mac_driver ctdma_mac_driver;
 
-void ctdma_mac_init(const struct radio_driver *r);
+const struct mac_driver *ctdma_mac_init(const struct radio_driver *r);
 
 #endif /* __CTDMA_MAC_H__ */
diff --git a/core/net/mac/cxmac.c b/core/net/mac/cxmac.c
index 66458b02ab..f34cdddc80 100644
--- a/core/net/mac/cxmac.c
+++ b/core/net/mac/cxmac.c
@@ -28,7 +28,7 @@
  *
  * This file is part of the Contiki operating system.
  *
- * $Id: cxmac.c,v 1.3 2009/03/12 21:58:20 adamdunkels Exp $
+ * $Id: cxmac.c,v 1.4 2009/06/22 11:14:11 nifi Exp $
  */
 
 /**
@@ -481,7 +481,7 @@ read(void)
   return 0;
 }
 /*---------------------------------------------------------------------------*/
-void
+const struct mac_driver *
 cxmac_init(const struct radio_driver *d)
 {
   radio_is_on = 0;
@@ -494,6 +494,7 @@ cxmac_init(const struct radio_driver *d)
 
   radio = d;
   radio->set_receive_function(input);
+  return &cxmac_driver;
 }
 /*---------------------------------------------------------------------------*/
 static int
@@ -511,22 +512,20 @@ on(void)
 }
 /*---------------------------------------------------------------------------*/
 static int
-off(int radio_status)
+off(int keep_radio_on)
 {
   should_stop = 1;
-  return radio->off();
-  
-/*  switch(radio_status) {
-  case MAC_RADIO_ON:
+  if(keep_radio_on) {
     return radio->on();
-  case MAC_RADIO_OFF:
+  } else {
     return radio->off();
   }
-  return 0; */
 }
 /*---------------------------------------------------------------------------*/
 const struct mac_driver cxmac_driver =
   {
+    "CXMAC",
+    cxmac_init,
     qsend,
     read,
     set_receive_function,
diff --git a/core/net/mac/cxmac.h b/core/net/mac/cxmac.h
index 87eefc5511..e8ba625286 100644
--- a/core/net/mac/cxmac.h
+++ b/core/net/mac/cxmac.h
@@ -28,7 +28,7 @@
  *
  * This file is part of the Contiki operating system.
  *
- * $Id: cxmac.h,v 1.1 2007/09/18 10:35:39 fros4943 Exp $
+ * $Id: cxmac.h,v 1.2 2009/06/22 11:14:11 nifi Exp $
  */
 
 /**
@@ -46,6 +46,6 @@
 
 extern const struct mac_driver cxmac_driver;
 
-void cxmac_init(const struct radio_driver *d);
+const struct mac_driver *cxmac_init(const struct radio_driver *d);
 
 #endif /* __CXMAC_H__ */
diff --git a/core/net/mac/lpp.c b/core/net/mac/lpp.c
index 8e42fdd04b..3a702d020c 100644
--- a/core/net/mac/lpp.c
+++ b/core/net/mac/lpp.c
@@ -28,7 +28,7 @@
  *
  * This file is part of the Contiki operating system.
  *
- * $Id: lpp.c,v 1.22 2009/05/10 21:09:05 adamdunkels Exp $
+ * $Id: lpp.c,v 1.23 2009/06/22 11:14:11 nifi Exp $
  */
 
 /**
@@ -818,8 +818,9 @@ off(int keep_radio_on)
   return 1;
 }
 /*---------------------------------------------------------------------------*/
-static const struct mac_driver lpp_driver = {
+const struct mac_driver lpp_driver = {
   "LPP",
+  lpp_init,
   send_packet,
   read_packet,
   set_receive_function,
diff --git a/core/net/mac/lpp.h b/core/net/mac/lpp.h
index 9587e8320a..be9152ad27 100644
--- a/core/net/mac/lpp.h
+++ b/core/net/mac/lpp.h
@@ -28,7 +28,7 @@
  *
  * This file is part of the Contiki operating system.
  *
- * $Id: lpp.h,v 1.1 2008/05/15 08:56:29 adamdunkels Exp $
+ * $Id: lpp.h,v 1.2 2009/06/22 11:14:11 nifi Exp $
  */
 
 /**
@@ -47,6 +47,8 @@
 #include "net/mac/mac.h"
 #include "dev/radio.h"
 
+extern const struct mac_driver lpp_driver;
+
 const struct mac_driver *lpp_init(const struct radio_driver *d);
 
 #endif /* __LPP_H__ */
diff --git a/core/net/mac/mac.h b/core/net/mac/mac.h
index c9f671075b..3f93b5bc8b 100644
--- a/core/net/mac/mac.h
+++ b/core/net/mac/mac.h
@@ -28,7 +28,7 @@
  *
  * This file is part of the Contiki operating system.
  *
- * $Id: mac.h,v 1.4 2008/07/02 14:10:28 adamdunkels Exp $
+ * $Id: mac.h,v 1.5 2009/06/22 11:14:11 nifi Exp $
  */
 
 /**
@@ -41,12 +41,17 @@
 #ifndef __MAC_H__
 #define __MAC_H__
 
+#include "dev/radio.h"
+
 /**
  * The structure of a MAC protocol driver in Contiki.
  */
 struct mac_driver {
   char *name;
-  
+
+  /** Initialize the MAC driver */
+  const struct mac_driver *(* init)(const struct radio_driver *r);
+
   /** Send a packet from the Rime buffer  */
   int (* send)(void);
 
diff --git a/core/net/mac/nullmac.c b/core/net/mac/nullmac.c
index 95a8cbe882..0b3a22d3c6 100644
--- a/core/net/mac/nullmac.c
+++ b/core/net/mac/nullmac.c
@@ -28,7 +28,7 @@
  *
  * This file is part of the Contiki operating system.
  *
- * $Id: nullmac.c,v 1.9 2009/03/12 21:58:20 adamdunkels Exp $
+ * $Id: nullmac.c,v 1.10 2009/06/22 11:14:11 nifi Exp $
  */
 
 /**
@@ -92,6 +92,7 @@ off(int keep_radio_on)
 /*---------------------------------------------------------------------------*/
 const struct mac_driver nullmac_driver = {
   "nullmac",
+  nullmac_init,
   send_packet,
   read_packet,
   set_receive_function,
diff --git a/core/net/mac/sicslowmac.c b/core/net/mac/sicslowmac.c
index dbc24779e0..86f13f12d9 100644
--- a/core/net/mac/sicslowmac.c
+++ b/core/net/mac/sicslowmac.c
@@ -28,7 +28,7 @@
  *
  * This file is part of the Contiki operating system.
  *
- * $Id: sicslowmac.c,v 1.2 2009/05/26 12:08:29 nifi Exp $
+ * $Id: sicslowmac.c,v 1.3 2009/06/22 11:14:11 nifi Exp $
  */
 
 
@@ -234,6 +234,7 @@ off(int keep_radio_on)
 /*---------------------------------------------------------------------------*/
 const struct mac_driver sicslowmac_driver = {
   "sicslowmac",
+  sicslowmac_init,
   send_packet,
   read_packet,
   set_receive_function,
diff --git a/core/net/mac/tdma_mac.c b/core/net/mac/tdma_mac.c
index 71e648c79f..14e1ea7399 100644
--- a/core/net/mac/tdma_mac.c
+++ b/core/net/mac/tdma_mac.c
@@ -28,7 +28,7 @@
  *
  * This file is part of the Contiki operating system.
  *
- * $Id: tdma_mac.c,v 1.6 2009/03/12 21:58:20 adamdunkels Exp $
+ * $Id: tdma_mac.c,v 1.7 2009/06/22 11:14:11 nifi Exp $
  */
 
 #include "contiki.h"
@@ -221,9 +221,13 @@ on(void)
 }
 /*---------------------------------------------------------------------------*/
 static int
-off(void)
+off(int keep_radio_on)
 {
-  return radio->off();
+  if(keep_radio_on) {
+    return radio->on();
+  } else {
+    return radio->off();
+  }
 }
 /*---------------------------------------------------------------------------*/
 const struct mac_driver *
@@ -243,6 +247,7 @@ tdma_mac_init(const struct radio_driver *d)
 /*---------------------------------------------------------------------------*/
 const struct mac_driver tdma_mac_driver = {
     "TDMA MAC",
+    tdma_mac_init,
     send,
     read,
     set_receive_function,
diff --git a/core/net/mac/xmac.c b/core/net/mac/xmac.c
index f3fdb7549c..856bc53018 100644
--- a/core/net/mac/xmac.c
+++ b/core/net/mac/xmac.c
@@ -28,7 +28,7 @@
  *
  * This file is part of the Contiki operating system.
  *
- * $Id: xmac.c,v 1.33 2009/05/06 15:05:28 adamdunkels Exp $
+ * $Id: xmac.c,v 1.34 2009/06/22 11:14:11 nifi Exp $
  */
 
 /**
@@ -745,6 +745,7 @@ turn_off(int keep_radio_on)
 const struct mac_driver xmac_driver =
   {
     "X-MAC",
+    xmac_init,
     qsend_packet,
     read_packet,
     set_receive_function,
diff --git a/platform/sky/contiki-conf.h b/platform/sky/contiki-conf.h
index 86210cce0a..3285804b15 100644
--- a/platform/sky/contiki-conf.h
+++ b/platform/sky/contiki-conf.h
@@ -1,9 +1,12 @@
 /* -*- C -*- */
-/* @(#)$Id: contiki-conf.h,v 1.53 2009/05/18 10:32:56 fros4943 Exp $ */
+/* @(#)$Id: contiki-conf.h,v 1.54 2009/06/22 11:14:11 nifi Exp $ */
 
 #ifndef CONTIKI_CONF_H
 #define CONTIKI_CONF_H
 
+/* Specifies the default MAC driver */
+#define MAC_CONF_DRIVER xmac_driver
+
 #define XMAC_CONF_COMPOWER 1
 #define XMAC_CONF_ANNOUNCEMENTS 1
 #define RIME_CONF_NO_POLITE_ANNOUCEMENTS 1
diff --git a/platform/sky/contiki-sky-main.c b/platform/sky/contiki-sky-main.c
index bcf954f6af..092c18ec0e 100644
--- a/platform/sky/contiki-sky-main.c
+++ b/platform/sky/contiki-sky-main.c
@@ -26,7 +26,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * @(#)$Id: contiki-sky-main.c,v 1.55 2009/05/12 17:32:49 adamdunkels Exp $
+ * @(#)$Id: contiki-sky-main.c,v 1.56 2009/06/22 11:14:11 nifi Exp $
  */
 
 #include <signal.h>
@@ -53,9 +53,6 @@
 #include "lib/random.h"
 
 #include "net/mac/frame802154.h"
-#include "net/mac/nullmac.h"
-#include "net/mac/xmac.h"
-#include "net/mac/lpp.h"
 
 #if WITH_UIP6
 #include "net/sicslowpan.h"
@@ -106,6 +103,20 @@ static uint8_t is_gateway;
 #include "experiment-setup.h"
 #endif
 
+#if WITH_NULLMAC
+#define MAC_DRIVER nullmac_driver
+#endif /* WITH_NULLMAC */
+
+#ifndef MAC_DRIVER
+#ifdef MAC_CONF_DRIVER
+#define MAC_DRIVER MAC_CONF_DRIVER
+#else
+#define MAC_DRIVER xmac_driver
+#endif /* MAC_CONF_DRIVER */
+#endif /* MAC_DRIVER */
+
+extern const struct mac_driver MAC_DRIVER;
+
 /*---------------------------------------------------------------------------*/
 #if 0
 int
@@ -271,11 +282,7 @@ main(int argc, char **argv)
   uip_router_register(&rimeroute);
 #endif /* UIP_CONF_ROUTER */
 #else /* WITH_UIP6 */
-#if WITH_NULLMAC
-  rime_init(nullmac_init(&cc2420_driver));
-#else /* WITH_NULLMAC */
-  rime_init(xmac_init(&cc2420_driver));
-#endif /* WITH_NULLMAC */
+  rime_init(MAC_DRIVER.init(&cc2420_driver));
   printf(" %s channel %u\n", rime_mac->name, RF_CHANNEL);
 #endif /* WITH_UIP6 */
 
-- 
GitLab