Change EmbOS API usage from v5.16 to v4.22

This necessary to match the version of an intended use case.

The Cl also includes the implementation of a function to
indicate CHRE's thread context.

Bug: 230134803
Test: Build archive, ensure no mismatched/unknown EmbOS symbols
show up during linking, load built firmware on target.

Change-Id: I383dd3d074d9546daa908fa7fc898233b40106c0
diff --git a/platform/embos/context.cc b/platform/embos/context.cc
new file mode 100644
index 0000000..54ea97b
--- /dev/null
+++ b/platform/embos/context.cc
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2022 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/platform/context.h"
+#include "chre/embos/init.h"
+
+#include <string.h>
+
+#include "RTOS.h"
+
+namespace chre {
+
+bool inEventLoopThread() {
+  bool rv = false;
+
+  OS_TASK *currentTask = OS_GetTaskID();
+  if (currentTask != nullptr) {
+    // Some task is executing or the scheduler has started.
+    const char *currentTaskName = OS_GetTaskName(currentTask);
+    if (currentTaskName != nullptr) {
+      // Current task has a name.
+      rv = strncmp(getChreTaskName(), currentTaskName, getChreTaskNameLen()) ==
+           0;
+    }
+  }
+
+  return rv;
+}
+
+}  // namespace chre
diff --git a/platform/embos/include/chre/target_platform/init.h b/platform/embos/include/chre/embos/init.h
similarity index 93%
rename from platform/embos/include/chre/target_platform/init.h
rename to platform/embos/include/chre/embos/init.h
index bb3f90b..ad91717 100644
--- a/platform/embos/include/chre/target_platform/init.h
+++ b/platform/embos/include/chre/embos/init.h
@@ -17,6 +17,8 @@
 #ifndef CHRE_PLATFORM_EMBOS_INIT_H_
 #define CHRE_PLATFORM_EMBOS_INIT_H_
 
+#include <stddef.h>
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -34,6 +36,10 @@
  */
 void chreEmbosDeinit();
 
+const char *getChreTaskName();
+
+size_t getChreTaskNameLen();
+
 #ifdef __cplusplus
 }  // extern "C"
 #endif
diff --git a/platform/embos/include/chre/target_platform/condition_variable_base.h b/platform/embos/include/chre/target_platform/condition_variable_base.h
index 1648e90..956f61b 100644
--- a/platform/embos/include/chre/target_platform/condition_variable_base.h
+++ b/platform/embos/include/chre/target_platform/condition_variable_base.h
@@ -21,9 +21,15 @@
 
 namespace chre {
 
+/**
+ * The EmbOS implementation of ConditionVariableBase.
+ *
+ * Note that this implementation is aimed at EmbOS v4.22.
+ */
+
 class ConditionVariableBase {
  protected:
-  OS_SEMAPHORE mCvSemaphore;
+  OS_CSEMA mCvSemaphore;
 };
 
 }  // namespace chre
diff --git a/platform/embos/include/chre/target_platform/condition_variable_impl.h b/platform/embos/include/chre/target_platform/condition_variable_impl.h
index 88f013c..10d3224 100644
--- a/platform/embos/include/chre/target_platform/condition_variable_impl.h
+++ b/platform/embos/include/chre/target_platform/condition_variable_impl.h
@@ -23,20 +23,20 @@
 namespace chre {
 
 inline ConditionVariable::ConditionVariable() {
-  OS_SEMAPHORE_CREATE(&mCvSemaphore);
+  OS_CREATECSEMA(&mCvSemaphore);
 }
 
 inline ConditionVariable::~ConditionVariable() {
-  OS_SEMAPHORE_Delete(&mCvSemaphore);
+  OS_DeleteCSema(&mCvSemaphore);
 }
 
 inline void ConditionVariable::notify_one() {
-  OS_SEMAPHORE_Give(&mCvSemaphore);
+  OS_SignalCSema(&mCvSemaphore);
 }
 
 inline void ConditionVariable::wait(Mutex &mutex) {
   mutex.unlock();
-  OS_SEMAPHORE_TakeBlocked(&mCvSemaphore);
+  OS_WaitCSema(&mCvSemaphore);
   mutex.lock();
 }
 
@@ -46,7 +46,7 @@
       static_cast<OS_TIME>(Milliseconds(timeout).getMilliseconds());
   if (timeoutTicks > 0) {
     mutex.unlock();
-    success = OS_SEMAPHORE_TakeTimed(&mCvSemaphore, timeoutTicks);
+    success = OS_WaitCSemaTimed(&mCvSemaphore, timeoutTicks);
     mutex.lock();
   }
   return success;
diff --git a/platform/embos/include/chre/target_platform/mutex_base.h b/platform/embos/include/chre/target_platform/mutex_base.h
index ff93190..9f8b73c 100644
--- a/platform/embos/include/chre/target_platform/mutex_base.h
+++ b/platform/embos/include/chre/target_platform/mutex_base.h
@@ -17,19 +17,20 @@
 #ifndef CHRE_PLATFORM_EMBOS_MUTEX_BASE_H_
 #define CHRE_PLATFORM_EMBOS_MUTEX_BASE_H_
 
-#include "chre/platform/fatal_error.h"
-#include "chre/platform/log.h"
-
 #include "RTOS.h"
 
 namespace chre {
 
 /**
- * The EmbOS implementation of MutexBase
+ * The EmbOS implementation of MutexBase.
+ *
+ * Note that the current implementation is aimed at EmbOS v4.22.
+ * A 'resource semaphore' is used to implement the mutex. It is not safe to
+ * do any Mutex operations from within an ISR.
  */
 class MutexBase {
  protected:
-  OS_MUTEX mMutex;
+  OS_RSEMA mResourceSemaphore;
 };
 
 }  // namespace chre
diff --git a/platform/embos/include/chre/target_platform/mutex_base_impl.h b/platform/embos/include/chre/target_platform/mutex_base_impl.h
index c8ffc6c..795d5d2 100644
--- a/platform/embos/include/chre/target_platform/mutex_base_impl.h
+++ b/platform/embos/include/chre/target_platform/mutex_base_impl.h
@@ -22,23 +22,31 @@
 namespace chre {
 
 inline Mutex::Mutex() {
-  OS_MUTEX_Create(&mMutex);
+  OS_CREATERSEMA(&mResourceSemaphore);
 }
 
 inline Mutex::~Mutex() {
-  OS_MUTEX_Delete(&mMutex);
+  OS_DeleteRSema(&mResourceSemaphore);
 }
 
 inline void Mutex::lock() {
-  OS_MUTEX_LockBlocked(&mMutex);
+  OS_Use(&mResourceSemaphore);
 }
 
 inline bool Mutex::try_lock() {
-  return OS_MUTEX_Lock(&mMutex) != 0;
+  // The return value of OS_Request indicates the availability: a value of 1
+  // indicates that the resource was available and is now in use by the calling
+  // task.
+  return OS_Request(&mResourceSemaphore) == 1;
 }
 
+// Note: Calling this function from a task that doesn't own the resource being
+// released or if called before a call to OS_Use leads to undefined behavior.
+// The EmbOS error handler (if enabled) OS_Error is invoked with code
+// `OS_ERR_UNUSE_BEFORE_USE` in the former case, and with code
+// `OS_ERR_RESOURCE_OWNER` for the latter.
 inline void Mutex::unlock() {
-  OS_MUTEX_Unlock(&mMutex);
+  OS_Unuse(&mResourceSemaphore);
 }
 
 }  // namespace chre
diff --git a/platform/embos/include/chre/target_platform/system_timer_base.h b/platform/embos/include/chre/target_platform/system_timer_base.h
index bc6cd64..e1a12d6 100644
--- a/platform/embos/include/chre/target_platform/system_timer_base.h
+++ b/platform/embos/include/chre/target_platform/system_timer_base.h
@@ -31,12 +31,14 @@
  * being stopped. In this scenario, if a lock were to be held by the CHRE (or
  * another) thread, it would execute until releasing the lock (possibly setting
  * or canceling the same timer). But since this can only happen prior to a call
- * to OS_TIMER_StopEx() returning, we know that the callback will be the one
+ * to OS_StopTimerEx() returning, we know that the callback will be the one
  * provided for the previous timer and not a mismatch.
  *
- * Note: A side effect of this is that there still exists a possible race
- * between getting the status of a timer (via OS_TIMER_GetStatusEx()) and
- * stopping a timer (via OS_TIMER_StopEx()) which probably needs guarantees
+ * @note
+ * 1: This implementation is aimed at EmbOS v4.22.
+ * 2: A side effect of this is that there still exists a possible race
+ * between getting the status of a timer (via OS_GetTimerStatusEx()) and
+ * stopping a timer (via OS_StopTimerEx()) which probably needs guarantees
  * at the OS implementation level - which means that the return value of the
  * system timer cancel call is not guaranteed to always be accurate.
  */
diff --git a/platform/embos/init.cc b/platform/embos/init.cc
index bad54d1..1ab2eb1 100644
--- a/platform/embos/init.cc
+++ b/platform/embos/init.cc
@@ -24,6 +24,7 @@
 namespace {
 
 constexpr char kChreTaskName[] = "CHRE";
+constexpr size_t kChreTaskNameLen = sizeof(kChreTaskName) - 1;
 
 // The CHRE task priority was requested to be between the sub_task (prio=60),
 // and the main task (prio=100).
@@ -54,7 +55,15 @@
 }
 
 void chreEmbosDeinit() {
-  if (OS_TASK_IsTask(&gChreTcb)) {
+  if (OS_IsTask(&gChreTcb)) {
     chre::EventLoopManagerSingleton::get()->getEventLoop().stop();
   }
 }
+
+const char *getChreTaskName() {
+  return kChreTaskName;
+}
+
+size_t getChreTaskNameLen() {
+  return kChreTaskNameLen;
+}
diff --git a/platform/embos/memory.cc b/platform/embos/memory.cc
index 31df8c5..523d661 100644
--- a/platform/embos/memory.cc
+++ b/platform/embos/memory.cc
@@ -24,19 +24,19 @@
 namespace chre {
 
 void *memoryAlloc(size_t size) {
-  return OS_HEAP_malloc(size);
+  return OS_malloc(size);
 }
 
 void *palSystemApiMemoryAlloc(size_t size) {
-  return OS_HEAP_malloc(size);
+  return OS_malloc(size);
 }
 
 void memoryFree(void *pointer) {
-  OS_HEAP_free(pointer);
+  OS_free(pointer);
 }
 
 void palSystemApiMemoryFree(void *pointer) {
-  OS_HEAP_free(pointer);
+  OS_free(pointer);
 }
 
 }  // namespace chre
diff --git a/platform/embos/system_timer.cc b/platform/embos/system_timer.cc
index 53e99a3..f0cf8ea 100644
--- a/platform/embos/system_timer.cc
+++ b/platform/embos/system_timer.cc
@@ -28,16 +28,18 @@
   timer->mCallback(timer->mData);
 }
 
+SystemTimer::SystemTimer() {}
+
 SystemTimer::~SystemTimer() {
   // cancel an existing timer if any, and delete the timer instance.
   cancel();
-  OS_TIMER_DeleteEx(&mTimer);
+  OS_DeleteTimerEx(&mTimer);
 }
 
 bool SystemTimer::init() {
   constexpr uint32_t kSomeInitialPeriod = 100;
-  OS_TIMER_CreateEx(&mTimer, SystemTimerBase::invokeCallback,
-                    kSomeInitialPeriod, this /*context*/);
+  OS_CreateTimerEx(&mTimer, SystemTimerBase::invokeCallback, kSomeInitialPeriod,
+                   this /*context*/);
   return true;
 }
 
@@ -52,13 +54,13 @@
   uint64_t delayMs = Milliseconds(delay).getMilliseconds();
   delayMs = std::min(std::max(delayMs, kMinDelayMs), kMaxDelayMs);
 
-  OS_TIMER_StopEx(&mTimer);
-  OS_TIMER_SetPeriodEx(&mTimer, static_cast<OS_TIME>(delayMs));
+  OS_StopTimerEx(&mTimer);
+  OS_SetTimerPeriodEx(&mTimer, static_cast<OS_TIME>(delayMs));
 
   mCallback = callback;
   mData = data;
 
-  OS_TIMER_RestartEx(&mTimer);
+  OS_RetriggerTimerEx(&mTimer);
   return true;
 }
 
@@ -67,14 +69,14 @@
 bool SystemTimer::cancel() {
   bool success = false;
   if (isActive()) {
-    OS_TIMER_StopEx(&mTimer);
+    OS_StopTimerEx(&mTimer);
     success = true;
   }
   return success;
 }
 
 bool SystemTimer::isActive() {
-  return (OS_TIMER_GetStatusEx(&mTimer) != 0);
+  return (OS_GetTimerStatusEx(&mTimer) != 0);
 }
 
 }  // namespace chre