~ubuntu-branches/ubuntu/saucy/nut/saucy

« back to all changes in this revision

Viewing changes to drivers/tripplitesu.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2005-07-20 19:48:50 UTC
  • mto: (16.1.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20050720194850-oo61wjr33rrx2mre
Tags: upstream-2.0.2
ImportĀ upstreamĀ versionĀ 2.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
124
124
 
125
125
 
126
126
#include "main.h"
 
127
#include "serial.h"
127
128
 
128
 
#define DRV_VERSION     "0.01"
 
129
#define DRV_VERSION     "0.02"
129
130
 
130
131
#define MAX_RESPONSE_LENGTH 256
131
132
 
132
 
static char *test_result_names[] = {
 
133
static const char *test_result_names[] = {
133
134
        "No test performed",
134
135
        "Passed",
135
136
        "In progress",
141
142
 
142
143
static struct {
143
144
        int code;
144
 
        char *name;
 
145
        const char *name;
145
146
} sensitivity[] = {
146
147
        {0, "Normal"},
147
148
        {1, "Reduced"},
214
215
        char *ptr;
215
216
 
216
217
        count = 0;
217
 
        upsflushin(0, nut_debug_level, "");
 
218
        ser_flush_in(upsfd, "", nut_debug_level);
218
219
 
219
 
        if (upssend("~00%c%03d%s%s", type,
 
220
        if (ser_send(upsfd, "~00%c%03d%s%s", type,
220
221
                    strlen(command) + strlen(parameters), command,
221
222
                    parameters) <= 0)
222
223
                return -1;
223
224
 
224
 
        if (upsrecvchars(buffer, 4) <= 0)
 
225
        if (ser_get_buf_len(upsfd, buffer, 4, 3, 0) <= 0)
225
226
                return -1;
226
227
        buffer[4] = '\0';
227
228
 
228
229
        if (!strcmp(buffer, "~00D")) {
229
 
                if (upsrecvchars(buffer, 3) <= 0)
 
230
                if (ser_get_buf_len(upsfd, buffer, 4, 3, 0) <= 0)
230
231
                        return -1;
231
232
                buffer[3] = '\0';
232
233
                count = atoi(buffer);
238
239
                        return 0;
239
240
                }
240
241
 
241
 
                if (upsrecvchars(response, count) <= 0)
 
242
                if (ser_get_buf_len(upsfd, response, count, 3, 0) <= 0)
242
243
                        return -1;
243
244
                response[count] = '\0';
244
245
                /* Tripp Lite pads their string responses with spaces.
249
250
                while (ptr > response && *ptr == ' ')
250
251
                        ptr--;
251
252
                *(ptr + 1) = '\0';
252
 
                nolongertimeout();
 
253
                ser_comm_good();
253
254
                return count;
254
255
        }
255
256
        if (!strcmp(buffer, "~00A")) {
256
257
                if (response)
257
258
                        *response = '\0';
258
 
                nolongertimeout();
 
259
                ser_comm_good();
259
260
                return count;
260
261
        }
261
262
        if (!strcmp(buffer, "~00R"))
262
 
                nolongertimeout();
 
263
                ser_comm_good();
263
264
 
264
265
        return -1;
265
266
}
266
267
 
267
 
static char *field(char *str, int field)
 
268
static char *field(char *str, int fieldnum)
268
269
{
269
270
 
270
 
        while (str && field--) {
 
271
        while (str && fieldnum--) {
271
272
                str = strchr(str, ';');
272
273
                if (str)
273
274
                        str++;
367
368
 
368
369
static int get_sensitivity(void) {
369
370
        char response[MAX_RESPONSE_LENGTH];
370
 
        int i;
 
371
        unsigned int i;
371
372
 
372
373
        if (do_command(POLL, VOLTAGE_SENSITIVITY, "", response) <= 0)
373
374
                return 0;
384
385
 
385
386
static void set_sensitivity(const char *val) {
386
387
        char parm[20];
387
 
        int i;
 
388
        unsigned int i;
388
389
 
389
390
        for (i = 0; i < sizeof(sensitivity) / sizeof(sensitivity[0]); i++) {
390
391
                if (!strcasecmp(val, sensitivity[i].name)) {
526
527
void upsdrv_initinfo(void)
527
528
{
528
529
        char response[MAX_RESPONSE_LENGTH];
529
 
        int min_low_transfer, max_low_transfer;
530
 
        int min_high_transfer, max_high_transfer;
531
 
        int i;
 
530
        unsigned int min_low_transfer, max_low_transfer;
 
531
        unsigned int min_high_transfer, max_high_transfer;
 
532
        unsigned int i;
532
533
        char *ptr;
533
534
 
534
535
        if (!init_comm())
615
616
        /* add all the variables that change regularly */
616
617
        upsdrv_updateinfo();
617
618
 
618
 
        upsh.new_instcmd = instcmd;
619
 
        upsh.new_setvar = setvar;
 
619
        upsh.instcmd = instcmd;
 
620
        upsh.setvar = setvar;
620
621
 
621
622
        printf("Detected %s %s on %s\n", dstate_getinfo("ups.mfr"),
622
623
               dstate_getinfo("ups.model"), device_path);
740
741
        }
741
742
 
742
743
        if (do_command(POLL, TEST_RESULT, "", response) > 0) {
743
 
                i = atoi(response);
744
 
                if (i < 0 || i >= sizeof(test_result_names) /
745
 
                                  sizeof(test_result_names[0]))
746
 
                        i = 0;
747
 
                dstate_setinfo("ups.test.result", "%s", test_result_names[i]);
 
744
                int     r;
 
745
                size_t  trsize;
 
746
 
 
747
                r = atoi(response);
 
748
                trsize = sizeof(test_result_names) / 
 
749
                        sizeof(test_result_names[0]);
 
750
 
 
751
                if ((r < 0) || (r >= (int) trsize))
 
752
                        r = 0;
 
753
 
 
754
                dstate_setinfo("ups.test.result", "%s", test_result_names[r]);
748
755
        }
749
756
 
750
757
        if (do_command(POLL, ENVIRONMENT_INFORMATION, "", response) > 0) {
808
815
 
809
816
void upsdrv_initups(void)
810
817
{
811
 
 
812
 
        open_serial(device_path, B2400);
813
 
        /* to get variables and flags from the command line, use this:
814
 
         *
815
 
         * first populate with upsdrv_buildvartable above, then...
816
 
         *
817
 
         *                   set flag foo : /bin/driver -x foo
818
 
         * set variable 'cable' to '1234' : /bin/driver -x cable=1234
819
 
         *
820
 
         * to test flag foo in your code:
821
 
         *
822
 
         *      if (testvar("foo"))
823
 
         *              do_something();
824
 
         *
825
 
         * to show the value of cable:
826
 
         *
827
 
         *      if ((cable == getval("cable")))
828
 
         *              printf("cable is set to %s\n", getval("cable"));
829
 
         *      else
830
 
         *              printf("cable is not set!\n");
831
 
         *
832
 
         * don't use NULL pointers - test the return result first!
833
 
         */
 
818
        upsfd = ser_open(device_path);
 
819
        ser_set_speed(upsfd, device_path, B2400);
834
820
}
835
821
 
836
822
void upsdrv_cleanup(void)
837
823
{
838
 
        /* free(dynamic_mem); */
 
824
        ser_close(upsfd, device_path);
839
825
}