X-Git-Url: https://oss.titaniummirror.com/gitweb/?a=blobdiff_plain;f=tos%2Flib%2Fnet%2FDeluge%2FDelugeP.nc;h=e5dc2efa62722082bf6d277096b649c26e086ec8;hb=e9bfab607e051bae6afb47b44892ce37541d1b44;hp=8adcf1d6b7b749ba98a3db59a3feb3ece4c544d5;hpb=2d422c11d7a829ca46b5440a7ba123ae2a5b94d9;p=tinyos-2.x.git diff --git a/tos/lib/net/Deluge/DelugeP.nc b/tos/lib/net/Deluge/DelugeP.nc index 8adcf1d6..e5dc2efa 100644 --- a/tos/lib/net/Deluge/DelugeP.nc +++ b/tos/lib/net/Deluge/DelugeP.nc @@ -20,149 +20,168 @@ */ /** - * @author Chieh-Jan Mike Liang * @author Razvan Musaloiu-E. + * @author Chieh-Jan Mike Liang */ module DelugeP { uses { + interface Boot; interface Leds; - interface Notify as StorageReadyNotify; - interface DisseminationUpdate; - interface DisseminationValue; - interface StdControl as StdControlDissemination; - interface DelugeMetadata; + interface DisseminationValue; + interface StdControl as DisseminationStdControl; interface ObjectTransfer; - interface NetProg; - interface InternalFlash as IFlash; interface SplitControl as RadioSplitControl; - -#ifdef DELUGE_BASESTATION - interface Notify as DissNotify; - interface Notify as ReprogNotify; -#endif + interface NetProg; + interface StorageMap[uint8_t volumeId]; + interface DelugeMetadata; + interface DelugeVolumeManager; + interface Resource; + } + provides { + event void storageReady(); + command void stop(); } } implementation { - //uint8_t img_num; - - /** - * Starts the radio - */ - event void StorageReadyNotify.notify(uint8_t val) + enum { + S_IDLE, + S_PUB, + S_RECV + }; + + DelugeCmd lastCmd; + uint8_t state = S_IDLE; + + event void storageReady() { call RadioSplitControl.start(); } -#ifdef DELUGE_BASESTATION - /** - * Starts disseminating image information - */ - event void DissNotify.notify(uint8_t new_img_num) + event void Boot.booted() { - DelugeImgDesc* imgDesc = call DelugeMetadata.getImgDesc(new_img_num); - if (imgDesc->uid != DELUGE_INVALID_UID) { - DelugeDissemination delugeDis; - - call ObjectTransfer.stop(); - call Leds.led0Toggle(); - - delugeDis.uid = imgDesc->uid; - delugeDis.vNum = imgDesc->vNum; - delugeDis.imgNum = new_img_num; - delugeDis.size = imgDesc->size; - delugeDis.msg_type = DISSMSG_DISS; - - call DisseminationUpdate.change(&delugeDis); // Disseminates command - call ObjectTransfer.publish(delugeDis.uid, - delugeDis.size, - delugeDis.imgNum); // Prepares to publish image data + lastCmd.uidhash = DELUGE_INVALID_UID; + } + + event void RadioSplitControl.startDone(error_t error) + { + if (error == SUCCESS) { + call DisseminationStdControl.start(); } } + + command void stop() + { + call Resource.release(); + if (state == S_RECV) { +// printf("erase %d\n", lastCmd.imgNum); + call DelugeVolumeManager.erase(lastCmd.imgNum); + } + call ObjectTransfer.stop(); + state = S_IDLE; + } + + task void taskRequest() + { + signal Resource.granted(); + } - event void ReprogNotify.notify(uint8_t new_img_num) + void request() { - DelugeDissemination delugeDis; - - call Leds.led2Toggle(); - - delugeDis.uid = 0; - delugeDis.vNum = 0; - delugeDis.imgNum = new_img_num; - delugeDis.size = 0; - delugeDis.msg_type = DISSMSG_REPROG; - - call DisseminationUpdate.change(&delugeDis); // Disseminates command + if (call Resource.isOwner()) { + post taskRequest(); + } else { + call Resource.request(); + } } -#endif - /** - * Receives a disseminated message. If the message contains information about a - * newer image, then we should grab this image from the network - */ event void DisseminationValue.changed() { - const DelugeDissemination *delugeDis = call DisseminationValue.get(); - DelugeImgDesc *imgDesc = call DelugeMetadata.getImgDesc(delugeDis->imgNum); - - switch (delugeDis->msg_type) { - case DISSMSG_DISS: - if (imgDesc->uid == delugeDis->uid && imgDesc->uid != DELUGE_INVALID_UID) { - if (imgDesc->vNum < delugeDis->vNum) { - call ObjectTransfer.receive(delugeDis->uid, delugeDis->size, delugeDis->imgNum); - } - } else { - call ObjectTransfer.receive(delugeDis->uid, delugeDis->size, delugeDis->imgNum); - } - - break; - case DISSMSG_REPROG: - if (imgDesc->uid != DELUGE_INVALID_UID) { - DelugeNodeDesc nodeDesc; - call IFlash.read((uint8_t*)IFLASH_NODE_DESC_ADDR, - &nodeDesc, - sizeof(DelugeNodeDesc)); // Reads which image was just reprogrammed - if (nodeDesc.uid != imgDesc->uid || nodeDesc.vNum != imgDesc->vNum) { - call NetProg.programImgAndReboot(delugeDis->imgNum); - } - } - - break; + const DelugeCmd *cmd = call DisseminationValue.get(); +// printf("cmd: %d uidhash: 0x%lx imgNum: %d size: %u\n", cmd->type, cmd->uidhash, cmd->imgNum, cmd->size); + switch (cmd->type) { + case DELUGE_CMD_STOP: + call stop(); + break; + case DELUGE_CMD_ONLY_DISSEMINATE: + case DELUGE_CMD_DISSEMINATE_AND_REPROGRAM: + if (state == S_RECV) { + if (cmd->uidhash == lastCmd.uidhash) { + if (cmd->imgNum == lastCmd.imgNum) { + // Same uidhash, same imgNum, only cmd should be + // different. That will be properly updated by the last + // statement from this function. + break; + } + } + call stop(); + } + if (cmd->uidhash != IDENT_UIDHASH) { + call DelugeMetadata.read(cmd->imgNum); + } else { + state = S_PUB; + request(); + } + break; } + lastCmd = *cmd; +// printf("lastCmd: %d uidhash: 0x%lx\n", lastCmd.type, lastCmd.uidhash); } event void ObjectTransfer.receiveDone(error_t error) { - //call ObjectTransfer.publish(imgDesc->uid, imgDesc->size, imgDesc->imgNum); - call ObjectTransfer.stop(); -// if (error == SUCCESS) { -// call NetProg.programImgAndReboot(img_num); -// } + call Leds.set(LEDS_LED1 | LEDS_LED2); + state = S_IDLE; + + if (error == SUCCESS) { + switch (lastCmd.type) { + case DELUGE_CMD_ONLY_DISSEMINATE: + state = S_PUB; + request(); + break; + case DELUGE_CMD_DISSEMINATE_AND_REPROGRAM: + call NetProg.programImageAndReboot(call StorageMap.getPhysicalAddress[lastCmd.imgNum](0)); + break; + } + } else { + call DelugeVolumeManager.erase(lastCmd.imgNum); + } } - /** - * Prepares to publish the image that was just reprogrammed - */ - event void RadioSplitControl.startDone(error_t error) + event void DelugeMetadata.readDone(uint8_t imgNum, DelugeIdent* ident, error_t error) { - if (error == SUCCESS) { -// // Start publishing the current image -// DelugeImgDesc *imgDesc; -// DelugeNodeDesc nodeDesc; -// call IFlash.read((uint8_t*)IFLASH_NODE_DESC_ADDR, -// &nodeDesc, -// sizeof(DelugeNodeDesc)); // Reads which image was just reprogrammed -// imgDesc = call DelugeMetadata.getImgDesc(nodeDesc.imgNum); -// if (nodeDesc.uid == imgDesc->uid && imgDesc->uid != DELUGE_INVALID_UID) { -// call ObjectTransfer.publish(imgDesc->uid, imgDesc->size, imgDesc->imgNum); -// } - - call StdControlDissemination.start(); +// printf("readDone 0x%lx imgNum: %d size: %lu\n", lastCmd.uidhash, lastCmd.imgNum, lastCmd.size); + if (ident->uidhash == lastCmd.uidhash) { + if (lastCmd.type == DELUGE_CMD_DISSEMINATE_AND_REPROGRAM) { + call NetProg.programImageAndReboot(call StorageMap.getPhysicalAddress[imgNum](0)); + } else { + // We already have the image so we'll go ahead and start publishing. + state = S_PUB; + request(); + } + } else { + state = S_RECV; + request(); } } - + + event void Resource.granted() + { + switch (state) { + case S_PUB: +// printf("start pub 0x%lx imgNum: %d size: %u\n", lastCmd.uidhash, lastCmd.imgNum, lastCmd.size); + call ObjectTransfer.publish(lastCmd.uidhash, lastCmd.size, lastCmd.imgNum); + break; + case S_RECV: + call ObjectTransfer.receive(lastCmd.uidhash, lastCmd.size, lastCmd.imgNum); + break; + } + } + + event void DelugeVolumeManager.eraseDone(uint8_t imgNum) {} event void RadioSplitControl.stopDone(error_t error) {} + default async void command Leds.set(uint8_t val) {} }