summaryrefslogtreecommitdiffstats
path: root/services/common_time/common_time_server_packets.cpp
blob: 9833c37f2519b10120f0f1f01817e3c4c98bedab (plain)
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * A service that exchanges time synchronization information between
 * a master that defines a timeline and clients that follow the timeline.
 */

#define LOG_TAG "common_time"
#include <utils/Log.h>

#include <arpa/inet.h>
#include <stdint.h>

#include "common_time_server_packets.h"

namespace android {

const uint32_t TimeServicePacketHeader::kMagic =
    (static_cast<uint32_t>('c') << 24) |
    (static_cast<uint32_t>('c') << 16) |
    (static_cast<uint32_t>('l') <<  8) |
     static_cast<uint32_t>('k');

const uint16_t TimeServicePacketHeader::kCurVersion = 1;

#define SERIALIZE_FIELD(field_name, type, converter)        \
    do {                                                    \
        if ((offset + sizeof(field_name)) > length)         \
            return -1;                                      \
        *((type*)(data + offset)) = converter(field_name);  \
        offset += sizeof(field_name);                       \
    } while (0)
#define SERIALIZE_INT16(field_name) SERIALIZE_FIELD(field_name, int16_t, htons)
#define SERIALIZE_INT32(field_name) SERIALIZE_FIELD(field_name, int32_t, htonl)
#define SERIALIZE_INT64(field_name) SERIALIZE_FIELD(field_name, int64_t, htonq)

#define DESERIALIZE_FIELD(field_name, type, converter)      \
    do {                                                    \
        if ((offset + sizeof(field_name)) > length)         \
            return -1;                                      \
        field_name = converter(*((type*)(data + offset)));  \
        offset += sizeof(field_name);                       \
    } while (0)
#define DESERIALIZE_INT16(field_name) DESERIALIZE_FIELD(field_name, int16_t, ntohs)
#define DESERIALIZE_INT32(field_name) DESERIALIZE_FIELD(field_name, int32_t, ntohl)
#define DESERIALIZE_INT64(field_name) DESERIALIZE_FIELD(field_name, int64_t, ntohq)

#define kDevicePriorityShift 56
#define kDeviceIDMask ((static_cast<uint64_t>(1) << kDevicePriorityShift) - 1)

inline uint64_t packDeviceID(uint64_t devID, uint8_t prio) {
    return (devID & kDeviceIDMask) |
           (static_cast<uint64_t>(prio) << kDevicePriorityShift);
}

inline uint64_t unpackDeviceID(uint64_t packed) {
    return (packed & kDeviceIDMask);
}

inline uint8_t unpackDevicePriority(uint64_t packed) {
    return static_cast<uint8_t>(packed >> kDevicePriorityShift);
}

ssize_t TimeServicePacketHeader::serializeHeader(uint8_t* data,
                                                 uint32_t length) {
    ssize_t offset = 0;
    int16_t pktType = static_cast<int16_t>(packetType);
    SERIALIZE_INT32(magic);
    SERIALIZE_INT16(version);
    SERIALIZE_INT16(pktType);
    SERIALIZE_INT64(timelineID);
    SERIALIZE_INT64(syncGroupID);
    return offset;
}

ssize_t TimeServicePacketHeader::deserializeHeader(const uint8_t* data,
                                                   uint32_t length) {
    ssize_t offset = 0;
    int16_t tmp;
    DESERIALIZE_INT32(magic);
    DESERIALIZE_INT16(version);
    DESERIALIZE_INT16(tmp);
    DESERIALIZE_INT64(timelineID);
    DESERIALIZE_INT64(syncGroupID);
    packetType = static_cast<TimeServicePacketType>(tmp);
    return offset;
}

ssize_t TimeServicePacketHeader::serializePacket(uint8_t* data,
                                                 uint32_t length) {
    ssize_t ret, tmp;

    ret = serializeHeader(data, length);
    if (ret < 0)
        return ret;

    data += ret;
    length -= ret;

    switch (packetType) {
        case TIME_PACKET_WHO_IS_MASTER_REQUEST:
            tmp =((WhoIsMasterRequestPacket*)(this))->serializePacket(data,
                                                                      length);
            break;
        case TIME_PACKET_WHO_IS_MASTER_RESPONSE:
            tmp =((WhoIsMasterResponsePacket*)(this))->serializePacket(data,
                                                                       length);
            break;
        case TIME_PACKET_SYNC_REQUEST:
            tmp =((SyncRequestPacket*)(this))->serializePacket(data, length);
            break;
        case TIME_PACKET_SYNC_RESPONSE:
            tmp =((SyncResponsePacket*)(this))->serializePacket(data, length);
            break;
        case TIME_PACKET_MASTER_ANNOUNCEMENT:
            tmp =((MasterAnnouncementPacket*)(this))->serializePacket(data,
                                                                      length);
            break;
        default:
            return -1;
    }

    if (tmp < 0)
        return tmp;

    return ret + tmp;
}

ssize_t UniversalTimeServicePacket::deserializePacket(
        const uint8_t* data,
        uint32_t length,
        uint64_t expectedSyncGroupID) {
    ssize_t ret;
    TimeServicePacketHeader* header;
    if (length < 8)
        return -1;

    packetType = ntohs(*((uint16_t*)(data + 6)));
    switch (packetType) {
        case TIME_PACKET_WHO_IS_MASTER_REQUEST:
            ret = p.who_is_master_request.deserializePacket(data, length);
            header = &p.who_is_master_request;
            break;
        case TIME_PACKET_WHO_IS_MASTER_RESPONSE:
            ret = p.who_is_master_response.deserializePacket(data, length);
            header = &p.who_is_master_response;
            break;
        case TIME_PACKET_SYNC_REQUEST:
            ret = p.sync_request.deserializePacket(data, length);
            header = &p.sync_request;
            break;
        case TIME_PACKET_SYNC_RESPONSE:
            ret = p.sync_response.deserializePacket(data, length);
            header = &p.sync_response;
            break;
        case TIME_PACKET_MASTER_ANNOUNCEMENT:
            ret = p.master_announcement.deserializePacket(data, length);
            header = &p.master_announcement;
            break;
        default:
            return -1;
    }

    if ((ret >= 0) && !header->checkPacket(expectedSyncGroupID))
        ret = -1;

    return ret;
}

ssize_t WhoIsMasterRequestPacket::serializePacket(uint8_t* data,
                                                  uint32_t length) {
    ssize_t offset = serializeHeader(data, length);
    if (offset > 0) {
        uint64_t packed = packDeviceID(senderDeviceID, senderDevicePriority);
        SERIALIZE_INT64(packed);
    }
    return offset;
}

ssize_t WhoIsMasterRequestPacket::deserializePacket(const uint8_t* data,
                                                    uint32_t length) {
    ssize_t offset = deserializeHeader(data, length);
    if (offset > 0) {
        uint64_t packed;
        DESERIALIZE_INT64(packed);
        senderDeviceID       = unpackDeviceID(packed);
        senderDevicePriority = unpackDevicePriority(packed);
    }
    return offset;
}

ssize_t WhoIsMasterResponsePacket::serializePacket(uint8_t* data,
                                                   uint32_t length) {
    ssize_t offset = serializeHeader(data, length);
    if (offset > 0) {
        uint64_t packed = packDeviceID(deviceID, devicePriority);
        SERIALIZE_INT64(packed);
    }
    return offset;
}

ssize_t WhoIsMasterResponsePacket::deserializePacket(const uint8_t* data,
                                                     uint32_t length) {
    ssize_t offset = deserializeHeader(data, length);
    if (offset > 0) {
        uint64_t packed;
        DESERIALIZE_INT64(packed);
        deviceID       = unpackDeviceID(packed);
        devicePriority = unpackDevicePriority(packed);
    }
    return offset;
}

ssize_t SyncRequestPacket::serializePacket(uint8_t* data,
                                           uint32_t length) {
    ssize_t offset = serializeHeader(data, length);
    if (offset > 0) {
        SERIALIZE_INT64(clientTxLocalTime);
    }
    return offset;
}

ssize_t SyncRequestPacket::deserializePacket(const uint8_t* data,
                                             uint32_t length) {
    ssize_t offset = deserializeHeader(data, length);
    if (offset > 0) {
        DESERIALIZE_INT64(clientTxLocalTime);
    }
    return offset;
}

ssize_t SyncResponsePacket::serializePacket(uint8_t* data,
                                            uint32_t length) {
    ssize_t offset = serializeHeader(data, length);
    if (offset > 0) {
        SERIALIZE_INT64(clientTxLocalTime);
        SERIALIZE_INT64(masterRxCommonTime);
        SERIALIZE_INT64(masterTxCommonTime);
        SERIALIZE_INT32(nak);
    }
    return offset;
}

ssize_t SyncResponsePacket::deserializePacket(const uint8_t* data,
                                              uint32_t length) {
    ssize_t offset = deserializeHeader(data, length);
    if (offset > 0) {
        DESERIALIZE_INT64(clientTxLocalTime);
        DESERIALIZE_INT64(masterRxCommonTime);
        DESERIALIZE_INT64(masterTxCommonTime);
        DESERIALIZE_INT32(nak);
    }
    return offset;
}

ssize_t MasterAnnouncementPacket::serializePacket(uint8_t* data,
                                                  uint32_t length) {
    ssize_t offset = serializeHeader(data, length);
    if (offset > 0) {
        uint64_t packed = packDeviceID(deviceID, devicePriority);
        SERIALIZE_INT64(packed);
    }
    return offset;
}

ssize_t MasterAnnouncementPacket::deserializePacket(const uint8_t* data,
                                                    uint32_t length) {
    ssize_t offset = deserializeHeader(data, length);
    if (offset > 0) {
        uint64_t packed;
        DESERIALIZE_INT64(packed);
        deviceID       = unpackDeviceID(packed);
        devicePriority = unpackDevicePriority(packed);
    }
    return offset;
}

}  // namespace android