Blame view

RIOT/sys/net/application_layer/sntp/sntp.c 2.8 KB
fb11e647   vrobic   reseau statique a...
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
  /*
   * Copyright (C) 2016 Luminița Lăzărescu <cluminita.lazarescu@gmail.com>
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @{
   *
   * @file
   * @brief       SNTP implementation
   *
   * @author      Luminița Lăzărescu <cluminita.lazarescu@gmail.com>
   * @author      Martine Lenders <m.lenders@fu-berlin.de>
   *
   * @}
   */
  
  #include <string.h>
  #include "net/sntp.h"
  #include "net/ntp_packet.h"
  #include "net/sock/udp.h"
  #include "xtimer.h"
  #include "mutex.h"
  #include "byteorder.h"
  
929398b3   vrobic   test offset horlo...
29
  #define ENABLE_DEBUG    (1)
fb11e647   vrobic   reseau statique a...
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  #include "debug.h"
  
  static sock_udp_t _sntp_sock;
  static int64_t _sntp_offset = 0;
  static mutex_t _sntp_mutex = MUTEX_INIT;
  static ntp_packet_t _sntp_packet;
  
  int sntp_sync(sock_udp_ep_t *server, uint32_t timeout)
  {
      int result;
  
      mutex_lock(&_sntp_mutex);
      if ((result = sock_udp_create(&_sntp_sock,
                                    NULL,
                                    server,
                                    0)) < 0) {
          DEBUG("Error creating UDP sock\n");
          mutex_unlock(&_sntp_mutex);
          return result;
      }
      memset(&_sntp_packet, 0, sizeof(_sntp_packet));
      ntp_packet_set_vn(&_sntp_packet);
      ntp_packet_set_mode(&_sntp_packet, NTP_MODE_CLIENT);
929398b3   vrobic   test offset horlo...
53
      _sntp_packet.transmit.seconds=byteorder_htonl( xtimer_now_usec());
fb11e647   vrobic   reseau statique a...
54
55
56
57
58
59
60
61
62
63
64
65
      if ((result = (int)sock_udp_send(&_sntp_sock,
                                       &_sntp_packet,
                                       sizeof(_sntp_packet),
                                       NULL)) < 0) {
          DEBUG("Error sending message\n");
          sock_udp_close(&_sntp_sock);
          mutex_unlock(&_sntp_mutex);
          return result;
      }
      if ((result = (int)sock_udp_recv(&_sntp_sock,
                                       &_sntp_packet,
                                       sizeof(_sntp_packet),
929398b3   vrobic   test offset horlo...
66
                                       SOCK_NO_TIMEOUT,
fb11e647   vrobic   reseau statique a...
67
68
69
70
                                       NULL)) < 0) {
          DEBUG("Error receiving message\n");
          sock_udp_close(&_sntp_sock);
          mutex_unlock(&_sntp_mutex);
fb11e647   vrobic   reseau statique a...
71
72
          return result;
      }
929398b3   vrobic   test offset horlo...
73
      //xtimer_ticks64_t now = xtimer_now64();
fb11e647   vrobic   reseau statique a...
74
      sock_udp_close(&_sntp_sock);
929398b3   vrobic   test offset horlo...
75
76
      _sntp_offset= (byteorder_ntohl(_sntp_packet.receive.seconds)+ byteorder_ntohl(_sntp_packet.transmit.seconds))/2 - (byteorder_ntohl(_sntp_packet.origin.seconds) + xtimer_now_usec())/2;
     /* _sntp_offset = (byteorder_ntohl(_sntp_packet.transmit.seconds) * SEC_IN_USEC) +
fb11e647   vrobic   reseau statique a...
77
                     ((byteorder_ntohl(_sntp_packet.transmit.fraction) * 232)
929398b3   vrobic   test offset horlo...
78
                     / 1000000) - xtimer_now_usec();*/
fb11e647   vrobic   reseau statique a...
79
80
81
82
83
84
85
86
87
88
89
90
91
      mutex_unlock(&_sntp_mutex);
      return 0;
  }
  
  int64_t sntp_get_offset(void)
  {
      int64_t result;
  
      mutex_lock(&_sntp_mutex);
      result = _sntp_offset;
      mutex_unlock(&_sntp_mutex);
      return result;
  }