int config_write(struct config_data *config_data) { FILE *config_fp; uint16_t crc; int i; crc = CRCCCITT(config_data->deflated_data, config_data->deflated_stream_size, 0xFFFF); printf("\n writing to %s - size 0x%04x crc 0x%04x 0x%04x\n", config_data->name, config_data->deflated_stream_size, config_data->crc, crc); config_fp = fopen(config_data->name, "wb"); if (!config_fp) { fprintf(stderr, "\ncan't open file %s for writing - error: %s\n", config_data->name, strerror(errno)); exit(EXIT_FAILURE); } else { for (i = 0; i < config_data->deflated_stream_size; i++) { if ((i % 8) == 0) printf("\n"); printf("%02x ", config_data->deflated_data[i]); } printf("\n"); } inflate_data(config_data); fwrite(config_data->inflated_data, 1, config_data->inflated_size, config_fp); fclose(config_fp); return 1; }
int get_data(struct trigger_t *trigger, struct can_frame *frame) { uint16_t crc; if (frame->can_dlc == 6) { trigger->length = (frame->data[2] << 8) | frame->data[3]; trigger->crc = (frame->data[4] << 8) | frame->data[5]; printf("length 0x%04x crc 0x%04x\n", trigger->length, trigger->crc); trigger->data = (uint8_t *) calloc(trigger->length + 1, 1); if (!trigger->data) { fprintf(stderr, "can't alloc memory for config data: %s\n", strerror(errno)); return (EXIT_FAILURE); } trigger->data_index = 0; return 0; } if (trigger->data_index < trigger->length) { memcpy(&trigger->data[trigger->data_index], frame->data, 8); trigger->data_index += 8; } else { printf("Error: unexpected data\n"); return (EXIT_FAILURE); } if (trigger->data_index == trigger->length) { if (!trigger->background && trigger->verbose) printf("data complete %d %d\n", trigger->data_index, trigger->length); crc = CRCCCITT(trigger->data, trigger->length, 0xFFFF); if (crc == trigger->crc) { if (!trigger->background && trigger->verbose) printf("crc 0x%04x 0x%04x\n", crc, trigger->crc); strip_ms2_spaces(trigger->data, trigger->length); } printf("Data:\n%s\n", trigger->data); read_loco_data((char *)trigger->data, CONFIG_STRING); print_locos(); printf("max locos : %d\n", get_loco_max()); set_led_pattern(trigger, LED_ST_HB_SLOW); free(trigger->data); } return 0; }