From 2ddc807a674613d7a5d2b1409a00bb8202681784 Mon Sep 17 00:00:00 2001 From: liang_mike Date: Thu, 10 Sep 2009 20:00:12 +0000 Subject: [PATCH] Add TOSThreads C API for random number generator The code is from Jo Agila Bitsch Link --- tos/lib/tosthreads/csystem/CRandomC.nc | 35 ++++++++++++++ tos/lib/tosthreads/csystem/CRandomP.nc | 46 +++++++++++++++++++ tos/lib/tosthreads/csystem/tosthread_random.h | 36 +++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 tos/lib/tosthreads/csystem/CRandomC.nc create mode 100644 tos/lib/tosthreads/csystem/CRandomP.nc create mode 100644 tos/lib/tosthreads/csystem/tosthread_random.h diff --git a/tos/lib/tosthreads/csystem/CRandomC.nc b/tos/lib/tosthreads/csystem/CRandomC.nc new file mode 100644 index 00000000..15b2bcb1 --- /dev/null +++ b/tos/lib/tosthreads/csystem/CRandomC.nc @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2009 RWTH Aachen University. + * All rights reserved. + * + * Permission to use, copy, modify, and distribute this software and its + * documentation for any purpose, without fee, and without written + * agreement is hereby granted, provided that the above copyright + * notice, the (updated) modification history and the author appear in + * all copies of this source code. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, LOSS OF USE, DATA, + * OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. +*/ + +/** + * @author J— çgila Bitsch Link + */ + +configuration CRandomC {} + +implementation { + components CRandomP, + RandomC; + + CRandomP.Random -> RandomC; + CRandomP.SeedInit -> RandomC; +} diff --git a/tos/lib/tosthreads/csystem/CRandomP.nc b/tos/lib/tosthreads/csystem/CRandomP.nc new file mode 100644 index 00000000..348836c5 --- /dev/null +++ b/tos/lib/tosthreads/csystem/CRandomP.nc @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2009 RWTH Aachen University. + * All rights reserved. + * + * Permission to use, copy, modify, and distribute this software and its + * documentation for any purpose, without fee, and without written + * agreement is hereby granted, provided that the above copyright + * notice, the (updated) modification history and the author appear in + * all copies of this source code. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, LOSS OF USE, DATA, + * OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. +*/ + +/** + * @author J— çgila Bitsch Link + */ + +module CRandomP { + uses { + interface ParameterInit as SeedInit; + interface Random; + } +} + +implementation { + error_t randSeed(uint16_t seed) @C() AT_SPONTANEOUS { + return call SeedInit.init(seed); + } + + uint16_t rand16() @C() AT_SPONTANEOUS { + return call Random.rand16(); + } + + uint32_t rand32() @C() AT_SPONTANEOUS { + return call Random.rand32(); + } +} diff --git a/tos/lib/tosthreads/csystem/tosthread_random.h b/tos/lib/tosthreads/csystem/tosthread_random.h new file mode 100644 index 00000000..fb8e54df --- /dev/null +++ b/tos/lib/tosthreads/csystem/tosthread_random.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2009 RWTH Aachen University. + * All rights reserved. + * + * Permission to use, copy, modify, and distribute this software and its + * documentation for any purpose, without fee, and without written + * agreement is hereby granted, provided that the above copyright + * notice, the (updated) modification history and the author appear in + * all copies of this source code. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, LOSS OF USE, DATA, + * OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. +*/ + +/** + * @author J— çgila Bitsch Link + */ + +#ifndef TOSTHREAD_RANDOM_H +#define TOSTHREAD_RANDOM_H + +#include "TinyError.h" + +extern error_t randSeed(uint16_t seed); +extern uint16_t rand16(); +extern uint32_t rand32(); + +#endif //TOSTHREAD_RANDOM_H -- 2.39.2