Works now with Windows Visual Studio C++ too
[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 class owLog {
88         int lcount;
89         std::string logtext;
90         int level;
91         int loglevel;
92                 
93 public:
94         owLog() {
95                 lcount=0;
96                 loglevel=0;
97         } 
98         virtual int set(int llevel,const char *format, ...) {
99                 char s[300];
100                 va_list arg;
101                 int done;
102                 va_start (arg, format);
103 #ifdef LINUX
104                 done = vsprintf (s, format, arg);
105 #endif
106 #ifdef WIN
107                 done = vsprintf_s(s,300, format, arg);
108 #endif
109                 va_end (arg);
110                 logtext=s;
111                 level=llevel;
112                 lcount++;
113                 if (level>=loglevel) {
114                         if (loglevel==OWLOG_ERROR) printf("\033[1;31m%s\033[0m\n",s);
115                         else if (loglevel==OWLOG_WARNING) printf("\033[1;33m%s\033[0m\n",s);
116                         else printf("%s\n",s);
117                 }
118                 return done;
119         }
120         virtual void clear() {
121                 lcount=0;
122         }
123         virtual int last() {
124                 if (lcount) return level; else return 0;
125         }
126         virtual void setLogLevel(int llevel ){
127                 loglevel=llevel;
128         }
129         
130 };
131
132
133 typedef union  {
134         uint8_t byte[8];
135         uint64_t num;
136 } snum_t;
137
138 class owInterface {
139 protected:
140         int block;
141         uint8_t crc8;
142         uint16_t crc16;
143         uint8_t docrc8(uint8_t value);
144         uint16_t docrc16(uint16_t cdata);
145         
146         // global search state
147         unsigned char ROM_NO[8];
148         int LastDiscrepancy;
149         int LastFamilyDiscrepancy;
150         int LastDeviceFlag;
151         
152         int devices_changed;
153         
154 public:
155         std::vector<snum_t> device_nums;
156         std::vector<owDevice*> devices;
157         int maxrepeat;
158         owLog *log;
159         owInterface() {
160                 block=0;
161                 log=new owLog();
162                 maxrepeat=4;
163                 devices_changed=0;
164         }
165         void Wait_Free() {
166                 while (block) {};
167                 block = 1;
168         }
169         void Free() {
170                 block = 0;
171         }       
172         
173         int isDevicesChanged() {
174                 int dc=devices_changed;
175                 devices_changed=0;
176                 return dc;
177         }
178         virtual void Clean();
179         virtual int Find() ;
180         virtual int InitAdapter(std::string s) {
181                 uint8_t param=atoi(s.c_str());
182                 return InitAdapter(param);
183         }
184         virtual void ReleaseAdapter() {}
185         virtual int InitAdapter(uint8_t)=0;
186         //Return 1 if Devices Present
187         virtual int Reset()=0;
188         virtual uint8_t sendrecivBit(uint8_t bit) =0;
189         
190
191
192         virtual uint8_t sendrecivByte(uint8_t byte) {
193                 uint8_t b=byte;
194                 uint8_t rb=0;
195                 for(int i=0;i<8;i++) {
196                 rb=rb>>1;
197                         rb|=sendrecivBit(b&1)<<7;
198                 b=b>>1;
199                 }
200                 return rb;
201         }
202         
203         virtual int MatchRom(snum_t snum);
204         virtual int Communicate(std::vector<uint8_t> *data, int scount, int rcount);
205         
206         
207
208         uint8_t calcCRC8(std::vector<uint8_t> data);
209         uint16_t calcCRC16(std::vector<uint8_t> data);
210         int testCRC16(std::vector<uint8_t> data);
211                 
212         void resetFlasher();
213         void resetID();
214         int programmPage(int pagenr, std::vector<uint8_t> page, int pagesize);
215         int flashHEXFile(std::string filename,snum_t dev,int resetid,int progress);
216         
217         
218         virtual int owFirst() {
219                 LastDiscrepancy = 0;
220                 LastDeviceFlag = FALSE;
221                 LastFamilyDiscrepancy = 0;
222                 return owSearch();              
223         }
224         virtual int owNext()  {
225                 return owSearch();
226         }
227         virtual ~owInterface()  {
228                 ReleaseAdapter();
229         }
230 protected:
231         int owSearch();
232         
233 };
234
235 #define OWDCS_NONE 0
236 #define OWDCS_DEFAULT 1
237 #define OWDCS_16 2
238 #define OWDCS_24 3
239
240
241 class owDevice {
242 protected:
243         owInterface* owi;
244         snum_t snum;
245         std::vector<int32_t> raw;
246 public:
247         owDeviceConfig *config;
248         int configstate;
249         std::vector<double> values;
250         int lastfound; //0 found in last searchrom, 1 found in the searchrom befor, and so on
251         owDevice(owInterface *owi_,snum_t num);
252         //return 1 for real Config, return 0 for DefaultConig
253         int readConfig();
254         virtual void setDefaultConfig();
255                 //Information zum owDevice      
256         std::vector<std::string> getFamilyInfo();
257         
258         snum_t getNum() {return snum;}
259
260         int Communicate(std::vector<uint8_t> *data, int scount, int rcount);
261         int CommunicateShort(std::vector<uint8_t> *data, int scount, int rcount);
262         void changeID(snum_t nid);
263         void runFlasher();
264         virtual int convertAll() {return 1;}
265         virtual ~owDevice() {}
266 };
267
268 class owDeviceDS18B20:public owDevice {
269 public:
270         owDeviceDS18B20(owInterface *owi_,snum_t num):owDevice(owi_,num) {      }
271         virtual void setDefaultConfig();
272         int readScratchpad(std::vector<uint8_t> *sp);
273         virtual int convertAll();
274 } ;
275
276
277 class owDeviceDS2438:public owDevice {
278 public:
279         owDeviceDS2438(owInterface *owi_,snum_t num):owDevice(owi_,num) {}
280         virtual void setDefaultConfig();
281
282         int setConfigByte(uint8_t cb);
283         int readScratchpad(std::vector<uint8_t> *sp, uint8_t page, int recall);
284
285         virtual int convertAll();
286
287 } ;
288 class owDeviceDS2450:public owDevice {
289 public:
290         owDeviceDS2450(owInterface *owi_,snum_t num):owDevice(owi_,num) {}
291
292         virtual void setDefaultConfig();
293         virtual int convertAll();
294         void readMemory(uint8_t addr,std::vector<uint8_t> *sp);
295         void writeMemory(uint8_t addr,std::vector<uint8_t> *sp);
296         void convert(uint8_t mask, uint8_t preset); 
297
298 } ;
299
300
301 class owDeviceDS2423:public owDevice {
302 public:
303         owDeviceDS2423(owInterface *owi_,snum_t num):owDevice(owi_,num) {}
304
305         virtual void setDefaultConfig();
306         virtual int convertAll();
307         uint32_t readCounter(uint8_t page);
308
309 } ;
310
311
312 #endif