/* * Generate status string from bitfield */ static void upsc_setstatus(unsigned int status) { /* * I'll look for all available statuses, even though they might not be * supported in the UPScode II protocol. */ status_init(); if (status & UPSC_STAT_ONLINE) status_set("OL"); if (status & UPSC_STAT_ONBATT) status_set("OB"); if (status & UPSC_STAT_LOBATT) status_set("LB"); if (status & UPSC_STAT_REPLACEBATT) status_set("RB"); if (status & UPSC_STAT_BOOST) status_set("BOOST"); if (status & UPSC_STAT_TRIM) status_set("TRIM"); if (status & UPSC_STAT_OVERLOAD) status_set("OVER"); if (status & UPSC_STAT_CALIBRATION) status_set("CAL"); if (status & UPSC_STAT_OFF) status_set("OFF"); if (status & UPSC_STAT_BYPASS) status_set("BYPASS"); status_commit(); }
void update_battery_charge_dchrg_status() { if (buf[BAT_CHRG_DISCHRG_STATUS] == '0') status_set("DISCHRG"); else status_set("CHRG"); }
void upsdrv_updateinfo(void) { if (ivt_status() < 7) { dstate_datastale(); return; } dstate_setinfo("battery.voltage", "%.2f", battery.voltage.act); dstate_setinfo("battery.voltage.minimum", "%.2f", battery.voltage.min); dstate_setinfo("battery.voltage.maximum", "%.2f", battery.voltage.max); dstate_setinfo("battery.current", "%.1f", battery.current.act); dstate_setinfo("battery.current.minimum", "%.1f", battery.current.min); dstate_setinfo("battery.current.maximum", "%.1f", battery.current.max); dstate_setinfo("battery.temperature", "%.0f", battery.temperature); status_init(); if (battery.current.act > 0) { status_set("OL"); /* charging */ } else { status_set("OB"); /* discharging */ } if (battery.voltage.act < battery.voltage.low) { status_set("LB"); } status_commit(); dstate_dataok(); }
void update_battery_status() { if((buf[BAT_STATUS])=='0') status_set("OB"); //battery normal else if((buf[BAT_STATUS])=='1') status_set("RB"); //battery low trip output else status_set("LB"); }
void upsdrv_updateinfo(void) { char reply[REPLY_PACKETSIZE]; int ret, online, battery_normal; if (!udev) { ret = usb_device_open(&udev, &usbdevice, &device_matcher, &driver_callback); if (ret < 0) { return; } } ret = query_ups(reply); if (ret < 4) { usb_comm_fail("Query to UPS failed"); dstate_datastale(); usb_device_close(udev); udev = NULL; return; } usb_comm_good(); dstate_dataok(); /* * 3rd bit of 4th byte indicates whether the UPS is on line (1) * or on battery (0) */ online = (reply[3]&4)>>2; /* * 2nd bit of 4th byte indicates battery status; normal (1) * or low (0) */ battery_normal = (reply[3]&2)>>1; status_init(); if (online) { status_set("OL"); } else { status_set("OB"); } if (!battery_normal) { status_set("LB"); } status_commit(); }
int main(void) { char str[30]; status_init(); adc_init(); status_set(false); uart_init(1); twi_init(0xC); twi_enable_interrupt(); twi_register_get(get); measurement = 0; sei(); while (1) { // VRef is 2.56V // at T = 0, VOut = 0V. VOut scales at 10mV / ºC // T = adc * (2.56 / 1024) * 100 // T = adc * 256 / 1024 // T = adc / 4 uint16_t read = adc_read(); measurement = read / 4; sprintf(str, "%dºC (raw = %d)", measurement, read); uart_send(str); _delay_ms(500); } }
bool advertise_advertise(void) { if (IS_CONNECTING) { return false; } //If advertising before, stop it. if (IS_ADVERTISING) { sd_ble_gap_adv_stop(); } advertise_set_data(); ble_gap_adv_params_t adv_settings = { 0 }; adv_settings.type = BLE_GAP_ADV_TYPE_ADV_IND; adv_settings.fp = BLE_GAP_ADV_FP_ANY; adv_settings.interval = MSEC_TO_UNITS(25, UNIT_0_625_MS); adv_settings.timeout = BLE_GAP_ADV_TIMEOUT_GENERAL_UNLIMITED; APP_ERROR_CHECK(sd_ble_gap_adv_start(&adv_settings)); status_set(STATUS_ADVERTISING); return true; }
void update_load_status() { int i; int x; char data[2]; for(i=0;i<LOAD_LEN;i++) { data[i] = buf[LOAD_STATUS+i]; } data[i] = '\0'; x = atoi(data); if(x == 2) status_set("OVER"); //over load else status_set("TRIM"); //over load trip out }
ISR(TWI_vect, ISR_BLOCK) { status_set(true); //uart_send("TWI ISR"); //DUMP_ERROR(); if (TW_STATUS == TW_SR_SLA_ACK) { //uart_send("Got my SLA+W"); command = 0xFF; } else if (TW_STATUS == TW_ST_SLA_ACK) { if (command == 0x0) { //uart_send("Got into a ST with a PUT --- wwwwat"); } else { //uart_send("Got a GET"); if (get_callback) { TWDR = get_callback(); } else { //uart_send("No callback, sending 0xFF"); TWDR = 0xFF; } // For the last data byte we need to set ack to 0 SEND(0, 0); return; } } else if (TW_STATUS == TW_SR_DATA_ACK) { if (command == 0xFF) { command = TWDR; //sprintf(str, "Got command = %x", command); //uart_send(str); } else if (command == 0x0) { //uart_send("Got a PUT"); if (put_callback) { put_callback(TWDR); } } else { //uart_send("WAAAAAAT -- Got a DATA ACK on a GET"); } } else if (TW_STATUS == TW_ST_DATA_ACK) { //uart_send("WTF - Got DATA ACK on ST"); } SEND(1, 0); }
static void status_reset(void) { LOG_INFO("Resetting status control data\n"); status_set(BSM2_NOFIX); MOVING_AVG_RESET(&alt_delta); MOVING_AVG_RESET(&press_delta); curr_vdir = HOVERING; }
static int setvar(const char *varname, const char *val) { dummy_info_t *item; upsdebugx(2, "entering setvar(%s, %s)", varname, val); /* FIXME: the below is only valid if (mode == MODE_DUMMY) * if (mode == MODE_REPEATER) => forward * if (mode == MODE_META) => ? */ if (!strncmp(varname, "ups.status", 10)) { status_init(); /* FIXME: split and check values (support multiple values), à la usbhid-ups */ status_set(val); status_commit(); return STAT_SET_HANDLED; } /* Check variable validity */ if (!is_valid_data(varname)) { upsdebugx(2, "setvar: invalid variable name (%s)", varname); return STAT_SET_UNKNOWN; } /* Check value validity */ if (!is_valid_value(varname, val)) { upsdebugx(2, "setvar: invalid value (%s) for variable (%s)", val, varname); return STAT_SET_UNKNOWN; } /* If value is empty, remove the variable (FIXME: do we need * a magic word?) */ if (strlen(val) == 0) { dstate_delinfo(varname); } else { dstate_setinfo(varname, "%s", val); if ( (item = find_info(varname)) != NULL) { dstate_setflags(item->info_type, item->info_flags); /* Set max length for strings, if needed */ if (item->info_flags & ST_FLAG_STRING) dstate_setaux(item->info_type, item->info_len); } } return STAT_SET_HANDLED; }
void status_printf(int lvl, const char *t, ...) { static char buf[256]; va_list l; va_start(l, t); vsnprintf(buf, sizeof buf, t, l); va_end(l); status_set(lvl, buf); }
void upsdrv_updateinfo(void) { getupdateinfo(); /* new package for updates */ dstate_setinfo("output.voltage", "%03.1f", OutVoltage); dstate_setinfo("input.voltage", "%03.1f", InVoltage); dstate_setinfo("battery.voltage", "%02.1f", BattVoltage); /* output and bypass tests */ if( OutputOn ) dstate_setinfo("outlet.switchable", "%s", "yes"); else dstate_setinfo("outlet.switchable", "%s", "no"); if( BypassOn ) dstate_setinfo("outlet.1.switchable", "%s", "yes"); else dstate_setinfo("outlet.1.switchable", "%s", "no"); status_init(); if (!SourceFail ) status_set("OL"); /* on line */ else status_set("OB"); /* on battery */ if (Autonomy < 5 ) status_set("LB"); /* low battery */ status_commit(); dstate_setinfo("ups.temperature", "%2.2f", Temperature); dstate_setinfo("input.frequency", "%2.1f", InFreq); dstate_dataok(); }
void update_pseudovars( void ) { status_init(); if(strcmp(sec_varlist[9].value,"1")== 0) { status_set("OFF"); } if(strcmp(sec_varlist[76].value,"0")== 0) { status_set("OL"); } if(strcmp(sec_varlist[76].value,"1")== 0) { status_set("OB"); } if(strcmp(sec_varlist[76].value,"2")== 0) { status_set("BYPASS"); } if(strcmp(sec_varlist[76].value,"3")== 0) { status_set("TRIM"); } if(strcmp(sec_varlist[76].value,"4")== 0) { status_set("BOOST"); } if(strcmp(sec_varlist[10].value,"1")== 0) { status_set("OVER"); } if(strcmp(sec_varlist[22].value,"1")== 0) { status_set("LB"); } if(strcmp(sec_varlist[19].value,"2")== 0) { status_set("RB"); } status_commit(); }
void _protocol_dispatch(uint8_t cmd, uint8_t length) { status_set(STATUS_MESSAGE_RX); switch(cmd) { case 'A': for (uint8_t i = 0; i < 4; i++) { _last_flight_val[i] = (_buf)[i]; } _last_flight_cmd = cmd; break; case 'M': _last_flight_cmd = cmd; break; case 'C': gyro_calibrate(); accel_calibrate(); protocol_send_diag("calibrated"); break; case 't': pid_send_tuning(); attitude_send_tuning(); protocol_send_diag("tuning sent"); break; case 'E': _telemetry_enabled = _telemetry_enabled ? 0x00 : 0x01; break; case 'r': _raw_enabled = _raw_enabled ? 0x00 : 0x01; break; case 'p': pid_receive_tuning(_buf); protocol_send_diag("pid received"); break; case 'c': case 'k': if (attitude_get_id() == 'K') { attitude_receive_tuning(_buf); protocol_send_diag("kalman received"); } else if (attitude_get_id() == 'C') { attitude_receive_tuning(_buf); protocol_send_diag("complementary received"); } break; } }
int status_init(void) { int rv; // initialize the GPIO hardware XGpio_Initialize(&LED, XPAR_XPS_GPIO_1_DEVICE_ID ); XGpio_SetDataDirection(&LED, 1, 0 ); XGpio_DiscreteWrite(&LED,1,0x1F); XGpio_Initialize(&JP1, XPAR_XPS_GPIO_0_DEVICE_ID ); XGpio_SetDataDirection(&JP1, 1, 0xF ); status_set( 0, s_init ); #ifdef __XMK__ // I think using pthread_create is OK since this thread does not access any LwIP stuff. return pthread_create( &status_tid, NULL, status_thread, NULL ); #else return 0; #endif }
void protocol_send_message(uint8_t cmd, uint8_t *bytes, uint8_t length) { _protocol_send_byte(START, 0); _protocol_send_byte(length + 1, 1); _protocol_send_byte(cmd, 0); uint8_t checksum = cmd; for (uint8_t i = 0; i < length; i++) { _protocol_send_byte(bytes[i], 1); checksum += bytes[i]; } checksum = 0xff - checksum; _protocol_send_byte(checksum, 1); status_set(STATUS_MESSAGE_TX); }
usbMsgLen_t usbFunctionSetup(uchar data[8]) { usbRequest_t *rq = (void *)data; if (rq->bRequest == CUSTOM_RQ_LED_SET_STATUS) { status_set(rq->wValue.word & 1); } else if (rq->bRequest == CUSTOM_RQ_KEY_GET_STATUS) { keypad_update(); DBG1(0x02, keypad_get(), 8); usbMsgPtr = keypad_get(); return 8; } else if (rq->bRequest == CUSTOM_RQ_LEDS_SET_STATUS) { return USB_NO_MSG; } else if (rq->bRequest == CUSTOM_RQ_LEDS_SET_LAYER) { if (rq->wValue.word >= 0 && rq->wValue.word < MAXLAYER) current_layer = rq->wValue.word; } else if (rq->bRequest == CUSTOM_RQ_LEDS_SET_INTENSITY) { led_send_command(LED_INTENSITY,rq->wValue.bytes[0]); } return 0; }
int main(int argc, char *argv[]) { SRunner *sr; int_t failed = 0; time_t prog_start, test_start, test_end; if (process_kill(PLACER("magmad", 18), SIGTERM, 10) < 0) { log_unit("Another instance of the Magma Daemon is already running and refuses to die."); exit(EXIT_FAILURE); } // Setup prog_start = time(NULL); // Updates the location of the config file if it was specified on the command line. check_args_parse(argc, argv); if (do_virus_check && !virus_check_data_path) { virus_check_data_path = ns_dupe(VIRUS_CHECK_DATA_PATH); } if (do_tank_check && !tank_check_data_path) { tank_check_data_path = ns_dupe(TANK_CHECK_DATA_PATH); } if (do_dspam_check && !dspam_check_data_path) { dspam_check_data_path = ns_dupe(DSPAM_CHECK_DATA_PATH); } /*if (do_virus_check) printf("doing virus check: [%s]\n", !virus_check_data_path ? "NONE" : virus_check_data_path); else printf ("skipping virus check\n"); if (do_tank_check) printf("doing tank check: [%s]\n", !tank_check_data_path ? "NONE" : tank_check_data_path); else printf ("skipping tank check\n"); if (do_dspam_check) printf("doing dspam check: [%s]\n", !dspam_check_data_path ? "NONE" : dspam_check_data_path); else printf ("skipping dspam check\n"); printf("config file: [%s]\n", magma.config.file); exit(EXIT_SUCCESS);*/ if (!process_start()) { log_unit("Initialization error. Exiting.\n"); status_set(-1); process_stop(); exit(EXIT_FAILURE); } // Only during development... cache_flush(); // Unit Test Config sr = srunner_create(suite_check_magma()); // Add the suites. srunner_add_suite(sr, suite_check_core()); srunner_add_suite(sr, suite_check_provide()); srunner_add_suite(sr, suite_check_network()); srunner_add_suite(sr, suite_check_objects()); srunner_add_suite(sr, suite_check_users()); // If were being run under Valgrind, we need to disable forking and increase the default timeout. // Under Valgrind, forked checks appear to improperly timeout. if (RUNNING_ON_VALGRIND == 0 && (failed = running_on_debugger()) == 0) { log_unit("Not being traced or profiled...\n"); srunner_set_fork_status (sr, CK_FORK); case_timeout = RUN_TEST_CASE_TIMEOUT; } else { // Trace detection attempted was thwarted. if (failed == -1) log_unit("Trace detection was thwarted.\n"); else log_unit("Tracing or debugging is active...\n"); srunner_set_fork_status (sr, CK_NOFORK); case_timeout = PROFILE_TEST_CASE_TIMEOUT; } // Execute log_unit("--------------------------------------------------------------------------\n"); test_start = time(NULL); srunner_run_all(sr, CK_SILENT); test_end = time(NULL); // Output timing. log_unit("--------------------------------------------------------------------------\n"); log_unit("%-63.63s %9lus\n", "TEST DURATION:", test_end - test_start); log_unit("%-63.63s %9lus\n", "TOTAL DURATION:", test_end - prog_start); // Summary log_unit("--------------------------------------------------------------------------\n"); failed = srunner_ntests_failed(sr); srunner_print(sr, CK_NORMAL); // The Check Output Ending log_unit("--------------------------------------------------------------------------\n"); // Cleanup and free the resources allocated by the check code. status_set(-1); srunner_free(sr); ns_cleanup(virus_check_data_path); ns_cleanup(tank_check_data_path); ns_cleanup(dspam_check_data_path); // Cleanup and free the resources allocated by the magma code. process_stop(); system_init_umask(); exit((failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE); }
static void xmpp_iq_gameroom_open_cb(const char *msg, enum xmpp_msg_type type, void *args) { /* Answer : <iq to='masterserver@warface/pve_2' type='get'> <query xmlns='urn:cryonline:k01'> <data query_name='gameroom_open' compressedData='...' originalSize='42'/> </query> </iq> */ struct cb_args *a = (struct cb_args *) args; if (type & XMPP_TYPE_ERROR) { int code = get_info_int(msg, "code='", "'", NULL); int custom_code = get_info_int(msg, "custom_code='", "'", NULL); const char *reason = NULL; switch (code) { case 1006: reason = "QoS limit reached"; break; case 8: switch (custom_code) { case 0: /* Expired mission, update and try again */ if (++a->tries < 2) { struct mission *m = mission_list_get_by_key(a->mission_key); if (m != NULL) { a->mission_name = strdup(m->name); mission_list_update( _open_updated_list, args); return; } } reason = "Expired missions"; break; case 1: reason = "Invalid or expired mission"; break; case 12: reason = "Rank restricted"; break; case 21: reason = "Invalid room name"; break; default: break; } break; default: break; } if (reason != NULL) eprintf("Failed to open room (%s)\n", reason); else eprintf("Failed to open room (%i:%i)\n", code, custom_code); } else { char *data = wf_get_query_content(msg); if (data == NULL) { free(a); return; } /* Leave previous room if any */ if (session.gameroom.jid != NULL) { xmpp_presence(session.gameroom.jid, XMPP_PRESENCE_LEAVE, NULL, NULL); free(session.gameroom.group_id); session.gameroom.group_id = NULL; free(session.gameroom.jid); session.gameroom.jid = NULL; gameroom_sync_free(); } char *room = get_info(data, "room_id='", "'", "Room ID"); if (room != NULL) { /* Join XMPP room */ char *room_jid; FORMAT(room_jid, "*****@*****.**", session.online.channel, room); xmpp_presence(room_jid, XMPP_PRESENCE_JOIN, NULL, NULL); session.gameroom.jid = room_jid; /* Reset auto-ready */ session.gameroom.desired_status = GAMEROOM_READY; gameroom_sync_init(); gameroom_sync(data); status_set(STATUS_ONLINE | STATUS_ROOM); } if (a->fun != NULL) a->fun(room, a->args); free(room); free(data); } free(a->mission_name); a->mission_name = NULL; free(a->mission_key); a->mission_key = NULL; free(a); }
static void xmpp_iq_gameroom_update_pvp_cb(const char *msg, enum xmpp_msg_type type, void *args) { /* Answer : <iq to='masterserver@warface/pve_2' type='get'> <query xmlns='urn:cryonline:k01'> <data query_name='gameroom_update_pvp' compressedData='...' originalSize='42'/> </query> </iq> */ struct cb_args *a = (struct cb_args *) args; if (type & XMPP_TYPE_ERROR) { const char *reason = NULL; int code = get_info_int(msg, "code='", "'", NULL); int custom_code = get_info_int(msg, "custom_code='", "'", NULL); switch (code) { case 8: switch (custom_code) { case 1: reason = LANG(error_unknown_mission); break; case 7: reason = LANG(error_room_started); break; default: break; } break; default: break; } if (reason != NULL) eprintf("%s (%s)", LANG(error_gameroom_setinfo), reason); else eprintf("%s (%i:%i)", LANG(error_gameroom_setinfo), code, custom_code); } else { char *data = wf_get_query_content(msg); if (data == NULL) return; gameroom_sync(data); status_set(session.online.status); if (a->cb) a->cb(a->args); free(data); } free(a); }
static int blazer_status(const char *cmd) { const struct { const char *var; const char *fmt; double (*conv)(const char *, char **); } status[] = { { "input.voltage", "%.1f", strtod }, { "input.voltage.fault", "%.1f", strtod }, { "output.voltage", "%.1f", strtod }, { "ups.load", "%.0f", blazer_load }, { "input.frequency", "%.1f", strtod }, { "battery.voltage", "%.2f", blazer_battery }, { "ups.temperature", "%.1f", strtod }, { NULL } }; char buf[SMALLBUF], *val, *last = NULL; int i; /* * > [Q1\r] * < [(226.0 195.0 226.0 014 49.0 27.5 30.0 00001000\r] * 01234567890123456789012345678901234567890123456 * 0 1 2 3 4 */ if (blazer_command(cmd, buf, sizeof(buf)) < 46) { upsdebugx(2, "%s: short reply", __func__); return -1; } if (buf[0] != '(') { upsdebugx(2, "%s: invalid start character [%02x]", __func__, buf[0]); return -1; } for (i = 0, val = strtok_r(buf+1, " ", &last); status[i].var; i++, val = strtok_r(NULL, " \r\n", &last)) { if (!val) { upsdebugx(2, "%s: parsing failed", __func__); return -1; } if (strspn(val, "0123456789.") != strlen(val)) { upsdebugx(2, "%s: non numerical value [%s]", __func__, val); continue; } dstate_setinfo(status[i].var, status[i].fmt, status[i].conv(val, NULL)); } if (!val) { upsdebugx(2, "%s: parsing failed", __func__); return -1; } if (strspn(val, "01") != 8) { upsdebugx(2, "Invalid status [%s]", val); return -1; } if (val[7] == '1') { /* Beeper On */ dstate_setinfo("ups.beeper.status", "enabled"); } else { dstate_setinfo("ups.beeper.status", "disabled"); } if (val[4] == '1') { /* UPS Type is Standby (0 is On_line) */ dstate_setinfo("ups.type", "offline / line interactive"); } else { dstate_setinfo("ups.type", "online"); } status_init(); if (val[0] == '1') { /* Utility Fail (Immediate) */ status_set("OB"); online = 0; } else { status_set("OL"); online = 1; } if (val[1] == '1') { /* Battery Low */ status_set("LB"); } if (val[2] == '1') { /* Bypass/Boost or Buck Active */ double vi, vo; vi = strtod(dstate_getinfo("input.voltage"), NULL); vo = strtod(dstate_getinfo("output.voltage"), NULL); if (vo < 0.5 * vi) { upsdebugx(2, "%s: output voltage too low", __func__); } else if (vo < 0.95 * vi) { status_set("TRIM"); } else if (vo < 1.05 * vi) { status_set("BYPASS"); } else if (vo < 1.5 * vi) { status_set("BOOST"); } else { upsdebugx(2, "%s: output voltage too high", __func__); } } if (val[5] == '1') { /* Test in Progress */ status_set("CAL"); } alarm_init(); if (val[3] == '1') { /* UPS Failed */ alarm_set("UPS selftest failed!"); } if (val[6] == '1') { /* Shutdown Active */ alarm_set("Shutdown imminent!"); status_set("FSD"); } alarm_commit(); status_commit(); return 0; }
/* update information */ void upsdrv_updateinfo(void) { char val[32]; if (!ups_getinfo()){ return; } /* input.frequency */ upsdebugx(3, "input.frequency (raw data): [raw: %u]", raw_data[INPUT_FREQUENCY]); dstate_setinfo("input.frequency", "%02.2f", input_freq()); upsdebugx(2, "input.frequency: %s", dstate_getinfo("input.frequency")); /* output.frequency */ upsdebugx(3, "output.frequency (raw data): [raw: %u]", raw_data[OUTPUT_FREQUENCY]); dstate_setinfo("output.frequency", "%02.2f", output_freq()); upsdebugx(2, "output.frequency: %s", dstate_getinfo("output.frequency")); /* ups.load */ upsdebugx(3, "ups.load (raw data): [raw: %u]", raw_data[UPS_LOAD]); dstate_setinfo("ups.load", "%03.1f", load_level()); upsdebugx(2, "ups.load: %s", dstate_getinfo("ups.load")); /* battery.charge */ upsdebugx(3, "battery.charge (raw data): [raw: %u]", raw_data[BATTERY_CHARGE]); dstate_setinfo("battery.charge", "%03.1f", batt_level()); upsdebugx(2, "battery.charge: %s", dstate_getinfo("battery.charge")); /* input.voltage */ upsdebugx(3, "input.voltage (raw data): [raw: %u]", raw_data[INPUT_VOLTAGE]); dstate_setinfo("input.voltage", "%03.1f",input_voltage()); upsdebugx(2, "input.voltage: %s", dstate_getinfo("input.voltage")); /* output.voltage */ upsdebugx(3, "output.voltage (raw data): [raw: %u]", raw_data[OUTPUT_VOLTAGE]); dstate_setinfo("output.voltage", "%03.1f",output_voltage()); upsdebugx(2, "output.voltage: %s", dstate_getinfo("output.voltage")); status_init(); *val = 0; if (!(raw_data[STATUS_A] & MAINS_FAILURE)) { !(raw_data[STATUS_A] & OFF) ? status_set("OL") : status_set("OFF"); } else { status_set("OB"); } if (raw_data[STATUS_A] & LOW_BAT) status_set("LB"); if (raw_data[STATUS_A] & AVR_ON) { input_voltage() < linevoltage ? status_set("BOOST") : status_set("TRIM"); } if (raw_data[STATUS_A] & OVERLOAD) status_set("OVER"); if (raw_data[STATUS_B] & BAD_BAT) status_set("RB"); if (raw_data[STATUS_B] & TEST) status_set("TEST"); status_commit(); upsdebugx(2, "STATUS: %s", dstate_getinfo("ups.status")); dstate_dataok(); }
void upsdrv_updateinfo(void) { typedef struct { const unsigned char cmd[6]; const char *var; const char *fmt; const int multindex; } cmd_s; static cmd_s vartab[] = { /* common vars */ { { 1,149,2,1,1,154 }, "battery.runtime", "%.0f", M_BAT_RUNTIME }, { { 1,149,2,1,2,155 }, "battery.voltage", "%.1f", M_VOLT_DC }, { { 1,149,2,1,3,156 }, "battery.current", "%.2f", M_CURRENT_DC }, { { 1,161,2,1,13,178 }, "battery.voltage.nominal", "%.1f", M_VOLT_DC }, { { 1,149,2,1,12,165 }, "battery.temperature", "%.1f", M_TEMPERATURE }, { { 1,149,2,1,14,167 }, "ups.temperature", "%.1f", M_TEMPERATURE }, { { 1,161,2,1,8,173 }, "ups.power.nominal", "%.0f", M_NOMPOWER }, { { 1,161,2,1,4,169 }, "ups.delay.start", "%.0f", M_10 }, { { 1,161,2,1,14,179 },"battery.runtime.low", "%.0f", M_BAT_RUNTIME }, { { 1,149,2,1,8,161 }, "input.frequency", "%.1f", M_FREQUENCY }, { { 1,149,2,1,10,163 }, "input.bypass.frequency", "%.1f", M_FREQUENCY }, { { 1,161,2,1,9,174 }, "input.frequency.nominal", "%.1f", M_FREQUENCY }, { { 1,149,2,1,9,162 }, "output.frequency", "%.1f", M_FREQUENCY }, { { 1,161,2,1,10,175 }, "output.frequency.nominal", "%.1f", M_FREQUENCY }, { { 0 }, NULL, NULL, 0 } }; static cmd_s vartab1o[] = { /* 1-phase out */ { { 1,149,2,1,7,160 }, "ups.load", "%.0f", M_LOADPERC }, { { 1,149,2,1,6,159 }, "ups.power", "%.0f", M_POWER }, { { 1,149,2,1,5,158 }, "ups.realpower", "%.0f", M_POWER }, { { 1,144,2,1,3,151 }, "output.voltage", "%.1f", M_VOLTAGE_O }, { { 1,144,2,1,4,152 }, "output.current", "%.1f", M_CURRENT_O }, { { 0 }, NULL, NULL, 0 } }; static cmd_s vartab1i[] = { /* 1-phase in*/ { { 1,144,2,1,1,149 }, "input.voltage", "%.1f", M_VOLTAGE_I }, { { 1,144,2,1,5,153 }, "input.bypass.voltage", "%.1f", M_VOLTAGE_B }, { { 1,144,2,1,6,154 }, "input.bypass.current", "%.1f", M_CURRENT_B }, { { 0 }, NULL, NULL, 0 } }; static cmd_s vartab3o[] = { /*3-phase out */ { { 1,144,2,1,24,172 }, "ups.L1.load", "%.0f", M_LOADPERC }, { { 1,145,2,1,24,173 }, "ups.L2.load", "%.0f", M_LOADPERC }, { { 1,146,2,1,24,174 }, "ups.L3.load", "%.0f", M_LOADPERC }, { { 1,144,2,1,22,170 }, "ups.L1.power", "%.0f", M_POWER }, { { 1,145,2,1,22,171 }, "ups.L2.power", "%.0f", M_POWER }, { { 1,146,2,1,22,172 }, "ups.L3.power", "%.0f", M_POWER }, { { 1,144,2,1,21,169 }, "ups.L1.realpower", "%.0f", M_POWER }, { { 1,145,2,1,21,170 }, "ups.L2.realpower", "%.0f", M_POWER }, { { 1,146,2,1,21,171 }, "ups.L3.realpower", "%.0f", M_POWER }, { { 1,144,2,1,3,151 }, "output.L1-N.voltage", "%.1f", M_VOLTAGE_O }, { { 1,145,2,1,3,152 }, "output.L2-N.voltage", "%.1f", M_VOLTAGE_O }, { { 1,146,2,1,3,153 }, "output.L3-N.voltage", "%.1f", M_VOLTAGE_O }, { { 1,144,2,1,14,162 }, "output.L1.crestfactor", "%.1f", M_0_1 }, { { 1,145,2,1,14,163 }, "output.L2.crestfactor", "%.1f", M_0_1 }, { { 1,146,2,1,14,164 }, "output.L3.crestfactor", "%.1f", M_0_1 }, { { 0 }, NULL, NULL, 0 } }; static cmd_s vartab3i[] = { /*3-phase in */ { { 1,144,2,1,1,149 }, "input.L1-N.voltage", "%.1f", M_VOLTAGE_I }, { { 1,145,2,1,1,150 }, "input.L2-N.voltage", "%.1f", M_VOLTAGE_I }, { { 1,146,2,1,1,151 }, "input.L3-N.voltage", "%.1f", M_VOLTAGE_I }, { { 1,144,2,1,5,153 }, "input.L1-N.bypass.voltage", "%.1f", M_VOLTAGE_B }, { { 1,145,2,1,5,154 }, "input.L2-N.bypass.voltage", "%.1f", M_VOLTAGE_B }, { { 1,146,2,1,5,155 }, "input.L3-N.bypass.voltage", "%.1f", M_VOLTAGE_B }, { { 1,144,2,1,6,154 }, "input.L1-N.bypass.current", "%.1f", M_CURRENT_B }, { { 1,145,2,1,6,155 }, "input.L2-N.bypass.current", "%.1f", M_CURRENT_B }, { { 1,146,2,1,6,156 }, "input.L3-N.bypass.current", "%.1f", M_CURRENT_B }, { { 1,144,2,1,2,150 }, "input.L1.current", "%.1f", M_CURRENT_I }, { { 1,145,2,1,2,151 }, "input.L2.current", "%.1f", M_CURRENT_I }, { { 1,146,2,1,2,152 }, "input.L3.current", "%.1f", M_CURRENT_I }, { { 0 }, NULL, NULL, 0 } }; static cmd_s * cmdin_p; static cmd_s * cmdout_p; const char *val; char reply[8]; int ret, i; for (i = 0; vartab[i].var; i++) { int16_t val; ret = do_command(vartab[i].cmd, reply, 6); if (ret < 8) { continue; } val = (unsigned char)reply[5]; val <<= 8; val += (unsigned char)reply[6]; dstate_setinfo(vartab[i].var, vartab[i].fmt, val * multi[vartab[i].multindex]); } if (num_inphases>1){ cmdin_p=vartab3i; } else { cmdin_p=vartab1i; } if (num_outphases>1){ cmdout_p=vartab3o; } else { cmdout_p=vartab1o; } for (i = 0; cmdin_p[i].var; i++) { int16_t val; ret = do_command(cmdin_p[i].cmd, reply, 6); if (ret < 8) { continue; } val = (unsigned char)reply[5]; val <<= 8; val += (unsigned char)reply[6]; dstate_setinfo(cmdin_p[i].var, cmdin_p[i].fmt, val * multi[cmdin_p[i].multindex]); } for (i = 0; cmdout_p[i].var; i++) { int16_t val; ret = do_command(cmdout_p[i].cmd, reply, 6); if (ret < 8) { continue; } val = (unsigned char)reply[5]; val <<= 8; val += (unsigned char)reply[6]; dstate_setinfo(cmdout_p[i].var, cmdout_p[i].fmt, val * multi[cmdout_p[i].multindex]); } status_init(); ret = do_command(cmd_bitfield1, reply, 6); if (ret < 8) { upslogx(LOG_ERR, "Failed reading bitfield #1"); dstate_datastale(); return; } if (reply[5] & (1<<0)) { /* ON_BATTERY */ status_set("OB"); } else { status_set("OL"); } val = dstate_getinfo("battery.current"); if (val) { if (atof(val) > 0.05) { status_set("CHRG"); } if (atof(val) < -0.05) { status_set("DISCHRG"); } } ret = do_command(cmd_bitfield2, reply, 6); if (ret < 8) { upslogx(LOG_ERR, "Failed reading bitfield #2"); dstate_datastale(); return; } if (reply[6] & (1<<0)) { /* ON_BYPASS */ status_set("BYPASS"); } if (reply[6] & (1<<5)) { /* REPLACE_BATTERY */ status_set("RB"); } if (reply[6] & (1<<6)) { /* BOOST_ON */ status_set("BOOST"); } if (reply[5] & (1<<1)) { /* BUCK_ON */ status_set("TRIM"); } ret = do_command(cmd_bitfield3, reply, 6); if (ret < 8) { upslogx(LOG_ERR, "Failed reading bitfield #3"); dstate_datastale(); return; } if (reply[6] & (1<<0) ) { /* UPS_OVERLOAD */ status_set("OVER"); } if (reply[6] & (1<<5) ) { /* LOW_BATTERY */ status_set("LB"); } status_commit(); dstate_dataok(); }
void upsdrv_updateinfo(void) { char response[MAX_RESPONSE_LENGTH]; char *ptr, *ptr2; int i; int flags; int contacts_set; int low_battery; status_init(); if (do_command(POLL, STATUS_OUTPUT, "", response) <= 0) { dstate_datastale(); return; } ptr = field(response, 0); /* require output status field to exist */ if (!ptr) { dstate_datastale(); return; } switch (atoi(ptr)) { case 0: status_set("OL"); break; case 1: status_set("OB"); break; case 2: status_set("BYPASS"); break; case 3: status_set("OL"); status_set("TRIM"); break; case 4: status_set("OL"); status_set("BOOST"); break; case 5: status_set("BYPASS"); break; case 6: break; case 7: status_set("OFF"); break; default: break; } ptr = field(response, 6); if (ptr) dstate_setinfo("ups.load", "%d", atoi(ptr)); ptr = field(response, 3); if (ptr) dstate_setinfo("output.voltage", "%03.1f", (double) atoi(ptr) / 10.0); ptr = field(response, 1); if (ptr) dstate_setinfo("output.frequency", "%03.1f", (double) atoi(ptr) / 10.0); ptr = field(response, 4); if (ptr) dstate_setinfo("output.current", "%03.1f", (double) atoi(ptr) / 10.0); low_battery = 0; if (do_command(POLL, STATUS_BATTERY, "", response) <= 0) { dstate_datastale(); return; } ptr = field(response, 0); if (ptr && atoi(ptr) == 2) status_set("RB"); ptr = field(response, 1); if (ptr && atoi(ptr)) low_battery = 1; ptr = field(response, 8); if (ptr) dstate_setinfo("battery.temperature", "%d", atoi(ptr)); ptr = field(response, 9); if (ptr) { dstate_setinfo("battery.charge", "%d", atoi(ptr)); ptr2 = getval("lowbatt"); if (ptr2 && atoi(ptr2) > 0 && atoi(ptr2) <= 99 && atoi(ptr) <= atoi(ptr2)) low_battery = 1; } ptr = field(response, 6); if (ptr) dstate_setinfo("battery.voltage", "%03.1f", (double) atoi(ptr) / 10.0); ptr = field(response, 7); if (ptr) dstate_setinfo("battery.current", "%03.1f", (double) atoi(ptr) / 10.0); if (low_battery) status_set("LB"); if (do_command(POLL, STATUS_ALARM, "", response) <= 0) { dstate_datastale(); return; } ptr = field(response, 3); if (ptr && atoi(ptr)) status_set("OVER"); if (do_command(POLL, STATUS_INPUT, "", response) > 0) { ptr = field(response, 2); if (ptr) dstate_setinfo("input.voltage", "%03.1f", (double) atoi(ptr) / 10.0); ptr = field(response, 1); if (ptr) dstate_setinfo("input.frequency", "%03.1f", (double) atoi(ptr) / 10.0); } if (do_command(POLL, TEST_RESULT, "", response) > 0) { int r; size_t trsize; r = atoi(response); trsize = sizeof(test_result_names) / sizeof(test_result_names[0]); if ((r < 0) || (r >= (int) trsize)) r = 0; dstate_setinfo("ups.test.result", "%s", test_result_names[r]); } if (do_command(POLL, ENVIRONMENT_INFORMATION, "", response) > 0) { ptr = field(response, 0); if (ptr) dstate_setinfo("ambient.temperature", "%d", atoi(ptr)); ptr = field(response, 1); if (ptr) dstate_setinfo("ambient.humidity", "%d", atoi(ptr)); flags = 0; contacts_set = 0; for (i = 0; i < 4; i++) { ptr = field(response, 2 + i); if (ptr) { contacts_set = 1; if (*ptr == '1') flags |= 1 << i; } } if (contacts_set) dstate_setinfo("ups.contacts", "%02X", flags); } /* if we are here, status is valid */ status_commit(); dstate_dataok(); }
void upsdrv_updateinfo(void) { char fstring[512]; if (! fc.valid) { fprintf(stderr, "upsupdate run before ups_ident() read ups config\n"); assert(0); } if (execute("f\r", fstring, sizeof(fstring)) > 0) { int inverter=0, charger=0, vin=0, vout=0, btimeleft=0, linestat=0, alstat=0, vaout=0; double ampsout=0.0, vbatt=0.0, battpercent=0.0, loadpercent=0.0, hstemp=0.0, acfreq=0.0, ambtemp=0.0; char tmp[16]; /* Inverter status. 0=off 1=on */ memcpy(tmp, fstring+16, 2); tmp[2] = '\0'; inverter = atoi(tmp); /* Charger status. 0=off 1=on */ memcpy(tmp, fstring+18, 2); tmp[2] = '\0'; charger = atoi(tmp); /* Input Voltage. integer number */ memcpy(tmp, fstring+24, 4); tmp[4] = '\0'; vin = atoi(tmp); /* Output Voltage. integer number */ memcpy(tmp, fstring+28, 4); tmp[4] = '\0'; vout = atoi(tmp); /* Iout. int times 10 */ memcpy(tmp, fstring+36, 4); tmp[4] = '\0'; ampsout = ((double)(atoi(tmp)) / 10.0); /* Battery voltage. int times 10 */ memcpy(tmp, fstring+50, 4); tmp[4] = '\0'; vbatt = ((double)(atoi(tmp)) / 10.0); /* Volt-amps out. int */ memcpy(tmp, fstring+40, 6); tmp[6] = '\0'; vaout = atoi(tmp); /* Line status. Bitmask */ memcpy(tmp, fstring+72, 2); tmp[2] = '\0'; linestat = atoi(tmp); /* Alarm status reg 1. Bitmask */ memcpy(tmp, fstring+20, 2); tmp[2] = '\0'; alstat = atoi(tmp); /* Alarm status reg 2. Bitmask */ memcpy(tmp, fstring+22, 2); tmp[2] = '\0'; alstat = alstat | (atoi(tmp) << 8); /* AC line frequency */ memcpy(tmp, fstring+54, 4); tmp[4]= '\0'; acfreq = ((double)(atoi(tmp)) / 100.0); /* Runtime remaining */ memcpy(tmp, fstring+58, 4); tmp[4]= '\0'; btimeleft = atoi(tmp); /* UPS Temperature */ memcpy(tmp, fstring+62, 4); tmp[4]= '\0'; ambtemp = (double)(atoi(tmp)); /* Percent Load */ switch(fc.model) { case ME3100: if (execute("d 16\r", fstring, sizeof(fstring)) > 0) { int l; sscanf(fstring, "16 FullLoad%% %d", &l); loadpercent = (double) l; } break; case RE1800: if (execute("d 16\r", fstring, sizeof(fstring)) > 0) { int l; sscanf(fstring, "16 FullLoad%% %d", &l); loadpercent = (double) l; } if (execute("d 12\r", fstring, sizeof(fstring)) > 0) { int l; sscanf(fstring, "12 HS Temp %dC", &l); hstemp = (double) l; } break; case MD1KVA: if (execute("d 22\r", fstring, sizeof(fstring)) > 0) { int l; sscanf(fstring, "22 FullLoad%% %d", &l); loadpercent = (double) l; } break; default: /* Will never happen, caught in upsdrv_initups() */ fatalx(EXIT_FAILURE, "Unknown model in upsdrv_updateinfo()"); } /* Compute battery percent left based on battery voltages. */ battpercent = ((vbatt - fc.emptyvolts) / (fc.fullvolts - fc.emptyvolts) * 100.0); if (battpercent < 0.0) battpercent = 0.0; else if (battpercent > 100.0) battpercent = 100.0; /* Compute status string */ { int lowbatt, overload, replacebatt, boosting, trimming; lowbatt = alstat & (1<<1); overload = alstat & (1<<6); replacebatt = alstat & (1<<10); boosting = inverter && (linestat & (1<<2)) && (vin < 115); trimming = inverter && (linestat & (1<<2)) && (vin > 115); status_init(); if (inverter) status_set("OB"); else status_set("OL"); if (lowbatt) status_set("LB"); if (trimming) status_set("TRIM"); if (boosting) status_set("BOOST"); if (replacebatt) status_set("RB"); if (overload) status_set("OVER"); status_commit(); } if (debugging) { fprintf(stderr, "Poll: inverter %d charger %d vin %d vout %d vaout %d btimeleft %d\n", inverter, charger, vin, vout, vaout, btimeleft); fprintf(stderr, " ampsout %5.1f vbatt %5.1f batpcnt %5.1f loadpcnt %5.1f upstemp %5.1f acfreq %5.2f ambtemp %5.1f\n", ampsout, vbatt, battpercent, loadpercent, hstemp, acfreq, ambtemp); } /* Stuff information into info structures */ dstate_setinfo("input.voltage", "%05.1f", (double)vin); dstate_setinfo("output.voltage", "%05.1f", (double)vout); dstate_setinfo("battery.charge", "%02.1f", battpercent); dstate_setinfo("ups.load", "%02.1f", loadpercent); dstate_setinfo("battery.voltage", "%02.1f", vbatt); dstate_setinfo("input.frequency", "%05.2f", (double)acfreq); dstate_setinfo("ups.temperature", "%05.1f", (double)hstemp); dstate_setinfo("battery.runtime", "%d", btimeleft); dstate_setinfo("ambient.temperature", "%05.1f", (double)ambtemp); dstate_dataok(); /* Tim: With out this return, it always falls over to the datastate() at the end of the function */ return; } else { dstate_datastale(); } /* if (execute("f\r", fstring, sizeof(fstring)) > 0) */ dstate_datastale(); return; }
/* Convert the local status information to NUT format and set NUT status. */ static void netxml_status_set(void) { if (STATUS_BIT(VRANGE)) { dstate_setinfo("input.transfer.reason", "input voltage out of range"); } else if (STATUS_BIT(FRANGE)) { dstate_setinfo("input.transfer.reason", "input frequency out of range"); } else { dstate_delinfo("input.transfer.reason"); } if (STATUS_BIT(ONLINE)) { status_set("OL"); /* on line */ } else { status_set("OB"); /* on battery */ } if (STATUS_BIT(DISCHRG)) { status_set("DISCHRG"); /* discharging */ } if (STATUS_BIT(CHRG)) { status_set("CHRG"); /* charging */ } if (STATUS_BIT(LOWBATT)) { status_set("LB"); /* low battery */ } if (STATUS_BIT(OVERLOAD)) { status_set("OVER"); /* overload */ } if (STATUS_BIT(REPLACEBATT)) { status_set("RB"); /* replace batt */ } if (STATUS_BIT(TRIM)) { status_set("TRIM"); /* SmartTrim */ } if (STATUS_BIT(BOOST)) { status_set("BOOST"); /* SmartBoost */ } if (STATUS_BIT(BYPASSAUTO) || STATUS_BIT(BYPASSMAN)) { status_set("BYPASS"); /* on bypass */ } if (STATUS_BIT(OFF)) { status_set("OFF"); /* ups is off */ } if (STATUS_BIT(SHUTDOWNIMM)) { status_set("FSD"); /* shutdown imminent */ } }
void upsdrv_updateinfo(void) { uint8_t getextendedOK; static int countlost = 0; int stat; upsdebugx(1, "countlost %d",countlost); if (countlost > 0){ upsdebugx(1, "Communication with UPS is lost: status read failed!"); if (countlost == COUNTLOST) { dstate_datastale(); upslogx(LOG_WARNING, "Communication with UPS is lost: status read failed!"); } } if (typeRielloProtocol == DEV_RIELLOGPSER) stat = get_ups_status(); else stat = get_ups_sentr(); if (stat < 0) { if (countlost < COUNTLOST) countlost++; return; } if (typeRielloProtocol == DEV_RIELLOGPSER) { if (get_ups_extended() == 0) getextendedOK = 1; else getextendedOK = 0; } else getextendedOK = 1; if (countlost == COUNTLOST) upslogx(LOG_NOTICE, "Communication with UPS is re-established!"); dstate_setinfo("input.frequency", "%.2f", DevData.Finp/10.0); dstate_setinfo("input.bypass.frequency", "%.2f", DevData.Fbypass/10.0); dstate_setinfo("output.frequency", "%.2f", DevData.Fout/10.0); dstate_setinfo("battery.voltage", "%.1f", DevData.Ubat/10.0); dstate_setinfo("battery.charge", "%u", DevData.BatCap); dstate_setinfo("battery.runtime", "%u", DevData.BatTime*60); dstate_setinfo("ups.temperature", "%u", DevData.Tsystem); if (input_monophase) { dstate_setinfo("input.voltage", "%u", DevData.Uinp1); dstate_setinfo("input.bypass.voltage", "%u", DevData.Ubypass1); } else { dstate_setinfo("input.L1-N.voltage", "%u", DevData.Uinp1); dstate_setinfo("input.L2-N.voltage", "%u", DevData.Uinp2); dstate_setinfo("input.L3-N.voltage", "%u", DevData.Uinp3); dstate_setinfo("input.bypass.L1-N.voltage", "%u", DevData.Ubypass1); dstate_setinfo("input.bypass.L2-N.voltage", "%u", DevData.Ubypass2); dstate_setinfo("input.bypass.L3-N.voltage", "%u", DevData.Ubypass3); } if (output_monophase) { dstate_setinfo("output.voltage", "%u", DevData.Uout1); dstate_setinfo("output.power.percent", "%u", DevData.Pout1); dstate_setinfo("ups.load", "%u", DevData.Pout1); } else { dstate_setinfo("output.L1-N.voltage", "%u", DevData.Uout1); dstate_setinfo("output.L2-N.voltage", "%u", DevData.Uout2); dstate_setinfo("output.L3-N.voltage", "%u", DevData.Uout3); dstate_setinfo("output.L1.power.percent", "%u", DevData.Pout1); dstate_setinfo("output.L2.power.percent", "%u", DevData.Pout2); dstate_setinfo("output.L3.power.percent", "%u", DevData.Pout3); dstate_setinfo("ups.load", "%u", (DevData.Pout1+DevData.Pout2+DevData.Pout3)/3); } status_init(); /* AC Fail */ if (riello_test_bit(&DevData.StatusCode[0], 1)) status_set("OB"); else status_set("OL"); /* LowBatt */ if ((riello_test_bit(&DevData.StatusCode[0], 1)) && (riello_test_bit(&DevData.StatusCode[0], 0))) status_set("LB"); /* Standby */ if (!riello_test_bit(&DevData.StatusCode[0], 3)) status_set("OFF"); /* On Bypass */ if (riello_test_bit(&DevData.StatusCode[1], 3)) status_set("BYPASS"); /* Overload */ if (riello_test_bit(&DevData.StatusCode[4], 2)) status_set("OVER"); /* Buck */ if (riello_test_bit(&DevData.StatusCode[1], 0)) status_set("TRIM"); /* Boost */ if (riello_test_bit(&DevData.StatusCode[1], 1)) status_set("BOOST"); /* Replace battery */ if (riello_test_bit(&DevData.StatusCode[2], 0)) status_set("RB"); /* Charging battery */ if (riello_test_bit(&DevData.StatusCode[2], 2)) status_set("CHRG"); status_commit(); dstate_dataok(); if (getextendedOK) { dstate_setinfo("output.L1.power", "%u", DevData.Pout1VA); dstate_setinfo("output.L2.power", "%u", DevData.Pout2VA); dstate_setinfo("output.L3.power", "%u", DevData.Pout3VA); dstate_setinfo("output.L1.realpower", "%u", DevData.Pout1W); dstate_setinfo("output.L2.realpower", "%u", DevData.Pout2W); dstate_setinfo("output.L3.realpower", "%u", DevData.Pout3W); dstate_setinfo("output.L1.current", "%u", DevData.Iout1); dstate_setinfo("output.L2.current", "%u", DevData.Iout2); dstate_setinfo("output.L3.current", "%u", DevData.Iout3); } poll_interval = 2; countlost = 0; /* if (get_ups_statuscode() != 0) upsdebugx(2, "Communication is lost"); else { }*/ /* * poll_interval = 2; */ }
void upsdrv_updateinfo(void) { CFPropertyListRef power_dictionary; CFStringRef power_source_state; CFNumberRef battery_voltage, battery_runtime; CFNumberRef current_capacity; CFBooleanRef is_charging; double max_capacity_value = 100.0, current_capacity_value; upsdebugx(1, "upsdrv_updateinfo()"); power_dictionary = copy_power_dictionary( g_power_key ); assert(power_dictionary); /* TODO: call dstate_datastale()? */ status_init(); /* Retrieve OL/OB state */ power_source_state = CFDictionaryGetValue(power_dictionary, CFSTR(kIOPSPowerSourceStateKey)); assert(power_source_state); CFRetain(power_source_state); upsdebugx(3, "Power Source State:"); if(nut_debug_level >= 3) CFShow(power_source_state); if(!CFStringCompare(power_source_state, CFSTR(kIOPSACPowerValue), 0)) { status_set("OL"); } else { status_set("OB"); } CFRelease(power_source_state); /* Retrieve CHRG state */ is_charging = CFDictionaryGetValue(power_dictionary, CFSTR(kIOPSIsChargingKey)); if(is_charging) { Boolean is_charging_value; is_charging_value = CFBooleanGetValue(is_charging); if(is_charging_value) { status_set("CHRG"); } } status_commit(); /* Retrieve battery voltage */ battery_voltage = CFDictionaryGetValue(power_dictionary, CFSTR(kIOPSVoltageKey)); if(battery_voltage) { int battery_voltage_value; CFNumberGetValue(battery_voltage, kCFNumberIntType, &battery_voltage_value); upsdebugx(2, "battery_voltage = %d mV", battery_voltage_value); dstate_setinfo("battery.voltage", "%.3f", battery_voltage_value/1000.0); } /* Retrieve battery runtime */ battery_runtime = CFDictionaryGetValue(power_dictionary, CFSTR(kIOPSTimeToEmptyKey)); if(battery_runtime) { double battery_runtime_value; CFNumberGetValue(battery_runtime, kCFNumberDoubleType, &battery_runtime_value); upsdebugx(2, "battery_runtime = %.f minutes", battery_runtime_value); if(battery_runtime_value > 0) { dstate_setinfo("battery.runtime", "%d", (int)(battery_runtime_value*60)); } else { dstate_delinfo("battery.runtime"); } } else { dstate_delinfo("battery.runtime"); } /* Retrieve current capacity */ current_capacity = CFDictionaryGetValue(power_dictionary, CFSTR(kIOPSCurrentCapacityKey)); if(current_capacity) { CFNumberGetValue(current_capacity, kCFNumberDoubleType, ¤t_capacity_value); upsdebugx(2, "Current Capacity = %.f/%.f units", current_capacity_value, max_capacity_value); if(max_capacity_value > 0) { dstate_setinfo("battery.charge", "%.f", 100.0 * current_capacity_value / max_capacity_value); } } /* TODO: it should be possible to set poll_interval (and maxage in the * server) to an absurdly large value, and use notify(3) to get * updates. */ /* * poll_interval = 2; */ dstate_dataok(); CFRelease(power_dictionary); }
/* for dummy mode * parse the definition file and process its content */ static int parse_data_file(int upsfd) { char fn[SMALLBUF]; char *ptr, var_value[MAX_STRING_SIZE]; int value_args = 0, counter; time_t now; time(&now); upsdebugx(1, "entering parse_data_file()"); if (now < next_update) { upsdebugx(1, "leaving (paused)..."); return 1; } /* initialise everything, to loop back at the beginning of the file */ if (ctx == NULL) { ctx = (PCONF_CTX_t *)xmalloc(sizeof(PCONF_CTX_t)); if (device_path[0] == '/') snprintf(fn, sizeof(fn), "%s", device_path); else snprintf(fn, sizeof(fn), "%s/%s", confpath(), device_path); pconf_init(ctx, upsconf_err); if (!pconf_file_begin(ctx, fn)) fatalx(EXIT_FAILURE, "Can't open dummy-ups definition file %s: %s", fn, ctx->errmsg); } /* Reset the next call time, so that we can loop back on the file * if there is no blocking action (ie TIMER) until the end of the file */ next_update = -1; /* Now start or continue parsing... */ while (pconf_file_next(ctx)) { if (pconf_parse_error(ctx)) { upsdebugx(2, "Parse error: %s:%d: %s", fn, ctx->linenum, ctx->errmsg); continue; } /* Check if we have something to process */ if (ctx->numargs < 1) continue; /* Process actions (only "TIMER" ATM) */ if (!strncmp(ctx->arglist[0], "TIMER", 5)) { /* TIMER <seconds> will wait "seconds" before * continuing the parsing */ int delay = atoi (ctx->arglist[1]); time(&next_update); next_update += delay; upsdebugx(1, "suspending execution for %i seconds...", delay); break; } /* Remove ":" suffix, after the variable name */ if ((ptr = strchr(ctx->arglist[0], ':')) != NULL) *ptr = '\0'; upsdebugx(3, "parse_data_file: variable \"%s\" with %d args", ctx->arglist[0], (int)ctx->numargs); /* Skip the driver.* collection data */ if (!strncmp(ctx->arglist[0], "driver.", 7)) { upsdebugx(2, "parse_data_file: skipping %s", ctx->arglist[0]); continue; } /* From there, we get varname in arg[0], and values in other arg[1...x] */ /* special handler for status */ if (!strncmp( ctx->arglist[0], "ups.status", 10)) { status_init(); for (counter = 1, value_args = ctx->numargs ; counter < value_args ; counter++) { status_set(ctx->arglist[counter]); } status_commit(); } else { for (counter = 1, value_args = ctx->numargs ; counter < value_args ; counter++) { if (counter == 1) /* don't append the first space separator */ snprintf(var_value, sizeof(var_value), "%s", ctx->arglist[counter]); else snprintfcat(var_value, sizeof(var_value), " %s", ctx->arglist[counter]); } if (setvar(ctx->arglist[0], var_value) == STAT_SET_UNKNOWN) { upsdebugx(2, "parse_data_file: can't add \"%s\" with value \"%s\"\nError: %s", ctx->arglist[0], var_value, ctx->errmsg); } else { upsdebugx(3, "parse_data_file: added \"%s\" with value \"%s\"", ctx->arglist[0], var_value); } } } /* Cleanup parseconf if there is no pending action */ if (next_update == -1) { pconf_finish(ctx); free(ctx); ctx=NULL; } return 1; }