From e57d3da6fc539a2815c1e3a98edd112bdb58743f Mon Sep 17 00:00:00 2001
From: Rahix <rahix@rahix.de>
Date: Fri, 14 Jun 2019 00:52:45 +0200
Subject: [PATCH] feat(api-demo): Make API calls work!

Signed-off-by: Rahix <rahix@rahix.de>
---
 hw-tests/api-demo/api/api_dispatcher.c |  9 +++--
 hw-tests/api-demo/core1-dispatcher.c   |  8 +++--
 hw-tests/api-demo/genapi.py            | 50 ++++++++++++++++++++++++--
 hw-tests/api-demo/main.c               |  3 ++
 4 files changed, 60 insertions(+), 10 deletions(-)

diff --git a/hw-tests/api-demo/api/api_dispatcher.c b/hw-tests/api-demo/api/api_dispatcher.c
index be5db0967..1c9c73dd8 100644
--- a/hw-tests/api-demo/api/api_dispatcher.c
+++ b/hw-tests/api-demo/api/api_dispatcher.c
@@ -12,6 +12,9 @@ int api_init (sys_cfg_sema_t *sys_cfg)
 	return ret;
 }
 
+/* Generated function */
+void __api_dispatch_call(uint32_t id, void*buffer);
+
 void api_dispatcher()
 {
 	while (SEMA_GetSema(API_CALL_SEMA) == E_BUSY) {}
@@ -22,11 +25,7 @@ void api_dispatcher()
 	}
 
 	printf("core1: Catched API CALL!\n");
-	printf("%d: ",ApiCallSpace->id);
-	for (int i = 0; i < 16; i++) {
-		printf("0x%02x ", ((char*)ApiCallSpace->buf)[i]);
-	}
-	printf("\n");
+	__api_dispatch_call(ApiCallSpace->id, ApiCallSpace->buf);
 
 	ApiCallSpace->returning = 1;
 
diff --git a/hw-tests/api-demo/core1-dispatcher.c b/hw-tests/api-demo/core1-dispatcher.c
index 570a32555..b0ad77431 100644
--- a/hw-tests/api-demo/core1-dispatcher.c
+++ b/hw-tests/api-demo/core1-dispatcher.c
@@ -3,6 +3,7 @@
 #include "mxc_delay.h"
 #include "api.h"
 #include "tmr_utils.h"
+#include "leds.h"
 
 #include "api/api_dispatcher.h"
 
@@ -22,18 +23,21 @@ void api_set_buzzer(uint8_t state)
 void api_set_led(uint8_t led, led_color_t color)
 {
 	printf("API: Changing color of led %d.\n", led);
+	printf("Color { r: %3d, g: %3d, b: %3d }\n", color.red, color.green, color.blue);
 	leds_set(led, color.red, color.green, color.blue);
+	leds_update();
 }
 
 void api_test(char test0, short test1, int test2, long test3)
 {
-	printf ("test0: %x, test1: %x, test2: %x, test3: %x\n",
-			test0, test1, test2, test3);
+	printf ("test0: %x, test1: %d, test2: %x, test3: %lx\n",
+			test0, (int)test1, test2, test3);
 }
 
 int main(void)
 {
 	api_init(NULL);
+	leds_init();
 
 	while (1) {
 		api_dispatcher();
diff --git a/hw-tests/api-demo/genapi.py b/hw-tests/api-demo/genapi.py
index 56782979a..40cf1099e 100644
--- a/hw-tests/api-demo/genapi.py
+++ b/hw-tests/api-demo/genapi.py
@@ -40,7 +40,7 @@ def main():
 
         # Parse the header for API definitions
         matcher = re.compile(
-            r"__GENERATE_API \$ __GEN_ID_(?P<id>\w+) \$ (?P<decl>.+?)\((?P<args>.*?)\) \$",
+            r"__GENERATE_API \$ __GEN_ID_(?P<id>\w+) \$ void (?P<decl>.+?)\((?P<args>.*?)\) \$",
             re.DOTALL | re.MULTILINE,
         )
 
@@ -53,7 +53,13 @@ def main():
         print('#include "{}"\n'.format(
             os.path.basename(args.header)
         ), file=f_client)
-        print('#include "{}"\n'.format(
+
+        print("""\
+#include "{}"
+
+void __api_dispatch_call(uint32_t id, void*buffer)
+{{
+    switch (id) {{""".format(
             os.path.basename(args.header)
         ), file=f_server)
 
@@ -78,7 +84,8 @@ def main():
             print(
                 """\
 /* Autogenerated stub for {id} */
-{cdecl}({cargs}) {{
+void {cdecl}({cargs})
+{{
     const int size = {total_size};
     void*buffer;
 
@@ -93,6 +100,12 @@ def main():
                 file=f_client,
             )
 
+            print("""\
+    case {id}:
+        {cdecl}(""".format(id=api_id, cdecl=api_decl),
+                file=f_server,
+            )
+
             for i, (arg, ty) in enumerate(zip(api_args_names, api_args_types)):
                 print(
                     """    *({type}*)(buffer + {offset}) = {arg};""".format(
@@ -103,6 +116,28 @@ def main():
                     file=f_client,
                 )
 
+                if i != 0:
+                    print(",", file=f_server)
+
+                print(
+                    """\
+            *({type}*)(buffer + {offset})""".format(
+                        type=ty,
+                        offset=" + ".join(api_args_sizes[:i]) if i > 0 else "0",
+                    ),
+                    file=f_server,
+                    end="",
+                )
+
+            print("""
+        );
+        break;""".format(
+                    cdecl=api_decl,
+                    args=", ".join(api_args_names),
+                ),
+                file=f_server,
+            )
+
             print(
                 """
     printf("Sending call {id}\\nBUF: ");
@@ -119,6 +154,15 @@ def main():
                 file=f_client,
             )
 
+        print("""\
+    default:
+        printf("Error: API function %x is unknown!!\\n", {id});
+        break;
+    }}
+}}""".format(
+            id=api_id,
+        ), file=f_server)
+
 
 if __name__ == "__main__":
     main()
diff --git a/hw-tests/api-demo/main.c b/hw-tests/api-demo/main.c
index bb49d153b..ac6153465 100644
--- a/hw-tests/api-demo/main.c
+++ b/hw-tests/api-demo/main.c
@@ -19,6 +19,7 @@ void Core1_Stop(void) {
 int main(void)
 {
     int count = 0;
+    led_color_t red = {0x10, 0, 0};
 
     card10_init();
     card10_diag();
@@ -31,6 +32,8 @@ int main(void)
     api_set_buzzer(1);
     TMR_Delay(MXC_TMR0, MSEC(300), 0);
     api_set_buzzer(0);
+    api_set_led(5, red);
+    api_test(0xAB, 0x7DEF, 0x01, 0x02);
 
     while(1) {
         printf("count = %d\n", count++);
-- 
GitLab