173953257SAxel Dörfler/*
2c6b85a0aSAxel Dörfler * Copyright 2005-2009, Axel D��rfler, axeld@pinc-software.de.
373953257SAxel Dörfler * Distributed under the terms of the MIT License.
473953257SAxel Dörfler */
573953257SAxel Dörfler
673953257SAxel Dörfler
773953257SAxel Dörfler#include <OS.h>
873953257SAxel Dörfler#include <syscalls.h>
973953257SAxel Dörfler
1073953257SAxel Dörfler#include <stdio.h>
1173953257SAxel Dörfler#include <string.h>
1273953257SAxel Dörfler
1373953257SAxel Dörfler
1473953257SAxel Dörflerstatic const char *kPortName = "transfer area test";
1573953257SAxel Dörfler
1673953257SAxel Dörfler
1773953257SAxel Dörflerint
1873953257SAxel Dörflermain(int argc, char **argv)
1973953257SAxel Dörfler{
2073953257SAxel Dörfler	port_id port;
2173953257SAxel Dörfler	area_id area;
2273953257SAxel Dörfler
2373953257SAxel Dörfler	if (argc > 1) {
2473953257SAxel Dörfler		// we're the sender
2573953257SAxel Dörfler		port = find_port(kPortName);
2673953257SAxel Dörfler		if (port < B_OK) {
2773953257SAxel Dörfler			fprintf(stderr, "Area receiver is not yet running.\n");
2873953257SAxel Dörfler			return -1;
2973953257SAxel Dörfler		}
3073953257SAxel Dörfler
3173953257SAxel Dörfler		port_info info;
3273953257SAxel Dörfler		get_port_info(port, &info);
3373953257SAxel Dörfler
3473953257SAxel Dörfler		char *address;
35c6b85a0aSAxel Dörfler		area = create_area("test transfer area", (void **)&address,
37c6b85a0aSAxel Dörfler		if (area < 0) {
3873953257SAxel Dörfler			fprintf(stderr, "Could not create area: %s.\n", strerror(area));
39c6b85a0aSAxel Dörfler			return 1;
4073953257SAxel Dörfler		}
4173953257SAxel Dörfler
4273953257SAxel Dörfler		sprintf(address, "Oh my god - it's working! (%s)", argv[1]);
4373953257SAxel Dörfler
44c6b85a0aSAxel Dörfler		area_id targetArea = _kern_transfer_area(area, (void **)&address,
45c6b85a0aSAxel Dörfler			B_ANY_ADDRESS, info.team);
46c6b85a0aSAxel Dörfler		if (targetArea < 0) {
47c6b85a0aSAxel Dörfler			fprintf(stderr, "Could not transfer area: %s.\n",
48c6b85a0aSAxel Dörfler				strerror(targetArea));
49c6b85a0aSAxel Dörfler			return 1;
5073953257SAxel Dörfler		}
5173953257SAxel Dörfler
52c6b85a0aSAxel Dörfler		write_port(port, targetArea, NULL, 0);
5373953257SAxel Dörfler	} else {
5473953257SAxel Dörfler		// we're the receiver
5573953257SAxel Dörfler		port = create_port(1, kPortName);
56c6b85a0aSAxel Dörfler		if (port < 0) {
5773953257SAxel Dörfler			fprintf(stderr, "Could not create port: %s.\n", strerror(area));
58c6b85a0aSAxel Dörfler			return 1;
5973953257SAxel Dörfler		}
6073953257SAxel Dörfler
61c6b85a0aSAxel Dörfler		puts("Waiting for an area to be received (start command again with an "
62c6b85a0aSAxel Dörfler			"argument)...");
6373953257SAxel Dörfler
6473953257SAxel Dörfler		ssize_t size;
6573953257SAxel Dörfler		if ((size = read_port(port, (int32 *)&area, NULL, 0)) < B_OK) {
6673953257SAxel Dörfler			fprintf(stderr, "Reading from port failed: %s.\n", strerror(size));
67c6b85a0aSAxel Dörfler			return 1;
6873953257SAxel Dörfler		}
6973953257SAxel Dörfler
7073953257SAxel Dörfler		printf("Received Area %ld\n", area);
7173953257SAxel Dörfler
7273953257SAxel Dörfler		area_info info;
73c6b85a0aSAxel Dörfler		if (get_area_info(area, &info) != B_OK) {
74c6b85a0aSAxel Dörfler			fprintf(stderr, "Could not get area info.\n");
75c6b85a0aSAxel Dörfler			return 1;
76c6b85a0aSAxel Dörfler		}
7773953257SAxel Dörfler		printf("  name = \"%s\", base = %p, size = %#lx, team = %ld\n",
7873953257SAxel Dörfler			info.name, info.address, info.size, info.team);
7973953257SAxel Dörfler		printf("  contents: %s\n", (char *)info.address);
8073953257SAxel Dörfler
8173953257SAxel Dörfler		delete_area(area);
8273953257SAxel Dörfler	}
8373953257SAxel Dörfler
8473953257SAxel Dörfler	return 0;
8573953257SAxel Dörfler}