Ejemplo n.º 1
0
/****************************************************************
        Function        :   Cloud_InitSocket
        Description     :   init socket connect to server.
        iSocketId       :   socketid.
        p_szServerIPAddr:   server ip address like "192.168.1.1"
        port            :   server socket port.
        flag            :   =0 init socket in no block.
                            =1 init socket in block.
        return          :   >0 the cloud socket id 
                            <=0 fail.
****************************************************************/
int32 Cloud_InitSocket( int32 iSocketId,int8 *p_szServerIPAddr,int32 port,int8 flag )
{
    int32 ret=0;

    ret = strlen( p_szServerIPAddr );
    
    if( ret<=0 || ret> 17 )
        return RET_FAILED;

    GAgent_Printf(GAGENT_DEBUG,"socket connect cloud ip:%s .",p_szServerIPAddr);
    if( iSocketId > 0 )
    {
        GAgent_Printf(GAGENT_DEBUG, "Cloud socket need to close SocketID:[%d]", iSocketId );
        close( iSocketId );
        iSocketId = INVALID_SOCKET;
    }

    if( (iSocketId = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))<=0)
    {
        GAgent_Printf(GAGENT_ERROR," Cloud socket init fail");
        return RET_FAILED;
    }

    GAgent_Printf(GAGENT_DEBUG, "New cloud socketID [%d]",iSocketId);
    ret = GAgent_connect( iSocketId, port, p_szServerIPAddr,flag );

    if ( RET_SUCCESS!=ret )
    {
        close(iSocketId);
        iSocketId=INVALID_SOCKET;
        GAgent_Printf(GAGENT_ERROR, "Cloud socket connect fail with:%d", ret);
        return -3;
    }
    return iSocketId;
}
int GAgent_GetHostByName(char *domain, char *IPAddress)
{
    unsigned char Err;
    ip_addr_t ip_addr;
    char str[32];

    memset(str, 0x0, sizeof(str));

    switch (dns_gethostbyname(domain, &ip_addr, Gagent_ServerFound, NULL)) {
        case ERR_OK:
			memcpy((unsigned char*)IPAddress, (unsigned char*)inet_ntoa(ip_addr.addr), strlen(inet_ntoa(ip_addr.addr)) + 1);
        	GAgent_Printf(GAGENT_LOG, "ok Server name %s, first address: %s", 
                domain, inet_ntoa(domain_ipaddr.addr));
			return 0;
        case ERR_INPROGRESS:
            GAgent_Printf(GAGENT_LOG, "dns_gethostbyname called back success.\n\r");

			/* Waiting for connect success */
		    OSSemPend(pdomainSem, 1000, &Err);
		
		    if (domain_flag == 1) {
		        memcpy((unsigned char*)IPAddress, (unsigned char*)inet_ntoa(domain_ipaddr.addr), strlen(inet_ntoa(domain_ipaddr.addr)) + 1);
		        GAgent_Printf(GAGENT_LOG, "call back Server name %s, first address: %s", 
		                domain, inet_ntoa(domain_ipaddr.addr));
				return 0;
		    } else {
		        return -1;
		    }
            break;

        default:
            GAgent_Printf(GAGENT_LOG, "dns_gethostbyname called back error.\n\r");
            return -1;
    }
}
int Socket_CreateUDPServer(int udp_port)
{
    struct sockaddr_in addr;
    if (g_UDPServerFd == -1) 
    {
        g_UDPServerFd = socket(AF_INET, SOCK_DGRAM, 0);
        if (g_UDPServerFd < 1)
        {
            GAgent_Printf(GAGENT_DEBUG, "UDPServer socket create error,errno:%d", errno);
            g_UDPServerFd = -1;
            return -1;
        }

        memset(&addr, 0x0, sizeof(addr));
        addr.sin_family = AF_INET;
        addr.sin_port = htons(udp_port);
        addr.sin_addr.s_addr = INADDR_ANY;

        if (bind(g_UDPServerFd, (struct sockaddr *)&addr, sizeof(addr)) != 0)
        {
            GAgent_Printf(GAGENT_DEBUG, "UDPServer socket bind error,errno:%d", errno);
            close(g_UDPServerFd);
            g_UDPServerFd = -1;
            return -1;
        }
    }
    GAgent_Printf(GAGENT_DEBUG, "UDP Server socketid:%d on port:%d", g_UDPServerFd, udp_port);
    return 0;
}
Ejemplo n.º 4
0
int32 Http_POST( int32 socketid, const int8 *host,const int8 *passcode,const int8 *mac,const uint8 *product_key )
{
    int32 ret=0;
    uint8 *postBuf=NULL;
    int8 *url = "/dev/devices"; 
    int8 Content[100]={0};
    int32 ContentLen=0;
    int32 totalLen=0;
    //int8 *contentType="application/x-www-form-urlencoded";
    
    postBuf = (uint8*)malloc(400);
    if (postBuf==NULL) return 1;
    //g_globalvar.http_sockettype =HTTP_GET_DID;//http_sockettype=1 :http_post type.

    sprintf(Content,"passcode=%s&mac=%s&product_key=%s",passcode,mac,product_key);
    ContentLen=strlen(Content);
    snprintf( postBuf,400,"%s %s %s%s%s %s%s%s %d%s%s%s%s%s",
              "POST" ,url,"HTTP/1.1",kCRLFNewLine,
              "Host:",host,kCRLFNewLine,
              "Content-Length:",ContentLen,kCRLFNewLine,
              "Content-Type: application/x-www-form-urlencoded",kCRLFNewLine,
              kCRLFNewLine,
              Content
        );
    totalLen = strlen( postBuf );
    GAgent_Printf(GAGENT_DEBUG,"http_post:%s %d",postBuf,totalLen);    
    ret = send( socketid,postBuf,totalLen,0 );
    GAgent_Printf(GAGENT_DEBUG,"http_post ret: %d",ret);    
    free( postBuf );
    return 0;
}
int Socket_CreateUDPBroadCastServer( int udp_port )
{
    int udpbufsize = 2;

    if ( g_UDPBroadcastServerFd == -1 )
    {
        g_UDPBroadcastServerFd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
        if (g_UDPBroadcastServerFd < 0)
        {
            GAgent_Printf(GAGENT_DEBUG, "UDP BC socket create error, errno:%d", errno);
            g_UDPBroadcastServerFd = -1;
            return -1;
        }

        g_stUDPBroadcastAddr.sin_family = AF_INET;
        g_stUDPBroadcastAddr.sin_port = htons(udp_port);
        g_stUDPBroadcastAddr.sin_addr.s_addr = htonl(INADDR_BROADCAST);

        if (setsockopt(g_UDPBroadcastServerFd, SOL_SOCKET, SO_BROADCAST, &udpbufsize, sizeof(int)) != 0)
        {
            GAgent_Printf(GAGENT_DEBUG, "UDP BC Server setsockopt error,errno:%d", errno);
            //return;
        }

        if (bind(g_UDPBroadcastServerFd, (struct sockaddr *)&g_stUDPBroadcastAddr, sizeof(g_stUDPBroadcastAddr)) != 0)
        {
            GAgent_Printf(GAGENT_DEBUG, "UDP BC Server bind error,errno:%d", errno);
            close(g_UDPBroadcastServerFd);
            g_UDPBroadcastServerFd = -1;
            return -1;
        }
    }
    GAgent_Printf(GAGENT_DEBUG, "UDP BC Server socketid:%d on port:%d", g_UDPBroadcastServerFd, udp_port);
    return 0;
}
Ejemplo n.º 6
0
int GAgent_Socket_DispatchUDPRecvData( u8 *pData, int varlen, int messagelen, struct sockaddr_t addr, socklen_t addrLen)
{
    int ret;
    u8 Udp_ReDiscover[200] =  {0};
    char Udp_ReOnBoarding[8] = {0x00,0x00,0x00,0x03,0x03,0x00,0x00,0x02};

    if( pData[7]==0x01 )//OnBoarding
    {

        GAgent_Printf(GAGENT_INFO,"OnBoarding");

        Udp_Onboarding(pData,messagelen);
        GAgent_Socket_sendto(g_GAgent_UDPServerFd, Udp_ReOnBoarding, 8, &addr, sizeof(struct sockaddr_t));
        DRV_GAgent_Reset();
        return 0;
    }
    if( pData[7]==0x03 ) //Ondiscover
    {
        GAgent_Printf(GAGENT_INFO,"Ondiscover");
        GAgent_Build_BroadCastPacket( 0, Udp_ReDiscover );
        ret = GAgent_Socket_sendto(g_GAgent_UDPServerFd, Udp_ReDiscover,Udp_ReDiscover[4]+5, &addr, sizeof(struct sockaddr_t));
        if(ret != Udp_ReDiscover[4]+5)
        {
            GAgent_Printf(GAGENT_ERROR,"send discover response fail.ret:0x%x", ret);
        }
        return 0;
    }
    GAgent_Printf(GAGENT_WARNING,"invalid udp packet [%d]", pData[7]);
    return -1;
}
Ejemplo n.º 7
0
/*************************************************
 *
 *		Function : check mqtt witch qos 1
 *		packet_bufferBUF: MQTT receive data
 *		packet_length :	the packet length 
 *   add by Ale lin  2014-04-03
 *		
 ***************************************************/
int check_mqttpushqos1( uint8_t *packet_bufferBUF,int packet_length, uint16_t msg_id )
{
    uint16_t msg_id_rcv;
    uint8_t *packet_buffer=NULL;
    packet_buffer = ( uint8_t* )malloc(packet_length);
    memset( packet_buffer,0,packet_length);
    memcpy( packet_buffer,packet_bufferBUF,packet_length);
    if(packet_length < 0)
    {
        GAgent_Printf(GAGENT_INFO,"Error on read packet!");	
        free(packet_buffer);
        return -1;
    }
    if(MQTTParseMessageType(packet_buffer) != MQTT_MSG_PUBACK)
    {
        GAgent_Printf(GAGENT_INFO,"PUBACK expected!");
        free(packet_buffer);
        return -1;						
    }
    msg_id_rcv = mqtt_parse_msg_id(packet_buffer);
    if(msg_id != msg_id_rcv)
    {
        GAgent_Printf(GAGENT_INFO," message id was expected, but message id was found!");
        free(packet_buffer);
        return -1;
    }			
    free(packet_buffer);						
    GAgent_Printf(GAGENT_INFO,"check_mqttpushqos1 OK");
    
    return 1;														
}
Ejemplo n.º 8
0
/***********************************************************
 *
 *   return :    0 success ,1 error
 *
 *************************************************************/
int32 Mqtt_SendConnetPacket( mqtt_broker_handle_t *pstBroketHandle, int32 socketid,const int8* username,const int8* password )
{       
    int32 ret;
    
    if( (username==NULL) || (password==NULL) )// 匿名登录 GAgent V4 will not runing this code.
    {
        return 1;
    }
    else // 用户登录
    {
        mqtt_init( pstBroketHandle,username );
        mqtt_init_auth(pstBroketHandle,username,password );
    }
    mqtt_set_alive(pstBroketHandle, CLOUD_MQTT_SET_ALIVE);
    pstBroketHandle->socketid = socketid;
    pstBroketHandle->mqttsend = send_packet;

    ret = mqtt_connect(pstBroketHandle);
    if (ret != 0)
        {
            GAgent_Printf(GAGENT_WARNING,"mqtt send connect packet is failed with:%d", ret); 
            return 1;
        }
    GAgent_Printf(GAGENT_INFO, "Mqtt_SendConnetPacket OK, write:%d", ret); 
    return 0;
}
Ejemplo n.º 9
0
/******************************************************
 *
 *   FUNCTION       :   get the uuid by productkey in HTTP GET
 *      productkey  :   productkey
 *      host        :   host
 *      return      :   0-OK 1-fail
 *   Add by Alex lin  --2014-11-10
 *
 ********************************************************/
int32 Http_JD_Get_uuid_req( const int8 *host,const int8 *product_key )
{
    int8 *getBuf=NULL;
    int32 totalLen=0;
    int32 ret=0;
    int8 *url = "/dev/jd/product/";    
    if( strlen(product_key)<=0 )
    {   
        GAgent_Printf(GAGENT_ERROR,"need a productkey to get uuid! ");
        return 1;
    }
    getBuf = (int8*)malloc(500);
    if (getBuf==NULL)  return 1;
    memset( getBuf,0,500 ); 
    //g_globalvar.http_sockettype =HTTP_GET_JD_UUID;//http get product_uuid for JD.

    sprintf( getBuf,"%s %s%s%s%s%s%s%s%s%s",
             "GET",url,product_key," HTTP/1.1",kCRLFNewLine,
             "Host: ",host,kCRLFNewLine,
             "Cache-Control: no-cache",kCRLFLineEnding );

    totalLen =strlen( getBuf );
    GAgent_Printf(GAGENT_DEBUG,"totalLen=%d\r\n",totalLen);
    GAgent_Printf(GAGENT_DEBUG,"Sent Http to get JD uuid.\n");
    GAgent_Printf(GAGENT_DUMP,"%s",getBuf );
    free(getBuf);
   
    if(ret>0) return 0; 
    else    return 1;
}
Ejemplo n.º 10
0
int32 Mqtt_Login2Server(  int32 socketid,const uint8 *username,const uint8 *password )
{
    if( Mqtt_SendConnetPacket( &g_stMQTTBroker,socketid,(const int8*)username,(const int8*)password ) == 0)
    {    
        GAgent_Printf(GAGENT_INFO," Mqtt_SendConnetPacket OK!");
        return 0;
    }   
    GAgent_Printf(GAGENT_INFO," Mqtt_SendConnetPacket NO!");    
    
    return 1;
}
Ejemplo n.º 11
0
void debugpacket(ppacket pp)
{
    if(pp->pend < pp->ppayload)
    {
        GAgent_Printf(GAGENT_DEBUG, "Wrong packet to dump");
        return;
    }
    GAgent_Printf(GAGENT_DEBUG, "Packet info: len = %x, head len = %x, payload len = %x",
                  pp->pend - pp->phead, pp->ppayload - pp->phead, pp->pend - pp->ppayload);
    GAgent_DebugPacket(pp->phead, pp->pend - pp->phead);
}
Ejemplo n.º 12
0
/****************************************************************
FunctionName    :   GAgent_LocalSendData
Description     :   send data to local io.
return          :   NULL
Add by Alex.lin     --2015-04-07
****************************************************************/
uint32 Local_SendData( int32 fd,uint8 *pData, int32 bufferMaxLen )
{
    int32 i=0;
    if( PF_SendData2MCU!=NULL )
    {
        GAgent_Printf( GAGENT_DUMP,"local send len = %d:\r\n",bufferMaxLen );
        for( i=0;i<bufferMaxLen;i++ )
            GAgent_Printf( GAGENT_DUMP," %02x",pData[i]);
        GAgent_Printf( GAGENT_DUMP,"\r\n");
        PF_SendData2MCU( fd,pData,bufferMaxLen );
    }
    return 0;
}
Ejemplo n.º 13
0
int GAgent_Socket_CheckNewTCPClient(void)
{
    fd_set readfds, exceptfds;
    int ret;
    int newClientfd;
    int i;
    struct sockaddr_t addr;
	struct timeval_t t;
    int addrlen= sizeof(addr);
    t.tv_sec = 0;//秒
    t.tv_usec = 0;//微秒
    /*Check status on erery sockets */
    FD_ZERO(&readfds);
    FD_SET(g_GAgent_TCPServerFd, &readfds);

    ret = select((g_GAgent_TCPServerFd+1), &readfds, NULL, &exceptfds, &t);

    if (ret > 0)
    {
        newClientfd = GAgent_Socket_accept(g_GAgent_TCPServerFd, &addr, &addrlen);
        GAgent_Printf(GAGENT_DEBUG, "detected new client as %d", newClientfd);
        if (newClientfd > 0)
        {
            if(g_GAgent_TCPSocketMaxClientFd<newClientfd)
            {
                g_GAgent_TCPSocketMaxClientFd=(newClientfd+1); /*Select接口要求+1*/
            }
            for(i=0;i<SOCKET_MAXTCPCLENT;i++) 
            {
                if (g_GAgent_stTCPSocketClients[i].SocketFD == -1) 
                {
                    memcpy(&g_GAgent_stTCPSocketClients[i].TCPClientAddr, &addr, sizeof(addr));
                    g_GAgent_stTCPSocketClients[i].SocketFD = newClientfd;
                    g_GAgent_stTCPSocketClients[i].TimeOut = LAN_CLIENT_MAXLIVETIME;
                    GAgent_Printf(GAGENT_INFO, "new tcp client, clientid:%d[%d] [time:%08x second: %08x MS]", 
                        newClientfd, i, DRV_GAgent_GetTime_S(), DRV_GAgent_GetTime_MS() );
                    
                    return newClientfd; //返回连接ID
                }
            }
            /*此时连接个数大于系统定义的最大个数,应该关闭释放掉对应的socket资源*/           
            close( newClientfd );
        }
        else
        {
            GAgent_Printf(GAGENT_DEBUG, "Failed to accept client");
        }
    }
    return -1;
}
void Gagent_ServerFound(const char *name, ip_addr_t *ipaddr, void *arg)
{
    if ((ipaddr) && (ipaddr->addr)) {
        memcpy(&domain_ipaddr, ipaddr, sizeof(ip_addr_t));
        domain_flag = 1;
        GAgent_Printf(GAGENT_LOG, "Get DNS server ip success.\n\r");

    } else {
        domain_flag = 0;
        GAgent_Printf(GAGENT_LOG, "Get DNS server ip error.\n\r");    
    }

    OSSemPost(pdomainSem);
}
Ejemplo n.º 15
0
/****************************************************************
FunctionName    :   GAgent_Local_ExtractOnePacket
Description     :   extract one packet from local cycle buf, and 
                    put data into buf.Will change pos_start
buf             :   dest buf
return          :   >0 the local packet data length.
                    <0 don't have one whole packet data
****************************************************************/
int32 GAgent_Local_ExtractOnePacket(uint8 *buf)
{
    int32 ret;
    uint32 i;
    uint16 packetLen;

    if(NULL == buf)
    {
        GAgent_Printf(GAGENT_WARNING,"%s,%d,Input buf is NULL", __FUNCTION__, __LINE__);
        return RET_FAILED;
    }

    /* find one packet data form position pos_start of local driver buf */
    ret = find_packet_forward(pos_start, &packetLen);
    if(ret >= 0)
    {
        /* a whole packet */
        pos_start = ret;
        for(i = 0; i < packetLen; i++)
        {
            buf[i] = __halbuf_read(pos_start + i);
        }
        pos_start += packetLen;
        
        return packetLen;
    }

    return RET_FAILED;
}
Ejemplo n.º 16
0
int32 MQTT_readPacket( int32 socketid,ppacket pbuf,int32 bufferLen )
{
    int32 bytes_rcvd;
    uint8_t *pData;
    int32 messageLen;
    int32 varLen;
    int32 packet_length;

    memset(pbuf->phead, 0, bufferLen);
    pData = pbuf->phead;
    bytes_rcvd = recv(socketid, pData , bufferLen, 0);
    if((bytes_rcvd) <= 0)
    {
        MQTTclose_socket( &g_stMQTTBroker );
        return -1;
    }
    //pData = packetBuffer + 0;
    pbuf->pend=pbuf->phead+bytes_rcvd;
    
    messageLen = mqtt_parse_rem_len(pData);
    varLen = mqtt_num_rem_len_bytes(pData);
    if(varLen<1 || varLen>4)
    {
         return -3;
    }
    packet_length = varLen + messageLen + 1;

    if (bytes_rcvd < packet_length)
    {
        GAgent_Printf(GAGENT_INFO, " packet length too long %s:%d ", __FUNCTION__, __LINE__);
        return -3;
    }
   
    return bytes_rcvd;
}
Ejemplo n.º 17
0
void GAgent_Socket_ClientTimeoutTimer(void)
{
    int i,j;

    for (i = 0; i < SOCKET_MAXTCPCLENT; i++)
    {
        if (g_GAgent_stTCPSocketClients[i].TimeOut > 0)
        {
            g_GAgent_stTCPSocketClients[i].TimeOut --;
            
            if (g_GAgent_stTCPSocketClients[i].TimeOut ==0)
            {
                GAgent_Printf(GAGENT_DEBUG, 
                    "Close socket(%d) now[time:%08x second: %08x MS] ", 
                g_GAgent_stTCPSocketClients[i].SocketFD, DRV_GAgent_GetTime_S(), 
                DRV_GAgent_GetTime_MS());
                close(g_GAgent_stTCPSocketClients[i].SocketFD);
								for( j=0;j<8;j++ )
								{
										if(g_SocketLogin[j]==g_GAgent_stTCPSocketClients[i].SocketFD)
											g_SocketLogin[j]=0;
								}
                g_GAgent_stTCPSocketClients[i].SocketFD = -1;
								
            }
        }
    }

    return;
}
Ejemplo n.º 18
0
/****************************************************************
FunctionName    :   GAgent_Clean_Config
Description     :   GAgent clean the device config                  
pgc             :   global staruc 
return          :   NULL
Add by Frank Liu     --2015-05-08
****************************************************************/
void GAgent_Clean_Config( pgcontext pgc )
{
    memset( pgc->gc.old_did,0,DID_LEN);
    memset( pgc->gc.old_wifipasscode,0,PASSCODE_MAXLEN + 1);
  
    memcpy( pgc->gc.old_did,pgc->gc.DID,DID_LEN );
    memcpy( pgc->gc.old_wifipasscode,pgc->gc.wifipasscode,PASSCODE_MAXLEN + 1 );
    GAgent_Printf(GAGENT_INFO,"Reset GAgent and goto Disable Device !");  
    Cloud_ReqDisable( pgc );
    GAgent_SetCloudConfigStatus( pgc,CLOUD_RES_DISABLE_DID );

    memset( pgc->gc.wifipasscode,0,PASSCODE_MAXLEN + 1);
    memset( pgc->gc.wifi_ssid,0,SSID_LEN_MAX + 1 );
    memset( pgc->gc.wifi_key,0, WIFIKEY_LEN_MAX + 1 );
    memset( pgc->gc.DID,0,DID_LEN);
    
    memset( (uint8*)&(pgc->gc.cloud3info),0,sizeof( GAgent3Cloud ) );
    
    memset( pgc->gc.GServer_ip,0,IP_LEN_MAX + 1);
    memset( pgc->gc.m2m_ip,0,IP_LEN_MAX + 1);
    make_rand(pgc->gc.wifipasscode);

    pgc->gc.flag &=~XPG_CFG_FLAG_CONNECTED;
    GAgent_DevSaveConfigData( &(pgc->gc) );
}
Ejemplo n.º 19
0
/****************************************************************
        FunctionName        :   GAgent_LocalSendGAgentstatus.
        Description         :   check Gagent's status whether it is update.
        Add by Nik.chen     --2015-04-18
****************************************************************/
void GAgent_LocalSendGAgentstatus(pgcontext pgc,uint32 dTime_s )
{
    uint16 GAgentStatus = 0; 
    uint16 LastGAgentStatus = 0; 
    resetPacket( pgc->rtinfo.Rxbuf );
    if( (pgc->rtinfo.GAgentStatus) != (pgc->rtinfo.lastGAgentStatus) )
    {
          GAgentStatus = pgc->rtinfo.GAgentStatus&LOCAL_GAGENTSTATUS_MASK;
          LastGAgentStatus = pgc->rtinfo.lastGAgentStatus&LOCAL_GAGENTSTATUS_MASK;
          GAgent_Printf( GAGENT_INFO,"GAgentStatus change, lastGAgentStatus=0x%04x, newGAgentStatus=0x%04x", LastGAgentStatus, GAgentStatus);
          pgc->rtinfo.lastGAgentStatus = pgc->rtinfo.GAgentStatus&LOCAL_GAGENTSTATUS_MASK;
          GAgentStatus = htons(GAgentStatus);
          memcpy((pgc->rtinfo.Rxbuf->ppayload), (uint8 *)&GAgentStatus, 2);
          pgc->rtinfo.Rxbuf->pend =  (pgc->rtinfo.Rxbuf->ppayload)+2;
          pgc->rtinfo.updatestatusinterval =  0; 
          //GAgent_Printf(GAGENT_CRITICAL,"updateGagentstatusLast time=%d", (pgc->rtinfo.send2LocalLastTime));
         GAgent_LocalDataWriteP0( pgc,pgc->rtinfo.local.uart_fd, (pgc->rtinfo.Rxbuf), WIFI_STATUS2MCU );
    }

     pgc->rtinfo.updatestatusinterval+= dTime_s;

    if( (pgc->rtinfo.updatestatusinterval)  > LOCAL_GAGENTSTATUS_INTERVAL)
    {
        pgc->rtinfo.updatestatusinterval = 0;
        GAgentStatus = pgc->rtinfo.GAgentStatus&LOCAL_GAGENTSTATUS_MASK;
        GAgentStatus = htons(GAgentStatus);
        memcpy((pgc->rtinfo.Rxbuf->ppayload), (uint8 *)&GAgentStatus, 2);
        pgc->rtinfo.Rxbuf->pend =  (pgc->rtinfo.Rxbuf->ppayload)+2;
        GAgent_LocalDataWriteP0( pgc,pgc->rtinfo.local.uart_fd, (pgc->rtinfo.Rxbuf), WIFI_STATUS2MCU );
    }
}
Ejemplo n.º 20
0
int send_packet(int socketid, const void* buf, unsigned int count)
{
    int ret;
    ret = send(socketid, buf, count, 0);
    GAgent_Printf(GAGENT_INFO,"MQTT Send packet length=%d",ret );
    return ret;
}
Ejemplo n.º 21
0
/****************************************************************
*       FunctionName    :   send_broadCastPacket
*       Description     :   Send BroadCast data.
*       Add by Frank Liu   --2015-04-14
****************************************************************/
void send_broadCastPacket(pgcontext pgc,uint8* ptxBuf,uint16 cmdWord)
{
    int32 ret = 0;
    int32 len = 0;
    struct sockaddr_t addr;
    memset(&addr,0,sizeof(addr));
    
    resetPacket(pgc->rtinfo.Rxbuf);
    addr = pgc->ls.addr;

    if(NULL == ptxBuf)
    {
        return;
    }

    ret = combination_broadcast_packet(pgc,ptxBuf,cmdWord);
    if((RET_SUCCESS == ret) && (INVALID_SOCKET != pgc->ls.udpBroadCastServerFd))
    {
        len = ptxBuf[4] + LAN_PROTOCOL_HEAD_LEN + 1;
        ret = Socket_sendto(pgc->ls.udpBroadCastServerFd,ptxBuf,len,&addr,sizeof(addr));
        GAgent_Printf( GAGENT_INFO,"Send BroadCast datalen=%d cmd=%04X ret :%d",len,cmdWord,ret );
    }

    return ;
}
void GAgent_Timer()
{
    int i;

    for (i = 0; i < GAGENT_MAXTIMER; i++)
    {
        if (g_stCoreGAgentTimer[i].flag != 0)
        {
            g_stCoreGAgentTimer[i].nextSeconds --;

            if (g_stCoreGAgentTimer[i].nextSeconds == 0)
            {
                g_stCoreGAgentTimer[i].pfFunc();

                if (GAGENT_TIMER_CLOCK == g_stCoreGAgentTimer[i].flag)
                {
                    GAgent_Printf(GAGENT_DEBUG,"delete timerid:%d , flag:%d, second:%d", 
                            i, g_stCoreGAgentTimer[i].flag, g_stCoreGAgentTimer[i].initSecods);

                    g_stCoreGAgentTimer[i].flag = 0; 
                    g_stCoreGAgentTimer[i].initSecods = 0;
                }
                else
                {
                    g_stCoreGAgentTimer[i].nextSeconds = g_stCoreGAgentTimer[i].initSecods;    
                }
            }
        }
    }

    return;
}
Ejemplo n.º 23
0
// Use?
int check_mqttconnect( uint8_t *packet_buffer,int packet_length )
{
    if(packet_length < 0)
    {
        GAgent_Printf(GAGENT_INFO,"Error on read packet!");	
        return 1;
    }

    if(packet_buffer[3] != 0x00)
    {
        GAgent_Printf(GAGENT_INFO,"check_mqttconnect CONNACK failed![%d]", packet_buffer[3]);							
        return 3;
    }

    return 0;
}
Ejemplo n.º 24
0
int32 Http_GET( const int8 *host,const int8 *did,int32 socketid )
{
    static int8 *getBuf=NULL;
    int32 totalLen=0;
    int32 ret=0;
    int8 *url = "/dev/devices/";

    getBuf = (int8*)malloc( 200 );
    if(getBuf == NULL)
    {
        return 1;
    }
    memset( getBuf,0,200 );
    //g_globalvar.http_sockettype =HTTP_PROVISION;//http get type.
    snprintf( getBuf,200,"%s %s%s %s%s%s %s%s%s%s%s",
              "GET",url,did,"HTTP/1.1",kCRLFNewLine,
              "Host:",host,kCRLFNewLine
              "Cache-Control: no-cache",kCRLFNewLine,
              "Content-Type: application/x-www-form-urlencoded",kCRLFLineEnding);
    totalLen =strlen( getBuf );
    ret = send( socketid, getBuf,totalLen,0 );
    GAgent_Printf(GAGENT_DEBUG,"Sent provision:\n %s\n", getBuf);
    free(getBuf);
    getBuf = NULL;

    if(ret<=0 ) 
    {
        return 1;
    }
    else
    {
        return 0;
    }    
}
int GAgent_CreateTimer(int flag, unsigned long ms, void (*pTimerHandler)(void)) 
{
    int seconds = (ms+900)/1000;
    int i;

    if ((flag != GAGENT_TIMER_PERIOD) && (flag != GAGENT_TIMER_CLOCK))
    {
        return -1;
    }

    for (i = 0; i < GAGENT_MAXTIMER; i++)
    {
        if (g_stCoreGAgentTimer[i].flag == 0)
        {
            g_stCoreGAgentTimer[i].flag = flag;
            g_stCoreGAgentTimer[i].nextSeconds = seconds;
            g_stCoreGAgentTimer[i].initSecods = seconds;
            g_stCoreGAgentTimer[i].pfFunc = pTimerHandler;

            GAgent_Printf(GAGENT_DEBUG,"create timerid:%d , flag:%d, second:%d", i, flag, seconds);
            return i;
        }
    }

    return -1;
}
Ejemplo n.º 26
0
/****************************************************************
        FunctionName        :   GAgent_CloudTick.
        Description         :   GAgent Send cloud heartbeat to cloud
                                when mqttstatus is MQTT_STATUS_RUNNING

        Add by Alex.lin     --2015-03-10
****************************************************************/
void GAgent_CloudTick( pgcontext pgc )
{
    uint32 cTime=0,dTime=0;

    if( pgc->rtinfo.waninfo.mqttstatus != MQTT_STATUS_RUNNING )
        return;
    
    cTime = GAgent_GetDevTime_MS();
    dTime = abs(cTime - pgc->rtinfo.waninfo.send2MqttLastTime );
    
    if( dTime < CLOUD_HEARTBEAT )
        return ;
    
    if(pgc->rtinfo.waninfo.cloudPingTime > 2 )
    {
        ERRORCODE
        pgc->rtinfo.waninfo.cloudPingTime=0;
        GAgent_SetCloudServerStatus( pgc,MQTT_STATUS_START );
        GAgent_SetWiFiStatus( pgc,WIFI_CLOUD_STATUS,1 );
    }
    else
    {
        MQTT_HeartbeatTime();
        pgc->rtinfo.waninfo.cloudPingTime++;
        GAgent_Printf(GAGENT_INFO,"GAgent MQTT Ping ...");
    }
    pgc->rtinfo.waninfo.send2MqttLastTime = GAgent_GetDevTime_MS();

}
Ejemplo n.º 27
0
int8 dealPacket( pgcontext pgc, ppacket pTxBuf )
{
    GAgent_Printf(GAGENT_DEBUG,"\r\n");
    GAgent_Printf(GAGENT_DEBUG,"IN %s packet type : %04x",__FUNCTION__ ,pTxBuf->type );
    if( ((pTxBuf->type)&(LOCAL_DATA_OUT)) == LOCAL_DATA_OUT )
    {
        GAgent_Printf( GAGENT_DEBUG,"packet Type : LOCAL_DATA_OUT ");
        GAgent_LocalDataWriteP0( pgc,pgc->rtinfo.local.uart_fd, pTxBuf,MCU_CTRL_CMD );
        GAgent_Printf( GAGENT_DEBUG,"ReSetpacket Type : LOCAL_DATA_OUT ");
        pTxBuf->type = SetPacketType(pTxBuf->type, LOCAL_DATA_OUT, 0);

    }
    if( ((pTxBuf->type)&(CLOUD_DATA_OUT)) == CLOUD_DATA_OUT )
    {
        if( ((pgc->rtinfo.GAgentStatus)&WIFI_CLOUD_CONNECTED)  == WIFI_CLOUD_CONNECTED )
        {
            GAgent_Printf( GAGENT_DEBUG,"packet Type : CLOUD_DATA_OUT ");
            GAgent_Cloud_SendData(  pgc,pTxBuf,(pTxBuf->pend)-(pTxBuf->ppayload) );
            GAgent_Printf( GAGENT_DEBUG,"ReSetpacket Type : CLOUD_DATA_OUT ");
        }
        pTxBuf->type = SetPacketType(pTxBuf->type, CLOUD_DATA_OUT, 0);
    }
    if( ((pTxBuf->type)&(LAN_TCP_DATA_OUT)) == LAN_TCP_DATA_OUT )
    {
        GAgent_Lan_SendTcpData(pgc, pTxBuf);
        pTxBuf->type = SetPacketType(pTxBuf->type, LAN_TCP_DATA_OUT, 0);
    }
    GAgent_Printf( GAGENT_DEBUG,"OUT packet type : %04X\r\n",pTxBuf->type );
    return 0;
}
Ejemplo n.º 28
0
void GAgent_DebugV4Packet(int flag, unsigned char *packet, int packetLen)
{
    if (flag == GAGENT_DEBUG_PACKET_SEND2MCU)
    {
        GAgent_Printf(GAGENT_DEBUG_PACKET,
                "Send to MCU Packet, SN:%d, CMD:%d, Len:%d",
                packet[MCU_SN_POS], packet[MCU_CMD_POS], packetLen);
    }
}
Ejemplo n.º 29
0
/****************************************************************
        FunctionName        :   GAgent_LANInit.
        Description         :   init clients socket and create tcp/udp server.
        Add by Will.zhou     --2015-03-10
****************************************************************/
void GAgent_LANInit(pgcontext pgc)
{
    LAN_tcpClientInit(pgc);
    LAN_InitSocket(pgc);  
    GAgent_SetWiFiStatus( pgc,WIFI_MODE_BINDING,1 );  //enable Bind
    pgc->ls.broResourceNum = 0;//enable public broadcast resource
    GAgent_SetWiFiStatus( pgc,WIFI_CLIENT_ON,0 );
    
    GAgent_Printf( GAGENT_DEBUG,"file:%s function:%s line:%d ",__FILE__,__FUNCTION__,__LINE__ );
}
Ejemplo n.º 30
0
/****************************************************************
        Function    :   Cloud_ResConnect
        Description :   handle packet form mqtt req connect 
        buf         :   data form mqtt.
        return      :   0: req connect ok
                        other req connect fail.
        Add by Alex.lin     --2015-03-09
****************************************************************/
uint32 Cloud_ResConnect( uint8* buf )
{
    int32 i=0;
    if( buf[3] == 0x00 )
    {
        if( (buf[0]!=0) && (buf[1] !=0) )
        {
        return 0;
        }
        else
        {
            GAgent_Printf( GAGENT_ERROR,"%s %s %d",__FILE__,__FUNCTION__,__LINE__ );
            GAgent_Printf( GAGENT_ERROR,"MQTT Connect res  fail ret =%d!!",buf[3] );
            return 1;
        }
    }
    GAgent_Printf( GAGENT_ERROR,"res connect fail with %d ",buf[3] );
        return buf[3];
}