bal packet indication bug fix, heartbeat and reboot procedures

Change-Id: If798c1802c488dd6b4a73595c72245b7d5daa8d7
diff --git a/device_simulator/bal_stub.c b/device_simulator/bal_stub.c
index 522c56a..78b4823 100644
--- a/device_simulator/bal_stub.c
+++ b/device_simulator/bal_stub.c
@@ -31,7 +31,7 @@
 
 char *voltha_ip_and_port = NULL;
 
-void *stub_thread(void *v) 
+void *stub_thread(void *v)
 {
    int status;
    pthread_mutex_lock(&lock);
@@ -65,7 +65,7 @@
                bal_access_terminal_key__init(&accTermKey);
                accTermKey.has_access_term_id = 1;
                accTermKey.access_term_id = 1;
-               
+
                BalAccessTerminalIndData data;
                memset(&data, 0, sizeof(BalAccessTerminalIndData));
                bal_access_terminal_ind_data__init(&data);
@@ -205,11 +205,12 @@
                BalPacketItuOmciChannelRx balomciresp;
                memset(&balomciresp, 0, sizeof(BalPacketItuOmciChannelRx));
                bal_packet_itu_omci_channel_rx__init(&balomciresp);
-                                              
+
                BalPacketKey balomcirespkey;
                memset(&balomcirespkey, 0, sizeof(BalPacketKey));
-               bal_packet_key__init(&balomcirespkey); 
+               bal_packet_key__init(&balomcirespkey);
                balomciresp.key = &balomcirespkey;
+               balIndCfg.balomciresp = &balomciresp;
 
                BalDest balomcirespkeydest;
                memset(&balomcirespkeydest, 0, sizeof(BalDest));
@@ -245,14 +246,14 @@
    return NULL;
 }
 
-void create_stub_thread() 
+void create_stub_thread()
 {
    pthread_t threadId = 0;
 
    /* create shared queue */
    shared_queue = createQueue();
 
-   pthread_create(&threadId, NULL, stub_thread, NULL);      
+   pthread_create(&threadId, NULL, stub_thread, NULL);
 
 }
 
@@ -276,7 +277,7 @@
       memcpy(temp->device_id, device_id, strlen(device_id));
    }
    temp->next = NULL;
-   return temp; 
+   return temp;
 }
 
 /* The function to add data to shared_queue - Add end of the queue */
@@ -320,7 +321,45 @@
 
 void stub_bal_init(BalInit *bal_init)
 {
-    client = grpc_c_client_init(bal_init->voltha_adapter_ip_port, "bal_client", NULL);
+    char *ip_and_port = NULL;
+    ip_and_port = bal_init->voltha_adapter_ip_port;
+    client = grpc_c_client_init(ip_and_port, "bal_client", NULL);
+}
+
+void stub_bal_stats_get(BalInterfaceStatData *statData)
+{
+    printf("Bal Stub - Get Stats In BalStubs : Got all the statistics\n");
+    statData->has_rx_bytes = 1;
+    statData->has_rx_packets=1;
+    statData->has_rx_ucast_packets=1;
+    statData->has_rx_mcast_packets=1;
+    statData->has_rx_bcast_packets=1;
+    statData->has_rx_error_packets=1;
+    statData->has_rx_unknown_protos=1;
+    statData->has_tx_bytes = 1;
+    statData->has_tx_packets=1;
+    statData->has_tx_ucast_packets=1;
+    statData->has_tx_mcast_packets=1;
+    statData->has_tx_bcast_packets=1;
+    statData->has_tx_error_packets=1;
+    statData->has_rx_crc_errors=1;
+    statData->has_bip_errors=1;
+
+    statData->rx_bytes = 1000;          /**< RFC 2233 */
+    statData->rx_packets = 100;        /**< RFC 1213 ucast + none-ucast */
+    statData->rx_ucast_packets = 5;  /**< RFC 2233 */
+    statData->rx_mcast_packets = 10;  /**< RFC 2233 */
+    statData->rx_bcast_packets = 15;  /**< RFC 2233 */
+    statData->rx_error_packets = 20;  /**< RFC 1213 */
+    statData->rx_unknown_protos = 45; /**< RFC 1213 */
+    statData->tx_bytes = 2000;          /**< RFC 2233 */
+    statData->tx_packets = 190;        /**< RFC 1213 ucast + none-ucast */
+    statData->tx_ucast_packets = 30;  /**< RFC 2233 */
+    statData->tx_mcast_packets = 50;  /**< RFC 2233 */
+    statData->tx_bcast_packets = 80;  /**< RFC 2233 */
+    statData->tx_error_packets = 40;  /**< RFC 1213 */
+    statData->rx_crc_errors = 5;     /**< Received packets with CRC error. */
+    statData->bip_errors = 15;
 }
 
 #endif
diff --git a/device_simulator/voltha_bal_driver.c b/device_simulator/voltha_bal_driver.c
index d919ba3..afdb73e 100755
--- a/device_simulator/voltha_bal_driver.c
+++ b/device_simulator/voltha_bal_driver.c
@@ -31,17 +31,175 @@
 #include "asfvolt16_driver.h"
 #endif
 
+#include <unistd.h>
+#include <sys/reboot.h>
+
 /* Global varibles */
 balCoreIpInfo coreIpPortInfo;
 
 static grpc_c_server_t *test_server;
 
-static void sigint_handler (int x) { 
+static void sigint_handler (int x) {
    grpc_c_server_destroy(test_server);
    exit(0);
 }
 
 /*
+ * This functions gets invoked whenever bal Heartbeat RPC gets called
+ */
+void bal__bal_api_heartbeat_cb(grpc_c_context_t *context)
+{
+   BalHeartbeat *bal_hb;
+   BalErr bal_err;
+
+   /*
+    * Read incoming message into set_cfg
+    */
+   printf("\nRecevied HeartBeat from Adapter\n");
+   if (context->gcc_payload) {
+	context->gcc_stream->read(context, (void **)&bal_hb, 0);
+   }
+
+   printf("Received Heart Beat msg\n");
+
+   bal_err__init(&bal_err);
+
+   bal_err.err= 0;
+
+   /*
+    * Write reply back to the client
+    */
+    if (!context->gcc_stream->write(context, &bal_err, 0)) {
+    } else {
+              printf("Failed to write\n");
+	      exit(1);
+    }
+
+    grpc_c_status_t status;
+    status.gcs_code = 0;
+
+    /*
+     * Finish response for RPC
+     */
+    if (context->gcc_stream->finish(context, &status)) {
+	printf("Failed to write status\n");
+	exit(1);
+    }
+
+    sleep(1);
+    printf("\nSent HeartBeat Response to Adapter\n");
+}
+
+/*
+ * This functions gets invoked whenever Bal reboot gets called
+ */
+void bal__bal_api_reboot_cb(grpc_c_context_t *context)
+{
+   BalReboot *read_device;
+   BalErr bal_err;
+   /*
+    * Read incoming message into get_cfg
+    */
+    if (context->gcc_payload) {
+       context->gcc_stream->read(context, (void **)&read_device, 0);
+    }
+
+   printf("Bal Server - Reboot : ======Entering Function Reboot ==============================\n");
+   printf("Bal Server - Reboot : Device ID is %s\n",read_device->device_id);
+
+   sync();
+   reboot(RB_AUTOBOOT);
+
+   /* system("init 6"); */
+   /*
+    * send it to BAL
+    */
+
+   bal_err__init(&bal_err);
+
+   bal_err.err= 0;
+
+   /*
+    * Write reply back to the client
+    */
+   if (!context->gcc_stream->write(context, &bal_err, 0)) {
+   } else {
+      printf("Bal Server - Reboot Failed to write\n");
+      exit(1);
+   }
+
+   grpc_c_status_t status;
+   status.gcs_code = 0;
+
+    /*
+    * Finish response for RPC
+    */
+    if (context->gcc_stream->finish(context, &status)) {
+        printf("Failed to write status\n");
+        exit(1);
+    }
+}
+
+/*
+ * This functions gets invoked whenever Bal Stats gets called
+ */
+void bal__bal_cfg_stat_get_cb(grpc_c_context_t *context)
+{
+    BalInterfaceKey *read_stats;
+
+    /*
+    * Read incoming message into get_cfg
+    */
+    if (context->gcc_payload) {
+       context->gcc_stream->read(context, (void **)&read_stats, 0);
+    }
+
+    printf("Bal Server - Get Stats :======Entering Function Get Stats ============\n");
+    printf("Bal Server - Get Stats :NNI port is %d\n",read_stats->intf_id);
+
+    BalInterfaceStat get_stats;
+    memset(&get_stats, 0, sizeof(BalInterfaceStat));
+    bal_interface_stat__init(&get_stats);
+
+    BalInterfaceStatData stat_data;
+    memset(&stat_data, 0, sizeof(BalInterfaceStatData));
+    bal_interface_stat_data__init(&stat_data);
+
+#ifndef BAL_STUB
+    /* Interface Type, Interface ID
+       stat_data - Statistics Data */
+    asfvolt16_bal_stats_get(read_stats->intf_type, read_stats->intf_id, &stat_data);
+    printf("Bal Server - Get Stats Not In BalStubs : Got all the statistics\n");
+#else
+    stub_bal_stats_get(&stat_data);
+    printf("Bal Server - Get Stats In BalStubs : Got all the statistics\n");
+#endif
+
+    get_stats.data = &stat_data;
+
+    if (!context->gcc_stream->write(context, &get_stats, 0)) {
+	   printf("Successfully Written Stats\n");
+    } else {
+       printf("Stats Failed to write\n");
+	   exit(1);
+    }
+
+    grpc_c_status_t status;
+    status.gcs_code = 0;
+
+    /*
+    * Finish response for RPC
+    */
+    if (context->gcc_stream->finish(context, &status)) {
+        printf("Failed to write status\n");
+        exit(1);
+    }
+
+    sleep(1);
+    printf("============ Returning from Stats Function============\n");
+}
+
+/*
  * This functions gets invoked whenever bal RPC gets called
  */
 void bal__bal_cfg_get_cb(grpc_c_context_t *context)
@@ -87,16 +245,17 @@
    /*
     * Write reply back to the client
     */
- 
+
    ret_val = context->gcc_stream->write(context, &bal_err, 0);
    if (ret_val != GRPC_C_WRITE_OK) {
       if(ret_val == GRPC_C_WRITE_PENDING) {
-         printf("write is pending, sleep for 10 sec: %d\n", ret_val);
+         printf("write is pending, sleep for 10 sec %d \n", ret_val);
          sleep(10);
       }
       else {
-         printf("Failed to write: %d\n", ret_val);
-         exit(1);
+         printf("Failed to write %d \n", ret_val);
+         printf("write is pending, sleep for 10 sec: %d\n", ret_val);
+         sleep(10);
       }
    }
 
@@ -114,8 +273,8 @@
 #ifdef BAL_STUB
    pthread_mutex_lock(&lock);
 
-   struct QNode *temp = newNode(set_cfg->hdr->obj_type, 
-         BAL_ERRNO__BAL_ERR_OK, 
+   struct QNode *temp = newNode(set_cfg->hdr->obj_type,
+         BAL_ERRNO__BAL_ERR_OK,
          set_cfg->device_id);
 
 /*   if(set_cfg->hdr->has_obj_type)
@@ -125,56 +284,63 @@
       {
          case BAL_OBJ_ID__BAL_OBJ_ID_ACCESS_TERMINAL:
             {
-               printf("\n***************************************************\n");  
+               printf("\n***************************************************\n");
                printf("Received Access Terminal Configuration msg\n");
-               printf("***************************************************\n");  
+               printf("***************************************************\n");
             }
             break;
          case BAL_OBJ_ID__BAL_OBJ_ID_INTERFACE:
             {
-               printf("\n***************************************************\n");  
+               printf("\n***************************************************\n");
                printf("Received PON Interface Configuration msg\n");
-               printf("***************************************************\n");  
+               printf("***************************************************\n");
                temp->intf_id = set_cfg->interface->key->intf_id;
                printf("Pon ID = %d\n", temp->intf_id);
             }
             break;
          case BAL_OBJ_ID__BAL_OBJ_ID_SUBSCRIBER_TERMINAL:
             {
-          printf("\n*****************************************************\n");  
+          printf("\n*****************************************************\n");
           printf("Received ONU Activation msg\n");
-          printf("*****************************************************\n");  
+          printf("*****************************************************\n");
           temp->intf_id = set_cfg->terminal->key->intf_id;
           temp->onu_id = set_cfg->terminal->key->sub_term_id;
           memset(temp->vendor_id, 0, BAL_DEVICE_STR_LEN);
-          memcpy(temp->vendor_id, 
-                set_cfg->terminal->data->serial_number->vendor_id, 
+          memcpy(temp->vendor_id,
+                set_cfg->terminal->data->serial_number->vendor_id,
                 strlen(set_cfg->terminal->data->serial_number->vendor_id));
           memset(temp->vendor_specific, 0, BAL_DEVICE_STR_LEN);
-          memcpy(temp->vendor_specific, 
-                set_cfg->terminal->data->serial_number->vendor_specific, 
+          memcpy(temp->vendor_specific,
+                set_cfg->terminal->data->serial_number->vendor_specific,
                 strlen(set_cfg->terminal->data->serial_number->vendor_specific));
 
             }
             break;
+         case BAL_OBJ_ID__BAL_OBJ_ID_TM_SCHED:
+            {
+          printf("\n*****************************************************\n");
+          printf("Received TM schedule msg\n");
+          printf("*****************************************************\n");
+            }
+            break;
     case BAL_OBJ_ID__BAL_OBJ_ID_PACKET:
        {
           switch(set_cfg->packet->key->packet_send_dest->type)
           {
              case BAL_DEST_TYPE__BAL_DEST_TYPE_ITU_OMCI_CHANNEL:
                 {
-                   printf("\n*****************************************************\n");  
+                   printf("\n*****************************************************\n");
                    printf("Received OMCI msg\n");
-                   printf("*****************************************************\n");  
+                   printf("*****************************************************\n");
                    temp->intf_id = set_cfg->terminal->key->intf_id;
                    temp->onu_id = set_cfg->terminal->key->sub_term_id;
                 }
                 break;
              default:
                 {
-                   ("\n*****************************************************\n");  
+                   ("\n*****************************************************\n");
                    printf("Dest type invalid\n");
-                   printf("*****************************************************\n");  
+                   printf("*****************************************************\n");
                 }
                 break;
           }
@@ -182,9 +348,9 @@
        break;
     default:
        {
-          ("\n*****************************************************\n");  
-          printf("Received Invalid msg\n");
-          printf("*****************************************************\n");  
+          ("\n*****************************************************\n");
+          printf("Received Invalid msg type === %d \n", set_cfg->hdr->obj_type);
+          printf("*****************************************************\n");
                     pthread_mutex_unlock(&lock);
                     return;
        }
@@ -198,19 +364,19 @@
       printf("BALSTUB:Cfg Set recevied without object type");
    } */
    pthread_mutex_unlock(&lock);
-   sleep(2);   
+   sleep(2);
    pthread_cond_signal(&cv);
 /*
    if(BAL_OBJ_ID__BAL_OBJ_ID_INTERFACE == set_cfg->hdr->obj_type)
    {
-      sleep(5); 
-      struct QNode *temp1 = newNode(BAL_OBJ_ID__BAL_OBJ_ID_SUBSCRIBER_TERMINAL, 
-            BAL_ERRNO__BAL_ERR_OK, 
+      sleep(5);
+      struct QNode *temp1 = newNode(BAL_OBJ_ID__BAL_OBJ_ID_SUBSCRIBER_TERMINAL,
+            BAL_ERRNO__BAL_ERR_OK,
             set_cfg->device_id);
       temp1->intf_id = set_cfg->interface->key->intf_id;
       temp1->onu_id = 65535;
       printf("sending _onu_discovery_indiaction\n");
-      enQueue(BAL_OBJ_ID__BAL_OBJ_ID_SUBSCRIBER_TERMINAL, temp1); 
+      enQueue(BAL_OBJ_ID__BAL_OBJ_ID_SUBSCRIBER_TERMINAL, temp1);
       pthread_cond_signal(&cv);
    }
 */
@@ -232,7 +398,7 @@
    BalKey *clear_key;
 
    /*
-    * Read incoming message into clear_key 
+    * Read incoming message into clear_key
     */
    if (context->gcc_payload) {
       context->gcc_stream->read(context, (void **)&clear_key, 0);
@@ -265,9 +431,9 @@
     */
 
 
-   ("\n*****************************************************\n");  
+   ("\n*****************************************************\n");
    printf("Received API Init msg\n");
-   printf("*****************************************************\n");  
+   printf("*****************************************************\n");
 
    bal_err__init(&bal_err);
 
@@ -279,12 +445,13 @@
    ret_val = context->gcc_stream->write(context, &bal_err, 0);
    if (ret_val != GRPC_C_WRITE_OK) {
       if(ret_val == GRPC_C_WRITE_PENDING) {
-         printf("write is pending, sleep for 10 sec: %d\n", ret_val);
+         printf("write is pending, sleep for 10 sec %d \n", ret_val);
          sleep(10);
       }
       else {
-         printf("Failed to write: %d\n", ret_val);
-         exit(1);
+         printf("Failed to write %d \n", ret_val);
+         printf("write is pending, sleep for 10 sec: %d\n", ret_val);
+         sleep(10);
       }
    }
 
@@ -403,53 +570,11 @@
 {
 }
 
-#if 0
-void bal__bal_api_heartbeat_cb(grpc_c_context_t *context)
-{
-   BalHeartbeat *bal_hb;
-   BalErr bal_err;
-
-   /*
-    * Read incoming message into set_cfg
-    */
-   printf("\nRecevied HeartBeat from Adapter\n");
-   if (context->gcc_payload) {
-	context->gcc_stream->read(context, (void **)&bal_hb, 0);
-   }
-
-   printf("Received Heart Beat msg\n");
-
-   bal_err__init(&bal_err);
-
-   bal_err.err= 0;
-
-   /*
-    * Write reply back to the client
-    */
-    if (!context->gcc_stream->write(context, &bal_err, 0)) {
-    } else {
-              printf("Failed to write\n");
-	      exit(1);
-    }
-
-    grpc_c_status_t status;
-    status.gcs_code = 0;
-
-    /*
-     * Finish response for RPC
-     */
-    if (context->gcc_stream->finish(context, &status)) {
-	printf("Failed to write status\n");
-	exit(1);
-    }
-   printf("\nSent HeartBeat Response to Adapter\n");
-}
-#endif
 
 /*
  * Takes socket path as argument
  */
-int main (int argc, char **argv) 
+int main (int argc, char **argv)
 {
    int i = 0;
    grpc_c_server_t *server = NULL;
@@ -483,7 +608,7 @@
    /*
     * Initialize greeter service
     */
-   printf("\nbal_voltha_app running.....\n");
+   printf("\nvoltha_bal_driver running.....\n");
    bal__service_init(test_server);
 
    /*