static AJ_Status AJApp_ConnectedHandler(AJ_BusAttachment* busAttachment)
{
    AJ_Status status = AJ_OK;

    if (AJ_GetUniqueName(busAttachment)) {
        if (currentServicesInitializationState == nextServicesInitializationState) {
            switch (currentServicesInitializationState) {
            case INIT_SERVICES:
                status = AJSVC_ConnectedHandler(busAttachment);
                if (status != AJ_OK) {
                    goto ErrorExit;
                }
                currentServicesInitializationState = nextServicesInitializationState = INIT_FINISHED;
                break;

            case INIT_FINISHED:
            default:
                break;
            }
        }
    }
    return status;

ErrorExit:

    AJ_ErrPrintf(("Application ConnectedHandler returned an error %s\n", (AJ_StatusText(status))));
    return status;
}
static AJ_Status AJApp_ConnectedHandler(AJ_BusAttachment* busAttachment)
{
    AJ_Status status = AJ_OK;

    if (AJ_GetUniqueName(busAttachment)) {
        if (currentServicesInitializationState == nextServicesInitializationState) {
            switch (currentServicesInitializationState) {
            case INIT_SERVICES:
                status = AJSVC_ConnectedHandler(busAttachment);
                if (status != AJ_OK) {
                    goto ErrorExit;
                }
                currentServicesInitializationState = nextServicesInitializationState = INIT_SERVICES_PORT;
                break;

            case INIT_SERVICES_PORT:
                status = AJ_BusBindSessionPort(busAttachment, AJ_ABOUT_SERVICE_PORT, NULL, 0);
                if (status != AJ_OK) {
                    goto ErrorExit;
                }
                nextServicesInitializationState = INIT_ADVERTISE_NAME;
                break;

            case INIT_ADVERTISE_NAME:
                status = AJ_BusAdvertiseName(busAttachment, AJ_GetUniqueName(busAttachment), AJ_TRANSPORT_ANY, AJ_BUS_START_ADVERTISING, 0);
                if (status != AJ_OK) {
                    goto ErrorExit;
                }
                currentServicesInitializationState = nextServicesInitializationState = INIT_ABOUT;
                break;

            case INIT_ABOUT:
                status = AJ_AboutInit(busAttachment, AJ_ABOUT_SERVICE_PORT);
                if (status != AJ_OK) {
                    goto ErrorExit;
                }
                currentServicesInitializationState = nextServicesInitializationState = INIT_CHECK_ANNOUNCE;
                break;

            case INIT_CHECK_ANNOUNCE:
                status = AJ_AboutAnnounce(busAttachment);
                if (status != AJ_OK) {
                    goto ErrorExit;
                }
                break;

            default:
                break;
            }
        }
    }
    return status;

ErrorExit:

    AJ_ErrPrintf(("Application ConnectedHandler returned an error %s\n", (AJ_StatusText(status))));
    return status;
}
Exemple #3
0
AJ_Status AJS_AttachAllJoyn(AJ_BusAttachment* aj)
{
    AJ_Status status;
    uint8_t isConnected = FALSE;
    uint32_t linkTO = LINK_TO;
    size_t sapSz;

    /*
     * Initialize the onboarding service
     */
    sapSz = min(sizeof(obSettings.AJOBS_SoftAPSSID), sizeof(softAPSSID));
    memcpy((char*)obSettings.AJOBS_SoftAPSSID, softAPSSID, sapSz);
    status = AJOBS_Start(&obSettings);
    if (status != AJ_OK) {
        goto Exit;
    }
    AJ_InfoPrintf(("Attempting to attach to AllJoyn\n"));
    while (!isConnected) {
        status = AJSVC_RoutingNodeConnect(aj, busNode, CONNECT_TO, CONNECT_PAUSE, linkTO, &isConnected);
        if (status != AJ_ERR_TIMEOUT) {
            break;
        }
    }
    if (isConnected) {
        status = AJS_ServicesInit(aj);
        if (status != AJ_OK) {
            AJ_ErrPrintf(("Failed to initialize services"));
        }
    }
    if (isConnected && (AJOBS_GetState() != AJOBS_STATE_CONFIGURED_VALIDATED)) {
        /*
         * Kick of onboarding
         */
        status = AJ_BusBindSessionPort(aj, ONBOARDING_PORT, NULL, 0);
        /*
         * Allow onboarding service to run its course
         */
        while (status == AJ_OK) {
            AJ_Message msg;
            status = AJ_UnmarshalMsg(aj, &msg, MSG_TO);
            if (status == AJ_ERR_NO_MATCH) {
                /*
                 * Discard unknown messages
                 */
                status = AJ_CloseMsg(&msg);
                continue;
            }
            if (status == AJ_ERR_TIMEOUT) {
                /*
                 * Check the link is still up
                 */
                status = AJ_BusLinkStateProc(aj);
                if (status == AJ_OK) {
                    continue;
                }
            }
            if (status != AJ_OK) {
                break;
            }
            switch (msg.msgId) {
            case AJ_REPLY_ID(AJ_METHOD_BIND_SESSION_PORT):
                AJ_AboutInit(aj, ONBOARDING_PORT);
                break;

            case AJ_METHOD_ACCEPT_SESSION:
                /*
                 * TODO - check that the port number is correct
                 */
                status = AJ_BusReplyAcceptSession(&msg, TRUE);
                if (status == AJ_OK) {
                    status = AJOBS_ConnectedHandler(aj);
                }
                break;

            default:
                /*
                 * Let the service message handlers have first dibs on the message
                 */
                status = AJS_ServicesMsgHandler(&msg);
                if (status == AJ_ERR_NO_MATCH) {
                    /*
                     * Pass the unhandled message to the standard bus handler
                     */
                    status = AJ_BusHandleBusMessage(&msg);
                    if (status == AJ_ERR_NO_MATCH) {
                        AJ_ErrPrintf(("Discarding unhandled message\n"));
                        status = AJ_OK;
                    }
                }
                break;
            }
            /*
             * Let the link monitor know we are receiving messages
             */
            AJ_NotifyLinkActive();
            /*
             * Free resources used by the message
             */
            AJ_CloseMsg(&msg);
            if (AJOBS_GetState() == AJOBS_STATE_CONFIGURED_VALIDATED) {
                AJ_InfoPrintf(("Onboarding completed\n"));
                break;
            }
            AJ_InfoPrintf(("Onboarding continuing\n"));
        }
        /*
         * If we got an error during onboarding we should restart
         */
        if (status != AJ_OK) {
            status = AJ_ERR_RESTART;
        }
    }
    /*
     * If all went well let the services know we are connected
     */
    if (isConnected && (status == AJ_OK)) {
        /*
         * Add match rules to subscribe to session related signals
         */
        status = AJ_BusSetSignalRuleFlags(aj, sessionLostMatchRule, AJ_BUS_SIGNAL_ALLOW, AJ_FLAG_NO_REPLY_EXPECTED);
        if (status == AJ_OK) {
            status = AJSVC_ConnectedHandler(aj);
        }
    }

Exit:
    return status;
}