Adapter.cpp.ftl 5.61 KB
Newer Older
Bernhard Rumpe's avatar
BR-sy    
Bernhard Rumpe committed
1
<#-- (c) https://github.com/MontiCore/monticore -->
2
3
4
<#import "SomeIPMacros.ftl" as m>
#include "<@m.mwIdent/>Adapter_${model.getEscapedCompName()}.h"

5
<@m.mwIdent/>Adapter_${model.getEscapedCompName()}::<@m.mwIdent/>Adapter_${model.getEscapedCompName()}() : public IAdapter_${model.getEscapedCompName()} {
Florian Raeth's avatar
Florian Raeth committed
6

7
}
8
9
10
11
12

void <@m.mwIdent/>Adapter_${model.getEscapedCompName()}::init(${model.getEscapedCompName()} *comp) {
    // Initialize component
	this->component = comp;

13

Florian Raeth's avatar
Florian Raeth committed
14
15
	// Intitialize Subscriber
	<#list model.getIncomingPorts() as sub>
16
		${sub.getName()}_Subscriber = vsomeip::runtime::get()->create_application("Subscriber ${sub.getName()}");
Florian Raeth's avatar
Florian Raeth committed
17
18
19
20
		if (!${sub.getName()}_Subscriber->init()) {
            std::cerr << "Couldn't initialize Subscriber ${sub.getName()}" << std::endl;
        }
		
21
22
		${sub.getName()}_Subscriber->register_availability_handler(${model.getServiceId(sub)}, ${model.getInstanceId(sub)}, std::bind(&SomeIPAdapter_${model.getEscapedCompName()}::on_availability_${sub.getName()}, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
		${sub.getName()}_Subscriber->request_service(${model.getServiceId(sub)}, ${model.getInstanceId(sub)});
23
		${sub.getName()}_Subscriber->register_message_handler(${model.getServiceId(sub)}, ${model.getInstanceId(sub)}, ${model.getMethodId(sub)}, std::bind(&SomeIPAdapter_${model.getEscapedCompName()}::on_message_${sub.getName()}, this, std::placeholders::_1));
Florian Raeth's avatar
Florian Raeth committed
24

25
26
		std::thread sender_${sub.getName()}(std::bind(&SomeIPAdapter_${model.getEscapedCompName()}::run_${sub.getName()}, this));
		${sub.getName()}_Subscriber->start();
Florian Raeth's avatar
Florian Raeth committed
27
28
29
30
31
    </#list>


	// Intitialize Publisher
	<#list model.getOutgoingPorts() as pub>
32
    	${pub.getName()}_Publisher = vsomeip::runtime::get()->create_application("Publisher ${pub.getName()}");
Florian Raeth's avatar
Florian Raeth committed
33
34
35
		if (!${pub.getName()}_Publisher->init()) {
            std::cerr << "Couldn't initialize Publisher ${pub.getName()}" << std::endl;
        }
36
    	${pub.getName()}_Publisher->offer_service(${model.getServiceId(pub)}, ${model.getInstanceId(pub)});
Florian Raeth's avatar
Florian Raeth committed
37
38
		${pub.getName()}_Publisher->start();
    </#list>
39
40
}

Florian Raeth's avatar
Florian Raeth committed
41
<#list model.getIncomingPorts() as sub>
42
43
44
45
46
47
48
49
50
51
52
void <@m.mwIdent/>Adapter_${model.getEscapedCompName()}::run_${sub.getName()}() {
	std::unique_lock<std::mutex> its_lock(mutex_${sub.getName()});
	condition_${sub.getName()}.wait(its_lock);

	std::set<vsomeip::eventgroup_t> event_group;
	event_group.insert(${model.getEventGroupId(sub)});
	${sub.getName()}_Subscriber->request_event(${model.getServiceId(sub)}, ${model.getInstanceId(sub)}, ${model.getEventId(sub)}, event_group, true);
	${sub.getName()}_Subscriber->subscribe(${model.getServiceId(sub)}, ${model.getInstanceId(sub)}, ${model.getEventGroupId(sub)});
}

void <@m.mwIdent/>Adapter_${model.getEscapedCompName()}::on_message_${sub.getName()}(const std::shared_ptr<vsomeip::message> &_message) {
53
	//read received message
54
    std::shared_ptr<vsomeip::payload> its_payload = _message->get_payload();
55
56
    vsomeip::length_t l = its_payload->get_length();
    double dataFromMessage = *((double*)its_payload->get_data());
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
	
	<#switch sub.getTypeReference().getName()>
		<#case "Q">
        // double
		component->${sub.getName()} = dataFromMessage;
        <#break>
        <#case "N">
        //int
		component->${sub.getName()} = (int) round(dataFromMessage);
        <#break>
        <#case "Z">
        //int
		component->${sub.getName()} = (int) round(dataFromMessage);
        <#break>
        <#case "B">
        //bool
		component->${sub.getName()} = (dataFromMessage > 1.0e-10);
        <#break>
        <#default>
        //error
    </#switch>

79
	//print data to std out
Florian Raeth's avatar
Florian Raeth committed
80
    std::cout << "SERVICE ${sub.getName()}: Received message from ["
81
82
			<< std::setw(4) << std::setfill('0') << std::hex << _message->get_client() << "/"
			<< std::setw(4) << std::setfill('0') << std::hex << _message->get_session() << "]: "
Florian Raeth's avatar
Florian Raeth committed
83
			<< dataFromMessage << std::endl;
84
}
85
86
87
88
89
90
91
92
93

void <@m.mwIdent/>Adapter_${model.getEscapedCompName()}::on_availability_${sub.getName()}(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) {
	std::cout << "Service ["
		<< std::setw(4) << std::setfill('0') << std::hex << _service << "." << _instance
		<< "] is "
		<< (_is_available ? "available." : "NOT available.")
		<< std::endl;
	condition_${sub.getName()}.notify_one();
}
Florian Raeth's avatar
Florian Raeth committed
94
</#list>
95

Florian Raeth's avatar
Florian Raeth committed
96
97
98

<#list model.getOutgoingPorts() as pub>
void <@m.mwIdent/>Adapter_${model.getEscapedCompName()}::publish${pub.getName()}_Publisher()
99
100
{
    //Read data from component
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
	<#switch pub.getTypeReference().getName()>
        <#case "Q">
        // double
    	double d = component->${pub.getName()};
        <#break>
        <#case "N">
        //int
    	double d = 1.0 * component->${pub.getName()};
        <#break>
        <#case "Z">
        //int
    	double d = 1.0 * component->${pub.getName()};
        <#break>
        <#case "B">
        //bool
    	double d = component->${pub.getName()} ? 1.0 : 0.0;
        <#break>
        <#default>
        //error
    </#switch>
121

122
123
124
	const vsomeip::byte_t its_data[] = { (uint8_t) d };
	std::shared_ptr<vsomeip::payload> payload = vsomeip::runtime::get()->create_payload();
	payload->set_data(its_data, sizeof(its_data));
125
126

	//Publish
Florian Raeth's avatar
Florian Raeth committed
127
	std::set<vsomeip::eventgroup_t> event_group;
128
129
130
	event_group.insert(${model.getEventGroupId(pub)});
	${pub.getName()}_Publisher->offer_event(${model.getServiceId(pub)}, ${model.getInstanceId(pub)}, ${model.getEventId(pub)}, event_group, true);
	${pub.getName()}_Publisher->notify(${model.getServiceId(pub)}, ${model.getInstanceId(pub)}, ${model.getEventId(pub)}, payload);
131
}
Florian Raeth's avatar
Florian Raeth committed
132
</#list>
133

Florian Raeth's avatar
Florian Raeth committed
134

135
136
void <@m.mwIdent/>Adapter_${model.getEscapedCompName()}::tick()
{
Florian Raeth's avatar
Florian Raeth committed
137
138
139
	<#list model.getOutgoingPorts() as pub>
    	publish${pub.getName()}_Publisher();
    </#list>
140
}