[Nut-upsdev] patch: Replace many usleep and some sleep calls with nanosleep

Regid Ichira regid23 at yahoo.com
Mon Oct 3 17:02:35 UTC 2011


Description: Replace many usleep and some sleep calls with nanosleep.
             usleep is stated in its manual page as removed by POSIX.
             Contrary to its predecessors, nanosleep semantics is well
             defined. The replacement, which is mostly in drivers, is
             untested. Supplements http://bugs.debian.org/633791.
Last-Update: Sat, 13 Aug 2011 14:04:48 +0300

Index: b/clients/upssched.c
===================================================================
--- a/clients/upssched.c	2011-05-31 13:36:47.000000000 +0300
+++ b/clients/upssched.c	2011-08-17 00:40:17.000000000 +0300
@@ -652,7 +652,7 @@ static int check_parent(const char *cmd,
 		unlink(lockfn);
 
 		/* give the other one a chance to start it, then try again */
-		usleep(250000);
+		struct timespec delay = {0, 250e6}; nanosleep(&delay, NULL);
 	}
 
 	upslog_with_errno(LOG_ERR, "Failed to connect to parent and failed to create parent");
@@ -705,7 +705,7 @@ static void sendcmd(const char *cmd, con
 		if (pipefd == PARENT_STARTED) {
 
 			/* loop back and try to connect now */
-			usleep(250000);
+			struct timespec delay = {0, 250e6}; nanosleep(&delay, NULL);
 			continue;
 		}
 
Index: b/clients/upsmon.c
===================================================================
--- a/clients/upsmon.c	2011-05-31 13:36:47.000000000 +0300
+++ b/clients/upsmon.c	2011-08-17 00:40:17.000000000 +0300
@@ -610,7 +610,7 @@ static void slavesync(void)
 			return;
 		}
 
-		usleep(250000);
+		struct timespec delay = {0, 250e6}; nanosleep(&delay, NULL);
 	}
 }
 
Index: b/docs/new-drivers.txt
===================================================================
--- a/docs/new-drivers.txt	2011-05-31 13:36:47.000000000 +0300
+++ b/docs/new-drivers.txt	2011-08-17 00:40:17.000000000 +0300
@@ -632,9 +632,9 @@ depends on how fast your system is.
 
 You should check your driver with `strace` or its equivalent on your
 system.  If the driver is calling read() multiple times, consider adding
-a call to usleep before going into the ser_read_* call.  That will give
-it a chance to accumulate so you get the whole thing with one call to
-read without looping back for more.
+a call to nanosleep before going into the ser_read_* call.  That will
+give it a chance to accumulate so you get the whole thing with one call
+to read without looping back for more.
 
 This is not a request to save CPU time, even though it may do that.  The
 important part here is making the strace/ktrace output easier to read.
Index: b/drivers/safenet.c
===================================================================
--- a/drivers/safenet.c	2011-05-31 13:36:49.000000000 +0300
+++ b/drivers/safenet.c	2011-08-17 00:40:17.000000000 +0300
@@ -299,7 +299,7 @@ void upsdrv_initinfo(void)
 
 	dstate_setinfo("driver.version.internal", "%s", DRIVER_VERSION);
 	
-	usleep(100000);
+	struct timespec delay = {0, 100e6}; nanosleep(&delay, NULL);
 
 	/*
 	 * Very crude hardware detection. If an UPS is attached, it will set DSR
Index: b/drivers/powerpanel.c
===================================================================
--- a/drivers/powerpanel.c	2011-05-31 13:36:49.000000000 +0300
+++ b/drivers/powerpanel.c	2011-08-17 00:40:17.000000000 +0300
@@ -165,7 +165,7 @@ void upsdrv_initups(void)
 		}
 
 		ser_set_dtr(upsfd, 1);
-		usleep(10000);
+		struct timespec delay = {0, 10e6}; nanosleep(&delay, NULL);
 
 		if (subdriver[mode]->initups() > 0) {
 			upslogx(LOG_INFO, "CyberPower UPS with %s protocol on %s detected", subdriver[mode]->version, device_path);
@@ -173,7 +173,7 @@ void upsdrv_initups(void)
 		}
 
 		ser_set_dtr(upsfd, 0);
-		usleep(10000);
+		nanosleep(&delay, NULL);
 	}
 
 	fatalx(EXIT_FAILURE, "CyberPower UPS not found on %s", device_path);
Index: b/drivers/bestups.c
===================================================================
--- a/drivers/bestups.c	2011-05-31 13:36:49.000000000 +0300
+++ b/drivers/bestups.c	2011-08-17 00:40:17.000000000 +0300
@@ -252,7 +252,7 @@ static void ups_sync(void)
 		if ((ret > 0) && (buf[0] == '('))
 			return;
 
-		usleep(250000);
+		struct timespec delay = {0, 250e6}; nanosleep(&delay, NULL);
 	}
 
 	fatalx(EXIT_FAILURE, "Unable to detect a Best/SOLA or Phoenix protocol UPS");
@@ -333,7 +333,7 @@ void upsdrv_updateinfo(void)
 	}
 
 	/* these things need a long time to respond completely */
-	usleep(200000);
+	struct timespec delay = {0, 200e6}; nanosleep(&delay, NULL);
 
 	ret = ser_get_line(upsfd, buf, sizeof(buf), ENDCHAR, "", 
 		SER_WAIT_SEC, SER_WAIT_USEC);
Index: b/drivers/microdowell.c
===================================================================
--- a/drivers/microdowell.c	2011-08-17 00:40:17.000000000 +0300
+++ b/drivers/microdowell.c	2011-08-17 00:40:17.000000000 +0300
@@ -223,8 +223,9 @@ unsigned char * CmdSerial(unsigned char
 	/* The default error code (no received character) */
 	ErrCode = ERR_COM_NO_CHARS ;
 
-   SendCmdToSerial(OutBuffer, Len) ;
-	usleep(10000) ; /* small delay (1/100 s) */
+	SendCmdToSerial(OutBuffer, Len) ;
+	/* small delay (1/100 s) */
+	struct timespec delay = {0, 10e6}; nanosleep(&delay, NULL);
 
 	/* get chars until timeout */
 	BuffLen = 0 ;
@@ -305,7 +306,8 @@ static int detect_hardware(void)
 			{
 			upsdebugx(1, "[%d] get 'UPS model': %s", retries, PrintErr(ups.ErrCode));
 			upslogx(LOG_ERR, "[%d] Unable to identify UPS model [%s]", retries, PrintErr(ups.ErrCode));
-			usleep(100000) ; /* small delay (1/10 s) for next retry */
+			/* small delay (1/10 s) for next retry */
+			struct timespec delay = {0, 100e6}; nanosleep(&delay, NULL);
 			}
 		}
 
@@ -971,7 +973,8 @@ void upsdrv_initups(void)
 	ser_set_dtr(upsfd, 0);
 	ser_set_rts(upsfd, 0);
 
-	usleep(10000) ; /* small delay (1/100 s)) */
+	/* small delay (1/100 s) */
+	struct timespec delay = {0, 10e6}; nanosleep(&delay, NULL);
 }
 
 void upsdrv_cleanup(void)
Index: b/drivers/victronups.c
===================================================================
--- a/drivers/victronups.c	2011-05-31 13:36:49.000000000 +0300
+++ b/drivers/victronups.c	2011-08-17 03:09:10.000000000 +0300
@@ -49,8 +49,8 @@ upsdrv_info_t upsdrv_info = {
 #define ENDCHAR	'\r'
 #define IGNCHARS "\n"
 
-#define UPS_DELAY 150000
-#define UPS_LONG_DELAY 450000
+#define UPS_DELAY 150e6
+#define UPS_LONG_DELAY 450e6
 
 #define VICTRON_OVER 128
 #define VICTRON_RB 1
@@ -86,7 +86,7 @@ static int get_data (const char *out_str
 {
 	int ret_code;
 	ser_send(upsfd, "%s%c", out_string, ENDCHAR);
-	usleep (UPS_DELAY);
+	struct timespec delay = {0, UPS_DELAY}; nanosleep(&delay, NULL);
 	ret_code = ser_get_line(upsfd, in_string, LENGTH_TEMP, ENDCHAR, 
 				IGNCHARS, 3, 0);
 	if (ret_code < 1) {            
@@ -498,7 +498,7 @@ void upsdrv_updateinfo(void)
 void upsdrv_shutdown(void)
 {
 	ser_send(upsfd, "vCc0!%c", ENDCHAR);
-	usleep(UPS_DELAY);
+	struct timespec delay = {0, UPS_DELAY}; nanosleep(&delay, NULL);
 
 	ser_send(upsfd, "vCb%i!%c", sdwdelay, ENDCHAR); 
 }
@@ -536,15 +536,16 @@ void upsdrv_initups(void)
 	/* inicializace a synchronizace UPS */
 
 	ser_send_char(upsfd, ENDCHAR);
-	usleep (UPS_LONG_DELAY);
+	struct timespec delay = {0, UPS_LONG_DELAY}; nanosleep(&delay, NULL);
 	ser_send(upsfd, "?%c", ENDCHAR);
-	usleep (UPS_LONG_DELAY);
+	nanosleep(&delay, NULL);
 	ser_get_line(upsfd, temp, sizeof(temp), ENDCHAR, IGNCHARS, 3, 0);
 	ser_send(upsfd, "?%c", ENDCHAR);
-	usleep (UPS_LONG_DELAY);
+	nanosleep(&delay, NULL);
 	ser_get_line(upsfd, temp, sizeof(temp), ENDCHAR, IGNCHARS, 3, 0);
 	ser_send(upsfd, "?%c", ENDCHAR);
-	usleep (UPS_DELAY);
+	delay.tv_sec = 0; delay.tv_nsec = UPS_DELAY;
+	nanosleep(&delay, NULL);
 	ser_get_line(upsfd, temp, sizeof(temp), ENDCHAR, IGNCHARS, 3, 0);
 
 
Index: b/drivers/apcsmart.c
===================================================================
--- a/drivers/apcsmart.c	2011-05-31 13:36:49.000000000 +0300
+++ b/drivers/apcsmart.c	2011-08-17 03:06:07.000000000 +0300
@@ -822,7 +822,7 @@ static int sdcmd_CS(int tval)
 	if (tval & APC_STAT_OL) {
 		upsdebugx(1, "On-line - forcing OB temporarily");
 		ser_send_char(upsfd, 'U');
-		usleep(UPSDELAY);
+		struct timespec delay = {0, UPSDELAY}; nanosleep(&delay, NULL);
 	}
 	return sdcmd_S(tval);
 }
@@ -837,6 +837,7 @@ static int sdcmd_ATn(int cnt)
 	int n = 0, mmax, ret;
 	const char *strval;
 	char timer[4];
+	struct timespec delay;
 
 	mmax = cnt == 2 ? 99 : 999;
 
@@ -853,7 +854,8 @@ static int sdcmd_ATn(int cnt)
 	upsdebugx(1, "Issuing hard hibernate with %d minutes additional wakeup delay", n*6);
 
 	ser_send_char(upsfd, APC_CMD_GRACEDOWN);
-	usleep(CMDLONGDELAY);
+	delay.tv_sec = CMDLONGDELS; delay.tv_nsec = CMDLONGDELNS;
+	nanosleep(&delay, NULL);
 	ser_send_pace(upsfd, UPSDELAY, "%s", timer);
 
 	ret = sdok();
@@ -867,7 +869,8 @@ static int sdcmd_ATn(int cnt)
 	 * silent (YMMV);
 	 */
 	ser_send_char(upsfd, APC_CMD_GRACEDOWN);
-	usleep(UPSDELAY);
+	delay.tv_sec = 0; delay.tv_nsec = UPSDELAY;
+	nanosleep(&delay, NULL);
 	ser_flush_in(upsfd, IGNCHARS, nut_debug_level);
 
 	return 0;
@@ -880,7 +883,8 @@ static int sdcmd_K(int dummy)
 	upsdebugx(1, "Issuing delayed poweroff");
 
 	ser_send_char(upsfd, APC_CMD_SHUTDOWN);
-	usleep(CMDLONGDELAY);
+	struct timespec delay = {CMDLONGDELS, CMDLONGDELNS};
+	nanosleep(&delay, NULL);
 	ser_send_char(upsfd, APC_CMD_SHUTDOWN);
 
 	return sdok();
@@ -893,7 +897,8 @@ static int sdcmd_Z(int dummy)
 	upsdebugx(1, "Issuing immediate poweroff");
 
 	ser_send_char(upsfd, APC_CMD_OFF);
-	usleep(CMDLONGDELAY);
+	struct timespec delay = {CMDLONGDELS, CMDLONGDELNS};
+	nanosleep(&delay, NULL);
 	ser_send_char(upsfd, APC_CMD_OFF);
 
 	return sdok();
@@ -1229,7 +1234,7 @@ static int setvar_string(apc_vartab_t *v
 		return STAT_SET_HANDLED;	/* FUTURE: failed */
 	}
 
-	usleep(UPSDELAY);
+	struct timespec delay = {0, UPSDELAY}; nanosleep(&delay, NULL);
 
 	for (i = 0; i < strlen(val); i++) {
 		ret = ser_send_char(upsfd, val[i]);
@@ -1239,7 +1244,7 @@ static int setvar_string(apc_vartab_t *v
 			return STAT_SET_HANDLED;	/* FUTURE: failed */
 		}
 
-		usleep(UPSDELAY);
+		nanosleep(&delay, NULL);
 	}
 
 	/* pad to 8 chars with CRs */
@@ -1251,7 +1256,7 @@ static int setvar_string(apc_vartab_t *v
 			return STAT_SET_HANDLED;	/* FUTURE: failed */
 		}
 
-		usleep(UPSDELAY);
+		nanosleep(&delay, NULL);
 	}
 
 	ret = read_buf(temp, sizeof(temp));
@@ -1314,7 +1319,8 @@ static int do_cmd(apc_cmdtab_t *ct)
 
 	/* some commands have to be sent twice with a 1.5s gap */
 	if (ct->flags & APC_REPEAT) {
-		usleep(CMDLONGDELAY);
+		struct timespec delay = {CMDLONGDELS, CMDLONGDELNS};
+		nanosleep(&delay, NULL);
 
 		ret = ser_send_char(upsfd, ct->cmd);
 
Index: b/drivers/bcmxcp_ser.c
===================================================================
--- a/drivers/bcmxcp_ser.c	2011-05-31 13:36:49.000000000 +0300
+++ b/drivers/bcmxcp_ser.c	2011-08-17 03:21:04.000000000 +0300
@@ -49,7 +49,7 @@ static void send_command(unsigned char *
 
 		if (retry == PW_MAX_TRY) {
 			ser_send_char(upsfd, 0x1d);	/* last retry is preceded by a ESC.*/
-			usleep(250000);
+			struct timespec delay = {0, 250e6}; nanosleep(&delay, NULL);
 		}
 
 		sent = ser_send_buf(upsfd, sbuf, command_length);
@@ -255,6 +255,7 @@ void pw_comm_setup(const char *port)
 	unsigned char	id_command = PW_ID_BLOCK_REQ;
 	unsigned char	answer[256];
 	int		i = 0, baud, mybaud = 0, ret = -1;
+	struct timespec delay;
 
 	if (getval("baud_rate") != NULL)
 	{
@@ -273,12 +274,14 @@ void pw_comm_setup(const char *port)
 
 		ser_set_speed(upsfd, device_path, mybaud);
 		ser_send_char(upsfd, 0x1d);	/* send ESC to take it out of menu */
-		usleep(90000);
+		delay.tv_sec = 0; delay.tv_nsec = 90e6;
+		nanosleep(&delay, NULL);
 		send_write_command(AUT, 4);
-		usleep(500000);
+		delay.tv_sec = 0; delay.tv_nsec = 500e6;
+		nanosleep(&delay, NULL);
 		ret = command_sequence(&command, 1, answer);
 		if (ret <= 0) {
-			usleep(500000);
+			nanosleep(&delay, NULL);  /* 0.5 sec delay */
 			ret = command_sequence(&id_command, 1, answer);
 		}
 
@@ -296,12 +299,14 @@ void pw_comm_setup(const char *port)
 
 		ser_set_speed(upsfd, device_path, pw_baud_rates[i].rate);
 		ser_send_char(upsfd, 0x1d);	/* send ESC to take it out of menu */
-		usleep(90000);
+		delay.tv_sec = 0; delay.tv_nsec = 90e6;
+		nanosleep(&delay, NULL);
 		send_write_command(AUT, 4);
-		usleep(500000);
+		delay.tv_sec = 0; delay.tv_nsec = 500e6;
+		nanosleep(&delay, NULL);
 		ret = command_sequence(&command, 1, answer);
 		if (ret <= 0) {
-			usleep(500000);
+			nanosleep(&delay, NULL);  /* 0.5 sec delay */
 			ret = command_sequence(&id_command, 1, answer);
 		}
 
Index: b/drivers/belkin.c
===================================================================
--- a/drivers/belkin.c	2011-05-31 13:36:49.000000000 +0300
+++ b/drivers/belkin.c	2011-08-17 00:40:17.000000000 +0300
@@ -113,7 +113,7 @@ static int get_belkin_reply(char *buf)
 	long	cnt;
 	char	tmp[8];
 
-	usleep(25000);
+	struct timespec delay = {0, 25e6}; nanosleep(&delay, NULL);
 
 	/* pull first 7 bytes to get data length - like ~00D004 */
 	ret = ser_get_buf_len(upsfd, (unsigned char *)tmp, 7, 2, 0);
@@ -160,7 +160,7 @@ static int do_broken_rat(char *buf)
 	long	cnt;
 	char	tmp[8];
 
-	usleep(25000);
+	struct timespec delay = {0, 25e6}; nanosleep(&delay, NULL);
 
 	/* pull first 7 bytes to get data length - like ~00D004 */
 	ret = ser_get_buf_len(upsfd, (unsigned char *)tmp, 7, 2, 0);
@@ -412,7 +412,7 @@ static void do_off(void)
 
 	upslogx(LOG_INFO, "Sending powerdown command to UPS\n");
 	send_belkin_command(CONTROL,POWER_OFF,"1;1");
-	usleep(1500000);
+	struct timespec delay = {1, 500e6}; nanosleep(&delay, NULL);
 	send_belkin_command(CONTROL,POWER_OFF,"1;1");
 }
 
Index: b/drivers/belkinunv.c
===================================================================
--- a/drivers/belkinunv.c	2011-08-17 00:40:17.000000000 +0300
+++ b/drivers/belkinunv.c	2011-08-17 00:40:17.000000000 +0300
@@ -239,7 +239,7 @@ static void belkin_nut_open_tty(void)
 	   "smart" mode */
 	ser_set_dtr(upsfd, 0);
 	ser_set_rts(upsfd, 1);
-	sleep(1);
+	struct timespec delay = {1, 0}; nanosleep(&delay, NULL);
 
 	ser_flush_io(upsfd);
 }
@@ -495,7 +495,7 @@ static int belkin_std_open_tty(const cha
 	
 	/* sleep at least 0.25 seconds for the UPS to wake up. Belkin's own
 	   software sleeps 1 second, so that's what we do, too. */
-	usleep(1000000);
+	struct timespec delay = {1, 0}; nanosleep(&delay, NULL);
 	
 	/* flush incoming data again, and read any remaining garbage
 	   bytes. There should not be any. */
@@ -526,7 +526,7 @@ static int belkin_std_upsread(int fd, un
 		r = read(fd, &buf[count], n-count);
 		if (r==-1 && errno==EAGAIN) { 
 			/* non-blocking i/o, no data available */
-			usleep(100000);
+			struct timespec delay = {0, 100e6}; nanosleep(&delay, NULL);
 			tries++;
 		} else if (r == -1) {
 			return -1;
@@ -550,7 +550,7 @@ static int belkin_std_upswrite(int fd, u
 		r = write(fd, &buf[count], n-count);
 		if (r==-1 && errno==EAGAIN) { 
 			/* non-blocking i/o, no data available */
-			usleep(100000);
+			struct timespec delay = {0, 100e6}; nanosleep(&delay, NULL);
 			tries++;
 		} else if (r == -1) {
 			return -1;
@@ -752,6 +752,8 @@ static int belkin_wait(void)
 	int r;
 	int bs, ov, bl, st;
 
+	struct timespec delay = {1, 0};
+
 	/* read command line '-x' options */
 	val = getval("wait");
 	if (val) {
@@ -790,7 +792,7 @@ static int belkin_wait(void)
 		if (fd == -1) {
 			failcount++;
 			failerrno = errno;
-			sleep(1);
+			nanosleep(&delay, NULL);
 			continue;
 		}
 
@@ -802,7 +804,7 @@ static int belkin_wait(void)
 			failerrno = errno;
 			close(fd);
 			fd = -1;
-			sleep(1);
+			nanosleep(&delay, NULL);
 			continue;
 		}
 		ov = belkin_std_read_int(fd, REG_OUTPUTVOLT); /* output voltage */
@@ -811,7 +813,7 @@ static int belkin_wait(void)
 			failerrno = errno;
 			close(fd);
 			fd = -1;
-			sleep(1);
+			nanosleep(&delay, NULL);
 			continue;
 		}
 		bl = belkin_std_read_int(fd, REG_BATLEVEL);   /* battery level */
@@ -820,7 +822,7 @@ static int belkin_wait(void)
 			failerrno = errno;
 			close(fd);
 			fd = -1;
-			sleep(1);
+			nanosleep(&delay, NULL);
 			continue;
 		}
 		/* successfully got data from UPS */
@@ -837,7 +839,7 @@ static int belkin_wait(void)
 		if (st == ST_ONLINE && bl >= level) {
 			break;
 		}
-		sleep(1);
+		nanosleep(&delay, NULL);
 	}
 
 	/* termination condition reached */
Index: b/drivers/bestfcom.c
===================================================================
--- a/drivers/bestfcom.c	2011-08-17 00:40:17.000000000 +0300
+++ b/drivers/bestfcom.c	2011-08-17 00:40:17.000000000 +0300
@@ -210,7 +210,7 @@ static int execute(const char *cmd, char
 	ser_send(upsfd, "%s", cmd);
 
 	/* Give the UPS some time to chew on what we just sent */
-	usleep(50000);
+	struct timespec delay = {0, 50e6}; nanosleep(&delay, NULL);
 
 	/* delete command echo up to \012 but no further */
 	for (ch = '\0'; ch != '\012'; ser_get_char(upsfd, &ch, 0, 10000));
@@ -458,18 +458,19 @@ void upsdrv_help(void)
 
 static void sync_serial(void) {
 	char buffer[10];
+	struct timespec delay = {UPSDELAY, 0};
 
 	ser_flush_in(upsfd, "", 1);
 
 	ser_send(upsfd, "\r");
-	sleep(UPSDELAY);
+	nanosleep(&delay, NULL);
 	ser_get_line(upsfd, buffer, sizeof(buffer), '\r', "\012", 3, 0);
 	ser_get_line(upsfd, buffer, sizeof(buffer), ENDCHAR, IGNCHARS, 3, 0);
 
 	while (ser_get_line(upsfd, buffer, sizeof(buffer), '>', "\012", 3, 0) <= 0) {
 		printf(".");
 		ser_send(upsfd, "\r");
-		sleep(UPSDELAY);
+		nanosleep(&delay, NULL);
 	}
 }
 
@@ -537,7 +538,7 @@ void upsdrv_init_nofc(void)
 	ser_send(upsfd, "id\r");
 
 	/* prevent upsrecv from timing out	*/
-	sleep(UPSDELAY);
+	struct timespec delay = {UPSDELAY, 0}; nanosleep(&delay, NULL);
 
 	ser_get_line(upsfd, rstring, sizeof(rstring), '>', "", 3, 0);
 
Index: b/drivers/bestfortress.c
===================================================================
--- a/drivers/bestfortress.c	2011-05-31 13:36:49.000000000 +0300
+++ b/drivers/bestfortress.c	2011-08-17 00:40:17.000000000 +0300
@@ -23,7 +23,7 @@
 #include "main.h"
 #include "serial.h"
 
-#define UPSDELAY 50000	/* 50 ms delay required for reliable operation */
+#define UPSDELAY 50e6	/* 50e6 ns delay required for reliable operation */
 #define SER_WAIT_SEC	2	/* allow 2.0 sec for ser_get calls */
 #define SER_WAIT_USEC	0
 #define ENDCHAR		'\r'
@@ -162,7 +162,7 @@ static int upssend(const char *fmt,...)
 	char buf[1024], *p;
 	va_list	ap;
 	unsigned int	sent = 0;
-	int d_usec = UPSDELAY;
+	struct timespec d_nsec = {0, UPSDELAY};
 
 	va_start(ap, fmt);
 	ret = vsnprintf(buf, sizeof(buf), fmt, ap);
@@ -175,8 +175,7 @@ static int upssend(const char *fmt,...)
 		if (write(upsfd, p, 1) != 1)
 			return -1;
 
-		if (d_usec)
-			usleep(d_usec);
+		nanosleep(&d_nsec, NULL);
 
 		sent++;
 	}
Index: b/drivers/blazer_ser.c
===================================================================
--- a/drivers/blazer_ser.c	2011-05-31 13:36:49.000000000 +0300
+++ b/drivers/blazer_ser.c	2011-08-17 00:40:17.000000000 +0300
@@ -179,7 +179,7 @@ void upsdrv_initups(void)
 	/*
 	 * Allow some time to settle for the cablepower
 	 */
-	usleep(100000);
+	struct timespec delay = {0, 100e6}; nanosleep(&delay, NULL);
 #endif
 	blazer_initups();
 }
Index: b/drivers/isbmex.c
===================================================================
--- a/drivers/isbmex.c	2011-08-17 00:40:17.000000000 +0300
+++ b/drivers/isbmex.c	2011-08-17 00:40:17.000000000 +0300
@@ -180,7 +180,7 @@ static const char *getpacket(int *we_kno
   if (bytes_per_packet && r < bytes_per_packet){
 	     ssize_t rr;
 	     D(printf("short read...\n");)
-	     usleep(500000);
+	     struct timespec delay = {0, 500e6}; nanosleep(&delay, NULL);
              tv.tv_sec = 2;
              tv.tv_usec = 0;
              ret=select(upsfd+1,  &readfds, NULL, NULL, &tv);
@@ -327,7 +327,7 @@ void upsdrv_shutdown(void)
 	for(i=0;i<=5;i++)
 	{
 		ser_send_char(upsfd, '#');
-		usleep(50000);
+		struct timespec delay = {0, 50e6}; nanosleep(&delay, NULL);
 	}
 }
 
Index: b/drivers/mge-utalk.c
===================================================================
--- a/drivers/mge-utalk.c	2011-05-31 13:36:49.000000000 +0300
+++ b/drivers/mge-utalk.c	2011-08-17 03:26:12.000000000 +0300
@@ -89,7 +89,7 @@ upsdrv_info_t upsdrv_info = {
 #define MGE_REPLY_DELAY 1000
 
 /* delay after enable_ups_comm    */
-#define MGE_CONNECT_DELAY 500000
+#define MGE_CONNECT_DELAY 500e6
 
 #define MGE_COMMAND_ENDCHAR "\r\n"   /* some UPS need \r and \n */
 #define MGE_REPLY_ENDCHAR   '\r'
@@ -644,7 +644,7 @@ static void disable_ups_comm(void)
 {
 	upsdebugx(1, "disable_ups_comm()");
 	ser_flush_in(upsfd, "?\r\n", 0);
-	usleep(MGE_CONNECT_DELAY);
+	struct timespec delay = {0, MGE_CONNECT_DELAY}; nanosleep(&delay, NULL);
 	mge_command(NULL, 0, "Ax 0");
 }
 
@@ -659,7 +659,7 @@ static void enable_ups_comm(void)
 		mge_command(NULL, 0, "Z");   /* send Z twice --- speeds up re-connect */
 		mge_command(NULL, 0, "Z");
 		mge_command(NULL, 0, "Ax 1");
-		usleep(MGE_CONNECT_DELAY);
+		struct timespec delay = {0, MGE_CONNECT_DELAY}; nanosleep(&delay, NULL);
 	}
 	
 	ser_flush_in(upsfd, "?\r\n", nut_debug_level);
@@ -863,6 +863,7 @@ static int mge_command(char *reply, int
 	int bytes_rcvd = 0;
 	int ret;
 	va_list ap;
+	struct timespec delay;
 
 	/* build command string */
 	va_start(ap, fmt);
@@ -875,11 +876,13 @@ static int mge_command(char *reply, int
 	va_end(ap);
 
 	/* Delay a bit to avoid overlap of a previous answer */
-	usleep(100000);
+	delay.tv_sec = 0; delay.tv_nsec = 100e6;
+	nanosleep(&delay, NULL);
 	
 	/* flush received, unread data */
 	tcflush(upsfd, TCIFLUSH);
 	
+	delay.tv_sec = 0; delay.tv_nsec = MGE_CHAR_DELAY;
 	/* send command */
 	for (p = command; *p; p++) {
 		if ( isprint(*p & 0xFF) )
@@ -891,7 +894,7 @@ static int mge_command(char *reply, int
 			return -1;
 	
 		bytes_sent++;
-		usleep(MGE_CHAR_DELAY);
+		nanosleep(&delay, NULL);  /* MGE_CHAR_DELAY */
 	}
 
 	/* send terminating string */
@@ -906,14 +909,14 @@ static int mge_command(char *reply, int
 				return -1;
 
 			bytes_sent++;
-			usleep(MGE_CHAR_DELAY);
+			nanosleep(&delay, NULL);  /* MGE_CHAR_DELAY */
 		}
 	}
 
 	if ( !reply )
 		return bytes_rcvd;
 	else
-		usleep(MGE_REPLY_DELAY);
+		nanosleep(&delay, NULL);  /* MGE_CHAR_DELAY */
 
 	bytes_rcvd = ser_get_line(upsfd, reply, replylen,
 		MGE_REPLY_ENDCHAR, MGE_REPLY_IGNCHAR, 3, 0);
Index: b/drivers/optiups.c
===================================================================
--- a/drivers/optiups.c	2011-05-31 13:36:49.000000000 +0300
+++ b/drivers/optiups.c	2011-08-17 00:40:17.000000000 +0300
@@ -133,7 +133,7 @@ static ezfill_t _initv[] = {
 static inline int optireadline(void)
 {
 	int r;
-	usleep(150000);
+	struct timespec delay = {0, 150e6}; nanosleep(&delay, NULL);
 	r = ser_get_line(upsfd, _buf, sizeof(_buf), ENDCHAR, IGNCHARS, 0, 500000 );
 	_buf[sizeof(_buf)-1] = 0;
 	if ( r > 0 )
Index: b/drivers/powerp-bin.c
===================================================================
--- a/drivers/powerp-bin.c	2011-05-31 13:36:49.000000000 +0300
+++ b/drivers/powerp-bin.c	2011-08-17 00:40:17.000000000 +0300
@@ -232,7 +232,7 @@ static int powpan_command(const char *bu
 
 	upsdebug_hex(3, "send", buf, bufsize);
 
-	usleep(100000);
+	struct timespec delay = {0, 100e6}; nanosleep(&delay, NULL);
 
 	ret = ser_get_buf_len(upsfd, powpan_answer, bufsize-1, SER_WAIT_SEC, SER_WAIT_USEC);
 
@@ -427,7 +427,7 @@ static int powpan_status(status_t *statu
 
 	upsdebug_hex(3, "send", "D\r", 2);
 
-	usleep(200000);
+	struct timespec delay = {0, 200e6}; nanosleep(&delay, NULL);
 
 	ret = ser_get_buf_len(upsfd, status, sizeof(*status), SER_WAIT_SEC, SER_WAIT_USEC);
 
@@ -576,7 +576,7 @@ static int powpan_initups(void)
 
 		upsdebug_hex(3, "send", "F\r", 2);
 
-		usleep(200000);
+		struct timespec delay = {0, 200e6}; nanosleep(&delay, NULL);
 
 		ret = ser_get_line(upsfd, powpan_answer, sizeof(powpan_answer),
 			ENDCHAR, IGNCHAR, SER_WAIT_SEC, SER_WAIT_USEC);
Index: b/drivers/powerp-txt.c
===================================================================
--- a/drivers/powerp-txt.c	2011-05-31 13:36:49.000000000 +0300
+++ b/drivers/powerp-txt.c	2011-08-17 00:40:17.000000000 +0300
@@ -92,7 +92,7 @@ static int powpan_command(const char *co
 
 	upsdebug_hex(3, "send", command, strlen(command));
 
-	usleep(100000);
+	struct timespec delay = {0, 100e6}; nanosleep(&delay, NULL);
 
 	ret = ser_get_line(upsfd, powpan_answer, sizeof(powpan_answer),
 		ENDCHAR, IGNCHAR, SER_WAIT_SEC, SER_WAIT_USEC);
@@ -379,7 +379,7 @@ static int powpan_status(status_t *statu
 
 	upsdebug_hex(3, "send", "D\r", 2);
 
-	usleep(200000);
+	struct timespec delay = {0, 200e6}; nanosleep(&delay, NULL);
 
 	ret = ser_get_buf_len(upsfd, powpan_answer, 35, SER_WAIT_SEC, SER_WAIT_USEC);
 
Index: b/drivers/rhino.c
===================================================================
--- a/drivers/rhino.c	2011-08-17 00:40:17.000000000 +0300
+++ b/drivers/rhino.c	2011-08-17 00:40:17.000000000 +0300
@@ -46,7 +46,7 @@ upsdrv_info_t upsdrv_info = {
 	{ NULL }
 };
 
-#define UPSDELAY 500 /* 0.5 ms delay */
+#define UPSDELAY 500e3 /* 500e3 ns delay */
 
 typedef int bool_t;
 
@@ -514,10 +514,10 @@ send_command( int cmd )
       for(i=0; i < 19; i++)
 	{
 	  ret = ser_send_char( upsfd, psend[i] );
-	  /* usleep ( UPSDELAY ); sending without delay */
+	  /* sending without delay */
 	}
       
-      usleep( UPSDELAY ); /* delay between sent command */
+      struct timespec delay = {0, UPSDELAY}; nanosleep(&delay, NULL); /* delay between sent command */
 
       kount++;
     }
@@ -530,10 +530,9 @@ send_command( int cmd )
 static void sendshut( void )
 {
 
-	int i;
+	struct timespec delay = {15, 0};
 
-	for(i=0; i < 30000; i++)
-	  usleep( UPSDELAY ); /* 15 seconds delay */
+	nanosleep(&delay, NULL); /* 15 seconds delay */
 
 	send_command( CMD_SHUT );
 	upslogx(LOG_NOTICE, "Ups shutdown command sent");
Index: b/drivers/tripplite_usb.c
===================================================================
--- a/drivers/tripplite_usb.c	2011-05-31 13:36:49.000000000 +0300
+++ b/drivers/tripplite_usb.c	2011-08-17 00:40:17.000000000 +0300
@@ -680,7 +680,8 @@ static int send_cmd(const unsigned char
 		}
 
 #if ! defined(__FreeBSD__)
-		if(!done) { usleep(1000*100); /* TODO: nanosleep */ }
+		struct timespec delay = {0, 100e6};
+		if(!done) nanosleep(&delay, NULL);
 #endif
 
 		for(recv_try=0; !done && recv_try < MAX_RECV_TRIES; recv_try++) {
Index: b/drivers/apcsmart.h
===================================================================
--- a/drivers/apcsmart.h	2011-05-31 13:36:49.000000000 +0300
+++ b/drivers/apcsmart.h	2011-08-17 01:15:16.000000000 +0300
@@ -40,8 +40,10 @@
 /* alert characters we care about - OL, OB, LB, not LB, RB, OVER, not OVER */
 #define POLL_ALERT "$!%+#?="
 
-#define UPSDELAY	  50000	/* slow down multicharacter commands        */
-#define CMDLONGDELAY	1500000	/* some commands need a 1.5s gap for safety */
+#define UPSDELAY	   50e6	/* slow down multicharacter commands */
+/* some commands need a CoManD LONG DELay of 1.5s gap for safety */
+#define CMDLONGDELS	1       /* 1 sec out of 1.5s             */
+#define CMDLONGDELNS	  500e6 /* 500e6 nsec out of 1.5s        */
 
 #define SER_WAIT_SEC	3	/* wait up to 3.0 sec for ser_get calls */
 #define SER_WAIT_USEC	0




More information about the Nut-upsdev mailing list