Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add code to optionally write status file contents to a JSON file #7

Open
wants to merge 8 commits into
base: wowczarek-2.3.2-libcck
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions packagebuild/rpm-rh/ptpd.spec
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Name: ptpd-libcck
Summary: Synchronises system time using the Precision Time Protocol (PTP) implementing the IEEE 1588-2008 (PTP v 2) standard. Full version with master and slave support.
%endif
Version: 2.3.2
Release: 6%{distver}%{?gittag}
Release: 6.8%{distver}%{?gittag}
License: distributable
Group: System Environment/Daemons
Vendor: PTPd project team
Expand Down Expand Up @@ -73,9 +73,9 @@ time synchronised via the PTP protocol or serving PTP time.
%build

%if %{slaveonly_build} == 1
./configure --enable-slave-only --with-max-unicast-destinations=512
./configure --enable-slave-only --with-max-unicast-destinations=512 --enable-runtime-debug
%else
./configure --with-max-unicast-destinations=512
./configure --with-max-unicast-destinations=512 --enable-runtime-debug
%endif

make -j8
Expand Down Expand Up @@ -217,6 +217,10 @@ fi
%{_datadir}/ptpd/*

%changelog
* Mon Dec 4 2017 Luke Bigum <[email protected]> 2.3.2-6.8
- Fix memory leak when using JSON file
* Sun Nov 12 2017 Luke Bigum <[email protected]> 2.3.2-6.2
- add option to write status contents to a JSON file
* Mon Feb 27 2017 Wojciech Owczarek <[email protected]> 2.3.2-6
- libcck progress import (clockdriver, transport, timer)
* Fri Nov 18 2016 Wojciech Owczarek <[email protected]> 2.3.2-5
Expand Down
2 changes: 2 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ ptpd_SOURCES = \
dep/iniparser/iniparser.h \
dep/iniparser/dictionary.c \
dep/iniparser/iniparser.c \
dep/parson/parson.c \
dep/parson/parson.h \
dep/configdefaults.h \
dep/configdefaults.c \
dep/daemonconfig.h \
Expand Down
3 changes: 2 additions & 1 deletion src/dep/cck_glue.c
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,9 @@ ptpPortStateChange(PtpClock *ptpClock, const uint8_t from, const uint8_t to)
if(to == PTP_SLAVE || to == PTP_MASTER || to == PTP_PASSIVE) {
if(to == PTP_SLAVE) {
cd->setExternalReference(cd, "PTP", RC_PTP);
cd->maintainLock = false;
}
cd->owner = ptpClock;
cd->maintainLock = false;
cd->callbacks.onStep = ptpPortStepNotify;
cd->callbacks.onLock = ptpPortLocked;
cd->callbacks.onFrequencyJump = ptpPortFrequencyJump;
Expand Down Expand Up @@ -412,6 +412,7 @@ setupPtpTimer(PtpTimer *timer, CckFdSet *fdSet, const char *name)

myTimer->owner = timer;
myTimer->config.randomDelay = true;
myTimer->config.delayOnce = true;
myTimer->callbacks.onExpired = ptpTimerExpiry;
timer->data = myTimer;

Expand Down
9 changes: 9 additions & 0 deletions src/dep/configdefaults.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ static const ConfigTemplate configTemplates[] = {
{ "full-logging", "Enable logging for all facilities (statistics, status, log file)", {
{"global:log_status", "y"},
{"global:status_file", "/var/run/ptpd.status"},
{"global:json_file", "/var/run/ptpd.json"},
{"global:statistics_log_interval", "1"},
{"global:lock_file", "/var/run/ptpd.pid"},
{"global:log_statistics", "y"},
Expand All @@ -121,6 +122,7 @@ static const ConfigTemplate configTemplates[] = {
{ "full-logging-instance", "Logging for all facilities using 'instance' variable which the user should provide", {
{"global:log_status", "y"},
{"global:status_file", "@rundir@/ptpd.@[email protected]"},
{"global:json_file", "@rundir@/ptpd.@[email protected]"},
{"global:statistics_log_interval", "1"},
{"global:lock_file", "@rundir@/ptpd.@[email protected]"},
{"global:log_statistics", "y"},
Expand Down Expand Up @@ -473,6 +475,13 @@ loadDefaultSettings( GlobalConfig* global )
global->statusLog.truncateOnReopen = FALSE;
global->statusLog.unlinkOnClose = TRUE;

global->jsonLog.logID = "status";
global->jsonLog.openMode = "w";
strncpy(global->jsonLog.logPath, DEFAULT_STATUSFILE, PATH_MAX);
global->jsonLog.logFP = NULL;
global->jsonLog.truncateOnReopen = FALSE;
global->jsonLog.unlinkOnClose = TRUE;

global->deduplicateLog = TRUE;

/* Management message support settings */
Expand Down
8 changes: 8 additions & 0 deletions src/dep/daemonconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -2426,6 +2426,14 @@ parseConfig ( int opCode, void *opArg, dictionary* dict, GlobalConfig *global )
PTPD_RESTART_NONE, &global->statusLog.logEnabled, global->statusLog.logEnabled,
"Enable / disable writing status information to file.");

CONFIG_KEY_TRIGGER("global:json_file", global->jsonLog.logEnabled,TRUE,FALSE);
parseResult &= configMapString(opCode, opArg, dict, target, "global:json_file",
PTPD_RESTART_LOGGING, global->jsonLog.logPath, sizeof(global->jsonLog.logPath), global->jsonLog.logPath,
"File used to log "PTPD_PROGNAME" status information in JSON format.");
parseResult &= configMapBoolean(opCode, opArg, dict, target, "global:log_json",
PTPD_RESTART_NONE, &global->jsonLog.logEnabled, global->jsonLog.logEnabled,
"Enable / disable writing status information to JSON file.");

parseResult &= configMapInt(opCode, opArg, dict, target, "global:status_update_interval",
PTPD_RESTART_LOGGING, INTTYPE_INT, &global->statusFileUpdateInterval, global->statusFileUpdateInterval,
"Status file update interval in seconds.", RANGECHECK_RANGE,
Expand Down
3 changes: 3 additions & 0 deletions src/dep/parson/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
test
*.o
21 changes: 21 additions & 0 deletions src/dep/parson/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2012 - 2017 Krzysztof Gabis

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.
145 changes: 145 additions & 0 deletions src/dep/parson/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
## About
Parson is a lighweight [json](http://json.org) library written in C.

## Features
* Full JSON support
* Lightweight (only 2 files)
* Simple API
* Addressing json values with dot notation (similar to C structs or objects in most OO languages, e.g. "objectA.objectB.value")
* C89 compatible
* Test suites

## Installation
Run:
```
git clone https://github.com/kgabis/parson.git
```
and copy parson.h and parson.c to you source code tree.

Run ```make test``` to compile and run tests.

## Examples
### Parsing JSON
Here is a function, which prints basic commit info (date, sha and author) from a github repository.
```c
void print_commits_info(const char *username, const char *repo) {
JSON_Value *root_value;
JSON_Array *commits;
JSON_Object *commit;
size_t i;

char curl_command[512];
char cleanup_command[256];
char output_filename[] = "commits.json";

/* it ain't pretty, but it's not a libcurl tutorial */
sprintf(curl_command,
"curl -s \"https://api.github.com/repos/%s/%s/commits\" > %s",
username, repo, output_filename);
sprintf(cleanup_command, "rm -f %s", output_filename);
system(curl_command);

/* parsing json and validating output */
root_value = json_parse_file(output_filename);
if (json_value_get_type(root_value) != JSONArray) {
system(cleanup_command);
return;
}

/* getting array from root value and printing commit info */
commits = json_value_get_array(root_value);
printf("%-10.10s %-10.10s %s\n", "Date", "SHA", "Author");
for (i = 0; i < json_array_get_count(commits); i++) {
commit = json_array_get_object(commits, i);
printf("%.10s %.10s %s\n",
json_object_dotget_string(commit, "commit.author.date"),
json_object_get_string(commit, "sha"),
json_object_dotget_string(commit, "commit.author.name"));
}

/* cleanup code */
json_value_free(root_value);
system(cleanup_command);
}

```
Calling ```print_commits_info("torvalds", "linux");``` prints:
```
Date SHA Author
2012-10-15 dd8e8c4a2c David Rientjes
2012-10-15 3ce9e53e78 Michal Marek
2012-10-14 29bb4cc5e0 Randy Dunlap
2012-10-15 325adeb55e Ralf Baechle
2012-10-14 68687c842c Russell King
2012-10-14 ddffeb8c4d Linus Torvalds
...
```

### Persistence
In this example I'm using parson to save user information to a file and then load it and validate later.
```c
void persistence_example(void) {
JSON_Value *schema = json_parse_string("{\"name\":\"\"}");
JSON_Value *user_data = json_parse_file("user_data.json");
char buf[256];
const char *name = NULL;
if (user_data == NULL || json_validate(schema, user_data) != JSONSuccess) {
puts("Enter your name:");
scanf("%s", buf);
user_data = json_value_init_object();
json_object_set_string(json_object(user_data), "name", buf);
json_serialize_to_file(user_data, "user_data.json");
}
name = json_object_get_string(json_object(user_data), "name");
printf("Hello, %s.", name);
json_value_free(schema);
json_value_free(user_data);
return;
}
```

### Serialization
Creating JSON values is very simple thanks to the dot notation.
Object hierarchy is automatically created when addressing specific fields.
In the following example I create a simple JSON value containing basic information about a person.
```c
void serialization_example(void) {
JSON_Value *root_value = json_value_init_object();
JSON_Object *root_object = json_value_get_object(root_value);
char *serialized_string = NULL;
json_object_set_string(root_object, "name", "John Smith");
json_object_set_number(root_object, "age", 25);
json_object_dotset_string(root_object, "address.city", "Cupertino");
json_object_dotset_value(root_object, "contact.emails", json_parse_string("[\"[email protected]\",\"[email protected]\"]"));
serialized_string = json_serialize_to_string_pretty(root_value);
puts(serialized_string);
json_free_serialized_string(serialized_string);
json_value_free(root_value);
}

```

Output:
```
{
"name": "John Smith",
"age": 25,
"address": {
"city": "Cupertino"
},
"contact": {
"emails": [
"[email protected]",
"[email protected]"
]
}
}
```

## Contributing

I will always merge *working* bug fixes. However, if you want to add something new to the API, please create an "issue" on github for this first so we can discuss if it should end up in the library before you start implementing it.
Remember to follow parson's code style and write appropriate tests.

## License
[The MIT License (MIT)](http://opensource.org/licenses/mit-license.php)
12 changes: 12 additions & 0 deletions src/dep/parson/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "parson",
"version": "0.0.0",
"repo": "kgabis/parson",
"description": "Small json parser and reader",
"keywords": [ "json", "parser" ],
"license": "MIT",
"src": [
"parson.c",
"parson.h"
]
}
Loading