00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include <pthread.h>
00044
00045
00046
00047
00048 void random_adc_init() {}
00049
00050 uint16_t random_adc_read(int moteID, uint8_t port, long long ftime) {
00051 return (uint16_t)(rand() & 0x3ff);
00052 }
00053
00054 adc_model* create_random_adc_model() {
00055 adc_model* model = (adc_model*)(malloc(sizeof(adc_model)));
00056 model->init = random_adc_init;
00057 model->read = random_adc_read;
00058 return model;
00059 }
00060
00061
00062
00063
00064
00065 enum {
00066 ADC_NUM_PORTS_PER_NODE = 256
00067 };
00068
00069 uint16_t adcValues[TOSNODES][ADC_NUM_PORTS_PER_NODE];
00070 pthread_mutex_t adcValuesLock;
00071
00072 void generic_adc_init() {
00073 int i, j;
00074 for (i = 0; i < TOSNODES; i++) {
00075 for (j = 0; j < ADC_NUM_PORTS_PER_NODE; j++) {
00076 adcValues[i][j] = 0xffff;
00077 }
00078 }
00079 pthread_mutex_init(&adcValuesLock, NULL);
00080
00081 }
00082
00083 uint16_t generic_adc_read(int moteID, uint8_t port, long long ftime) {
00084 uint16_t value;
00085
00086 if ((moteID >= TOSNODES) || (moteID < 0)) {
00087 dbg(DBG_ERROR, "GENERIC_ADC_MODEL: trying to read value with invalid parameters: [moteID = %d] [port = %d]", moteID, port);
00088 return -1;
00089 }
00090 pthread_mutex_lock(&adcValuesLock);
00091 value = adcValues[moteID][(int)port];
00092 pthread_mutex_unlock(&adcValuesLock);
00093 if (value == 0xffff)
00094 return (short)(rand() & 0x3ff);
00095 else
00096 return value;
00097 }
00098
00099 adc_model* create_generic_adc_model() {
00100 adc_model* model = (adc_model*)(malloc(sizeof(adc_model)));
00101 model->init = generic_adc_init;
00102 model->read = generic_adc_read;
00103 return model;
00104 }
00105
00106 void set_adc_value(int moteID, uint8_t port, uint16_t value) {
00107 if ((moteID >= TOSNODES) || (moteID < 0)) {
00108 dbg(DBG_ERROR, "GENERIC_ADC_MODEL: trying to set value with invalid parameters: [moteID = %d] [port = %d]", moteID, port);
00109 return;
00110 }
00111 pthread_mutex_lock(&adcValuesLock);
00112 adcValues[moteID][(int)port] = value;
00113 pthread_mutex_unlock(&adcValuesLock);
00114 }
00115
00116
00117
00118
00119
00120 #include "external_adc.h"
00121
00122 uint16_t external_adc_connected;
00123 int external_adc_socket;
00124
00125 void external_adc_init() {
00126 external_adc_connected = 0;
00127 external_adc_socket = -1;
00128 }
00129
00130 int16_t external_adc_connect() {
00131 struct hostent *he;
00132 struct sockaddr_in their_addr;
00133
00134
00135 if (external_adc_socket > 0) {
00136 close(external_adc_socket);
00137 external_adc_socket = -1;
00138 }
00139
00140 if (external_adc_host == NULL)
00141 external_adc_host = "localhost";
00142
00143 if ((he = gethostbyname(external_adc_host)) == NULL) {
00144 perror("gethostbyname");
00145 return -1;
00146 }
00147 if ((external_adc_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
00148 perror("socket");
00149 return -1;
00150 }
00151
00152 memset(&their_addr, 0, sizeof(struct sockaddr_in));
00153 their_addr.sin_family = AF_INET;
00154 their_addr.sin_port = htons(EXTERNAL_ADC_PORT);
00155 their_addr.sin_addr = *((struct in_addr *)he->h_addr);
00156
00157 if (connect(external_adc_socket, (struct sockaddr *)&their_addr,
00158 sizeof(struct sockaddr)) < 0) {
00159 perror("connect");
00160 return -1;
00161 }
00162
00163
00164
00165
00166
00167
00168
00169
00170 external_adc_connected = 1;
00171 return 0;
00172 }
00173
00174 uint16_t external_adc_read(int moteID, uint8_t port, long long ftime) {
00175
00176 uint16_t err_val = 0x0000;
00177 int numbytes;
00178 struct external_adc x_adc;
00179
00180 memset(&x_adc, 0, sizeof(struct external_adc));
00181 if (external_adc_connected == 0) {
00182 if (external_adc_connect() < 0)
00183 return err_val;
00184 }
00185 x_adc.moteid = moteID;
00186 x_adc.port = port;
00187 x_adc.time = ftime;
00188 if (send(external_adc_socket, &x_adc, sizeof(struct external_adc), 0) < 0) {
00189 perror("send");
00190 if (errno == EPIPE)
00191 external_adc_connected = 0;
00192 return err_val;
00193 }
00194 if ((numbytes = recv(external_adc_socket, &x_adc,
00195 sizeof(struct external_adc), 0)) < 0) {
00196 perror("recv");
00197 return err_val;
00198 }
00199 if (numbytes == 0) {
00200 external_adc_connected = 0;
00201 return err_val;
00202 }
00203 if (x_adc.error < 0) {
00204 return err_val;
00205 }
00206 return x_adc.value;
00207 }
00208
00209 adc_model* create_external_adc_model() {
00210 adc_model* model = (adc_model*)(malloc(sizeof(adc_model)));
00211 model->init = external_adc_init;
00212 model->read = external_adc_read;
00213 return model;
00214 }