VOL-513: Support ASFVOLT16 Adapter in Swarm Mode -VOL-467 Phase 1 - Polling Mode

Follow up changes to do the following.

1) Remove reduntant spaces in the code
2) Change the grpc-c stream write mechanism from non-blocking to blocking
3) Increase the bal_indication queue size from 25 to 1000
4) Compilation warning fixes

Change-Id: I3917d526d4db59601ca9d8468f66f4219d735e17
diff --git a/device_simulator/voltha_bal_driver.c b/device_simulator/voltha_bal_driver.c
index 83a83ef..c430b64 100755
--- a/device_simulator/voltha_bal_driver.c
+++ b/device_simulator/voltha_bal_driver.c
@@ -96,7 +96,7 @@
    /*
     * Write reply back to the client
     */
-   ret_val = context->gcc_stream->write(context, &bal_reboot, 0);
+   ret_val = context->gcc_stream->write(context, &bal_reboot, -1);
    is_grpc_write_pending(ret_val);
 
    grpc_c_status_t status;
@@ -142,7 +142,7 @@
    /*
     * Write reply back to the client
     */
-   ret_val = context->gcc_stream->write(context, &bal_err, 0);
+   ret_val = context->gcc_stream->write(context, &bal_err, -1);
    is_grpc_write_pending(ret_val);
 
    grpc_c_status_t status;
@@ -204,7 +204,7 @@
     get_stats.data = &stat_data;
     get_stats.key = &stat_key;
 
-    ret_val = context->gcc_stream->write(context, &get_stats, 0);
+    ret_val = context->gcc_stream->write(context, &get_stats, -1);
     is_grpc_write_pending(ret_val);
 
     grpc_c_status_t status;
@@ -266,7 +266,7 @@
     * Write reply back to the client
     */
 
-   ret_val = context->gcc_stream->write(context, &bal_err, 0);
+   ret_val = context->gcc_stream->write(context, &bal_err, -1);
    is_grpc_write_pending(ret_val);
 
    grpc_c_status_t status;
@@ -428,7 +428,7 @@
    /*
     * Write reply back to the client
     */
-   ret_val = context->gcc_stream->write(context, &bal_err, 0);
+   ret_val = context->gcc_stream->write(context, &bal_err, -1);
    is_grpc_write_pending(ret_val);
 
    grpc_c_status_t status;
@@ -495,15 +495,15 @@
 
 void bal_get_ind__free_mem_group_ind(BalIndications *balIndCfg)
 {
-   int i = 0;
+   unsigned int i = 0;
    free(balIndCfg->group_ind->data->flows->val);
    free(balIndCfg->group_ind->data->flows);
    for (i = 0; i < balIndCfg->group_ind->data->members->n_val; i++)
    {
       free(balIndCfg->group_ind->data->members->val[i]->queue);
       free(balIndCfg->group_ind->data->members->val[i]->action);
+      free(balIndCfg->group_ind->data->members->val[i]);
    }
-   free(balIndCfg->group_ind->data->members->val);
    free(balIndCfg->group_ind->data->members);
    free(balIndCfg->group_ind->data);
    free(balIndCfg->group_ind->key);
@@ -525,7 +525,7 @@
 {
    free(balIndCfg->interface_los->data);
    free(balIndCfg->interface_los->hdr);
-   free(balIndCfg->interface_los); 
+   free(balIndCfg->interface_los);
    free(balIndCfg);
 }
 
@@ -707,7 +707,7 @@
        case BAL_INDICATIONS__U_ACCESS_TERM_IND:
           bal_get_ind__free_mem_access_term_ind(balIndCfg);
           break;
- 
+
        case BAL_INDICATIONS__U_ACCESS_TERM_IND_OP_STATE:
           bal_get_ind__free_mem_access_term_ind_op_state(balIndCfg);
           break;
@@ -777,7 +777,7 @@
           break;
 
        default:
-          ASFVOLT_LOG(ASFVOLT_ERROR, "Unknown case %lu\n", balIndCfg->u_case);
+          ASFVOLT_LOG(ASFVOLT_ERROR, "Unknown case %u\n", balIndCfg->u_case);
           break;
     }
 }
@@ -803,7 +803,7 @@
    pthread_mutex_lock(&bal_ind_queue_lock);
    node = get_bal_indication_node();
    pthread_mutex_unlock(&bal_ind_queue_lock);
-     
+
    if(node != NULL)
    {
       bal_indication = node->bal_indication;
@@ -823,7 +823,7 @@
     * Write reply back to the client
     */
 
-   ret_val = context->gcc_stream->write(context, bal_indication, 0);
+   ret_val = context->gcc_stream->write(context, bal_indication, -1);
    is_grpc_write_pending(ret_val);
 
    grpc_c_status_t status;
@@ -836,7 +836,7 @@
    {
       ASFVOLT_LOG(ASFVOLT_ERROR, "Failed to write status\n");
    }
-   
+
    /*Free memory for 'bal_indication' and 'node'*/
    if (bal_indication->ind_present)
    {
diff --git a/src/asfvolt16_driver.h b/src/asfvolt16_driver.h
index 6871e2b..ab24309 100755
--- a/src/asfvolt16_driver.h
+++ b/src/asfvolt16_driver.h
@@ -26,6 +26,8 @@
 #include "bal_errno.grpc-c.h"
 #include "bal.grpc-c.h"
 
+#include <stdio.h>
+
 #define ASFVOLT_ERROR 1
 #define ASFVOLT_INFO  2
 #define ASFVOLT_DEBUG 3
diff --git a/src/bal_indications_hdlr.c b/src/bal_indications_hdlr.c
index b807c73..8066d18 100755
--- a/src/bal_indications_hdlr.c
+++ b/src/bal_indications_hdlr.c
@@ -141,7 +141,7 @@
 
       list_node *bal_indication_node = malloc(sizeof(list_node));
       bal_indication_node->bal_indication = balIndCfg;
-      
+
       pthread_mutex_lock(&bal_ind_queue_lock);
       add_bal_indication_node(bal_indication_node);
       pthread_mutex_unlock(&bal_ind_queue_lock);
@@ -491,6 +491,7 @@
 bcmos_errno bal_group_indication_cb(bcmbal_obj *obj)
 {
    bcmos_errno result = BCM_ERR_OK;
+   unsigned int i = 0;
 
    if(BCMBAL_OBJ_ID_GROUP != obj->obj_type ||
       bcmbal_group_auto_id_ind != obj->subgroup)
@@ -557,51 +558,54 @@
       balIndCfg->group_ind->data->members->n_val = group_ind->data.members.len;
 
       BalGroupMemberInfo *balMemberInfo;
-      balMemberInfo = (BalGroupMemberInfo *)malloc(sizeof(BalGroupMemberInfo) * \
-                                         balIndCfg->group_ind->data->members->n_val);
-      memset(balMemberInfo, 0, sizeof(BalGroupMemberInfo) * \
-                                         balIndCfg->group_ind->data->members->n_val);
-      bal_group_member_info__init(balMemberInfo);
-      balIndCfg->group_ind->data->members->val = balMemberInfo;
-
-      balMemberInfo->has_intf_id = BAL_GRPC_PRES;
-      balMemberInfo->intf_id = group_ind->data.members.val->intf_id;
-      balMemberInfo->has_svc_port_id = BAL_GRPC_PRES;
-      balMemberInfo->svc_port_id = group_ind->data.members.val->svc_port_id;
-
       BalAction *balAction;
-      balAction = malloc(sizeof(BalAction));
-      memset(balAction, 0, sizeof(BalAction));
-      bal_action__init(balAction);
-      balMemberInfo->action = balAction;
-
-      balMemberInfo->action->has_presence_mask = BAL_GRPC_PRES;
-      balMemberInfo->action->presence_mask = group_ind->data.members.val->action.presence_mask;
-      balMemberInfo->action->has_cmds_bitmask = BAL_GRPC_PRES;
-      balMemberInfo->action->cmds_bitmask = group_ind->data.members.val->action.cmds_bitmask;
-      balMemberInfo->action->has_o_vid = BAL_GRPC_PRES;
-      balMemberInfo->action->o_vid = group_ind->data.members.val->action.o_vid;
-      balMemberInfo->action->has_o_pbits = BAL_GRPC_PRES;
-      balMemberInfo->action->o_pbits = group_ind->data.members.val->action.o_pbits;
-      balMemberInfo->action->has_o_tpid = BAL_GRPC_PRES;
-      balMemberInfo->action->o_tpid = group_ind->data.members.val->action.o_tpid;
-      balMemberInfo->action->has_i_vid = BAL_GRPC_PRES;
-      balMemberInfo->action->i_vid = group_ind->data.members.val->action.i_vid;
-      balMemberInfo->action->has_i_pbits = BAL_GRPC_PRES;
-      balMemberInfo->action->i_pbits = group_ind->data.members.val->action.i_pbits;
-      balMemberInfo->action->has_i_tpid = BAL_GRPC_PRES;
-      balMemberInfo->action->i_tpid = group_ind->data.members.val->action.i_tpid;
-
       BalTmQueueRef *balQueue;
-      balQueue = malloc(sizeof(BalTmQueueRef));
-      memset(balQueue, 0, sizeof(BalTmQueueRef));
-      bal_tm_queue_ref__init(balQueue);
-      balMemberInfo->queue = balQueue;
+      for (i = 0; i < balIndCfg->group_ind->data->members->n_val; i++)
+      {
+         balMemberInfo = malloc(sizeof(BalGroupMemberInfo));
+         memset(balMemberInfo, 0, sizeof(BalGroupMemberInfo));
+         bal_group_member_info__init(balMemberInfo);
 
-      balMemberInfo->queue->has_sched_id = BAL_GRPC_PRES;
-      balMemberInfo->queue->sched_id = group_ind->data.members.val->queue.sched_id;
-      balMemberInfo->queue->has_queue_id = BAL_GRPC_PRES;
-      balMemberInfo->queue->queue_id = group_ind->data.members.val->queue.queue_id;
+         balMemberInfo->has_intf_id = BAL_GRPC_PRES;
+         balMemberInfo->intf_id = group_ind->data.members.val->intf_id;
+         balMemberInfo->has_svc_port_id = BAL_GRPC_PRES;
+         balMemberInfo->svc_port_id = group_ind->data.members.val->svc_port_id;
+
+         balAction = malloc(sizeof(BalAction));
+         memset(balAction, 0, sizeof(BalAction));
+         bal_action__init(balAction);
+         balMemberInfo->action = balAction;
+
+         balMemberInfo->action->has_presence_mask = BAL_GRPC_PRES;
+         balMemberInfo->action->presence_mask = group_ind->data.members.val->action.presence_mask;
+         balMemberInfo->action->has_cmds_bitmask = BAL_GRPC_PRES;
+         balMemberInfo->action->cmds_bitmask = group_ind->data.members.val->action.cmds_bitmask;
+         balMemberInfo->action->has_o_vid = BAL_GRPC_PRES;
+         balMemberInfo->action->o_vid = group_ind->data.members.val->action.o_vid;
+         balMemberInfo->action->has_o_pbits = BAL_GRPC_PRES;
+         balMemberInfo->action->o_pbits = group_ind->data.members.val->action.o_pbits;
+         balMemberInfo->action->has_o_tpid = BAL_GRPC_PRES;
+         balMemberInfo->action->o_tpid = group_ind->data.members.val->action.o_tpid;
+         balMemberInfo->action->has_i_vid = BAL_GRPC_PRES;
+         balMemberInfo->action->i_vid = group_ind->data.members.val->action.i_vid;
+         balMemberInfo->action->has_i_pbits = BAL_GRPC_PRES;
+         balMemberInfo->action->i_pbits = group_ind->data.members.val->action.i_pbits;
+         balMemberInfo->action->has_i_tpid = BAL_GRPC_PRES;
+         balMemberInfo->action->i_tpid = group_ind->data.members.val->action.i_tpid;
+
+         balQueue = malloc(sizeof(BalTmQueueRef));
+         memset(balQueue, 0, sizeof(BalTmQueueRef));
+         bal_tm_queue_ref__init(balQueue);
+         balMemberInfo->queue = balQueue;
+
+         balMemberInfo->queue->has_sched_id = BAL_GRPC_PRES;
+         balMemberInfo->queue->sched_id = group_ind->data.members.val->queue.sched_id;
+         balMemberInfo->queue->has_queue_id = BAL_GRPC_PRES;
+         balMemberInfo->queue->queue_id = group_ind->data.members.val->queue.queue_id;
+
+         balIndCfg->group_ind->data->members->val[i] = balMemberInfo;
+      }
+
 
       BalIdList *balFlows;
       balFlows = malloc(sizeof(BalIdList));
diff --git a/src/bal_indications_hdlr.h b/src/bal_indications_hdlr.h
index abaa88c..84b8e27 100755
--- a/src/bal_indications_hdlr.h
+++ b/src/bal_indications_hdlr.h
@@ -32,7 +32,7 @@
 #include "asfvolt16_driver.h"
 #include "bal_indications_queue.h"
 
-#define BAL_GRPC_PRES    1 
+#define BAL_GRPC_PRES    1
 
 extern bcmos_errno bal_indication_cb(bcmbal_obj *obj);
 extern bcmos_errno bal_acc_term_osc_indication_cb(bcmbal_obj *obj);
diff --git a/src/bal_indications_queue.c b/src/bal_indications_queue.c
index ffc9a15..36ec466 100644
--- a/src/bal_indications_queue.c
+++ b/src/bal_indications_queue.c
@@ -15,6 +15,9 @@
 */
 
 #include "bal_indications_queue.h"
+#include "asfvolt16_driver.h"
+
+long unsigned int indication_drop_count = 0;
 
 /*
  * Implements a circular queue whose size can grow upto MAX_QUEUE_SIZE defined.
@@ -50,8 +53,8 @@
    /*'bal_ind_queue_head' should be thread safe there could be simultaneous
      write from BAL and adapter trying to read from the 'bal_ind_queue_head'.
      So, protect it under a mutex(bal_ind_queue_lock), before using this MACRO*/
-   if (num_of_nodes == 0 && 
-       bal_ind_queue_head == NULL && 
+   if (num_of_nodes == 0 &&
+       bal_ind_queue_head == NULL &&
        bal_ind_queue_tail == NULL ) {
       /*Case where the queue is empty*/
       bal_ind_queue_head = node;
@@ -60,27 +63,29 @@
       bal_ind_queue_tail->next = node;
       ++num_of_nodes;
    }
-   else if (num_of_nodes == 1 && 
-            bal_ind_queue_head != NULL && 
+   else if (num_of_nodes == 1 &&
+            bal_ind_queue_head != NULL &&
             bal_ind_queue_tail != NULL ) {
       bal_ind_queue_head->next = node;
       bal_ind_queue_tail = node;
       bal_ind_queue_tail->next = bal_ind_queue_head;
       ++num_of_nodes;
    }
-   else if (num_of_nodes > 1 && 
-            num_of_nodes < MAX_QUEUE_SIZE && 
-            bal_ind_queue_head != NULL && 
+   else if (num_of_nodes > 1 &&
+            num_of_nodes < MAX_QUEUE_SIZE &&
+            bal_ind_queue_head != NULL &&
             bal_ind_queue_tail != NULL ) {
       bal_ind_queue_tail->next = node;
       bal_ind_queue_tail = node;
       bal_ind_queue_tail->next = bal_ind_queue_head;
       ++num_of_nodes;
    }
-   else if (num_of_nodes == MAX_QUEUE_SIZE && 
-            bal_ind_queue_head != NULL && 
+   else if (num_of_nodes == MAX_QUEUE_SIZE &&
+            bal_ind_queue_head != NULL &&
             bal_ind_queue_tail != NULL  ) {
       list_node *head_ref = bal_ind_queue_head;
+      ASFVOLT_LOG(ASFVOLT_DEBUG, "Queue full, deleting a node. Drop cnt = %lu\n",\
+                                    ++indication_drop_count);
       bal_ind_queue_tail->next = node;
       bal_ind_queue_tail = node;
       bal_ind_queue_head = bal_ind_queue_head->next;
diff --git a/src/bal_indications_queue.h b/src/bal_indications_queue.h
index 075f747..72b6013 100755
--- a/src/bal_indications_queue.h
+++ b/src/bal_indications_queue.h
@@ -19,7 +19,7 @@
 
 #include "bal_indications.grpc-c.h"
 
-#define MAX_QUEUE_SIZE 25
+#define MAX_QUEUE_SIZE 1000
 
 typedef struct node {
     struct node *next;
@@ -38,4 +38,4 @@
 
 
 
-#endif /*__BAL_INDICATIONS_QUEUE_H__*/
\ No newline at end of file
+#endif /*__BAL_INDICATIONS_QUEUE_H__*/