Lines Matching refs:ctx

99     struct rym_ctx *ctx,  in _rym_read_code()  argument
103 if (rt_device_read(ctx->dev, 0, ctx->buf, 1) == 1) in _rym_read_code()
104 return (enum rym_code)(*ctx->buf); in _rym_read_code()
112 if (rt_sem_take(&ctx->sem, timeout) != RT_EOK) in _rym_read_code()
116 rsz = rt_device_read(ctx->dev, 0, ctx->buf, 1); in _rym_read_code()
118 return (enum rym_code)(*ctx->buf); in _rym_read_code()
125 struct rym_ctx *ctx, in _rym_read_data() argument
129 rt_uint8_t *buf = ctx->buf + 1; in _rym_read_data()
134 readlen += rt_device_read(ctx->dev, in _rym_read_data()
139 while (rt_sem_take(&ctx->sem, RYM_WAIT_CHR_TICK) == RT_EOK); in _rym_read_data()
145 struct rym_ctx *ctx, in _rym_send_packet() argument
166 send_crc = CRC16(ctx->buf + 3, packetlen - 5); in _rym_send_packet()
167 ctx->buf[0] = code; in _rym_send_packet()
168 ctx->buf[1] = index; in _rym_send_packet()
169 ctx->buf[2] = index_inv; in _rym_send_packet()
170 ctx->buf[packetlen - 2] = (rt_uint8_t)(send_crc >> 8); in _rym_send_packet()
171 ctx->buf[packetlen - 1] = (rt_uint8_t)send_crc & 0xff; in _rym_send_packet()
175 writelen += rt_device_write(ctx->dev, 0, ctx->buf + writelen, in _rym_send_packet()
183 static rt_ssize_t _rym_putchar(struct rym_ctx *ctx, rt_uint8_t code) in _rym_putchar() argument
185 rt_device_write(ctx->dev, 0, &code, sizeof(code)); in _rym_putchar()
189 static rt_ssize_t _rym_getchar(struct rym_ctx *ctx) in _rym_getchar() argument
193 while (rt_device_read(ctx->dev, 0, &getc_ack, 1) != 1) in _rym_getchar()
195 rt_sem_take(&ctx->sem, RT_WAITING_FOREVER); in _rym_getchar()
201 struct rym_ctx *ctx, in _rym_do_handshake() argument
210 ctx->stage = RYM_STAGE_ESTABLISHING; in _rym_do_handshake()
214 _rym_putchar(ctx, RYM_CODE_C); in _rym_do_handshake()
215 code = _rym_read_code(ctx, in _rym_do_handshake()
239 i += _rym_read_data(ctx, data_sz - 1); in _rym_do_handshake()
247 if (ctx->buf[1] != 0 || ctx->buf[2] != 0xFF) in _rym_do_handshake()
250 recv_crc = (rt_uint16_t)(*(ctx->buf + data_sz - 2) << 8) | *(ctx->buf + data_sz - 1); in _rym_do_handshake()
251 cal_crc = CRC16(ctx->buf + 3, data_sz - 5); in _rym_do_handshake()
256 if (ctx->on_begin && ctx->on_begin(ctx, ctx->buf + 3, data_sz - 5) != RYM_CODE_ACK) in _rym_do_handshake()
263 struct rym_ctx *ctx, in _rym_do_send_handshake() argument
272 ctx->stage = RYM_STAGE_ESTABLISHING; in _rym_do_send_handshake()
278 code = _rym_read_code(ctx, in _rym_do_send_handshake()
291 if (ctx->on_begin && ctx->on_begin(ctx, ctx->buf + 3, data_sz - 5) != RYM_CODE_SOH) in _rym_do_send_handshake()
295 _rym_send_packet(ctx, code, index); in _rym_do_send_handshake()
297 rt_device_set_rx_indicate(ctx->dev, _rym_rx_ind); in _rym_do_send_handshake()
298 getc_ack = _rym_getchar(ctx); in _rym_do_send_handshake()
305 getc_ack = _rym_getchar(ctx); in _rym_do_send_handshake()
312 ctx->stage = RYM_STAGE_ESTABLISHED; in _rym_do_send_handshake()
318 struct rym_ctx *ctx, in _rym_trans_data() argument
326 rt_size_t i = _rym_read_data(ctx, tsz); in _rym_trans_data()
330 if ((ctx->buf[1] + ctx->buf[2]) != 0xFF) in _rym_trans_data()
339 if (ctx->stage == RYM_STAGE_ESTABLISHED && ctx->buf[1] == 0x00) in _rym_trans_data()
345 ctx->stage = RYM_STAGE_TRANSMITTING; in _rym_trans_data()
348 recv_crc = (rt_uint16_t)(*(ctx->buf + tsz - 1) << 8) | *(ctx->buf + tsz); in _rym_trans_data()
349 if (recv_crc != CRC16(ctx->buf + 3, data_sz)) in _rym_trans_data()
353 if (ctx->on_data) in _rym_trans_data()
354 *code = ctx->on_data(ctx, ctx->buf + 3, data_sz); in _rym_trans_data()
360 static rt_err_t _rym_do_trans(struct rym_ctx *ctx) in _rym_do_trans() argument
362 _rym_putchar(ctx, RYM_CODE_ACK); in _rym_do_trans()
363 _rym_putchar(ctx, RYM_CODE_C); in _rym_do_trans()
364 ctx->stage = RYM_STAGE_ESTABLISHED; in _rym_do_trans()
373 code = _rym_read_code(ctx, in _rym_do_trans()
393 _rym_putchar(ctx, RYM_CODE_NAK);/* Ask for a packet */ in _rym_do_trans()
398 err = _rym_trans_data(ctx, data_sz, &code); in _rym_do_trans()
408 _rym_putchar(ctx, RYM_CODE_NAK);/* Ask for a packet */ in _rym_do_trans()
423 _rym_putchar(ctx, RYM_CODE_CAN); in _rym_do_trans()
427 _rym_putchar(ctx, RYM_CODE_ACK); in _rym_do_trans()
436 static rt_err_t _rym_do_send_trans(struct rym_ctx *ctx) in _rym_do_send_trans() argument
438 ctx->stage = RYM_STAGE_TRANSMITTING; in _rym_do_send_trans()
447 if (!ctx->on_data) in _rym_do_send_trans()
451 code = ctx->on_data(ctx, ctx->buf + 3, data_sz - 5); in _rym_do_send_trans()
453 _rym_send_packet(ctx, code, index); in _rym_do_send_trans()
455 rt_device_set_rx_indicate(ctx->dev, _rym_rx_ind); in _rym_do_send_trans()
457 getc_ack = _rym_getchar(ctx); in _rym_do_send_trans()
463 if (ctx->stage == RYM_STAGE_FINISHING) in _rym_do_send_trans()
470 static rt_err_t _rym_do_fin(struct rym_ctx *ctx) in _rym_do_fin() argument
477 ctx->stage = RYM_STAGE_FINISHING; in _rym_do_fin()
480 if (ctx->on_end) in _rym_do_fin()
481 ctx->on_end(ctx, ctx->buf + 3, 128); in _rym_do_fin()
483 _rym_putchar(ctx, RYM_CODE_NAK); in _rym_do_fin()
484 code = _rym_read_code(ctx, RYM_WAIT_PKG_TICK); in _rym_do_fin()
488 _rym_putchar(ctx, RYM_CODE_ACK); in _rym_do_fin()
489 _rym_putchar(ctx, RYM_CODE_C); in _rym_do_fin()
491 code = _rym_read_code(ctx, RYM_WAIT_PKG_TICK); in _rym_do_fin()
503 i = _rym_read_data(ctx, data_sz - 1); in _rym_do_fin()
509 if (ctx->buf[1] != 0 || ctx->buf[2] != 0xFF) in _rym_do_fin()
512 recv_crc = (rt_uint16_t)(*(ctx->buf + data_sz - 2) << 8) | *(ctx->buf + data_sz - 1); in _rym_do_fin()
513 if (recv_crc != CRC16(ctx->buf + 3, data_sz - 5)) in _rym_do_fin()
517 if (ctx->buf[3] != 0) in _rym_do_fin()
519 if (ctx->on_begin && ctx->on_begin(ctx, ctx->buf + 3, data_sz - 5) != RYM_CODE_ACK) in _rym_do_fin()
525 ctx->stage = RYM_STAGE_FINISHED; in _rym_do_fin()
528 _rym_putchar(ctx, RYM_CODE_ACK); in _rym_do_fin()
533 static rt_err_t _rym_do_send_fin(struct rym_ctx *ctx) in _rym_do_send_fin() argument
541 rt_device_set_rx_indicate(ctx->dev, _rym_rx_ind); in _rym_do_send_fin()
543 _rym_putchar(ctx, RYM_CODE_EOT); in _rym_do_send_fin()
544 getc_ack = _rym_getchar(ctx); in _rym_do_send_fin()
551 _rym_putchar(ctx, RYM_CODE_EOT); in _rym_do_send_fin()
552 getc_ack = _rym_getchar(ctx); in _rym_do_send_fin()
559 getc_ack = _rym_getchar(ctx); in _rym_do_send_fin()
566 if (ctx->on_end && ctx->on_end(ctx, ctx->buf + 3, data_sz - 5) != RYM_CODE_SOH) in _rym_do_send_fin()
571 _rym_send_packet(ctx, code, index); in _rym_do_send_fin()
573 ctx->stage = RYM_STAGE_FINISHED; in _rym_do_send_fin()
579 struct rym_ctx *ctx, in _rym_do_recv() argument
584 ctx->stage = RYM_STAGE_NONE; in _rym_do_recv()
586 ctx->buf = rt_malloc(_RYM_STX_PKG_SZ); in _rym_do_recv()
587 if (ctx->buf == RT_NULL) in _rym_do_recv()
590 err = _rym_do_handshake(ctx, handshake_timeout); in _rym_do_recv()
593 rt_free(ctx->buf); in _rym_do_recv()
598 err = _rym_do_trans(ctx); in _rym_do_recv()
600 err = _rym_do_fin(ctx); in _rym_do_recv()
603 rt_free(ctx->buf); in _rym_do_recv()
607 if (ctx->stage == RYM_STAGE_FINISHED) in _rym_do_recv()
610 rt_free(ctx->buf); in _rym_do_recv()
615 struct rym_ctx *ctx, in _rym_do_send() argument
620 ctx->stage = RYM_STAGE_NONE; in _rym_do_send()
622 ctx->buf = rt_malloc(_RYM_STX_PKG_SZ); in _rym_do_send()
623 if (ctx->buf == RT_NULL) in _rym_do_send()
626 err = _rym_do_send_handshake(ctx, handshake_timeout); in _rym_do_send()
629 rt_free(ctx->buf); in _rym_do_send()
633 err = _rym_do_send_trans(ctx); in _rym_do_send()
636 rt_free(ctx->buf); in _rym_do_send()
640 err = _rym_do_send_fin(ctx); in _rym_do_send()
643 rt_free(ctx->buf); in _rym_do_send()
647 rt_free(ctx->buf); in _rym_do_send()
652 struct rym_ctx *ctx, in rym_recv_on_device() argument
666 _rym_the_ctx = ctx; in rym_recv_on_device()
668 ctx->on_begin = on_begin; in rym_recv_on_device()
669 ctx->on_data = on_data; in rym_recv_on_device()
670 ctx->on_end = on_end; in rym_recv_on_device()
671 ctx->dev = dev; in rym_recv_on_device()
672 rt_sem_init(&ctx->sem, "rymsem", 0, RT_IPC_FLAG_FIFO); in rym_recv_on_device()
689 res = _rym_do_recv(ctx, handshake_timeout); in rym_recv_on_device()
697 rt_sem_detach(&ctx->sem); in rym_recv_on_device()
709 struct rym_ctx *ctx, in rym_send_on_device() argument
723 _rym_the_ctx = ctx; in rym_send_on_device()
725 ctx->on_begin = on_begin; in rym_send_on_device()
726 ctx->on_data = on_data; in rym_send_on_device()
727 ctx->on_end = on_end; in rym_send_on_device()
728 ctx->dev = dev; in rym_send_on_device()
729 rt_sem_init(&ctx->sem, "rymsem", 0, RT_IPC_FLAG_FIFO); in rym_send_on_device()
746 res = _rym_do_send(ctx, handshake_timeout); in rym_send_on_device()
753 rt_sem_detach(&ctx->sem); in rym_send_on_device()