Show SwMote.c syntax highlighted
/*
*
* Copyright (c) 2003 The Regents of the University of California. 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.
*
* - Neither the name of the University 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 REGENTS 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 REGENTS OR
* 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.
*
*/
/*
* Copyright (c) 2001 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgement:
* This product includes software developed by Networked &
* Embedded Systems Lab at UCLA
* 4. Neither the name of the University nor that of the Laboratory
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS
* OR 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.
*
* Author: Simon Han (simonhan@ee.ucla.edu)
*/
#include "sensorware.h"
void *swMoteThread(void *arg);
/*
int swMotePs(Tcl_Interp *interp, int argc, char *argv[]);
int swMoteLs(Tcl_Interp *interp, int argc, char *argv[]);
int swMoteInterest(Tcl_Interp *interp, int argc, char *argv[],
void **dataPtr);
int swMoteActivate(SwInterest *interest);
int swMoteDeactivate(SwInterest *interest);
int swMoteDispose(void *param);
*/
int swMoteAct(Tcl_Interp *interp, int argc, char *argv[]);
int swMoteQuery(Tcl_Interp *interp, int argc, char *argv[]);
static int fd; // serial file descriptor
// register magnetic sensor device
static SwDeviceOp op = {
/*
interest: swMoteInterest,
ps: swMotePs,
ls: swMoteLs,
activate: swMoteActivate,
deactivate: swMoteDeactivate,
dispose: swMoteDispose,
*/
thread: swMoteThread,
act: swMoteAct,
query: swMoteQuery,
};
//static swDiffQ dq;
/*
void *swMoteRead(void *arg){
while(1){
output_sensor(state.fd, "*00P ", 4);
usleep(20000);
read_data_b(state.fd, &state);
}
}
*/
int swMoteInit(){
if((fd = openSerial("/dev/ttySA0", CS8, B19200)) < 0){
return -ENXIO;
}
// change to blocking mode
fcntl(fd, F_SETFL, O_RDWR | O_NOCTTY);
swDeviceRegister("mote", &op);
return 0;
}
int swMoteAct(Tcl_Interp *interp, int argc, char *argv[]){
// send packet
char buf[10] = "t";
//char buf[10];
//write(fd, "this is act", 12);
write(fd, buf, 1);
return TCL_OK;
}
int swMoteQuery(Tcl_Interp *interp, int argc, char *argv[]){
return TCL_OK;
}
void *swMoteThread(void *arg){
char buf[100];
printf("mote thread init\n");
while(1){
int size;
int i = 0;
size = read(fd, buf, 100);
buf[size] = 0;
/*
for(i = 0; i < size; i++){
printf("***%c***\n", buf[i]);
}
*/
if(size > 0){
printf(buf);
}
else{
perror("mote");
}
}
}
/*
// thread responsible for reading sensor data
// based on agent's configuration
void *swMoteThread(void *arg){
char *buf;
swDiffQInit(&dq);
while(1){
SwInterest *interest;
int size;
interest = (SwInterest*)swDiffQWait(&dq);
if(interest != NULL){
size = strlen(interest->namePtr) + 30;
buf = (char *)ckalloc(size);
if(buf != NULL){
sprintf(buf, "%s %hd %hd %hd", interest->namePtr,
state.x, state.y, state.z);
if(swInterestSendMail(interest, buf, size) < 0){
ckfree(buf);
}
}
}
}
pthread_exit(NULL);
}
// event interface
int swMoteInterest(Tcl_Interp *interp, int argc, char *argv[],
void **dataPtr){
SwMote *arg;
arg = (SwMote*)ckalloc(sizeof(SwMote));
if(arg == NULL){
Tcl_AppendResult(interp, SW_ERR_MEM, NULL);
return TCL_ERROR;
}
if(swDeviceParseSensorArg(argc, argv,
&(arg->samplePeriod), &(arg->bufSize),
&(arg->wakeupPeriod), &(arg->threshold)) < 0){
Tcl_AppendResult(interp, "magsensor: invalid parameters", NULL);
return TCL_ERROR;
}
*dataPtr = (void*)arg;
return TCL_OK;
}
// device ps interface
int swMotePs(Tcl_Interp *interp, int argc, char *argv[]){
return TCL_OK;
}
int swMoteLs(Tcl_Interp *interp, int argc, char *argv[]){
Tcl_AppendResult(interp, "20\t", NULL);
return TCL_OK;
}
int swMoteActivate(SwInterest *interest){
swDiffQNewItem(&dq,
((SwMote*)interest->devParamPtr)->samplePeriod,
(void*)interest);
return 0;
}
int swMoteDeactivate(SwInterest *interest){
swDiffQDeleteItem(&dq, (void*)interest);
return 0;
}
// device cancel interface
int swMoteDispose(void *param){
ckfree(param);
return 0;
}
*/
See more files for this project here