Exemplo n.º 1
0
//--------------------------------------------------------------------------------------------------
static void AddWatchdog
(
    WatchdogObj_t* newDogPtr   ///< A pointer to the watchdog that is to be added to our container
)
{
    // The procId is the unique identifier for this watchdog. There shouldn't already be one.
    LE_ASSERT(NULL == le_hashmap_Put(WatchdogRefsContainer, &(newDogPtr->procId), newDogPtr));
}
Exemplo n.º 2
0
static int mangoh_bridge_air_vantage_pushString(void* param, const unsigned char* data, uint32_t size)
{
    mangoh_bridge_air_vantage_t* airVantage = (mangoh_bridge_air_vantage_t*)param;
    int32_t res = LE_OK;

    LE_ASSERT(airVantage);
    LE_ASSERT(data);

    LE_DEBUG("---> PUSH STRING");
    uint8_t* ptr = (uint8_t*)data;

    uint8_t len = 0;
    memcpy(&len, ptr, sizeof(len));
    LE_DEBUG("len(%u)", len);
    ptr += sizeof(len);

    char fieldName[MANGOH_BRIDGE_AIR_VANTAGE_FIELD_NAME_MAX_LEN] = {0};
    memcpy(fieldName, ptr, len);
    LE_DEBUG("field('%s')", fieldName);
    ptr += len;

    char val[MANGOH_BRIDGE_AIR_VANTAGE_VALUE_MAX_LEN] = {0};
    memcpy(val, ptr, size - len - sizeof(len));
    LE_DEBUG("value('%s')", val);

    dataRouter_WriteString(fieldName, val, time(NULL));

    dataRouter_DataUpdateHandlerRef_t dataUpdateHandlerRef = le_hashmap_Get(airVantage->dataUpdateHandlers, fieldName);
    if (!dataUpdateHandlerRef)
    {
        LE_DEBUG("add data update handler('%s')", fieldName);
        dataUpdateHandlerRef = dataRouter_AddDataUpdateHandler(fieldName, mangoh_bridge_air_vantage_dataUpdateHdlr, airVantage);
        if (!dataUpdateHandlerRef)
        {
            LE_ERROR("ERROR dataRouter_AddDataUpdateHandler() failed");
            res = LE_FAULT;
            goto cleanup;
        }

        if (le_hashmap_Put(airVantage->dataUpdateHandlers, fieldName, dataUpdateHandlerRef))
        {
            LE_ERROR("ERROR le_hashmap_Put() failed");
            res = LE_FAULT;
            goto cleanup;
        }
    }

    res = mangoh_bridge_sendResult(airVantage->bridge, 0);
    if (res != LE_OK)
    {
        LE_ERROR("ERROR mangoh_bridge_sendResult() failed(%d)", res);
        goto cleanup;
    }

cleanup:
    return res;
}
Exemplo n.º 3
0
//--------------------------------------------------------------------------------------------------
le_pm_WakeupSourceRef_t le_pm_NewWakeupSource(uint32_t opts, const char *tag)
{
    WakeupSource_t *ws;
    Client_t *cl;
    char name[LEGATO_WS_NAME_LEN];

    if (!tag || *tag == '\0' || strlen(tag) > LE_PM_TAG_LEN) {
        LE_KILL_CLIENT("Error: Tag value is invalid or NULL.");
        return NULL;
    }

    // Find and validate client record
    cl = to_Client_t(le_hashmap_Get(PowerManager.clients,
                                    le_pm_GetClientSessionRef()));

    // Check if identical wakeup source already exists for this client
    sprintf(name, LEGATO_WS_NAME_FORMAT, tag, cl->pid);

    // Lookup wakeup source by name
    ws = (WakeupSource_t*)le_hashmap_Get(PowerManager.locks, name);
    if (ws) {
        LE_KILL_CLIENT("Error: Tag '%s' already exists.", tag);
        return NULL;
    }

    // Allocate and populate wakeup source record (exits on error)
    ws = (WakeupSource_t*)le_mem_ForceAlloc(PowerManager.lpool);
    ws->cookie = PM_WAKEUP_SOURCE_COOKIE;
    strcpy(ws->name, name);
    ws->taken = LE_OFF;
    ws->pid = cl->pid;
    ws->wsref = le_ref_CreateRef(PowerManager.refs, ws);

    // Store record in table of wakeup sources
    if (le_hashmap_Put(PowerManager.locks, ws->name, ws))
        LE_FATAL("Error adding wakeup source '%s'.", ws->name);

    LE_INFO("Created new wakeup source '%s' for pid %d.", ws->name, ws->pid);

    return (le_pm_WakeupSourceRef_t)ws->wsref;
}
Exemplo n.º 4
0
//--------------------------------------------------------------------------------------------------
static void OnClientConnect
(
    le_msg_SessionRef_t sessionRef,
    void *contextPtr
)
{
    Client_t *c;

    // Allocate and populate client record (exits on error)
    c = (Client_t*)le_mem_ForceAlloc(PowerManager.cpool);
    c->cookie = PM_CLIENT_COOKIE;
    c->session = sessionRef;
    if (LE_OK != le_msg_GetClientProcessId(sessionRef, &c->pid))
        LE_FATAL("Error getting client pid.");

    // Store client record in table
    if (le_hashmap_Put(PowerManager.clients, sessionRef, c))
        LE_FATAL("Error adding client record for pid %d.", c->pid);

    LE_INFO("Connection from client pid = %d.", c->pid);
}
Exemplo n.º 5
0
//--------------------------------------------------------------------------------------------------
static void ListeningRecvHandler(void* handle, short events)
{
    int clientFd = -1;

    if (events != POLLIN)
    {
        LE_ERROR("Unexpected fd event(s): 0x%hX", events);
        return;
    }

    // Retrieve the Handle record, using the FD
    HandleRecord_t* parentRecordPtr = le_hashmap_Get(HandleRecordByFileDescriptor, handle);
    if (parentRecordPtr == NULL)
    {
        LE_ERROR("Unable to find matching Handle Record, fd [%" PRIiPTR "]", (intptr_t) handle);
        return;
    }

    // Accept the connection, setting the connection to be non-blocking.
    clientFd = accept((int)(size_t)handle, NULL, NULL);
    if (clientFd < 0)
    {
        LE_ERROR("Failed to accept client connection. Errno %d", errno);
    }

    LE_INFO("Accepting Client socket connection, fd [%d]", clientFd);
    HandleRecord_t *connectionRecordPtr = le_mem_AssertAlloc(HandleRecordPoolRef);

    // Initialize the connection record
    connectionRecordPtr->fd = clientFd;
    connectionRecordPtr->isListeningFd = false;
    connectionRecordPtr->parentRecordPtr = parentRecordPtr;

    le_hashmap_Put(HandleRecordByFileDescriptor, (void*)(intptr_t) connectionRecordPtr->fd, connectionRecordPtr);

    LE_INFO("Notifying RPC Proxy Client socket connected, fd [%d]", clientFd);

    // Notify the RPC Proxy of the Client connection
    AsyncConnectionHandlerFuncPtr(connectionRecordPtr, events);
}
Exemplo n.º 6
0
//--------------------------------------------------------------------------------------------------
static Service_t* CreateService
(
    le_msg_ProtocolRef_t    protocolRef,
    const char*             serviceName
)
//--------------------------------------------------------------------------------------------------
{
    Service_t* servicePtr = le_mem_ForceAlloc(ServicePoolRef);
    servicePtr->id.protocolRef = protocolRef;
    le_result_t result = le_utf8_Copy(servicePtr->id.name,
                                      serviceName,
                                      sizeof(servicePtr->id.name),
                                      NULL);
    LE_FATAL_IF(result != LE_OK,
                "Service ID '%s' too long (should only be %zu bytes total).",
                serviceName,
                sizeof(servicePtr->id.name));

    servicePtr->state = LE_MSG_SERVICE_HIDDEN;

    servicePtr->directorySocketFd = -1;
    servicePtr->fdMonitorRef = NULL;
    servicePtr->serverThread = NULL;    // NULL indicates no server in this process.

    servicePtr->sessionList = LE_DLS_LIST_INIT;

    servicePtr->recvHandler = NULL;
    servicePtr->recvContextPtr = NULL;

    // Initialize the close handlers dls
    servicePtr->closeListPtr = LE_DLS_LIST_INIT;

    // Initialize the open handlers dls
    servicePtr->openListPtr = LE_DLS_LIST_INIT;

    le_hashmap_Put(ServiceMapRef, &servicePtr->id, servicePtr);

    return servicePtr;
}
Exemplo n.º 7
0
// -------------------------------------------------------------------------------------------------
static tu_UserRef_t CreateUserInfo
(
    uid_t userId,          ///< [IN] The linux Id of the user in question.
    const char* userName,  ///< [IN] The name of the user.
    const char* treeName   ///< [IN] The name of the default tree for this user.
)
// -------------------------------------------------------------------------------------------------
{
    tu_UserRef_t userRef = le_mem_ForceAlloc(UserPoolRef);

    userRef->userId = userId;
    LE_ASSERT(le_utf8_Copy(userRef->userName, userName, sizeof(userRef->userName), NULL) == LE_OK);
    LE_ASSERT(le_utf8_Copy(userRef->treeName, treeName, sizeof(userRef->treeName), NULL) == LE_OK);

    LE_ASSERT(le_hashmap_Put(UserCollectionRef, &userRef->userId, userRef) == NULL);

    LE_DEBUG("** Allocated new user object <%p>: '%s', %u with default tree, '%s'.",
             userRef,
             userRef->userName,
             userRef->userId,
             userRef->treeName);

    return userRef;
}
Exemplo n.º 8
0
//--------------------------------------------------------------------------------------------------
LE_SHARED void* le_comm_Create
(
    const int argc,         ///< [IN] Number of strings pointed to by argv.
    const char *argv[],     ///< [IN] Pointer to an array of character strings.
    le_result_t* resultPtr  ///< [OUT] Return Code
)
{
    //
    // Create Connection Socket
    //

    // Verify result pointer is valid
    if (resultPtr == NULL)
    {
        LE_ERROR("resultPtr is NULL");
        return NULL;
    }

    if (!le_hashmap_isEmpty(HandleRecordByFileDescriptor))
    {
        LE_ERROR("Sanity Check Failure: Hashmap is not empty");
        *resultPtr = LE_FAULT;
        return NULL;
    }

    // Parse the Command Line arguments to extract the IP Address and TCP Port number
    *resultPtr = ParseCommandLineArgs(argc, argv);
    if (*resultPtr != LE_OK)
    {
        return NULL;
    }

    HandleRecord_t* connectionRecordPtr = le_mem_AssertAlloc(HandleRecordPoolRef);

    // Initialize the connection record
    connectionRecordPtr->fd = -1;
    connectionRecordPtr->isListeningFd = false;
    connectionRecordPtr->parentRecordPtr = NULL;

    // Create the socket
    connectionRecordPtr->fd = socket(AF_INET, SOCK_STREAM, 0);
    if (connectionRecordPtr->fd < 0)
    {
        LE_WARN("Failed to create AF_INET socket.  Errno = %d", errno);

        // Set the result pointer with a fault code
        *resultPtr = LE_FAULT;
        connectionRecordPtr->fd = -1;
        return (connectionRecordPtr);
    }

#ifdef SOCKET_SERVER
    struct sockaddr_in sockAddr;

    // Prepare the sockaddr_in structure
    sockAddr.sin_family = AF_INET;
    sockAddr.sin_addr.s_addr = INADDR_ANY;
    sockAddr.sin_port = htons(NetworkSocketTCPListeningPort);

    // Bind
    if (bind(connectionRecordPtr->fd, (struct sockaddr *)&sockAddr, sizeof(sockAddr)) < 0)
    {
        LE_WARN("Failed to bind socket, fd %d, result = %d",
                connectionRecordPtr->fd,
                errno);

        // Close the Socket handle
        close(connectionRecordPtr->fd);
        connectionRecordPtr->fd = -1;

        // Set the result pointer with a fault code
        *resultPtr = LE_FAULT;
        return (connectionRecordPtr);
    }

    // Listen
    if (listen(connectionRecordPtr->fd, NETWORK_SOCKET_MAX_CONNECT_REQUEST_BACKLOG) != 0)
    {
        LE_WARN("Server socket listen() call failed with errno %d", errno);
    }

    LE_INFO("Registering handle_monitor callback");

    char socketName[80];
    snprintf(socketName, sizeof(socketName) - 1, "inetSocket-%d", connectionRecordPtr->fd);

    // Create thread to monitor FD handle for activity, as defined by the events
    ListeningFdMonitorRef = le_fdMonitor_Create(socketName,
                                                connectionRecordPtr->fd,
                                                (le_fdMonitor_HandlerFunc_t) &ListeningRecvHandler,
                                                POLLIN);

    // Flag as a listening socket
    connectionRecordPtr->isListeningFd = true;

    LE_INFO("Successfully registered listening callback function, events [0x%x]", POLLIN);

#endif

    LE_INFO("Created AF_INET Socket, fd %d", connectionRecordPtr->fd);

    // Set the return code to reflect if client can proceed with connect call, or whether it needs to
    // wait for asynchronous connection
#ifdef SOCKET_SERVER
    *resultPtr = LE_IN_PROGRESS;
#else
    *resultPtr = LE_OK;
#endif

    // Store the Handle record
    le_hashmap_Put(HandleRecordByFileDescriptor, (void*)(intptr_t) connectionRecordPtr->fd, connectionRecordPtr);

    return (connectionRecordPtr);
}
Exemplo n.º 9
0
static int mangoh_bridge_air_vantage_pushFloat(void* param, const unsigned char* data, uint32_t size)
{
    mangoh_bridge_air_vantage_t* airVantage = (mangoh_bridge_air_vantage_t*)param;
    double fVal = 0;
    int32_t res = LE_OK;

    LE_ASSERT(airVantage);
    LE_ASSERT(data);

    LE_DEBUG("---> PUSH FLOAT");
    uint8_t* ptr = (uint8_t*)data;

    uint8_t len = 0;
    memcpy(&len, ptr, sizeof(len));
    LE_DEBUG("len(%u)", len);
    ptr += sizeof(len);

    char fieldName[MANGOH_BRIDGE_AIR_VANTAGE_FIELD_NAME_MAX_LEN] = {0};
    memcpy(fieldName, ptr, len);
    LE_DEBUG("field('%s')", fieldName);
    ptr += len;

    int8_t precision = 0;
    memcpy(&precision, ptr, sizeof(precision));
    LE_DEBUG("precision(%u)", precision);
    ptr += sizeof(precision);

    int32_t val = 0;
    memcpy(&val, ptr, sizeof(val));
    LE_DEBUG("value(%d)", val);
    fVal = val;
    while (precision-- > 0) fVal /= 10.0;

    LE_DEBUG("write(%f)", fVal);
    dataRouter_WriteFloat(fieldName, fVal, time(NULL));

    dataRouter_DataUpdateHandlerRef_t dataUpdateHandlerRef = le_hashmap_Get(airVantage->dataUpdateHandlers, fieldName);
    if (!dataUpdateHandlerRef)
    {
        LE_DEBUG("add data update handler('%s')", fieldName);
        dataUpdateHandlerRef = dataRouter_AddDataUpdateHandler(fieldName, mangoh_bridge_air_vantage_dataUpdateHdlr, airVantage);
        if (!dataUpdateHandlerRef)
        {
            LE_ERROR("ERROR dataRouter_AddDataUpdateHandler() failed");
            res = LE_FAULT;
            goto cleanup;
        }

        if (le_hashmap_Put(airVantage->dataUpdateHandlers, fieldName, dataUpdateHandlerRef))
        {
            LE_ERROR("ERROR le_hashmap_Put() failed");
            res = LE_FAULT;
            goto cleanup;
        }
    }

    res = mangoh_bridge_sendResult(airVantage->bridge, 0);
    if (res != LE_OK)
    {
        LE_ERROR("ERROR mangoh_bridge_sendResult() failed(%d)", res);
        goto cleanup;
    }

cleanup:
    return res;
}