summaryrefslogtreecommitdiff
path: root/lib/avtp_pipeline/platform/Linux/openavb_time_osal.c
blob: d5b0fff9e3686a31f3cd744ed43633aa269ee46c (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
/*************************************************************************************************************
Copyright (c) 2012-2015, Symphony Teleca Corporation, a Harman International Industries, Incorporated company
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS LISTED "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS LISTED BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Attributions: The inih library portion of the source code is licensed from
Brush Technology and Ben Hoyt - Copyright (c) 2009, Brush Technology and Copyright (c) 2009, Ben Hoyt.
Complete license and copyright information can be found at
https://github.com/benhoyt/inih/commit/74d2ca064fb293bc60a77b0bd068075b293cf175.
*************************************************************************************************************/

#include <inttypes.h>
#include <linux/ptp_clock.h>
#include <sys/mman.h>
#include <sys/ioctl.h>

#include "avb_gptp.h"

#include "openavb_platform.h"
#include "openavb_time_osal.h"
#include "openavb_trace.h"

#define	AVB_LOG_COMPONENT	"osalTime"
#include "openavb_pub.h"
#include "openavb_log.h"

//#include "openavb_time_util_osal.h"

static pthread_mutex_t gOSALTimeInitMutex = PTHREAD_MUTEX_INITIALIZER;
#define LOCK()  	pthread_mutex_lock(&gOSALTimeInitMutex)
#define UNLOCK()	pthread_mutex_unlock(&gOSALTimeInitMutex)

static bool bInitialized = FALSE;
static int gPtpShmFd = -1;
static char *gPtpMmap = NULL;
gPtpTimeData gPtpTD;

static bool x_timeInit(void) {
	AVB_TRACE_ENTRY(AVB_TRACE_TIME);

	if (gptpinit(&gPtpShmFd, &gPtpMmap) < 0) {
		AVB_LOG_ERROR("GPTP init failed");
		AVB_TRACE_EXIT(AVB_TRACE_TIME);
		return FALSE;
	}

	if (gptpgetdata(gPtpMmap, &gPtpTD) < 0) {
		AVB_LOG_ERROR("GPTP data fetch failed");
		AVB_TRACE_EXIT(AVB_TRACE_TIME);
		return FALSE;
	}

	AVB_LOGF_INFO("local_time = %" PRIu64, gPtpTD.local_time);
	AVB_LOGF_INFO("ml_phoffset = %" PRId64 ", ls_phoffset = %" PRId64, gPtpTD.ml_phoffset, gPtpTD.ls_phoffset);
	AVB_LOGF_INFO("ml_freqffset = %Lf, ls_freqoffset = %Lf", gPtpTD.ml_freqoffset, gPtpTD.ls_freqoffset);

	AVB_TRACE_EXIT(AVB_TRACE_TIME);
	return TRUE;
}

static bool x_getPTPTime(U64 *timeNsec) {
	AVB_TRACE_ENTRY(AVB_TRACE_TIME);

	if (gptpgetdata(gPtpMmap, &gPtpTD) < 0) {
		AVB_LOG_ERROR("GPTP data fetch failed");
		AVB_TRACE_EXIT(AVB_TRACE_TIME);
		return FALSE;
	}

	uint64_t now_local;
	uint64_t update_8021as;
	int64_t delta_8021as;
	int64_t delta_local;

	if (gptplocaltime(&gPtpTD, &now_local)) {
		update_8021as = gPtpTD.local_time - gPtpTD.ml_phoffset;
		delta_local = now_local - gPtpTD.local_time;
		delta_8021as = gPtpTD.ml_freqoffset * delta_local;
		*timeNsec = update_8021as + delta_8021as;

		AVB_TRACE_EXIT(AVB_TRACE_TIME);
		return TRUE;
	}

	AVB_TRACE_EXIT(AVB_TRACE_TIME);
	return FALSE;
}

bool osalAVBTimeInit(void) {
	AVB_TRACE_ENTRY(AVB_TRACE_TIME);

	LOCK();
	if (!bInitialized) {
		if (x_timeInit())
			bInitialized = TRUE;
	}
	UNLOCK();

	AVB_TRACE_EXIT(AVB_TRACE_TIME);
	return TRUE;
}

bool osalAVBTimeClose(void) {
	AVB_TRACE_ENTRY(AVB_TRACE_TIME);

	gptpdeinit(&gPtpShmFd, &gPtpMmap);

	AVB_TRACE_EXIT(AVB_TRACE_TIME);
	return TRUE;
}

bool osalClockGettime(openavb_clockId_t openavbClockId, struct timespec *getTime) {
	AVB_TRACE_ENTRY(AVB_TRACE_TIME);

	if (openavbClockId < OPENAVB_CLOCK_WALLTIME) {
		clockid_t clockId = CLOCK_MONOTONIC;
		switch (openavbClockId) {
		case OPENAVB_CLOCK_REALTIME:
			clockId = CLOCK_REALTIME;
			break;
		case OPENAVB_CLOCK_MONOTONIC:
			clockId = CLOCK_MONOTONIC;
			break;
		case OPENAVB_TIMER_CLOCK:
			clockId = CLOCK_MONOTONIC;
			break;
		case OPENAVB_CLOCK_WALLTIME:
			break;
		}
		if (!clock_gettime(clockId, getTime)) return TRUE;
	}
	else if (openavbClockId == OPENAVB_CLOCK_WALLTIME) {
		U64 timeNsec;
		if (!x_getPTPTime(&timeNsec)) {
			AVB_TRACE_EXIT(AVB_TRACE_TIME);
			return FALSE;
		}
		getTime->tv_sec = timeNsec / NANOSECONDS_PER_SECOND;
		getTime->tv_nsec = timeNsec % NANOSECONDS_PER_SECOND;
		AVB_TRACE_EXIT(AVB_TRACE_TIME);
		return TRUE;
	}
	AVB_TRACE_EXIT(AVB_TRACE_TIME);
	return FALSE;
}

bool osalClockGettime64(openavb_clockId_t openavbClockId, U64 *timeNsec) {
	if (openavbClockId < OPENAVB_CLOCK_WALLTIME) {
		clockid_t clockId = CLOCK_MONOTONIC;
		switch (openavbClockId) {
		case OPENAVB_CLOCK_REALTIME:
			clockId = CLOCK_REALTIME;
			break;
		case OPENAVB_CLOCK_MONOTONIC:
			clockId = CLOCK_MONOTONIC;
			break;
		case OPENAVB_TIMER_CLOCK:
			clockId = CLOCK_MONOTONIC;
			break;
		case OPENAVB_CLOCK_WALLTIME:
			break;
		}
		struct timespec getTime;
		if (!clock_gettime(clockId, &getTime)) {
			*timeNsec = ((U64)getTime.tv_sec * (U64)NANOSECONDS_PER_SECOND) + (U64)getTime.tv_nsec;
			AVB_TRACE_EXIT(AVB_TRACE_TIME);
			return TRUE;
		}
	}
	else if (openavbClockId == OPENAVB_CLOCK_WALLTIME) {
		AVB_TRACE_EXIT(AVB_TRACE_TIME);
		return x_getPTPTime(timeNsec);
	}
	AVB_TRACE_EXIT(AVB_TRACE_TIME);
	return FALSE;
}