mvirusextended.nc
来自「tinyos最新版」· NC 代码 · 共 545 行 · 第 1/2 页
NC
545 行
/* tab:4 * * * "Copyright (c) 2000-2004 The Regents of the University of California. * All rights reserved. * * Permission to use, copy, modify, and distribute this software and * its documentation for any purpose, without fee, and without written * agreement is hereby granted, provided that the above copyright * notice, the following two paragraphs and the author appear in all * copies of this software. * * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS * DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, * UPDATES, ENHANCEMENTS, OR MODIFICATIONS." * *//* tab:4 * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By * downloading, copying, installing or using the software you agree to * this license. If you do not agree to this license, do not download, * install, copy or use the software. * * Intel Open Source License * * Copyright (c) 2004 Intel Corporation * All rights reserved. * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of the Intel Corporation nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR ITS * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * *//* * Authors: Philip Levis * History: March 10, 2004 Extended version (includes routing). * * *//** * @author Philip Levis */includes Mate;module MVirusExtended { provides interface StdControl; provides interface MateVirus as Virus; uses { interface MateError; interface Timer as VersionTimer; interface ReceiveMsg as VersionReceive; interface SendMsg as VersionSend; interface Timer as CapsuleTimer; interface ReceiveMsg as CapsuleReceive; interface SendMsg as CapsuleSend; interface Random as Random; interface StdControl as SubControl; interface Receive as ReceiveRouted; interface Intercept as InterceptRouted; interface Timer as BCastTimer; interface ReceiveMsg as BCastReceive; interface SendMsg as BCastSend; }}implementation { typedef enum { BVIRUS_TIMER_VERSION = 100, BVIRUS_TIMER_CAPSULE = 1000, BVIRUS_CAPSULE_INIT = 1, BVIRUS_CAPSULE_MAX = 16, BVIRUS_VERSION_THRESHOLD_INIT = 1, BVIRUS_VERSION_THRESHOLD_MAX = 300, TAU_INIT = 10, TAU_MAX = 600, BVIRUS_VERSION_HEARD_THRESHOLD = 1 } MVirusConstants; typedef enum { BVIRUS_IDLE, BVIRUS_PULLING, BVIRUS_PUSHING } MVirusState; MateCapsule* capsules[MATE_CAPSULE_NUM]; uint8_t capsuleTimerThresholds[MATE_CAPSULE_NUM]; uint8_t capsuleHeard[MATE_CAPSULE_NUM]; uint8_t capsuleTimerCounters[MATE_CAPSULE_NUM]; uint8_t bCastIdx; uint16_t versionCounter; uint16_t versionThreshold; uint16_t versionCancelled; uint16_t tau; uint16_t versionHeard; MVirusState state; bool sendBusy; bool capsuleBusy; bool amBCasting; TOS_Msg sendMessage; TOS_MsgPtr sendPtr; TOS_Msg receiveMsg; TOS_MsgPtr receivePtr; void cancelVersionCounter() { versionCancelled = 1; } void newVersionCounter() { versionThreshold = (tau/2) + (((uint16_t)call Random.rand()) % (tau/2)); //printf("%i: MVirus: resetting t to %i (%i)\n", (int)TOS_LOCAL_ADDRESS, (int)versionThreshold, (int)tau); versionHeard = 0; versionCounter = 0; versionCancelled = 0; } uint8_t typeToIndex(uint8_t type) { type &= MATE_OPTION_MASK; if (type <= MATE_CAPSULE_NUM) { return type; } else { return MATE_CAPSULE_INVALID; } } command result_t StdControl.init() { int i; call SubControl.init(); call Random.init(); for (i = 0; i < MATE_CAPSULE_NUM; i++) { capsuleTimerCounters[i] = 0; capsuleTimerThresholds[i] = BVIRUS_CAPSULE_MAX + 1; capsules[i] = NULL; } state = BVIRUS_IDLE; tau = TAU_MAX; sendPtr = (TOS_MsgPtr)&sendMessage; receivePtr = (TOS_MsgPtr)&receiveMsg; newVersionCounter(); dbg(DBG_USR3, "MVirus initialized.\n"); sendBusy = FALSE; capsuleBusy = FALSE; return SUCCESS; } command result_t StdControl.start() { call SubControl.start(); state = BVIRUS_PULLING; dbg(DBG_USR3, "MVirus started.\n"); call VersionTimer.start(TIMER_REPEAT, BVIRUS_TIMER_VERSION); dbg(DBG_USR3, "MVirus version timer started.\n"); return SUCCESS; } command result_t StdControl.stop() { call VersionTimer.stop(); call CapsuleTimer.stop(); dbg(DBG_USR3, "MVirus stopped.\n"); return call SubControl.stop(); } command result_t Virus.registerCapsule(uint8_t type, MateCapsule* capsule) { uint8_t idx = typeToIndex(type); if (idx >= MATE_CAPSULE_NUM) { return FAIL; } capsules[idx] = capsule; return SUCCESS; } result_t receiveCapsule(MateCapsule* capsule, uint16_t payloadLen, char* type, int backoff) { uint8_t idx = typeToIndex(capsule->type); if ((capsules[idx] != NULL) && (capsule->version > capsules[idx]->version)) { int len = (payloadLen < sizeof(MateCapsule))? payloadLen : sizeof(MateCapsule);#ifdef PLATFORM_PC { char timeVal[128]; printTime(timeVal, 128); dbg(DBG_USR3|DBG_TEMP,"MVirus: Received and installing %s capsule %i, version %i @ %s\n", type, (int)capsule->type, (int)capsule->version, timeVal); }#endif if (capsule->type & MATE_OPTION_FORCE) { signal Virus.capsuleForce(capsule->type & MATE_OPTION_MASK); nmemcpy(capsules[idx], capsule, len); signal Virus.capsuleInstalled(capsules[idx]); dbg(DBG_USR3, "MVirus: installed a routed forced capsule: %i\n", (int)idx); tau = TAU_INIT * backoff; newVersionCounter(); tau = TAU_INIT; return SUCCESS; } else if (signal Virus.capsuleHeard(capsule->type) == SUCCESS) { nmemcpy(capsules[idx], capsule, len); signal Virus.capsuleInstalled(capsules[idx]); dbg(DBG_USR3, "MVirus: installed a routed capsule\n"); tau = TAU_INIT * backoff; newVersionCounter(); tau = TAU_INIT; return SUCCESS; } else { dbg(DBG_USR3, "MVirus: routed capsule installation rejected\n"); } } else { dbg(DBG_USR3, "%i: MVirus: Received capsule %i (idx %i), version %i, no such capsule or already have it.\n", (int)capsule->type, (int)idx, (int)capsule->version); } return FAIL; } result_t sendCapsule(uint8_t idx) { MateCapsule* capsule = capsules[idx]; MateCapsuleMsg* msg = (MateCapsuleMsg*)sendPtr->data; if (sendBusy) {return FAIL;} else { // Fill in random bits to packet sendBusy = TRUE; // Fill in capsule nmemcpy(&msg->capsule, capsule, sizeof(MateCapsule)); if (!call CapsuleSend.send(TOS_BCAST_ADDR, sizeof(MateCapsuleMsg), sendPtr)) { sendBusy = FALSE;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?