154c34be7SAxel Dörfler/*
254c34be7SAxel Dörfler * Copyright 2006-2015, Haiku, Inc. All Rights Reserved.
354c34be7SAxel Dörfler * Distributed under the terms of the MIT License.
454c34be7SAxel Dörfler *
554c34be7SAxel Dörfler * Authors:
654c34be7SAxel Dörfler *		Axel D��rfler, axeld@pinc-software.de
754c34be7SAxel Dörfler *		Oliver Tappe, zooey@hirschkaefer.de
854c34be7SAxel Dörfler *		Atis Elsts, the.kfx@gmail.com
954c34be7SAxel Dörfler */
1054c34be7SAxel Dörfler
1154c34be7SAxel Dörfler
1254c34be7SAxel Dörfler#include "MediaTypes.h"
1354c34be7SAxel Dörfler
1454c34be7SAxel Dörfler#include <net/if_media.h>
1549b69c86SAxel Dörfler#include <string.h>
1654c34be7SAxel Dörfler
1754c34be7SAxel Dörfler
1854c34be7SAxel Dörflerstruct media_type {
1954c34be7SAxel Dörfler	int			type;
2054c34be7SAxel Dörfler	const char*	name;
2154c34be7SAxel Dörfler	const char* pretty;
2254c34be7SAxel Dörfler	struct {
2354c34be7SAxel Dörfler		int subtype;
2454c34be7SAxel Dörfler		const char* name;
2554c34be7SAxel Dörfler		const char* pretty;
2654c34be7SAxel Dörfler	} subtypes [6];
2754c34be7SAxel Dörfler	struct {
2854c34be7SAxel Dörfler		int option;
2954c34be7SAxel Dörfler		bool read_only;
3054c34be7SAxel Dörfler		const char* name;
3154c34be7SAxel Dörfler		const char* pretty;
3254c34be7SAxel Dörfler	} options [6];
3354c34be7SAxel Dörfler};
3454c34be7SAxel Dörfler
3554c34be7SAxel Dörfler
3654c34be7SAxel Dörflerconst media_type kMediaTypes[] = {
3754c34be7SAxel Dörfler	{
3854c34be7SAxel Dörfler		0, // for generic options
3954c34be7SAxel Dörfler		"all",
4054c34be7SAxel Dörfler		"All",
4154c34be7SAxel Dörfler		{
4254c34be7SAxel Dörfler			{ IFM_AUTO, "auto", "Auto-select" },
4354c34be7SAxel Dörfler			{ -1, NULL, NULL }
4454c34be7SAxel Dörfler		},
4554c34be7SAxel Dörfler		{
4654c34be7SAxel Dörfler			{ IFM_FULL_DUPLEX, true, "fullduplex", "Full Duplex" },
4754c34be7SAxel Dörfler			{ IFM_HALF_DUPLEX, true, "halfduplex", "Half Duplex" },
4854c34be7SAxel Dörfler			{ IFM_LOOP, true, "loop", "Loop" },
4954c34be7SAxel Dörfler			//{ IFM_ACTIVE, false, "active", "Active" },
5054c34be7SAxel Dörfler			{ -1, false, NULL, NULL }
5154c34be7SAxel Dörfler		}
5254c34be7SAxel Dörfler	},
5354c34be7SAxel Dörfler	{
5454c34be7SAxel Dörfler		IFM_ETHER,
5554c34be7SAxel Dörfler		"ether",
5654c34be7SAxel Dörfler		"Ethernet",
5754c34be7SAxel Dörfler		{
5854c34be7SAxel Dörfler			//{ IFM_AUTO, "auto", "Auto-select" },
5954c34be7SAxel Dörfler			//{ IFM_AUI, "AUI", "10 MBit, AUI" },
6054c34be7SAxel Dörfler			//{ IFM_10_2, "10base2", "10 MBit, 10BASE-2" },
6154c34be7SAxel Dörfler			{ IFM_10_T, "10baseT", "10 MBit, 10BASE-T" },
6254c34be7SAxel Dörfler			{ IFM_100_TX, "100baseTX", "100 MBit, 100BASE-TX" },
6354c34be7SAxel Dörfler			{ IFM_1000_T, "1000baseT", "1 GBit, 1000BASE-T" },
6454c34be7SAxel Dörfler			{ IFM_1000_SX, "1000baseSX", "1 GBit, 1000BASE-SX" },
6554c34be7SAxel Dörfler			{ IFM_10G_T, "10GbaseT", "10 GBit, 10GBASE-T" },
6654c34be7SAxel Dörfler			{ -1, NULL, NULL }
6754c34be7SAxel Dörfler		},
6854c34be7SAxel Dörfler		{
6954c34be7SAxel Dörfler			{ -1, false, NULL, NULL }
7054c34be7SAxel Dörfler		}
7154c34be7SAxel Dörfler	},
7254c34be7SAxel Dörfler	{ -1, NULL, NULL, {{ -1, NULL, NULL }}, {{ -1, false, NULL, NULL }} }
7354c34be7SAxel Dörfler};
7454c34be7SAxel Dörfler
7554c34be7SAxel Dörfler
7654c34be7SAxel Dörflerconst char*
7754c34be7SAxel Dörflerget_media_type_name(size_t index)
7854c34be7SAxel Dörfler{
7954c34be7SAxel Dörfler	if (index < sizeof(kMediaTypes) / sizeof(kMediaTypes[0]))
8054c34be7SAxel Dörfler		return kMediaTypes[index].pretty;
8154c34be7SAxel Dörfler
8254c34be7SAxel Dörfler	return NULL;
8354c34be7SAxel Dörfler}
8454c34be7SAxel Dörfler
8554c34be7SAxel Dörfler
8654c34be7SAxel Dörflerconst char*
8754c34be7SAxel Dörflerget_media_subtype_name(size_t typeIndex, size_t subIndex)
8854c34be7SAxel Dörfler{
8954c34be7SAxel Dörfler	if (typeIndex < sizeof(kMediaTypes) / sizeof(kMediaTypes[0])) {
9054c34be7SAxel Dörfler		if (kMediaTypes[typeIndex].subtypes[subIndex].subtype >= 0)
9154c34be7SAxel Dörfler			return kMediaTypes[typeIndex].subtypes[subIndex].name;
9254c34be7SAxel Dörfler	}
9354c34be7SAxel Dörfler
9454c34be7SAxel Dörfler	return NULL;
9554c34be7SAxel Dörfler}
9654c34be7SAxel Dörfler
9754c34be7SAxel Dörfler
9854c34be7SAxel Dörflerbool
9954c34be7SAxel Dörflermedia_parse_subtype(const char* string, int media, int* type)
10054c34be7SAxel Dörfler{
10154c34be7SAxel Dörfler	for (size_t i = 0; kMediaTypes[i].type >= 0; i++) {
10254c34be7SAxel Dörfler		// only check for generic or correct subtypes
10354c34be7SAxel Dörfler		if (kMediaTypes[i].type &&
10454c34be7SAxel Dörfler			kMediaTypes[i].type != media)
10554c34be7SAxel Dörfler			continue;
10654c34be7SAxel Dörfler		for (size_t j = 0; kMediaTypes[i].subtypes[j].subtype >= 0; j++) {
10754c34be7SAxel Dörfler			if (strcmp(kMediaTypes[i].subtypes[j].name, string) == 0) {
10854c34be7SAxel Dörfler				// found a match
10954c34be7SAxel Dörfler				*type = kMediaTypes[i].subtypes[j].subtype;
11054c34be7SAxel Dörfler				return true;
11154c34be7SAxel Dörfler			}
11254c34be7SAxel Dörfler		}
11354c34be7SAxel Dörfler	}
11454c34be7SAxel Dörfler	return false;
11554c34be7SAxel Dörfler}
11654c34be7SAxel Dörfler
11754c34be7SAxel Dörfler
11854c34be7SAxel Dörflerconst char*
11954c34be7SAxel Dörflermedia_type_to_string(int media)
12054c34be7SAxel Dörfler{
12154c34be7SAxel Dörfler	for (size_t i = 0; kMediaTypes[i].type >= 0; i++) {
12254c34be7SAxel Dörfler		// loopback doesn't really have a media anyway
12354c34be7SAxel Dörfler		if (IFM_TYPE(media) == 0)
12454c34be7SAxel Dörfler			break;
12554c34be7SAxel Dörfler
12654c34be7SAxel Dörfler		// only check for generic or correct subtypes
12754c34be7SAxel Dörfler		if (kMediaTypes[i].type
12854c34be7SAxel Dörfler			&& kMediaTypes[i].type != IFM_TYPE(media))
12954c34be7SAxel Dörfler			continue;
13054c34be7SAxel Dörfler
13154c34be7SAxel Dörfler		for (size_t j = 0; kMediaTypes[i].subtypes[j].subtype >= 0; j++) {
13254c34be7SAxel Dörfler			if (kMediaTypes[i].subtypes[j].subtype == IFM_SUBTYPE(media)) {
13354c34be7SAxel Dörfler				// found a match
13454c34be7SAxel Dörfler				return kMediaTypes[i].subtypes[j].pretty;
13554c34be7SAxel Dörfler			}
13654c34be7SAxel Dörfler		}
13754c34be7SAxel Dörfler	}
13854c34be7SAxel Dörfler
13954c34be7SAxel Dörfler	return NULL;
14054c34be7SAxel Dörfler}
141