Exemplo n.º 1
0
static QStatus build_interface(alljoyn_busattachment bus)
{
    QStatus status;

    alljoyn_interfacedescription intf = NULL;
    status = alljoyn_busattachment_createinterface(bus, INTF_NAME, &intf);
    QCC_ASSERT(ER_OK == status);
    status = alljoyn_interfacedescription_addproperty(intf, "IsOpen", "b", ALLJOYN_PROP_ACCESS_READ);
    QCC_ASSERT(ER_OK == status);
    status = alljoyn_interfacedescription_addpropertyannotation(intf, "IsOpen", "org.freedesktop.DBus.Property.EmitsChangedSignal", "true");
    QCC_ASSERT(ER_OK == status);
    status = alljoyn_interfacedescription_addproperty(intf, "Location", "s", ALLJOYN_PROP_ACCESS_READ);
    QCC_ASSERT(ER_OK == status);
    status = alljoyn_interfacedescription_addpropertyannotation(intf, "Location", "org.freedesktop.DBus.Property.EmitsChangedSignal", "true");
    QCC_ASSERT(ER_OK == status);
    status = alljoyn_interfacedescription_addproperty(intf, "KeyCode", "u", ALLJOYN_PROP_ACCESS_READ);
    QCC_ASSERT(ER_OK == status);
    status = alljoyn_interfacedescription_addpropertyannotation(intf, "KeyCode", "org.freedesktop.DBus.Property.EmitsChangedSignal", "invalidates");
    QCC_ASSERT(ER_OK == status);

    status = alljoyn_interfacedescription_addmethod(intf, "Open", "", "", "", 0, NULL);
    QCC_ASSERT(ER_OK == status);
    status = alljoyn_interfacedescription_addmethod(intf, "Close", "", "", "", 0, NULL);
    QCC_ASSERT(ER_OK == status);
    status = alljoyn_interfacedescription_addmethod(intf, "KnockAndRun", "", "", "", ALLJOYN_MEMBER_ANNOTATE_NO_REPLY, NULL);
    QCC_ASSERT(ER_OK == status);

    status = alljoyn_interfacedescription_addsignal(intf, "PersonPassedThrough", "s", "name", 0, NULL);
    QCC_ASSERT(ER_OK == status);

    alljoyn_interfacedescription_activate(intf);

    return status;
}
Exemplo n.º 2
0
int main(int argc, char** argv)
{
    QStatus status = ER_OK;
    const char* keyStore = NULL;
    int i = 0;

    alljoyn_interfacedescription intf = NULL;
    alljoyn_interfacedescription intfvalue = NULL;

    /* SessionPort Listeners. */
    alljoyn_sessionportlistener_callbacks spl_cbs = {
        &accept_session_joiner,
        &session_joined
    };

    /* SessionPort Listeners. */
    alljoyn_sessionlistener_callbacks sl_cbs = {
        &session_lost, //session lost callback
        NULL, //session member added callback
        NULL //session member lost callback
    };

    /* Bus Object callbacks */
    alljoyn_busobject_callbacks busObjCbs = {
        &property_get,
        &property_set,
        &busobject_object_registered,
        &busobject_object_unregistered
    };

    /* Auth listener callbacks. */
    alljoyn_authlistenerasync_callbacks authcbs = {
        request_credentials_async,
        verify_credentials_async,
        security_violation,
        authentication_complete
    };

    alljoyn_authlistener authListener;

    printf("AllJoyn Library version: %s\n", alljoyn_getversion());
    printf("AllJoyn Library build info: %s\n", alljoyn_getbuildinfo());

    /* Install SIGINT handler */
    signal(SIGINT, SigIntHandler);

    g_wellKnownName = (char*)DEFAULT_WELLKNOWN_NAME;

    g_sessionOpts = alljoyn_sessionopts_create(ALLJOYN_TRAFFIC_TYPE_MESSAGES, QCC_FALSE, ALLJOYN_PROXIMITY_ANY, ALLJOYN_TRANSPORT_ANY);

    /* Parse command line args */
    for (i = 1; i < argc; ++i) {
        if (0 == strcmp("-h", argv[i]) || 0 == strcmp("-?", argv[i])) {
            usage();
            exit(0);
        } else if (0 == strcmp("-p", argv[i])) {
            if (g_echo_signal) {
                printf("options -e and -p are mutually exclusive\n");
                usage();
                exit(1);
            }
            g_ping_back = QCC_TRUE;
        } else if (0 == strcmp("-e", argv[i])) {
            if (g_ping_back) {
                printf("options -p and -e are mutually exclusive\n");
                usage();
                exit(1);
            }
            g_echo_signal = QCC_TRUE;;
        } else if (0 == strcmp("-x", argv[i])) {
            g_compress = QCC_TRUE;
        } else if (0 == strcmp("-i", argv[i])) {
            ++i;
            if (i == argc) {
                printf("option %s requires a parameter\n", argv[i - 1]);
                usage();
                exit(1);
            } else {
                g_reportInterval = strtoul(argv[i], NULL, 10);
            }
        } else if (0 == strcmp("-n", argv[i])) {
            ++i;
            if (i == argc) {
                printf("option %s requires a parameter\n", argv[i - 1]);
                usage();
                exit(1);
            } else {
                g_wellKnownName = argv[i];
            }
        } else if (0 == strcmp("-k", argv[i])) {
            ++i;
            if (i == argc) {
                printf("option %s requires a parameter\n", argv[i - 1]);
                usage();
                exit(1);
            } else {
                keyStore = argv[i];
            }
        } else if (0 == strcmp("-kx", argv[i])) {
            ++i;
            if (i == argc) {
                printf("option %s requires a parameter\n", argv[i - 1]);
                usage();
                exit(1);
            } else {
                g_keyExpiration = strtoul(argv[i], NULL, 10);
            }
        } else if (0 == strcmp("-m", argv[i])) {
            alljoyn_sessionopts_set_multipoint(g_sessionOpts, QCC_TRUE);
        } else if (0 == strcmp("-t", argv[i])) {
            alljoyn_sessionopts_set_transports(g_sessionOpts, ALLJOYN_TRANSPORT_TCP);
        } else if (0 == strcmp("-u", argv[i])) {
            alljoyn_sessionopts_set_transports(g_sessionOpts, ALLJOYN_TRANSPORT_UDP);
        } else if (0 == strcmp("-l", argv[i])) {
            alljoyn_sessionopts_set_transports(g_sessionOpts, ALLJOYN_TRANSPORT_LOCAL);
        } else if (0 == strcmp("-a", argv[i])) {
            g_cancelAdvertise = QCC_TRUE;
        } else {
            status = ER_FAIL;
            printf("Unknown option %s\n", argv[i]);
            usage();
            exit(1);
        }
    }

    //Create bus attachment
    g_msgBus = alljoyn_busattachment_create("bbcservice", QCC_TRUE);

    //Create and add interfaces to the bus

    status = alljoyn_busattachment_createinterface(g_msgBus, INTERFACE_NAME, &intf);
    if (status != ER_OK) {
        printf("Could not create %s interface because of %s. \n", INTERFACE_NAME, QCC_StatusText(status));
        return status;
    }

    status = alljoyn_busattachment_createinterface(g_msgBus, INTERFACE_VALUE_NAME, &intfvalue);
    if (status != ER_OK) {
        printf("Could not create %s interface because of %s. \n", INTERFACE_VALUE_NAME, QCC_StatusText(status));
        return status;
    }


    /* Activate org.alljoyn.alljoyn_test */
    status = alljoyn_interfacedescription_addmethod(intf, "my_ping", "s", "s", "i,i", 0, NULL);
    if (status != ER_OK) {
        printf("Could not add method %s to interface %s because of %s. \n", "my_ping", INTERFACE_NAME, QCC_StatusText(status));
        return status;
    }

    status = alljoyn_interfacedescription_addmethod(intf, "delayed_ping", "su", "s", "i,i", 0, NULL);
    if (status != ER_OK) {
        printf("Could not add method %s to interface %s because of %s. \n", "delayed_ping", INTERFACE_NAME, QCC_StatusText(status));
        return status;
    }

    status = alljoyn_interfacedescription_addmethod(intf, "time_ping", "uq", "uq", "i,i", 0, NULL);
    if (status != ER_OK) {
        printf("Could not add method %s to interface %s because of %s. \n", "time_ping", INTERFACE_NAME, QCC_StatusText(status));
        return status;
    }

    status = alljoyn_interfacedescription_addmember(intf, ALLJOYN_MESSAGE_SIGNAL, "my_signal", "a{ys}",  NULL, "inStr", 0);
    if (status != ER_OK) {
        printf("Could not add signal %s to interface %s because of %s. \n", "my_signal", INTERFACE_NAME, QCC_StatusText(status));
        return status;
    }



    alljoyn_interfacedescription_activate(intf);

    /*Activate org.alljoyn.alljoyn_test.values */
    status = alljoyn_interfacedescription_addproperty(intfvalue, "int_val", "i", ALLJOYN_PROP_ACCESS_RW);
    if (status != ER_OK) {
        printf("Could not add property %s to interface %s because of %s. \n", "int_val", INTERFACE_VALUE_NAME, QCC_StatusText(status));
        return status;
    }

    status = alljoyn_interfacedescription_addproperty(intfvalue, "str_val", "s", ALLJOYN_PROP_ACCESS_RW);
    if (status != ER_OK) {
        printf("Could not add method %s to interface %s because of %s. \n", "str_val", INTERFACE_VALUE_NAME, QCC_StatusText(status));
        return status;
    }

    status = alljoyn_interfacedescription_addproperty(intfvalue, "ro_str", "s", ALLJOYN_PROP_ACCESS_READ);
    if (status != ER_OK) {
        printf("Could not add method %s to interface %s because of %s. \n", "ro_str", INTERFACE_VALUE_NAME, QCC_StatusText(status));
        return status;
    }

    alljoyn_interfacedescription_activate(intfvalue);

    //Start the bus
    status = alljoyn_busattachment_start(g_msgBus);
    if (status != ER_OK) {
        printf("Could not start the bus because of %s. \n", QCC_StatusText(status));
        return status;
    }

    //Connect to the bus TODO - change the connect spec
    status = alljoyn_busattachment_connect(g_msgBus, "null:");
    if (status != ER_OK) {
        printf("Could not connect to the bus because of %s. \n", QCC_StatusText(status));
        return status;
    }

    /* SessionPort Listeners. */
    g_sessionPortListener = alljoyn_sessionportlistener_create(&spl_cbs, NULL);

    /* SessionPort Listeners. */
    g_sessionListener = alljoyn_sessionlistener_create(&sl_cbs, NULL);


    /* Bus Object call backs. */
    g_testObj = alljoyn_busobject_create(OBJECT_PATH, QCC_FALSE, &busObjCbs, NULL);
    status = bus_object_init();
    if (status != ER_OK) {
        printf("Bus object init failed because of %s. \n", QCC_StatusText(status));
        return status;
    }

    alljoyn_busattachment_registerbusobject(g_msgBus, g_testObj);

    /* Auth listener callbacks. */
    authListener = alljoyn_authlistenerasync_create(&authcbs, NULL);

    status = alljoyn_busattachment_enablepeersecurity(g_msgBus, "ALLJOYN_SRP_KEYX ALLJOYN_PIN_KEYX ALLJOYN_RSA_KEYX ALLJOYN_SRP_LOGON", authListener, keyStore, keyStore != NULL);
    if (ER_OK != status) {
        printf("enablePeerSecurity failed (%s)\n", QCC_StatusText(status));
        return status;
    }

    /* Add a logon entry. */
    alljoyn_busattachment_addlogonentry(g_msgBus, "ALLJOYN_SRP_LOGON", "sleepy", "123456");

    if (ER_OK == status) {
        printf("bbcservice %s ready to accept connections\n", g_wellKnownName);
        while (g_interrupt == QCC_FALSE) {
#ifdef _WIN32
            Sleep(100 * 10 * 300);
#else
            usleep(100 * 1000 * 10 * 300);
#endif
        }
    }

    /* Clean up. */
    alljoyn_busattachment_unregisterbusobject(g_msgBus, g_testObj);

    alljoyn_sessionopts_destroy(g_sessionOpts);
    alljoyn_authlistenerasync_destroy(authListener);
    alljoyn_busobject_destroy(g_testObj);
    alljoyn_sessionportlistener_destroy(g_sessionPortListener);
    alljoyn_sessionlistener_destroy(g_sessionListener);
    alljoyn_busattachment_destroy(g_msgBus);

    return 0;
}