1/*
2 * Copyright 2006-2015, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Axel Dörfler, axeld@pinc-software.de
7 *		Oliver Tappe, zooey@hirschkaefer.de
8 *		Atis Elsts, the.kfx@gmail.com
9 */
10
11
12#include "MediaTypes.h"
13
14#include <net/if_media.h>
15#include <string.h>
16
17
18struct media_type {
19	int			type;
20	const char*	name;
21	const char* pretty;
22	struct {
23		int subtype;
24		const char* name;
25		const char* pretty;
26	} subtypes [6];
27	struct {
28		int option;
29		bool read_only;
30		const char* name;
31		const char* pretty;
32	} options [6];
33};
34
35
36const media_type kMediaTypes[] = {
37	{
38		0, // for generic options
39		"all",
40		"All",
41		{
42			{ IFM_AUTO, "auto", "Auto-select" },
43			{ -1, NULL, NULL }
44		},
45		{
46			{ IFM_FULL_DUPLEX, true, "fullduplex", "Full Duplex" },
47			{ IFM_HALF_DUPLEX, true, "halfduplex", "Half Duplex" },
48			{ IFM_LOOP, true, "loop", "Loop" },
49			//{ IFM_ACTIVE, false, "active", "Active" },
50			{ -1, false, NULL, NULL }
51		}
52	},
53	{
54		IFM_ETHER,
55		"ether",
56		"Ethernet",
57		{
58			//{ IFM_AUTO, "auto", "Auto-select" },
59			//{ IFM_AUI, "AUI", "10 MBit, AUI" },
60			//{ IFM_10_2, "10base2", "10 MBit, 10BASE-2" },
61			{ IFM_10_T, "10baseT", "10 MBit, 10BASE-T" },
62			{ IFM_100_TX, "100baseTX", "100 MBit, 100BASE-TX" },
63			{ IFM_1000_T, "1000baseT", "1 GBit, 1000BASE-T" },
64			{ IFM_1000_SX, "1000baseSX", "1 GBit, 1000BASE-SX" },
65			{ IFM_10G_T, "10GbaseT", "10 GBit, 10GBASE-T" },
66			{ -1, NULL, NULL }
67		},
68		{
69			{ -1, false, NULL, NULL }
70		}
71	},
72	{ -1, NULL, NULL, {{ -1, NULL, NULL }}, {{ -1, false, NULL, NULL }} }
73};
74
75
76const char*
77get_media_type_name(size_t index)
78{
79	if (index < sizeof(kMediaTypes) / sizeof(kMediaTypes[0]))
80		return kMediaTypes[index].pretty;
81
82	return NULL;
83}
84
85
86const char*
87get_media_subtype_name(size_t typeIndex, size_t subIndex)
88{
89	if (typeIndex < sizeof(kMediaTypes) / sizeof(kMediaTypes[0])) {
90		if (kMediaTypes[typeIndex].subtypes[subIndex].subtype >= 0)
91			return kMediaTypes[typeIndex].subtypes[subIndex].name;
92	}
93
94	return NULL;
95}
96
97
98bool
99media_parse_subtype(const char* string, int media, int* type)
100{
101	for (size_t i = 0; kMediaTypes[i].type >= 0; i++) {
102		// only check for generic or correct subtypes
103		if (kMediaTypes[i].type &&
104			kMediaTypes[i].type != media)
105			continue;
106		for (size_t j = 0; kMediaTypes[i].subtypes[j].subtype >= 0; j++) {
107			if (strcmp(kMediaTypes[i].subtypes[j].name, string) == 0) {
108				// found a match
109				*type = kMediaTypes[i].subtypes[j].subtype;
110				return true;
111			}
112		}
113	}
114	return false;
115}
116
117
118const char*
119media_type_to_string(int media)
120{
121	for (size_t i = 0; kMediaTypes[i].type >= 0; i++) {
122		// loopback doesn't really have a media anyway
123		if (IFM_TYPE(media) == 0)
124			break;
125
126		// only check for generic or correct subtypes
127		if (kMediaTypes[i].type
128			&& kMediaTypes[i].type != IFM_TYPE(media))
129			continue;
130
131		for (size_t j = 0; kMediaTypes[i].subtypes[j].subtype >= 0; j++) {
132			if (kMediaTypes[i].subtypes[j].subtype == IFM_SUBTYPE(media)) {
133				// found a match
134				return kMediaTypes[i].subtypes[j].pretty;
135			}
136		}
137	}
138
139	return NULL;
140}
141