Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First commit of NHS drivers/nhs-nut.c #2692

Merged
merged 61 commits into from
Dec 4, 2024

Conversation

Challado
Copy link

@Challado Challado commented Nov 25, 2024

First commit of driver NHS-NUT for NHS senoidal lines.
Signed-off-by: Lucas Willian Bocchi [email protected]

MAINTAINER UPDATE: Started from mailing list submission at https://alioth-lists.debian.net/pipermail/nut-upsdev/2024-November/008046.html

Lucas Bocchi added 2 commits November 25, 2024 20:16
@jimklimov
Copy link
Member

MAINTAINER NOTE: The initial driver code has some Linux-specific bits and can get a number of portability complaints from the NUT CI farm agents running on other platforms. This will be addressed later as the PR evolves to get merged.

Copy link
Member

@jimklimov jimklimov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First of all, thanks for a solid-looking driver that yields a lot of info. As a stand-alone program, it looks mostly good (some spots with C memory handling are commented, e.g. with upsdata returns).

Most of the requested fixes (maybe I'll handle at least some of those notes myself) would be required to ensure portability of this driver to other platforms than yours, and to improve maintainability by using more of common NUT methods and approaches - probably including the serial-port communications.

Some general concerns are about which logical code landed into which standard methods for NUT driver structure, as documented at docs/new-drivers.txt - fixes there would be needed, but probably simple ones, moving blocks of code into suitably named methods from wrong current locations.

Also a NEWS.adoc entry to announce the driver, and data/driver.list.in HCL entry (or several) to suggest use of this driver for some NHS device name(s), would be useful. You have a lot of names and VA values mapped in a method - perhaps these names can become the first pass of HCL data enhancement.

Also needs a manual page to describe the supported devices and the driver options (per addvar() in code) and maybe other nuances - known caveats, etc. Ideally this comes from a driver author, as they best know if there is anything special to know about the devices.

Finally, as "naming is one of the hardest problems in IT" (citation needed), nhs-nut would need to be changed, I think. There was a guideline (alas, mostly overlooked) to name new drivers as nutdrv_something to help identify them in process listing, and there are several precedents for that. There is also one nut-*ipmi* set of files. But none with nut in the middle/end...
I think following the precedent of bcmxcp, blazer, riello or sms drivers suffuxed with _ser and/or _usb for different media, e.g. drivers/sms_ser.c - this one deserves to be nhs_ser.

#include "nut_stdint.h"
#include "serial.h"
#include <stdio.h>
#include <linux/serial.h>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably some NUT CI agents (and end-user non-Linux platforms) will complain about this one.

Generally this can be revised as needed, by comparing to other serial-media drivers (using methods in drivers/serial.c etc.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UPDATE: As found later, initial code relies on struct serial_struct and TIOCSSERIAL which are only defined there. So platform expansion would need finding equivalents for whatever those bits solved.

}

void upsdrv_initups(void) {
upsdebugx(1,"Start initups()");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

upsdrv_initups() should take care of hardware communications init (maybe fail with fatalx() if port is not accessible).

The upsdrv_initinfo() method is called later to detect the device details (maybe fail with fatalx() if no compatible device was found) and set write-once values like device model etc. and populate the list of supported commands and/or settable variables, if any.

Please do check docs/new-drivers.txt for general driver code structure overview.

strcpy(porta,DEFAULTPORT);
else
strcpy(porta,device_path);
serial_fd = openfd(porta,baudrate);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

upsdrv_initups() should take care of hardware communications init (maybe fail with fatalx() if port is not accessible).

The upsdrv_initinfo() method is called later to detect the device details (maybe fail with fatalx() if no compatible device was found) and set write-once values like device model etc. and populate the list of supported commands and/or settable variables, if any.

Please do check docs/new-drivers.txt for general driver code structure overview.

@jimklimov jimklimov added feature serial port portability We want NUT to build and run everywhere possible labels Nov 26, 2024
@jimklimov jimklimov added this to the 2.8.3 milestone Nov 26, 2024
Lucas Bocchi and others added 15 commits November 26, 2024 11:44
1) Miscalculations of battery voltage and amps
2) Miscalculations in battery capacity
3) Miscalculations in time remaining
Signed-off-by: Lucas Willian Bocchi [email protected]
        nhs-nut.c: In function ‘print_pkt_data’:
        nhs-nut.c:464:17: error: zero-length gnu_printf format string [-Werror=format-zero-length]
          464 |     upsdebugx(1,"");
              |                 ^~

Signed-off-by: Jim Klimov <[email protected]>
        nhs-nut.c:604:59: error: unused parameter ‘size’ [-Werror=unused-parameter]
          604 | pkt_data mount_datapacket(unsigned char * datapacket, int size, double tempodecorrido,pkt_hwinfo pkt_upsinfo)  {
              |                                                       ~~~~^~~~
        nhs-nut.c:604:72: error: unused parameter ‘tempodecorrido’ [-Werror=unused-parameter]
          604 | pkt_data mount_datapacket(unsigned char * datapacket, int size, double tempodecorrido,pkt_hwinfo pkt_upsinfo)  {
              |                                                                 ~~~~~~~^~~~~~~~~~~~~~

Signed-off-by: Jim Klimov <[email protected]>
        nhs-nut.c:713:43: error: suggest braces around empty body in an ‘if’ statement [-Werror=empty-body]
          713 |             if (pktdata.perc_output > 100);
              |                                           ^
        nhs-nut.c:713:13: error: this ‘if’ clause does not guard... [-Werror=misleading-indentation]
          713 |             if (pktdata.perc_output > 100);
              |             ^~
        nhs-nut.c:714:17: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘if’
          714 |                 pktdata.perc_output = 100;
              |                 ^~~~~~~

Signed-off-by: Jim Klimov <[email protected]>
        nhs-nut.c:797:29: error: iteration 6 invokes undefined behavior [-Werror=aggressive-loop-optimizations]
          797 |         pkthwinfo.status[i] = get_bit_in_position(&datapacket[14],sizeof(datapacket[14]),i,0);
              |         ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Signed-off-by: Jim Klimov <[email protected]>
        nhs-nut.c: In function ‘upsdrv_updateinfo’:
        nhs-nut.c:1733:53: error: format not a string literal and no format arguments [-Werror=format-security]
         1733 |                                         upsdebugx(1,alarm);
              |                                                     ^~~~~

Signed-off-by: Jim Klimov <[email protected]>
        nhs-nut.c: In function ‘upsdrv_makevartable’:
        nhs-nut.c:2090:88: error: format ‘%f’ expects argument of type ‘double’, but argument 3 has type ‘int’ [-Werror=format=]
         2090 |     sprintf(help,"Voltage In Percentage to calculate warning low level. Default is %0.2f",DEFAULLTPERC);
              |                                                                                    ~~~~^
              |                                                                                        |
              |                                                                                        double
              |                                                                                    %0.2d

etc. for other default macros

Signed-off-by: Jim Klimov <[email protected]>
        nhs-nut.c: In function ‘get_bit_in_position’:
        nhs-nut.c:357:22: error: comparison of integer expressions of different signedness: ‘int’ and ‘size_t’ {aka ‘long unsigned int’} [-Werror=sign-compare]
          357 |     if (bit_position >= size * 8) {
              |                      ^~

Signed-off-by: Jim Klimov <[email protected]>
Copy-paste typo?

        nhs-nut.c: In function ‘mount_datapacket’:
        nhs-nut.c:693:29: error: operation on ‘pktdata.icarregrms_real’ may be undefined [-Werror=sequence-point]
          693 |     pktdata.icarregrms_real = pktdata.icarregrms_real = pktdata.icarregrms * 30;
              |     ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Signed-off-by: Jim Klimov <[email protected]>
        nhs-nut.c: In function ‘mount_hwinfo’:
        nhs-nut.c:786:5: error: missing initializer for field ‘end_marker’ of ‘pkt_hwinfo’ [-Werror=missing-field-initializers]
          786 |     };
              |     ^
        nhs-nut.c:177:18: note: ‘end_marker’ declared here
          177 |     unsigned int end_marker;
              |                  ^~~~~~~~~~

Signed-off-by: Jim Klimov <[email protected]>
@AppVeyorBot
Copy link

@jimklimov
Copy link
Member

jimklimov commented Nov 29, 2024

@Challado : note, large changes of raw C text have landed as NUT code style was applied and some comments reformatted.
Logging verbosity also revised. The nhs.* values moved to experimental.nhs.* namespace.

Overall, most (not all) of the initial review comments above got addressed and resolved.

It would be great if you can confirm that this code base state still builds and works for your device, before I get into some more significant refactoring to share some of your code with common serial.c/.h and vice versa, and/or shuffle it around the file's methods.

…_ser.txt, NEWS.adoc: temporarily constrain nhs_ser to Linux-only NUT builds [networkupstools#2692]

Signed-off-by: Jim Klimov <[email protected]>
@jimklimov jimklimov added the Linux Some issues are specific to Linux as a platform label Nov 29, 2024
@AppVeyorBot
Copy link

@jimklimov
Copy link
Member

jimklimov commented Nov 29, 2024

@Challado : upon the closer inspection, I found that you parse hwinfo details (ups.mfr etc.) for every run of upsdrv_updateinfo(). Is that information expected to change often/ever? Usually that is seeded in upsdrv_initinfo() once and left for the duration of the program. (Some devices and drivers, e.g. SNMP, have a notion of semi-static values - like admin contacts or device location - that are re-fetched rarely, because they can be changed out of band e.g. in Web-UI of that UPS).

@jimklimov
Copy link
Member

jimklimov commented Nov 29, 2024

NOTE: I've also constrained builds of this driver and its man page to happen by default just on Linux for now, and added respective wording to the NEWS.adoc and the man page itself. Hopefully git revert c857014d62c7e0a4a5c4675d78294386e1be0bb0 some time later would be all that is needed to restore its glory (in some other PR after merging this one, using common "contributor-owned" git workflow without rubbing elbows in someone else's repository/branch :) ).

I've checked that this arrangement does not preclude (cd drivers && make nhs_ser) attempts on those other systems, so experiments to extend the platform support should be easy to iterate.

@Challado
Copy link
Author

@Challado : note, large changes of raw C text have landed as NUT code style was applied and some comments reformatted. Logging verbosity also revised. The nhs.* values moved to experimental.nhs.* namespace.

Overall, most (not all) of the initial review comments above got addressed and resolved.

It would be great if you can confirm that this code base state still builds and works for your device, before I get into some more significant refactoring to share some of your code with common serial.c/.h and vice versa, and/or shuffle it around the file's methods.

Yep. Compiled and still working here in arm / amd4 platforms.

@jimklimov
Copy link
Member

NOTE: CI hiccups about openbsd are due to build agent reconfiguration.

@jimklimov jimklimov merged commit 8514451 into networkupstools:master Dec 4, 2024
30 checks passed
@jimklimov
Copy link
Member

Probably some clean-up remains e.g. to reshuffle code between methods, improve portability, etc. - but as a first code drop that passes CI this is "good enough" to not keep the PR indefinitely open.

jimklimov added a commit to jimklimov/nut that referenced this pull request Dec 9, 2024
jimklimov added a commit to jimklimov/nut that referenced this pull request Dec 9, 2024
…indent main code a bit [networkupstools#2692]

Fixed the retries from being an infinite loop along the way...

Signed-off-by: Jim Klimov <[email protected]>
jimklimov added a commit to jimklimov/nut that referenced this pull request Dec 9, 2024
…a-stale related messages and status changes [networkupstools#2692]

Signed-off-by: Jim Klimov <[email protected]>
jimklimov added a commit to jimklimov/nut that referenced this pull request Dec 9, 2024
jimklimov added a commit to jimklimov/nut that referenced this pull request Dec 9, 2024
jimklimov added a commit to jimklimov/nut that referenced this pull request Dec 9, 2024
jimklimov added a commit to jimklimov/nut that referenced this pull request Dec 9, 2024
…packet data interpretation; un-indent more code [networkupstools#2692]

Add range-check to not overflow the datapacket[] buffer.

Signed-off-by: Jim Klimov <[email protected]>
jimklimov added a commit to jimklimov/nut that referenced this pull request Dec 9, 2024
…winfo() and interpret_pkt_data() logic, separated by domain [networkupstools#2692]

Also rename some data points that do not conform to NUT standards,
or drop if redundant.

Signed-off-by: Jim Klimov <[email protected]>
jimklimov added a commit to jimklimov/nut that referenced this pull request Dec 9, 2024
…at and min_input_power in static vars [networkupstools#2692]

Signed-off-by: Jim Klimov <[email protected]>
jimklimov added a commit to jimklimov/nut that referenced this pull request Dec 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Linux Some issues are specific to Linux as a platform portability We want NUT to build and run everywhere possible serial port
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants