/****************************************************************************
 * This file is part of the project AqHome.
 * AqHome (c) by 2023 Martin Preuss, all rights reserved.
 *
 * The license for this file can be found in the file COPYING which you
 * should have received along with this file.
 ****************************************************************************/

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif


#include "aqhome/ipc/endpoint_ipc_p.h"

#include <gwenhywfar/endpoint_ipc.h>
#include <gwenhywfar/debug.h>




#define AQH_MSG_ENDPOINT_IPC_NAME     "ipc"


GWEN_INHERIT(GWEN_MSG_ENDPOINT, AQH_ENDPOINT_IPC)



/* ------------------------------------------------------------------------------------------------
 * forward declarations
 * ------------------------------------------------------------------------------------------------
 */

static void GWENHYWFAR_CB _freeData(void *bp, void *p);



/* ------------------------------------------------------------------------------------------------
 * implementations
 * ------------------------------------------------------------------------------------------------
 */

void AQH_IpcEndpoint_Extend(GWEN_MSG_ENDPOINT *ep)
{
  AQH_ENDPOINT_IPC *xep;

  GWEN_NEW_OBJECT(AQH_ENDPOINT_IPC, xep);
  GWEN_INHERIT_SETDATA(GWEN_MSG_ENDPOINT, AQH_ENDPOINT_IPC, ep, xep, _freeData);
}



void _freeData(void *bp, void *p)
{
  AQH_ENDPOINT_IPC *xep;

  xep=(AQH_ENDPOINT_IPC*) p;

  free(xep->serviceName);
  free(xep->userName);
  free(xep->password);

  GWEN_FREE_OBJECT(xep);
}



uint32_t AQH_IpcEndpoint_GetAcceptedMsgGroups(const GWEN_MSG_ENDPOINT *ep)
{
  if (ep) {
    AQH_ENDPOINT_IPC *xep;

    xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT, AQH_ENDPOINT_IPC, ep);
    if (xep) {
      return xep->acceptedMsgGroups;
    }
  }

  return 0;
}



void AQH_IpcEndpoint_SetAcceptedMsgGroups(GWEN_MSG_ENDPOINT *ep, uint32_t i)
{
  if (ep) {
    AQH_ENDPOINT_IPC *xep;

    xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT, AQH_ENDPOINT_IPC, ep);
    if (xep)
      xep->acceptedMsgGroups=i;
  }
}



void AQH_IpcEndpoint_AddAcceptedMsgGroups(GWEN_MSG_ENDPOINT *ep, uint32_t i)
{
  if (ep) {
    AQH_ENDPOINT_IPC *xep;

    xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT, AQH_ENDPOINT_IPC, ep);
    if (xep)
      xep->acceptedMsgGroups|=i;
  }
}



void AQH_IpcEndpoint_SubAcceptedMsgGroups(GWEN_MSG_ENDPOINT *ep, uint32_t i)
{
  if (ep) {
    AQH_ENDPOINT_IPC *xep;

    xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT, AQH_ENDPOINT_IPC, ep);
    if (xep)
      xep->acceptedMsgGroups&=~i;
  }
}



const char *AQH_IpcEndpoint_GetServiceName(const GWEN_MSG_ENDPOINT *ep)
{
  if (ep) {
    AQH_ENDPOINT_IPC *xep;

    xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT, AQH_ENDPOINT_IPC, ep);
    if (xep)
      return xep->serviceName;
  }
  return NULL;
}



void AQH_IpcEndpoint_SetServiceName(GWEN_MSG_ENDPOINT *ep, const char *s)
{
  if (ep) {
    AQH_ENDPOINT_IPC *xep;

    xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT, AQH_ENDPOINT_IPC, ep);
    if (xep) {
      free(xep->serviceName);
      xep->serviceName=s?strdup(s):NULL;
    }
  }
}



const char *AQH_IpcEndpoint_GetUserName(const GWEN_MSG_ENDPOINT *ep)
{
  if (ep) {
    AQH_ENDPOINT_IPC *xep;

    xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT, AQH_ENDPOINT_IPC, ep);
    if (xep)
      return xep->userName;
  }
  return NULL;
}



void AQH_IpcEndpoint_SetUserName(GWEN_MSG_ENDPOINT *ep, const char *s)
{
  if (ep) {
    AQH_ENDPOINT_IPC *xep;

    xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT, AQH_ENDPOINT_IPC, ep);
    if (xep) {
      free(xep->userName);
      xep->userName=s?strdup(s):NULL;
    }
  }
}



const char *AQH_IpcEndpoint_GetPassword(const GWEN_MSG_ENDPOINT *ep)
{
  if (ep) {
    AQH_ENDPOINT_IPC *xep;

    xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT, AQH_ENDPOINT_IPC, ep);
    if (xep)
      return xep->password;
  }
  return NULL;
}



void AQH_IpcEndpoint_SetPassword(GWEN_MSG_ENDPOINT *ep, const char *s)
{
  if (ep) {
    AQH_ENDPOINT_IPC *xep;

    xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT, AQH_ENDPOINT_IPC, ep);
    if (xep) {
      free(xep->password);
      xep->password=s?strdup(s):NULL;
    }
  }
}



uint32_t AQH_IpcEndpoint_GetPermissions(const GWEN_MSG_ENDPOINT *ep)
{
  if (ep) {
    AQH_ENDPOINT_IPC *xep;

    xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT, AQH_ENDPOINT_IPC, ep);
    if (xep)
      return xep->permissions;
  }
  return 0;
}



void AQH_IpcEndpoint_SetPermissions(GWEN_MSG_ENDPOINT *ep, uint32_t i)
{
  if (ep) {
    AQH_ENDPOINT_IPC *xep;

    xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT, AQH_ENDPOINT_IPC, ep);
    if (xep)
      xep->permissions=i;
  }
}






GWEN_MSG_ENDPOINT *AQH_IpcEndpoint_CreateIpcTcpClient(const char *host, int port, const char *name, int groupId)
{
  GWEN_MSG_ENDPOINT *ep;

  ep=GWEN_IpcEndpoint_CreateIpcTcpClient(host, port, name?name:AQH_MSG_ENDPOINT_IPC_NAME, groupId);
  AQH_IpcEndpoint_Extend(ep);
  return ep;
}



GWEN_MSG_ENDPOINT *AQH_IpcEndpoint_CreateIpcTcpServiceForSocket(GWEN_SOCKET *sk, const char *name, int groupId)
{
  GWEN_MSG_ENDPOINT *ep;

  ep=GWEN_IpcEndpoint_CreateIpcTcpServiceForSocket(sk, name?name:AQH_MSG_ENDPOINT_IPC_NAME, groupId);
  AQH_IpcEndpoint_Extend(ep);
  return ep;
}



