void Mqtt_Ack2Cloud( uint8 *pPhoneClient,uint8* szDID,uint8 *pData,uint32 datalen ) { uint8 msgtopic[60]={0}; uint8 pos=0; memcpy( msgtopic+pos,"dev2app/",strlen("dev2app/")); pos+=strlen( "dev2app/" ); memcpy( msgtopic+pos,szDID,strlen((const int8 *)szDID) ); pos+=strlen((const int8*)szDID); msgtopic[pos] = '/'; pos++; memcpy( msgtopic+pos,pPhoneClient,strlen( (const int8 *)pPhoneClient) ); pos+=strlen( (const int8*)pPhoneClient ); msgtopic[pos] = '\0'; GAgent_Printf( GAGENT_DEBUG,"msgtopic:%s :len=%d",msgtopic,strlen( (const int8 *)msgtopic) ); //TODO ack buf. PubMsg( &g_stMQTTBroker,( const int8*)msgtopic,( int8* )pData,datalen,0 ); return ; }
/************************************************* * FUNCTION : send mcu cmd to cloud * uartbuf : uart receive buf * *************************************************/ void MCU_SendPacket2Cloud( u8* uartbuf,int uartbufLen ) { u8 *SendPack=NULL; int SendPackLen=0; u8 *ClientId=NULL; int ClientIdLen=0; int UartP0Len=0; u8 McuSendTopic[200]={0}; short VarLen=0; varc SendVarc; int i; ClientIdLen = uartbuf[8]*256+uartbuf[9]; ClientId = (u8*)malloc( ClientIdLen ); if( ClientId==NULL ) { return; } //protocolVer(4B)+len(2B)+cmd(2B)+phoneClientIdLen(2B)+phoneClientId+p0 UartP0Len = uartbufLen-10-ClientIdLen; memset(ClientId,0,ClientIdLen); memcpy(ClientId,uartbuf+10,ClientIdLen); memcpy( McuSendTopic,"dev2app/",strlen("dev2app/")); memcpy( McuSendTopic+strlen("dev2app/"),g_Xpg_GlobalVar.DID,strlen(g_Xpg_GlobalVar.DID)); McuSendTopic[strlen("dev2app/")+strlen(g_Xpg_GlobalVar.DID)]='/'; memcpy( McuSendTopic+strlen("dev2app/")+strlen(g_Xpg_GlobalVar.DID)+1,ClientId,ClientIdLen); //protocolVer(4B)+varLen(1~4B)+flag(1B)+cmd(2B)+P0 VarLen=1+2+UartP0Len; SendVarc = Tran2varc(VarLen); SendPackLen = 4+SendVarc.varcbty+1+2+UartP0Len; SendPack = (u8*)malloc(SendPackLen); if( SendPack==NULL ) { free(ClientId); return ; } //protocolVer SendPack[0] = 0x00; SendPack[1] = 0x00; SendPack[2] = 0x00; SendPack[3] = 0x03; //varLen for(i=0;i<SendVarc.varcbty;i++) { SendPack[4+i] = SendVarc.var[i]; } //flag SendPack[4+SendVarc.varcbty] = 0x00; //CMD SendPack[4+SendVarc.varcbty+1]=0x00; SendPack[4+SendVarc.varcbty+2]=0x91; //p0 memcpy( (SendPack+4+SendVarc.varcbty+3),uartbuf+10+ClientIdLen,UartP0Len); GAgent_Printf(GAGENT_INFO,"MCU data to Topic: %s",McuSendTopic); PubMsg( &g_stMQTTBroker,McuSendTopic,SendPack,SendPackLen,0); free( SendPack ); free( ClientId ); return; }
/******************************************************************** * * FUNCTION : Mqtt send request packbag to server ask online client * add by alex.lin --2014-12-11 ********************************************************************/ void Mqtt_ReqOnlineClient(void) { char req_buf[6] = {0x00,0x00,0x00,0x03,0x02,0x0f}; PubMsg( &g_stMQTTBroker,"cli2ser_req",req_buf,6,0); }
/******************************************************************** Function : MQTT_SenData. Description : send buf data to m2m. szDID : Device ID. buf : need to send data pointer. buflen : buf length . return : 0-ok other fail. Add by Alex.lin --2015-03-17 ********************************************************************/ int32 MQTT_SenData( pgcontext pgc, int8 *szDID, ppacket pbuf,/*uint8 *buf,*/ int32 buflen ) { uint8 *sendpack=NULL; int32 i=0,sendpacklen=0,headlen=0; int16 varlen=0; volatile varc sendvarc; int8 msgtopic[64]= {0}; int32 didlen=0; uint16 cmd = pgc->rtinfo.stChannelAttrs.cloudClient.cmd; int32 sn = pgc->rtinfo.stChannelAttrs.cloudClient.sn; int8 *clienid = pgc->rtinfo.stChannelAttrs.cloudClient.phoneClientId; uint8 pos = 0; int32 clienIdLen; didlen = strlen(szDID); if(didlen!=22) return -1; memcpy( msgtopic,"dev2app/",strlen("dev2app/")); pos += strlen("dev2app/"); memcpy( msgtopic+strlen("dev2app/"),szDID,didlen ); pos += didlen; if(0x0094 == cmd) { clienIdLen = strlen( (const int8 *)clienid); if(clienIdLen > 0) { msgtopic[pos] = '/'; pos++; memcpy( msgtopic+pos,clienid,clienIdLen ); pos+=clienIdLen; } } msgtopic[pos] = '\0'; //protocolVer(4B)+varLen(1~4B)+flag(1B)+cmd(2B)+P0 varlen = 1+2+buflen; if(0x0093 == cmd || 0x0094 == cmd) { varlen += sizeof(sn); } sendvarc=Tran2varc(varlen); sendpacklen = 4+sendvarc.varcbty+varlen; headlen = sendpacklen-buflen; sendpack = ( (pbuf->ppayload)-headlen ); //protocolVer sendpack[0] = 0x00; sendpack[1] = 0x00; sendpack[2] = 0x00; sendpack[3] = 0x03; //varLen for(i=0;i<sendvarc.varcbty;i++) { sendpack[4+i] = sendvarc.var[i]; } //flag sendpack[4+sendvarc.varcbty] = 0x00; //CMD *(uint16 *)&sendpack[4+sendvarc.varcbty+1] = htons(cmd); if(0x0093 == cmd || 0x0094 == cmd) { *(int32 *)&sendpack[4+sendvarc.varcbty+1 + 2] = htonl(sn); } GAgent_Printf(GAGENT_DEBUG,"------SEND TO Cloud ------\r\n"); for(i=0;i<sendpacklen;i++) GAgent_Printf(GAGENT_DUMP," %02X",sendpack[i] ); PubMsg( &g_stMQTTBroker,msgtopic,(int8 *)sendpack,sendpacklen,0 ); return 0; }