/** @brief Stack Status
 *
 * This function is called by the application framework from the stack status
 * handler.  This callbacks provides applications an opportunity to be
 * notified of changes to the stack status and take appropriate action.  The
 * application should return TRUE if the status has been handled and should
 * not be handled by the application framework.
 *
 * @param status   Ver.: always
 */
boolean emberAfStackStatusCallback(EmberStatus status)
{
  // Whenever the network comes up, permit joining.  If we go down, let the
  // user know, although this shouldn't happen.
  if (status == EMBER_NETWORK_UP) {
    pjoin();
  } else if (status == EMBER_NETWORK_DOWN) {
    halPlayTune_P(sadTune, TRUE);
  }
  return FALSE;
}
void buttonEventHandler(void)
{
  // On a press-and-hold button event, form a network if we don't have one or
  // permit joining if we do.  If we form, we'll automatically permit joining
  // when we come up.
  emberEventControlSetInactive(buttonEventControl);
  if (halButtonState(BUTTON0) == BUTTON_PRESSED) {
    if (emberNetworkState() == EMBER_NO_NETWORK) {
      halPlayTune_P(((emberFormAndJoinIsScanning()
                      || emberAfFindUnusedPanIdAndForm() == EMBER_SUCCESS)
                     ? waitTune
                     : sadTune),
                    TRUE);
    } else {
      pjoin();
    }
  } else if (halButtonState(BUTTON1) == BUTTON_PRESSED) {
    emberAfEzmodeServerCommission(emberAfPrimaryEndpoint());
  }
}
Beispiel #3
0
int main(int argc, char **argv) {
  pid_t child_pid; /* child process id */
  pthread_t monitor_thread; /* monitor thread */
  char *args[CMD_ARGS_MAX+1], buffer_cmd[LINE_COMMAND_MAX]; /* cmd related */
  char buffer_log[LINE_LOGFILE_MAX]; /* log related */

  if((g_lst_children = lst_new()) == NULL) {
    if(fprintf(stderr, "lst_new: couldn't create list\n") < 0)
      handle_error("fprintf");
    exit(EXIT_FAILURE);
  }

  g_log_file = f_open(PATH_LOGFILE_STR, "a+");

  while(!feof(g_log_file)) {
    f_gets(buffer_log, LINE_LOGFILE_MAX, g_log_file); /* NULL or iteracao # */
    if(feof(g_log_file)) break;
    if(sscanf(buffer_log, "%*s %d", &g_iterations) != 1)
      handle_error("sscanf");
    
    f_gets(buffer_log, LINE_LOGFILE_MAX, g_log_file); /* PID: # time: # s */
    f_gets(buffer_log, LINE_LOGFILE_MAX, g_log_file); /* total time: # s */
    if(sscanf(buffer_log, "%*[^0-9] %d", &g_total_time) != 1)
      handle_error("sscanf");
  }
  
  ++g_iterations;
  
  cond_init(&g_child_cv);
  cond_init(&g_monitoring_cv);
  mutex_init(&g_mutex);
  pcreate(&monitor_thread, process_monitor, NULL);
  
  while(true) {
    int numargs = readLineArguments(args,
      CMD_ARGS_MAX + 1, buffer_cmd, LINE_COMMAND_MAX);
    
    if(args[0] == NULL) continue;
    if(numargs < 0 || (numargs > 0 && !strcmp(args[0], COMMAND_EXIT_STR))) {
      
      mutex_lock(&g_mutex);
      g_monitoring = false;
      cond_signal(&g_monitoring_cv);
      mutex_unlock(&g_mutex);
      
      pjoin(monitor_thread);
      
      lst_print(g_lst_children);
      
      destroySharedResources();
      f_close(g_log_file);
      return EXIT_SUCCESS;
    }
    else {
      FILE *fp; /* To check file existance */
      if ((fp = fopen(args[0], "r")) == NULL)
        perror(args[0]);
      else {
        f_close(fp);
        
        mutex_lock(&g_mutex);
        while(g_num_children == MAXPAR)
          cond_wait(&g_child_cv, &g_mutex);
        mutex_unlock(&g_mutex);
        
        if((child_pid = fork()) < 0) perror("fork");
        else if(child_pid == 0) {
          execv(args[0],args);
          
          destroySharedResources();
          handle_error("execv");
        }
        else {
          mutex_lock(&g_mutex);
          if(insert_new_process(g_lst_children, child_pid, time(NULL)) != 0) {
            fprintf(stderr,
              "insert_new_process: failed to insert new process\n");
            exit(EXIT_FAILURE);
          }
          ++g_num_children;
          
          cond_signal(&g_monitoring_cv);
          mutex_unlock(&g_mutex);
        }
      }
    }
  }
}