// Make ping method call to service static public AJ_Status SendPing(UInt32 bus, UInt32 sessionId, AJ myAlljoyn) { AJ_Status status; AJ_Message msg = new AJ_Message(); status = myAlljoyn.MarshalMethodCall(bus, msg, PING_METHOD, FullServiceName, sessionId, AJ_FLAG_ENCRYPTED, METHOD_TIMEOUT); if (AJ_Status.AJ_OK == status) { status = myAlljoyn.MarshalArg(msg, "s", pingString); } else { } if (AJ_Status.AJ_OK == status) { status = myAlljoyn.DeliverMsg(msg); } else { } if (AJ_Status.AJ_OK != status) { } return(status); }
public AJ_Status AboutPropGetter(AJ_Message msg, string language) { AJ_Status status = AJ_Status.AJ_ERR_INVALID; int langIndex = 0; AJSVC_PropertyStoreCategoryFilter filter; filter.bit0About = false; filter.bit2Announce = false; if (msg.msgId == AJ.AJ_SIGNAL_ABOUT_ANNOUNCE) { filter.bit2Announce = true; langIndex = propertyStore.GetLanguageIndex(language); status = AJ_Status.AJ_OK; } else if (msg.msgId == ((AJ.AJ_METHOD_ABOUT_GET_ABOUT_DATA) | (uint)(AJ.AJ_REP_ID_FLAG << 24))) { filter.bit0About = true; langIndex = propertyStore.GetLanguageIndex(language); status = (langIndex == PropertyStore.ERROR_LANGUAGE_INDEX) ? AJ_Status.AJ_ERR_UNKNOWN : AJ_Status.AJ_OK; } if (status == AJ_Status.AJ_OK) { status = PropertyStore_ReadAll(msg, filter, langIndex); } return(status); }
// Handle ping message from client static AJ_Status AppHandlePing(AJ_Message msg, AJ myAlljoyn) { AJ_Status status; AJ_Message reply = new AJ_Message(); UInt32 argPtr = myAlljoyn.GetArgPtr(3); status = myAlljoyn.UnmarshalArg(msg, argPtr); if (AJ_Status.AJ_OK == status) { string msgArg = myAlljoyn.GetArgString(3); Debug.Print("Received ping request " + msgArg); status = myAlljoyn.MarshalReplyMsg(msg, reply); if (AJ_Status.AJ_OK == status) { // Just return the arg we received status = myAlljoyn.MarshalArg(reply, "s", msgArg); if (AJ_Status.AJ_OK == status) { status = myAlljoyn.DeliverMsg(reply); } } } return(status); }
// SetPropertyHandler: // // Callback function used to process set property requests from clients // public static AJ_Status SetPropertyHandler(AJ_Message reply, AJ_Message msg, uint propId, AJ aj) { if (propId == DARKNESS_PROPERTY_ID) { aj.UnmarshalArgs(msg, "u", ref Darkness); } return(AJ_Status.AJ_OK); }
AJ_Status AJSVC_MarshalAppIdAsVariant(AJ_Message msg, string appId) { AJ_Status status = AJ_Status.AJ_OK; byte [] b = new byte[appId.Length / 2]; for (int i = 0; i < appId.Length / 2; i++) { b[i] = (byte)((A2H(appId[i << 1]) << 4) + (A2H(appId[(i << 1) + 1]))); } status = AjInst.MarshalArgs(msg, "v", AJ.APP_ID_SIGNATURE, b); return(status); }
static AJ_Status AppHandleCat(AJ_Message msg, AJ myAlljoyn) { AJ_Message reply = new AJ_Message(); string s1 = myAlljoyn.UnmarshalArgs(msg, "s"); string s2 = myAlljoyn.UnmarshalArgs(msg, "s"); myAlljoyn.MarshalReplyMsg(msg, reply); // We have the arguments. Now do the concatenation. string buffer = s1 + s2; myAlljoyn.MarshalArg(reply, "s", buffer); return(myAlljoyn.DeliverMsg(reply)); }
public static AJ_Status SetPropertyHandler(AJ_Message reply, AJ_Message msg, uint propId, AJ aj) { if (propId == PROPERTY1_ID) { Property1 = aj.UnmarshalArgs(msg, "s"); } else if (propId == PROPERTY2_ID) { Property2 = aj.UnmarshalArgs(msg, "s"); } else if (propId == PROPERTY3_ID) { Property3 = aj.UnmarshalArgs(msg, "s"); } return(AJ_Status.AJ_OK); }
// Callback to retrieve WeatherStation properties public static AJ_Status GetPropertyHandler(AJ_Message reply, AJ_Message msg, uint propId, AJ aj) { if (propId == PROPERTY1_ID) { aj.MarshalArg(reply, "s", Property1); } else if (propId == PROPERTY2_ID) { aj.MarshalArg(reply, "s", Property2); } else if (propId == PROPERTY3_ID) { aj.MarshalArg(reply, "s", Property3); } return(AJ_Status.AJ_OK); }
static public void MakeMethodCall(UInt32 bus, UInt32 sessionId, AJ myAlljoyn) { AJ_Status status = AJ_Status.AJ_OK; AJ_Message msg = new AJ_Message(); status = myAlljoyn.MarshalMethodCall(bus, msg, BASIC_CLIENT_CAT, fullServiceName, sessionId, 0, METHOD_TIMEOUT); if (status == AJ_Status.AJ_OK) { status = myAlljoyn.MarshalArgs(msg, "ss", "Hello ", "World!"); } if (status == AJ_Status.AJ_OK) { status = myAlljoyn.DeliverMsg(msg); } }
AJ_Status PropertyStore_ReadAll(AJ_Message msg, AJSVC_PropertyStoreCategoryFilter filter, int langIndex) { AJ_Status status = AJ_Status.AJ_OK; // FIXME: In order to marshal containers, // a native AJ_Arg struct must hang around // in memory because several fields in teh // AJ_Message struct point to the address. // The GetArgPtr(N) function gets the address // of an AJ_Arg struct defined in static // memory. UInt32 array = AjInst.GetArgPtr(0); UInt32 array2 = AjInst.GetArgPtr(1); UInt32 dict = AjInst.GetArgPtr(2); string value = String.Empty; byte index; string ajVersion = String.Empty; byte fieldIndex; status = AjInst.MarshalContainer(msg, array, (byte)AJ_Args.AJ_ARG_ARRAY); if (status != AJ_Status.AJ_OK) { return(status); } for (fieldIndex = 0; fieldIndex < (byte)PropertyStoreFieldIndices.AJSVC_PROPERTY_STORE_NUMBER_OF_KEYS; fieldIndex++) { #if CONFIG_SERVICE if (propertyStore.Properties[fieldIndex].mode7Public && (filter.bit0About || (filter.bit1Config && propertyStoreProperties[fieldIndex].mode0Write) || (filter.bit2Announce && propertyStore.Properties[fieldIndex].mode1Announce))) { #else if ((propertyStore.Properties[fieldIndex].mode7Public == 1) && (filter.bit0About || (filter.bit2Announce && (propertyStore.Properties[fieldIndex].mode1Announce == 1)))) { #endif value = propertyStore.GetValueForLang(fieldIndex, langIndex); if (value == null && fieldIndex >= (byte)PropertyStoreFieldIndices.AJSVC_PROPERTY_STORE_NUMBER_OF_MANDATORY_KEYS) // Non existing values are skipped! { } else { if (fieldIndex == (byte)PropertyStoreFieldIndices.AJSVC_PROPERTY_STORE_APP_ID) { if (value == String.Empty) { return(AJ_Status.AJ_ERR_NULL); } status = AjInst.MarshalContainer(msg, dict, (byte)AJ_Args.AJ_ARG_DICT_ENTRY); if (status != AJ_Status.AJ_OK) { return(status); } status = AjInst.MarshalArg(msg, "s", propertyStore.Properties[fieldIndex].keyName); if (status != AJ_Status.AJ_OK) { return(status); } status = AJSVC_MarshalAppIdAsVariant(msg, value); if (status != AJ_Status.AJ_OK) { return(status); } status = AjInst.MarshalCloseContainer(msg, dict); if (status != AJ_Status.AJ_OK) { return(status); } #if CONFIG_SERVICE } else if (fieldIndex == PropertyStoreFieldIndices.AJSVC_PROPERTY_STORE_MAX_LENGTH) { status = AjInst.AJ_MarshalArgs(msg, "{sv}", propertyStore.Properties[fieldIndex].keyName, "q", DEVICE_NAME_VALUE_LENGTH); if (status != AJ_Status.AJ_OK) { return(status); } #endif } else if (fieldIndex == (byte)PropertyStoreFieldIndices.AJSVC_PROPERTY_STORE_AJ_SOFTWARE_VERSION) { ajVersion = AjInst.GetVersion(); if (ajVersion == String.Empty) { return(AJ_Status.AJ_ERR_NULL); } status = AjInst.MarshalArgs(msg, "{sv}", propertyStore.Properties[fieldIndex].keyName, "s", ajVersion); if (status != AJ_Status.AJ_OK) { return(status); } } else { if (value == String.Empty) { return(AJ_Status.AJ_ERR_NULL); } status = AjInst.MarshalArgs(msg, "{sv}", propertyStore.Properties[fieldIndex].keyName, "s", value); if (status != AJ_Status.AJ_OK) { return(status); } } } } } if (filter.bit0About == true) { // Add supported languages status = AjInst.MarshalContainer(msg, dict, (byte)AJ_Args.AJ_ARG_DICT_ENTRY); if (status != AJ_Status.AJ_OK) { return(status); } status = AjInst.MarshalArg(msg, "s", propertyStore.DefaultLanguagesKeyName); if (status != AJ_Status.AJ_OK) { return(status); } status = AjInst.MarshalVariant(msg, "as"); if (status != AJ_Status.AJ_OK) { return(status); } status = AjInst.MarshalContainer(msg, array2, (byte)AJ_Args.AJ_ARG_ARRAY); if (status != AJ_Status.AJ_OK) { return(status); } for (index = 0; index < propertyStore.NumberOfLanguages; index++) { status = AjInst.MarshalArg(msg, "s", propertyStore.SupportedLanguages[index]); if (status != AJ_Status.AJ_OK) { return(status); } } status = AjInst.MarshalCloseContainer(msg, array2); if (status != AJ_Status.AJ_OK) { return(status); } status = AjInst.MarshalCloseContainer(msg, dict); if (status != AJ_Status.AJ_OK) { return(status); } } status = AjInst.MarshalCloseContainer(msg, array); if (status != AJ_Status.AJ_OK) { return(status); } return(status); } }
public static void Main( ) { #region Initialize ThinClient Service string[] weatherInterface = { "com.microsoft.NetMicroFrameWork.WeatherStation", "!WeatherEvent Arg1>s", "@String1=s", "@String2=s", "@String3=s", " " }; UInt32 AJ_METHOD_ACCEPT_SESSION = AJ.BusMessageId(2, 0, 0); UInt32 AJ_SIGNAL_SESSION_JOINED = AJ.BusMessageId(2, 0, 1); UInt32 WEATHER_EVENT_SIGNAL = AJ.AppMessageId(0, 1, 0); const byte AJ_FLAG_GLOBAL_BROADCAST = 0x20; bool connected = false; const ushort port = 0; UInt32 bus = 0; var msg = new AJ_Message( ); // Initialize Alljoyn object var myAlljoyn = new AJ( ); var ajServices = new AJ_Services(myAlljoyn); // Create and set the property store var ps = new PropertyStore( ); ps.SetDefaultDeviceNames(defaultDeviceNames); ps.SetSupportedLanguages(supportedLanguages); ps.SetDefaultLanguage(defaultLanguage); ps.SetDefaultSupportUrls(defaultSupportUrls); ps.SetDefaultDeviceNames(defaultDeviceNames); ps.SetDefaultDescriptions(defaultDescriptions); ps.SetDefaultManufacturers(defaultManufacturers); ps.SetDefaultModelNumber(defaultModelNumber); ps.SetDefaultSoftwareVersion(defaultSoftwareVersion); ps.InitMandatoryFields( ); ajServices.SetPropertyStore(ps); // Set About attributes myAlljoyn.SetAboutIconContent(aboutIconContent); myAlljoyn.SetAboutIconURL(aboutIconUrl); myAlljoyn.CreateBus(ref bus); myAlljoyn.Initialize( ); myAlljoyn.RegisterObjectsInterface(new AJ_Object( ) { path = ServicePath, interfaces = weatherInterface }, true, true); #endregion ajServices.Initialize_NotificationService("DEFAULT"); // Attach to Netowrk Link state change notifications var netmon = new NetworkStateMonitor( ); // Start the message processing loop while (true) { netmon.WaitForIpAddress( ); AJ_Status status; if (!connected) { status = myAlljoyn.StartService(bus, DaemonServiceName, CONNECT_TIMEOUT, AJ.AJ_FALSE, ServicePort, ServiceName, AJ.AJ_NAME_REQ_DO_NOT_QUEUE); if (status != AJ_Status.AJ_OK) { continue; } Debug.Print("StartService returned AJ_OK; running \n"); connected = true; // once connected to the Alljoyn router do the "About" annoucement if (status == AJ_Status.AJ_OK) { myAlljoyn.doAnnounce = true; myAlljoyn.AboutAnnounce(bus, ServicePort); } } // wait for a message from the router status = myAlljoyn.UnmarshalMsg(bus, msg, UNMARSHAL_TIMEOUT); if (status == AJ_Status.AJ_ERR_TIMEOUT) { Debug.Print("do work\n"); continue; } // if the message is ok, check for message types this app wants to handle if (status == AJ_Status.AJ_OK) { string str = "Received message + msgId=" + msg.msgId.ToString("X") + "\n"; Debug.Print(str); // Session connection from a client? if (msg.msgId == AJ_METHOD_ACCEPT_SESSION) { string joiner = ""; UInt32 sessionId = 0; myAlljoyn.UnmarshalArgs(msg, "qus", port, sessionId, joiner); status = myAlljoyn.BusReplyAcceptSession(msg, 1); if (status == AJ_Status.AJ_OK) { Debug.Print("Accepted session session_id \n"); } else { Debug.Print("AJ_BusReplyAcceptSession: error \n"); } } // Session connected else if (msg.msgId == AJ_SIGNAL_SESSION_JOINED) { // do nothing... } // Request to read an interface property else if (msg.msgId == GET_PROPERTY) { myAlljoyn.BusGetProp(msg, GetPropertyHandler); } // Set a property on the advertised interface else if (msg.msgId == SET_PROPERTY) { myAlljoyn.BusSetProp(msg, SetPropertyHandler); // This is how to send a signal. You can put this code in // the handler for a weather event, etc. uint sn = 4; ajServices.SendNotification(bus, "Sample notify", 2, 500, ref sn); var sig = new AJ_Message(); AJ_Status s = myAlljoyn.MarshalSignal(bus, sig, WEATHER_EVENT_SIGNAL, 0, 0, AJ_FLAG_GLOBAL_BROADCAST, 0); s = myAlljoyn.MarshalArg(sig, "s", "22222 This is a bogus weather event"); myAlljoyn.DeliverMsg(sig); } else // default handling (pass it on to the bus) { myAlljoyn.BusHandleBusMessage(msg, bus, ServicePort); } } myAlljoyn.CloseMsg(msg); if (status == AJ_Status.AJ_ERR_READ) { Debug.Print("AllJoyn disconnect\n"); myAlljoyn.Disconnect(bus); connected = false; // sleep a little while before trying to connect again myAlljoyn.Sleep(10 * 1000); } } }
public static void Main() { #region Initialize AlljoynSecurityService // Define device interface. Interfaces beginning with // dollar sign ('$') are deemed secure string[] deviceInterface = { "$org.alljoyn.bus.samples.secure.SecureInterface", "?Ping inStr<s outStr>s", " " }; UInt32 AJ_METHOD_ACCEPT_SESSION = AJ.BusMessageId(2, 0, 0); UInt32 AJ_SIGNAL_SESSION_JOINED = AJ.BusMessageId(2, 0, 1); UInt32 BASIC_SERVICE_PING = AJ.AppMessageId(0, 0, 0); bool connected = false; const ushort port = 0; UInt32 bus = 0; var msg = new AJ_Message(); var myAlljoyn = new AJ(); // Initialize Alljoyn object myAlljoyn.CreateBus(ref bus); myAlljoyn.Initialize(); myAlljoyn.RegisterObjectsInterface(new AJ_Object() { path = ServicePath, interfaces = deviceInterface }, false, true); #endregion // Attach to Netowrk Link state change notifications var netmon = new NetworkStateMonitor(); // Start the message processing loop while (true) { netmon.WaitForIpAddress(); AJ_Status status; if (!connected) { status = myAlljoyn.StartService(bus, DaemonServiceName, CONNECT_TIMEOUT, AJ.AJ_FALSE, ServicePort, ServiceName, AJ.AJ_NAME_REQ_DO_NOT_QUEUE); if (status != AJ_Status.AJ_OK) { Debug.Print(status.ToString()); continue; } Debug.Print("StartService returned AJ_OK; running \n"); connected = true; // Provide credentials for all security // suites by setting write-only properties myAlljoyn.KeyExpiration = KeyExpiration; myAlljoyn.PskHint = PskHint; myAlljoyn.PskString = PskChar; myAlljoyn.PemPriv = PemPrv; myAlljoyn.PemX509 = PemX509; // Enable security suites myAlljoyn.EnableSecurity(bus, SecuritySuites); // once connected to the Alljoyn router do the "About" annoucement if (status == AJ_Status.AJ_OK) { myAlljoyn.doAnnounce = true; myAlljoyn.AboutAnnounce(bus, ServicePort); } } // wait for a message from the router status = myAlljoyn.UnmarshalMsg(bus, msg, UNMARSHAL_TIMEOUT); if (status == AJ_Status.AJ_ERR_TIMEOUT) { Debug.Print("do work\n"); continue; } // if the message is ok, check for message types this app wants to handle if (status == AJ_Status.AJ_OK) { string str = "Received message + msgId=" + msg.msgId.ToString("X") + "\n"; Debug.Print(str); // Session connection from a client? if (msg.msgId == AJ_METHOD_ACCEPT_SESSION) { string joiner = ""; UInt32 sessionId = 0; myAlljoyn.UnmarshalArgs(msg, "qus", port, sessionId, joiner); status = myAlljoyn.BusReplyAcceptSession(msg, 1); if (status == AJ_Status.AJ_OK) { Debug.Print("Accepted session session_id \n"); } else { Debug.Print("AJ_BusReplyAcceptSession: error \n"); } } // Session connected else if (msg.msgId == BASIC_SERVICE_PING) { status = AppHandlePing(msg, myAlljoyn); } else if (msg.msgId == AJ_SIGNAL_SESSION_JOINED) { // do nothing... } // Request to read an interface property else // default handling (pass it on to the bus) { myAlljoyn.BusHandleBusMessage(msg, bus, ServicePort); } } myAlljoyn.CloseMsg(msg); if (status == AJ_Status.AJ_ERR_READ) { Debug.Print("AllJoyn disconnect\n"); myAlljoyn.Disconnect(bus); connected = false; // sleep a little while before trying to connect again myAlljoyn.Sleep(10 * 1000); } } }
// program entry point public static void Main() { // Initialize Alljoyn class instance AJ myAlljoyn = new AJ(); var ajServices = new AJ_Services(myAlljoyn); // some state variables bool connected = false; AJ_Status status = AJ_Status.AJ_OK; // constants used in the message processing loop UInt32 CONNECT_TIMEOUT = (1000 * 1000); UInt32 UNMARSHAL_TIMEOUT = 5000; UInt32 AJ_METHOD_ACCEPT_SESSION = AJ.BusMessageId(2, 0, 0); UInt32 AJ_SIGNAL_SESSION_JOINED = AJ.BusMessageId(2, 0, 1); UInt32 START_TOASTING = AJ.AppMessageId(0, 1, 0); UInt32 STOP_TOASTING = AJ.AppMessageId(0, 1, 1); UInt32 TOAST_DONE_SIGNAL = AJ.AppMessageId(0, 1, 2); UInt32 DARKNESS = AJ.AppPropertyId(0, 1, 3); byte AJ_FLAG_GLOBAL_BROADCAST = 0x20; UInt16 port = 0; UInt32 bus = 0; AJ_Message msg = new AJ_Message(); // Define the application that the outside world // will see string ServiceName = "com.microsoft.sample.toaster"; string ServicePath = "/toaster"; UInt16 ServicePort = 42; string DaemonServiceName = "org.alljoyn.BusNode.Led"; // Define the Alljoyn interface that will be // published to the outside world string[] testInterface = { "com.microsoft.sample.toaster", "?startToasting", "?stopToasting", "!toastDone status>i", "@Darkness=u", " " }; // Create and set the property store PropertyStore ps = new PropertyStore(); ps.SetDefaultDeviceNames(defaultDeviceNames); ps.SetSupportedLanguages(supportedLanguages); ps.SetDefaultLanguage(defaultLanguage); ps.SetDefaultSupportUrls(defaultSupportUrls); ps.SetDefaultDeviceNames(defaultDeviceNames); ps.SetDefaultDescriptions(defaultDescriptions); ps.SetDefaultManufacturers(defaultManufacturers); ps.SetDefaultModelNumber(defaultModelNumber); ps.SetDefaultSoftwareVersion(defaultSoftwareVersion); ps.InitMandatoryFields(); ajServices.SetPropertyStore(ps); // Set About attributes myAlljoyn.SetAboutIconContent(aboutIconContent); myAlljoyn.SetAboutIconURL(aboutIconUrl); // Create the Alljoyn bus instance myAlljoyn.CreateBus(ref bus); // Initialize Alljoyn instance myAlljoyn.Initialize(); // Register alljoyn interface. We set the 2nd parameter to true to indicate that // properties are going to be used myAlljoyn.RegisterObjectsInterface(new AJ_Object() { path = ServicePath, interfaces = testInterface }, true, true); // Start the message processing loop while (true) { if (!connected) { IntPtr arg = new IntPtr(); status = myAlljoyn.StartService(bus, DaemonServiceName, CONNECT_TIMEOUT, AJ.AJ_FALSE, ServicePort, ServiceName, AJ.AJ_NAME_REQ_DO_NOT_QUEUE); if (status != AJ_Status.AJ_OK) { goto exit; } myAlljoyn.AlwaysPrintf(("StartService returned AJ_OK; running \n")); connected = true; if (status == AJ_Status.AJ_OK) { myAlljoyn.doAnnounce = true; myAlljoyn.AboutAnnounce(bus, ServicePort); } } status = myAlljoyn.UnmarshalMsg(bus, msg, UNMARSHAL_TIMEOUT); if (status != AJ_Status.AJ_OK) { if (status == AJ_Status.AJ_ERR_TIMEOUT) { myAlljoyn.AlwaysPrintf(("do work\n")); goto exit; } } if (status == AJ_Status.AJ_OK) { string str = "Received message + msgId=" + msg.msgId.ToString("X") + "\n"; myAlljoyn.AlwaysPrintf((str)); if (msg.msgId == AJ_METHOD_ACCEPT_SESSION) { string joiner = ""; UInt32 sessionId = 0; myAlljoyn.UnmarshalArgs(msg, "qus", port, sessionId, joiner); status = myAlljoyn.BusReplyAcceptSession(msg, 1); if (status == AJ_Status.AJ_OK) { myAlljoyn.AlwaysPrintf(("Accepted session session_id \n")); } else { myAlljoyn.AlwaysPrintf(("AJ_BusReplyAcceptSession: error \n")); } } else if (msg.msgId == AJ_SIGNAL_SESSION_JOINED) { // do nothing... } else if (msg.msgId == START_TOASTING) { AJ_Message reply = new AJ_Message(); status = myAlljoyn.MarshalReplyMsg(msg, reply); if (status == AJ_Status.AJ_OK) { status = myAlljoyn.DeliverMsg(reply); } } else if (msg.msgId == STOP_TOASTING) { AJ_Status s = 0; AJ_Message reply = new AJ_Message(); s = myAlljoyn.MarshalReplyMsg(msg, reply); if (s == AJ_Status.AJ_OK) { s = myAlljoyn.DeliverMsg(reply); } // send a signal here AJ_Message sig = new AJ_Message(); s = myAlljoyn.MarshalSignal(bus, sig, TOAST_DONE_SIGNAL, 0, 0, AJ_FLAG_GLOBAL_BROADCAST, 0); s = myAlljoyn.MarshalArg(sig, "i", (uint)s); myAlljoyn.DeliverMsg(sig); } else if (msg.msgId == GET_DARKNESS) { myAlljoyn.BusGetProp(msg, GetPropertyHandler); } else if (msg.msgId == SET_DARKNESS) { myAlljoyn.BusSetProp(msg, SetPropertyHandler); } else { myAlljoyn.BusHandleBusMessage(msg, bus, ServicePort); } } myAlljoyn.CloseMsg(msg); if (status == AJ_Status.AJ_ERR_READ) { myAlljoyn.AlwaysPrintf(("AllJoyn disconnect\n")); myAlljoyn.Disconnect(bus); connected = false; // sleep a little while before trying to connect myAlljoyn.Sleep(10 * 1000); } exit: myAlljoyn.AlwaysPrintf((" Exit \n")); } }
public static void Main() { AJ_Status status = AJ_Status.AJ_OK; UInt32 bus = 0; bool connected = false; bool done = false; UInt32 sessionId = 0; AJ myAlljoyn = new AJ(); myAlljoyn.CreateBus(ref bus); myAlljoyn.Initialize(); myAlljoyn.RegisterObjectsInterface(new AJ_Object( ) { path = ServicePath, interfaces = SampleInterface }, false, false); // Attach to Netowrk Link state change notifications var netmon = new NetworkStateMonitor(); int retry = 0; while (!done) { netmon.WaitForIpAddress(); AJ_Message msg = new AJ_Message(); if (!connected) { do { status = myAlljoyn.ClientConnectBus(bus, null, CONNECT_TIMEOUT); } while (status != AJ_Status.AJ_OK); do { status = myAlljoyn.ClientFindService(bus, ServiceName, null, CONNECT_TIMEOUT); } while (status != AJ_Status.AJ_OK); retry = 0; do { status = myAlljoyn.ClientConnectService(bus, CONNECT_TIMEOUT, ServiceName, ServicePort, ref sessionId, null, ref fullServiceName); if (retry++ > 10) { break; } } while (status != AJ_Status.AJ_OK); if (status == AJ_Status.AJ_OK) { connected = true; MakeMethodCall(bus, sessionId, myAlljoyn); } else { Debug.Print("Timed out"); myAlljoyn.Disconnect(bus); continue; } } status = myAlljoyn.UnmarshalMsg(bus, msg, UNMARSHAL_TIMEOUT); if (AJ_Status.AJ_ERR_TIMEOUT == status) { continue; } if (AJ_Status.AJ_OK == status) { if (msg.msgId == AJ_REPLY_ID(BASIC_CLIENT_CAT)) { UInt32 argPtr = myAlljoyn.GetArgPtr(4); status = myAlljoyn.UnmarshalArg(msg, argPtr); string value = myAlljoyn.GetArgString(4); if (AJ_Status.AJ_OK == status) { Debug.Print(ServiceName + "cat returned " + value); done = true; } else { MakeMethodCall(bus, sessionId, myAlljoyn); } } else if (msg.msgId == AJ_SIGNAL_SESSION_LOST_WITH_REASON) { { UInt32 id = 0, reason = 0; myAlljoyn.UnmarshalArgs(msg, "uu", ref id, ref reason); } status = AJ_Status.AJ_ERR_SESSION_LOST; } else { /* Pass to the built-in handlers. */ status = myAlljoyn.BusHandleBusMessage(msg, bus, ServicePort); } } /* Messages MUST be discarded to free resources. */ myAlljoyn.CloseMsg(msg); if (status == AJ_Status.AJ_ERR_SESSION_LOST) { myAlljoyn.Disconnect(bus); //exit(0); } } }
public static void Main() { // Define device interface. Interfaces beginning with // dollar sign ('$') are deemed secure string[] deviceInterface = { "$org.alljoyn.bus.samples.secure.SecureInterface", "?Ping inStr<s outStr>s", " " }; AJ_Status status = AJ_Status.AJ_OK; AJ_Status authStatus = AJ_Status.AJ_ERR_NULL; UInt32 bus = 0; bool connected = false; bool done = false; UInt32 sessionId = 0; AJ myAlljoyn = new AJ(); // Initialize Alljoyn object myAlljoyn.CreateBus(ref bus); myAlljoyn.Initialize(); myAlljoyn.RegisterObjectsInterface(new AJ_Object() { path = ServicePath, interfaces = deviceInterface }, false, false); // Attach to Netowrk Link state change notifications var netmon = new NetworkStateMonitor(); while (!done) { netmon.WaitForIpAddress(); AJ_Message msg = new AJ_Message(); if (!connected) { // the original AJ_StartClientByName is splitted into 3 functions instead of one, as it will block the other CLR functions // and have a more efficient connection, especially when the service it is looking for is not existed yet. Instead of // keep connect and disconnect to router, send the findserive, we can just wait at the 3rd stage,ClientConnectservice. // // 1st stage - ClientconnectBus , found router and attached bus to it, wait until router found or timeout is set. // 2nd stage - ClientFindService, Adverstise the service it is looking for // 3rd stage - ClientConnectService, Connect to the services it is looking for, it will wait until the service pop up or timeout is set, do { status = myAlljoyn.ClientConnectBus(bus, null, CONNECT_TIMEOUT); }while (status != AJ_Status.AJ_OK); do { status = myAlljoyn.ClientFindService(bus, ServiceName, null, CONNECT_TIMEOUT); }while (status != AJ_Status.AJ_OK); int retry = 0; do { status = myAlljoyn.ClientConnectService(bus, CONNECT_TIMEOUT, ServiceName, ServicePort, ref sessionId, null, ref FullServiceName); // user can break from this call, instead of having a while loop retry++; if (retry > 10) { break; } } while (status != AJ_Status.AJ_OK); // if break from here, need to call myAlljoyn.Disconnect(bus); if (status != AJ_Status.AJ_OK) { myAlljoyn.Disconnect(bus); } if (status == AJ_Status.AJ_OK) { connected = true; authStatus = AJ_Status.AJ_ERR_NULL; // Provide credentials for all security // suites by setting write-only properties. // Only the security suites specified // in SecuritySuites will be used myAlljoyn.KeyExpiration = KeyExpiration; myAlljoyn.PskHint = PskHint; myAlljoyn.PskString = PskChar; myAlljoyn.PemPriv = PemPrv; myAlljoyn.PemX509 = PemX509; // Enable security suites myAlljoyn.EnableSecurity(bus, SecuritySuites); // Clear any stored credentials myAlljoyn.ClearCredentials(); // Begin authentication process with the service. This is // an asynchronous process so we will need to poll the // authStatus value during the main message processing loop status = myAlljoyn.AuthenticatePeer(bus, FullServiceName); if (status != AJ_Status.AJ_OK) { Debug.Print("AJ_BusAuthenticatePeer returned " + status.ToString()); break; } } else { continue; } } // Poll for authStatus to determine when // authentication handshaking is complete. authStatus = myAlljoyn.GetAuthStatus(); if (authStatus != AJ_Status.AJ_ERR_NULL) { if (authStatus != AJ_Status.AJ_OK) { myAlljoyn.Disconnect(bus); break; } // We set the authStatus to NULL when the // handshaking is finished to indicate that // we should proceed with normal alljoyn // message processing myAlljoyn.SetAuthStatus(AJ_Status.AJ_ERR_NULL); // Make ping method call to service status = SendPing(bus, sessionId, myAlljoyn); if (status != AJ_Status.AJ_OK) { Debug.Print("SendPing returned " + status.ToString()); continue; } } // process messages from the router status = myAlljoyn.UnmarshalMsg(bus, msg, UNMARSHAL_TIMEOUT); if (AJ_Status.AJ_ERR_TIMEOUT == status) { continue; } if (AJ_Status.AJ_OK == status) { // Handle Ping response if (msg.msgId == AJ_REPLY_ID(PING_METHOD)) { UInt32 argPtr = myAlljoyn.GetArgPtr(4); if (AJ_Status.AJ_OK == myAlljoyn.UnmarshalArg(msg, argPtr)) { string value = myAlljoyn.GetArgString(4); if (value == pingString) { Debug.Print("Ping was successful"); } else { Debug.Print("Ping returned different string"); } } else { } done = true; } else if (msg.msgId == AJ_SIGNAL_SESSION_LOST_WITH_REASON) { // Just eating the reason for this demo, in production // reason should be inspected and acted upon { UInt32 id = 0, reason = 0; myAlljoyn.UnmarshalArgs(msg, "uu", ref id, ref reason); } status = AJ_Status.AJ_ERR_SESSION_LOST; } else { // Pass to the built-in handlers status = myAlljoyn.BusHandleBusMessage(msg, bus, ServicePort); } } // Clean up myAlljoyn.CloseMsg(msg); if (status == AJ_Status.AJ_ERR_SESSION_LOST) { myAlljoyn.Disconnect(bus); } } }
// program entry point public static void Main() { // Initialize Alljoyn class instance AJ myAlljoyn = new AJ(); // some state variables bool connected = false; AJ_Status status = AJ_Status.AJ_OK; // constants used in the message processing loop UInt32 CONNECT_TIMEOUT = (1000 * 1000); UInt32 UNMARSHAL_TIMEOUT = 5000; UInt32 AJ_METHOD_ACCEPT_SESSION = AJ.BusMessageId(2, 0, 0); UInt32 AJ_SIGNAL_SESSION_JOINED = AJ.BusMessageId(2, 0, 1); byte AJ_FLAG_GLOBAL_BROADCAST = 0x20; UInt16 port = 0; UInt32 bus = 0; AJ_Message msg = new AJ_Message(); // Define the Alljoyn interface that will be // published to the outside world string[] testInterface = { "org.alljoyn.Bus.sample", // The first entry is the interface name. "?Dummy foo<i", // This is just a dummy entry at index 0 for illustration purposes. "?cat inStr1<s inStr2<s outStr>s", // Method at index 1. " " }; // Create the Alljoyn bus instance myAlljoyn.CreateBus(ref bus); // Initialize Alljoyn instance myAlljoyn.Initialize(); // Register alljoyn interface. We set the 2nd parameter to false to indicate that // properties will not be used myAlljoyn.RegisterObjectsInterface(new AJ_Object() { path = ServicePath, interfaces = testInterface }, false, true); // Start the message processing loop while (true) { if (!connected) { IntPtr arg = new IntPtr(); status = myAlljoyn.StartService(bus, null, CONNECT_TIMEOUT, AJ.AJ_FALSE, ServicePort, ServiceName, AJ.AJ_NAME_REQ_DO_NOT_QUEUE); if (status != AJ_Status.AJ_OK) { goto exit; } myAlljoyn.AlwaysPrintf(("StartService returned AJ_OK; running \n")); connected = true; if (status == AJ_Status.AJ_OK) { myAlljoyn.doAnnounce = true; myAlljoyn.AboutAnnounce(bus, ServicePort); } } status = myAlljoyn.UnmarshalMsg(bus, msg, UNMARSHAL_TIMEOUT); if (status != AJ_Status.AJ_OK) { if (status == AJ_Status.AJ_ERR_TIMEOUT) { myAlljoyn.AlwaysPrintf(("do work\n")); goto exit; } } if (status == AJ_Status.AJ_OK) { string str = "Received message + msgId=" + msg.msgId.ToString("X") + "\n"; myAlljoyn.AlwaysPrintf((str)); if (msg.msgId == AJ_METHOD_ACCEPT_SESSION) { string joiner = ""; UInt32 sessionId = 0; myAlljoyn.UnmarshalArgs(msg, "qus", port, sessionId, joiner); status = myAlljoyn.BusReplyAcceptSession(msg, 1); if (status == AJ_Status.AJ_OK) { myAlljoyn.AlwaysPrintf(("Accepted session session_id \n")); } else { myAlljoyn.AlwaysPrintf(("AJ_BusReplyAcceptSession: error \n")); } } else if (msg.msgId == AJ_SIGNAL_SESSION_JOINED) { // do nothing... } else if (msg.msgId == BASIC_SERVICE_CAT) { status = AppHandleCat(msg, myAlljoyn); } else { myAlljoyn.BusHandleBusMessage(msg, bus, ServicePort); } } myAlljoyn.CloseMsg(msg); if (status == AJ_Status.AJ_ERR_READ) { myAlljoyn.AlwaysPrintf(("AllJoyn disconnect\n")); myAlljoyn.Disconnect(bus); connected = false; // sleep a little while before trying to connect myAlljoyn.Sleep(10 * 1000); } exit: myAlljoyn.AlwaysPrintf((" Exit \n")); } }