X-Git-Url: http://git.smho.de/gw/?p=owTools.git;a=blobdiff_plain;f=src%2FowW1Interface.cpp;fp=src%2FowW1Interface.cpp;h=60d65035c2fd2c47f68866fd4289151c74d2651e;hp=0000000000000000000000000000000000000000;hb=6cc62e3c72ae861a7d1c7f770e052b7b054d0c7a;hpb=1a6465a924428af072a8eb5e75ee547c394f4d8e 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; +}