summaryrefslogtreecommitdiff
path: root/src/pmdas/roomtemp/mlan/linuxlnk.c
blob: 4c9079684a5064508e3a7e6605fa09f02b14e26d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//---------------------------------------------------------------------------
// Copyright (C) 1999 Dallas Semiconductor Corporation, All Rights Reserved.
// 
// Permission is hereby granted, free of charge, to any person obtaining a 
// copy of this software and associated documentation files (the "Software"), 
// to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, 
// and/or sell copies of the Software, and to permit persons to whom the 
// Software is furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included 
// in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
// MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
// IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES 
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 
// OTHER DEALINGS IN THE SOFTWARE.
// 
// Except as contained in this notice, the name of Dallas Semiconductor 
// shall not be used except as stated in the Dallas Semiconductor 
// Branding Policy. 
//---------------------------------------------------------------------------
//
//  TODO.C - COM functions required by MLANLL.C, MLANTRNU, MLANNETU.C and
//           MLanFile.C for MLANU to communicate with the DS2480 based 
//           Universal Serial Adapter 'U'.  Fill in the platform specific code.
//
//  Version: 1.02
//
//  History: 1.00 -> 1.01  Added function msDelay. 
//
//           1.01 -> 1.02  Changed to generic OpenCOM/CloseCOM for easier 
//                         use with other platforms.
//

//--------------------------------------------------------------------------
// Copyright (C) 1998 Andrea Chambers and University of Newcastle upon Tyne,
// All Rights Reserved.
//--------------------------------------------------------------------------
//
// Permission is hereby granted, free of charge, to any person obtaining a 
// copy of this software and associated documentation files (the "Software"), 
// to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, 
// and/or sell copies of the Software, and to permit persons to whom the 
// Software is furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included 
// in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
// MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
// IN NO EVENT SHALL THE UNIVERSITY OF NEWCASTLE UPON TYNE OR ANDREA CHAMBERS
// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//---------------------------------------------------------------------------
//
//  LinuxLNK.C - COM functions required by MLANLLU.C, MLANTRNU.C, MLANNETU.C 
//             and MLanFile.C for MLANU to communicate with the DS2480 based
//             Universal Serial Adapter 'U'.  Platform specific code.
//
//  Version: 1.03
//  History: 1.00 -> 1.03 modifications by David Smiczek
//                        Changed to use generic OpenCOM/CloseCOM  
//                        Pass port name to OpenCOM instead of hard coded
//                        Changed msDelay to handle long delays 
//                        Reformatted to look like 'TODO.C' 
//                        Added #include "ds2480.h" to use constants.
//                        Added function SetBaudCOM() 
//                        Added function msGettick()
//                        Removed delay from WriteCOM(), used tcdrain()
//                        Added wait for byte available with timeout using
//                          select() in ReadCOM()
/* 
   cfmakeraw function from nut-0.45.0 package
   common.c - common useful functions

   Copyright (C) 2000  Russell Kroll <rkroll@exploits.org>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "config.h"

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <time.h>
#include <termios.h> 
#include <sys/time.h>

#include "mlan.h"
#include "ds2480.h"
#include "pmapi.h"

// Exportable functions required for MLANLL.C, MLANTRNU.C, or MLANNETU.C
void FlushCOM(void);
int  WriteCOM(int, unsigned char*);
int  ReadCOM(int, unsigned char*);
void BreakCOM(void);
void msDelay(int);
long msGettick(void);

// Exportable functions for opening/closing serial port
int OpenCOM(char *);
void CloseCOM(void);

// LinuxLNK global
int fd;

#ifndef HAVE_CFMAKERAW
int cfmakeraw(struct termios *termios_p)
{
  termios_p->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
			  |INLCR|IGNCR|ICRNL|IXON);
  termios_p->c_oflag &= ~OPOST;
  termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
  termios_p->c_cflag &= ~(CSIZE|PARENB);
  termios_p->c_cflag |= CS8;
  return 0;
}
#endif

//--------------------------------------------------------------------------
// Write an array of bytes to the COM port, verify that it was
// sent out.  Assume that baud rate has been set.
//
// Returns 1 for success and 0 for failure
//   
int WriteCOM(int outlen, uchar *outbuf)
{
   long count = outlen;
   int i;
   int	sts;

   if (MLanDebug) {
       fprintf(stderr, "WriteCOM: calling write: %d bytes:", outlen);
       for (i = 0; i < outlen; i++) fprintf(stderr, " %02x", 0xff & outbuf[i]);
       fputc('\n', stderr);
       fflush(stderr);
   }

   i = write(fd, outbuf, outlen);

   if (MLanDebug) {
       fprintf(stderr, "WriteCOM: write returns %d\nWriteCOM: calling tcdrain\n", i);
       fflush(stderr);
   }

   sts = tcdrain(fd);        

   if (MLanDebug) {
       fprintf(stderr, "WriteCOM: tcdrain returns %d\n", sts);
       fflush(stderr);
   }

   return (i == count);
}  


//--------------------------------------------------------------------------
// Read an array of bytes to the COM port, verify that it was
// sent out.  Assume that baud rate has been set.
//
// Returns number of characters read
//
int ReadCOM(int inlen, uchar *inbuf)
{  
   fd_set         filedescr;
   struct timeval tval;
   int            cnt; 
   int		  sts;

   if (MLanDebug) {
       fprintf(stderr, "ReadCOM: calling read: want %d bytes:", inlen);
       fflush(stderr);
   }

   // loop to wait until each byte is available and read it
   for (cnt = 0; cnt < inlen; cnt++)
   {
      // set a descriptor to wait for a character available
      FD_ZERO(&filedescr);
      FD_SET(fd,&filedescr);
      // set timeout to 10ms  
      tval.tv_sec = 0;
      tval.tv_usec = 10000;

      // if byte available read or return bytes read 
      if (0 != select(fd+1,&filedescr,NULL,NULL,&tval)) {
         if ((sts = read(fd,&inbuf[cnt],1)) != 1) {
	    if (MLanDebug) {
	       fprintf(stderr, ": read returns %d, got %d bytes\n", sts, cnt);
	       fflush(stderr);
	    }
            return cnt;
	 }
         if (MLanDebug) {
	    fprintf(stderr, " %02x", 0xff & inbuf[cnt]);
	    fflush(stderr);
	 }
      }
      else {
	 if (MLanDebug) {
	    fprintf(stderr, ": select timeout, got %d bytes\n", cnt);
	    fflush(stderr);
	 }
         return cnt;
      }
      
   }
   
   // success, so return desired length
   if (MLanDebug) {
      fprintf(stderr, ": got 'em all\n");
      fflush(stderr);
   }
   return inlen;
}


//---------------------------------------------------------------------------
//  Description:
//     flush the rx and tx buffers
//
void FlushCOM(void)    
{    
   tcflush (fd, TCIOFLUSH);  
}  


//--------------------------------------------------------------------------
//  Description:
//     Delay for at least 'len' ms
// 
void msDelay(int len)
{
   struct timespec s;              // Set aside memory space on the stack      

   s.tv_sec = len / 1000;
   s.tv_nsec = (len - (s.tv_sec * 1000)) * 1000000;   
   nanosleep(&s, NULL);
}


//--------------------------------------------------------------------------
//  Description:
//     Send a break on the com port for at least 2 ms
// 
void BreakCOM(void)      
{
   int duration = 0;              // see man termios break may be 
   tcsendbreak(fd, duration);     // too long       
}


//---------------------------------------------------------------------------
// Attempt to open a com port.  
// Set the starting baud rate to 9600.
//
// 'port_zstr' - zero terminate port name.  Format is platform
//               dependent.
//
// Returns: TRUE - success, COM port opened
//
int OpenCOM(char *port_zstr)
{     
   struct termios t;               // see man termios - declared as above 
   int rc;

   fd = open(port_zstr, O_RDWR|O_NONBLOCK); 
   if (fd<0) return fd;
   rc = tcgetattr (fd, &t);
   if (rc < 0)
   {
      int tmp;
      tmp = oserror();
      close(fd);
      setoserror(tmp);
      return rc;
   }
   if (MLanDebug) {
       fprintf(stderr, "OpenCOM: initial tty settings\niflag: %07o oflag: %07o lflag: %07o cflag: %07o\n", (unsigned int)t.c_iflag, (unsigned int)t.c_oflag, (unsigned int)t.c_lflag, (unsigned int)t.c_cflag);
       fflush(stderr);
   }

   cfsetospeed(&t, B9600);
   cfsetispeed (&t, B9600);

#ifdef IRIX
// IRIX tty games ...
//
// per the Linux man page cfmakeraw sets the terminal attributes as follows:
//
   t.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
   t.c_iflag &= ~(ISTRIP|INLCR|IGNCR|ICRNL|IXON);
   t.c_oflag &= ~OPOST;
   t.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
   t.c_cflag &= ~(CSIZE|PARENB);
   t.c_cflag |= CS8;
// to this we have to clear the additional IRIX goodies like:
//
   t.c_iflag &= ~(IXANY|IGNPAR);
   t.c_oflag &= ~(TAB3);
// and finally, CLOCAL is in c_cflag, not c_lflag
//
   t.c_cflag |= CLOCAL;            // ignore modem signals
#else
// assumed to be Linux
//
   cfmakeraw(&t);                  // don't generate signals, translate or echo
   t.c_lflag |= CLOCAL;            // ignore modem signals
#endif

   if (MLanDebug) {
       fprintf(stderr, "OpenCOM: new tty settings\niflag: %07o oflag: %07o lflag: %07o cflag: %07o\n", (unsigned int)t.c_iflag, (unsigned int)t.c_oflag, (unsigned int)t.c_lflag, (unsigned int)t.c_cflag);
       fprintf(stderr, "OpenCOM: calling tcsetattr\n");
       fflush(stderr);
   }
   rc = tcsetattr (fd, TCSAFLUSH, &t);
   if (MLanDebug) {
       fprintf(stderr, "OpenCOM: tcsetattr returns %d\n", rc);
       fflush(stderr);
   }
   if (rc < 0)
   {
      int tmp;
      tmp = oserror();
      close(fd);
      setoserror(tmp);
      return rc;
   }

   return fd; 
}


//---------------------------------------------------------------------------
// Closes the connection to the port.
//
void CloseCOM(void)
{
   FlushCOM();
   close(fd);
}


//--------------------------------------------------------------------------
// Set the baud rate on the com port.  The possible baud rates for
// 'new_baud' are:
//
// PARMSET_9600     0x00
// PARMSET_19200    0x02
// PARMSET_57600    0x04
// PARMSET_115200   0x06
// 
void SetBaudCOM(int new_baud)
{
   struct termios t;               
   int rc;
   speed_t baud = B0;

   // read the attribute structure
   rc = tcgetattr(fd, &t);
   if (rc < 0)
   {
      close(fd);
      return;
   }

   // convert parameter to linux baud rate
   switch(new_baud)
   {
      case PARMSET_9600:
         baud = B9600;
         break;
      case PARMSET_19200:
         baud = B19200;
         break;
      case PARMSET_57600:
#ifdef B57600
         baud = B57600;
         break;
#else
#define ERR_MSG_57600 "SetBaudCOM: no support for 57600 baud, sorry!"
	 write(2, ERR_MSG_57600, strlen(ERR_MSG_57600));
	 exit(1);
#endif
      case PARMSET_115200:
#ifdef B115200
         baud = B115200;
         break;
#else
#define ERR_MSG_115200 "SetBaudCOM: no support for 115200 baud, sorry!"
	 write(2, ERR_MSG_115200, strlen(ERR_MSG_115200));
	 exit(1);
#endif
   }

   // set baud in structure 
   cfsetospeed(&t, baud);
   cfsetispeed(&t, baud);
  
   // change baud on port 
   rc = tcsetattr(fd, TCSAFLUSH, &t);
   if (rc < 0)
      close(fd);
}


//--------------------------------------------------------------------------
// Get the current millisecond tick count.  Does not have to represent
// an actual time, it just needs to be an incrementing timer.
//
long msGettick(void)
{
   struct timezone tmzone;
   struct timeval  tmval;
   long ms; 

   gettimeofday(&tmval,&tmzone);
   ms = (tmval.tv_sec & 0xFFFF) * 1000 + tmval.tv_usec / 1000;   
   return ms;
}