summaryrefslogtreecommitdiffstats
path: root/src/core/hw.c
blob: 91736a65294c181029d46546e88a3834e37f78df (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ipxe/refcnt.h>
#include <ipxe/process.h>
#include <ipxe/xfer.h>
#include <ipxe/open.h>

/** @file
 *
 * "Hello World" data source
 *
 */

struct hw {
	struct refcnt refcnt;
	struct interface xfer;
	struct process process;
};

static const char hw_msg[] = "Hello world!\n";

static void hw_finished ( struct hw *hw, int rc ) {
	intf_shutdown ( &hw->xfer, rc );
	process_del ( &hw->process );
}

static void hw_step ( struct hw *hw ) {
	int rc;

	if ( xfer_window ( &hw->xfer ) ) {
		rc = xfer_deliver_raw ( &hw->xfer, hw_msg, sizeof ( hw_msg ) );
		hw_finished ( hw, rc );
	}
}

static struct interface_operation hw_xfer_operations[] = {
	INTF_OP ( xfer_window_changed, struct hw *, hw_step ),
	INTF_OP ( intf_close, struct hw *, hw_finished ),
};

static struct interface_descriptor hw_xfer_desc =
	INTF_DESC ( struct hw, xfer, hw_xfer_operations );

static struct process_descriptor hw_process_desc =
	PROC_DESC_ONCE ( struct hw, process, hw_step );

static int hw_open ( struct interface *xfer, struct uri *uri __unused ) {
	struct hw *hw;

	/* Allocate and initialise structure */
	hw = zalloc ( sizeof ( *hw ) );
	if ( ! hw )
		return -ENOMEM;
	ref_init ( &hw->refcnt, NULL );
	intf_init ( &hw->xfer, &hw_xfer_desc, &hw->refcnt );
	process_init ( &hw->process, &hw_process_desc, &hw->refcnt );

	/* Attach parent interface, mortalise self, and return */
	intf_plug_plug ( &hw->xfer, xfer );
	ref_put ( &hw->refcnt );
	return 0;
}

struct uri_opener hw_uri_opener __uri_opener = {
	.scheme = "hw",
	.open = hw_open,
};