/****************************************************************************
 * 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 "./c_connect.h"
#include "./aqhome_data_p.h"
#include "aqhome/ipc/data/ipc_data.h"
#include "aqhome/ipc/endpoint_ipc.h"
#include "aqhome/ipc/msg_ipc_result.h"
#include "aqhome/ipc/data/msg_data_connect.h"
#include "aqhome/ipc/msg_ipc_tag16.h"

#include <gwenhywfar/debug.h>



/* ------------------------------------------------------------------------------------------------
 * defines
 * ------------------------------------------------------------------------------------------------
 */




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




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

void AqHomeData_HandleConnect(AQHOME_DATA *aqh, GWEN_MSG_ENDPOINT *ep, GWEN_MSG *msg)
{
  GWEN_MSG *outMsg;
  int resultCode=AQH_MSG_IPC_SUCCESS;
  char *clientId=NULL;
  char *userId=NULL;
  char *passw=NULL;
  uint32_t flags;

  AQH_ConnectDataIpcMsg_Parse(msg, 0);
  clientId=AQH_Tag16IpcMsg_GetTagDataAsNewString(msg, AQH_MSGDATA_CONNECT_TAGS_CLIENTID, NULL);
  userId=AQH_Tag16IpcMsg_GetTagDataAsNewString(msg, AQH_MSGDATA_CONNECT_TAGS_USERID, NULL);
  flags=AQH_Tag16IpcMsg_GetTagDataAsUint32(msg, AQH_MSGDATA_CONNECT_TAGS_FLAGS, 0);
  passw=AQH_Tag16IpcMsg_GetTagDataAsNewString(msg, AQH_MSGDATA_CONNECT_TAGS_PASSWORD, NULL);

  if (clientId)
    AQH_IpcEndpoint_SetServiceName(ep, clientId);
  if (userId)
    AQH_IpcEndpoint_SetUserName(ep, userId);

  if (flags & AQH_MSGDATA_CONNECT_FLAGS_WANTUPDATES)
    GWEN_MsgEndpoint_AddFlags(ep, AQH_IPCENDPOINT_FLAGS_WANTUPDATES);

  /* TODO: add user management, for now we allow all */
  AQH_IpcEndpoint_SetPermissions(ep,
                                 AQH_IPCENDPOINT_PERMS_LISTVALUES |
                                 AQH_IPCENDPOINT_PERMS_READVALUE |
                                 AQH_IPCENDPOINT_PERMS_ADDVALUE |
                                 AQH_IPCENDPOINT_PERMS_LISTDATA |
                                 AQH_IPCENDPOINT_PERMS_READDATA |
                                 AQH_IPCENDPOINT_PERMS_ADDDATA |
                                 AQH_IPCENDPOINT_PERMS_LISTDEVICES |
                                 AQH_IPCENDPOINT_PERMS_READDEVICE |
                                 AQH_IPCENDPOINT_PERMS_ADDDEVICE |
                                 AQH_IPCENDPOINT_PERMS_MODDEVICE);
  free(passw);
  free(userId);
  free(clientId);

  outMsg=AQH_ResultIpcMsg_new(AQH_MSGTYPE_IPC_DATA_RESULT, resultCode);
  GWEN_MsgEndpoint_AddSendMessage(ep, outMsg);
}



