Example #1
0
// See if we can recover from tunnel creation issues.
static bool handleRequestTunnelFailure(uint8_t tunnelIndex, EmberAfPluginTunnelingClientStatus status)
{
  uint8_t i;

  emberAfDebugPrintln("CHF: handleRequestTunnelFailure 0x%x, 0x%x",
                      tunnelIndex, status);

  if (status == EMBER_AF_PLUGIN_TUNNELING_CLIENT_BUSY) {
    // Per GBCS send another request 3 minutes from now
    tunnels[tunnelIndex].state = REQUEST_PENDING_TUNNEL;
    tunnels[tunnelIndex].timeoutMSec = halCommonGetInt32uMillisecondTick() +
        (MILLISECOND_TICKS_PER_SECOND * 180);
    emberEventControlSetActive(emberAfPluginCommsHubFunctionTunnelEventControl);
    emberAfPluginCommsHubFunctionPrintln("CHF: Busy status received from node ID 0x%2x",tunnels[tunnelIndex].remoteNodeId);
    return true;
  } else if (status == EMBER_AF_PLUGIN_TUNNELING_CLIENT_NO_MORE_TUNNEL_IDS) {
    // Per GBCS close any other tunnels we may have with the device
    // and once all responses are received try the RequestTunnel again
    bool retryRequest = false;
    for (i = 0; i < EMBER_AF_PLUGIN_COMMS_HUB_FUNCTION_TUNNEL_LIMIT; i++) {
      if (i != tunnelIndex && tunnels[i].remoteNodeId == tunnels[tunnelIndex].remoteNodeId) {
        retryRequest = true;
        emAfPluginCommsHubFunctionTunnelDestroy(tunnels[i].remoteDeviceId);
      }
    }
    if (retryRequest) {
      // We'll retry the request in the tunnel event handler so as to give the
      // tunnel(s) a chance to clean up.
      tunnels[tunnelIndex].state = REQUEST_PENDING_TUNNEL;
      tunnels[tunnelIndex].timeoutMSec = halCommonGetInt32uMillisecondTick() +
          (MILLISECOND_TICKS_PER_SECOND * 5);
      emberEventControlSetActive(emberAfPluginCommsHubFunctionTunnelEventControl);
      return true;
    }
    // no tunnels were closed so nothing more we can do
    emberAfPluginCommsHubFunctionPrintln("%p%p%p",
                                         "Error: ",
                                         "Tunnel Create failed: ",
                                         "No more tunnel ids");
    tunnels[tunnelIndex].state = CLOSED_TUNNEL;
    return false;
  }

  // All other errors are either due to mis-configuration or errors that we
  // cannot recover from so print the error and return false.
  emberAfPluginCommsHubFunctionPrintln("%p%p%p0x%x",
                                       "Error: ",
                                       "Tunnel Create failed: ",
                                       "Tunneling Client Status: ",
                                       status);
  tunnels[tunnelIndex].state = CLOSED_TUNNEL;
  return false;
}
void emberAfPluginConcentratorStatus(void)
{
  bool active = (emberAfPluginConcentratorUpdateEventControl.status
                    != EMBER_EVENT_INACTIVE);
  uint32_t nowMS32 = halCommonGetInt32uMillisecondTick();

  emberAfAppPrintln("Active: %p", 
                    (active
                     ? "yes"
                     : "no"));
  emberAfAppPrintln("Type:  %p RAM",
                    ((EMBER_AF_PLUGIN_CONCENTRATOR_CONCENTRATOR_TYPE
                      == EMBER_LOW_RAM_CONCENTRATOR)
                     ? "Low"
                     : "High"));
  emberAfAppPrintln("Time before next broadcast (ms):   %l",
                    emberAfPluginConcentratorUpdateEventControl.timeToExecute - nowMS32);
  emberAfAppPrintln("Min Time Between Broadcasts (sec): %d", 
                    EMBER_AF_PLUGIN_CONCENTRATOR_MIN_TIME_BETWEEN_BROADCASTS_SECONDS);
  emberAfAppPrintln("Max Time Between Broadcasts (sec): %d", 
                    EMBER_AF_PLUGIN_CONCENTRATOR_MAX_TIME_BETWEEN_BROADCASTS_SECONDS);
  emberAfAppPrintln("Max Hops: %d",
                    (EMBER_AF_PLUGIN_CONCENTRATOR_MAX_HOPS == 0
                     ? EMBER_MAX_HOPS
                     : EMBER_AF_PLUGIN_CONCENTRATOR_MAX_HOPS));
  emberAfAppPrintln("Route Error Threshold:      %d",
                    EMBER_AF_PLUGIN_CONCENTRATOR_ROUTE_ERROR_THRESHOLD);
  emberAfAppPrintln("Delivery Failure Threshold: %d",
                    EMBER_AF_PLUGIN_CONCENTRATOR_DELIVERY_FAILURE_THRESHOLD);
}
Example #3
0
static bool requestTunnel(uint8_t tunnelIndex)
{
  EmberAfPluginTunnelingClientStatus status;

  // The tunneling cluster client code can only process one tunneling reuqest
  // at a time so if there's already on outstanding then mark this one pending
  // and let the tunnel event handler take care of the retry.
  if (responsePendingIndex != EM_AF_PLUGIN_COMMS_HUB_FUNCTION_NULL_TUNNEL_INDEX) {
    tunnels[tunnelIndex].state = REQUEST_PENDING_TUNNEL;
    tunnels[tunnelIndex].timeoutMSec = halCommonGetInt32uMillisecondTick();
    emberEventControlSetActive(emberAfPluginCommsHubFunctionTunnelEventControl);
    return true;
  }

  status = emberAfPluginTunnelingClientRequestTunnel(tunnels[tunnelIndex].remoteNodeId,
                                                     localTunnelEndpoint,
                                                     tunnels[tunnelIndex].remoteEndpoint,
                                                     GBCS_TUNNELING_PROTOCOL_ID,
                                                     GBCS_TUNNELING_MANUFACTURER_CODE,
                                                     GBCS_TUNNELING_FLOW_CONTROL_SUPPORT);
  if (status != EMBER_AF_PLUGIN_TUNNELING_CLIENT_SUCCESS
      && !handleRequestTunnelFailure(tunnelIndex, status)) {
    return false;
  }

  responsePendingIndex = tunnelIndex;
  tunnels[tunnelIndex].state = RESPONSE_PENDING_TUNNEL;
  return true;
}
// Return the number of milliseconds to the start of the next frame.
static uint32_t irGetDelayToNextFrameInMs(uint32_t intervalInMs)
{
  uint32_t now;
  uint32_t elapsed;

  now = halCommonGetInt32uMillisecondTick();
  elapsed = elapsedTimeInt32u(irStartTimeInMs, now);
  return (elapsed >= intervalInMs) ? 1 : (intervalInMs - elapsed);
}
Example #5
0
void emberAfPluginHeartbeatTickCallback(void)
{
  static uint32_t lastMs = 0;
  uint32_t nowMs = halCommonGetInt32uMillisecondTick();
  if (EMBER_AF_PLUGIN_HEARTBEAT_PERIOD_QS * MILLISECOND_TICKS_PER_QUARTERSECOND
      < elapsedTimeInt32u(lastMs, nowMs)) {
    halToggleLed(BOARD_HEARTBEAT_LED);
    lastMs = nowMs;
  }
}
Example #6
0
void printEvents(void)
{
  uint8_t i = 0;
  uint32_t nowMS32 = halCommonGetInt32uMillisecondTick();
  while (emAfEvents[i].control != NULL) {
    emberAfCorePrint("%p  : ", emAfEventStrings[i]);
    if (emAfEvents[i].control->status == EMBER_EVENT_INACTIVE) {
      emberAfCorePrintln("inactive");
    } else {
      emberAfCorePrintln("%l ms", emAfEvents[i].control->timeToExecute - nowMS32);
    }
    i++;
  }
}
Example #7
0
static bool buttonPress(uint8_t button, uint8_t state)
{
  // ISR CONTEXT!!!
  static uint32_t timeMs;
  EmberEventControl* event;
	
  if (button == BUTTON0) {
    event = &buttonEvent0;
  } else if (button == BUTTON1) {
		event = &buttonEvent1;
  } else {
		return false;
	}
	if (state == BUTTON_PRESSED) {
		buttonPressDurationMs = 0;
		timeMs = halCommonGetInt32uMillisecondTick();
  } else {
		buttonPressDurationMs = elapsedTimeInt32u(timeMs, halCommonGetInt32uMillisecondTick());
		emberEventControlSetActive(*event);
	}

  return true;
}
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 );
  }
Example #9
0
// Tunnel event handler used to retry previously attempted tunnel creations
void emberAfPluginCommsHubFunctionTunnelEventHandler(void)
{
  uint8_t tunnelIndex;
  uint32_t timeNowMs;
  uint32_t nearestEventTimeoutDelayMs = UINT32_MAX;
  uint32_t currentTunnelTimeoutMs = 0;

  emberEventControlSetInactive(emberAfPluginCommsHubFunctionTunnelEventControl);
  timeNowMs = halCommonGetInt32uMillisecondTick();

  emberAfPluginCommsHubFunctionPrintln("CHF: emberAfPluginCommsHubFunctionTunnelEventHandler");

  // If we're no longer waiting for a tunnel to come up then find which tunnel
  // (or tunnels) are ready for another attempt at tunnel creation
  if (responsePendingIndex == EM_AF_PLUGIN_COMMS_HUB_FUNCTION_NULL_TUNNEL_INDEX) {
    for (tunnelIndex=0; tunnelIndex<EMBER_AF_PLUGIN_COMMS_HUB_FUNCTION_TUNNEL_LIMIT; tunnelIndex++) {
      if (tunnels[tunnelIndex].type == CLIENT_TUNNEL
	        && tunnels[tunnelIndex].state == REQUEST_PENDING_TUNNEL) {
        // JIRA EMAPPFWKV2-1392: Event handler was not registered and was not working properly
        // if timeout time has passed, then request a tunnel, else retry after least time remaining
        if (timeGTorEqualInt32u(timeNowMs, tunnels[tunnelIndex].timeoutMSec)) {
          emberAfPluginCommsHubFunctionPrintln("Retrying tunnel creation to node ID 0x%2x",
                                                tunnels[tunnelIndex].remoteNodeId);
          if (requestTunnel(tunnelIndex)) {
            emberEventControlSetDelayMS(emberAfPluginCommsHubFunctionTunnelEventControl,
                                        MILLISECOND_TICKS_PER_SECOND);
            return;
          }
        } else {
          currentTunnelTimeoutMs = elapsedTimeInt32u(timeNowMs, tunnels[tunnelIndex].timeoutMSec);
          if (currentTunnelTimeoutMs < nearestEventTimeoutDelayMs) {
            nearestEventTimeoutDelayMs = currentTunnelTimeoutMs;
          }
        }
      }
    }
  }

  if (nearestEventTimeoutDelayMs != MAX_INT32U_VALUE) {
    emberEventControlSetDelayMS(emberAfPluginCommsHubFunctionTunnelEventControl, nearestEventTimeoutDelayMs);
  }
}
Example #10
0
void initAndRunMainLoop(void)
{
  EmberEventControl dataReportControl;
  EmberStatus status;
  int counter_reset = 0;
  emberTaskEnableIdling(true);

  emAppTask = emberTaskInit(emAppEvents);

  // Initialize the radio and the stack.  If this fails, we have to assert
  // because something is wrong.
  status = emberInit();
  emberSerialPrintfLine(COM_USART2, "Inititialization: 0x%x", status);	//ember success;0x00 so emberinit worked
  assert(status == EMBER_SUCCESS);


  emberAfInit();
  emberAfMainInitCallback();       //emberNetworkInit() called here

  emberResetNetworkState(); /////////////////leave network to join coordinator

  EmberNetworkStatus net_status;
  EmberNetworkParameters parameters;

  emberSetSecurityKey(&securityKey);

  MEMSET(&parameters, 0, sizeof(EmberNetworkParameters));
  parameters.radioTxPower = 0;
  parameters.radioChannel = 1;
  parameters.panId = 0x01FF;

  halStackSeedRandom(halCommonGetInt32uMillisecondTick());

  while (TRUE) {
    // Let the stack or EZSP layer run periodic tasks.
    emberTick();
    // Let the application and plugins run periodic tasks.

    emberAfMainTickCallback();
    emberAfTick();
    emberEventControlSetActive(dataReportControl);

    net_status = emberNetworkState();
    //emberResetNetworkState();
    //emberJoinNetwork(EMBER_STAR_END_DEVICE, &parameters);
    /*
    if(!emberStackIsUp()){
    	emberAfGuaranteedPrintln("stack wasn't up");
    	emberJoinNetwork(EMBER_STAR_END_DEVICE, &parameters);
    }
	*/
    //emberAfGuaranteedPrintln("net_status is: %d", net_status);
    if(counter_reset == 10000){
    	emberResetNetworkState();
    	counter_reset = 0;
    }
    else{
    	counter_reset++;
    }
    if (net_status == EMBER_NO_NETWORK){
        //emberPermitJoining(0xFF);
    	//emberResetNetworkState();
    	emberAfGuaranteedPrintln("Network has been reset");
    	emberJoinNetwork(EMBER_STAR_END_DEVICE, &parameters);
    }



    emberRunTask(emAppTask);
  }
}
Example #11
0
int32u emberGetTime(void) {
  return halCommonGetInt32uMillisecondTick();
}
// Cluster: On/off, server
EmberAfStatus emberAfOnOffClusterServerCommandParse(EmberAfClusterCommand *cmd) {
	boolean wasHandled = FALSE;
	int8u OnOfffCmdBeforeNumber;
	if (!cmd->mfgSpecific) {
		switch (cmd->apsFrame->destinationEndpoint){
			case 1 : OnOfffCmdBeforeNumber = 1;
			break;
			case 3 : OnOfffCmdBeforeNumber = 2;
			break;
			case 5 : OnOfffCmdBeforeNumber = 3;
			break;
			case 7 : OnOfffCmdBeforeNumber = 4;
			break;
			case LedLightOptionEndpoint:
					 OnOfffCmdBeforeNumber = 5;
			break;
		}
		if (CommonGetDurationTime(OnOffCmdBeforeTimer[OnOfffCmdBeforeNumber]) > 1000){
			OnOffCmdBeforeTimer[OnOfffCmdBeforeNumber] = (int16u)halCommonGetInt32uMillisecondTick();
			switch (cmd->commandId) {
				case ZCL_OFF_COMMAND_ID: {
					// Command is fixed length: 0
					wasHandled = emberAfOnOffClusterOffCallback();
					break;
				}
				case ZCL_ON_COMMAND_ID: {
					// Command is fixed length: 0
					wasHandled = emberAfOnOffClusterOnCallback();
					break;
				}
				case ZCL_TOGGLE_COMMAND_ID: {
					// Command is fixed length: 0
					wasHandled = emberAfOnOffClusterToggleCallback();
					break;
				}
			}



		//	User Controller Command
			int8u CurrentState;
			emberAfReadServerAttribute(cmd->apsFrame->destinationEndpoint,
						ZCL_ON_OFF_CLUSTER_ID, ZCL_ON_OFF_ATTRIBUTE_ID, &CurrentState,
						sizeof(CurrentState));
			if (status(wasHandled, cmd->mfgSpecific) == EMBER_ZCL_STATUS_SUCCESS) {
				if (cmd->apsFrame->destinationEndpoint == LedLightOptionEndpoint) {
					if (CurrentState == 1) {
						halClearLed(LED_LIGHT);
					}
					else{
						halSetLed(LED_LIGHT);
					}
				}
				else {
					if (CurrentState == 1) {
						UartSendSwitchControlNumber(
								(((cmd->apsFrame->destinationEndpoint) >> 1) + 1),
								OnRelay);
					} else {
						UartSendSwitchControlNumber(
								(((cmd->apsFrame->destinationEndpoint) >> 1) + 1),
								OffRelay);
					}

					//	 	Send to Bind Device

					SendViaBindingTable(cmd->apsFrame->destinationEndpoint,
							CurrentState, OnOffType);

					//		Send On-Off AttributeResponse, Zipato stupid update :D-----------------------------//
					SendOnOffControlReadAttribute(cmd->apsFrame->destinationEndpoint,
							CurrentState);

					// on-off led

					if (CurrentState == 1) {
						CommonSetLed(cmd->apsFrame->destinationEndpoint >> 1);
					} else {