1

signal from AJTC to AJSC

asked 2014-05-06 20:54:44 -0700

zhengkun gravatar image

updated 2014-05-07 14:04:26 -0700

georgen gravatar image

Hi, I have an Arduino board as a thin client and a PC as a standard service . They have join the same session, and the method call between them is successful. But when I send signal from Arduino to PC, there is nothing happen. Here is my code about Arduino:

#include <stdint.h>
#include <stddef.h>
#include <aj_debug.h>
#include <alljoyn.h>
#include <Wire.h>
#include "measure_due.h"


/**
  * Statics.
*/
static AJ_BusAttachment bus;

static const char DeviceName[] = "Alljoyn Thin Client";
static const char GatewayServiceName[] = "org.alljoyn.Bus.centralcontrol";
static const char DaemonBusName[] = "org.alljoyn.BusNode.measure";
static const uint16_t ServicePort = 24;
uint32_t sessionId = 0;

 static const char MyServiceName[] = "org.alljoyn.sample.measure";
 const char* busUniqueName;


static const char* const testInterface[] = {
       "org.alljoyn.sample.measure",
   "!AddDUSName DUS>s",

       NULL
 };


 static const AJ_InterfaceDescription testInterfaces[] = {
      testInterface,
      NULL
 };

 /**
    * Objects implemented by the application
  */
static const AJ_Object AppObjects[] = {
      { "/org/alljoyn/sample/measure", testInterfaces },
      { NULL }
 };

  #define APP_ADDDUSNAME                  AJ_APP_MESSAGE_ID(0, 0, 0)


static void AppDoWork()
{

AJ_Printf("do work\n");
}

static const char PWD[] = "1234";

static uint32_t PasswordCallback(uint8_t* buffer, uint32_t bufLen)
{
   memcpy(buffer, PWD, sizeof(PWD));
   return sizeof(PWD) - 1;
}


//register signal

static AJ_Status AddDUSNameSignal(AJ_Message* msg,const char* busUniqueName,uint32_t sessionId)
{

        AJ_Message registertion;
    char* DUS ="Alljoyn Thin Client,uniquename,org.alljoyn.sample.measure";

   AJ_Printf("Emitting Registertion Signal. The device name is %s,uniquename is %s, service name is     %s.\n",DeviceName,busUniqueName,MyServiceName);



  AJ_MarshalSignal(&bus, &registertion, APP_ADDDUSNAME,GatewayServiceName, sessionId, 0, 0);
  AJ_MarshalArgs(&registertion, "s", DUS);

return AJ_DeliverMsg(&registertion);
 }


 #define CONNECT_TIMEOUT    (1000 * 1000)
 #define UNMARSHAL_TIMEOUT  (1000 * 5)

int AJ_Main(void)
{


AJ_Status status = AJ_OK;

uint8_t connected = FALSE;
uint8_t getunique = FALSE;

uint32_t sessionId = 0;


/*
 * One time initialization before calling any other AllJoyn APIs
 */
AJ_Initialize();
AJ_PrintXML(AppObjects);
AJ_RegisterObjects(AppObjects, NULL);


while (TRUE) {
    AJ_Message msg;

    if (!connected) {
        status = AJ_StartClient(&bus,
                                   NULL, 
                                   CONNECT_TIMEOUT, 
                                   FALSE,
                                   GatewayServiceName,
                                   ServicePort,
                                   &sessionId,
                                   NULL);
       if (status == AJ_OK) {
            AJ_Printf("StartClient returned %d, sessionId=%u.\n", status, sessionId);
            connected = TRUE;
        } else {
            AJ_Printf("StartClient returned 0x%04x.\n", status);
            break;
        }
        AJ_InfoPrintf(("StartService returned AJ_OK; running %s:%u\n", GatewayServiceName, ServicePort));
        connected = TRUE;
        AJ_BusSetPasswordCallback(&bus, PasswordCallback);

        busUniqueName = AJ_GetUniqueName(&bus);
           if (busUniqueName == NULL) {
           AJ_Printf("Failed to GetUniqueName() from newly connected bus,retrying\n");
               continue;
           }else{
           AJ_Printf("Connected to daemon with BusUniqueName=%s\n",busUniqueName);
           }

        status = AddDUSNameSignal(&msg,busUniqueName,sessionId);

    }

    status = AJ_UnmarshalMsg(&bus, &msg, UNMARSHAL_TIMEOUT);
    if (status != AJ_OK) {
        if (status == AJ_ERR_TIMEOUT) {
            AppDoWork();
            continue;
        }
    }

    if (status == AJ_OK) {

     // AJ_Printf("msgId is %s\n",msg.msgId);
        switch (msg.msgId) {
         case AJ_REPLY_ID(AJ_METHOD_JOIN_SESSION):
            AJ_Printf("JoinSession SUCCESS (Session id=%d).\n", sessionId);
            break;

        case APP_ADDDUSNAME:
            AddDUSNameSignal(&msg,busUniqueName,sessionId);

            break;

       case AJ_SIGNAL_SESSION_LOST_WITH_REASON:
            /* A session was lost so return error to force a disconnect. */
            {
                uint32_t id, reason;
                AJ_UnmarshalArgs(&msg, "uu", &id, &reason);
                AJ_AlwaysPrintf(("Session lost. ID = %u, reason = %u", id, reason));
            }
            status = AJ_ERR_SESSION_LOST;
            break;

        default:
            /*
             * Pass to the built-in bus message handlers
             */
            status = AJ_BusHandleBusMessage(&msg);
            break;
        }
    }
    /*
     * Unarshaled messages must be closed to free resources
     */
    AJ_CloseMsg(&msg);

    if (status == AJ_ERR_SESSION_LOST) {
        AJ_Printf("AllJoyn disconnect\n");
        AJ_Disconnect(&bus);
        connected = FALSE;
        /*
         * Sleep a little while before trying to reconnect
         */
        AJ_Sleep(10 * 1000);
    }
}
AJ_Printf("Measure client exiting with status %d.\n", status);

return status;
   }

Here is ... (more)

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2014-05-07 14:32:31 -0700

mquon gravatar image

Hi, I think you're missing an addMatch call on the BusAttachment, adding a required match rule.

After invoking "RegisterSignalHandler", please add the following code:

bus.AddMatch("type='signal',interface='org.alljoyn.Bus.centralcontrol',member='AddDUSName'");

Please try that out and let me know if that works for you.

Thanks, Mike

edit flag offensive delete publish link more

Comments

Hi,Mike I have done what you say, but it doesn't work. Arduino has emitted signal successful, but PC has nothing happen. I confirm that method AddMatch implements successful. zhengkun

zhengkun ( 2014-05-08 00:55:27 -0700 )edit
Login/Signup to Answer

Question Tools

Follow
1 follower

Stats

Asked: 2014-05-06 20:54:44 -0700

Seen: 245 times

Last updated: May 07 '14