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 | # variable length arrays do not need special handling in requests, |
| 214 | # because the length of Java array is known: |
| 215 | field_length = t[2][0] |
| 216 | |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 217 | java_field_name = util.underscore_to_camelcase(c_name) |
| 218 | |
| 219 | struct_setter_template = struct_setter_templates[c_type] |
| 220 | |
| 221 | struct_setters += struct_setter_template.substitute( |
| 222 | c_name=c_name, |
Marek Gradzki | c4cb44c | 2016-05-24 13:32:26 +0200 | [diff] [blame] | 223 | java_name=java_field_name, |
| 224 | field_length=field_length) |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 225 | |
| 226 | jni_impl.append(jni_impl_template.substitute( |
| 227 | inputfile=inputfile, |
| 228 | api_data=util.api_message_to_javadoc(f), |
| 229 | java_name=camel_case_function_name, |
| 230 | c_name_uppercase=f_name_uppercase, |
| 231 | c_name=f_name, |
| 232 | request_class=request_class, |
| 233 | field_identifiers=field_identifiers, |
| 234 | struct_setters=struct_setters, |
| 235 | args=arguments)) |
| 236 | |
| 237 | return "\n".join(jni_impl) |
| 238 | |
| 239 | |
| 240 | dto_field_id_template = Template(""" |
Marek Gradzki | 0aaf92f | 2016-05-03 17:05:27 +0200 | [diff] [blame] | 241 | 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] | 242 | |
| 243 | default_dto_field_setter_template = Template(""" |
| 244 | (*env)->Set${jni_setter}(env, dto, ${java_name}FieldId, mp->${c_name}); |
| 245 | """) |
| 246 | |
Marek Gradzki | fa42e25 | 2016-06-15 16:38:33 +0200 | [diff] [blame] | 247 | variable_length_array_value_template = Template("""mp->${length_var_name}""") |
| 248 | variable_length_array_template = Template("""clib_net_to_host_${length_field_type}(${value})""") |
| 249 | |
Marek Gradzki | 34e7772 | 2016-05-30 21:52:37 +0200 | [diff] [blame] | 250 | u16_dto_field_setter_template = Template(""" |
| 251 | (*env)->Set${jni_setter}(env, dto, ${java_name}FieldId, clib_net_to_host_u16(mp->${c_name})); |
| 252 | """) |
| 253 | |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 254 | u32_dto_field_setter_template = Template(""" |
| 255 | (*env)->Set${jni_setter}(env, dto, ${java_name}FieldId, clib_net_to_host_u32(mp->${c_name})); |
| 256 | """) |
| 257 | |
| 258 | u64_dto_field_setter_template = Template(""" |
| 259 | (*env)->Set${jni_setter}(env, dto, ${java_name}FieldId, clib_net_to_host_u64(mp->${c_name})); |
| 260 | """) |
| 261 | |
| 262 | u8_array_dto_field_setter_template = Template(""" |
Marek Gradzki | c4cb44c | 2016-05-24 13:32:26 +0200 | [diff] [blame] | 263 | jbyteArray ${java_name} = (*env)->NewByteArray(env, ${field_length}); |
| 264 | (*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] | 265 | (*env)->SetObjectField(env, dto, ${java_name}FieldId, ${java_name}); |
| 266 | """) |
| 267 | |
Marek Gradzki | 97d9951 | 2016-06-06 12:36:09 +0200 | [diff] [blame] | 268 | u32_array_dto_field_setter_template = Template(""" |
| 269 | { |
| 270 | jintArray ${java_name} = (*env)->NewIntArray(env, ${field_length}); |
| 271 | jint * ${java_name}ArrayElements = (*env)->GetIntArrayElements(env, ${java_name}, NULL); |
| 272 | unsigned int _i; |
| 273 | for (_i = 0; _i < ${field_length}; _i++) { |
| 274 | ${java_name}ArrayElements[_i] = clib_net_to_host_u32(mp->${c_name}[_i]); |
| 275 | } |
Marek Gradzki | fa42e25 | 2016-06-15 16:38:33 +0200 | [diff] [blame] | 276 | |
| 277 | (*env)->ReleaseIntArrayElements(env, ${java_name}, ${java_name}ArrayElements, 0); |
Marek Gradzki | 97d9951 | 2016-06-06 12:36:09 +0200 | [diff] [blame] | 278 | (*env)->SetObjectField(env, dto, ${java_name}FieldId, ${java_name}); |
| 279 | } |
| 280 | """) |
| 281 | |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 282 | # 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] | 283 | # 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] | 284 | u64_array_dto_field_setter_template = Template(""" |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 285 | { |
Marek Gradzki | c4cb44c | 2016-05-24 13:32:26 +0200 | [diff] [blame] | 286 | jlongArray ${java_name} = (*env)->NewLongArray(env, ${field_length}); |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 287 | jlong * ${java_name}ArrayElements = (*env)->GetLongArrayElements(env, ${java_name}, NULL); |
Marek Gradzki | c4cb44c | 2016-05-24 13:32:26 +0200 | [diff] [blame] | 288 | unsigned int _i; |
| 289 | for (_i = 0; _i < ${field_length}; _i++) { |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 290 | ${java_name}ArrayElements[_i] = clib_net_to_host_u64(mp->${c_name}[_i]); |
| 291 | } |
Marek Gradzki | fa42e25 | 2016-06-15 16:38:33 +0200 | [diff] [blame] | 292 | |
| 293 | (*env)->ReleaseLongArrayElements(env, ${java_name}, ${java_name}ArrayElements, 0); |
Marek Gradzki | c4cb44c | 2016-05-24 13:32:26 +0200 | [diff] [blame] | 294 | (*env)->SetObjectField(env, dto, ${java_name}FieldId, ${java_name}); |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 295 | } |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 296 | """) |
| 297 | |
| 298 | dto_field_setter_templates = {'u8': default_dto_field_setter_template, |
Marek Gradzki | 34e7772 | 2016-05-30 21:52:37 +0200 | [diff] [blame] | 299 | 'u16': u16_dto_field_setter_template, |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 300 | 'u32': u32_dto_field_setter_template, |
| 301 | 'i32': u32_dto_field_setter_template, |
| 302 | 'u64': u64_dto_field_setter_template, |
Marek Gradzki | 34e7772 | 2016-05-30 21:52:37 +0200 | [diff] [blame] | 303 | 'f64': default_dto_field_setter_template, #fixme |
Marek Gradzki | 97d9951 | 2016-06-06 12:36:09 +0200 | [diff] [blame] | 304 | 'u8[]': u8_array_dto_field_setter_template, |
| 305 | 'u32[]': u32_array_dto_field_setter_template, |
| 306 | 'u64[]': u64_array_dto_field_setter_template |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 307 | } |
| 308 | |
Tibor Sirovatka | 42bb61f | 2016-05-18 14:54:50 +0200 | [diff] [blame] | 309 | # code fragment for checking result of the operation before sending request reply |
| 310 | callback_err_handler_template = Template(""" |
| 311 | // for negative result don't send callback message but send error callback |
| 312 | if (mp->retval<0) { |
| 313 | CallOnError("${handler_name}",mp->context,mp->retval); |
| 314 | return; |
| 315 | } |
| 316 | if (mp->retval == VNET_API_ERROR_IN_PROGRESS) { |
| 317 | clib_warning("Result in progress"); |
| 318 | return; |
| 319 | } |
| 320 | """) |
| 321 | |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 322 | msg_handler_template = Template(""" |
| 323 | /** |
| 324 | * Handler for ${handler_name} vpe.api message. |
| 325 | * Generated based on $inputfile preparsed data: |
| 326 | $api_data |
| 327 | */ |
| 328 | static void vl_api_${handler_name}_t_handler (vl_api_${handler_name}_t * mp) |
| 329 | { |
| 330 | vppjni_main_t * jm = &vppjni_main; |
| 331 | JNIEnv *env = jm->jenv; |
Tibor Sirovatka | 42bb61f | 2016-05-18 14:54:50 +0200 | [diff] [blame] | 332 | $err_handler |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 333 | |
Marek Gradzki | 0aaf92f | 2016-05-03 17:05:27 +0200 | [diff] [blame] | 334 | jmethodID constructor = (*env)->GetMethodID(env, ${class_ref_name}Class, "<init>", "()V"); |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 335 | jmethodID callbackMethod = (*env)->GetMethodID(env, jm->callbackClass, "on${dto_name}", "(Lorg/openvpp/jvpp/dto/${dto_name};)V"); |
| 336 | |
Marek Gradzki | 0aaf92f | 2016-05-03 17:05:27 +0200 | [diff] [blame] | 337 | jobject dto = (*env)->NewObject(env, ${class_ref_name}Class, constructor); |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 338 | $dto_setters |
| 339 | (*env)->CallVoidMethod(env, jm->callback, callbackMethod, dto); |
| 340 | }""") |
| 341 | |
| 342 | def generate_msg_handlers(func_list, inputfile): |
| 343 | handlers = [] |
| 344 | for f in func_list: |
| 345 | handler_name = f['name'] |
| 346 | dto_name = util.underscore_to_camelcase_upper(handler_name) |
Marek Gradzki | 0aaf92f | 2016-05-03 17:05:27 +0200 | [diff] [blame] | 347 | ref_name = util.underscore_to_camelcase(handler_name) |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 348 | |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 349 | if is_manually_generated(handler_name) or util.is_ignored(handler_name): |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 350 | continue |
| 351 | |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 352 | if not util.is_reply(dto_name) and not util.is_notification(handler_name): |
| 353 | continue |
| 354 | |
| 355 | if util.is_notification(handler_name): |
| 356 | dto_name = util.add_notification_suffix(dto_name) |
| 357 | ref_name = util.add_notification_suffix(ref_name) |
| 358 | |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 359 | dto_setters = '' |
Tibor Sirovatka | 42bb61f | 2016-05-18 14:54:50 +0200 | [diff] [blame] | 360 | err_handler = '' |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 361 | # dto setters |
Marek Gradzki | c4cb44c | 2016-05-24 13:32:26 +0200 | [diff] [blame] | 362 | 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] | 363 | c_type = t[0] |
| 364 | jni_type = t[1] |
| 365 | c_name = t[2] |
Marek Gradzki | fa42e25 | 2016-06-15 16:38:33 +0200 | [diff] [blame] | 366 | field_length = t[3][0] |
| 367 | |
| 368 | # check if we are processing variable length array |
| 369 | if t[3][1]: |
| 370 | length_var_name = t[3][0] |
| 371 | length_field_type = f['c_types'][f['args'].index(length_var_name)] |
| 372 | field_length = variable_length_array_value_template.substitute(length_var_name=length_var_name) |
| 373 | if length_field_type != 'u8': # we need net to host conversion: |
| 374 | field_length = variable_length_array_template.substitute( |
| 375 | length_field_type=length_field_type, value=field_length) |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 376 | |
Tibor Sirovatka | 42bb61f | 2016-05-18 14:54:50 +0200 | [diff] [blame] | 377 | # for retval don't generate setters and generate retval check |
| 378 | if util.is_retval_field(c_name): |
| 379 | err_handler = callback_err_handler_template.substitute( |
| 380 | handler_name=handler_name |
| 381 | ) |
| 382 | continue |
| 383 | |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 384 | java_field_name = util.underscore_to_camelcase(c_name) |
| 385 | jni_signature = util.jni_2_signature_mapping[jni_type] |
| 386 | jni_setter = util.jni_field_accessors[jni_type] |
| 387 | |
| 388 | dto_setters += dto_field_id_template.substitute( |
| 389 | java_name=java_field_name, |
Marek Gradzki | 0aaf92f | 2016-05-03 17:05:27 +0200 | [diff] [blame] | 390 | class_ref_name=ref_name, |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 391 | jni_signature=jni_signature) |
| 392 | |
| 393 | dto_setter_template = dto_field_setter_templates[c_type] |
| 394 | |
| 395 | dto_setters += dto_setter_template.substitute( |
| 396 | java_name=java_field_name, |
| 397 | jni_signature=jni_signature, |
| 398 | c_name=c_name, |
Marek Gradzki | c4cb44c | 2016-05-24 13:32:26 +0200 | [diff] [blame] | 399 | jni_setter=jni_setter, |
| 400 | field_length=field_length) |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 401 | |
| 402 | handlers.append(msg_handler_template.substitute( |
Tibor Sirovatka | 42bb61f | 2016-05-18 14:54:50 +0200 | [diff] [blame] | 403 | inputfile=inputfile, |
| 404 | api_data=util.api_message_to_javadoc(f), |
| 405 | handler_name=handler_name, |
| 406 | dto_name=dto_name, |
| 407 | class_ref_name=ref_name, |
| 408 | dto_setters=dto_setters, |
| 409 | err_handler=err_handler)) |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 410 | |
| 411 | return "\n".join(handlers) |
| 412 | |
| 413 | |
| 414 | handler_registration_template = Template("""_(${upercase_name}, ${name}) \\ |
| 415 | """) |
| 416 | |
| 417 | |
| 418 | def generate_handler_registration(func_list): |
| 419 | handler_registration = ["#define foreach_vpe_api_msg \\\n"] |
| 420 | for f in func_list: |
| 421 | name = f['name'] |
| 422 | camelcase_name = util.underscore_to_camelcase(f['name']) |
| 423 | |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 424 | 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] | 425 | continue |
| 426 | |
| 427 | handler_registration.append(handler_registration_template.substitute( |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 428 | name=name, |
| 429 | upercase_name=name.upper())) |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 430 | |
| 431 | return "".join(handler_registration) |
| 432 | |
Marek Gradzki | 0aaf92f | 2016-05-03 17:05:27 +0200 | [diff] [blame] | 433 | |
| 434 | jvpp_c_template = Template("""/** |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 435 | * This file contains JNI bindings for jvpp Java API. |
| 436 | * It was generated by jvpp_c_gen.py based on $inputfile |
| 437 | * (python representation of vpe.api generated by vppapigen). |
| 438 | */ |
| 439 | |
Tibor Sirovatka | 42bb61f | 2016-05-18 14:54:50 +0200 | [diff] [blame] | 440 | void CallOnError(const char* call, int context, int retval); |
| 441 | |
Marek Gradzki | 0aaf92f | 2016-05-03 17:05:27 +0200 | [diff] [blame] | 442 | // JAVA class reference cache |
| 443 | $class_cache |
| 444 | |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 445 | // JNI bindings |
| 446 | $jni_implementations |
| 447 | |
| 448 | // Message handlers |
| 449 | $msg_handlers |
| 450 | |
| 451 | // Registration of message handlers in vlib |
| 452 | $handler_registration |
| 453 | """) |
| 454 | |
| 455 | def generate_jvpp(func_list, inputfile): |
| 456 | """ Generates jvpp C file """ |
| 457 | print "Generating jvpp C" |
| 458 | |
Marek Gradzki | 0aaf92f | 2016-05-03 17:05:27 +0200 | [diff] [blame] | 459 | class_cache = generate_class_cache(func_list) |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 460 | jni_impl = generate_jni_impl(func_list, inputfile) |
| 461 | msg_handlers = generate_msg_handlers(func_list, inputfile) |
| 462 | handler_registration = generate_handler_registration(func_list) |
| 463 | |
| 464 | jvpp_c_file = open("jvpp_gen.h", 'w') |
| 465 | jvpp_c_file.write(jvpp_c_template.substitute( |
| 466 | inputfile=inputfile, |
Marek Gradzki | 0aaf92f | 2016-05-03 17:05:27 +0200 | [diff] [blame] | 467 | class_cache=class_cache, |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 468 | jni_implementations=jni_impl, |
| 469 | msg_handlers=msg_handlers, |
| 470 | handler_registration=handler_registration)) |
| 471 | jvpp_c_file.flush() |
| 472 | jvpp_c_file.close() |
| 473 | |