XINU
Functions
getutime.c File Reference
#include <xinu.h>
#include <stdio.h>
Include dependency graph for getutime.c:

Go to the source code of this file.

Functions

status getutime (uint32 *timvar)
 

Function Documentation

◆ getutime()

status getutime ( uint32 timvar)

Definition at line 10 of file getutime.c.

References clktime, Date, dot2ip(), dateinfo::dt_boot, dateinfo::dt_bootvalid, fprintf(), getlocalip(), memset(), NetData, ntim2xtim, ntohl, network::ntpserver, OK, stderr, SYSERR, TIMELPORT, TIMEOUT, TIMERPORT, TIMESERVER, TIMETIMEOUT, TRUE, udp_recv(), udp_register(), udp_release(), and udp_send().

Referenced by gettime().

13 {
14  uint32 now; /* Current time in xinu format */
15  int32 retval; /* Return value from call */
16  uid32 slot; /* Slot in UDP table */
17  struct ntp { /* Format of an NTP message */
18  byte livn; /* LI:2 VN:3 and mode:3 fields */
19  byte strat; /* Stratum */
20  byte poll; /* Poll interval */
21  byte precision; /* Precision */
22  uint32 rootdelay; /* Root delay */
23  uint32 rootdisp; /* Root dispersion */
24  uint32 refid; /* Reference identifier */
25  uint32 reftimestamp[2];/* Reference timestamp */
26  uint32 oritimestamp[2];/* Originate timestamp */
27  uint32 rectimestamp[2];/* Receive timestamp */
28  uint32 trntimestamp[2];/* Transmit timestamp */
29  } ntpmsg;
30 
31  if (Date.dt_bootvalid) { /* Return time from local info */
32  *timvar = Date.dt_boot + clktime;
33  return OK;
34  }
35 
36  /* Verify that we have obtained an IP address */
37 
38  if (getlocalip() == SYSERR) {
39  return SYSERR;
40  }
41 
42  /* If the DHCP response did not contain an NTP server address */
43  /* use the default server */
44 
45  if (NetData.ntpserver == 0) {
47  return SYSERR;
48  }
49  }
50 
51  /* Contact the time server to get the date and time */
52 
54  if (slot == SYSERR) {
55  fprintf(stderr,"getutime: cannot register a udp port %d\n",
56  TIMERPORT);
57  return SYSERR;
58  }
59 
60  /* Send a request message to the NTP server */
61 
62  memset((char *)&ntpmsg, 0x00, sizeof(ntpmsg));
63  ntpmsg.livn = 0x1b; /* Client request, protocol version 3 */
64  retval = udp_send(slot, (char *)&ntpmsg, sizeof(ntpmsg));
65  if (retval == SYSERR) {
66  fprintf(stderr,"getutime: cannot send to the server\n");
67  udp_release(slot);
68  return SYSERR;
69  }
70 
71  /* Read the response from the NTP server */
72 
73  retval = udp_recv(slot, (char *) &ntpmsg, sizeof(ntpmsg),
74  TIMETIMEOUT);
75  if ( (retval == SYSERR) || (retval == TIMEOUT) ) {
76  udp_release(slot);
77  return SYSERR;
78  }
79  udp_release(slot);
80 
81  /* Extract the seconds since Jan 1900 and convert */
82 
83  now = ntim2xtim( ntohl(ntpmsg.trntimestamp[0]) );
84  Date.dt_boot = now - clktime;
86  *timvar = now;
87  return OK;
88 }
int32 udp_recv(uid32, char *, int32, uint32)
Definition: udp.c:146
status udp_send(uid32, char *, int32)
Definition: udp.c:316
unsigned char byte
符号なし8ビット値(unsigned char)
Definition: kernel.h:7
bool8 dt_bootvalid
Definition: date.h:30
struct network NetData
Definition: net.c:6
#define SYSERR
処理が失敗した場合
Definition: kernel.h:79
#define TIMESERVER
Definition: date.h:64
#define stderr
Definition: stdio.h:17
#define OK
処理が成功した場合
Definition: kernel.h:77
#define TIMEOUT
システムコールがタイムアウトした場合
Definition: kernel.h:83
#define TIMELPORT
Definition: date.h:61
#define ntim2xtim(x)
Definition: date.h:15
#define ntohl(x)
Definition: prototypes.h:623
#define TRUE
Boolean True(1)
Definition: kernel.h:65
int int32
符号あり32ビット整数(int)
Definition: kernel.h:11
struct dateinfo Date
Definition: ascdate.c:6
uint32 dt_boot
Definition: date.h:27
uid32 udp_register(uint32, uint16, uint16)
Definition: udp.c:85
void * memset(void *, const int, int32)
指定のByteブロックに対して、同じ値をNバイト分書き込む。
Definition: memset.c:13
#define TIMERPORT
Definition: date.h:60
status udp_release(uid32)
Definition: udp.c:502
uint32 getlocalip(void)
Definition: dhcp.c:142
int32 fprintf(int, char *,...)
Definition: fprintf.c:14
uint32 clktime
起動してからの現在の時間[s]
Definition: clkinit.c:5
uint32 ntpserver
Definition: net.h:62
unsigned int uint32
符号なし32ビット整数(unsigned int)
Definition: kernel.h:15
#define TIMETIMEOUT
Definition: date.h:67
uint32 dot2ip(char *, uint32 *)
Definition: dot2ip.c:9
int32 uid32
UDPテーブルディスクリプタのID.
Definition: kernel.h:44
Here is the call graph for this function:
Here is the caller graph for this function: