Commit 77be9983 by Luca Fulchir

initial commit

parents
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.
0. Additional Definitions.
As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.
"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.
An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.
A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".
The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.
The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.
1. Exception to Section 3 of the GNU GPL.
You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.
2. Conveying Modified Versions.
If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:
a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or
b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.
3. Object Code Incorporating Material from Library Header Files.
The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:
a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the object code with a copy of the GNU GPL and this license
document.
4. Combined Works.
You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:
a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.
c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.
d) Do one of the following:
0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.
1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.
e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)
5. Combined Libraries.
You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.
b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.
6. Revised Versions of the GNU Lesser General Public License.
The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.
If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.
here we have a list of supported inverters models and their ids.
well, not much by now, but it's a start.
1 = Enersolis 3300W (aka Sunny Roo 3000 TL)
# This file is part of libsolar.
#
# libsolar is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# libsolar is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with libsolar. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2011 Luca Fulchir <luker@codepedia.eu>
ifndef CFLAGS
CFLAGS = -Wall -fPIC -g -fvisibility=hidden
endif
ifndef CC
CC=gcc
endif
ifndef DESTDIR
DESTDIR=
endif
ifndef PREFIX
PREFIX=/usr
endif
INCLUDEDIR=$(DESTDIR)/$(PREFIX)/include/
INSTALL=/usr/bin/install
LIBDIR=$(DESTDIR)/$(PREFIX)/lib
LDFLAGS=
MAKE=make
all: link
link: libsolardata.h common.o inverters/enersolis3300.o channels/rs232.o libsolar.o
$(CC) $(CFLAGS) $(LDFLAGS) -shared -Wl,-soname,libsolar.so.1.0 \
-o libsolar.so.1.0 common.o channels/rs232.o \
inverters/enersolis3300.o libsolar.o
libsolar.o: libsolar.h libsolar.c libsolardata.h
$(CC) $(CFLAGS) -c libsolar.c
rs232.o:
cd channels && $(MAKE) rs232
enersolis3300.o:
cd inverters && $(MAKE) enersolis3300
common.o: libsolardata.h common.c common.h
$(CC) $(CFLAGS) -c common.c
distclean: clean
clean:
-rm *.o libsolar.so* *~ &>/dev/null
cd inverters && $(MAKE) clean
cd channels && $(MAKE) clean
install:
$(INSTALL) -o root -g root -m 0755 libsolar.so.1.0 \
$(LIBDIR)/libsolar.so.1.0
ln -s $(LIBDIR)/libsolar.so.1.0 $(LIBDIR)/libsolar.so
$(INSTALL) -o root -g root -m 0664 libsolar.h $(INCLUDEDIR)/libsolar.h
$(INSTALL) -o root -g root -m 0664 libsolardata.h $(INCLUDEDIR)/libsolardata.h
deinstall: uninstall
uninstall:
rm -f $(INCLUDEDIR)/libsolar.h
rm -f $(LIBDIR)/libsolar.so
rm -f $(LIBDIR)/libsolar.so.1.0
# This file is part of libsolar.
#
# libsolar is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# libsolar is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with libsolar. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2011 Luca Fulchir <luker@codepedia.eu>
ifndef CFLAGS
CFLAGS = -Wall -g -fPIC
endif
ifndef CC
CC=gcc
endif
MAKE=make
all: rs232
rs232:rs232.c rs232.h
$(CC) $(CFLAGS) -c rs232.c
rs485:rs485.c re485.h
$(CC) $(CFLAGS) -c rs485.c
ethernet:ethernet.c ethernet.h
$(CC) $(CFLAGS) -c ethernet.c.c
usb: usb.c usb.h
$(CC) $(CFLAGS) -c usb.c
clean:
-rm *.o *~ &>/dev/null
/*
* This file is part of libsolar.
*
* libsolar is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libsolar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with libsolar. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright (C) 2011 Luca Fulchir <luker@codepedia.eu>
*/
#ifndef ETHERNETH
#define ETHERNETH
struct eth {
/* type == 4 */
char unused;
};
#endif
/*
* This file is part of libsolar.
*
* libsolar is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libsolar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with libsolar. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright (C) 2011 Luca Fulchir <luker@codepedia.eu>
*/
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <strings.h>
#include <termios.h>
#include "rs232.h"
#include "../debug.h"
int open_rs232(struct rs232 *serial)
{
/* returns the fd on succesful open, -1 on error. oldtty contains
* the old tty settings */
int fd;
struct termios *oldtty;
struct termios *tty;
struct termdata *tdata;
tty = &serial->tty;
tdata = &serial->tdata;
oldtty = &serial->oldtty;
if ((fd = open(tdata->port,
O_RDWR | O_NOCTTY | O_NONBLOCK | O_NDELAY)) == -1){
fprintf(stderr, "Err: opening of device %s\n", tdata->port);
oldtty = NULL;
return -1 ;
}
if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
fprintf(stderr, "Err:rs232 fcntl\n");
return -1;
}
if (debug)
printf("libsolar: succesfully opened serial port %s\n",
tdata->port);
/* save old port settings */
if (tcgetattr(fd, oldtty) < 0){
perror("Err:Can't get terminal parameters\n");
oldtty = NULL;
return -1 ;
}
bzero(tty, sizeof(struct termios));
tty->c_iflag = 0;
tty->c_oflag = 0;
tty->c_cflag = 0;
tty->c_lflag = 0;
/* tty->c_cc[VMIN]=1;
tty->c_cc[VTIME]=5; */
tcflush(fd, TCIOFLUSH);
if (debug)
printf("libsolar: saved old config\n");
switch (tdata->speed) {
case 0:
cfsetospeed(tty,B0);
cfsetispeed(tty,B0);
break;
case 1200:
cfsetospeed(tty,B1200);
cfsetispeed(tty,B1200);
break;
case 1800:
cfsetospeed(tty,B1800);
cfsetispeed(tty,B1800);
break;
case 2400:
cfsetospeed(tty,B2400);
cfsetispeed(tty,B2400);
break;
case 4800:
cfsetospeed(tty,B4800);
cfsetispeed(tty,B4800);
break;
case 9600:
cfsetospeed(tty,B9600);
cfsetispeed(tty,B9600);
break;
case 19200:
cfsetospeed(tty,B19200);
cfsetispeed(tty,B19200);
break;
case 38400:
cfsetospeed(tty,B38400);
cfsetispeed(tty,B38400);
break;
case 57600:
cfsetospeed(tty,B57600);
cfsetispeed(tty,B57600);
break;
case 115200:
cfsetospeed(tty,B115200);
cfsetispeed(tty,B115200);
break;
default:
cfsetospeed(tty,B9600);
cfsetispeed(tty,B9600);
}
if (tdata->bits == 7) {
tty->c_cflag |= CS7;
} else {
tty->c_cflag |= CS8;
}
switch (tdata->parity) {
case -1:
tty->c_iflag |= PARODD;
break;
case 1:
tty->c_iflag |= PARENB;
break;
case 0:
default:
tty->c_iflag |= IGNPAR;
break;
}
tty->c_iflag |= IGNBRK | IGNCR;
/* Stop bits either 1(false) or 2(true). */
if (tdata->stopbits)
tty->c_cflag |= CSTOPB;
tty->c_cflag |= (CLOCAL | CREAD);
cfmakeraw(tty);
/* activate the new port settings */
if (tcsetattr(fd, TCSANOW, tty) < 0) {
perror("Can't set terminal parameters\n");
return -1;
}
/* discard pending data */
tcflush(fd, TCIOFLUSH);
if (debug)
printf("libsolar: rs232 data: %i %i\n", tdata->speed,
tdata->bits);
return fd;
}
int close_rs232 (struct rs232 *serial, int fd)
{
/* reset the port settings*/
if (tcsetattr(fd, TCSANOW, &serial->oldtty) < 0) {
perror("Can't set terminal parameters\n");
close(fd);
return -1;
}
close(fd);
return 0;
}
void free_rs232 (struct rs232 *serial)
{
if (serial == NULL)
return;
if (serial->tdata.port != NULL)
free(serial->tdata.port);
/* do NOT free serial, as it could be part of an other structure,
* and is not always a pointer */
}
/*
* This file is part of libsolar.
*
* libsolar is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libsolar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with libsolar. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright (C) 2011 Luca Fulchir <luker@codepedia.eu>
*/
#ifndef RS232H
#define RS232H
#include <termios.h>
#include <unistd.h>
struct termdata {
char *port;
int speed;
int bits;
int parity;
int stopbits;
};
struct rs232 {
/* type = 1 */
struct termdata tdata;
struct termios oldtty;
struct termios tty;
};
int open_rs232 (struct rs232 *serial);
int close_rs232(struct rs232 *serial, int fd);
void free_rs232 (struct rs232 *serial);
#endif
/*
* This file is part of libsolar.
*
* libsolar is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libsolar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with libsolar. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright (C) 2011 Luca Fulchir <luker@codepedia.eu>
*/
#ifndef RS485H
#define RS485H
struct rs485 {
/* type = 2 */
char unused;
};
#endif
/*
* This file is part of libsolar.
*
* libsolar is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libsolar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with libsolar. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright (C) 2011 Luca Fulchir <luker@codepedia.eu>
*/
#ifndef USBH
#define USBH
struct usb {
/* type == 3 */
char unused;
};
#endif
/*
* This file is part of libsolar.
*
* libsolar is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libsolar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with libsolar. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright (C) 2011 Luca Fulchir <luker@codepedia.eu>
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/select.h>
#include <time.h>
#include "libsolardata.h"
#include "common.h"
#include "channels/rs232.h"
#include "debug.h"
int readtimeout (int fd, struct timeval timeout, int length,
unsigned char *buffer)
{
/* wait at maximum TIMEOUT time for each byte to read.
* reaturns 0 on success, -1 on error
*/
fd_set fds;
int ret, readed = 0;
struct timeval time2;
FILE *fp;
while(readed != length) {
FD_ZERO(&fds);
FD_SET(fd, &fds);
time2.tv_sec = timeout.tv_sec;
time2.tv_usec = timeout.tv_usec;
ret = select(fd + 1, &fds, NULL, NULL, &time2);
if (ret <= 0) {
/* timeout reached or error */
if (errno || !FD_ISSET(fd, &fds)) {
fprintf(stderr, "libsolar: %s on select\n",
strerror(errno));
errno = 0;
}
break;
}
ret = read(fd, (void *)&buffer[readed], length - readed);
if (ret < 0) {
fprintf(stderr, "libsolar err: read - %i\n", errno);
break;
}
readed += ret;
}
if (readed != length) {
fprintf(stderr, "libsolar:readtimeout: %i-%i\n",readed, length);
fp = fopen("/tmp/lslr", "a");
fprintf(fp, "timeout/unknown pkt len %i:", length);
for (ret = 0; ret < readed; ret++) {
fprintf(fp, " %x ", buffer[ret]);
}
fprintf(fp, "\n");
fclose(fp);
return -1;
}
return 0;
}
int newdata (struct libsolar_data **ret, int type)
{
/* allocate new struct data, with the right type.
* returns 0 on success, -1 on error
*
* dirty trick: last structure has type == 0
* since you can NOT have a type == 0, when you find it you know it's
* the end of the array.
*/
struct libsolar_data *d;
int i;
if (type < 0)
return -1;
i = 2; /* start from 2: allocate space for one more structure and
* terminate with NULL pointer */
d = *ret;
if (d != NULL) {
while (d->type >= 0) {
d++;
i++;
}
}
d = (struct libsolar_data *) realloc((void *)*ret,
i * sizeof(struct libsolar_data));
if (d == NULL) {
perror("newdata realloc\n");
return -1;
}
*ret = d;
d += (i - 1);
d->type = -1;
d->id = 0;
d->v = NULL; /* last is NULL */
d--; /* work on last -1 */
d->type = type;
d->id = 0;
switch (type) {
case 0:
d->d = (struct libsolar_desc *)malloc(
sizeof(struct libsolar_desc));
if (d->d == NULL) {
perror("Err: malloc0 newdata\n");
d->d = NULL;
return -1;
}
d->d->name = NULL;
d->d->model = NULL;