Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (c) 2016 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 | # l |
| 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 | |
| 17 | import os, util |
| 18 | from string import Template |
| 19 | |
| 20 | def is_manually_generated(f_name): |
| 21 | return f_name in {'control_ping_reply'} |
| 22 | |
Marek Gradzki | 0aaf92f | 2016-05-03 17:05:27 +0200 | [diff] [blame] | 23 | |
| 24 | class_reference_template = Template("""jclass ${ref_name}Class; |
| 25 | """) |
| 26 | |
| 27 | find_class_invocation_template = Template(""" |
| 28 | ${ref_name}Class = (jclass)(*env)->NewGlobalRef(env, (*env)->FindClass(env, "org/openvpp/jvpp/dto/${class_name}")); |
| 29 | if ((*env)->ExceptionCheck(env)) { |
| 30 | (*env)->ExceptionDescribe(env); |
| 31 | return JNI_ERR; |
| 32 | }""") |
| 33 | |
Tibor Sirovatka | 42bb61f | 2016-05-18 14:54:50 +0200 | [diff] [blame] | 34 | find_class_template = Template(""" |
| 35 | ${ref_name}Class = (jclass)(*env)->NewGlobalRef(env, (*env)->FindClass(env, "${class_name}")); |
| 36 | if ((*env)->ExceptionCheck(env)) { |
| 37 | (*env)->ExceptionDescribe(env); |
| 38 | return JNI_ERR; |
| 39 | }""") |
| 40 | |
Marek Gradzki | 0aaf92f | 2016-05-03 17:05:27 +0200 | [diff] [blame] | 41 | class_cache_template = Template(""" |
| 42 | $class_references |
| 43 | static int cache_class_references(JNIEnv* env) { |
| 44 | $find_class_invocations |
| 45 | return 0; |
| 46 | }""") |
| 47 | |
| 48 | def generate_class_cache(func_list): |
| 49 | class_references = [] |
| 50 | find_class_invocations = [] |
| 51 | for f in func_list: |
| 52 | c_name = f['name'] |
| 53 | class_name = util.underscore_to_camelcase_upper(c_name) |
| 54 | ref_name = util.underscore_to_camelcase(c_name) |
| 55 | |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 56 | if util.is_ignored(c_name): |
Marek Gradzki | 0aaf92f | 2016-05-03 17:05:27 +0200 | [diff] [blame] | 57 | continue |
| 58 | |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 59 | if util.is_reply(class_name): |
| 60 | class_references.append(class_reference_template.substitute( |
Marek Gradzki | 0aaf92f | 2016-05-03 17:05:27 +0200 | [diff] [blame] | 61 | ref_name=ref_name)) |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 62 | find_class_invocations.append(find_class_invocation_template.substitute( |
Marek Gradzki | 0aaf92f | 2016-05-03 17:05:27 +0200 | [diff] [blame] | 63 | ref_name=ref_name, |
| 64 | class_name=class_name)) |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 65 | elif util.is_notification(c_name): |
| 66 | class_references.append(class_reference_template.substitute( |
| 67 | ref_name=util.add_notification_suffix(ref_name))) |
| 68 | find_class_invocations.append(find_class_invocation_template.substitute( |
| 69 | ref_name=util.add_notification_suffix(ref_name), |
| 70 | class_name=util.add_notification_suffix(class_name))) |
Marek Gradzki | 0aaf92f | 2016-05-03 17:05:27 +0200 | [diff] [blame] | 71 | |
Tibor Sirovatka | 42bb61f | 2016-05-18 14:54:50 +0200 | [diff] [blame] | 72 | # add exception class to class cache |
| 73 | ref_name = 'callbackException' |
| 74 | class_name = 'org/openvpp/jvpp/VppCallbackException' |
| 75 | class_references.append(class_reference_template.substitute( |
| 76 | ref_name=ref_name)) |
| 77 | find_class_invocations.append(find_class_template.substitute( |
| 78 | ref_name=ref_name, |
| 79 | class_name=class_name)) |
Marek Gradzki | 0aaf92f | 2016-05-03 17:05:27 +0200 | [diff] [blame] | 80 | return class_cache_template.substitute( |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 81 | class_references="".join(class_references), find_class_invocations="".join(find_class_invocations)) |
Marek Gradzki | 0aaf92f | 2016-05-03 17:05:27 +0200 | [diff] [blame] | 82 | |
| 83 | |
| 84 | # TODO: cache method and field identifiers to achieve better performance |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 85 | # https://jira.fd.io/browse/HONEYCOMB-42 |
| 86 | request_class_template = Template(""" |
| 87 | jclass requestClass = (*env)->FindClass(env, "org/openvpp/jvpp/dto/${java_name_upper}");""") |
| 88 | |
| 89 | request_field_identifier_template = Template(""" |
| 90 | jfieldID ${java_name}FieldId = (*env)->GetFieldID(env, requestClass, "${java_name}", "${jni_signature}"); |
| 91 | ${jni_type} ${java_name} = (*env)->Get${jni_getter}(env, request, ${java_name}FieldId); |
| 92 | """) |
| 93 | |
| 94 | u8_struct_setter_template = Template(""" |
| 95 | mp->${c_name} = ${java_name};""") |
| 96 | |
| 97 | u16_struct_setter_template = Template(""" |
| 98 | mp->${c_name} = clib_host_to_net_u16(${java_name});""") |
| 99 | |
| 100 | u32_struct_setter_template = Template(""" |
| 101 | mp->${c_name} = clib_host_to_net_u32(${java_name});""") |
| 102 | |
| 103 | i32_struct_setter_template = Template(""" |
| 104 | mp->${c_name} = clib_host_to_net_i32(${java_name});!""") |
| 105 | |
| 106 | u64_struct_setter_template = Template(""" |
| 107 | mp->${c_name} = clib_host_to_net_u64(${java_name});""") |
| 108 | |
| 109 | u8_array_struct_setter_template = Template(""" |
| 110 | { |
| 111 | jsize cnt = (*env)->GetArrayLength (env, ${java_name}); |
Marek Gradzki | c4cb44c | 2016-05-24 13:32:26 +0200 | [diff] [blame] | 112 | size_t max_size = ${field_length}; |
| 113 | if (max_size != 0 && cnt > max_size) cnt = max_size; |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 114 | (*env)->GetByteArrayRegion(env, ${java_name}, 0, cnt, (jbyte *)mp->${c_name}); |
| 115 | } |
| 116 | """) |
| 117 | |
| 118 | u32_array_struct_setter_template = Template(""" |
| 119 | jint * ${java_name}ArrayElements = (*env)->GetIntArrayElements(env, ${java_name}, NULL); |
| 120 | { |
Marek Gradzki | c4cb44c | 2016-05-24 13:32:26 +0200 | [diff] [blame] | 121 | size_t _i; |
| 122 | jsize cnt = (*env)->GetArrayLength (env, ${java_name}); |
| 123 | size_t max_size = ${field_length}; |
| 124 | if (max_size != 0 && cnt > max_size) cnt = max_size; |
| 125 | for (_i = 0; _i < cnt; _i++) { |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 126 | mp->${c_name}[_i] = clib_host_to_net_u32(${java_name}ArrayElements[_i]); |
| 127 | } |
| 128 | } |
| 129 | (*env)->ReleaseIntArrayElements (env, ${java_name}, ${java_name}ArrayElements, 0); |
| 130 | """) |
| 131 | |
| 132 | vl_api_ip4_fib_counter_t_array_struct_setter_template = Template(""" |
| 133 | // vl_api_ip4_fib_counter_t_array_field_setter_template FIXME""") |
| 134 | |
| 135 | vl_api_ip6_fib_counter_t_array_struct_setter_template = Template(""" |
| 136 | // vl_api_ip6_fib_counter_t_array_field_setter_template FIXME""") |
| 137 | |
| 138 | struct_setter_templates = {'u8': u8_struct_setter_template, |
Marek Gradzki | 34e7772 | 2016-05-30 21:52:37 +0200 | [diff] [blame] | 139 | 'u16': u16_struct_setter_template, |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 140 | 'u32': u32_struct_setter_template, |
| 141 | 'i32': u32_struct_setter_template, |
| 142 | 'u64': u64_struct_setter_template, |
| 143 | 'u8[]': u8_array_struct_setter_template, |
| 144 | 'u32[]': u32_array_struct_setter_template, |
| 145 | 'vl_api_ip4_fib_counter_t[]': vl_api_ip4_fib_counter_t_array_struct_setter_template, |
| 146 | 'vl_api_ip6_fib_counter_t[]': vl_api_ip6_fib_counter_t_array_struct_setter_template |
| 147 | } |
| 148 | |
| 149 | jni_impl_template = Template(""" |
| 150 | /** |
| 151 | * JNI binding for sending ${c_name} vpe.api message. |
| 152 | * Generated based on $inputfile preparsed data: |
| 153 | $api_data |
| 154 | */ |
| 155 | JNIEXPORT jint JNICALL Java_org_openvpp_jvpp_JVppImpl_${java_name}0 |
| 156 | (JNIEnv * env, jclass clazz$args) { |
| 157 | vppjni_main_t *jm = &vppjni_main; |
| 158 | vl_api_${c_name}_t * mp; |
| 159 | u32 my_context_id; |
| 160 | int rv; |
| 161 | rv = vppjni_sanity_check (jm); |
| 162 | if (rv) return rv; |
| 163 | my_context_id = vppjni_get_context_id (jm); |
| 164 | $request_class |
| 165 | $field_identifiers |
| 166 | M(${c_name_uppercase}, ${c_name}); |
| 167 | mp->context = clib_host_to_net_u32 (my_context_id); |
| 168 | $struct_setters |
| 169 | S; |
Tibor Sirovatka | 42bb61f | 2016-05-18 14:54:50 +0200 | [diff] [blame] | 170 | if ((*env)->ExceptionCheck(env)) { |
| 171 | return JNI_ERR; |
| 172 | } |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 173 | return my_context_id; |
| 174 | }""") |
| 175 | |
| 176 | def generate_jni_impl(func_list, inputfile): |
| 177 | jni_impl = [] |
| 178 | for f in func_list: |
| 179 | f_name = f['name'] |
| 180 | camel_case_function_name = util.underscore_to_camelcase(f_name) |
| 181 | if is_manually_generated(f_name) or util.is_reply(camel_case_function_name) \ |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 182 | or util.is_ignored(f_name) or util.is_just_notification(f_name): |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 183 | continue |
| 184 | |
| 185 | arguments = '' |
| 186 | request_class = '' |
| 187 | field_identifiers = '' |
| 188 | struct_setters = '' |
| 189 | f_name_uppercase = f_name.upper() |
| 190 | |
| 191 | if f['args']: |
| 192 | arguments = ', jobject request' |
| 193 | camel_case_function_name_upper = util.underscore_to_camelcase_upper(f_name) |
| 194 | |
| 195 | request_class = request_class_template.substitute(java_name_upper=camel_case_function_name_upper) |
| 196 | |
| 197 | # field identifiers |
| 198 | for t in zip(f['types'], f['args']): |
| 199 | jni_type = t[0] |
| 200 | java_field_name = util.underscore_to_camelcase(t[1]) |
| 201 | jni_signature = util.jni_2_signature_mapping[jni_type] |
| 202 | jni_getter = util.jni_field_accessors[jni_type] |
| 203 | field_identifiers += request_field_identifier_template.substitute( |
| 204 | jni_type=jni_type, |
| 205 | java_name=java_field_name, |
| 206 | jni_signature=jni_signature, |
| 207 | jni_getter=jni_getter) |
| 208 | |
| 209 | # field setters |
Marek Gradzki | c4cb44c | 2016-05-24 13:32:26 +0200 | [diff] [blame] | 210 | for t in zip(f['c_types'], f['args'], f['lengths']): |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 211 | c_type = t[0] |
| 212 | c_name = t[1] |
Marek Gradzki | fa42e25 | 2016-06-15 16:38:33 +0200 | [diff] [blame] | 213 | field_length = t[2][0] |
| 214 | |
Marek Gradzki | 3659a5a | 2016-07-11 08:35:32 +0200 | [diff] [blame^] | 215 | # check if we are processing variable length array: |
| 216 | if t[2][1]: |
| 217 | field_length = util.underscore_to_camelcase(t[2][0]) |
| 218 | |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 219 | java_field_name = util.underscore_to_camelcase(c_name) |
| 220 | |
| 221 | struct_setter_template = struct_setter_templates[c_type] |
| 222 | |
| 223 | struct_setters += struct_setter_template.substitute( |
| 224 | c_name=c_name, |
Marek Gradzki | c4cb44c | 2016-05-24 13:32:26 +0200 | [diff] [blame] | 225 | java_name=java_field_name, |
| 226 | field_length=field_length) |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 227 | |
| 228 | jni_impl.append(jni_impl_template.substitute( |
| 229 | inputfile=inputfile, |
| 230 | api_data=util.api_message_to_javadoc(f), |
| 231 | java_name=camel_case_function_name, |
| 232 | c_name_uppercase=f_name_uppercase, |
| 233 | c_name=f_name, |
| 234 | request_class=request_class, |
| 235 | field_identifiers=field_identifiers, |
| 236 | struct_setters=struct_setters, |
| 237 | args=arguments)) |
| 238 | |
| 239 | return "\n".join(jni_impl) |
| 240 | |
| 241 | |
| 242 | dto_field_id_template = Template(""" |
Marek Gradzki | 0aaf92f | 2016-05-03 17:05:27 +0200 | [diff] [blame] | 243 | jfieldID ${java_name}FieldId = (*env)->GetFieldID(env, ${class_ref_name}Class, "${java_name}", "${jni_signature}");""") |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 244 | |
| 245 | default_dto_field_setter_template = Template(""" |
| 246 | (*env)->Set${jni_setter}(env, dto, ${java_name}FieldId, mp->${c_name}); |
| 247 | """) |
| 248 | |
Marek Gradzki | fa42e25 | 2016-06-15 16:38:33 +0200 | [diff] [blame] | 249 | variable_length_array_value_template = Template("""mp->${length_var_name}""") |
| 250 | variable_length_array_template = Template("""clib_net_to_host_${length_field_type}(${value})""") |
| 251 | |
Marek Gradzki | 34e7772 | 2016-05-30 21:52:37 +0200 | [diff] [blame] | 252 | u16_dto_field_setter_template = Template(""" |
| 253 | (*env)->Set${jni_setter}(env, dto, ${java_name}FieldId, clib_net_to_host_u16(mp->${c_name})); |
| 254 | """) |
| 255 | |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 256 | u32_dto_field_setter_template = Template(""" |
| 257 | (*env)->Set${jni_setter}(env, dto, ${java_name}FieldId, clib_net_to_host_u32(mp->${c_name})); |
| 258 | """) |
| 259 | |
| 260 | u64_dto_field_setter_template = Template(""" |
| 261 | (*env)->Set${jni_setter}(env, dto, ${java_name}FieldId, clib_net_to_host_u64(mp->${c_name})); |
| 262 | """) |
| 263 | |
| 264 | u8_array_dto_field_setter_template = Template(""" |
Marek Gradzki | c4cb44c | 2016-05-24 13:32:26 +0200 | [diff] [blame] | 265 | jbyteArray ${java_name} = (*env)->NewByteArray(env, ${field_length}); |
| 266 | (*env)->SetByteArrayRegion(env, ${java_name}, 0, ${field_length}, (const jbyte*)mp->${c_name}); |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 267 | (*env)->SetObjectField(env, dto, ${java_name}FieldId, ${java_name}); |
| 268 | """) |
| 269 | |
Marek Gradzki | 97d9951 | 2016-06-06 12:36:09 +0200 | [diff] [blame] | 270 | u32_array_dto_field_setter_template = Template(""" |
| 271 | { |
| 272 | jintArray ${java_name} = (*env)->NewIntArray(env, ${field_length}); |
| 273 | jint * ${java_name}ArrayElements = (*env)->GetIntArrayElements(env, ${java_name}, NULL); |
| 274 | unsigned int _i; |
| 275 | for (_i = 0; _i < ${field_length}; _i++) { |
| 276 | ${java_name}ArrayElements[_i] = clib_net_to_host_u32(mp->${c_name}[_i]); |
| 277 | } |
Marek Gradzki | fa42e25 | 2016-06-15 16:38:33 +0200 | [diff] [blame] | 278 | |
| 279 | (*env)->ReleaseIntArrayElements(env, ${java_name}, ${java_name}ArrayElements, 0); |
Marek Gradzki | 97d9951 | 2016-06-06 12:36:09 +0200 | [diff] [blame] | 280 | (*env)->SetObjectField(env, dto, ${java_name}FieldId, ${java_name}); |
| 281 | } |
| 282 | """) |
| 283 | |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 284 | # For each u64 array we get its elements. Then we convert values to host byte order. |
Marek Gradzki | 97d9951 | 2016-06-06 12:36:09 +0200 | [diff] [blame] | 285 | # All changes to jlong* buffer are written to jlongArray (isCopy is set to NULL) |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 286 | u64_array_dto_field_setter_template = Template(""" |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 287 | { |
Marek Gradzki | c4cb44c | 2016-05-24 13:32:26 +0200 | [diff] [blame] | 288 | jlongArray ${java_name} = (*env)->NewLongArray(env, ${field_length}); |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 289 | jlong * ${java_name}ArrayElements = (*env)->GetLongArrayElements(env, ${java_name}, NULL); |
Marek Gradzki | c4cb44c | 2016-05-24 13:32:26 +0200 | [diff] [blame] | 290 | unsigned int _i; |
| 291 | for (_i = 0; _i < ${field_length}; _i++) { |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 292 | ${java_name}ArrayElements[_i] = clib_net_to_host_u64(mp->${c_name}[_i]); |
| 293 | } |
Marek Gradzki | fa42e25 | 2016-06-15 16:38:33 +0200 | [diff] [blame] | 294 | |
| 295 | (*env)->ReleaseLongArrayElements(env, ${java_name}, ${java_name}ArrayElements, 0); |
Marek Gradzki | c4cb44c | 2016-05-24 13:32:26 +0200 | [diff] [blame] | 296 | (*env)->SetObjectField(env, dto, ${java_name}FieldId, ${java_name}); |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 297 | } |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 298 | """) |
| 299 | |
| 300 | dto_field_setter_templates = {'u8': default_dto_field_setter_template, |
Marek Gradzki | 34e7772 | 2016-05-30 21:52:37 +0200 | [diff] [blame] | 301 | 'u16': u16_dto_field_setter_template, |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 302 | 'u32': u32_dto_field_setter_template, |
| 303 | 'i32': u32_dto_field_setter_template, |
| 304 | 'u64': u64_dto_field_setter_template, |
Marek Gradzki | 34e7772 | 2016-05-30 21:52:37 +0200 | [diff] [blame] | 305 | 'f64': default_dto_field_setter_template, #fixme |
Marek Gradzki | 97d9951 | 2016-06-06 12:36:09 +0200 | [diff] [blame] | 306 | 'u8[]': u8_array_dto_field_setter_template, |
| 307 | 'u32[]': u32_array_dto_field_setter_template, |
| 308 | 'u64[]': u64_array_dto_field_setter_template |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 309 | } |
| 310 | |
Tibor Sirovatka | 42bb61f | 2016-05-18 14:54:50 +0200 | [diff] [blame] | 311 | # code fragment for checking result of the operation before sending request reply |
| 312 | callback_err_handler_template = Template(""" |
| 313 | // for negative result don't send callback message but send error callback |
| 314 | if (mp->retval<0) { |
| 315 | CallOnError("${handler_name}",mp->context,mp->retval); |
| 316 | return; |
| 317 | } |
| 318 | if (mp->retval == VNET_API_ERROR_IN_PROGRESS) { |
| 319 | clib_warning("Result in progress"); |
| 320 | return; |
| 321 | } |
| 322 | """) |
| 323 | |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 324 | msg_handler_template = Template(""" |
| 325 | /** |
| 326 | * Handler for ${handler_name} vpe.api message. |
| 327 | * Generated based on $inputfile preparsed data: |
| 328 | $api_data |
| 329 | */ |
| 330 | static void vl_api_${handler_name}_t_handler (vl_api_${handler_name}_t * mp) |
| 331 | { |
| 332 | vppjni_main_t * jm = &vppjni_main; |
| 333 | JNIEnv *env = jm->jenv; |
Tibor Sirovatka | 42bb61f | 2016-05-18 14:54:50 +0200 | [diff] [blame] | 334 | $err_handler |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 335 | |
Marek Gradzki | 0aaf92f | 2016-05-03 17:05:27 +0200 | [diff] [blame] | 336 | jmethodID constructor = (*env)->GetMethodID(env, ${class_ref_name}Class, "<init>", "()V"); |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 337 | jmethodID callbackMethod = (*env)->GetMethodID(env, jm->callbackClass, "on${dto_name}", "(Lorg/openvpp/jvpp/dto/${dto_name};)V"); |
| 338 | |
Marek Gradzki | 0aaf92f | 2016-05-03 17:05:27 +0200 | [diff] [blame] | 339 | jobject dto = (*env)->NewObject(env, ${class_ref_name}Class, constructor); |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 340 | $dto_setters |
| 341 | (*env)->CallVoidMethod(env, jm->callback, callbackMethod, dto); |
| 342 | }""") |
| 343 | |
| 344 | def generate_msg_handlers(func_list, inputfile): |
| 345 | handlers = [] |
| 346 | for f in func_list: |
| 347 | handler_name = f['name'] |
| 348 | dto_name = util.underscore_to_camelcase_upper(handler_name) |
Marek Gradzki | 0aaf92f | 2016-05-03 17:05:27 +0200 | [diff] [blame] | 349 | ref_name = util.underscore_to_camelcase(handler_name) |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 350 | |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 351 | if is_manually_generated(handler_name) or util.is_ignored(handler_name): |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 352 | continue |
| 353 | |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 354 | if not util.is_reply(dto_name) and not util.is_notification(handler_name): |
| 355 | continue |
| 356 | |
| 357 | if util.is_notification(handler_name): |
| 358 | dto_name = util.add_notification_suffix(dto_name) |
| 359 | ref_name = util.add_notification_suffix(ref_name) |
| 360 | |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 361 | dto_setters = '' |
Tibor Sirovatka | 42bb61f | 2016-05-18 14:54:50 +0200 | [diff] [blame] | 362 | err_handler = '' |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 363 | # dto setters |
Marek Gradzki | c4cb44c | 2016-05-24 13:32:26 +0200 | [diff] [blame] | 364 | for t in zip(f['c_types'], f['types'], f['args'], f['lengths']): |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 365 | c_type = t[0] |
| 366 | jni_type = t[1] |
| 367 | c_name = t[2] |
Marek Gradzki | fa42e25 | 2016-06-15 16:38:33 +0200 | [diff] [blame] | 368 | field_length = t[3][0] |
| 369 | |
| 370 | # check if we are processing variable length array |
| 371 | if t[3][1]: |
| 372 | length_var_name = t[3][0] |
| 373 | length_field_type = f['c_types'][f['args'].index(length_var_name)] |
| 374 | field_length = variable_length_array_value_template.substitute(length_var_name=length_var_name) |
| 375 | if length_field_type != 'u8': # we need net to host conversion: |
| 376 | field_length = variable_length_array_template.substitute( |
| 377 | length_field_type=length_field_type, value=field_length) |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 378 | |
Tibor Sirovatka | 42bb61f | 2016-05-18 14:54:50 +0200 | [diff] [blame] | 379 | # for retval don't generate setters and generate retval check |
| 380 | if util.is_retval_field(c_name): |
| 381 | err_handler = callback_err_handler_template.substitute( |
| 382 | handler_name=handler_name |
| 383 | ) |
| 384 | continue |
| 385 | |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 386 | java_field_name = util.underscore_to_camelcase(c_name) |
| 387 | jni_signature = util.jni_2_signature_mapping[jni_type] |
| 388 | jni_setter = util.jni_field_accessors[jni_type] |
| 389 | |
| 390 | dto_setters += dto_field_id_template.substitute( |
| 391 | java_name=java_field_name, |
Marek Gradzki | 0aaf92f | 2016-05-03 17:05:27 +0200 | [diff] [blame] | 392 | class_ref_name=ref_name, |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 393 | jni_signature=jni_signature) |
| 394 | |
| 395 | dto_setter_template = dto_field_setter_templates[c_type] |
| 396 | |
| 397 | dto_setters += dto_setter_template.substitute( |
| 398 | java_name=java_field_name, |
| 399 | jni_signature=jni_signature, |
| 400 | c_name=c_name, |
Marek Gradzki | c4cb44c | 2016-05-24 13:32:26 +0200 | [diff] [blame] | 401 | jni_setter=jni_setter, |
| 402 | field_length=field_length) |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 403 | |
| 404 | handlers.append(msg_handler_template.substitute( |
Tibor Sirovatka | 42bb61f | 2016-05-18 14:54:50 +0200 | [diff] [blame] | 405 | inputfile=inputfile, |
| 406 | api_data=util.api_message_to_javadoc(f), |
| 407 | handler_name=handler_name, |
| 408 | dto_name=dto_name, |
| 409 | class_ref_name=ref_name, |
| 410 | dto_setters=dto_setters, |
| 411 | err_handler=err_handler)) |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 412 | |
| 413 | return "\n".join(handlers) |
| 414 | |
| 415 | |
| 416 | handler_registration_template = Template("""_(${upercase_name}, ${name}) \\ |
| 417 | """) |
| 418 | |
| 419 | |
| 420 | def generate_handler_registration(func_list): |
| 421 | handler_registration = ["#define foreach_vpe_api_msg \\\n"] |
| 422 | for f in func_list: |
| 423 | name = f['name'] |
| 424 | camelcase_name = util.underscore_to_camelcase(f['name']) |
| 425 | |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 426 | if (not util.is_reply(camelcase_name) and not util.is_notification(name)) or util.is_ignored(name): |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 427 | continue |
| 428 | |
| 429 | handler_registration.append(handler_registration_template.substitute( |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 430 | name=name, |
| 431 | upercase_name=name.upper())) |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 432 | |
| 433 | return "".join(handler_registration) |
| 434 | |
Marek Gradzki | 0aaf92f | 2016-05-03 17:05:27 +0200 | [diff] [blame] | 435 | |
| 436 | jvpp_c_template = Template("""/** |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 437 | * This file contains JNI bindings for jvpp Java API. |
| 438 | * It was generated by jvpp_c_gen.py based on $inputfile |
| 439 | * (python representation of vpe.api generated by vppapigen). |
| 440 | */ |
| 441 | |
Tibor Sirovatka | 42bb61f | 2016-05-18 14:54:50 +0200 | [diff] [blame] | 442 | void CallOnError(const char* call, int context, int retval); |
| 443 | |
Marek Gradzki | 0aaf92f | 2016-05-03 17:05:27 +0200 | [diff] [blame] | 444 | // JAVA class reference cache |
| 445 | $class_cache |
| 446 | |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 447 | // JNI bindings |
| 448 | $jni_implementations |
| 449 | |
| 450 | // Message handlers |
| 451 | $msg_handlers |
| 452 | |
| 453 | // Registration of message handlers in vlib |
| 454 | $handler_registration |
| 455 | """) |
| 456 | |
| 457 | def generate_jvpp(func_list, inputfile): |
| 458 | """ Generates jvpp C file """ |
| 459 | print "Generating jvpp C" |
| 460 | |
Marek Gradzki | 0aaf92f | 2016-05-03 17:05:27 +0200 | [diff] [blame] | 461 | class_cache = generate_class_cache(func_list) |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 462 | jni_impl = generate_jni_impl(func_list, inputfile) |
| 463 | msg_handlers = generate_msg_handlers(func_list, inputfile) |
| 464 | handler_registration = generate_handler_registration(func_list) |
| 465 | |
| 466 | jvpp_c_file = open("jvpp_gen.h", 'w') |
| 467 | jvpp_c_file.write(jvpp_c_template.substitute( |
| 468 | inputfile=inputfile, |
Marek Gradzki | 0aaf92f | 2016-05-03 17:05:27 +0200 | [diff] [blame] | 469 | class_cache=class_cache, |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 470 | jni_implementations=jni_impl, |
| 471 | msg_handlers=msg_handlers, |
| 472 | handler_registration=handler_registration)) |
| 473 | jvpp_c_file.flush() |
| 474 | jvpp_c_file.close() |
| 475 | |