Просмотр исходного кода

Merge pull request #23 from yangfasheng/master

添加管道功能代码 jerry_message.c
yangfasheng 7 лет назад
Родитель
Сommit
dfeeb6fefe

+ 3 - 1
rtthread-port/jerry_callbacks.c

@@ -116,7 +116,7 @@ void js_call_callback(struct js_callback *callback, const void *data, uint32_t s
     }
 }
 
-void js_send_callback(struct js_callback *callback, const void *args, uint32_t size)
+rt_bool_t js_send_callback(struct js_callback *callback, const void *args, uint32_t size)
 {
     rt_bool_t ret = RT_FALSE;
     struct js_mq_callback *jmc = NULL;
@@ -143,6 +143,8 @@ void js_send_callback(struct js_callback *callback, const void *args, uint32_t s
             rt_free(jmc);
         }
     }
+    
+    return ret;
 }
 
 void js_mq_func_set(js_mq_func signal)

+ 1 - 1
rtthread-port/jerry_callbacks.h

@@ -31,7 +31,7 @@ struct js_callback *js_add_callback(js_callback_func callback);
 void js_remove_callback(struct js_callback *callback);
 void js_remove_all_callbacks(void);
 void js_call_callback(struct js_callback *callback, const void *data, uint32_t size);
-void js_send_callback(struct js_callback *callback, const void *args, uint32_t size);
+rt_bool_t js_send_callback(struct js_callback *callback, const void *args, uint32_t size);
 void js_mq_func_set(js_mq_func signal);
 
 #ifdef __cplusplus

+ 106 - 0
rtthread-port/jerry_message.c

@@ -0,0 +1,106 @@
+
+
+#include <jerry_message.h>
+#include <jerry_buffer.h>
+
+static jerry_value_t js_message_obj;
+static struct js_callback *js_message_cb = NULL;
+
+struct js_message
+{
+    char *name;
+    rt_uint8_t *data;
+    rt_uint16_t size;
+};
+
+rt_bool_t js_message_send(const char *name, rt_uint8_t *data, rt_uint16_t size)
+{
+    rt_bool_t ret = RT_FALSE;
+
+    if (js_message_cb)
+    {
+        struct js_message *msg = rt_malloc(sizeof(struct js_message));
+        if (msg)
+        {
+            msg->name = rt_strdup(name);
+            msg->size = size;
+            msg->data = rt_malloc(msg->size);
+            if (msg->data)
+            {
+                rt_memcpy(msg->data, data, size);
+                ret = js_send_callback(js_message_cb, msg, sizeof(struct js_message));
+            }
+            else
+            {
+                rt_free(msg);
+            }
+        }
+    }
+
+    return ret;
+}
+
+static void js_callback_message(const void *args, uint32_t size)
+{
+    struct js_message *msg = (struct js_message *)args;
+    if (msg)
+    {
+        js_buffer_t *buffer;
+        jerry_value_t buf_obj = jerry_buffer_create(msg->size, &buffer);
+        if (buffer)
+        {
+            if (buffer->bufsize > 0)
+            {
+                js_set_property(buf_obj, "length", jerry_create_number(buffer->bufsize));
+                rt_memcpy(buffer->buffer, msg->data, msg->size);
+                js_emit_event(js_message_obj, msg->name, &buf_obj, 1);
+            }
+        }
+        /* release value */
+        jerry_release_value(buf_obj);
+        rt_free(msg->name);
+        rt_free(msg->data);
+        rt_free(msg);
+    }
+}
+
+DECLARE_HANDLER(Message)
+{
+    js_message_obj = jerry_create_object();
+    js_make_emitter(js_message_obj, jerry_create_undefined());
+    js_message_cb = js_add_callback(js_callback_message);
+    if (js_message_cb)
+    {
+        return jerry_acquire_value(js_message_obj);
+    }
+    else
+    {
+        jerry_release_value(js_message_obj);
+        return jerry_create_undefined();
+    }
+}
+
+int js_message_init(void)
+{
+    REGISTER_HANDLER(Message);
+
+    return 0;
+}
+
+int js_message_deinit(void)
+{
+    if (js_message_cb)
+    {
+        jerry_release_value(js_message_obj);
+        js_message_cb = NULL;
+    }
+
+    return 0;
+}
+
+rt_bool_t js_msg_test(void)
+{
+    char *data = "jerry message test!!!";
+    return js_message_send("test", (rt_uint8_t *)data, rt_strlen(data));
+}
+MSH_CMD_EXPORT(js_msg_test, jerry message test);

+ 22 - 0
rtthread-port/jerry_message.h

@@ -0,0 +1,22 @@
+
+#ifndef JERRY_MESSAGE_H__
+#define JERRY_MESSAGE_H__
+
+#include <rtthread.h>
+#include <jerry_util.h>
+#include <jerry_callbacks.h>
+#include <jerry_event.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+rt_bool_t js_message_send(const char *name, rt_uint8_t *data, rt_uint16_t size);
+int js_message_init(void);
+int js_message_deinit(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif

+ 3 - 0
rtthread-port/jerry_util.c

@@ -6,6 +6,7 @@
 #include <dfs_posix.h>
 #include "jerry_util.h"
 #include <jerry_event.h>
+#include <jerry_message.h>
 
 static rt_mutex_t _util_lock = RT_NULL;
 
@@ -250,6 +251,7 @@ int js_util_init(void)
     js_module_init();
     js_buffer_init();
     js_event_init();
+    js_message_init();
     if (_user_init != NULL)
     {
         _user_init();
@@ -262,6 +264,7 @@ int js_util_cleanup(void)
 {
     js_buffer_cleanup();
     js_event_deinit();
+    js_message_deinit();
     if (_user_cleanup != NULL)
     {
         _user_cleanup();