Author: wmb Date: 2007-11-08 00:59:05 +0100 (Thu, 08 Nov 2007) New Revision: 723
Added: ofw/inet/sntp.fth Modified: cpu/x86/pc/olpc/fw.bth Log: Added SNTP support and the new OLPC command "ntp-set-clock".
Modified: cpu/x86/pc/olpc/fw.bth =================================================================== --- cpu/x86/pc/olpc/fw.bth 2007-11-07 23:57:36 UTC (rev 722) +++ cpu/x86/pc/olpc/fw.bth 2007-11-07 23:59:05 UTC (rev 723) @@ -138,12 +138,14 @@ [then]
[ifdef] linux-support +\needs unix-seconds> fload ${BP}/ofw/fs/unixtime.fth \ Unix time calculation support-package: ext2-file-system fload ${BP}/ofw/fs/ext2fs/ext2fs.fth \ Linux file system end-support-package [then]
[ifdef] jffs2-support +\needs unix-seconds> fload ${BP}/ofw/fs/unixtime.fth \ Unix time calculation support-package: jffs2-file-system fload ${BP}/ofw/fs/jffs2/jffs2.fth \ Journaling flash file system 2 end-support-package @@ -419,6 +421,23 @@ ; ' olpc-ssids to default-ssids
+fload ${BP}/ofw/inet/sntp.fth +: olpc-ntp-servers ( -- ) + " time 172.18.0.1 0.pool.ntp.org 1.pool.ntp.org 2.pool.ntp.org" +; +' olpc-ntp-servers to ntp-servers +: ntp-time&date ( -- s m h d m y ) + ntp-timestamp abort" Can't contact NTP server" + ntp>time&date +; +: .clock ( -- ) + time&date .date space .time ." UTC" cr +; +: ntp-set-clock ( -- ) + ntp-time&date " set-time" clock-node @ $call-method + .clock +; + [ifdef] use-ppp fload ${BP}/ofw/ppp/loadppp.fth [then]
Added: ofw/inet/sntp.fth =================================================================== --- ofw/inet/sntp.fth (rev 0) +++ ofw/inet/sntp.fth 2007-11-07 23:59:05 UTC (rev 723) @@ -0,0 +1,101 @@ +purpose: Get data and time from an NTP server using SNTP +\ See license at end of file + +0 value ip-ih +: $call-ip ip-ih $call-method ; + +d# 123 constant ntp-port# +h# 30 constant /sntp-request + +: send-sntp-request ( -- ) + /sntp-request " allocate-udp" $call-ip >r + r@ /sntp-request erase + 3 3 lshift \ SNTP version 3 + 3 or \ Client request + r@ c! + r@ /sntp-request ntp-port# ntp-port# " send-udp-packet" $call-ip + r> /sntp-request " free-udp" $call-ip +; +: receive-sntp-reply ( -- true | d.timestamp false ) + ntp-port# " receive-udp-packet" $call-ip if ( ) \ Timeout + true exit + then ( adr len src-port# ) + drop ( adr len ) + h# 30 < if ( adr len ) + ." Bad NTP reply length" cr + drop true exit + then ( adr ) +\ dup c@ h# c0 and if ( adr ) \ Check LI field +\ ." NTP server not synchronized" cr +\ drop true exit +\ then ( adr ) + dup h# 2c + be-l@ ( adr fraction ) + dup 0= if ( adr fraction ) + ." NTP server not synchronized" cr + 2drop true exit + then ( adr fraction ) + swap h# 28 + be-l@ ( fraction seconds ) + false ( d.timestamp false ) +; +: try-sntp ( hostname$ -- true | d.timestamp false ) + " ip" open-dev to ip-ih + ip-ih 0= if + ." Networking not available" cr + true exit + then + + d# 5,000 " set-timeout" $call-ip + " $set-host" $call-ip + send-sntp-request + receive-sntp-reply + ip-ih close-dev +; + +defer ntp-servers +: default-ntp-servers " 0.pool.ntp.org 1.pool.ntp.org 2.pool.ntp.org" ; +' default-ntp-servers to ntp-servers + +: ntp-timestamp ( -- true | d.timestamp false ) + ntp-servers begin dup while ( rem$ ) + bl left-parse-string ( rem$ server$ ) + ['] try-sntp catch if ( rem$ x x ) + 2drop ( rem$ ) + else ( rem$ [ true | d.timestamp false ] ) + 0= if ( rem$ d.timestamp ) + 2nip false exit + then ( rem$ ) + then ( rem$ ) + repeat ( rem$ ) + 2drop true +; + +d# 2,208,988,800 constant unix-epoch + +: ntp>time&date ( d.timestamp -- s m h m d y ) + nip unix-epoch - unix-seconds> +; + + +\ LICENSE_BEGIN +\ Copyright (c) 2007 FirmWorks +\ +\ Permission is hereby granted, free of charge, to any person obtaining +\ a copy of this software and associated documentation files (the +\ "Software"), to deal in the Software without restriction, including +\ without limitation the rights to use, copy, modify, merge, publish, +\ distribute, sublicense, and/or sell copies of the Software, and to +\ permit persons to whom the Software is furnished to do so, subject to +\ the following conditions: +\ +\ The above copyright notice and this permission notice shall be +\ included in all copies or substantial portions of the Software. +\ +\ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +\ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +\ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +\ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +\ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +\ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +\ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +\ +\ LICENSE_END