00001 /* Copyright (c) 2008, Swedish Institute of Computer Science 00002 * All rights reserved. 00003 * 00004 * Additional fixes for AVR contributed by: 00005 * 00006 * Colin O'Flynn coflynn@newae.com 00007 * Eric Gnoske egnoske@gmail.com 00008 * Blake Leverett bleverett@gmail.com 00009 * Mike Vidales mavida404@gmail.com 00010 * Kevin Brown kbrown3@uccs.edu 00011 * Nate Bohlmann nate@elfwerks.com 00012 * 00013 * All rights reserved. 00014 * 00015 * Redistribution and use in source and binary forms, with or without 00016 * modification, are permitted provided that the following conditions are met: 00017 * 00018 * * Redistributions of source code must retain the above copyright 00019 * notice, this list of conditions and the following disclaimer. 00020 * * Redistributions in binary form must reproduce the above copyright 00021 * notice, this list of conditions and the following disclaimer in 00022 * the documentation and/or other materials provided with the 00023 * distribution. 00024 * * Neither the name of the copyright holders nor the names of 00025 * contributors may be used to endorse or promote products derived 00026 * from this software without specific prior written permission. 00027 * 00028 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00029 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00030 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00031 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00032 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00033 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00034 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00035 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00036 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00037 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00038 * POSSIBILITY OF SUCH DAMAGE. 00039 **/ 00040 /** 00041 * \addtogroup wireless 00042 * @{ 00043 */ 00044 /** 00045 * \defgroup rf230mac RF230 MAC 00046 * @{ 00047 */ 00048 /** 00049 * \file 00050 * \brief The IEEE 802.15.4 (2003/2006) MAC utility functions. 00051 * 00052 */ 00053 00054 /** 00055 * \author 00056 * Eric Gnoske <egnoske@gmail.com> 00057 * Blake Leverett <bleverett@gmail.com> 00058 * Mike Vidales <mavida404@gmail.com> 00059 * Colin O'Flynn <coflynn@newae.com> 00060 * 00061 */ 00062 00063 /* Includes */ 00064 00065 #include <stdlib.h> 00066 #include <string.h> 00067 #include "zmac.h" 00068 #include "radio.h" 00069 #include "hal.h" 00070 #include "tcpip.h" 00071 #include "uip.h" 00072 #include "sicslowpan.h" 00073 #include "sicslowmac.h" 00074 00075 /* Globals */ 00076 /** \brief Interface structure for this module */ 00077 ieee_15_4_manager_t ieee15_4ManagerAddress; 00078 00079 //dataRequest_t dataRequestStructAddress; 00080 00081 /* Macros & Defines */ 00082 00083 uint8_t msduHandle; 00084 bool iAmCoord; 00085 bool autoModes; 00086 00087 00088 /** \brief The RF channel to use for all following transmissions and 00089 * receptions (see 6.1.2). Allowable values are 0-26 00090 */ 00091 uint8_t phyCurrentChannel; 00092 00093 /** \brief The 64-bit address of the coordinator/router through which 00094 * the network layer wishes to communicate. 00095 */ 00096 uint64_t macCoordExtendedAddress; 00097 00098 /** \brief The 16-bit short address assigned to the coordinator 00099 * through which the network layer wishes to communicate. A value 00100 * of 0xfffe indicates th the coordinator is only using it's 64-bit 00101 * extended address. A value of 0xffff indicates that this value is 00102 * unknown. The default value is 0xfff. 00103 */ 00104 uint16_t macCoordShortAddress; 00105 00106 /** \brief This address is the 64-bit address that will be used as 00107 * the mechanism to provide a destination to the upper layers. The 00108 * default value is 0xfff. 00109 */ 00110 uint64_t macDestAddress; 00111 00112 /** \brief The sequence number (0x00 - 0xff) added to the transmitted 00113 * data or MAC command frame. The default is a random value within 00114 * the range. 00115 */ 00116 uint8_t macDSN; 00117 00118 /** \brief The 16-bit identifier of the PAN on which the device is 00119 * sending to. If this value is 0xffff, the device is not 00120 * associated. The default value is 0xffff. 00121 */ 00122 uint16_t macDstPANId; 00123 00124 /** \brief The 16-bit identifier of the PAN on which the device is 00125 * operating. If this value is 0xffff, the device is not 00126 * associated. The default value is 0xffff. 00127 */ 00128 uint16_t macSrcPANId; 00129 00130 /** \brief The 16-bit address that the device uses to communicate in 00131 * the PAN. If the device is the PAN coordinator, this value shall 00132 * be chosen before a PAN is started. Otherwise, the address is 00133 * allocated by a coordinator during association. A value of 0xfffe 00134 * indicates that the device has associated but has not been 00135 * allocated an address. A value of 0xffff indicates that the 00136 * device does not have a short address. The default value is 00137 * 0xffff. 00138 */ 00139 uint16_t macShortAddress; 00140 00141 00142 /** \brief Our own long address. This needs to be read from EEPROM or 00143 * other secure memory storage. 00144 */ 00145 uint64_t macLongAddr; 00146 00147 /* Implementation */ 00148 00149 /** \brief Initializes the (quasi) 802.15.4 MAC. This function should 00150 * be called only once on startup. 00151 */ 00152 void 00153 mac_init(void) 00154 { 00155 volatile uint8_t buf[8]; 00156 00157 sicslowmac_resetRequest(true); 00158 00159 /* Set up the radio for auto mode operation. */ 00160 hal_subregister_write( SR_MAX_FRAME_RETRIES, 2 ); 00161 00162 /* Need to laod PANID for auto modes */ 00163 radio_set_pan_id(DEST_PAN_ID); 00164 00165 /* Buffer the uint64_t address for easy loading and debug. */ 00166 /** \todo Find a better location to load the IEEE address. */ 00167 buf[0] = macLongAddr & 0xFF; 00168 buf[1] = (macLongAddr >> 8) & 0xFF; 00169 buf[2] = (macLongAddr >> 16) & 0xFF; 00170 buf[3] = (macLongAddr >> 24) & 0xFF; 00171 buf[4] = (macLongAddr >> 32) & 0xFF; 00172 buf[5] = (macLongAddr >> 40) & 0xFF; 00173 buf[6] = (macLongAddr >> 48) & 0xFF; 00174 buf[7] = (macLongAddr >> 56) & 0xFF; 00175 /* Load the long address into the radio. This is required for auto mode */ 00176 /* operation. */ 00177 radio_set_extended_address((uint8_t *)&macLongAddr); 00178 00179 srand(1234 ); 00180 msduHandle = rand(); 00181 00182 /* Ping6 debug */ 00183 memcpy(uip_lladdr.addr, &macLongAddr, 8); 00184 00185 /* Convert expected byte order */ 00186 byte_reverse((uint8_t *)uip_lladdr.addr, 8); 00187 } 00188 00189 00190 /** @} */ 00191 /** @} */