]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - tos/lib/mac/tkn154/BeaconSynchronizeP.nc
Added a note to README that MAC interface naming deviates from TEP3 recommended pract...
[tinyos-2.x.git] / tos / lib / mac / tkn154 / BeaconSynchronizeP.nc
index f58c22f8bb374e19225d71f5c43efde4a609bc09..e7c39110a2a2f6cba0bc48281ddebd4e3dd0b6f9 100644 (file)
@@ -45,7 +45,7 @@ module BeaconSynchronizeP
     interface MLME_SYNC;
     interface MLME_BEACON_NOTIFY;
     interface MLME_SYNC_LOSS;
-    interface Get<bool> as IsTrackingBeacons;
+    interface GetNow<bool> as IsTrackingBeacons;
     interface GetNow<uint32_t> as CapStart;
     interface GetNow<ieee154_reftime_t*> as CapStartRefTime; 
     interface GetNow<uint32_t> as CapLen;
@@ -66,6 +66,7 @@ module BeaconSynchronizeP
     interface MLME_GET;
     interface MLME_SET;
     interface FrameUtility;
+    interface Notify<bool> as FindBeacon;
     interface IEEE154BeaconFrame as BeaconFrame;
     interface Alarm<TSymbolIEEE802154,uint32_t> as TrackAlarm;
     interface RadioRx as BeaconRx;
@@ -95,12 +96,13 @@ implementation
   };
 
   norace bool m_tracking = FALSE;
-  norace bool m_updatePending = FALSE;
+  bool m_updatePending = FALSE;
   uint8_t m_updateLogicalChannel;
   bool m_updateTrackBeacon;
   bool m_stopTracking = FALSE;
+  bool m_internalRequest = FALSE;
 
-  norace uint8_t m_numBeaconsLost;
+  uint8_t m_numBeaconsLost;
   uint8_t m_coordAddress[8];
   message_t m_beaconBuffer;
   norace message_t *m_beaconBufferPtr = &m_beaconBuffer;
@@ -162,7 +164,8 @@ implementation
       m_stopTracking = FALSE;
       m_updateLogicalChannel = logicalChannel;
       m_updateTrackBeacon = trackBeacon;
-      atomic m_updatePending = TRUE;
+      m_updatePending = TRUE;
+      m_internalRequest = FALSE;
       call Debug.log(LEVEL_INFO,SyncP_RESOURCE_REQUEST, 0, 0, 0);
       call Token.request();
     }
@@ -170,9 +173,22 @@ implementation
     return IEEE154_SUCCESS;
   }
 
+  event void FindBeacon.notify( bool val )
+  {
+    if (!m_tracking && !m_updatePending){
+      // find a single beacon now (treat this like a user request)
+      m_updateLogicalChannel = call MLME_GET.phyCurrentChannel();
+      m_updateTrackBeacon = FALSE;
+      m_updatePending = TRUE;
+      m_internalRequest = TRUE;
+      call Token.request();
+    }
+  }
+
   event void Token.granted()
   {
     bool missed = FALSE;
+    call Debug.flush();
     call Debug.log(LEVEL_INFO,SyncP_GOT_RESOURCE, m_lastBeaconRxTime+m_beaconInterval, 
         m_beaconInterval, (m_updatePending<<1)+m_tracking);
     if (m_updatePending){
@@ -220,7 +236,7 @@ implementation
 
   async event void TrackAlarm.fired()
   {
-    call Debug.log(LEVEL_IMPORTANT,SyncP_SWITCHOFF, 0,0,0);
+    call Debug.log(LEVEL_IMPORTANT,SyncP_TRACK_ALARM, m_state,m_lastBeaconRxTime,m_dt);
     atomic {
       switch (m_state)
       {
@@ -228,7 +244,6 @@ implementation
           call BeaconRx.prepare();
           break;
         case S_RADIO_OFF: 
-          call Debug.log(LEVEL_INFO, SyncP_SWITCHOFF, 0, 0,0);
           call RadioOff.off(); 
           break;
       }
@@ -237,6 +252,7 @@ implementation
 
   async event void BeaconRx.prepareDone()
   {
+    error_t result;
     if (m_state == S_FIRST_SCAN){
       m_state = S_RADIO_OFF;
       atomic {
@@ -246,8 +262,10 @@ implementation
       }
     } else {
       m_state = S_RADIO_OFF;
-      call BeaconRx.receive(&m_lastBeaconRxRefTime, m_dt-RX_LAG);
-      call Debug.log(LEVEL_IMPORTANT,SyncP_RX_ON, call TrackAlarm.getNow(),m_lastBeaconRxTime+m_dt,RX_LAG);
+      result = call BeaconRx.receive(&m_lastBeaconRxRefTime, m_dt-RX_LAG);
+      call Debug.log(LEVEL_IMPORTANT,SyncP_RX_ON, m_lastBeaconRxTime, call TrackAlarm.getNow(), m_dt+RX_DURATION);
+      if (result != SUCCESS)
+        call Debug.log(LEVEL_IMPORTANT,SyncP_RADIO_BUSY, result, 0, 0);
       call TrackAlarm.startAt(m_lastBeaconRxTime, m_dt + RX_DURATION);
     }
   }
@@ -259,6 +277,7 @@ implementation
         mhr[MHR_INDEX_FC1] & FC1_FRAMETYPE_MASK,mhr[MHR_INDEX_SEQNO]);
     if (!m_beaconSwapBufferReady || !call FrameUtility.isBeaconFromCoord(frame))
     {
+      call Debug.log(LEVEL_IMPORTANT,SyncP_RX_GARBAGE, m_beaconSwapBufferReady, 0, 0);
       return frame;
     } else {
       message_t *tmp = m_beaconBufferPtr;
@@ -295,13 +314,16 @@ implementation
         m_tracking = FALSE;
         call Debug.log(LEVEL_IMPORTANT, SyncP_LOST_SYNC,0,0,0);
         call Leds.led2Off();
-        signal MLME_SYNC_LOSS.indication(
-            IEEE154_BEACON_LOSS, 
-            call MLME_GET.macPANId(),
-            call MLME_GET.phyCurrentChannel(),
-            call MLME_GET.phyCurrentPage(), 
-            NULL // security
-            );
+        if (m_internalRequest)
+          call TokenToCap.transfer();
+        else
+          signal MLME_SYNC_LOSS.indication(
+              IEEE154_BEACON_LOSS, 
+              call MLME_GET.macPANId(),
+              call MLME_GET.phyCurrentChannel(),
+              call MLME_GET.phyCurrentPage(), 
+              NULL // security
+              );
       } else
         call Token.request(); // make another request again (before giving the token up)
       call Debug.log(LEVEL_INFO,SyncP_RELEASE_RESOURCE, 0, 0, 0);
@@ -324,7 +346,7 @@ implementation
       call Debug.log(LEVEL_INFO, SyncP_BEACON_RX, m_lastBeaconRxTime, timestamp, mhr[2]);
       m_numGtsSlots = (payload[2] & 7);
       gtsFieldLength = 1 + ((m_numGtsSlots > 0) ? 1 + m_numGtsSlots * 3: 0);
-      m_lastBeaconRxTime = timestamp + IEEE154_SYNC_SYMBOL_OFFSET - IEEE154_SYMBOLS_PER_OCTET - IEEE154_PREAMBLE_LENGTH;
+      m_lastBeaconRxTime = timestamp;
       m_finalCapSlot = (payload[1] & 0x0F);
       m_sfSlotDuration = (((uint32_t) 1) << ((payload[0] & 0xF0) >> 4)) * IEEE154_aBaseSlotDuration;
       memcpy(m_gtsField, &payload[2], gtsFieldLength);
@@ -385,7 +407,7 @@ implementation
     }
   }
 
-  command bool IsTrackingBeacons.get(){ return m_tracking;}
+  async command bool IsTrackingBeacons.getNow(){ return m_tracking;}
 
   default event message_t* MLME_BEACON_NOTIFY.indication (message_t* frame){return frame;}