SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
rtprandomurandom.cpp
Go to the documentation of this file.
1 /*
2 
3  This file is a part of JRTPLIB
4  Copyright (c) 1999-2017 Jori Liesenborgs
5 
6  Contact: jori.liesenborgs@gmail.com
7 
8  This library was developed at the Expertise Centre for Digital Media
9  (http://www.edm.uhasselt.be), a research center of the Hasselt University
10  (http://www.uhasselt.be). The library is based upon work done for
11  my thesis at the School for Knowledge Technology (Belgium/The Netherlands).
12 
13  Permission is hereby granted, free of charge, to any person obtaining a
14  copy of this software and associated documentation files (the "Software"),
15  to deal in the Software without restriction, including without limitation
16  the rights to use, copy, modify, merge, publish, distribute, sublicense,
17  and/or sell copies of the Software, and to permit persons to whom the
18  Software is furnished to do so, subject to the following conditions:
19 
20  The above copyright notice and this permission notice shall be included
21  in all copies or substantial portions of the Software.
22 
23  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
29  IN THE SOFTWARE.
30 
31  */
32 
33 #include "rtprandomurandom.h"
34 #include "rtperrors.h"
35 #include <Qt>
36 #include <cstdio>
37 
38 namespace qrtplib
39 {
40 
42 {
43  device = 0;
44 }
45 
47 {
48  if (device) {
49  fclose(device);
50  }
51 }
52 
54 {
55  if (device) {
57  }
58 
59  device = fopen("/dev/urandom", "rb");
60 
61  if (device == 0) {
63  }
64 
65  return 0;
66 }
67 
69 {
70  if (!device)
71  {
72  qWarning("RTPRandomURandom::GetRandom8: no device");
73  return 0;
74  }
75 
76  uint8_t value;
77 
78  if (fread(&value, 1, sizeof(uint8_t), device) != sizeof(uint8_t))
79  {
80  qWarning("RTPRandomURandom::GetRandom8: cannot read unsigned 8 bit value from device");
81  return 0;
82  }
83 
84  return value;
85 }
86 
88 {
89  if (!device)
90  {
91  qWarning("RTPRandomURandom::GetRandom16: no device");
92  return 0;
93  }
94 
95  uint16_t value;
96 
97  if (fread(&value, 1, sizeof(uint16_t), device) != sizeof(uint16_t))
98  {
99  qWarning("RTPRandomURandom::GetRandom16: cannot read unsigned 16 bit value from device");
100  return 0;
101  }
102 
103  return value;
104 }
105 
107 {
108  if (!device)
109  {
110  qWarning("RTPRandomURandom::GetRandom32: no device");
111  return 0;
112  }
113 
114  uint32_t value;
115 
116  if (fread(&value, 1, sizeof(uint32_t), device) != sizeof(uint32_t))
117  {
118  qWarning("RTPRandomURandom::GetRandom32: cannot read unsigned 32 bit value from device");
119  return 0;
120  }
121 
122  return value;
123 }
124 
126 {
127  if (!device)
128  {
129  qWarning("RTPRandomURandom::GetRandomDouble: no device");
130  return 0;
131  }
132 
133  uint64_t value;
134 
135  if (fread(&value, 1, sizeof(uint64_t), device) != sizeof(uint64_t))
136  {
137  qWarning("RTPRandomURandom::GetRandomDouble: cannot read unsigned 64 bit value from device");
138  return 0;
139  }
140 
141  value &= 0x7fffffffffffffffULL;
142  int64_t value2 = (int64_t) value;
143  double x = RTPRANDOM_2POWMIN63 * (double) value2;
144 
145  return x;
146 }
147 
148 } // end namespace
149 
#define ERR_RTP_RTPRANDOMURANDOM_CANTOPEN
Definition: rtperrors.h:188
#define RTPRANDOM_2POWMIN63
Definition: rtprandom.h:47
#define ERR_RTP_RTPRANDOMURANDOM_ALREADYOPEN
Definition: rtperrors.h:189
__int64 int64_t
Definition: rtptypes_win.h:47
unsigned int uint32_t
Definition: rtptypes_win.h:46
unsigned char uint8_t
Definition: rtptypes_win.h:42
unsigned short uint16_t
Definition: rtptypes_win.h:44
unsigned __int64 uint64_t
Definition: rtptypes_win.h:48