/*
 *------------------------------------------------------------------
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *------------------------------------------------------------------
 */

#include <stdio.h>
#include <endian.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <setjmp.h>
#include <check.h>
#include <vapi/vapi.h>
#include <vapi/vpe.api.vapi.h>
#include <vapi/interface.api.vapi.h>
#include <vapi/l2.api.vapi.h>
#include <vapi/vpncrack.api.vapi.h>
#include <fake.api.vapi.h>

DEFINE_VAPI_MSG_IDS_VPE_API_JSON;
DEFINE_VAPI_MSG_IDS_INTERFACE_API_JSON;
DEFINE_VAPI_MSG_IDS_L2_API_JSON;
DEFINE_VAPI_MSG_IDS_STATS_API_JSON;
DEFINE_VAPI_MSG_IDS_FAKE_API_JSON;
DEFINE_VAPI_MSG_IDS_VPNCRACK_API_JSON;
static char *app_name = "test";
static char *api_prefix = NULL;
static const int max_outstanding_requests = 64;
static const int response_queue_size = 32;

int test_connect(vapi_ctx_t ctx)
{
  

  vapi_error_e rv0;
  rv0 = vapi_connect (ctx, app_name, api_prefix, max_outstanding_requests,
		     response_queue_size, VAPI_MODE_BLOCKING);
  printf("vapi connect success!\n");
  
  printf("rv0 = %d\n", rv0);

  return rv0;
}
int test_disconnect(vapi_ctx_t ctx)
{
   vapi_error_e rv1;
  rv1 = vapi_disconnect (ctx);
  printf("rv1 = %d\n", rv1);
  vapi_ctx_free (ctx);
  return rv1;
}
vapi_error_e
show_version_cb (vapi_ctx_t ctx, void *caller_ctx,
		 vapi_error_e rv, bool is_last,
		 vapi_payload_show_version_reply * p)
{
  printf("show_version_reply: program: `%s', version: `%s', build directory: `%s', build date: `%s'\n", p->program, p->version, p->build_directory, p->build_date);
  ++*(int *) caller_ctx;
  return 0;
}
/*int main(int argc, char **argv)
{
	vapi_ctx_t ctx;
	int called = 0;
	vapi_error_e rv = vapi_ctx_alloc (&ctx);
	test_connect(ctx);
	printf ("--- Show version via blocking callback API ---\n");
	const int attempts = response_queue_size * 4;
  	int i = 0;
  	for (i = 0; i < attempts; ++i)
   	{
      		vapi_msg_show_version *sv = vapi_alloc_show_version (ctx);
      		rv = vapi_show_version (ctx, sv, show_version_cb, &called);
      
    	}
	printf("rv = %d\n", rv);
	test_disconnect(ctx);
	return 0;


}
*/
/*vapi_error_e
interface_simple_stats_cb (vapi_ctx_t ctx, void *callback_ctx,
			   vapi_error_e rv, bool is_last,
			   vapi_payload_want_interface_simple_stats_reply *
			   payload)
{
	printf("i am here\n");  
	return VAPI_OK;
}

vapi_error_e
simple_counters_cb (vapi_ctx_t ctx, void *callback_ctx,
		    vapi_payload_vnet_interface_simple_counters * payload)
{
  int *called = callback_ctx;
  ++*called;
  printf ("simple counters: first_sw_if_index=%u\n", payload->first_sw_if_index);
  return VAPI_OK;
}*/
/*int main(int argc, char **argv)
{
	vapi_ctx_t ctx;
	int called = 0;
	vapi_error_e rv = vapi_ctx_alloc (&ctx);
	test_connect(ctx);
	printf ("--- Receive stats using generic blocking API ---\n");
  	vapi_msg_want_interface_simple_stats *ws = vapi_alloc_want_interface_simple_stats (ctx);
  	ws->payload.enable_disable = 1;
  	ws->payload.pid = getpid ();
  	rv = vapi_want_interface_simple_stats (ctx, ws, interface_simple_stats_cb, NULL);
  
  	vapi_set_event_cb (ctx, vapi_msg_id_vnet_interface_simple_counters, (vapi_event_cb) simple_counters_cb, &called);
	while(1){
  	rv = vapi_dispatch_one (ctx);}
	test_disconnect(ctx);
	return 0;


}*/
vapi_error_e
vpncrack_decrypt_event_cb (vapi_ctx_t ctx, void *callback_ctx,
			   vapi_error_e rv, bool is_last,
			   vapi_payload_want_vpncrack_decrypt_event_reply *
			   payload)
{
	printf("i am here\n");  
	return VAPI_OK;
}

vapi_error_e
decrypt_event_cb (vapi_ctx_t ctx, void *callback_ctx,
		    vapi_payload_vpncrack_decrypt_event * payload)
{
  int *called = callback_ctx;
  ++*called;
  printf ("decrypt event: event_type=%u\n", payload->event_type);
  return VAPI_OK;
}
int main(int argc, char **argv)
{
	vapi_ctx_t ctx;
	int called = 0;
	vapi_error_e rv = vapi_ctx_alloc (&ctx);
	test_connect(ctx);
	printf ("--- Receive stats using generic blocking API ---\n");
  	vapi_msg_want_vpncrack_decrypt_event *ws = vapi_alloc_want_vpncrack_decrypt_event (ctx);
  	ws->payload.enable = 1;
  	ws->payload.pid = getpid ();
  	rv = vapi_want_vpncrack_decrypt_event (ctx, ws, vpncrack_decrypt_event_cb, NULL);
  
  	vapi_set_event_cb (ctx, vapi_msg_id_vpncrack_decrypt_event, (vapi_event_cb) decrypt_event_cb, &called);
	while(1){
  	rv = vapi_dispatch_one (ctx);}
	test_disconnect(ctx);
	return 0;


}
