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