Rajeswara Rao | f6b4e6c | 2017-08-31 17:26:27 +0530 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright 2017-present Open Networking Foundation |
| 3 | ** |
| 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 | |
| 17 | #include <signal.h> |
| 18 | #include <stdio.h> |
| 19 | #include <unistd.h> |
| 20 | #include <sys/time.h> |
| 21 | #include "bal_msg_type.grpc-c.h" |
| 22 | #include "bal_osmsg.grpc-c.h" |
| 23 | #include "bal_model_ids.grpc-c.h" |
| 24 | #include "bal_obj.grpc-c.h" |
| 25 | #include "bal_model_types.grpc-c.h" |
| 26 | #include "bal_errno.grpc-c.h" |
| 27 | #include "bal.grpc-c.h" |
| 28 | |
| 29 | #ifdef BAL_STUB |
| 30 | #include "bal_stub.h" |
| 31 | #else |
| 32 | #include "asfvolt16_driver.h" |
| 33 | #endif |
| 34 | |
| 35 | /* Global varibles */ |
| 36 | balCoreIpInfo coreIpPortInfo; |
| 37 | |
| 38 | static grpc_c_server_t *test_server; |
| 39 | |
| 40 | static void sigint_handler (int x) { |
| 41 | grpc_c_server_destroy(test_server); |
| 42 | exit(0); |
| 43 | } |
| 44 | |
| 45 | /* |
| 46 | * This functions gets invoked whenever bal RPC gets called |
| 47 | */ |
| 48 | void bal__bal_cfg_get_cb(grpc_c_context_t *context) |
| 49 | { |
| 50 | BalCfg *get_cfg; |
| 51 | |
| 52 | /* |
| 53 | * Read incoming message into get_cfg |
| 54 | */ |
| 55 | if (context->gcc_payload) { |
| 56 | context->gcc_stream->read(context, (void **)&get_cfg, 0); |
| 57 | } |
| 58 | |
| 59 | #ifndef BAL_STUB |
| 60 | //asfvolt16_bal_cfg_get(key, get_cfg); |
| 61 | #endif |
| 62 | } |
| 63 | |
| 64 | /* |
| 65 | * This functions gets invoked whenever bal RPC gets called |
| 66 | */ |
| 67 | void bal__bal_cfg_set_cb(grpc_c_context_t *context) |
| 68 | { |
| 69 | BalCfg *set_cfg; |
| 70 | BalErr bal_err; |
| 71 | int ret_val = 0; |
| 72 | |
| 73 | /* |
| 74 | * Read incoming message into set_cfg |
| 75 | */ |
| 76 | if (context->gcc_payload) { |
| 77 | context->gcc_stream->read(context, (void **)&set_cfg, 0); |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * send it to BAL |
| 82 | */ |
| 83 | |
| 84 | bal_err__init(&bal_err); |
| 85 | |
| 86 | bal_err.err= 0; |
| 87 | |
| 88 | /* |
| 89 | * Write reply back to the client |
| 90 | */ |
| 91 | |
| 92 | ret_val = context->gcc_stream->write(context, &bal_err, 0); |
| 93 | if (ret_val != GRPC_C_WRITE_OK) { |
| 94 | if(ret_val == GRPC_C_WRITE_PENDING) { |
| 95 | printf("write is pending, sleep for 10 sec\n", ret_val); |
| 96 | sleep(10); |
| 97 | } |
| 98 | else { |
| 99 | printf("Failed to write\n", ret_val); |
| 100 | exit(1); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | grpc_c_status_t status; |
| 105 | status.gcs_code = 0; |
| 106 | |
| 107 | /* |
| 108 | * Finish response for RPC |
| 109 | */ |
| 110 | if (context->gcc_stream->finish(context, &status)) { |
| 111 | printf("Failed to write status\n"); |
| 112 | exit(1); |
| 113 | } |
| 114 | |
| 115 | #ifdef BAL_STUB |
| 116 | pthread_mutex_lock(&lock); |
| 117 | |
| 118 | struct QNode *temp = newNode(set_cfg->hdr->obj_type, |
| 119 | BAL_ERRNO__BAL_ERR_OK, |
| 120 | set_cfg->device_id); |
| 121 | |
| 122 | /* if(set_cfg->hdr->has_obj_type) |
| 123 | { */ |
| 124 | |
| 125 | switch(set_cfg->hdr->obj_type) |
| 126 | { |
| 127 | case BAL_OBJ_ID__BAL_OBJ_ID_ACCESS_TERMINAL: |
| 128 | { |
| 129 | printf("\n***************************************************\n"); |
| 130 | printf("Received Access Terminal Configuration msg\n"); |
| 131 | printf("***************************************************\n"); |
| 132 | } |
| 133 | break; |
| 134 | case BAL_OBJ_ID__BAL_OBJ_ID_INTERFACE: |
| 135 | { |
| 136 | printf("\n***************************************************\n"); |
| 137 | printf("Received PON Interface Configuration msg\n"); |
| 138 | printf("***************************************************\n"); |
| 139 | temp->intf_id = set_cfg->interface->key->intf_id; |
| 140 | printf("Pon ID = %d\n", temp->intf_id); |
| 141 | } |
| 142 | break; |
| 143 | case BAL_OBJ_ID__BAL_OBJ_ID_SUBSCRIBER_TERMINAL: |
| 144 | { |
| 145 | printf("\n*****************************************************\n"); |
| 146 | printf("Received ONU Activation msg\n"); |
| 147 | printf("*****************************************************\n"); |
| 148 | temp->intf_id = set_cfg->terminal->key->intf_id; |
| 149 | temp->onu_id = set_cfg->terminal->key->sub_term_id; |
| 150 | memset(temp->vendor_id, 0, BAL_DEVICE_STR_LEN); |
| 151 | memcpy(temp->vendor_id, |
| 152 | set_cfg->terminal->data->serial_number->vendor_id, |
| 153 | strlen(set_cfg->terminal->data->serial_number->vendor_id)); |
| 154 | memset(temp->vendor_specific, 0, BAL_DEVICE_STR_LEN); |
| 155 | memcpy(temp->vendor_specific, |
| 156 | set_cfg->terminal->data->serial_number->vendor_specific, |
| 157 | strlen(set_cfg->terminal->data->serial_number->vendor_specific)); |
| 158 | |
| 159 | } |
| 160 | break; |
| 161 | case BAL_OBJ_ID__BAL_OBJ_ID_PACKET: |
| 162 | { |
| 163 | switch(set_cfg->packet->key->packet_send_dest->type) |
| 164 | { |
| 165 | case BAL_DEST_TYPE__BAL_DEST_TYPE_ITU_OMCI_CHANNEL: |
| 166 | { |
| 167 | printf("\n*****************************************************\n"); |
| 168 | printf("Received OMCI msg\n"); |
| 169 | printf("*****************************************************\n"); |
| 170 | temp->intf_id = set_cfg->terminal->key->intf_id; |
| 171 | temp->onu_id = set_cfg->terminal->key->sub_term_id; |
| 172 | } |
| 173 | break; |
| 174 | default: |
| 175 | { |
| 176 | ("\n*****************************************************\n"); |
| 177 | printf("Dest type invalid\n"); |
| 178 | printf("*****************************************************\n"); |
| 179 | } |
| 180 | break; |
| 181 | } |
| 182 | } |
| 183 | break; |
| 184 | default: |
| 185 | { |
| 186 | ("\n*****************************************************\n"); |
| 187 | printf("Received Invalid msg\n"); |
| 188 | printf("*****************************************************\n"); |
| 189 | pthread_mutex_unlock(&lock); |
| 190 | return; |
| 191 | } |
| 192 | break; |
| 193 | } |
| 194 | enQueue(set_cfg->hdr->obj_type, temp); |
| 195 | /* |
| 196 | } |
| 197 | else |
| 198 | { |
| 199 | printf("BALSTUB:Cfg Set recevied without object type"); |
| 200 | } */ |
| 201 | pthread_mutex_unlock(&lock); |
| 202 | sleep(2); |
| 203 | pthread_cond_signal(&cv); |
| 204 | /* |
| 205 | if(BAL_OBJ_ID__BAL_OBJ_ID_INTERFACE == set_cfg->hdr->obj_type) |
| 206 | { |
| 207 | sleep(5); |
| 208 | struct QNode *temp1 = newNode(BAL_OBJ_ID__BAL_OBJ_ID_SUBSCRIBER_TERMINAL, |
| 209 | BAL_ERRNO__BAL_ERR_OK, |
| 210 | set_cfg->device_id); |
| 211 | temp1->intf_id = set_cfg->interface->key->intf_id; |
| 212 | temp1->onu_id = 65535; |
| 213 | printf("sending _onu_discovery_indiaction\n"); |
| 214 | enQueue(BAL_OBJ_ID__BAL_OBJ_ID_SUBSCRIBER_TERMINAL, temp1); |
| 215 | pthread_cond_signal(&cv); |
| 216 | } |
| 217 | */ |
| 218 | #else |
| 219 | if(BAL_OBJ_ID__BAL_OBJ_ID_ACCESS_TERMINAL == set_cfg->hdr->obj_type) |
| 220 | { |
| 221 | sleep(5);//enable this if running with gdb |
| 222 | } |
| 223 | asfvolt16_bal_cfg_set(set_cfg); |
| 224 | #endif |
| 225 | } |
| 226 | |
| 227 | |
| 228 | /* |
| 229 | * This functions gets invoked whenever bal clear RPC gets called |
| 230 | */ |
| 231 | void bal__bal_cfg_clear_cb(grpc_c_context_t *context) |
| 232 | { |
| 233 | BalKey *clear_key; |
| 234 | |
| 235 | /* |
| 236 | * Read incoming message into clear_key |
| 237 | */ |
| 238 | if (context->gcc_payload) { |
| 239 | context->gcc_stream->read(context, (void **)&clear_key, 0); |
| 240 | } |
| 241 | |
| 242 | #ifndef BAL_STUB |
| 243 | asfvolt16_bal_cfg_clear(clear_key); |
| 244 | #endif |
| 245 | } |
| 246 | |
| 247 | |
| 248 | /* |
| 249 | * This functions gets invoked whenever bal Init RPC gets called |
| 250 | */ |
| 251 | void bal__bal_api_init_cb(grpc_c_context_t *context) |
| 252 | { |
| 253 | BalInit *bal_init; |
| 254 | BalErr bal_err; |
| 255 | int ret_val; |
| 256 | |
| 257 | /* |
| 258 | * Read incoming message into set_cfg |
| 259 | */ |
| 260 | if (context->gcc_payload) { |
| 261 | context->gcc_stream->read(context, (void **)&bal_init, 0); |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * send it to BAL |
| 266 | */ |
| 267 | |
| 268 | |
| 269 | ("\n*****************************************************\n"); |
| 270 | printf("Received API Init msg\n"); |
| 271 | printf("*****************************************************\n"); |
| 272 | |
| 273 | bal_err__init(&bal_err); |
| 274 | |
| 275 | bal_err.err= 0; |
| 276 | |
| 277 | /* |
| 278 | * Write reply back to the client |
| 279 | */ |
| 280 | ret_val = context->gcc_stream->write(context, &bal_err, 0); |
| 281 | if (ret_val != GRPC_C_WRITE_OK) { |
| 282 | if(ret_val == GRPC_C_WRITE_PENDING) { |
| 283 | printf("write is pending, sleep for 10 sec\n", ret_val); |
| 284 | sleep(10); |
| 285 | } |
| 286 | else { |
| 287 | printf("Failed to write\n", ret_val); |
| 288 | exit(1); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | grpc_c_status_t status; |
| 293 | status.gcs_code = 0; |
| 294 | |
| 295 | /* |
| 296 | * Finish response for RPC |
| 297 | */ |
| 298 | if (context->gcc_stream->finish(context, &status)) { |
| 299 | printf("Failed to write status\n"); |
| 300 | exit(1); |
| 301 | } |
| 302 | |
| 303 | #ifndef BAL_STUB |
| 304 | asfvolt16_bal_init(bal_init, &coreIpPortInfo); |
| 305 | #else |
| 306 | printf("\nRecevied IP Address == %s \n", bal_init->voltha_adapter_ip_port); |
| 307 | stub_bal_init(bal_init); |
| 308 | #endif |
| 309 | |
| 310 | } |
| 311 | |
| 312 | |
| 313 | /* |
| 314 | * This functions gets invoked whenever bal finish RPC gets called |
| 315 | */ |
| 316 | void bal__bal_api_finish_cb(grpc_c_context_t *context) |
| 317 | { |
| 318 | #if 0 |
| 319 | void *finish_init; |
| 320 | |
| 321 | /* |
| 322 | * Read incoming message into set_cfg |
| 323 | */ |
| 324 | if (context->gcc_payload) { |
| 325 | context->gcc_stream->read(context, (void **)&finish_init); |
| 326 | } |
| 327 | #endif |
| 328 | } |
| 329 | |
| 330 | #if 0 |
| 331 | /* |
| 332 | * This functions gets invoked whenever bal finish RPC gets called |
| 333 | */ |
| 334 | void bal_ind__bal_ind_info_cb(grpc_c_context_t *context) |
| 335 | { |
| 336 | #if 0 |
| 337 | void *finish_init; |
| 338 | |
| 339 | /* |
| 340 | * Read incoming message into set_cfg |
| 341 | */ |
| 342 | if (context->gcc_payload) { |
| 343 | context->gcc_stream->read(context, (void **)&finish_init); |
| 344 | } |
| 345 | #endif |
| 346 | } |
| 347 | #endif |
| 348 | |
| 349 | void bal_ind__bal_acc_term_ind_cb(grpc_c_context_t *context) |
| 350 | { |
| 351 | } |
| 352 | void bal_ind__bal_acc_term_oper_sts_cng_ind_cb(grpc_c_context_t *context) |
| 353 | { |
| 354 | } |
| 355 | void bal_ind__bal_flow_oper_sts_cng_cb(grpc_c_context_t *context) |
| 356 | { |
| 357 | } |
| 358 | void bal_ind__bal_flow_ind_cb(grpc_c_context_t *context) |
| 359 | { |
| 360 | } |
| 361 | void bal_ind__bal_group_ind_cb(grpc_c_context_t *context) |
| 362 | { |
| 363 | } |
| 364 | void bal_ind__bal_iface_oper_sts_cng_cb(grpc_c_context_t *context) |
| 365 | { |
| 366 | } |
| 367 | void bal_ind__bal_iface_los_cb(grpc_c_context_t *context) |
| 368 | { |
| 369 | } |
| 370 | void bal_ind__bal_iface_ind_cb(grpc_c_context_t *context) |
| 371 | { |
| 372 | } |
| 373 | void bal_ind__bal_iface_stat_cb(grpc_c_context_t *context) |
| 374 | { |
| 375 | } |
| 376 | void bal_ind__bal_subs_term_oper_sts_cng_cb(grpc_c_context_t *context) |
| 377 | { |
| 378 | } |
| 379 | void bal_ind__bal_subs_term_discovery_ind_cb(grpc_c_context_t *context) |
| 380 | { |
| 381 | } |
| 382 | void bal_ind__bal_subs_term_alarm_ind_cb(grpc_c_context_t *context) |
| 383 | { |
| 384 | } |
| 385 | void bal_ind__bal_subs_term_dgi_ind_cb(grpc_c_context_t *context) |
| 386 | { |
| 387 | } |
| 388 | void bal_ind__bal_subs_term_ind_cb(grpc_c_context_t *context) |
| 389 | { |
| 390 | } |
| 391 | void bal_ind__bal_tm_queue_ind_info_cb(grpc_c_context_t *context) |
| 392 | { |
| 393 | } |
| 394 | void bal_ind__bal_tm_sched_ind_info_cb(grpc_c_context_t *context) |
| 395 | { |
| 396 | } |
| 397 | void bal_ind__bal_pkt_bearer_channel_rx_ind_cb(grpc_c_context_t *context) |
| 398 | { |
| 399 | } |
| 400 | void bal_ind__bal_pkt_omci_channel_rx_ind_cb(grpc_c_context_t *context) |
| 401 | { |
| 402 | } |
| 403 | void bal_ind__bal_pkt_ieee_oam_channel_rx_ind_cb(grpc_c_context_t *context) |
| 404 | { |
| 405 | } |
| 406 | void bal__bal_api_heartbeat_cb(grpc_c_context_t *context) |
| 407 | { |
| 408 | BalHeartbeat *bal_hb; |
| 409 | BalErr bal_err; |
| 410 | |
| 411 | /* |
| 412 | * Read incoming message into set_cfg |
| 413 | */ |
| 414 | printf("\nRecevied HeartBeat from Adapter\n"); |
| 415 | if (context->gcc_payload) { |
| 416 | context->gcc_stream->read(context, (void **)&bal_hb, 0); |
| 417 | } |
| 418 | |
| 419 | printf("Received Heart Beat msg\n"); |
| 420 | |
| 421 | bal_err__init(&bal_err); |
| 422 | |
| 423 | bal_err.err= 0; |
| 424 | |
| 425 | /* |
| 426 | * Write reply back to the client |
| 427 | */ |
| 428 | if (!context->gcc_stream->write(context, &bal_err, 0)) { |
| 429 | } else { |
| 430 | printf("Failed to write\n"); |
| 431 | exit(1); |
| 432 | } |
| 433 | |
| 434 | grpc_c_status_t status; |
| 435 | status.gcs_code = 0; |
| 436 | |
| 437 | /* |
| 438 | * Finish response for RPC |
| 439 | */ |
| 440 | if (context->gcc_stream->finish(context, &status)) { |
| 441 | printf("Failed to write status\n"); |
| 442 | exit(1); |
| 443 | } |
| 444 | printf("\nSent HeartBeat Response to Adapter\n"); |
| 445 | } |
| 446 | |
| 447 | /* |
| 448 | * Takes socket path as argument |
| 449 | */ |
| 450 | int main (int argc, char **argv) |
| 451 | { |
| 452 | int i = 0; |
| 453 | grpc_c_server_t *server = NULL; |
| 454 | |
| 455 | if (argc < 6) { |
| 456 | fprintf(stderr, "Missing socket path argument\n"); |
| 457 | exit(1); |
| 458 | } |
| 459 | |
| 460 | strcpy(coreIpPortInfo.bal_core_arg1, argv[2] /*, strlen(argv[2])*/); |
| 461 | strcpy(coreIpPortInfo.bal_core_ip_port, argv[3]/*, strlen(argv[3])*/); |
| 462 | strcpy(coreIpPortInfo.bal_core_arg2, argv[4]/*, strlen(argv[4])*/); |
| 463 | strcpy(coreIpPortInfo.bal_shared_lib_ip_port, argv[5]/*, strlen(argv[5])*/); |
| 464 | |
| 465 | signal(SIGINT, sigint_handler); |
| 466 | /* |
| 467 | * Initialize grpc-c library to be used with vanilla gRPC |
| 468 | */ |
| 469 | grpc_c_init(GRPC_THREADS, NULL); |
| 470 | |
| 471 | /* |
| 472 | * Create server object |
| 473 | */ |
| 474 | test_server = grpc_c_server_create(argv[1]); |
| 475 | |
| 476 | if (test_server == NULL) { |
| 477 | printf("Failed to create server\n"); |
| 478 | exit(1); |
| 479 | } |
| 480 | |
| 481 | /* |
| 482 | * Initialize greeter service |
| 483 | */ |
| 484 | printf("\nbal_voltha_app running.....\n"); |
| 485 | bal__service_init(test_server); |
| 486 | |
| 487 | /* |
| 488 | * Start server |
| 489 | */ |
| 490 | grpc_c_server_start(test_server); |
| 491 | |
| 492 | #ifdef BAL_STUB |
| 493 | printf("\nCreating a stub thread\n"); |
| 494 | create_stub_thread(); |
| 495 | #endif |
| 496 | |
| 497 | /* |
| 498 | * Blocks server to wait to completion |
| 499 | */ |
| 500 | grpc_c_server_wait(test_server); |
| 501 | |
| 502 | /* code added for example Makefile to compile grpc-c along with edgecore driver */ |
| 503 | bal__service_init(server); |
| 504 | |
| 505 | } |