Skip to content

Commit

Permalink
Debugging: removed loop reset, to avoid distribution anomaly. Backpor…
Browse files Browse the repository at this point in the history
…ted improvements from gen 3.
  • Loading branch information
gbonacini committed Jan 13, 2024
1 parent 010d5bf commit bb32b0f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 20 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ nuclear rng gen 2 (0.1.3) any; urgency=low
Debugging.
-- Gabriele Bonacini <[email protected]> Thu, 11 Jan 2023 03:23:32 -0100

nuclear rng gen 2 (0.1.4) any; urgency=low

* Beta Test
Debugging: removed loop reset, to avoid distribution anomaly. Backported improvements from gen 3.
-- Gabriele Bonacini <[email protected]> Sat, 13 Jan 2023 22:15:22 -0100
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,18 @@ req
```
* Then you'll receive a RN in an answer with the following format:
```shell
<random_number><separator><available_numbers><newline>
<random_number><separator><generator_number><separator><available_numbers><newline>
```
<sp><sp><sp>where:
- the first field is a random number in the range 0-15 or the number 16 if an error was generated or no number is available yet;
- the chosen range is 0-15 is convenient because the union of two generated random numbers represent a full random byte;
- the second field represent the original value of the register incremented in loop to extract the random number using module operator of integer division by the specific range (0-15), it's provided as safeguard to verify that the loop cover every possible value for a given event frequency;
- the separator is the character ':';
- then a field with an integer telling you how many RNs are available in the appliance buffer, ready to be requested;
- a newline ( '\n' ) ends the message.
* Example:
```shell
1:1393\n
52:3473460:1384\n
```

* You can terminate the connection with the command:
Expand Down
47 changes: 30 additions & 17 deletions geiger_gen2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
#include <iostream>
#include <array>
#include <deque>
#include <utility>
#include <string>
#include <algorithm>
#include <limits>

namespace geigergen2 {

Expand All @@ -39,14 +41,22 @@ namespace geigergen2 {
std::string,
std::to_string,
std::copy_n,
std::deque;
std::deque,
std::numeric_limits;

using rng=unsigned char;
using registry=unsigned int;
static_assert( numeric_limits<rng>::max() < numeric_limits<registry>::max() );
using Rng=std::pair<rng, registry>;

class GeigerGen2 {
public:
static inline const unsigned int MIN_RESULT { 0 },
MAX_RESULT { 15 },
INVALID_RESULT { MAX_RESULT + 1 };

static_assert( MIN_RESULT < MAX_RESULT );
static_assert( INVALID_RESULT < numeric_limits<registry>::max() );
static_assert( MAX_RESULT < INVALID_RESULT );

static GeigerGen2* getInstance(unsigned int pin,
Expand All @@ -55,12 +65,12 @@ namespace geigergen2 {
void init(void) noexcept;
static void abort(const char* msg) noexcept;
void detect(void) noexcept;
static unsigned char getRnd(void) noexcept;
static Rng getRnd(void) noexcept;
static size_t getAvailable(void) noexcept;

private:
static inline mutex_t rndMutex;
static inline deque<unsigned char> rndQueue;
static inline deque<Rng> rndQueue;
static inline const size_t MAX_QUEUE_LEN { 10240 };
static inline long count { 0L },
genCount { 0L },
Expand All @@ -70,7 +80,7 @@ namespace geigergen2 {
zerothreshold;

static inline GeigerGen2* instance { nullptr };
static inline unsigned char roulette { 0 },
static inline unsigned int roulette { 0 },
lastRnd { INVALID_RESULT };

explicit GeigerGen2(unsigned int pin,
Expand All @@ -89,8 +99,8 @@ namespace geigergen2 {
for(;;) sleep_ms(1000);
}

unsigned char GeigerGen2::getRnd(void) noexcept{
unsigned char ret { INVALID_RESULT };
Rng GeigerGen2::getRnd(void) noexcept{
Rng ret { INVALID_RESULT, 0 };
if( ! GeigerGen2::rndQueue.empty()){
mutex_enter_blocking(&GeigerGen2::rndMutex);
ret = GeigerGen2::rndQueue.front();
Expand Down Expand Up @@ -123,20 +133,21 @@ namespace geigergen2 {
auto detectionThread = [](){
for(;;){
uint16_t result { adc_read() };
if(result > vthreshold && GeigerGen2::rndQueue.size() <= GeigerGen2::MAX_QUEUE_LEN){
if(result > vthreshold){

mutex_enter_blocking(&GeigerGen2::rndMutex);
GeigerGen2::rndQueue.push_back(GeigerGen2::roulette);
if(GeigerGen2::rndQueue.size() > GeigerGen2::MAX_QUEUE_LEN) GeigerGen2::rndQueue.pop_front();
GeigerGen2::rndQueue.push_back({GeigerGen2::roulette % (MAX_RESULT + 1), GeigerGen2::roulette});
mutex_exit(&GeigerGen2::rndMutex);

for(;;){ result = adc_read();
if(result > zerothreshold ) sleep_us(100);
if(result > zerothreshold ) sleep_us(10);
else break;
}
sleep_us(100);
GeigerGen2::roulette = GeigerGen2::MIN_RESULT;
GeigerGen2::count++;
}
GeigerGen2::roulette++;
if(GeigerGen2::roulette > GeigerGen2::MAX_RESULT) GeigerGen2::roulette = GeigerGen2::MIN_RESULT;
}
};

Expand Down Expand Up @@ -288,30 +299,32 @@ err_t GeigerGen2NetworkLayer::serverRecvClbk(void *ctx, TcpPcb *tpcb, Pbuf* pb,
};
int par { ckeckReq() };
cerr << "ServerRecvClbk: detect type : " << par <<'\n';
err_t err { ERR_OK };
switch(par){
case 0:
{
cerr << "ServerRecvClbk: send for req\n";
unsigned char rndn { GeigerGen2::getRnd() };
string msg { to_string(rndn).append(":").append(to_string(GeigerGen2::getAvailable())).append("\n") };
Rng rndn { GeigerGen2::getRnd() };
string msg { to_string(rndn.first).append(":").append(to_string(rndn.second)).append(":")
.append(to_string(GeigerGen2::getAvailable())).append("\n") };

context->toSendLen = msg.size() <= context->bufferSend.size() ? msg.size() : context->bufferSend.size();
copy_n(msg.data(), context->toSendLen, context->bufferSend.data());
return serverSendData(context, context->client_pcb);
err = serverSendData(context, context->client_pcb);
}
break;
case 1:
cerr << "ServerRecvClbk: close for end\n";
clientClose(context);
err = clientClose(context);
break;
default:
cerr << "ServerRecvClbk: error\n";
clientClose(context);
err = clientClose(context);
}
}

cerr << "ServerRecvClbk : end \n";
return ERR_OK;
return err;
}

void GeigerGen2NetworkLayer::serverErrClbk(void *ctx, err_t err) noexcept{
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.3
0.1.4

0 comments on commit bb32b0f

Please sign in to comment.