Harald Welte | ccea8dd | 2016-12-24 10:27:55 +0100 | [diff] [blame] | 1 | /* |
| 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 Welte | f5d6fee | 2016-01-13 22:29:10 +0100 | [diff] [blame] | 19 | #include <osmocom/core/msgb.h> |
Harald Welte | 84ec50f | 2016-12-24 10:16:00 +0100 | [diff] [blame] | 20 | #include "protocol/protocol.h" |
| 21 | #include "protocol/diagcmd.h" |
Harald Welte | f5d6fee | 2016-01-13 22:29:10 +0100 | [diff] [blame] | 22 | |
| 23 | int 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 Welte | 2c36375 | 2016-12-24 00:19:55 +0100 | [diff] [blame] | 33 | |
| 34 | #include <sys/time.h> |
| 35 | #include <osmocom/gsm/gsm_utils.h> |
| 36 | |
| 37 | uint32_t diag_ts_to_fn(uint64_t ts) |
| 38 | { |
| 39 | return (ts/204800)%GSM_MAX_FN; |
| 40 | } |
| 41 | |
Harald Welte | 035e146 | 2016-12-31 20:44:42 +0100 | [diff] [blame] | 42 | /* 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 Welte | 2c36375 | 2016-12-24 00:19:55 +0100 | [diff] [blame] | 45 | uint32_t diag_ts_to_epoch(uint64_t qd_time) |
| 46 | { |
| 47 | double qd_ts; |
| 48 | |
Harald Welte | 035e146 | 2016-12-31 20:44:42 +0100 | [diff] [blame] | 49 | qd_ts = osmo_load64le(&qd_time) >> 16; |
| 50 | qd_ts *= 1.25; |
| 51 | qd_ts /= 1000.0; |
Harald Welte | 2c36375 | 2016-12-24 00:19:55 +0100 | [diff] [blame] | 52 | |
| 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 Welte | 035e146 | 2016-12-31 20:44:42 +0100 | [diff] [blame] | 63 | /* Adjust timestamp from GPS to UNIX, i.e. number of |
| 64 | * seconds between 1970 and Jan 6 1980 */ |
Harald Welte | 2c36375 | 2016-12-24 00:19:55 +0100 | [diff] [blame] | 65 | qd_ts += 315964800.0; |
| 66 | } |
| 67 | |
| 68 | return qd_ts; |
| 69 | } |