This is an example of setting up a responder

My setup: two CAN channels, channel numbers 1 and 2.

This responder listens to channel 1 for any message with the value 0x23 in header byte #2. It responds with a message transmitted on channel 1 (header = 0x01 0x02, data = 0x5a).

Message structure Msg is from the gryphon example code's gclient.h; reproduced here for convenience.


   struct Msg { 
      struct gframehdr fh; 
      union { 
           struct { 
                 struct gcmdhdr header; 
                 unsigned char data[MAXPAYLOAD]; 
           } command; 
           struct { 
                 struct gdatahdr header; 
                 unsigned char data[MAXPAYLOAD]; 
           } data; 
           struct { 
                 struct geventhdr header; 
                 unsigned char data[MAXPAYLOAD]; 
           } event; 
           struct { 
                 struct gresphdr header; 
                 unsigned char data[MAXPAYLOAD]; 
           } response; 
           struct { 
                 unsigned char data[MAXPAYLOAD]; 
           } misc; 
           struct { 
                 unsigned char data[MAXPAYLOAD]; 
           } text; 
      } subhdr; 
   }; 

// Macros to shorten common Msg structure element names 
#define FH fh 

#define DH subhdr.data.header 
#define EH subhdr.event.header 
#define RH subhdr.response.header 
#define CH subhdr.command.header 

#define DD subhdr.data.data 
#define ED subhdr.event.data 
#define RD subhdr.response.data 
#define CD subhdr.command.data 
#define TD subhdr.text.data 

    struct Msg m; 
    struct gresponse *gr; 
    struct gframehdr *gf; 
    struct gdatahdr *gd; 

// Set up the responder command 

// frame header 

m.FH.src=SD_CLIENT; 
m.FH.srcchan=c_id; 
m.FH.dst=SD_RESP; 
m.FH.dstchan=1; 
m.FH.frametype=FT_CMD; 
m.FH.msglen=sizeof(struct gcmdhdr)+sizeof(struct gresponse)+sizeof(struct gframehdr)+sizeof(struct gdatahdr)+3+3; 

// command header 

m.CH.cmd=CMD_MSGRESP_ADD; 
m.CH.context=1; 

// make a pointer to the gresponse structure in command data 

gr=(struct gresponse *)(m.CD); 

// gresponse structure 

gr->header.flags=FILTER_FLAG_ACTIVE; 
gr->header.num_fields=1; 
gr->header.num_responses=1; 
gr->header.old_handle=0; 
gr->header.action=FR_RESP_AFTER_EVENT; 
gr->header.value=0; 

// filter body 

gr->body.offset=htons(1); 
gr->body.length=htons(1); 
gr->body.data_type=FILTER_DATA_TYPE_HEADER; 
gr->body.op=VALUE_EQ; 
gr->body.value1[0]=0x23; 


// make a pointer to the output message: frame header 

gf=(struct gframehdr *)(m.CD + sizeof(struct gresponse) + 2); 

gf->src=SD_CLIENT; 
gf->srcchan=c_id; 
gf->dst=SD_CARD; 
gf->dstchan=1; 
gf->msglen=htons( sizeof(struct gdatahdr) + 3); 
gf->frametype=FT_DATA; 

// make a pointer to the output message: data header 

gd=(struct gdatahdr *)((unsigned char *)(gf) + sizeof(struct gframehdr)); 

gd->hdrlen=2; 
gd->hdrbits=11; 
gd->datalen=htons(1); 
gd->extralen=0; 
gd->mode=MODE_TX; 
gd->time=0; 

// And finally the output message's actual message header/data 

p=(unsigned char *)(gd)+sizeof(struct gdatahdr); 

*p++ = 1; 
*p++ = 2; 
*p++ = 0x5a; 
*p++ = 0; 

gsend(s, &m); 
respid=0; 

// Get the response to cmd_msgresp_add: we need the responder handle 

while(!respid) 
{ 
  grcv(s,&m); 
  if(m.FH.frametype == FT_RESP) 
        if(m.RH.cmdhdr.cmd == CMD_MSGRESP_ADD) 
              if(m.RH.resp == 0) 
                    respid = (unsigned char)*(m.RD); 
} 
printf("\nGot resp ID %02x.",respid); 

// Activate the above response via cmd_msgresp_modify command 

m.FH.src=SD_CLIENT; 
m.FH.srcchan=c_id; 
m.FH.dst=SD_RESP; 
m.FH.dstchan=1; 
m.FH.frametype=FT_CMD; 
m.FH.msglen=sizeof(struct gcmdhdr)+4; 
m.CH.cmd=CMD_MSGRESP_MODIFY; 
m.CH.context=1; 

p=m.CD; 
*p++ = respid; 
*p++ = ACTIVATE_RESPONSE; 
gsend(s, &m); 


// Set loopback on/off with CMD_CARD_TX_LOOP_(ON/OFF) (modified for test cases) 

m.FH.src=SD_CLIENT; 
m.FH.srcchan=c_id; 
m.FH.dst=SD_CARD; 
m.FH.dstchan=1; 
m.FH.frametype=FT_CMD; 
m.FH.msglen=sizeof(struct gcmdhdr); 
m.CH.cmd=CMD_CARD_TX_LOOP_OFF; 
m.CH.context=1; 
gsend(s, &m); 

This document was obtained from:

Dearborn Group, Inc.
(248) 488-2080
dg@dgtech.com
http://www.dgtech.com

Copyright © 2003 Dearborn Group Inc.