1 #include <stddef.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <gpxe/refcnt.h>
6 #include <gpxe/process.h>
7 #include <gpxe/xfer.h>
8 #include <gpxe/open.h>
9
10 /** @file
11 *
12 * "Hello World" data source
13 *
14 */
15
16 struct hw {
17 struct refcnt refcnt;
18 struct xfer_interface xfer;
19 struct process process;
20 };
21
22 static const char hw_msg[] = "Hello world!\n";
23
hw_finished(struct hw * hw,int rc)24 static void hw_finished ( struct hw *hw, int rc ) {
25 xfer_nullify ( &hw->xfer );
26 xfer_close ( &hw->xfer, rc );
27 process_del ( &hw->process );
28 }
29
hw_xfer_close(struct xfer_interface * xfer,int rc)30 static void hw_xfer_close ( struct xfer_interface *xfer, int rc ) {
31 struct hw *hw = container_of ( xfer, struct hw, xfer );
32
33 hw_finished ( hw, rc );
34 }
35
36 static struct xfer_interface_operations hw_xfer_operations = {
37 .close = hw_xfer_close,
38 .vredirect = ignore_xfer_vredirect,
39 .window = unlimited_xfer_window,
40 .alloc_iob = default_xfer_alloc_iob,
41 .deliver_iob = xfer_deliver_as_raw,
42 .deliver_raw = ignore_xfer_deliver_raw,
43 };
44
hw_step(struct process * process)45 static void hw_step ( struct process *process ) {
46 struct hw *hw = container_of ( process, struct hw, process );
47 int rc;
48
49 if ( xfer_window ( &hw->xfer ) ) {
50 rc = xfer_deliver_raw ( &hw->xfer, hw_msg, sizeof ( hw_msg ) );
51 hw_finished ( hw, rc );
52 }
53 }
54
hw_open(struct xfer_interface * xfer,struct uri * uri __unused)55 static int hw_open ( struct xfer_interface *xfer, struct uri *uri __unused ) {
56 struct hw *hw;
57
58 /* Allocate and initialise structure */
59 hw = zalloc ( sizeof ( *hw ) );
60 if ( ! hw )
61 return -ENOMEM;
62 xfer_init ( &hw->xfer, &hw_xfer_operations, &hw->refcnt );
63 process_init ( &hw->process, hw_step, &hw->refcnt );
64
65 /* Attach parent interface, mortalise self, and return */
66 xfer_plug_plug ( &hw->xfer, xfer );
67 ref_put ( &hw->refcnt );
68 return 0;
69 }
70
71 struct uri_opener hw_uri_opener __uri_opener = {
72 .scheme = "hw",
73 .open = hw_open,
74 };
75