/* * printByNameOrNumber(sensor); * * Look up the sensor that has the name or the ID specified in the * string 'sensor' and print it to OUT_FH. Also print its * class(es) if requested. */ static void printByNameOrNumber( const char *sensor) { char sensor_name[SK_MAX_STRLEN_SENSOR+1]; int rv; uint32_t temp; sensorID_t sid; int count; sensor_iter_t si; /* try to parse as a number */ rv = skStringParseUint32(&temp, sensor, 0, SK_INVALID_SENSOR-1); if (rv < 0 && rv != SKUTILS_ERR_BAD_CHAR) { skAppPrintErr("Invalid Sensor Number '%s': %s", sensor, skStringParseStrerror(rv)); return; } if (rv == 0) { /* got a clean parse */ sid = (sensorID_t)temp; if ( !sksiteSensorExists(sid) ) { skAppPrintErr("Number '%s' is not a valid sensor number", sensor); return; } printSensor(sid, MAP_NUM_TO_NAME); return; } /* didn't get a clean parse. try to treat as a name */ sid = sksiteSensorLookup(sensor); if (sid != SK_INVALID_SENSOR) { printSensor(sid, MAP_NAME_TO_NUM); return; } /* try a case-insensitive search, manually iterating over all * the sensors */ count = 0; sksiteSensorIterator(&si); while (sksiteSensorIteratorNext(&si, &sid)) { sksiteSensorGetName(sensor_name, sizeof(sensor_name), sid); if (0 == strcasecmp(sensor_name, sensor)) { printSensor(sid, MAP_NAME_TO_NUM); ++count; } } if (count == 0) { skAppPrintErr("Name '%s' is not a valid sensor name", sensor); } }
void Arduilink::printSensors() { SensorItem* sensor = head; while (sensor != NULL) { printSensor(sensor, nodeId); sensor = sensor->next; } // Add an empty line to indicate the end of list Serial.println(); Serial.flush(); }
/* * printAllSensors(); * * Print all sensor IDs and Names to the OUT_FH. Also, print class * list if requested. */ static void printAllSensors( void) { sensor_iter_t si; sensorID_t sid; int sensor_count = 0; sksiteSensorIterator(&si); while ( sksiteSensorIteratorNext(&si, &sid) ) { printSensor(sid, MAP_NUM_TO_NAME); sensor_count++; } fprintf(OUT_FH, "Total sensors %d\n", sensor_count); }
int Arduilink::handleInput() { while (Serial.available() > 0) { while (write) {} write = true; String str = Serial.readString(); char buf[str.length() + 1]; str.toCharArray(buf, str.length() + 1); char* opcode = NULL; int node = -1; int sensorId = -1; char* pch; pch = strtok(buf, ";"); while (pch != NULL) { if (opcode == NULL) { if (strcmp(pch, "PRESENT") == 0) { printSensors(); write = false; return 0; } if (strcmp(pch, "GET") != 0 && strcmp(pch, "INFO") != 0 && strcmp(pch, "SET") != 0) { Serial.print("400;OPCODE;"); Serial.println(pch); write = false; return 2; } opcode = pch; } else if (node == -1) { node = atoi(pch); } else if (sensorId == -1) { sensorId = atoi(pch); break; } pch = strtok(NULL, ";"); } /*Serial.print("Opcode: "); Serial.print(opcode); Serial.print(" NodeId: "); Serial.print(node); Serial.print(" SensorId: "); Serial.println(sensorId);*/ if (nodeId != node) { Serial.print("404;NODE;"); Serial.println(node); write = false; return 3; } SensorItem* sensor = getSensor(sensorId); if (sensor == NULL) { Serial.print("404;SENSOR;"); Serial.println(sensorId); write = false; return 4; } // INFO - Récupérer la description d'un capteur if (strcmp(opcode, "INFO") == 0) { printSensor(sensor, node); } // GET - Récupérer la valeur d'un capteur if (strcmp(opcode, "GET") == 0) { char buff[256]; sprintf(buff, "200;%d;%d;", node, sensor->id); Serial.print(buff); Serial.println(sensor->value); } // SET - Modifier un attribut d'un capteur else if (strcmp(opcode, "SET") == 0) { // Choose attribute mode pch = strtok(NULL, ";"); int mode = 0; if (strcmp(pch, "VERBOSE") == 0) mode = 1; else if (strcmp(pch, "VAL") == 0) mode = 2; else { Serial.print("400;ATTR;"); Serial.println(pch); write = false; return 5; } // Ack pch = strtok(NULL, ";"); bool ack = strcmp(pch, "1") == 0; // Value pch = strtok(NULL, ";"); // Handle Set Verbose if (mode == 1) { if (strcmp(pch, "1") == 0) { if (ack) { Serial.println("201;VERBOSE;1"); Serial.flush(); } sensor->verbose = true; } else if (strcmp(pch, "0") == 0) { sensor->verbose = false; if (ack) { Serial.println("201;VERBOSE;0"); // TODO Ameliorer avec ids Serial.flush(); } } else { Serial.print("400;OPT;VERBOSE;"); // TODO Ameliorer avec ids Serial.println(pch); write = false; return 6; } } // Handle Set Value else if (mode == 2) { sensor->value = pch; if (sensor->writter != NULL) sensor->writter(pch); if (ack) { Serial.print("201;SET;"); Serial.print(node); Serial.print(";"); Serial.print(sensor->id); Serial.print(";VAL;"); Serial.println(pch); } } } Serial.flush(); write = false; return 0; } return 1; }
void SensorServer() { SensorRequest_t req, reply; int tid, callee, status, calleeByte; unsigned int i, timeout; unsigned int bytes, notifier, *sensors; unsigned int maxId = TRAIN_SENSOR_COUNT * TRAIN_MODULE_COUNT; SensorQueue_t sensorQueue[TRAIN_SENSOR_COUNT * TRAIN_MODULE_COUNT] = {{0}}; CircularBuffer_int waitAnyQueue; volatile uint32_t lastPoll[TRAIN_SENSOR_COUNT * TRAIN_MODULE_COUNT] = {0}; (void)reply; /* TODO: May use this in future */ initcb_int(&waitAnyQueue); RegisterAs(SENSOR_SERVER); sensor_server_tid = MyTid(); notifier = Create(13, SensorSlave); for (i = 0; i < TRAIN_SENSOR_COUNT * TRAIN_MODULE_COUNT; ++i) { sensorQueue[i].tid = -1; sensorQueue[i].timeout = 0; } while (true) { bytes = Receive(&callee, &req, sizeof(req)); if (bytes < 0) { error("SensorServer: Failed to receive message from Task %d with status %d", callee, bytes); } timeout = 0; switch (req.type) { case SENSOR_LAST_POLL: for (i = 0; i < TRAIN_SENSOR_COUNT * TRAIN_MODULE_COUNT; ++i) { *((uint32_t*)req.sensor + i) = lastPoll[i]; } status = 1; Reply(callee, &status, sizeof(status)); break; case SENSOR_WAIT_ANY: calleeByte = (int)callee; write_int(&waitAnyQueue, &calleeByte, 1); break; case SENSOR_WAIT_TIMEOUT: timeout = req.timeout; case SENSOR_WAIT: if (req.sensor < maxId && sensorQueue[req.sensor].tid < 0) { status = 0; sensorQueue[req.sensor].tid = callee; sensorQueue[req.sensor].timeout = timeout; } else { status = OTHER_TASK_WAITING_ON_SENSOR; Reply(callee, &status, sizeof(status)); } break; case SENSOR_FREE: Reply(callee, &status, sizeof(status)); if (req.sensor > maxId || sensorQueue[req.sensor].tid < 0) { status = INVALID_SENSOR; break; } status = TIMER_TRIP; Reply(sensorQueue[req.sensor].tid, &status, sizeof(status)); sensorQueue[req.sensor].tid = -1; break; case SENSOR_RETURNED: if (callee == notifier) { int tripped_sensor = -1; sensors = (uint32_t*)req.sensor; for (i = 0; i < maxId; ++i) { bool tripped = false; tid = -1; if (sensors[i] && !lastPoll[i]) { Log("TRUE TRIP - %c%d", (i / TRAIN_SENSOR_COUNT) + 'A', (i % TRAIN_SENSOR_COUNT) + 1); printSensor((i / TRAIN_SENSOR_COUNT) + 'A', // sensor module index (i % TRAIN_SENSOR_COUNT) + 1); // index in module tid = sensorQueue[i].tid; sensorQueue[i].tid = -1; status = SENSOR_TRIP; tripped = true; } if (tid >= 0) { Reply(tid, &status, sizeof(status)); } else if (tripped_sensor == -1 && tripped) { /* assign the sensor to the one not being waited on */ tripped_sensor = i; } lastPoll[i] = sensors[i]; } if ((status = tripped_sensor) >= 0) { while (length_int(&waitAnyQueue) > 0) { read_int(&waitAnyQueue, &calleeByte, 1); Reply(calleeByte, &status, sizeof(status)); } } status = 1; } else { status = -1; } Reply(callee, &status, sizeof(status)); break; } } Exit(); }