Add enforcement of module dependency declaration

Move dependency access into module, enforce dependency was listed
in ListDependencies.

Remove ModuleRegistry from Start/Stop.

Test: atest --host bluetooth_test_gd and cert/run_cert.sh
Change-Id: If5879a76aae764286f1b5bd7df2b497ea8f51998
diff --git a/gd/module.h b/gd/module.h
index fc3292d..e760279 100644
--- a/gd/module.h
+++ b/gd/module.h
@@ -40,6 +40,7 @@
 
 class ModuleList {
  friend ModuleRegistry;
+ friend Module;
  public:
   template <class T>
   void add() {
@@ -65,28 +66,35 @@
   // Populate the provided list with modules that must start before yours
   virtual void ListDependencies(ModuleList* list) = 0;
 
-  // You can grab your started dependencies from the registry in this call
-  virtual void Start(const ModuleRegistry* registry) = 0;
+  // You can grab your started dependencies during or after this call
+  // using GetDependency(), or access the module registry via GetModuleRegistry()
+  virtual void Start() = 0;
 
   // Release all resources, you're about to be deleted
-  virtual void Stop(const ModuleRegistry* registry) = 0;
+  virtual void Stop() = 0;
 
   ::bluetooth::os::Handler* GetHandler();
 
+  ModuleRegistry* GetModuleRegistry();
+
+  template <class T>
+  T* GetDependency() const {
+    return static_cast<T*>(GetDependency(&T::Factory));
+  }
+
  private:
+  Module* GetDependency(const ModuleFactory* module) const;
+
   ::bluetooth::os::Handler* handler_;
+  ModuleList dependencies_;
+  ModuleRegistry* registry_;
 };
 
 class ModuleRegistry {
+ friend Module;
+ friend class StackManager;
  public:
   template <class T>
-  T* GetInstance() const {
-    auto instance = started_modules_.find(&T::Factory);
-    ASSERT(instance != started_modules_.end());
-    return static_cast<T *>(instance->second);
-  };
-
-  template <class T>
   bool IsStarted() const {
     return IsStarted(&T::Factory);
   }
@@ -98,16 +106,18 @@
   void Start(ModuleList* modules, ::bluetooth::os::Thread* thread);
 
   template <class T>
-  void Start(::bluetooth::os::Thread* thread) {
-    Start(&T::Factory, thread);
+  T* Start(::bluetooth::os::Thread* thread) {
+    return static_cast<T*>(Start(&T::Factory, thread));
   }
 
-  void Start(const ModuleFactory* id, ::bluetooth::os::Thread* thread);
+  Module* Start(const ModuleFactory* id, ::bluetooth::os::Thread* thread);
 
   // Stop all running modules in reverse order of start
   void StopAll();
 
  private:
+  Module* Get(const ModuleFactory* module) const;
+
   std::map<const ModuleFactory*, Module*> started_modules_;
   std::vector<const ModuleFactory*> start_order_;
 };