aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: c71c872dc44ab9762bde439ab89ce04104f15157 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>


#include <linux/i2c-dev.h>
#include <sys/ioctl.h>

#include "lm75.h"
#include "hih61xx.h"
 
extern void lm75_list_all(void);
extern void lm75_read_all(const char *opts);
extern void lm75_read_one(const char *opts);
extern void hih61xx_list_all(void);
extern void hih61xx_read_all(const char *opts); 
extern void hih61xx_read_one(const char *opts);


int file;
void (*list_all)(void) = NULL;
void (*read_all)(const char *opts) = NULL;
void (*read_one)(const char *opts) = NULL;


void print_help(void) 
{
    printf ("Usage:\n"
            "   i2sensor <op> <bus> <dev_type> <dev_addr> (<options>)\n"
            "Aviable options: <op>\n"
            "   list_all -- list all measured parameters off device\n"
            "   read_all -- read all measured parameters from device\n"
            "   read_one -- read the selected parameters from device\n"
            "Exmples:\n"
            "   i2sensor read_all <bus> <device> <address> <offset_for_param-01,offset_for_param-02,...>\n"
            "   i2sensor read_one <bus> <device> <address> <param-id,offset_for_param-id>\n"
            "   i2sensor read_all 1 lm75 0x4f -1.35,\n"
            "   i2sensor read_one 1 lm75 0x4f  01,-1.35\n"
            "" "\n");
}

void bus_err(int ern)
{
    printf("I2C communication(rd) error. errno: %d\n",errno);
}

void preinit(const char *dev_type)
{
    if(!strcmp("lm75", dev_type))
    {
        list_all = lm75_list_all;
        read_all = lm75_read_all;
        read_one = lm75_read_one;
    }

    else if(!strcmp("hih61xx", dev_type)) 
    {                            
        list_all = hih61xx_list_all;
        read_all = hih61xx_read_all;
        read_one = hih61xx_read_one;
    }
}



int main(int argc, char *argv[])
{
    
// >> i2sensor <op> <bus> <dev_typ> <dev_addr> (<others>) 
// >> i2sensor read_all 0 lm75 4f 1,-1.0;2,+0.2;3,+10;4,-2
//    read_all, list_all, 
//    ...read_all...<dev_addr>, <param_id,offs,param_id,offs>    
//    read_one...<dev_addr>, <param_id,offs>    
//    ...read_reg_hex... <dev_addr>, <reg_addr,b/w/dw> 
//    ...read_reg_dec... <dev_addr>, <reg_addr,b/w/dw>
//    ...set_param... <dev_addr>, <param_id,value_dec>
//    ...set_reg... <dev_addr>, <reg_addr,value_hex>    
    

    if(4 < argc && 7 > argc)  //Minimal 5 argument passed
    {
        
        char filename[32];
        int i;
        preinit(argv[3]);

        for(i=0;i<strlen(argv[2]);i++)  //Some test on bus address, for safety.
        {
            if(!isdigit(argv[2][i]))
            {
                printf("\n\tThe BUS Address must be an integer\n\n");
                print_help();
                exit (EXIT_FAILURE);
            }
        }
  
        snprintf(filename, 31, "/dev/i2c-%s", argv[2]);   //Open i2c bus
        file = open(filename, O_RDWR);
    
        if (file < 0) 
        {
            printf("\n\tCan't open i2c BUS, are you root?\n\n");    
            print_help();
            exit (EXIT_FAILURE);
        }


        if(argv[4][0]=='0' && argv[4][1]=='x') //?
        {
      
            int addr = (int)strtol(argv[4], NULL, 0);       //Open i2c device

            if (ioctl(file, I2C_SLAVE, addr) < 0) 
            {
                printf("\n\tCan't open i2c DEVICE, errno: %d\n\n",errno);
                print_help();
                exit (EXIT_FAILURE);
            }
        }
        else
        {
            printf("\n\tThe DEVICE address must be a hexadecimal number\n\n");
            print_help();
            exit (EXIT_FAILURE);
        }
        
        
        
        if(!strcmp("list_all",argv[1]) && list_all != NULL)        // If list_all is selected.
        {
            list_all();
        }
        else if(!strcmp("read_all",argv[1]) && read_all != NULL)
        {
            read_all(argv[5]);
        }
        else if(!strcmp("read_one",argv[1]) && read_one != NULL && argc == 5)
        {
            read_one(argv[5]);
        }
        else
        {
           // print_help();
        }
    
	close(file);
  
  }
  else
  {
    print_help();
  }
  return 0;
}