aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 26e79ba13c023326602481de8766813675334f94 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#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 <linux/i2c.h>
#include <i2c/smbus.h>
#include <sys/ioctl.h>
  
#include "deftypes.h"  
#include "lm75.h"
#include "hih61xx.h"
#include "hmc5883.h"

#define VERSION 1.28
#ifndef _BUILD_TIME_
    #define _BUILD_TIME_  __DATE__" "__TIME__ 
#endif

// extern void lm75_print_help(void);
// extern void lm75_read_all(const char *opts);
// extern void lm75_read_one(const char *opts);
// extern void hih61xx_print_help(void);
// extern void hih61xx_read_all(const char *opts); 
// extern void hih61xx_read_one(const char *opts);
    
// >> 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>    
    


__int16_t devicef=0;    //# Bus-Device file 
long addr=0, funcs=0;
//__uint8_t temp[256];
void (*print_all)(void) = NULL;
void (*read_all)(const uchar *opts) = NULL;
void (*read_one)(const uchar *opts) = NULL;
void (*conf_set)(const uchar *opts) = NULL;


void print_help(void) {  
    printf ("\n"
            "   i2sensors  <op> <bus> <dev_type> <dev_addr> (<options>)\n"
            "\n"
            "Aviable options: <op>\n"
            "   print_all   -- list all available measurement parameters of the device\n"
            "   read_all    -- read all measured parameters from device\n"
            "   read_one    -- read the selected parameters from device\n"
            "   read_repeat -- read measurements continously\n"
            "   conf_set    -- set the configuration register\n"
            "   get_info    -- List I2C driver functions\n"
            "\n"
            "Usage:\n"
            "   i2sensors get_info <bus> \n"
            "   i2sensors read_all <bus> <device-type> <address> \n"
            "   i2sensors read_one <bus> <device-type> <address> <param-id,offset_for_param>\n"
            "   i2sensors conf_set <bus> <device-type> <address> <reg-id,word-of-bits>\n"
            "\n"
            "Examples:\n"
            "   i2sensors get_info 2 \n"
            "   i2sensors read_all 1 lm75 0x4f \n"
            "   i2sensors read_one 1 lm75 0x4f  00,-1.35\n"
            "\n" );
    printf ("Version:  v%1.2f,  build: [%s] (gcc %s) \n\n",VERSION,_BUILD_TIME_,__VERSION__);
}



__uint8_t   *xchg_data  (__uint8_t *buf, __uint16_t wrlen, __uint16_t waitlen, __uint16_t rdlen){                     //# [buf] = 32 byte         

    __int32_t data=0;

    if( funcs &  I2C_FUNC_I2C  ){

        if(write(devicef, buf, 1) != 1){                //# write one byte to device
            fprintf (stderr, "I2C write error: [NO: %d, MSG: %s] at %s, line %d. (function: %s)\n", errno, strerror(errno), __FILE__, __LINE__, __func__);
        }
        usleep(waitlen*1000);                                //# Wait 10ms for reading
        if(read(devicef, buf, rdlen) != rdlen) {                //# read the result
            fprintf (stderr, "I2C read error: [NO: %d, MSG: %s] at %s, line %d. (function: %s)\n", errno, strerror(errno), __FILE__, __LINE__, __func__);
        }

    } else if ( funcs && I2C_FUNC_SMBUS_WORD_DATA ){
        if(i2c_smbus_write_byte(devicef, buf[0] ) == -1){                //# write one byte to device
            fprintf (stderr, "I2C write error: [NO: %d, MSG: %s] at %s, line %d. (function: %s)\n", errno, strerror(errno), __FILE__, __LINE__, __func__);
        }
        usleep(waitlen*1000);                                //# Wait 10ms for reading
        if((data = i2c_smbus_read_word_data(devicef, buf[0])) == -1) {                //# read the result
            fprintf (stderr, "I2C read error: [NO: %d, MSG: %s] at %s, line %d. (function: %s)\n", errno, strerror(errno), __FILE__, __LINE__, __func__);
        } else {
            buf[0] = data & 0x0FF;
            buf[1] = (data >> 8) & 0x0FF;
        }
    }
    return buf;
}



void preinit(const char *dev_type){

    if(!strcmp("lm75", (char *)dev_type)){
        print_all = lm75_print_all;
        read_all = lm75_read_all;
        read_one = lm75_read_one;
        conf_set = lm75_conf_set;
    }

    else if(!strcmp("hih61xx", (char *)dev_type)) {                            
        print_all = hih61xx_print_all;
        read_all = hih61xx_read_all;
        read_one = hih61xx_read_one;
        conf_set = hih61xx_conf_set;
    }

    // else if(!strcmp("hmc5883", dev_type)) {                            
    //     print_all = hmc5883_print_all;
    //     read_all = hmc5883_read_all;
    //     read_one = hmc5883_read_one;
    //     conf_set = hmc5883_conf_set;
    // }
}



int main( int argc, char *argv[] ){

    if( (argc == 3 && !strcmp("get_info", argv[1])) || (4 < argc && argc < 10)){   //Minimal 5 arguments should be passed. (or get_info is called)
        
        char filename[256];
        int i, file1;
        
        snprintf(filename, 256, "/dev/i2c-%s", argv[2]);   
        devicef = open(filename, O_RDWR);                               //# Open the i2c bus     
        if (devicef < 0){
            printf("\n\tCan't open \"%s\" BUS. (Have you any permission?)\n\n", filename);    
            exit(EXIT_FAILURE);
        }

        if (ioctl(devicef, I2C_FUNCS, &funcs) < 0) {                    //Get i2c device features
	        fprintf (stderr, "\n\tAdapter function error  [ERR: %d, MSG: %s] at %s, line %d. \n", errno, strerror(errno), __FILE__, __LINE__);
	        exit(EXIT_FAILURE);
        }


        if( argc > 4 ){                        //If not get_info was called
            if(argv[4][0]=='0' && argv[4][1]=='x'){                         //# Hexadecimal representetion of device address

                addr = (int)strtol(argv[4], NULL, 0);                  //# strtol() is working with hex-string correctly

                if (ioctl(devicef, I2C_SLAVE, addr) < 0){                    //# Seeking/Opening the device on the Bus
                    //printf("\n\tCan't open i2c DEVICE, errno: %d\n\n",errno);
                    fprintf (stderr, "\n\tCan't open i2c DEVICE!  [ERR: %d, MSG: %s] at %s, line %d. \n", errno, strerror(errno), __FILE__, __LINE__);
                    exit(EXIT_FAILURE);
                }
            } else {
                printf("\n\tThe DEVICE address must be a hexadecimal number\n\n");
                exit(EXIT_FAILURE);
            }
            preinit((char *)argv[3]);                                   //Initialization of subcommands
        }
        
        
        if (!strcmp("print_all",argv[1]) && print_all != NULL){          //# Gets the device's help message.
            print_all();
        
        } else if  (!strcmp("read_all",argv[1]) && read_all != NULL){       //# Pulls all readable registers data from device
            // read_all(argv[5]);
            read_all(NULL);
        
        } else if (!strcmp("read_one",argv[1]) && read_one != NULL && argc > 5) {   //# Prints out the selected register
            read_one((uchar*)argv[5]);
        
        } else if (!strcmp("conf_set",argv[1]) && conf_set != NULL && argc > 5){    //# Prints out the selected register
            conf_set((uchar*)argv[5]);
        
        } else if ( !strcmp("get_info", argv[1]) ){

            snprintf(filename, 256, "/sys/class/i2c-dev/i2c-%s/name", argv[2]);
            file1 = open(filename, O_RDONLY);
            i = read(file1, filename, 256 );
            if(i > 0) { filename[i-1] = '\0'; }
            close(file1);

            printf("\n Functions, supported by the [%s] adapter:\n\n", filename);

            printf("\tI2C_FUNC_I2C -- ");
            if (funcs & I2C_FUNC_I2C){  printf("YES\n");  } else { printf("NO\n"); }
            printf("\tI2C_FUNC_SMBUS_BYTE -- ");
            if (funcs & I2C_FUNC_SMBUS_BYTE){  printf("YES\n");  } else { printf("NO\n"); }
            printf("\tI2C_FUNC_SMBUS_BYTE_DATA -- ");
            if (funcs & I2C_FUNC_SMBUS_BYTE_DATA){  printf("YES\n");  } else { printf("NO\n"); }
            printf("\tI2C_FUNC_SMBUS_WORD_DATA -- ");
            if (funcs & I2C_FUNC_SMBUS_WORD_DATA){  printf("YES\n");  } else { printf("NO\n"); }
            printf("\tI2C_FUNC_SMBUS_BLOCK_DATA -- ");
            if (funcs & I2C_FUNC_SMBUS_BLOCK_DATA){  printf("YES\n");  } else { printf("NO\n"); }
            printf("\tI2C_FUNC_SMBUS_I2C_BLOCK -- ");
            if (funcs & I2C_FUNC_SMBUS_I2C_BLOCK){  printf("YES\n");  } else { printf("NO\n"); }
            printf("\tI2C_FUNC_SMBUS_EMUL -- ");
            if (funcs & I2C_FUNC_SMBUS_EMUL){  printf("YES\n");  } else { printf("NO\n"); }
            printf("\tI2C_FUNC_NOSTART -- ");
            if (funcs & I2C_FUNC_NOSTART){  printf("YES\n");  } else { printf("NO\n"); }
            printf("\tI2C_FUNC_10BIT_ADDR -- ");
            if (funcs & I2C_FUNC_10BIT_ADDR){  printf("YES\n");  } else { printf("NO\n"); }
            printf("\tI2C_FUNC_PROTOCOL_MANGLING -- ");
            if (funcs & I2C_FUNC_PROTOCOL_MANGLING){  printf(" YES\n");  } else { printf("NO\n"); }

            printf("\n\tMore info in Linux Kernel Documentation: i2c/functionality.rst\n\n");
            
            
        } else {                                                             //# No other option
            print_help();
        }
    
	close(devicef);
  
  } else {
    print_help();
  }
  return 0;
}