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