ARDUINO as Master
[owTools.git] / src / owInterface.h
1 // Copyright (c) 2017, Tobias Mueller tm(at)tm3d.de
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //  * Redistributions of source code must retain the above copyright
9 //    notice, this list of conditions and the following disclaimer.
10 //  * Redistributions in binary form must reproduce the above copyright
11 //    notice, this list of conditions and the following disclaimer in the
12 //    documentation and/or other materials provided with the
13 //    distribution.
14 //  * All advertising materials mentioning features or use of this
15 //    software must display the following acknowledgement: This product
16 //    includes software developed by tm3d.de and its contributors.
17 //  * Neither the name of tm3d.de nor the names of its contributors may
18 //    be used to endorse or promote products derived from this software
19 //    without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33
34 #ifndef __OWINTERFACES_H_
35 #define __OWINTERFACES_H_
36
37 #include <vector>
38 #include <stdint.h>
39 #include <string>
40 #include <termios.h>
41 #include <stdarg.h>
42
43
44 class owDevice;
45 class owDeviceConfig;
46 #include "owDeviceConfig.h"
47
48 #if defined(_WINDOWS) || defined(__WINDOWS__) || defined(_WIN32) || defined(WIN32)
49    #define USE_WINDOWS_TIME 1
50    #include <windows.h>
51    #include "win32/usb.h" // libusb header
52 #else
53    #define USE_WINDOWS_TIME 0
54    #include <time.h>
55    #include <sys/time.h>
56
57    #include <usb.h>
58 #endif
59
60 #ifndef FALSE
61 #define FALSE 0
62 #endif
63 #ifndef TRUE
64 #define TRUE 1
65 #endif
66
67 #define OWLOG_INFO 1
68 #define OWLOG_WARNING 2
69 #define OWLOG_ERROR 3
70
71 class owLog {
72         int lcount;
73         std::string logtext;
74         int level;
75         int loglevel;
76                 
77 public:
78         owLog() {
79                 lcount=0;
80                 loglevel=0;
81         } 
82         virtual int set(int llevel,const char *format, ...) {
83                 char s[300];
84                 va_list arg;
85                 int done;
86                 va_start (arg, format);
87                 done = vsprintf (s, format, arg);
88                 va_end (arg);
89                 logtext=s;
90                 level=llevel;
91                 lcount++;
92                 if (level>=loglevel) {
93                         if (loglevel==OWLOG_ERROR) printf("\033[1;31m%s\033[0m\n",s);
94                         else if (loglevel==OWLOG_WARNING) printf("\033[1;33m%s\033[0m\n",s);
95                         else printf("%s\n",s);
96                 }
97                 return done;
98         }
99         virtual void clear() {
100                 lcount=0;
101         }
102         virtual int last() {
103                 if (lcount) return level; else return 0;
104         }
105         virtual void setLogLevel(int llevel ){
106                 loglevel=llevel;
107         }
108         
109 };
110
111
112 typedef union  {
113         uint8_t byte[8];
114         uint64_t num;
115 } snum_t;
116
117 class owInterface {
118 protected:
119         int block;
120         uint8_t crc8;
121         uint16_t crc16;
122         uint8_t docrc8(uint8_t value);
123         uint16_t docrc16(uint16_t cdata);
124         
125         // global search state
126         unsigned char ROM_NO[8];
127         int LastDiscrepancy;
128         int LastFamilyDiscrepancy;
129         int LastDeviceFlag;
130         
131         int devices_changed;
132         
133 public:
134         std::vector<snum_t> device_nums;
135         std::vector<owDevice*> devices;
136         int maxrepeat;
137         owLog *log;
138         owInterface() {
139                 block=0;
140                 log=new owLog();
141                 maxrepeat=4;
142                 devices_changed=0;
143         }
144         void Wait_Free() {
145                 while (block) {};
146                 block = 1;
147         }
148         void Free() {
149                 block = 0;
150         }       
151         
152         int isDevicesChanged() {
153                 int dc=devices_changed;
154                 devices_changed=0;
155                 return dc;
156         }
157         virtual void Clean();
158         virtual int Find() ;
159         virtual int InitAdapter(std::string s) {
160                 uint8_t param=atoi(s.c_str());
161                 return InitAdapter(param);
162         }
163         virtual void ReleaseAdapter() {}
164         virtual int InitAdapter(uint8_t)=0;
165         //Return 1 if Devices Present
166         virtual int Reset()=0;
167         virtual uint8_t sendrecivBit(uint8_t bit) =0;
168         
169
170
171         virtual uint8_t sendrecivByte(uint8_t byte) {
172                 uint8_t b=byte;
173                 uint8_t rb=0;
174                 for(int i=0;i<8;i++) {
175                 rb=rb>>1;
176                         rb|=sendrecivBit(b&1)<<7;
177                 b=b>>1;
178                 }
179                 return rb;
180         }
181         
182         virtual int MatchRom(snum_t snum);
183         virtual int Communicate(std::vector<uint8_t> *data, int scount, int rcount);
184         
185         
186
187         uint8_t calcCRC8(std::vector<uint8_t> data);
188         uint16_t calcCRC16(std::vector<uint8_t> data);
189         int testCRC16(std::vector<uint8_t> data);
190                 
191         void resetFlasher();
192         void resetID();
193         int programmPage(int pagenr, std::vector<uint8_t> page, int pagesize);
194         int flashHEXFile(std::string filename,snum_t dev,int resetid,int progress);
195         
196         
197         virtual int owFirst() {
198                 LastDiscrepancy = 0;
199                 LastDeviceFlag = FALSE;
200                 LastFamilyDiscrepancy = 0;
201                 return owSearch();              
202         }
203         virtual int owNext()  {
204                 return owSearch();
205         }
206         virtual ~owInterface()  {
207                 ReleaseAdapter();
208         }
209 protected:
210         int owSearch();
211         
212 };
213
214 #define OWDCS_NONE 0
215 #define OWDCS_DEFAULT 1
216 #define OWDCS_16 2
217 #define OWDCS_24 3
218
219
220 class owDevice {
221 protected:
222         owInterface* owi;
223         snum_t snum;
224         std::vector<int32_t> raw;
225 public:
226         owDeviceConfig *config;
227         int configstate;
228         std::vector<double> values;
229         int lastfound; //0 found in last searchrom, 1 found in the searchrom befor, and so on
230         owDevice(owInterface *owi_,snum_t num);
231         //return 1 for real Config, return 0 for DefaultConig
232         int readConfig();
233         virtual void setDefaultConfig();
234                 //Information zum owDevice      
235         std::vector<std::string> getFamilyInfo();
236         
237         snum_t getNum() {return snum;}
238
239         int Communicate(std::vector<uint8_t> *data, int scount, int rcount);
240         int CommunicateShort(std::vector<uint8_t> *data, int scount, int rcount);
241         void changeID(snum_t nid);
242         void runFlasher();
243         virtual int convertAll() {return 1;}
244         virtual ~owDevice() {}
245 };
246
247 class owDeviceDS18B20:public owDevice {
248 public:
249         owDeviceDS18B20(owInterface *owi_,snum_t num):owDevice(owi_,num) {      }
250         virtual void setDefaultConfig();
251         int readScratchpad(std::vector<uint8_t> *sp);
252         virtual int convertAll();
253 } ;
254
255
256 class owDeviceDS2438:public owDevice {
257 public:
258         owDeviceDS2438(owInterface *owi_,snum_t num):owDevice(owi_,num) {}
259         virtual void setDefaultConfig();
260
261         int setConfigByte(uint8_t cb);
262         int readScratchpad(std::vector<uint8_t> *sp, uint8_t page, int recall);
263
264         virtual int convertAll();
265
266 } ;
267 class owDeviceDS2450:public owDevice {
268 public:
269         owDeviceDS2450(owInterface *owi_,snum_t num):owDevice(owi_,num) {}
270
271         virtual void setDefaultConfig();
272         virtual int convertAll();
273         void readMemory(uint8_t addr,std::vector<uint8_t> *sp);
274         void writeMemory(uint8_t addr,std::vector<uint8_t> *sp);
275         void convert(uint8_t mask, uint8_t preset); 
276
277 } ;
278
279
280 class owDeviceDS2423:public owDevice {
281 public:
282         owDeviceDS2423(owInterface *owi_,snum_t num):owDevice(owi_,num) {}
283
284         virtual void setDefaultConfig();
285         virtual int convertAll();
286         uint32_t readCounter(uint8_t page);
287
288 } ;
289
290
291 #endif