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:
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 from string import Template
18 from jni_common_gen import generate_j2c_swap, generate_j2c_identifiers, generate_c2j_swap
19 from jvpp_model import Class
22 def generate_type_handlers(model, logger):
24 Generates host-to-net and net-to-host functions for all custom types defined in the VPP API
25 :param model: meta-model of VPP API used for jVPP generation.
26 :param logger: jVPP logger
30 #TODO(VPP-1186): move the logic to JNI generators
31 if isinstance(t, Class):
32 _generate_class(model, t, type_handlers)
34 logger.debug("Skipping custom JNI type handler generation for %s", t)
36 return "\n".join(type_handlers)
39 def _generate_class(model, t, type_handlers):
40 ref_name = t.java_name_lower
41 jni_identifiers = generate_j2c_identifiers(t, class_ref_name="%sClass" % ref_name, object_ref_name="_host")
42 type_handlers.append(_TYPE_NET_TO_HOST_TEMPLATE.substitute(
44 json_filename=model.json_api_files,
45 json_definition=t.doc,
46 type_reference_name=ref_name,
48 jni_identifiers=jni_identifiers,
49 type_swap=generate_j2c_swap(t, struct_ref_name="_net")
52 type_handlers.append(_TYPE_HOST_TO_NET_TEMPLATE.substitute(
54 json_filename=model.json_api_files,
55 json_definition=t.doc,
56 type_reference_name=ref_name,
58 jni_identifiers=jni_identifiers,
59 type_swap=generate_c2j_swap(t, object_ref_name="_host", struct_ref_name="_net")
62 _TYPE_NET_TO_HOST_TEMPLATE = Template("""
64 * Host to network byte order conversion for ${c_name} type.
65 * Generated based on $json_filename:
68 static inline void _host_to_net_${c_name}(JNIEnv * env, jobject _host, vl_api_${c_name}_t * _net)
70 jclass ${type_reference_name}Class = (*env)->FindClass(env, "${class_FQN}");
75 _TYPE_HOST_TO_NET_TEMPLATE = Template("""
77 * Network to host byte order conversion for ${c_name} type.
78 * Generated based on $json_filename:
81 static inline void _net_to_host_${c_name}(JNIEnv * env, vl_api_${c_name}_t * _net, jobject _host)
83 jclass ${type_reference_name}Class = (*env)->FindClass(env, "${class_FQN}");
88 def _generate_scalar_host_to_net_swap(field):
89 field_type = field.type
90 if field_type.is_swap_needed:
91 return field_type.get_host_to_net_function(field.java_name, "*_net")
93 return "*_net = %s" % field.java_name
96 def _generate_scalar_net_to_host_swap(field):
97 field_type = field.type
98 if field_type.is_swap_needed:
99 return "%s((%s) _net);" % (field_type.net_to_host_function, field_type.name)