summaryrefslogtreecommitdiff
path: root/osi
diff options
context:
space:
mode:
authorMarie Janssen <jamuraa@google.com>2016-03-10 11:27:53 -0800
committerAndre Eisenbach <eisenbach@google.com>2016-03-25 13:17:57 -0700
commit8d0a6a408ff57cb221e67963a95822f080911f20 (patch)
treea60f61a8615c6a0ad00820f5c26fcab1e05bafbd /osi
parent407b3bf182630e764ac9b34f806264a324a2e187 (diff)
downloadandroid-system-bt-8d0a6a408ff57cb221e67963a95822f080911f20.tar.gz
android-system-bt-8d0a6a408ff57cb221e67963a95822f080911f20.tar.xz
osi: add osi_rand()
Provide a replacement for rand() which has a better random source. Change-Id: I38a8c74d86b89ec160b43b0f648f53b463be89bb
Diffstat (limited to 'osi')
-rw-r--r--osi/Android.mk2
-rw-r--r--osi/BUILD.gn2
-rw-r--r--osi/include/osi.h24
-rw-r--r--osi/src/osi.c53
-rw-r--r--osi/test/rand_test.cpp19
5 files changed, 100 insertions, 0 deletions
diff --git a/osi/Android.mk b/osi/Android.mk
index 23b01f1..4d372ec 100644
--- a/osi/Android.mk
+++ b/osi/Android.mk
@@ -41,6 +41,7 @@ btosiCommonSrc := \
./src/list.c \
./src/metrics.cpp \
./src/mutex.c \
+ ./src/osi.c \
./src/properties.c \
./src/reactor.c \
./src/ringbuffer.c \
@@ -68,6 +69,7 @@ btosiCommonTestSrc := \
./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 \
diff --git a/osi/BUILD.gn b/osi/BUILD.gn
index 88cdbdd..ed91fb1 100644
--- a/osi/BUILD.gn
+++ b/osi/BUILD.gn
@@ -33,6 +33,7 @@ static_library("osi") {
"src/list.c",
"src/metrics_linux.cpp",
"src/mutex.c",
+ "src/osi.c",
"src/properties.c",
"src/reactor.c",
"src/ringbuffer.c",
@@ -72,6 +73,7 @@ executable("net_test_osi") {
"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",
diff --git a/osi/include/osi.h b/osi/include/osi.h
index d2cc0a3..8cdf808 100644
--- a/osi/include/osi.h
+++ b/osi/include/osi.h
@@ -1,3 +1,21 @@
+/******************************************************************************
+ *
+ * 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>
@@ -35,3 +53,9 @@
#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);
diff --git a/osi/src/osi.c b/osi/src/osi.c
new file mode 100644
index 0000000..2a6a779
--- /dev/null
+++ b/osi/src/osi.c
@@ -0,0 +1,53 @@
+/******************************************************************************
+ *
+ * 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;
+}
diff --git a/osi/test/rand_test.cpp b/osi/test/rand_test.cpp
new file mode 100644
index 0000000..cf0d7f6
--- /dev/null
+++ b/osi/test/rand_test.cpp
@@ -0,0 +1,19 @@
+#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);
+ }
+}