xmos_radio.c
来自「MANTIS是由科罗拉多大学开发的传感器网络嵌入式操作系统。 这是mantis」· C语言 代码 · 共 114 行
C
114 行
// This file is part of MANTIS OS, Operating System// See http://mantis.cs.colorado.edu///// Copyright (C) 2003,2004,2005 University of Colorado, Boulder//// This program is free software; you can redistribute it and/or// modify it under the terms of the mos license (see file LICENSE)/* Project Mantis File: xmos_radio.c Authors: Jeff Rose, Lane Phillips Date: 2004-08-04 Simulate the radio by sending packets to a simulation server.*/#include "mos.h"#ifdef PLATFORM_LINUX#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include "node_id.h"#include "msched.h"#include "xmos_radio.h"#include "gevent.h"#if defined(XMOS_RADIO) || !defined(SCONS)//#define HEXDUMP(BUF,SIZE) {int hdi; for (hdi=0; hdi<SIZE; hdi++) printf("%02hhx ", BUF[hdi]); printf("\n");}static uint8_t current_mode;static void listenTo (void);/* Initialize the XMOS radio. Called by preStart() in src/mos/sys/main.c */void xmos_radio_init (void){ current_mode = IF_OFF; // Don't even bother launching the listen thread if we are not // connected to the simulation server. if (!gevent_connected) return; // Create a thread to constantly listen on the socket and // save packets. mos_thread_new(listenTo, 128, PRIORITY_NORMAL); return;}/* Send the packet to the server as a simulator event. */uint8_t com_send_IFACE_RADIO (comBuf *buf){ return (uint8_t)gevent_send(RADIO_PACKET_EVENT, "ix", "Send packet: node %i, %i bytes.\n", mos_node_id_get(), buf->size, buf->data);}void com_mode_IFACE_RADIO (uint8_t mode){ current_mode = mode;}void com_ioctl_IFACE_RADIO (uint8_t request, ...){}/** @brief See complete description. * * This function is meant to be run as a separate thread that is * constantly reading bytes off a udp socket. In order to make * the behavior the same in xmos and amos I am dropping packets * unless the user has explicitly turned on the radio device. */static void listenTo (void){ static char* pkt[COM_DATA_SIZE]; comBuf *buf = NULL; int size; uint32_t id; while(gevent_connected) { /* Receive a radio packet as a simulation event. */ size = gevent_recv(RADIO_PACKET_EVENT, "ix", &id, pkt); if (size < 0) { fprintf(stderr, "gevent_recv returned %i.\n", size); return; } /* Drop packets if the radio is not listening. */ if (current_mode != IF_LISTEN) continue; if (!buf) { com_swap_bufs(IFACE_RADIO, NULL, &buf); /* Drop packets if we can't get a buffer. */ if (!buf) continue; } buf->size = size - 4; memcpy(buf->data, pkt, buf->size); /* Make the buffer available for com_recv(). */ com_swap_bufs(IFACE_RADIO, buf, &buf); }}#endif#endif
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?