eb6ab235ba51346759a40f3bf710dfe8cde5023a
[vpp.git] / src / vpp-api / java / jvpp / gen / jvppgen / jni_type_handlers_gen.py
1 #!/usr/bin/env python2
2 #
3 # Copyright (c) 2018 Cisco and/or its affiliates.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16 from string import Template
17
18 from jni_common_gen import generate_j2c_swap, generate_j2c_identifiers, generate_c2j_swap
19 from jvpp_model import Class
20
21
22 def generate_type_handlers(model):
23     """
24     Generates msg handlers for all messages except for dumps and requests (handled by vpp, not client).
25     :param model: meta-model of VPP API used for jVPP generation.
26     """
27     type_handlers = []
28     for t in model.types:
29         if not isinstance(t, Class):
30             continue
31         ref_name = t.java_name_lower
32         jni_identifiers = generate_j2c_identifiers(t, class_ref_name="%sClass" % ref_name, object_ref_name="_host")
33         type_handlers.append(_TYPE_NET_TO_HOST_TEMPLATE.substitute(
34             c_name=t.name,
35             json_filename=model.json_api_files,
36             json_definition=t.doc,
37             type_reference_name=ref_name,
38             class_FQN=t.jni_name,
39             jni_identifiers=jni_identifiers,
40             type_swap=generate_j2c_swap(t, struct_ref_name="_net")
41         ))
42
43         type_handlers.append(_TYPE_HOST_TO_NET_TEMPLATE.substitute(
44             c_name=t.name,
45             json_filename=model.json_api_files,
46             json_definition=t.doc,
47             type_reference_name=ref_name,
48             class_FQN=t.jni_name,
49             jni_identifiers=jni_identifiers,
50             type_swap=generate_c2j_swap(t, object_ref_name="_host", struct_ref_name="_net")
51         ))
52
53     return "\n".join(type_handlers)
54
55 _TYPE_NET_TO_HOST_TEMPLATE = Template("""
56 /**
57  * Host to network byte order conversion for ${c_name} type.
58  * Generated based on $json_filename:
59 $json_definition
60  */
61 static inline void _host_to_net_${c_name}(JNIEnv * env, jobject _host, vl_api_${c_name}_t * _net)
62 {
63     jclass ${type_reference_name}Class = (*env)->FindClass(env, "${class_FQN}");
64 $jni_identifiers
65 $type_swap
66 }""")
67
68 _TYPE_HOST_TO_NET_TEMPLATE = Template("""
69 /**
70  * Network to host byte order conversion for ${c_name} type.
71  * Generated based on $json_filename:
72 $json_definition
73  */
74 static inline void _net_to_host_${c_name}(JNIEnv * env, vl_api_${c_name}_t * _net, jobject _host)
75 {
76     jclass ${type_reference_name}Class = (*env)->FindClass(env, "${class_FQN}");
77 $type_swap
78 }""")