Esempio n. 1
0
/*
 * @return: 1 the @cmd is response cmd
 */
int front_panel_check_recv_cmd(struct fp_cmd *recv_cmd)
{
	int cmd = SWAP_U16(recv_cmd->header.seq) & 0x7FFF;
	int is_read = SWAP_U16(recv_cmd->header.seq) & 0x8000;
	bool check_enter_fp = true;

	/*
	 * check force enter to fp management mode
	 */
	if (cmd == FP_CMD_SYS) {
		uint16_t sys_cmd = READ_U16_BE(recv_cmd->data);
		if (sys_cmd == FP_SYS_CMD_READ_TS_STATUS ||
			sys_cmd == FP_SYS_CMD_ENTER_FP_MANAGEMENT_MODE ||
			sys_cmd == FP_SYS_CMD_LEAVE_FP_MANAGEMENT_MODE) {
			check_enter_fp = false;
		}
	} else if (cmd == FP_CMD_OUT_RATE && is_read) {
		check_enter_fp = false;
	}
	if (check_enter_fp && (management_mode != MANAGEMENT_MODE_FP)) {
		trace_warn("recv cmd in none fp mode! force switch!");
		enter_fp_management_mode();
	}

	if (cmd == fp_expect_cmd) {
		static char buf[1024];
		struct fp_cmd *resp_cmd = (struct fp_cmd *)buf;
		fp_cmd_copy(resp_cmd, recv_cmd);
		if (wu_swait_wakeup(&fp_swait, resp_cmd)) {
			trace_err("recv respone cmd %d, but maybe had timeouted!", cmd);
		}
		return 1;
	}

	return 0;
}
Esempio n. 2
0
/*
	构造一个或者一堆的Frame 。
	//服务器端发往客户端的数据 不允许使用 mask
*/
void wsframe_make(automem_t * mem,enum wsFrameType opcode, int _mask, unsigned char * payload, size_t len) {
	struct wsFrameHeader * fh; 
	uint8_t mask[4];
	size_t i;
	srand((unsigned int)time(NULL));
	
	automem_ensure_newspace(mem, len + 20);
	fh = (struct wsFrameHeader * )(&mem->pdata[mem->size]);

	fh->fin = 1;
	fh->opcode = opcode;
	fh->rsv13 = 0;
	fh->mask = 0;
	mem->size += 2;
	if (len < 126) {
		fh->plen = (uint8_t)len;	
	}
	else if (len <= 0xFFFF) {
		uint16_t nlen = SWAP_U16(len);
		fh->plen = 126;
		automem_append_voidp(mem, &nlen, 2);
	}
	else {
		uint64_t nlen = SWAP_U64(len);
		fh->plen = 127;
		automem_append_voidp(mem, &nlen, sizeof(uint64_t));
	}
	if (payload && len > 0){
		if (_mask) {
			fh->mask = 1;
			mask[0] = rand() % 256; mask[1] = rand() % 256; mask[2] = rand() % 256; mask[3] = rand() % 256;
			automem_append_voidp(mem, mask, 4);

			for (i = 0; i < len; i++) {
				mem->pdata[mem->size++] = payload[0] ^ mask[i % 4];
			}
		}
		else {
			automem_append_voidp(mem, payload, len);
		}
	}
}
Esempio n. 3
0
void wsparser_pushdata(wsparser_t * parser, unsigned char * data, uint32_t size, wsparser_onhandler handler, void * eParam)
{
	unsigned char *p = data;
	uint32_t i;
	if (parser->buf.size > 0) {
		automem_append_voidp(&parser->buf, data, size);
		data = parser->buf.pdata;
		size = parser->buf.size;
	}

	while (parser->offset < size) {
		switch (parser->st) {
		case WS_STATE_OPCODE:
			if (size - parser->offset > 2) {
				struct wsFrameHeader * fh = (struct wsFrameHeader *)&data[parser->offset];
				parser->fin = fh->fin;
				parser->mask = fh->mask;
				parser->opcode = fh->opcode==WS_CONTINUATION_FRAME? parser->opcode: fh->opcode;
				parser->offset += 2;
				parser->st = WS_STATE_PLEN; 
				printf("OpCode = %d, Fin=%d\n", parser->opcode,parser->fin);
				switch (fh->plen) {
				case 126:
					parser->st = WS_STATE_PLEN;
					break;
				case 127:
					parser->st = WS_STATE_PLLEN;
					break;
				default:
					parser->len = fh->plen;
					parser->st = parser->mask ? WS_STATE_MASK : WS_STATE_PAYLOAD;
					break;
				}
				if (WS_PING_FRAME == parser->opcode) {
					handler(parser, NULL, eParam);
				}

			}
			break;
		case WS_STATE_PLEN:
			if (size - parser->offset < 2) goto wsparser_pushdata_final;
			parser->len = SWAP_U16(*(uint16_t *)&data[parser->offset]);
			parser->offset += 2;
			parser->st = parser->mask ? WS_STATE_MASK : WS_STATE_PAYLOAD;
			break;
		case WS_STATE_PLLEN:
			if (size - parser->offset < 8) goto wsparser_pushdata_final;
			parser->len = SWAP_U64(*(uint64_t *)&data[parser->offset]);
#if defined(_DEBUG)
			printf("size = %lld", parser->len);
#endif
			parser->offset += 8;
			parser->st = parser->mask ? WS_STATE_MASK : WS_STATE_PAYLOAD;
			break;
		case WS_STATE_MASK:
			if (size - parser->offset < 4) goto wsparser_pushdata_final;
			memcpy(parser->msk_bytes, &data[parser->offset], 4);
			parser->offset += 4;
			parser->st = WS_STATE_PAYLOAD;
			break;
		case WS_STATE_PAYLOAD:
			if (size - parser->offset < parser->len) goto wsparser_pushdata_final;
			//处理帧数据
			if (parser->mask) {
				automem_t * m = &parser->payload;
				automem_ensure_newspace(m, parser->len);
				for (i = 0; i < parser->len; i++) {
					m->pdata[m->size++] = data[parser->offset+i] ^ parser->msk_bytes[i % 4];
				}
			}
			if (parser->fin == 1) {
				wsFrame * f = malloc(sizeof(wsFrame));
				f->len = parser->payload.size;
				f->opcode = parser->opcode;
				f->pdata = f->len > 0 ? parser->payload.pdata : NULL;

				handler(parser, f, eParam);
				if (f->len){
					parser->payload.pdata = NULL;
					parser->payload.size = parser->payload.buffersize = 0;
					automem_init(&parser->payload, 2048);
				}
			}

			parser->offset += (uint32_t)parser->len;
			parser->st = WS_STATE_OPCODE;
			break;
		}
	}
wsparser_pushdata_final:
	
	if (size > parser->offset) {
		if (data != parser->buf.pdata) {
			automem_append_voidp(&parser->buf, data + parser->offset, size - parser->offset);//将未用完的加入到 buf.
			parser->offset = 0;
		}
	}
	else if (size <= parser->offset) {
		if (data == parser->buf.pdata) {

		}
		if (parser->buf.buffersize > parser->maxsize)
			automem_clean(&parser->buf, parser->maxsize);
		else
			automem_reset(&parser->buf);
		parser->offset = 0;
	}

	return;
}