File: udp_demo.m

package info (click to toggle)
octave-instrument-control 0.3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 860 kB
  • sloc: cpp: 5,846; makefile: 436; ansic: 18; sh: 4
file content (87 lines) | stat: -rw-r--r-- 2,243 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
## Copyright (C) 2016 John Donoghue
## 
## This program is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
## 
## This program is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see <http://www.gnu.org/licenses/>.

## -*- texinfo -*- 
## @deftypefn {Function File} {@var{result} =} udp_demo ()
## Run test SNTP demonstration for udp class
##
## @seealso{udp}
## @end deftypefn

## Author: john donoghue <john.donoghue@ieee.org>
## Created: 2016-11-23

function result = udp_demo ()

  result = false;
  server = 'pool.ntp.org';
  
  fprintf ('getting time from %s\n', server);
  
  # query time using SNTP
  s = udp (server, 123);

  # send request packet
  data = [uint8(0x1b) uint8(zeros (1,47))];
  res = udp_write (s, data);

  # recieve packet
  data = udp_read (s,48, 4000);

  # should have 48 bytes reply
  if length (data) == 48
    
    # convert uint8s to integer32
    dataL = typecast (data, 'uint32');
    
    # convert to little endian if we are on a little endian machine
    if typecast (uint8([1 0]), 'uint16') == 1
        dataL = swapbytes (dataL);
    endif

    # network time
    ntimeval = dataL(11) - 2208988800;
    # get system time 
    ltimeval = uint32 (time());
    
    fprintf ('network time=%lu local time=%lu\n', ...
      ntimeval, ltimeval);
    
    result = true;
  endif

  fclose (s);
endfunction

%!test
% assert(udp_demo)

%!test
% s = udp('127.0.0.1', 80);
% assert(!isempty(s));
% assert(get(s,'name'), 'UDP-127.0.0.1');
% assert(get(s,'remoteport'), 80);
% assert(get(s,'remotehost','127.0.0.1');
% assert(get(s,'localport'))
% set(s,'name', 'test');
% assert(get(s,'name'), 'test');
% udp_close(s);

%!test
% s = udp('127.0.0.1', 80);
% assert(get(s,'status'), 'open');
% fclose(s);
% assert(get(s,'status'), 'closed');