Exemplo n.º 1
0
int gpio_get_direction(uint8_t gpio_pin, uint8_t *dir)
{
    char value[MAX_STR_LENGTH];

    if (!check_pin(gpio_pin))
        return -1;

    if (dir == NULL) {
        fprintf(stderr, "gpio: Cannot store direction of pin %d to null variable.\n", gpio_pin);
        return -1;
    }

    if (!is_gpio_exported(gpio_pin))
        return 0;

    if (read_str_gpio_file(gpio_pin, "direction", value, MAX_STR_LENGTH) < 0)
        return -1;

    if (strncmp(value, "out", 3) == 0) {
        *dir = GPIO_OUTPUT;
    } else if (strncmp(value, "in", 2) == 0) {
        *dir = GPIO_INPUT;
    } else {
        /*
         * This should never happen because the content of /sys/class/gpio/gpioN/direction
         * is set by the gpio controller (unless the driver is buggy)
         */
        fprintf(stderr, "gpio: Invalid direction read from gpio %d.\n", gpio_pin);
        return -1;
    }

    return 0;
}
Exemplo n.º 2
0
/* void Arduino50::checkpoint()
 * @param1: int checkpoint_id 
 * purpose: based on the checkpoint id, obtains the correct solution object
 */
void 
Arduino50::checkpoint(int checkpoint_id)
{	
	// get the specific solution for this checkpoint
	solution s = outputs[checkpoint_id];
	
	// based on the ENUM to Solution.Type pairing, check the current input
	switch(s.type)
	{
		case READ:
			inputs[checkpoint_id] = check_read(s);
		break;
		case PIN:
			inputs[checkpoint_id] = check_pin(s);			
		break;
		case NUMBER:
			inputs[checkpoint_id] = check_number(s);
		break;
		case MODE:
		break;
		default:
		break;
	}

}
Exemplo n.º 3
0
int gpio_set_direction(uint8_t gpio_pin, uint8_t dir)
{
    char str[4];

    if (!check_pin(gpio_pin)) {
        fprintf(stderr, "Cannot set direction to invalid pin %d\n", gpio_pin);
        return -1;
    }

    /* Only strings "out" and "in" can be written to file /sys/class/gpio/gpioN/direction/ */
    if (dir == GPIO_OUTPUT) {
        /* Remove any existing IRQ on gpio */
        if (write_str_gpio_file(gpio_pin, "edge", "none") < 0)
            return -1;
        strcpy(str, "out");
    } else if (dir == GPIO_INPUT) {
        strcpy(str, "in");
    } else {
        fprintf(stderr, "gpio: Cannot set gpio %d to invalid direction.\n", gpio_pin);
        return -1;
    }

    if (!is_gpio_exported(gpio_pin)) {
        fprintf(stderr, "gpio: Cannot set direction of uninitialised gpio %d\n", gpio_pin);
        return -1;
    }

    return write_str_gpio_file(gpio_pin, "direction", str);
}
Exemplo n.º 4
0
int gpio_release(uint8_t gpio_pin)
{
    if (!check_pin(gpio_pin))
        return -1;

    if (!is_gpio_exported(gpio_pin))
        return 0;

    /* Write gpio index to /sys/class/gpio/unexport */
    return unexport_pin(GPIO_DIR_BASE_PATH, gpio_pin);
}
Exemplo n.º 5
0
int gpio_get_value(uint8_t gpio_pin, uint8_t *value)
{
    if (!check_pin(gpio_pin))
        return -1;

    if (value == NULL) {
        fprintf(stderr, "gpio: Cannot store value of pin %d to null variable.\n", gpio_pin);
        return -1;
    }

    if (!is_gpio_exported(gpio_pin))
        return 0;

    /* The value returned by read_int_gpio_file is either 0 or 1 */
    return read_int_gpio_file(gpio_pin, "value", value);
}
Exemplo n.º 6
0
Arquivo: bank.c Projeto: yufangfred/hm
void bank_create_user(Bank* bank, char* args1, char* args2, char* args3) {	
	if (!args1 || !args2 || !args3) {
      printf("Usage: create-user <user-name> <pin> <balance>\n");
		return;		
	}
	
	if (check_username(args1) != 0 || check_pin(args2) != 0 
		|| check_balance(args3) != 0) {
   	printf("Usage: create-user <user-name> <pin> <balance>\n");
		return;	
	} 
	 
   if ((char*)hash_table_find(bank->user_balance_ht, args1) != NULL) {
       printf("Error: user %s already exists\n", args1);
       return;
   }

	FILE *fp;
	char card_name[MAX_CARD_NAME_SIZE + 1];
	
	memset(card_name, 0, MAX_CARD_NAME_SIZE);
   strcat(card_name, args1);
   strcat(card_name, ".card");

   fp = fopen(card_name, "w");
   if (!fp) {
       printf("Error creating card file for user %s\n", args1);
       return;
   }

	printf("Created user %s\n", args1);

	/* write to card*/
   fprintf(fp, "%s\n", args1);
   fprintf(fp, "%s\n", args2);
   fclose(fp);

 	char* tmp;
   tmp = (char*)malloc(64);
	int len = strlen(args3);
	if (len > 64) {
		printf("Usage: create-user <user-name> <pin> <balance>\n");
		return;
	}
   strncpy(tmp, args3, len);
   hash_table_add(bank->user_balance_ht, args1, tmp);
}
Exemplo n.º 7
0
int gpio_set_value(uint8_t gpio_pin, uint8_t value)
{
    uint8_t dir;

    if (!check_pin(gpio_pin))
        return -1;

    if (!is_gpio_exported(gpio_pin))
        return 0;

    if (gpio_get_direction(gpio_pin, &dir) < 0)
        return -1;

    if (dir == GPIO_INPUT) {
        fprintf(stderr, "gpio: Cannot set value to an input.\n");
        return -1;
    }

    /* Only write "0" or "1" to /sys/class/gpio/gpioN/value */
    return write_int_gpio_file(gpio_pin, "value", value == 0 ? 0 : 1);
}
Exemplo n.º 8
0
static void check_mode(gboolean ok, GAtResult *result, gpointer user_data)
{
	GAtResultIter iter;

	if (!ok) {
		g_print("Checking modem mode failed\n");
		exit(1);
	}

	g_at_result_iter_init(&iter, result);
	g_at_result_iter_next(&iter, "+CFUN:");
	g_at_result_iter_next_number(&iter, &oldmode);

	g_print("Current modem mode is %d\n", oldmode);

	if (oldmode == 1) {
		check_pin(ok, result, user_data);
		return;
	}

	g_at_chat_send(control, "AT+CFUN=1", NULL, check_pin, NULL, NULL);
}
Exemplo n.º 9
0
int gpio_init(uint8_t gpio_pin)
{
    if (!check_pin(gpio_pin))
        return -1;

    /* Ensure that PWM GPIO's are not used as PWM output at the same time */
    switch (gpio_pin) {
    case MIKROBUS_1_PWM:
        pwm_release(MIKROBUS_1);
        break;
    case MIKROBUS_2_PWM:
        pwm_release(MIKROBUS_2);
        break;
    }

    /* Export a GPIO by writing its index to file /sys/class/gpio/export. */
    if (!is_gpio_exported(gpio_pin)) {
        if (export_pin(GPIO_DIR_BASE_PATH, gpio_pin) < 0)
            return -1;
    }

    return gpio_set_direction(gpio_pin, GPIO_INPUT);
}
Exemplo n.º 10
0
Arquivo: bank.c Projeto: evanqi/atm
void bank_process_local_command(Bank *bank, char *command, size_t len)
{

    char *comm = (char *)calloc(MAX_COMMAND_SIZE, sizeof(char));
    char *name = (char *)calloc(MAX_COMMAND_SIZE, sizeof(char));
    char *misc = (char *)calloc(MAX_COMMAND_SIZE, sizeof(char));
    char *misc_two = (char *)calloc(MAX_COMMAND_SIZE, sizeof(char));

    char temp_comm[MAX_LINE_SIZE];
    char temp_name[MAX_LINE_SIZE];
    char temp_misc[MAX_LINE_SIZE];
    char temp_misc_two[MAX_LINE_SIZE];

    if(strlen(command) >= MAX_LINE_SIZE)
    {
        printf("Invalid command\n");
        return;
    }

    sscanf(command, "%s %s %s %s", temp_comm,temp_name,temp_misc,temp_misc_two);
    
    if(strlen(temp_comm) < 1 || strlen(temp_comm) >= MAX_COMMAND_SIZE)
    {
        printf("Invalid command\n");
        return;
    }

    strncpy(comm, temp_comm, strlen(temp_comm) + 1);

    if(strcmp(comm, "create-user") == 0)
    {
        if(strlen(temp_name) < 1 || strlen(temp_misc) < 1 || strlen(temp_misc_two) < 1 || strlen(temp_name) >= MAX_NAME_SIZE)
        {
            printf("Usage: create-user <user-name> <pin> <balance>\n");
            return;
        }

        strncpy(name, temp_name, strlen(temp_name) + 1);

        if(check_username(name) == 0)
        {
            printf("Usage: create-user <user-name> <pin> <balance>\n");
            return;
        }

        if(hash_table_find(bank->user_bal, name) != NULL)
        {
            printf("%s already exists\n", name);
            return;
        }

        if(strlen(temp_misc) != 4 || strlen(temp_misc_two) > 10)
        {
            printf("Usage: create-user <user-name> <pin> <balance>\n");
            return;
        }

        strncpy(misc, temp_misc, strlen(temp_misc) + 1);
        if(check_pin(misc) == 0)
        {
            printf("Usage: create-user <user-name> <pin> <balance>\n");
            return;
        }

        strncpy(misc_two, temp_misc_two, strlen(temp_misc_two) + 1);
        if(check_bal(misc_two) == 0)
        {
            printf("Usage: create-user <user-name> <pin> <balance>\n");
            return;
        }

         // create card
         int filelen = strlen(name) + 6;
         char cardfile[filelen];
         memset(cardfile, '\0', filelen);
         strncpy(cardfile, name, strlen(name));
         strncat(cardfile, ".card", 5);
         FILE *card = fopen(cardfile, "w");
         if(card == NULL){
             printf("Error creating card file for user %s\n", name);
             remove(cardfile);
             return;
        }
	unsigned char *buff = (unsigned char *)calloc(BLOCK_SIZE+1, sizeof(char));
	do_crypt(bank, (unsigned char *)misc, buff, 1);
	fputs((char *)buff, card);
        fclose(card);

        hash_table_add(bank->user_bal, name, misc_two); //username balance
        hash_table_add(bank->user_pin, name, misc);	//username pin

        printf("Created user %s\n", name);
        return;
    } 
    else if(strcmp(comm, "deposit") == 0)
    {
        if(strlen(temp_name) < 1 || strlen(temp_misc) < 1 || strlen(temp_name) >= MAX_NAME_SIZE)
        {
            printf("Usage: deposit <user-name> <amt>\n");
            return;
        }

        strncpy(name, temp_name, strlen(temp_name) + 1);
        if(check_username(name) != 1 || strlen(temp_misc) > 10)
        {
            printf("Usage: deposit <user-name> <amt>\n");
            return;
        }

        if(hash_table_find(bank->user_bal, name) == NULL)
        {
            printf("No such user\n");
            return;
        }

        strncpy(misc, temp_misc, strlen(temp_misc) + 1);
        if(check_bal(misc) == 0)
        {
            printf("Usage: deposit <user-name> <amt>\n");
            return;
        }
       	char *ptr;
        char* curr_bal = (char*) hash_table_find(bank->user_bal, name);
        
        unsigned int curr_bal_int = (unsigned int)strtoul(curr_bal, &ptr, 10);
    	unsigned int amt = (unsigned int)strtoul(misc, &ptr, 10);
   
        unsigned int new_bal = (unsigned int)amt + (unsigned int)curr_bal_int;  

        //checks if new_bal was capped
        if( new_bal > UINT_MAX || (new_bal - amt) != curr_bal_int || new_bal < curr_bal_int){
            printf("Too rich for this program\n");
            return;   
        }       

        char *new_bal_char = (char *)calloc(MAX_MISC_SIZE, sizeof(char));

        sprintf(new_bal_char, "%u", new_bal);

        hash_table_del(bank->user_bal, name);
        hash_table_add(bank->user_bal, name, new_bal_char);

        printf("$%s added to %s's account\n", misc, name);
        return;

    }
    else if(strcmp(comm, "balance") == 0)
    {
        if(strlen(temp_name) < 1)
        {
            printf("Usage: balance <user-name>\n");
            return;
        }

        if(strlen(temp_name) >= MAX_NAME_SIZE)
        {
            printf("Usage: balance <user-name>\n");
            return;
        }

        strncpy(name, temp_name, strlen(temp_name) + 1);
        if(check_username(name) != 1)
        {
            printf("Usage: balance <user-name>\n");
            return;
        }

        char *curr_bal = (char *) hash_table_find(bank->user_bal, name);
        if(curr_bal == NULL)
        {
            printf("No such user\n");
            return;
        }

        printf("$%s\n", curr_bal);
        return;

    }
    else
    {
        printf("Invalid command\n");
        return;
    }
}
Exemplo n.º 11
0
/**
 * Initialize the driver.
 * \param drvthis  Pointer to driver structure.
 * \retval 0       Success.
 * \retval -1      Error.
 */
int
hd_init_rpi(Driver *drvthis)
{
	PrivateData *p = (PrivateData *) drvthis->private_data;
	const int *allowed_gpio_pins = NULL;
	int used_pins[GPIO_PINS] = {};

	if ((allowed_gpio_pins = check_board_rev(drvthis)) == NULL)
		return -1;

	/* Get GPIO configuration */
	p->rpi_gpio = malloc(sizeof(struct rpi_gpio_map));
	if (p->rpi_gpio == NULL) {
		report(RPT_ERR, "hd_init_rpi: unable to allocate memory");
		return -1;
	}

	p->rpi_gpio->en = drvthis->config_get_int(drvthis->name, "pin_EN", 0, RPI_DEF_EN);
	p->rpi_gpio->rs = drvthis->config_get_int(drvthis->name, "pin_RS", 0, RPI_DEF_RS);
	p->rpi_gpio->d7 = drvthis->config_get_int(drvthis->name, "pin_D7", 0, RPI_DEF_D7);
	p->rpi_gpio->d6 = drvthis->config_get_int(drvthis->name, "pin_D6", 0, RPI_DEF_D6);
	p->rpi_gpio->d5 = drvthis->config_get_int(drvthis->name, "pin_D5", 0, RPI_DEF_D5);
	p->rpi_gpio->d4 = drvthis->config_get_int(drvthis->name, "pin_D4", 0, RPI_DEF_D4);

	debug(RPT_INFO, "hd_init_rpi: Pin EN mapped to GPIO%d", p->rpi_gpio->en);
	debug(RPT_INFO, "hd_init_rpi: Pin RS mapped to GPIO%d", p->rpi_gpio->rs);
	debug(RPT_INFO, "hd_init_rpi: Pin D4 mapped to GPIO%d", p->rpi_gpio->d4);
	debug(RPT_INFO, "hd_init_rpi: Pin D5 mapped to GPIO%d", p->rpi_gpio->d5);
	debug(RPT_INFO, "hd_init_rpi: Pin D6 mapped to GPIO%d", p->rpi_gpio->d6);
	debug(RPT_INFO, "hd_init_rpi: Pin D7 mapped to GPIO%d", p->rpi_gpio->d7);

	if (check_pin(drvthis, p->rpi_gpio->en, allowed_gpio_pins, used_pins) ||
	    check_pin(drvthis, p->rpi_gpio->rs, allowed_gpio_pins, used_pins) ||
	    check_pin(drvthis, p->rpi_gpio->d7, allowed_gpio_pins, used_pins) ||
	    check_pin(drvthis, p->rpi_gpio->d6, allowed_gpio_pins, used_pins) ||
	    check_pin(drvthis, p->rpi_gpio->d5, allowed_gpio_pins, used_pins) ||
	    check_pin(drvthis, p->rpi_gpio->d4, allowed_gpio_pins, used_pins)) {
		free(p->rpi_gpio);
		return -1;
	}

	if (p->numDisplays > 1) {	/* For displays with two controllers */
		p->rpi_gpio->en2 = drvthis->config_get_int(drvthis->name, "pin_EN2", 0, RPI_DEF_EN2);
		debug(RPT_INFO, "hd_init_rpi: Pin EN2 mapped to GPIO%d", p->rpi_gpio->en2);
		if (check_pin(drvthis, p->rpi_gpio->en2, allowed_gpio_pins, used_pins)) {
			free(p->rpi_gpio);
			return -1;
		}
	}

	if (p->have_backlight) {	/* Backlight setup is optional */
		p->backlight_bit = drvthis->config_get_int(drvthis->name, "pin_BL", 0, RPI_DEF_BL);
		debug(RPT_INFO, "hd_init_rpi: Backlight mapped to GPIO%d", p->backlight_bit);

		if (check_pin(drvthis, p->backlight_bit, allowed_gpio_pins, used_pins) != 0) {
			report(RPT_WARNING, "hd_init_rpi: Invalid backlight configuration - disabling backlight");
			p->have_backlight = 0;
		}
	}

	/* Now that configuration should be correct, set up the GPIO pins */
	if (setup_io(drvthis) < 0) {
		report(RPT_ERR, "hd_init_rpi: Failed to set up GPIO");
		free(p->rpi_gpio);
		return -1;
	}

	setup_gpio(drvthis, p->rpi_gpio->en);
	setup_gpio(drvthis, p->rpi_gpio->rs);
	setup_gpio(drvthis, p->rpi_gpio->d7);
	setup_gpio(drvthis, p->rpi_gpio->d6);
	setup_gpio(drvthis, p->rpi_gpio->d5);
	setup_gpio(drvthis, p->rpi_gpio->d4);

	p->hd44780_functions->senddata = lcdrpi_HD44780_senddata;
	p->hd44780_functions->close = lcdrpi_HD44780_close;

	if (p->have_backlight) {
		setup_gpio(drvthis, p->backlight_bit);
		p->hd44780_functions->backlight = lcdrpi_HD44780_backlight;
	}

	if (p->numDisplays > 1) {
		setup_gpio(drvthis, p->rpi_gpio->en2);
	}

	/* Setup the lcd in 4 bit mode: Send (FUNCSET | IF_8BIT) three times
	 * followed by (FUNCSET | IF_4BIT) using four nibbles. Timing is not
	 * exactly what is required by HD44780. */
	p->hd44780_functions->senddata(p, 0, RS_INSTR, 0x33);
	p->hd44780_functions->uPause(p, 4100);
	p->hd44780_functions->senddata(p, 0, RS_INSTR, 0x32 );
	p->hd44780_functions->uPause(p, 150);

	common_init(p, IF_4BIT);

	return 0;
}