Interface: Refine OS wrapper APIs

For the following OS wrapper APIs
    os_wrapper_thread_new
    os_wrapper_thread_get_id
    os_wrapper_thread_get_priority
    os_wrapper_semaphore_create
    os_wrapper_mutex_create
They return the corresponding IDs or priorities on success and
OS_WRAPPER_ERROR on failure.
This makes the "OS_WRAPPER_ERROR" value invalid for thread IDs,
mutex IDs...etc on all OSes.
But it could be a valid value on some OSes.

This patch fixes this problem by
1. Changing mutex, semaphore and thread IDs to handles
2. For os_wrapper_thread_get_priority, putting outputs to
   parameters and returning only success or error.
   For the others, treating NULL return as error and others for
   valid handles.

Change-Id: I19aa0415ce0ba4388fb96602fea8f10f702d8c45
Signed-off-by: Kevin Peng <kevin.peng@arm.com>
diff --git a/app/os_wrapper_cmsis_rtos_v2.c b/app/os_wrapper_cmsis_rtos_v2.c
index 0fe3799..ecbf474 100644
--- a/app/os_wrapper_cmsis_rtos_v2.c
+++ b/app/os_wrapper_cmsis_rtos_v2.c
@@ -14,12 +14,11 @@
 
 /* This is an example OS abstraction layer for CMSIS-RTOSv2 */
 
-uint32_t os_wrapper_thread_new(const char *name, int32_t stack_size,
-                               os_wrapper_thread_func func, void *arg,
-                               uint32_t priority)
+void *os_wrapper_thread_new(const char *name, int32_t stack_size,
+                            os_wrapper_thread_func func, void *arg,
+                            uint32_t priority)
 {
     osThreadAttr_t task_attribs = {.tz_module = 1};
-    osThreadId_t thread_id;
 
     /* By default, the thread starts as osThreadDetached */
     if (stack_size != OS_WRAPPER_DEFAULT_STACK_SIZE) {
@@ -28,36 +27,24 @@
     task_attribs.name = name;
     task_attribs.priority = (osPriority_t) priority;
 
-    thread_id = osThreadNew(func, arg, &task_attribs);
-    if (thread_id == NULL) {
-        return OS_WRAPPER_ERROR;
-    }
-
-    return (uint32_t)thread_id;
+    return (void *)osThreadNew(func, arg, &task_attribs);
 }
 
-
-uint32_t os_wrapper_semaphore_create(uint32_t max_count, uint32_t initial_count,
-                                     const char *name)
+void *os_wrapper_semaphore_create(uint32_t max_count, uint32_t initial_count,
+                                  const char *name)
 {
     osSemaphoreAttr_t sema_attrib = {0};
-    osSemaphoreId_t semaphore;
 
     sema_attrib.name = name;
 
-    semaphore = osSemaphoreNew(max_count, initial_count, &sema_attrib);
-    if (semaphore == NULL) {
-        return OS_WRAPPER_ERROR;
-    }
-
-    return (uint32_t)semaphore;
+    return (void *)osSemaphoreNew(max_count, initial_count, &sema_attrib);
 }
 
-uint32_t os_wrapper_semaphore_acquire(uint32_t semaphore_id, uint32_t timeout)
+uint32_t os_wrapper_semaphore_acquire(void *handle, uint32_t timeout)
 {
     osStatus_t status;
 
-    status = osSemaphoreAcquire((osSemaphoreId_t)semaphore_id,
+    status = osSemaphoreAcquire((osSemaphoreId_t)handle,
                                 (timeout == OS_WRAPPER_WAIT_FOREVER) ?
                                 osWaitForever : timeout);
     if (status != osOK) {
@@ -67,11 +54,11 @@
     return OS_WRAPPER_SUCCESS;
 }
 
-uint32_t os_wrapper_semaphore_release(uint32_t sema)
+uint32_t os_wrapper_semaphore_release(void *handle)
 {
     osStatus_t status;
 
-    status = osSemaphoreRelease((osSemaphoreId_t)sema);
+    status = osSemaphoreRelease((osSemaphoreId_t)handle);
     if (status != osOK) {
         return OS_WRAPPER_ERROR;
     }
@@ -79,11 +66,11 @@
     return OS_WRAPPER_SUCCESS;
 }
 
-uint32_t os_wrapper_semaphore_delete(uint32_t sema)
+uint32_t os_wrapper_semaphore_delete(void *handle)
 {
     osStatus_t status;
 
-    status = osSemaphoreDelete((osSemaphoreId_t)sema);
+    status = osSemaphoreDelete((osSemaphoreId_t)handle);
     if (status != osOK) {
         return OS_WRAPPER_ERROR;
     }
@@ -91,9 +78,8 @@
     return OS_WRAPPER_SUCCESS;
 }
 
-uint32_t os_wrapper_mutex_create(void)
+void *os_wrapper_mutex_create(void)
 {
-    osMutexId_t id;
     const osMutexAttr_t attr = {
         .name = NULL,
         .attr_bits = osMutexPrioInherit, /* Priority inheritance is recommended
@@ -106,23 +92,18 @@
         .cb_size = 0U
     };
 
-    id = osMutexNew(&attr);
-    if (!id) {
-        return OS_WRAPPER_ERROR;
-    }
-
-    return (uint32_t)id;
+    return (void *)osMutexNew(&attr);
 }
 
-uint32_t os_wrapper_mutex_acquire(uint32_t mutex_id, uint32_t timeout)
+uint32_t os_wrapper_mutex_acquire(void *handle, uint32_t timeout)
 {
     osStatus_t status = osOK;
 
-    if (!mutex_id) {
+    if (!handle) {
         return OS_WRAPPER_ERROR;
     }
 
-    status = osMutexAcquire((osMutexId_t)mutex_id,
+    status = osMutexAcquire((osMutexId_t)handle,
                             (timeout == OS_WRAPPER_WAIT_FOREVER) ?
                              osWaitForever : timeout);
     if (status != osOK) {
@@ -132,15 +113,15 @@
     return OS_WRAPPER_SUCCESS;
 }
 
-uint32_t os_wrapper_mutex_release(uint32_t mutex_id)
+uint32_t os_wrapper_mutex_release(void *handle)
 {
     osStatus_t status = osOK;
 
-    if (!mutex_id) {
+    if (!handle) {
         return OS_WRAPPER_ERROR;
     }
 
-    status = osMutexRelease((osMutexId_t)mutex_id);
+    status = osMutexRelease((osMutexId_t)handle);
     if (status != osOK) {
         return OS_WRAPPER_ERROR;
     }
@@ -148,15 +129,15 @@
     return OS_WRAPPER_SUCCESS;
 }
 
-uint32_t os_wrapper_mutex_delete(uint32_t mutex_id)
+uint32_t os_wrapper_mutex_delete(void *handle)
 {
     osStatus_t status = osOK;
 
-    if (!mutex_id) {
+    if (!handle) {
         return OS_WRAPPER_ERROR;
     }
 
-    status = osMutexDelete((osMutexId_t)mutex_id);
+    status = osMutexDelete((osMutexId_t)handle);
     if (status != osOK) {
         return OS_WRAPPER_ERROR;
     }
@@ -164,28 +145,23 @@
     return OS_WRAPPER_SUCCESS;
 }
 
-uint32_t os_wrapper_thread_get_id(void)
+void *os_wrapper_thread_get_handle(void)
 {
-    osThreadId_t thread_id;
-
-    thread_id = osThreadGetId();
-    if (thread_id == NULL) {
-        return OS_WRAPPER_ERROR;
-    }
-
-    return (uint32_t)thread_id;
+    return (void *)osThreadGetId();
 }
 
-uint32_t os_wrapper_thread_get_priority(uint32_t id)
+uint32_t os_wrapper_thread_get_priority(void *handle, uint32_t *priority)
 {
     osPriority_t prio;
 
-    prio = osThreadGetPriority((osThreadId_t)id);
+    prio = osThreadGetPriority((osThreadId_t)handle);
     if (prio == osPriorityError) {
         return OS_WRAPPER_ERROR;
     }
 
-    return prio;
+    *priority = (uint32_t)prio;
+
+    return OS_WRAPPER_SUCCESS;
 }
 
 void os_wrapper_thread_exit(void)
diff --git a/interface/include/os_wrapper/mutex.h b/interface/include/os_wrapper/mutex.h
index 2bdca0c..e55ef70 100644
--- a/interface/include/os_wrapper/mutex.h
+++ b/interface/include/os_wrapper/mutex.h
@@ -17,16 +17,15 @@
 /**
  * \brief Creates a mutex for mutual exclusion of resources
  *
- * \return The ID of the created mutex on success or \ref OS_WRAPPER_ERROR on
- *         error
+ * \return The handle of the created mutex on success or NULL on error
  */
-uint32_t os_wrapper_mutex_create(void);
+void *os_wrapper_mutex_create(void);
 
 /**
  * \brief Acquires a mutex that is created by \ref os_wrapper_mutex_create()
  *
- * \param[in] mutex_id The ID of the mutex to acquire. Should be one of the IDs
- *                     returned by \ref os_wrapper_mutex_create()
+ * \param[in] handle   The handle of the mutex to acquire. Should be one of the
+ *                     handles returned by \ref os_wrapper_mutex_create()
  * \param[in] timeout  The maximum amount of time(in tick periods) for the
  *                     thread to wait for the mutex to be available.
  *                     If timeout is zero, the function will return immediately.
@@ -35,25 +34,26 @@
  *
  * \return \ref OS_WRAPPER_SUCCESS on success or \ref OS_WRAPPER_ERROR on error
  */
-uint32_t os_wrapper_mutex_acquire(uint32_t mutex_id, uint32_t timeout);
+uint32_t os_wrapper_mutex_acquire(void *handle, uint32_t timeout);
 
 /**
  * \brief Releases the mutex acquired previously
  *
- * \param[in] mutex_id The ID of the mutex that has been acquired
+
+ * \param[in] handle The handle of the mutex that has been acquired
  *
  * \return \ref OS_WRAPPER_SUCCESS on success or \ref OS_WRAPPER_ERROR on error
  */
-uint32_t os_wrapper_mutex_release(uint32_t mutex_id);
+uint32_t os_wrapper_mutex_release(void *handle);
 
 /**
  * \brief Deletes a mutex that is created by \ref os_wrapper_mutex_create()
  *
- * \param[in] mutex_id The ID of the mutex to be deleted
+ * \param[in] handle The handle of the mutex to be deleted
  *
  * \return \ref OS_WRAPPER_SUCCESS on success or \ref OS_WRAPPER_ERROR on error
  */
-uint32_t os_wrapper_mutex_delete(uint32_t mutex_id);
+uint32_t os_wrapper_mutex_delete(void *handle);
 
 #ifdef __cplusplus
 }
diff --git a/interface/include/os_wrapper/semaphore.h b/interface/include/os_wrapper/semaphore.h
index 38b9a16..89b00a0 100644
--- a/interface/include/os_wrapper/semaphore.h
+++ b/interface/include/os_wrapper/semaphore.h
@@ -21,42 +21,41 @@
  * \param[in] initial_count   Starting count of the semaphore
  * \param[in] name            Name of the semaphore
  *
- * \return Returns ID of the semaphore created, or \ref OS_WRAPPER_ERROR in case
- *         of error
+ * \return Returns handle of the semaphore created, or NULL in case of error
  */
-uint32_t os_wrapper_semaphore_create(uint32_t max_count, uint32_t initial_count,
-                                     const char *name);
+void *os_wrapper_semaphore_create(uint32_t max_count, uint32_t initial_count,
+                                  const char *name);
 
 /**
  * \brief Acquires the semaphore
  *
- * \param[in] semaphore_id Semaphore ID
- * \param[in] timeout      Timeout value
+ * \param[in] hanlde  Semaphore handle
+ * \param[in] timeout Timeout value
  *
  * \return \ref OS_WRAPPER_SUCCESS in case of successful acquision, or
  *         \ref OS_WRAPPER_ERROR in case of error
  */
-uint32_t os_wrapper_semaphore_acquire(uint32_t semaphore_id, uint32_t timeout);
+uint32_t os_wrapper_semaphore_acquire(void *handle, uint32_t timeout);
 
 /**
  * \brief Releases the semaphore
  *
- * \param[in] semaphore_id Semaphore ID
+ * \param[in] hanlde Semaphore handle
  *
  * \return \ref OS_WRAPPER_SUCCESS in case of successful release, or
  *         \ref OS_WRAPPER_ERROR in case of error
  */
-uint32_t os_wrapper_semaphore_release(uint32_t semaphore_id);
+uint32_t os_wrapper_semaphore_release(void *handle);
 
 /**
  * \brief Deletes the semaphore
  *
- * \param[in] semaphore_id Semaphore ID
+ * \param[in] handle Semaphore handle
  *
  * \return \ref OS_WRAPPER_SUCCESS in case of successful release, or
  *         \ref OS_WRAPPER_ERROR in case of error
  */
-uint32_t os_wrapper_semaphore_delete(uint32_t semaphore_id);
+uint32_t os_wrapper_semaphore_delete(void *handle);
 
 #ifdef __cplusplus
 }
diff --git a/interface/include/os_wrapper/thread.h b/interface/include/os_wrapper/thread.h
index cfd2e13..8386bd5 100644
--- a/interface/include/os_wrapper/thread.h
+++ b/interface/include/os_wrapper/thread.h
@@ -28,27 +28,28 @@
  * \param[in] arg         Argument to pass to the function invoked by thread
  * \param[in] priority    Initial thread priority
  *
- * \return Returns thread ID, or \ref OS_WRAPPER_ERROR in case of error
+ * \return Returns the thread handle created, or NULL in case of error
  */
-uint32_t os_wrapper_thread_new(const char *name, int32_t stack_size,
-                               os_wrapper_thread_func func, void *arg,
-                               uint32_t priority);
+void *os_wrapper_thread_new(const char *name, int32_t stack_size,
+                            os_wrapper_thread_func func, void *arg,
+                            uint32_t priority);
 /**
- * \brief Gets current thread ID
+ * \brief Gets current thread handle
  *
- * \return Returns thread ID, or \ref OS_WRAPPER_ERROR in case of error
+ * \return Returns the thread handle, or NULL in case of error
  */
-uint32_t os_wrapper_thread_get_id(void);
+void *os_wrapper_thread_get_handle(void);
 
 /**
  * \brief Gets thread priority
  *
- * \param[in] id Thread ID
+ * \param[in]  handle   Thread handle
+ * \param[out] priority The priority of the thread
  *
- * \return Returns thread priority value, or \ref OS_WRAPPER_ERROR in case of
- *         error
+ * \return Returns \ref OS_WRAPPER_SUCCESS on success, or \ref OS_WRAPPER_ERROR
+ *                 in case of error
  */
-uint32_t os_wrapper_thread_get_priority(uint32_t id);
+uint32_t os_wrapper_thread_get_priority(void *handle, uint32_t *priority);
 
 /**
  * \brief Exits the calling thread
diff --git a/interface/src/tfm_ns_interface.c b/interface/src/tfm_ns_interface.c
index 7ec8c40..7b01695 100644
--- a/interface/src/tfm_ns_interface.c
+++ b/interface/src/tfm_ns_interface.c
@@ -15,7 +15,7 @@
 /**
  * \brief the ns_lock ID
  */
-static uint32_t ns_lock_id = (uint32_t)NULL;
+static void *ns_lock_handle = NULL;
 
 __attribute__((weak))
 int32_t tfm_ns_interface_dispatch(veneer_fn fn,
@@ -25,14 +25,14 @@
     int32_t result;
 
     /* TFM request protected by NS lock */
-    if (os_wrapper_mutex_acquire(ns_lock_id, OS_WRAPPER_WAIT_FOREVER)
+    if (os_wrapper_mutex_acquire(ns_lock_handle, OS_WRAPPER_WAIT_FOREVER)
             != OS_WRAPPER_SUCCESS) {
         return (int32_t)TFM_ERROR_GENERIC;
     }
 
     result = fn(arg0, arg1, arg2, arg3);
 
-    if (os_wrapper_mutex_release(ns_lock_id) != OS_WRAPPER_SUCCESS) {
+    if (os_wrapper_mutex_release(ns_lock_handle) != OS_WRAPPER_SUCCESS) {
         return (int32_t)TFM_ERROR_GENERIC;
     }
 
@@ -42,13 +42,13 @@
 __attribute__((weak))
 enum tfm_status_e tfm_ns_interface_init(void)
 {
-    uint32_t id;
+    void *handle;
 
-    id = os_wrapper_mutex_create();
-    if (id == OS_WRAPPER_ERROR) {
+    handle = os_wrapper_mutex_create();
+    if (!handle) {
         return TFM_ERROR_GENERIC;
     }
 
-    ns_lock_id = id;
+    ns_lock_handle = handle;
     return TFM_SUCCESS;
 }
diff --git a/test/suites/sst/non_secure/ns_test_helpers.c b/test/suites/sst/non_secure/ns_test_helpers.c
index 1947e25..a513e13 100644
--- a/test/suites/sst/non_secure/ns_test_helpers.c
+++ b/test/suites/sst/non_secure/ns_test_helpers.c
@@ -19,7 +19,7 @@
     struct test_result_t *ret;
 };
 
-static uint32_t test_semaphore;
+static void *test_semaphore;
 
 /**
  * \brief Executes the supplied test task and then releases the test semaphore.
@@ -48,28 +48,29 @@
 void tfm_sst_run_test(const char *thread_name, struct test_result_t *ret,
                       test_func_t *test_func)
 {
-    uint32_t current_thread_id;
+    void *current_thread_handle;
     uint32_t current_thread_priority;
     uint32_t err;
-    uint32_t thread;
+    void *thread;
     struct test_task_t test_task = { .func = test_func, .ret = ret };
 
     /* Create a binary semaphore with initial count of 0 tokens available */
     test_semaphore = os_wrapper_semaphore_create(1, 0, "sst_tests_sema");
-    if (test_semaphore == OS_WRAPPER_ERROR) {
+    if (!test_semaphore) {
         TEST_FAIL("Semaphore creation failed");
         return;
     }
 
-    current_thread_id = os_wrapper_thread_get_id();
-    if (current_thread_id == OS_WRAPPER_ERROR) {
+    current_thread_handle = os_wrapper_thread_get_handle();
+    if (!current_thread_handle) {
         os_wrapper_semaphore_delete(test_semaphore);
         TEST_FAIL("Failed to get current thread ID");
         return;
     }
 
-    current_thread_priority = os_wrapper_thread_get_priority(current_thread_id);
-    if (current_thread_priority == OS_WRAPPER_ERROR) {
+    err = os_wrapper_thread_get_priority(current_thread_handle,
+                                         &current_thread_priority);
+    if (err == OS_WRAPPER_ERROR) {
         os_wrapper_semaphore_delete(test_semaphore);
         TEST_FAIL("Failed to get current thread priority");
         return;
@@ -79,7 +80,7 @@
     thread = os_wrapper_thread_new(thread_name, SST_TEST_TASK_STACK_SIZE,
                                    test_task_runner, &test_task,
                                    current_thread_priority);
-    if (thread == OS_WRAPPER_ERROR) {
+    if (!thread) {
         os_wrapper_semaphore_delete(test_semaphore);
         TEST_FAIL("Failed to create test thread");
         return;