示例#1
0
/* This is similar to calling BusObject constructor. */
QStatus AJ_CALL bus_object_init() {

    QStatus status = ER_OK;
    QCC_BOOL foundMember = QCC_FALSE;
    alljoyn_interfacedescription_member my_ping_member, my_delayed_ping_member, my_time_ping_member, my_signal_member;

    alljoyn_interfacedescription intf = NULL;
    alljoyn_interfacedescription valuesintf = NULL;

    alljoyn_busobject_methodentry methodEntries[3];

    intf = alljoyn_busattachment_getinterface(g_msgBus, INTERFACE_NAME);
    valuesintf = alljoyn_busattachment_getinterface(g_msgBus, INTERFACE_VALUE_NAME);

    if (!intf) {
        printf("ERROR - Could not fetch %s interface from the bus. \n", INTERFACE_NAME);
        return ER_FAIL;
    }

    if (!valuesintf) {
        printf("ERROR - Could not fetch %s interface from the bus. \n", INTERFACE_VALUE_NAME);
        return ER_FAIL;
    }

    /* Add interfaces to the bus object. */
    alljoyn_busobject_addinterface(g_testObj, intf);
    alljoyn_busobject_addinterface(g_testObj, valuesintf);

    /*Get the members. */
    foundMember = alljoyn_interfacedescription_getmember(intf, "my_signal", &my_signal_member);
    if (!foundMember) {
        printf("ERROR - Could not fetch %s member from %s interface. \n", "my_signal", INTERFACE_NAME);
        return ER_FAIL;
    }

    foundMember = QCC_FALSE;
    foundMember = alljoyn_interfacedescription_getmember(intf, "my_ping", &my_ping_member);
    if (!foundMember) {
        printf("ERROR - Could not fetch %s member from %s interface. \n", "my_ping", INTERFACE_NAME);
        return ER_FAIL;
    }

    foundMember = QCC_FALSE;
    foundMember = alljoyn_interfacedescription_getmember(intf, "delayed_ping", &my_delayed_ping_member);
    if (!foundMember) {
        printf("ERROR - Could not fetch %s member from %s interface. \n", "delayed_ping", INTERFACE_NAME);
        return ER_FAIL;
    }

    foundMember = QCC_FALSE;
    foundMember = alljoyn_interfacedescription_getmember(intf, "time_ping", &my_time_ping_member);
    if (!foundMember) {
        printf("ERROR - Could not fetch %s member from %s interface. \n", "time_ping", INTERFACE_NAME);
        return ER_FAIL;
    }


    /* Register a signal handler. */
    status = alljoyn_busattachment_registersignalhandler(g_msgBus, &signal_handler, my_signal_member, NULL);
    if (ER_OK != status) {
        printf("Failed to register signal handler with %s", QCC_StatusText(status));
        return status;
    }

    /* Register Method handlers. */
    methodEntries[0].member = &my_ping_member;
    methodEntries[0].method_handler = ping;

    methodEntries[1].member = &my_delayed_ping_member;
    methodEntries[1].method_handler = delayed_ping;

    methodEntries[2].member = &my_time_ping_member;
    methodEntries[2].method_handler = time_ping;


    status = alljoyn_busobject_addmethodhandlers(g_testObj, methodEntries, sizeof(methodEntries) / sizeof(methodEntries[0]));
    if (ER_OK != status) {
        printf("Failed to register method handlers because of %s\n", QCC_StatusText(status));
        return status;
    }

    return status;
}
示例#2
0
int CDECL_CALL main(void)
{
    alljoyn_busattachment bus;
    alljoyn_observer obs;
    alljoyn_observerlistener listener;
    alljoyn_interfacedescription intf;
    alljoyn_interfacedescription_member member;
    QCC_BOOL done;
    const char* intfname = INTF_NAME;

    if (alljoyn_init() != ER_OK) {
        return 1;
    }
#ifdef ROUTER
    if (alljoyn_routerinit() != ER_OK) {
        alljoyn_shutdown();
        return 1;
    }
#endif

    bus = alljoyn_busattachment_create("door_consumer_c", QCC_TRUE);
    setup_busattachment(bus);

    obs = alljoyn_observer_create(bus, &intfname, 1);
    listener_ctx.observer = obs;
    listener_ctx.bus = bus;
    listener = alljoyn_observerlistener_create(&listener_cbs, &listener_ctx);
    alljoyn_observer_registerlistener(obs, listener, QCC_TRUE);

    intf = alljoyn_busattachment_getinterface(bus, INTF_NAME);
    alljoyn_interfacedescription_getmember(intf, "PersonPassedThrough", &member);
    alljoyn_busattachment_registersignalhandler(bus, person_passed_through, member, NULL);

    done = QCC_FALSE;
    while (!done) {
        char input[200];
        char* str;

        printf("> "); fflush(stdout);
        str = fgets(input, sizeof(input), stdin);
        if (str == NULL) {
            break;
        }
        str = strchr(input, '\n');
        if (str) {
            *str = '\0';
        }
        done = !parse(bus, obs, input);
    }

    // Cleanup
    alljoyn_observer_destroy(obs);
    alljoyn_observerlistener_destroy(listener);

    /* Deallocate bus */
    if (bus) {
        alljoyn_busattachment deleteMe = bus;
        bus = NULL;
        alljoyn_busattachment_destroy(deleteMe);
    }

#ifdef ROUTER
    alljoyn_routershutdown();
#endif
    alljoyn_shutdown();
    return 0;
}
示例#3
0
/* Signal handler. */
void AJ_CALL signal_handler(const alljoyn_interfacedescription_member* member, const char* srcPath, alljoyn_message msg)
{
    static uint32_t rxCounts = 0;
    QStatus status = ER_OK;

    /* Enable concurrent callbacks since some of the calls below could block */
    alljoyn_busattachment_enableconcurrentcallbacks(g_msgBus);

    if ((++rxCounts % g_reportInterval) == 0) {
        printf("RxSignal: %s - %u\n", srcPath, rxCounts);

        if (alljoyn_message_isencrypted(msg) == QCC_TRUE) {
            printf("Authenticated using %s\n", alljoyn_message_getauthmechanism(msg));
        }
    }

    if (g_echo_signal) {
        alljoyn_msgarg arg;
        uint8_t flags = 0;
        alljoyn_interfacedescription intf = NULL;
        alljoyn_interfacedescription_member my_signal_member;
        QCC_BOOL foundMember = QCC_FALSE;

        arg = alljoyn_msgarg_create_and_set("a{ys}", 0);

        if (g_compress) {
            flags |= ALLJOYN_MESSAGE_FLAG_COMPRESSED;
        }

        intf = alljoyn_busattachment_getinterface(g_msgBus, alljoyn_message_getinterface(msg));
        if (intf != NULL) {
            foundMember = alljoyn_interfacedescription_getmember(intf, "my_signal", &my_signal_member);
        }

        if (foundMember) {
            status = alljoyn_busobject_signal(g_testObj, alljoyn_message_getsender(msg), alljoyn_message_getsessionid(msg), my_signal_member, arg, 1, 0, 0, msg);
            if (status != ER_OK) {
                printf("Failed to send Signal because of %s. \n", QCC_StatusText(status));
            }
        } else {
            printf("Not able to send signal as could not find signal member. \n");
        }

        /* Destroy the msgarg */
        alljoyn_msgarg_destroy(arg);
    }

    /* ping back means make a method call when you receive a signal. */
    if (g_ping_back) {

        alljoyn_msgarg arg;
        alljoyn_interfacedescription intf = NULL;
        alljoyn_interfacedescription_member my_ping_member;
        QCC_BOOL foundMember = QCC_FALSE;

        arg = alljoyn_msgarg_create_and_set("s", "Ping back");

        intf = alljoyn_busattachment_getinterface(g_msgBus, alljoyn_message_getinterface(msg));
        if (intf != NULL) {
            foundMember = alljoyn_interfacedescription_getmember(intf, "my_ping", &my_ping_member);
            if (foundMember) {

                alljoyn_message reply = NULL;
                alljoyn_proxybusobject remoteObj = NULL;

                reply = alljoyn_message_create(g_msgBus);

                remoteObj = alljoyn_proxybusobject_create(g_msgBus, alljoyn_message_getsender(msg), OBJECT_PATH, alljoyn_message_getsessionid(msg));
                alljoyn_proxybusobject_addinterface(remoteObj, intf);
                /*
                 * Make a fire-and-forget method call. If the signal was encrypted encrypt the ping
                 */
                status = alljoyn_proxybusobject_methodcall(remoteObj, INTERFACE_NAME, "my_ping", arg, 1, reply, 5000, alljoyn_message_isencrypted(msg) ? ALLJOYN_MESSAGE_FLAG_ENCRYPTED : 0);
                if (status != ER_OK) {
                    printf("MethodCall on %s.%s failed due to %s. \n", INTERFACE_NAME, "my_ping", QCC_StatusText(status));
                }

                /* Destroy the message reply. */
                alljoyn_message_destroy(reply);
                /* Destroy the proxy bus object. */
                alljoyn_proxybusobject_destroy(remoteObj);
            }
        }
        /* Destroy the msgarg */
        alljoyn_msgarg_destroy(arg);
    }
}
/** Main entry point */
int CDECL_CALL main(void)
{
    QStatus status = ER_OK;
    char* connectArgs = "unix:abstract=alljoyn";
    alljoyn_interfacedescription testIntf = NULL;
    alljoyn_busobject_callbacks busObjCbs = {
        NULL,
        NULL,
        &busobject_object_registered,
        NULL
    };
    alljoyn_busobject testObj = NULL;
    alljoyn_interfacedescription exampleIntf;
    alljoyn_interfacedescription_member cat_member;
    QCC_BOOL foundMember = QCC_FALSE;
    alljoyn_busobject_methodentry methodEntries[] = {
        { &cat_member, cat_method },
    };
    alljoyn_sessionportlistener_callbacks spl_cbs = {
        accept_session_joiner,
        NULL
    };
    alljoyn_sessionopts opts = NULL;

    if (alljoyn_init() != ER_OK) {
        return 1;
    }
#ifdef ROUTER
    if (alljoyn_routerinit() != ER_OK) {
        alljoyn_shutdown();
        return 1;
    }
#endif

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

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

    /* Create message bus */
    g_msgBus = alljoyn_busattachment_create("myApp", QCC_TRUE);

    if (g_msgBus != NULL) {
        /* Add org.alljoyn.Bus.method_sample interface */
        status = alljoyn_busattachment_createinterface(g_msgBus, INTERFACE_NAME, &testIntf);
        if (status == ER_OK) {
            alljoyn_interfacedescription_addmember(testIntf, ALLJOYN_MESSAGE_METHOD_CALL, "cat", "ss",  "s", "inStr1,inStr2,outStr", 0);
            alljoyn_interfacedescription_activate(testIntf);
            printf("Interface Created.\n");
        } else {
            printf("Failed to create interface 'org.alljoyn.Bus.method_sample'\n");
        }

        /* Register a bus listener */
        if (ER_OK == status) {
            /* Create a bus listener */
            alljoyn_buslistener_callbacks callbacks = {
                NULL,
                NULL,
                NULL,
                NULL,
                &name_owner_changed,
                NULL,
                NULL,
                NULL
            };
            g_busListener = alljoyn_buslistener_create(&callbacks, NULL);
            alljoyn_busattachment_registerbuslistener(g_msgBus, g_busListener);
        }

        /* Set up bus object */
        testObj = alljoyn_busobject_create(OBJECT_PATH, QCC_FALSE, &busObjCbs, NULL);
        exampleIntf = alljoyn_busattachment_getinterface(g_msgBus, INTERFACE_NAME);
        assert(exampleIntf);
        alljoyn_busobject_addinterface(testObj, exampleIntf);

        foundMember = alljoyn_interfacedescription_getmember(exampleIntf, "cat", &cat_member);
        assert(foundMember == QCC_TRUE);
        if (!foundMember) {
            printf("Failed to get cat member of interface\n");
        }

        status = alljoyn_busobject_addmethodhandlers(testObj, methodEntries, sizeof(methodEntries) / sizeof(methodEntries[0]));
        if (ER_OK != status) {
            printf("Failed to register method handlers for BasicSampleObject\n");
        }

        /* Start the msg bus */
        status = alljoyn_busattachment_start(g_msgBus);
        if (ER_OK == status) {
            printf("alljoyn_busattachment started.\n");
            /* Register  local objects and connect to the daemon */
            status = alljoyn_busattachment_registerbusobject(g_msgBus, testObj);

            /* Create the client-side endpoint */
            if (ER_OK == status) {
                status = alljoyn_busattachment_connect(g_msgBus, connectArgs);
                if (ER_OK != status) {
                    printf("alljoyn_busattachment_connect(\"%s\") failed\n", (connectArgs) ? connectArgs : "NULL");
                } else {
                    printf("alljoyn_busattachment connected to \"%s\"\n", alljoyn_busattachment_getconnectspec(g_msgBus));
                }
            }
        } else {
            printf("alljoyn_busattachment_start failed\n");
        }

        /*
         * Advertise this service on the bus
         * There are three steps to advertising this service on the bus
         * 1) Request a well-known name that will be used by the client to discover
         *    this service
         * 2) Create a session
         * 3) Advertise the well-known name
         */
        /* Request name */
        if (ER_OK == status) {
            uint32_t flags = DBUS_NAME_FLAG_REPLACE_EXISTING | DBUS_NAME_FLAG_DO_NOT_QUEUE;
            QStatus status = alljoyn_busattachment_requestname(g_msgBus, OBJECT_NAME, flags);
            if (ER_OK != status) {
                printf("alljoyn_busattachment_requestname(%s) failed (status=%s)\n", OBJECT_NAME, QCC_StatusText(status));
            }
        }

        /* Create session port listener */
        s_sessionPortListener = alljoyn_sessionportlistener_create(&spl_cbs, NULL);

        /* Create session */
        opts = alljoyn_sessionopts_create(ALLJOYN_TRAFFIC_TYPE_MESSAGES, QCC_FALSE, ALLJOYN_PROXIMITY_ANY, ALLJOYN_TRANSPORT_ANY);
        if (ER_OK == status) {
            alljoyn_sessionport sp = SERVICE_PORT;
            status = alljoyn_busattachment_bindsessionport(g_msgBus, &sp, opts, s_sessionPortListener);
            if (ER_OK != status) {
                printf("alljoyn_busattachment_bindsessionport failed (%s)\n", QCC_StatusText(status));
            }
        }

        /* Advertise name */
        if (ER_OK == status) {
            status = alljoyn_busattachment_advertisename(g_msgBus, OBJECT_NAME, alljoyn_sessionopts_get_transports(opts));
            if (status != ER_OK) {
                printf("Failed to advertise name %s (%s)\n", OBJECT_NAME, QCC_StatusText(status));
            }
        }

        if (ER_OK == status) {
            while (g_interrupt == QCC_FALSE) {
    #ifdef _WIN32
                Sleep(100);
    #else
                usleep(100 * 1000);
    #endif
            }
        }
    }
    /* Deallocate sessionopts */
    if (opts) {
        alljoyn_sessionopts_destroy(opts);
    }
    /* Deallocate bus */
    if (g_msgBus) {
        alljoyn_busattachment deleteMe = g_msgBus;
        g_msgBus = NULL;
        alljoyn_busattachment_destroy(deleteMe);
    }

    /* Deallocate bus listener */
    if (g_busListener) {
        alljoyn_buslistener_destroy(g_busListener);
    }

    /* Deallocate session port listener */
    if (s_sessionPortListener) {
        alljoyn_sessionportlistener_destroy(s_sessionPortListener);
    }

    /* Deallocate the bus object */
    if (testObj) {
        alljoyn_busobject_destroy(testObj);
    }

#ifdef ROUTER
    alljoyn_routershutdown();
#endif
    alljoyn_shutdown();
    return (int) status;
}