--- /dev/null
+// 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 <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <sys/mman.h>
+#include <fcntl.h> /* For O_RDWR */
+#include <unistd.h>
+#include <pthread.h>
+#include <sched.h>
+#include <memory.h>
+#include <strings.h>
+#include <dirent.h>
+
+
+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<uint8_t> 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<uint8_t> *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;i<res;i++) {
+ data->push_back(r[i]);
+ }
+ for(int i=0;i<(rcount-res);i++) {
+ data->push_back(0xFF);
+ }
+ }
+ }
+ return 1;
+}
--- /dev/null
+// 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<uint8_t> *data, int scount, int rcount);
+
+};
+
+#endif