crypto-client test: implement the first IPC call for psa_crypto_init()

This commit implements the first useful IPC communication between
the client and the server. The implemented command is simple,
psa_crypto_init(), and its return value is sent back to the client.

Note: the newly added file psa_functions_codes.h is temporary
and it's probably the one that needs to be automatically
generated by a python script to support all crypto functions.

Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
diff --git a/tests/psa-client-server/psasim/test/Makefile b/tests/psa-client-server/psasim/test/Makefile
index 5afc8f5..41f4bd4 100644
--- a/tests/psa-client-server/psasim/test/Makefile
+++ b/tests/psa-client-server/psasim/test/Makefile
@@ -26,10 +26,10 @@
 all: $(TEST_BIN)
 
 psa_client: client.c $(GENERATED_H_FILES)
-	$(CC) $(COMMON_INCLUDE) $(LIBPSACLIENT_H) $(CFLAGS) $< $(LIBPSASIM) $(LIBPSACLIENT) -o $@
+	$(CC) $(COMMON_INCLUDE) $(LIBPSACLIENT_H) $(CFLAGS) $< $(LIBPSASIM) $(LIBPSACLIENT) $(LDFLAGS) -o $@
 
 psa_partition: $(PARTITION_SERVER_BOOTSTRAP) $(GENERATED_H_FILES)
-	$(CC) $(COMMON_INCLUDE) $(LIBPSASERVER_H) $(CFLAGS) $< $(LIBPSASIM) $(LIBPSASERVER) -o $@
+	$(CC) $(COMMON_INCLUDE) $(LIBPSASERVER_H) $(CFLAGS) $< $(LIBPSASIM) $(LIBPSASERVER) $(LDFLAGS) -o $@
 
 $(PARTITION_SERVER_BOOTSTRAP) $(GENERATED_H_FILES): manifest.json server.c
 	../tools/psa_autogen.py $<
diff --git a/tests/psa-client-server/psasim/test/client.c b/tests/psa-client-server/psasim/test/client.c
index 3c61120..74e7bcb 100644
--- a/tests/psa-client-server/psasim/test/client.c
+++ b/tests/psa-client-server/psasim/test/client.c
@@ -8,11 +8,15 @@
 #include <stdio.h>
 #include <unistd.h>
 
+/* Includes from psasim */
 #include <psa/client.h>
 #include <psa/util.h>
 #include "psa_manifest/sid.h"
+#include "psa_functions_codes.h"
 
+/* Includes from mbedtls */
 #include "mbedtls/version.h"
+#include "psa/crypto.h"
 
 #define CLIENT_PRINT(fmt, ...) \
     PRINT("Client: " fmt, ##__VA_ARGS__)
@@ -20,8 +24,9 @@
 int main()
 {
     char mbedtls_version[18];
-    const char *text = "FOOBARCOOL!!";
-    char output[100] = { 0 };
+    // psa_invec invecs[1];
+    // psa_outvec outvecs[1];
+    psa_status_t status;
 
     mbedtls_version_get_string_full(mbedtls_version);
     CLIENT_PRINT("%s", mbedtls_version);
@@ -34,23 +39,16 @@
     if (h < 0) {
         CLIENT_PRINT("Couldn't connect %d", h);
         return 1;
-    } else {
-        int type = 2;
-        CLIENT_PRINT("psa_call() w/o invec returned: %d", psa_call(h, type, NULL, 0, NULL, 0));
-        psa_invec invecs[1];
-        psa_outvec outvecs[1];
-        invecs[0].base = text;
-        invecs[0].len = sizeof(text);
-        outvecs[0].base = output;
-        outvecs[0].len = sizeof(output);
-
-        CLIENT_PRINT("invec len: %lu", invecs[0].len);
-        CLIENT_PRINT("psa_call() w/ invec returned: %d", psa_call(h, type, invecs, 1, outvecs, 1));
-        CLIENT_PRINT("Received payload len: %ld", outvecs[0].len);
-        CLIENT_PRINT("Received payload content: %s", output);
-        CLIENT_PRINT("Closing handle");
-        psa_close(h);
     }
 
+    status = psa_call(h, PSA_CRYPTO_INIT, NULL, 0, NULL, 0);
+    CLIENT_PRINT("PSA_CRYPTO_INIT returned: %d", status);
+
+    CLIENT_PRINT("Closing handle");
+    psa_close(h);
+
+    if (status != PSA_SUCCESS) {
+        return 1;
+    }
     return 0;
 }
diff --git a/tests/psa-client-server/psasim/test/manifest.json b/tests/psa-client-server/psasim/test/manifest.json
index 0ab83ef..d90c7ed 100644
--- a/tests/psa-client-server/psasim/test/manifest.json
+++ b/tests/psa-client-server/psasim/test/manifest.json
@@ -3,14 +3,14 @@
    "name":"TEST_PARTITION",
    "type":"PSA-ROT",
    "priority":"LOW",
-   "entry_point":"psa_sha256_main",
+   "entry_point":"psa_server_main",
    "stack_size":"0x400",
    "heap_size":"0x100",
    "services":[
       {
          "name":"PSA_SID_SHA256",
          "sid":"0x0000F000",
-         "signal":"PSA_SHA256",
+         "signal":"PSA_CRYPTO",
          "non_secure_clients": "true",
          "minor_version":1,
          "minor_policy":"STRICT"
diff --git a/tests/psa-client-server/psasim/test/psa_functions_codes.h b/tests/psa-client-server/psasim/test/psa_functions_codes.h
new file mode 100644
index 0000000..34897b9
--- /dev/null
+++ b/tests/psa-client-server/psasim/test/psa_functions_codes.h
@@ -0,0 +1,9 @@
+#ifndef _PSA_FUNCTIONS_CODES_H_
+#define  _PSA_FUNCTIONS_CODES_H_
+
+enum {
+    PSA_CRYPTO_INIT = 0x00,
+    /* Add other PSA functions here */
+};
+
+#endif /*  _PSA_FUNCTIONS_CODES_H_ */
diff --git a/tests/psa-client-server/psasim/test/run_test.sh b/tests/psa-client-server/psasim/test/run_test.sh
index 0ffaaea..6a5605f 100755
--- a/tests/psa-client-server/psasim/test/run_test.sh
+++ b/tests/psa-client-server/psasim/test/run_test.sh
@@ -11,7 +11,10 @@
 
 set -e
 
+cd "$(dirname "$0")"
+
 function clean_run() {
+    rm -f psa_notify_*
     pkill psa_partition || true
     pkill psa_client || true
     ipcs | grep q | awk '{ printf " -q " $$2 }' | xargs ipcrm > /dev/null 2>&1 || true
@@ -21,7 +24,7 @@
 # event as signal that the server is ready so that we can start client(s).
 function wait_for_server_startup() {
     while [ ! -f ./psa_notify_* ]; do
-    sleep 0.1
+        sleep 0.1
     done
 }
 
diff --git a/tests/psa-client-server/psasim/test/server.c b/tests/psa-client-server/psasim/test/server.c
index 1c873c6..b88a7ba 100644
--- a/tests/psa-client-server/psasim/test/server.c
+++ b/tests/psa-client-server/psasim/test/server.c
@@ -8,12 +8,16 @@
 #include <unistd.h>
 #include <stdio.h>
 
+/* Includes from psasim */
 #include "psa/service.h"
-#include "psa/error.h"
+#include "psa/error_ext.h"
 #include "psa/util.h"
 #include "psa_manifest/manifest.h"
+#include "psa_functions_codes.h"
 
+/* Includes from mbedtls */
 #include "mbedtls/version.h"
+#include "psa/crypto.h"
 
 #define SERVER_PRINT(fmt, ...) \
     PRINT("Server: " fmt, ##__VA_ARGS__)
@@ -38,11 +42,10 @@
     }
 }
 
-int psa_sha256_main(int argc, char *argv[])
+int psa_server_main(int argc, char *argv[])
 {
     psa_status_t ret = PSA_ERROR_PROGRAMMER_ERROR;
     psa_msg_t msg = { -1 };
-    char foo[BUF_SIZE] = { 0 };
     const int magic_num = 66;
     int client_disconnected = 0;
     char mbedtls_version[18];
@@ -60,10 +63,9 @@
             SERVER_PRINT("Signals: 0x%08x", signals);
         }
 
-        if (signals & PSA_SHA256_SIGNAL) {
-            if (PSA_SUCCESS == psa_get(PSA_SHA256_SIGNAL, &msg)) {
-                SERVER_PRINT("My handle is %d", msg.handle);
-                SERVER_PRINT("My rhandle is %p", (int *) msg.rhandle);
+        if (signals & PSA_CRYPTO_SIGNAL) {
+            if (PSA_SUCCESS == psa_get(PSA_CRYPTO_SIGNAL, &msg)) {
+                SERVER_PRINT("handle: %d - rhandle: %p", msg.handle, (int *) msg.rhandle);
                 switch (msg.type) {
                     case PSA_IPC_CONNECT:
                         SERVER_PRINT("Got a connection message");
@@ -75,34 +77,23 @@
                         ret = PSA_SUCCESS;
                         client_disconnected = 1;
                         break;
-
                     default:
                         SERVER_PRINT("Got an IPC call of type %d", msg.type);
-                        ret = 42;
-                        size_t size = msg.in_size[0];
-
-                        if ((size > 0) && (size <= sizeof(foo))) {
-                            psa_read(msg.handle, 0, foo, 6);
-                            foo[(BUF_SIZE-1)] = '\0';
-                            SERVER_PRINT("Reading payload: %s", foo);
-                            psa_read(msg.handle, 0, foo+6, 6);
-                            foo[(BUF_SIZE-1)] = '\0';
-                            SERVER_PRINT("Reading payload: %s", foo);
+                        switch (msg.type) {
+                            case PSA_CRYPTO_INIT:
+                                ret = psa_crypto_init();
+                                break;
+                            default:
+                                SERVER_PRINT("Unknown PSA function code");
+                                break;
                         }
-
-                        size = msg.out_size[0];
-                        if ((size > 0)) {
-                            SERVER_PRINT("Writing response");
-                            psa_write(msg.handle, 0, "RESP", 4);
-                            psa_write(msg.handle, 0, "ONSE", 4);
-                        }
+                        SERVER_PRINT("Internal function call returned %d", ret);
 
                         if (msg.client_id > 0) {
                             psa_notify(msg.client_id);
                         } else {
                             SERVER_PRINT("Client is non-secure, so won't notify");
                         }
-
                 }
 
                 psa_reply(msg.handle, ret);