Starts building the TimerPool

- Renames Timer to TimerPool

Change-Id: Icef2fc8b791a9fb60111c44a690b3e9c51390c79
diff --git a/core/BUILD b/core/BUILD
index 8b28b01..7dd1807 100644
--- a/core/BUILD
+++ b/core/BUILD
@@ -24,13 +24,16 @@
     name = "event_loop",
     hdrs = [
         "include/chre/core/event_loop.h",
+        "include/chre/core/timer_pool.h",
     ],
     srcs = [
         "event_loop.cc",
+        "timer_pool.cc",
     ],
     deps = [
         "//core:event",
         "//core:nanoapp",
+        "//platform:system_timer",
     ],
 )
 
@@ -60,14 +63,3 @@
         "//platform:platform_nanoapp",
     ],
 )
-
-chre_cc_library(
-    name = "timer",
-    hdrs = [
-        "include/chre/core/timer.h",
-    ],
-    deps = [
-        "//chre_api:chre_api",
-        "//util:util",
-    ],
-)
diff --git a/core/event_loop.cc b/core/event_loop.cc
index 2fa0f18..bf829fd 100644
--- a/core/event_loop.cc
+++ b/core/event_loop.cc
@@ -22,6 +22,9 @@
 
 namespace chre {
 
+EventLoop::EventLoop()
+    : mTimerPool(*this) {}
+
 void EventLoop::run() {
   LOGI("EventLoop start");
   mRunning = true;
diff --git a/core/include/chre/core/event_loop.h b/core/include/chre/core/event_loop.h
index 6d194b9..0919ad9 100644
--- a/core/include/chre/core/event_loop.h
+++ b/core/include/chre/core/event_loop.h
@@ -23,6 +23,7 @@
 
 #include "chre/core/event.h"
 #include "chre/core/nanoapp.h"
+#include "chre/core/timer_pool.h"
 #include "chre/util/blocking_queue.h"
 #include "chre/util/non_copyable.h"
 #include "chre/util/synchronized_memory_pool.h"
@@ -34,6 +35,11 @@
  */
 class EventLoop : public NonCopyable {
  public:
+  /**
+   * Setup the event loop.
+   */
+  EventLoop();
+
   // TODO: need a clear delineation for which methods are safe to call from
   // threads other than the one calling run()... should include stop() and
   // postEvent()... everything else would decidedly be not thread-safe
@@ -121,6 +127,9 @@
   //! The memory pool to allocate incoming events from.
   SynchronizedMemoryPool<Event, kMaxEventCount> mEventPool;
 
+  //! The timer used schedule timed events for tasks running in this event loop.
+  TimerPool mTimerPool;
+
   // TODO: replace STL use with our own data structures
   std::vector<Nanoapp*> mNanoapps;
   BlockingQueue<Event*> mEvents;
diff --git a/core/include/chre/core/timer.h b/core/include/chre/core/timer.h
deleted file mode 100644
index d47bf2b..0000000
--- a/core/include/chre/core/timer.h
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * Copyright (C) 2016 The Android Open Source Project
- *
- * 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.
- */
-
-#ifndef CHRE_CORE_TIMER_H_
-#define CHRE_CORE_TIMER_H_
-
-// TODO: common timer module
-//  - provide callback interface, build delayed event capability on top
-//  - eventually, condense to single system timer (i.e. one that fires next),
-//    but for now, can map 1:1 into system timer
-//  - collection of pending timer events (list initially, but priority queue would be nice)
-
-#include "chre/platform/system_timer.h"
-
-namespace chre {
-
-/**
- * The type to use when referring to a timer instance.
- *
- * Note that this mirrors the CHRE API definition of a timer handle, so should
- * not be changed without appropriate consideration.
- */
-typedef uint32_t TimerHandle;
-
-class Timer {
- public:
-  // TODO: static method to invoke a callback after the specified time has elapsed.
-  // the callback should be invoked within the context of the calling taskrunner.
-  // this is the underlying API we would use to implement the corresponding CHRE API
-  static TimerHandle setTimer(uint64_t delayNs, uint64_t intervalNs=0);
-
-  // TODO: should also add methods here to:
-  //   - post an event after a delay
-  //   - invoke a callback in "unsafe" context (i.e. from other thread), which the
-  //     CHRE system could use to trigger things while the task runner is busy
-
- private:
-  // TODO: eventually, just use one SystemTimer on the bottom end here
-};
-
-}  // namespace chre
-
-#endif  // CHRE_CORE_TIMER_H_
diff --git a/core/include/chre/core/timer_pool.h b/core/include/chre/core/timer_pool.h
new file mode 100644
index 0000000..19ac180
--- /dev/null
+++ b/core/include/chre/core/timer_pool.h
@@ -0,0 +1,107 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * 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.
+ */
+
+#ifndef CHRE_CORE_TIMER_POOL_H_
+#define CHRE_CORE_TIMER_POOL_H_
+
+// TODO: common timer module
+//  - provide callback interface, build delayed event capability on top
+//  - eventually, condense to single system timer (i.e. one that fires next),
+//    but for now, can map 1:1 into system timer
+//  - collection of pending timer events (list initially, but priority queue would be nice)
+
+#include "chre/platform/system_timer.h"
+#include "chre/util/dynamic_vector.h"
+
+namespace chre {
+
+/**
+ * The type to use when referring to a timer instance.
+ *
+ * Note that this mirrors the CHRE API definition of a timer handle, so should
+ * not be changed without appropriate consideration.
+ */
+typedef uint32_t TimerHandle;
+
+//! Forward declare the EventLoop class to avoid a circular dependency between
+//! TimerPool and EventLoop.
+class EventLoop;
+
+/**
+ * Tracks requests from CHRE apps for timed events.
+ */
+class TimerPool {
+ public:
+  /**
+   * Sets up the timer instance initial conditions.
+   *
+   * @param The event loop that owns this timer.
+   */
+  TimerPool(EventLoop& eventLoop);
+
+  /**
+   * Requests a timer for the currently running nanoapp given a cookie to pass
+   * to the nanoapp when the timer event is published.
+   *
+   * @param The duration of the timer.
+   * @param A cookie to pass to the app when the timer elapses.
+   * @param Whether or not the timer should automatically re-arm.
+   */
+  TimerHandle setTimer(Nanoseconds duration, void *cookie, bool oneShot);
+
+  // TODO: should also add methods here to:
+  //   - post an event after a delay
+  //   - invoke a callback in "unsafe" context (i.e. from other thread), which the
+  //     CHRE system could use to trigger things while the task runner is busy
+
+ private:
+  /**
+   * Tracks metadata associated with a request for a timed event.
+   */
+  struct TimerRequest {
+    //! The nanoapp from which this request was made.
+    Nanoapp *requestingNanoapp;
+
+    //! The TimerHandle assigned to this request.
+    TimerHandle timerHandle;
+
+    //! The time when the request was made.
+    Nanoseconds expirationTime;
+
+    //! The requested duration of the timer.
+    Nanoseconds duration;
+
+    //! Whether or not the request is a one shot or should be rescheduled.
+    bool isOneShot;
+
+    //! The cookie pointer to be passed as an event to the requesting nanoapp.
+    void *cookie;
+  };
+
+  //! The event loop that owns this timer pool.
+  EventLoop& mEventLoop;
+
+  //! The list of outstanding timer requests.
+  //TODO: Make this a priority queue.
+  DynamicVector<TimerRequest> mTimerRequests;
+
+  //! The underlying system timer used to schedule delayed callbacks.
+  SystemTimer mSystemTimer;
+};
+
+}  // namespace chre
+
+#endif  // CHRE_CORE_TIMER_POOL_H_
diff --git a/core/timer_pool.cc b/core/timer_pool.cc
new file mode 100644
index 0000000..f1a7b7b
--- /dev/null
+++ b/core/timer_pool.cc
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * 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.
+ */
+
+#include "chre/core/event_loop.h"
+#include "chre/core/timer_pool.h"
+
+namespace chre {
+
+TimerPool::TimerPool(EventLoop& eventLoop)
+    : mEventLoop(eventLoop) {}
+
+TimerHandle TimerPool::setTimer(Nanoseconds duration, void *cookie,
+    bool isOneShot) {
+  bool mTimerIsActive = false; // TODO: Remove this.
+  if (mTimerIsActive) {
+    // TODO: Push the request onto the list of requests.
+  } else {
+    // TODO: Immediately request the timer and set mTimerIsActive to true.
+  }
+
+  // TODO: Implement this.
+  return 0;
+}
+
+}  // namespace chre
diff --git a/platform/BUILD b/platform/BUILD
index 78d2ec1..46aa536 100644
--- a/platform/BUILD
+++ b/platform/BUILD
@@ -13,10 +13,6 @@
 #
 ################################################################################
 
-LINUX_PLATFORM_HDRS = [
-    "linux/include/chre/target_platform/system_timer_base.h",
-]
-
 LINUX_PLATFORM_INCLUDES = [
     "linux/include",
 ]
@@ -30,7 +26,6 @@
     "linux/chre_api_re.cc",
     "linux/chre_api_version.cc",
     "linux/system_time.cc",
-    "linux/system_timer.cc",
     "shared/chre_api_re.cc",
     "shared/memory.cc",
     "shared/system_time.cc",
@@ -42,11 +37,7 @@
         "include/chre/platform/context.h",
         "include/chre/platform/memory.h",
         "include/chre/platform/system_time.h",
-        "include/chre/platform/system_timer.h",
-    ] + select({
-        "//:platform_linux" : LINUX_PLATFORM_HDRS,
-        "//conditions:default" : LINUX_PLATFORM_HDRS,
-    }),
+    ],
     srcs = select({
         "//:platform_linux" : LINUX_PLATFORM_SRCS,
         "//conditions:default" : LINUX_PLATFORM_SRCS,
@@ -205,6 +196,43 @@
 
 ################################################################################
 #
+# Platform System Timer
+#
+################################################################################
+
+LINUX_PLATFORM_SYSTEM_TIMER_HDRS = [
+    "linux/include/chre/target_platform/system_timer_base.h",
+]
+
+
+LINUX_PLATFORM_SYSTEM_TIMER_SRCS = [
+    "linux/system_timer.cc",
+]
+
+chre_cc_library(
+    name = "system_timer",
+    hdrs = [
+        "include/chre/platform/system_timer.h",
+    ] + select({
+        "//:platform_linux" : LINUX_PLATFORM_SYSTEM_TIMER_HDRS,
+        "//conditions:default" : LINUX_PLATFORM_SYSTEM_TIMER_HDRS,
+    }),
+    srcs = select({
+        "//:platform_linux" : LINUX_PLATFORM_SYSTEM_TIMER_SRCS,
+        "//conditions:default" : LINUX_PLATFORM_SYSTEM_TIMER_SRCS,
+    }),
+    includes = select({
+        "//:platform_linux" : LINUX_PLATFORM_INCLUDES,
+        "//conditions:default" : LINUX_PLATFORM_INCLUDES,
+    }),
+    deps = [
+        "//util:non_copyable",
+        "//util:util",
+    ],
+)
+
+################################################################################
+#
 # Platform Binary
 #
 ################################################################################
@@ -224,6 +252,7 @@
         "//core:init",
         "//core:nanoapp",
         "//platform:platform",
+        "//platform:system_timer",
         "//platform:log",
     ],
 )
diff --git a/platform/include/chre/platform/system_timer.h b/platform/include/chre/platform/system_timer.h
index ae97e0f..e1ffe1a 100644
--- a/platform/include/chre/platform/system_timer.h
+++ b/platform/include/chre/platform/system_timer.h
@@ -99,6 +99,6 @@
   void *mData;
 };
 
-}
+}  // namespace chre
 
 #endif  // CHRE_PLATFORM_TIMER_H_