aboutsummaryrefslogtreecommitdiffstats
path: root/system/doc/tutorial/port_driver.c
diff options
context:
space:
mode:
authorErlang/OTP <[email protected]>2009-11-20 14:54:40 +0000
committerErlang/OTP <[email protected]>2009-11-20 14:54:40 +0000
commit84adefa331c4159d432d22840663c38f155cd4c1 (patch)
treebff9a9c66adda4df2106dfd0e5c053ab182a12bd /system/doc/tutorial/port_driver.c
downloadotp-84adefa331c4159d432d22840663c38f155cd4c1.tar.gz
otp-84adefa331c4159d432d22840663c38f155cd4c1.tar.bz2
otp-84adefa331c4159d432d22840663c38f155cd4c1.zip
The R13B03 release.OTP_R13B03
Diffstat (limited to 'system/doc/tutorial/port_driver.c')
-rw-r--r--system/doc/tutorial/port_driver.c52
1 files changed, 52 insertions, 0 deletions
diff --git a/system/doc/tutorial/port_driver.c b/system/doc/tutorial/port_driver.c
new file mode 100644
index 0000000000..d428d08ff3
--- /dev/null
+++ b/system/doc/tutorial/port_driver.c
@@ -0,0 +1,52 @@
+/* port_driver.c */
+
+#include <stdio.h>
+#include "erl_driver.h"
+
+typedef struct {
+ ErlDrvPort port;
+} example_data;
+
+static ErlDrvData example_drv_start(ErlDrvPort port, char *buff)
+{
+ example_data* d = (example_data*)driver_alloc(sizeof(example_data));
+ d->port = port;
+ return (ErlDrvData)d;
+}
+
+static void example_drv_stop(ErlDrvData handle)
+{
+ driver_free((char*)handle);
+}
+
+static void example_drv_output(ErlDrvData handle, char *buff, int bufflen)
+{
+ example_data* d = (example_data*)handle;
+ char fn = buff[0], arg = buff[1], res;
+ if (fn == 1) {
+ res = foo(arg);
+ } else if (fn == 2) {
+ res = bar(arg);
+ }
+ driver_output(d->port, &res, 1);
+}
+
+ErlDrvEntry example_driver_entry = {
+ NULL, /* F_PTR init, N/A */
+ example_drv_start, /* L_PTR start, called when port is opened */
+ example_drv_stop, /* F_PTR stop, called when port is closed */
+ example_drv_output, /* F_PTR output, called when erlang has sent */
+ NULL, /* F_PTR ready_input, called when input descriptor ready */
+ NULL, /* F_PTR ready_output, called when output descriptor ready */
+ "example_drv", /* char *driver_name, the argument to open_port */
+ NULL, /* F_PTR finish, called when unloaded */
+ NULL, /* F_PTR control, port_command callback */
+ NULL, /* F_PTR timeout, reserved */
+ NULL /* F_PTR outputv, reserved */
+};
+
+DRIVER_INIT(example_drv) /* must match name in driver_entry */
+{
+ return &example_driver_entry;
+}
+