Exemple #1
0
void recv_data_process(char * buffer, int len)
{
    int rtn;
    uint8 mtype;
    EdpPacket* pkg;

    //LOUIS add
	uint8 jsonorbin;
    char* src_devid;
    cJSON* save_json;
    char* save_json_str;

    cJSON* desc_json;
    char* desc_json_str;
    char* save_bin; 
    uint32 save_binlen;
    char* json_ack;


    char* push_data;
    uint32 push_datalen;

    char* cmdid;
    uint16 cmdid_len;
    char*  cmd_req;
    uint32 cmd_req_len;
    EdpPacket* send_pkg;
//    char* ds_id;
//    double dValue = 0;

    char* simple_str = NULL;
    char cmd_resp[] = "ok";
    unsigned cmd_resp_len = 0;

    RecvBuffer recvbuf;

    recvbuf._data = (uint8*)buffer;
    recvbuf._write_pos = len;
    recvbuf._read_pos = 0;
    recvbuf._capacity = BUFFER_SIZE;

	log_info("Recv data(len:%d).\n", len);

    while (1)
    {
        /* 获取一个完成的EDP包 */
        if ((pkg = GetEdpPacket(&recvbuf)) == 0)
        {
            log_info("need more bytes...\n");
            break;
        }

        /* 获取这个EDP包的消息类型,根据这个EDP包的消息类型, 分别做EDP包解析 */
        mtype = EdpPacketType(pkg);
        switch(mtype)
        {
            case CONNRESP:
                /* 解析EDP包 - 连接响应 */
                rtn = UnpackConnectResp(pkg);
                log_info("Receive connect response, rtn: %d\n", rtn);
			
				/* 启动Sensor Task任务 */
				SensorTaskFlag = 1;
				cloud_conn_status.conn_status = CLOUD_CONN_DONE;
				
                break;

            case PINGRESP:
                /* 解析EDP包 - 心跳响应 */
                UnpackPingResp(pkg); 
                log_info("recv ping resp\n");
                break;

            case PUSHDATA:
                /* 解析EDP包 - 数据转发 */
                UnpackPushdata(pkg, &src_devid, &push_data, &push_datalen);
                log_info("recv push data, src_devid: %s, push_data: %s, len: %d\n", 
                        src_devid, push_data, push_datalen);
                free(src_devid);
                free(push_data);
                break;

            case SAVEDATA:
                /* 解析EDP包 - 数据存储 */
                 if (UnpackSavedata(pkg, &src_devid, &jsonorbin) == 0)
                 {
                      if (jsonorbin == kTypeFullJson 
						    || jsonorbin == kTypeSimpleJsonWithoutTime
						    || jsonorbin == kTypeSimpleJsonWithTime) 
						    {
							     printf("json type is %d\n", jsonorbin);
					             /* 解析EDP包 - json数据存储 */ 
	                             UnpackSavedataJson(pkg, &save_json); 
	                             save_json_str = cJSON_Print(save_json);
	                             log_info("recv save data json, src_devid: %s, json: %s\n", 
	                                 src_devid, save_json_str); 
	                             free(save_json_str); 
	                             cJSON_Delete(save_json); 

								/* UnpackSavedataInt(jsonorbin, pkg, &ds_id, &iValue); */
								/* log_info("ds_id = %s\nvalue= %d\n", ds_id, iValue); */
				
								//UnpackSavedataDouble((SaveDataType)jsonorbin, pkg, &ds_id, &dValue);
								//log_info("ds_id = %s\nvalue = %f\n", ds_id, dValue);
				
								/* UnpackSavedataString(jsonorbin, pkg, &ds_id, &cValue); */
								/* log_info("ds_id = %s\nvalue = %s\n", ds_id, cValue); */
								/* free(cValue); */

								//free(ds_id);
                        }
		                else if (jsonorbin == kTypeBin)
		                {/* 解析EDP包 - bin数据存储 */
		                    UnpackSavedataBin(pkg, &desc_json, (uint8**)&save_bin, &save_binlen);
		                    desc_json_str=cJSON_Print(desc_json);
		                    log_info("recv save data bin, src_devid: %s, desc json: %s, bin: %s, binlen: %d\n", 
		                            src_devid, desc_json_str, save_bin, save_binlen);
		                    free(desc_json_str);
		                    cJSON_Delete(desc_json);
		                    free(save_bin);
		                }
		             	else if (jsonorbin == kTypeString){
						    UnpackSavedataSimpleString(pkg, &simple_str);
						    
						    log_info("%s\n", simple_str);
						    free(simple_str);
						}
		                free(src_devid);
	            	}else{
						printf("error\n");
					}

                    break;

            case SAVEACK:
				json_ack = NULL;
				UnpackSavedataAck(pkg, &json_ack);
				log_info("save json ack = %s\n", json_ack);
				free(json_ack);
				break;

            case CMDREQ:
			    log_info("recevice server cmd:\r\n");
				dump_hex(pkg->_data, pkg->_write_pos);
			 	if (UnpackCmdReq(pkg, &cmdid, &cmdid_len, 
							 &cmd_req, &cmd_req_len) == 0){
					    /* 
					     * 用户按照自己的需求处理并返回,响应消息体可以为空,此处假设返回2个字符"ok"。
					     * 处理完后需要释放
					     */
					    cmd_resp_len = strlen(cmd_resp);
					    send_pkg = PacketCmdResp(cmdid, cmdid_len,
								     cmd_resp, cmd_resp_len);
			#ifdef _ENCRYPT
					    if (g_is_encrypt){
						SymmEncrypt(send_pkg);
					    }
			#endif
					    Socket_TCPClientSendData((char*)send_pkg->_data, send_pkg->_write_pos);
					    DeleteBuffer(&send_pkg);
					    
					    free(cmdid);
					    free(cmd_req);
					}
					break;

            default:
                /* 未知消息类型 */
                log_info("recv failed...\n");
                break;
        }
        DeleteBuffer(&pkg);
    }
}
/*
 *  @brief  串口接收处理线程
 */
void Recv_Thread_Func(void)
{
    int32_t error = 0;
    int32_t rtn;
    uint8_t mtype, jsonorbin;
    uint8_t buffer[128];
    RecvBuffer *recv_buf = NewBuffer();
    EdpPacket *pkg;

    int8_t *src_devid;
    int8_t *push_data;
    uint32_t push_datalen;

    cJSON *desc_json;
    int8_t *desc_json_str;
    int8_t *save_bin;
    uint32_t save_binlen;
    int8_t *json_ack;

    int8_t *cmdid;
    uint16_t cmdid_len;
    int8_t *cmd_req;
    uint32_t cmd_req_len;
    EdpPacket *send_pkg;
    int8_t *ds_id;
    double dValue = 0;

    int8_t *simple_str = NULL;
    int8_t cmd_resp[] = "ok";
    uint32_t cmd_resp_len = 0;

    printf("\n[%s] recv thread start ...\r\n", __func__);

    while (error == 0)
    {
        /* 试着接收1024个字节的数据 */
        int32_t rcv_len = 0;

        rcv_len = USART2_GetRcvNum();
        if (rcv_len <= 0)
        {
            printf("%s %d No Data\n", __func__, __LINE__);
            break;
        }
				mDelay(50);
				rcv_len = USART2_GetRcvNum();
        USART2_GetRcvData(buffer, rcv_len);
        printf("recv from server, bytes: %d\r\n", rcv_len);
        /* wululu test print send bytes */
        hexdump((const uint8_t *)buffer, rcv_len);
        printf("\n");
        /* 成功接收了n个字节的数据 */
        WriteBytes(recv_buf, buffer, rcv_len);
        while (1)
        {
            /* 获取一个完成的EDP包 */
            if ((pkg = GetEdpPacket(recv_buf)) == 0)
            {
                printf("need more bytes...\n");
                break;
            }
            /* 获取这个EDP包的消息类型 */
            mtype = EdpPacketType(pkg);
            printf("mtype=%d\n", mtype);
            /* 根据这个EDP包的消息类型, 分别做EDP包解析 */
            switch (mtype)
            {
                case CONNRESP:
                    /* 解析EDP包 - 连接响应 */
                    rtn = UnpackConnectResp(pkg);
                    printf("recv connect resp, rtn: %d\n", rtn);
                    break;
                case PUSHDATA:
                    /* 解析EDP包 - 数据转发 */
                    UnpackPushdata(pkg, &src_devid, &push_data,
                                   &push_datalen);
                    printf
                    ("recv push data, src_devid: %s, push_data: %s, len: %d\n",
                     src_devid, push_data, push_datalen);
                    free(src_devid);
                    free(push_data);
                    break;
                case SAVEDATA:
                    /* 解析EDP包 - 数据存储 */
                    if (UnpackSavedata(pkg, &src_devid, &jsonorbin)
                        == 0)
                    {
                        if (jsonorbin == kTypeFullJson
                            || jsonorbin ==
                            kTypeSimpleJsonWithoutTime
                            || jsonorbin ==
                            kTypeSimpleJsonWithTime)
                        {
                            printf("json type is %d\n",
                                   jsonorbin);
                            /* 解析EDP包 - json数据存储 */
                            /* UnpackSavedataJson(pkg, &save_json); */
                            /* save_json_str=cJSON_Print(save_json); */
                            /* printf("recv save data json, src_devid: %s, json: %s\n", */
                            /*     src_devid, save_json_str); */
                            /* free(save_json_str); */
                            /* cJSON_Delete(save_json); */

                            /* UnpackSavedataInt(jsonorbin, pkg, &ds_id, &iValue); */
                            /* printf("ds_id = %s\nvalue= %d\n", ds_id, iValue); */

                            UnpackSavedataDouble(jsonorbin,
                                                 pkg,
                                                 &ds_id,
                                                 &dValue);
                            printf
                            ("ds_id = %s\nvalue = %f\n",
                             ds_id, dValue);

                            /* UnpackSavedataString(jsonorbin, pkg, &ds_id, &cValue); */
                            /* printf("ds_id = %s\nvalue = %s\n", ds_id, cValue); */
                            /* free(cValue); */
                            free(ds_id);
                        }
                        else if (jsonorbin == kTypeBin)     /* 解析EDP包 - bin数据存储 */
                        {
                            UnpackSavedataBin(pkg,
                                              &desc_json,
                                              (uint8_t **) &
                                              save_bin,
                                              &save_binlen);
                            desc_json_str =
                                cJSON_Print(desc_json);
                            printf
                            ("recv save data bin, src_devid: %s, desc json: %s, bin: %s, binlen: %d\n",
                             src_devid, desc_json_str,
                             save_bin, save_binlen);
                            free(desc_json_str);
                            cJSON_Delete(desc_json);
                            free(save_bin);
                        }
                        else if (jsonorbin == kTypeString)
                        {
                            UnpackSavedataSimpleString(pkg,
                                                       &simple_str);
                            printf("%s\n", simple_str);
                            free(simple_str);
                        }
                        free(src_devid);
                    }
                    else
                    {
                        printf("error\n");
                    }
                    break;
                case SAVEACK:
                    json_ack = NULL;
                    UnpackSavedataAck(pkg, &json_ack);
                    printf("save json ack = %s\n", json_ack);
                    free(json_ack);
                    break;
                case CMDREQ:
                    if (UnpackCmdReq
                        (pkg, &cmdid, &cmdid_len, &cmd_req,
                         &cmd_req_len) == 0)
                    {
                        /*
                         * 用户按照自己的需求处理并返回,响应消息体可以为空,此处假设返回2个字符"ok"。
                         * 处理完后需要释放
                         */
                        cmd_resp_len = strlen(cmd_resp);
                        send_pkg =
                            PacketCmdResp(cmdid, cmdid_len,
                                          cmd_resp,
                                          cmd_resp_len);
#ifdef _ENCRYPT
                        if (g_is_encrypt)
                        {
                            SymmEncrypt(send_pkg);
                        }
#endif
                        DoSend(0, (const uint8_t *)
                               send_pkg->_data,
                               send_pkg->_write_pos);
                        DeleteBuffer(&send_pkg);

                        free(cmdid);
                        free(cmd_req);
                    }
                    break;
                case PINGRESP:
                    /* 解析EDP包 - 心跳响应 */
                    UnpackPingResp(pkg);
                    printf("recv ping resp\n");
                    break;
                default:
                    /* 未知消息类型 */
                    error = 1;
                    printf("recv failed...\n");
                    break;
            }
            DeleteBuffer(&pkg);
        }
    }
    DeleteBuffer(&recv_buf);

#ifdef _DEBUG
    printf("[%s] recv thread end ...\n", __func__);
#endif
}