]> oss.titaniummirror.com Git - tinyos-2.x.git/blob - tos/lib/mac/tkn154/DisassociateP.nc
+ modified radio driver (PHY) interfaces: CSMA-CA algorithm is now pushed to radio...
[tinyos-2.x.git] / tos / lib / mac / tkn154 / DisassociateP.nc
1 /*
2 * Copyright (c) 2008, Technische Universitaet Berlin
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * - Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * - Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * - Neither the name of the Technische Universitaet Berlin nor the names
14 * of its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
23 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
25 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
27 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * - Revision -------------------------------------------------------------
30 * $Revision$
31 * $Date$
32 * @author Jan Hauer <hauer@tkn.tu-berlin.de>
33 * ========================================================================
34 */
35
36
37 #include "TKN154_MAC.h"
38
39 module DisassociateP
40 {
41 provides
42 {
43 interface Init;
44 interface MLME_DISASSOCIATE;
45 } uses {
46
47 interface FrameTx as DisassociationIndirectTx;
48 interface FrameTx as DisassociationDirectTx;
49 interface FrameTx as DisassociationToCoord;
50
51 interface FrameRx as DisassociationDirectRxFromCoord;
52 interface FrameExtracted as DisassociationExtractedFromCoord;
53 interface FrameRx as DisassociationRxFromDevice;
54
55 interface Pool<ieee154_txframe_t> as TxFramePool;
56 interface Pool<ieee154_txcontrol_t> as TxControlPool;
57 interface MLME_GET;
58 interface FrameUtility;
59 interface IEEE154Frame as Frame;
60 interface Get<uint64_t> as LocalExtendedAddress;
61 interface Ieee802154Debug as Debug;
62 }
63 }
64 implementation
65 {
66 enum {
67 S_IDLE = 0xFF,
68 };
69 uint8_t m_payloadDisassocRequest[2];
70 uint8_t m_coordAddrMode;
71 bool m_disAssociationOngoing;
72
73 command error_t Init.init()
74 {
75 m_payloadDisassocRequest[0] = S_IDLE;
76 m_coordAddrMode = 0;
77 m_disAssociationOngoing = FALSE;
78 return SUCCESS;
79 }
80
81 /* ------------------- MLME_DISASSOCIATE (initiating) ------------------- */
82
83 command ieee154_status_t MLME_DISASSOCIATE.request (
84 uint8_t DeviceAddrMode,
85 uint16_t DevicePANID,
86 ieee154_address_t DeviceAddress,
87 ieee154_disassociation_reason_t DisassociateReason,
88 bool TxIndirect,
89 ieee154_security_t *security
90 )
91 {
92 ieee154_status_t status = IEEE154_SUCCESS;
93 ieee154_txframe_t *txFrame=0;
94 ieee154_txcontrol_t *txControl=0;
95 ieee154_address_t srcAddress;
96
97 if (security && security->SecurityLevel)
98 status = IEEE154_UNSUPPORTED_SECURITY;
99 else if (call MLME_GET.macPANId() != DevicePANID ||
100 (DeviceAddrMode != ADDR_MODE_SHORT_ADDRESS && DeviceAddrMode != ADDR_MODE_EXTENDED_ADDRESS))
101 status = IEEE154_INVALID_PARAMETER;
102 else if (m_disAssociationOngoing || !(txFrame = call TxFramePool.get()))
103 status = IEEE154_TRANSACTION_OVERFLOW;
104 else if (!(txControl = call TxControlPool.get())){
105 call TxFramePool.put(txFrame);
106 status = IEEE154_TRANSACTION_OVERFLOW;
107 }
108 if (status == IEEE154_SUCCESS){
109 txFrame->header = &txControl->header;
110 txFrame->metadata = &txControl->metadata;
111 srcAddress.extendedAddress = call LocalExtendedAddress.get();
112 txFrame->headerLen = call FrameUtility.writeHeader(
113 txFrame->header->mhr,
114 DeviceAddrMode,
115 call MLME_GET.macPANId(),
116 &DeviceAddress,
117 ADDR_MODE_EXTENDED_ADDRESS,
118 call MLME_GET.macPANId(),
119 &srcAddress,
120 TRUE);
121 txFrame->header->mhr[MHR_INDEX_FC1] = FC1_ACK_REQUEST | FC1_FRAMETYPE_CMD | FC1_PAN_ID_COMPRESSION;
122 txFrame->header->mhr[MHR_INDEX_FC2] = FC2_SRC_MODE_EXTENDED |
123 (DeviceAddrMode == ADDR_MODE_SHORT_ADDRESS ? FC2_DEST_MODE_SHORT : FC2_DEST_MODE_EXTENDED);
124 m_payloadDisassocRequest[0] = CMD_FRAME_DISASSOCIATION_NOTIFICATION;
125 m_payloadDisassocRequest[1] = DisassociateReason;
126 txFrame->payload = m_payloadDisassocRequest;
127 txFrame->payloadLen = 2;
128 m_disAssociationOngoing = TRUE;
129 if ((DeviceAddrMode == ADDR_MODE_SHORT_ADDRESS &&
130 DeviceAddress.shortAddress == call MLME_GET.macCoordShortAddress()) ||
131 (DeviceAddrMode == ADDR_MODE_EXTENDED_ADDRESS &&
132 DeviceAddress.extendedAddress == call MLME_GET.macCoordExtendedAddress())){
133 status = call DisassociationToCoord.transmit(txFrame);
134 } else if (TxIndirect) {
135 status = call DisassociationIndirectTx.transmit(txFrame);
136 } else {
137 status = call DisassociationDirectTx.transmit(txFrame);
138 }
139 if (status != IEEE154_SUCCESS){
140 m_disAssociationOngoing = FALSE;
141 call TxFramePool.put(txFrame);
142 call TxControlPool.put(txControl);
143 }
144 }
145 call Debug.log(DEBUG_LEVEL_INFO, DISSASSOCIATE_REQUEST, status, 0, 0);
146 return status;
147 }
148
149 event void DisassociationToCoord.transmitDone(ieee154_txframe_t *data, ieee154_status_t status)
150 {
151 // transmitted a disassociation notification to our coordinator
152 uint8_t *mhr = MHR(data), srcAddrOffset = 7;
153 uint8_t DeviceAddrMode = (mhr[MHR_INDEX_FC2] & FC2_SRC_MODE_MASK) >> FC2_SRC_MODE_OFFSET;
154 uint16_t DevicePANID = *((nxle_uint16_t*) (&(mhr[MHR_INDEX_ADDRESS])));
155 ieee154_address_t DeviceAddress;
156 if ((mhr[MHR_INDEX_FC2] & FC2_DEST_MODE_MASK) == FC2_DEST_MODE_EXTENDED)
157 srcAddrOffset += 6;
158 call FrameUtility.convertToNative(&DeviceAddress.extendedAddress, &mhr[srcAddrOffset]);
159 call TxControlPool.put((ieee154_txcontrol_t*) ((uint8_t*) data->header - offsetof(ieee154_txcontrol_t, header)));
160 call TxFramePool.put(data);
161 call Debug.log(DEBUG_LEVEL_INFO, DISSASSOCIATE_TXDONE, status, 2, 0);
162 m_disAssociationOngoing = FALSE;
163 signal MLME_DISASSOCIATE.confirm(status, DeviceAddrMode, DevicePANID, DeviceAddress);
164 }
165
166 event void DisassociationIndirectTx.transmitDone(ieee154_txframe_t *data, ieee154_status_t status)
167 {
168 signal DisassociationDirectTx.transmitDone(data, status);
169 }
170
171 event void DisassociationDirectTx.transmitDone(ieee154_txframe_t *data, ieee154_status_t status)
172 {
173 // transmitted a disassociation notification to a device
174 uint8_t *mhr = MHR(data), dstAddrOffset = 5;
175 uint8_t DeviceAddrMode = (mhr[1] & FC2_DEST_MODE_MASK) >> FC2_DEST_MODE_OFFSET;
176 uint16_t DevicePANID = *((nxle_uint16_t*) (&(mhr[MHR_INDEX_ADDRESS])));
177 ieee154_address_t DeviceAddress;
178 call FrameUtility.convertToNative(&DeviceAddress.extendedAddress, &mhr[dstAddrOffset]);
179 call Debug.log(DEBUG_LEVEL_INFO, DISSASSOCIATE_TXDONE, status, 1, 0);
180 call TxControlPool.put((ieee154_txcontrol_t*) ((uint8_t*) data->header - offsetof(ieee154_txcontrol_t, header)));
181 call TxFramePool.put(data);
182 call Debug.log(DEBUG_LEVEL_INFO, DISSASSOCIATE_TXDONE, status, 2, 0);
183 m_disAssociationOngoing = FALSE;
184 signal MLME_DISASSOCIATE.confirm(status, DeviceAddrMode, DevicePANID, DeviceAddress);
185 }
186
187 /* ------------------- MLME_DISASSOCIATE (receiving) ------------------- */
188
189 event message_t* DisassociationDirectRxFromCoord.received(message_t* frame)
190 {
191 // received a disassociation notification from the coordinator (direct tx)
192 ieee154_address_t address;
193 address.extendedAddress = call LocalExtendedAddress.get();
194 signal MLME_DISASSOCIATE.indication(address.extendedAddress, frame->data[1], NULL);
195 return frame;
196 }
197
198 event message_t* DisassociationExtractedFromCoord.received(message_t* frame, ieee154_txframe_t *txFrame)
199 {
200 // received a disassociation notification from the coordinator (indirect transmission)
201 return signal DisassociationDirectRxFromCoord.received(frame);
202 }
203
204 event message_t* DisassociationRxFromDevice.received(message_t* frame)
205 {
206 // received a disassociation notification from the device
207 ieee154_address_t address;
208 call Debug.log(DEBUG_LEVEL_INFO, DISSASSOCIATE_RX, 0, 0, 0);
209 if (call Frame.getSrcAddrMode(frame) == ADDR_MODE_EXTENDED_ADDRESS &&
210 call Frame.getSrcAddr(frame, &address) == SUCCESS)
211 signal MLME_DISASSOCIATE.indication(address.extendedAddress, frame->data[1], NULL);
212 return frame;
213 }
214
215 /* ------------------- Defaults ------------------- */
216
217 default event void MLME_DISASSOCIATE.indication (
218 uint64_t DeviceAddress,
219 ieee154_disassociation_reason_t DisassociateReason,
220 ieee154_security_t *security
221 ){}
222 default event void MLME_DISASSOCIATE.confirm (
223 ieee154_status_t status,
224 uint8_t DeviceAddrMode,
225 uint16_t DevicePANID,
226 ieee154_address_t DeviceAddress
227 ){}
228
229 }