int net_send(const void * buf, int len) { void * pkt; pkt = pktbuf_alloc(); if (pkt == NULL) { DCC_LOG(LOG_ERROR, "pktbuf_alloc() failed!"); return -1; } DCC_LOG2(LOG_TRACE, "pkt=%p len=%d", pkt, len); len = MIN(len, pktbuf_len); memcpy(pkt, buf, len); pkt = rs485_pkt_enqueue(&net.link, pkt, len); net.stat.tx.pkt_cnt++; net.stat.tx.octet_cnt += len; if (pkt != NULL) pktbuf_free(pkt); return 0; }
int net_probe(void) { void * pkt; int ret; /* drain the transmmit queue */ pkt = rs485_pkt_drain(&net.link); if (pkt != NULL) pktbuf_free(pkt); /* set the probe pin low */ net.probe_mode = true; stm32_gpio_clr(RS485_MODE); DCC_LOG(LOG_TRACE, "Probe mode."); thinkos_flag_clr(net.probe_flag); if ((pkt = pktbuf_alloc()) != NULL) { uint32_t seq; while ((seq = rand()) == 0); /* send a probe packet */ sprintf((char *)pkt, "PRB=%08x", seq); DCC_LOG1(LOG_TRACE, "seq=0x%08x", seq); rs485_pkt_enqueue(&net.link, pkt, 12); net.stat.tx.pkt_cnt++; net.stat.tx.octet_cnt += 12; /* wait for the end of transmission */ pkt = rs485_pkt_drain(&net.link); pktbuf_free(pkt); if ((ret = thinkos_flag_timedwait(net.probe_flag, 10)) == 0) { if (seq != net.probe_seq) { DCC_LOG(LOG_WARNING, "probe sequence mismatch!"); ret = -1; } } else if (ret == THINKOS_ETIMEDOUT) { DCC_LOG(LOG_WARNING, "probe sequence timedout!"); } DCC_LOG1(LOG_TRACE, "seq=0x%08x", seq); } else { DCC_LOG(LOG_ERROR, "pktbuf_alloc() failed!"); ret = -1; } /* set the probe pin high */ net.probe_mode = false; stm32_gpio_set(RS485_MODE); DCC_LOG(LOG_TRACE, "Probe mode."); return ret; }
int net_pkt_send(const void * buf, int len) { struct net_pkt * pkt; unsigned int data_len; uint8_t * dst; uint8_t * src; int i; pkt = (struct net_pkt *)pktbuf_alloc(); if (pkt == NULL) { DCC_LOG(LOG_ERROR, "pktbuf_alloc() failed!"); DBG("pktbuf_alloc() failed!\n"); net.stat.tx.err_cnt++; return -1; } data_len = MIN(len, pktbuf_len - sizeof(struct net_pkt)); pkt->crc = 0; pkt->data_len = data_len; pkt->seq = net.tx_seq++; dst = (uint8_t *)pkt->data; src = (uint8_t *)buf; for (i = 0; i < data_len; ++i) dst[i] = src[i]; pkt->crc = crc16ccitt(0, pkt, data_len + sizeof(struct net_pkt)); pkt = rs485_pkt_enqueue(&net.link, pkt, data_len + sizeof(struct net_pkt)); net.stat.tx.pkt_cnt++; net.stat.tx.octet_cnt += data_len + sizeof(struct net_pkt); if (pkt != NULL) pktbuf_free(pkt); return 0; }