]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - tos/lib/mac/tkn154/ScanP.nc
Energy scan returned wrong status code if EnergyDetectList array size was equal to...
[tinyos-2.x.git] / tos / lib / mac / tkn154 / ScanP.nc
index 21cdfb9e8fc7757085c703cef9135f26b1806141..37ac03b661bfd485c25c48ee5c125a90b3960de4 100644 (file)
@@ -132,25 +132,28 @@ implementation
     } else if (security && security->SecurityLevel) {
       status = IEEE154_UNSUPPORTED_SECURITY;
     } if ((ScanType > 3) || (ScanType < 3 && ScanDuration > 14) || 
-          (ChannelPage != IEEE154_SUPPORTED_CHANNELPAGE) ||
-          !(supportedChannels & ScanChannels) ||
-          (EnergyDetectListNumEntries && PANDescriptorListNumEntries) ||
+        (ChannelPage != IEEE154_SUPPORTED_CHANNELPAGE) ||
+        !(supportedChannels & ScanChannels) ||
+        ((ScanType != ORPHAN_SCAN) &&
+         ((EnergyDetectListNumEntries && PANDescriptorListNumEntries) ||
           (EnergyDetectList != NULL && PANDescriptorList != NULL) ||
           (EnergyDetectListNumEntries && EnergyDetectList == NULL) ||
-          (PANDescriptorListNumEntries && PANDescriptorList == NULL)) {
+          (PANDescriptorListNumEntries && PANDescriptorList == NULL)))) {
       status = IEEE154_INVALID_PARAMETER;
-    } else if (ScanType != ENERGY_DETECTION_SCAN &&
-        !(m_txFrame = call TxFramePool.get())) { 
+    } else if ((ScanType == ACTIVE_SCAN || ScanType == ORPHAN_SCAN) &&
+        ((m_txFrame = call TxFramePool.get()) == NULL)) {
       status = IEEE154_TRANSACTION_OVERFLOW;
-    } else if (ScanType != ENERGY_DETECTION_SCAN &&
-        !(txControl = call TxControlPool.get())) { 
+    } else if ((ScanType == ACTIVE_SCAN || ScanType == ORPHAN_SCAN) &&
+        ((txControl = call TxControlPool.get()) == NULL)) { 
       call TxFramePool.put(m_txFrame);
       m_txFrame = NULL;
       status = IEEE154_TRANSACTION_OVERFLOW;
     } else {
-      m_txFrame->header = &txControl->header;
-      m_txFrame->payload = m_payload;
-      m_txFrame->metadata = &txControl->metadata;
+      if (m_txFrame != NULL){
+        m_txFrame->header = &txControl->header;
+        m_txFrame->payload = m_payload;
+        m_txFrame->metadata = &txControl->metadata;
+      }
       m_busy = TRUE;
       m_scanType = ScanType;
       m_scanChannels = ScanChannels;
@@ -269,21 +272,23 @@ implementation
       uint32_t unscannedChannels = 0;
 
       if (m_terminateScan){
-        // scan operation terminated prematurely because the max. 
-        // number of PAN descriptors/ED samples was reached
-        result = IEEE154_LIMIT_REACHED;
-
-        // mark the channel on which we received the last beacon
-        // as unscanned, because it was not completely scanned
+        // Scan operation terminated because the max. 
+        // number of PAN descriptors/ED samples was reached.
+        // Check if there are channels that were unscanned.
+        // In active/passive scan we consider a channel 
+        // unscanned if it was not completely scanned.
         if (m_scanType == PASSIVE_SCAN || m_scanType == ACTIVE_SCAN) 
-          currentChannelBit >>= 1;
+          currentChannelBit >>= 1; // last (partially) scanned channel
         while (!(currentChannelBit & INVALID_CHANNEL_BITMASK) &&
                (m_scanChannels & currentChannelBit)){
           unscannedChannels |= currentChannelBit;
           currentChannelBit <<= 1;
         }
+        if (unscannedChannels) // some channels were not (completely) scanned
+          result = IEEE154_LIMIT_REACHED;
       } else if (m_scanType != ENERGY_DETECTION_SCAN && !m_resultIndex)
         result = IEEE154_NO_BEACON;
+
       if (m_scanType == PASSIVE_SCAN || m_scanType == ACTIVE_SCAN) 
         call MLME_SET.macPANId(m_PANID);
       if (m_txFrame != NULL) {
@@ -323,7 +328,8 @@ implementation
       ((uint8_t*) m_resultList)[m_resultIndex++] = EnergyLevel;
     if (m_resultIndex == m_resultListNumEntries)
       m_terminateScan = TRUE; // done
-    call RadioOff.off();
+    if (call RadioOff.off() == EALREADY)
+      signal RadioOff.offDone();
   }
 
   /* ----------------------- Active/Orphan scan ----------------------- */