blob: 1da7c137619816dcc69677e26d07a7900047aba8 [file] [log] [blame]
Harald Welteccea8dd2016-12-24 10:27:55 +01001/*
2 * (C) 2013-2016 by Harald Welte <laforge@gnumonks.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
Harald Weltef5d6fee2016-01-13 22:29:10 +010019#include <osmocom/core/msgb.h>
Harald Welte84ec50f2016-12-24 10:16:00 +010020#include "protocol/protocol.h"
21#include "protocol/diagcmd.h"
Harald Weltef5d6fee2016-01-13 22:29:10 +010022
23int diag_push_subsys_hdr(struct msgb *msg, uint8_t subsys, uint8_t code)
24{
25 struct diagpkt_subsys_hdr *ssh;
26 ssh = (struct diagpkt_subsys_hdr *) msgb_push(msg, sizeof(*ssh));
27 ssh->command = DIAG_SUBSYS_CMD_F;
28 ssh->subsys_id = subsys;
29 ssh->subsys_cmd_code = code;
30
31 return 0;
32}
Harald Welte2c363752016-12-24 00:19:55 +010033
34#include <sys/time.h>
35#include <osmocom/gsm/gsm_utils.h>
36
37uint32_t diag_ts_to_fn(uint64_t ts)
38{
39 return (ts/204800)%GSM_MAX_FN;
40}
41
Harald Welte035e1462016-12-31 20:44:42 +010042/* DIAG timestamps consist of two parts:
43 * upper 48 bits: time since Jan 6 1980 00:00:00 (GPS) in 1.25ms units
44 * lower 16 bits: time since last 1.25ms tick in 1/32 chip units */
Harald Welte2c363752016-12-24 00:19:55 +010045uint32_t diag_ts_to_epoch(uint64_t qd_time)
46{
47 double qd_ts;
48
Harald Welte035e1462016-12-31 20:44:42 +010049 qd_ts = osmo_load64le(&qd_time) >> 16;
50 qd_ts *= 1.25;
51 qd_ts /= 1000.0;
Harald Welte2c363752016-12-24 00:19:55 +010052
53 /* Sanity check on timestamp (year > 2011) */
54 if (qd_ts < 1000000000) {
55 /* Use current time */
56 int rv = -1;
57 struct timeval tv;
58
59 rv = gettimeofday(&tv, NULL);
60 if (0 == rv)
61 return tv.tv_sec;
62 } else {
Harald Welte035e1462016-12-31 20:44:42 +010063 /* Adjust timestamp from GPS to UNIX, i.e. number of
64 * seconds between 1970 and Jan 6 1980 */
Harald Welte2c363752016-12-24 00:19:55 +010065 qd_ts += 315964800.0;
66 }
67
68 return qd_ts;
69}