-
Notifications
You must be signed in to change notification settings - Fork 102
/
InterfaceGroup.cpp
125 lines (111 loc) · 3.87 KB
/
InterfaceGroup.cpp
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* Copyright 2013 Dominic Spill
* Copyright 2013 Adam Stasiak
*
* This file is part of USBProxy.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*
* InterfaceGroup.cpp
*
* Created on: Nov 6, 2013
*/
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include "TRACE.h"
#include "HexString.h"
#include "InterfaceGroup.h"
#include "Device.h"
#include "Interface.h"
InterfaceGroup::InterfaceGroup(__u8 _number) {
number=_number;
alternateCount=0;
activeAlternateIndex=-1;
interfaces=NULL;
}
InterfaceGroup::~InterfaceGroup() {
if (interfaces) {
int i;
for(i=0;i<alternateCount;i++) {
if (interfaces[i]) {
delete(interfaces[i]);
interfaces[i]=NULL;
}
}
free(interfaces);
interfaces=NULL;
}
}
size_t InterfaceGroup::get_full_descriptor_length() {
size_t total=0;
int i;
for(i=0;i<alternateCount;i++) {total+=interfaces[i]->get_full_descriptor_length();}
return total;
}
void InterfaceGroup::get_full_descriptor(__u8** p) {
int i;
for(i=0;i<alternateCount;i++) {interfaces[i]->get_full_descriptor(p);}
}
void InterfaceGroup::add_interface(Interface* interface) {
__u8 alternate=interface->get_descriptor()->bAlternateSetting;
if (alternate>=alternateCount) {
Interface** newInterfaces=(Interface **)calloc(alternate+1,sizeof(*interfaces));
if (alternateCount) {
memcpy(newInterfaces,interfaces,sizeof(*interfaces)*alternateCount);
free(interfaces);
/* not needed interaces=NULL; */
}
interfaces=newInterfaces;
alternateCount=alternate+1;
} else {
if (interfaces[alternate]) {delete(interfaces[alternate]);/* not needed interfaces[alternate]=NULL;*/}
}
interfaces[alternate]=interface;
}
Interface* InterfaceGroup::get_interface(__u8 alternate) {
if (alternate>=alternateCount || alternate<0) {return NULL;}
return interfaces[alternate];
}
void InterfaceGroup::print(__u8 tabs) {
int i;
printf("%.*sInterface(%d):\n",tabs,TABPADDING,number);
for(i=0;i<alternateCount;i++) {
interfaces[i]->print(tabs+1,i==activeAlternateIndex?true:false);
}
}
__u8 InterfaceGroup::get_number() {
return number;
}
__u8 InterfaceGroup::get_alternate_count() {
return alternateCount;
}
Interface* InterfaceGroup::get_active_interface() {
if (activeAlternateIndex<0) {return NULL;}
return get_interface(activeAlternateIndex);
}
const definition_error InterfaceGroup::is_defined(__u8 configId) {
if (!alternateCount) {return definition_error(DE_ERR_NULL_OBJECT,0x0, DE_OBJ_INTERFACE,configId,number,0);}
int i;
for(i=0;i<alternateCount;i++) {
if (!interfaces[i]) {return definition_error(DE_ERR_NULL_OBJECT,0x0, DE_OBJ_INTERFACE,configId,number,i);}
if (interfaces[i]->get_descriptor()->bInterfaceNumber!=number) {return definition_error(DE_ERR_MISPLACED_IF_NUM,interfaces[i]->get_descriptor()->bAlternateSetting, DE_OBJ_INTERFACE,configId,number,i);}
if (interfaces[i]->get_descriptor()->bAlternateSetting!=i) {return definition_error(DE_ERR_MISPLACED_OBJECT,interfaces[i]->get_descriptor()->bAlternateSetting, DE_OBJ_INTERFACE,configId,number,i);}
definition_error rc=interfaces[i]->is_defined(configId,number);
if (rc.error) {return rc;}
}
return definition_error();
}