From d18ae43123fcd7604d1c36a1ec8450dbe6071824 Mon Sep 17 00:00:00 2001 From: Luca Muscariello Date: Thu, 23 Feb 2017 20:44:26 +0100 Subject: Initial commit: ccnxlibs. Change-Id: I1b376527a7dd01a6b9e083a6cb646955902f45c0 Signed-off-by: Luca Muscariello --- .../ccnx/api/control/cpi_ManageWldr.c | 167 +++++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100644 libccnx-transport-rta/ccnx/api/control/cpi_ManageWldr.c (limited to 'libccnx-transport-rta/ccnx/api/control/cpi_ManageWldr.c') diff --git a/libccnx-transport-rta/ccnx/api/control/cpi_ManageWldr.c b/libccnx-transport-rta/ccnx/api/control/cpi_ManageWldr.c new file mode 100644 index 00000000..d0799d6b --- /dev/null +++ b/libccnx-transport-rta/ccnx/api/control/cpi_ManageWldr.c @@ -0,0 +1,167 @@ +/* + * 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 +#include +#include + +#include +#include +#include + +#include + +#include "cpi_private.h" + +static const char *cpiWldrString = "WLDR"; +static const char *cpiWldrConn = "CONN"; +static const char *cpiWldrOn = "ON"; +static const char *cpiWldrOff = "OFF"; + +struct cpi_manage_wldr { + char *connectionId; + bool active; +}; + +void +cpiManageWldr_Destroy(CPIManageWldr **cpiWldrPtr) +{ + assertNotNull(cpiWldrPtr, "Parameter must be non-null double pointer"); + assertNotNull(*cpiWldrPtr, "Parameter must dereference to non-null pointer"); + CPIManageWldr *cpiWldr = *cpiWldrPtr; + + parcMemory_Deallocate((void **) &cpiWldr->connectionId); + parcMemory_Deallocate((void **) &cpiWldr); + + *cpiWldrPtr = NULL; +} + +CPIManageWldr * +cpiManageWldr_Create(bool active, char *conn) +{ + CPIManageWldr *cpiWldr = parcMemory_AllocateAndClear(sizeof(cpiWldr)); + assertNotNull(cpiWldr, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(cpiWldr)); + + cpiWldr->connectionId = parcMemory_StringDuplicate(conn, strlen(conn)); + cpiWldr->active = active; + + return cpiWldr; +} + +char * +CPIManageWldr_ToString(CPIManageWldr *cpiWldr) +{ + PARCBufferComposer *composer = parcBufferComposer_Create(); + + parcBufferComposer_PutString(composer, cpiManageWldr_GetConnection(cpiWldr)); + + parcBufferComposer_PutString(composer, cpiWldrString); + + if (cpiManageWldr_IsActive(cpiWldr)) { + parcBufferComposer_PutString(composer, cpiWldrOn); + } else { + parcBufferComposer_PutString(composer, cpiWldrOff); + } + + PARCBuffer *tempBuffer = parcBufferComposer_ProduceBuffer(composer); + char *result = parcBuffer_ToString(tempBuffer); + parcBuffer_Release(&tempBuffer); + parcBufferComposer_Release(&composer); + return result; +} + +CPIManageWldr * +cpiManageWldr_Copy(const CPIManageWldr *original) +{ + assertNotNull(original, "Parameter a must be non-null"); + CPIManageWldr *copy = cpiManageWldr_Create(original->active, + parcMemory_StringDuplicate(original->connectionId, strlen(original->connectionId))); + + return copy; +} + +bool +cpiManageWldr_Equals(const CPIManageWldr *a, const CPIManageWldr *b) +{ + assertNotNull(a, "Parameter a must be non-null"); + assertNotNull(b, "Parameter b must be non-null"); + if (a == b) { + return true; + } + + if ((a->active == b->active) && (strcmp(a->connectionId, b->connectionId) == 0)) { + return true; + } + + return false; +} + +bool +cpiManageWldr_IsActive(const CPIManageWldr *cpiWldr) +{ + assertNotNull(cpiWldr, "Parameter must be non-null"); + return cpiWldr->active; +} + +const char * +cpiManageWldr_GetConnection(const CPIManageWldr *cpiWldr) +{ + assertNotNull(cpiWldr, "Parameter must be non-null"); + return cpiWldr->connectionId; +} + + +PARCJSON * +cpiManageWldr_ToJson(const CPIManageWldr *cpiWldr) +{ + PARCJSON *wldrJson = parcJSON_Create(); + + parcJSON_AddString(wldrJson, cpiWldrConn, cpiWldr->connectionId); + + if (cpiWldr->active) { + parcJSON_AddString(wldrJson, cpiWldrString, cpiWldrOn); + } else { + parcJSON_AddString(wldrJson, cpiWldrString, cpiWldrOff); + } + + return wldrJson; +} +CPIManageWldr * +cpiManageWldr_FromJson(PARCJSON *json) +{ + assertNotNull(json, "Parameter json must be non-null"); + PARCJSON *cpiWldrJson = json; + + PARCJSONValue *value = parcJSON_GetValueByName(cpiWldrJson, cpiWldrConn); + assertNotNull(value, "Couldn't locate tag %s in: %s", cpiWldrConn, parcJSON_ToString(json)); + + PARCBuffer *wBuf = parcJSONValue_GetString(value); + char *conn = parcBuffer_Overlay(wBuf, 0); + + value = parcJSON_GetValueByName(cpiWldrJson, cpiWldrString); + assertNotNull(value, "Couldn't locate tag %s in: %s", cpiWldrString, parcJSON_ToString(json)); + + wBuf = parcJSONValue_GetString(value); + char *active = parcBuffer_Overlay(wBuf, 0); + + CPIManageWldr *cpiWldr; + if (strcmp(active, cpiWldrOn) == 0) { + cpiWldr = cpiManageWldr_Create(true, conn); + } else { + cpiWldr = cpiManageWldr_Create(false, conn); + } + + return cpiWldr; +} -- cgit 1.2.3-korg