From 6cc62e3c72ae861a7d1c7f770e052b7b054d0c7a Mon Sep 17 00:00:00 2001 From: Tobias Date: Tue, 9 May 2017 13:51:08 +0200 Subject: [PATCH] Add access with W1-Kernelmodule --- src/Makefile.am | 2 +- src/main.cpp | 6 ++ src/owTools.h | 1 + src/owW1Interface.cpp | 158 ++++++++++++++++++++++++++++++++++++++++++ src/owW1Interface.h | 61 ++++++++++++++++ 5 files changed, 227 insertions(+), 1 deletion(-) create mode 100644 src/owW1Interface.cpp create mode 100644 src/owW1Interface.h diff --git a/src/Makefile.am b/src/Makefile.am index 2dc8b37..c58bf48 100755 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -2,7 +2,7 @@ AM_CPPFLAGS = -Wall -std=c++11 -I/usr/include/mysql owToolsprgdir=../ owToolsprg_PROGRAMS=owTools -owTools_SOURCES=main.cpp mySensorDB.cpp owInterface.cpp owPiGPioInterface.cpp libusbds2490.c owUSBInterface.cpp hexfile.cpp owCOMInterface.cpp owDevice.cpp owDeviceConfig.cpp owARDUINOInterface.cpp +owTools_SOURCES=main.cpp mySensorDB.cpp owInterface.cpp owPiGPioInterface.cpp libusbds2490.c owUSBInterface.cpp hexfile.cpp owCOMInterface.cpp owDevice.cpp owDeviceConfig.cpp owARDUINOInterface.cpp owW1Interface.cpp owTools_LDADD = -lpthread -lrt -lusb -lmysqlclient diff --git a/src/main.cpp b/src/main.cpp index 7c28e12..8bc4452 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -476,6 +476,12 @@ int main(int argc, char *argv[]) { printf("\033[1;31mGPIO works with Raspberry PI only \033[0m\n"); } } else + if(adapter.find("W1")!=std::string::npos) { + owi=new owW1Interface(); + if (owi->InitAdapter(0)) { + printf("Use W1-Kernel 1-Wire functions on /sys/bus/w1\n"); + } else printf("No W1-Kernel module found!!\n"); + } else #endif if (adapter.find("ARDUINO") != std::string::npos) { owi = new owARDUINOInterface(); diff --git a/src/owTools.h b/src/owTools.h index b4a942c..dac67d9 100755 --- a/src/owTools.h +++ b/src/owTools.h @@ -53,5 +53,6 @@ #include "owUSBInterface.h" #include "owCOMInterface.h" #include "owPiGPioInterface.h" +#include "owW1Interface.h" #endif #endif diff --git a/src/owW1Interface.cpp b/src/owW1Interface.cpp new file mode 100644 index 0000000..60d6503 --- /dev/null +++ b/src/owW1Interface.cpp @@ -0,0 +1,158 @@ +// Copyright (c) 2017, Tobias Mueller tm(at)tm3d.de +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the +// distribution. +// * All advertising materials mentioning features or use of this +// software must display the following acknowledgement: This product +// includes software developed by tm3d.de and its contributors. +// * Neither the name of tm3d.de nor the names of its contributors may +// be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + +#include "owW1Interface.h" +#include +#include +#include +#include +#include /* For O_RDWR */ +#include +#include +#include +#include +#include +#include + + +snum_t owW1Interface::getSnum(const char *s) { + snum_t snum; + int res=sscanf( s,"%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx",&snum.byte[0],&snum.byte[6],&snum.byte[5],&snum.byte[4],&snum.byte[3],&snum.byte[2],&snum.byte[1]); + if (res==7) { + std::vector id; + for(int i=0;i<7;i++) id.push_back(snum.byte[i]); + snum.byte[7]=calcCRC8(id); + } else snum.num=0; + return snum; + +} +void owW1Interface::getStrnum(snum_t snum,char *s) { + sprintf(s,"%02x-%02x%02x%02x%02x%02x%02x",snum.byte[0],snum.byte[6],snum.byte[5],snum.byte[4],snum.byte[3],snum.byte[2],snum.byte[1]); +} + + +int owW1Interface::InitAdapter(uint8_t nr) { + d = opendir("/sys/bus/w1/devices"); + if (d) { + printf("W1 direcotry ok\n"); + } else { + log->set(OWLOG_ERROR,"W1 directory not found!"); + return 0; + } + + return 1; +} +int owW1Interface::Reset() { + return 1; +} +uint8_t owW1Interface::sendrecivBit(uint8_t bit) { + return 1; +} +uint8_t owW1Interface::sendrecivByte(uint8_t byte) { + return 0xFF; +} +void owW1Interface::ReleaseAdapter() {} +int owW1Interface::owFirst() { + struct dirent *dir; + d = opendir("/sys/bus/w1/devices"); + if (d==0) { + log->set(OWLOG_ERROR,"W1 directory not found!"); + return 0; + } + while ((dir = readdir(d)) != NULL) { + snum_t snum=getSnum(dir->d_name); + if (snum.num!=0) { + for (int i=0;i<8;i++) ROM_NO[i]=snum.byte[i]; + return 1; + } + } + return 0; +} +int owW1Interface::owNext() { + struct dirent *dir; + while ((dir = readdir(d)) != NULL) { + snum_t snum=getSnum(dir->d_name); + if (snum.num!=0) { + for (int i=0;i<8;i++) ROM_NO[i]=snum.byte[i]; + return 1; + } + } + return 0; +} +FILE* owW1Interface::openW1Stream(snum_t snum) { + char s[200]; + FILE *f=NULL; + sprintf(s,"/sys/bus/w1/devices/%02x-%02x%02x%02x%02x%02x%02x/rw",snum.byte[0],snum.byte[6],snum.byte[5],snum.byte[4],snum.byte[3],snum.byte[2],snum.byte[1]); + f=fopen(s,"rb+"); + if (f==NULL) { + printf("Error open rw-file, try to remove device-modules\n"); + int res=system("rmmod w1_therm w1_smem w1_ds2433"); + f=fopen(s,"rb+"); + if (f!=NULL) { + setbuf(f,NULL); + return f; + } + log->set(OWLOG_ERROR,"device-directory not found! (%i)",res); + } else { + setbuf(f,NULL); + return f; + } + + return NULL; +} + +int owW1Interface::Communicate(std::vector *data, int scount, int rcount) { + int pointer=0; + if ((*data)[0]==0x55) { + for(int i=0;i<8;i++) activedevice.byte[i]=(*data)[i+1]; + //printf("Setaktiv %016llX\n",(unsigned long long)activedevice.num); + pointer=8; + } + if (scount>pointer) { //noch mehr senden + FILE *f; + f=openW1Stream(activedevice); + if (f!=NULL) { + uint8_t w[256],r[256]; + std::copy(data->begin()+pointer, data->end(), w); + fwrite(w,1,scount-pointer,f); + int res=fread(r,1,rcount,f); + for(int i=0;ipush_back(r[i]); + } + for(int i=0;i<(rcount-res);i++) { + data->push_back(0xFF); + } + } + } + return 1; +} diff --git a/src/owW1Interface.h b/src/owW1Interface.h new file mode 100644 index 0000000..f150542 --- /dev/null +++ b/src/owW1Interface.h @@ -0,0 +1,61 @@ +// Copyright (c) 2016, Tobias Mueller tm(at)tm3d.de +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the +// distribution. +// * All advertising materials mentioning features or use of this +// software must display the following acknowledgement: This product +// includes software developed by tm3d.de and its contributors. +// * Neither the name of tm3d.de nor the names of its contributors may +// be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + +#ifndef __OWPIGOIOINTERFACES_H_ +#define __OWPIGPIOINTERFACES_H_ + +#include "owInterface.h" + +class owW1Interface:public owInterface{ +protected: + snum_t getSnum(const char *s); + void getStrnum(snum_t snum,char *s); + FILE *openW1Stream(snum_t snum); + DIR *d; + snum_t activedevice; +public: + owW1Interface():owInterface() { + d=NULL; + } + virtual int InitAdapter(uint8_t); + virtual int Reset(); + virtual uint8_t sendrecivBit(uint8_t bit); + virtual uint8_t sendrecivByte(uint8_t byte); + virtual void ReleaseAdapter(); + virtual int owFirst(); + virtual int owNext(); + virtual int Communicate(std::vector *data, int scount, int rcount); + +}; + +#endif -- 2.34.1