/** * * * @author chenxb (10/2/2010) * * @return int */ int load_cfg() { INI *inidev, *iniplan, *iniaction; inidev = ini_open(DEV_INI); if( !inidev) { config_debug("(load_cfg) err: open %s\n", DEV_INI); return -1; } dev_cfg(inidev); ini_close(inidev); iniplan = ini_open(PLAN_INI); if( !iniplan) { config_debug("(load_cfg): err, open %s\n", PLAN_INI); return -1; } plan_cfg(iniplan); ini_close(iniplan); iniaction = ini_open(AC_INI); if(!iniaction) { config_debug("(load_cfg): err, open %s\n", AC_INI); return -1; } ac_cfg(iniaction); ini_close(iniaction); printf("load_cfg ok !\n"); return 0; }
int usbsign_open(int vendorid, int productid, int interface, usbsign_handle** dev) { usb_init(); usb_find_busses(); usb_find_devices(); struct usb_bus* bus; struct usb_device* usbdev; int found = 0; for (bus = usb_get_busses(); bus; bus = bus->next) { config_debug("BUS: %s",bus->dirname); for (usbdev = bus->devices; usbdev; usbdev = usbdev->next) { struct usb_device_descriptor *desc = &(usbdev->descriptor); config_debug(" Device: %s", usbdev->filename); config_debug(" %x:%x",desc->idVendor,desc->idProduct); if (usbdev->descriptor.idVendor == vendorid && usbdev->descriptor.idProduct == productid) { found = 1; break; } } if (found > 0) { break; } } if (found > 0) { *dev = usb_open(usbdev); } else { *dev = NULL; } if (*dev == NULL) { config_error("Could not find/open USB device with vid=0x%X pid=0x%X. Is the sign plugged in?", vendorid, productid); return -1; } int ret = usb_claim_interface(*dev, interface); if (ret < 0) { config_error("Could not claim device (%d)", ret); return ret; } return 0; }
//services //---------------------------------------------------------------- void init() { //configure metadata config_metadata(); //set debug mode , all contexts if ( config_debug() ) { //set callbacks config_callbacks(); } run( true ); }
void ISOP2P1::initialize() { /// 读取配置文件. config("config"); /// debug config_debug(); getchar(); /// 构建网格. buildMesh(); /// 构建混合有限元 ISOP1P2 空间. buildFEMSpace(); /// 构建稀疏矩阵模板. buildMatrixStruct(); /// 构建矩阵. buildMatrix(); if (Stokes_init == 1 || NS_init == 1) solveStokes(); };
static int dev_cfg( INI *dev) { int ret = 0; config_debug("(dev_cfg): start\n"); ret = lead_amp_load_cfg(dev); if( ret < 0) { config_debug( "(head_amp_load_cfg) : return err! dev = %x\n", (unsigned int)dev); } ret = peripheral_load_cfg(dev); if( ret < 0) { config_debug("(peripheral_load_cfg): return err! ret=%d, dev=%x\n",ret, (unsigned int)dev); } ret = caller_load_cfg(dev); if( ret < 0) { config_debug("(caller_load_cfg): return err! ret=%d, dev=%x\n",ret, (unsigned int)dev); } ret = select_amp_load_cfg(dev); if( ret < 0) { config_debug("(select_amp_load_cfg): return err! ret=%d, dev=%x\n",ret, (unsigned int)dev); } ret = matrix_load_cfg(dev); if(ret <0){ config_debug("(matrix_load_cfg): return err! ret = %d, dev= %x\n", ret, ( unsigned int )dev); } ret = power_load_cfg(dev); if(ret <0){ power_debug("(power_load_cfg): return err! ret = %d, dev= %x\n", ret, ( unsigned int )dev); } ret = sound_load_cfg(dev); if(ret < 0) { config_debug("(sound_load_cfg): err!, ret = %d, dev = %x\n", ret, (unsigned int) dev); } return ret; }
int main(int argc, char* argv[]) { config_fout = stdout; config_ferr = stderr; if (argc == 1) { help(argv[0]); return -1; } int mode_specified = 0, do_init = 0; char* configpath = NULL; FILE* configfile; int c; while (1) { static struct option long_options[] = { {"help", 0, NULL, 'h'}, {"verbose", 0, NULL, 'v'}, {"log", required_argument, NULL, 'l'}, {"init", 0, NULL, 'i'}, {"update", 0, NULL, 'u'}, {0,0,0,0} }; int option_index = 0; c = getopt_long(argc, argv, "hvl:iu", long_options, &option_index); if (c == -1) {//unknown arg (doesnt match -x/--x format) if (optind >= argc) { //at end of successful parse break; } //getopt refuses to continue, so handle infile manually: int i = optind; for (; i < argc; ++i) { configpath = argv[i]; //debug("%d %d %s", argc, i, arg); configfile = fopen(configpath,"r"); if (configfile == NULL) { config_error("Unable to open config file %s: %s", configpath, strerror(errno)); mini_help(argv[0]); return -1; } } break; } switch (c) { case 'h': help(argv[0]); return -1; case 'v': config_debug_enabled = 1; break; case 'l': { FILE* logfile = fopen(optarg, "a"); if (logfile == NULL) { config_error("Unable to open log file %s: %s", optarg, strerror(errno)); return -1; } config_fout = logfile; config_ferr = logfile; } break; case 'i': mode_specified = 1; do_init = 1; break; case 'u': mode_specified = 1; do_init = 0; break; default: mini_help(argv[0]); return -1; } } if (!mode_specified) { config_error("-i/-u mode argument required."); mini_help(argv[0]); } int error = -1; if (configpath == NULL) { configpath = "<stdin>"; configfile = stdin; } config_log("Parsing %s",configpath); //Get and parse bb_frames (both STRINGs and TEXTs) from config: struct bb_frame* startframe = NULL; if (parsefile(&startframe,configfile) < 0) { fclose(configfile); config_error("Error encountered when parsing config file. "); mini_help(argv[0]); goto end_noclose; } fclose(configfile); if (startframe == NULL) { config_error("Empty config file, nothing to do. "); mini_help(argv[0]); goto end_noclose; } usbsign_handle* devh = NULL; if (hardware_init(&devh) < 0) { config_error("USB init failed: Exiting. "); mini_help(argv[0]); goto end_noclose; } char* packet = NULL; int pktsize; config_log("Writing to sign"); //send sequence header before we start sending packets if (!hardware_seqstart(devh)) { //try resetting device once config_error("Initial write failed, attempting reset."); if (!hardware_reset(&devh)) { config_error("Reset failed: Exiting."); goto end; } if (!hardware_seqstart(devh)) { config_error("Initial write retry failed, giving up."); goto end; } config_log("Reset successful, continuing"); } if (do_init) { //this packet allocates sign memory for messages: if ((pktsize = packet_buildmemconf(&packet,startframe)) < 0) { goto end; } if (hardware_sendpkt(devh,packet,pktsize) != pktsize) { goto end; } free(packet); packet = NULL; } //now on to the real messages: struct bb_frame* curframe = startframe; while (curframe != NULL) { config_debug("result: data=%s",curframe->data); if (curframe->frame_type == STRING_FRAME_TYPE) { //data will be updated often, store in a STRING file pktsize = packet_buildstring(&packet,curframe->filename, curframe->data); } else if (curframe->frame_type == TEXT_FRAME_TYPE) { if (!do_init) { curframe = curframe->next; config_debug(" ^-- SKIPPING: init-only packet"); continue; } //data wont be updated often, use a TEXT file pktsize = packet_buildtext(&packet,curframe->filename, curframe->mode,curframe->mode_special, curframe->data); } else { config_error("Internal error: Unknown frame type %d",curframe->frame_type); goto end; } if (hardware_sendpkt(devh,packet,pktsize) != pktsize) { goto end; } free(packet); packet = NULL; curframe = curframe->next; } if (do_init) { //set display order for the messages: pktsize = packet_buildrunseq(&packet,startframe); if (hardware_sendpkt(devh,packet,pktsize) != pktsize) { goto end; } free(packet); packet = NULL; } //finish it off with a sequence footer if (!hardware_seqend(devh)) { goto end; } error = 0; end: hardware_close(devh); end_noclose: if (startframe != NULL) { packet_delete(startframe); free(startframe); } return error; }
int parsefile(struct bb_frame** output, FILE* file) { int error = 0, linenum = 0; char filename = 0; char* line = NULL; ssize_t line_len; struct bb_frame* head = NULL; struct bb_frame* curframe = NULL; struct bb_frame** nextframeptr = &curframe; while ((line_len = readline(&line,file)) > 0) { ++linenum; config_debug("%s",line); char delim[] = {' ','\0'}; char delim_endline[] = {'\n','\0'}; char* tmp; char* cmd = strtok_r(line,delim,&tmp); if (strcmp(cmd,"txt") == 0) { char* mode = strtok_r(NULL,delim,&tmp); char* text = strtok_r(NULL,delim_endline,&tmp); if (checkmode(mode,text,linenum) < 0) { error = 1; break; } *nextframeptr = malloc(sizeof(struct bb_frame)); curframe = *nextframeptr; curframe->next = NULL; curframe->mode = tolower(mode[0]); if (strlen(mode) > 1) { curframe->mode_special = toupper(mode[1]); } else { curframe->mode_special = NO_SPECIAL; } if (text == NULL) { curframe->data = NULL; } else { int is_trimmed = 0; int charsparsed = parse_inline_cmds(&curframe->data,&is_trimmed, text,MAX_TEXTFILE_DATA_SIZE); if (is_trimmed) { config_error("Warning, line %d: Data has been truncated at input index %d to fit %d available output bytes.", linenum,charsparsed,MAX_TEXTFILE_DATA_SIZE); config_error("Input vs output bytecount can vary if you used inline commands in your input."); } } curframe->frame_type = TEXT_FRAME_TYPE; filename = packet_next_filename(filename); if (filename <= 0) { error = 1; break; } curframe->filename = filename; nextframeptr = &curframe->next; } else if (strcmp(cmd,"cmd") == 0) { char* mode = strtok_r(NULL,delim,&tmp); char* command = strtok_r(NULL,delim_endline,&tmp); if (checkmode(mode,command,linenum) < 0) { error = 1; break; } char* raw_result; if (runcmd(&raw_result,command) < 0) { error = 1; break; } //data for the TEXT frame which will reference these STRING frames: char refchar = 0x10;//format for each reference is 2 bytes: "0x10, filename" (pg55) char* textrefs = (char*)calloc(2*MAX_STRINGFILE_GROUP_COUNT+1,sizeof(char));//include \0 in size textrefs[2*MAX_STRINGFILE_GROUP_COUNT] = 0;//set \0 //Create and append STRING frames: int i, cumulative_parsed = 0; for (i = 0; i < MAX_STRINGFILE_GROUP_COUNT; i++) { *nextframeptr = malloc(sizeof(struct bb_frame)); curframe = *nextframeptr; curframe->next = NULL; if (head == NULL) { head = curframe; } int is_trimmed = 0; char* parsed_result; int charsparsed = parse_inline_cmds((char**)&parsed_result,&is_trimmed, &raw_result[cumulative_parsed], MAX_STRINGFILE_DATA_SIZE); cumulative_parsed += charsparsed; if (is_trimmed && i+1 == MAX_STRINGFILE_GROUP_COUNT) { config_error("Warning, line %d: Data has been truncated at input index %d to fit %d available output bytes.", linenum,cumulative_parsed,MAX_STRINGFILE_GROUP_COUNT*MAX_STRINGFILE_DATA_SIZE); config_error("Input vs output bytecount can vary if you used inline commands in your input."); } curframe->data = malloc(MAX_STRINGFILE_DATA_SIZE+1); strncpy(curframe->data,parsed_result, MAX_STRINGFILE_DATA_SIZE); curframe->data[MAX_STRINGFILE_DATA_SIZE] = 0; free(parsed_result); config_debug(">%d %s",i,curframe->data); filename = packet_next_filename(filename); if (filename <= 0) { error = 1; break; } curframe->filename = filename; //add a reference for ourselves to the TEXT frame: textrefs[2*i] = refchar; textrefs[2*i+1] = filename; curframe->frame_type = STRING_FRAME_TYPE; nextframeptr = &curframe->next; } free(raw_result); if (error == 1) { break; } //Append TEXT frame containing references to those STRINGs: *nextframeptr = malloc(sizeof(struct bb_frame)); curframe = *nextframeptr; curframe->next = NULL; curframe->mode = mode[0]; if (strlen(mode) > 1) { curframe->mode_special = mode[1]; } else { curframe->mode_special = NO_SPECIAL; } curframe->data = textrefs; curframe->frame_type = TEXT_FRAME_TYPE; filename = packet_next_filename(filename); if (filename <= 0) { error = 1; break; } curframe->filename = filename; nextframeptr = &curframe->next; } else if ((strlen(cmd) >= 2 && cmd[0] == '/' && cmd[1] == '/') || (strlen(cmd) >= 1 && cmd[0] == '#')) { //comment in input file, do nothing } else { config_error("Syntax error, line %d: Unknown command.",linenum); error = 1; break; } if (head == NULL) { head = curframe; } free(line); line = NULL; } if (line != NULL) { free(line); line = NULL; } *output = head; return (error == 0) ? 0 : -1; }
static int parse_inline_cmds(char** outptr, int* output_is_trimmed, char* in, unsigned int maxout) { config_debug("orig: %s",in); size_t iin = 0, iout = 0; char* out = malloc(maxout); while (iin < strlen(in)) { if (iout == maxout) { //stop!: reached max output size *output_is_trimmed = 1; break; } if (in[iin] == '\n' || in[iin] == '\t') { //replace newlines and tabs with spaces: out[iout++] = ' '; ++iin; } else if (in[iin] < 0x20) { //ignore all other special chars <0x20: ++iin; } else if (in[iin] == '<' || in[iin] == '&') { int i = 0, parsedlen = 0; char addme[32]; while (replacesrc[i] != 0) { //search replacement table: if (strncmp(replacesrc[i],&in[iin], strlen(replacesrc[i])) == 0) { strncpy(addme,replacedst[i],strlen(replacedst[i])+1); parsedlen = strlen(replacesrc[i]); break; } ++i; } if (parsedlen == 0) { //special tags: colorRGB/scolorRGB(0-3) speedN(1-6) if (strncmp("<color",&in[iin],6) == 0 && index(&in[iin+6],'>') == &in[iin+9]) {//<colorRGB> if (parse_color_code(&addme[2],&in[iin+6]) >= 0) { addme[0] = 0x1c; addme[1] = 'Z'; addme[COLOR_LEN+2] = 0; parsedlen = 10; } } else if (strncmp("<scolor",&in[iin],7) == 0 && index(&in[iin+7],'>') == &in[iin+10]) {//<colorRGB> if (parse_color_code(&addme[2],&in[iin+7]) >= 0) { addme[0] = 0x1c; addme[1] = 'Y'; addme[COLOR_LEN+2] = 0; parsedlen = 11; } } else if (strncmp("<speed",&in[iin],6) == 0 && index(&in[iin+6],'>') == &in[iin+7]) {//<speedN> char speedin = in[iin+6]; if (speedin >= '1' && speedin <= '5') { //speeds 1(0x31)-5(0x35) -> char 0x15-0x19 addme[0] = speedin-'0'+0x14; addme[1] = 0; parsedlen = 8; } else if (speedin == '6') { //speed 6 -> char 0x9 (nohold) addme[0] = 0x9; addme[1] = 0; parsedlen = 8; } else { config_error("Found invalid speed \"%c\" in <speedN> (N=1-6).", speedin); } } } if (parsedlen != 0) { int addme_size = strlen(addme); if (addme_size + iout > maxout) { //stop!: too big to fit in buffer *output_is_trimmed = 1; break; } else { memcpy(&out[iout],addme,addme_size); iout += addme_size; iin += parsedlen; } } else { out[iout++] = in[iin++];//nothing found, pass thru the '<' } } else { out[iout++] = in[iin++]; } } //append \0 to result, shrink buffer to match length of result: out = realloc(out,iout+1); out[iout] = '\0'; *outptr = out; config_debug("new (%d): %s",(int)strlen(out),out); return iin;//tell caller how far we got through their data }