remove compiler warnings of new compilerversion /
[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 #if defined(WIN) || defined(LINUX)
38 #else
39 #if defined(_WINDOWS) || defined(__WINDOWS__) || defined(_WIN32) || defined(WIN32)
40 #define WIN
41 #else
42 #define LINUX
43 #endif
44 #endif
45
46
47 #include <vector>
48 #include <stdint.h>
49 #include <string>
50 #ifdef LINUX
51 #include <termios.h>
52 #endif
53 #include <stdarg.h>
54
55 #ifdef WIN
56 #include <Windows.h>
57
58 #endif
59
60 class owDevice;
61 class owDeviceConfig;
62 #include "owDeviceConfig.h"
63
64 #if defined(_WINDOWS) || defined(__WINDOWS__) || defined(_WIN32) || defined(WIN32)
65    #define USE_WINDOWS_TIME 1
66    #include <windows.h>
67    //#include "win32/usb.h" // libusb header
68 #else
69    #define USE_WINDOWS_TIME 0
70    #include <time.h>
71    #include <sys/time.h>
72
73    #include <usb.h>
74 #endif
75
76 #ifndef FALSE
77 #define FALSE 0
78 #endif
79 #ifndef TRUE
80 #define TRUE 1
81 #endif
82
83 #define OWLOG_INFO 1
84 #define OWLOG_WARNING 2
85 #define OWLOG_ERROR 3
86
87 #ifdef LINUX
88
89 #define BLUE_B "\033[1;34m"
90 #define COLOR_E "\033[0m"
91 #define C2_B "\033[0;36m"
92 #define C3_B "\033[3;34m"
93 #define C4_B "\033[1;33m"
94 #define C5_B "\033[4;33m"
95 #define CLE_B "\033[1;31m"
96 #define CLW_B "\033[1;33m"
97 #endif
98 #ifdef WIN
99 #define BLUE_B ""
100 #define C2_B ""
101 #define C3_B ""
102 #define C4_B ""
103 #define COLOR_E ""
104 #define C5_B ""
105 #define CLE_B ""
106 #define CLW_B ""
107
108 #endif
109
110 extern uint8_t owCC_44_Temp;
111
112 class owLog {
113         int lcount;
114         std::string logtext;
115         int level;
116         int loglevel;
117                 
118 public:
119         owLog() {
120                 lcount=0;
121                 loglevel=0;
122         } 
123         virtual int set(int llevel,const char *format, ...) {
124                 char s[300];
125                 va_list arg;
126                 int done;
127                 va_start (arg, format);
128 #ifdef LINUX
129                 done = vsprintf (s, format, arg);
130 #endif
131 #ifdef WIN
132                 done = vsprintf_s(s,300, format, arg);
133 #endif
134                 va_end (arg);
135                 logtext=s;
136                 level=llevel;
137                 lcount++;
138                 if (level>=loglevel) {
139                         if (loglevel==OWLOG_ERROR) printf(CLE_B "%s" COLOR_E "\n",s);
140                         else if (loglevel==OWLOG_WARNING) printf(CLW_B "%s" COLOR_E "\n",s);
141                         else printf("%s\n",s);
142                 }
143                 return done;
144         }
145         virtual void clear() {
146                 lcount=0;
147         }
148         virtual int last() {
149                 if (lcount) return level; else return 0;
150         }
151         virtual void setLogLevel(int llevel ){
152                 loglevel=llevel;
153         }
154         
155 };
156
157
158 typedef union  {
159         uint8_t byte[8];
160         uint64_t num;
161 } snum_t;
162
163 class owInterface {
164 protected:
165         int block;
166         uint8_t crc8;
167         uint16_t crc16;
168         uint8_t docrc8(uint8_t value);
169         uint16_t docrc16(uint16_t cdata);
170         
171         // global search state
172         unsigned char ROM_NO[8];
173         int LastDiscrepancy;
174         int LastFamilyDiscrepancy;
175         int LastDeviceFlag;
176         
177         int devices_changed;
178         
179 public:
180         std::vector<snum_t> device_nums;
181         std::vector<owDevice*> devices;
182         int maxrepeat;
183         owLog *log;
184         owInterface() {
185                 block=0;
186                 log=new owLog();
187                 maxrepeat=4;
188                 devices_changed=0;
189         }
190         void Wait_Free() {
191                 while (block) {};
192                 block = 1;
193         }
194         void Free() {
195                 block = 0;
196         }       
197         
198         int isDevicesChanged() {
199                 int dc=devices_changed;
200                 devices_changed=0;
201                 return dc;
202         }
203         virtual void Clean();
204         virtual int Find() ;
205         virtual int InitAdapter(std::string s) {
206                 uint8_t param=atoi(s.c_str());
207                 return InitAdapter(param);
208         }
209         virtual void ReleaseAdapter() {}
210         virtual int InitAdapter(uint8_t)=0;
211         //Return 1 if Devices Present
212         virtual int Reset()=0;
213         virtual uint8_t sendrecivBit(uint8_t bit) =0;
214         
215
216
217         virtual uint8_t sendrecivByte(uint8_t byte) {
218                 uint8_t b=byte;
219                 uint8_t rb=0;
220                 for(int i=0;i<8;i++) {
221                 rb=rb>>1;
222                         rb|=sendrecivBit(b&1)<<7;
223                 b=b>>1;
224                 }
225                 return rb;
226         }
227         
228         virtual int MatchRom(snum_t snum);
229         virtual int Communicate(std::vector<uint8_t> *data, int scount, int rcount);
230         
231         
232
233         uint8_t calcCRC8(std::vector<uint8_t> data);
234         uint16_t calcCRC16(std::vector<uint8_t> data);
235         int testCRC16(std::vector<uint8_t> data);
236         int testCRC16(std::vector<uint8_t> data,uint16_t load);
237                 
238         void resetFlasher(uint64_t id);
239         void resetID(uint64_t id);
240         int programmPage(uint64_t id,int pagenr, std::vector<uint8_t> page, int pagesize);
241         int flashHEXFile(std::string filename,snum_t dev,int resetid,int progress);
242         
243         
244         virtual int owFirst() {
245                 LastDiscrepancy = 0;
246                 LastDeviceFlag = FALSE;
247                 LastFamilyDiscrepancy = 0;
248                 return owSearch();              
249         }
250         virtual int owNext()  {
251                 return owSearch();
252         }
253         virtual ~owInterface()  {
254                 ReleaseAdapter();
255         }
256 protected:
257         int owSearch();
258         
259 };
260
261 #define OWDCS_NONE 0
262 #define OWDCS_DEFAULT 1
263 #define OWDCS_16 2
264 #define OWDCS_24 3
265
266
267 class owDevice {
268 protected:
269         owInterface* owi;
270         snum_t snum;
271         std::vector<int32_t> raw;
272 public:
273         owDeviceConfig *config;
274         int configstate;
275         std::vector<double> values;
276         int lastfound; //0 found in last searchrom, 1 found in the searchrom befor, and so on
277         owDevice(owInterface *owi_,snum_t num);
278         //return 1 for real Config, return 0 for DefaultConig
279         int readConfig();
280         virtual void setDefaultConfig();
281                 //Information zum owDevice      
282         std::vector<std::string> getFamilyInfo();
283         
284         snum_t getNum() {return snum;}
285
286         int Communicate(std::vector<uint8_t> *data, int scount, int rcount);
287         int CommunicateShort(std::vector<uint8_t> *data, int scount, int rcount);
288         void changeID(snum_t nid);
289         void runFlasher();
290         virtual int convertAll() {return 1;}
291         virtual int readMemory(int page,int start, int count,std::vector<uint8_t> *data) {return 0;};
292         virtual int writeMemory(int page,int start, int count,std::vector<uint8_t> *data) {return 0;};
293         virtual int getPageSize() {return 0;}
294         virtual ~owDevice() {}
295 };
296
297 class owDeviceDS18B20:public owDevice {
298 public:
299         owDeviceDS18B20(owInterface *owi_,snum_t num):owDevice(owi_,num) {      }
300         virtual void setDefaultConfig();
301         int readScratchpad(std::vector<uint8_t> *sp);
302         virtual int convertAll();
303         virtual int getPageSize() {return 8;}
304 } ;
305
306
307 class owDeviceDS2438:public owDevice {
308 public:
309         owDeviceDS2438(owInterface *owi_,snum_t num):owDevice(owi_,num) {}
310         virtual void setDefaultConfig();
311
312         int setConfigByte(uint8_t cb);
313         int readScratchpad(std::vector<uint8_t> *sp, uint8_t page, int recall);
314         virtual int readMemory(int page,int start, int count,std::vector<uint8_t> *data);
315         virtual int writeMemory(int page,int start, int count,std::vector<uint8_t> *data);
316         virtual int convertAll();
317         virtual int getPageSize() {return 8;}
318
319 } ;
320 class owDeviceDS2450:public owDevice {
321 public:
322         owDeviceDS2450(owInterface *owi_,snum_t num):owDevice(owi_,num) {}
323
324         virtual void setDefaultConfig();
325         virtual int convertAll();
326         virtual int readMemory(int page,int start, int count,std::vector<uint8_t> *data);
327         virtual int writeMemory(int page,int start, int count,std::vector<uint8_t> *data);
328         void readMemory_int(uint8_t addr,std::vector<uint8_t> *sp);
329         void writeMemory_int(uint8_t addr,std::vector<uint8_t> *sp);
330         void convert(uint8_t mask, uint8_t preset); 
331         virtual int getPageSize() {return 8;}
332
333 } ;
334
335
336 class owDeviceDS2423:public owDevice {
337 public:
338         owDeviceDS2423(owInterface *owi_,snum_t num):owDevice(owi_,num) {}
339
340         virtual void setDefaultConfig();
341         virtual int convertAll();
342         uint32_t readCounter(uint8_t page);
343         virtual int readMemory(int page,int start, int count,std::vector<uint8_t> *data);
344         virtual int writeMemory(int page,int start, int count,std::vector<uint8_t> *data);
345         virtual int getPageSize() {return 32;}
346
347
348 } ;
349
350
351 #endif