Provide a replacement for rand() which has a better random source.
Change-Id: I38a8c74d86b89ec160b43b0f648f53b463be89bb
if (!valid_bda)
{
bdstr_t bdstr;
- /* Seed the random number generator */
- srand((unsigned int) (time(0)));
/* No autogen BDA. Generate one now. */
local_addr->address[0] = 0x22;
local_addr->address[1] = 0x22;
- local_addr->address[2] = (uint8_t) ((rand() >> 8) & 0xFF);
- local_addr->address[3] = (uint8_t) ((rand() >> 8) & 0xFF);
- local_addr->address[4] = (uint8_t) ((rand() >> 8) & 0xFF);
- local_addr->address[5] = (uint8_t) ((rand() >> 8) & 0xFF);
+ local_addr->address[2] = (uint8_t) osi_rand();
+ local_addr->address[3] = (uint8_t) osi_rand();
+ local_addr->address[4] = (uint8_t) osi_rand();
+ local_addr->address[5] = (uint8_t) osi_rand();
/* Convert to ascii, and store as a persistent property */
bdaddr_to_string(local_addr, bdstr, sizeof(bdstr));
./src/list.c \
./src/metrics.cpp \
./src/mutex.c \
+ ./src/osi.c \
./src/properties.c \
./src/reactor.c \
./src/ringbuffer.c \
./test/hash_map_utils_test.cpp \
./test/list_test.cpp \
./test/properties_test.cpp \
+ ./test/rand_test.cpp \
./test/reactor_test.cpp \
./test/ringbuffer_test.cpp \
./test/semaphore_test.cpp \
"src/list.c",
"src/metrics_linux.cpp",
"src/mutex.c",
+ "src/osi.c",
"src/properties.c",
"src/reactor.c",
"src/ringbuffer.c",
"test/hash_map_utils_test.cpp",
"test/list_test.cpp",
"test/properties_test.cpp",
+ "test/rand_test.cpp",
"test/reactor_test.cpp",
"test/ringbuffer_test.cpp",
"test/thread_test.cpp",
+/******************************************************************************
+ *
+ * Copyright (C) 2016 Google, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************************/
+
#pragma once
#include <stdbool.h>
#define PTR_TO_INT(p) ((int) ((intptr_t) (p)))
#define INT_TO_PTR(i) ((void *) ((intptr_t) (i)))
+
+// Obtain a random number between 0 and INT_MAX inclusive.
+// Taken from a system random source such as /dev/random.
+// No guarantees of distribution are made.
+// Effort is made for this to come from a real random source.
+int osi_rand(void);
--- /dev/null
+/******************************************************************************
+ *
+ * Copyright (C) 2016 Google, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************************/
+
+#define LOG_TAG "bt_osi_rand"
+
+#include <assert.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "osi/include/log.h"
+#include "osi/include/osi.h"
+
+#define RANDOM_PATH "/dev/urandom"
+
+int osi_rand(void) {
+ int rand;
+ int rand_fd = open(RANDOM_PATH, O_RDONLY);
+
+ if (rand_fd == INVALID_FD) {
+ LOG_ERROR(LOG_TAG, "%s can't open rand fd %s: %s ", __func__, RANDOM_PATH,
+ strerror(errno));
+ assert(0);
+ }
+
+ ssize_t read_bytes = read(rand_fd, &rand, sizeof(rand));
+ close(rand_fd);
+
+ assert(read_bytes == sizeof(rand));
+
+ if (rand < 0)
+ rand = -rand;
+
+ return rand;
+}
--- /dev/null
+#include <gtest/gtest.h>
+
+#include "AllocationTestHarness.h"
+
+extern "C" {
+#include "osi/include/osi.h"
+}
+
+class RandTest : public AllocationTestHarness {};
+
+TEST_F(RandTest, test_rand) {
+ // We can't guarantee any distribution
+ // We'd like it to not crash though.
+ for (int i = 0; i < 10; i++) {
+ int x;
+ x = osi_rand();
+ EXPECT_TRUE(x >= 0);
+ }
+}