Beispiel #1
0
int main(int argc, char *argv[]) {

    if (argc == 1) {
        printf("Usage: %s name1 size1 ... nameN sizeN\n\n"
               "Creates device files for dataports in /dev with given names\n"
               "and sizes (in bytes).\n"
               "Dataport ids will be allocated in increasing order from left\n"
               "to right, starting from 1.\n"
               "Sizes must match those given in the corresponding camkes spec.\n"
               "This should only be run once per boot.\n", argv[0]);
        return 1;
    }

    dataport_t dataports[MAX_NUM_DATAPORTS];
    int num_dataports = process_dataport_args(argc - 1, argv + 1,
                                              dataports,
                                              MAX_NUM_DATAPORTS);

    unsigned major = device_get_major(DEVICE_NAME);
    assert(major != NO_MAJOR);

    printf("Device \"%s\" found with major number %d\n\n", DEVICE_NAME, major);

    make_nodes(dataports, num_dataports, major);
    init_nodes(dataports, num_dataports);

    return 0;
}
Beispiel #2
0
int main(void) {

	int serial_fd, major;
	struct termios tio;
	int ldisc = N_GSM0710;
	struct gsm_config gsm;
	char atcommand[40];

	/* print global parameters */
	dbg("SERIAL_PORT = %s", SERIAL_PORT);

	/* open the serial port */
	serial_fd = open(SERIAL_PORT, O_RDWR | O_NOCTTY | O_NDELAY);
	if (serial_fd == -1)
		err(EXIT_FAILURE, "Cannot open %s", SERIAL_PORT);
	
	/* get the current attributes of the serial port */
	if (tcgetattr(serial_fd, &tio) == -1)
		err(EXIT_FAILURE, "Cannot get line attributes");
	
	/* set the new attrbiutes */
	tio.c_iflag = 0;
	tio.c_oflag = 0;
	tio.c_cflag = CS8 | CREAD | CLOCAL;
//	tio.c_cflag |= CRTSCTS;
	tio.c_lflag = 0;
	tio.c_cc[VMIN] = 1;
	tio.c_cc[VTIME] = 0;
	
	/* write the speed of the serial line */
	if (cfsetospeed(&tio, LINE_SPEED) < 0 || cfsetispeed(&tio, LINE_SPEED) < 0)
		err(EXIT_FAILURE, "Cannot set line speed");
	
	/* write the attributes */
	if (tcsetattr(serial_fd, TCSANOW, &tio) == -1)
		err(EXIT_FAILURE, "Cannot set line attributes");

	/**
	*	Send AT commands to put the modem in CMUX mode.
	*	This is vendor specific and should be changed 
	*	to fit your modem needs.
	*	The following matches Quectel M95.
	*/
//	if (send_at_command(serial_fd, "AT+IFC=2,2\r") == -1)
//		errx(EXIT_FAILURE, "AT+IFC=2,2: bad response");	
	if (send_at_command(serial_fd, "AT+GMM\r") == -1)
		warnx("AT+GMM: bad response");
	if (send_at_command(serial_fd, "AT\r") == -1)
		warnx("AT: bad response");
	if (send_at_command(serial_fd, "AT+IPR=38400\r") == -1)
		errx(EXIT_FAILURE, "AT+IPR=38400: bad response");
	sprintf(atcommand, "AT+CMUX=0,0,3,%d,10,3,30,10,2\r", MTU);
	if (send_at_command(serial_fd, atcommand) == -1)
		errx(EXIT_FAILURE, "Cannot enable modem CMUX");

	/* use n_gsm line discipline */
	sleep(2);
	if (ioctl(serial_fd, TIOCSETD, &ldisc) < 0)
		err(EXIT_FAILURE, "Cannot set line dicipline. Is 'n_gsm' module registred?");

	/* get n_gsm configuration */
	if (ioctl(serial_fd, GSMIOC_GETCONF, &gsm) < 0)
		err(EXIT_FAILURE, "Cannot get GSM multiplex parameters");

	/* set and write new attributes */
	gsm.initiator = 1;
	gsm.encapsulation = 0; /* 0 or 1, basic/advanced */
	gsm.mru = MTU;
	gsm.mtu = MTU;
	gsm.t1 = 10;
	gsm.n2 = 3;
	gsm.t2 = 30;
	gsm.t3 = 10;

	if (ioctl(serial_fd, GSMIOC_SETCONF, &gsm) < 0)
		err(EXIT_FAILURE, "Cannot set GSM multiplex parameters");
	dbg("Line dicipline set");
	
	/* create the virtual TTYs */
	if (CREATE_NODES) {
		int created;
		if ((major = get_major(DRIVER_NAME)) < 0)
			errx(EXIT_FAILURE, "Cannot get major number");
		if ((created = make_nodes(major, BASENAME_NODES, NUM_NODES)) < NUM_NODES)
			warnx("Cannot create all nodes, only %d/%d have been created.", created, NUM_NODES);
	}

	/* detach from the terminal if needed */
	if (DAEMONIZE) {
		dbg("Going to background");
		if (daemon(0,0) != 0)
			err(EXIT_FAILURE, "Cannot daemonize");
	}

	/* wait to keep the line discipline enabled, wake it up with a signal */
	signal(SIGINT, signal_callback_handler);
	signal(SIGTERM, signal_callback_handler);
	pause();
	
	/* remove the created virtual TTYs */
	if (CREATE_NODES) {
		remove_nodes(BASENAME_NODES, NUM_NODES);
	}

	/* close the serial line */
	close(serial_fd);

	return EXIT_SUCCESS;
}
Beispiel #3
0
int main(int argc, char **argv)
{
    if(argc < 2)
    {
        printf("Usage: %s file\n", argv[0]);
        return 1;
    }

    char *contents = readFile(argv[1]);
    if(!contents)
    {
        printf("File '%s' doesn't exist\n", argv[1]);
        return 1;
    }

    char_node *root = make_nodes(contents);
    //print_node(root);

    while(root->next != NULL)
    {
        BinaryTree<char> *left;
        BinaryTree<char> *right;


        if(root->tree)
            left = root->tree;
        else
        {
            left = new BinaryTree<char>(root->character, NULL, NULL, root->count);
        }

        if(root->next->tree)
            right = root->next->tree;
        else
        {
            right = new BinaryTree<char>(root->next->character, NULL, NULL, root->next->count);
        }

        BinaryTree<char> *tree = new BinaryTree<char>('\0', left, right, 0);
        char_node *newnode = new_node('\0', root->next->next);
        newnode->tree = tree;
        newnode->count = root->count + root->next->count;


        root = newnode;

        char_node *node = root;
        char_node *prev = NULL;
        while(node->next && (node->next->count < node->count || node->next->count == node->count && node->tree && node->next->tree == NULL))
        {
            char_node *second = node->next;
            char_node *third = node;

            third->next = second->next;
            second->next = third;
            if(prev)
                prev->next = second;
            else
                root = second;

            prev = second;
        }
    }

    char encoding[2000];
    root->tree->printTree(encoding, 0);

    return 0;
}