summaryrefslogtreecommitdiffstats
path: root/src/openxc/can_bus.cpp
blob: 545a6c5c24531e6d87b8ed1880a729f48079b618 (plain)
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
88
89
#include "can_bus.hpp"

namespace openxc
{
	std::uint32_t can_bus::controller() const
	{
		return controller_;
	}
	
	std::uint32_t can_bus::speed() const
	{
		return speed_;
	}
	
	can_bus_mode can_bus::raw_can_mode() const
	{
		return raw_can_mode_;
	}
	
	bool can_bus::raw_writable() const
	{
		return raw_writable_;
	}
	
	std::uint32_t can_bus::max_message_frequency() const
	{
		return max_message_frequency_;
	}
	
	bool can_bus::force_send_changed() const
	{
		return force_send_changed_;
	}
	
	void can_bus::from_json(const nlohmann::json& j)
	{
		controller_ = j.count("controller") ? j["controller"].get<std::uint32_t>() : 1;
		speed_ = j.count("speed") ? j["speed"].get<std::uint32_t>() : 0;
		raw_can_mode_ = j.count("raw_can_mode") ? j["raw_can_mode"].get<can_bus_mode>() : can_bus_mode::off;
		raw_writable_ = j.count("raw_writable") ? j["raw_writable"].get<bool>() : false;
		max_message_frequency_ = j.count("max_message_frequency") ? j["max_message_frequency"].get<std::uint32_t>() : 0;
		force_send_changed_ = j.count("force_send_changed") ? j["force_send_changed"].get<bool>() : false;
	}

	nlohmann::json can_bus::to_json() const
	{
		nlohmann::json j;
		j["controller"] = controller_;
		j["speed"] = speed_;
		j["raw_can_mode"] = raw_can_mode_;
		j["raw_writable"] = raw_writable_;
		j["max_message_frequency"] = max_message_frequency_;
		j["force_send_changed"] = force_send_changed_;
		return j;
	}

	void to_json(nlohmann::json& j, const can_bus& p)
	{
		j = p.to_json();
	}

	void from_json(const nlohmann::json& j, can_bus& p)
	{
		p.from_json(j);
	}

	void to_json(nlohmann::json& j, const can_bus_mode& p)
	{
		switch (p)
		{
		case can_bus_mode::off:
			j = std::string("off");
			break;
		case can_bus_mode::filtered:
			j = std::string("filtered");
			break;
		case can_bus_mode::unfiltered:
			j = std::string("unfiltered");
			break;
		}
	}

	void from_json(const nlohmann::json& j, can_bus_mode& p)
	{
		if (j.get<std::string>() == "off") p = can_bus_mode::off;
		else if (j.get<std::string>() == "filtered") p = can_bus_mode::filtered;
		else p = can_bus_mode::unfiltered;
	}
}