Ejemplo n.º 1
0
/**
 * Initialise dsmesock connection
 *
 * @return TRUE on success, FALSE on failure
 */
static gboolean init_dsmesock(void)
{
	gboolean status = FALSE;

	if (dsme_conn == NULL) {
		if ((dsme_conn = dsmesock_connect()) == NULL) {
			mce_log(LL_CRIT,
				"Failed to open DSME socket");
			goto EXIT;
		}
	}

	if ((dsme_iochan = g_io_channel_unix_new(dsme_conn->fd)) == NULL) {
		mce_log(LL_CRIT,
			"Failed to set up I/O channel for DSME socket");
		goto EXIT;
	}

	dsme_data_source_id = g_io_add_watch(dsme_iochan,
					     G_IO_IN | G_IO_PRI,
					     io_data_ready_cb, NULL);
	dsme_error_source_id = g_io_add_watch(dsme_iochan,
					      G_IO_ERR | G_IO_HUP,
					      io_error_cb, NULL);

	/* Query the current system state; if the mainloop isn't running,
	 * this will trigger an update when the mainloop starts
	 */
	query_system_state();

	status = TRUE;

EXIT:
	return status;
}
Ejemplo n.º 2
0
int main(int argc, char* argv[]) {

    struct timeval start;
    struct timeval now;
    dsmesock_connection_t *conn;
    DSM_MSGTYPE_GET_VERSION req_msg = DSME_MSG_INIT(DSM_MSGTYPE_GET_VERSION);

    gettimeofday(&start, NULL);

    printf("%s (pid %i): Wait for DSME socket...\n", argv[0], getpid());
    fflush(stdout);

    while (1) {
        conn = dsmesock_connect();
        if (conn > 0) {
            /* Give DSME time to accept the client socket before closing
               the socket connection */
            if (!send_to_dsme(conn, &req_msg)) {
                dsmesock_close(conn);
                printf("%s: ERROR: send to DSME socket failed\n", argv[0]);
                return EXIT_FAILURE;
            } else {
                dsmesock_close(conn);
                printf("%s: OK: send to DSME socket succeeded\n", argv[0]);
                return EXIT_SUCCESS;
            }
        }

        gettimeofday(&now, NULL);

        if (now.tv_sec >= (start.tv_sec + DSME_START_TIMEOUT)) {
            fprintf(stderr,
                    "%s: ERROR: Timeout waiting for DSME socket\n",
                    argv[0]);
            return EXIT_FAILURE;
        }
        usleep(20000);
    }

    return EXIT_FAILURE;
}
Ejemplo n.º 3
0
Archivo: mce-dsme.c Proyecto: g7/mce
/**
 * Initialise dsmesock connection
 *
 * @return true on success, false on failure
 */
static bool mce_dsme_connect(void)
{
	GIOChannel *iochan = NULL;

	/* Make sure we start from closed state */
	mce_dsme_disconnect();

	mce_log(LL_DEBUG, "Opening DSME socket");

	if( !(mce_dsme_connection = dsmesock_connect()) ) {
		mce_log(LL_ERR, "Failed to open DSME socket");
		goto EXIT;
	}

	mce_log(LL_DEBUG, "Adding DSME socket notifier");

	if( !(iochan = g_io_channel_unix_new(mce_dsme_connection->fd)) ) {
		mce_log(LL_ERR,"Failed to set up I/O channel for DSME socket");
		goto EXIT;
	}

	mce_dsme_iowatch_id =
		g_io_add_watch(iochan,
			       G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
			       mce_dsme_iowatch_cb, NULL);

	/* Query the current system state; if the mainloop isn't running,
	 * this will trigger an update when the mainloop starts
	 */
	mce_dsme_query_system_state();

	/* Register with DSME's process watchdog */
	mce_dsme_init_processwd();

EXIT:
	if( iochan ) g_io_channel_unref(iochan);

	return mce_dsme_connected();
}
Ejemplo n.º 4
0
int main(int argc, char *argv[])
{
	const char *program_name = argv[0];
	if (argc > 1) {
		usage(program_name);
		return EXIT_FAILURE;
	}

	conn = dsmesock_connect();
	if (conn == 0) {
		perror("dsmesock_connect");
		return EXIT_FAILURE;
	}

	if (send_kick_wd() <= 0) {
		printf("sending failed!\n");
		return EXIT_FAILURE;
	}

	dsmesock_close(conn);

	return EXIT_SUCCESS;
}
Ejemplo n.º 5
0
int main(int argc, char *argv[])
{
  dsmesock_connection_t * conn;

  int next_option;
  const char* program_name = argv[0];

  int batt_status = -1;
  int charger_status = -1;

  const char* short_options = "hb:c:";
  const struct option long_options[] = {
    { "battery",     0, NULL, 'b' },
    { "charger",     0, NULL, 'c' },
    { "help",        0, NULL, 'h' }
  };

  do {
    next_option = getopt_long(argc, argv, short_options, long_options, NULL);
    switch (next_option) {
    case 'b':
      batt_status = atoi(optarg);
      break;
    case 'c':
      charger_status = atoi(optarg);
      break;
    case 'h':
      usage(program_name);
      return EXIT_SUCCESS;
      break;
    case '?':
      usage(program_name);
      return EXIT_FAILURE;
      break;
    }
  } while (next_option != -1);

  if (batt_status == -1 && charger_status == -1) {
    usage(program_name);
    return EXIT_FAILURE;
  }

  conn = dsmesock_connect();
  if (conn == 0) {
    perror("dsmesock_connect");
    return 2;
  }

  if (batt_status != -1) {
      DSM_MSGTYPE_SET_BATTERY_STATE msg =
          DSME_MSG_INIT(DSM_MSGTYPE_SET_BATTERY_STATE);

      msg.empty = !batt_status;

      dsmesock_send(conn, &msg);
  }

  if (charger_status != -1) {
      DSM_MSGTYPE_SET_CHARGER_STATE msg =
          DSME_MSG_INIT(DSM_MSGTYPE_SET_CHARGER_STATE);

      msg.connected = !!charger_status;

      dsmesock_send(conn, &msg);
  }

  dsmesock_close(conn);

  exit(0);
}
Ejemplo n.º 6
0
int main(void)
{
  dsmesock_connection_t*        dsme_conn;
  fd_set                        rfds;
  DSM_MSGTYPE_STATE_QUERY       req_msg =
    DSME_MSG_INIT(DSM_MSGTYPE_STATE_QUERY);
  dsmemsg_generic_t*            msg;
  DSM_MSGTYPE_STATE_CHANGE_IND* msg2;
  int ret;

  dsme_conn = dsmesock_connect();
  if (dsme_conn == 0) {
      fprintf(stderr, "dsmesock_connect\n");
      return state;
  }

  /* Sending a query if the original message has already gone by */
  dsmesock_send(dsme_conn, &req_msg);

  while (1) {
      struct timeval tv;
      tv.tv_sec = DSME_STATE_TIMEOUT;
      tv.tv_usec = 0;
      FD_ZERO(&rfds);
      FD_SET(dsme_conn->fd, &rfds);

      ret = select(dsme_conn->fd + 1, &rfds, NULL, NULL, &tv); 
      if (ret == -1) {
          fprintf(stderr, "error in select()\n");
          printf("MALF");
          return EXIT_FAILURE;
      } 
      if (ret == 0) {
          fprintf(stderr, "Timeout!\n");
          printf("MALF");
          return EXIT_FAILURE;
      }

      msg = (dsmemsg_generic_t*)dsmesock_receive(dsme_conn);
      if ((msg2 = DSMEMSG_CAST(DSM_MSGTYPE_STATE_CHANGE_IND, msg)) != 0) {
          fprintf(stderr, "received state:%i\n", msg2->state);
          switch (msg2->state) {
            case DSME_STATE_ACTDEAD:
              printf("ACTDEAD");
              return EXIT_SUCCESS;
            case DSME_STATE_USER:
              printf("USER");
              return EXIT_SUCCESS;
            case DSME_STATE_TEST:
              printf("TEST");
              return EXIT_SUCCESS;
            case DSME_STATE_LOCAL:
              printf("LOCAL");
              return EXIT_SUCCESS;
            case DSME_STATE_MALF:
              printf("MALF");
              return EXIT_SUCCESS;
            case DSME_STATE_SHUTDOWN:
              printf("SHUTDOWN");
              return EXIT_SUCCESS;
            case DSME_STATE_BOOT:
              printf("BOOT");
              return EXIT_SUCCESS;
            default:
              fprintf(stderr, "unknown state: %d\n", msg2->state);
              break;
          }
        } else {
            fprintf(stderr, "The received message wasn't state change indication\n");
        }
      free(msg);
  }

  return EXIT_FAILURE;
}