0674427e3978471c5c344a631fd1b774dba17d3a
[profile/ivi/automotive-message-broker.git] / plugins / chrony / chrony.cpp
1 /*
2     Copyright (C) 2012  Intel Corporation
3
4     This library is free software; you can redistribute it and/or
5     modify it under the terms of the GNU Lesser General Public
6     License as published by the Free Software Foundation; either
7     version 2.1 of the License, or (at your option) any later version.
8
9     This library is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12     Lesser General Public License for more details.
13
14     You should have received a copy of the GNU Lesser General Public
15     License along with this library; if not, write to the Free Software
16     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19
20 #include "chrony.h"
21 #include "abstractroutingengine.h"
22 #include "debugout.h"
23 #include "listplusplus.h"
24
25 #include <glib.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <sys/un.h>
29 #include <sys/time.h>
30 #include <math.h>
31
32 #define GPSTIME "GpsTime"
33
34 extern "C" void create(AbstractRoutingEngine* routingEngine, map<string, string> config)
35 {
36         new ChronySink(routingEngine, config);
37 }
38
39 ChronySink::ChronySink(AbstractRoutingEngine* engine, map<string, string> config): AbstractSink(engine, config)
40 {
41         routingEngine->subscribeToProperty(GPSTIME, this);
42         supportedChanged(engine->supported());
43 }
44
45 PropertyList ChronySink::subscriptions()
46 {
47 }
48
49 void ChronySink::supportedChanged(const PropertyList & supportedProperties)
50 {
51         DebugOut()<<"Support changed!"<<endl;
52 }
53
54 void ChronySink::propertyChanged(AbstractPropertyType *value)
55 {
56         int sockfd;
57         struct sockaddr_un s;
58         struct chrony_sock_sample chronydata;
59
60         sockfd = socket(AF_UNIX, SOCK_DGRAM, 0);
61         if (sockfd < 0) return;
62
63         s.sun_family = AF_UNIX;
64         strcpy(s.sun_path, CHRONYD_SOCKET );
65
66         if(connect(sockfd, (struct sockaddr *)&s, sizeof(s)) == -1) 
67         {
68                 return;
69         }
70
71         gettimeofday(&(chronydata.tv), NULL);
72         chronydata.offset  = (value->value<double>() - chronydata.tv.tv_sec) - (chronydata.tv.tv_usec / 1000000.0);
73         chronydata.offset -= (amb::currentTime()-value->timestamp);
74         chronydata.pulse = 0;
75         chronydata.leap = 0;
76         chronydata.magic = 0x534f434b;
77         send(sockfd,&chronydata,sizeof(chronydata),0);
78
79         close(sockfd);
80 }
81
82 const string ChronySink::uuid()
83 {
84         return "35324592-db72-11e4-b432-0022684a4a24";
85 }