int main() { char ini_name[80]="/root/test.ini"; char entry_name[80]=""; int exists_flag; int radars; int r; ini= iniparser_load(ini_name); printf("Testing INI reading\n"); // if (ini==NULL) { // fprintf(stderr, "cannot parse file: %s\n", ini_name); // return -1 ; // } exists_flag=iniparser_find_entry(ini,"site_settings:number_of_radars"); if(exists_flag) { radars=iniparser_getint(ini,"site_settings:number_of_radars",0); printf(" Number of radars:%d\n",radars); } else { printf("Entry does not exist: %s\n","site_settings:number_of_radars"); } for(r=1;r<=radars;r++) { sprintf(entry_name,"radar_%d",r); exists_flag=iniparser_find_entry(ini,entry_name); if(exists_flag) { printf(" radar %d section exists\n",r); sprintf(entry_name,"radar_%d:stid",r); printf(" Station ID: %d\n",iniparser_getint(ini,entry_name,-1)); } } // iniparser_dump(ini, stderr); iniparser_freedict(ini); return 0; }
/* check a **CHPRINT-file for completeness */ int check_chprint_file(int index) { char *chprint_cmdset_v01[6] = {"LOGIN", "CHROOM", "LOGOUT", "SENDMSG", "RECVERR", "KEEPALIVE"}; int i; char chprint_cmd[15]; /* check INFO-Section */ if(iniparser_find_entry(chprint_data[index].data, "INFO") == 0) { return 210; } else { if(iniparser_getstring(chprint_data[index].data, "INFO:Name", "NULL") == "NULL") { return 211; } if(iniparser_getstring(chprint_data[index].data, "INFO:LongName", "NULL") == "NULL") { return 211; } } /* check CMD-Section for **CHPRINT Version 0.1 commands */ if(iniparser_find_entry(chprint_data[index].data, "CMD") == 0) { return 212; } else { for(i = 0; i < 6; i++) { sprintf(chprint_cmd, "CMD:"); strcat(chprint_cmd, chprint_cmdset_v01[i]); if(iniparser_getstring(chprint_data[index].data, chprint_cmd, "NULL") == "NULL") { return 213; } /* check CMD-Section for Subsections */ if(strcmp(iniparser_getstring(chprint_data[index].data, chprint_cmd, "NULL"), "SUB") == 0) { sprintf(chprint_cmd, "CMD/"); strcat(chprint_cmd, chprint_cmdset_v01[i]); if(iniparser_find_entry(chprint_data[index].data, chprint_cmd) == 0) { return 214; } } } } chprint_data[index].version = 1; return 0; }
/*--------------------------------------------------------------------------*/ int iniparser_getsecnkeys(dictionary * d, char * s) { int secsize, nkeys ; char *keym; int j ; nkeys = 0; if (d==NULL) return nkeys; if (! iniparser_find_entry(d, s)) return nkeys; secsize = (int)strlen(s)+2; keym = malloc(secsize); snprintf(keym, secsize, "%s:", s); for (j=0 ; j<d->size ; j++) { if (d->key[j]==NULL) continue ; if (!strncmp(d->key[j], keym, secsize-1)) nkeys++; } free(keym); return nkeys; }
/*--------------------------------------------------------------------------*/ int iniparser_getsecnkeys(dictionary * d, char * s) { int secsize, nkeys ; char *keym = NULL; int j ; nkeys = 0; if (d==NULL) return nkeys; if (! iniparser_find_entry(d, s)) return nkeys; secsize = (int)strlen(s)+2; keym = malloc(secsize); if (!keym) { fprintf(stderr, "iniparser: memory alloc error\n"); goto out; } snprintf(keym, secsize, "%s:", s); for (j=0 ; j<d->size ; j++) { if (d->key[j]==NULL) continue ; if (!strncmp(d->key[j], keym, secsize-1)) nkeys++; } free(keym); out: return nkeys; }
/*--------------------------------------------------------------------------*/ int iniparser_getsecnkeys(const dictionary * d, const char * s) { int seclen, nkeys ; char keym[ASCIILINESZ+1]; int j ; nkeys = 0; if (d==NULL) return nkeys; if (! iniparser_find_entry(d, s)) return nkeys; seclen = (int)strlen(s); strlwc(s, keym, sizeof(keym)); keym[seclen] = ':'; for (j=0 ; j<d->size ; j++) { if (d->key[j]==NULL) continue ; if (!strncmp(d->key[j], keym, seclen+1)) nkeys++; } return nkeys; }
/*--------------------------------------------------------------------------*/ void iniparser_dumpsection_ini(dictionary * d, char * s, FILE * f) { int j ; char keym[ASCIILINESZ+1]; int seclen ; if (d==NULL || f==NULL) return ; if (! iniparser_find_entry(d, s)) return ; seclen = (int)strlen(s); fprintf(f, "\n[%s]\n", s); sprintf(keym, "%s:", s); for (j=0 ; j<d->size ; j++) { if (d->key[j]==NULL) continue ; if (!strncmp(d->key[j], keym, seclen+1)) { fprintf(f, "%-30s = %s\n", d->key[j]+seclen+1, d->val[j] ? d->val[j] : ""); } } fprintf(f, "\n"); return ; }
/*--------------------------------------------------------------------------*/ void iniparser_dumpsection_ini(dictionary * d, char * s, FILE * f) { int j ; char *keym; int secsize ; if (d==NULL || f==NULL) return ; if (! iniparser_find_entry(d, s)) return ; fprintf(f, "\n[%s]\n", s); secsize = (int)strlen(s) + 2; keym = malloc(secsize); snprintf(keym, secsize, "%s:", s); for (j=0 ; j<d->size ; j++) { if (d->key[j]==NULL) continue ; if (!strncmp(d->key[j], keym, secsize-1)) { fprintf(f, "%-30s = %s\n", d->key[j]+secsize-1, d->val[j] ? d->val[j] : ""); } } fprintf(f, "\n"); free(keym); return ; }
static int mod_init(void) { int ld_count = 0, i = 0; char* section_name; char* ldap_version; LM_INFO("LDAP_H350 module - initializing\n"); /* * read config file */ if (strlen(ldap_config.s) == 0) { LM_ERR("config_file is empty - this module param is mandatory\n"); return -2; } if ((config_vals = iniparser_new(ldap_config.s)) == NULL) { LM_ERR("failed to read config_file [%s]\n", ldap_config.s); return -2; } if ((ld_count = iniparser_getnsec(config_vals)) < 1) { LM_ERR("no section found in config_file [%s]\n", ldap_config.s); return -2; } /* check if mandatory settings are present */ for (i = 0; i < ld_count; i++) { section_name = iniparser_getsecname(config_vals, i); if (strlen(section_name) > 255) { LM_ERR( "config_file section name [%s]" " longer than allowed 255 characters", section_name); return -2; } if (!iniparser_find_entry(config_vals, get_ini_key_name(section_name, CFG_N_LDAP_HOST))) { LM_ERR( "mandatory %s not defined in [%s]\n", CFG_N_LDAP_HOST, section_name); return -2; } } /* * print ldap version string */ if (ldap_get_vendor_version(&ldap_version) != 0) { LM_ERR("ldap_get_vendor_version failed\n"); return -2; } LM_INFO("%s\n", ldap_version); return 0; }
void finiparser_find(int* out,char *key,int* ifnd,int key_len) { int tmp; *ifnd = 0; tmp = iniparser_find_entry(dic,addchar0(key,key_len)); if (tmp == 1) { *out = tmp; *ifnd = 1; } return; }
int wsmand_read_config(dictionary * ini) { if (!iniparser_find_entry(ini, "server")) { return 0; } server_port = iniparser_getint(ini, "server:port", 5985); server_ssl_port = iniparser_getint(ini, "server:ssl_port", 5986); debug_level = iniparser_getint(ini, "server:debug_level", 0); enumIdleTimeout = (unsigned long) iniparser_getint(ini, "server:enum_idle_timeout", 100); service_path = iniparser_getstring(ini, "server:service_path", "/wsman"); ssl_key_file = iniparser_getstr(ini, "server:ssl_key_file"); ssl_cert_file = iniparser_getstr(ini, "server:ssl_cert_file"); use_ipv4 = iniparser_getboolean(ini, "server:ipv4", 1); #ifdef ENABLE_IPV6 use_ipv6 = iniparser_getboolean(ini, "server:ipv6", 1); if (! (use_ipv4 || use_ipv6)) { fprintf(stderr, "Neither ipv4 nor ipv6 is enabled in openwsman.conf !\n"); exit(1); } #endif use_digest = iniparser_getboolean(ini, "server:use_digest", 0); digest_password_file = iniparser_getstr(ini, "server:digest_password_file"); basic_password_file = iniparser_getstr(ini, "server:basic_password_file"); custom_identify_file = iniparser_getstr(ini, "server:identify_file"); custom_anon_identify_file = iniparser_getstr(ini, "server:anon_identify_file"); basic_authenticator = iniparser_getstr(ini, "server:basic_authenticator"); basic_authenticator_arg = iniparser_getstr(ini, "server:basic_authenticator_arg"); log_location = iniparser_getstr(ini, "server:log_location"); min_threads = iniparser_getint(ini, "server:min_threads", 1); max_threads = iniparser_getint(ini, "server:max_threads", 0); uri_subscription_repository = iniparser_getstring(ini, "server:subs_repository", DEFAULT_SUBSCRIPTION_REPOSITORY); max_connections_per_thread = iniparser_getint(ini, "server:max_connextions_per_thread", 20); thread_stack_size = iniparser_getstring(ini, "server:thread_stack_size", "0"); #ifdef ENABLE_EVENTING_SUPPORT wsman_server_set_subscription_repos(uri_subscription_repository); #endif return 1; }
static void setIniString (IniDictionary *dictionary, const char *section, const char *entry, const char *value) { char *sectionName; asprintf (§ionName, "%s:%s", section, entry); if (!iniparser_find_entry (dictionary, (char*) section)) iniparser_add_entry (dictionary, (char*) section, NULL, NULL); iniparser_setstr (dictionary, sectionName, (char*) value); free (sectionName); }
void NodeConfig::getInitiatorIQN(string& iqn) { dictionary* d = iniparser_load(iscsiInitiatorNameFile.c_str()); iqn = ""; if (d == NULL) { LOG("Unable to load %s. The IQN returned will be empty.", iscsiInitiatorNameFile.c_str()); } if (iniparser_find_entry(d, ":InitiatorName") != 0) { iqn = getStringProperty(d, ":InitiatorName"); } LOG("Request for node ISCSI initiator iqn = '%s'", iqn.c_str()); }
void INI::set(std::string what, std::string value) { size_t pos = what.find(':'); if (pos != std::string::npos) { // User's adding a section - like "section:key". // Let's make sure it exists std::string section = what.substr(0, pos); if (iniparser_find_entry(this->ini, section.c_str()) == 0) { // Doesn't exist - creating... this->set(section, ""); } } iniparser_set(this->ini, what.c_str(), value.c_str()); }
int flib_ini_enter_section(flib_ini *ini, const char *section) { int result = INI_ERROR_OTHER; if(ini) { free(ini->currentSection); ini->currentSection = NULL; } if(!log_badargs_if2(ini==NULL, section==NULL)) { if(!iniparser_find_entry(ini->inidict, section)) { flib_log_d("Ini section %s not found", section); result = INI_ERROR_NOTFOUND; } else { ini->currentSection = flib_strdupnull(section); if(ini->currentSection) { // Usually iniparser ignores case, but some section-handling functions don't, // so we set it to lowercase manually strToLower(ini->currentSection); result = 0; } } } return result; }
dictionary *ParseInput(int argc, char** argv) { dictionary *programInput; char t1[3] = "-c"; // specify that this is a boolean type parameter char *d[1]; d[0] = t1; programInput = paraparser_load(argc, argv, 1, d); // Check if ini file is specified iniparser_copystring(programInput, "argument:1", DatabaseName, DatabaseName, MAX_FILENAME_LEN); if (DatabaseName[0] == '\0') { fprintf(stderr, "Syntax: %s <database> <ini> <pattern files> [-c Confirm] -l <log file>\n", argv[0]); exit(1); } iniparser_copystring(programInput, "argument:2", IniFileName, IniFileName, MAX_FILENAME_LEN); if (IniFileName[0] == '\0') { fprintf(stderr, "Syntax: %s <database> <ini> <pattern files> [-c Confirm] -l <log file>\n", argv[0]); exit(1); } iniparser_copystring(programInput, "argument:3", PatternFileName, PatternFileName, MAX_FILENAME_LEN); if (PatternFileName[0] == '\0') { fprintf(stderr, "Syntax: %s <database> <ini> <pattern files> [-c Confirm] -l <log file>\n", argv[0]); exit(1); } NumberOfSearchPatternFile = argc - 3; // Whether confirmation is needed Confirmation = iniparser_find_entry(programInput, "parameter:-c"); // Log file iniparser_copystring(programInput, "-l:3", LogFileName, LogFileName, MAX_FILENAME_LEN); return programInput; }
/************************************************************************************************************* ** @brief : Load all watch's in ini file ** #conf : ini file dictionary ** #watch : parser ini configure file data ** @return : how many item in conf file *************************************************************************************************************/ int conf_init(dictionary *conf, P_WATCH_INFO watch, const unsigned int size) { char entry[64]; char *cp = NULL; unsigned int i; #define GET_INT_ENTRY(v, d) do { v = iniparser_getint(conf, entry, (d)); } while(0) #define GET_BOOL_ENTRY(v, d) do { v = iniparser_getboolean(conf, entry, (d)); } while(0) #define GET_STR_ENTRY(s, d) do { cp = iniparser_getstring(conf, entry, (d)); bcopy(cp, s, strlen(cp)); } while (0) #define FORMAT_ENTRY(s, k) do { bzero(entry, sizeof(entry)); snprintf(entry, sizeof(entry), "%s:%s", (s), (k)); } while(0) /* Debug print all conf */ if (debug){ iniparser_dump(conf, stderr); } /* Get section name's */ for (i = 0; i < size; i++){ cp = iniparser_getsecname(conf, i); bcopy(cp, watch[i].name, strlen(cp)); } /* Get each of them data */ for (i = 0; i < size; i++){ /* Ignore ? */ FORMAT_ENTRY(watch[i].name, IGNORE_KEY); GET_BOOL_ENTRY(watch[i].ignore, 0); /* If ignore, do not read jump to next */ if (watch[i].ignore){ continue; } /* Path */ FORMAT_ENTRY(watch[i].name, PATH_KEY); GET_STR_ENTRY(watch[i].path, ""); /* Check if path is dir set is_dir mark */ watch[i].is_dir = conf_is_path_is_dir(watch[i].path); /* Comment */ FORMAT_ENTRY(watch[i].name, COMMENT_KEY); GET_STR_ENTRY(watch[i].comment, ""); /* Find if setting special file */ FORMAT_ENTRY(watch[i].name, SPECIAL_KEY); /* Only dir have this option */ if (watch[i].is_dir && iniparser_find_entry(conf, entry)){ /* Read form ini file */ if ((cp = iniparser_getstring(conf, entry, ""))){ /* Get special file name */ watch[i].spc_file_cnt = conf_get_words(cp, watch[i].spc_file, MAX_SPC_FILE); } } /* events */ FORMAT_ENTRY(watch[i].name, EVENTS_KEY); GET_STR_ENTRY(watch[i].events_str, ""); /* Parser events */ watch[i].events = conf_parser_events(watch[i].events_str, watch[i].is_dir); /* Debug */ if (debug){ conf_print_watch(&watch[i]); } } /* end for */ /* Arrange watching list */ return conf_arrange_watch(watch, (i == size) ? size : i); }
bool LANSensor::initAll(std::string configFileName) { if (getuid() != 0) { printError("Root priviledges needed"); return false; } bool manualSensorID = false; print("Reading config file... "); dictionary* ini; ini = iniparser_load(configFileName.c_str()); if (!ini) { print("Cannot parse config file. Using default values"); } else { m_arpscan_parameters = iniparser_getstring(ini, ":arp-scan_parameters", (char*)DEFAULT_ARPSCAN_PARAMETERS.c_str()); m_brokerAddress = iniparser_getstring(ini, ":broker_address", (char*)DEFAULT_BROKER_ADDRESS.c_str()); m_brokerPort = iniparser_getint(ini, ":broker_port", DEFAULT_BROKER_PORT); m_dataFetchUrl = iniparser_getstring(ini, ":data_fetch_url", (char*)DEFAULT_DATA_FETCH_URL.c_str()); m_scanInterval = iniparser_getint(ini, ":scan_interval", DEFAULT_SCAN_INTERVAL); m_connectAttemptInterval = iniparser_getint(ini, ":connect_attempt_interval", DEFAULT_CONNECT_ATTEMPT_INTERVAL); if (iniparser_find_entry(ini, ":sensor_id")) { m_sensorID = iniparser_getstring(ini, ":sensor_id", 0); manualSensorID = true; } } iniparser_freedict(ini); if (!manualSensorID) { // create sensor id from MAC address std::string id = getOwnMAC(); if (!id.size()) { id = "default"; } m_sensorID = "lan-sensor_" + id; } if (!m_ARPScanner) { print("Initializing ARP scanner... "); m_ARPScanner = new ARPScanner(); if (!m_ARPScanner->init(m_arpscan_parameters)) { printError(m_ARPScanner->getLastErrorString()); delete m_ARPScanner; m_ARPScanner = 0; return false; } } if (!m_dataGetter) { print("Initializing Curl... "); m_dataGetter = new DataGetter(); if (!m_dataGetter->init()) { printError(m_dataGetter->getLastErrorString()); return false; } } if (!m_mosquitto) { print("Initializing Mosquitto... "); m_mosquitto = new MosquittoHandler; std::string mosqID = m_sensorID; if (!m_mosquitto->init(mosqID.c_str()) || !m_mosquitto->connectToBroker(m_brokerAddress.c_str(), m_brokerPort)) { printError(m_mosquitto->getLastErrorString()); return false; } m_mosquitto->subscribe("command/fetch_device_database"); m_mosquitto->subscribe(std::string("command/scan/lan/" + m_sensorID).c_str()); m_mosquitto->subscribe("command/scan/lan"); print("Connecting to broker... "); if (!m_mosquitto->waitForConnect()) { printError(m_mosquitto->getLastErrorString()); while (!connectMosquitto(false)) { if (quit) return false; } } } m_updateDBNeeded = !updateDeviceData(); sendHello(); print("Sensor ID: " + m_sensorID); return true; }
bool BluetoothSensor::initAll(std::string configFileName) { bool manualSensorID = false; print("Reading config file..."); dictionary* ini ; ini = iniparser_load(configFileName.c_str()); if (!ini) { print("Cannot parse config file. Using default values"); } else { m_brokerAddress = iniparser_getstring(ini, ":broker_address", (char*)DEFAULT_BROKER_ADDRESS.c_str()); m_brokerPort = iniparser_getint(ini, ":broker_port", DEFAULT_BROKER_PORT); m_dataFetchUrl = iniparser_getstring(ini, ":data_fetch_url", (char*)DEFAULT_DATA_FETCH_URL.c_str()); m_connectAttemptInterval = iniparser_getint(ini, ":connect_attempt_interval", DEFAULT_CONNECT_ATTEMPT_INTERVAL); if (iniparser_find_entry(ini, ":sensor_id")) { m_sensorID = iniparser_getstring(ini, ":sensor_id", 0); manualSensorID = true; } } iniparser_freedict(ini); if (!m_bluetoothPoller) { print("Initializing Bluetooth... "); std::string btAddress; m_bluetoothPoller = new BluetoothPoller(); if (!m_bluetoothPoller->init(btAddress)) { printError(m_bluetoothPoller->getLastErrorString()); return false; } if (!manualSensorID) m_sensorID = "bt-sensor_" + btAddress; } if (!m_dataGetter) { print("Initializing Curl..."); m_dataGetter = new DataGetter(); if (!m_dataGetter->init()) { printError(m_dataGetter->getLastErrorString()); return false; } } if (!m_mosquitto) { print("Initializing Mosquitto... "); m_mosquitto = new MosquittoHandler; std::string mosqID = m_sensorID; if (!m_mosquitto->init(mosqID.c_str()) || !m_mosquitto->connectToBroker(m_brokerAddress.c_str(), m_brokerPort)) { printError(m_mosquitto->getLastErrorString()); return false; } m_mosquitto->subscribe("command/fetch_device_database"); m_mosquitto->subscribe(std::string("command/scan/bluetooth/" + m_sensorID).c_str()); m_mosquitto->subscribe("command/scan/bluetooth"); print("Connecting to broker... "); if (!m_mosquitto->waitForConnect()) { // first connection attempt failed, go into retry loop printError(m_mosquitto->getLastErrorString()); while (!connectMosquitto(false)) { if (quit) return false; } } } m_updateDBNeeded = !updateDeviceData(); sendHello(); print("Sensor ID: " + m_sensorID); return true; }
int main(int argc, char * argv[]) { char query[1024]; char Sym[16]; char Price[16]; char Ticker[32]; char mail_msg[1024]; char ini_file[1024]; char errbuf[CURL_ERROR_SIZE]; char market_open[12]=MARKET_OPEN; char market_close[12]=MARKET_CLOSE; char *saveptr; int x; float StopPrice=FLT_MAX; float cur_price=0.0; CURL *curl; CURLcode res=0; MYSQL_RES *result; MYSQL_ROW row; time_t t; struct tm *TM=0; dictionary *d; // parse cli parms if (argc < 3) Usage(argv[0]); // set up variables memset(query,0,sizeof(query)); memset(Ticker,0,sizeof(Ticker)); memset(Sym,0,sizeof(Sym)); for (x=0;x<strlen(argv[1]);x++) Sym[x]=toupper(argv[1][x]); StopPrice = strtof(argv[2],NULL); // initialize CURL if ((chunk.memory = calloc(1,1)) == NULL) { fprintf(stderr,"calloc failed, aborting process\n"); exit(EXIT_FAILURE); } curl_global_init(CURL_GLOBAL_NOTHING); curl=curl_easy_init(); if(!curl) { fprintf(stderr,"curl init failed, aborting process\n"); exit(EXIT_FAILURE); } curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ParseData); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk); curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10); curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 5); curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 0); #include "Includes/beancounter-conn.inc" valid_sym(Sym); // get exchange for Sym sprintf(query,"select trim(exchange) from stockinfo where symbol = \"%s\"",Sym); if (mysql_query(mysql,query)) print_error(mysql, "Failed to query database"); if ((result=mysql_store_result(mysql)) == NULL) print_error(mysql, "store_results failed"); row=mysql_fetch_row(result); sprintf(Ticker,"%s:%s",row[0],Sym); if (DEBUG) printf("%s\n",Ticker); // finished with the database mysql_free_result(result); #include "Includes/mysql-disconn.inc" sprintf(query,"http://www.google.com/finance?q=%s",Ticker); curl_easy_setopt(curl, CURLOPT_URL, query); // Loop while price is less than target if (DEBUG) printf("Monitoring price for %s\n",Sym); do { // check the time t = time(NULL); if ((TM = localtime(&t)) == NULL) { perror("localtime"); exit(EXIT_FAILURE); } if (TM->tm_hour>strtoul(market_close,&saveptr,10) || (TM->tm_hour==strtoul(market_close,&saveptr,10) && TM->tm_min>(strtoul(saveptr+1,NULL,10)+(SLEEP_INTERVAL/60)))) { // market is closed for the day printf("Stock market is closed for the day (after %s)\n",market_close); exit(EXIT_FAILURE); } if (TM->tm_hour<strtoul(market_open,&saveptr,10) || (TM->tm_hour==strtoul(market_open,&saveptr,10) && TM->tm_min<strtoul(saveptr+1,NULL,10))) { printf("Stock market not open yet, sleeping until %s\n",market_open); sleep((strtoul(market_open,&saveptr,10)-TM->tm_hour)*3600); while (atoi(saveptr+1)>TM->tm_min) { sleep(SLEEP_INTERVAL); t = time(NULL); if ((TM = localtime(&t)) == NULL) { perror("localtime"); exit(EXIT_FAILURE); } x=strtoul(market_open,&saveptr,10); } } // download current price chunk.size = 0; res=curl_easy_perform(curl); if (res) { // 0 means no errors if (res != 56 && res != 28) { printf("01 Error %d Unable to access the internet\n",res); printf("%s\n",errbuf); exit(EXIT_FAILURE); } else { // retry sleep(SLEEP_INTERVAL); continue; } } // was any data returned? if (strstr(chunk.memory,"404 Not Found")) { printf("%s not found\n",Sym); curl_easy_cleanup(curl); free(chunk.memory); exit(EXIT_FAILURE); } if (chunk.size == 0) { // printf("Error receiving data for %s, retrying\n",Sym); sleep(SLEEP_INTERVAL); continue; } // do the token parsing here if ((saveptr = strstr(chunk.memory,"<span class=\"pr\">")) == NULL) { // printf("Error parsing data for %s, retrying\n",Sym); sleep(SLEEP_INTERVAL); continue; } saveptr += strlen("<span class=\"pr\">")+1; saveptr = strchr(saveptr,'>')+1; // should now be pointing at the current price memset(Price,0,sizeof(Price)); strncpy(Price,saveptr,(strchr(saveptr,'<'))-saveptr); cur_price = strtof(Price,NULL); if (DEBUG) printf("cur_price: %.2f\n",cur_price); if (DEBUG) printf("Pausing for %d seconds\n",SLEEP_INTERVAL); if (cur_price <= StopPrice) break; sleep(SLEEP_INTERVAL); } while(cur_price >= StopPrice); // end While loop curl_easy_cleanup(curl); free(chunk.memory); // process the output message sprintf(ini_file, "%s/%s", getenv("HOME"), INIFILE); d = iniparser_load(ini_file); if (d==NULL) { puts("INI file not opened, aborting"); exit(EXIT_FAILURE); } sprintf(query,"Sell %s at %.2f stoploss price",Sym,cur_price); printf("%s\n",query); if (iniparser_find_entry(d, "Dest:email")) { sprintf(mail_msg,"echo %s |mailx -s \"%s\" %s",query,query, iniparser_getstring(d, "Dest:email", "null")); system(mail_msg); } if (iniparser_find_entry(d, "Dest:sms")) { sprintf(mail_msg,"echo %s |mailx -s \"%s\" %s",query,query, iniparser_getstring(d, "Dest:sms", "null")); system(mail_msg); } iniparser_freedict(d); exit(EXIT_SUCCESS); }
/** * Load all Sensors and read their * preferences from the ini file */ int LoadSensors(void) { //load the iniparser on the sensor path dictionary*ini=iniparser_load(sensorinipath()); if(!ini) return EXIT_FAILURE; // just to be sure //SetupSensors(); if(!iniparser_find_entry(ini, "sensor")) { Log(LOGT_SERVER, LOGL_ERROR, "File %s does not contain sensor config section", sensorinipath()); goto exit_failure; } if(!iniparser_find_entry(ini, "sensor:typecount")) { Log(LOGT_SERVER, LOGL_ERROR, "No sensor typecount present"); goto exit_failure; } { int typecount = iniparser_getint(ini, "sensor:typecount", 0); interval = iniparser_getint(ini, "sensor:interval", 1); // user requested to turn off sensors. if(typecount<1)goto exit_success; for(;typecount;typecount--) { const unsigned int idstringlen=12+numlen((unsigned)typecount); const unsigned int namelen=idstringlen+4; const unsigned int amountlen=idstringlen+5; const unsigned int typelen=idstringlen+4; char idstring[idstringlen]; char name[namelen]; char amount[amountlen]; char typeq[typelen]; sensortype stype=integersensor; snprintf(idstring, sizeof(char)*idstringlen, "sensor:type%d", typecount); snprintf(name, sizeof(char)*namelen, "%s%s", idstring, "name"); snprintf(amount, sizeof(char)*amountlen, "%s%s", idstring, "count"); if(!iniparser_find_entry(ini, name)||!iniparser_find_entry(ini, amount)) { Log(LOGT_SERVER, LOGL_WARNING, "Skipping incomplete %s definition", idstring); continue; } snprintf(typeq, sizeof(char)*typelen, "%s%s", idstring, "type"); { char const*const typea = iniparser_getstring(ini, typeq, "integer"); if(!strcmp(typea, "binary")) { stype=binarysensor; } else if(!strcmp(typea, "integer")){;/*fallthrough*/;} else { Log(LOGT_SERVER, LOGL_WARNING, "Unrecognised type %s, falling back on integer"); } if(stype==binarysensor) { const unsigned int alarmlen=idstringlen+5; char alarmq[alarmlen]; snprintf(alarmq, sizeof(char)*alarmlen, "%s%s", idstring, "alarm"); genbSensors( iniparser_getstring(ini, name, "genericb"), iniparser_getint(ini, amount, 0), iniparser_getstring(ini, alarmq, "Alarm!")); } else if(stype==integersensor) { const unsigned int startlen=idstringlen+5; const unsigned int alarmlen=idstringlen+6; const unsigned int boundlen=idstringlen+6; const unsigned int boundcrosslen=idstringlen+11; signed int min, max; char startq[startlen]; char lalarmq[alarmlen]; char ualarmq[alarmlen]; char lboundq[boundlen]; char uboundq[boundlen]; char lboundcrossq[boundcrosslen]; char uboundcrossq[boundcrosslen]; snprintf(startq, sizeof(char)*startlen, "%s%s", idstring, "start"); snprintf(lalarmq, sizeof(char)*alarmlen, "%s%s", idstring, "lalarm"); snprintf(ualarmq, sizeof(char)*alarmlen, "%s%s", idstring, "ualarm"); snprintf(lboundq, sizeof(char)*boundlen, "%s%s", idstring, "lbound"); snprintf(uboundq, sizeof(char)*boundlen, "%s%s", idstring, "ubound"); snprintf(lboundcrossq, sizeof(char)*boundcrosslen, "%s%s", idstring, "lboundcross"); snprintf(uboundcrossq, sizeof(char)*boundcrosslen, "%s%s", idstring, "uboundcross"); min = iniparser_getint(ini, lboundq, INT_MIN); max = iniparser_getint(ini, uboundq, INT_MAX); geniSensors( iniparser_getstring(ini, name, "generici"), iniparser_getint(ini, amount, 0), iniparser_getint(ini, startq, ((min+max)/2)), min, max, iniparser_getstring(ini, lalarmq, "lower bound Alarm!"), iniparser_getstring(ini, ualarmq, "upper bound Alarm!"), iniparser_getboolean(ini, lboundcrossq, true), iniparser_getboolean(ini, uboundcrossq, true)); } else { Log(LOGT_SERVER, LOGL_ERROR, "Unknown sensor type %d from %s", stype, typea); } } } } exit_success: iniparser_freedict(ini); return EXIT_SUCCESS; exit_failure: iniparser_freedict(ini); return EXIT_FAILURE; }
void *control_handler(struct ControlProgram *control_program) { int tid,i,r=-1,c=-1,status,rc,tmp; struct pollfd pfd; char command; int sockflag,poll_err_count,retval,socket,oldv,socket_err,length=sizeof(int); int32_t current_freq,radar=0,channel=0; struct timeval tv,current_time,last_report,t_get_data_start,t_get_data_end; struct ROSMsg msg; struct DriverMsg dmsg; struct DataPRM control_data; struct ControlPRM control_parameters; struct SiteSettings settings; struct TSGbuf *pulseseq; struct SeqPRM tprm; int data_int; pthread_t thread,threads[10]; struct timeval t0,t1,t2,t3,t4; unsigned long elapsed; unsigned long ultemp; int32_t data_length; char entry_type,entry_name[80]; int return_type,entry_exists; char *temp_strp; int32_t temp_int32; double temp_double; /* * Init the Control Program state */ r=control_program->radarinfo->radar-1; c=control_program->radarinfo->channel-1; pthread_mutex_lock(&exit_lock); pthread_mutex_lock(&controlprogram_list_lock); setbuf(stdout, 0); setbuf(stderr, 0); tid = pthread_self(); /* set the cancellation parameters -- - Enable thread cancellation - Defer the action of the cancellation */ pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL); pthread_cleanup_push((void *)&controlprogram_exit,(void *)control_program); if(control_program!=NULL) { socket=control_program->state->socket; } pthread_mutex_unlock(&controlprogram_list_lock); pthread_mutex_unlock(&exit_lock); poll_err_count=0; gettimeofday(&last_report,NULL); while (1) { if(control_program==NULL) { fprintf(stderr,"Client Error: cp is null: %p\n",control_program); fflush(stderr); break; } retval=getsockopt(socket, SOL_SOCKET, SO_ERROR, &socket_err, &length); if ((retval!=0) || (socket_err!=0)) { fprintf(stderr,"Client Error: socket error: %d : %d %d : %p\n",socket,retval,socket_err,control_program); fflush(stderr); break; } /* poll the socket and check for waiting messages */ pfd.fd = socket; pfd.events = POLLIN | POLLHUP | POLLRDNORM ; pfd.revents = 0; sockflag=0; retval=poll(&pfd, 1, 1000); if (retval > 0) { if(pfd.revents & POLLHUP) { fprintf(stderr,"Client: poll socket: %d cp: %p retval: %d poll_revents: %d :: %d %d %d\n",socket,control_program,retval, pfd.revents,POLLHUP,POLLIN,POLLRDNORM); break; } else { char buffer[32]; if (recv(socket, buffer, sizeof(buffer), MSG_PEEK | MSG_DONTWAIT) > 0) { sockflag=1; poll_err_count=0; } else { fprintf(stdout,"Client: poll socket: %d cp: %p poll_revents: %d :: No recv que'd\n",socket,control_program,pfd.revents); fflush(stdout); poll_err_count++; } } } else { if (retval < 0 ) { perror("poll()"); break; } if (retval == 0 ){ fprintf(stdout,"Client: poll socket: %d cp: %p retval: %d :: timeout\n",socket,control_program,retval); fflush(stdout); poll_err_count++; } } if (poll_err_count > 3 ) { break; } /* sockflag: socket looks good, time to read from it */ if(sockflag) { r=control_program->radarinfo->radar-1; c=control_program->radarinfo->channel-1; pthread_mutex_lock(&controlprogram_list_lock); r=control_program->radarinfo->radar-1; c=control_program->radarinfo->channel-1; if ((r<0) || (c<0)) control_program->data->status=-1; /* Read controlprogram msg */ recv_data(socket, &msg, sizeof(struct ROSMsg)); gettimeofday(¤t_time,NULL); if((current_time.tv_sec-last_report.tv_sec)>5) { system("date -t > /tmp/server_cmd_time"); last_report=current_time; } control_program->state->thread->last_seen=current_time; pthread_mutex_unlock(&controlprogram_list_lock); /* Process controlprogram msg */ switch(msg.type) { case PING: //printf("PING: START\n"); gettimeofday(&t0,NULL); msg.status=1; send_data(socket, &msg, sizeof(struct ROSMsg)); //printf("PING: END\n"); break; case SET_INACTIVE: //printf("SET_RADAR_INACTIVE\n"); gettimeofday(&t0,NULL); if (verbose > 1 ) fprintf(stderr,"Client: Set INACTIVE: %ld %ld :: %d %d\n",(long)t0.tv_sec,(long)t0.tv_usec,r,c); if ( (r < 0) || (c < 0)) { msg.status=-1; send_data(socket, &msg, sizeof(struct ROSMsg)); } else { pthread_mutex_lock(&controlprogram_list_lock); if(control_program->active!=0) { control_program->active=-1; control_program->state->ready=0; pthread_mutex_lock(&coord_lock); rc = pthread_create(&thread, NULL, (void *)&coordination_handler,(void *) control_program); pthread_join(thread,NULL); pthread_mutex_unlock(&coord_lock); } pthread_mutex_unlock(&controlprogram_list_lock); msg.status=1; send_data(socket, &msg, sizeof(struct ROSMsg)); } gettimeofday(&t1,NULL); if (verbose > 1 ) fprintf(stderr,"Client: End INACTIVE: %ld %ld :: %d %d\n",(long)t1.tv_sec,(long)t1.tv_usec,r,c); //printf("end SET_RADAR_INACTIVE\n"); break; case SET_ACTIVE: gettimeofday(&t0,NULL); if (verbose > 1 ) fprintf(stderr,"Client: Set ACTIVE: %ld %ld :: %d %d\n",(long)t0.tv_sec,(long)t0.tv_usec,r,c); //printf("SET_RADAR_ACTIVE\n"); gettimeofday(&t0,NULL); if ( (r < 0) || (c < 0)) { msg.status=-1; send_data(socket, &msg, sizeof(struct ROSMsg)); } else { pthread_mutex_lock(&controlprogram_list_lock); if(control_program->active!=0) { control_program->active=1; control_program->state->ready=0; /* JDS : Do not need to run coordinator when setting active pthread_mutex_lock(&coord_lock); rc = pthread_create(&thread, NULL, (void *)&coordination_handler,(void *) control_program); pthread_join(thread,NULL); pthread_mutex_unlock(&coord_lock); */ } pthread_mutex_unlock(&controlprogram_list_lock); msg.status=1; send_data(socket, &msg, sizeof(struct ROSMsg)); } gettimeofday(&t1,NULL); if (verbose > 1 ) fprintf(stderr,"Client: End ACTIVE: %ld %ld :: %d %d\n",(long)t1.tv_sec,(long)t1.tv_usec,r,c); //printf("end SET_RADAR_ACTIVE\n"); break; /* case UPDATE_SITE_SETTINGS: gettimeofday(&t0,NULL); SettingsInit(&settings); recv_data(socket, &settings, sizeof(struct SiteSettings)); msg.status=-1; send_data(socket, &msg, sizeof(struct ROSMsg)); SettingsCpy(&settings,&site_settings); rc = pthread_create(&thread, NULL, (void *)&settings_rxfe_update_rf,(void *)&site_settings.rf_settings); pthread_join(thread,NULL); rc = pthread_create(&thread, NULL, (void *)&settings_rxfe_update_if,(void *)&site_settings.if_settings); pthread_join(thread,NULL); break; */ case QUERY_INI_SETTING: //fprintf(stdout,"start QUERY_INI_SETTING\n"); recv_data(socket, &data_length, sizeof(int32_t)); recv_data(socket, &entry_name, data_length*sizeof(char)); recv_data(socket, &entry_type, sizeof(char)); entry_exists=iniparser_find_entry(Site_INI,entry_name); msg.status=entry_exists; switch(entry_type) { case 'i': //fprintf(stdout," entry type: i\n"); return_type='i'; temp_int32=iniparser_getint(Site_INI,entry_name,-1); send_data(socket, &return_type, sizeof(char)); data_length=1; send_data(socket, &data_length, sizeof(int32_t)); send_data(socket, &temp_int32, data_length*sizeof(int32_t)); break; case 'b': //fprintf(stdout," entry type: b\n"); return_type='b'; temp_int32=iniparser_getboolean(Site_INI,entry_name,-1); send_data(socket, &return_type, sizeof(char)); data_length=1; send_data(socket, &data_length, sizeof(int32_t)); send_data(socket, &temp_int32, data_length*sizeof(int32_t)); break; case 's': //fprintf(stdout," entry type: s\n"); return_type='s'; temp_strp=iniparser_getstring(Site_INI,entry_name,NULL); send_data(socket, &return_type, sizeof(char)); data_length=strlen(temp_strp); send_data(socket, &data_length, sizeof(int32_t)); send_data(socket, temp_strp, data_length*sizeof(char)); default: return_type=' '; send_data(socket, &return_type, sizeof(char)); data_length=0; send_data(socket, &data_length, sizeof(int32_t)); send_data(socket, temp_strp, data_length*sizeof(char)); } send_data(socket, &msg, sizeof(struct ROSMsg)); //fprintf(stdout,"end QUERY_INI_SETTING\n"); break; case GET_SITE_SETTINGS: gettimeofday(&t0,NULL); settings=site_settings; send_data(socket, &settings, sizeof(struct SiteSettings)); msg.status=-1; send_data(socket, &msg, sizeof(struct ROSMsg)); break; case SET_SITE_IFMODE: gettimeofday(&t0,NULL); settings=site_settings; recv_data(socket, &settings.ifmode, sizeof(settings.ifmode)); msg.status=-1; send_data(socket, &msg, sizeof(struct ROSMsg)); break; case SET_RADAR_CHAN: gettimeofday(&t0,NULL); fprintf(stdout,"SET_RADAR_CHAN: %d.%d\n",(int)t0.tv_sec,(int)t0.tv_usec); fflush(stdout); msg.status=1; recv_data(socket, &radar, sizeof(int32_t)); //requested radar recv_data(socket, &channel, sizeof(int32_t)); //requested channel fprintf(stdout," Radar: %d Chan: %d\n",radar,channel); fflush(stdout); pthread_mutex_lock(&controlprogram_list_lock); status=register_radar_channel(control_program,radar,channel); if (status) { } else { if (verbose>-1) fprintf(stderr,"Control Program thread %p Bad status %d no radar channel registered\n", tid,status); } msg.status=status; pthread_mutex_unlock(&controlprogram_list_lock); send_data(socket, &msg, sizeof(struct ROSMsg)); //printf(" END SET_RADAR_CHAN\n"); break; case LINK_RADAR_CHAN: gettimeofday(&t0,NULL); msg.status=1; recv_data(socket, &r, sizeof(r)); //requested radar recv_data(socket, &c, sizeof(c)); //requested channel pthread_mutex_lock(&controlprogram_list_lock); control_program->state->linked_program=find_registered_controlprogram_by_radar_channel(r,c); control_program->state->linked=1; if (control_program->state->linked_program!=NULL) { status=1; } else { status=0; } msg.status=status; pthread_mutex_unlock(&controlprogram_list_lock); send_data(socket, &msg, sizeof(struct ROSMsg)); break; case GET_PARAMETERS: //fprintf(stdout,"GET_PARAMETERS: START\n"); //fflush(stdout); gettimeofday(&t0,NULL); if ( (r < 0) || (c < 0)) { send_data(socket, &control_parameters, sizeof(struct ControlPRM)); msg.status=-1; send_data(socket, &msg, sizeof(struct ROSMsg)); } else { pthread_mutex_lock(&controlprogram_list_lock); msg.status=status; control_parameters=controlprogram_fill_parameters(control_program); pthread_mutex_unlock(&controlprogram_list_lock); send_data(socket, &control_parameters, sizeof(struct ControlPRM)); send_data(socket, &msg, sizeof(struct ROSMsg)); } //fprintf(stdout,"GET_PARAMETERS: END\n"); //fflush(stdout); break; case GET_DATA: //printf("GET_DATA: START\n"); //printf("GET_DATA: Event :: sec: %d nsec: %d\n",control_program->data->event_secs,control_program->data->event_nsecs); //printf("GET_DATA: bad_transmit_times:: length %d \n",bad_transmit_times.length); //for(i=0;i<bad_transmit_times.length;i++) { // printf("GET_DATA: bad_transmit_times:: %d : %d %d\n",i,bad_transmit_times.start_usec[i],bad_transmit_times.duration_usec[i]); //} gettimeofday(&t0,NULL); gettimeofday(&t_get_data_start,NULL); if (control_program->active != 1) { control_program->data->status=-1; send_data(socket, control_program->data, sizeof(struct DataPRM)); msg.status=-1; send_data(socket, &msg, sizeof(struct ROSMsg)); } else { if ( (r < 0) || (c < 0)) { control_program->data->status=-1; send_data(socket, control_program->data, sizeof(struct DataPRM)); msg.status=-1; send_data(socket, &msg, sizeof(struct ROSMsg)); } else { msg.status=status; rc = pthread_create(&thread, NULL,(void *)&receiver_controlprogram_get_data,(void *) control_program); pthread_join(thread,NULL); send_data(socket, control_program->data, sizeof(struct DataPRM)); if(control_program->data->status==0) { //printf("GET_DATA: main: %d %d\n",sizeof(uint32_t),sizeof(uint32)*control_program->data->samples); send_data(socket, control_program->main, sizeof(uint32_t)*control_program->data->samples); send_data(socket, control_program->back, sizeof(uint32_t)*control_program->data->samples); send_data(socket, &bad_transmit_times.length, sizeof(bad_transmit_times.length)); send_data(socket, bad_transmit_times.start_usec, sizeof(uint32_t)*bad_transmit_times.length); send_data(socket, bad_transmit_times.duration_usec, sizeof(uint32_t)*bad_transmit_times.length); tmp=MAX_TRANSMITTERS; send_data(socket,&tmp,sizeof(int)); send_data(socket,&txstatus[r].AGC,sizeof(int)*tmp); send_data(socket,&txstatus[r].LOWPWR,sizeof(int)*tmp); } else { printf("GET_DATA: Bad status %d\n",control_program->data->status); } send_data(socket, &msg, sizeof(struct ROSMsg)); } } gettimeofday(&t1,NULL); if (verbose > 1) { elapsed=(t1.tv_sec-t0.tv_sec)*1E6; elapsed+=(t1.tv_usec-t0.tv_usec); if (verbose > 1 ) printf("Client: Get Data Elapsed Microseconds: %ld\n",elapsed); } //printf("GET_DATA: END\n"); gettimeofday(&t_get_data_end,NULL); if (verbose > 1) { elapsed=(t_get_data_end.tv_sec-t_pre_start.tv_sec)*1E6; elapsed+=(t_get_data_end.tv_usec-t_pre_start.tv_usec); fprintf(stderr,"Client %2d %2d: From Pretrig start to Get Data end Elapsed Microseconds: %10ld :: sec: %10d usec: %10d\n",r,c,elapsed,t_get_data_end.tv_sec,t_get_data_end.tv_usec); elapsed=(t_get_data_end.tv_sec-t_ready_first.tv_sec)*1E6; elapsed+=(t_get_data_end.tv_usec-t_ready_first.tv_usec); fprintf(stderr,"Client %2d %2d: From Client Ready start to Get Data end Elapsed Microseconds: %10ld :: sec: %10d usec: %10d\n",r,c,elapsed,t_get_data_end.tv_sec,t_get_data_end.tv_usec); fflush(stderr); } break; case SET_PARAMETERS: //printf("SET_PARAMETERS: START\n"); gettimeofday(&t0,NULL); if ( (r < 0) || (c < 0)) { recv_data(socket, control_program->parameters, sizeof(struct ControlPRM)); msg.status=-1; send_data(socket, &msg, sizeof(struct ROSMsg)); } else { msg.status=1; pthread_mutex_lock(&controlprogram_list_lock); recv_data(socket, control_program->parameters, sizeof(struct ControlPRM)); if(control_program->parameters->rfreq<0) control_program->parameters->rfreq=control_program->parameters->tfreq; send_data(socket, &msg, sizeof(struct ROSMsg)); pthread_mutex_unlock(&controlprogram_list_lock); } //printf("SET_PARAMETERS: END\n"); break; case REGISTER_SEQ: gettimeofday(&t0,NULL); fprintf(stdout,"REGISTER_SEQ: r: %d c: %d %d.%d\n",r,c,(int)t0.tv_sec,(int)t0.tv_usec); fflush(stdout); msg.status=1; recv_data(socket,&tprm, sizeof(struct SeqPRM)); // requested pulseseq pthread_mutex_lock(&controlprogram_list_lock); control_program->state->pulseseqs[tprm.index]=malloc(sizeof(struct TSGbuf)); control_program->parameters->current_pulseseq_index=tprm.index; control_program->state->pulseseqs[tprm.index]->len=tprm.len; control_program->state->pulseseqs[tprm.index]->step=tprm.step; control_program->state->pulseseqs[tprm.index]->index=tprm.index; control_program->state->pulseseqs[tprm.index]->rep= malloc(sizeof(unsigned char)*control_program->state->pulseseqs[tprm.index]->len); control_program->state->pulseseqs[tprm.index]->code= malloc(sizeof(unsigned char)*control_program->state->pulseseqs[tprm.index]->len); recv_data(socket,control_program->state->pulseseqs[tprm.index]->rep, sizeof(unsigned char)*control_program->state->pulseseqs[tprm.index]->len); // requested pulseseq recv_data(socket,control_program->state->pulseseqs[tprm.index]->code, sizeof(unsigned char)*control_program->state->pulseseqs[tprm.index]->len); // requested pulseseq if ( (r < 0) || (c < 0)) { msg.status=-1; } else { //send on to timing socket rc = pthread_create(&threads[0], NULL, (void *)&timing_register_seq,(void *) control_program); //send on to dds socket rc = pthread_create(&threads[1], NULL, (void *)&dds_register_seq,(void *) control_program); //printf("Waiting on Timing Thread\n"); pthread_join(threads[0],NULL); //printf("Waiting on DDS\n"); pthread_join(threads[1],NULL); } pthread_mutex_unlock(&controlprogram_list_lock); //printf("REGISTER_SEQ: SEND ROSMsg\n"); send_data(socket, &msg, sizeof(struct ROSMsg)); gettimeofday(&t1,NULL); if (verbose > 1) { elapsed=(t1.tv_sec-t0.tv_sec)*1E6; elapsed+=(t1.tv_usec-t0.tv_usec); if (verbose > 1 ) printf("Client: Reg Seq Elapsed Microseconds: %ld\n",elapsed); } //printf("REGISTER_SEQ: END\n"); break; case SET_READY_FLAG: gettimeofday(&t0,NULL); if (verbose > 1 ) fprintf(stderr,"Client: Set READY: %ld %ld :: %d %d\n",(long)t0.tv_sec,(long)t0.tv_usec,r,c); if ( (r < 0) || (c < 0)) { msg.status=-1; } else { msg.status=0; pthread_mutex_lock(&controlprogram_list_lock); if (control_program->active!=0) { control_program->active=1; control_program->state->ready=1; } pthread_mutex_unlock(&controlprogram_list_lock); i=0; rc = pthread_create(&threads[i], NULL,(void *) &timing_wait, NULL); pthread_join(threads[0],NULL); pthread_mutex_lock(&controlprogram_list_lock); i=0; rc = pthread_create(&threads[i], NULL, (void *) &DIO_ready_controlprogram, control_program); pthread_join(threads[i],NULL); i++; rc = pthread_create(&threads[i], NULL, (void *) &timing_ready_controlprogram, control_program); pthread_join(threads[i],NULL); i++; rc = pthread_create(&threads[i], NULL, (void *) &dds_ready_controlprogram, control_program); pthread_join(threads[i],NULL); i++; rc = pthread_create(&threads[i], NULL, (void *) &receiver_ready_controlprogram, control_program); pthread_join(threads[i],NULL); for (;i>=0;i--) { gettimeofday(&t2,NULL); pthread_join(threads[i],NULL); gettimeofday(&t3,NULL); if (verbose > 1) { elapsed=(t3.tv_sec-t2.tv_sec)*1E6; elapsed+=(t3.tv_usec-t2.tv_usec); if (verbose > 1 ) printf("Client:Set Ready: %d Elapsed Microseconds: %ld\n",i,elapsed); } } gettimeofday(&t2,NULL); pthread_mutex_lock(&coord_lock); rc = pthread_create(&thread, NULL, (void *)&coordination_handler,(void *) control_program); pthread_join(thread,NULL); pthread_mutex_unlock(&coord_lock); gettimeofday(&t3,NULL); if (verbose > 1) { elapsed=(t3.tv_sec-t2.tv_sec)*1E6; elapsed+=(t3.tv_usec-t2.tv_usec); if (verbose > 1 ) printf("Client:Set Ready: Coord Elapsed Microseconds: %ld\n",elapsed); } pthread_mutex_unlock(&controlprogram_list_lock); } send_data(socket, &msg, sizeof(struct ROSMsg)); gettimeofday(&t1,NULL); if (verbose > 1) { elapsed=(t1.tv_sec-t0.tv_sec)*1E6; elapsed+=(t1.tv_usec-t0.tv_usec); if (verbose > 1 ) printf("Client: Set Ready Elapsed Microseconds: %ld\n",elapsed); } if (verbose > 1 ) fprintf(stderr,"Client: End READY: %ld %ld :: %d %d\n",(long)t1.tv_sec,(long)t1.tv_usec,r,c); break; case REQUEST_CLEAR_FREQ_SEARCH: gettimeofday(&t0,NULL); pthread_mutex_lock(&controlprogram_list_lock); recv_data(socket,&control_program->clrfreqsearch, sizeof(struct CLRFreqPRM)); // requested search parameters if (verbose > 1 )printf("Client: Requst CLRSearch: %d %d\n",control_program->clrfreqsearch.start,control_program->clrfreqsearch.end); if ( (r < 0) || (c < 0)) { msg.status=-1; } else { rc = pthread_create(&threads[0], NULL, (void *) &DIO_clrfreq,control_program); pthread_join(threads[0],NULL); rc = pthread_create(&threads[0], NULL, (void *) &receiver_clrfreq,control_program); pthread_join(threads[0],NULL); rc = pthread_create(&threads[0], NULL, (void *) &DIO_rxfe_reset,NULL); pthread_join(threads[0],NULL); msg.status=control_program->state->freq_change_needed; } send_data(socket, &msg, sizeof(struct ROSMsg)); pthread_mutex_unlock(&controlprogram_list_lock); gettimeofday(&t1,NULL); if (verbose > 1) { elapsed=(t1.tv_sec-t0.tv_sec)*1E6; elapsed+=(t1.tv_usec-t0.tv_usec); if (verbose > 1 ) printf("Client: CLR Elapsed Microseconds: %ld\n",elapsed); } break; case REQUEST_ASSIGNED_FREQ: gettimeofday(&t0,NULL); pthread_mutex_lock(&controlprogram_list_lock); if ( (r < 0) || (c < 0)) { msg.status=-1; control_program->state->current_assigned_freq=0; control_program->state->current_assigned_noise=0; } else { rc = pthread_create(&threads[0], NULL, (void *) &receiver_assign_frequency,(void *) control_program); pthread_join(threads[0],NULL); msg.status=control_program->state->best_assigned_freq!=control_program->state->current_assigned_freq; } //control_program->state->current_assigned_noise=1; current_freq=control_program->state->current_assigned_freq; send_data(socket, ¤t_freq, sizeof(int32_t)); send_data(socket, &control_program->state->current_assigned_noise, sizeof(float)); send_data(socket, &msg, sizeof(struct ROSMsg)); pthread_mutex_unlock(&controlprogram_list_lock); gettimeofday(&t1,NULL); if (verbose > 1) { elapsed=(t1.tv_sec-t0.tv_sec)*1E6; elapsed+=(t1.tv_usec-t0.tv_usec); if (verbose > 1 ) printf("Client: Request Freq Elapsed Microseconds: %ld\n",elapsed); } break; case QUIT: gettimeofday(&t0,NULL); fprintf(stdout,"Client QUIT:: %d.%06d \n",(int)t0.tv_sec,(int)t0.tv_usec); fflush(stdout); msg.status=0; send_data(socket, &msg, sizeof(struct ROSMsg)); //controlprogram_exit(control_program); pthread_exit(NULL); break; default: msg.status=1; send_data(socket, &msg, sizeof(struct ROSMsg)); } /* FD_ISSET(0, &rfds) will be true. */ } //else printf("No data within five seconds.\n"); // if (verbose>1) printf("Client: test cancel\n"); pthread_testcancel(); } fprintf(stdout,"Outside of socket while loop : %p\n",control_program); fflush(stdout); pthread_testcancel(); pthread_cleanup_pop(0); controlprogram_exit(control_program); pthread_exit(NULL); };
/* ... */ void CBaIniParse::TwistedIni() { IBaIniParser *pNewHdl = 0; dictionary *pOrgHdl = 0; std::string tmp; CPPUNIT_ASSERT(!IBaIniParserCreate("You/Shall/Not/Path")); CPPUNIT_ASSERT(!iniparser_load("You/Shall/Not/Path")); CPPUNIT_ASSERT(!IBaIniParserCreate(ERRORINI)); CPPUNIT_ASSERT(!iniparser_load(ERRORINI)); CPPUNIT_ASSERT(!iniparser_load(OFKEYINI)); CPPUNIT_ASSERT(!IBaIniParserCreate(OFKEYINI)); CPPUNIT_ASSERT(!IBaIniParserCreate(OFVALINI)); CPPUNIT_ASSERT(!iniparser_load(OFVALINI)); pNewHdl = IBaIniParserCreate(TWISTEDINI); pOrgHdl = iniparser_load(TWISTEDINI); CPPUNIT_ASSERT(pNewHdl); CPPUNIT_ASSERT(pOrgHdl); pNewHdl->Dump(stdout); std::cout << "a==============================================================\n\n"; iniparser_dump(pOrgHdl, stdout); std::cout << "a==============================================================\n\n"; pNewHdl->DumpIni(stdout); std::cout << "a==============================================================\n\n"; iniparser_dump_ini(pOrgHdl, stdout); CPPUNIT_ASSERT_EQUAL( (bool) iniparser_find_entry(pOrgHdl, "open["), pNewHdl->Exists("open[")); // Multi-lines tmp = pNewHdl->GetString("multi:multi line key", ""); CPPUNIT_ASSERT_MESSAGE(tmp, tmp == "multi line value"); tmp = pNewHdl->GetString("multi:visible", ""); CPPUNIT_ASSERT_MESSAGE(tmp, tmp == "1"); tmp = pNewHdl->GetString("multi:a", ""); CPPUNIT_ASSERT_MESSAGE(tmp, tmp == "beginend"); tmp = pNewHdl->GetString("multi:c", ""); CPPUNIT_ASSERT_MESSAGE(tmp, tmp == "begin end"); CPPUNIT_ASSERT(IBaIniParserDestroy(pNewHdl)); iniparser_freedict(pOrgHdl); pNewHdl = 0; pOrgHdl = 0; pNewHdl = IBaIniParserCreate(0); pOrgHdl = dictionary_new(10); CPPUNIT_ASSERT(pNewHdl); CPPUNIT_ASSERT(pOrgHdl); CPPUNIT_ASSERT_EQUAL(pNewHdl->Set(0, 0), !iniparser_set(pOrgHdl, 0, 0)); CPPUNIT_ASSERT_EQUAL(pNewHdl->Set("section", 0), !iniparser_set(pOrgHdl, "section", 0)); CPPUNIT_ASSERT_EQUAL(pNewHdl->Set("section:key", "value"), !iniparser_set(pOrgHdl, "section:key", "value")); tmp = pNewHdl->GetString("section:key", 0); CPPUNIT_ASSERT_MESSAGE(tmp, tmp == iniparser_getstring(pOrgHdl, "section:key", 0)); /* reset the key's value*/ CPPUNIT_ASSERT_EQUAL(pNewHdl->Set("section:key", 0), !iniparser_set(pOrgHdl, "section:key", 0)); tmp = iniparser_getstring(pOrgHdl, "section:key", "dummy") ? iniparser_getstring(pOrgHdl, "section:key", "dummy") : ""; CPPUNIT_ASSERT_MESSAGE(tmp, tmp == pNewHdl->GetString("section:key", "dummy")); CPPUNIT_ASSERT_EQUAL(pNewHdl->Set("section:key", "value"), !iniparser_set(pOrgHdl, "section:key", "value")); tmp = pNewHdl->GetString("section:key", 0); CPPUNIT_ASSERT_MESSAGE(tmp, tmp == iniparser_getstring(pOrgHdl, "section:key", 0)); iniparser_unset(pOrgHdl, "section:key"); pNewHdl->Reset("section:key"); tmp = pNewHdl->GetString("section:key", "dummy"); CPPUNIT_ASSERT_MESSAGE(tmp, tmp == iniparser_getstring(pOrgHdl, "section:key", "dummy")); CPPUNIT_ASSERT_EQUAL(pNewHdl->Set("section:key", 0), !iniparser_set(pOrgHdl, "section:key", 0)); CPPUNIT_ASSERT_EQUAL(pNewHdl->Set("section:key1", 0), !iniparser_set(pOrgHdl, "section:key1", 0)); CPPUNIT_ASSERT_EQUAL(pNewHdl->Set("section:key2", 0), !iniparser_set(pOrgHdl, "section:key2", 0)); }
int ini_find (char *key) { return iniparser_find_entry (yap_config, key); }
// Return 1 if the entry exists, 0 otherwise int InitParser::find_entry(const string& entry) const { return iniparser_find_entry(dico, entry.c_str()); }
int gen_fru_data(dictionary *ini, char **raw_data) { int total_length, offset, len_mul8, iua_len, size, cksum; char *iua, *cia, *bia, *pia, *data; iua = cia = bia = pia = data = NULL; total_length = offset = len_mul8 = iua_len = size = cksum = 0; /* A common header always exists even if there's no FRU data */ struct fru_common_header *fch = (struct fru_common_header *) calloc(sizeof(struct fru_common_header), 1); fch->format_version = 0x01; total_length += sizeof(struct fru_common_header); offset = total_length / 8; /* Parse "Internal Use Area" (IUA) section */ if (iniparser_find_entry(ini, IUA)) { iua_len = gen_iua(ini, &iua); fch->internal_use_offset = offset; offset += (iua_len/8); total_length += iua_len; } /* Parse "Chassis Info Area" (CIA) section */ if (iniparser_find_entry(ini, CIA)) { len_mul8 = gen_cia(ini, &cia); fch->chassis_info_offset = offset; offset += len_mul8; total_length += len_mul8 * 8; } /* Parse "Board Info Area" (BIA) section */ if (iniparser_find_entry(ini, BIA)) { len_mul8 = gen_bia(ini, &bia); fch->board_info_offset = offset; offset += len_mul8; total_length += len_mul8 * 8; } /* Parse "Product Info Area" (PIA) section */ if (iniparser_find_entry(ini, PIA)) { len_mul8 = gen_pia(ini, &pia); fch->product_info_offset = offset; offset += len_mul8; total_length += len_mul8 * 8; } /* calculate header checksum */ cksum = get_zero_cksum((uint8_t *) fch, sizeof(*fch)-1); fch->checksum = cksum; data = (char *) malloc(total_length); /* Copy common header first */ memcpy(data, fch, sizeof(struct fru_common_header)); /* Copy each section's data if any */ if (iua) { offset = fch->internal_use_offset * 8; size = *(iua + 1) * 8; memcpy(data + offset, iua, size); } if (cia) { offset = fch->chassis_info_offset * 8; size = *(cia + 1) * 8; memcpy(data + offset, cia, size); } if (bia) { offset = fch->board_info_offset * 8; size = *(bia + 1) * 8; memcpy(data + offset, bia, size); } if (pia) { offset = fch->product_info_offset * 8; size = *(pia + 1) * 8; memcpy(data + offset, pia, size); } *raw_data = data; return total_length; }