1 #include <stddef.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <ipxe/refcnt.h>
6 #include <ipxe/process.h>
7 #include <ipxe/xfer.h>
8 #include <ipxe/open.h>
9 
10 /** @file
11  *
12  * "Hello World" data source
13  *
14  */
15 
16 struct hw {
17 	struct refcnt refcnt;
18 	struct 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 	intf_shutdown ( &hw->xfer, rc );
26 	process_del ( &hw->process );
27 }
28 
hw_step(struct hw * hw)29 static void hw_step ( struct hw *hw ) {
30 	int rc;
31 
32 	if ( xfer_window ( &hw->xfer ) ) {
33 		rc = xfer_deliver_raw ( &hw->xfer, hw_msg, sizeof ( hw_msg ) );
34 		hw_finished ( hw, rc );
35 	}
36 }
37 
38 static struct interface_operation hw_xfer_operations[] = {
39 	INTF_OP ( xfer_window_changed, struct hw *, hw_step ),
40 	INTF_OP ( intf_close, struct hw *, hw_finished ),
41 };
42 
43 static struct interface_descriptor hw_xfer_desc =
44 	INTF_DESC ( struct hw, xfer, hw_xfer_operations );
45 
46 static struct process_descriptor hw_process_desc =
47 	PROC_DESC_ONCE ( struct hw, process, hw_step );
48 
hw_open(struct interface * xfer,struct uri * uri __unused)49 static int hw_open ( struct interface *xfer, struct uri *uri __unused ) {
50 	struct hw *hw;
51 
52 	/* Allocate and initialise structure */
53 	hw = zalloc ( sizeof ( *hw ) );
54 	if ( ! hw )
55 		return -ENOMEM;
56 	ref_init ( &hw->refcnt, NULL );
57 	intf_init ( &hw->xfer, &hw_xfer_desc, &hw->refcnt );
58 	process_init ( &hw->process, &hw_process_desc, &hw->refcnt );
59 
60 	/* Attach parent interface, mortalise self, and return */
61 	intf_plug_plug ( &hw->xfer, xfer );
62 	ref_put ( &hw->refcnt );
63 	return 0;
64 }
65 
66 struct uri_opener hw_uri_opener __uri_opener = {
67 	.scheme = "hw",
68 	.open = hw_open,
69 };
70