int i2cdump_main(int argc UNUSED_PARAM, char **argv) { const unsigned opt_f = (1 << 0), opt_y = (1 << 1), opt_r = (1 << 2); const char *const optstr = "fyr:"; int bus_num, bus_addr, mode = I2C_SMBUS_BYTE_DATA, even = 0, pec = 0; unsigned first = 0x00, last = 0xff, opts; int *block = (int *)bb_common_bufsiz1; char *opt_r_str, *dash; int fd, res; opt_complementary = "-2:?3"; /* from 2 to 3 args */ opts = getopt32(argv, optstr, &opt_r_str); argv += optind; bus_num = i2c_bus_lookup(argv[0]); bus_addr = i2c_parse_bus_addr(argv[1]); if (argv[2]) { switch (argv[2][0]) { case 'b': /* Already set. */ break; case 'c': mode = I2C_SMBUS_BYTE; break; case 'w': mode = I2C_SMBUS_WORD_DATA; break; case 'W': mode = I2C_SMBUS_WORD_DATA; even = 1; break; case 's': mode = I2C_SMBUS_BLOCK_DATA; break; case 'i': mode = I2C_SMBUS_I2C_BLOCK_DATA; break; default: bb_error_msg_and_die("invalid mode"); } if (argv[2][1] == 'p') { if (argv[2][0] == 'W' || argv[2][0] == 'i') { bb_error_msg_and_die( "pec not supported for -W and -i"); } else { pec = 1; } } } if (opts & opt_r) { first = strtol(opt_r_str, &dash, 0); if (dash == opt_r_str || *dash != '-' || first > 0xff) bb_error_msg_and_die("invalid range"); last = xstrtou_range(++dash, 0, first, 0xff); /* Range is not available for every mode. */ switch (mode) { case I2C_SMBUS_BYTE: case I2C_SMBUS_BYTE_DATA: break; case I2C_SMBUS_WORD_DATA: if (!even || (!(first % 2) && last % 2)) break; /* Fall through */ default: bb_error_msg_and_die( "range not compatible with selected mode"); } } fd = i2c_dev_open(bus_num); check_read_funcs(fd, mode, -1 /* data_addr */, pec); i2c_set_slave_addr(fd, bus_addr, opts & opt_f); if (pec) i2c_set_pec(fd, 1); if (!(opts & opt_y)) confirm_action(bus_addr, mode, -1 /* data_addr */, pec); /* All but word data. */ if (mode != I2C_SMBUS_WORD_DATA || even) { int blen = 0; if (mode == I2C_SMBUS_BLOCK_DATA || mode == I2C_SMBUS_I2C_BLOCK_DATA) blen = read_block_data(fd, mode, block); if (mode == I2C_SMBUS_BYTE) { res = i2c_smbus_write_byte(fd, first); if (res < 0) bb_perror_msg_and_die("write start address"); } dump_data(fd, mode, first, last, block, blen); } else { dump_word_data(fd, first, last); } return 0; }
int i2cset_main(int argc, char **argv) { const unsigned opt_f = (1 << 0), opt_y = (1 << 1), opt_m = (1 << 2), opt_r = (1 << 3); const char *const optstr = "fym:r"; int bus_num, bus_addr, data_addr, mode = I2C_SMBUS_BYTE, pec = 0; int val, blen = 0, mask = 0, fd, status; unsigned char block[I2C_SMBUS_BLOCK_MAX]; char *opt_m_arg = NULL; unsigned opts; opt_complementary = "-3"; /* from 3 to ? args */ opts = getopt32(argv, optstr, &opt_m_arg); argv += optind; argc -= optind; bus_num = i2c_bus_lookup(argv[0]); bus_addr = i2c_parse_bus_addr(argv[1]); data_addr = i2c_parse_data_addr(argv[2]); if (argv[3]) { if (!argv[4] && argv[3][0] != 'c') { mode = I2C_SMBUS_BYTE_DATA; /* Implicit b */ } else { switch (argv[argc-1][0]) { case 'c': /* Already set */ break; case 'b': mode = I2C_SMBUS_BYTE_DATA; break; case 'w': mode = I2C_SMBUS_WORD_DATA; break; case 's': mode = I2C_SMBUS_BLOCK_DATA; break; case 'i': mode = I2C_SMBUS_I2C_BLOCK_DATA; break; default: bb_error_msg("invalid mode"); bb_show_usage(); } pec = argv[argc-1][1] == 'p'; if (mode == I2C_SMBUS_BLOCK_DATA || mode == I2C_SMBUS_I2C_BLOCK_DATA) { if (pec && mode == I2C_SMBUS_I2C_BLOCK_DATA) bb_error_msg_and_die( "PEC not supported for I2C " "block writes"); if (opts & opt_m) bb_error_msg_and_die( "mask not supported for block " "writes"); } } } /* Prepare the value(s) to be written according to current mode. */ switch (mode) { case I2C_SMBUS_BYTE_DATA: val = xstrtou_range(argv[3], 0, 0, 0xff); break; case I2C_SMBUS_WORD_DATA: val = xstrtou_range(argv[3], 0, 0, 0xffff); break; case I2C_SMBUS_BLOCK_DATA: case I2C_SMBUS_I2C_BLOCK_DATA: for (blen = 3; blen < (argc - 1); blen++) block[blen] = xstrtou_range(argv[blen], 0, 0, 0xff); val = -1; break; default: val = -1; break; } if (opts & opt_m) { mask = xstrtou_range(opt_m_arg, 0, 0, (mode == I2C_SMBUS_BYTE || mode == I2C_SMBUS_BYTE_DATA) ? 0xff : 0xffff); } fd = i2c_dev_open(bus_num); check_write_funcs(fd, mode, pec); i2c_set_slave_addr(fd, bus_addr, opts & opt_f); if (!(opts & opt_y)) confirm_action(bus_addr, mode, data_addr, pec); /* * If we're using mask - read the current value here and adjust the * value to be written. */ if (opts & opt_m) { int tmpval; switch (mode) { case I2C_SMBUS_BYTE: tmpval = i2c_smbus_read_byte(fd); break; case I2C_SMBUS_WORD_DATA: tmpval = i2c_smbus_read_word_data(fd, data_addr); break; default: tmpval = i2c_smbus_read_byte_data(fd, data_addr); } if (tmpval < 0) bb_perror_msg_and_die("can't read old value"); val = (val & mask) | (tmpval & ~mask); if (!(opts & opt_y)) { bb_error_msg("old value 0x%0*x, write mask " "0x%0*x, will write 0x%0*x to register " "0x%02x", mode == I2C_SMBUS_WORD_DATA ? 4 : 2, tmpval, mode == I2C_SMBUS_WORD_DATA ? 4 : 2, mask, mode == I2C_SMBUS_WORD_DATA ? 4 : 2, val, data_addr); confirm_or_abort(); } } if (pec) i2c_set_pec(fd, 1); switch (mode) { case I2C_SMBUS_BYTE: status = i2c_smbus_write_byte(fd, data_addr); break; case I2C_SMBUS_WORD_DATA: status = i2c_smbus_write_word_data(fd, data_addr, val); break; case I2C_SMBUS_BLOCK_DATA: status = i2c_smbus_write_block_data(fd, data_addr, blen, block); break; case I2C_SMBUS_I2C_BLOCK_DATA: status = i2c_smbus_write_i2c_block_data(fd, data_addr, blen, block); break; default: /* I2C_SMBUS_BYTE_DATA */ status = i2c_smbus_write_byte_data(fd, data_addr, val); break; } if (status < 0) bb_perror_msg_and_die("write failed"); if (pec) i2c_set_pec(fd, 0); /* Clear PEC. */ /* No readback required - we're done. */ if (!(opts & opt_r)) return 0; switch (mode) { case I2C_SMBUS_BYTE: status = i2c_smbus_read_byte(fd); val = data_addr; break; case I2C_SMBUS_WORD_DATA: status = i2c_smbus_read_word_data(fd, data_addr); break; default: /* I2C_SMBUS_BYTE_DATA */ status = i2c_smbus_read_byte_data(fd, data_addr); } if (status < 0) { puts("Warning - readback failed"); } else if (status != val) { printf("Warning - data mismatch - wrote " "0x%0*x, read back 0x%0*x\n", mode == I2C_SMBUS_WORD_DATA ? 4 : 2, val, mode == I2C_SMBUS_WORD_DATA ? 4 : 2, status); } else { printf("Value 0x%0*x written, readback matched\n", mode == I2C_SMBUS_WORD_DATA ? 4 : 2, val); } return 0; }
int i2cget_main(int argc UNUSED_PARAM, char **argv) { const unsigned opt_f = (1 << 0), opt_y = (1 << 1); const char *const optstr = "fy"; int bus_num, bus_addr, data_addr = -1, status; int mode = I2C_SMBUS_BYTE, pec = 0, fd; unsigned opts; opt_complementary = "-2:?4"; /* from 2 to 4 args */ opts = getopt32(argv, optstr); argv += optind; bus_num = i2c_bus_lookup(argv[0]); bus_addr = i2c_parse_bus_addr(argv[1]); if (argv[2]) { data_addr = i2c_parse_data_addr(argv[2]); mode = I2C_SMBUS_BYTE_DATA; if (argv[3]) { switch (argv[3][0]) { case 'b': /* Already set */ break; case 'w': mode = I2C_SMBUS_WORD_DATA; break; case 'c': mode = I2C_SMBUS_BYTE; break; default: bb_error_msg("invalid mode"); bb_show_usage(); } pec = argv[3][1] == 'p'; } } fd = i2c_dev_open(bus_num); check_read_funcs(fd, mode, data_addr, pec); i2c_set_slave_addr(fd, bus_addr, opts & opt_f); if (!(opts & opt_y)) confirm_action(bus_addr, mode, data_addr, pec); if (pec) i2c_set_pec(fd, 1); switch (mode) { case I2C_SMBUS_BYTE: if (data_addr >= 0) { status = i2c_smbus_write_byte(fd, data_addr); if (status < 0) bb_error_msg("warning - write failed"); } status = i2c_smbus_read_byte(fd); break; case I2C_SMBUS_WORD_DATA: status = i2c_smbus_read_word_data(fd, data_addr); break; default: /* I2C_SMBUS_BYTE_DATA */ status = i2c_smbus_read_byte_data(fd, data_addr); } close(fd); if (status < 0) bb_perror_msg_and_die("read failed"); printf("0x%0*x\n", mode == I2C_SMBUS_WORD_DATA ? 4 : 2, status); return 0; }
/*! \brief Save/Load menu * * This is the actual save/load menu. The only parameter to * the function indicates whether we are saving or loading. * * \param am_saving 0 if loading, 1 if saving * \returns 0 if an error occurred or save/load cancelled */ static int saveload (int am_saving) { int stop = 0; // Have no more than 5 savestate boxes onscreen, but fewer if NUMSG < 5 max_onscreen = 5; if (max_onscreen > NUMSG) max_onscreen = NUMSG; play_effect (SND_MENU, 128); while (!stop) { check_animation (); clear_bitmap (double_buffer); show_sgstats (am_saving); blit2screen (0, 0); readcontrols (); if (up) { unpress (); save_ptr--; if (save_ptr < 0) save_ptr = NUMSG - 1; // Determine whether to update TOP if (save_ptr < top_pointer) top_pointer--; else if (save_ptr == NUMSG - 1) top_pointer = NUMSG - max_onscreen; play_effect (SND_CLICK, 128); } if (down) { unpress (); save_ptr++; if (save_ptr > NUMSG - 1) save_ptr = 0; // Determine whether to update TOP if (save_ptr >= top_pointer + max_onscreen) top_pointer++; else if (save_ptr == 0) top_pointer = 0; play_effect (SND_CLICK, 128); } if (right) { unpress (); if (am_saving < 2) am_saving = am_saving + 2; } if (left) { unpress (); if (am_saving >= 2) am_saving = am_saving - 2; } if (balt) { unpress (); switch (am_saving) { case 0: // Load if (snc[save_ptr] != 0) { if (load_game () == 1) stop = 2; else stop = 1; } break; case 1: // Save if (confirm_action () == 1) { if (save_game () == 1) stop = 2; else stop = 1; } break; case 2: // Delete (was LOAD) previously case 3: // Delete (was SAVE) previously if (snc[save_ptr] != 0) { if (confirm_action () == 1) delete_game (); } break; } } if (bctrl) { unpress (); stop = 1; } } return stop - 1; }
void file_write (File *file, const char* directory) { char *path = NULL; assert (file); if (!file) return; if (file->name == NULL) { file->name = strdup( TNEF_DEFAULT_FILENAME ); debug_print ("No file name specified, using default %s.\n", TNEF_DEFAULT_FILENAME); } if ( file->path == NULL ) { file->path = munge_fname( file->name ); if (file->path == NULL) { file->path = strdup( TNEF_DEFAULT_FILENAME ); debug_print ("No path name available, using default %s.\n", TNEF_DEFAULT_FILENAME); } } path = concat_fname( directory, file->path ); if (path == NULL) { path = strdup( TNEF_DEFAULT_FILENAME ); debug_print ("No path generated, using default %s.\n", TNEF_DEFAULT_FILENAME); } debug_print ("%sWRITING\t|\t%s\t|\t%s\n", ((LIST_ONLY==0)?"":"NOT "), file->name, path); if (!LIST_ONLY) { FILE *fp = NULL; if (!confirm_action ("extract %s?", file->name)) return; if (!OVERWRITE_FILES) { if (file_exists (path)) { if (!NUMBER_FILES) { fprintf (stderr, "tnef: %s: Could not create file: File exists\n", path); return; } else { char *tmp = find_free_number (path); debug_print ("Renaming %s to %s\n", path, tmp); XFREE (path); path = tmp; } } } fp = fopen (path, "wb"); if (fp == NULL) { perror (path); exit (1); } if (fwrite (file->data, 1, file->len, fp) != file->len) { perror (path); exit (1); } fclose (fp); } if (LIST_ONLY || VERBOSE_ON) { if (LIST_ONLY && VERBOSE_ON) { /* FIXME: print out date and stuff */ const char *date_str = date_to_str(&file->dt); fprintf (stdout, "%11lu\t|\t%s\t|\t%s\t|\t%s", (unsigned long)file->len, date_str+4, /* skip the day of week */ file->name, path); } else { fprintf (stdout, "%s\t|\t%s", file->name, path); } if ( SHOW_MIME ) { fprintf (stdout, "\t|\t%s", file->mime_type ? file->mime_type : "unknown"); fprintf (stdout, "\t|\t%s", file->content_id ? file->content_id : ""); } fprintf (stdout, "\n"); } XFREE(path); }
int main(int argc, char** argv) { struct eeprom e; int ret, op, i2c_addr, memaddr, size, want_hex, dummy, force, sixteen; char *device, *arg = 0, *i2c_addr_s; struct stat st; int eeprom_type = 0; op = want_hex = dummy = force = sixteen = 0; g_quiet = 0; while((ret = getopt(argc, argv, "1:8fr:qhw:xd")) != -1) { switch(ret) { case '1': usage_if(*optarg != '6' || strlen(optarg) != 1); die_if(eeprom_type, "EEPROM type switch (-8 or -16) used twice"); eeprom_type = EEPROM_TYPE_16BIT_ADDR; break; case 'x': want_hex++; break; case 'd': dummy++; break; case '8': die_if(eeprom_type, "EEPROM type switch (-8 or -16) used twice"); eeprom_type = EEPROM_TYPE_8BIT_ADDR; break; case 'f': force++; break; case 'q': g_quiet++; break; case 'h': usage_if(1); break; default: die_if(op != 0, "Both read and write requested"); arg = optarg; op = ret; } } if(!eeprom_type) eeprom_type = EEPROM_TYPE_8BIT_ADDR; // default usage_if(op == 0); // no switches // set device and i2c_addr reading from cmdline or env device = i2c_addr_s = 0; switch(argc - optind) { case 0: device = getenv(ENV_DEV); i2c_addr_s = getenv(ENV_I2C_ADDR); break; case 1: if(stat(argv[optind], &st) != -1) { device = argv[optind]; i2c_addr_s = getenv(ENV_I2C_ADDR); } else { device = getenv(ENV_DEV); i2c_addr_s = argv[optind]; } break; case 2: device = argv[optind++]; i2c_addr_s = argv[optind]; break; default: usage_if(1); } usage_if(!device || !i2c_addr_s); i2c_addr = strtoul(i2c_addr_s, 0, 0); print_info("eeprog %s, a 24Cxx EEPROM reader/writer\n", VERSION); print_info("Copyright (c) 2003 by Stefano Barbato - All rights reserved.\n"); print_info(" Bus: %s, Address: 0x%x, Mode: %dbit\n", device, i2c_addr, (eeprom_type == EEPROM_TYPE_8BIT_ADDR ? 8 : 16) ); if(dummy) { fprintf(stderr, "Dummy mode selected, nothing done.\n"); return 0; } die_if(eeprom_open(device, i2c_addr, eeprom_type, &e) < 0, "unable to open eeprom device file (check that the file exists and that it's readable)"); switch(op) { case 'r': if(force == 0) confirm_action(); size = 1; // default parse_arg(arg, &memaddr, &size); print_info(" Reading %d bytes from 0x%x\n", size, memaddr); read_from_eeprom(&e, memaddr, size, want_hex); break; case 'w': if(force == 0) confirm_action(); parse_arg(arg, &memaddr, &size); print_info(" Writing stdin starting at address 0x%x\n", memaddr); write_to_eeprom(&e, memaddr); break; default: usage_if(1); exit(1); } eeprom_close(&e); return 0; }