amiga-ntimed/time_amiga.c

352 lines
8.3 KiB
C

/*
* Copyright (c) 2015 Carsten Larsen
* 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 AUTHOR ``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 AUTHOR 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.
*
*/
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "ntimed.h"
#include "ntimed_platform.h"
#include <stdio.h>
#include <time.h>
#include <exec/io.h>
#include <exec/types.h>
#include <exec/memory.h>
#include <devices/timer.h>
#include <proto/timer.h>
#include <proto/battclock.h>
#include <clib/alib_protos.h>
#include <clib/exec_protos.h>
#include <clib/locale_protos.h>
#include <clib/utility_protos.h>
#include <clib/battclock_protos.h>
#ifdef Debug
#undef Debug
#endif
// 2922 is the number of days between 1.1.1970 and 1.1.1978 (2 leap years and 6 normal)
#define AMIGAOFFSET 2922 * 24 * 60 * 60
int gmtoffset = 0;
int create_timer();
int delete_timer();
void adjust_timeval(struct timeval *tv, double offset);
struct Library *BattClockBase;
struct Library *UtilityBase = NULL;
struct Library *LocaleBase = NULL;
struct Library *SocketBase = NULL;
struct Locale *locale = NULL;
struct timerequest *request = NULL;
struct timeval sync_time;
int validtime = 0;
int limited_sync = 0;
void amiga_open_libs()
{
if(!(LocaleBase = (struct Library *)OpenLibrary(LOCALELIB_NAME, LOCALELIB_REV))) {
Put(NULL, OCX_DIAG, "Cannot open locale library.\n");
exit(10);
}
locale = OpenLocale(NULL);
gmtoffset = (int)locale->loc_GMTOffset * 60;
// TODO: use tzlib.a
// get tm header and invoke:
// time_t now = time(NULL);
// struct tm lcl = *localtime(&now)
// And use gmt filed from tm header if != 0
if(!(UtilityBase = (struct Library *)OpenLibrary(UTILLIB_NAME, UTILLIB_REV))) {
Put(NULL, OCX_DIAG, "Cannot open utility library.\n");
exit(10);
}
BattClockBase = OpenResource(BATTCLOCK_NAME);
#ifdef AROS
if(!(SocketBase = OpenLibrary(BSDLIB_NAME, BSDLIB_REV))) {
Put(NULL, OCX_DIAG, "No TCP/IP Stack running.\n");
exit(10);
}
if(SocketBaseTags(SBTM_SETVAL(SBTC_ERRNOPTR(sizeof(errno))), (IPTR)&errno,
SBTM_SETVAL(SBTC_HERRNOLONGPTR), (IPTR)&h_errno, TAG_DONE)) {
Put(NULL, OCX_DIAG, "Error initializing bsdsocket.\n");
exit(10);
}
#endif
if (create_timer() != 0) {
Put(NULL, OCX_DIAG, "Cannot open timer device.\n");
exit(10);
}
}
void amiga_close_libs()
{
if (request != NULL) {
delete_timer();
}
if (locale != NULL) {
CloseLocale(locale);
locale = NULL;
}
if (LocaleBase != NULL) {
CloseLibrary(LocaleBase);
LocaleBase = NULL;
}
if (UtilityBase != NULL) {
CloseLibrary(UtilityBase);
UtilityBase = NULL;
}
#ifdef AROS
if (SocketBase != NULL) {
CloseLibrary(SocketBase);
SocketBase = NULL;
}
#endif
}
int create_timer()
{
LONG error;
struct MsgPort *port = CreatePort(0, 0);
if (port == NULL)
return 1;
request = (struct timerequest*)CreateExtIO(port, sizeof(struct timerequest));
if (request == NULL)
{
DeletePort(port);
return 1;
}
error = OpenDevice(TIMER_NAME, UNIT_MICROHZ, (struct IORequest*)request, 0L);
if (error != 0)
{
delete_timer(request);
return 1;
}
return 0;
}
int delete_timer()
{
struct MsgPort *port;
if (request == NULL)
return 1;
port = request->tr_node.io_Message.mn_ReplyPort;
if (port != 0)
DeletePort(port);
CloseDevice((struct IORequest*)request);
DeleteExtIO((struct IORequest*)request);
return 0;
}
/**********************************************************************/
int gettimeofday(struct timeval *tv, struct timezone *tz)
{
request->tr_node.io_Command = TR_GETSYSTIME;
DoIO((struct IORequest*)request);
tv->tv_secs = (int)request->tr_time.tv_secs - gmtoffset + AMIGAOFFSET;
tv->tv_micro = request->tr_time.tv_micro;
return 0;
}
int settimeofday(const struct timeval *tv, const struct timezone *tz)
{
request->tr_node.io_Command = TR_SETSYSTIME;
request->tr_time.tv_secs = (int)tv->tv_secs + gmtoffset - AMIGAOFFSET;
request->tr_time.tv_micro = tv->tv_micro;
DoIO((struct IORequest*)request);
validtime = 1;
return 0;
}
/**********************************************************************/
void amiga_save_time(void)
{
struct timeval tv;
if(!validtime)
return;
ResetBattClock();
gettimeofday(&tv, NULL);
WriteBattClock(tv.tv_secs + gmtoffset - AMIGAOFFSET);
}
/**********************************************************************/
void amiga_sync_time(int seconds)
{
limited_sync = 1;
sync_time.tv_secs = seconds;
sync_time.tv_micro = 0;
}
/**********************************************************************/
int amiga_sleep_time(double dur)
{
ULONG sigs, timersig;
struct timeval tv;
tv.tv_secs = 0;
tv.tv_micro = 0;
if (limited_sync) {
adjust_timeval(&sync_time, -dur);
if ((LONG)sync_time.tv_secs < 0L) {
exit(1);
}
}
adjust_timeval(&tv, dur);
request->tr_node.io_Command = TR_ADDREQUEST;
request->tr_time = tv;
timersig = (1L << request->tr_node.io_Message.mn_ReplyPort->mp_SigBit);
SendIO((struct IORequest*)request);
sigs = Wait(SIGBREAKF_CTRL_C | timersig);
if (sigs & SIGBREAKF_CTRL_C) {
AbortIO((struct IORequest*)request);
WaitIO((struct IORequest*)request);
exit(1);
}
return 0;
}
/**********************************************************************/
void adjust_timeval(struct timeval *tv, double offset)
{
double d1, d2;
int secs, micro;
d1 = floor(offset);
d2 = offset - d1;
secs = tv->tv_secs + (int)d1;
micro = tv->tv_micro + (int)floor(d2 * 1e6);
if (micro < 0) {
secs -= 1;
micro += 1000000;
} else if (micro >= 1000000) {
secs += 1;
micro -= 1000000;
}
tv->tv_secs = secs;
tv->tv_micro = micro;
}
/**********************************************************************/
#ifndef HAVE_POLL
int poll(struct pollfd *pfds, nfds_t nfds, int timeout)
{
unsigned int i;
int maxfd = -1, ret;
fd_set rset,wset,xset;
struct timeval timeout_tv, *tvp = NULL;
if(pfds == NULL || nfds < 1)
{
return amiga_sleep_time(timeout / 1000);
}
if (timeout >= 0)
{
timeout_tv.tv_sec = (timeout / 1000);
timeout_tv.tv_usec = (timeout % 1000) * 1000;
tvp = &timeout_tv;
}
FD_ZERO(&rset);
FD_ZERO(&wset);
FD_ZERO(&xset);
for (i = 0; i < nfds; i++)
{
pfds[i].revents = 0;
if (pfds[i].events == 0)
continue;
if (pfds[i].fd > maxfd)
maxfd = pfds[i].fd;
if (pfds[i].events & POLLIN)
FD_SET(pfds[i].fd, &rset);
}
#ifdef HAVE_WAITSELECT
ret = WaitSelect(maxfd + 1, &rset, &wset, &xset, tvp, NULL);
#else
ret = select(maxfd + 1, &rset, &wset, &xset, tvp);
#endif
if(ret == -1)
return ret;
for (i = 0; i < nfds; i++)
{
if (pfds[i].events == 0)
continue;
if (FD_ISSET(pfds[i].fd, &rset))
pfds[i].revents |= POLLIN;
}
return ret;
}
#endif