Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: blinky_max32630fthr
features/FEATURE_COMMON_PAL/mbed-client-randlib/linux/randLIB.c@3:1198227e6421, 2016-12-16 (annotated)
- Committer:
- switches
- Date:
- Fri Dec 16 16:27:57 2016 +0000
- Revision:
- 3:1198227e6421
- Parent:
- 0:5c4d7b2438d3
Changed ADC scale for MAX32625 platforms to 1.2V full scale to match MAX32630 platforms
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| switches | 0:5c4d7b2438d3 | 1 | /* |
| switches | 0:5c4d7b2438d3 | 2 | * Copyright (c) 2014-2015 ARM Limited. All rights reserved. |
| switches | 0:5c4d7b2438d3 | 3 | * SPDX-License-Identifier: Apache-2.0 |
| switches | 0:5c4d7b2438d3 | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
| switches | 0:5c4d7b2438d3 | 5 | * not use this file except in compliance with the License. |
| switches | 0:5c4d7b2438d3 | 6 | * You may obtain a copy of the License at |
| switches | 0:5c4d7b2438d3 | 7 | * |
| switches | 0:5c4d7b2438d3 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| switches | 0:5c4d7b2438d3 | 9 | * |
| switches | 0:5c4d7b2438d3 | 10 | * Unless required by applicable law or agreed to in writing, software |
| switches | 0:5c4d7b2438d3 | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| switches | 0:5c4d7b2438d3 | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| switches | 0:5c4d7b2438d3 | 13 | * See the License for the specific language governing permissions and |
| switches | 0:5c4d7b2438d3 | 14 | * limitations under the License. |
| switches | 0:5c4d7b2438d3 | 15 | */ |
| switches | 0:5c4d7b2438d3 | 16 | #include <stdint.h> |
| switches | 0:5c4d7b2438d3 | 17 | #include <stdlib.h> |
| switches | 0:5c4d7b2438d3 | 18 | #include <sys/types.h> |
| switches | 0:5c4d7b2438d3 | 19 | #include <sys/stat.h> |
| switches | 0:5c4d7b2438d3 | 20 | #include <fcntl.h> |
| switches | 0:5c4d7b2438d3 | 21 | #include <unistd.h> |
| switches | 0:5c4d7b2438d3 | 22 | #include "randLIB.h" |
| switches | 0:5c4d7b2438d3 | 23 | |
| switches | 0:5c4d7b2438d3 | 24 | /** |
| switches | 0:5c4d7b2438d3 | 25 | * \brief Init seed for Pseudo Random. |
| switches | 0:5c4d7b2438d3 | 26 | * On a Linux, this does nothing. |
| switches | 0:5c4d7b2438d3 | 27 | * |
| switches | 0:5c4d7b2438d3 | 28 | * \return None |
| switches | 0:5c4d7b2438d3 | 29 | * |
| switches | 0:5c4d7b2438d3 | 30 | */ |
| switches | 0:5c4d7b2438d3 | 31 | void randLIB_seed_random(void) |
| switches | 0:5c4d7b2438d3 | 32 | { |
| switches | 0:5c4d7b2438d3 | 33 | } |
| switches | 0:5c4d7b2438d3 | 34 | |
| switches | 0:5c4d7b2438d3 | 35 | /** |
| switches | 0:5c4d7b2438d3 | 36 | * \brief Generate 8-bit random number. |
| switches | 0:5c4d7b2438d3 | 37 | * |
| switches | 0:5c4d7b2438d3 | 38 | * \param None |
| switches | 0:5c4d7b2438d3 | 39 | * \return 8-bit random number |
| switches | 0:5c4d7b2438d3 | 40 | * |
| switches | 0:5c4d7b2438d3 | 41 | */ |
| switches | 0:5c4d7b2438d3 | 42 | uint8_t randLIB_get_8bit(void) |
| switches | 0:5c4d7b2438d3 | 43 | { |
| switches | 0:5c4d7b2438d3 | 44 | uint8_t ret_val; |
| switches | 0:5c4d7b2438d3 | 45 | randLIB_get_n_bytes_random(&ret_val, 1); |
| switches | 0:5c4d7b2438d3 | 46 | return ret_val; |
| switches | 0:5c4d7b2438d3 | 47 | } |
| switches | 0:5c4d7b2438d3 | 48 | |
| switches | 0:5c4d7b2438d3 | 49 | /** |
| switches | 0:5c4d7b2438d3 | 50 | * \brief Generate 16-bit random number. |
| switches | 0:5c4d7b2438d3 | 51 | * |
| switches | 0:5c4d7b2438d3 | 52 | * \param None |
| switches | 0:5c4d7b2438d3 | 53 | * \return 16-bit random number |
| switches | 0:5c4d7b2438d3 | 54 | * |
| switches | 0:5c4d7b2438d3 | 55 | */ |
| switches | 0:5c4d7b2438d3 | 56 | uint16_t randLIB_get_16bit(void) |
| switches | 0:5c4d7b2438d3 | 57 | { |
| switches | 0:5c4d7b2438d3 | 58 | uint16_t ret_val; |
| switches | 0:5c4d7b2438d3 | 59 | |
| switches | 0:5c4d7b2438d3 | 60 | randLIB_get_n_bytes_random((uint8_t*)&ret_val, 2); |
| switches | 0:5c4d7b2438d3 | 61 | return ret_val; |
| switches | 0:5c4d7b2438d3 | 62 | } |
| switches | 0:5c4d7b2438d3 | 63 | /** |
| switches | 0:5c4d7b2438d3 | 64 | * \brief Generate 32-bit random number. |
| switches | 0:5c4d7b2438d3 | 65 | * |
| switches | 0:5c4d7b2438d3 | 66 | * \param None |
| switches | 0:5c4d7b2438d3 | 67 | * \return 32-bit random number |
| switches | 0:5c4d7b2438d3 | 68 | * |
| switches | 0:5c4d7b2438d3 | 69 | */ |
| switches | 0:5c4d7b2438d3 | 70 | uint32_t randLIB_get_32bit(void) |
| switches | 0:5c4d7b2438d3 | 71 | { |
| switches | 0:5c4d7b2438d3 | 72 | uint32_t ret_val; |
| switches | 0:5c4d7b2438d3 | 73 | randLIB_get_n_bytes_random((uint8_t*)&ret_val, 4); |
| switches | 0:5c4d7b2438d3 | 74 | return ret_val; |
| switches | 0:5c4d7b2438d3 | 75 | } |
| switches | 0:5c4d7b2438d3 | 76 | |
| switches | 0:5c4d7b2438d3 | 77 | |
| switches | 0:5c4d7b2438d3 | 78 | /** |
| switches | 0:5c4d7b2438d3 | 79 | * \brief Generate n-bytes random numbers. |
| switches | 0:5c4d7b2438d3 | 80 | * |
| switches | 0:5c4d7b2438d3 | 81 | * \param data_ptr pointer where random will be stored |
| switches | 0:5c4d7b2438d3 | 82 | * \param eight_bit_boundary how many bytes need random |
| switches | 0:5c4d7b2438d3 | 83 | * \return 0 process valid |
| switches | 0:5c4d7b2438d3 | 84 | * \return -1 Unsupported Parameters or failed to get random data. |
| switches | 0:5c4d7b2438d3 | 85 | * |
| switches | 0:5c4d7b2438d3 | 86 | */ |
| switches | 0:5c4d7b2438d3 | 87 | int8_t randLIB_get_n_bytes_random(uint8_t *data_ptr, uint8_t eight_bit_boundary) |
| switches | 0:5c4d7b2438d3 | 88 | { |
| switches | 0:5c4d7b2438d3 | 89 | if ((data_ptr == 0) || (eight_bit_boundary == 0)) { |
| switches | 0:5c4d7b2438d3 | 90 | return -1; |
| switches | 0:5c4d7b2438d3 | 91 | } |
| switches | 0:5c4d7b2438d3 | 92 | |
| switches | 0:5c4d7b2438d3 | 93 | int fd = open("/dev/urandom", O_RDONLY); |
| switches | 0:5c4d7b2438d3 | 94 | if (fd != -1) { |
| switches | 0:5c4d7b2438d3 | 95 | size_t len = read(fd, data_ptr, eight_bit_boundary); |
| switches | 0:5c4d7b2438d3 | 96 | close(fd); |
| switches | 0:5c4d7b2438d3 | 97 | if (len == eight_bit_boundary) |
| switches | 0:5c4d7b2438d3 | 98 | return 0; |
| switches | 0:5c4d7b2438d3 | 99 | } |
| switches | 0:5c4d7b2438d3 | 100 | |
| switches | 0:5c4d7b2438d3 | 101 | return -1; |
| switches | 0:5c4d7b2438d3 | 102 | } |
| switches | 0:5c4d7b2438d3 | 103 | |
| switches | 0:5c4d7b2438d3 | 104 | /** |
| switches | 0:5c4d7b2438d3 | 105 | * \brief Generate a random number within a range. |
| switches | 0:5c4d7b2438d3 | 106 | * |
| switches | 0:5c4d7b2438d3 | 107 | * The result is linearly distributed in the range [min..max], inclusive. |
| switches | 0:5c4d7b2438d3 | 108 | * |
| switches | 0:5c4d7b2438d3 | 109 | * \param min minimum value that can be generated |
| switches | 0:5c4d7b2438d3 | 110 | * \param max maximum value that can be generated |
| switches | 0:5c4d7b2438d3 | 111 | */ |
| switches | 0:5c4d7b2438d3 | 112 | uint16_t randLIB_get_random_in_range(uint16_t min, uint16_t max) |
| switches | 0:5c4d7b2438d3 | 113 | { |
| switches | 0:5c4d7b2438d3 | 114 | /* This special case is potentially common, particularly in this routine's |
| switches | 0:5c4d7b2438d3 | 115 | * first user (Trickle), so worth catching immediately */ |
| switches | 0:5c4d7b2438d3 | 116 | if (min == max) { |
| switches | 0:5c4d7b2438d3 | 117 | return min; |
| switches | 0:5c4d7b2438d3 | 118 | } |
| switches | 0:5c4d7b2438d3 | 119 | |
| switches | 0:5c4d7b2438d3 | 120 | /* 16-bit arithmetic below fails in this extreme case; we can optimise it */ |
| switches | 0:5c4d7b2438d3 | 121 | if (max - min == 0xFFFF) { |
| switches | 0:5c4d7b2438d3 | 122 | return randLIB_get_16bit(); |
| switches | 0:5c4d7b2438d3 | 123 | } |
| switches | 0:5c4d7b2438d3 | 124 | |
| switches | 0:5c4d7b2438d3 | 125 | unsigned int values_needed = max + 1 - min; |
| switches | 0:5c4d7b2438d3 | 126 | unsigned int band_size = 0x10000u / values_needed; |
| switches | 0:5c4d7b2438d3 | 127 | unsigned int top_of_bands = band_size * values_needed; |
| switches | 0:5c4d7b2438d3 | 128 | unsigned int result; |
| switches | 0:5c4d7b2438d3 | 129 | do { |
| switches | 0:5c4d7b2438d3 | 130 | result = randLIB_get_16bit(); |
| switches | 0:5c4d7b2438d3 | 131 | } while (result >= top_of_bands); |
| switches | 0:5c4d7b2438d3 | 132 | |
| switches | 0:5c4d7b2438d3 | 133 | return min + (uint16_t)(result / band_size); |
| switches | 0:5c4d7b2438d3 | 134 | } |
| switches | 0:5c4d7b2438d3 | 135 | |
| switches | 0:5c4d7b2438d3 | 136 | /** |
| switches | 0:5c4d7b2438d3 | 137 | * \brief Randomise a base 32-bit number by a jitter factor |
| switches | 0:5c4d7b2438d3 | 138 | * |
| switches | 0:5c4d7b2438d3 | 139 | * The result is linearly distributed in the jitter range, which is expressed |
| switches | 0:5c4d7b2438d3 | 140 | * as fixed-point unsigned 1.15 values. For example, to produce a number in the |
| switches | 0:5c4d7b2438d3 | 141 | * range [0.75 * base, 1.25 * base], set min_factor to 0x6000 and max_factor to |
| switches | 0:5c4d7b2438d3 | 142 | * 0xA000. |
| switches | 0:5c4d7b2438d3 | 143 | * |
| switches | 0:5c4d7b2438d3 | 144 | * Result is clamped to 0xFFFFFFFF if it overflows. |
| switches | 0:5c4d7b2438d3 | 145 | * |
| switches | 0:5c4d7b2438d3 | 146 | * \param base The base 32-bit value |
| switches | 0:5c4d7b2438d3 | 147 | * \param min_factor The minimum value for the random factor |
| switches | 0:5c4d7b2438d3 | 148 | * \param max_factor The maximum value for the random factor |
| switches | 0:5c4d7b2438d3 | 149 | */ |
| switches | 0:5c4d7b2438d3 | 150 | uint32_t randLIB_randomise_base(uint32_t base, uint16_t min_factor, uint16_t max_factor) |
| switches | 0:5c4d7b2438d3 | 151 | { |
| switches | 0:5c4d7b2438d3 | 152 | uint16_t random_factor = randLIB_get_random_in_range(min_factor, max_factor); |
| switches | 0:5c4d7b2438d3 | 153 | |
| switches | 0:5c4d7b2438d3 | 154 | /* 32x16-bit long multiplication, to get 48-bit result */ |
| switches | 0:5c4d7b2438d3 | 155 | uint32_t hi = (base >> 16) * random_factor; |
| switches | 0:5c4d7b2438d3 | 156 | uint32_t lo = (base & 0xFFFF) * random_factor; |
| switches | 0:5c4d7b2438d3 | 157 | /* Add halves, and take top 32 bits of 48-bit result */ |
| switches | 0:5c4d7b2438d3 | 158 | uint32_t res = hi + (lo >> 16); |
| switches | 0:5c4d7b2438d3 | 159 | |
| switches | 0:5c4d7b2438d3 | 160 | /* Randomisation factor is *2^15, so need to shift up 1 more bit, avoiding overflow */ |
| switches | 0:5c4d7b2438d3 | 161 | if (res & 0x80000000) { |
| switches | 0:5c4d7b2438d3 | 162 | res = 0xFFFFFFFF; |
| switches | 0:5c4d7b2438d3 | 163 | } else { |
| switches | 0:5c4d7b2438d3 | 164 | res = (res << 1) | ((lo >> 15) & 1); |
| switches | 0:5c4d7b2438d3 | 165 | } |
| switches | 0:5c4d7b2438d3 | 166 | |
| switches | 0:5c4d7b2438d3 | 167 | return res; |
| switches | 0:5c4d7b2438d3 | 168 | } |
