/*  Copyright (C) MOXA Inc. All rights reserved.

    This software is distributed under the terms of the
    MOXA License.  See the file COPYING-MOXA for details.

    2010-12-10 Wade Liang Wade.Liang@moxa.com
		new release
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include "mxdk02.h"

int main(int argc, char *argv[]) {
	int fd;
	int slot_num;
	int val;
	char *endptr;

	if (argc != 3) {
		printf("Incorrect number of parameters\n");
		printf("Usage: mx-dk02-control slot_num [1/0]\n");
		return -1;
    } 

	slot_num = strtol(argv[1], &endptr, 10);
	if (strcmp(endptr, "")) {
		printf("Error: slot=%s invalid part=%s\n", argv[1], endptr);
		exit(1);
	}
	val = strtol(argv[2], &endptr, 10);
	if (strcmp(endptr, "")) {
		printf("Error: val=%s invalid part=%s\n", argv[2], endptr);
		exit(1);
	}

	if (((slot_num !=1) && (slot_num !=2) && (slot_num !=3) && (slot_num != 4)) 
			|| ((val != 0) && (val != 1))) {
		printf("Incorrect parameter\n");
		printf("Usage: mx-dk02-control slot_num [1/0]\n");
		return -1;
	}

	fd = mxdk02_open();
	if (fd == 0)  {
		printf("Can't open the device /dev/mxdk02");
		return -1;
	}

	if (mxdk02_set_slot(fd, slot_num, val)) 
		printf("Incorrect IOCTL!\n");
	else  if (val)
		printf("Power on the USB interface in slot %d\n", slot_num);
	else 
		printf("Power off the USB interface in slot %d\n", slot_num);

	mxdk02_close(fd);

	return 0;
}

