Added chrony sink plugin
authorOlivier Delbeke <olivier.delbeke@awtce.be>
Wed, 8 Apr 2015 09:25:07 +0000 (11:25 +0200)
committerOlivier Delbeke <olivier.delbeke@gmail.com>
Wed, 15 Apr 2015 18:15:28 +0000 (20:15 +0200)
CMakeLists.txt
examples/CMakeLists.txt
examples/chrony.in.json [new file with mode: 0644]
plugins/CMakeLists.txt
plugins/chrony/CMakeLists.txt [new file with mode: 0644]
plugins/chrony/README [new file with mode: 0644]
plugins/chrony/chrony.cpp [new file with mode: 0644]
plugins/chrony/chrony.h [new file with mode: 0644]

index e46c364..3f92b39 100644 (file)
@@ -38,6 +38,7 @@ option(murphy_plugin "murphy policy framework plugin" OFF)
 option(test_plugin "Test Plugin" ON)
 option(bluemonkey_plugin "bluemonkey plugin" OFF)
 option(gpsnmea_plugin "gps NMEA location plugin" OFF)
+option(chrony_plugin "chrony plugin" OFF)
 option(openxc_plugin "OpenXC plugin" OFF)
 option(bluetooth_plugin "bluetooth plugin" OFF)
 option(cansim_plugin "Can simulator plugin" OFF)
index 5437a9f..0867233 100644 (file)
@@ -11,7 +11,8 @@ set(amb_examples ${CMAKE_CURRENT_BINARY_DIR}/configwheel
                                 ${CMAKE_CURRENT_BINARY_DIR}/websocketsource2
                                 ${CMAKE_CURRENT_BINARY_DIR}/testsourceconfig
                                 ${CMAKE_CURRENT_BINARY_DIR}/bluemonkey/bluemonkeyconfig
-                                ${CMAKE_CURRENT_BINARY_DIR}/bluetooth)
+                                ${CMAKE_CURRENT_BINARY_DIR}/bluetooth
+                                ${CMAKE_CURRENT_BINARY_DIR}/chrony)
 
 configure_file (${CMAKE_CURRENT_SOURCE_DIR}/configwheel.in.json ${CMAKE_CURRENT_BINARY_DIR}/configwheel @ONLY)
 configure_file (${CMAKE_CURRENT_SOURCE_DIR}/databasesource.in.json ${CMAKE_CURRENT_BINARY_DIR}/databasesource @ONLY)
@@ -31,6 +32,7 @@ configure_file (${CMAKE_CURRENT_SOURCE_DIR}/cangenconfig.in.json ${CMAKE_CURRENT
 configure_file (${CMAKE_CURRENT_SOURCE_DIR}/bluemonkey/bluemonkeyconfig.in.json ${CMAKE_CURRENT_BINARY_DIR}/bluemonkey/bluemonkeyconfig @ONLY)
 configure_file (${CMAKE_CURRENT_SOURCE_DIR}/bluetooth.in.json ${CMAKE_CURRENT_BINARY_DIR}/bluetooth @ONLY)
 configure_file (${CMAKE_CURRENT_SOURCE_DIR}/testplugins.d.in.json ${CMAKE_CURRENT_SOURCE_DIR}/testplugins.d.json @ONLY)
+configure_file (${CMAKE_CURRENT_SOURCE_DIR}/chrony.in.json ${CMAKE_CURRENT_BINARY_DIR}/chrony @ONLY)
 
 install (FILES ${amb_examples} DESTINATION /etc/ambd/examples)
 
diff --git a/examples/chrony.in.json b/examples/chrony.in.json
new file mode 100644 (file)
index 0000000..360ceec
--- /dev/null
@@ -0,0 +1,16 @@
+{
+       "sources" : [
+               {
+                       "name" : "gps nmea plugin",
+                       "path" : "@PLUGIN_INSTALL_PATH@/gpsnmea.so",
+                       "device" : "/dev/ttyUSB0",
+                       "baudrate" : "4800"
+               }
+       ],
+       "sinks": [
+               {
+                       "path" : "@PLUGIN_INSTALL_PATH@/chrony.so"
+               }
+       ]
+}
+
index ed744e0..6b119c8 100644 (file)
@@ -50,3 +50,4 @@ add_subdirectory(openxc)
 add_subdirectory(bluetooth)
 add_subdirectory(cansimplugin)
 add_subdirectory(cangenplugin)
+add_subdirectory(chrony)
diff --git a/plugins/chrony/CMakeLists.txt b/plugins/chrony/CMakeLists.txt
new file mode 100644 (file)
index 0000000..a66a196
--- /dev/null
@@ -0,0 +1,16 @@
+if(chrony_plugin)
+
+set(chrony_headers chrony.h)
+set(chrony_sources chrony.cpp )
+
+add_library(chrony MODULE ${chrony_sources})
+set_target_properties(chrony PROPERTIES PREFIX "")
+target_link_libraries(chrony amb -L${CMAKE_CURRENT_BINARY_DIR}/lib  amb-plugins-common -L${CMAKE_CURRENT_BINARY_DIR}/plugins/common ${link_libraries} ${gio_LIBRARIES})
+
+install(TARGETS chrony LIBRARY DESTINATION ${PLUGIN_INSTALL_PATH})
+
+configure_file(${CMAKE_CURRENT_SOURCE_DIR}/README ${CMAKE_CURRENT_BINARY_DIR}/chrony.README @ONLY)
+
+install (FILES ${CMAKE_CURRENT_BINARY_DIR}/chrony.README DESTINATION ${DOC_INSTALL_DIR}/plugins)
+
+endif(chrony_plugin)
diff --git a/plugins/chrony/README b/plugins/chrony/README
new file mode 100644 (file)
index 0000000..a9f28c2
--- /dev/null
@@ -0,0 +1,24 @@
+CHRONY plugin
+Version: @PROJECT_VERSION@
+
+This plugin will forward the GpsTime property to the CHRONYD daemon.
+
+To enable the CHRONY plugin, run cmake and enable the chrony_plugin option:
+
+cmake -Dchrony_plugin=On ..
+
+To use this plugin, add the following to the "sinks" array in /etc/ambd/config:
+
+{
+       "name" : "chrony",
+       "path" : "@PLUGIN_INSTALL_PATH@/chrony.so",
+}
+
+Configuration Key Definitions:
+
+"name"
+name of plugin.  This key is not used by the plugin at this moment.
+
+"path"
+path to plugin on the filesystem.
+
diff --git a/plugins/chrony/chrony.cpp b/plugins/chrony/chrony.cpp
new file mode 100644 (file)
index 0000000..0674427
--- /dev/null
@@ -0,0 +1,85 @@
+/*
+    Copyright (C) 2012  Intel Corporation
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Lesser General Public
+    License as published by the Free Software Foundation; either
+    version 2.1 of the License, or (at your option) any later version.
+
+    This library 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
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with this library; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+
+#include "chrony.h"
+#include "abstractroutingengine.h"
+#include "debugout.h"
+#include "listplusplus.h"
+
+#include <glib.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <sys/time.h>
+#include <math.h>
+
+#define GPSTIME "GpsTime"
+
+extern "C" void create(AbstractRoutingEngine* routingEngine, map<string, string> config)
+{
+       new ChronySink(routingEngine, config);
+}
+
+ChronySink::ChronySink(AbstractRoutingEngine* engine, map<string, string> config): AbstractSink(engine, config)
+{
+       routingEngine->subscribeToProperty(GPSTIME, this);
+       supportedChanged(engine->supported());
+}
+
+PropertyList ChronySink::subscriptions()
+{
+}
+
+void ChronySink::supportedChanged(const PropertyList & supportedProperties)
+{
+       DebugOut()<<"Support changed!"<<endl;
+}
+
+void ChronySink::propertyChanged(AbstractPropertyType *value)
+{
+       int sockfd;
+       struct sockaddr_un s;
+       struct chrony_sock_sample chronydata;
+
+       sockfd = socket(AF_UNIX, SOCK_DGRAM, 0);
+       if (sockfd < 0) return;
+
+       s.sun_family = AF_UNIX;
+       strcpy(s.sun_path, CHRONYD_SOCKET );
+
+       if(connect(sockfd, (struct sockaddr *)&s, sizeof(s)) == -1) 
+       {
+               return;
+       }
+
+       gettimeofday(&(chronydata.tv), NULL);
+       chronydata.offset  = (value->value<double>() - chronydata.tv.tv_sec) - (chronydata.tv.tv_usec / 1000000.0);
+       chronydata.offset -= (amb::currentTime()-value->timestamp);
+       chronydata.pulse = 0;
+       chronydata.leap = 0;
+       chronydata.magic = 0x534f434b;
+       send(sockfd,&chronydata,sizeof(chronydata),0);
+
+       close(sockfd);
+}
+
+const string ChronySink::uuid()
+{
+       return "35324592-db72-11e4-b432-0022684a4a24";
+}
diff --git a/plugins/chrony/chrony.h b/plugins/chrony/chrony.h
new file mode 100644 (file)
index 0000000..aca57a3
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+    Copyright (C) 2012  Intel Corporation
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Lesser General Public
+    License as published by the Free Software Foundation; either
+    version 2.1 of the License, or (at your option) any later version.
+
+    This library 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
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with this library; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+
+#ifndef CHRONYSINK_H
+#define CHRONYSINK_H
+
+#include "abstractsink.h"
+
+#define CHRONYD_SOCKET "/tmp/chrony.gps.sock"
+
+struct chrony_sock_sample {
+  struct timeval tv;
+  double offset;
+  int pulse;
+  int leap;
+  int _pad;
+  int magic;
+};
+
+class ChronySink : public AbstractSink
+{
+
+public:
+       ChronySink(AbstractRoutingEngine* engine, map<string, string> config);
+       virtual PropertyList subscriptions();
+       virtual void supportedChanged(const PropertyList & supportedProperties);
+       virtual void propertyChanged( AbstractPropertyType* value);
+       virtual const std::string uuid();
+};
+
+#endif // CHRONYSINK_H