/****************************************************************************
 * This file is part of the project AqHome.
 * AqHome (c) by 2025 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 "./r_connect.h"
#include "./server_p.h"
#include "aqhome/ipc2/endpoint.h"
#include "aqhome/msg/ipc/m_ipc.h"
#include "aqhome/msg/ipc/nodes/m_ipcn.h"
#include "aqhome/msg/ipc/m_ipc_connect.h"
#include "aqhome/msg/ipc/m_ipc_result.h"
#include "aqhome/msg/ipc/m_ipc_tag16.h"

#include <gwenhywfar/debug.h>



/* ------------------------------------------------------------------------------------------------
 * code
 * ------------------------------------------------------------------------------------------------
 */

void AQH_NodeServer_HandleConnect(GWEN_UNUSED AQH_OBJECT *o, AQH_OBJECT *ep, const AQH_MESSAGE *msg, const GWEN_TAG16_LIST *tagList)
{
  if (tagList) {
    AQH_MESSAGE *outMsg;
    int resultCode=AQH_MSGDATA_RESULT_SUCCESS;
    char *clientId=NULL;
    char *userId=NULL;
    char *passw=NULL;
    uint32_t flags;
  
    clientId=AQH_Tag16_GetTagDataAsNewString(tagList, AQH_MSG_CONNECT_TAGS_CLIENTID, NULL);
    userId=AQH_Tag16_GetTagDataAsNewString(tagList, AQH_MSG_CONNECT_TAGS_USERID, NULL);
    flags=AQH_Tag16_GetTagDataAsUint32(tagList, AQH_MSG_CONNECT_TAGS_FLAGS, 0);
    passw=AQH_Tag16_GetTagDataAsNewString(tagList, AQH_MSG_CONNECT_TAGS_PASSWORD, NULL);
  
    if (clientId)
      AQH_Endpoint_SetServiceName(ep, clientId);
    if (userId)
      AQH_Endpoint_SetUserName(ep, userId);
  
    if (flags & AQH_MSG_CONNECT_FLAGS_WANTUPDATES)
      AQH_Endpoint_AddFlags(ep, AQH_ENDPOINT_FLAGS_WANTUPDATES);
  
    /* TODO: add user management, for now we allow all */
    AQH_Endpoint_SetPermissions(ep,
                                AQH_ENDPOINT_PERMS_LISTVALUES |
                                AQH_ENDPOINT_PERMS_READVALUE |
                                AQH_ENDPOINT_PERMS_ADDVALUE |
                                AQH_ENDPOINT_PERMS_LISTDATA |
                                AQH_ENDPOINT_PERMS_READDATA |
                                AQH_ENDPOINT_PERMS_ADDDATA |
                                AQH_ENDPOINT_PERMS_LISTDEVICES |
                                AQH_ENDPOINT_PERMS_READDEVICE |
                                AQH_ENDPOINT_PERMS_ADDDEVICE |
                                AQH_ENDPOINT_PERMS_MODDEVICE);
    free(passw);
    free(userId);
    free(clientId);
  
    outMsg=AQH_IpcMessageResult_new(AQH_IPC_PROTOCOL_NODES_ID,
                                    AQH_IPC_PROTOCOL_NODES_VERSION,
                                    AQH_MSGTYPE_IPC_NODES_RESULT,
                                    AQH_Endpoint_GetNextMessageId(ep),
                                    AQH_IpcMessage_GetMsgId(msg),
                                    resultCode, NULL);
    AQH_Endpoint_AddMsgOut(ep, outMsg);
  }
}



