forked from richardcochran/linuxptp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pmc_common.c
187 lines (163 loc) · 4.56 KB
/
pmc_common.c
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* @file pmc_common.c
* @note Copyright (C) 2012 Richard Cochran <[email protected]>
* @note Copyright (C) 2013 Miroslav Lichvar <[email protected]>
*
* 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 2 of the License, 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; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <string.h>
#include <stdlib.h>
#include "print.h"
#include "tlv.h"
#include "transport.h"
#include "util.h"
struct pmc {
UInteger16 sequence_id;
UInteger8 boundary_hops;
UInteger8 domain_number;
UInteger8 transport_specific;
struct PortIdentity port_identity;
struct transport *transport;
struct fdarray fdarray;
};
struct pmc *pmc_create(enum transport_type transport_type, char *iface_name,
UInteger8 boundary_hops, UInteger8 domain_number,
UInteger8 transport_specific)
{
struct pmc *pmc;
pmc = calloc(1, sizeof *pmc);
if (!pmc)
return NULL;
if (transport_type != TRANS_UDS &&
generate_clock_identity(&pmc->port_identity.clockIdentity,
iface_name)) {
pr_err("failed to generate a clock identity");
goto failed;
}
pmc->port_identity.portNumber = 1;
pmc->boundary_hops = boundary_hops;
pmc->domain_number = domain_number;
pmc->transport_specific = transport_specific;
pmc->transport = transport_create(transport_type);
if (!pmc->transport) {
pr_err("failed to create transport");
goto failed;
}
if (transport_open(pmc->transport, iface_name,
&pmc->fdarray, TS_SOFTWARE)) {
pr_err("failed to open transport");
goto failed;
}
return pmc;
failed:
if (pmc->transport)
transport_destroy(pmc->transport);
free(pmc);
return NULL;
}
void pmc_destroy(struct pmc *pmc)
{
transport_close(pmc->transport, &pmc->fdarray);
transport_destroy(pmc->transport);
free(pmc);
}
static struct ptp_message *pmc_message(struct pmc *pmc, uint8_t action)
{
struct ptp_message *msg;
int pdulen;
msg = msg_allocate();
if (!msg)
return NULL;
pdulen = sizeof(struct management_msg);
msg->hwts.type = TS_SOFTWARE;
msg->header.tsmt = MANAGEMENT | pmc->transport_specific;
msg->header.ver = PTP_VERSION;
msg->header.messageLength = pdulen;
msg->header.domainNumber = pmc->domain_number;
msg->header.sourcePortIdentity = pmc->port_identity;
msg->header.sequenceId = pmc->sequence_id++;
msg->header.control = CTL_MANAGEMENT;
msg->header.logMessageInterval = 0x7f;
memset(&msg->management.targetPortIdentity, 0xff,
sizeof(msg->management.targetPortIdentity));
msg->management.startingBoundaryHops = pmc->boundary_hops;
msg->management.boundaryHops = pmc->boundary_hops;
msg->management.flags = action;
return msg;
}
static int pmc_send(struct pmc *pmc, struct ptp_message *msg, int pdulen)
{
int cnt, err;
err = msg_pre_send(msg);
if (err) {
pr_err("msg_pre_send failed");
return -1;
}
cnt = transport_send(pmc->transport, &pmc->fdarray, 0,
msg, pdulen, &msg->hwts);
if (cnt < 0) {
pr_err("failed to send message");
return -1;
}
return 0;
}
int pmc_get_transport_fd(struct pmc *pmc)
{
return pmc->fdarray.fd[FD_GENERAL];
}
int pmc_send_get_action(struct pmc *pmc, int id)
{
int pdulen;
struct ptp_message *msg;
struct management_tlv *mgt;
msg = pmc_message(pmc, GET);
if (!msg) {
return -1;
}
mgt = (struct management_tlv *) msg->management.suffix;
mgt->type = TLV_MANAGEMENT;
mgt->length = 2;
mgt->id = id;
pdulen = msg->header.messageLength + sizeof(*mgt);
msg->header.messageLength = pdulen;
msg->tlv_count = 1;
pmc_send(pmc, msg, pdulen);
msg_put(msg);
return 0;
}
struct ptp_message *pmc_recv(struct pmc *pmc)
{
struct ptp_message *msg;
int cnt;
msg = msg_allocate();
if (!msg) {
pr_err("low memory");
return NULL;
}
msg->hwts.type = TS_SOFTWARE;
cnt = transport_recv(pmc->transport, pmc_get_transport_fd(pmc),
msg, sizeof(msg->data), &msg->hwts);
if (cnt <= 0) {
pr_err("recv message failed");
goto failed;
} else if (msg_post_recv(msg, cnt)) {
pr_err("bad message");
goto failed;
}
return msg;
failed:
msg_put(msg);
return NULL;
}