Example #1
0
AJ_Status AJ_Net_Connect(AJ_NetSocket* netSock, uint16_t port, uint8_t addrType, const uint32_t* addr)
{
    int ret;

    IPAddress ip(*addr);

    AJ_InfoPrintf(("AJ_Net_Connect(nexSock=0x%p, port=%d., addrType=%d., addr=0x%p)\n", netSock, port, addrType, addr));

    AJ_InfoPrintf(("AJ_Net_Connect(): Connect to 0x%x:%u.\n", addr, port));;

    ret = g_client.connect(ip, port);

#ifdef NOTDEF
    Serial.print("Connecting to: ");
    Serial.print(ip);
    Serial.print(':');
    Serial.println(port);
#endif

    if (ret == -1) {
        AJ_ErrPrintf(("AJ_Net_Connect(): connect() failed: %d: status=AJ_ERR_CONNECT\n", ret));
        return AJ_ERR_CONNECT;
    } else {
        AJ_IOBufInit(&netSock->rx, rxData, sizeof(rxData), AJ_IO_BUF_RX, (void*)&g_client);
        netSock->rx.recv = AJ_Net_Recv;
        AJ_IOBufInit(&netSock->tx, txData, sizeof(txData), AJ_IO_BUF_TX, (void*)&g_client);
        netSock->tx.send = AJ_Net_Send;
        AJ_ErrPrintf(("AJ_Net_Connect(): connect() success: status=AJ_OK\n"));
        return AJ_OK;
    }
    AJ_ErrPrintf(("AJ_Net_Connect(): connect() failed: %d: status=AJ_ERR_CONNECT\n", ret));
    return AJ_ERR_CONNECT;
}
static AJ_Status MatchProp(const char* member, const char* prop, uint8_t op, const char** sig)
{
    const char* encoding = member;

    if (*encoding++ != '@') {
        AJ_ErrPrintf(("MatchProp(): AJ_ERR_NO_MATCH\n"));
        return AJ_ERR_NO_MATCH;
    }
    while (*prop) {
        if (*encoding++ != *prop++) {
            AJ_ErrPrintf(("MatchProp(): AJ_ERR_NO_MATCH\n"));
            return AJ_ERR_NO_MATCH;
        }
    }
    if ((op == AJ_PROP_GET) && (*encoding == WRITE_ONLY)) {
        AJ_ErrPrintf(("MatchProp(): AJ_ERR_DISALLOWED\n"));
        return AJ_ERR_DISALLOWED;
    }
    if ((op == AJ_PROP_SET) && (*encoding == READ_ONLY)) {
        AJ_ErrPrintf(("MatchProp(): AJ_ERR_DISALLOWED\n"));
        return AJ_ERR_DISALLOWED;
    }
    *sig = ++encoding;
    return AJ_OK;
}
Example #3
0
static AJ_Status ExportIfNeeded(const char* dev, int deviceId, const char* root)
{
    int fd;
    DIR* dir;
    char buf[256];

    snprintf(buf, sizeof(buf), "%s%s", root, dev);
    dir = opendir(buf);
    if (dir) {
        closedir(dir);
        return AJ_OK;
    }
    if (errno != ENOENT) {
        AJ_ErrPrintf(("Failed opendir %d %s\n", errno, buf));
        return AJ_ERR_INVALID;
    }
    /*
     * Try to create the device entry
     */
    snprintf(buf, sizeof(buf), "%s%s", root, "export");
    fd = open(buf, O_WRONLY);
    if (fd >= 0) {
        size_t sz = snprintf(buf, sizeof(buf), "%d", deviceId);
        write(fd, buf, sz + 1);
        close(fd);
        return AJ_OK;
    } else {
        AJ_ErrPrintf(("Failed to open %s\n", buf));
        return AJ_ERR_INVALID;
    }
}
Example #4
0
static AJ_Status AJ_ARDP_UDP_Send(void* context, uint8_t* buf, size_t len, size_t* sent)
{
    AJ_Status status = AJ_OK;
    DWORD ret;
    NetContext* ctx = (NetContext*) context;
    WSAOVERLAPPED ov;
    DWORD flags = 0;
    WSABUF wsbuf;

    memset(&ov, 0, sizeof(ov));
    ov.hEvent = sendEvent;
    wsbuf.len = len;
    wsbuf.buf = buf;

    AJ_InfoPrintf(("AJ_ARDP_UDP_Send(buf=0x%p, len=%lu)\n", buf, len));

    ret = WSASend(ctx->udpSock, &wsbuf, 1, NULL, flags, &ov, NULL);
    if (ret == SOCKET_ERROR) {
        AJ_ErrPrintf(("AJ_ARDP_UDP_Send(): WSASend() failed. WSAGetLastError()=0x%x, status=AJ_ERR_WRITE\n", WSAGetLastError()));
        *sent = 0;
        return AJ_ERR_WRITE;
    }

    if (!WSAGetOverlappedResult(ctx->udpSock, &ov, sent, TRUE, &flags)) {
        AJ_ErrPrintf(("AJ_ARDP_UDP_Send(): WSAGetOverlappedResult() failed. WSAGetLastError()=0x%x, status=AJ_ERR_WRITE\n", WSAGetLastError()));
        return AJ_ERR_WRITE;
    }

    return status;
}
Example #5
0
AJ_Status AJNS_Producer_CancelNotification(AJ_BusAttachment* busAttachment, uint32_t serialNum)
{
    AJ_Status status;
    uint16_t messageType = 0;

    AJ_InfoPrintf(("In CancelNotificationBySerialNum\n"));

    if (serialNum == 0) {
        AJ_ErrPrintf(("Could not cancel Message - no message to cancel\n"));
        return AJ_OK;
    }
    for (; messageType < AJNS_NUM_MESSAGE_TYPES; messageType++) {
        if (lastSentNotifications[messageType].serialNum == serialNum) {
            break;
        }
    }
    if (messageType >= AJNS_NUM_MESSAGE_TYPES) {
        AJ_ErrPrintf(("Could not find matching Message serial number - no message to cancel\n"));
        return AJ_OK;
    }

    status = AJ_BusCancelSessionless(busAttachment, serialNum);

    if (status != AJ_OK) {
        AJ_ErrPrintf(("Failed to send cancelation\n"));
        return status;
    }

    AJ_InfoPrintf(("***************** Message with Notification id %d and serialNum %u deleted successfully *****************\n", lastSentNotifications[messageType].notificationId, lastSentNotifications[messageType].serialNum));

    lastSentNotifications[messageType].notificationId = 0;
    lastSentNotifications[messageType].serialNum = 0;

    return status;
}
AJ_Status AJNS_Producer_DeleteLastNotification(AJ_BusAttachment* busAttachment, uint16_t messageType)
{
    AJ_Status status;
    uint32_t lastSentSerialNumber;

   // AJ_InfoPrintf(("In DeleteLastNotification\n"));
    if (messageType >= AJNS_NUM_MESSAGE_TYPES) 
	{
        AJ_ErrPrintf(("Could not delete Notification - MessageType is not valid\n"));
        return AJ_ERR_DISALLOWED;
    }

    lastSentSerialNumber = lastSentNotifications[messageType].serialNum;
    if (lastSentSerialNumber == 0) 
	{
        AJ_ErrPrintf(("Could not Delete Message - no message to delete\n"));
        return AJ_OK;
    }

    status = AJ_BusCancelSessionless(busAttachment, lastSentSerialNumber);

    if (status != AJ_OK) 
	{
        AJ_ErrPrintf(("Could not Delete Message\n"));
        return status;
    }

  //  AJ_InfoPrintf(("***************** Message with Notification id %d and serialNum %u deleted successfully *****************\n", lastSentNotifications[messageType].notificationId, lastSentNotifications[messageType].serialNum));

    lastSentNotifications[messageType].notificationId = 0;
    lastSentNotifications[messageType].serialNum = 0;

    return status;
}
AJ_Status CPS_StartService(AJ_BusAttachment* bus, const char* busAddress, uint32_t timeout, uint8_t connected)
{
    AJ_Status status = AJ_OK;
    while (TRUE) {
        AJ_InfoPrintf(("Attempting to connect to bus '%s'\n", busAddress));

        status = AJ_Connect(bus, busAddress, timeout);
        if (status != AJ_OK) {
            AJ_ErrPrintf(("Failed to connect to bus '%s', sleeping for %d seconds...\n", busAddress, CPSC_CONNECT_PAUSE / 1000));
            AJ_Sleep(CPSC_CONNECT_PAUSE);
            continue;
        }

        AJ_InfoPrintf(("Connected successfully\n"));
        isBusConnected = TRUE;

        status = AJ_BusSetSignalRule(bus, CPSAnnounceMatch, AJ_BUS_SIGNAL_ALLOW);
        if (status != AJ_OK) {
            AJ_ErrPrintf(("Could not set Announcement Interface AddMatch\n"));
            return status;
        }
        break;
    }
    ;
    return status;
}
AJ_Status AJNS_Consumer_SendDismissRequest(AJ_BusAttachment* busAttachment, uint16_t version, int32_t notificationId, const char* appId, const char* senderName, uint32_t sessionId)
{
    AJ_Status status = AJ_OK;

    if ((status == AJ_OK) && (sessionId != 0)) {
        AJ_Message dismissMsg;
        status = AJ_MarshalMethodCall(busAttachment, &dismissMsg, NOTIFICATION_PRODUCER_DISMISS_PROXY, senderName, sessionId, AJ_NO_FLAGS, AJ_CALL_TIMEOUT);
        if (status != AJ_OK) {
            AJ_ErrPrintf(("Could not marshal method call\n"));
            return status;
        }
        status = AJ_MarshalArgs(&dismissMsg, "i", notificationId);
        if (status != AJ_OK) {
            AJ_ErrPrintf(("Could not marshal arguments\n"));
            return status;
        }
        status = AJ_DeliverMsg(&dismissMsg);
        if (status != AJ_OK) {
            AJ_ErrPrintf(("Could not deliver message\n"));
            return status;
        }
        AJ_CloseMsg(&dismissMsg);
    }

    return status;
}
Example #9
0
AJ_Status AJ_Net_Connect(AJ_BusAttachment* bus, const AJ_Service* service)
{
    int ret;
    IPAddress ip(service->ipv4);

    if (!(service->addrTypes & AJ_ADDR_TCP4)) {
        AJ_ErrPrintf(("AJ_Net_Connect(): only IPV4 TCP supported\n", ret));
        return AJ_ERR_CONNECT;
    }


    AJ_InfoPrintf(("AJ_Net_Connect(bus=0x%p, addrType=%d.)\n", bus, service->addrTypes));

    ret = g_client.connect(ip, service->ipv4port);


    if (ret != 1) {
        AJ_ErrPrintf(("AJ_Net_Connect(): connect() failed: %d: status=AJ_ERR_CONNECT\n", ret));
        return AJ_ERR_CONNECT;
    } else {
        AJ_IOBufInit(&bus->sock.rx, rxData, sizeof(rxData), AJ_IO_BUF_RX, (void*)&g_client);
        bus->sock.rx.recv = AJ_Net_Recv;
        AJ_IOBufInit(&bus->sock.tx, txData, sizeof(txData), AJ_IO_BUF_TX, (void*)&g_client);
        bus->sock.tx.send = AJ_Net_Send;
        AJ_ErrPrintf(("AJ_Net_Connect(): connect() success: status=AJ_OK\n"));
        return AJ_OK;
    }
    AJ_ErrPrintf(("AJ_Net_Connect(): connect() failed: %d: status=AJ_ERR_CONNECT\n", ret));
    return AJ_ERR_CONNECT;
}
static void OpenSimIO()
{
    pthread_attr_t attr;
    struct sockaddr_un sa;

    memset(&sa, 0, sizeof(sa));
    sa.sun_family = AF_UNIX;
    memcpy(sa.sun_path, gui_server, sizeof(gui_server));

    sock = socket(AF_UNIX,  SOCK_STREAM, 0);
    if (sock >= 0) {
        pthread_t threadId;
        int ret = connect(sock, (struct sockaddr*)&sa, sizeof(sa));
        if (ret == -1) {
            AJ_ErrPrintf(("Failed to connect to I/O gui server\n"));
            close(sock);
            sock = -1;
        }
        /*
         * Create thread to handle reads
         */
        pthread_attr_init(&attr);
        ret = pthread_create(&threadId, NULL, SockRead, NULL);
        if (ret) {
            AJ_ErrPrintf(("Failed to create read thread\n"));
            close(sock);
            sock = -1;
        } else {
            pthread_detach(threadId);
            pthread_cond_init(&cond, NULL);
            pthread_mutex_init(&mutex, NULL);
        }
    }
}
Example #11
0
static AJ_Status AJ_ARDP_UDP_Recv(void* context, uint8_t** data, uint32_t* recved, uint32_t timeout)
{
    NetContext* ctx = (NetContext*) context;
    DWORD ret = SOCKET_ERROR;
    WSAEVENT events[2];
    DWORD flags = 0;
    static uint8_t buffer[UDP_SEGBMAX];

    *data = NULL;

    if (wsaOverlapped.hEvent == INVALID_HANDLE_VALUE) {
        wsbuf.len = sizeof(buffer);
        wsbuf.buf = buffer;
        memset(&wsaOverlapped, 0, sizeof(WSAOVERLAPPED));
        wsaOverlapped.hEvent = recvEvent;
        ret = WSARecvFrom(ctx->udpSock, &wsbuf, 1, NULL, &flags, NULL, NULL, &wsaOverlapped, NULL);
        if ((ret == SOCKET_ERROR) && (WSAGetLastError() != WSA_IO_PENDING)) {
            AJ_ErrPrintf(("WSARecvFrom(): failed WSAGetLastError()=%d\n", WSAGetLastError()));
            return AJ_ERR_READ;
        }
    }

    events[0] = wsaOverlapped.hEvent;
    events[1] = interruptEvent;

    ret = WSAWaitForMultipleEvents(2, events, FALSE, timeout, TRUE);
    switch (ret) {
    case WSA_WAIT_EVENT_0:
        flags = 0;
        if (WSAGetOverlappedResult(ctx->udpSock, &wsaOverlapped, recved, TRUE, &flags)) {
            WSAResetEvent(wsaOverlapped.hEvent);
            wsaOverlapped.hEvent = INVALID_HANDLE_VALUE;
            *data = buffer;
            return AJ_OK;
        } else {
            AJ_ErrPrintf(("AJ_ARDP_UDP_Recv(): WSAGetOverlappedResult error; WSAGetLastError()=%d\n", WSAGetLastError()));
            return AJ_ERR_READ;
        }
        break;

    case WSA_WAIT_EVENT_0 + 1:
        WSAResetEvent(interruptEvent);
        return AJ_ERR_INTERRUPTED;

    case WSA_WAIT_TIMEOUT:
        return AJ_ERR_TIMEOUT;
        break;

    default:
        AJ_ErrPrintf(("AJ_ARDP_UDP_Recv(): WSAWaitForMultipleEvents error; WSAGetLastError()=%d\n", WSAGetLastError()));
        return AJ_ERR_READ;
    }
}
Example #12
0
AJ_Status AJNS_Producer_ConnectedHandler(AJ_BusAttachment* busAttachment)
{
    AJ_Status status;
    AJ_SessionOpts sessionOpts = {
        AJ_SESSION_TRAFFIC_MESSAGES,
        AJ_SESSION_PROXIMITY_ANY,
        AJ_TRANSPORT_ANY,
        FALSE
    };
    uint8_t serviceStarted;
    AJ_Message msg;

    status = AJ_BusBindSessionPort(busAttachment, AJNS_NotificationProducerPort, &sessionOpts, 0);
    if (status != AJ_OK) {
        AJ_ErrPrintf(("Failed to send bind session port message\n"));
    }

    serviceStarted = FALSE;
    while (!serviceStarted && (status == AJ_OK)) {

        status = AJ_UnmarshalMsg(busAttachment, &msg, AJ_UNMARSHAL_TIMEOUT);
        if (status == AJ_ERR_NO_MATCH) {
            status = AJ_OK;
            continue;
        } else if (status != AJ_OK) {
            break;
        }

        switch (msg.msgId) {
        case AJ_REPLY_ID(AJ_METHOD_BIND_SESSION_PORT):
            if (msg.hdr->msgType == AJ_MSG_ERROR) {
                status = AJ_ERR_FAILURE;
            } else {
                serviceStarted = TRUE;
            }
            break;

        default:
            /*
             * Pass to the built-in bus message handlers
             */
            status = AJ_BusHandleBusMessage(&msg);
            break;
        }
        AJ_CloseMsg(&msg);
    }

    if (status != AJ_OK) {
        AJ_ErrPrintf(("AllJoyn disconnect bus status=%d\n", status));
        status = AJ_ERR_READ;
    }
    return status;
}
AJ_Status AJ_BusLinkStateProc(AJ_BusAttachment* bus)
{
    AJ_Status status = AJ_OK;
    if (bus->isProbeRequired && busLinkTimeout) 
	{
        if (!busLinkWatcher.linkTimerInited) 
		{
            busLinkWatcher.linkTimerInited = TRUE;
            AJ_InitTimer(&(busLinkWatcher.linkTimer));
        }
		else
		{
            uint32_t eclipse = AJ_GetElapsedTime(&(busLinkWatcher.linkTimer), TRUE);
            if (eclipse >= busLinkTimeout) 
			{
                if (!busLinkWatcher.pingTimerInited) 
				{
                    busLinkWatcher.pingTimerInited = TRUE;
                    AJ_InitTimer(&(busLinkWatcher.pingTimer));
                    if (AJ_OK != AJ_SendLinkProbeReq(bus)) 
					{
                        AJ_ErrPrintf(("AJ_BusLinkStateProc(): AJ_SendLinkProbeReq() failure"));
                    }
                } 
				else
				{
                    eclipse = AJ_GetElapsedTime(&(busLinkWatcher.pingTimer), TRUE);
                    if (eclipse >=  AJ_BUS_LINK_PING_TIMEOUT) 
					{
                        if (++busLinkWatcher.numOfPingTimedOut < AJ_MAX_LINK_PING_PACKETS) 
						{
                            AJ_InitTimer(&(busLinkWatcher.pingTimer));
                            if (AJ_OK != AJ_SendLinkProbeReq(bus)) 
							{
                                AJ_ErrPrintf(("AJ_BusLinkStateProc(): AJ_SendLinkProbeReq() failure"));
                            }
                        } 
						else
						{
                            AJ_ErrPrintf(("AJ_BusLinkStateProc(): AJ_ERR_LINK_TIMEOUT"));
                            status = AJ_ERR_LINK_TIMEOUT;
                            // stop sending probe messages until next link timeout event
                            memset(&busLinkWatcher, 0, sizeof(AJ_BusLinkWatcher));
                        }
                    }
                }
            }
        }
    }
    return status;
}
Example #14
0
/*
 * Implements AES-CCM (Counter with CBC-MAC) decryption as described in RFC 3610
 */
AJ_Status AJ_Decrypt_CCM(const uint8_t* key,
                         uint8_t* msg,
                         uint32_t msgLen,
                         uint32_t hdrLen,
                         uint8_t tagLen,
                         const uint8_t* nonce,
                         uint32_t nLen)
{
    AJ_Status status = AJ_OK;
    CCM_Context* context;

    if (!(context = InitCCMContext(nonce, nLen, hdrLen, msgLen, tagLen))) {
        AJ_ErrPrintf(("AJ_Decrypt_CCM(): AJ_ERR_RESOURCES\n"));
        return AJ_ERR_RESOURCES;
    }
    /*
     * Do any platform specific operations to enable AES
     */
    AJ_AES_Enable(key);
    /*
     * Decrypt the authentication field
     */
    AJ_AES_CTR_128(key, msg + msgLen, msg + msgLen, tagLen, context->ivec.data);
    /*
     * Decrypt message.
     */
    if (msgLen != hdrLen) {
        AJ_AES_CTR_128(key, msg + hdrLen, msg + hdrLen, msgLen - hdrLen, context->ivec.data);
    }
    /*
     * Compute and verify the authentication tag T.
     */
    Compute_CCM_AuthTag(key, context, msg, msgLen - hdrLen, hdrLen);
    /*
     * Balance the enable call above
     */
    AJ_AES_Disable();
    if (AJ_Crypto_Compare(context->T.data, msg + msgLen, tagLen) != 0) {
        /*
         * Authentication failed Clear the decrypted data
         */
        memset(msg, 0, msgLen + tagLen);
        AJ_ErrPrintf(("AJ_Decrypt_CCM(): AJ_ERR_SECURITY\n"));
        status = AJ_ERR_SECURITY;
    }
    /*
     * Done with the context
     */
    AJ_Free(context);
    return status;
}
Example #15
0
static AJ_Status AJ_Net_Send(AJ_IOBuffer* buf)
{
    DWORD ret;
    DWORD tx = AJ_IO_BUF_AVAIL(buf);

    AJ_InfoPrintf(("AJ_Net_Send(buf=0x%p)\n", buf));

    assert(buf->direction == AJ_IO_BUF_TX);

    if (tx > 0) {
        NetContext* ctx = (NetContext*) buf->context;
        WSAOVERLAPPED ov;
        DWORD flags = 0;
        WSABUF wsbuf;

        memset(&ov, 0, sizeof(ov));
        ov.hEvent = sendEvent;
        wsbuf.len = tx;
        wsbuf.buf = buf->readPtr;

        ret = WSASend(ctx->tcpSock, &wsbuf, 1, NULL, flags, &ov, NULL);
        if (!WSAGetOverlappedResult(ctx->tcpSock, &ov, &tx, TRUE, &flags)) {
            AJ_ErrPrintf(("AJ_Net_Send(): send() failed. WSAGetLastError()=0x%x, status=AJ_ERR_WRITE\n", WSAGetLastError()));
            return AJ_ERR_WRITE;
        }
        buf->readPtr += tx;
    }
    if (AJ_IO_BUF_AVAIL(buf) == 0) {
        AJ_IO_BUF_RESET(buf);
    }
    AJ_InfoPrintf(("AJ_Net_Send(): status=AJ_OK\n"));
    return AJ_OK;
}
Example #16
0
/**
 * Since the routing node expects any of its clients to use SASL with Anonymous
 * or PINX in order to connect, this method will send the necessary SASL
 * Anonymous exchange in order to connect.  PINX is no longer supported on the
 * Thin Client.  All thin clients will connect as untrusted clients to the
 * routing node.
 */
static AJ_Status AnonymousAuthAdvance(AJ_IOBuffer* rxBuf, AJ_IOBuffer* txBuf)
{
    AJ_Status status = AJ_OK;
    AJ_GUID localGuid;
    char buf[40];

    /* initiate the SASL exchange with AUTH ANONYMOUS */
    status = WriteLine(txBuf, "AUTH ANONYMOUS\n");
    ResetRead(rxBuf);

    if (status == AJ_OK) {
        /* expect server to send back OK GUID */
        status = ReadLine(rxBuf);
        if (status == AJ_OK) {
            if (memcmp(rxBuf->readPtr, "OK", 2) != 0) {
                return AJ_ERR_ACCESS_ROUTING_NODE;
            }
        }
    }

    if (status == AJ_OK) {
        status = WriteLine(txBuf, "INFORM_PROTO_VERSION 10\n");
        ResetRead(rxBuf);
    }

    if (status == AJ_OK) {
        /* expect server to send back INFORM_PROTO_VERSION version# */
        status = ReadLine(rxBuf);
    }

    if (status == AJ_OK) {
        if (memcmp(rxBuf->readPtr, "INFORM_PROTO_VERSION", strlen("INFORM_PROTO_VERSION")) != 0) {
            status = AJ_ERR_ACCESS_ROUTING_NODE;
        }
    }

    if (status == AJ_OK) {
        routingProtoVersion = atoi((const char*)(rxBuf->readPtr + strlen("INFORM_PROTO_VERSION") + 1));
        if (routingProtoVersion < AJ_GetMinProtoVersion()) {
            AJ_InfoPrintf(("AnonymousAuthAdvance():: Found version %u but minimum %u required", routingProtoVersion, AJ_GetMinProtoVersion()));
            status = AJ_ERR_OLD_VERSION;
        }
    }

    if (status == AJ_OK) {
        /* send BEGIN LocalGUID to server */
        AJ_GetLocalGUID(&localGuid);
        strcpy(buf, "BEGIN ");
        status = AJ_GUID_ToString(&localGuid, buf + strlen(buf), 33);
        strcat(buf, "\n");
        status = WriteLine(txBuf, buf);
        ResetRead(rxBuf);
    }

    if (status != AJ_OK) {
        AJ_ErrPrintf(("AnonymousAuthAdvance(): status=%s\n", AJ_StatusText(status)));
    }

    return status;
}
AJ_Status AJS_TargetIO_PinEnableTrigger(void* pinCtx, AJS_IO_PinTriggerMode trigger, int32_t* trigId, uint8_t debounce)
{
    GPIO* gpio = (GPIO*)pinCtx;

    if ((trigger != AJS_IO_PIN_TRIGGER_ON_RISE) && (trigger != AJS_IO_PIN_TRIGGER_ON_FALL)) {
        /*
         * Disable triggers for this pin
         */
        if (gpio->trigId != AJS_IO_PIN_NO_TRIGGER) {
            SendCmd('i', gpio, 0, 0);
            *trigId = gpio->trigId;
            BIT_CLR(trigSet, gpio->trigId);
            gpio->trigId = AJS_IO_PIN_NO_TRIGGER;
        } else {
            *trigId = AJS_IO_PIN_NO_TRIGGER;
        }
        return AJ_OK;
    }
    SendCmd('i', gpio, (trigger == AJS_IO_PIN_TRIGGER_ON_RISE) ? 1 : 2, 0);
    gpio->trigId = gpio->pinId;
    *trigId = gpio->trigId;

    AJ_ErrPrintf(("AJS_TargetIO_PinEnableTrigger pinId %d\n", gpio->pinId));
    return AJ_OK;
}
static void* SockRead(void* arg)
{
    while (sock != -1) {
        uint8_t buf[3];
        int ret = recv(sock, buf, sizeof(buf), MSG_WAITALL);
        if (ret != 3) {
            AJ_ErrPrintf(("Recv failed - closing socket\n"));
            close(sock);
            sock = -1;
            break;
        }
        if (buf[0] == 'r') {
            memcpy(recvBuf, buf, ret);
            pthread_mutex_lock(&mutex);
            pthread_cond_signal(&cond);
            pthread_mutex_unlock(&mutex);
        } else if (buf[0] == 'i') {
            /*
             * Record which trigger fired
             */
            BIT_SET(trigSet, buf[1] - 1);
            AJ_Net_Interrupt();
        }
    }
    pthread_cond_destroy(&cond);
    pthread_mutex_destroy(&mutex);
    return NULL;
}
void AJS_TargetIO_PinSet(void* pinCtx, uint32_t val)
{
    GPIO* gpio = (GPIO*)pinCtx;
    if (!SendCmd('w', gpio, (uint8_t)val, 0)) {
        AJ_ErrPrintf(("AJS_TargetIO_PinSet(%d, %d)\n", gpio->pinId, val));
    }
}
/*
 * Check that the signature in the message matches the encoded signature in the string
 */
static AJ_Status CheckSignature(const char* encoding, const AJ_Message* msg)
{
    const char* sig = msg->signature ? msg->signature : "";
    char direction = (msg->hdr->msgType == AJ_MSG_METHOD_CALL) ? IN_ARG : OUT_ARG;

    /*
     * Wild card in the message is ok.
     */
    if (*sig == '*') {
        return AJ_OK;
    }
    while (*encoding) {
        /*
         * Skip until we find a direction character
         */
        while (*encoding && (*encoding++ != direction)) {
        }
        /*
         * Match a single arg to the signature
         */
        while (*encoding && (*sig == *encoding)) {
            ++sig;
            ++encoding;
        }
        if (*encoding && (*encoding != ' ')) {
            AJ_ErrPrintf(("CheckSignature(): AJ_ERR_SIGNATURE\n"));
            return AJ_ERR_SIGNATURE;
        }
    }
    /*
     * On a match we should have consumed both strings
     */
    return (*encoding == *sig) ? AJ_OK : AJ_ERR_SIGNATURE;
}
static uint8_t AJRouter_Connect(AJ_BusAttachment* busAttachment, const char* routerName)
{
    AJ_Status status;
    const char* busUniqueName;

    while (TRUE) {
        AJ_InfoPrintf(("Attempting to connect to bus '%s'\n", routerName));
        status = AJ_FindBusAndConnect(busAttachment, routerName, AJAPP_CONNECT_TIMEOUT);
        if (status != AJ_OK) {
            AJ_InfoPrintf(("Failed to connect to bus sleeping for %d seconds\n", AJAPP_CONNECT_PAUSE / 1000));
            AJ_Sleep(AJAPP_CONNECT_PAUSE);
            continue;
        }
        busUniqueName = AJ_GetUniqueName(busAttachment);
        if (busUniqueName == NULL) {
            AJ_ErrPrintf(("Failed to GetUniqueName() from newly connected bus, retrying\n"));
            continue;
        }
        AJ_InfoPrintf(("Connected to router with BusUniqueName=%s\n", busUniqueName));
        break;
    }

    /* Setup password based authentication listener for secured peer to peer connections */
    AJ_BusSetPasswordCallback(busAttachment, PasswordCallback);

    /* Configure timeout for the link to the Router bus */
    AJ_SetBusLinkTimeout(busAttachment, 60);     // 60 seconds

    return TRUE;
}
Example #22
0
AJ_Status AJ_GUID_HandleAddMatchReply(AJ_Message* msg)
{
    AJ_Status status;
    NameToGUID* mapping;
    uint32_t serialNum = 0;

  //  AJ_InfoPrintf(("AJ_GUID_HandleAddMatchReply(msg=0x%p)\n", msg));

    mapping = LookupReplySerial(msg->replySerial);
    if (!mapping) 
	{
        return AJ_OK;
    }
    mapping->replySerial = 0;

    if (msg->hdr->msgType == AJ_MSG_ERROR) 
	{
        AJ_ErrPrintf(("AJ_GUID_HandleAddMatchReply(msg=0x%p): error=%s.\n", msg, msg->error));
        AJ_GUID_DeleteNameMapping(msg->bus, mapping->uniqueName);
        return AJ_ERR_FAILURE;
    }

    // Add match complete.
   // AJ_InfoPrintf(("Add match Complete\n"));
    status = NameHasOwner(msg, mapping->uniqueName, &serialNum);
    if (status == AJ_OK) 
	{
        mapping->replySerial = serialNum;
    } 
	else 
	{
        AJ_GUID_DeleteNameMapping(msg->bus, mapping->uniqueName);
    }
    return status;
}
Example #23
0
AJ_Status AJ_Net_MCastUp(AJ_MCastSocket* mcastSock)
{
    uint8_t ret = 0;

    AJ_InfoPrintf(("AJ_Net_MCastUp(mcastSock=0x%p)\n", mcastSock));

    //
    // Arduino does not choose an ephemeral port if we enter 0 -- it happily
    // uses 0 and then increments each time we bind, up through the well-known
    // system ports.
    //
    ret = g_clientUDP.begin(AJ_EphemeralPort());

    if (ret != 1) {
        g_clientUDP.stop();
        AJ_ErrPrintf(("AJ_Net_MCastUp(): begin() fails. status=AJ_ERR_READ\n"));
        return AJ_ERR_READ;
    } else {
        AJ_IOBufInit(&mcastSock->rx, rxData, sizeof(rxData), AJ_IO_BUF_RX, (void*)&g_clientUDP);
        mcastSock->rx.recv = AJ_Net_RecvFrom;
        AJ_IOBufInit(&mcastSock->tx, txData, sizeof(txData), AJ_IO_BUF_TX, (void*)&g_clientUDP);
        mcastSock->tx.send = AJ_Net_SendTo;
    }

    AJ_InfoPrintf(("AJ_Net_MCastUp(): status=AJ_OK\n"));
    return AJ_OK;
}
Example #24
0
void EXTI9_5_IRQHandler(void)
{
    if (EXTI_GetITStatus(EXTI_Line5) != RESET) {
        //Handle the interrupt
        AJ_Printf("EXTI5_IRQHandler\n");
        EXTI_ClearITPendingBit(EXTI_Line5);
        EXTI_ClearFlag(EXTI_Line5);
    } else if (EXTI_GetITStatus(EXTI_Line6) != RESET) {
        AJ_Printf("EXTI6_IRQHandler\n");
        EXTI_ClearITPendingBit(EXTI_Line6);
        EXTI_ClearFlag(EXTI_Line6);
    } else if (EXTI_GetITStatus(EXTI_Line7) != RESET) {
        AJ_Printf("EXTI7_IRQHandler\n");
        EXTI_ClearITPendingBit(EXTI_Line7);
        EXTI_ClearFlag(EXTI_Line7);
    } else if (EXTI_GetITStatus(EXTI_Line8) != RESET) {
        AJ_Printf("EXTI8_IRQHandler\n");
        EXTI_ClearITPendingBit(EXTI_Line8);
        EXTI_ClearFlag(EXTI_Line8);
    } else if (EXTI_GetITStatus(EXTI_Line9) != RESET) {
        AJ_Printf("EXTI9_IRQHandler\n");
        EXTI_ClearITPendingBit(EXTI_Line9);
        EXTI_ClearFlag(EXTI_Line9);
    } else {
        AJ_ErrPrintf(("EXTI9_5_IRQHandler(): IRQ Error\n"));
    }
}
/**
 * Send notify signal
 */
static AJ_Status AJNS_Producer_SendNotifySignal(AJ_BusAttachment* busAttachment, AJNS_Notification* notification, uint32_t ttl, uint32_t* messageSerialNumber)
{
    AJ_Status status;
    AJ_Message msg;
    uint32_t serialNum;

//    AJ_InfoPrintf(("In SendNotifySignal\n"));
    status = AJNS_Producer_MarshalNotificationMsg(busAttachment, &msg, notification, ttl);
    if (status != AJ_OK) 
	{
 //       AJ_InfoPrintf(("Could not Marshal Message\n"));
        return status;
    }
    serialNum = msg.hdr->serialNum;
    status = AJ_DeliverMsg(&msg);
    if (status != AJ_OK) 
	{
        AJ_ErrPrintf(("Could not Deliver Message\n"));
        return status;
    }
 //   AJ_InfoPrintf(("***************** Notification id %d delivered successfully with serial number %u *****************\n", notification->notificationId, serialNum));
    if (messageSerialNumber != NULL)
	{
        *messageSerialNumber = serialNum;
    }

    AJ_CloseMsg(&msg);

    return status;
}
Example #26
0
void EXTI15_10_IRQHandler(void)
{
    if (EXTI_GetITStatus(EXTI_Line10) != RESET) {
        //Handle the interrupt
        AJ_Printf("EXTI10_IRQHandler\n");
        EXTI_ClearITPendingBit(EXTI_Line10);
        EXTI_ClearFlag(EXTI_Line10);
    } else if (EXTI_GetITStatus(EXTI_Line11) != RESET) {
        AJ_Printf("EXTI11_IRQHandler\n");
        EXTI_ClearITPendingBit(EXTI_Line11);
        EXTI_ClearFlag(EXTI_Line11);
    } else if (EXTI_GetITStatus(EXTI_Line12) != RESET) {
        AJ_Printf("EXTI12_IRQHandler\n");
        EXTI_ClearITPendingBit(EXTI_Line12);
        EXTI_ClearFlag(EXTI_Line12);
    } else if (EXTI_GetITStatus(EXTI_Line13) != RESET) {
        AJ_Printf("EXTI13_IRQHandler\n");
        EXTI_ClearITPendingBit(EXTI_Line13);
        EXTI_ClearFlag(EXTI_Line13);
    } else if (EXTI_GetITStatus(EXTI_Line14) != RESET) {
        AJ_Printf("EXTI14_IRQHandler\n");
        EXTI_ClearITPendingBit(EXTI_Line14);
        EXTI_ClearFlag(EXTI_Line14);
    } else if (EXTI_GetITStatus(EXTI_Line15) != RESET) {
        AJ_Printf("EXTI15_IRQHandler\n");
        EXTI_ClearITPendingBit(EXTI_Line15);
        EXTI_ClearFlag(EXTI_Line15);
    } else {
        AJ_ErrPrintf(("EXTI15_10_IRQHandler(): IRQ Error\n"));
    }
}
Example #27
0
void AJ_WSL_ModuleInit(void)
{
    //prepare the WSL heap
    size_t heapSz;

    heapSz = AJ_PoolRequired(wsl_heapConfig, ArraySize(wsl_heapConfig));
    if (heapSz > sizeof(wsl_heap)) {
        AJ_ErrPrintf(("Heap space is too small %d required %d\n", sizeof(wsl_heap), heapSz));
        return;
    }
    AJ_InfoPrintf(("Allocated heap %d bytes\n", (int)heapSz));
    AJ_PoolInit(wsl_heap, heapSz, wsl_heapConfig, ArraySize(wsl_heapConfig));
    AJ_PoolDump();

    AJ_WSL_SOCKNUM i;
    for (i = 0; i < AJ_WSL_SOCKET_MAX; i++) {
        memset(&AJ_WSL_SOCKET_CONTEXT[i], 0, sizeof(AJ_WSL_SOCKET_CONTEXT[0]));
        AJ_WSL_SOCKET_CONTEXT[i].targetHandle = AJ_WSL_SOCKET_HANDLE_INVALID;
        AJ_WSL_SOCKET_CONTEXT[i].stashedRxList = AJ_BufListCreate();
        AJ_WSL_SOCKET_CONTEXT[i].workRxQueue = AJ_QueueCreate("RxQueue");
        AJ_WSL_SOCKET_CONTEXT[i].workTxQueue = AJ_QueueCreate("TxQueue");
    }

    AJ_WSL_WMI_ModuleInit();

}
AJ_Status AJS_TargetIO_PinClose(void* pinCtx)
{
    GPIO* gpio = (GPIO*)pinCtx;
    AJ_ErrPrintf(("AJS_TargetIO_PinClose(%d)\n", gpio->pinId));
    free(gpio);
    return AJ_OK;
}
Example #29
0
static AJ_Status ComposeWhoHas(AJ_IOBuffer* txBuf, const char* prefix)
{
    size_t preLen = strlen(prefix);
    NSHeader* hdr = (NSHeader*)txBuf->writePtr;
    uint8_t* p = txBuf->writePtr + 6;
    size_t outLen = (6 + preLen + 2);

    AJ_InfoPrintf(("ComposeWhoHas(txbuf=0x%p, prefix=\"%s\")\n", txBuf, prefix));

    if (outLen > AJ_IO_BUF_SPACE(txBuf)) {
        AJ_ErrPrintf(("ComposeWhoHas(): AJ_ERR_RESOURCES\n"));
        return AJ_ERR_RESOURCES;
    }
    hdr->version = MSG_V1 | NSV_V1;
    hdr->qCount = 1;
    hdr->aCount = 0;
    hdr->ttl = 0;
    hdr->flags = WHO_HAS_MSG;
    hdr->nameCount = 1;
    *p++ = (uint8_t)(preLen + 1);
    memcpy(p, prefix, preLen);
    /*
     * Tack wild-card onto the end of the name to indicate it's prefix
     */
    p[preLen] = '*';
    txBuf->writePtr += outLen;
    return AJ_OK;
}
void AJS_TargetIO_PinToggle(void* pinCtx)
{
    GPIO* gpio = (GPIO*)pinCtx;
    if (!SendCmd('t', gpio, 0, 0)) {
        AJ_ErrPrintf(("AJS_TargetIO_PinToggle(%d)\n", gpio->pinId));
    }
}