aml-dbus: fixed stub code for signal handle and async call [1/1]

PD#SWPL-155651

Problem:
stub code for signal send reply message

Solution:
do not send reply message if it is not a method call
add ambus_method_call<>::set_async_reply for async reply

Verify:
local

Change-Id: Ibf7a065c509c5d88a67540fc42639b6f92205231
Signed-off-by: Daogao Xu <daogao.xu@amlogic.com>
diff --git a/aml_dbus/aml-dbus.c b/aml_dbus/aml-dbus.c
index 184c652..e4e8cbb 100644
--- a/aml_dbus/aml-dbus.c
+++ b/aml_dbus/aml-dbus.c
@@ -255,6 +255,7 @@
 
 int ambus_call_async_general(struct aml_dbus *ambus, const char *destination, const char *path, const char *interface,
                              const char *member, void *userdata, int (*msgpack)(sd_bus_message *m, void *userdata),
+                             void *reply_userdata,
                              int (*reply_msg_handle)(sd_bus_message *m, void *userdata, sd_bus_error *ret_error)) {
   int r = -1;
   int done = 0;
@@ -264,7 +265,7 @@
     if (r >= 0 && msgpack)
       r = msgpack(msg, userdata);
     if (r >= 0)
-      r = sd_bus_call_async(ambus->bus, NULL, msg, reply_msg_handle, userdata, 0);
+      r = sd_bus_call_async(ambus->bus, NULL, msg, reply_msg_handle, reply_userdata, 0);
     if (msg)
       sd_bus_message_unref(msg);
     if (p)
@@ -299,7 +300,7 @@
   int ret = ambus_unpack_all(m, pi);
   if (ret >= 0)
     ret = call();
-  if (ret >= 0) {
+  if (sd_bus_message_get_expect_reply(m) && ret >= 0) {
     sd_bus_message *reply = NULL;
     ret = sd_bus_message_new_method_return(m, &reply);
     if (ret >= 0) {
diff --git a/aml_dbus/aml-dbus.h b/aml_dbus/aml-dbus.h
index 2db459a..665e52f 100644
--- a/aml_dbus/aml-dbus.h
+++ b/aml_dbus/aml-dbus.h
@@ -452,6 +452,7 @@
 // call dbus method asynchronously, msgpack is used to pack input arguments of the method
 int ambus_call_async_general(struct aml_dbus *ambus, const char *destination, const char *path, const char *interface,
                              const char *member, void *userdata, int (*msgpack)(sd_bus_message *m, void *userdata),
+                             void *reply_userdata,
                              int (*reply_msg_handle)(sd_bus_message *m, void *userdata, sd_bus_error *ret_error));
 // call dbus method synchronously, it MUST be call in a thread other than dispatch thread
 int ambus_call_sync_general(struct aml_dbus *ambus, const char *destination, const char *path, const char *interface,
@@ -741,9 +742,20 @@
   }
   static int handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
     struct ambus_method_call<> *pcall = (struct ambus_method_call<> *)userdata;
-    return pcall->call(m);
+    pcall->async_reply = 0;
+    int r = pcall->call(m);
+    return r;
   }
   virtual int call(sd_bus_message *m) { return 0; };
+  bool async_reply;
+  static sd_bus_message *set_async_reply(struct aml_dbus *ambus) {
+    sd_bus_message *reply = NULL;
+    sd_bus_message *m = sd_bus_get_current_message(ambus_sdbus(ambus));
+    sd_bus_message_new_method_return(m, &reply);
+    struct ambus_method_call<> *pcall = (struct ambus_method_call<> *)sd_bus_get_current_userdata(ambus_sdbus(ambus));
+    pcall->async_reply = 1;
+    return reply;
+  }
 };
 template <typename RET, typename CLS, typename... Args>
 struct ambus_method_call<RET (CLS::*)(Args...)> : public ambus_method_call<> {
@@ -761,6 +773,8 @@
     RET ret;
     if (r >= 0)
       r = val.apply_member(pThis, ret, callback);
+    if (!sd_bus_message_get_expect_reply(m) || async_reply)
+      return 1;
     sd_bus_message *reply = NULL;
     if (r >= 0)
       r = sd_bus_message_new_method_return(m, &reply);
@@ -792,6 +806,8 @@
     RET ret;
     if (r >= 0)
       r = val.apply(ret, callback);
+    if (!sd_bus_message_get_expect_reply(m) || async_reply)
+      return 1;
     sd_bus_message *reply = NULL;
     if (r >= 0)
       r = sd_bus_message_new_method_return(m, &reply);
@@ -833,6 +849,14 @@
                                     ambus_method_proxy_packer::unpack);
     return ret;
   }
+  static int call_async(sd_bus_message_handler_t cb, void *userdata, struct aml_dbus *ambus, const char *service,
+                        const char *object, const char *interface, const char *member, Args... args) {
+    RET ret;
+    ambus_method_proxy_packer p = {
+        [&](sd_bus_message *msg) { return ambus_data_pack<Args...>::packall(false, msg, args...); }};
+    return ambus_call_async_general(ambus, service, object, interface, member, &p, ambus_method_proxy_packer::pack,
+                                    userdata, cb);
+  }
 };
 
 #endif