static CommissioningState_t CheckNetwork(void) { emberAfDebugPrintln("DEBUG: Check Network state"); EmberNetworkStatus nw_status = emberNetworkState(); emberAfDebugPrintln("DEBUG: network state 0x%X", nw_status); if (nw_status == EMBER_JOINING_NETWORK || nw_status == EMBER_LEAVING_NETWORK) { // Try to check again after 5 seconds emberEventControlSetDelayQS(StateMachineEvent, SIMPLE_COMMISSIONING_NETWORK_RETRY_DELAY); return SC_EZ_START; } if (nw_status == EMBER_JOINED_NETWORK) { SetNextEvent(SC_EZEV_BCAST_IDENT_QUERY); // Send Permit Join broadcast to the current network // in case of ZED nothing will happen // TODO: Make this hadrcoded value as plugin's option emberAfPermitJoin(SIMPLE_COMMISSIONING_PERMIT_JOIN_TIME, TRUE); } else if (nw_status == EMBER_NO_NETWORK && GetNetworkTries() < NETWORK_ACCESS_CONS_TRIES) { // Form or join available network SetNextEvent(SC_EZEV_FORM_JOIN_NETWORK); } else { SetNextEvent(SC_EZEV_NETWORK_FAILED); } // if the device is in the network continue commissioning // by sending Identify Query emberEventControlSetActive(StateMachineEvent); return SC_EZ_START; }
static CommissioningState_t FormJoinNetwork(void) { emberAfDebugPrintln("DEBUG: Form/Join network"); // Form or join depends on the device type // Coordinator: form a network // Router/ZED/SED/MED: find a network EmberStatus status = EMBER_SUCCESS; if (emAfCurrentZigbeeProNetwork->nodeType == EMBER_COORDINATOR) { status = emberAfFindUnusedPanIdAndForm(); } else { status = emberAfStartSearchForJoinableNetwork(); } if (status != EMBER_SUCCESS) { SetNextEvent(SC_EZEV_UNKNOWN); } else { SetNextEvent(SC_EZEV_CHECK_NETWORK); } IncNetworkTries(); // run state machine again after 10 seconds emberEventControlSetDelayQS(StateMachineEvent, SIMPLE_COMMISSIONING_NETWORK_CHECK_RETRY_TIME); return SC_EZ_START; }
static void scheduleEvent(bool withDelay) { if (withDelay) { debugPrintln("%p scheduled event for %d qs", PLUGIN_NAME, DISCOVERY_DELAY_QS); emberEventControlSetDelayQS(emberAfPluginDeviceQueryServiceMyEventControl, DISCOVERY_DELAY_QS); } else { emberEventControlSetActive(emberAfPluginDeviceQueryServiceMyEventControl); } }
void halLedBlinkLedOff( uint8_t time ) { turnLedOff(activeLed); ledEventState = LED_OFF; if (time > 0) { emberEventControlSetDelayQS(emberAfPluginLedBlinkLedEventFunctionEventControl, ((uint16_t) time) * 4); } else { emberEventControlSetInactive(emberAfPluginLedBlinkLedEventFunctionEventControl); } }
static void emRestartMessageTimer() { uint32_t smallestTimeoutIndex = SLEEPY_MSG_QUEUE_NUM_ENTRIES; uint32_t smallestTimeoutMSec = 0xFFFFFFFF; uint32_t timeNowMs; uint32_t remainingMs; uint32_t delayQs; uint8_t x; timeNowMs = halCommonGetInt32uMillisecondTick(); for( x=0; x<SLEEPY_MSG_QUEUE_NUM_ENTRIES; x++ ) { if( SleepyMessageQueue[x].status == SLEEPY_MSG_QUEUE_STATUS_USED ) { if( timeGTorEqualInt32u( timeNowMs, SleepyMessageQueue[x].timeoutMSec ) ){ // Timeout already expired - break out of loop - process immediately. smallestTimeoutIndex = x; smallestTimeoutMSec = 0; break; } else{ remainingMs = elapsedTimeInt32u( timeNowMs, SleepyMessageQueue[x].timeoutMSec ); if( remainingMs < smallestTimeoutMSec ){ smallestTimeoutMSec = remainingMs; smallestTimeoutIndex = x; } } } } // Now know the smallest timeout index, and the smallest timeout value. if( smallestTimeoutIndex < SLEEPY_MSG_QUEUE_NUM_ENTRIES ) { // Run the actual timer as a QS timer since that allows us to delay longer // than a u16 MS timer would. delayQs = smallestTimeoutMSec >> 8; delayQs++; // Round up to the next quarter second tick. if( delayQs > MAX_DELAY_QS ){ delayQs = MAX_DELAY_QS; } emberEventControlSetDelayQS( msgTimeoutEvent, delayQs ); emberAfAppPrintln("Restarting sleepy message timer for %d Qsec.", delayQs ); }